sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:intersect_range_array; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:bed1; 5, identifier:beds2; 6, default_parameter; 6, 7; 6, 8; 7, identifier:payload; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:is_sorted; 11, False; 12, block; 12, 13; 12, 15; 12, 26; 12, 30; 12, 108; 12, 116; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 18; 16, not_operator; 16, 17; 17, identifier:is_sorted; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:beds2; 22, call; 22, 23; 22, 24; 23, identifier:sort_ranges; 24, argument_list; 24, 25; 25, identifier:beds2; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:output; 29, list:[]; 30, for_statement; 30, 31; 30, 32; 30, 33; 31, identifier:bed2; 32, identifier:beds2; 33, block; 33, 34; 33, 43; 33, 44; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:cval; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:bed2; 40, identifier:cmp; 41, argument_list; 41, 42; 42, identifier:bed1; 43, comment; 44, if_statement; 44, 45; 44, 49; 44, 51; 44, 102; 45, comparison_operator:==; 45, 46; 45, 47; 46, identifier:cval; 47, unary_operator:-; 47, 48; 48, integer:1; 49, block; 49, 50; 50, continue_statement; 51, elif_clause; 51, 52; 51, 55; 52, comparison_operator:==; 52, 53; 52, 54; 53, identifier:cval; 54, integer:0; 55, block; 55, 56; 55, 68; 55, 85; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:output; 60, identifier:append; 61, argument_list; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:bed1; 65, identifier:intersect; 66, argument_list; 66, 67; 67, identifier:bed2; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:==; 69, 70; 69, 71; 70, identifier:payload; 71, integer:1; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 81; 75, attribute; 75, 76; 75, 80; 76, subscript; 76, 77; 76, 78; 77, identifier:output; 78, unary_operator:-; 78, 79; 79, integer:1; 80, identifier:set_payload; 81, argument_list; 81, 82; 82, attribute; 82, 83; 82, 84; 83, identifier:bed1; 84, identifier:payload; 85, if_statement; 85, 86; 85, 89; 86, comparison_operator:==; 86, 87; 86, 88; 87, identifier:payload; 88, integer:2; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 98; 92, attribute; 92, 93; 92, 97; 93, subscript; 93, 94; 93, 95; 94, identifier:output; 95, unary_operator:-; 95, 96; 96, integer:1; 97, identifier:set_payload; 98, argument_list; 98, 99; 99, attribute; 99, 100; 99, 101; 100, identifier:bed2; 101, identifier:payload; 102, elif_clause; 102, 103; 102, 106; 103, comparison_operator:==; 103, 104; 103, 105; 104, identifier:cval; 105, integer:1; 106, block; 106, 107; 107, break_statement; 108, if_statement; 108, 109; 108, 110; 109, identifier:payload; 110, block; 110, 111; 111, return_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:sort_ranges; 114, argument_list; 114, 115; 115, identifier:output; 116, return_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:merge_ranges; 119, argument_list; 119, 120; 120, identifier:output
def intersect_range_array(bed1,beds2,payload=None,is_sorted=False): """ Does not do a merge if the payload has been set :param bed1: :param bed2: :param payload: payload=1 return the payload of bed1 on each of the intersect set, payload=2 return the payload of bed2 on each of the union set, payload=3 return the payload of bed1 and bed2 on each of the union set :param is_sorted: :type bed1: GenomicRange :type bed2: GenomicRange :type payload: int :type is_sorted: bool """ if not is_sorted: beds2 = sort_ranges(beds2) output = [] for bed2 in beds2: cval = bed2.cmp(bed1) #print str(cval)+" "+bed1.get_range_string()+" "+bed2.get_range_string() if cval == -1: continue elif cval == 0: output.append(bed1.intersect(bed2)) if payload==1: output[-1].set_payload(bed1.payload) if payload==2: output[-1].set_payload(bed2.payload) elif cval == 1: break if payload: return sort_ranges(output) return merge_ranges(output)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_genomic_ranges; 3, parameters; 3, 4; 4, identifier:rngs; 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; 11, 13; 12, identifier:rngs; 13, keyword_argument; 13, 14; 13, 15; 14, identifier:key; 15, lambda; 15, 16; 15, 18; 16, lambda_parameters; 16, 17; 17, identifier:x; 18, tuple; 18, 19; 18, 22; 18, 25; 19, attribute; 19, 20; 19, 21; 20, identifier:x; 21, identifier:chr; 22, attribute; 22, 23; 22, 24; 23, identifier:x; 24, identifier:start; 25, attribute; 25, 26; 25, 27; 26, identifier:x; 27, identifier:end
def sort_genomic_ranges(rngs): """sort multiple ranges""" return sorted(rngs, key=lambda x: (x.chr, x.start, x.end))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:topological_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:dependencies; 5, identifier:start_nodes; 6, block; 6, 7; 6, 9; 6, 13; 6, 24; 6, 35; 6, 53; 6, 97; 6, 119; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:retval; 12, list:[]; 13, function_definition; 13, 14; 13, 15; 13, 17; 14, function_name:edges; 15, parameters; 15, 16; 16, identifier:node; 17, block; 17, 18; 18, return_statement; 18, 19; 19, subscript; 19, 20; 19, 23; 20, subscript; 20, 21; 20, 22; 21, identifier:dependencies; 22, identifier:node; 23, integer:1; 24, function_definition; 24, 25; 24, 26; 24, 28; 25, function_name:in_degree; 26, parameters; 26, 27; 27, identifier:node; 28, block; 28, 29; 29, return_statement; 29, 30; 30, subscript; 30, 31; 30, 34; 31, subscript; 31, 32; 31, 33; 32, identifier:dependencies; 33, identifier:node; 34, integer:0; 35, function_definition; 35, 36; 35, 37; 35, 39; 36, function_name:remove_incoming; 37, parameters; 37, 38; 38, identifier:node; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 47; 42, subscript; 42, 43; 42, 46; 43, subscript; 43, 44; 43, 45; 44, identifier:dependencies; 45, identifier:node; 46, integer:0; 47, binary_operator:-; 47, 48; 47, 52; 48, call; 48, 49; 48, 50; 49, identifier:in_degree; 50, argument_list; 50, 51; 51, identifier:node; 52, integer:1; 53, while_statement; 53, 54; 53, 55; 54, identifier:start_nodes; 55, block; 55, 56; 55, 64; 55, 71; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:node; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:start_nodes; 62, identifier:pop; 63, argument_list; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:retval; 68, identifier:append; 69, argument_list; 69, 70; 70, identifier:node; 71, for_statement; 71, 72; 71, 73; 71, 77; 72, identifier:child; 73, call; 73, 74; 73, 75; 74, identifier:edges; 75, argument_list; 75, 76; 76, identifier:node; 77, block; 77, 78; 77, 83; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:remove_incoming; 81, argument_list; 81, 82; 82, identifier:child; 83, if_statement; 83, 84; 83, 89; 84, not_operator; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:in_degree; 87, argument_list; 87, 88; 88, identifier:child; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:start_nodes; 94, identifier:append; 95, argument_list; 95, 96; 96, identifier:child; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:leftover_nodes; 100, list_comprehension; 100, 101; 100, 102; 100, 112; 101, identifier:node; 102, for_in_clause; 102, 103; 102, 104; 103, identifier:node; 104, call; 104, 105; 104, 106; 105, identifier:list; 106, argument_list; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:dependencies; 110, identifier:keys; 111, argument_list; 112, if_clause; 112, 113; 113, comparison_operator:>; 113, 114; 113, 118; 114, call; 114, 115; 114, 116; 115, identifier:in_degree; 116, argument_list; 116, 117; 117, identifier:node; 118, integer:0; 119, if_statement; 119, 120; 119, 121; 119, 127; 120, identifier:leftover_nodes; 121, block; 121, 122; 122, raise_statement; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:CyclicDependency; 125, argument_list; 125, 126; 126, identifier:leftover_nodes; 127, else_clause; 127, 128; 128, block; 128, 129; 129, return_statement; 129, 130; 130, identifier:retval
def topological_sort(dependencies, start_nodes): """ Perform a topological sort on the dependency graph `dependencies`, starting from list `start_nodes`. """ retval = [] def edges(node): return dependencies[node][1] def in_degree(node): return dependencies[node][0] def remove_incoming(node): dependencies[node][0] = in_degree(node) - 1 while start_nodes: node = start_nodes.pop() retval.append(node) for child in edges(node): remove_incoming(child) if not in_degree(child): start_nodes.append(child) leftover_nodes = [node for node in list(dependencies.keys()) if in_degree(node) > 0] if leftover_nodes: raise CyclicDependency(leftover_nodes) else: return retval
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_do_parse; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:inp; 5, identifier:fmt; 6, identifier:encoding; 7, identifier:force_types; 8, block; 8, 9; 8, 11; 8, 15; 8, 21; 8, 196; 8, 197; 8, 198; 8, 199; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:res; 14, dictionary; 15, expression_statement; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:_check_lib_installed; 18, argument_list; 18, 19; 18, 20; 19, identifier:fmt; 20, string:'parse'; 21, if_statement; 21, 22; 21, 25; 21, 46; 21, 81; 21, 115; 21, 159; 21, 176; 21, 192; 22, comparison_operator:==; 22, 23; 22, 24; 23, identifier:fmt; 24, string:'ini'; 25, block; 25, 26; 25, 38; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:cfg; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:configobj; 32, identifier:ConfigObj; 33, argument_list; 33, 34; 33, 35; 34, identifier:inp; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:encoding; 37, identifier:encoding; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:res; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:cfg; 44, identifier:dict; 45, argument_list; 46, elif_clause; 46, 47; 46, 50; 47, comparison_operator:==; 47, 48; 47, 49; 48, identifier:fmt; 49, string:'json'; 50, block; 50, 51; 50, 69; 51, if_statement; 51, 52; 51, 55; 51, 56; 52, attribute; 52, 53; 52, 54; 53, identifier:six; 54, identifier:PY3; 55, comment; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:inp; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:io; 63, identifier:TextIOWrapper; 64, argument_list; 64, 65; 64, 66; 65, identifier:inp; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:encoding; 68, identifier:encoding; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:res; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:json; 75, identifier:load; 76, argument_list; 76, 77; 76, 78; 77, identifier:inp; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:encoding; 80, identifier:encoding; 81, elif_clause; 81, 82; 81, 85; 82, comparison_operator:==; 82, 83; 82, 84; 83, identifier:fmt; 84, string:'json5'; 85, block; 85, 86; 85, 103; 86, if_statement; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:six; 89, identifier:PY3; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:inp; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:io; 97, identifier:TextIOWrapper; 98, argument_list; 98, 99; 98, 100; 99, identifier:inp; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:encoding; 102, identifier:encoding; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:res; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:json5; 109, identifier:load; 110, argument_list; 110, 111; 110, 112; 111, identifier:inp; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:encoding; 114, identifier:encoding; 115, elif_clause; 115, 116; 115, 119; 116, comparison_operator:==; 116, 117; 116, 118; 117, identifier:fmt; 118, string:'toml'; 119, block; 119, 120; 119, 132; 119, 150; 120, if_statement; 120, 121; 120, 126; 121, not_operator; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:_is_utf8; 124, argument_list; 124, 125; 125, identifier:encoding; 126, block; 126, 127; 127, raise_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:AnyMarkupError; 130, argument_list; 130, 131; 131, string:'toml is always utf-8 encoded according to specification'; 132, if_statement; 132, 133; 132, 136; 132, 137; 133, attribute; 133, 134; 133, 135; 134, identifier:six; 135, identifier:PY3; 136, comment; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:inp; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:io; 144, identifier:TextIOWrapper; 145, argument_list; 145, 146; 145, 147; 146, identifier:inp; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:encoding; 149, identifier:encoding; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:res; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:toml; 156, identifier:load; 157, argument_list; 157, 158; 158, identifier:inp; 159, elif_clause; 159, 160; 159, 163; 160, comparison_operator:==; 160, 161; 160, 162; 161, identifier:fmt; 162, string:'xml'; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:res; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:xmltodict; 170, identifier:parse; 171, argument_list; 171, 172; 171, 173; 172, identifier:inp; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:encoding; 175, identifier:encoding; 176, elif_clause; 176, 177; 176, 180; 176, 181; 176, 182; 177, comparison_operator:==; 177, 178; 177, 179; 178, identifier:fmt; 179, string:'yaml'; 180, comment; 181, comment; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:res; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:yaml; 189, identifier:safe_load; 190, argument_list; 190, 191; 191, identifier:inp; 192, else_clause; 192, 193; 193, block; 193, 194; 193, 195; 194, raise_statement; 195, comment; 196, comment; 197, comment; 198, comment; 199, return_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:_ensure_proper_types; 202, argument_list; 202, 203; 202, 204; 202, 205; 203, identifier:res; 204, identifier:encoding; 205, identifier:force_types
def _do_parse(inp, fmt, encoding, force_types): """Actually parse input. Args: inp: bytes yielding file-like object fmt: format to use for parsing encoding: encoding of `inp` force_types: if `True`, integers, floats, booleans and none/null are recognized and returned as proper types instead of strings; if `False`, everything is converted to strings if `None`, backend return value is used Returns: parsed `inp` (dict or list) containing unicode values Raises: various sorts of errors raised by used libraries while parsing """ res = {} _check_lib_installed(fmt, 'parse') if fmt == 'ini': cfg = configobj.ConfigObj(inp, encoding=encoding) res = cfg.dict() elif fmt == 'json': if six.PY3: # python 3 json only reads from unicode objects inp = io.TextIOWrapper(inp, encoding=encoding) res = json.load(inp, encoding=encoding) elif fmt == 'json5': if six.PY3: inp = io.TextIOWrapper(inp, encoding=encoding) res = json5.load(inp, encoding=encoding) elif fmt == 'toml': if not _is_utf8(encoding): raise AnyMarkupError('toml is always utf-8 encoded according to specification') if six.PY3: # python 3 toml prefers unicode objects inp = io.TextIOWrapper(inp, encoding=encoding) res = toml.load(inp) elif fmt == 'xml': res = xmltodict.parse(inp, encoding=encoding) elif fmt == 'yaml': # guesses encoding by its own, there seems to be no way to pass # it explicitly res = yaml.safe_load(inp) else: raise # unknown format # make sure it's all unicode and all int/float values were parsed correctly # the unicode part is here because of yaml on PY2 and also as workaround for # https://github.com/DiffSK/configobj/issues/18#issuecomment-76391689 return _ensure_proper_types(res, encoding, force_types)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_do_serialize; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:struct; 5, identifier:fmt; 6, identifier:encoding; 7, block; 7, 8; 7, 10; 7, 14; 7, 20; 7, 187; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:res; 13, None; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:_check_lib_installed; 17, argument_list; 17, 18; 17, 19; 18, identifier:fmt; 19, string:'serialize'; 20, if_statement; 20, 21; 20, 24; 20, 65; 20, 106; 20, 137; 20, 163; 20, 183; 21, comparison_operator:==; 21, 22; 21, 23; 22, identifier:fmt; 23, string:'ini'; 24, block; 24, 25; 24, 36; 24, 52; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:config; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:configobj; 31, identifier:ConfigObj; 32, argument_list; 32, 33; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:encoding; 35, identifier:encoding; 36, for_statement; 36, 37; 36, 40; 36, 45; 37, pattern_list; 37, 38; 37, 39; 38, identifier:k; 39, identifier:v; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:struct; 43, identifier:items; 44, argument_list; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:config; 50, identifier:k; 51, identifier:v; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:res; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, string:b'\n'; 58, identifier:join; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:config; 63, identifier:write; 64, argument_list; 65, elif_clause; 65, 66; 65, 71; 65, 72; 65, 73; 65, 74; 66, comparison_operator:in; 66, 67; 66, 68; 67, identifier:fmt; 68, list:['json', 'json5']; 68, 69; 68, 70; 69, string:'json'; 70, string:'json5'; 71, comment; 72, comment; 73, comment; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:res; 78, call; 78, 79; 78, 104; 79, attribute; 79, 80; 79, 103; 80, call; 80, 81; 80, 90; 81, attribute; 81, 82; 81, 89; 82, parenthesized_expression; 82, 83; 83, conditional_expression:if; 83, 84; 83, 85; 83, 88; 84, identifier:json; 85, comparison_operator:==; 85, 86; 85, 87; 86, identifier:fmt; 87, string:'json'; 88, identifier:json5; 89, identifier:dumps; 90, argument_list; 90, 91; 90, 92; 90, 95; 90, 100; 91, identifier:struct; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:indent; 94, integer:2; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:separators; 97, tuple; 97, 98; 97, 99; 98, string:','; 99, string:': '; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:ensure_ascii; 102, False; 103, identifier:encode; 104, argument_list; 104, 105; 105, identifier:encoding; 106, elif_clause; 106, 107; 106, 110; 107, comparison_operator:==; 107, 108; 107, 109; 108, identifier:fmt; 109, string:'toml'; 110, block; 110, 111; 110, 123; 111, if_statement; 111, 112; 111, 117; 112, not_operator; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:_is_utf8; 115, argument_list; 115, 116; 116, identifier:encoding; 117, block; 117, 118; 118, raise_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:AnyMarkupError; 121, argument_list; 121, 122; 122, string:'toml must always be utf-8 encoded according to specification'; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:res; 126, call; 126, 127; 126, 135; 127, attribute; 127, 128; 127, 134; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:toml; 131, identifier:dumps; 132, argument_list; 132, 133; 133, identifier:struct; 134, identifier:encode; 135, argument_list; 135, 136; 136, identifier:encoding; 137, elif_clause; 137, 138; 137, 141; 137, 142; 138, comparison_operator:==; 138, 139; 138, 140; 139, identifier:fmt; 140, string:'xml'; 141, comment; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:res; 146, call; 146, 147; 146, 161; 147, attribute; 147, 148; 147, 160; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:xmltodict; 151, identifier:unparse; 152, argument_list; 152, 153; 152, 154; 152, 157; 153, identifier:struct; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:pretty; 156, True; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:encoding; 159, string:'utf-8'; 160, identifier:encode; 161, argument_list; 161, 162; 162, string:'utf-8'; 163, elif_clause; 163, 164; 163, 167; 164, comparison_operator:==; 164, 165; 164, 166; 165, identifier:fmt; 166, string:'yaml'; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:res; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:yaml; 174, identifier:safe_dump; 175, argument_list; 175, 176; 175, 177; 175, 180; 176, identifier:struct; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:encoding; 179, string:'utf-8'; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:default_flow_style; 182, False; 183, else_clause; 183, 184; 184, block; 184, 185; 184, 186; 185, raise_statement; 186, comment; 187, return_statement; 187, 188; 188, identifier:res
def _do_serialize(struct, fmt, encoding): """Actually serialize input. Args: struct: structure to serialize to fmt: format to serialize to encoding: encoding to use while serializing Returns: encoded serialized structure Raises: various sorts of errors raised by libraries while serializing """ res = None _check_lib_installed(fmt, 'serialize') if fmt == 'ini': config = configobj.ConfigObj(encoding=encoding) for k, v in struct.items(): config[k] = v res = b'\n'.join(config.write()) elif fmt in ['json', 'json5']: # specify separators to get rid of trailing whitespace # specify ensure_ascii to make sure unicode is serialized in \x... sequences, # not in \u sequences res = (json if fmt == 'json' else json5).dumps(struct, indent=2, separators=(',', ': '), ensure_ascii=False).encode(encoding) elif fmt == 'toml': if not _is_utf8(encoding): raise AnyMarkupError('toml must always be utf-8 encoded according to specification') res = toml.dumps(struct).encode(encoding) elif fmt == 'xml': # passing encoding argument doesn't encode, just sets the xml property res = xmltodict.unparse(struct, pretty=True, encoding='utf-8').encode('utf-8') elif fmt == 'yaml': res = yaml.safe_dump(struct, encoding='utf-8', default_flow_style=False) else: raise # unknown format return res
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_header; 3, parameters; 3, 4; 4, identifier:header_text; 5, block; 5, 6; 5, 8; 5, 21; 5, 25; 5, 58; 5, 62; 5, 66; 5, 128; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:lines; 11, call; 11, 12; 11, 19; 12, attribute; 12, 13; 12, 18; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:header_text; 16, identifier:rstrip; 17, argument_list; 18, identifier:split; 19, argument_list; 19, 20; 20, string:"\n"; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:rlens; 24, dictionary; 25, for_statement; 25, 26; 25, 27; 25, 28; 26, identifier:ln; 27, identifier:lines; 28, block; 28, 29; 28, 39; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:m; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:re; 35, identifier:match; 36, argument_list; 36, 37; 36, 38; 37, string:'@SQ\tSN:(\S+)\tLN:(\S+)'; 38, identifier:ln; 39, if_statement; 39, 40; 39, 41; 40, identifier:m; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 52; 44, subscript; 44, 45; 44, 46; 45, identifier:rlens; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:m; 49, identifier:group; 50, argument_list; 50, 51; 51, integer:1; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:m; 55, identifier:group; 56, argument_list; 56, 57; 57, integer:2; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:output; 61, string:''; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:done_lens; 65, False; 66, for_statement; 66, 67; 66, 68; 66, 69; 67, identifier:ln; 68, identifier:lines; 69, block; 69, 70; 70, if_statement; 70, 71; 70, 78; 70, 115; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:re; 74, identifier:match; 75, argument_list; 75, 76; 75, 77; 76, string:'@SQ\tSN:'; 77, identifier:ln; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 82; 80, not_operator; 80, 81; 81, identifier:done_lens; 82, block; 82, 83; 82, 87; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:done_lens; 86, True; 87, for_statement; 87, 88; 87, 89; 87, 97; 88, identifier:chr; 89, call; 89, 90; 89, 91; 90, identifier:sorted; 91, argument_list; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:rlens; 95, identifier:keys; 96, argument_list; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, augmented_assignment:+=; 99, 100; 99, 101; 100, identifier:output; 101, binary_operator:+; 101, 102; 101, 114; 102, binary_operator:+; 102, 103; 102, 108; 103, binary_operator:+; 103, 104; 103, 107; 104, binary_operator:+; 104, 105; 104, 106; 105, string:"@SQ\tSN:"; 106, identifier:chr; 107, string:"\tLN:"; 108, call; 108, 109; 108, 110; 109, identifier:str; 110, argument_list; 110, 111; 111, subscript; 111, 112; 111, 113; 112, identifier:rlens; 113, identifier:chr; 114, string:"\n"; 115, else_clause; 115, 116; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, augmented_assignment:+=; 118, 119; 118, 120; 119, identifier:output; 120, binary_operator:+; 120, 121; 120, 127; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:ln; 124, identifier:rstrip; 125, argument_list; 125, 126; 126, string:"\n"; 127, string:"\n"; 128, return_statement; 128, 129; 129, identifier:output
def sort_header(header_text): """sort the chromosomes in a header text""" lines = header_text.rstrip().split("\n") rlens = {} for ln in lines: m = re.match('@SQ\tSN:(\S+)\tLN:(\S+)',ln) if m: rlens[m.group(1)] = m.group(2) output = '' done_lens = False for ln in lines: if re.match('@SQ\tSN:',ln): if not done_lens: done_lens = True for chr in sorted(rlens.keys()): output += "@SQ\tSN:"+chr+"\tLN:"+str(rlens[chr])+"\n" else: output += ln.rstrip("\n")+"\n" return output
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:field; 6, default_parameter; 6, 7; 6, 8; 7, identifier:direction; 8, string:"asc"; 9, block; 9, 10; 9, 12; 9, 25; 9, 37; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 19; 13, not_operator; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:isinstance; 16, argument_list; 16, 17; 16, 18; 17, identifier:field; 18, identifier:basestring; 19, block; 19, 20; 20, raise_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:ValueError; 23, argument_list; 23, 24; 24, string:"Field should be a string"; 25, if_statement; 25, 26; 25, 31; 26, comparison_operator:not; 26, 27; 26, 28; 27, identifier:direction; 28, list:["asc", "desc"]; 28, 29; 28, 30; 29, string:"asc"; 30, string:"desc"; 31, block; 31, 32; 32, raise_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:ValueError; 35, argument_list; 35, 36; 36, string:"Sort direction should be `asc` or `desc`"; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:sorts; 43, identifier:append; 44, argument_list; 44, 45; 45, dictionary; 45, 46; 46, pair; 46, 47; 46, 48; 47, identifier:field; 48, identifier:direction
def sort(self, field, direction="asc"): """ Adds sort criteria. """ if not isinstance(field, basestring): raise ValueError("Field should be a string") if direction not in ["asc", "desc"]: raise ValueError("Sort direction should be `asc` or `desc`") self.sorts.append({field: direction})
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:remove_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:field_name; 6, block; 6, 7; 6, 9; 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:sorts; 14, list_comprehension; 14, 15; 14, 21; 14, 28; 15, call; 15, 16; 15, 17; 16, identifier:dict; 17, argument_list; 17, 18; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:field; 20, identifier:value; 21, for_in_clause; 21, 22; 21, 25; 22, pattern_list; 22, 23; 22, 24; 23, identifier:field; 24, identifier:value; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:sorts; 28, if_clause; 28, 29; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:field; 31, identifier:field_name
def remove_sort(self, field_name): """ Clears sorting criteria affecting ``field_name``. """ self.sorts = [dict(field=value) for field, value in self.sorts if field is not field_name]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:aggregate_registry_timers; 3, parameters; 4, block; 4, 5; 4, 7; 4, 10; 4, 29; 4, 33; 4, 103; 4, 109; 5, expression_statement; 5, 6; 6, comment; 7, import_statement; 7, 8; 8, dotted_name; 8, 9; 9, identifier:itertools; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:timers; 13, call; 13, 14; 13, 15; 14, identifier:sorted; 15, argument_list; 15, 16; 15, 21; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:shared_registry; 19, identifier:values; 20, argument_list; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:key; 23, lambda; 23, 24; 23, 26; 24, lambda_parameters; 24, 25; 25, identifier:t; 26, attribute; 26, 27; 26, 28; 27, identifier:t; 28, identifier:desc; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:aggregate_timers; 32, list:[]; 33, for_statement; 33, 34; 33, 37; 33, 51; 34, pattern_list; 34, 35; 34, 36; 35, identifier:k; 36, identifier:g; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:itertools; 40, identifier:groupby; 41, argument_list; 41, 42; 41, 43; 42, identifier:timers; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:key; 45, lambda; 45, 46; 45, 48; 46, lambda_parameters; 46, 47; 47, identifier:t; 48, attribute; 48, 49; 48, 50; 49, identifier:t; 50, identifier:desc; 51, block; 51, 52; 51, 59; 51, 66; 51, 78; 51, 90; 51, 91; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:group; 55, call; 55, 56; 55, 57; 56, identifier:list; 57, argument_list; 57, 58; 58, identifier:g; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:num_calls; 62, call; 62, 63; 62, 64; 63, identifier:len; 64, argument_list; 64, 65; 65, identifier:group; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:total_elapsed_ms; 69, call; 69, 70; 69, 71; 70, identifier:sum; 71, generator_expression; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:t; 74, identifier:elapsed_time_ms; 75, for_in_clause; 75, 76; 75, 77; 76, identifier:t; 77, identifier:group; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:first_start_time; 81, call; 81, 82; 81, 83; 82, identifier:min; 83, generator_expression; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:t; 86, identifier:start_time; 87, for_in_clause; 87, 88; 87, 89; 88, identifier:t; 89, identifier:group; 90, comment; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:aggregate_timers; 95, identifier:append; 96, argument_list; 96, 97; 97, tuple; 97, 98; 97, 99; 98, identifier:first_start_time; 99, tuple; 99, 100; 99, 101; 99, 102; 100, identifier:k; 101, identifier:total_elapsed_ms; 102, identifier:num_calls; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:aggregate_timers; 107, identifier:sort; 108, argument_list; 109, return_statement; 109, 110; 110, subscript; 110, 111; 110, 116; 111, call; 111, 112; 111, 113; 112, identifier:zip; 113, argument_list; 113, 114; 114, list_splat; 114, 115; 115, identifier:aggregate_timers; 116, integer:1
def aggregate_registry_timers(): """Returns a list of aggregate timing information for registered timers. Each element is a 3-tuple of - timer description - aggregate elapsed time - number of calls The list is sorted by the first start time of each aggregate timer. """ import itertools timers = sorted(shared_registry.values(), key=lambda t: t.desc) aggregate_timers = [] for k, g in itertools.groupby(timers, key=lambda t: t.desc): group = list(g) num_calls = len(group) total_elapsed_ms = sum(t.elapsed_time_ms for t in group) first_start_time = min(t.start_time for t in group) # We'll use the first start time as a sort key. aggregate_timers.append( (first_start_time, (k, total_elapsed_ms, num_calls))) aggregate_timers.sort() return zip(*aggregate_timers)[1]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:spread; 3, parameters; 3, 4; 4, identifier:iterable; 5, block; 5, 6; 5, 8; 5, 18; 5, 26; 5, 32; 5, 62; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 15; 9, comparison_operator:==; 9, 10; 9, 14; 10, call; 10, 11; 10, 12; 11, identifier:len; 12, argument_list; 12, 13; 13, identifier:iterable; 14, integer:1; 15, block; 15, 16; 16, return_statement; 16, 17; 17, integer:0; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:iterable; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:iterable; 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:iterable; 30, identifier:sort; 31, argument_list; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:max_diff; 35, call; 35, 36; 35, 37; 36, identifier:max; 37, generator_expression; 37, 38; 37, 44; 38, call; 38, 39; 38, 40; 39, identifier:abs; 40, argument_list; 40, 41; 41, binary_operator:-; 41, 42; 41, 43; 42, identifier:i; 43, identifier:j; 44, for_in_clause; 44, 45; 44, 48; 45, tuple_pattern; 45, 46; 45, 47; 46, identifier:i; 47, identifier:j; 48, call; 48, 49; 48, 50; 49, identifier:zip; 50, argument_list; 50, 51; 50, 56; 51, subscript; 51, 52; 51, 53; 52, identifier:iterable; 53, slice; 53, 54; 53, 55; 54, integer:1; 55, colon; 56, subscript; 56, 57; 56, 58; 57, identifier:iterable; 58, slice; 58, 59; 58, 60; 59, colon; 60, unary_operator:-; 60, 61; 61, integer:1; 62, return_statement; 62, 63; 63, identifier:max_diff
def spread(iterable): """Returns the maximal spread of a sorted list of numbers. Parameters ---------- iterable A list of numbers. Returns ------- max_diff The maximal difference when the iterable is sorted. Examples ------- >>> spread([1, 11, 13, 15]) 10 >>> spread([1, 15, 11, 13]) 10 """ if len(iterable) == 1: return 0 iterable = iterable.copy() iterable.sort() max_diff = max(abs(i - j) for (i, j) in zip(iterable[1:], iterable[:-1])) return max_diff
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_transcripts; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 38; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:txs; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:transcripts; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:key; 19, lambda; 19, 20; 19, 22; 20, lambda_parameters; 20, 21; 21, identifier:x; 22, tuple; 22, 23; 22, 28; 22, 33; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:x; 26, identifier:range; 27, identifier:chr; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:x; 31, identifier:range; 32, identifier:start; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:x; 36, identifier:range; 37, identifier:end; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_transcripts; 43, identifier:txs
def sort_transcripts(self): """Sort the transcripts stored here""" txs = sorted(self.transcripts,key=lambda x: (x.range.chr, x.range.start, x.range.end)) self._transcripts = txs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:in1d_events; 3, parameters; 3, 4; 3, 5; 4, identifier:ar1; 5, identifier:ar2; 6, block; 6, 7; 6, 9; 6, 18; 6, 19; 6, 28; 6, 29; 6, 43; 6, 44; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:ar1; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:np; 15, identifier:ascontiguousarray; 16, argument_list; 16, 17; 17, identifier:ar1; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:ar2; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:np; 25, identifier:ascontiguousarray; 26, argument_list; 26, 27; 27, identifier:ar2; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:tmp; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:np; 35, identifier:empty_like; 36, argument_list; 36, 37; 36, 38; 37, identifier:ar1; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:dtype; 40, attribute; 40, 41; 40, 42; 41, identifier:np; 42, identifier:uint8; 43, comment; 44, return_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:analysis_functions; 48, identifier:get_in1d_sorted; 49, argument_list; 49, 50; 49, 51; 49, 52; 50, identifier:ar1; 51, identifier:ar2; 52, identifier:tmp
def in1d_events(ar1, ar2): """ Does the same than np.in1d but uses the fact that ar1 and ar2 are sorted and the c++ library. Is therefore much much faster. """ ar1 = np.ascontiguousarray(ar1) # change memory alignement for c++ library ar2 = np.ascontiguousarray(ar2) # change memory alignement for c++ library tmp = np.empty_like(ar1, dtype=np.uint8) # temporary result array filled by c++ library, bool type is not supported with cython/numpy return analysis_functions.get_in1d_sorted(ar1, ar2, tmp)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:sort_by_priority; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:iterable; 5, default_parameter; 5, 6; 5, 7; 6, identifier:reverse; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:default_priority; 10, integer:10; 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, 22; 18, identifier:iterable; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:reverse; 21, identifier:reverse; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:key; 24, lambda; 24, 25; 24, 27; 25, lambda_parameters; 25, 26; 26, identifier:o; 27, call; 27, 28; 27, 29; 28, identifier:getattr; 29, argument_list; 29, 30; 29, 31; 29, 32; 30, identifier:o; 31, string:'priority'; 32, identifier:default_priority
def sort_by_priority(iterable, reverse=False, default_priority=10): """ Return a list or objects sorted by a priority value. """ return sorted(iterable, reverse=reverse, key=lambda o: getattr(o, 'priority', default_priority))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:apply; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:func; 6, default_parameter; 6, 7; 6, 8; 7, identifier:mapping; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:new_dtype; 11, None; 12, dictionary_splat_pattern; 12, 13; 13, identifier:kwargs; 14, block; 14, 15; 14, 17; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 22; 17, 49; 17, 121; 18, call; 18, 19; 18, 20; 19, identifier:callable; 20, argument_list; 20, 21; 21, identifier:func; 22, block; 22, 23; 23, return_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:Series; 26, argument_list; 26, 27; 26, 40; 26, 43; 26, 46; 27, call; 27, 28; 27, 29; 28, identifier:func; 29, argument_list; 29, 30; 29, 33; 29, 38; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:values; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:weld_type; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:weld_type; 38, dictionary_splat; 38, 39; 39, identifier:kwargs; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:index; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:dtype; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:name; 49, elif_clause; 49, 50; 49, 55; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:func; 54, identifier:str; 55, block; 55, 56; 55, 62; 55, 67; 55, 76; 55, 94; 55, 105; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:check_type; 59, argument_list; 59, 60; 59, 61; 60, identifier:mapping; 61, identifier:dict; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:check_dtype; 65, argument_list; 65, 66; 66, identifier:new_dtype; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:default_mapping; 70, dictionary; 70, 71; 71, pair; 71, 72; 71, 73; 72, string:'self'; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:values; 76, if_statement; 76, 77; 76, 80; 76, 85; 77, comparison_operator:is; 77, 78; 77, 79; 78, identifier:mapping; 79, None; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:mapping; 84, identifier:default_mapping; 85, else_clause; 85, 86; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:mapping; 91, identifier:update; 92, argument_list; 92, 93; 93, identifier:default_mapping; 94, if_statement; 94, 95; 94, 98; 95, comparison_operator:is; 95, 96; 95, 97; 96, identifier:new_dtype; 97, None; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:new_dtype; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:dtype; 105, return_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:Series; 108, argument_list; 108, 109; 108, 114; 108, 117; 108, 118; 109, call; 109, 110; 109, 111; 110, identifier:weld_udf; 111, argument_list; 111, 112; 111, 113; 112, identifier:func; 113, identifier:mapping; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:index; 117, identifier:new_dtype; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:name; 121, else_clause; 121, 122; 122, block; 122, 123; 123, raise_statement; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:TypeError; 126, argument_list; 126, 127; 127, string:'Expected function or str defining a weld_template'
def apply(self, func, mapping=None, new_dtype=None, **kwargs): """Apply an element-wise UDF to the Series. There are currently 6 options for using a UDF. First 4 are lazy, other 2 are eager and require the use of the raw decorator: - One of the predefined functions in baloo.functions. - Implementing a function which encodes the result. kwargs are automatically passed to it. - Pure Weld code and mapping. - Weld code and mapping along with a dynamically linked C++ lib containing the UDF. - Using a NumPy function, which however is EAGER and hence requires self.values to be raw. Additionally, NumPy does not support kwargs in (all) functions so must use raw decorator to strip away weld_type. - Implementing an eager function with the same precondition as above. Use the raw decorator to check this. Parameters ---------- func : function or str Weld code as a str to encode or function from baloo.functions. mapping : dict, optional Additional mappings in the weld_template to replace on execution. self is added by default to reference to this Series. new_dtype : numpy.dtype, optional Specify the new dtype of the result Series. If None, it assumes it's the same dtype as before the apply. Returns ------- Series With UDF result. Examples -------- >>> import baloo as bl >>> sr = bl.Series([1, 2, 3]) >>> weld_template = 'map({self}, |e| e + {scalar})' >>> mapping = {'scalar': '2L'} >>> print(sr.apply(weld_template, mapping).evaluate()) <BLANKLINE> --- -- 0 3 1 4 2 5 >>> weld_template2 = 'map({self}, |e| e + 3L)' >>> print(sr.apply(weld_template2).evaluate()) <BLANKLINE> --- -- 0 4 1 5 2 6 >>> print(bl.Series([1., 4., 100.]).apply(bl.sqrt).evaluate()) # lazy predefined function <BLANKLINE> --- -- 0 1 1 2 2 10 >>> sr = bl.Series([4, 2, 3, 1]) >>> print(sr.apply(bl.sort, kind='q').evaluate()) # eager wrapper over np.sort (which uses raw decorator) <BLANKLINE> --- -- 0 1 1 2 2 3 3 4 >>> print(sr.apply(bl.raw(np.sort, kind='q')).evaluate()) # np.sort directly <BLANKLINE> --- -- 0 1 1 2 2 3 3 4 >>> print(sr.apply(bl.raw(lambda x: np.sort(x, kind='q'))).evaluate()) # lambda also works, with x = np.array <BLANKLINE> --- -- 0 1 1 2 2 3 3 4 # check tests/core/cudf/* and tests/core/test_series.test_cudf for C UDF example """ if callable(func): return Series(func(self.values, weld_type=self.weld_type, **kwargs), self.index, self.dtype, self.name) elif isinstance(func, str): check_type(mapping, dict) check_dtype(new_dtype) default_mapping = {'self': self.values} if mapping is None: mapping = default_mapping else: mapping.update(default_mapping) if new_dtype is None: new_dtype = self.dtype return Series(weld_udf(func, mapping), self.index, new_dtype, self.name) else: raise TypeError('Expected function or str defining a weld_template')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:weld_sort; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:arrays; 5, identifier:weld_types; 6, identifier:readable_text; 7, default_parameter; 7, 8; 7, 9; 8, identifier:ascending; 9, True; 10, block; 10, 11; 10, 13; 10, 22; 10, 30; 10, 38; 10, 48; 10, 58; 10, 70; 10, 79; 10, 90; 10, 98; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:weld_obj_sort; 16, call; 16, 17; 16, 18; 17, identifier:_weld_sort; 18, argument_list; 18, 19; 18, 20; 18, 21; 19, identifier:arrays; 20, identifier:weld_types; 21, identifier:ascending; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:weld_obj_struct; 25, call; 25, 26; 25, 27; 26, identifier:weld_vec_of_struct_to_struct_of_vec; 27, argument_list; 27, 28; 27, 29; 28, identifier:weld_obj_sort; 29, identifier:weld_types; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:weld_obj_indices; 33, call; 33, 34; 33, 35; 34, identifier:weld_select_from_struct; 35, argument_list; 35, 36; 35, 37; 36, identifier:weld_obj_struct; 37, integer:0; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:intermediate_result; 41, call; 41, 42; 41, 43; 42, identifier:LazyArrayResult; 43, argument_list; 43, 44; 43, 45; 44, identifier:weld_obj_indices; 45, call; 45, 46; 45, 47; 46, identifier:WeldLong; 47, argument_list; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:dependency_name; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:Cache; 54, identifier:cache_intermediate_result; 55, argument_list; 55, 56; 55, 57; 56, identifier:intermediate_result; 57, identifier:readable_text; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:fake_weld_input; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:Cache; 64, identifier:create_fake_array_input; 65, argument_list; 65, 66; 65, 67; 66, identifier:dependency_name; 67, binary_operator:+; 67, 68; 67, 69; 68, identifier:readable_text; 69, string:'_indices'; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 75; 72, pattern_list; 72, 73; 72, 74; 73, identifier:obj_id; 74, identifier:weld_obj; 75, call; 75, 76; 75, 77; 76, identifier:create_weld_object; 77, argument_list; 77, 78; 78, identifier:fake_weld_input; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:weld_obj; 83, identifier:weld_code; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, string:'{}'; 87, identifier:format; 88, argument_list; 88, 89; 89, identifier:obj_id; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:Cache; 94, identifier:cache_fake_input; 95, argument_list; 95, 96; 95, 97; 96, identifier:obj_id; 97, identifier:fake_weld_input; 98, return_statement; 98, 99; 99, identifier:weld_obj
def weld_sort(arrays, weld_types, readable_text, ascending=True): """Sort the arrays. Parameters ---------- arrays : list of (numpy.ndarray or WeldObject) Arrays to put in a struct. weld_types : list of WeldType The Weld types of the arrays in the same order. readable_text : str Explanatory string to add in the Weld placeholder. ascending : bool, optional Returns ------- list of WeldObject Representation of this computation. """ weld_obj_sort = _weld_sort(arrays, weld_types, ascending) weld_obj_struct = weld_vec_of_struct_to_struct_of_vec(weld_obj_sort, weld_types) weld_obj_indices = weld_select_from_struct(weld_obj_struct, 0) intermediate_result = LazyArrayResult(weld_obj_indices, WeldLong()) dependency_name = Cache.cache_intermediate_result(intermediate_result, readable_text) fake_weld_input = Cache.create_fake_array_input(dependency_name, readable_text + '_indices') obj_id, weld_obj = create_weld_object(fake_weld_input) weld_obj.weld_code = '{}'.format(obj_id) Cache.cache_fake_input(obj_id, fake_weld_input) return weld_obj
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:chooseBestDuplicates; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:tped; 5, identifier:samples; 6, identifier:oldSamples; 7, identifier:completion; 8, identifier:concordance_all; 9, identifier:prefix; 10, block; 10, 11; 10, 13; 10, 14; 10, 18; 10, 46; 10, 59; 10, 63; 10, 91; 10, 104; 10, 105; 10, 109; 10, 113; 10, 449; 10, 450; 10, 456; 10, 462; 11, expression_statement; 11, 12; 12, comment; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:chosenFile; 17, None; 18, try_statement; 18, 19; 18, 30; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:chosenFile; 23, call; 23, 24; 23, 25; 24, identifier:open; 25, argument_list; 25, 26; 25, 29; 26, binary_operator:+; 26, 27; 26, 28; 27, identifier:prefix; 28, string:".chosen_samples.info"; 29, string:"w"; 30, except_clause; 30, 31; 30, 32; 31, identifier:IOError; 32, block; 32, 33; 32, 41; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:msg; 36, binary_operator:%; 36, 37; 36, 38; 37, string:"%(prefix)s.chosen_samples.info: can't write file"; 38, call; 38, 39; 38, 40; 39, identifier:locals; 40, argument_list; 41, raise_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:ProgramError; 44, argument_list; 44, 45; 45, identifier:msg; 46, print_statement; 46, 47; 46, 49; 47, chevron; 47, 48; 48, identifier:chosenFile; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, string:"\t"; 52, identifier:join; 53, argument_list; 53, 54; 54, list:["origIndex", "dupIndex", "famID", "indID"]; 54, 55; 54, 56; 54, 57; 54, 58; 55, string:"origIndex"; 56, string:"dupIndex"; 57, string:"famID"; 58, string:"indID"; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:excludedFile; 62, None; 63, try_statement; 63, 64; 63, 75; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:excludedFile; 68, call; 68, 69; 68, 70; 69, identifier:open; 70, argument_list; 70, 71; 70, 74; 71, binary_operator:+; 71, 72; 71, 73; 72, identifier:prefix; 73, string:".excluded_samples.info"; 74, string:"w"; 75, except_clause; 75, 76; 75, 77; 76, identifier:IOError; 77, block; 77, 78; 77, 86; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:msg; 81, binary_operator:%; 81, 82; 81, 83; 82, string:"%(prefix)s.excluded_samples.info: can't write file"; 83, call; 83, 84; 83, 85; 84, identifier:locals; 85, argument_list; 86, raise_statement; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:ProgramError; 89, argument_list; 89, 90; 90, identifier:msg; 91, print_statement; 91, 92; 91, 94; 92, chevron; 92, 93; 93, identifier:excludedFile; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, string:"\t"; 97, identifier:join; 98, argument_list; 98, 99; 99, list:["origIndex", "dupIndex", "famID", "indID"]; 99, 100; 99, 101; 99, 102; 99, 103; 100, string:"origIndex"; 101, string:"dupIndex"; 102, string:"famID"; 103, string:"indID"; 104, comment; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:chosenIndexes; 108, dictionary; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:sampleConcordance; 112, dictionary; 113, for_statement; 113, 114; 113, 117; 113, 122; 113, 123; 114, pattern_list; 114, 115; 114, 116; 115, identifier:sample; 116, identifier:indexes; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:samples; 120, identifier:iteritems; 121, argument_list; 122, comment; 123, block; 123, 124; 123, 130; 123, 131; 123, 140; 123, 141; 123, 147; 123, 161; 123, 207; 123, 216; 123, 227; 123, 228; 123, 237; 123, 238; 123, 242; 123, 246; 123, 342; 123, 361; 123, 362; 123, 395; 123, 396; 123, 441; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:currCompletion; 127, subscript; 127, 128; 127, 129; 128, identifier:completion; 129, identifier:indexes; 130, comment; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:sortedCompletionIndexes; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:np; 137, identifier:argsort; 138, argument_list; 138, 139; 139, identifier:currCompletion; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:concordance; 144, subscript; 144, 145; 144, 146; 145, identifier:concordance_all; 146, identifier:sample; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:currConcordance; 150, list_comprehension; 150, 151; 150, 152; 151, list:[]; 152, for_in_clause; 152, 153; 152, 154; 153, identifier:i; 154, call; 154, 155; 154, 156; 155, identifier:xrange; 156, argument_list; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:len; 159, argument_list; 159, 160; 160, identifier:indexes; 161, for_statement; 161, 162; 161, 163; 161, 170; 162, identifier:i; 163, call; 163, 164; 163, 165; 164, identifier:xrange; 165, argument_list; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:len; 168, argument_list; 168, 169; 169, identifier:indexes; 170, block; 170, 171; 170, 193; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:indexToKeep; 174, call; 174, 175; 174, 176; 175, identifier:list; 176, argument_list; 176, 177; 177, binary_operator:-; 177, 178; 177, 188; 178, call; 178, 179; 178, 180; 179, identifier:set; 180, argument_list; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:range; 183, argument_list; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:len; 186, argument_list; 186, 187; 187, identifier:indexes; 188, call; 188, 189; 188, 190; 189, identifier:set; 190, argument_list; 190, 191; 191, list:[i]; 191, 192; 192, identifier:i; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:currConcordance; 197, identifier:i; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:np; 201, identifier:mean; 202, argument_list; 202, 203; 203, subscript; 203, 204; 203, 205; 203, 206; 204, identifier:concordance; 205, identifier:i; 206, identifier:indexToKeep; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:currConcordance; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:np; 213, identifier:array; 214, argument_list; 214, 215; 215, identifier:currConcordance; 216, if_statement; 216, 217; 216, 220; 217, comparison_operator:not; 217, 218; 217, 219; 218, identifier:sample; 219, identifier:sampleConcordance; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 226; 223, subscript; 223, 224; 223, 225; 224, identifier:sampleConcordance; 225, identifier:sample; 226, identifier:currConcordance; 227, comment; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:sortedConcordanceIndexes; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:np; 234, identifier:argsort; 235, argument_list; 235, 236; 236, identifier:currConcordance; 237, comment; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:nbToCheck; 241, integer:1; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:chosenIndex; 245, None; 246, while_statement; 246, 247; 246, 253; 246, 254; 247, comparison_operator:<=; 247, 248; 247, 249; 248, identifier:nbToCheck; 249, call; 249, 250; 249, 251; 250, identifier:len; 251, argument_list; 251, 252; 252, identifier:indexes; 253, comment; 254, block; 254, 255; 254, 266; 254, 277; 254, 278; 254, 294; 254, 310; 254, 311; 254, 317; 254, 338; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:completionValue; 258, subscript; 258, 259; 258, 260; 259, identifier:currCompletion; 260, subscript; 260, 261; 260, 262; 261, identifier:sortedCompletionIndexes; 262, binary_operator:*; 262, 263; 262, 264; 263, identifier:nbToCheck; 264, unary_operator:-; 264, 265; 265, integer:1; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:concordanceValue; 269, subscript; 269, 270; 269, 271; 270, identifier:currConcordance; 271, subscript; 271, 272; 271, 273; 272, identifier:sortedConcordanceIndexes; 273, binary_operator:*; 273, 274; 273, 275; 274, identifier:nbToCheck; 275, unary_operator:-; 275, 276; 276, integer:1; 277, comment; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:completionToConsider; 281, call; 281, 282; 281, 283; 282, identifier:set; 283, argument_list; 283, 284; 284, subscript; 284, 285; 284, 293; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:np; 288, identifier:where; 289, argument_list; 289, 290; 290, comparison_operator:>=; 290, 291; 290, 292; 291, identifier:currCompletion; 292, identifier:completionValue; 293, integer:0; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:concordanceToConsider; 297, call; 297, 298; 297, 299; 298, identifier:set; 299, argument_list; 299, 300; 300, subscript; 300, 301; 300, 309; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:np; 304, identifier:where; 305, argument_list; 305, 306; 306, comparison_operator:>=; 306, 307; 306, 308; 307, identifier:currConcordance; 308, identifier:concordanceValue; 309, integer:0; 310, comment; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:toConsider; 314, binary_operator:&; 314, 315; 314, 316; 315, identifier:concordanceToConsider; 316, identifier:completionToConsider; 317, if_statement; 317, 318; 317, 324; 318, comparison_operator:>=; 318, 319; 318, 323; 319, call; 319, 320; 319, 321; 320, identifier:len; 321, argument_list; 321, 322; 322, identifier:toConsider; 323, integer:1; 324, block; 324, 325; 324, 337; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:chosenIndex; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:random; 331, identifier:choice; 332, argument_list; 332, 333; 333, call; 333, 334; 333, 335; 334, identifier:list; 335, argument_list; 335, 336; 336, identifier:toConsider; 337, break_statement; 338, expression_statement; 338, 339; 339, augmented_assignment:+=; 339, 340; 339, 341; 340, identifier:nbToCheck; 341, integer:1; 342, if_statement; 342, 343; 342, 346; 343, comparison_operator:is; 343, 344; 343, 345; 344, identifier:chosenIndex; 345, None; 346, block; 346, 347; 346, 356; 347, expression_statement; 347, 348; 348, assignment; 348, 349; 348, 350; 349, identifier:msg; 350, call; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, string:"Could not choose the best sample ID for {}"; 353, identifier:format; 354, argument_list; 354, 355; 355, identifier:sample; 356, raise_statement; 356, 357; 357, call; 357, 358; 357, 359; 358, identifier:ProgramError; 359, argument_list; 359, 360; 360, identifier:msg; 361, comment; 362, print_statement; 362, 363; 362, 365; 363, chevron; 363, 364; 364, identifier:chosenFile; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, string:"\t"; 368, identifier:join; 369, argument_list; 369, 370; 370, list:[str(oldSamples[sample][chosenIndex]+1), str(indexes[chosenIndex]+1), sample[0], sample[1]]; 370, 371; 370, 381; 370, 389; 370, 392; 371, call; 371, 372; 371, 373; 372, identifier:str; 373, argument_list; 373, 374; 374, binary_operator:+; 374, 375; 374, 380; 375, subscript; 375, 376; 375, 379; 376, subscript; 376, 377; 376, 378; 377, identifier:oldSamples; 378, identifier:sample; 379, identifier:chosenIndex; 380, integer:1; 381, call; 381, 382; 381, 383; 382, identifier:str; 383, argument_list; 383, 384; 384, binary_operator:+; 384, 385; 384, 388; 385, subscript; 385, 386; 385, 387; 386, identifier:indexes; 387, identifier:chosenIndex; 388, integer:1; 389, subscript; 389, 390; 389, 391; 390, identifier:sample; 391, integer:0; 392, subscript; 392, 393; 392, 394; 393, identifier:sample; 394, integer:1; 395, comment; 396, for_statement; 396, 397; 396, 400; 396, 404; 397, pattern_list; 397, 398; 397, 399; 398, identifier:i; 399, identifier:index; 400, call; 400, 401; 400, 402; 401, identifier:enumerate; 402, argument_list; 402, 403; 403, identifier:indexes; 404, block; 404, 405; 405, if_statement; 405, 406; 405, 409; 406, comparison_operator:!=; 406, 407; 406, 408; 407, identifier:i; 408, identifier:chosenIndex; 409, block; 409, 410; 410, print_statement; 410, 411; 410, 413; 411, chevron; 411, 412; 412, identifier:excludedFile; 413, call; 413, 414; 413, 417; 414, attribute; 414, 415; 414, 416; 415, string:"\t"; 416, identifier:join; 417, argument_list; 417, 418; 418, list:[str(oldSamples[sample][i]+1), str(index+1), sample[0], sample[1]]; 418, 419; 418, 429; 418, 435; 418, 438; 419, call; 419, 420; 419, 421; 420, identifier:str; 421, argument_list; 421, 422; 422, binary_operator:+; 422, 423; 422, 428; 423, subscript; 423, 424; 423, 427; 424, subscript; 424, 425; 424, 426; 425, identifier:oldSamples; 426, identifier:sample; 427, identifier:i; 428, integer:1; 429, call; 429, 430; 429, 431; 430, identifier:str; 431, argument_list; 431, 432; 432, binary_operator:+; 432, 433; 432, 434; 433, identifier:index; 434, integer:1; 435, subscript; 435, 436; 435, 437; 436, identifier:sample; 437, integer:0; 438, subscript; 438, 439; 438, 440; 439, identifier:sample; 440, integer:1; 441, expression_statement; 441, 442; 442, assignment; 442, 443; 442, 446; 443, subscript; 443, 444; 443, 445; 444, identifier:chosenIndexes; 445, identifier:sample; 446, subscript; 446, 447; 446, 448; 447, identifier:indexes; 448, identifier:chosenIndex; 449, comment; 450, expression_statement; 450, 451; 451, call; 451, 452; 451, 455; 452, attribute; 452, 453; 452, 454; 453, identifier:chosenFile; 454, identifier:close; 455, argument_list; 456, expression_statement; 456, 457; 457, call; 457, 458; 457, 461; 458, attribute; 458, 459; 458, 460; 459, identifier:excludedFile; 460, identifier:close; 461, argument_list; 462, return_statement; 462, 463; 463, expression_list; 463, 464; 463, 465; 463, 466; 464, identifier:chosenIndexes; 465, identifier:completion; 466, identifier:sampleConcordance
def chooseBestDuplicates(tped, samples, oldSamples, completion, concordance_all, prefix): """Choose the best duplicates according to the completion rate. :param tped: the ``tped`` containing the duplicated samples. :param samples: the updated position of the samples in the tped containing only duplicated samples. :param oldSamples: the original duplicated sample positions. :param completion: the completion of each of the duplicated samples. :param concordance_all: the concordance of every duplicated samples. :param prefix: the prefix of all the files. :type tped: :py:class:`numpy.array` :type samples: dict :type oldSamples: dict :type completion: :py:class:`numpy.array` :type concordance_all: dict :type prefix: str :returns: a tuple where the first element is a list of the chosen samples' indexes, the second on is the completion and the last one is the concordance (a map). These are the steps to find the best duplicated sample: 1. Sort the list of concordances. 2. Sort the list of completions. 3. Choose the best of the concordance and put in a set. 4. Choose the best of the completion and put it in a set. 5. Compute the intersection of the two sets. If there is one sample or more, then randomly choose one sample. 6. If the intersection doesn't contain at least one sample, redo steps 3 and 4, but increase the number of chosen best by one. Redo step 5 and 6 (if required). The chosen samples are written in ``prefix.chosen_samples.info``. The rest are written in ``prefix.excluded_samples.info``. """ # The output files chosenFile = None try: chosenFile = open(prefix + ".chosen_samples.info", "w") except IOError: msg = "%(prefix)s.chosen_samples.info: can't write file" % locals() raise ProgramError(msg) print >>chosenFile, "\t".join(["origIndex", "dupIndex", "famID", "indID"]) excludedFile = None try: excludedFile = open(prefix + ".excluded_samples.info", "w") except IOError: msg = "%(prefix)s.excluded_samples.info: can't write file" % locals() raise ProgramError(msg) print >>excludedFile, "\t".join(["origIndex", "dupIndex", "famID", "indID"]) # For each duplicated sample chosenIndexes = {} sampleConcordance = {} for sample, indexes in samples.iteritems(): # Getting the completion for those duplicated samples currCompletion = completion[indexes] # Sorting those completion sortedCompletionIndexes = np.argsort(currCompletion) # Getting the concordance concordance = concordance_all[sample] currConcordance = [[] for i in xrange(len(indexes))] for i in xrange(len(indexes)): indexToKeep = list(set(range(len(indexes))) - set([i])) currConcordance[i] = np.mean(concordance[i, indexToKeep]) currConcordance = np.array(currConcordance) if sample not in sampleConcordance: sampleConcordance[sample] = currConcordance # Sorting the concordance sortedConcordanceIndexes = np.argsort(currConcordance) # Trying to find the best duplicate to keep nbToCheck = 1 chosenIndex = None while nbToCheck <= len(indexes): # Getting the `nbToCheck` best value (higher to lower) completionValue = currCompletion[ sortedCompletionIndexes[nbToCheck*-1] ] concordanceValue = currConcordance[ sortedConcordanceIndexes[nbToCheck*-1] ] # Getting the indexes to consider completionToConsider = set( np.where(currCompletion >= completionValue)[0] ) concordanceToConsider = set( np.where(currConcordance >= concordanceValue)[0]) # Getting the intersection of the indexes toConsider = concordanceToConsider & completionToConsider if len(toConsider) >= 1: chosenIndex = random.choice(list(toConsider)) break nbToCheck += 1 if chosenIndex is None: msg = "Could not choose the best sample ID for {}".format(sample) raise ProgramError(msg) # Printing the chosen samples print >>chosenFile, "\t".join([str(oldSamples[sample][chosenIndex]+1), str(indexes[chosenIndex]+1), sample[0], sample[1]]) # Printing the excluded samples for i, index in enumerate(indexes): if i != chosenIndex: print >>excludedFile, "\t".join([str(oldSamples[sample][i]+1), str(index+1), sample[0], sample[1]]) chosenIndexes[sample] = indexes[chosenIndex] # Closing the output files chosenFile.close() excludedFile.close() return chosenIndexes, completion, sampleConcordance
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:order_qc_dir; 3, parameters; 3, 4; 4, identifier:dirnames; 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; 11, 13; 12, identifier:dirnames; 13, keyword_argument; 13, 14; 13, 15; 14, identifier:key; 15, lambda; 15, 16; 15, 18; 16, lambda_parameters; 16, 17; 17, identifier:dn; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:time; 21, identifier:strptime; 22, argument_list; 22, 23; 22, 40; 23, subscript; 23, 24; 23, 37; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:os; 28, identifier:path; 29, identifier:basename; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:dn; 34, identifier:rstrip; 35, argument_list; 35, 36; 36, string:"/"; 37, slice; 37, 38; 37, 39; 38, integer:14; 39, colon; 40, string:"%Y-%m-%d_%H.%M.%S"
def order_qc_dir(dirnames): """Order the QC directory names according to their date. :param dirnames: the list of directories to merge data from. :type dirnames: list :returns: the sorted list of directories :rtype: list """ return sorted( dirnames, key=lambda dn: time.strptime( os.path.basename(dn.rstrip("/"))[14:], "%Y-%m-%d_%H.%M.%S", ) )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:fetch; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:webfonts; 6, block; 6, 7; 6, 9; 6, 20; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:sorted_keys; 12, call; 12, 13; 12, 14; 13, identifier:sorted; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:webfonts; 18, identifier:keys; 19, argument_list; 20, for_statement; 20, 21; 20, 22; 20, 23; 21, identifier:webfont_name; 22, identifier:sorted_keys; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:get; 29, argument_list; 29, 30; 29, 31; 30, identifier:webfont_name; 31, subscript; 31, 32; 31, 33; 32, identifier:webfonts; 33, identifier:webfont_name
def fetch(self, webfonts): """ Store every defined webfonts. Webfont are stored with sort on their name. Args: webfonts (dict): Dictionnary of webfont settings from ``settings.ICOMOON_WEBFONTS``. """ sorted_keys = sorted(webfonts.keys()) for webfont_name in sorted_keys: self.get(webfont_name, webfonts[webfont_name])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:sort_index; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ascending; 7, True; 8, block; 8, 9; 8, 11; 8, 25; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 19; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:index; 18, identifier:MultiIndex; 19, block; 19, 20; 20, raise_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:NotImplementedError; 23, argument_list; 23, 24; 24, string:'Weld does not yet support sorting on multiple columns'; 25, return_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:sort_values; 30, argument_list; 30, 31; 30, 38; 31, call; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:index; 36, identifier:_gather_names; 37, argument_list; 38, identifier:ascending
def sort_index(self, ascending=True): """Sort the index of the DataFrame. Currently MultiIndex is not supported since Weld is missing multiple-column sort. Note this is an expensive operation (brings all data to Weld). Parameters ---------- ascending : bool, optional Returns ------- DataFrame DataFrame sorted according to the index. """ if isinstance(self.index, MultiIndex): raise NotImplementedError('Weld does not yet support sorting on multiple columns') return self.sort_values(self.index._gather_names(), ascending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort_values; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:by; 6, default_parameter; 6, 7; 6, 8; 7, identifier:ascending; 8, True; 9, block; 9, 10; 9, 12; 9, 18; 9, 23; 9, 30; 9, 43; 9, 51; 9, 57; 9, 77; 9, 88; 9, 99; 9, 109; 9, 122; 9, 133; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:check_type; 15, argument_list; 15, 16; 15, 17; 16, identifier:ascending; 17, identifier:bool; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:check_str_or_list_str; 21, argument_list; 21, 22; 22, identifier:by; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:by; 26, call; 26, 27; 26, 28; 27, identifier:as_list; 28, argument_list; 28, 29; 29, identifier:by; 30, if_statement; 30, 31; 30, 37; 31, comparison_operator:>; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:len; 34, argument_list; 34, 35; 35, identifier:by; 36, integer:1; 37, block; 37, 38; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:NotImplementedError; 41, argument_list; 41, 42; 42, string:'Weld does not yet support sorting on multiple columns'; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:all_data; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:reset_index; 50, argument_list; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:by_data; 54, subscript; 54, 55; 54, 56; 55, identifier:all_data; 56, identifier:by; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:sorted_indices; 60, call; 60, 61; 60, 62; 61, identifier:weld_sort; 62, argument_list; 62, 63; 62, 68; 62, 73; 62, 74; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:by_data; 66, identifier:_gather_data_for_weld; 67, argument_list; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:by_data; 71, identifier:_gather_weld_types; 72, argument_list; 73, string:'sort_index'; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:ascending; 76, identifier:ascending; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:new_index; 80, call; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:index; 85, identifier:_iloc_indices; 86, argument_list; 86, 87; 87, identifier:sorted_indices; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:new_columns; 91, call; 91, 92; 91, 93; 92, identifier:list; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_iter; 98, argument_list; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:new_column_names; 102, list_comprehension; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:column; 105, identifier:name; 106, for_in_clause; 106, 107; 106, 108; 107, identifier:column; 108, identifier:new_columns; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:new_columns; 112, list_comprehension; 112, 113; 112, 119; 113, call; 113, 114; 113, 115; 114, identifier:_series_iloc; 115, argument_list; 115, 116; 115, 117; 115, 118; 116, identifier:column; 117, identifier:sorted_indices; 118, identifier:new_index; 119, for_in_clause; 119, 120; 119, 121; 120, identifier:column; 121, identifier:new_columns; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:new_data; 125, call; 125, 126; 125, 127; 126, identifier:OrderedDict; 127, argument_list; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:zip; 130, argument_list; 130, 131; 130, 132; 131, identifier:new_column_names; 132, identifier:new_columns; 133, return_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:DataFrame; 136, argument_list; 136, 137; 136, 138; 137, identifier:new_data; 138, identifier:new_index
def sort_values(self, by, ascending=True): """Sort the DataFrame based on a column. Unlike Pandas, one can sort by data from both index and regular columns. Currently possible to sort only on a single column since Weld is missing multiple-column sort. Note this is an expensive operation (brings all data to Weld). Parameters ---------- by : str or list of str Column names to sort. ascending : bool, optional Returns ------- DataFrame DataFrame sorted according to the column. """ check_type(ascending, bool) check_str_or_list_str(by) by = as_list(by) if len(by) > 1: raise NotImplementedError('Weld does not yet support sorting on multiple columns') all_data = self.reset_index() by_data = all_data[by] sorted_indices = weld_sort(by_data._gather_data_for_weld(), by_data._gather_weld_types(), 'sort_index', ascending=ascending) new_index = self.index._iloc_indices(sorted_indices) new_columns = list(self._iter()) new_column_names = [column.name for column in new_columns] new_columns = [_series_iloc(column, sorted_indices, new_index) for column in new_columns] new_data = OrderedDict(zip(new_column_names, new_columns)) return DataFrame(new_data, new_index)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:optimize_with_repeates; 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:fast; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:verbose; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:n_times; 13, integer:10; 14, default_parameter; 14, 15; 14, 16; 15, identifier:lambd; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:lambd_g; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:lambd_n; 22, None; 23, block; 23, 24; 23, 26; 23, 35; 23, 48; 23, 52; 23, 69; 23, 70; 23, 227; 23, 228; 23, 251; 23, 265; 23, 269; 23, 287; 23, 338; 24, expression_statement; 24, 25; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:verbose; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:dlimix; 32, identifier:getVerbose; 33, argument_list; 33, 34; 34, identifier:verbose; 35, if_statement; 35, 36; 35, 40; 36, not_operator; 36, 37; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:init; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:_initGP; 46, argument_list; 46, 47; 47, identifier:fast; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:opt_list; 51, list:[]; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:fixed0; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:sp; 58, identifier:zeros_like; 59, argument_list; 59, 60; 60, subscript; 60, 61; 60, 68; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:gp; 66, identifier:getParams; 67, argument_list; 68, string:'dataTerm'; 69, comment; 70, for_statement; 70, 71; 70, 72; 70, 76; 71, identifier:i; 72, call; 72, 73; 72, 74; 73, identifier:range; 74, argument_list; 74, 75; 75, identifier:n_times; 76, block; 76, 77; 76, 85; 76, 105; 76, 131; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:scales1; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_getScalesRand; 84, argument_list; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:fixed1; 88, binary_operator:*; 88, 89; 88, 90; 89, float:1e-1; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:sp; 93, identifier:randn; 94, argument_list; 94, 95; 94, 100; 95, subscript; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:fixed0; 98, identifier:shape; 99, integer:0; 100, subscript; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:fixed0; 103, identifier:shape; 104, integer:1; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:conv; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:trainGP; 112, argument_list; 112, 113; 112, 116; 112, 119; 112, 122; 112, 125; 112, 128; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:fast; 115, identifier:fast; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:scales0; 118, identifier:scales1; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:fixed0; 121, identifier:fixed1; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:lambd; 124, identifier:lambd; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:lambd_g; 127, identifier:lambd_g; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:lambd_n; 130, identifier:lambd_n; 131, if_statement; 131, 132; 131, 133; 131, 134; 132, identifier:conv; 133, comment; 134, block; 134, 135; 134, 139; 134, 185; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:temp; 138, integer:1; 139, for_statement; 139, 140; 139, 141; 139, 148; 140, identifier:j; 141, call; 141, 142; 141, 143; 142, identifier:range; 143, argument_list; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:len; 146, argument_list; 146, 147; 147, identifier:opt_list; 148, block; 148, 149; 149, if_statement; 149, 150; 149, 171; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:sp; 153, identifier:allclose; 154, argument_list; 154, 155; 154, 163; 155, call; 155, 156; 155, 157; 156, identifier:abs; 157, argument_list; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:getScales; 162, argument_list; 163, call; 163, 164; 163, 165; 164, identifier:abs; 165, argument_list; 165, 166; 166, subscript; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:opt_list; 169, identifier:j; 170, string:'scales'; 171, block; 171, 172; 171, 176; 171, 184; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:temp; 175, integer:0; 176, expression_statement; 176, 177; 177, augmented_assignment:+=; 177, 178; 177, 183; 178, subscript; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:opt_list; 181, identifier:j; 182, string:'counter'; 183, integer:1; 184, break_statement; 185, if_statement; 185, 186; 185, 189; 186, comparison_operator:==; 186, 187; 186, 188; 187, identifier:temp; 188, integer:1; 189, block; 189, 190; 189, 194; 189, 200; 189, 210; 189, 220; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:opt; 193, dictionary; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 199; 196, subscript; 196, 197; 196, 198; 197, identifier:opt; 198, string:'counter'; 199, integer:1; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:opt; 204, string:'LML'; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:getLML; 209, argument_list; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, subscript; 212, 213; 212, 214; 213, identifier:opt; 214, string:'scales'; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:getScales; 219, argument_list; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:opt_list; 224, identifier:append; 225, argument_list; 225, 226; 226, identifier:opt; 227, comment; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:LML; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:sp; 234, identifier:array; 235, argument_list; 235, 236; 236, list_comprehension; 236, 237; 236, 242; 237, subscript; 237, 238; 237, 241; 238, subscript; 238, 239; 238, 240; 239, identifier:opt_list; 240, identifier:i; 241, string:'LML'; 242, for_in_clause; 242, 243; 242, 244; 243, identifier:i; 244, call; 244, 245; 244, 246; 245, identifier:range; 246, argument_list; 246, 247; 247, call; 247, 248; 247, 249; 248, identifier:len; 249, argument_list; 249, 250; 250, identifier:opt_list; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:index; 254, subscript; 254, 255; 254, 260; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:LML; 258, identifier:argsort; 259, argument_list; 260, slice; 260, 261; 260, 262; 260, 263; 261, colon; 262, colon; 263, unary_operator:-; 263, 264; 264, integer:1; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:out; 268, list:[]; 269, if_statement; 269, 270; 269, 271; 270, identifier:verbose; 271, block; 271, 272; 271, 277; 271, 282; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 275; 274, identifier:print; 275, argument_list; 275, 276; 276, string:"\nLocal mimima\n"; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 280; 279, identifier:print; 280, argument_list; 280, 281; 281, string:"n_times\t\tLML"; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 285; 284, identifier:print; 285, argument_list; 285, 286; 286, string:"------------------------------------"; 287, for_statement; 287, 288; 287, 289; 287, 296; 288, identifier:i; 289, call; 289, 290; 289, 291; 290, identifier:range; 291, argument_list; 291, 292; 292, call; 292, 293; 292, 294; 293, identifier:len; 294, argument_list; 294, 295; 295, identifier:opt_list; 296, block; 296, 297; 296, 308; 297, expression_statement; 297, 298; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:out; 301, identifier:append; 302, argument_list; 302, 303; 303, subscript; 303, 304; 303, 305; 304, identifier:opt_list; 305, subscript; 305, 306; 305, 307; 306, identifier:index; 307, identifier:i; 308, if_statement; 308, 309; 308, 310; 309, identifier:verbose; 310, block; 310, 311; 310, 333; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 314; 313, identifier:print; 314, argument_list; 314, 315; 315, parenthesized_expression; 315, 316; 316, binary_operator:%; 316, 317; 316, 318; 317, string:"%d\t\t%f"; 318, tuple; 318, 319; 318, 326; 319, subscript; 319, 320; 319, 325; 320, subscript; 320, 321; 320, 322; 321, identifier:opt_list; 322, subscript; 322, 323; 322, 324; 323, identifier:index; 324, identifier:i; 325, string:'counter'; 326, subscript; 326, 327; 326, 332; 327, subscript; 327, 328; 327, 329; 328, identifier:opt_list; 329, subscript; 329, 330; 329, 331; 330, identifier:index; 331, identifier:i; 332, string:'LML'; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 336; 335, identifier:print; 336, argument_list; 336, 337; 337, string:""; 338, return_statement; 338, 339; 339, identifier:out
def optimize_with_repeates(self,fast=None,verbose=None,n_times=10,lambd=None,lambd_g=None,lambd_n=None): """ Train the model repeadly up to a number specified by the users with random restarts and return a list of all relative minima that have been found. This list is sorted according to least likelihood. Each list term is a dictionary with keys "counter", "LML", and "scales". After running this function, the vc object will be set at the last iteration. Thus, if you wish to get the vc object of one of the repeats, then set the scales. For example: vc.setScales(scales=optimize_with_repeates_output[0]["scales"]) Args: fast: Boolean. if set to True initalize kronSumGP verbose: Boolean. If set to True, verbose output is produced. (default True) n_times: number of re-starts of the optimization. (default 10) """ verbose = dlimix.getVerbose(verbose) if not self.init: self._initGP(fast) opt_list = [] fixed0 = sp.zeros_like(self.gp.getParams()['dataTerm']) # minimize n_times for i in range(n_times): scales1 = self._getScalesRand() fixed1 = 1e-1*sp.randn(fixed0.shape[0],fixed0.shape[1]) conv = self.trainGP(fast=fast,scales0=scales1,fixed0=fixed1,lambd=lambd,lambd_g=lambd_g,lambd_n=lambd_n) if conv: # compare with previous minima temp=1 for j in range(len(opt_list)): if sp.allclose(abs(self.getScales()),abs(opt_list[j]['scales'])): temp=0 opt_list[j]['counter']+=1 break if temp==1: opt = {} opt['counter'] = 1 opt['LML'] = self.getLML() opt['scales'] = self.getScales() opt_list.append(opt) # sort by LML LML = sp.array([opt_list[i]['LML'] for i in range(len(opt_list))]) index = LML.argsort()[::-1] out = [] if verbose: print("\nLocal mimima\n") print("n_times\t\tLML") print("------------------------------------") for i in range(len(opt_list)): out.append(opt_list[index[i]]) if verbose: print(("%d\t\t%f" % (opt_list[index[i]]['counter'], opt_list[index[i]]['LML']))) print("") return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:sort_prefixes; 3, parameters; 3, 4; 3, 5; 4, identifier:orig; 5, default_parameter; 5, 6; 5, 7; 6, identifier:prefixes; 7, string:'@+'; 8, block; 8, 9; 8, 11; 8, 15; 8, 28; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:new; 14, string:''; 15, for_statement; 15, 16; 15, 17; 15, 18; 16, identifier:prefix; 17, identifier:prefixes; 18, block; 18, 19; 19, if_statement; 19, 20; 19, 23; 20, comparison_operator:in; 20, 21; 20, 22; 21, identifier:prefix; 22, identifier:orig; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, augmented_assignment:+=; 25, 26; 25, 27; 26, identifier:new; 27, identifier:prefix; 28, return_statement; 28, 29; 29, identifier:new
def sort_prefixes(orig, prefixes='@+'): """Returns a sorted list of prefixes. Args: orig (str): Unsorted list of prefixes. prefixes (str): List of prefixes, from highest-priv to lowest. """ new = '' for prefix in prefixes: if prefix in orig: new += prefix return new
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:sort_children; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:attribute; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:reverse_order; 10, False; 11, block; 11, 12; 11, 14; 11, 18; 11, 115; 11, 121; 11, 135; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:sorted_children; 17, list:[]; 18, if_statement; 18, 19; 18, 20; 18, 90; 19, identifier:attribute; 20, block; 20, 21; 20, 25; 20, 29; 20, 59; 20, 83; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:sortable_children; 24, list:[]; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:unsortable_children; 28, list:[]; 29, for_statement; 29, 30; 29, 31; 29, 34; 30, identifier:child; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:__children; 34, block; 34, 35; 35, if_statement; 35, 36; 35, 42; 35, 50; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:child; 39, identifier:attribute_exists; 40, argument_list; 40, 41; 41, identifier:attribute; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:sortable_children; 47, identifier:append; 48, argument_list; 48, 49; 49, identifier:child; 50, else_clause; 50, 51; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:unsortable_children; 56, identifier:append; 57, argument_list; 57, 58; 58, identifier:child; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:sorted_children; 62, call; 62, 63; 62, 64; 63, identifier:sorted; 64, argument_list; 64, 65; 64, 66; 64, 78; 65, identifier:sortable_children; 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, attribute; 71, 72; 71, 77; 72, call; 72, 73; 72, 74; 73, identifier:getattr; 74, argument_list; 74, 75; 74, 76; 75, identifier:x; 76, identifier:attribute; 77, identifier:value; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:reverse; 80, boolean_operator:or; 80, 81; 80, 82; 81, identifier:reverse_order; 82, False; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:sorted_children; 87, identifier:extend; 88, argument_list; 88, 89; 89, identifier:unsortable_children; 90, else_clause; 90, 91; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:sorted_children; 95, call; 95, 96; 95, 97; 96, identifier:sorted; 97, argument_list; 97, 98; 97, 101; 97, 110; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:children; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:key; 103, lambda; 103, 104; 103, 106; 104, lambda_parameters; 104, 105; 105, identifier:x; 106, parenthesized_expression; 106, 107; 107, attribute; 107, 108; 107, 109; 108, identifier:x; 109, identifier:name; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:reverse; 112, boolean_operator:or; 112, 113; 112, 114; 113, identifier:reverse_order; 114, False; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:__children; 120, identifier:sorted_children; 121, for_statement; 121, 122; 121, 123; 121, 126; 122, identifier:child; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:__children; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:child; 131, identifier:sort_children; 132, argument_list; 132, 133; 132, 134; 133, identifier:attribute; 134, identifier:reverse_order; 135, return_statement; 135, 136; 136, True
def sort_children(self, attribute=None, reverse_order=False): """ Sorts the children using either the given attribute or the Node name. :param attribute: Attribute name used for sorting. :type attribute: unicode :param reverse_order: Sort in reverse order. :type reverse_order: bool :return: Method success. :rtype: bool """ sorted_children = [] if attribute: sortable_children = [] unsortable_children = [] for child in self.__children: if child.attribute_exists(attribute): sortable_children.append(child) else: unsortable_children.append(child) sorted_children = sorted(sortable_children, key=lambda x: getattr( x, attribute).value, reverse=reverse_order or False) sorted_children.extend(unsortable_children) else: sorted_children = sorted(self.children, key=lambda x: (x.name), reverse=reverse_order or False) self.__children = sorted_children for child in self.__children: child.sort_children(attribute, reverse_order) return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_sort_kw_matches; 3, parameters; 3, 4; 3, 5; 4, identifier:skw_matches; 5, default_parameter; 5, 6; 5, 7; 6, identifier:limit; 7, integer:0; 8, block; 8, 9; 8, 11; 8, 22; 8, 33; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:sorted_keywords; 14, call; 14, 15; 14, 16; 15, identifier:list; 16, argument_list; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:skw_matches; 20, identifier:items; 21, argument_list; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:sorted; 25, argument_list; 25, 26; 25, 27; 26, identifier:sorted_keywords; 27, keyword_argument; 27, 28; 27, 29; 28, identifier:key; 29, call; 29, 30; 29, 31; 30, identifier:cmp_to_key; 31, argument_list; 31, 32; 32, identifier:_skw_matches_comparator; 33, return_statement; 33, 34; 34, boolean_operator:or; 34, 35; 34, 42; 35, boolean_operator:and; 35, 36; 35, 37; 36, identifier:limit; 37, subscript; 37, 38; 37, 39; 38, identifier:sorted_keywords; 39, slice; 39, 40; 39, 41; 40, colon; 41, identifier:limit; 42, identifier:sorted_keywords
def _sort_kw_matches(skw_matches, limit=0): """Return a resized version of keywords to the given length.""" sorted_keywords = list(skw_matches.items()) sorted(sorted_keywords, key=cmp_to_key(_skw_matches_comparator)) return limit and sorted_keywords[:limit] or sorted_keywords
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:trim; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:n_peaks; 6, block; 6, 7; 6, 9; 6, 15; 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:self; 13, identifier:sortByIntensity; 14, argument_list; 15, expression_statement; 15, 16; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:ims; 19, identifier:spectrum_trim; 20, argument_list; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:ptr; 24, identifier:n_peaks
def trim(self, n_peaks): """ Sorts mass and intensities arrays in descending intensity order, then removes low-intensity peaks from the spectrum. :param n_peaks: number of peaks to keep """ self.sortByIntensity() ims.spectrum_trim(self.ptr, n_peaks)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:centroids; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:window_size; 7, integer:5; 8, block; 8, 9; 8, 11; 8, 17; 8, 27; 8, 37; 8, 43; 8, 62; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:sortByMass; 16, argument_list; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:mzs; 20, call; 20, 21; 20, 22; 21, identifier:_cffi_buffer; 22, argument_list; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:masses; 26, string:'d'; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:intensities; 30, call; 30, 31; 30, 32; 31, identifier:_cffi_buffer; 32, argument_list; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:intensities; 36, string:'f'; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:n; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:size; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:p; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:ims; 49, identifier:spectrum_new_from_raw; 50, argument_list; 50, 51; 50, 52; 50, 55; 50, 58; 51, identifier:n; 52, attribute; 52, 53; 52, 54; 53, identifier:mzs; 54, identifier:ptr; 55, attribute; 55, 56; 55, 57; 56, identifier:intensities; 57, identifier:ptr; 58, call; 58, 59; 58, 60; 59, identifier:int; 60, argument_list; 60, 61; 61, identifier:window_size; 62, return_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:_new_spectrum; 65, argument_list; 65, 66; 65, 67; 66, identifier:CentroidedSpectrum; 67, identifier:p
def centroids(self, window_size=5): """ Detects peaks in raw data. :param mzs: sorted array of m/z values :param intensities: array of corresponding intensities :param window_size: size of m/z averaging window :returns: isotope pattern containing the centroids :rtype: CentroidedSpectrum """ self.sortByMass() mzs = _cffi_buffer(self.masses, 'd') intensities = _cffi_buffer(self.intensities, 'f') n = self.size p = ims.spectrum_new_from_raw(n, mzs.ptr, intensities.ptr, int(window_size)) return _new_spectrum(CentroidedSpectrum, p)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:list_engines_by_priority; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:engines; 6, None; 7, block; 7, 8; 7, 10; 7, 19; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 14; 11, comparison_operator:is; 11, 12; 11, 13; 12, identifier:engines; 13, None; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:engines; 18, identifier:ENGINES; 19, return_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:sorted; 22, argument_list; 22, 23; 22, 24; 23, identifier:engines; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:key; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:operator; 29, identifier:methodcaller; 30, argument_list; 30, 31; 31, string:"priority"
def list_engines_by_priority(engines=None): """ Return a list of engines supported sorted by each priority. """ if engines is None: engines = ENGINES return sorted(engines, key=operator.methodcaller("priority"))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:union; 3, parameters; 3, 4; 3, 5; 4, identifier:dict1; 5, identifier:dict2; 6, block; 6, 7; 6, 9; 6, 50; 7, expression_statement; 7, 8; 8, comment; 9, for_statement; 9, 10; 9, 13; 9, 18; 10, pattern_list; 10, 11; 10, 12; 11, identifier:key; 12, identifier:value; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:dict2; 16, identifier:items; 17, argument_list; 18, block; 18, 19; 19, if_statement; 19, 20; 19, 29; 19, 42; 20, boolean_operator:and; 20, 21; 20, 24; 21, comparison_operator:in; 21, 22; 21, 23; 22, identifier:key; 23, identifier:dict1; 24, call; 24, 25; 24, 26; 25, identifier:isinstance; 26, argument_list; 26, 27; 26, 28; 27, identifier:value; 28, identifier:dict; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:dict1; 34, identifier:key; 35, call; 35, 36; 35, 37; 36, identifier:union; 37, argument_list; 37, 38; 37, 41; 38, subscript; 38, 39; 38, 40; 39, identifier:dict1; 40, identifier:key; 41, identifier:value; 42, else_clause; 42, 43; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:dict1; 48, identifier:key; 49, identifier:value; 50, return_statement; 50, 51; 51, identifier:dict1
def union(dict1, dict2): """ Deep merge of dict2 into dict1. May be dictionaries or dictobj's. Values in dict2 will replace values in dict1 where they vary but have the same key. When lists are encountered, the dict2 list will replace the dict1 list. This will alter the first dictionary. The returned result is dict1, but it will have references to both dictionaries. If you do not want this, use union_copy(), which is less efficient but data-safe. >>> a = dict(a=dict(b=dict(c=1))) >>> b = dict(a=dict(b=dict(d=2)),e=[1,2]) >>> # sorted json so that it is predictably the same >>> import json >>> json.dumps(union(a, b), sort_keys=True) '{"a": {"b": {"c": 1, "d": 2}}, "e": [1, 2]}' >>> json.dumps(a, sort_keys=True) '{"a": {"b": {"c": 1, "d": 2}}, "e": [1, 2]}' >>> json.dumps(b, sort_keys=True) '{"a": {"b": {"d": 2}}, "e": [1, 2]}' >>> a['e'][0] = 3 >>> json.dumps(a, sort_keys=True) '{"a": {"b": {"c": 1, "d": 2}}, "e": [3, 2]}' >>> json.dumps(b, sort_keys=True) '{"a": {"b": {"d": 2}}, "e": [3, 2]}' """ for key, value in dict2.items(): if key in dict1 and isinstance(value, dict): dict1[key] = union(dict1[key], value) else: dict1[key] = value return dict1
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_format_data; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:data; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, return_statement; 9, 10; 10, list_comprehension; 10, 11; 10, 12; 10, 39; 11, identifier:spectrum; 12, for_in_clause; 12, 13; 12, 14; 12, 15; 13, identifier:spectrum; 14, line_continuation:\; 15, call; 15, 16; 15, 17; 16, identifier:sorted; 17, argument_list; 17, 18; 17, 29; 18, conditional_expression:if; 18, 19; 18, 20; 18, 27; 19, identifier:data; 20, call; 20, 21; 20, 22; 21, identifier:isinstance; 22, argument_list; 22, 23; 22, 24; 23, identifier:data; 24, tuple; 24, 25; 24, 26; 25, identifier:list; 26, identifier:tuple; 27, list:[data]; 27, 28; 28, identifier:data; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:key; 31, lambda; 31, 32; 31, 34; 32, lambda_parameters; 32, 33; 33, identifier:x; 34, subscript; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:x; 37, identifier:disp; 38, integer:0; 39, if_clause; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:np; 43, identifier:any; 44, argument_list; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:np; 48, identifier:isfinite; 49, argument_list; 49, 50; 50, attribute; 50, 51; 50, 52; 51, identifier:spectrum; 52, identifier:flux
def _format_data(self, data): """ Sort the data in blue wavelengths to red, and ignore any spectra that have entirely non-finite or negative fluxes. """ return [spectrum for spectrum in \ sorted(data if isinstance(data, (list, tuple)) else [data], key=lambda x: x.disp[0]) if np.any(np.isfinite(spectrum.flux))]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_sorting; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:request; 6, dictionary_splat_pattern; 6, 7; 7, identifier:resources; 8, block; 8, 9; 8, 11; 8, 15; 8, 23; 8, 33; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:sorting; 14, list:[]; 15, if_statement; 15, 16; 15, 20; 16, not_operator; 16, 17; 17, attribute; 17, 18; 17, 19; 18, identifier:request; 19, identifier:GET; 20, block; 20, 21; 21, return_statement; 21, 22; 22, identifier:sorting; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:prefix; 26, binary_operator:+; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:_meta; 31, identifier:dyn_prefix; 32, string:'sort'; 33, return_statement; 33, 34; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:request; 38, identifier:GET; 39, identifier:getlist; 40, argument_list; 40, 41; 41, identifier:prefix
def get_sorting(self, request, **resources): """ Get sorting options. :return list: sorting order """ sorting = [] if not request.GET: return sorting prefix = self._meta.dyn_prefix + 'sort' return request.GET.getlist(prefix)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:sort_tuples; 7, block; 7, 8; 7, 10; 7, 14; 7, 72; 8, expression_statement; 8, 9; 9, string:''' pymongo-style sorting. Accepts a list of tuples. :param sort_tuples: varargs of sort tuples. '''; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:query; 13, identifier:self; 14, for_statement; 14, 15; 14, 18; 14, 19; 15, pattern_list; 15, 16; 15, 17; 16, identifier:name; 17, identifier:direction; 18, identifier:sort_tuples; 19, block; 19, 20; 19, 30; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:field; 23, call; 23, 24; 23, 25; 24, identifier:resolve_name; 25, argument_list; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:type; 29, identifier:name; 30, if_statement; 30, 31; 30, 36; 30, 46; 30, 63; 31, comparison_operator:in; 31, 32; 31, 33; 32, identifier:direction; 33, tuple; 33, 34; 33, 35; 34, identifier:ASCENDING; 35, integer:1; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:query; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:query; 43, identifier:ascending; 44, argument_list; 44, 45; 45, identifier:field; 46, elif_clause; 46, 47; 46, 53; 47, comparison_operator:in; 47, 48; 47, 49; 48, identifier:direction; 49, tuple; 49, 50; 49, 51; 50, identifier:DESCENDING; 51, unary_operator:-; 51, 52; 52, integer:1; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:query; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:query; 60, identifier:descending; 61, argument_list; 61, 62; 62, identifier:field; 63, else_clause; 63, 64; 64, block; 64, 65; 65, raise_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:BadQueryException; 68, argument_list; 68, 69; 69, binary_operator:%; 69, 70; 69, 71; 70, string:'Bad sort direction: %s'; 71, identifier:direction; 72, return_statement; 72, 73; 73, identifier:query
def sort(self, *sort_tuples): ''' pymongo-style sorting. Accepts a list of tuples. :param sort_tuples: varargs of sort tuples. ''' query = self for name, direction in sort_tuples: field = resolve_name(self.type, name) if direction in (ASCENDING, 1): query = query.ascending(field) elif direction in (DESCENDING, -1): query = query.descending(field) else: raise BadQueryException('Bad sort direction: %s' % direction) return query
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_has_bad_coords; 3, parameters; 3, 4; 3, 5; 4, identifier:root; 5, identifier:stream; 6, block; 6, 7; 6, 9; 6, 16; 6, 31; 6, 40; 6, 57; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 10, comparison_operator:==; 10, 11; 10, 12; 11, identifier:stream; 12, string:"com.dc3/dc3.broker"; 13, block; 13, 14; 14, return_statement; 14, 15; 15, True; 16, if_statement; 16, 17; 16, 28; 17, not_operator; 17, 18; 18, comparison_operator:==; 18, 19; 18, 27; 19, subscript; 19, 20; 19, 26; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:stream; 23, identifier:split; 24, argument_list; 24, 25; 25, string:'/'; 26, integer:0; 27, string:'nasa.gsfc.gcn'; 28, block; 28, 29; 29, return_statement; 29, 30; 30, False; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:toplevel_params; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:vp; 37, identifier:get_toplevel_params; 38, argument_list; 38, 39; 39, identifier:root; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:in; 41, 42; 41, 43; 42, string:"Coords_String"; 43, identifier:toplevel_params; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 54; 46, parenthesized_expression; 46, 47; 47, comparison_operator:==; 47, 48; 47, 53; 48, subscript; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:toplevel_params; 51, string:"Coords_String"; 52, string:'value'; 53, string:"unavailable/inappropriate"; 54, block; 54, 55; 55, return_statement; 55, 56; 56, True; 57, return_statement; 57, 58; 58, False
def _has_bad_coords(root, stream): """ Predicate function encapsulating 'data clean up' filter code. Currently minimal, but these sort of functions tend to grow over time. Problem 1: Some of the GCN packets have an RA /Dec equal to (0,0) in the WhereWhen, and a flag in the What signifying that those are actually dummy co-ords. (This is used for time-stamping an event which is not localised). So, we don't load those positions, to avoid muddying the database corpus. Problem 2: com.dc3/dc3.broker#BrokerTest packets have dummy RA/Dec values, with no units specified. (They're also marked role=test, so it's not such a big deal, but it generates a lot of debug-log churn.) """ if stream == "com.dc3/dc3.broker": return True if not stream.split('/')[0] == 'nasa.gsfc.gcn': return False toplevel_params = vp.get_toplevel_params(root) if "Coords_String" in toplevel_params: if (toplevel_params["Coords_String"]['value'] == "unavailable/inappropriate"): return True return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 1, 31; 2, function_name:sorted_chain; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 7; 5, list_splat_pattern; 5, 6; 6, identifier:ranges; 7, type; 7, 8; 8, generic_type; 8, 9; 8, 10; 9, identifier:Iterable; 10, type_parameter; 10, 11; 11, type; 11, 12; 12, generic_type; 12, 13; 12, 14; 13, identifier:Tuple; 14, type_parameter; 14, 15; 14, 17; 15, type; 15, 16; 16, identifier:int; 17, type; 17, 18; 18, identifier:int; 19, type; 19, 20; 20, generic_type; 20, 21; 20, 22; 21, identifier:List; 22, type_parameter; 22, 23; 23, type; 23, 24; 24, generic_type; 24, 25; 24, 26; 25, identifier:Tuple; 26, type_parameter; 26, 27; 26, 29; 27, type; 27, 28; 28, identifier:int; 29, type; 29, 30; 30, identifier:int; 31, block; 31, 32; 31, 34; 32, expression_statement; 32, 33; 33, comment; 34, return_statement; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:sorted; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:itertools; 41, identifier:chain; 42, argument_list; 42, 43; 43, list_splat; 43, 44; 44, identifier:ranges
def sorted_chain(*ranges: Iterable[Tuple[int, int]]) -> List[Tuple[int, int]]: """Chain & sort ranges.""" return sorted(itertools.chain(*ranges))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:partition_range; 3, parameters; 3, 4; 3, 5; 4, identifier:stop; 5, default_parameter; 5, 6; 5, 7; 6, identifier:annotations; 7, None; 8, block; 8, 9; 8, 11; 8, 17; 8, 21; 8, 29; 8, 30; 8, 31; 8, 132; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:annotations; 14, boolean_operator:or; 14, 15; 14, 16; 15, identifier:annotations; 16, list:[]; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:partitioning; 20, list:[]; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 26; 23, pattern_list; 23, 24; 23, 25; 24, identifier:part_start; 25, identifier:part_levels; 26, expression_list; 26, 27; 26, 28; 27, integer:0; 28, None; 29, comment; 30, comment; 31, for_statement; 31, 32; 31, 33; 31, 55; 32, identifier:p; 33, call; 33, 34; 33, 35; 34, identifier:sorted; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:set; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:itertools; 42, identifier:chain; 43, argument_list; 43, 44; 43, 47; 44, list:[0, stop]; 44, 45; 44, 46; 45, integer:0; 46, identifier:stop; 47, list_splat; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:itertools; 51, identifier:chain; 52, argument_list; 52, 53; 53, list_splat; 53, 54; 54, identifier:annotations; 55, block; 55, 56; 55, 72; 55, 73; 55, 99; 55, 109; 56, if_statement; 56, 57; 56, 60; 57, comparison_operator:==; 57, 58; 57, 59; 58, identifier:p; 59, identifier:stop; 60, block; 60, 61; 60, 71; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:partitioning; 65, identifier:append; 66, argument_list; 66, 67; 67, tuple; 67, 68; 67, 69; 67, 70; 68, identifier:part_start; 69, identifier:p; 70, identifier:part_levels; 71, break_statement; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:levels; 76, set_comprehension; 76, 77; 76, 78; 76, 86; 77, identifier:level; 78, for_in_clause; 78, 79; 78, 82; 79, pattern_list; 79, 80; 79, 81; 80, identifier:level; 81, identifier:regions; 82, call; 82, 83; 82, 84; 83, identifier:enumerate; 84, argument_list; 84, 85; 85, identifier:annotations; 86, if_clause; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:any; 89, generator_expression; 89, 90; 89, 94; 90, comparison_operator:<=; 90, 91; 90, 92; 90, 93; 91, identifier:x; 92, identifier:p; 93, identifier:y; 94, for_in_clause; 94, 95; 94, 98; 95, pattern_list; 95, 96; 95, 97; 96, identifier:x; 97, identifier:y; 98, identifier:regions; 99, if_statement; 99, 100; 99, 103; 100, comparison_operator:==; 100, 101; 100, 102; 101, identifier:p; 102, integer:0; 103, block; 103, 104; 103, 108; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:part_levels; 107, identifier:levels; 108, continue_statement; 109, if_statement; 109, 110; 109, 113; 110, comparison_operator:!=; 110, 111; 110, 112; 111, identifier:levels; 112, identifier:part_levels; 113, block; 113, 114; 113, 124; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:partitioning; 118, identifier:append; 119, argument_list; 119, 120; 120, tuple; 120, 121; 120, 122; 120, 123; 121, identifier:part_start; 122, identifier:p; 123, identifier:part_levels; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, pattern_list; 126, 127; 126, 128; 127, identifier:part_start; 128, identifier:part_levels; 129, expression_list; 129, 130; 129, 131; 130, identifier:p; 131, identifier:levels; 132, return_statement; 132, 133; 133, identifier:partitioning
def partition_range(stop, annotations=None): """ Partition the range from 0 to `stop` based on annotations. >>> partition_range(50, annotations=[[(0, 21), (30, 35)], ... [(15, 32), (40, 46)]]) [(0, 15, {0}), (15, 21, {0, 1}), (21, 30, {1}), (30, 32, {0, 1}), (32, 35, {0}), (35, 40, set()), (40, 46, {1}), (46, 50, set())] :arg stop: End point (not included) of the range (similar to the `stop` argument of the built-in :func:`range` function). :type stop: int :arg annotations: For each annotation level, a list of (`start`, `stop`) pairs defining an annotated region. :type annotations: list :return: Partitioning of the range as (`start`, `stop`, `levels`) tuples defining a region with a set of annotation levels. :rtype: list All regions (`start`, `stop`) are defined as in slicing notation, so zero-based and `stop` is not included. The `annotations` argument is a list of annotations. An annotation is a list of regions as (`start`, `stop`) tuples. The level of each annotation is its index in `annotations`. Annotation regions can overlap (overlap within one level is ignored) and do not need to be sorted. """ annotations = annotations or [] partitioning = [] part_start, part_levels = 0, None # We loop over the range, only touching positions where levels potentially # change. for p in sorted(set(itertools.chain([0, stop], *itertools.chain(*annotations)))): if p == stop: partitioning.append( (part_start, p, part_levels) ) break # Annotation levels for position p. levels = {level for level, regions in enumerate(annotations) if any(x <= p < y for x, y in regions)} if p == 0: part_levels = levels continue if levels != part_levels: partitioning.append( (part_start, p, part_levels) ) part_start, part_levels = p, levels return partitioning
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:pprint_sequence; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:sequence; 5, default_parameter; 5, 6; 5, 7; 6, identifier:annotations; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:block_length; 10, integer:10; 11, default_parameter; 11, 12; 11, 13; 12, identifier:blocks_per_line; 13, integer:6; 14, default_parameter; 14, 15; 14, 16; 15, identifier:format; 16, identifier:PlaintextFormat; 17, block; 17, 18; 17, 20; 17, 26; 17, 37; 17, 38; 17, 39; 17, 75; 17, 99; 17, 276; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:annotations; 23, boolean_operator:or; 23, 24; 23, 25; 24, identifier:annotations; 25, list:[]; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:partitioning; 29, call; 29, 30; 29, 31; 30, identifier:partition_range; 31, argument_list; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:len; 34, argument_list; 34, 35; 35, identifier:sequence; 36, identifier:annotations; 37, comment; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:margin; 42, binary_operator:+; 42, 43; 42, 67; 43, call; 43, 44; 43, 45; 44, identifier:int; 45, argument_list; 45, 46; 46, binary_operator:+; 46, 47; 46, 66; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:math; 50, identifier:floor; 51, argument_list; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:math; 55, identifier:log; 56, argument_list; 56, 57; 56, 65; 57, call; 57, 58; 57, 59; 58, identifier:max; 59, argument_list; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:sequence; 64, integer:1; 65, integer:10; 66, integer:1; 67, call; 67, 68; 67, 69; 68, identifier:len; 69, argument_list; 69, 70; 70, subscript; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:format; 73, identifier:margin; 74, integer:0; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:result; 78, binary_operator:+; 78, 79; 78, 98; 79, binary_operator:+; 79, 80; 79, 93; 80, call; 80, 81; 80, 91; 81, attribute; 81, 82; 81, 90; 82, parenthesized_expression; 82, 83; 83, binary_operator:+; 83, 84; 83, 89; 84, subscript; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:format; 87, identifier:margin; 88, integer:0; 89, string:'1'; 90, identifier:rjust; 91, argument_list; 91, 92; 92, identifier:margin; 93, subscript; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:format; 96, identifier:margin; 97, integer:1; 98, string:' '; 99, for_statement; 99, 100; 99, 101; 99, 110; 99, 111; 100, identifier:p; 101, call; 101, 102; 101, 103; 102, identifier:range; 103, argument_list; 103, 104; 103, 105; 103, 109; 104, integer:0; 105, call; 105, 106; 105, 107; 106, identifier:len; 107, argument_list; 107, 108; 108, identifier:sequence; 109, identifier:block_length; 110, comment; 111, block; 111, 112; 111, 146; 111, 150; 111, 220; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:block; 115, list_comprehension; 115, 116; 115, 130; 115, 136; 116, tuple; 116, 117; 116, 122; 116, 129; 117, call; 117, 118; 117, 119; 118, identifier:max; 119, argument_list; 119, 120; 119, 121; 120, identifier:start; 121, identifier:p; 122, call; 122, 123; 122, 124; 123, identifier:min; 124, argument_list; 124, 125; 124, 126; 125, identifier:stop; 126, binary_operator:+; 126, 127; 126, 128; 127, identifier:p; 128, identifier:block_length; 129, identifier:levels; 130, for_in_clause; 130, 131; 130, 135; 131, pattern_list; 131, 132; 131, 133; 131, 134; 132, identifier:start; 133, identifier:stop; 134, identifier:levels; 135, identifier:partitioning; 136, if_clause; 136, 137; 137, boolean_operator:and; 137, 138; 137, 143; 138, comparison_operator:<; 138, 139; 138, 140; 139, identifier:start; 140, binary_operator:+; 140, 141; 140, 142; 141, identifier:p; 142, identifier:block_length; 143, comparison_operator:>; 143, 144; 143, 145; 144, identifier:stop; 145, identifier:p; 146, expression_statement; 146, 147; 147, augmented_assignment:+=; 147, 148; 147, 149; 148, identifier:result; 149, string:' '; 150, for_statement; 150, 151; 150, 155; 150, 156; 151, pattern_list; 151, 152; 151, 153; 151, 154; 152, identifier:start; 153, identifier:stop; 154, identifier:levels; 155, identifier:block; 156, block; 156, 157; 156, 180; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:delimiters; 160, list_comprehension; 160, 161; 160, 164; 160, 176; 161, tuple; 161, 162; 161, 163; 162, identifier:left; 163, identifier:right; 164, for_in_clause; 164, 165; 164, 170; 165, pattern_list; 165, 166; 165, 167; 166, identifier:level; 167, tuple_pattern; 167, 168; 167, 169; 168, identifier:left; 169, identifier:right; 170, call; 170, 171; 170, 172; 171, identifier:enumerate; 172, argument_list; 172, 173; 173, attribute; 173, 174; 173, 175; 174, identifier:format; 175, identifier:annotations; 176, if_clause; 176, 177; 177, comparison_operator:in; 177, 178; 177, 179; 178, identifier:level; 179, identifier:levels; 180, expression_statement; 180, 181; 181, augmented_assignment:+=; 181, 182; 181, 183; 182, identifier:result; 183, parenthesized_expression; 183, 184; 184, binary_operator:+; 184, 185; 184, 209; 185, binary_operator:+; 185, 186; 185, 200; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, string:''; 189, identifier:join; 190, generator_expression; 190, 191; 190, 192; 191, identifier:left; 192, for_in_clause; 192, 193; 192, 196; 193, pattern_list; 193, 194; 193, 195; 194, identifier:left; 195, identifier:right; 196, call; 196, 197; 196, 198; 197, identifier:reversed; 198, argument_list; 198, 199; 199, identifier:delimiters; 200, call; 200, 201; 200, 202; 201, identifier:str; 202, argument_list; 202, 203; 203, subscript; 203, 204; 203, 205; 204, identifier:sequence; 205, slice; 205, 206; 205, 207; 205, 208; 206, identifier:start; 207, colon; 208, identifier:stop; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, string:''; 212, identifier:join; 213, generator_expression; 213, 214; 213, 215; 214, identifier:right; 215, for_in_clause; 215, 216; 215, 219; 216, pattern_list; 216, 217; 216, 218; 217, identifier:left; 218, identifier:right; 219, identifier:delimiters; 220, if_statement; 220, 221; 220, 241; 221, parenthesized_expression; 221, 222; 222, boolean_operator:and; 222, 223; 222, 233; 223, not_operator; 223, 224; 224, binary_operator:%; 224, 225; 224, 229; 225, parenthesized_expression; 225, 226; 226, binary_operator:+; 226, 227; 226, 228; 227, identifier:p; 228, identifier:block_length; 229, parenthesized_expression; 229, 230; 230, binary_operator:*; 230, 231; 230, 232; 231, identifier:block_length; 232, identifier:blocks_per_line; 233, comparison_operator:<; 233, 234; 233, 237; 234, binary_operator:+; 234, 235; 234, 236; 235, identifier:p; 236, identifier:block_length; 237, call; 237, 238; 237, 239; 238, identifier:len; 239, argument_list; 239, 240; 240, identifier:sequence; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, augmented_assignment:+=; 243, 244; 243, 245; 244, identifier:result; 245, parenthesized_expression; 245, 246; 246, binary_operator:+; 246, 247; 246, 275; 247, binary_operator:+; 247, 248; 247, 270; 248, binary_operator:+; 248, 249; 248, 250; 249, string:'\n'; 250, call; 250, 251; 250, 268; 251, attribute; 251, 252; 251, 267; 252, parenthesized_expression; 252, 253; 253, binary_operator:+; 253, 254; 253, 259; 254, subscript; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:format; 257, identifier:margin; 258, integer:0; 259, call; 259, 260; 259, 261; 260, identifier:str; 261, argument_list; 261, 262; 262, binary_operator:+; 262, 263; 262, 266; 263, binary_operator:+; 263, 264; 263, 265; 264, identifier:p; 265, identifier:block_length; 266, integer:1; 267, identifier:rjust; 268, argument_list; 268, 269; 269, identifier:margin; 270, subscript; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:format; 273, identifier:margin; 274, integer:1; 275, string:' '; 276, return_statement; 276, 277; 277, identifier:result
def pprint_sequence(sequence, annotations=None, block_length=10, blocks_per_line=6, format=PlaintextFormat): """ Pretty-print sequence for use with a monospace font. >>> sequence = 'MIMANQPLWLDSEVEMNHYQQSHIKSKSPYFPEDKHICWIKIFKAFGT' * 4 >>> print pprint_sequence(sequence, format=PlaintextFormat) 1 MIMANQPLWL DSEVEMNHYQ QSHIKSKSPY FPEDKHICWI KIFKAFGTMI MANQPLWLDS 61 EVEMNHYQQS HIKSKSPYFP EDKHICWIKI FKAFGTMIMA NQPLWLDSEV EMNHYQQSHI 121 KSKSPYFPED KHICWIKIFK AFGTMIMANQ PLWLDSEVEM NHYQQSHIKS KSPYFPEDKH 181 ICWIKIFKAF GT :arg sequence: Sequence to pretty-print. :type sequence: str or any sliceable yielding slices representable as strings. :arg annotations: For each annotation level, a list of (`start`, `stop`) pairs defining an annotated region. :type annotations: list :arg block_length: Length of space-separated blocks. :type block_length: int :arg blocks_per_line: Number of blocks per line. :type blocks_per_line: int :arg format: Output format to use for pretty-printing. Some formats are pre-defined as :data:`HtmlFormat`, :data:`AnsiFormat`, and :data:`PlaintextFormat`. :type format: :class:`Format` :return: Pretty-printed version of `sequence`. :rtype: str All regions (`start`, `stop`) are defined as in slicing notation, so zero-based and `stop` is not included. The `annotations` argument is a list of annotations. An annotation is a list of regions as (`start`, `stop`) tuples. The level of each annotation is its index in `annotations`. Annotation regions can overlap (overlap within one level is ignored) and do not need to be sorted. The number of annotation levels supported depends on `format`. :data:`HtmlFormat` supports 10 levels, :data:`AnsiFormat` supports 3 levels and annotations are ignored completely with :data:`PlaintextFormat`. """ annotations = annotations or [] partitioning = partition_range(len(sequence), annotations) # The maximum length for positions is the 10_log of the length of the # sequence. margin = int(math.floor(math.log(max(len(sequence), 1), 10)) + 1) + len(format.margin[0]) result = (format.margin[0] + '1').rjust(margin) + format.margin[1] + ' ' for p in range(0, len(sequence), block_length): # Partitioning of the block starting at position p. block = [(max(start, p), min(stop, p + block_length), levels) for start, stop, levels in partitioning if start < p + block_length and stop > p] result += ' ' for start, stop, levels in block: delimiters = [(left, right) for level, (left, right) in enumerate(format.annotations) if level in levels] result += (''.join(left for left, right in reversed(delimiters)) + str(sequence[start:stop]) + ''.join(right for left, right in delimiters)) if (not (p + block_length) % (block_length * blocks_per_line) and p + block_length < len(sequence)): result += ('\n' + (format.margin[0] + str(p + block_length + 1)).rjust(margin) + format.margin[1] + ' ') return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:tabulate; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:obj; 5, default_parameter; 5, 6; 5, 7; 6, identifier:v_level_indexes; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:h_level_indexes; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:v_level_visibility; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:h_level_visibility; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:v_level_sort_keys; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:h_level_sort_keys; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:v_level_titles; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:h_level_titles; 28, None; 29, default_parameter; 29, 30; 29, 31; 30, identifier:empty; 31, string:""; 32, block; 32, 33; 32, 35; 32, 42; 32, 56; 32, 71; 32, 86; 32, 101; 32, 111; 32, 121; 32, 129; 32, 137; 33, expression_statement; 33, 34; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:level_keys; 38, call; 38, 39; 38, 40; 39, identifier:breadth_first; 40, argument_list; 40, 41; 41, identifier:obj; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 47; 44, pattern_list; 44, 45; 44, 46; 45, identifier:v_level_indexes; 46, identifier:h_level_indexes; 47, call; 47, 48; 47, 49; 48, identifier:validate_level_indexes; 49, argument_list; 49, 50; 49, 54; 49, 55; 50, call; 50, 51; 50, 52; 51, identifier:len; 52, argument_list; 52, 53; 53, identifier:level_keys; 54, identifier:v_level_indexes; 55, identifier:h_level_indexes; 56, if_statement; 56, 57; 56, 60; 57, comparison_operator:is; 57, 58; 57, 59; 58, identifier:v_level_visibility; 59, None; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:v_level_visibility; 64, binary_operator:*; 64, 65; 64, 67; 65, list:[True]; 65, 66; 66, True; 67, call; 67, 68; 67, 69; 68, identifier:len; 69, argument_list; 69, 70; 70, identifier:v_level_indexes; 71, if_statement; 71, 72; 71, 75; 72, comparison_operator:is; 72, 73; 72, 74; 73, identifier:h_level_visibility; 74, None; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:h_level_visibility; 79, binary_operator:*; 79, 80; 79, 82; 80, list:[True]; 80, 81; 81, True; 82, call; 82, 83; 82, 84; 83, identifier:len; 84, argument_list; 84, 85; 85, identifier:h_level_indexes; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 92; 88, pattern_list; 88, 89; 88, 90; 88, 91; 89, identifier:table; 90, identifier:v_key_tuples; 91, identifier:h_key_tuples; 92, call; 92, 93; 92, 94; 93, identifier:tabulate_body; 94, argument_list; 94, 95; 94, 96; 94, 97; 94, 98; 94, 99; 94, 100; 95, identifier:obj; 96, identifier:level_keys; 97, identifier:v_level_indexes; 98, identifier:h_level_indexes; 99, identifier:v_level_sort_keys; 100, identifier:h_level_sort_keys; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, pattern_list; 103, 104; 103, 105; 104, identifier:table; 105, identifier:v_key_tuples; 106, call; 106, 107; 106, 108; 107, identifier:strip_missing_rows; 108, argument_list; 108, 109; 108, 110; 109, identifier:table; 110, identifier:v_key_tuples; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, pattern_list; 113, 114; 113, 115; 114, identifier:table; 115, identifier:h_key_tuples; 116, call; 116, 117; 116, 118; 117, identifier:strip_missing_columns; 118, argument_list; 118, 119; 118, 120; 119, identifier:table; 120, identifier:h_key_tuples; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:v_key_tuples; 124, call; 124, 125; 124, 126; 125, identifier:strip_hidden; 126, argument_list; 126, 127; 126, 128; 127, identifier:v_key_tuples; 128, identifier:v_level_visibility; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:h_key_tuples; 132, call; 132, 133; 132, 134; 133, identifier:strip_hidden; 134, argument_list; 134, 135; 134, 136; 135, identifier:h_key_tuples; 136, identifier:h_level_visibility; 137, return_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:assemble_table; 140, argument_list; 140, 141; 140, 142; 140, 143; 140, 144; 140, 145; 140, 146; 141, identifier:table; 142, identifier:v_key_tuples; 143, identifier:h_key_tuples; 144, identifier:v_level_titles; 145, identifier:h_level_titles; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:empty; 148, identifier:empty
def tabulate( obj, v_level_indexes=None, h_level_indexes=None, v_level_visibility=None, h_level_visibility=None, v_level_sort_keys=None, h_level_sort_keys=None, v_level_titles=None, h_level_titles=None, empty="", ): """Render a nested data structure into a two-dimensional table. Args: obj: The indexable data structure to be rendered, which can either be a non-string sequence or a mapping containing other sequences and mappings nested to arbitrarily many levels, with all the leaf items (which are neither sequences nor mappings, excluding strings). v_level_indexes: An iterable of the zero-based indexes of the levels for which the keys/indexes will be displayed along the vertical axis of the table. Taken together with the levels in h_levels these must represent the complete set of levels in the obj data structure. No level index should appear in both v_level_indexes and h_level_indexes, but all level indexes must appear in either v_level_indexes or h_level_indexes. If None, the levels not used in h_level_indexes will be used. If both v_level_indexes and h_level_indexes are not alternate indexes will be used as v_level and h_level indexes. h_level_indexes: An iterable of the zero-based indexes of the levels for which the keys/indexes will be displayed along the horizontal axis of the table. Taken together with the levels in v_levels these must represent the complete set of levels in the obj data structure. No level index should appear in both h_level_indexes and v_level_indexes, but all level indexes must appear in either h_level_indexes or v_level_indexes. If None, the levels not used in v_level_indexes will be used. If both v_level_indexes and h_level_indexes are not alternate indexes will be used as v_level and h_level indexes. v_level_visibility: An optional iterable of booleans, where each item corresponds to a level in v_level_indexes, and controls whether than level of index is included in the table stub columns. This iterable must contain the same number of items as v_level_indexes. h_level_visibility: An optional iterable of booleans, where each item corresponds to a level in h_level_indexes, and controls whether than level of index is included in the table header rows. This iterable must contain the same number of items as h_level_indexes. v_level_sort_keys: An optional iterable of Keys, where each key corresponds to a level in v_level_indexes, and controls how that key is sorted. If None, keys are sorted as-is. h_level_sort_keys: An optional iterable of Keys, where each key corresponds to a level in v_level_indexes, and controls how that key is sorted. If None, keys are sorted as-is. v_level_titles: An optional iterable of strings, where each string is a title which corresponds to a level in v_level_indexes, and which will be displayed against the row keys for that level. If None, no titles will be included. h_level_titles: An optional iterable of strings, where each string is a title which corresponds to a level in h_level_indexes, and which will be displayed against the column keys for that level. If None, no titles will be included. empty: An optional string value to use for empty cells. Returns: A list of lists representing the rows of cells. Example: tabulate(dict_of_dicts, [0, 1], []) """ level_keys = breadth_first(obj) v_level_indexes, h_level_indexes = validate_level_indexes( len(level_keys), v_level_indexes, h_level_indexes ) if v_level_visibility is None: v_level_visibility = [True] * len(v_level_indexes) if h_level_visibility is None: h_level_visibility = [True] * len(h_level_indexes) table, v_key_tuples, h_key_tuples = tabulate_body( obj, level_keys, v_level_indexes, h_level_indexes, v_level_sort_keys, h_level_sort_keys ) table, v_key_tuples = strip_missing_rows(table, v_key_tuples) table, h_key_tuples = strip_missing_columns(table, h_key_tuples) v_key_tuples = strip_hidden(v_key_tuples, v_level_visibility) h_key_tuples = strip_hidden(h_key_tuples, h_level_visibility) return assemble_table( table, v_key_tuples, h_key_tuples, v_level_titles, h_level_titles, empty=empty )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_ordered_entries; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:queryset; 7, False; 8, block; 8, 9; 8, 11; 8, 34; 8, 157; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 13; 11, 20; 12, identifier:queryset; 13, block; 13, 14; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:queryset; 19, identifier:queryset; 20, else_clause; 20, 21; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:queryset; 27, call; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:EntryCategory; 31, identifier:objects; 32, identifier:all; 33, argument_list; 34, if_statement; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:queryset; 38, block; 38, 39; 38, 135; 39, for_statement; 39, 40; 39, 41; 39, 44; 40, identifier:category; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:queryset; 44, block; 44, 45; 44, 53; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:entries; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:category; 51, identifier:get_entries; 52, argument_list; 53, if_statement; 53, 54; 53, 55; 53, 116; 54, identifier:entries; 55, block; 55, 56; 55, 66; 55, 78; 55, 90; 55, 102; 55, 110; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:amount_list; 59, list_comprehension; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:e; 62, identifier:amount_of_views; 63, for_in_clause; 63, 64; 63, 65; 64, identifier:e; 65, identifier:entries; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:rating_list; 69, list_comprehension; 69, 70; 69, 75; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:e; 73, identifier:rating; 74, argument_list; 75, for_in_clause; 75, 76; 75, 77; 76, identifier:e; 77, identifier:entries; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:views_per_entry; 81, binary_operator:/; 81, 82; 81, 86; 82, call; 82, 83; 82, 84; 83, identifier:fsum; 84, argument_list; 84, 85; 85, identifier:amount_list; 86, call; 86, 87; 86, 88; 87, identifier:len; 88, argument_list; 88, 89; 89, identifier:amount_list; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:rating_per_entry; 93, binary_operator:/; 93, 94; 93, 98; 94, call; 94, 95; 94, 96; 95, identifier:fsum; 96, argument_list; 96, 97; 97, identifier:rating_list; 98, call; 98, 99; 98, 100; 99, identifier:len; 100, argument_list; 100, 101; 101, identifier:rating_list; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:category; 106, identifier:last_rank; 107, binary_operator:*; 107, 108; 107, 109; 108, identifier:views_per_entry; 109, identifier:rating_per_entry; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:category; 114, identifier:save; 115, argument_list; 116, else_clause; 116, 117; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:queryset; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:queryset; 128, identifier:exclude; 129, argument_list; 129, 130; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:pk; 132, attribute; 132, 133; 132, 134; 133, identifier:category; 134, identifier:pk; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:queryset; 140, call; 140, 141; 140, 142; 141, identifier:sorted; 142, argument_list; 142, 143; 142, 146; 142, 154; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:queryset; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:key; 148, lambda; 148, 149; 148, 151; 149, lambda_parameters; 149, 150; 150, identifier:c; 151, attribute; 151, 152; 151, 153; 152, identifier:c; 153, identifier:last_rank; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:reverse; 156, True; 157, return_statement; 157, 158; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:queryset
def get_ordered_entries(self, queryset=False): """ Custom ordering. First we get the average views and rating for the categories's entries. Second we created a rank by multiplying both. Last, we sort categories by this rank from top to bottom. Example: - Cat_1 - Entry_1 (500 Views, Rating 2) - Entry_2 (200 Views, Rating -4) - Entry_3 (100 Views, Rating 3) - Cat_2 - Entry_1 (200 Views, Rating 7) - Entry_2 (50 Views, Rating 2) Result: Cat_1 has a rank by: 88.88 (avg. views: 266.66, avg. rating: 0.33) Cat_2 has a rank by: 562.5 (avg. views: 125, avg. rating: 4.5) Cat_2 will be displayed at the top. The algorithm is quality-oriented, as you can see. """ if queryset: self.queryset = queryset else: self.queryset = EntryCategory.objects.all() if self.queryset: for category in self.queryset: entries = category.get_entries() if entries: amount_list = [e.amount_of_views for e in entries] rating_list = [e.rating() for e in entries] views_per_entry = fsum(amount_list) / len(amount_list) rating_per_entry = fsum(rating_list) / len(rating_list) category.last_rank = views_per_entry * rating_per_entry category.save() else: self.queryset = self.queryset.exclude(pk=category.pk) self.queryset = sorted(self.queryset, key=lambda c: c.last_rank, reverse=True) return self.queryset
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:parallel_progbar; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 4, identifier:mapper; 5, identifier:iterable; 6, default_parameter; 6, 7; 6, 8; 7, identifier:nprocs; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:starmap; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:flatmap; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:shuffle; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:verbose; 20, True; 21, default_parameter; 21, 22; 21, 23; 22, identifier:verbose_flatmap; 23, None; 24, dictionary_splat_pattern; 24, 25; 25, identifier:kwargs; 26, block; 26, 27; 26, 29; 26, 45; 27, expression_statement; 27, 28; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:results; 32, call; 32, 33; 32, 34; 33, identifier:_parallel_progbar_launch; 34, argument_list; 34, 35; 34, 36; 34, 37; 34, 38; 34, 39; 34, 40; 34, 41; 34, 42; 34, 43; 35, identifier:mapper; 36, identifier:iterable; 37, identifier:nprocs; 38, identifier:starmap; 39, identifier:flatmap; 40, identifier:shuffle; 41, identifier:verbose; 42, identifier:verbose_flatmap; 43, dictionary_splat; 43, 44; 44, identifier:kwargs; 45, return_statement; 45, 46; 46, list_comprehension; 46, 47; 46, 48; 47, identifier:x; 48, for_in_clause; 48, 49; 48, 52; 49, pattern_list; 49, 50; 49, 51; 50, identifier:i; 51, identifier:x; 52, call; 52, 53; 52, 54; 53, identifier:sorted; 54, argument_list; 54, 55; 54, 56; 55, identifier:results; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:key; 58, lambda; 58, 59; 58, 61; 59, lambda_parameters; 59, 60; 60, identifier:p; 61, subscript; 61, 62; 61, 63; 62, identifier:p; 63, integer:0
def parallel_progbar(mapper, iterable, nprocs=None, starmap=False, flatmap=False, shuffle=False, verbose=True, verbose_flatmap=None, **kwargs): """Performs a parallel mapping of the given iterable, reporting a progress bar as values get returned :param mapper: The mapping function to apply to elements of the iterable :param iterable: The iterable to map :param nprocs: The number of processes (defaults to the number of cpu's) :param starmap: If true, the iterable is expected to contain tuples and the mapper function gets each element of a tuple as an argument :param flatmap: If true, flatten out the returned values if the mapper function returns a list of objects :param shuffle: If true, randomly sort the elements before processing them. This might help provide more uniform runtimes if processing different objects takes different amounts of time. :param verbose: Whether or not to print the progress bar :param verbose_flatmap: If performing a flatmap, whether or not to report each object as it's returned :param kwargs: Any other keyword arguments to pass to the progress bar (see ``progbar``) :return: A list of the returned objects, in the same order as provided """ results = _parallel_progbar_launch(mapper, iterable, nprocs, starmap, flatmap, shuffle, verbose, verbose_flatmap, **kwargs) return [x for i, x in sorted(results, key=lambda p: p[0])]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 30; 2, function_name:iparallel_progbar; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 28; 4, identifier:mapper; 5, identifier:iterable; 6, default_parameter; 6, 7; 6, 8; 7, identifier:nprocs; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:starmap; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:flatmap; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:shuffle; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:verbose; 20, True; 21, default_parameter; 21, 22; 21, 23; 22, identifier:verbose_flatmap; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:max_cache; 26, unary_operator:-; 26, 27; 27, integer:1; 28, dictionary_splat_pattern; 28, 29; 29, identifier:kwargs; 30, block; 30, 31; 30, 33; 30, 50; 31, expression_statement; 31, 32; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:results; 36, call; 36, 37; 36, 38; 37, identifier:_parallel_progbar_launch; 38, argument_list; 38, 39; 38, 40; 38, 41; 38, 42; 38, 43; 38, 44; 38, 45; 38, 46; 38, 47; 38, 48; 39, identifier:mapper; 40, identifier:iterable; 41, identifier:nprocs; 42, identifier:starmap; 43, identifier:flatmap; 44, identifier:shuffle; 45, identifier:verbose; 46, identifier:verbose_flatmap; 47, identifier:max_cache; 48, dictionary_splat; 48, 49; 49, identifier:kwargs; 50, return_statement; 50, 51; 51, generator_expression; 51, 52; 51, 53; 52, identifier:x; 53, for_in_clause; 53, 54; 53, 57; 54, pattern_list; 54, 55; 54, 56; 55, identifier:i; 56, identifier:x; 57, identifier:results
def iparallel_progbar(mapper, iterable, nprocs=None, starmap=False, flatmap=False, shuffle=False, verbose=True, verbose_flatmap=None, max_cache=-1, **kwargs): """Performs a parallel mapping of the given iterable, reporting a progress bar as values get returned. Yields objects as soon as they're computed, but does not guarantee that they'll be in the correct order. :param mapper: The mapping function to apply to elements of the iterable :param iterable: The iterable to map :param nprocs: The number of processes (defaults to the number of cpu's) :param starmap: If true, the iterable is expected to contain tuples and the mapper function gets each element of a tuple as an argument :param flatmap: If true, flatten out the returned values if the mapper function returns a list of objects :param shuffle: If true, randomly sort the elements before processing them. This might help provide more uniform runtimes if processing different objects takes different amounts of time. :param verbose: Whether or not to print the progress bar :param verbose_flatmap: If performing a flatmap, whether or not to report each object as it's returned :param max_cache: Maximum number of mapped objects to permit in the queue at once :param kwargs: Any other keyword arguments to pass to the progress bar (see ``progbar``) :return: A list of the returned objects, in whatever order they're done being computed """ results = _parallel_progbar_launch(mapper, iterable, nprocs, starmap, flatmap, shuffle, verbose, verbose_flatmap, max_cache, **kwargs) return (x for i, x in results)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_creation_date_tags; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:url; 5, identifier:domain; 6, default_parameter; 6, 7; 6, 8; 7, identifier:as_dicts; 8, False; 9, block; 9, 10; 9, 12; 9, 24; 9, 43; 9, 49; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:creation_date_tags; 15, list:[ mementoweb_api_tags(url), get_whois_tags(domain), ]; 15, 16; 15, 20; 16, call; 16, 17; 16, 18; 17, identifier:mementoweb_api_tags; 18, argument_list; 18, 19; 19, identifier:url; 20, call; 20, 21; 20, 22; 21, identifier:get_whois_tags; 22, argument_list; 22, 23; 23, identifier:domain; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:creation_date_tags; 27, call; 27, 28; 27, 29; 28, identifier:sorted; 29, argument_list; 29, 30; 29, 35; 30, call; 30, 31; 30, 32; 31, identifier:sum; 32, argument_list; 32, 33; 32, 34; 33, identifier:creation_date_tags; 34, list:[]; 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, attribute; 40, 41; 40, 42; 41, identifier:x; 42, identifier:date; 43, if_statement; 43, 44; 43, 46; 44, not_operator; 44, 45; 45, identifier:as_dicts; 46, block; 46, 47; 47, return_statement; 47, 48; 48, identifier:creation_date_tags; 49, return_statement; 49, 50; 50, list_comprehension; 50, 51; 50, 56; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:item; 54, identifier:_as_dict; 55, argument_list; 56, for_in_clause; 56, 57; 56, 58; 57, identifier:item; 58, identifier:creation_date_tags
def get_creation_date_tags(url, domain, as_dicts=False): """ Put together all data sources in this module and return it's output. Args: url (str): URL of the web. With relative paths and so on. domain (str): Just the domain of the web. as_dicts (bool, default False): Convert output to dictionaries compatible with :class:`.SourceString`? Returns: list: Sorted list of :class:`TimeResource` objects or dicts. """ creation_date_tags = [ mementoweb_api_tags(url), get_whois_tags(domain), ] creation_date_tags = sorted( sum(creation_date_tags, []), key=lambda x: x.date ) if not as_dicts: return creation_date_tags return [ item._as_dict() for item in creation_date_tags ]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:image_from_name; 3, parameters; 3, 4; 3, 5; 4, identifier:name; 5, identifier:images; 6, block; 6, 7; 6, 9; 6, 26; 6, 52; 6, 81; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:prefixed_images; 12, list_comprehension; 12, 13; 12, 14; 12, 17; 13, identifier:i; 14, for_in_clause; 14, 15; 14, 16; 15, identifier:i; 16, identifier:images; 17, if_clause; 17, 18; 18, call; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:i; 22, identifier:name; 23, identifier:startswith; 24, argument_list; 24, 25; 25, identifier:name; 26, if_statement; 26, 27; 26, 36; 27, comparison_operator:in; 27, 28; 27, 29; 28, identifier:name; 29, list_comprehension; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:i; 32, identifier:name; 33, for_in_clause; 33, 34; 33, 35; 34, identifier:i; 35, identifier:prefixed_images; 36, block; 36, 37; 37, return_statement; 37, 38; 38, subscript; 38, 39; 38, 50; 39, list_comprehension; 39, 40; 39, 41; 39, 44; 40, identifier:i; 41, for_in_clause; 41, 42; 41, 43; 42, identifier:i; 43, identifier:prefixed_images; 44, if_clause; 44, 45; 45, comparison_operator:==; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:i; 48, identifier:name; 49, identifier:name; 50, unary_operator:-; 50, 51; 51, integer:1; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:decorated; 55, call; 55, 56; 55, 57; 56, identifier:sorted; 57, argument_list; 57, 58; 58, list_comprehension; 58, 59; 58, 78; 59, tuple; 59, 60; 59, 77; 60, call; 60, 61; 60, 62; 61, identifier:int; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 75; 64, attribute; 64, 65; 64, 74; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:re; 68, identifier:search; 69, argument_list; 69, 70; 69, 71; 70, string:'\d+'; 71, attribute; 71, 72; 71, 73; 72, identifier:i; 73, identifier:name; 74, identifier:group; 75, argument_list; 75, 76; 76, integer:0; 77, identifier:i; 78, for_in_clause; 78, 79; 78, 80; 79, identifier:i; 80, identifier:prefixed_images; 81, return_statement; 81, 82; 82, subscript; 82, 83; 82, 90; 83, list_comprehension; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:i; 86, integer:1; 87, for_in_clause; 87, 88; 87, 89; 88, identifier:i; 89, identifier:decorated; 90, unary_operator:-; 90, 91; 91, integer:1
def image_from_name(name, images): """Return an image from a list of images. If the name is an exact match, return the last exactly matching image. Otherwise, sort images by 'natural' order, using decorate-sort-undecorate, and return the largest. see: http://code.activestate.com/recipes/285264-natural-string-sorting/ """ prefixed_images = [i for i in images if i.name.startswith(name)] if name in [i.name for i in prefixed_images]: return [i for i in prefixed_images if i.name == name][-1] decorated = sorted( [(int(re.search('\d+', i.name).group(0)), i) for i in prefixed_images]) return [i[1] for i in decorated][-1]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:func_on_enter; 3, parameters; 3, 4; 4, identifier:func; 5, block; 5, 6; 5, 8; 5, 32; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 12; 9, function_name:function_after_enter_pressed; 10, parameters; 10, 11; 11, identifier:ev; 12, block; 12, 13; 12, 19; 12, 20; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:ev; 17, identifier:stopPropagation; 18, argument_list; 19, comment; 20, if_statement; 20, 21; 20, 26; 21, comparison_operator:==; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:ev; 24, identifier:keyCode; 25, integer:13; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:func; 30, argument_list; 30, 31; 31, identifier:ev; 32, return_statement; 32, 33; 33, identifier:function_after_enter_pressed
def func_on_enter(func): """ Register the `func` as a callback reacting only to ENTER. Note: This function doesn't bind the key to the element, just creates sort of filter, which ignores all other events. """ def function_after_enter_pressed(ev): ev.stopPropagation() # if the key was `enter` .. if ev.keyCode == 13: func(ev) return function_after_enter_pressed
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:prev_next_group; 3, parameters; 3, 4; 3, 5; 4, identifier:project; 5, identifier:group; 6, block; 6, 7; 6, 9; 6, 10; 6, 26; 6, 44; 6, 57; 6, 75; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:groups; 13, call; 13, 14; 13, 15; 14, identifier:sorted; 15, generator_expression; 15, 16; 15, 17; 15, 22; 16, identifier:x; 17, for_in_clause; 17, 18; 17, 19; 18, identifier:x; 19, attribute; 19, 20; 19, 21; 20, identifier:project; 21, identifier:groups; 22, if_clause; 22, 23; 23, attribute; 23, 24; 23, 25; 24, identifier:x; 25, identifier:submissions; 26, try_statement; 26, 27; 26, 37; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:index; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:groups; 34, identifier:index; 35, argument_list; 35, 36; 36, identifier:group; 37, except_clause; 37, 38; 37, 39; 38, identifier:ValueError; 39, block; 39, 40; 40, return_statement; 40, 41; 41, expression_list; 41, 42; 41, 43; 42, None; 43, None; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:prev_group; 47, conditional_expression:if; 47, 48; 47, 53; 47, 56; 48, subscript; 48, 49; 48, 50; 49, identifier:groups; 50, binary_operator:-; 50, 51; 50, 52; 51, identifier:index; 52, integer:1; 53, comparison_operator:>; 53, 54; 53, 55; 54, identifier:index; 55, integer:0; 56, None; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:next_group; 60, conditional_expression:if; 60, 61; 60, 66; 60, 74; 61, subscript; 61, 62; 61, 63; 62, identifier:groups; 63, binary_operator:+; 63, 64; 63, 65; 64, identifier:index; 65, integer:1; 66, comparison_operator:<; 66, 67; 66, 70; 67, binary_operator:+; 67, 68; 67, 69; 68, identifier:index; 69, integer:1; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, identifier:groups; 74, None; 75, return_statement; 75, 76; 76, expression_list; 76, 77; 76, 78; 77, identifier:prev_group; 78, identifier:next_group
def prev_next_group(project, group): """Return adjacent group objects or None for the given project and group. The previous and next group objects are relative to sort order of the project's groups with respect to the passed in group. """ # TODO: Profile and optimize this query if necessary groups = sorted(x for x in project.groups if x.submissions) try: index = groups.index(group) except ValueError: return None, None prev_group = groups[index - 1] if index > 0 else None next_group = groups[index + 1] if index + 1 < len(groups) else None return prev_group, next_group
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:do_minus; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:parser; 6, identifier:group; 7, block; 7, 8; 7, 10; 7, 18; 7, 22; 7, 112; 7, 124; 8, expression_statement; 8, 9; 9, string:'''This filter sorts nodes in a flat group into "required", "default", and "banned" subgroups based on the presence of plus and minus nodes. '''; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:grouper; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:group; 16, identifier:__class__; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:next_not; 21, None; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:node; 24, identifier:group; 25, block; 25, 26; 26, if_statement; 26, 27; 26, 34; 26, 61; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 31; 30, identifier:node; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:Minus; 34, block; 34, 35; 34, 42; 34, 54; 35, if_statement; 35, 36; 35, 39; 35, 40; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:next_not; 38, None; 39, comment; 40, block; 40, 41; 41, continue_statement; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:next_not; 45, call; 45, 46; 45, 53; 46, attribute; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:whoosh; 50, identifier:qparser; 51, identifier:syntax; 52, identifier:NotGroup; 53, argument_list; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:grouper; 58, identifier:append; 59, argument_list; 59, 60; 60, identifier:next_not; 61, else_clause; 61, 62; 61, 63; 62, comment; 63, block; 63, 64; 63, 87; 64, if_statement; 64, 65; 64, 76; 65, call; 65, 66; 65, 67; 66, identifier:isinstance; 67, argument_list; 67, 68; 67, 69; 68, identifier:node; 69, attribute; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:whoosh; 73, identifier:qparser; 74, identifier:syntax; 75, identifier:GroupNode; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:node; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:do_minus; 84, argument_list; 84, 85; 84, 86; 85, identifier:parser; 86, identifier:node; 87, if_statement; 87, 88; 87, 91; 87, 103; 88, comparison_operator:is; 88, 89; 88, 90; 89, identifier:next_not; 90, None; 91, block; 91, 92; 91, 99; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:next_not; 96, identifier:append; 97, argument_list; 97, 98; 98, identifier:node; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:next_not; 102, None; 103, else_clause; 103, 104; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:grouper; 109, identifier:append; 110, argument_list; 110, 111; 111, identifier:node; 112, if_statement; 112, 113; 112, 116; 112, 117; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:next_not; 115, None; 116, comment; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:grouper; 122, identifier:pop; 123, argument_list; 124, return_statement; 124, 125; 125, identifier:grouper
def do_minus(self, parser, group): '''This filter sorts nodes in a flat group into "required", "default", and "banned" subgroups based on the presence of plus and minus nodes. ''' grouper = group.__class__() next_not = None for node in group: if isinstance(node, self.Minus): if next_not is not None: # Two Minuses in a row; skip the second one continue next_not = whoosh.qparser.syntax.NotGroup() grouper.append(next_not) else: # Nodes with children: search for nested Minus nodes if isinstance(node, whoosh.qparser.syntax.GroupNode): node = self.do_minus(parser, node) if next_not is not None: next_not.append(node) next_not = None else: grouper.append(node) if next_not is not None: # Remove the empty NotGroup grouper.pop() return grouper
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:topological_sort; 3, parameters; 3, 4; 4, identifier:dependency_pairs; 5, block; 5, 6; 5, 8; 5, 15; 5, 16; 5, 23; 5, 24; 5, 28; 5, 29; 5, 71; 5, 83; 5, 112; 5, 128; 6, expression_statement; 6, 7; 7, string:"Sort values subject to dependency constraints"; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:num_heads; 11, call; 11, 12; 11, 13; 12, identifier:defaultdict; 13, argument_list; 13, 14; 14, identifier:int; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:tails; 19, call; 19, 20; 19, 21; 20, identifier:defaultdict; 21, argument_list; 21, 22; 22, identifier:list; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:heads; 27, list:[]; 28, comment; 29, for_statement; 29, 30; 29, 33; 29, 34; 30, pattern_list; 30, 31; 30, 32; 31, identifier:h; 32, identifier:t; 33, identifier:dependency_pairs; 34, block; 34, 35; 34, 41; 35, expression_statement; 35, 36; 36, augmented_assignment:+=; 36, 37; 36, 40; 37, subscript; 37, 38; 37, 39; 38, identifier:num_heads; 39, identifier:t; 40, integer:1; 41, if_statement; 41, 42; 41, 45; 41, 55; 42, comparison_operator:in; 42, 43; 42, 44; 43, identifier:h; 44, identifier:tails; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:tails; 51, identifier:h; 52, identifier:append; 53, argument_list; 53, 54; 54, identifier:t; 55, else_clause; 55, 56; 56, block; 56, 57; 56, 64; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:tails; 61, identifier:h; 62, list:[t]; 62, 63; 63, identifier:t; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:heads; 68, identifier:append; 69, argument_list; 69, 70; 70, identifier:h; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:ordered; 74, list_comprehension; 74, 75; 74, 76; 74, 79; 75, identifier:h; 76, for_in_clause; 76, 77; 76, 78; 77, identifier:h; 78, identifier:heads; 79, if_clause; 79, 80; 80, comparison_operator:not; 80, 81; 80, 82; 81, identifier:h; 82, identifier:num_heads; 83, for_statement; 83, 84; 83, 85; 83, 86; 84, identifier:h; 85, identifier:ordered; 86, block; 86, 87; 87, for_statement; 87, 88; 87, 89; 87, 92; 88, identifier:t; 89, subscript; 89, 90; 89, 91; 90, identifier:tails; 91, identifier:h; 92, block; 92, 93; 92, 99; 93, expression_statement; 93, 94; 94, augmented_assignment:-=; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:num_heads; 97, identifier:t; 98, integer:1; 99, if_statement; 99, 100; 99, 104; 100, not_operator; 100, 101; 101, subscript; 101, 102; 101, 103; 102, identifier:num_heads; 103, identifier:t; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:ordered; 109, identifier:append; 110, argument_list; 110, 111; 111, identifier:t; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:cyclic; 115, list_comprehension; 115, 116; 115, 117; 115, 126; 116, identifier:n; 117, for_in_clause; 117, 118; 117, 121; 118, pattern_list; 118, 119; 118, 120; 119, identifier:n; 120, identifier:heads; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:num_heads; 124, identifier:items; 125, argument_list; 126, if_clause; 126, 127; 127, identifier:heads; 128, return_statement; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:Results; 131, argument_list; 131, 132; 131, 133; 132, identifier:ordered; 133, identifier:cyclic
def topological_sort(dependency_pairs): "Sort values subject to dependency constraints" num_heads = defaultdict(int) # num arrows pointing in tails = defaultdict(list) # list of arrows going out heads = [] # unique list of heads in order first seen for h, t in dependency_pairs: num_heads[t] += 1 if h in tails: tails[h].append(t) else: tails[h] = [t] heads.append(h) ordered = [h for h in heads if h not in num_heads] for h in ordered: for t in tails[h]: num_heads[t] -= 1 if not num_heads[t]: ordered.append(t) cyclic = [n for n, heads in num_heads.items() if heads] return Results(ordered, cyclic)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:multisorted; 3, parameters; 3, 4; 3, 5; 4, identifier:items; 5, list_splat_pattern; 5, 6; 6, identifier:keys; 7, block; 7, 8; 7, 10; 7, 25; 7, 49; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 17; 11, comparison_operator:==; 11, 12; 11, 16; 12, call; 12, 13; 12, 14; 13, identifier:len; 14, argument_list; 14, 15; 15, identifier:keys; 16, integer:0; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:keys; 21, list:[asc()]; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:asc; 24, argument_list; 25, for_statement; 25, 26; 25, 27; 25, 31; 26, identifier:key; 27, call; 27, 28; 27, 29; 28, identifier:reversed; 29, argument_list; 29, 30; 30, identifier:keys; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:items; 35, call; 35, 36; 35, 37; 36, identifier:sorted; 37, argument_list; 37, 38; 37, 39; 37, 44; 38, identifier:items; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:key; 41, attribute; 41, 42; 41, 43; 42, identifier:key; 43, identifier:func; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:reverse; 46, attribute; 46, 47; 46, 48; 47, identifier:key; 48, identifier:reverse; 49, return_statement; 49, 50; 50, identifier:items
def multisorted(items, *keys): """Sort by multiple attributes. Args: items: An iterable series to be sorted. *keys: Key objects which extract key values from the items. The first key will be the most significant, and the last key the least significant. If no key functions are provided, the items will be sorted in ascending natural order. Returns: A list of items sorted according to keys. """ if len(keys) == 0: keys = [asc()] for key in reversed(keys): items = sorted(items, key=key.func, reverse=key.reverse) return items
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:tuplesorted; 3, parameters; 3, 4; 3, 5; 4, identifier:items; 5, list_splat_pattern; 5, 6; 6, identifier:keys; 7, block; 7, 8; 7, 10; 7, 11; 7, 50; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:tuple_keys; 14, list_comprehension; 14, 15; 14, 42; 15, call; 15, 16; 15, 17; 16, identifier:Key; 17, argument_list; 17, 18; 17, 37; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:func; 20, lambda; 20, 21; 20, 29; 21, lambda_parameters; 21, 22; 21, 23; 21, 26; 22, identifier:t; 23, default_parameter; 23, 24; 23, 25; 24, identifier:i; 25, identifier:index; 26, default_parameter; 26, 27; 26, 28; 27, identifier:k; 28, identifier:key; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:k; 32, identifier:func; 33, argument_list; 33, 34; 34, subscript; 34, 35; 34, 36; 35, identifier:t; 36, identifier:i; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:reverse; 39, attribute; 39, 40; 39, 41; 40, identifier:key; 41, identifier:reverse; 42, for_in_clause; 42, 43; 42, 46; 43, pattern_list; 43, 44; 43, 45; 44, identifier:index; 45, identifier:key; 46, call; 46, 47; 46, 48; 47, identifier:enumerate; 48, argument_list; 48, 49; 49, identifier:keys; 50, return_statement; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:multisorted; 53, argument_list; 53, 54; 53, 55; 54, identifier:items; 55, list_splat; 55, 56; 56, identifier:tuple_keys
def tuplesorted(items, *keys): """Sort by tuples with a different key for each item. Args: items: An iterable series of sequences (typically tuples) *keys: Key objects which transform individual elements of each tuple into sort keys. The zeroth object transforms the zeroth element of each tuple, the first key object transforms the first element of each tuple, and so on. Returns: A list of items sorted according to keys. """ # Transform the keys so each works on one item of the tuple tuple_keys = [ Key(func=lambda t, i=index, k=key: k.func(t[i]), reverse=key.reverse) for index, key in enumerate(keys) ] return multisorted(items, *tuple_keys)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:list_formats; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:node; 6, default_parameter; 6, 7; 6, 8; 7, identifier:path; 8, tuple; 9, default_parameter; 9, 10; 9, 11; 10, identifier:formats; 11, None; 12, block; 12, 13; 12, 15; 12, 24; 12, 44; 12, 58; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:==; 16, 17; 16, 18; 17, identifier:formats; 18, None; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:formats; 23, list:[]; 24, for_statement; 24, 25; 24, 26; 24, 29; 25, identifier:child; 26, attribute; 26, 27; 26, 28; 27, identifier:node; 28, identifier:children; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:list_formats; 35, argument_list; 35, 36; 35, 37; 35, 43; 36, identifier:child; 37, binary_operator:+; 37, 38; 37, 39; 38, identifier:path; 39, tuple; 39, 40; 40, attribute; 40, 41; 40, 42; 41, identifier:child; 42, identifier:name; 43, identifier:formats; 44, expression_statement; 44, 45; 45, boolean_operator:and; 45, 46; 45, 47; 46, identifier:path; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:formats; 50, identifier:append; 51, argument_list; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, string:"."; 55, identifier:join; 56, argument_list; 56, 57; 57, identifier:path; 58, return_statement; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:sorted; 61, argument_list; 61, 62; 62, identifier:formats
def list_formats(self, node, path=(), formats=None): """ Lists the object formats in sorted order. :param node: Root node to start listing the formats from. :type node: AbstractCompositeNode :param path: Walked paths. :type path: tuple :param formats: Formats. :type formats: list :return: Formats. :rtype: list """ if formats == None: formats = [] for child in node.children: self.list_formats(child, path + (child.name,), formats) path and formats.append(".".join(path)) return sorted(formats)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:ls; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:root; 6, string:"."; 7, default_parameter; 7, 8; 7, 9; 8, identifier:abspaths; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:recursive; 12, False; 13, block; 13, 14; 13, 16; 13, 59; 14, expression_statement; 14, 15; 15, comment; 16, function_definition; 16, 17; 16, 18; 16, 20; 17, function_name:_expand_subdirs; 18, parameters; 18, 19; 19, identifier:file; 20, block; 20, 21; 21, if_statement; 21, 22; 21, 30; 21, 54; 22, call; 22, 23; 22, 24; 23, identifier:isdir; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:path; 27, argument_list; 27, 28; 27, 29; 28, identifier:root; 29, identifier:file; 30, block; 30, 31; 31, return_statement; 31, 32; 32, binary_operator:+; 32, 33; 32, 35; 33, list:[file]; 33, 34; 34, identifier:file; 35, list_comprehension; 35, 36; 35, 41; 36, call; 36, 37; 36, 38; 37, identifier:path; 38, argument_list; 38, 39; 38, 40; 39, identifier:file; 40, identifier:x; 41, for_in_clause; 41, 42; 41, 43; 42, identifier:x; 43, call; 43, 44; 43, 45; 44, identifier:ls; 45, argument_list; 45, 46; 45, 51; 46, call; 46, 47; 46, 48; 47, identifier:path; 48, argument_list; 48, 49; 48, 50; 49, identifier:root; 50, identifier:file; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:recursive; 53, True; 54, else_clause; 54, 55; 55, block; 55, 56; 56, return_statement; 56, 57; 57, list:[file]; 57, 58; 58, identifier:file; 59, if_statement; 59, 60; 59, 64; 59, 65; 59, 79; 59, 114; 59, 145; 60, call; 60, 61; 60, 62; 61, identifier:isfile; 62, argument_list; 62, 63; 63, identifier:root; 64, comment; 65, block; 65, 66; 66, return_statement; 66, 67; 67, conditional_expression:if; 67, 68; 67, 73; 67, 74; 68, list:[abspath(root)]; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:abspath; 71, argument_list; 71, 72; 72, identifier:root; 73, identifier:abspaths; 74, list:[basename(root)]; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:basename; 77, argument_list; 77, 78; 78, identifier:root; 79, elif_clause; 79, 80; 79, 81; 79, 82; 80, identifier:abspaths; 81, comment; 82, block; 82, 83; 82, 96; 82, 97; 82, 104; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:relpaths; 86, call; 86, 87; 86, 88; 87, identifier:ls; 88, argument_list; 88, 89; 88, 90; 88, 93; 89, identifier:root; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:recursive; 92, identifier:recursive; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:abspaths; 95, False; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:base; 100, call; 100, 101; 100, 102; 101, identifier:abspath; 102, argument_list; 102, 103; 103, identifier:root; 104, return_statement; 104, 105; 105, list_comprehension; 105, 106; 105, 111; 106, call; 106, 107; 106, 108; 107, identifier:path; 108, argument_list; 108, 109; 108, 110; 109, identifier:base; 110, identifier:relpath; 111, for_in_clause; 111, 112; 111, 113; 112, identifier:relpath; 113, identifier:relpaths; 114, elif_clause; 114, 115; 114, 116; 114, 117; 115, identifier:recursive; 116, comment; 117, block; 117, 118; 117, 131; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:paths; 121, call; 121, 122; 121, 123; 122, identifier:ls; 123, argument_list; 123, 124; 123, 125; 123, 128; 124, identifier:root; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:abspaths; 127, identifier:abspaths; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:recursive; 130, False; 131, return_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:labtypes; 135, identifier:flatten; 136, argument_list; 136, 137; 137, list_comprehension; 137, 138; 137, 142; 138, call; 138, 139; 138, 140; 139, identifier:_expand_subdirs; 140, argument_list; 140, 141; 141, identifier:file; 142, for_in_clause; 142, 143; 142, 144; 143, identifier:file; 144, identifier:paths; 145, else_clause; 145, 146; 145, 147; 146, comment; 147, block; 147, 148; 148, return_statement; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:list; 151, argument_list; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:sorted; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:os; 158, identifier:listdir; 159, argument_list; 159, 160; 160, identifier:root
def ls(root=".", abspaths=False, recursive=False): """ Return a list of files in directory. Directory listings are sorted alphabetically. If the named directory is a file, return it's path. Examples: >>> fs.ls("foo") ["a", "b", "c"] >>> fs.ls("foo/a") ["foo/a"] >>> fs.ls("foo", abspaths=True) ["/home/test/foo/a", "/home/test/foo/b", "/home/test/foo/c"] >>> fs.ls("foo", recursive=True) ["a", "b", "b/d", "b/d/e", "c"] Arguments: root (str): Path to directory. Can be relative or absolute. abspaths (bool, optional): Return absolute paths if true. recursive (bool, optional): Recursively list subdirectories if true. Returns: list of str: A list of paths. Raises: OSError: If root directory does not exist. """ def _expand_subdirs(file): if isdir(path(root, file)): return [file] + [path(file, x) for x in ls(path(root, file), recursive=True)] else: return [file] if isfile(root): # If argument is a file, return path. return [abspath(root)] if abspaths else [basename(root)] elif abspaths: # Get relative names. relpaths = ls(root, recursive=recursive, abspaths=False) # Prepend the absolute path to each relative name. base = abspath(root) return [path(base, relpath) for relpath in relpaths] elif recursive: # Recursively expand subdirectories. paths = ls(root, abspaths=abspaths, recursive=False) return labtypes.flatten([_expand_subdirs(file) for file in paths]) else: # List directory contents. return list(sorted(os.listdir(root)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:list; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:offset; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:limit; 10, integer:0; 11, default_parameter; 11, 12; 11, 13; 12, identifier:fields; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sort; 16, None; 17, dictionary_splat_pattern; 17, 18; 18, identifier:kwargs; 19, block; 19, 20; 19, 22; 20, expression_statement; 20, 21; 21, comment; 22, try_statement; 22, 23; 22, 57; 23, block; 23, 24; 23, 46; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:cursor; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:_cursor; 31, argument_list; 31, 32; 31, 35; 31, 38; 31, 41; 31, 44; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:offset; 34, identifier:offset; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:limit; 37, identifier:limit; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:fields; 40, identifier:fields; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:sort; 43, identifier:sort; 44, dictionary_splat; 44, 45; 45, identifier:kwargs; 46, return_statement; 46, 47; 47, expression_list; 47, 48; 47, 52; 48, call; 48, 49; 48, 50; 49, identifier:list; 50, argument_list; 50, 51; 51, identifier:cursor; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:cursor; 55, identifier:count; 56, argument_list; 57, except_clause; 57, 58; 57, 66; 57, 67; 58, as_pattern; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:pymongo; 62, identifier:errors; 63, identifier:OperationFailure; 64, as_pattern_target; 64, 65; 65, identifier:exc; 66, comment; 67, block; 67, 68; 67, 87; 67, 94; 67, 105; 67, 113; 67, 135; 68, try_statement; 68, 69; 68, 80; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, subscript; 71, 72; 71, 79; 72, subscript; 72, 73; 72, 78; 73, subscript; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:kwargs; 76, string:'$or'; 77, integer:0; 78, string:'$text'; 79, string:'$search'; 80, except_clause; 80, 81; 80, 84; 81, tuple; 81, 82; 81, 83; 82, identifier:KeyError; 83, identifier:IndexError; 84, block; 84, 85; 85, raise_statement; 85, 86; 86, identifier:exc; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:LOG; 91, identifier:warn; 92, argument_list; 92, 93; 93, string:"Falling back to hard-coded mongo v2.4 search behavior"; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:kwargs; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:search_alternative; 101, argument_list; 101, 102; 101, 103; 102, identifier:limit; 103, dictionary_splat; 103, 104; 104, identifier:kwargs; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:LOG; 109, identifier:debug; 110, argument_list; 110, 111; 110, 112; 111, string:"Modified kwargs: %s"; 112, identifier:kwargs; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:cursor; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:_cursor; 120, argument_list; 120, 121; 120, 124; 120, 127; 120, 130; 120, 133; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:offset; 123, identifier:offset; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:limit; 126, identifier:limit; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:fields; 129, identifier:fields; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:sort; 132, identifier:sort; 133, dictionary_splat; 133, 134; 134, identifier:kwargs; 135, return_statement; 135, 136; 136, expression_list; 136, 137; 136, 141; 137, call; 137, 138; 137, 139; 138, identifier:list; 139, argument_list; 139, 140; 140, identifier:cursor; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:cursor; 144, identifier:count; 145, argument_list
def list(self, offset=0, limit=0, fields=None, sort=None, **kwargs): """Return filtered list of documents in a collection. For text-based search, we support searching on a name/string field by regex and text index. So strings passed in to a r=text search are used to filter collections by text index and regex on a named field. :param offset: for pagination, which record to start attribute :param limit: for pagination, how many records to return :param fields: list of field names to return (otherwise returns all) :param sort: list of fields to sort by (prefix with '-' for descending) :param kwargs: key/values to find (only supports equality for now) :returns: a tuple of the list of documents and the total count """ try: cursor = self._cursor(offset=offset, limit=limit, fields=fields, sort=sort, **kwargs) return list(cursor), cursor.count() except pymongo.errors.OperationFailure as exc: # This is workaround for mongodb v2.4 and 'q' filter params try: kwargs['$or'][0]['$text']['$search'] except (KeyError, IndexError): raise exc LOG.warn("Falling back to hard-coded mongo v2.4 search behavior") kwargs = self.search_alternative(limit, **kwargs) LOG.debug("Modified kwargs: %s", kwargs) cursor = self._cursor(offset=offset, limit=limit, fields=fields, sort=sort, **kwargs) return list(cursor), cursor.count()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:_cursor; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:offset; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:limit; 10, integer:0; 11, default_parameter; 11, 12; 11, 13; 12, identifier:fields; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sort; 16, None; 17, dictionary_splat_pattern; 17, 18; 18, identifier:kwargs; 19, block; 19, 20; 19, 22; 19, 29; 19, 45; 19, 57; 19, 116; 19, 132; 20, expression_statement; 20, 21; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:projection; 25, dictionary; 25, 26; 26, pair; 26, 27; 26, 28; 27, string:'_id'; 28, False; 29, if_statement; 29, 30; 29, 31; 30, identifier:fields; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:projection; 36, identifier:update; 37, argument_list; 37, 38; 38, dictionary_comprehension; 38, 39; 38, 42; 39, pair; 39, 40; 39, 41; 40, identifier:field; 41, True; 42, for_in_clause; 42, 43; 42, 44; 43, identifier:field; 44, identifier:fields; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:results; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_collection; 53, identifier:find; 54, argument_list; 54, 55; 54, 56; 55, identifier:kwargs; 56, identifier:projection; 57, if_statement; 57, 58; 57, 59; 58, identifier:sort; 59, block; 59, 60; 59, 67; 59, 109; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:sort_pairs; 63, subscript; 63, 64; 63, 65; 64, identifier:sort; 65, slice; 65, 66; 66, colon; 67, for_statement; 67, 68; 67, 71; 67, 75; 68, pattern_list; 68, 69; 68, 70; 69, identifier:index; 70, identifier:field; 71, call; 71, 72; 71, 73; 72, identifier:enumerate; 73, argument_list; 73, 74; 74, identifier:sort; 75, block; 75, 76; 76, if_statement; 76, 77; 76, 82; 76, 97; 77, comparison_operator:==; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:field; 80, integer:0; 81, string:"-"; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:sort_pairs; 87, identifier:index; 88, tuple; 88, 89; 88, 94; 89, subscript; 89, 90; 89, 91; 90, identifier:field; 91, slice; 91, 92; 91, 93; 92, integer:1; 93, colon; 94, attribute; 94, 95; 94, 96; 95, identifier:pymongo; 96, identifier:DESCENDING; 97, else_clause; 97, 98; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 104; 101, subscript; 101, 102; 101, 103; 102, identifier:sort_pairs; 103, identifier:index; 104, tuple; 104, 105; 104, 106; 105, identifier:field; 106, attribute; 106, 107; 106, 108; 107, identifier:pymongo; 108, identifier:ASCENDING; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:results; 113, identifier:sort; 114, argument_list; 114, 115; 115, identifier:sort_pairs; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 128; 118, attribute; 118, 119; 118, 127; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:results; 122, identifier:skip; 123, argument_list; 123, 124; 124, boolean_operator:or; 124, 125; 124, 126; 125, identifier:offset; 126, integer:0; 127, identifier:limit; 128, argument_list; 128, 129; 129, boolean_operator:or; 129, 130; 129, 131; 130, identifier:limit; 131, integer:0; 132, return_statement; 132, 133; 133, identifier:results
def _cursor(self, offset=0, limit=0, fields=None, sort=None, **kwargs): """Return a cursor on a filtered list of documents in a collection. :param offset: for pagination, which record to start attribute :param limit: for pagination, how many records to return :param fields: list of field names to return (otherwise returns all) :param sort: list of fields to sort by (prefix with '-' for descending) :param kwargs: key/values to find (only supports equality for now) :returns: a tuple of a cursor on documents and the total count Note: close the cursor after using it if you don't exhaust it """ projection = {'_id': False} if fields: projection.update({field: True for field in fields}) results = self._collection.find(kwargs, projection) if sort: sort_pairs = sort[:] for index, field in enumerate(sort): if field[0] == "-": sort_pairs[index] = (field[1:], pymongo.DESCENDING) else: sort_pairs[index] = (field, pymongo.ASCENDING) results.sort(sort_pairs) results.skip(offset or 0).limit(limit or 0) return results
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:insert; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:item; 6, identifier:low_value; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, return_statement; 10, 11; 11, call; 11, 12; 11, 13; 12, identifier:c_void_p; 13, argument_list; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:lib; 17, identifier:zlistx_insert; 18, argument_list; 18, 19; 18, 22; 18, 23; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_as_parameter_; 22, identifier:item; 23, identifier:low_value
def insert(self, item, low_value): """ Create a new node and insert it into a sorted list. Calls the item duplicator, if any, on the item. If low_value is true, starts searching from the start of the list, otherwise searches from the end. Use the item comparator, if any, to find where to place the new node. Returns a handle to the new node, or NULL if memory was exhausted. Resets the cursor to the list head. """ return c_void_p(lib.zlistx_insert(self._as_parameter_, item, low_value))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:reorder; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:handle; 6, identifier:low_value; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, return_statement; 10, 11; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:lib; 14, identifier:zlistx_reorder; 15, argument_list; 15, 16; 15, 19; 15, 20; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_as_parameter_; 19, identifier:handle; 20, identifier:low_value
def reorder(self, handle, low_value): """ Move an item, specified by handle, into position in a sorted list. Uses the item comparator, if any, to determine the new location. If low_value is true, starts searching from the start of the list, otherwise searches from the end. """ return lib.zlistx_reorder(self._as_parameter_, handle, low_value)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:process_params; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:request; 5, default_parameter; 5, 6; 5, 7; 6, identifier:standard_params; 7, identifier:STANDARD_QUERY_PARAMS; 8, default_parameter; 8, 9; 8, 10; 9, identifier:filter_fields; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:defaults; 13, None; 14, block; 14, 15; 14, 17; 14, 25; 14, 49; 14, 76; 14, 82; 14, 152; 14, 199; 14, 245; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 20; 18, not_operator; 18, 19; 19, identifier:filter_fields; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:filter_fields; 24, list:[]; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:unfilterable; 28, parenthesized_expression; 28, 29; 29, binary_operator:-; 29, 30; 29, 45; 30, binary_operator:-; 30, 31; 30, 41; 31, call; 31, 32; 31, 33; 32, identifier:set; 33, argument_list; 33, 34; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:request; 38, identifier:query; 39, identifier:keys; 40, argument_list; 41, call; 41, 42; 41, 43; 42, identifier:set; 43, argument_list; 43, 44; 44, identifier:filter_fields; 45, call; 45, 46; 45, 47; 46, identifier:set; 47, argument_list; 47, 48; 48, identifier:standard_params; 49, if_statement; 49, 50; 49, 51; 50, identifier:unfilterable; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:bottle; 56, identifier:abort; 57, argument_list; 57, 58; 57, 59; 58, integer:400; 59, binary_operator:%; 59, 60; 59, 63; 60, concatenated_string; 60, 61; 60, 62; 61, string:"The following query params were invalid: %s. "; 62, string:"Try one (or more) of %s."; 63, tuple; 63, 64; 63, 70; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, string:", "; 67, identifier:join; 68, argument_list; 68, 69; 69, identifier:unfilterable; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, string:", "; 73, identifier:join; 74, argument_list; 74, 75; 75, identifier:filter_fields; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:query_fields; 79, boolean_operator:or; 79, 80; 79, 81; 80, identifier:defaults; 81, dictionary; 82, for_statement; 82, 83; 82, 84; 82, 87; 83, identifier:key; 84, attribute; 84, 85; 84, 86; 85, identifier:request; 86, identifier:query; 87, block; 87, 88; 88, if_statement; 88, 89; 88, 92; 88, 93; 88, 94; 89, comparison_operator:in; 89, 90; 89, 91; 90, identifier:key; 91, identifier:filter_fields; 92, comment; 93, comment; 94, block; 94, 95; 94, 106; 94, 128; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:matches; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:request; 102, identifier:query; 103, identifier:getall; 104, argument_list; 104, 105; 105, identifier:key; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:matches; 109, call; 109, 110; 109, 111; 110, identifier:list; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:itertools; 115, identifier:chain; 116, argument_list; 116, 117; 117, list_splat; 117, 118; 118, generator_expression; 118, 119; 118, 125; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:k; 122, identifier:split; 123, argument_list; 123, 124; 124, string:','; 125, for_in_clause; 125, 126; 125, 127; 126, identifier:k; 127, identifier:matches; 128, if_statement; 128, 129; 128, 135; 128, 142; 129, comparison_operator:>; 129, 130; 129, 134; 130, call; 130, 131; 130, 132; 131, identifier:len; 132, argument_list; 132, 133; 133, identifier:matches; 134, integer:1; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:query_fields; 140, identifier:key; 141, identifier:matches; 142, else_clause; 142, 143; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:query_fields; 148, identifier:key; 149, subscript; 149, 150; 149, 151; 150, identifier:matches; 151, integer:0; 152, if_statement; 152, 153; 152, 158; 153, comparison_operator:in; 153, 154; 153, 155; 154, string:'sort'; 155, attribute; 155, 156; 155, 157; 156, identifier:request; 157, identifier:query; 158, block; 158, 159; 158, 170; 158, 193; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:sort; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:request; 166, identifier:query; 167, identifier:getall; 168, argument_list; 168, 169; 169, string:'sort'; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:sort; 173, call; 173, 174; 173, 175; 174, identifier:list; 175, argument_list; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:itertools; 179, identifier:chain; 180, argument_list; 180, 181; 181, list_splat; 181, 182; 182, generator_expression; 182, 183; 182, 190; 183, call; 183, 184; 183, 185; 184, identifier:comma_separated_strings; 185, argument_list; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:str; 188, argument_list; 188, 189; 189, identifier:k; 190, for_in_clause; 190, 191; 190, 192; 191, identifier:k; 192, identifier:sort; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:query_fields; 197, string:'sort'; 198, identifier:sort; 199, if_statement; 199, 200; 199, 205; 200, comparison_operator:in; 200, 201; 200, 202; 201, string:'q'; 202, attribute; 202, 203; 202, 204; 203, identifier:request; 204, identifier:query; 205, block; 205, 206; 205, 217; 205, 239; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:search; 209, call; 209, 210; 209, 215; 210, attribute; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:request; 213, identifier:query; 214, identifier:getall; 215, argument_list; 215, 216; 216, string:'q'; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:search; 220, call; 220, 221; 220, 222; 221, identifier:list; 222, argument_list; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:itertools; 226, identifier:chain; 227, argument_list; 227, 228; 228, list_splat; 228, 229; 229, generator_expression; 229, 230; 229, 234; 229, 237; 230, call; 230, 231; 230, 232; 231, identifier:comma_separated_strings; 232, argument_list; 232, 233; 233, identifier:k; 234, for_in_clause; 234, 235; 234, 236; 235, identifier:k; 236, identifier:search; 237, if_clause; 237, 238; 238, identifier:k; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:query_fields; 243, string:'q'; 244, identifier:search; 245, return_statement; 245, 246; 246, identifier:query_fields
def process_params(request, standard_params=STANDARD_QUERY_PARAMS, filter_fields=None, defaults=None): """Parse query params. Parses, validates, and converts query into a consistent format. :keyword request: the bottle request :keyword standard_params: query params that are present in most of our (opinionated) APIs (ex. limit, offset, sort, q, and facets) :keyword filter_fields: list of field names to allow filtering on :keyword defaults: dict of params and their default values :retuns: dict of query params with supplied values (string or list) """ if not filter_fields: filter_fields = [] unfilterable = (set(request.query.keys()) - set(filter_fields) - set(standard_params)) if unfilterable: bottle.abort(400, "The following query params were invalid: %s. " "Try one (or more) of %s." % (", ".join(unfilterable), ", ".join(filter_fields))) query_fields = defaults or {} for key in request.query: if key in filter_fields: # turns ?netloc=this.com&netloc=that.com,what.net into # {'netloc': ['this.com', 'that.com', 'what.net']} matches = request.query.getall(key) matches = list(itertools.chain(*(k.split(',') for k in matches))) if len(matches) > 1: query_fields[key] = matches else: query_fields[key] = matches[0] if 'sort' in request.query: sort = request.query.getall('sort') sort = list(itertools.chain(*( comma_separated_strings(str(k)) for k in sort))) query_fields['sort'] = sort if 'q' in request.query: search = request.query.getall('q') search = list(itertools.chain(*( comma_separated_strings(k) for k in search if k))) query_fields['q'] = search return query_fields
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:_weave_dohist; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 4, identifier:data; 5, identifier:s; 6, identifier:binsize; 7, identifier:hist; 8, identifier:rev; 9, default_parameter; 9, 10; 9, 11; 10, identifier:dorev; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:verbose; 14, integer:0; 15, block; 15, 16; 15, 29; 15, 31; 15, 35; 15, 63; 16, if_statement; 16, 17; 16, 18; 16, 23; 17, identifier:dorev; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:dorev; 22, integer:1; 23, else_clause; 23, 24; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:dorev; 28, integer:0; 29, expression_statement; 29, 30; 30, comment; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:code; 34, comment; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:scipy; 40, identifier:weave; 41, identifier:inline; 42, argument_list; 42, 43; 42, 44; 42, 51; 42, 60; 43, identifier:code; 44, list:['data','s','binsize','hist','rev','dorev']; 44, 45; 44, 46; 44, 47; 44, 48; 44, 49; 44, 50; 45, string:'data'; 46, string:'s'; 47, string:'binsize'; 48, string:'hist'; 49, string:'rev'; 50, string:'dorev'; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:type_converters; 53, attribute; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:scipy; 57, identifier:weave; 58, identifier:converters; 59, identifier:blitz; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:verbose; 62, identifier:verbose; 63, return_statement
def _weave_dohist(data, s, binsize, hist, rev, dorev=False, verbose=0): if dorev: dorev=1 else: dorev=0 """ Weave version of histogram with reverse_indices s is an index into data, sorted and possibly a subset """ code = """ int64_t nbin = hist.size(); int64_t binnum_old = -1; // index of minimum value int64_t imin = s(0); for (int64_t i=0; i<s.size(); i++) { int64_t offset = i+nbin+1; int64_t data_index = s(i); if (dorev) { rev(offset) = data_index; } int64_t binnum = (int64_t) ( (data(data_index)-data(imin))/binsize); if (binnum >= 0 && binnum < nbin) { if (dorev && (binnum > binnum_old) ) { int64_t tbin = binnum_old + 1; while (tbin <= binnum) { rev(tbin) = offset; tbin++; } } hist(binnum) = hist(binnum) + 1; binnum_old = binnum; } } int64_t tbin = binnum_old + 1; while (tbin <= nbin) { if (dorev) { rev(tbin) = rev.size(); } tbin++; } """ scipy.weave.inline(code, ['data','s','binsize','hist','rev','dorev'], type_converters = scipy.weave.converters.blitz, verbose=verbose) return
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:loose_search; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:asset_manager_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:query; 8, string:''; 9, dictionary_splat_pattern; 9, 10; 10, identifier:kwargs; 11, block; 11, 12; 11, 14; 11, 24; 11, 40; 11, 47; 11, 92; 11, 106; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:logger; 20, identifier:info; 21, argument_list; 21, 22; 21, 23; 22, string:'Asset Search - Asset Manager: %s'; 23, identifier:asset_manager_id; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:url; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, string:'{endpoint}/assets/search/{asset_manager_id}'; 30, identifier:format; 31, argument_list; 31, 32; 31, 35; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:asset_manager_id; 34, identifier:asset_manager_id; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:endpoint; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:endpoint; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:params; 43, dictionary; 43, 44; 44, pair; 44, 45; 44, 46; 45, string:'query'; 46, identifier:query; 47, for_statement; 47, 48; 47, 51; 47, 56; 48, pattern_list; 48, 49; 48, 50; 49, identifier:k; 50, identifier:v; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:kwargs; 54, identifier:items; 55, argument_list; 56, block; 56, 57; 56, 86; 57, if_statement; 57, 58; 57, 70; 58, boolean_operator:and; 58, 59; 58, 65; 59, not_operator; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:isinstance; 62, argument_list; 62, 63; 62, 64; 63, identifier:v; 64, identifier:str; 65, call; 65, 66; 65, 67; 66, identifier:isinstance; 67, argument_list; 67, 68; 67, 69; 68, identifier:v; 69, identifier:Iterable; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:v; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, string:','; 77, identifier:join; 78, generator_expression; 78, 79; 78, 83; 79, call; 79, 80; 79, 81; 80, identifier:str; 81, argument_list; 81, 82; 82, identifier:i; 83, for_in_clause; 83, 84; 83, 85; 84, identifier:i; 85, identifier:v; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, subscript; 88, 89; 88, 90; 89, identifier:params; 90, identifier:k; 91, identifier:v; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:response; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:session; 100, identifier:get; 101, argument_list; 101, 102; 101, 103; 102, identifier:url; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:params; 105, identifier:params; 106, if_statement; 106, 107; 106, 110; 106, 151; 107, attribute; 107, 108; 107, 109; 108, identifier:response; 109, identifier:ok; 110, block; 110, 111; 110, 119; 110, 136; 110, 149; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:data; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:response; 117, identifier:json; 118, argument_list; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:assets; 122, list_comprehension; 122, 123; 122, 127; 123, call; 123, 124; 123, 125; 124, identifier:json_to_asset; 125, argument_list; 125, 126; 126, identifier:json_asset; 127, for_in_clause; 127, 128; 127, 129; 128, identifier:json_asset; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:data; 132, identifier:get; 133, argument_list; 133, 134; 133, 135; 134, string:'hits'; 135, list:[]; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:logger; 142, identifier:info; 143, argument_list; 143, 144; 143, 145; 144, string:'Returned %s Assets.'; 145, call; 145, 146; 145, 147; 146, identifier:len; 147, argument_list; 147, 148; 148, identifier:assets; 149, return_statement; 149, 150; 150, identifier:assets; 151, else_clause; 151, 152; 152, block; 152, 153; 152, 164; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:logger; 159, identifier:error; 160, argument_list; 160, 161; 161, attribute; 161, 162; 161, 163; 162, identifier:response; 163, identifier:text; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:response; 168, identifier:raise_for_status; 169, argument_list
def loose_search(self, asset_manager_id, query='', **kwargs): """ Asset search API. Possible kwargs: * threshold: int (default = 0) * page_no: int (default = 1) * page_size: int (default = 100) * sort_fields: list (default = []) * asset_types: list (default = []) * include_public: bool (default = True) * include_data_sources: bool (default = True) """ self.logger.info('Asset Search - Asset Manager: %s', asset_manager_id) url = '{endpoint}/assets/search/{asset_manager_id}'.format( asset_manager_id=asset_manager_id, endpoint=self.endpoint, ) params = {'query': query} for k, v in kwargs.items(): if not isinstance(v, str) and isinstance(v, Iterable): v = ','.join(str(i) for i in v) params[k] = v response = self.session.get(url, params=params) if response.ok: data = response.json() assets = [json_to_asset(json_asset) for json_asset in data.get('hits', [])] self.logger.info('Returned %s Assets.', len(assets)) return assets else: self.logger.error(response.text) response.raise_for_status()
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, 15; 5, 32; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 9, comparison_operator:is; 9, 10; 9, 11; 10, identifier:item; 11, None; 12, block; 12, 13; 13, return_statement; 13, 14; 14, integer:0; 15, function_definition; 15, 16; 15, 17; 15, 19; 16, function_name:try_int; 17, parameters; 17, 18; 18, identifier:s; 19, block; 19, 20; 20, try_statement; 20, 21; 20, 27; 21, block; 21, 22; 22, return_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:int; 25, argument_list; 25, 26; 26, identifier:s; 27, except_clause; 27, 28; 27, 29; 28, identifier:ValueError; 29, block; 29, 30; 30, return_statement; 30, 31; 31, identifier:s; 32, return_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:tuple; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:map; 38, argument_list; 38, 39; 38, 40; 39, identifier:try_int; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:re; 43, identifier:findall; 44, argument_list; 44, 45; 44, 46; 45, string:r'(\d+|\D+)'; 46, identifier:item
def natural_sort(item): """ Sort strings that contain numbers correctly. >>> l = ['v1.3.12', 'v1.3.3', 'v1.2.5', 'v1.2.15', 'v1.2.3', 'v1.2.1'] >>> l.sort(key=natural_sort) >>> print l "['v1.2.1', 'v1.2.3', 'v1.2.5', 'v1.2.15', 'v1.3.3', 'v1.3.12']" """ if item is None: return 0 def try_int(s): try: return int(s) except ValueError: return s return tuple(map(try_int, re.findall(r'(\d+|\D+)', item)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:get_html_attrs; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:kwargs; 6, None; 7, block; 7, 8; 7, 10; 7, 16; 7, 20; 7, 24; 7, 38; 7, 75; 7, 85; 7, 153; 7, 159; 7, 165; 7, 172; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:kwargs; 13, boolean_operator:or; 13, 14; 13, 15; 14, identifier:kwargs; 15, dictionary; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:attrs; 19, list:[]; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:props; 23, list:[]; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:classes; 27, call; 27, 28; 27, 37; 28, attribute; 28, 29; 28, 36; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:kwargs; 32, identifier:get; 33, argument_list; 33, 34; 33, 35; 34, string:'classes'; 35, string:''; 36, identifier:strip; 37, argument_list; 38, if_statement; 38, 39; 38, 40; 39, identifier:classes; 40, block; 40, 41; 40, 56; 40, 66; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:classes; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, string:' '; 47, identifier:join; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:re; 52, identifier:split; 53, argument_list; 53, 54; 53, 55; 54, string:r'\s+'; 55, identifier:classes; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:classes; 59, call; 59, 60; 59, 61; 60, identifier:to_unicode; 61, argument_list; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:quoteattr; 64, argument_list; 64, 65; 65, identifier:classes; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:attrs; 70, identifier:append; 71, argument_list; 71, 72; 72, binary_operator:%; 72, 73; 72, 74; 73, string:'class=%s'; 74, identifier:classes; 75, try_statement; 75, 76; 75, 81; 76, block; 76, 77; 77, delete_statement; 77, 78; 78, subscript; 78, 79; 78, 80; 79, identifier:kwargs; 80, string:'classes'; 81, except_clause; 81, 82; 81, 83; 82, identifier:KeyError; 83, block; 83, 84; 84, pass_statement; 85, for_statement; 85, 86; 85, 89; 85, 93; 86, pattern_list; 86, 87; 86, 88; 87, identifier:key; 88, identifier:value; 89, call; 89, 90; 89, 91; 90, identifier:iteritems; 91, argument_list; 91, 92; 92, identifier:kwargs; 93, block; 93, 94; 93, 104; 93, 111; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:key; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:key; 100, identifier:replace; 101, argument_list; 101, 102; 101, 103; 102, string:'_'; 103, string:'-'; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:key; 107, call; 107, 108; 107, 109; 108, identifier:to_unicode; 109, argument_list; 109, 110; 110, identifier:key; 111, if_statement; 111, 112; 111, 117; 111, 130; 112, call; 112, 113; 112, 114; 113, identifier:isinstance; 114, argument_list; 114, 115; 114, 116; 115, identifier:value; 116, identifier:bool; 117, block; 117, 118; 118, if_statement; 118, 119; 118, 122; 119, comparison_operator:is; 119, 120; 119, 121; 120, identifier:value; 121, True; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:props; 127, identifier:append; 128, argument_list; 128, 129; 129, identifier:key; 130, else_clause; 130, 131; 131, block; 131, 132; 131, 142; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:value; 135, call; 135, 136; 135, 137; 136, identifier:quoteattr; 137, argument_list; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:Markup; 140, argument_list; 140, 141; 141, identifier:value; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:attrs; 146, identifier:append; 147, argument_list; 147, 148; 148, binary_operator:%; 148, 149; 148, 150; 149, string:u'%s=%s'; 150, tuple; 150, 151; 150, 152; 151, identifier:key; 152, identifier:value; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:attrs; 157, identifier:sort; 158, argument_list; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:props; 163, identifier:sort; 164, argument_list; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:attrs; 169, identifier:extend; 170, argument_list; 170, 171; 171, identifier:props; 172, return_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, string:u' '; 176, identifier:join; 177, argument_list; 177, 178; 178, identifier:attrs
def get_html_attrs(kwargs=None): """Generate HTML attributes from the provided keyword arguments. The output value is sorted by the passed keys, to provide consistent output. Because of the frequent use of the normally reserved keyword `class`, `classes` is used instead. Also, all underscores are translated to regular dashes. Set any property with a `True` value. >>> _get_html_attrs({'id': 'text1', 'classes': 'myclass', 'data_id': 1, 'checked': True}) u'class="myclass" data-id="1" id="text1" checked' """ kwargs = kwargs or {} attrs = [] props = [] classes = kwargs.get('classes', '').strip() if classes: classes = ' '.join(re.split(r'\s+', classes)) classes = to_unicode(quoteattr(classes)) attrs.append('class=%s' % classes) try: del kwargs['classes'] except KeyError: pass for key, value in iteritems(kwargs): key = key.replace('_', '-') key = to_unicode(key) if isinstance(value, bool): if value is True: props.append(key) else: value = quoteattr(Markup(value)) attrs.append(u'%s=%s' % (key, value)) attrs.sort() props.sort() attrs.extend(props) return u' '.join(attrs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_dependencies; 3, parameters; 3, 4; 4, identifier:objects; 5, block; 5, 6; 5, 8; 5, 15; 5, 16; 5, 20; 5, 26; 5, 32; 5, 39; 5, 65; 5, 178; 5, 184; 5, 185; 5, 186; 5, 187; 5, 188; 5, 189; 5, 190; 5, 191; 5, 192; 5, 196; 5, 325; 5, 329; 5, 342; 6, expression_statement; 6, 7; 7, comment; 8, import_from_statement; 8, 9; 8, 13; 9, dotted_name; 9, 10; 9, 11; 9, 12; 10, identifier:django; 11, identifier:db; 12, identifier:models; 13, dotted_name; 13, 14; 14, identifier:get_model; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:model_dependencies; 19, list:[]; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:models; 23, call; 23, 24; 23, 25; 24, identifier:set; 25, argument_list; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:model_list; 29, call; 29, 30; 29, 31; 30, identifier:set; 31, argument_list; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:objs_by_model; 35, call; 35, 36; 35, 37; 36, identifier:defaultdict; 37, argument_list; 37, 38; 38, identifier:list; 39, for_statement; 39, 40; 39, 41; 39, 42; 40, identifier:o; 41, identifier:objects; 42, block; 42, 43; 42, 49; 42, 58; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:model; 46, attribute; 46, 47; 46, 48; 47, identifier:o; 48, identifier:__class__; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:objs_by_model; 54, identifier:model; 55, identifier:append; 56, argument_list; 56, 57; 57, identifier:o; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:model_list; 62, identifier:add; 63, argument_list; 63, 64; 64, identifier:model; 65, for_statement; 65, 66; 65, 67; 65, 68; 66, identifier:model; 67, identifier:model_list; 68, block; 68, 69; 68, 76; 68, 77; 68, 121; 68, 122; 68, 123; 68, 169; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:models; 73, identifier:add; 74, argument_list; 74, 75; 75, identifier:model; 76, comment; 77, if_statement; 77, 78; 77, 83; 77, 115; 78, call; 78, 79; 78, 80; 79, identifier:hasattr; 80, argument_list; 80, 81; 80, 82; 81, identifier:model; 82, string:'natural_key'; 83, block; 83, 84; 83, 95; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:deps; 87, call; 87, 88; 87, 89; 88, identifier:getattr; 89, argument_list; 89, 90; 89, 93; 89, 94; 90, attribute; 90, 91; 90, 92; 91, identifier:model; 92, identifier:natural_key; 93, string:'dependencies'; 94, list:[]; 95, if_statement; 95, 96; 95, 97; 96, identifier:deps; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:deps; 101, list_comprehension; 101, 102; 101, 112; 102, call; 102, 103; 102, 104; 103, identifier:get_model; 104, argument_list; 104, 105; 105, list_splat; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:d; 109, identifier:split; 110, argument_list; 110, 111; 111, string:'.'; 112, for_in_clause; 112, 113; 112, 114; 113, identifier:d; 114, identifier:deps; 115, else_clause; 115, 116; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:deps; 120, list:[]; 121, comment; 122, comment; 123, for_statement; 123, 124; 123, 125; 123, 140; 124, identifier:field; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:itertools; 128, identifier:chain; 129, argument_list; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:model; 133, identifier:_meta; 134, identifier:fields; 135, attribute; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:model; 138, identifier:_meta; 139, identifier:many_to_many; 140, block; 140, 141; 141, if_statement; 141, 142; 141, 157; 142, boolean_operator:and; 142, 143; 142, 150; 143, call; 143, 144; 143, 145; 144, identifier:hasattr; 145, argument_list; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:field; 148, identifier:rel; 149, string:'to'; 150, comparison_operator:!=; 150, 151; 150, 156; 151, attribute; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:field; 154, identifier:rel; 155, identifier:to; 156, identifier:model; 157, block; 157, 158; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:deps; 162, identifier:append; 163, argument_list; 163, 164; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:field; 167, identifier:rel; 168, identifier:to; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:model_dependencies; 173, identifier:append; 174, argument_list; 174, 175; 175, tuple; 175, 176; 175, 177; 176, identifier:model; 177, identifier:deps; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:model_dependencies; 182, identifier:reverse; 183, argument_list; 184, comment; 185, comment; 186, comment; 187, comment; 188, comment; 189, comment; 190, comment; 191, comment; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:model_list; 195, list:[]; 196, while_statement; 196, 197; 196, 198; 197, identifier:model_dependencies; 198, block; 198, 199; 198, 203; 198, 207; 198, 275; 198, 321; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:skipped; 202, list:[]; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:changed; 206, False; 207, while_statement; 207, 208; 207, 209; 208, identifier:model_dependencies; 209, block; 209, 210; 209, 220; 209, 221; 209, 222; 209, 223; 209, 227; 209, 250; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, pattern_list; 212, 213; 212, 214; 213, identifier:model; 214, identifier:deps; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:model_dependencies; 218, identifier:pop; 219, argument_list; 220, comment; 221, comment; 222, comment; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:found; 226, True; 227, for_statement; 227, 228; 227, 229; 227, 241; 228, identifier:candidate; 229, generator_expression; 229, 230; 229, 238; 230, parenthesized_expression; 230, 231; 231, boolean_operator:or; 231, 232; 231, 235; 232, comparison_operator:not; 232, 233; 232, 234; 233, identifier:d; 234, identifier:models; 235, comparison_operator:in; 235, 236; 235, 237; 236, identifier:d; 237, identifier:model_list; 238, for_in_clause; 238, 239; 238, 240; 239, identifier:d; 240, identifier:deps; 241, block; 241, 242; 242, if_statement; 242, 243; 242, 245; 243, not_operator; 243, 244; 244, identifier:candidate; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:found; 249, False; 250, if_statement; 250, 251; 250, 252; 250, 264; 251, identifier:found; 252, block; 252, 253; 252, 260; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:model_list; 257, identifier:append; 258, argument_list; 258, 259; 259, identifier:model; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:changed; 263, True; 264, else_clause; 264, 265; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:skipped; 270, identifier:append; 271, argument_list; 271, 272; 272, tuple; 272, 273; 272, 274; 273, identifier:model; 274, identifier:deps; 275, if_statement; 275, 276; 275, 278; 276, not_operator; 276, 277; 277, identifier:changed; 278, block; 278, 279; 279, raise_statement; 279, 280; 280, call; 280, 281; 280, 282; 281, identifier:CommandError; 282, argument_list; 282, 283; 283, binary_operator:%; 283, 284; 283, 285; 284, string:"Can't resolve dependencies for %s in serialized app list."; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, string:', '; 288, identifier:join; 289, generator_expression; 289, 290; 289, 303; 290, binary_operator:%; 290, 291; 290, 292; 291, string:'%s.%s'; 292, tuple; 292, 293; 292, 298; 293, attribute; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:model; 296, identifier:_meta; 297, identifier:app_label; 298, attribute; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:model; 301, identifier:_meta; 302, identifier:object_name; 303, for_in_clause; 303, 304; 303, 307; 304, pattern_list; 304, 305; 304, 306; 305, identifier:model; 306, identifier:deps; 307, call; 307, 308; 307, 309; 308, identifier:sorted; 309, argument_list; 309, 310; 309, 311; 310, identifier:skipped; 311, keyword_argument; 311, 312; 311, 313; 312, identifier:key; 313, lambda; 313, 314; 313, 316; 314, lambda_parameters; 314, 315; 315, identifier:obj; 316, attribute; 316, 317; 316, 320; 317, subscript; 317, 318; 317, 319; 318, identifier:obj; 319, integer:0; 320, identifier:__name__; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:model_dependencies; 324, identifier:skipped; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:sorted_results; 328, list:[]; 329, for_statement; 329, 330; 329, 331; 329, 332; 330, identifier:model; 331, identifier:model_list; 332, block; 332, 333; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:sorted_results; 337, identifier:extend; 338, argument_list; 338, 339; 339, subscript; 339, 340; 339, 341; 340, identifier:objs_by_model; 341, identifier:model; 342, return_statement; 342, 343; 343, identifier:sorted_results
def sort_dependencies(objects): """ Sort a list of instances by their model dependancy graph. This is very similar to Django's sort_dependencies method except for two big differences: 1. We graph dependencies unrelated to natural_key. 2. We take a list of objects, and return a sorted list of objects. """ from django.db.models import get_model # Process the list of models, and get the list of dependencies model_dependencies = [] models = set() model_list = set() objs_by_model = defaultdict(list) for o in objects: model = o.__class__ objs_by_model[model].append(o) model_list.add(model) for model in model_list: models.add(model) # Add any explicitly defined dependencies if hasattr(model, 'natural_key'): deps = getattr(model.natural_key, 'dependencies', []) if deps: deps = [get_model(*d.split('.')) for d in deps] else: deps = [] # Now add a dependency for any FK or M2M relation with # a model that defines a natural key for field in itertools.chain(model._meta.fields, model._meta.many_to_many): if hasattr(field.rel, 'to') and field.rel.to != model: deps.append(field.rel.to) model_dependencies.append((model, deps)) model_dependencies.reverse() # Now sort the models to ensure that dependencies are met. This # is done by repeatedly iterating over the input list of models. # If all the dependencies of a given model are in the final list, # that model is promoted to the end of the final list. This process # continues until the input list is empty, or we do a full iteration # over the input models without promoting a model to the final list. # If we do a full iteration without a promotion, that means there are # circular dependencies in the list. model_list = [] while model_dependencies: skipped = [] changed = False while model_dependencies: model, deps = model_dependencies.pop() # If all of the models in the dependency list are either already # on the final model list, or not on the original serialization list, # then we've found another model with all it's dependencies satisfied. found = True for candidate in ((d not in models or d in model_list) for d in deps): if not candidate: found = False if found: model_list.append(model) changed = True else: skipped.append((model, deps)) if not changed: raise CommandError("Can't resolve dependencies for %s in serialized app list." % ', '.join('%s.%s' % (model._meta.app_label, model._meta.object_name) for model, deps in sorted(skipped, key=lambda obj: obj[0].__name__)) ) model_dependencies = skipped sorted_results = [] for model in model_list: sorted_results.extend(objs_by_model[model]) return sorted_results
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:sort_languages; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:order; 7, attribute; 7, 8; 7, 9; 8, identifier:Qt; 9, identifier:AscendingOrder; 10, block; 10, 11; 10, 13; 10, 19; 10, 42; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:beginResetModel; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:__languages; 24, call; 24, 25; 24, 26; 25, identifier:sorted; 26, argument_list; 26, 27; 26, 30; 26, 39; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:__languages; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:key; 32, lambda; 32, 33; 32, 35; 33, lambda_parameters; 33, 34; 34, identifier:x; 35, parenthesized_expression; 35, 36; 36, attribute; 36, 37; 36, 38; 37, identifier:x; 38, identifier:name; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:reverse; 41, identifier:order; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:endResetModel; 47, argument_list
def sort_languages(self, order=Qt.AscendingOrder): """ Sorts the Model languages. :param order: Order. ( Qt.SortOrder ) """ self.beginResetModel() self.__languages = sorted(self.__languages, key=lambda x: (x.name), reverse=order) self.endResetModel()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:fmt_pairs; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:obj; 5, default_parameter; 5, 6; 5, 7; 6, identifier:indent; 7, integer:4; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort_key; 10, None; 11, block; 11, 12; 11, 14; 11, 27; 11, 33; 11, 40; 11, 50; 11, 60; 11, 81; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:lengths; 17, list_comprehension; 17, 18; 17, 24; 18, call; 18, 19; 18, 20; 19, identifier:len; 20, argument_list; 20, 21; 21, subscript; 21, 22; 21, 23; 22, identifier:x; 23, integer:0; 24, for_in_clause; 24, 25; 24, 26; 25, identifier:x; 26, identifier:obj; 27, if_statement; 27, 28; 27, 30; 28, not_operator; 28, 29; 29, identifier:lengths; 30, block; 30, 31; 31, return_statement; 31, 32; 32, string:''; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:longest; 36, call; 36, 37; 36, 38; 37, identifier:max; 38, argument_list; 38, 39; 39, identifier:lengths; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:obj; 43, call; 43, 44; 43, 45; 44, identifier:sorted; 45, argument_list; 45, 46; 45, 47; 46, identifier:obj; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:key; 49, identifier:sort_key; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:formatter; 53, binary_operator:%; 53, 54; 53, 55; 54, string:'%s{: <%d} {}'; 55, tuple; 55, 56; 55, 59; 56, binary_operator:*; 56, 57; 56, 58; 57, string:' '; 58, identifier:indent; 59, identifier:longest; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:string; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, string:'\n'; 66, identifier:join; 67, argument_list; 67, 68; 68, list_comprehension; 68, 69; 68, 76; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:formatter; 72, identifier:format; 73, argument_list; 73, 74; 73, 75; 74, identifier:k; 75, identifier:v; 76, for_in_clause; 76, 77; 76, 80; 77, pattern_list; 77, 78; 77, 79; 78, identifier:k; 79, identifier:v; 80, identifier:obj; 81, return_statement; 81, 82; 82, identifier:string
def fmt_pairs(obj, indent=4, sort_key=None): """Format and sort a list of pairs, usually for printing. If sort_key is provided, the value will be passed as the 'key' keyword argument of the sorted() function when sorting the items. This allows for the input such as [('A', 3), ('B', 5), ('Z', 1)] to be sorted by the ints but formatted like so: l = [('A', 3), ('B', 5), ('Z', 1)] print(fmt_pairs(l, sort_key=lambda x: x[1])) Z 1 A 3 B 5 where the default behavior would be: print(fmt_pairs(l)) A 3 B 5 Z 1 """ lengths = [len(x[0]) for x in obj] if not lengths: return '' longest = max(lengths) obj = sorted(obj, key=sort_key) formatter = '%s{: <%d} {}' % (' ' * indent, longest) string = '\n'.join([formatter.format(k, v) for k, v in obj]) return string
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:serialize; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:as_dict; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort; 10, None; 11, block; 11, 12; 11, 14; 11, 27; 11, 50; 11, 51; 11, 67; 11, 91; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:files; 17, call; 17, 18; 17, 19; 18, identifier:getattr; 19, argument_list; 19, 20; 19, 21; 19, 22; 20, identifier:self; 21, string:'files'; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:run; 26, argument_list; 27, if_statement; 27, 28; 27, 29; 28, identifier:as_dict; 29, block; 29, 30; 30, return_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:dict; 33, generator_expression; 33, 34; 33, 41; 34, tuple; 34, 35; 34, 36; 35, identifier:fn; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:p; 39, identifier:to_dict; 40, argument_list; 41, for_in_clause; 41, 42; 41, 45; 42, pattern_list; 42, 43; 42, 44; 43, identifier:fn; 44, identifier:p; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:files; 48, identifier:items; 49, argument_list; 50, comment; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:data; 54, generator_expression; 54, 55; 54, 60; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:p; 58, identifier:to_dict; 59, argument_list; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:p; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:files; 65, identifier:values; 66, argument_list; 67, if_statement; 67, 68; 67, 72; 67, 81; 68, call; 68, 69; 68, 70; 69, identifier:callable; 70, argument_list; 70, 71; 71, identifier:sort; 72, block; 72, 73; 73, return_statement; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:sorted; 76, argument_list; 76, 77; 76, 78; 77, identifier:data; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:key; 80, identifier:sort; 81, elif_clause; 81, 82; 81, 85; 82, comparison_operator:is; 82, 83; 82, 84; 83, identifier:sort; 84, True; 85, block; 85, 86; 86, return_statement; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:sorted; 89, argument_list; 89, 90; 90, identifier:data; 91, return_statement; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:list; 94, argument_list; 94, 95; 95, identifier:data
def serialize(self, as_dict=False, sort=None): """ Dump built files as a list or dictionary, for JSON or other serialization. sort: a key function to sort a list, or simply True """ files = getattr(self, 'files', self.run()) if as_dict: return dict((fn, p.to_dict()) for fn, p in files.items()) # generate a list data = (p.to_dict() for p in files.values()) if callable(sort): return sorted(data, key=sort) elif sort is True: return sorted(data) return list(data)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:iteritems; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 30; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:sorted_data; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 13, 21; 13, 24; 13, 27; 14, call; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:data; 19, identifier:iteritems; 20, argument_list; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:cmp; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:key; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:reverse; 30, for_statement; 30, 31; 30, 34; 30, 35; 31, pattern_list; 31, 32; 31, 33; 32, identifier:k; 33, identifier:v; 34, identifier:sorted_data; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, yield; 37, 38; 38, expression_list; 38, 39; 38, 40; 39, identifier:k; 40, identifier:v
def iteritems(self): """ Sort and then iterate the dictionary """ sorted_data = sorted(self.data.iteritems(), self.cmp, self.key, self.reverse) for k,v in sorted_data: yield k,v
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_posts; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:num; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:tag; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:private; 13, False; 14, block; 14, 15; 14, 17; 14, 23; 14, 39; 14, 56; 14, 65; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:posts; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:posts; 23, if_statement; 23, 24; 23, 26; 24, not_operator; 24, 25; 25, identifier:private; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:posts; 30, list_comprehension; 30, 31; 30, 32; 30, 35; 31, identifier:post; 32, for_in_clause; 32, 33; 32, 34; 33, identifier:post; 34, identifier:posts; 35, if_clause; 35, 36; 36, attribute; 36, 37; 36, 38; 37, identifier:post; 38, identifier:public; 39, if_statement; 39, 40; 39, 41; 40, identifier:tag; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:posts; 45, list_comprehension; 45, 46; 45, 47; 45, 50; 46, identifier:post; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:post; 49, identifier:posts; 50, if_clause; 50, 51; 51, comparison_operator:in; 51, 52; 51, 53; 52, identifier:tag; 53, attribute; 53, 54; 53, 55; 54, identifier:post; 55, identifier:tags; 56, if_statement; 56, 57; 56, 58; 57, identifier:num; 58, block; 58, 59; 59, return_statement; 59, 60; 60, subscript; 60, 61; 60, 62; 61, identifier:posts; 62, slice; 62, 63; 62, 64; 63, colon; 64, identifier:num; 65, return_statement; 65, 66; 66, identifier:posts
def get_posts(self, num=None, tag=None, private=False): """ Get all the posts added to the blog. Args: num (int): Optional. If provided, only return N posts (sorted by date, most recent first). tag (Tag): Optional. If provided, only return posts that have a specific tag. private (bool): By default (if False), private posts are not included. If set to True, private posts will also be included. """ posts = self.posts if not private: posts = [post for post in posts if post.public] if tag: posts = [post for post in posts if tag in post.tags] if num: return posts[:num] return posts
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_sort_function; 3, parameters; 3, 4; 4, identifier:order; 5, block; 5, 6; 5, 8; 5, 28; 5, 73; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:stable; 11, call; 11, 12; 11, 13; 12, identifier:tuple; 13, generator_expression; 13, 14; 13, 25; 14, tuple; 14, 15; 14, 18; 15, subscript; 15, 16; 15, 17; 16, identifier:d; 17, string:'key'; 18, conditional_expression:if; 18, 19; 18, 21; 18, 24; 19, unary_operator:-; 19, 20; 20, integer:1; 21, subscript; 21, 22; 21, 23; 22, identifier:d; 23, string:'reverse'; 24, integer:1; 25, for_in_clause; 25, 26; 25, 27; 26, identifier:d; 27, identifier:order; 28, function_definition; 28, 29; 28, 30; 28, 33; 29, function_name:sort_function; 30, parameters; 30, 31; 30, 32; 31, identifier:a; 32, identifier:b; 33, block; 33, 34; 33, 71; 34, for_statement; 34, 35; 34, 38; 34, 39; 35, pattern_list; 35, 36; 35, 37; 36, identifier:name; 37, identifier:direction; 38, identifier:stable; 39, block; 39, 40; 39, 62; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:v; 43, call; 43, 44; 43, 45; 44, identifier:cmp; 45, argument_list; 45, 46; 45, 54; 46, conditional_expression:if; 46, 47; 46, 52; 46, 53; 47, call; 47, 48; 47, 49; 48, identifier:getattr; 49, argument_list; 49, 50; 49, 51; 50, identifier:a; 51, identifier:name; 52, identifier:a; 53, identifier:a; 54, conditional_expression:if; 54, 55; 54, 60; 54, 61; 55, call; 55, 56; 55, 57; 56, identifier:getattr; 57, argument_list; 57, 58; 57, 59; 58, identifier:b; 59, identifier:name; 60, identifier:b; 61, identifier:b; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:!=; 63, 64; 63, 65; 64, identifier:v; 65, integer:0; 66, block; 66, 67; 67, return_statement; 67, 68; 68, binary_operator:*; 68, 69; 68, 70; 69, identifier:v; 70, identifier:direction; 71, return_statement; 71, 72; 72, integer:0; 73, return_statement; 73, 74; 74, identifier:sort_function
def get_sort_function(order): """ Returns a callable similar to the built-in `cmp`, to be used on objects. Takes a list of dictionaries. In each, 'key' must be a string that is used to get an attribute of the objects to compare, and 'reverse' must be a boolean indicating whether the result should be reversed. """ stable = tuple((d['key'], -1 if d['reverse'] else 1) for d in order) def sort_function(a, b): for name, direction in stable: v = cmp(getattr(a, name) if a else a, getattr(b, name) if b else b) if v != 0: return v * direction return 0 return sort_function
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:create_proxy_model; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:model; 6, block; 6, 7; 6, 9; 6, 16; 6, 23; 6, 34; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:proxy; 12, call; 12, 13; 12, 14; 13, identifier:ReftrackSortFilterModel; 14, argument_list; 14, 15; 15, identifier:self; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:proxy; 20, identifier:setSourceModel; 21, argument_list; 21, 22; 22, identifier:model; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:model; 28, identifier:rowsInserted; 29, identifier:connect; 30, argument_list; 30, 31; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:sort_model; 34, return_statement; 34, 35; 35, identifier:proxy
def create_proxy_model(self, model): """Create a sort filter proxy model for the given model :param model: the model to wrap in a proxy :type model: :class:`QtGui.QAbstractItemModel` :returns: a new proxy model that can be used for sorting and filtering :rtype: :class:`QtGui.QAbstractItemModel` :raises: None """ proxy = ReftrackSortFilterModel(self) proxy.setSourceModel(model) model.rowsInserted.connect(self.sort_model) return proxy
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort_model; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:args; 7, dictionary_splat_pattern; 7, 8; 8, identifier:kwargs; 9, block; 9, 10; 9, 12; 9, 21; 9, 22; 9, 31; 9, 32; 9, 41; 9, 42; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:proxy; 18, identifier:sort; 19, argument_list; 19, 20; 20, integer:17; 21, comment; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 29; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:proxy; 28, identifier:sort; 29, argument_list; 29, 30; 30, integer:2; 31, comment; 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:proxy; 38, identifier:sort; 39, argument_list; 39, 40; 40, integer:1; 41, comment; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:proxy; 48, identifier:sort; 49, argument_list; 49, 50; 50, integer:0
def sort_model(self, *args, **kwargs): """Sort the proxy model :returns: None :rtype: None :raises: None """ self.proxy.sort(17) # sort the identifier self.proxy.sort(2) # sort the element self.proxy.sort(1) # sort the elementgrp self.proxy.sort(0)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:prepare_value; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:value; 6, block; 6, 7; 6, 9; 6, 43; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 17; 10, boolean_operator:and; 10, 11; 10, 14; 11, comparison_operator:is; 11, 12; 11, 13; 12, identifier:value; 13, None; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:required; 17, block; 17, 18; 17, 27; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:choices; 21, call; 21, 22; 21, 23; 22, identifier:list; 23, argument_list; 23, 24; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:choices; 27, if_statement; 27, 28; 27, 34; 28, comparison_operator:==; 28, 29; 28, 33; 29, call; 29, 30; 29, 31; 30, identifier:len; 31, argument_list; 31, 32; 32, identifier:choices; 33, integer:1; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:value; 38, subscript; 38, 39; 38, 42; 39, subscript; 39, 40; 39, 41; 40, identifier:choices; 41, integer:0; 42, integer:0; 43, return_statement; 43, 44; 44, call; 44, 45; 44, 52; 45, attribute; 45, 46; 45, 51; 46, call; 46, 47; 46, 48; 47, identifier:super; 48, argument_list; 48, 49; 48, 50; 49, identifier:TemplateChoiceField; 50, identifier:self; 51, identifier:prepare_value; 52, argument_list; 52, 53; 53, identifier:value
def prepare_value(self, value): """ To avoid evaluating the lazysorted callable more than necessary to establish a potential initial value for the field, we do it here. If there's - only one template choice, and - the field is required, and - there's no prior initial set (either by being bound or by being set higher up the stack then forcibly select the only "good" value as the default. """ if value is None and self.required: choices =list(self.choices) if len(choices) == 1: value = choices[0][0] return super(TemplateChoiceField, self).prepare_value(value)
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; 5, 17; 5, 41; 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:_log; 14, identifier:debug; 15, argument_list; 15, 16; 16, string:'Sorting responses by priority'; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_responses; 22, call; 22, 23; 22, 24; 23, identifier:OrderedDict; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:sorted; 27, argument_list; 27, 28; 27, 38; 28, call; 28, 29; 28, 30; 29, identifier:list; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:_responses; 36, identifier:items; 37, argument_list; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:reverse; 40, True; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:sorted; 46, True
def _sort(self): """ Sort the response dictionaries priority levels for ordered iteration """ self._log.debug('Sorting responses by priority') self._responses = OrderedDict(sorted(list(self._responses.items()), reverse=True)) self.sorted = True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:csv_row_to_transaction; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:index; 5, identifier:row; 6, default_parameter; 6, 7; 6, 8; 7, identifier:source_encoding; 8, string:"latin1"; 9, default_parameter; 9, 10; 9, 11; 10, identifier:date_format; 11, string:"%d-%m-%Y"; 12, default_parameter; 12, 13; 12, 14; 13, identifier:thousand_sep; 14, string:"."; 15, default_parameter; 15, 16; 15, 17; 16, identifier:decimal_sep; 17, string:","; 18, block; 18, 19; 18, 21; 18, 30; 18, 39; 18, 48; 18, 58; 18, 67; 18, 76; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 29; 23, pattern_list; 23, 24; 23, 25; 23, 26; 23, 27; 23, 28; 24, identifier:xfer; 25, identifier:posted; 26, identifier:message; 27, identifier:amount; 28, identifier:total; 29, identifier:row; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:xfer; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:Parse; 36, identifier:date; 37, argument_list; 37, 38; 38, identifier:xfer; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:posted; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:Parse; 45, identifier:date; 46, argument_list; 46, 47; 47, identifier:posted; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:message; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:Parse; 54, identifier:to_utf8; 55, argument_list; 55, 56; 55, 57; 56, identifier:message; 57, identifier:source_encoding; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:amount; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:Parse; 64, identifier:money; 65, argument_list; 65, 66; 66, identifier:amount; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:total; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:Parse; 73, identifier:money; 74, argument_list; 74, 75; 75, identifier:total; 76, return_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:Transaction; 79, argument_list; 79, 80; 79, 81; 79, 82; 79, 83; 79, 84; 79, 85; 80, identifier:index; 81, identifier:xfer; 82, identifier:posted; 83, identifier:message; 84, identifier:amount; 85, identifier:total
def csv_row_to_transaction(index, row, source_encoding="latin1", date_format="%d-%m-%Y", thousand_sep=".", decimal_sep=","): """ Parses a row of strings to a ``Transaction`` object. Args: index: The index of this row in the original CSV file. Used for sorting ``Transaction``s by their order of appearance. row: The row containing strings for [transfer_date, posted_date, message, money_amount, money_total]. source_encoding: The encoding that will be used to decode strings to UTF-8. date_format: The format of dates in this row. thousand_sep: The thousand separator in money amounts. decimal_sep: The decimal separator in money amounts. Returns: A ``Transaction`` object. """ xfer, posted, message, amount, total = row xfer = Parse.date(xfer) posted = Parse.date(posted) message = Parse.to_utf8(message, source_encoding) amount = Parse.money(amount) total = Parse.money(total) return Transaction(index, xfer, posted, message, amount, total)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:csv_to_transactions; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:handle; 5, default_parameter; 5, 6; 5, 7; 6, identifier:source_encoding; 7, string:"latin1"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:date_format; 10, string:"%d-%m-%Y"; 11, default_parameter; 11, 12; 11, 13; 12, identifier:thousand_sep; 13, string:"."; 14, default_parameter; 14, 15; 14, 16; 15, identifier:decimal_sep; 16, string:","; 17, block; 17, 18; 17, 20; 17, 26; 17, 41; 17, 63; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:trans; 23, call; 23, 24; 23, 25; 24, identifier:Transactions; 25, argument_list; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:rows; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:csv; 32, identifier:reader; 33, argument_list; 33, 34; 33, 35; 33, 38; 34, identifier:handle; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:delimiter; 37, string:";"; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:quotechar; 40, string:"\""; 41, for_statement; 41, 42; 41, 45; 41, 49; 42, pattern_list; 42, 43; 42, 44; 43, identifier:index; 44, identifier:row; 45, call; 45, 46; 45, 47; 46, identifier:enumerate; 47, argument_list; 47, 48; 48, identifier:rows; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:trans; 54, identifier:append; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:Parse; 59, identifier:csv_row_to_transaction; 60, argument_list; 60, 61; 60, 62; 61, identifier:index; 62, identifier:row; 63, return_statement; 63, 64; 64, identifier:trans
def csv_to_transactions(handle, source_encoding="latin1", date_format="%d-%m-%Y", thousand_sep=".", decimal_sep=","): """ Parses CSV data from stream and returns ``Transactions``. Args: index: The index of this row in the original CSV file. Used for sorting ``Transaction``s by their order of appearance. row: The row containing strings for [transfer_date, posted_date, message, money_amount, money_total]. source_encoding: The encoding that will be used to decode strings to UTF-8. date_format: The format of dates in this row. thousand_sep: The thousand separator in money amounts. decimal_sep: The decimal separator in money amounts. Returns: A ``Transactions`` object. """ trans = Transactions() rows = csv.reader(handle, delimiter=";", quotechar="\"") for index, row in enumerate(rows): trans.append(Parse.csv_row_to_transaction(index, row)) return trans
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:filterAcceptsRow; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:row; 6, identifier:parentindex; 7, block; 7, 8; 7, 10; 7, 26; 7, 51; 7, 62; 7, 71; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 23; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 20; 13, attribute; 13, 14; 13, 19; 14, call; 14, 15; 14, 16; 15, identifier:super; 16, argument_list; 16, 17; 16, 18; 17, identifier:ReftrackSortFilterModel; 18, identifier:self; 19, identifier:filterAcceptsRow; 20, argument_list; 20, 21; 20, 22; 21, identifier:row; 22, identifier:parentindex; 23, block; 23, 24; 24, return_statement; 24, 25; 25, False; 26, if_statement; 26, 27; 26, 32; 26, 41; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:parentindex; 30, identifier:isValid; 31, argument_list; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:m; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:parentindex; 39, identifier:model; 40, argument_list; 41, else_clause; 41, 42; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:m; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:sourceModel; 50, argument_list; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:i; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:m; 57, identifier:index; 58, argument_list; 58, 59; 58, 60; 58, 61; 59, identifier:row; 60, integer:18; 61, identifier:parentindex; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:reftrack; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:i; 68, identifier:data; 69, argument_list; 69, 70; 70, identifier:REFTRACK_OBJECT_ROLE; 71, if_statement; 71, 72; 71, 74; 71, 77; 72, not_operator; 72, 73; 73, identifier:reftrack; 74, block; 74, 75; 75, return_statement; 75, 76; 76, True; 77, else_clause; 77, 78; 78, block; 78, 79; 79, return_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:filter_accept_reftrack; 84, argument_list; 84, 85; 85, identifier:reftrack
def filterAcceptsRow(self, row, parentindex): """Return True, if the filter accepts the given row of the parent :param row: the row to filter :type row: :class:`int` :param parentindex: the parent index :type parentindex: :class:`QtCore.QModelIndex` :returns: True, if the filter accepts the row :rtype: :class:`bool` :raises: None """ if not super(ReftrackSortFilterModel, self).filterAcceptsRow(row, parentindex): return False if parentindex.isValid(): m = parentindex.model() else: m = self.sourceModel() i = m.index(row, 18, parentindex) reftrack = i.data(REFTRACK_OBJECT_ROLE) if not reftrack: return True else: return self.filter_accept_reftrack(reftrack)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:load_grid_data; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:file_list; 5, default_parameter; 5, 6; 5, 7; 6, identifier:data_type; 7, string:"binary"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:delim; 13, string:" "; 14, block; 14, 15; 14, 17; 14, 18; 14, 58; 14, 68; 14, 69; 14, 77; 14, 78; 14, 224; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, if_statement; 18, 19; 18, 26; 18, 32; 19, not_operator; 19, 20; 20, comparison_operator:is; 20, 21; 20, 25; 21, call; 21, 22; 21, 23; 22, identifier:type; 23, argument_list; 23, 24; 24, identifier:file_list; 25, identifier:list; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:file_list; 30, list:[file_list]; 30, 31; 31, identifier:file_list; 32, elif_clause; 32, 33; 32, 34; 32, 35; 33, identifier:sort; 34, comment; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:file_list; 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:f; 47, call; 47, 48; 47, 49; 48, identifier:int; 49, argument_list; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:re; 53, identifier:sub; 54, argument_list; 54, 55; 54, 56; 54, 57; 55, string:"[^0-9]"; 56, string:""; 57, identifier:f; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:world_size; 61, call; 61, 62; 61, 63; 62, identifier:get_world_dimensions; 63, argument_list; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:file_list; 66, integer:0; 67, identifier:delim; 68, comment; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:data; 72, call; 72, 73; 72, 74; 73, identifier:initialize_grid; 74, argument_list; 74, 75; 74, 76; 75, identifier:world_size; 76, list:[]; 77, comment; 78, for_statement; 78, 79; 78, 80; 78, 81; 79, identifier:f; 80, identifier:file_list; 81, block; 81, 82; 81, 89; 81, 97; 81, 218; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:infile; 85, call; 85, 86; 85, 87; 86, identifier:open; 87, argument_list; 87, 88; 88, identifier:f; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:lines; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:infile; 95, identifier:readlines; 96, argument_list; 97, for_statement; 97, 98; 97, 99; 97, 105; 98, identifier:i; 99, call; 99, 100; 99, 101; 100, identifier:range; 101, argument_list; 101, 102; 102, subscript; 102, 103; 102, 104; 103, identifier:world_size; 104, integer:1; 105, block; 105, 106; 105, 123; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:lines; 110, identifier:i; 111, call; 111, 112; 111, 121; 112, attribute; 112, 113; 112, 120; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:lines; 117, identifier:i; 118, identifier:strip; 119, argument_list; 120, identifier:split; 121, argument_list; 121, 122; 122, identifier:delim; 123, for_statement; 123, 124; 123, 125; 123, 131; 124, identifier:j; 125, call; 125, 126; 125, 127; 126, identifier:range; 127, argument_list; 127, 128; 128, subscript; 128, 129; 128, 130; 129, identifier:world_size; 130, integer:0; 131, block; 131, 132; 131, 207; 132, if_statement; 132, 133; 132, 136; 132, 151; 132, 167; 132, 183; 132, 199; 133, comparison_operator:==; 133, 134; 133, 135; 134, identifier:data_type; 135, string:"binary"; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:val; 140, call; 140, 141; 140, 142; 141, identifier:bin; 142, argument_list; 142, 143; 143, call; 143, 144; 143, 145; 144, identifier:int; 145, argument_list; 145, 146; 146, subscript; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:lines; 149, identifier:i; 150, identifier:j; 151, elif_clause; 151, 152; 151, 155; 152, comparison_operator:==; 152, 153; 152, 154; 153, identifier:data_type; 154, string:"float"; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:val; 159, call; 159, 160; 159, 161; 160, identifier:float; 161, argument_list; 161, 162; 162, subscript; 162, 163; 162, 166; 163, subscript; 163, 164; 163, 165; 164, identifier:lines; 165, identifier:i; 166, identifier:j; 167, elif_clause; 167, 168; 167, 171; 168, comparison_operator:==; 168, 169; 168, 170; 169, identifier:data_type; 170, string:"int"; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:val; 175, call; 175, 176; 175, 177; 176, identifier:int; 177, argument_list; 177, 178; 178, subscript; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:lines; 181, identifier:i; 182, identifier:j; 183, elif_clause; 183, 184; 183, 187; 184, comparison_operator:==; 184, 185; 184, 186; 185, identifier:data_type; 186, string:"string"; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:val; 191, call; 191, 192; 191, 193; 192, identifier:str; 193, argument_list; 193, 194; 194, subscript; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:lines; 197, identifier:i; 198, identifier:j; 199, else_clause; 199, 200; 200, block; 200, 201; 200, 206; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:print; 204, argument_list; 204, 205; 205, string:"Unsupported data_type passed to load_grid"; 206, return_statement; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 216; 209, attribute; 209, 210; 209, 215; 210, subscript; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:data; 213, identifier:i; 214, identifier:j; 215, identifier:append; 216, argument_list; 216, 217; 217, identifier:val; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:infile; 222, identifier:close; 223, argument_list; 224, return_statement; 224, 225; 225, identifier:data
def load_grid_data(file_list, data_type="binary", sort=True, delim=" "): """ Loads data from one or multiple grid_task files. Arguments: file_list - either a string or a list of strings indicating files to load data from. Files are assumed to be in grid_task.dat format (space delimited values, one per cell). data_type - a string representing what type of data is in the file. Either "binary", "int", "float", or "string". sort - If you're making a movie, you want the files to be in chronological order. By default, they will be sorted. If for some reason you don't want them in chronological order, set sort to False. Returns: A three-dimensional array. The first dimension is columns, the second is rows. At each row,column index in the array is another list which holds the values that each of the requested files has at that location in the grid. If you want this list collapsed to a single representative number, you should use agg_niche_grid. """ # If there's only one file, we pretend it's a list if not type(file_list) is list: file_list = [file_list] elif sort: # put file_list in chronological order file_list.sort(key=lambda f: int(re.sub("[^0-9]", "", f))) world_size = get_world_dimensions(file_list[0], delim) # Initialize empty data array data = initialize_grid(world_size, []) # Loop through file list, reading in data for f in file_list: infile = open(f) lines = infile.readlines() for i in range(world_size[1]): lines[i] = lines[i].strip().split(delim) for j in range(world_size[0]): if data_type == "binary": val = bin(int(lines[i][j])) elif data_type == "float": val = float(lines[i][j]) elif data_type == "int": val = int(lines[i][j]) elif data_type == "string": val = str(lines[i][j]) else: print("Unsupported data_type passed to load_grid") return data[i][j].append(val) infile.close() return data
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; 5, 9; 5, 118; 5, 119; 5, 125; 5, 162; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, for_statement; 9, 10; 9, 13; 9, 20; 10, pattern_list; 10, 11; 10, 12; 11, identifier:priority; 12, identifier:triggers; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:_triggers; 18, identifier:items; 19, argument_list; 20, block; 20, 21; 20, 37; 20, 38; 20, 50; 20, 63; 20, 85; 20, 107; 20, 108; 21, expression_statement; 21, 22; 22, call; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:_log; 27, identifier:debug; 28, argument_list; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, string:'Sorting priority {priority} triggers'; 32, identifier:format; 33, argument_list; 33, 34; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:priority; 36, identifier:priority; 37, comment; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:atomics; 41, list_comprehension; 41, 42; 41, 43; 41, 46; 42, identifier:trigger; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:trigger; 45, identifier:triggers; 46, if_clause; 46, 47; 47, attribute; 47, 48; 47, 49; 48, identifier:trigger; 49, identifier:pattern_is_atomic; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:wildcards; 53, list_comprehension; 53, 54; 53, 55; 53, 58; 54, identifier:trigger; 55, for_in_clause; 55, 56; 55, 57; 56, identifier:trigger; 57, identifier:triggers; 58, if_clause; 58, 59; 59, not_operator; 59, 60; 60, attribute; 60, 61; 60, 62; 61, identifier:trigger; 62, identifier:pattern_is_atomic; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:atomics; 66, call; 66, 67; 66, 68; 67, identifier:sorted; 68, argument_list; 68, 69; 68, 70; 68, 82; 69, identifier:atomics; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:key; 72, lambda; 72, 73; 72, 75; 73, lambda_parameters; 73, 74; 74, identifier:trigger; 75, tuple; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:trigger; 78, identifier:pattern_words; 79, attribute; 79, 80; 79, 81; 80, identifier:trigger; 81, identifier:pattern_len; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:reverse; 84, True; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:wildcards; 88, call; 88, 89; 88, 90; 89, identifier:sorted; 90, argument_list; 90, 91; 90, 92; 90, 104; 91, identifier:wildcards; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:key; 94, lambda; 94, 95; 94, 97; 95, lambda_parameters; 95, 96; 96, identifier:trigger; 97, tuple; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:trigger; 100, identifier:pattern_words; 101, attribute; 101, 102; 101, 103; 102, identifier:trigger; 103, identifier:pattern_len; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:reverse; 106, True; 107, comment; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 115; 110, subscript; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:_triggers; 114, identifier:priority; 115, binary_operator:+; 115, 116; 115, 117; 116, identifier:atomics; 117, identifier:wildcards; 118, comment; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:_sorted_triggers; 124, list:[]; 125, for_statement; 125, 126; 125, 127; 125, 148; 126, identifier:triggers; 127, list_comprehension; 127, 128; 127, 133; 128, subscript; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:_triggers; 132, identifier:priority; 133, for_in_clause; 133, 134; 133, 135; 134, identifier:priority; 135, call; 135, 136; 135, 137; 136, identifier:sorted; 137, argument_list; 137, 138; 137, 145; 138, call; 138, 139; 138, 144; 139, attribute; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:_triggers; 143, identifier:keys; 144, argument_list; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:reverse; 147, True; 148, block; 148, 149; 149, for_statement; 149, 150; 149, 151; 149, 152; 150, identifier:trigger; 151, identifier:triggers; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:_sorted_triggers; 159, identifier:append; 160, argument_list; 160, 161; 161, identifier:trigger; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:sorted; 167, True
def sort(self): """ Sort triggers and their associated responses """ # Sort triggers by word and character length first for priority, triggers in self._triggers.items(): self._log.debug('Sorting priority {priority} triggers'.format(priority=priority)) # Get and sort our atomic and wildcard patterns atomics = [trigger for trigger in triggers if trigger.pattern_is_atomic] wildcards = [trigger for trigger in triggers if not trigger.pattern_is_atomic] atomics = sorted(atomics, key=lambda trigger: (trigger.pattern_words, trigger.pattern_len), reverse=True) wildcards = sorted(wildcards, key=lambda trigger: (trigger.pattern_words, trigger.pattern_len), reverse=True) # Replace our sorted triggers self._triggers[priority] = atomics + wildcards # Finally, sort triggers by priority self._sorted_triggers = [] for triggers in [self._triggers[priority] for priority in sorted(self._triggers.keys(), reverse=True)]: for trigger in triggers: self._sorted_triggers.append(trigger) self.sorted = True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:log_message; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:user; 6, identifier:message; 7, block; 7, 8; 7, 10; 7, 38; 7, 46; 7, 58; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 16; 10, 23; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 15; 14, identifier:user; 15, identifier:SeshetUser; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:user; 20, attribute; 20, 21; 20, 22; 21, identifier:user; 22, identifier:nick; 23, elif_clause; 23, 24; 23, 30; 24, not_operator; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:isinstance; 27, argument_list; 27, 28; 27, 29; 28, identifier:user; 29, identifier:IRCstr; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:user; 34, call; 34, 35; 34, 36; 35, identifier:IRCstr; 36, argument_list; 36, 37; 37, identifier:user; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:time; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:datetime; 44, identifier:utcnow; 45, argument_list; 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:message_log; 52, identifier:append; 53, argument_list; 53, 54; 54, tuple; 54, 55; 54, 56; 54, 57; 55, identifier:time; 56, identifier:user; 57, identifier:message; 58, while_statement; 58, 59; 58, 69; 59, comparison_operator:>; 59, 60; 59, 66; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:message_log; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:_log_size; 69, block; 69, 70; 70, delete_statement; 70, 71; 71, subscript; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:message_log; 75, integer:0
def log_message(self, user, message): """Log a channel message. This log acts as a sort of cache so that recent activity can be searched by the bot and command modules without querying the database. """ if isinstance(user, SeshetUser): user = user.nick elif not isinstance(user, IRCstr): user = IRCstr(user) time = datetime.utcnow() self.message_log.append((time, user, message)) while len(self.message_log) > self._log_size: del self.message_log[0]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:add_item; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 4, identifier:self; 5, identifier:path; 6, identifier:name; 7, default_parameter; 7, 8; 7, 9; 8, identifier:icon; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:url; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:order; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:permission; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:active_regex; 21, None; 22, block; 22, 23; 22, 25; 22, 42; 22, 48; 22, 52; 22, 130; 22, 143; 22, 160; 23, expression_statement; 23, 24; 24, comment; 25, if_statement; 25, 26; 25, 31; 26, comparison_operator:is; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:root_item; 30, None; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:root_item; 37, call; 37, 38; 37, 39; 38, identifier:MenuItem; 39, argument_list; 39, 40; 39, 41; 40, string:'ROOT'; 41, string:'ROOT'; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:root_item; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:root_item; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:current_path; 51, string:''; 52, for_statement; 52, 53; 52, 54; 52, 65; 53, identifier:node; 54, subscript; 54, 55; 54, 61; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:path; 58, identifier:split; 59, argument_list; 59, 60; 60, string:'/'; 61, slice; 61, 62; 61, 63; 62, colon; 63, unary_operator:-; 63, 64; 64, integer:1; 65, block; 65, 66; 65, 71; 65, 88; 65, 97; 65, 126; 66, if_statement; 66, 67; 66, 69; 67, not_operator; 67, 68; 68, identifier:node; 69, block; 69, 70; 70, continue_statement; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:current_path; 74, binary_operator:+; 74, 75; 74, 76; 75, string:'/'; 76, call; 76, 77; 76, 86; 77, attribute; 77, 78; 77, 85; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, string:'{}/{}'; 81, identifier:format; 82, argument_list; 82, 83; 82, 84; 83, identifier:current_path; 84, identifier:node; 85, identifier:strip; 86, argument_list; 86, 87; 87, string:'/'; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:new_root; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:root_item; 94, identifier:child_by_code; 95, argument_list; 95, 96; 96, identifier:node; 97, if_statement; 97, 98; 97, 100; 97, 101; 98, not_operator; 98, 99; 99, identifier:new_root; 100, comment; 101, block; 101, 102; 101, 119; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:new_root; 105, call; 105, 106; 105, 107; 106, identifier:MenuItem; 107, argument_list; 107, 108; 107, 109; 108, identifier:current_path; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:name; 111, call; 111, 112; 111, 118; 112, attribute; 112, 113; 112, 117; 113, call; 113, 114; 113, 115; 114, identifier:str; 115, argument_list; 115, 116; 116, identifier:node; 117, identifier:capitalize; 118, argument_list; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:root_item; 123, identifier:add_child; 124, argument_list; 124, 125; 125, identifier:new_root; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:root_item; 129, identifier:new_root; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:new_item; 133, call; 133, 134; 133, 135; 134, identifier:MenuItem; 135, argument_list; 135, 136; 135, 137; 135, 138; 135, 139; 135, 140; 135, 141; 135, 142; 136, identifier:path; 137, identifier:name; 138, identifier:icon; 139, identifier:url; 140, identifier:order; 141, identifier:permission; 142, identifier:active_regex; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:current_item; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:root_item; 149, identifier:child_by_code; 150, argument_list; 150, 151; 151, subscript; 151, 152; 151, 158; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:path; 155, identifier:split; 156, argument_list; 156, 157; 157, string:'/'; 158, unary_operator:-; 158, 159; 159, integer:1; 160, if_statement; 160, 161; 160, 162; 160, 170; 161, identifier:current_item; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:current_item; 167, identifier:merge; 168, argument_list; 168, 169; 169, identifier:new_item; 170, else_clause; 170, 171; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:root_item; 176, identifier:add_child; 177, argument_list; 177, 178; 178, identifier:new_item
def add_item(self, path, name, icon=None, url=None, order=None, permission=None, active_regex=None): """ Add new menu item to menu :param path: Path of menu :param name: Display name :param icon: CSS icon :param url: link to page :param order: Sort order :param permission: :return: """ if self.root_item is None: self.root_item = MenuItem('ROOT', 'ROOT') root_item = self.root_item current_path = '' for node in path.split('/')[:-1]: if not node: continue current_path = '/' + '{}/{}'.format(current_path, node).strip('/') new_root = root_item.child_by_code(node) if not new_root: # Create menu item if not exists new_root = MenuItem(current_path, name=str(node).capitalize()) root_item.add_child(new_root) root_item = new_root new_item = MenuItem(path, name, icon, url, order, permission, active_regex) current_item = root_item.child_by_code(path.split('/')[-1]) if current_item: current_item.merge(new_item) else: root_item.add_child(new_item)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:assist; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:project_path; 6, identifier:source; 7, identifier:position; 8, identifier:filename; 9, block; 9, 10; 9, 12; 10, expression_statement; 10, 11; 11, comment; 12, return_statement; 12, 13; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:_call; 17, argument_list; 17, 18; 17, 19; 17, 20; 17, 21; 17, 22; 18, string:'assist'; 19, identifier:project_path; 20, identifier:source; 21, identifier:position; 22, identifier:filename
def assist(self, project_path, source, position, filename): """Return completion match and list of completion proposals :param project_path: absolute project path :param source: unicode or byte string code source :param position: character or byte cursor position :param filename: absolute path of file with source code :returns: tuple (completion match, sorted list of proposals) """ return self._call('assist', project_path, source, position, filename)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sorted_key_list; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 20; 5, 43; 5, 53; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 13; 9, not_operator; 9, 10; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:is_baked; 13, block; 13, 14; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:bake; 19, argument_list; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:key_value_tuple; 23, call; 23, 24; 23, 25; 24, identifier:sorted; 25, argument_list; 25, 26; 25, 33; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:dct; 31, identifier:items; 32, argument_list; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:key; 35, lambda; 35, 36; 35, 38; 36, lambda_parameters; 36, 37; 37, identifier:x; 38, subscript; 38, 39; 38, 42; 39, subscript; 39, 40; 39, 41; 40, identifier:x; 41, integer:1; 42, string:'__abs_time__'; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:skl; 46, list_comprehension; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:k; 49, integer:0; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:k; 52, identifier:key_value_tuple; 53, return_statement; 53, 54; 54, identifier:skl
def sorted_key_list(self): """Returns list of keys sorted according to their absolute time.""" if not self.is_baked: self.bake() key_value_tuple = sorted(self.dct.items(), key=lambda x: x[1]['__abs_time__']) skl = [k[0] for k in key_value_tuple] return skl
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_used_key_frames; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 18; 5, 19; 5, 23; 5, 51; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:skl; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:key_frame_list; 16, identifier:sorted_key_list; 17, argument_list; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:used_key_frames; 22, list:[]; 23, for_statement; 23, 24; 23, 25; 23, 26; 24, identifier:kf; 25, identifier:skl; 26, block; 26, 27; 27, if_statement; 27, 28; 27, 35; 28, comparison_operator:in; 28, 29; 28, 30; 29, identifier:kf; 30, subscript; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:dct; 34, string:'keys'; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:used_key_frames; 40, identifier:append; 41, argument_list; 41, 42; 42, tuple; 42, 43; 42, 44; 43, identifier:kf; 44, subscript; 44, 45; 44, 50; 45, subscript; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:dct; 49, string:'keys'; 50, identifier:kf; 51, return_statement; 51, 52; 52, identifier:used_key_frames
def get_used_key_frames(self): """Returns a list of the keyframes used by this channel, sorted with time. Each element in the list is a tuple. The first element is the key_name and the second is the channel data at that keyframe.""" skl = self.key_frame_list.sorted_key_list() # each element in used_key_frames is a tuple (key_name, key_dict) used_key_frames = [] for kf in skl: if kf in self.dct['keys']: used_key_frames.append((kf, self.dct['keys'][kf])) return used_key_frames
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:flds_firstsort; 3, parameters; 3, 4; 4, identifier:d; 5, block; 5, 6; 5, 8; 5, 29; 5, 47; 6, expression_statement; 6, 7; 7, string:''' Perform a lexsort and return the sort indices and shape as a tuple. '''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:shape; 11, list_comprehension; 11, 12; 11, 23; 12, call; 12, 13; 12, 14; 13, identifier:len; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:np; 18, identifier:unique; 19, argument_list; 19, 20; 20, subscript; 20, 21; 20, 22; 21, identifier:d; 22, identifier:l; 23, for_in_clause; 23, 24; 23, 25; 24, identifier:l; 25, list:['xs', 'ys', 'zs']; 25, 26; 25, 27; 25, 28; 26, string:'xs'; 27, string:'ys'; 28, string:'zs'; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:si; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:np; 35, identifier:lexsort; 36, argument_list; 36, 37; 37, tuple; 37, 38; 37, 41; 37, 44; 38, subscript; 38, 39; 38, 40; 39, identifier:d; 40, string:'z'; 41, subscript; 41, 42; 41, 43; 42, identifier:d; 43, string:'y'; 44, subscript; 44, 45; 44, 46; 45, identifier:d; 46, string:'x'; 47, return_statement; 47, 48; 48, expression_list; 48, 49; 48, 50; 49, identifier:si; 50, identifier:shape
def flds_firstsort(d): ''' Perform a lexsort and return the sort indices and shape as a tuple. ''' shape = [ len( np.unique(d[l]) ) for l in ['xs', 'ys', 'zs'] ]; si = np.lexsort((d['z'],d['y'],d['x'])); return si,shape;
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:flds_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:d; 5, identifier:s; 6, block; 6, 7; 6, 9; 6, 31; 6, 37; 6, 69; 7, expression_statement; 7, 8; 8, string:''' Sort based on position. Sort with s as a tuple of the sort indices and shape from first sort. Parameters: ----------- d -- the flds/sclr data s -- (si, shape) sorting and shaping data from firstsort. '''; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:labels; 12, list_comprehension; 12, 13; 12, 14; 12, 21; 13, identifier:key; 14, for_in_clause; 14, 15; 14, 16; 15, identifier:key; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:d; 19, identifier:keys; 20, argument_list; 21, if_clause; 21, 22; 22, comparison_operator:not; 22, 23; 22, 24; 23, identifier:key; 24, list:['t', 'xs', 'ys', 'zs', 'fd', 'sd']; 24, 25; 24, 26; 24, 27; 24, 28; 24, 29; 24, 30; 25, string:'t'; 26, string:'xs'; 27, string:'ys'; 28, string:'zs'; 29, string:'fd'; 30, string:'sd'; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 36; 33, pattern_list; 33, 34; 33, 35; 34, identifier:si; 35, identifier:shape; 36, identifier:s; 37, for_statement; 37, 38; 37, 39; 37, 40; 38, identifier:l; 39, identifier:labels; 40, block; 40, 41; 40, 56; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 46; 43, subscript; 43, 44; 43, 45; 44, identifier:d; 45, identifier:l; 46, call; 46, 47; 46, 54; 47, attribute; 47, 48; 47, 53; 48, subscript; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:d; 51, identifier:l; 52, identifier:si; 53, identifier:reshape; 54, argument_list; 54, 55; 55, identifier:shape; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:d; 60, identifier:l; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:np; 64, identifier:squeeze; 65, argument_list; 65, 66; 66, subscript; 66, 67; 66, 68; 67, identifier:d; 68, identifier:l; 69, return_statement; 69, 70; 70, identifier:d
def flds_sort(d,s): ''' Sort based on position. Sort with s as a tuple of the sort indices and shape from first sort. Parameters: ----------- d -- the flds/sclr data s -- (si, shape) sorting and shaping data from firstsort. ''' labels = [ key for key in d.keys() if key not in ['t', 'xs', 'ys', 'zs', 'fd', 'sd'] ]; si,shape = s; for l in labels: d[l] = d[l][si].reshape(shape); d[l] = np.squeeze(d[l]); return d;
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:read; 3, parameters; 3, 4; 3, 5; 4, identifier:fname; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kw; 7, block; 7, 8; 7, 10; 7, 37; 7, 50; 7, 324; 8, expression_statement; 8, 9; 9, string:''' Reads an lsp output file and returns a raw dump of data, sectioned into quantities either as an dictionary or a typed numpy array. Parameters: ----------- fname -- filename of thing to read Keyword Arguments: ------------------ vprint -- Verbose printer. Used in scripts override -- (type, start) => A tuple of a dump type and a place to start in the passed file, useful to attempting to read semicorrupted files. gzip -- Read as a gzip file. flds/sclr Specific Arguments: ----------------------------- var -- list of quantities to be read. For fields, this can consist of strings that include vector components, e.g., 'Ex'. If None (default), read all quantities. keep_edges -- If set to truthy, then don't remove the edges from domains before concatenation and don't reshape the flds data. sort -- If not None, sort using these indices, useful for avoiding resorting. If True and not an ndarray, just sort. first_sort -- If truthy, sort, and return the sort data for future flds that should have the same shape. keep_xs -- Keep the xs's, that is, the grid information. Usually redundant with x,y,z returned. return_array -- If set to truthy, then try to return a numpy array with a dtype. Requires of course that the quantities have the same shape. '''; 10, if_statement; 10, 11; 10, 22; 11, boolean_operator:and; 11, 12; 11, 17; 12, call; 12, 13; 12, 14; 13, identifier:test; 14, argument_list; 14, 15; 14, 16; 15, identifier:kw; 16, string:'gzip'; 17, comparison_operator:==; 17, 18; 17, 21; 18, subscript; 18, 19; 18, 20; 19, identifier:kw; 20, string:'gzip'; 21, string:'guess'; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 28; 25, subscript; 25, 26; 25, 27; 26, identifier:kw; 27, string:'gzip'; 28, comparison_operator:is; 28, 29; 28, 36; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:re; 32, identifier:search; 33, argument_list; 33, 34; 33, 35; 34, string:r'\.gz$'; 35, identifier:fname; 36, None; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:openf; 40, conditional_expression:if; 40, 41; 40, 44; 40, 49; 41, attribute; 41, 42; 41, 43; 42, identifier:gzip; 43, identifier:open; 44, call; 44, 45; 44, 46; 45, identifier:test; 46, argument_list; 46, 47; 46, 48; 47, identifier:kw; 48, string:'gzip'; 49, identifier:open; 50, with_statement; 50, 51; 50, 61; 51, with_clause; 51, 52; 52, with_item; 52, 53; 53, as_pattern; 53, 54; 53, 59; 54, call; 54, 55; 54, 56; 55, identifier:openf; 56, argument_list; 56, 57; 56, 58; 57, identifier:fname; 58, string:'rb'; 59, as_pattern_target; 59, 60; 60, identifier:file; 61, block; 61, 62; 61, 120; 61, 136; 61, 223; 61, 304; 62, if_statement; 62, 63; 62, 68; 62, 111; 63, call; 63, 64; 63, 65; 64, identifier:test; 65, argument_list; 65, 66; 65, 67; 66, identifier:kw; 67, string:'override'; 68, block; 68, 69; 68, 77; 68, 84; 68, 91; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, pattern_list; 71, 72; 71, 73; 72, identifier:dump; 73, identifier:start; 74, subscript; 74, 75; 74, 76; 75, identifier:kw; 76, string:'override'; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:file; 81, identifier:seek; 82, argument_list; 82, 83; 83, identifier:start; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:header; 87, dictionary; 87, 88; 88, pair; 88, 89; 88, 90; 89, string:'dump_type'; 90, identifier:dump; 91, if_statement; 91, 92; 91, 105; 92, boolean_operator:and; 92, 93; 92, 99; 93, not_operator; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:test; 96, argument_list; 96, 97; 96, 98; 97, identifier:kw; 98, string:'var'; 99, comparison_operator:<=; 99, 100; 99, 101; 99, 104; 100, integer:2; 101, subscript; 101, 102; 101, 103; 102, identifier:header; 103, string:'dump_type'; 104, integer:3; 105, block; 105, 106; 106, raise_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:ValueError; 109, argument_list; 109, 110; 110, string:"If you want to force to read as a scalar, you need to supply the quantities"; 111, else_clause; 111, 112; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:header; 116, call; 116, 117; 116, 118; 117, identifier:get_header; 118, argument_list; 118, 119; 119, identifier:file; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:vprint; 123, conditional_expression:if; 123, 124; 123, 127; 123, 132; 124, subscript; 124, 125; 124, 126; 125, identifier:kw; 126, string:'vprint'; 127, call; 127, 128; 127, 129; 128, identifier:test; 129, argument_list; 129, 130; 129, 131; 130, identifier:kw; 131, string:'vprint'; 132, lambda; 132, 133; 132, 135; 133, lambda_parameters; 133, 134; 134, identifier:s; 135, None; 136, if_statement; 136, 137; 136, 143; 137, comparison_operator:<=; 137, 138; 137, 139; 137, 142; 138, integer:2; 139, subscript; 139, 140; 139, 141; 140, identifier:header; 141, string:'dump_type'; 142, integer:3; 143, block; 143, 144; 143, 172; 143, 180; 143, 188; 143, 207; 143, 215; 144, if_statement; 144, 145; 144, 151; 144, 164; 145, not_operator; 145, 146; 146, call; 146, 147; 146, 148; 147, identifier:test; 148, argument_list; 148, 149; 148, 150; 149, identifier:kw; 150, string:'var'; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:var; 155, list_comprehension; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:i; 158, integer:0; 159, for_in_clause; 159, 160; 159, 161; 160, identifier:i; 161, subscript; 161, 162; 161, 163; 162, identifier:header; 163, string:'quantities'; 164, else_clause; 164, 165; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:var; 169, subscript; 169, 170; 169, 171; 170, identifier:kw; 171, string:'var'; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:keep_edges; 175, call; 175, 176; 175, 177; 176, identifier:test; 177, argument_list; 177, 178; 177, 179; 178, identifier:kw; 179, string:'keep_edges'; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:first_sort; 183, call; 183, 184; 183, 185; 184, identifier:test; 185, argument_list; 185, 186; 185, 187; 186, identifier:kw; 187, string:'first_sort'; 188, if_statement; 188, 189; 188, 194; 188, 201; 189, call; 189, 190; 189, 191; 190, identifier:test; 191, argument_list; 191, 192; 191, 193; 192, identifier:kw; 193, string:'sort'; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:sort; 198, subscript; 198, 199; 198, 200; 199, identifier:kw; 200, string:'sort'; 201, else_clause; 201, 202; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:sort; 206, None; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:keep_xs; 210, call; 210, 211; 210, 212; 211, identifier:test; 212, argument_list; 212, 213; 212, 214; 213, identifier:kw; 214, string:'keep_xs'; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:return_array; 218, call; 218, 219; 218, 220; 219, identifier:test; 220, argument_list; 220, 221; 220, 222; 221, identifier:kw; 222, string:'return_array'; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:readers; 226, dictionary; 226, 227; 226, 235; 226, 260; 226, 288; 226, 296; 227, pair; 227, 228; 227, 229; 228, integer:1; 229, lambda; 229, 230; 230, call; 230, 231; 230, 232; 231, identifier:read_particles; 232, argument_list; 232, 233; 232, 234; 233, identifier:file; 234, identifier:header; 235, pair; 235, 236; 235, 237; 236, integer:2; 237, lambda; 237, 238; 238, call; 238, 239; 238, 240; 239, identifier:read_flds; 240, argument_list; 240, 241; 240, 242; 240, 243; 240, 244; 240, 245; 240, 248; 240, 251; 240, 254; 240, 257; 241, identifier:file; 242, identifier:header; 243, identifier:var; 244, identifier:vprint; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:keep_edges; 247, identifier:keep_edges; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:first_sort; 250, identifier:first_sort; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:sort; 253, identifier:sort; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:keep_xs; 256, identifier:keep_xs; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:return_array; 259, identifier:return_array; 260, pair; 260, 261; 260, 262; 261, integer:3; 262, lambda; 262, 263; 263, call; 263, 264; 263, 265; 264, identifier:read_flds; 265, argument_list; 265, 266; 265, 267; 265, 268; 265, 269; 265, 270; 265, 273; 265, 276; 265, 279; 265, 282; 265, 285; 266, identifier:file; 267, identifier:header; 268, identifier:var; 269, identifier:vprint; 270, keyword_argument; 270, 271; 270, 272; 271, identifier:keep_edges; 272, identifier:keep_edges; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:first_sort; 275, identifier:first_sort; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:sort; 278, identifier:sort; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:keep_xs; 281, identifier:keep_xs; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:return_array; 284, identifier:return_array; 285, keyword_argument; 285, 286; 285, 287; 286, identifier:vector; 287, False; 288, pair; 288, 289; 288, 290; 289, integer:6; 290, lambda; 290, 291; 291, call; 291, 292; 291, 293; 292, identifier:read_movie; 293, argument_list; 293, 294; 293, 295; 294, identifier:file; 295, identifier:header; 296, pair; 296, 297; 296, 298; 297, integer:10; 298, lambda; 298, 299; 299, call; 299, 300; 299, 301; 300, identifier:read_pext; 301, argument_list; 301, 302; 301, 303; 302, identifier:file; 303, identifier:header; 304, try_statement; 304, 305; 304, 316; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 309; 308, identifier:d; 309, call; 309, 310; 309, 315; 310, subscript; 310, 311; 310, 312; 311, identifier:readers; 312, subscript; 312, 313; 312, 314; 313, identifier:header; 314, string:'dump_type'; 315, argument_list; 316, except_clause; 316, 317; 316, 318; 317, identifier:KeyError; 318, block; 318, 319; 319, raise_statement; 319, 320; 320, call; 320, 321; 320, 322; 321, identifier:NotImplementedError; 322, argument_list; 322, 323; 323, string:"Other file types not implemented yet!"; 324, return_statement; 324, 325; 325, identifier:d
def read(fname,**kw): ''' Reads an lsp output file and returns a raw dump of data, sectioned into quantities either as an dictionary or a typed numpy array. Parameters: ----------- fname -- filename of thing to read Keyword Arguments: ------------------ vprint -- Verbose printer. Used in scripts override -- (type, start) => A tuple of a dump type and a place to start in the passed file, useful to attempting to read semicorrupted files. gzip -- Read as a gzip file. flds/sclr Specific Arguments: ----------------------------- var -- list of quantities to be read. For fields, this can consist of strings that include vector components, e.g., 'Ex'. If None (default), read all quantities. keep_edges -- If set to truthy, then don't remove the edges from domains before concatenation and don't reshape the flds data. sort -- If not None, sort using these indices, useful for avoiding resorting. If True and not an ndarray, just sort. first_sort -- If truthy, sort, and return the sort data for future flds that should have the same shape. keep_xs -- Keep the xs's, that is, the grid information. Usually redundant with x,y,z returned. return_array -- If set to truthy, then try to return a numpy array with a dtype. Requires of course that the quantities have the same shape. ''' if test(kw,'gzip') and kw['gzip'] == 'guess': kw['gzip'] = re.search(r'\.gz$', fname) is not None; openf = gzip.open if test(kw, 'gzip') else open; with openf(fname,'rb') as file: if test(kw,'override'): dump, start = kw['override']; file.seek(start); header = {'dump_type': dump}; if not test(kw, 'var') and 2 <= header['dump_type'] <= 3 : raise ValueError( "If you want to force to read as a scalar, you need to supply the quantities" ); else: header = get_header(file); vprint = kw['vprint'] if test(kw, 'vprint') else lambda s: None; if 2 <= header['dump_type'] <= 3 : if not test(kw, 'var'): var=[i[0] for i in header['quantities']]; else: var=kw['var']; keep_edges = test(kw, 'keep_edges'); first_sort = test(kw, 'first_sort'); if test(kw,'sort'): sort = kw['sort'] else: sort = None; keep_xs = test(kw, 'keep_xs'); return_array = test(kw, 'return_array'); readers = { 1: lambda: read_particles(file, header), 2: lambda: read_flds( file,header,var,vprint, keep_edges=keep_edges, first_sort=first_sort, sort=sort, keep_xs=keep_xs, return_array=return_array), 3: lambda: read_flds( file,header,var, vprint, keep_edges=keep_edges, first_sort=first_sort, sort=sort, keep_xs=keep_xs, return_array=return_array, vector=False), 6: lambda: read_movie(file, header), 10:lambda: read_pext(file,header) }; try: d = readers[header['dump_type']](); except KeyError: raise NotImplementedError("Other file types not implemented yet!"); return d;
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_region_nt_counts; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:region; 5, identifier:bam; 6, default_parameter; 6, 7; 6, 8; 7, identifier:stranded; 8, False; 9, block; 9, 10; 9, 12; 9, 13; 9, 14; 9, 15; 9, 16; 9, 34; 9, 188; 9, 197; 9, 224; 9, 239; 9, 476; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, comment; 14, comment; 15, comment; 16, if_statement; 16, 17; 16, 23; 17, comparison_operator:==; 17, 18; 17, 22; 18, call; 18, 19; 18, 20; 19, identifier:type; 20, argument_list; 20, 21; 21, identifier:bam; 22, identifier:str; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:bam; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:pysam; 30, identifier:AlignmentFile; 31, argument_list; 31, 32; 31, 33; 32, identifier:bam; 33, string:'rb'; 34, if_statement; 34, 35; 34, 41; 34, 128; 35, comparison_operator:is; 35, 36; 35, 40; 36, call; 36, 37; 36, 38; 37, identifier:type; 38, argument_list; 38, 39; 39, identifier:region; 40, identifier:str; 41, block; 41, 42; 41, 49; 41, 80; 41, 87; 41, 94; 41, 114; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:r; 45, call; 45, 46; 45, 47; 46, identifier:parse_region; 47, argument_list; 47, 48; 48, identifier:region; 49, if_statement; 49, 50; 49, 56; 49, 64; 50, comparison_operator:==; 50, 51; 50, 55; 51, call; 51, 52; 51, 53; 52, identifier:len; 53, argument_list; 53, 54; 54, identifier:r; 55, integer:3; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 63; 59, pattern_list; 59, 60; 59, 61; 59, 62; 60, identifier:chrom; 61, identifier:start; 62, identifier:end; 63, identifier:r; 64, elif_clause; 64, 65; 64, 71; 65, comparison_operator:==; 65, 66; 65, 70; 66, call; 66, 67; 66, 68; 67, identifier:len; 68, argument_list; 68, 69; 69, identifier:r; 70, integer:4; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 79; 74, pattern_list; 74, 75; 74, 76; 74, 77; 74, 78; 75, identifier:chrom; 76, identifier:start; 77, identifier:end; 78, identifier:strand; 79, identifier:r; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:start; 83, call; 83, 84; 83, 85; 84, identifier:int; 85, argument_list; 85, 86; 86, identifier:start; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:end; 90, call; 90, 91; 90, 92; 91, identifier:int; 92, argument_list; 92, 93; 93, identifier:end; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:ind; 97, list_comprehension; 97, 98; 97, 105; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, string:'{}:{}'; 101, identifier:format; 102, argument_list; 102, 103; 102, 104; 103, identifier:chrom; 104, identifier:x; 105, for_in_clause; 105, 106; 105, 107; 106, identifier:x; 107, call; 107, 108; 107, 109; 108, identifier:range; 109, argument_list; 109, 110; 109, 111; 110, identifier:start; 111, binary_operator:+; 111, 112; 111, 113; 112, identifier:end; 113, integer:1; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:pp; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:bam; 120, identifier:pileup; 121, argument_list; 121, 122; 121, 125; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:region; 124, identifier:region; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:truncate; 127, True; 128, elif_clause; 128, 129; 128, 138; 129, comparison_operator:is; 129, 130; 129, 134; 130, call; 130, 131; 130, 132; 131, identifier:type; 132, argument_list; 132, 133; 133, identifier:region; 134, parenthesized_expression; 134, 135; 135, boolean_operator:or; 135, 136; 135, 137; 136, identifier:list; 137, identifier:tuple; 138, block; 138, 139; 138, 146; 138, 174; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 145; 141, pattern_list; 141, 142; 141, 143; 141, 144; 142, identifier:chrom; 143, identifier:start; 144, identifier:end; 145, identifier:region; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:ind; 149, list_comprehension; 149, 150; 149, 157; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, string:'{}:{}'; 153, identifier:format; 154, argument_list; 154, 155; 154, 156; 155, identifier:chrom; 156, identifier:x; 157, for_in_clause; 157, 158; 157, 159; 158, identifier:x; 159, call; 159, 160; 159, 161; 160, identifier:range; 161, argument_list; 161, 162; 161, 168; 162, binary_operator:+; 162, 163; 162, 167; 163, call; 163, 164; 163, 165; 164, identifier:int; 165, argument_list; 165, 166; 166, identifier:start; 167, integer:1; 168, binary_operator:+; 168, 169; 168, 173; 169, call; 169, 170; 169, 171; 170, identifier:int; 171, argument_list; 171, 172; 172, identifier:end; 173, integer:1; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:pp; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:bam; 180, identifier:pileup; 181, argument_list; 181, 182; 181, 183; 181, 184; 181, 185; 182, identifier:chrom; 183, identifier:start; 184, identifier:end; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:truncate; 187, True; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:cols; 191, list:['A', 'T', 'C', 'G', 'N']; 191, 192; 191, 193; 191, 194; 191, 195; 191, 196; 192, string:'A'; 193, string:'T'; 194, string:'C'; 195, string:'G'; 196, string:'N'; 197, if_statement; 197, 198; 197, 199; 198, identifier:stranded; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:cols; 203, binary_operator:+; 203, 204; 203, 214; 204, list_comprehension; 204, 205; 204, 211; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, string:'{}+'; 208, identifier:format; 209, argument_list; 209, 210; 210, identifier:x; 211, for_in_clause; 211, 212; 211, 213; 212, identifier:x; 213, identifier:cols; 214, list_comprehension; 214, 215; 214, 221; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, string:'{}-'; 218, identifier:format; 219, argument_list; 219, 220; 220, identifier:x; 221, for_in_clause; 221, 222; 221, 223; 222, identifier:x; 223, identifier:cols; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:counts; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:pd; 230, identifier:DataFrame; 231, argument_list; 231, 232; 231, 233; 231, 236; 232, integer:0; 233, keyword_argument; 233, 234; 233, 235; 234, identifier:index; 235, identifier:ind; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:columns; 238, identifier:cols; 239, for_statement; 239, 240; 239, 241; 239, 242; 239, 243; 239, 244; 240, identifier:pc; 241, identifier:pp; 242, comment; 243, comment; 244, block; 244, 245; 244, 253; 244, 257; 244, 261; 244, 265; 244, 269; 244, 331; 244, 343; 244, 355; 244, 373; 244, 392; 244, 400; 244, 404; 244, 417; 244, 430; 244, 442; 244, 455; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:pos; 248, binary_operator:+; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:pc; 251, identifier:reference_pos; 252, integer:1; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:r1_qnames; 256, list:[]; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:r1_nts; 260, list:[]; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:r2_qnames; 264, list:[]; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:r2_nts; 268, list:[]; 269, for_statement; 269, 270; 269, 271; 269, 274; 270, identifier:pr; 271, attribute; 271, 272; 271, 273; 272, identifier:pc; 273, identifier:pileups; 274, block; 274, 275; 274, 287; 274, 299; 274, 310; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:qnames; 278, subscript; 278, 279; 278, 282; 279, list:[r1_qnames, r2_qnames]; 279, 280; 279, 281; 280, identifier:r1_qnames; 281, identifier:r2_qnames; 282, attribute; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:pr; 285, identifier:alignment; 286, identifier:is_read2; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:nts; 290, subscript; 290, 291; 290, 294; 291, list:[r1_nts, r2_nts]; 291, 292; 291, 293; 292, identifier:r1_nts; 293, identifier:r2_nts; 294, attribute; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:pr; 297, identifier:alignment; 298, identifier:is_read2; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:nt; 302, call; 302, 303; 302, 304; 303, identifier:_pos_nt; 304, argument_list; 304, 305; 304, 306; 304, 309; 305, identifier:pr; 306, attribute; 306, 307; 306, 308; 307, identifier:pc; 308, identifier:reference_pos; 309, identifier:stranded; 310, if_statement; 310, 311; 310, 312; 311, identifier:nt; 312, block; 312, 313; 312, 324; 313, expression_statement; 313, 314; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:qnames; 317, identifier:append; 318, argument_list; 318, 319; 319, attribute; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:pr; 322, identifier:alignment; 323, identifier:qname; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:nts; 328, identifier:append; 329, argument_list; 329, 330; 330, identifier:nt; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 334; 333, identifier:r1; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:pd; 337, identifier:Series; 338, argument_list; 338, 339; 338, 340; 339, identifier:r1_nts; 340, keyword_argument; 340, 341; 340, 342; 341, identifier:index; 342, identifier:r1_qnames; 343, expression_statement; 343, 344; 344, assignment; 344, 345; 344, 346; 345, identifier:r2; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:pd; 349, identifier:Series; 350, argument_list; 350, 351; 350, 352; 351, identifier:r2_nts; 352, keyword_argument; 352, 353; 352, 354; 353, identifier:index; 354, identifier:r2_qnames; 355, expression_statement; 355, 356; 356, assignment; 356, 357; 356, 358; 357, identifier:df; 358, attribute; 358, 359; 358, 372; 359, call; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:pd; 362, identifier:DataFrame; 363, argument_list; 363, 364; 363, 367; 364, list:[r1, r2]; 364, 365; 364, 366; 365, identifier:r1; 366, identifier:r2; 367, keyword_argument; 367, 368; 367, 369; 368, identifier:index; 369, list:['R1', 'R2']; 369, 370; 369, 371; 370, string:'R1'; 371, string:'R2'; 372, identifier:T; 373, expression_statement; 373, 374; 374, assignment; 374, 375; 374, 376; 375, identifier:singles; 376, subscript; 376, 377; 376, 378; 377, identifier:df; 378, comparison_operator:==; 378, 379; 378, 391; 379, call; 379, 380; 379, 387; 380, attribute; 380, 381; 380, 386; 381, call; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, identifier:df; 384, identifier:isnull; 385, argument_list; 386, identifier:sum; 387, argument_list; 387, 388; 388, keyword_argument; 388, 389; 388, 390; 389, identifier:axis; 390, integer:1; 391, integer:1; 392, expression_statement; 392, 393; 393, assignment; 393, 394; 393, 395; 394, identifier:doubles; 395, call; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:df; 398, identifier:dropna; 399, argument_list; 400, expression_statement; 400, 401; 401, assignment; 401, 402; 401, 403; 402, identifier:vcs; 403, list:[]; 404, expression_statement; 404, 405; 405, call; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:vcs; 408, identifier:append; 409, argument_list; 409, 410; 410, call; 410, 411; 410, 416; 411, attribute; 411, 412; 411, 415; 412, subscript; 412, 413; 412, 414; 413, identifier:singles; 414, string:'R1'; 415, identifier:value_counts; 416, argument_list; 417, expression_statement; 417, 418; 418, call; 418, 419; 418, 422; 419, attribute; 419, 420; 419, 421; 420, identifier:vcs; 421, identifier:append; 422, argument_list; 422, 423; 423, call; 423, 424; 423, 429; 424, attribute; 424, 425; 424, 428; 425, subscript; 425, 426; 425, 427; 426, identifier:singles; 427, string:'R2'; 428, identifier:value_counts; 429, argument_list; 430, expression_statement; 430, 431; 431, assignment; 431, 432; 431, 433; 432, identifier:doubles; 433, subscript; 433, 434; 433, 435; 434, identifier:doubles; 435, comparison_operator:==; 435, 436; 435, 439; 436, attribute; 436, 437; 436, 438; 437, identifier:doubles; 438, identifier:R1; 439, attribute; 439, 440; 439, 441; 440, identifier:doubles; 441, identifier:R2; 442, expression_statement; 442, 443; 443, call; 443, 444; 443, 447; 444, attribute; 444, 445; 444, 446; 445, identifier:vcs; 446, identifier:append; 447, argument_list; 447, 448; 448, call; 448, 449; 448, 454; 449, attribute; 449, 450; 449, 453; 450, attribute; 450, 451; 450, 452; 451, identifier:doubles; 452, identifier:R1; 453, identifier:value_counts; 454, argument_list; 455, for_statement; 455, 456; 455, 457; 455, 458; 456, identifier:vc; 457, identifier:vcs; 458, block; 458, 459; 459, expression_statement; 459, 460; 460, augmented_assignment:+=; 460, 461; 460, 475; 461, subscript; 461, 462; 461, 465; 461, 472; 462, attribute; 462, 463; 462, 464; 463, identifier:counts; 464, identifier:ix; 465, call; 465, 466; 465, 469; 466, attribute; 466, 467; 466, 468; 467, string:'{}:{}'; 468, identifier:format; 469, argument_list; 469, 470; 469, 471; 470, identifier:chrom; 471, identifier:pos; 472, attribute; 472, 473; 472, 474; 473, identifier:vc; 474, identifier:index; 475, identifier:vc; 476, return_statement; 476, 477; 477, identifier:counts
def get_region_nt_counts(region, bam, stranded=False): """ Get counts of each nucleotide from a bam file for a given region. If R1 and R2 reads both overlap a position, only one count will be added. If the R1 and R2 reads disagree at a position they both overlap, that read pair is not used for that position. Can optionally output strand-specific counts. Parameters ---------- region : str or list Region of type chrom:start-end, chrom:start-end:strand, or [chrom, start, end]. The strand is ignored for chrom:start-end:strand. For chrom:start-end, the coordinates are one-based inclusive. For example, the query chr1:10-11 will give you the counts for the 10th and 11th bases of chr1. For [chrom, start, end], the coordinates are zero-based and end exclusive (like a bed file). The query [chr1, 9, 11] will give you the coverage of the 10th and 11th bases of chr1. The region value is passed directly to pysam's pileup function. bam : pysam.calignmentfile.AlignmentFile or str Bam file opened with pysam or path to bam file (must be sorted and indexed). stranded : boolean Boolean indicating whether read data is stranded and stranded nucleotide counts should be returned. Assumes R1 read on reverse strand implies + strand coverage etc. Returns ------- counts : pandas.DataFrame Data frame with the counts for each base in the region. The index of this data frame is one-based for compatibility with VCF files. """ # TODO: I should figure out what the different possible values are that # pysam could give me back (so far I only have ATCGN). Can I get deletions # and insertions? # TODO: This could probably be parallelized. if type(bam) == str: bam = pysam.AlignmentFile(bam, 'rb') if type(region) is str: r = parse_region(region) if len(r) == 3: chrom, start, end = r elif len(r) == 4: chrom, start, end, strand = r start = int(start) end = int(end) ind = ['{}:{}'.format(chrom, x) for x in range(start, end + 1)] pp = bam.pileup(region=region, truncate=True) elif type(region) is (list or tuple): chrom, start, end = region ind = ['{}:{}'.format(chrom, x) for x in range(int(start) + 1, int(end) + 1)] pp = bam.pileup(chrom, start, end, truncate=True) cols = ['A', 'T', 'C', 'G', 'N'] if stranded: cols = ['{}+'.format(x) for x in cols] + ['{}-'.format(x) for x in cols] counts = pd.DataFrame(0, index=ind, columns=cols) for pc in pp: # Most of this code deals with R1 and R2 reads that overlap so that we # don't get two counts from one fragment. pos = pc.reference_pos + 1 r1_qnames = [] r1_nts = [] r2_qnames = [] r2_nts = [] for pr in pc.pileups: qnames = [r1_qnames, r2_qnames][pr.alignment.is_read2] nts = [r1_nts, r2_nts][pr.alignment.is_read2] nt = _pos_nt(pr, pc.reference_pos, stranded) if nt: qnames.append(pr.alignment.qname) nts.append(nt) r1 = pd.Series(r1_nts, index=r1_qnames) r2 = pd.Series(r2_nts, index=r2_qnames) df = pd.DataFrame([r1, r2], index=['R1', 'R2']).T singles = df[df.isnull().sum(axis=1) == 1] doubles = df.dropna() vcs = [] vcs.append(singles['R1'].value_counts()) vcs.append(singles['R2'].value_counts()) doubles = doubles[doubles.R1 == doubles.R2] vcs.append(doubles.R1.value_counts()) for vc in vcs: counts.ix['{}:{}'.format(chrom, pos), vc.index] += vc return counts
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:nt_counts; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:bam; 5, identifier:positions; 6, default_parameter; 6, 7; 6, 8; 7, identifier:stranded; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:vcf; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:bed; 14, False; 15, block; 15, 16; 15, 18; 15, 86; 15, 156; 15, 160; 15, 200; 15, 209; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 24; 19, boolean_operator:and; 19, 20; 19, 22; 20, not_operator; 20, 21; 21, identifier:bed; 22, not_operator; 22, 23; 23, identifier:vcf; 24, block; 24, 25; 25, if_statement; 25, 26; 25, 36; 25, 45; 25, 59; 25, 73; 26, comparison_operator:==; 26, 27; 26, 31; 27, call; 27, 28; 27, 29; 28, identifier:type; 29, argument_list; 29, 30; 30, identifier:positions; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:pbt; 34, identifier:bedtool; 35, identifier:BedTool; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:df; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:positions; 43, identifier:to_dataframe; 44, argument_list; 45, elif_clause; 45, 46; 45, 54; 46, comparison_operator:==; 46, 47; 46, 53; 47, subscript; 47, 48; 47, 49; 48, identifier:positions; 49, slice; 49, 50; 49, 52; 50, unary_operator:-; 50, 51; 51, integer:4; 52, colon; 53, string:'.bed'; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:bed; 58, True; 59, elif_clause; 59, 60; 59, 68; 60, comparison_operator:==; 60, 61; 60, 67; 61, subscript; 61, 62; 61, 63; 62, identifier:positions; 63, slice; 63, 64; 63, 66; 64, unary_operator:-; 64, 65; 65, integer:4; 66, colon; 67, string:'.vcf'; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:vcf; 72, True; 73, else_clause; 73, 74; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:sys; 80, identifier:stderr; 81, identifier:write; 82, argument_list; 82, 83; 83, concatenated_string; 83, 84; 83, 85; 84, string:'Positions must be BedTool, bed file, or vcf '; 85, string:'file.\n'; 86, if_statement; 86, 87; 86, 88; 86, 102; 87, identifier:bed; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:df; 92, call; 92, 93; 92, 101; 93, attribute; 93, 94; 93, 100; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:pbt; 97, identifier:BedTool; 98, argument_list; 98, 99; 99, identifier:positions; 100, identifier:to_dataframe; 101, argument_list; 102, elif_clause; 102, 103; 102, 104; 103, identifier:vcf; 104, block; 104, 105; 104, 110; 104, 117; 104, 130; 104, 138; 104, 148; 105, import_from_statement; 105, 106; 105, 108; 106, dotted_name; 106, 107; 107, identifier:variants; 108, dotted_name; 108, 109; 109, identifier:vcf_as_df; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:tdf; 113, call; 113, 114; 113, 115; 114, identifier:vcf_as_df; 115, argument_list; 115, 116; 116, identifier:positions; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:df; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:pd; 123, identifier:DataFrame; 124, argument_list; 124, 125; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:index; 127, attribute; 127, 128; 127, 129; 128, identifier:tdf; 129, identifier:index; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:df; 134, string:'chrom'; 135, attribute; 135, 136; 135, 137; 136, identifier:tdf; 137, identifier:CHROM; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 143; 140, subscript; 140, 141; 140, 142; 141, identifier:df; 142, string:'start'; 143, binary_operator:-; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:tdf; 146, identifier:POS; 147, integer:1; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:df; 152, string:'end'; 153, attribute; 153, 154; 153, 155; 154, identifier:tdf; 155, identifier:POS; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:res; 159, list:[]; 160, for_statement; 160, 161; 160, 162; 160, 165; 161, identifier:i; 162, attribute; 162, 163; 162, 164; 163, identifier:df; 164, identifier:index; 165, block; 165, 166; 165, 188; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:region; 169, list:[df.ix[i, 'chrom'], df.ix[i, 'start'], df.ix[i, 'end']]; 169, 170; 169, 176; 169, 182; 170, subscript; 170, 171; 170, 174; 170, 175; 171, attribute; 171, 172; 171, 173; 172, identifier:df; 173, identifier:ix; 174, identifier:i; 175, string:'chrom'; 176, subscript; 176, 177; 176, 180; 176, 181; 177, attribute; 177, 178; 177, 179; 178, identifier:df; 179, identifier:ix; 180, identifier:i; 181, string:'start'; 182, subscript; 182, 183; 182, 186; 182, 187; 183, attribute; 183, 184; 183, 185; 184, identifier:df; 185, identifier:ix; 186, identifier:i; 187, string:'end'; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:res; 192, identifier:append; 193, argument_list; 193, 194; 194, call; 194, 195; 194, 196; 195, identifier:get_region_nt_counts; 196, argument_list; 196, 197; 196, 198; 196, 199; 197, identifier:region; 198, identifier:bam; 199, identifier:stranded; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:res; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:pd; 206, identifier:concat; 207, argument_list; 207, 208; 208, identifier:res; 209, return_statement; 209, 210; 210, identifier:res
def nt_counts(bam, positions, stranded=False, vcf=False, bed=False): """ Find the number of nucleotides covered at all positions in a bed or vcf file. Parameters ---------- bam : str or pysam.calignmentfile.AlignmentFile Bam file opened with pysam or path to bam file (must be sorted and indexed). positions : str or pybedtools.BedTool Path to bed or vcf file or pybedtools.BedTool object. The extension is used to determine whether the file is a bed or vcf (.bed vs .vcf). stranded : boolean Boolean indicating whether read data is stranded and stranded nucleotide counts should be returned. Assumes R1 read on reverse strand implies + strand coverage etc. vcf : boolean Set to True if you are providing a vcf file that doesn't have a .vcf suffix. bed : boolean Set to True if you are providing a bed file that doesn't have a .bed suffix. Returns ------- counts : pandas.DataFrame Data frame with the counts for each base in the region. The index of this data frame is one-based for compatibility with VCF files. """ if not bed and not vcf: if type(positions) == pbt.bedtool.BedTool: df = positions.to_dataframe() elif positions[-4:] == '.bed': bed = True elif positions[-4:] == '.vcf': vcf = True else: sys.stderr.write('Positions must be BedTool, bed file, or vcf ' 'file.\n') if bed: df = pbt.BedTool(positions).to_dataframe() elif vcf: from variants import vcf_as_df tdf = vcf_as_df(positions) df = pd.DataFrame(index=tdf.index) df['chrom'] = tdf.CHROM df['start'] = tdf.POS - 1 df['end'] = tdf.POS res = [] for i in df.index: region = [df.ix[i, 'chrom'], df.ix[i, 'start'], df.ix[i, 'end']] res.append(get_region_nt_counts(region, bam, stranded)) res = pd.concat(res) return res
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:add_host; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:host; 6, default_parameter; 6, 7; 6, 8; 7, identifier:group_names; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:host_vars; 11, None; 12, block; 12, 13; 12, 15; 12, 22; 12, 29; 12, 30; 12, 31; 12, 32; 12, 38; 12, 48; 12, 58; 12, 75; 12, 85; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:gnames; 18, conditional_expression:if; 18, 19; 18, 20; 18, 21; 19, identifier:group_names; 20, identifier:group_names; 21, list:[]; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:hvars; 25, conditional_expression:if; 25, 26; 25, 27; 25, 28; 26, identifier:host_vars; 27, identifier:host_vars; 28, dictionary; 29, comment; 30, comment; 31, comment; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:gnames; 36, identifier:sort; 37, argument_list; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 47; 40, subscript; 40, 41; 40, 42; 41, identifier:hvars; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:A; 45, identifier:server; 46, identifier:GROUP_NAMES; 47, identifier:gnames; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 57; 50, subscript; 50, 51; 50, 52; 51, identifier:hvars; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:A; 55, identifier:server; 56, identifier:INV_NAME; 57, identifier:host; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 67; 60, subscript; 60, 61; 60, 62; 61, identifier:hvars; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:A; 65, identifier:server; 66, identifier:INV_NAME_SHORT; 67, subscript; 67, 68; 67, 74; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:host; 71, identifier:split; 72, argument_list; 72, 73; 73, string:'.'; 74, integer:0; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:groups_and_vars; 81, identifier:merge; 82, argument_list; 82, 83; 82, 84; 83, identifier:host; 84, identifier:hvars; 85, for_statement; 85, 86; 85, 87; 85, 88; 86, identifier:gname; 87, identifier:group_names; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:groups_and_vars; 95, identifier:append; 96, argument_list; 96, 97; 96, 98; 97, identifier:gname; 98, identifier:host
def add_host(self, host, group_names=None, host_vars=None): """ Used by deployers to add hosts to the inventory. :param str host: The host identifier (e.g. hostname, IP address) to use in the inventory. :param list group_names: A list of group names to which the host belongs. **Note: This list will be sorted in-place.** :param dict host_vars: A mapping object of host *variables*. This can be a nested structure, and is used as the source of all the variables provided to the ansible playbooks. **Note: Additional key-value pairs (e.g. dynamic ansible values like ``inventory_hostname``) will be inserted into this mapping object.** """ gnames = group_names if group_names else [] hvars = host_vars if host_vars else {} # Add in ansible's magic variables. Assign them here because this is # just about the earliest point we can calculate them before anything # ansible-related (e.g. Stack.configure(), ``bang --host``) executes. gnames.sort() hvars[A.server.GROUP_NAMES] = gnames hvars[A.server.INV_NAME] = host hvars[A.server.INV_NAME_SHORT] = host.split('.')[0] self.groups_and_vars.merge(host, hvars) for gname in group_names: self.groups_and_vars.append(gname, host)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:beds_to_boolean; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:beds; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ref; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:beds_sorted; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:ref_sorted; 13, False; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kwargs; 16, block; 16, 17; 16, 19; 16, 28; 16, 32; 16, 94; 16, 102; 16, 143; 16, 147; 16, 171; 16, 186; 16, 249; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:beds; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:copy; 25, identifier:deepcopy; 26, argument_list; 26, 27; 27, identifier:beds; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:fns; 31, list:[]; 32, for_statement; 32, 33; 32, 36; 32, 40; 33, pattern_list; 33, 34; 33, 35; 34, identifier:i; 35, identifier:v; 36, call; 36, 37; 36, 38; 37, identifier:enumerate; 38, argument_list; 38, 39; 39, identifier:beds; 40, block; 40, 41; 40, 78; 41, if_statement; 41, 42; 41, 48; 41, 67; 42, comparison_operator:==; 42, 43; 42, 47; 43, call; 43, 44; 43, 45; 44, identifier:type; 45, argument_list; 45, 46; 46, identifier:v; 47, identifier:str; 48, block; 48, 49; 48, 56; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:fns; 53, identifier:append; 54, argument_list; 54, 55; 55, identifier:v; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:beds; 60, identifier:i; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:pbt; 64, identifier:BedTool; 65, argument_list; 65, 66; 66, identifier:v; 67, else_clause; 67, 68; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:fns; 73, identifier:append; 74, argument_list; 74, 75; 75, attribute; 75, 76; 75, 77; 76, identifier:v; 77, identifier:fn; 78, if_statement; 78, 79; 78, 81; 79, not_operator; 79, 80; 80, identifier:beds_sorted; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:beds; 86, identifier:i; 87, call; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:beds; 91, identifier:i; 92, identifier:sort; 93, argument_list; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:names; 97, call; 97, 98; 97, 99; 98, identifier:_sample_names; 99, argument_list; 99, 100; 99, 101; 100, identifier:fns; 101, identifier:kwargs; 102, if_statement; 102, 103; 102, 104; 102, 134; 103, identifier:ref; 104, block; 104, 105; 104, 122; 105, if_statement; 105, 106; 105, 112; 106, comparison_operator:==; 106, 107; 106, 111; 107, call; 107, 108; 107, 109; 108, identifier:type; 109, argument_list; 109, 110; 110, identifier:ref; 111, identifier:str; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:ref; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:pbt; 119, identifier:BedTool; 120, argument_list; 120, 121; 121, identifier:ref; 122, if_statement; 122, 123; 122, 125; 123, not_operator; 123, 124; 124, identifier:ref_sorted; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:ref; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:ref; 132, identifier:sort; 133, argument_list; 134, else_clause; 134, 135; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:ref; 139, call; 139, 140; 139, 141; 140, identifier:combine; 141, argument_list; 141, 142; 142, identifier:beds; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:ind; 146, list:[]; 147, for_statement; 147, 148; 147, 149; 147, 150; 148, identifier:r; 149, identifier:ref; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:ind; 155, identifier:append; 156, argument_list; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, string:'{}:{}-{}'; 160, identifier:format; 161, argument_list; 161, 162; 161, 165; 161, 168; 162, attribute; 162, 163; 162, 164; 163, identifier:r; 164, identifier:chrom; 165, attribute; 165, 166; 165, 167; 166, identifier:r; 167, identifier:start; 168, attribute; 168, 169; 168, 170; 169, identifier:r; 170, identifier:stop; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:bdf; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:pd; 177, identifier:DataFrame; 178, argument_list; 178, 179; 178, 180; 178, 183; 179, integer:0; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:index; 182, identifier:ind; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:columns; 185, identifier:names; 186, for_statement; 186, 187; 186, 190; 186, 194; 187, pattern_list; 187, 188; 187, 189; 188, identifier:i; 189, identifier:bed; 190, call; 190, 191; 190, 192; 191, identifier:enumerate; 192, argument_list; 192, 193; 193, identifier:beds; 194, block; 194, 195; 194, 210; 194, 214; 194, 238; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:res; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:ref; 201, identifier:intersect; 202, argument_list; 202, 203; 202, 204; 202, 207; 203, identifier:bed; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:sorted; 206, True; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:wa; 209, True; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:ind; 213, list:[]; 214, for_statement; 214, 215; 214, 216; 214, 217; 215, identifier:r; 216, identifier:res; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:ind; 222, identifier:append; 223, argument_list; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, string:'{}:{}-{}'; 227, identifier:format; 228, argument_list; 228, 229; 228, 232; 228, 235; 229, attribute; 229, 230; 229, 231; 230, identifier:r; 231, identifier:chrom; 232, attribute; 232, 233; 232, 234; 233, identifier:r; 234, identifier:start; 235, attribute; 235, 236; 235, 237; 236, identifier:r; 237, identifier:stop; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 248; 240, subscript; 240, 241; 240, 244; 240, 245; 241, attribute; 241, 242; 241, 243; 242, identifier:bdf; 243, identifier:ix; 244, identifier:ind; 245, subscript; 245, 246; 245, 247; 246, identifier:names; 247, identifier:i; 248, integer:1; 249, return_statement; 249, 250; 250, identifier:bdf
def beds_to_boolean(beds, ref=None, beds_sorted=False, ref_sorted=False, **kwargs): """ Compare a list of bed files or BedTool objects to a reference bed file and create a boolean matrix where each row is an interval and each column is a 1 if that file has an interval that overlaps the row interval and a 0 otherwise. If no reference bed is provided, the provided bed files will be merged into a single bed and compared to that. Parameters ---------- beds : list List of paths to bed files or BedTool objects. ref : str or BedTool Reference bed file to compare against. If no reference bed is provided, the provided bed files will be merged into a single bed and compared to that. beds_sorted : boolean Whether the bed files in beds are already sorted. If False, all bed files in beds will be sorted. ref_sorted : boolean Whether the reference bed file is sorted. If False, ref will be sorted. names : list of strings Names to use for columns of output files. Overrides define_sample_name if provided. define_sample_name : function that takes string as input Function mapping filename to sample name (or basename). For instance, you may have the basename in the path and use a regex to extract it. The basenames will be used as the column names. If this is not provided, the columns will be named as the input files. Returns ------- out : pandas.DataFrame Boolean data frame indicating whether each bed file has an interval that overlaps each interval in the reference bed file. """ beds = copy.deepcopy(beds) fns = [] for i,v in enumerate(beds): if type(v) == str: fns.append(v) beds[i] = pbt.BedTool(v) else: fns.append(v.fn) if not beds_sorted: beds[i] = beds[i].sort() names = _sample_names(fns, kwargs) if ref: if type(ref) == str: ref = pbt.BedTool(ref) if not ref_sorted: ref = ref.sort() else: ref = combine(beds) ind = [] for r in ref: ind.append('{}:{}-{}'.format(r.chrom, r.start, r.stop)) bdf = pd.DataFrame(0, index=ind, columns=names) for i,bed in enumerate(beds): res = ref.intersect(bed, sorted=True, wa=True) ind = [] for r in res: ind.append('{}:{}-{}'.format(r.chrom, r.start, r.stop)) bdf.ix[ind, names[i]] = 1 return bdf
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:combine; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:beds; 5, default_parameter; 5, 6; 5, 7; 6, identifier:beds_sorted; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:postmerge; 10, True; 11, block; 11, 12; 11, 14; 11, 23; 11, 67; 11, 68; 11, 69; 11, 70; 11, 71; 11, 91; 11, 99; 11, 110; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:beds; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:copy; 20, identifier:deepcopy; 21, argument_list; 21, 22; 22, identifier:beds; 23, for_statement; 23, 24; 23, 27; 23, 31; 24, pattern_list; 24, 25; 24, 26; 25, identifier:i; 26, identifier:v; 27, call; 27, 28; 27, 29; 28, identifier:enumerate; 29, argument_list; 29, 30; 30, identifier:beds; 31, block; 31, 32; 31, 51; 32, if_statement; 32, 33; 32, 39; 33, comparison_operator:==; 33, 34; 33, 38; 34, call; 34, 35; 34, 36; 35, identifier:type; 36, argument_list; 36, 37; 37, identifier:v; 38, identifier:str; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 45; 42, subscript; 42, 43; 42, 44; 43, identifier:beds; 44, identifier:i; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:pbt; 48, identifier:BedTool; 49, argument_list; 49, 50; 50, identifier:v; 51, if_statement; 51, 52; 51, 54; 52, not_operator; 52, 53; 53, identifier:beds_sorted; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 60; 57, subscript; 57, 58; 57, 59; 58, identifier:beds; 59, identifier:i; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:beds; 64, identifier:i; 65, identifier:sort; 66, argument_list; 67, comment; 68, comment; 69, comment; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:out; 74, call; 74, 75; 74, 76; 75, identifier:reduce; 76, argument_list; 76, 77; 76, 90; 77, lambda; 77, 78; 77, 81; 78, lambda_parameters; 78, 79; 78, 80; 79, identifier:x; 80, identifier:y; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:x; 84, identifier:cat; 85, argument_list; 85, 86; 85, 87; 86, identifier:y; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:postmerge; 89, False; 90, identifier:beds; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:out; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:out; 97, identifier:sort; 98, argument_list; 99, if_statement; 99, 100; 99, 101; 100, identifier:postmerge; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:out; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:out; 108, identifier:merge; 109, argument_list; 110, return_statement; 110, 111; 111, identifier:out
def combine(beds, beds_sorted=False, postmerge=True): """ Combine a list of bed files or BedTool objects into a single BedTool object. Parameters ---------- beds : list List of paths to bed files or BedTool objects. beds_sorted : boolean Whether the bed files in beds are already sorted. If False, all bed files in beds will be sorted. postmerge : boolean Whether to merge intervals after combining beds together. Returns ------- out : pybedtools.BedTool New sorted BedTool with intervals from all input beds. """ beds = copy.deepcopy(beds) for i,v in enumerate(beds): if type(v) == str: beds[i] = pbt.BedTool(v) if not beds_sorted: beds[i] = beds[i].sort() # For some reason, doing the merging in the reduce statement doesn't work. I # think this might be a pybedtools bug. In any fashion, I can merge # afterward although I think it makes a performance hit because the combined # bed file grows larger than it needs to. out = reduce(lambda x,y : x.cat(y, postmerge=False), beds) out = out.sort() if postmerge: out = out.merge() return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sanitize_config_loglevel; 3, parameters; 3, 4; 4, identifier:level; 5, block; 5, 6; 5, 8; 5, 31; 5, 35; 5, 53; 5, 64; 6, expression_statement; 6, 7; 7, string:''' Kinda sorta backport of loglevel sanitization for Python 2.6. '''; 8, if_statement; 8, 9; 8, 28; 9, boolean_operator:or; 9, 10; 9, 21; 10, comparison_operator:!=; 10, 11; 10, 18; 11, subscript; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:sys; 14, identifier:version_info; 15, slice; 15, 16; 15, 17; 16, colon; 17, integer:2; 18, tuple; 18, 19; 18, 20; 19, integer:2; 20, integer:6; 21, call; 21, 22; 21, 23; 22, identifier:isinstance; 23, argument_list; 23, 24; 23, 25; 24, identifier:level; 25, tuple; 25, 26; 25, 27; 26, identifier:int; 27, identifier:long; 28, block; 28, 29; 29, return_statement; 29, 30; 30, identifier:level; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:lvl; 34, None; 35, if_statement; 35, 36; 35, 41; 36, call; 36, 37; 36, 38; 37, identifier:isinstance; 38, argument_list; 38, 39; 38, 40; 39, identifier:level; 40, identifier:basestring; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:lvl; 45, call; 45, 46; 45, 51; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:logging; 49, identifier:_levelNames; 50, identifier:get; 51, argument_list; 51, 52; 52, identifier:level; 53, if_statement; 53, 54; 53, 56; 54, not_operator; 54, 55; 55, identifier:lvl; 56, block; 56, 57; 57, raise_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:ValueError; 60, argument_list; 60, 61; 61, binary_operator:%; 61, 62; 61, 63; 62, string:'Invalid log level, %s'; 63, identifier:level; 64, return_statement; 64, 65; 65, identifier:lvl
def sanitize_config_loglevel(level): ''' Kinda sorta backport of loglevel sanitization for Python 2.6. ''' if sys.version_info[:2] != (2, 6) or isinstance(level, (int, long)): return level lvl = None if isinstance(level, basestring): lvl = logging._levelNames.get(level) if not lvl: raise ValueError('Invalid log level, %s' % level) return lvl
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:prt_js; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:js; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort_keys; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:indent; 10, integer:4; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:print; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:js2str; 20, argument_list; 20, 21; 20, 22; 20, 23; 21, identifier:js; 22, identifier:sort_keys; 23, identifier:indent
def prt_js(js, sort_keys=True, indent=4): """Print Json in pretty format. There's a standard module pprint, can pretty print python dict and list. But it doesn't support sorted key. That why we need this func. Usage:: >>> from weatherlab.lib.dataIO.js import prt_js >>> prt_js({"a": 1, "b": 2}) { "a": 1, "b": 2 } **中文文档** 以人类可读的方式打印可Json化的Python对象。 """ print(js2str(js, sort_keys, indent))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:find_field_names; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:fields; 5, default_parameter; 5, 6; 5, 7; 6, identifier:model; 7, identifier:DEFAULT_MODEL; 8, default_parameter; 8, 9; 8, 10; 9, identifier:app; 10, identifier:DEFAULT_APP; 11, default_parameter; 11, 12; 11, 13; 12, identifier:score_cutoff; 13, integer:50; 14, default_parameter; 14, 15; 14, 16; 15, identifier:pad_with_none; 16, False; 17, block; 17, 18; 17, 20; 17, 29; 17, 37; 17, 47; 17, 51; 17, 98; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:fields; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:util; 26, identifier:listify; 27, argument_list; 27, 28; 28, identifier:fields; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:model; 32, call; 32, 33; 32, 34; 33, identifier:get_model; 34, argument_list; 34, 35; 34, 36; 35, identifier:model; 36, identifier:app; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:available_field_names; 40, call; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:model; 44, identifier:_meta; 45, identifier:get_all_field_names; 46, argument_list; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:matched_fields; 50, list:[]; 51, for_statement; 51, 52; 51, 53; 51, 54; 52, identifier:field_name; 53, identifier:fields; 54, block; 54, 55; 54, 68; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:match; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:fuzzy; 61, identifier:extractOne; 62, argument_list; 62, 63; 62, 67; 63, call; 63, 64; 63, 65; 64, identifier:str; 65, argument_list; 65, 66; 66, identifier:field_name; 67, identifier:available_field_names; 68, if_statement; 68, 69; 68, 82; 68, 90; 69, boolean_operator:and; 69, 70; 69, 77; 70, boolean_operator:and; 70, 71; 70, 72; 71, identifier:match; 72, comparison_operator:is; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:match; 75, integer:1; 76, None; 77, comparison_operator:>=; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:match; 80, integer:1; 81, identifier:score_cutoff; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, augmented_assignment:+=; 84, 85; 84, 86; 85, identifier:matched_fields; 86, list:[match[0]]; 86, 87; 87, subscript; 87, 88; 87, 89; 88, identifier:match; 89, integer:0; 90, elif_clause; 90, 91; 90, 92; 91, identifier:pad_with_none; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, augmented_assignment:+=; 94, 95; 94, 96; 95, identifier:matched_fields; 96, list:[None]; 96, 97; 97, None; 98, return_statement; 98, 99; 99, identifier:matched_fields
def find_field_names(fields, model=DEFAULT_MODEL, app=DEFAULT_APP, score_cutoff=50, pad_with_none=False): """Use fuzzy string matching to find similar model field names without consulting a synonyms list Returns: list: A list model field names (strings) sorted from most likely to least likely. [] If no similar field names could be found in the indicated model [None] If none found and and `pad_with_none` set Examples: >>> find_field_names(['date_time', 'title_prefix', 'sales'], model='WikiItem') ['date', 'model', 'net_sales'] """ fields = util.listify(fields) model = get_model(model, app) available_field_names = model._meta.get_all_field_names() matched_fields = [] for field_name in fields: match = fuzzy.extractOne(str(field_name), available_field_names) if match and match[1] is not None and match[1] >= score_cutoff: matched_fields += [match[0]] elif pad_with_none: matched_fields += [None] return matched_fields
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 34; 2, function_name:lagged_in_date; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 3, 28; 3, 31; 4, default_parameter; 4, 5; 4, 6; 5, identifier:x; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:y; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:filter_dict; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:model; 15, string:'WikiItem'; 16, default_parameter; 16, 17; 16, 18; 17, identifier:app; 18, identifier:DEFAULT_APP; 19, default_parameter; 19, 20; 19, 21; 20, identifier:sort; 21, True; 22, default_parameter; 22, 23; 22, 24; 23, identifier:limit; 24, integer:30000; 25, default_parameter; 25, 26; 25, 27; 26, identifier:lag; 27, integer:1; 28, default_parameter; 28, 29; 28, 30; 29, identifier:pad; 30, integer:0; 31, default_parameter; 31, 32; 31, 33; 32, identifier:truncate; 33, True; 34, block; 34, 35; 34, 37; 34, 46; 34, 47; 34, 90; 34, 164; 34, 240; 35, expression_statement; 35, 36; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:lag; 40, call; 40, 41; 40, 42; 41, identifier:int; 42, argument_list; 42, 43; 43, boolean_operator:or; 43, 44; 43, 45; 44, identifier:lag; 45, integer:0; 46, comment; 47, if_statement; 47, 48; 47, 59; 48, boolean_operator:and; 48, 49; 48, 54; 49, call; 49, 50; 49, 51; 50, identifier:isinstance; 51, argument_list; 51, 52; 51, 53; 52, identifier:x; 53, identifier:basestring; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 58; 57, identifier:y; 58, identifier:basestring; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 65; 62, pattern_list; 62, 63; 62, 64; 63, identifier:x; 64, identifier:y; 65, call; 65, 66; 65, 67; 66, identifier:sequence_from_filter_spec; 67, argument_list; 67, 68; 67, 77; 67, 78; 67, 81; 67, 84; 67, 87; 68, list:[find_synonymous_field(x), find_synonymous_field(y)]; 68, 69; 68, 73; 69, call; 69, 70; 69, 71; 70, identifier:find_synonymous_field; 71, argument_list; 71, 72; 72, identifier:x; 73, call; 73, 74; 73, 75; 74, identifier:find_synonymous_field; 75, argument_list; 75, 76; 76, identifier:y; 77, identifier:filter_dict; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:model; 80, identifier:model; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:app; 83, identifier:app; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:sort; 86, identifier:sort; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:limit; 89, identifier:limit; 90, if_statement; 90, 91; 90, 102; 91, boolean_operator:and; 91, 92; 91, 93; 92, identifier:y; 93, comparison_operator:==; 93, 94; 93, 98; 94, call; 94, 95; 94, 96; 95, identifier:len; 96, argument_list; 96, 97; 97, identifier:y; 98, call; 98, 99; 98, 100; 99, identifier:len; 100, argument_list; 100, 101; 101, identifier:x; 102, block; 102, 103; 102, 148; 103, if_statement; 103, 104; 103, 105; 104, identifier:sort; 105, block; 105, 106; 105, 128; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:xy; 109, call; 109, 110; 109, 111; 110, identifier:sorted; 111, argument_list; 111, 112; 111, 117; 112, call; 112, 113; 112, 114; 113, identifier:zip; 114, argument_list; 114, 115; 114, 116; 115, identifier:x; 116, identifier:y; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:reverse; 119, call; 119, 120; 119, 121; 120, identifier:bool; 121, argument_list; 121, 122; 122, comparison_operator:<; 122, 123; 122, 127; 123, call; 123, 124; 123, 125; 124, identifier:int; 125, argument_list; 125, 126; 126, identifier:sort; 127, integer:0; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 133; 130, pattern_list; 130, 131; 130, 132; 131, identifier:x; 132, identifier:y; 133, expression_list; 133, 134; 133, 141; 134, list_comprehension; 134, 135; 134, 136; 135, identifier:col1; 136, for_in_clause; 136, 137; 136, 140; 137, pattern_list; 137, 138; 137, 139; 138, identifier:col1; 139, identifier:col2; 140, identifier:xy; 141, list_comprehension; 141, 142; 141, 143; 142, identifier:col2; 143, for_in_clause; 143, 144; 143, 147; 144, pattern_list; 144, 145; 144, 146; 145, identifier:col1; 146, identifier:col2; 147, identifier:xy; 148, return_statement; 148, 149; 149, expression_list; 149, 150; 149, 151; 150, identifier:x; 151, call; 151, 152; 151, 153; 152, identifier:lagged_seq; 153, argument_list; 153, 154; 153, 155; 153, 158; 153, 161; 154, identifier:y; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:lag; 157, identifier:lag; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:pad; 160, identifier:pad; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:truncate; 163, identifier:truncate; 164, if_statement; 164, 165; 164, 184; 164, 185; 165, boolean_operator:and; 165, 166; 165, 172; 166, boolean_operator:and; 166, 167; 166, 168; 167, identifier:x; 168, call; 168, 169; 168, 170; 169, identifier:len; 170, argument_list; 170, 171; 171, identifier:x; 172, comparison_operator:==; 172, 173; 172, 174; 172, 178; 173, integer:2; 174, call; 174, 175; 174, 176; 175, identifier:len; 176, argument_list; 176, 177; 177, identifier:x; 178, call; 178, 179; 178, 180; 179, identifier:len; 180, argument_list; 180, 181; 181, subscript; 181, 182; 181, 183; 182, identifier:x; 183, integer:0; 184, comment; 185, block; 185, 186; 185, 210; 185, 239; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 191; 188, pattern_list; 188, 189; 188, 190; 189, identifier:x; 190, identifier:y; 191, expression_list; 191, 192; 191, 195; 192, subscript; 192, 193; 192, 194; 193, identifier:x; 194, integer:0; 195, call; 195, 196; 195, 197; 196, identifier:lagged_seq; 197, argument_list; 197, 198; 197, 201; 197, 204; 197, 207; 198, subscript; 198, 199; 198, 200; 199, identifier:x; 200, integer:1; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:lag; 203, identifier:lag; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:pad; 206, identifier:pad; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:truncate; 209, identifier:truncate; 210, if_statement; 210, 211; 210, 212; 211, identifier:truncate; 212, block; 212, 213; 212, 216; 213, print_statement; 213, 214; 213, 215; 214, identifier:truncate; 215, identifier:lag; 216, if_statement; 216, 217; 216, 220; 216, 229; 217, comparison_operator:>=; 217, 218; 217, 219; 218, identifier:lag; 219, integer:0; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:x; 224, subscript; 224, 225; 224, 226; 225, identifier:x; 226, slice; 226, 227; 226, 228; 227, identifier:lag; 228, colon; 229, else_clause; 229, 230; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:x; 234, subscript; 234, 235; 234, 236; 235, identifier:x; 236, slice; 236, 237; 236, 238; 237, colon; 238, identifier:lag; 239, comment; 240, return_statement; 240, 241; 241, expression_list; 241, 242; 241, 243; 242, identifier:x; 243, identifier:y
def lagged_in_date(x=None, y=None, filter_dict=None, model='WikiItem', app=DEFAULT_APP, sort=True, limit=30000, lag=1, pad=0, truncate=True): """ Lag the y values by the specified number of samples. FIXME: sort has no effect when sequences provided in x, y instead of field names >>> lagged_in_date(x=[.1,.2,.3,.4], y=[1,2,3,4], limit=4, lag=1) ([0.1, 0.2, 0.3, 0.4], [0, 1, 2, 3]) >>> lagged_in_date(x=[.1,.2,.3,.4], y=[1,2,3,4], lag=1, truncate=True) ([0.1, 0.2, 0.3, 0.4], [0, 1, 2, 3]) """ lag = int(lag or 0) #print 'X, Y:', x, y if isinstance(x, basestring) and isinstance(y, basestring): x, y = sequence_from_filter_spec([find_synonymous_field(x), find_synonymous_field(y)], filter_dict, model=model, app=app, sort=sort, limit=limit) if y and len(y) == len(x): if sort: xy = sorted(zip(x,y), reverse=bool(int(sort) < 0)) x, y = [col1 for col1, col2 in xy], [col2 for col1, col2 in xy] return x, lagged_seq(y, lag=lag, pad=pad, truncate=truncate) if x and len(x) and 2 == len(x) <= len(x[0]): #print 'X:', x x, y = x[0], lagged_seq(x[1], lag=lag, pad=pad, truncate=truncate) if truncate: print truncate, lag if lag >= 0: x = x[lag:] else: x = x[:lag] #print x, y return x, y
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:percentile; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:sorted_list; 5, identifier:percent; 6, default_parameter; 6, 7; 6, 8; 7, identifier:key; 8, lambda; 8, 9; 8, 11; 9, lambda_parameters; 9, 10; 10, identifier:x; 11, identifier:x; 12, block; 12, 13; 12, 15; 12, 21; 12, 34; 12, 46; 12, 53; 12, 59; 12, 87; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 18; 16, not_operator; 16, 17; 17, identifier:sorted_list; 18, block; 18, 19; 19, return_statement; 19, 20; 20, None; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:==; 22, 23; 22, 24; 23, identifier:percent; 24, integer:1; 25, block; 25, 26; 26, return_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:float; 29, argument_list; 29, 30; 30, subscript; 30, 31; 30, 32; 31, identifier:sorted_list; 32, unary_operator:-; 32, 33; 33, integer:1; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:==; 35, 36; 35, 37; 36, identifier:percent; 37, integer:0; 38, block; 38, 39; 39, return_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:float; 42, argument_list; 42, 43; 43, subscript; 43, 44; 43, 45; 44, identifier:sorted_list; 45, integer:0; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:n; 49, call; 49, 50; 49, 51; 50, identifier:len; 51, argument_list; 51, 52; 52, identifier:sorted_list; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:i; 56, binary_operator:*; 56, 57; 56, 58; 57, identifier:percent; 58, identifier:n; 59, if_statement; 59, 60; 59, 66; 60, comparison_operator:==; 60, 61; 60, 65; 61, call; 61, 62; 61, 63; 62, identifier:ceil; 63, argument_list; 63, 64; 64, identifier:i; 65, identifier:i; 66, block; 66, 67; 66, 74; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:i; 70, call; 70, 71; 70, 72; 71, identifier:int; 72, argument_list; 72, 73; 73, identifier:i; 74, return_statement; 74, 75; 75, binary_operator:/; 75, 76; 75, 86; 76, parenthesized_expression; 76, 77; 77, binary_operator:+; 77, 78; 77, 83; 78, subscript; 78, 79; 78, 80; 79, identifier:sorted_list; 80, binary_operator:-; 80, 81; 80, 82; 81, identifier:i; 82, integer:1; 83, subscript; 83, 84; 83, 85; 84, identifier:sorted_list; 85, identifier:i; 86, integer:2; 87, return_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:float; 90, argument_list; 90, 91; 91, subscript; 91, 92; 91, 93; 92, identifier:sorted_list; 93, binary_operator:-; 93, 94; 93, 98; 94, call; 94, 95; 94, 96; 95, identifier:ceil; 96, argument_list; 96, 97; 97, identifier:i; 98, integer:1
def percentile(sorted_list, percent, key=lambda x: x): """Find the percentile of a sorted list of values. Arguments --------- sorted_list : list A sorted (ascending) list of values. percent : float A float value from 0.0 to 1.0. key : function, optional An optional function to compute a value from each element of N. Returns ------- float The desired percentile of the value list. Examples -------- >>> sorted_list = [4,6,8,9,11] >>> percentile(sorted_list, 0.4) 7.0 >>> percentile(sorted_list, 0.44) 8.0 >>> percentile(sorted_list, 0.6) 8.5 >>> percentile(sorted_list, 0.99) 11.0 >>> percentile(sorted_list, 1) 11.0 >>> percentile(sorted_list, 0) 4.0 """ if not sorted_list: return None if percent == 1: return float(sorted_list[-1]) if percent == 0: return float(sorted_list[0]) n = len(sorted_list) i = percent * n if ceil(i) == i: i = int(i) return (sorted_list[i-1] + sorted_list[i]) / 2 return float(sorted_list[ceil(i)-1])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:tags; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 21; 5, 30; 5, 43; 5, 44; 5, 57; 5, 65; 6, expression_statement; 6, 7; 7, string:'''Return a list of all tags that have this semantic tag, sorted by name. :rtype: list of ckan.model.tag.Tag objects '''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:q; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:meta; 15, identifier:Session; 16, identifier:query; 17, argument_list; 17, 18; 18, attribute; 18, 19; 18, 20; 19, identifier:_tag; 20, identifier:Tag; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:q; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:q; 27, identifier:join; 28, argument_list; 28, 29; 29, identifier:TagSemanticTag; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:q; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:q; 36, identifier:filter_by; 37, argument_list; 37, 38; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:tag_id; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:id; 43, comment; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:q; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:q; 50, identifier:order_by; 51, argument_list; 51, 52; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:_tag; 55, identifier:Tag; 56, identifier:name; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:tags; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:q; 63, identifier:all; 64, argument_list; 65, return_statement; 65, 66; 66, identifier:tags
def tags(self): '''Return a list of all tags that have this semantic tag, sorted by name. :rtype: list of ckan.model.tag.Tag objects ''' q = meta.Session.query(_tag.Tag) q = q.join(TagSemanticTag) q = q.filter_by(tag_id=self.id) # q = q.filter_by(state='active') q = q.order_by(_tag.Tag.name) tags = q.all() return tags
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:make_feature_bed; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:gtf; 5, identifier:feature; 6, default_parameter; 6, 7; 6, 8; 7, identifier:out; 8, None; 9, block; 9, 10; 9, 12; 9, 16; 9, 190; 9, 207; 9, 208; 9, 216; 9, 226; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:bed_lines; 15, list:[]; 16, with_statement; 16, 17; 16, 26; 17, with_clause; 17, 18; 18, with_item; 18, 19; 19, as_pattern; 19, 20; 19, 24; 20, call; 20, 21; 20, 22; 21, identifier:open; 22, argument_list; 22, 23; 23, identifier:gtf; 24, as_pattern_target; 24, 25; 25, identifier:f; 26, block; 26, 27; 26, 39; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:line; 30, call; 30, 31; 30, 38; 31, attribute; 31, 32; 31, 37; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:f; 35, identifier:readline; 36, argument_list; 37, identifier:strip; 38, argument_list; 39, while_statement; 39, 40; 39, 43; 40, comparison_operator:!=; 40, 41; 40, 42; 41, identifier:line; 42, string:''; 43, block; 43, 44; 43, 178; 44, if_statement; 44, 45; 44, 50; 45, comparison_operator:!=; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:line; 48, integer:0; 49, string:'#'; 50, block; 50, 51; 50, 60; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:line; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:line; 57, identifier:split; 58, argument_list; 58, 59; 59, string:'\t'; 60, if_statement; 60, 61; 60, 66; 61, comparison_operator:==; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:line; 64, integer:2; 65, identifier:feature; 66, block; 66, 67; 66, 73; 66, 87; 66, 93; 66, 152; 66, 158; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:chrom; 70, subscript; 70, 71; 70, 72; 71, identifier:line; 72, integer:0; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:start; 76, call; 76, 77; 76, 78; 77, identifier:str; 78, argument_list; 78, 79; 79, binary_operator:-; 79, 80; 79, 86; 80, call; 80, 81; 80, 82; 81, identifier:int; 82, argument_list; 82, 83; 83, subscript; 83, 84; 83, 85; 84, identifier:line; 85, integer:3; 86, integer:1; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:end; 90, subscript; 90, 91; 90, 92; 91, identifier:line; 92, integer:4; 93, if_statement; 93, 94; 93, 97; 93, 123; 94, comparison_operator:==; 94, 95; 94, 96; 95, identifier:feature; 96, string:'gene'; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:name; 101, call; 101, 102; 101, 121; 102, attribute; 102, 103; 102, 120; 103, subscript; 103, 104; 103, 119; 104, call; 104, 105; 104, 117; 105, attribute; 105, 106; 105, 116; 106, subscript; 106, 107; 106, 115; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:line; 111, integer:8; 112, identifier:split; 113, argument_list; 113, 114; 114, string:';'; 115, integer:0; 116, identifier:split; 117, argument_list; 117, 118; 118, string:' '; 119, integer:1; 120, identifier:strip; 121, argument_list; 121, 122; 122, string:'"'; 123, else_clause; 123, 124; 123, 125; 123, 126; 124, comment; 125, comment; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:name; 130, call; 130, 131; 130, 150; 131, attribute; 131, 132; 131, 149; 132, subscript; 132, 133; 132, 148; 133, call; 133, 134; 133, 146; 134, attribute; 134, 135; 134, 145; 135, subscript; 135, 136; 135, 144; 136, call; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:line; 140, integer:8; 141, identifier:split; 142, argument_list; 142, 143; 143, string:';'; 144, integer:1; 145, identifier:split; 146, argument_list; 146, 147; 147, string:' '; 148, integer:2; 149, identifier:strip; 150, argument_list; 150, 151; 151, string:'"'; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:strand; 155, subscript; 155, 156; 155, 157; 156, identifier:line; 157, integer:6; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:bed_lines; 162, identifier:append; 163, argument_list; 163, 164; 164, binary_operator:+; 164, 165; 164, 177; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, string:'\t'; 168, identifier:join; 169, argument_list; 169, 170; 170, list:[chrom, start, end, name, '.', strand]; 170, 171; 170, 172; 170, 173; 170, 174; 170, 175; 170, 176; 171, identifier:chrom; 172, identifier:start; 173, identifier:end; 174, identifier:name; 175, string:'.'; 176, identifier:strand; 177, string:'\n'; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:line; 181, call; 181, 182; 181, 189; 182, attribute; 182, 183; 182, 188; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:f; 186, identifier:readline; 187, argument_list; 188, identifier:strip; 189, argument_list; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:bt; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:pbt; 196, identifier:BedTool; 197, argument_list; 197, 198; 197, 204; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, string:''; 201, identifier:join; 202, argument_list; 202, 203; 203, identifier:bed_lines; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:from_string; 206, True; 207, comment; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:bt; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:bt; 214, identifier:sort; 215, argument_list; 216, if_statement; 216, 217; 216, 218; 217, identifier:out; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:bt; 223, identifier:saveas; 224, argument_list; 224, 225; 225, identifier:out; 226, return_statement; 226, 227; 227, identifier:bt
def make_feature_bed(gtf, feature, out=None): """ Make a bed file with the start and stop coordinates for all of a particular feature in Gencode. Valid features are the features present in the third column of the Gencode GTF file. Parameters ---------- gtf : str Filename of the Gencode gtf file. feature : str Feature from third column of Gencode GTF file. As of v19, these include CDS, exon, gene, Selenocysteine, start_codon, stop_codon, transcript, and UTR. out : str If provided, the bed file will be written to a file with this name. Returns ------- bed : pybedtools.BedTool A sorted pybedtools BedTool object. """ bed_lines = [] with open(gtf) as f: line = f.readline().strip() while line != '': if line[0] != '#': line = line.split('\t') if line[2] == feature: chrom = line[0] start = str(int(line[3]) - 1) end = line[4] if feature == 'gene': name = line[8].split(';')[0].split(' ')[1].strip('"') else: # TODO: I may want to have some smarter naming for # things that aren't genes or transcripts. name = line[8].split(';')[1].split(' ')[2].strip('"') strand = line[6] bed_lines.append('\t'.join([chrom, start, end, name, '.', strand]) + '\n') line = f.readline().strip() bt = pbt.BedTool(''.join(bed_lines), from_string=True) # We'll sort so bedtools operations can be done faster. bt = bt.sort() if out: bt.saveas(out) return bt
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:filter; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:order_by; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:limit; 10, integer:0; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kwargs; 13, block; 13, 14; 13, 16; 14, expression_statement; 14, 15; 15, comment; 16, with_statement; 16, 17; 16, 25; 17, with_clause; 17, 18; 18, with_item; 18, 19; 19, as_pattern; 19, 20; 19, 23; 20, call; 20, 21; 20, 22; 21, identifier:rconnect; 22, argument_list; 23, as_pattern_target; 23, 24; 24, identifier:conn; 25, block; 25, 26; 25, 36; 26, if_statement; 26, 27; 26, 33; 27, comparison_operator:==; 27, 28; 27, 32; 28, call; 28, 29; 28, 30; 29, identifier:len; 30, argument_list; 30, 31; 31, identifier:kwargs; 32, integer:0; 33, block; 33, 34; 34, raise_statement; 34, 35; 35, identifier:ValueError; 36, try_statement; 36, 37; 36, 101; 36, 115; 36, 129; 37, block; 37, 38; 37, 46; 37, 55; 37, 70; 37, 85; 37, 92; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:query; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:_base; 45, argument_list; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:query; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:query; 52, identifier:filter; 53, argument_list; 53, 54; 54, identifier:kwargs; 55, if_statement; 55, 56; 55, 59; 56, comparison_operator:is; 56, 57; 56, 58; 57, identifier:order_by; 58, None; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:query; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:_order_by; 67, argument_list; 67, 68; 67, 69; 68, identifier:query; 69, identifier:order_by; 70, if_statement; 70, 71; 70, 74; 71, comparison_operator:>; 71, 72; 71, 73; 72, identifier:limit; 73, integer:0; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:query; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:_limit; 82, argument_list; 82, 83; 82, 84; 83, identifier:query; 84, identifier:limit; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:log; 89, identifier:debug; 90, argument_list; 90, 91; 91, identifier:query; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:rv; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:query; 98, identifier:run; 99, argument_list; 99, 100; 100, identifier:conn; 101, except_clause; 101, 102; 101, 106; 102, as_pattern; 102, 103; 102, 104; 103, identifier:ReqlOpFailedError; 104, as_pattern_target; 104, 105; 105, identifier:e; 106, block; 106, 107; 106, 114; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:log; 111, identifier:warn; 112, argument_list; 112, 113; 113, identifier:e; 114, raise_statement; 115, except_clause; 115, 116; 115, 120; 116, as_pattern; 116, 117; 116, 118; 117, identifier:Exception; 118, as_pattern_target; 118, 119; 119, identifier:e; 120, block; 120, 121; 120, 128; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:log; 125, identifier:warn; 126, argument_list; 126, 127; 127, identifier:e; 128, raise_statement; 129, else_clause; 129, 130; 130, block; 130, 131; 130, 144; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:data; 134, list_comprehension; 134, 135; 134, 141; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:_model; 139, argument_list; 139, 140; 140, identifier:_; 141, for_in_clause; 141, 142; 141, 143; 142, identifier:_; 143, identifier:rv; 144, return_statement; 144, 145; 145, identifier:data
def filter(self, order_by=None, limit=0, **kwargs): """ Fetch a list of instances. :param order_by: column on which to order the results. \ To change the sort, prepend with < or >. :param limit: How many rows to fetch. :param kwargs: keyword args on which to filter, column=value """ with rconnect() as conn: if len(kwargs) == 0: raise ValueError try: query = self._base() query = query.filter(kwargs) if order_by is not None: query = self._order_by(query, order_by) if limit > 0: query = self._limit(query, limit) log.debug(query) rv = query.run(conn) except ReqlOpFailedError as e: log.warn(e) raise except Exception as e: log.warn(e) raise else: data = [self._model(_) for _ in rv] return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:all; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:order_by; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:limit; 10, integer:0; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, with_statement; 14, 15; 14, 23; 15, with_clause; 15, 16; 16, with_item; 16, 17; 17, as_pattern; 17, 18; 17, 21; 18, call; 18, 19; 18, 20; 19, identifier:rconnect; 20, argument_list; 21, as_pattern_target; 21, 22; 22, identifier:conn; 23, block; 23, 24; 24, try_statement; 24, 25; 24, 80; 24, 94; 25, block; 25, 26; 25, 34; 25, 49; 25, 64; 25, 71; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:query; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_base; 33, argument_list; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:order_by; 37, None; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:query; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:_order_by; 46, argument_list; 46, 47; 46, 48; 47, identifier:query; 48, identifier:order_by; 49, if_statement; 49, 50; 49, 53; 50, comparison_operator:>; 50, 51; 50, 52; 51, identifier:limit; 52, integer:0; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:query; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:_limit; 61, argument_list; 61, 62; 61, 63; 62, identifier:query; 63, identifier:limit; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:log; 68, identifier:debug; 69, argument_list; 69, 70; 70, identifier:query; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:rv; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:query; 77, identifier:run; 78, argument_list; 78, 79; 79, identifier:conn; 80, except_clause; 80, 81; 80, 85; 81, as_pattern; 81, 82; 81, 83; 82, identifier:Exception; 83, as_pattern_target; 83, 84; 84, identifier:e; 85, block; 85, 86; 85, 93; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:log; 90, identifier:warn; 91, argument_list; 91, 92; 92, identifier:e; 93, raise_statement; 94, else_clause; 94, 95; 95, block; 95, 96; 95, 109; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:data; 99, list_comprehension; 99, 100; 99, 106; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:_model; 104, argument_list; 104, 105; 105, identifier:_; 106, for_in_clause; 106, 107; 106, 108; 107, identifier:_; 108, identifier:rv; 109, return_statement; 109, 110; 110, identifier:data
def all(self, order_by=None, limit=0): """ Fetch all items. :param limit: How many rows to fetch. :param order_by: column on which to order the results. \ To change the sort, prepend with < or >. """ with rconnect() as conn: try: query = self._base() if order_by is not None: query = self._order_by(query, order_by) if limit > 0: query = self._limit(query, limit) log.debug(query) rv = query.run(conn) except Exception as e: log.warn(e) raise else: data = [self._model(_) for _ in rv] return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:safe_dump_js; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:js; 5, identifier:abspath; 6, default_parameter; 6, 7; 6, 8; 7, identifier:fastmode; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:compress; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:enable_verbose; 14, True; 15, block; 15, 16; 15, 18; 15, 25; 15, 26; 15, 32; 15, 50; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:abspath; 21, call; 21, 22; 21, 23; 22, identifier:str; 23, argument_list; 23, 24; 24, identifier:abspath; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:temp_abspath; 29, binary_operator:%; 29, 30; 29, 31; 30, string:"%s.tmp"; 31, identifier:abspath; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:dump_js; 35, argument_list; 35, 36; 35, 37; 35, 38; 35, 41; 35, 44; 35, 47; 36, identifier:js; 37, identifier:temp_abspath; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:fastmode; 40, identifier:fastmode; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:replace; 43, True; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:compress; 46, identifier:compress; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:enable_verbose; 49, identifier:enable_verbose; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:shutil; 54, identifier:move; 55, argument_list; 55, 56; 55, 57; 56, identifier:temp_abspath; 57, identifier:abspath
def safe_dump_js(js, abspath, fastmode=False, compress=False, enable_verbose=True): """A stable version of dump_js, silently overwrite existing file. When your program been interrupted, you lose nothing. Typically if your program is interrupted by any reason, it only leaves a incomplete file. If you use replace=True, then you also lose your old file. So a bettr way is to: 1. dump json to a temp file. 2. when it's done, rename it to #abspath, overwrite the old one. This way guarantee atomic write. :param js: Serializable python object. :type js: dict or list :param abspath: ``save as`` path, file extension has to be ``.json`` or ``.gz`` (for compressed json). :type abspath: string :param fastmode: (default False) If ``True``, then dumping json without sorted keys and pretty indent, and it's faster and smaller in size. :type fastmode: boolean :param compress: (default False) If ``True``, use GNU program gzip to compress the json file. Disk usage can be greatly reduced. But you have to use :func:`load_js(abspath, compress=True)<load_js>` in loading. :type compress: boolean :param enable_verbose: (default True) Trigger for message. :type enable_verbose: boolean Usage:: >>> from weatherlab.lib.dataIO.js import dump_js >>> js = {"a": 1, "b": 2} >>> safe_dump_js(js, "test.json") Dumping to test.json... Complete! Elapse 0.002432 sec **中文文档** 在对文件进行写入时, 如果程序中断, 则会留下一个不完整的文件。如果你使用了覆盖式 写入, 则你同时也丢失了原文件。所以为了保证写操作的原子性(要么全部完成, 要么全部 都不完成), 更好的方法是: 首先将文件写入一个临时文件中, 完成后再讲文件重命名, 覆盖旧文件。这样即使中途程序被中断, 也仅仅是留下了一个未完成的临时文件而已, 不会 影响原文件。 参数列表 :param js: 可Json化的Python对象 :type js: ``字典`` 或 ``列表`` :param abspath: 写入文件的路径。扩展名必须为 ``.json`` 或 ``.gz``, 其中gz用于被压 缩的Json :type abspath: ``字符串`` :param replace: (默认 False) 当为``True``时, 如果写入路径已经存在, 则会自动覆盖 原文件。而为``False``时, 则会抛出异常。防止误操作覆盖源文件。 :type replace: ``布尔值`` :param compress: (默认 False) 当为``True``时, 使用开源压缩标准gzip压缩Json文件。 通常能让文件大小缩小10-20倍不等。如要读取文件, 则需要使用函数 :func:`load_js(abspath, compress=True)<load_js>`. :type compress: ``布尔值`` :param enable_verbose: (默认 True) 是否打开信息提示开关, 批处理时建议关闭. :type enable_verbose: ``布尔值`` """ abspath = str(abspath) # try stringlize temp_abspath = "%s.tmp" % abspath dump_js(js, temp_abspath, fastmode=fastmode, replace=True, compress=compress, enable_verbose=enable_verbose) shutil.move(temp_abspath, abspath)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:all_selectors; 3, parameters; 3, 4; 3, 5; 4, identifier:Class; 5, identifier:fn; 6, block; 6, 7; 6, 9; 6, 13; 6, 24; 6, 33; 6, 66; 6, 79; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:selectors; 12, list:[]; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:cssparser; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:cssutils; 19, identifier:CSSParser; 20, argument_list; 20, 21; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:validate; 23, False; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:css; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:cssparser; 30, identifier:parseFile; 31, argument_list; 31, 32; 32, identifier:fn; 33, for_statement; 33, 34; 33, 35; 33, 53; 34, identifier:rule; 35, list_comprehension; 35, 36; 35, 37; 35, 42; 36, identifier:r; 37, for_in_clause; 37, 38; 37, 39; 38, identifier:r; 39, attribute; 39, 40; 39, 41; 40, identifier:css; 41, identifier:cssRules; 42, if_clause; 42, 43; 43, comparison_operator:==; 43, 44; 43, 48; 44, call; 44, 45; 44, 46; 45, identifier:type; 46, argument_list; 46, 47; 47, identifier:r; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:cssutils; 51, identifier:css; 52, identifier:CSSStyleRule; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, augmented_assignment:+=; 55, 56; 55, 57; 56, identifier:selectors; 57, list_comprehension; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:sel; 60, identifier:selectorText; 61, for_in_clause; 61, 62; 61, 63; 62, identifier:sel; 63, attribute; 63, 64; 63, 65; 64, identifier:rule; 65, identifier:selectorList; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:selectors; 69, call; 69, 70; 69, 71; 70, identifier:sorted; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:list; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:set; 77, argument_list; 77, 78; 78, identifier:selectors; 79, return_statement; 79, 80; 80, identifier:selectors
def all_selectors(Class, fn): """return a sorted list of selectors that occur in the stylesheet""" selectors = [] cssparser = cssutils.CSSParser(validate=False) css = cssparser.parseFile(fn) for rule in [r for r in css.cssRules if type(r)==cssutils.css.CSSStyleRule]: selectors += [sel.selectorText for sel in rule.selectorList] selectors = sorted(list(set(selectors))) return selectors
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:create_logger; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:args; 7, dictionary; 8, block; 8, 9; 8, 11; 8, 12; 8, 21; 8, 29; 8, 49; 8, 59; 8, 68; 8, 69; 8, 82; 8, 89; 8, 96; 8, 97; 8, 143; 8, 161; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:logger; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:logging; 18, identifier:getLogger; 19, argument_list; 19, 20; 20, string:"SmartFileSorter"; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:logger; 25, identifier:level; 26, attribute; 26, 27; 26, 28; 27, identifier:logging; 28, identifier:INFO; 29, if_statement; 29, 30; 29, 39; 30, boolean_operator:and; 30, 31; 30, 34; 31, comparison_operator:in; 31, 32; 31, 33; 32, string:'--debug'; 33, identifier:args; 34, comparison_operator:is; 34, 35; 34, 38; 35, subscript; 35, 36; 35, 37; 36, identifier:args; 37, string:'--debug'; 38, True; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:logger; 44, identifier:setLevel; 45, argument_list; 45, 46; 46, attribute; 46, 47; 46, 48; 47, identifier:logging; 48, identifier:DEBUG; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:file_log_formatter; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:logging; 55, identifier:Formatter; 56, argument_list; 56, 57; 56, 58; 57, string:'%(asctime)s %(name)s %(levelname)s %(message)s'; 58, string:'%Y-%m-%d %H:%M:%S'; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:console_log_formatter; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:logging; 65, identifier:Formatter; 66, argument_list; 66, 67; 67, string:'%(message)s'; 68, comment; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:stdout_stream; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:logging; 75, identifier:StreamHandler; 76, argument_list; 76, 77; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:stream; 79, attribute; 79, 80; 79, 81; 80, identifier:sys; 81, identifier:stdout; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:stdout_stream; 86, identifier:setFormatter; 87, argument_list; 87, 88; 88, identifier:console_log_formatter; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:logger; 93, identifier:addHandler; 94, argument_list; 94, 95; 95, identifier:stdout_stream; 96, comment; 97, if_statement; 97, 98; 97, 107; 98, boolean_operator:and; 98, 99; 98, 102; 99, comparison_operator:in; 99, 100; 99, 101; 100, string:'--log'; 101, identifier:args; 102, comparison_operator:is; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:args; 105, string:'--log'; 106, None; 107, block; 107, 108; 107, 118; 107, 129; 107, 136; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:logfile; 111, call; 111, 112; 111, 113; 112, identifier:open; 113, argument_list; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:args; 116, string:'--log'; 117, string:'w'; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:logfile_stream; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:logging; 124, identifier:StreamHandler; 125, argument_list; 125, 126; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:stream; 128, identifier:logfile; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:logfile_stream; 133, identifier:setFormatter; 134, argument_list; 134, 135; 135, identifier:file_log_formatter; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:logger; 140, identifier:addHandler; 141, argument_list; 141, 142; 142, identifier:logfile_stream; 143, if_statement; 143, 144; 143, 153; 144, boolean_operator:and; 144, 145; 144, 148; 145, comparison_operator:in; 145, 146; 145, 147; 146, string:'--dry-run'; 147, identifier:args; 148, comparison_operator:is; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:args; 151, string:'--dry-run'; 152, True; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:logger; 158, identifier:info; 159, argument_list; 159, 160; 160, string:'Running with --dry-run parameter. Actions will not be performed.'; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:logger; 166, identifier:logger
def create_logger(self, args={}): """ Create and configure the program's logger object. Log levels: DEBUG - Log everything. Hidden unless --debug is used. INFO - information only ERROR - Critical error :param args: Object containing program's parsed command line arguments :return: None """ # Set up logging logger = logging.getLogger("SmartFileSorter") logger.level = logging.INFO if '--debug' in args and args['--debug'] is True: logger.setLevel(logging.DEBUG) file_log_formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s', '%Y-%m-%d %H:%M:%S') console_log_formatter = logging.Formatter('%(message)s') # Log to stdout stdout_stream = logging.StreamHandler(stream=sys.stdout) stdout_stream.setFormatter(console_log_formatter) logger.addHandler(stdout_stream) # Log to file if the option is chosen if '--log' in args and args['--log'] is not None: logfile = open(args['--log'], 'w') logfile_stream = logging.StreamHandler(stream=logfile) logfile_stream.setFormatter(file_log_formatter) logger.addHandler(logfile_stream) if '--dry-run' in args and args['--dry-run'] is True: logger.info('Running with --dry-run parameter. Actions will not be performed.') self.logger = logger
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:slug; 6, block; 6, 7; 6, 9; 6, 18; 6, 19; 6, 24; 6, 32; 6, 45; 6, 58; 6, 71; 6, 84; 6, 97; 6, 110; 6, 118; 6, 126; 6, 170; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:kb; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:api; 15, identifier:get_kb_by_slug; 16, argument_list; 16, 17; 17, identifier:slug; 18, comment; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:check_knowledge_access; 22, argument_list; 22, 23; 23, identifier:kb; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:parser; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:reqparse; 30, identifier:RequestParser; 31, argument_list; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:parser; 36, identifier:add_argument; 37, argument_list; 37, 38; 37, 39; 37, 42; 38, string:'from'; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:type; 41, identifier:str; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:help; 44, string:"Return only entries where key matches this."; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:parser; 49, identifier:add_argument; 50, argument_list; 50, 51; 50, 52; 50, 55; 51, string:'to'; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:type; 54, identifier:str; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:help; 57, string:"Return only entries where value matches this."; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:parser; 62, identifier:add_argument; 63, argument_list; 63, 64; 63, 65; 63, 68; 64, string:'page'; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:type; 67, identifier:int; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:help; 70, string:"Require a specific page"; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:parser; 75, identifier:add_argument; 76, argument_list; 76, 77; 76, 78; 76, 81; 77, string:'per_page'; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:type; 80, identifier:int; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:help; 83, string:"Set how much result per page"; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:parser; 88, identifier:add_argument; 89, argument_list; 89, 90; 89, 91; 89, 94; 90, string:'match_type'; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:type; 93, identifier:str; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:help; 96, string:"s=substring, e=exact, sw=startswith"; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:parser; 101, identifier:add_argument; 102, argument_list; 102, 103; 102, 104; 102, 107; 103, string:'sortby'; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:type; 106, identifier:str; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:help; 109, string:"the sorting criteria ('from' or 'to')"; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:args; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:parser; 116, identifier:parse_args; 117, argument_list; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:kb_dict; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:kb; 124, identifier:to_dict; 125, argument_list; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:kb_dict; 130, string:'mappings'; 131, call; 131, 132; 131, 136; 132, attribute; 132, 133; 132, 134; 132, 135; 133, identifier:KnwKBMappingsResource; 134, line_continuation:\; 135, identifier:search_mappings; 136, argument_list; 136, 137; 136, 140; 136, 145; 136, 150; 136, 155; 136, 160; 136, 165; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:kb; 139, identifier:kb; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:key; 142, subscript; 142, 143; 142, 144; 143, identifier:args; 144, string:'from'; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:value; 147, subscript; 147, 148; 147, 149; 148, identifier:args; 149, string:'to'; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:match_type; 152, subscript; 152, 153; 152, 154; 153, identifier:args; 154, string:'match_type'; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:sortby; 157, subscript; 157, 158; 157, 159; 158, identifier:args; 159, string:'sortby'; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:page; 162, subscript; 162, 163; 162, 164; 163, identifier:args; 164, string:'page'; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:per_page; 167, subscript; 167, 168; 167, 169; 168, identifier:args; 169, string:'per_page'; 170, return_statement; 170, 171; 171, identifier:kb_dict
def get(self, slug): """Get KnwKB. Url parameters: - from: filter "mappings from" - to: filter "mappings to" - page - per_page - match_type: s=substring, e=exact, sw=startswith - sortby: 'from' or 'to' """ kb = api.get_kb_by_slug(slug) # check if is accessible from api check_knowledge_access(kb) parser = reqparse.RequestParser() parser.add_argument( 'from', type=str, help="Return only entries where key matches this.") parser.add_argument( 'to', type=str, help="Return only entries where value matches this.") parser.add_argument('page', type=int, help="Require a specific page") parser.add_argument('per_page', type=int, help="Set how much result per page") parser.add_argument('match_type', type=str, help="s=substring, e=exact, sw=startswith") parser.add_argument('sortby', type=str, help="the sorting criteria ('from' or 'to')") args = parser.parse_args() kb_dict = kb.to_dict() kb_dict['mappings'] = KnwKBMappingsResource \ .search_mappings(kb=kb, key=args['from'], value=args['to'], match_type=args['match_type'], sortby=args['sortby'], page=args['page'], per_page=args['per_page']) return kb_dict