nodes
stringlengths
501
22.4k
edges
stringlengths
138
5.07k
code
stringlengths
108
19.3k
0, module; 1, function_definition; 2, function_name:filter_sorted_apps; 3, parameters; 4, block; 5, identifier:admin_apps; 6, identifier:request; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, expression_statement; 11, for_statement; 12, expression_statement; 13, return_statement; 14, comment:""" Filter admin_apps to show the ones in ``DASHBOARD_SORTED_APPS`` first, and remove them from the subsequent listings. """; 15, assignment; 16, identifier:orig_app_spec; 17, attribute; 18, comment:# make a copy that we can write to, to fix deprecations without; 19, comment:# changing settings; 20, block; 21, assignment; 22, identifier:app; 23, identifier:sorted_apps; 24, block; 25, augmented_assignment; 26, identifier:sorted_apps; 27, identifier:sorted_apps; 28, list; 29, identifier:appsettings; 30, identifier:DASHBOARD_SORTED_APPS; 31, expression_statement; 32, comment:# lookup the models from the names; 33, expression_statement; 34, comment:# Only add the panel if at least one model is listed.; 35, if_statement; 36, identifier:used_models; 37, list; 38, expression_statement; 39, identifier:sorted_apps; 40, call; 41, assignment; 42, assignment; 43, subscript; 44, block; 45, augmented_assignment; 46, identifier:_remove_app_models; 47, argument_list; 48, identifier:app_spec; 49, call; 50, subscript; 51, call; 52, identifier:app_spec; 53, string; 54, expression_statement; 55, identifier:used_models; 56, subscript; 57, identifier:admin_apps; 58, identifier:used_models; 59, attribute; 60, argument_list; 61, identifier:app_spec; 62, string; 63, identifier:_build_app_models; 64, argument_list; 65, string_content:models; 66, call; 67, identifier:app; 68, string; 69, identifier:orig_app_spec; 70, identifier:copy; 71, string_content:models; 72, identifier:request; 73, identifier:admin_apps; 74, subscript; 75, keyword_argument; 76, attribute; 77, argument_list; 78, string_content:models; 79, identifier:app_spec; 80, string; 81, identifier:ensure_all_models; 82, True; 83, identifier:sorted_apps; 84, identifier:append; 85, identifier:app_spec; 86, string_content:models
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 7, 14; 8, 15; 9, 16; 9, 17; 9, 18; 9, 19; 9, 20; 10, 21; 11, 22; 11, 23; 11, 24; 12, 25; 13, 26; 15, 27; 15, 28; 17, 29; 17, 30; 20, 31; 20, 32; 20, 33; 20, 34; 20, 35; 21, 36; 21, 37; 24, 38; 25, 39; 25, 40; 31, 41; 33, 42; 35, 43; 35, 44; 38, 45; 40, 46; 40, 47; 41, 48; 41, 49; 42, 50; 42, 51; 43, 52; 43, 53; 44, 54; 45, 55; 45, 56; 47, 57; 47, 58; 49, 59; 49, 60; 50, 61; 50, 62; 51, 63; 51, 64; 53, 65; 54, 66; 56, 67; 56, 68; 59, 69; 59, 70; 62, 71; 64, 72; 64, 73; 64, 74; 64, 75; 66, 76; 66, 77; 68, 78; 74, 79; 74, 80; 75, 81; 75, 82; 76, 83; 76, 84; 77, 85; 80, 86
def filter_sorted_apps(admin_apps, request): """ Filter admin_apps to show the ones in ``DASHBOARD_SORTED_APPS`` first, and remove them from the subsequent listings. """ sorted_apps = [] for orig_app_spec in appsettings.DASHBOARD_SORTED_APPS: # make a copy that we can write to, to fix deprecations without # changing settings app_spec = orig_app_spec.copy() # lookup the models from the names app_spec['models'] = _build_app_models( request, admin_apps, app_spec['models'], ensure_all_models=True ) # Only add the panel if at least one model is listed. if app_spec['models']: sorted_apps.append(app_spec) used_models = [] for app in sorted_apps: used_models += app['models'] sorted_apps += _remove_app_models(admin_apps, used_models) return sorted_apps
0, module; 1, function_definition; 2, function_name:render_stats; 3, parameters; 4, block; 5, identifier:stats; 6, identifier:sort; 7, identifier:format; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, expression_statement; 13, return_statement; 14, comment:""" Returns a StringIO containing the formatted statistics from _statsfile_. _sort_ is a list of fields to sort by. _format_ is the name of the method that pstats uses to format the data. """; 15, assignment; 16, call; 17, block; 18, call; 19, call; 20, attribute; 21, identifier:output; 22, call; 23, identifier:hasattr; 24, argument_list; 25, expression_statement; 26, attribute; 27, argument_list; 28, call; 29, argument_list; 30, identifier:output; 31, identifier:stream; 32, identifier:StdoutWrapper; 33, argument_list; 34, identifier:stats; 35, string:"stream"; 36, assignment; 37, identifier:stats; 38, identifier:sort_stats; 39, list_splat; 40, identifier:getattr; 41, argument_list; 42, attribute; 43, attribute; 44, identifier:sort; 45, identifier:stats; 46, identifier:format; 47, identifier:stats; 48, identifier:stream; 49, identifier:output; 50, identifier:stream
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 8, 14; 9, 15; 10, 16; 10, 17; 11, 18; 12, 19; 13, 20; 15, 21; 15, 22; 16, 23; 16, 24; 17, 25; 18, 26; 18, 27; 19, 28; 19, 29; 20, 30; 20, 31; 22, 32; 22, 33; 24, 34; 24, 35; 25, 36; 26, 37; 26, 38; 27, 39; 28, 40; 28, 41; 36, 42; 36, 43; 39, 44; 41, 45; 41, 46; 42, 47; 42, 48; 43, 49; 43, 50
def render_stats(stats, sort, format): """ Returns a StringIO containing the formatted statistics from _statsfile_. _sort_ is a list of fields to sort by. _format_ is the name of the method that pstats uses to format the data. """ output = StdoutWrapper() if hasattr(stats, "stream"): stats.stream = output.stream stats.sort_stats(*sort) getattr(stats, format)() return output.stream
0, module; 1, function_definition; 2, function_name:render_queries; 3, parameters; 4, block; 5, identifier:queries; 6, identifier:sort; 7, expression_statement; 8, expression_statement; 9, if_statement; 10, if_statement; 11, print_statement; 12, expression_statement; 13, for_statement; 14, expression_statement; 15, for_statement; 16, return_statement; 17, comment:""" Returns a StringIO containing the formatted SQL queries. _sort_ is a field to sort by. """; 18, assignment; 19, comparison_operator:sort == 'order'; 20, block; 21, comparison_operator:sort == 'time'; 22, block; 23, elif_clause; 24, else_clause; 25, chevron; 26, string:" queries time query"; 27, assignment; 28, identifier:query; 29, identifier:queries; 30, block; 31, assignment; 32, identifier:result; 33, identifier:results; 34, block; 35, identifier:output; 36, identifier:output; 37, call; 38, identifier:sort; 39, string; 40, print_statement; 41, for_statement; 42, return_statement; 43, identifier:sort; 44, string; 45, function_definition; 46, comparison_operator:sort == 'queries'; 47, block; 48, block; 49, identifier:output; 50, identifier:results; 51, dictionary; 52, try_statement; 53, identifier:results; 54, call; 55, print_statement; 56, identifier:StringIO; 57, argument_list; 58, string_content:order; 59, chevron; 60, string:" time query"; 61, identifier:query; 62, identifier:queries; 63, block; 64, identifier:output; 65, string_content:time; 66, function_name:sorter; 67, parameters; 68, block; 69, identifier:sort; 70, string; 71, function_definition; 72, raise_statement; 73, block; 74, except_clause; 75, identifier:sorted; 76, argument_list; 77, chevron; 78, binary_operator:" %8d %8.3f %s" % ( result[1][0], result[1][1], result[0] ); 79, identifier:output; 80, print_statement; 81, identifier:x; 82, identifier:y; 83, return_statement; 84, string_content:queries; 85, function_name:sorter; 86, parameters; 87, block; 88, call; 89, expression_statement; 90, expression_statement; 91, expression_statement; 92, identifier:KeyError; 93, block; 94, call; 95, keyword_argument; 96, keyword_argument; 97, identifier:output; 98, string:" %8d %8.3f %s"; 99, tuple; 100, chevron; 101, binary_operator:" %8s %s" % (query["time"], query["sql"]); 102, call; 103, identifier:x; 104, identifier:y; 105, return_statement; 106, identifier:RuntimeError; 107, argument_list; 108, assignment; 109, augmented_assignment; 110, augmented_assignment; 111, expression_statement; 112, attribute; 113, argument_list; 114, identifier:cmp; 115, identifier:sorter; 116, identifier:reverse; 117, True; 118, subscript; 119, subscript; 120, subscript; 121, identifier:output; 122, string:" %8s %s"; 123, tuple; 124, identifier:cmp; 125, argument_list; 126, call; 127, binary_operator:"Unknown sort: %s" % sort; 128, identifier:result; 129, subscript; 130, subscript; 131, integer:1; 132, subscript; 133, call; 134, assignment; 135, identifier:results; 136, identifier:iteritems; 137, subscript; 138, integer:0; 139, subscript; 140, integer:1; 141, identifier:result; 142, integer:0; 143, subscript; 144, subscript; 145, subscript; 146, subscript; 147, identifier:cmp; 148, argument_list; 149, string:"Unknown sort: %s"; 150, identifier:sort; 151, identifier:results; 152, subscript; 153, identifier:result; 154, integer:0; 155, identifier:result; 156, integer:1; 157, identifier:Decimal; 158, argument_list; 159, subscript; 160, list; 161, identifier:result; 162, integer:1; 163, identifier:result; 164, integer:1; 165, identifier:query; 166, string:"time"; 167, identifier:query; 168, string:"sql"; 169, subscript; 170, integer:1; 171, subscript; 172, integer:1; 173, subscript; 174, subscript; 175, identifier:query; 176, string:"sql"; 177, subscript; 178, identifier:results; 179, subscript; 180, integer:1; 181, call; 182, identifier:x; 183, integer:1; 184, identifier:y; 185, integer:1; 186, subscript; 187, integer:0; 188, subscript; 189, integer:0; 190, identifier:query; 191, string:"time"; 192, identifier:query; 193, string:"sql"; 194, identifier:Decimal; 195, argument_list; 196, identifier:x; 197, integer:1; 198, identifier:y; 199, integer:1; 200, subscript; 201, identifier:query; 202, string:"time"
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 7, 17; 8, 18; 9, 19; 9, 20; 10, 21; 10, 22; 10, 23; 10, 24; 11, 25; 11, 26; 12, 27; 13, 28; 13, 29; 13, 30; 14, 31; 15, 32; 15, 33; 15, 34; 16, 35; 18, 36; 18, 37; 19, 38; 19, 39; 20, 40; 20, 41; 20, 42; 21, 43; 21, 44; 22, 45; 23, 46; 23, 47; 24, 48; 25, 49; 27, 50; 27, 51; 30, 52; 31, 53; 31, 54; 34, 55; 37, 56; 37, 57; 39, 58; 40, 59; 40, 60; 41, 61; 41, 62; 41, 63; 42, 64; 44, 65; 45, 66; 45, 67; 45, 68; 46, 69; 46, 70; 47, 71; 48, 72; 52, 73; 52, 74; 54, 75; 54, 76; 55, 77; 55, 78; 59, 79; 63, 80; 67, 81; 67, 82; 68, 83; 70, 84; 71, 85; 71, 86; 71, 87; 72, 88; 73, 89; 73, 90; 73, 91; 74, 92; 74, 93; 76, 94; 76, 95; 76, 96; 77, 97; 78, 98; 78, 99; 80, 100; 80, 101; 83, 102; 86, 103; 86, 104; 87, 105; 88, 106; 88, 107; 89, 108; 90, 109; 91, 110; 93, 111; 94, 112; 94, 113; 95, 114; 95, 115; 96, 116; 96, 117; 99, 118; 99, 119; 99, 120; 100, 121; 101, 122; 101, 123; 102, 124; 102, 125; 105, 126; 107, 127; 108, 128; 108, 129; 109, 130; 109, 131; 110, 132; 110, 133; 111, 134; 112, 135; 112, 136; 118, 137; 118, 138; 119, 139; 119, 140; 120, 141; 120, 142; 123, 143; 123, 144; 125, 145; 125, 146; 126, 147; 126, 148; 127, 149; 127, 150; 129, 151; 129, 152; 130, 153; 130, 154; 132, 155; 132, 156; 133, 157; 133, 158; 134, 159; 134, 160; 137, 161; 137, 162; 139, 163; 139, 164; 143, 165; 143, 166; 144, 167; 144, 168; 145, 169; 145, 170; 146, 171; 146, 172; 148, 173; 148, 174; 152, 175; 152, 176; 158, 177; 159, 178; 159, 179; 160, 180; 160, 181; 169, 182; 169, 183; 171, 184; 171, 185; 173, 186; 173, 187; 174, 188; 174, 189; 177, 190; 177, 191; 179, 192; 179, 193; 181, 194; 181, 195; 186, 196; 186, 197; 188, 198; 188, 199; 195, 200; 200, 201; 200, 202
def render_queries(queries, sort): """ Returns a StringIO containing the formatted SQL queries. _sort_ is a field to sort by. """ output = StringIO() if sort == 'order': print >>output, " time query" for query in queries: print >>output, " %8s %s" % (query["time"], query["sql"]) return output if sort == 'time': def sorter(x, y): return cmp(x[1][1], y[1][1]) elif sort == 'queries': def sorter(x, y): return cmp(x[1][0], y[1][0]) else: raise RuntimeError("Unknown sort: %s" % sort) print >>output, " queries time query" results = {} for query in queries: try: result = results[query["sql"]] result[0] += 1 result[1] += Decimal(query["time"]) except KeyError: results[query["sql"]] = [1, Decimal(query["time"])] results = sorted(results.iteritems(), cmp=sorter, reverse=True) for result in results: print >>output, " %8d %8.3f %s" % ( result[1][0], result[1][1], result[0] ) return output
0, module; 1, function_definition; 2, function_name:process_request; 3, parameters; 4, block; 5, identifier:self; 6, identifier:request; 7, expression_statement; 8, function_definition; 9, if_statement; 10, if_statement; 11, comment:""" Setup the profiler for a profiling run and clear the SQL query log. If this is a resort of an existing profiling run, just return the resorted list. """; 12, function_name:unpickle; 13, parameters; 14, block; 15, boolean_operator; 16, block; 17, parenthesized_expression; 18, block; 19, identifier:params; 20, expression_statement; 21, expression_statement; 22, return_statement; 23, comparison_operator:request.method != 'GET'; 24, line_continuation:\; 25, not_operator; 26, return_statement; 27, boolean_operator; 28, expression_statement; 29, expression_statement; 30, if_statement; 31, assignment; 32, assignment; 33, expression_list; 34, attribute; 35, string; 36, parenthesized_expression; 37, call; 38, parenthesized_expression; 39, assignment; 40, assignment; 41, parenthesized_expression; 42, comment:# Instantly re-sort the existing stats data; 43, block; 44, elif_clause; 45, else_clause; 46, identifier:stats; 47, call; 48, identifier:queries; 49, call; 50, identifier:stats; 51, identifier:queries; 52, identifier:request; 53, identifier:method; 54, string_content:GET; 55, comparison_operator:request.META.get( 'HTTP_CONTENT_TYPE', request.META.get('CONTENT_TYPE', '') ) in ['multipart/form-data', 'application/x-www-form-urlencoded']; 56, attribute; 57, argument_list; 58, boolean_operator; 59, attribute; 60, call; 61, identifier:params; 62, attribute; 63, boolean_operator; 64, expression_statement; 65, return_statement; 66, parenthesized_expression; 67, block; 68, comment:# We don't have previous data, so initialize the profiler; 69, block; 70, identifier:unpickle_stats; 71, argument_list; 72, attribute; 73, argument_list; 74, call; 75, list; 76, attribute; 77, identifier:get; 78, string; 79, False; 80, comparison_operator:settings.DEBUG == True; 81, attribute; 82, identifier:request; 83, identifier:statsfile; 84, attribute; 85, argument_list; 86, identifier:request; 87, identifier:REQUEST; 88, call; 89, comparison_operator:params.get('show_queries', '1') == '1'; 90, assignment; 91, call; 92, boolean_operator; 93, expression_statement; 94, return_statement; 95, expression_statement; 96, expression_statement; 97, call; 98, identifier:cPickle; 99, identifier:loads; 100, call; 101, attribute; 102, argument_list; 103, string; 104, string; 105, identifier:request; 106, identifier:REQUEST; 107, string_content:profile; 108, attribute; 109, True; 110, attribute; 111, identifier:is_staff; 112, identifier:tempfile; 113, identifier:NamedTemporaryFile; 114, attribute; 115, argument_list; 116, call; 117, string; 118, pattern_list; 119, call; 120, identifier:display_stats; 121, argument_list; 122, call; 123, comparison_operator:params.get('show_stats', '1') == '1'; 124, assignment; 125, call; 126, assignment; 127, call; 128, identifier:b64decode; 129, argument_list; 130, identifier:b64decode; 131, argument_list; 132, attribute; 133, identifier:get; 134, string; 135, call; 136, string_content:multipart/form-data; 137, string_content:application/x-www-form-urlencoded; 138, identifier:settings; 139, identifier:DEBUG; 140, identifier:request; 141, identifier:user; 142, identifier:params; 143, identifier:get; 144, string; 145, False; 146, attribute; 147, argument_list; 148, string_content:1; 149, identifier:stats; 150, identifier:queries; 151, identifier:unpickle; 152, argument_list; 153, identifier:request; 154, identifier:stats; 155, identifier:queries; 156, attribute; 157, argument_list; 158, call; 159, string; 160, pattern_list; 161, call; 162, identifier:display_queries; 163, argument_list; 164, attribute; 165, call; 166, identifier:reset_queries; 167, argument_list; 168, call; 169, call; 170, identifier:request; 171, identifier:META; 172, string_content:HTTP_CONTENT_TYPE; 173, attribute; 174, argument_list; 175, string_content:show_stats; 176, identifier:params; 177, identifier:get; 178, string; 179, string; 180, identifier:params; 181, identifier:params; 182, identifier:get; 183, string; 184, False; 185, attribute; 186, argument_list; 187, string_content:1; 188, identifier:stats; 189, identifier:queries; 190, identifier:unpickle; 191, argument_list; 192, identifier:request; 193, identifier:stats; 194, identifier:queries; 195, identifier:request; 196, identifier:profiler; 197, attribute; 198, argument_list; 199, attribute; 200, argument_list; 201, attribute; 202, argument_list; 203, attribute; 204, identifier:get; 205, string; 206, string; 207, string_content:show_queries; 208, string_content:1; 209, string_content:show_queries; 210, identifier:params; 211, identifier:get; 212, string; 213, string; 214, identifier:params; 215, identifier:hotshot; 216, identifier:Profile; 217, attribute; 218, identifier:params; 219, identifier:get; 220, string; 221, string; 222, identifier:params; 223, identifier:get; 224, string; 225, string; 226, identifier:request; 227, identifier:META; 228, string_content:CONTENT_TYPE; 229, string_content:show_stats; 230, string_content:1; 231, attribute; 232, identifier:name; 233, string_content:stats; 234, string_content:queries; 235, identifier:request; 236, identifier:statsfile
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 7, 11; 8, 12; 8, 13; 8, 14; 9, 15; 9, 16; 10, 17; 10, 18; 13, 19; 14, 20; 14, 21; 14, 22; 15, 23; 15, 24; 15, 25; 16, 26; 17, 27; 18, 28; 18, 29; 18, 30; 20, 31; 21, 32; 22, 33; 23, 34; 23, 35; 25, 36; 27, 37; 27, 38; 28, 39; 29, 40; 30, 41; 30, 42; 30, 43; 30, 44; 30, 45; 31, 46; 31, 47; 32, 48; 32, 49; 33, 50; 33, 51; 34, 52; 34, 53; 35, 54; 36, 55; 37, 56; 37, 57; 38, 58; 39, 59; 39, 60; 40, 61; 40, 62; 41, 63; 43, 64; 43, 65; 44, 66; 44, 67; 45, 68; 45, 69; 47, 70; 47, 71; 49, 72; 49, 73; 55, 74; 55, 75; 56, 76; 56, 77; 57, 78; 57, 79; 58, 80; 58, 81; 59, 82; 59, 83; 60, 84; 60, 85; 62, 86; 62, 87; 63, 88; 63, 89; 64, 90; 65, 91; 66, 92; 67, 93; 67, 94; 69, 95; 69, 96; 71, 97; 72, 98; 72, 99; 73, 100; 74, 101; 74, 102; 75, 103; 75, 104; 76, 105; 76, 106; 78, 107; 80, 108; 80, 109; 81, 110; 81, 111; 84, 112; 84, 113; 88, 114; 88, 115; 89, 116; 89, 117; 90, 118; 90, 119; 91, 120; 91, 121; 92, 122; 92, 123; 93, 124; 94, 125; 95, 126; 96, 127; 97, 128; 97, 129; 100, 130; 100, 131; 101, 132; 101, 133; 102, 134; 102, 135; 103, 136; 104, 137; 108, 138; 108, 139; 110, 140; 110, 141; 114, 142; 114, 143; 115, 144; 115, 145; 116, 146; 116, 147; 117, 148; 118, 149; 118, 150; 119, 151; 119, 152; 121, 153; 121, 154; 121, 155; 122, 156; 122, 157; 123, 158; 123, 159; 124, 160; 124, 161; 125, 162; 125, 163; 126, 164; 126, 165; 127, 166; 127, 167; 129, 168; 131, 169; 132, 170; 132, 171; 134, 172; 135, 173; 135, 174; 144, 175; 146, 176; 146, 177; 147, 178; 147, 179; 152, 180; 156, 181; 156, 182; 157, 183; 157, 184; 158, 185; 158, 186; 159, 187; 160, 188; 160, 189; 161, 190; 161, 191; 163, 192; 163, 193; 163, 194; 164, 195; 164, 196; 165, 197; 165, 198; 168, 199; 168, 200; 169, 201; 169, 202; 173, 203; 173, 204; 174, 205; 174, 206; 178, 207; 179, 208; 183, 209; 185, 210; 185, 211; 186, 212; 186, 213; 191, 214; 197, 215; 197, 216; 198, 217; 199, 218; 199, 219; 200, 220; 200, 221; 201, 222; 201, 223; 202, 224; 202, 225; 203, 226; 203, 227; 205, 228; 212, 229; 213, 230; 217, 231; 217, 232; 220, 233; 224, 234; 231, 235; 231, 236
def process_request(self, request): """ Setup the profiler for a profiling run and clear the SQL query log. If this is a resort of an existing profiling run, just return the resorted list. """ def unpickle(params): stats = unpickle_stats(b64decode(params.get('stats', ''))) queries = cPickle.loads(b64decode(params.get('queries', ''))) return stats, queries if request.method != 'GET' and \ not (request.META.get( 'HTTP_CONTENT_TYPE', request.META.get('CONTENT_TYPE', '') ) in ['multipart/form-data', 'application/x-www-form-urlencoded']): return if (request.REQUEST.get('profile', False) and (settings.DEBUG == True or request.user.is_staff)): request.statsfile = tempfile.NamedTemporaryFile() params = request.REQUEST if (params.get('show_stats', False) and params.get('show_queries', '1') == '1'): # Instantly re-sort the existing stats data stats, queries = unpickle(params) return display_stats(request, stats, queries) elif (params.get('show_queries', False) and params.get('show_stats', '1') == '1'): stats, queries = unpickle(params) return display_queries(request, stats, queries) else: # We don't have previous data, so initialize the profiler request.profiler = hotshot.Profile(request.statsfile.name) reset_queries()
0, module; 1, function_definition; 2, function_name:dedupe_and_sort; 3, parameters; 4, block; 5, identifier:sequence; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, comment:# Add items that should be sorted first.; 12, expression_statement; 13, comment:# Add remaining items in their current order, ignoring duplicates and items; 14, comment:# that should be sorted last.; 15, for_statement; 16, comment:# Add items that should be sorted last.; 17, expression_statement; 18, comment:# Return a sequence of the same type as given.; 19, return_statement; 20, identifier:first; 21, None; 22, identifier:last; 23, None; 24, comment:""" De-dupe and partially sort a sequence. The `first` argument should contain all the items that might appear in `sequence` and for which the order (relative to each other) is important. The `last` argument is the same, but matching items will be placed at the end of the sequence. For example, `INSTALLED_APPS` and `MIDDLEWARE_CLASSES` settings. Items from `first` will only be included if they also appear in `sequence`. Items from `sequence` that don't appear in `first` will come after any that do, and retain their existing order. Returns a sequence of the same type as given. """; 25, assignment; 26, assignment; 27, assignment; 28, identifier:item; 29, identifier:sequence; 30, block; 31, call; 32, call; 33, identifier:first; 34, boolean_operator; 35, identifier:last; 36, boolean_operator; 37, identifier:new_sequence; 38, list_comprehension; 39, if_statement; 40, attribute; 41, argument_list; 42, call; 43, argument_list; 44, identifier:first; 45, list; 46, identifier:last; 47, list; 48, identifier:i; 49, for_in_clause; 50, if_clause; 51, boolean_operator; 52, block; 53, identifier:new_sequence; 54, identifier:extend; 55, list_comprehension; 56, identifier:type; 57, argument_list; 58, identifier:new_sequence; 59, identifier:i; 60, identifier:first; 61, comparison_operator:i in sequence; 62, comparison_operator:item not in new_sequence; 63, comparison_operator:item not in last; 64, expression_statement; 65, identifier:i; 66, for_in_clause; 67, if_clause; 68, identifier:sequence; 69, identifier:i; 70, identifier:sequence; 71, identifier:item; 72, identifier:new_sequence; 73, identifier:item; 74, identifier:last; 75, call; 76, identifier:i; 77, identifier:last; 78, comparison_operator:i in sequence; 79, attribute; 80, argument_list; 81, identifier:i; 82, identifier:sequence; 83, identifier:new_sequence; 84, identifier:append; 85, identifier:item
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 6, 20; 6, 21; 7, 22; 7, 23; 8, 24; 9, 25; 10, 26; 12, 27; 15, 28; 15, 29; 15, 30; 17, 31; 19, 32; 25, 33; 25, 34; 26, 35; 26, 36; 27, 37; 27, 38; 30, 39; 31, 40; 31, 41; 32, 42; 32, 43; 34, 44; 34, 45; 36, 46; 36, 47; 38, 48; 38, 49; 38, 50; 39, 51; 39, 52; 40, 53; 40, 54; 41, 55; 42, 56; 42, 57; 43, 58; 49, 59; 49, 60; 50, 61; 51, 62; 51, 63; 52, 64; 55, 65; 55, 66; 55, 67; 57, 68; 61, 69; 61, 70; 62, 71; 62, 72; 63, 73; 63, 74; 64, 75; 66, 76; 66, 77; 67, 78; 75, 79; 75, 80; 78, 81; 78, 82; 79, 83; 79, 84; 80, 85
def dedupe_and_sort(sequence, first=None, last=None): """ De-dupe and partially sort a sequence. The `first` argument should contain all the items that might appear in `sequence` and for which the order (relative to each other) is important. The `last` argument is the same, but matching items will be placed at the end of the sequence. For example, `INSTALLED_APPS` and `MIDDLEWARE_CLASSES` settings. Items from `first` will only be included if they also appear in `sequence`. Items from `sequence` that don't appear in `first` will come after any that do, and retain their existing order. Returns a sequence of the same type as given. """ first = first or [] last = last or [] # Add items that should be sorted first. new_sequence = [i for i in first if i in sequence] # Add remaining items in their current order, ignoring duplicates and items # that should be sorted last. for item in sequence: if item not in new_sequence and item not in last: new_sequence.append(item) # Add items that should be sorted last. new_sequence.extend([i for i in last if i in sequence]) # Return a sequence of the same type as given. return type(sequence)(new_sequence)
0, module; 1, function_definition; 2, function_name:sortedSemver; 3, parameters; 4, block; 5, identifier:versions; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, identifier:sort; 10, string:"asc"; 11, comment:"""Semantically sort the list of version Numbers"""; 12, boolean_operator; 13, block; 14, else_clause; 15, identifier:versions; 16, call; 17, if_statement; 18, block; 19, identifier:isinstance; 20, argument_list; 21, identifier:PY2; 22, block; 23, else_clause; 24, raise_statement; 25, identifier:versions; 26, tuple; 27, return_statement; 28, block; 29, call; 30, identifier:list; 31, identifier:tuple; 32, call; 33, import_from_statement; 34, return_statement; 35, identifier:TypeError; 36, argument_list; 37, identifier:sorted; 38, argument_list; 39, dotted_name; 40, dotted_name; 41, call; 42, string:"Invaild Versions, a list or tuple is right."; 43, identifier:versions; 44, keyword_argument; 45, keyword_argument; 46, identifier:functools; 47, identifier:cmp_to_key; 48, identifier:sorted; 49, argument_list; 50, identifier:cmp; 51, attribute; 52, identifier:reverse; 53, conditional_expression:True if sort.upper() == "DESC" else False; 54, identifier:versions; 55, keyword_argument; 56, keyword_argument; 57, identifier:semver; 58, identifier:compare; 59, True; 60, comparison_operator:sort.upper() == "DESC"; 61, False; 62, identifier:key; 63, call; 64, identifier:reverse; 65, conditional_expression:True if sort.upper() == "DESC" else False; 66, call; 67, string:"DESC"; 68, identifier:cmp_to_key; 69, argument_list; 70, True; 71, comparison_operator:sort.upper() == "DESC"; 72, False; 73, attribute; 74, argument_list; 75, attribute; 76, call; 77, string:"DESC"; 78, identifier:sort; 79, identifier:upper; 80, identifier:semver; 81, identifier:compare; 82, attribute; 83, argument_list; 84, identifier:sort; 85, identifier:upper
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 6, 9; 6, 10; 7, 11; 8, 12; 8, 13; 8, 14; 12, 15; 12, 16; 13, 17; 14, 18; 16, 19; 16, 20; 17, 21; 17, 22; 17, 23; 18, 24; 20, 25; 20, 26; 22, 27; 23, 28; 24, 29; 26, 30; 26, 31; 27, 32; 28, 33; 28, 34; 29, 35; 29, 36; 32, 37; 32, 38; 33, 39; 33, 40; 34, 41; 36, 42; 38, 43; 38, 44; 38, 45; 39, 46; 40, 47; 41, 48; 41, 49; 44, 50; 44, 51; 45, 52; 45, 53; 49, 54; 49, 55; 49, 56; 51, 57; 51, 58; 53, 59; 53, 60; 53, 61; 55, 62; 55, 63; 56, 64; 56, 65; 60, 66; 60, 67; 63, 68; 63, 69; 65, 70; 65, 71; 65, 72; 66, 73; 66, 74; 69, 75; 71, 76; 71, 77; 73, 78; 73, 79; 75, 80; 75, 81; 76, 82; 76, 83; 82, 84; 82, 85
def sortedSemver(versions, sort="asc"): """Semantically sort the list of version Numbers""" if versions and isinstance(versions, (list, tuple)): if PY2: return sorted(versions, cmp=semver.compare, reverse=True if sort.upper() == "DESC" else False) else: from functools import cmp_to_key return sorted(versions, key=cmp_to_key(semver.compare), reverse=True if sort.upper() == "DESC" else False) else: raise TypeError("Invaild Versions, a list or tuple is right.")
0, module; 1, function_definition; 2, function_name:unique; 3, parameters; 4, type; 5, block; 6, identifier:arr; 7, default_parameter; 8, attribute; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, while_statement; 13, return_statement; 14, identifier:tolerance; 15, float:1e-6; 16, identifier:np; 17, identifier:ndarray; 18, comment:"""Return unique elements in 1D array, within tolerance. Parameters ---------- arr : array_like Input array. This will be flattened if it is not already 1D. tolerance : number (optional) The tolerance for uniqueness. Returns ------- array The sorted unique values. """; 19, assignment; 20, assignment; 21, comparison_operator:len(arr) > 0; 22, block; 23, call; 24, identifier:arr; 25, call; 26, identifier:unique; 27, list; 28, call; 29, integer:0; 30, expression_statement; 31, expression_statement; 32, expression_statement; 33, expression_statement; 34, expression_statement; 35, attribute; 36, argument_list; 37, identifier:sorted; 38, argument_list; 39, identifier:len; 40, argument_list; 41, assignment; 42, assignment; 43, assignment; 44, assignment; 45, call; 46, identifier:np; 47, identifier:array; 48, identifier:unique; 49, call; 50, identifier:arr; 51, identifier:current; 52, subscript; 53, identifier:lis; 54, list_comprehension; 55, identifier:arr; 56, list_comprehension; 57, identifier:xi_lis_average; 58, binary_operator:sum(lis) / len(lis); 59, attribute; 60, argument_list; 61, attribute; 62, argument_list; 63, identifier:arr; 64, integer:0; 65, identifier:xi; 66, for_in_clause; 67, if_clause; 68, identifier:xi; 69, for_in_clause; 70, if_clause; 71, call; 72, call; 73, identifier:unique; 74, identifier:append; 75, identifier:xi_lis_average; 76, identifier:arr; 77, identifier:flatten; 78, identifier:xi; 79, identifier:arr; 80, comparison_operator:np.abs(current - xi) < tolerance; 81, identifier:xi; 82, identifier:arr; 83, not_operator; 84, identifier:sum; 85, argument_list; 86, identifier:len; 87, argument_list; 88, call; 89, identifier:tolerance; 90, comparison_operator:np.abs(lis[0] - xi) < tolerance; 91, identifier:lis; 92, identifier:lis; 93, attribute; 94, argument_list; 95, call; 96, identifier:tolerance; 97, identifier:np; 98, identifier:abs; 99, binary_operator:current - xi; 100, attribute; 101, argument_list; 102, identifier:current; 103, identifier:xi; 104, identifier:np; 105, identifier:abs; 106, binary_operator:lis[0] - xi; 107, subscript; 108, identifier:xi; 109, identifier:lis; 110, integer:0
0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 3, 7; 4, 8; 5, 9; 5, 10; 5, 11; 5, 12; 5, 13; 7, 14; 7, 15; 8, 16; 8, 17; 9, 18; 10, 19; 11, 20; 12, 21; 12, 22; 13, 23; 19, 24; 19, 25; 20, 26; 20, 27; 21, 28; 21, 29; 22, 30; 22, 31; 22, 32; 22, 33; 22, 34; 23, 35; 23, 36; 25, 37; 25, 38; 28, 39; 28, 40; 30, 41; 31, 42; 32, 43; 33, 44; 34, 45; 35, 46; 35, 47; 36, 48; 38, 49; 40, 50; 41, 51; 41, 52; 42, 53; 42, 54; 43, 55; 43, 56; 44, 57; 44, 58; 45, 59; 45, 60; 49, 61; 49, 62; 52, 63; 52, 64; 54, 65; 54, 66; 54, 67; 56, 68; 56, 69; 56, 70; 58, 71; 58, 72; 59, 73; 59, 74; 60, 75; 61, 76; 61, 77; 66, 78; 66, 79; 67, 80; 69, 81; 69, 82; 70, 83; 71, 84; 71, 85; 72, 86; 72, 87; 80, 88; 80, 89; 83, 90; 85, 91; 87, 92; 88, 93; 88, 94; 90, 95; 90, 96; 93, 97; 93, 98; 94, 99; 95, 100; 95, 101; 99, 102; 99, 103; 100, 104; 100, 105; 101, 106; 106, 107; 106, 108; 107, 109; 107, 110
def unique(arr, tolerance=1e-6) -> np.ndarray: """Return unique elements in 1D array, within tolerance. Parameters ---------- arr : array_like Input array. This will be flattened if it is not already 1D. tolerance : number (optional) The tolerance for uniqueness. Returns ------- array The sorted unique values. """ arr = sorted(arr.flatten()) unique = [] while len(arr) > 0: current = arr[0] lis = [xi for xi in arr if np.abs(current - xi) < tolerance] arr = [xi for xi in arr if not np.abs(lis[0] - xi) < tolerance] xi_lis_average = sum(lis) / len(lis) unique.append(xi_lis_average) return np.array(unique)
0, module; 1, function_definition; 2, function_name:_get_cache_key; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, return_statement; 12, comment:""" The cache key is a string of concatenated sorted names and values. """; 13, assignment; 14, call; 15, assignment; 16, identifier:key; 17, identifier:keys; 18, block; 19, call; 20, identifier:keys; 21, call; 22, attribute; 23, argument_list; 24, identifier:cache_key; 25, call; 26, if_statement; 27, attribute; 28, argument_list; 29, identifier:list; 30, argument_list; 31, identifier:keys; 32, identifier:sort; 33, identifier:str; 34, argument_list; 35, boolean_operator; 36, block; 37, call; 38, identifier:hexdigest; 39, call; 40, boolean_operator; 41, comparison_operator:key != "sk"; 42, expression_statement; 43, attribute; 44, argument_list; 45, attribute; 46, argument_list; 47, comparison_operator:key != "api_sig"; 48, comparison_operator:key != "api_key"; 49, identifier:key; 50, string:"sk"; 51, augmented_assignment; 52, identifier:hashlib; 53, identifier:sha1; 54, call; 55, attribute; 56, identifier:keys; 57, identifier:key; 58, string:"api_sig"; 59, identifier:key; 60, string:"api_key"; 61, identifier:cache_key; 62, binary_operator:key + self.params[key]; 63, attribute; 64, argument_list; 65, identifier:self; 66, identifier:params; 67, identifier:key; 68, subscript; 69, identifier:cache_key; 70, identifier:encode; 71, string:"utf-8"; 72, attribute; 73, identifier:key; 74, identifier:self; 75, identifier:params
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 6, 12; 7, 13; 8, 14; 9, 15; 10, 16; 10, 17; 10, 18; 11, 19; 13, 20; 13, 21; 14, 22; 14, 23; 15, 24; 15, 25; 18, 26; 19, 27; 19, 28; 21, 29; 21, 30; 22, 31; 22, 32; 25, 33; 25, 34; 26, 35; 26, 36; 27, 37; 27, 38; 30, 39; 35, 40; 35, 41; 36, 42; 37, 43; 37, 44; 39, 45; 39, 46; 40, 47; 40, 48; 41, 49; 41, 50; 42, 51; 43, 52; 43, 53; 44, 54; 45, 55; 45, 56; 47, 57; 47, 58; 48, 59; 48, 60; 51, 61; 51, 62; 54, 63; 54, 64; 55, 65; 55, 66; 62, 67; 62, 68; 63, 69; 63, 70; 64, 71; 68, 72; 68, 73; 72, 74; 72, 75
def _get_cache_key(self): """ The cache key is a string of concatenated sorted names and values. """ keys = list(self.params.keys()) keys.sort() cache_key = str() for key in keys: if key != "api_sig" and key != "api_key" and key != "sk": cache_key += key + self.params[key] return hashlib.sha1(cache_key.encode("utf-8")).hexdigest()
0, module; 1, function_definition; 2, function_name:fast_float; 3, parameters; 4, block; 5, identifier:x; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, expression_statement; 12, if_statement; 13, identifier:key; 14, lambda; 15, identifier:nan; 16, None; 17, identifier:_uni; 18, attribute; 19, identifier:_nan_inf; 20, identifier:NAN_INF; 21, identifier:_first_char; 22, identifier:POTENTIAL_FIRST_CHAR; 23, comment:""" Convert a string to a float quickly, return input as-is if not possible. We don't need to accept all input that the real fast_int accepts because natsort is controlling what is passed to this function. Parameters ---------- x : str String to attempt to convert to a float. key : callable Single-argument function to apply to *x* if conversion fails. nan : object Value to return instead of NaN if NaN would be returned. Returns ------- *str* or *float* """; 24, boolean_operator; 25, block; 26, else_clause; 27, lambda_parameters; 28, identifier:x; 29, identifier:unicodedata; 30, identifier:numeric; 31, comparison_operator:x[0] in _first_char; 32, comparison_operator:x.lstrip()[:3] in _nan_inf; 33, try_statement; 34, block; 35, identifier:x; 36, subscript; 37, identifier:_first_char; 38, subscript; 39, identifier:_nan_inf; 40, block; 41, except_clause; 42, try_statement; 43, identifier:x; 44, integer:0; 45, call; 46, slice; 47, expression_statement; 48, return_statement; 49, identifier:ValueError; 50, block; 51, block; 52, except_clause; 53, attribute; 54, argument_list; 55, integer:3; 56, assignment; 57, conditional_expression:nan if nan is not None and x != x else x; 58, try_statement; 59, return_statement; 60, identifier:TypeError; 61, comment:# pragma: no cover; 62, block; 63, identifier:x; 64, identifier:lstrip; 65, identifier:x; 66, call; 67, identifier:nan; 68, boolean_operator; 69, identifier:x; 70, block; 71, except_clause; 72, conditional_expression:_uni(x, key(x)) if len(x) == 1 else key(x); 73, return_statement; 74, identifier:float; 75, argument_list; 76, comparison_operator:nan is not None; 77, comparison_operator:x != x; 78, return_statement; 79, identifier:TypeError; 80, comment:# pragma: no cover; 81, block; 82, call; 83, comparison_operator:len(x) == 1; 84, call; 85, call; 86, identifier:x; 87, identifier:nan; 88, None; 89, identifier:x; 90, identifier:x; 91, conditional_expression:_uni(x, key(x)) if len(x) == 1 else key(x); 92, return_statement; 93, identifier:_uni; 94, argument_list; 95, call; 96, integer:1; 97, identifier:key; 98, argument_list; 99, identifier:key; 100, argument_list; 101, call; 102, comparison_operator:len(x) == 1; 103, call; 104, call; 105, identifier:x; 106, call; 107, identifier:len; 108, argument_list; 109, identifier:x; 110, identifier:x; 111, identifier:_uni; 112, argument_list; 113, call; 114, integer:1; 115, identifier:key; 116, argument_list; 117, identifier:key; 118, argument_list; 119, identifier:key; 120, argument_list; 121, identifier:x; 122, identifier:x; 123, call; 124, identifier:len; 125, argument_list; 126, identifier:x; 127, identifier:x; 128, identifier:x; 129, identifier:key; 130, argument_list; 131, identifier:x; 132, identifier:x
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, 11; 4, 12; 6, 13; 6, 14; 7, 15; 7, 16; 8, 17; 8, 18; 9, 19; 9, 20; 10, 21; 10, 22; 11, 23; 12, 24; 12, 25; 12, 26; 14, 27; 14, 28; 18, 29; 18, 30; 24, 31; 24, 32; 25, 33; 26, 34; 27, 35; 31, 36; 31, 37; 32, 38; 32, 39; 33, 40; 33, 41; 34, 42; 36, 43; 36, 44; 38, 45; 38, 46; 40, 47; 40, 48; 41, 49; 41, 50; 42, 51; 42, 52; 45, 53; 45, 54; 46, 55; 47, 56; 48, 57; 50, 58; 51, 59; 52, 60; 52, 61; 52, 62; 53, 63; 53, 64; 56, 65; 56, 66; 57, 67; 57, 68; 57, 69; 58, 70; 58, 71; 59, 72; 62, 73; 66, 74; 66, 75; 68, 76; 68, 77; 70, 78; 71, 79; 71, 80; 71, 81; 72, 82; 72, 83; 72, 84; 73, 85; 75, 86; 76, 87; 76, 88; 77, 89; 77, 90; 78, 91; 81, 92; 82, 93; 82, 94; 83, 95; 83, 96; 84, 97; 84, 98; 85, 99; 85, 100; 91, 101; 91, 102; 91, 103; 92, 104; 94, 105; 94, 106; 95, 107; 95, 108; 98, 109; 100, 110; 101, 111; 101, 112; 102, 113; 102, 114; 103, 115; 103, 116; 104, 117; 104, 118; 106, 119; 106, 120; 108, 121; 112, 122; 112, 123; 113, 124; 113, 125; 116, 126; 118, 127; 120, 128; 123, 129; 123, 130; 125, 131; 130, 132
def fast_float( x, key=lambda x: x, nan=None, _uni=unicodedata.numeric, _nan_inf=NAN_INF, _first_char=POTENTIAL_FIRST_CHAR, ): """ Convert a string to a float quickly, return input as-is if not possible. We don't need to accept all input that the real fast_int accepts because natsort is controlling what is passed to this function. Parameters ---------- x : str String to attempt to convert to a float. key : callable Single-argument function to apply to *x* if conversion fails. nan : object Value to return instead of NaN if NaN would be returned. Returns ------- *str* or *float* """ if x[0] in _first_char or x.lstrip()[:3] in _nan_inf: try: x = float(x) return nan if nan is not None and x != x else x except ValueError: try: return _uni(x, key(x)) if len(x) == 1 else key(x) except TypeError: # pragma: no cover return key(x) else: try: return _uni(x, key(x)) if len(x) == 1 else key(x) except TypeError: # pragma: no cover return key(x)
0, module; 1, function_definition; 2, function_name:fast_int; 3, parameters; 4, block; 5, identifier:x; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, identifier:key; 12, lambda; 13, identifier:_uni; 14, attribute; 15, identifier:_first_char; 16, identifier:POTENTIAL_FIRST_CHAR; 17, comment:""" Convert a string to a int quickly, return input as-is if not possible. We don't need to accept all input that the real fast_int accepts because natsort is controlling what is passed to this function. Parameters ---------- x : str String to attempt to convert to an int. key : callable Single-argument function to apply to *x* if conversion fails. Returns ------- *str* or *int* """; 18, comparison_operator:x[0] in _first_char; 19, block; 20, else_clause; 21, lambda_parameters; 22, identifier:x; 23, identifier:unicodedata; 24, identifier:digit; 25, subscript; 26, identifier:_first_char; 27, try_statement; 28, block; 29, identifier:x; 30, identifier:x; 31, integer:0; 32, block; 33, except_clause; 34, try_statement; 35, return_statement; 36, identifier:ValueError; 37, block; 38, block; 39, except_clause; 40, call; 41, try_statement; 42, return_statement; 43, identifier:TypeError; 44, comment:# pragma: no cover; 45, block; 46, identifier:long; 47, argument_list; 48, block; 49, except_clause; 50, conditional_expression:_uni(x, key(x)) if len(x) == 1 else key(x); 51, return_statement; 52, identifier:x; 53, return_statement; 54, identifier:TypeError; 55, comment:# pragma: no cover; 56, block; 57, call; 58, comparison_operator:len(x) == 1; 59, call; 60, call; 61, conditional_expression:_uni(x, key(x)) if len(x) == 1 else key(x); 62, return_statement; 63, identifier:_uni; 64, argument_list; 65, call; 66, integer:1; 67, identifier:key; 68, argument_list; 69, identifier:key; 70, argument_list; 71, call; 72, comparison_operator:len(x) == 1; 73, call; 74, call; 75, identifier:x; 76, call; 77, identifier:len; 78, argument_list; 79, identifier:x; 80, identifier:x; 81, identifier:_uni; 82, argument_list; 83, call; 84, integer:1; 85, identifier:key; 86, argument_list; 87, identifier:key; 88, argument_list; 89, identifier:key; 90, argument_list; 91, identifier:x; 92, identifier:x; 93, call; 94, identifier:len; 95, argument_list; 96, identifier:x; 97, identifier:x; 98, identifier:x; 99, identifier:key; 100, argument_list; 101, identifier:x; 102, identifier:x
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 6, 11; 6, 12; 7, 13; 7, 14; 8, 15; 8, 16; 9, 17; 10, 18; 10, 19; 10, 20; 12, 21; 12, 22; 14, 23; 14, 24; 18, 25; 18, 26; 19, 27; 20, 28; 21, 29; 25, 30; 25, 31; 27, 32; 27, 33; 28, 34; 32, 35; 33, 36; 33, 37; 34, 38; 34, 39; 35, 40; 37, 41; 38, 42; 39, 43; 39, 44; 39, 45; 40, 46; 40, 47; 41, 48; 41, 49; 42, 50; 45, 51; 47, 52; 48, 53; 49, 54; 49, 55; 49, 56; 50, 57; 50, 58; 50, 59; 51, 60; 53, 61; 56, 62; 57, 63; 57, 64; 58, 65; 58, 66; 59, 67; 59, 68; 60, 69; 60, 70; 61, 71; 61, 72; 61, 73; 62, 74; 64, 75; 64, 76; 65, 77; 65, 78; 68, 79; 70, 80; 71, 81; 71, 82; 72, 83; 72, 84; 73, 85; 73, 86; 74, 87; 74, 88; 76, 89; 76, 90; 78, 91; 82, 92; 82, 93; 83, 94; 83, 95; 86, 96; 88, 97; 90, 98; 93, 99; 93, 100; 95, 101; 100, 102
def fast_int( x, key=lambda x: x, _uni=unicodedata.digit, _first_char=POTENTIAL_FIRST_CHAR, ): """ Convert a string to a int quickly, return input as-is if not possible. We don't need to accept all input that the real fast_int accepts because natsort is controlling what is passed to this function. Parameters ---------- x : str String to attempt to convert to an int. key : callable Single-argument function to apply to *x* if conversion fails. Returns ------- *str* or *int* """ if x[0] in _first_char: try: return long(x) except ValueError: try: return _uni(x, key(x)) if len(x) == 1 else key(x) except TypeError: # pragma: no cover return key(x) else: try: return _uni(x, key(x)) if len(x) == 1 else key(x) except TypeError: # pragma: no cover return key(x)
0, module; 1, function_definition; 2, function_name:sort_and_print_entries; 3, parameters; 4, block; 5, identifier:entries; 6, identifier:args; 7, expression_statement; 8, comment:# Extract the proper number type.; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, comment:# Pre-remove entries that don't pass the filtering criteria; 13, comment:# Make sure we use the same searching algorithm for filtering; 14, comment:# as for sorting.; 15, expression_statement; 16, if_statement; 17, comment:# Print off the sorted results; 18, for_statement; 19, comment:"""Sort the entries, applying the filters first if necessary."""; 20, assignment; 21, assignment; 22, assignment; 23, assignment; 24, boolean_operator; 25, block; 26, identifier:entry; 27, call; 28, block; 29, identifier:is_float; 30, comparison_operator:args.number_type in ("float", "real", "f", "r"); 31, identifier:signed; 32, boolean_operator; 33, identifier:alg; 34, parenthesized_expression; 35, identifier:do_filter; 36, boolean_operator; 37, identifier:do_filter; 38, attribute; 39, expression_statement; 40, expression_statement; 41, if_statement; 42, if_statement; 43, if_statement; 44, attribute; 45, argument_list; 46, expression_statement; 47, attribute; 48, tuple; 49, attribute; 50, comparison_operator:args.number_type in ("real", "r"); 51, binary_operator:natsort.ns.FLOAT * is_float | natsort.ns.SIGNED * signed | natsort.ns.NOEXP * (not args.exp) | natsort.ns.PATH * args.paths | natsort.ns.LOCALE * args.locale; 52, comparison_operator:args.filter is not None; 53, comparison_operator:args.reverse_filter is not None; 54, identifier:args; 55, identifier:exclude; 56, assignment; 57, assignment; 58, comparison_operator:args.filter is not None; 59, block; 60, comparison_operator:args.reverse_filter is not None; 61, block; 62, attribute; 63, block; 64, identifier:natsort; 65, identifier:natsorted; 66, identifier:entries; 67, keyword_argument; 68, keyword_argument; 69, call; 70, identifier:args; 71, identifier:number_type; 72, string:"float"; 73, string:"real"; 74, string:"f"; 75, string:"r"; 76, identifier:args; 77, identifier:signed; 78, attribute; 79, tuple; 80, binary_operator:natsort.ns.FLOAT * is_float | natsort.ns.SIGNED * signed | natsort.ns.NOEXP * (not args.exp) | natsort.ns.PATH * args.paths; 81, binary_operator:natsort.ns.LOCALE * args.locale; 82, attribute; 83, None; 84, attribute; 85, None; 86, identifier:inp_options; 87, parenthesized_expression; 88, identifier:regex; 89, call; 90, attribute; 91, None; 92, expression_statement; 93, expression_statement; 94, attribute; 95, None; 96, expression_statement; 97, expression_statement; 98, identifier:args; 99, identifier:exclude; 100, expression_statement; 101, expression_statement; 102, identifier:reverse; 103, attribute; 104, identifier:alg; 105, identifier:alg; 106, identifier:print; 107, argument_list; 108, identifier:args; 109, identifier:number_type; 110, string:"real"; 111, string:"r"; 112, binary_operator:natsort.ns.FLOAT * is_float | natsort.ns.SIGNED * signed | natsort.ns.NOEXP * (not args.exp); 113, binary_operator:natsort.ns.PATH * args.paths; 114, attribute; 115, attribute; 116, identifier:args; 117, identifier:filter; 118, identifier:args; 119, identifier:reverse_filter; 120, binary_operator:natsort.ns.FLOAT * is_float | natsort.ns.SIGNED * signed | natsort.ns.NOEXP * (not args.exp); 121, identifier:regex_chooser; 122, argument_list; 123, identifier:args; 124, identifier:filter; 125, assignment; 126, assignment; 127, identifier:args; 128, identifier:reverse_filter; 129, assignment; 130, assignment; 131, assignment; 132, assignment; 133, identifier:args; 134, identifier:reverse; 135, identifier:entry; 136, binary_operator:natsort.ns.FLOAT * is_float | natsort.ns.SIGNED * signed; 137, binary_operator:natsort.ns.NOEXP * (not args.exp); 138, attribute; 139, attribute; 140, attribute; 141, identifier:LOCALE; 142, identifier:args; 143, identifier:locale; 144, binary_operator:natsort.ns.FLOAT * is_float | natsort.ns.SIGNED * signed; 145, binary_operator:natsort.ns.NOEXP * (not args.exp); 146, identifier:inp_options; 147, pattern_list; 148, tuple; 149, identifier:entries; 150, list_comprehension; 151, pattern_list; 152, tuple; 153, identifier:entries; 154, list_comprehension; 155, identifier:exclude; 156, call; 157, identifier:entries; 158, list_comprehension; 159, binary_operator:natsort.ns.FLOAT * is_float; 160, binary_operator:natsort.ns.SIGNED * signed; 161, attribute; 162, parenthesized_expression; 163, attribute; 164, identifier:PATH; 165, identifier:args; 166, identifier:paths; 167, identifier:natsort; 168, identifier:ns; 169, binary_operator:natsort.ns.FLOAT * is_float; 170, binary_operator:natsort.ns.SIGNED * signed; 171, attribute; 172, parenthesized_expression; 173, identifier:lows; 174, identifier:highs; 175, list_comprehension; 176, list_comprehension; 177, identifier:entry; 178, for_in_clause; 179, if_clause; 180, identifier:lows; 181, identifier:highs; 182, list_comprehension; 183, list_comprehension; 184, identifier:entry; 185, for_in_clause; 186, if_clause; 187, identifier:set; 188, argument_list; 189, identifier:entry; 190, for_in_clause; 191, if_clause; 192, attribute; 193, identifier:is_float; 194, attribute; 195, identifier:signed; 196, attribute; 197, identifier:NOEXP; 198, not_operator; 199, identifier:natsort; 200, identifier:ns; 201, attribute; 202, identifier:is_float; 203, attribute; 204, identifier:signed; 205, attribute; 206, identifier:NOEXP; 207, not_operator; 208, subscript; 209, for_in_clause; 210, subscript; 211, for_in_clause; 212, identifier:entry; 213, identifier:entries; 214, call; 215, subscript; 216, for_in_clause; 217, subscript; 218, for_in_clause; 219, identifier:entry; 220, identifier:entries; 221, not_operator; 222, attribute; 223, identifier:entry; 224, identifier:entries; 225, call; 226, attribute; 227, identifier:FLOAT; 228, attribute; 229, identifier:SIGNED; 230, identifier:natsort; 231, identifier:ns; 232, attribute; 233, attribute; 234, identifier:FLOAT; 235, attribute; 236, identifier:SIGNED; 237, identifier:natsort; 238, identifier:ns; 239, attribute; 240, identifier:f; 241, integer:0; 242, identifier:f; 243, attribute; 244, identifier:f; 245, integer:1; 246, identifier:f; 247, attribute; 248, identifier:keep_entry_range; 249, argument_list; 250, identifier:f; 251, integer:0; 252, identifier:f; 253, attribute; 254, identifier:f; 255, integer:1; 256, identifier:f; 257, attribute; 258, call; 259, identifier:args; 260, identifier:exclude; 261, identifier:keep_entry_value; 262, argument_list; 263, identifier:natsort; 264, identifier:ns; 265, identifier:natsort; 266, identifier:ns; 267, identifier:args; 268, identifier:exp; 269, identifier:natsort; 270, identifier:ns; 271, identifier:natsort; 272, identifier:ns; 273, identifier:args; 274, identifier:exp; 275, identifier:args; 276, identifier:filter; 277, identifier:args; 278, identifier:filter; 279, identifier:entry; 280, identifier:lows; 281, identifier:highs; 282, identifier:float; 283, identifier:regex; 284, identifier:args; 285, identifier:reverse_filter; 286, identifier:args; 287, identifier:reverse_filter; 288, identifier:keep_entry_range; 289, argument_list; 290, identifier:entry; 291, identifier:exclude; 292, identifier:float; 293, identifier:regex; 294, identifier:entry; 295, identifier:lows; 296, identifier:highs; 297, identifier:float; 298, identifier:regex
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 7, 19; 9, 20; 10, 21; 11, 22; 15, 23; 16, 24; 16, 25; 18, 26; 18, 27; 18, 28; 20, 29; 20, 30; 21, 31; 21, 32; 22, 33; 22, 34; 23, 35; 23, 36; 24, 37; 24, 38; 25, 39; 25, 40; 25, 41; 25, 42; 25, 43; 27, 44; 27, 45; 28, 46; 30, 47; 30, 48; 32, 49; 32, 50; 34, 51; 36, 52; 36, 53; 38, 54; 38, 55; 39, 56; 40, 57; 41, 58; 41, 59; 42, 60; 42, 61; 43, 62; 43, 63; 44, 64; 44, 65; 45, 66; 45, 67; 45, 68; 46, 69; 47, 70; 47, 71; 48, 72; 48, 73; 48, 74; 48, 75; 49, 76; 49, 77; 50, 78; 50, 79; 51, 80; 51, 81; 52, 82; 52, 83; 53, 84; 53, 85; 56, 86; 56, 87; 57, 88; 57, 89; 58, 90; 58, 91; 59, 92; 59, 93; 60, 94; 60, 95; 61, 96; 61, 97; 62, 98; 62, 99; 63, 100; 63, 101; 67, 102; 67, 103; 68, 104; 68, 105; 69, 106; 69, 107; 78, 108; 78, 109; 79, 110; 79, 111; 80, 112; 80, 113; 81, 114; 81, 115; 82, 116; 82, 117; 84, 118; 84, 119; 87, 120; 89, 121; 89, 122; 90, 123; 90, 124; 92, 125; 93, 126; 94, 127; 94, 128; 96, 129; 97, 130; 100, 131; 101, 132; 103, 133; 103, 134; 107, 135; 112, 136; 112, 137; 113, 138; 113, 139; 114, 140; 114, 141; 115, 142; 115, 143; 120, 144; 120, 145; 122, 146; 125, 147; 125, 148; 126, 149; 126, 150; 129, 151; 129, 152; 130, 153; 130, 154; 131, 155; 131, 156; 132, 157; 132, 158; 136, 159; 136, 160; 137, 161; 137, 162; 138, 163; 138, 164; 139, 165; 139, 166; 140, 167; 140, 168; 144, 169; 144, 170; 145, 171; 145, 172; 147, 173; 147, 174; 148, 175; 148, 176; 150, 177; 150, 178; 150, 179; 151, 180; 151, 181; 152, 182; 152, 183; 154, 184; 154, 185; 154, 186; 156, 187; 156, 188; 158, 189; 158, 190; 158, 191; 159, 192; 159, 193; 160, 194; 160, 195; 161, 196; 161, 197; 162, 198; 163, 199; 163, 200; 169, 201; 169, 202; 170, 203; 170, 204; 171, 205; 171, 206; 172, 207; 175, 208; 175, 209; 176, 210; 176, 211; 178, 212; 178, 213; 179, 214; 182, 215; 182, 216; 183, 217; 183, 218; 185, 219; 185, 220; 186, 221; 188, 222; 190, 223; 190, 224; 191, 225; 192, 226; 192, 227; 194, 228; 194, 229; 196, 230; 196, 231; 198, 232; 201, 233; 201, 234; 203, 235; 203, 236; 205, 237; 205, 238; 207, 239; 208, 240; 208, 241; 209, 242; 209, 243; 210, 244; 210, 245; 211, 246; 211, 247; 214, 248; 214, 249; 215, 250; 215, 251; 216, 252; 216, 253; 217, 254; 217, 255; 218, 256; 218, 257; 221, 258; 222, 259; 222, 260; 225, 261; 225, 262; 226, 263; 226, 264; 228, 265; 228, 266; 232, 267; 232, 268; 233, 269; 233, 270; 235, 271; 235, 272; 239, 273; 239, 274; 243, 275; 243, 276; 247, 277; 247, 278; 249, 279; 249, 280; 249, 281; 249, 282; 249, 283; 253, 284; 253, 285; 257, 286; 257, 287; 258, 288; 258, 289; 262, 290; 262, 291; 262, 292; 262, 293; 289, 294; 289, 295; 289, 296; 289, 297; 289, 298
def sort_and_print_entries(entries, args): """Sort the entries, applying the filters first if necessary.""" # Extract the proper number type. is_float = args.number_type in ("float", "real", "f", "r") signed = args.signed or args.number_type in ("real", "r") alg = ( natsort.ns.FLOAT * is_float | natsort.ns.SIGNED * signed | natsort.ns.NOEXP * (not args.exp) | natsort.ns.PATH * args.paths | natsort.ns.LOCALE * args.locale ) # Pre-remove entries that don't pass the filtering criteria # Make sure we use the same searching algorithm for filtering # as for sorting. do_filter = args.filter is not None or args.reverse_filter is not None if do_filter or args.exclude: inp_options = ( natsort.ns.FLOAT * is_float | natsort.ns.SIGNED * signed | natsort.ns.NOEXP * (not args.exp) ) regex = regex_chooser(inp_options) if args.filter is not None: lows, highs = ([f[0] for f in args.filter], [f[1] for f in args.filter]) entries = [ entry for entry in entries if keep_entry_range(entry, lows, highs, float, regex) ] if args.reverse_filter is not None: lows, highs = ( [f[0] for f in args.reverse_filter], [f[1] for f in args.reverse_filter], ) entries = [ entry for entry in entries if not keep_entry_range(entry, lows, highs, float, regex) ] if args.exclude: exclude = set(args.exclude) entries = [ entry for entry in entries if keep_entry_value(entry, exclude, float, regex) ] # Print off the sorted results for entry in natsort.natsorted(entries, reverse=args.reverse, alg=alg): print(entry)
0, module; 1, function_definition; 2, function_name:natsort_key; 3, parameters; 4, block; 5, identifier:val; 6, identifier:key; 7, identifier:string_func; 8, identifier:bytes_func; 9, identifier:num_func; 10, expression_statement; 11, comment:# Apply key if needed; 12, if_statement; 13, comment:# Assume the input are strings, which is the most common case; 14, try_statement; 15, comment:""" Key to sort strings and numbers naturally. It works by splitting the string into components of strings and numbers, and then converting the numbers into actual ints or floats. Parameters ---------- val : str | unicode | bytes | int | float | iterable key : callable | None A key to apply to the *val* before any other operations are performed. string_func : callable If *val* (or the output of *key* if given) is of type *str*, this function will be applied to it. The function must return a tuple. bytes_func : callable If *val* (or the output of *key* if given) is of type *bytes*, this function will be applied to it. The function must return a tuple. num_func : callable If *val* (or the output of *key* if given) is not of type *bytes*, *str*, nor is iterable, this function will be applied to it. The function must return a tuple. Returns ------- out : tuple The string split into its string and numeric components. It *always* starts with a string, and then alternates between numbers and strings (unless it was applied recursively, in which case it will return tuples of tuples, but the lowest-level tuples will then *always* start with a string etc.). See Also -------- parse_string_factory parse_bytes_factory parse_number_factory """; 16, comparison_operator:key is not None; 17, block; 18, block; 19, except_clause; 20, identifier:key; 21, None; 22, expression_statement; 23, return_statement; 24, tuple; 25, comment:# If bytes type, use the bytes_func; 26, block; 27, assignment; 28, call; 29, identifier:TypeError; 30, identifier:AttributeError; 31, if_statement; 32, comment:# Otherwise, assume it is an iterable that must be parsed recursively.; 33, comment:# Do not apply the key recursively.; 34, try_statement; 35, identifier:val; 36, call; 37, identifier:string_func; 38, argument_list; 39, comparison_operator:type(val) in (bytes,); 40, block; 41, block; 42, comment:# If that failed, it must be a number.; 43, except_clause; 44, identifier:key; 45, argument_list; 46, identifier:val; 47, call; 48, tuple; 49, return_statement; 50, return_statement; 51, identifier:TypeError; 52, block; 53, identifier:val; 54, identifier:type; 55, argument_list; 56, identifier:bytes; 57, call; 58, call; 59, return_statement; 60, identifier:val; 61, identifier:bytes_func; 62, argument_list; 63, identifier:tuple; 64, generator_expression; 65, call; 66, identifier:val; 67, call; 68, for_in_clause; 69, identifier:num_func; 70, argument_list; 71, identifier:natsort_key; 72, argument_list; 73, identifier:x; 74, identifier:val; 75, identifier:val; 76, identifier:x; 77, None; 78, identifier:string_func; 79, identifier:bytes_func; 80, identifier:num_func
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 10, 15; 12, 16; 12, 17; 14, 18; 14, 19; 16, 20; 16, 21; 17, 22; 18, 23; 19, 24; 19, 25; 19, 26; 22, 27; 23, 28; 24, 29; 24, 30; 26, 31; 26, 32; 26, 33; 26, 34; 27, 35; 27, 36; 28, 37; 28, 38; 31, 39; 31, 40; 34, 41; 34, 42; 34, 43; 36, 44; 36, 45; 38, 46; 39, 47; 39, 48; 40, 49; 41, 50; 43, 51; 43, 52; 45, 53; 47, 54; 47, 55; 48, 56; 49, 57; 50, 58; 52, 59; 55, 60; 57, 61; 57, 62; 58, 63; 58, 64; 59, 65; 62, 66; 64, 67; 64, 68; 65, 69; 65, 70; 67, 71; 67, 72; 68, 73; 68, 74; 70, 75; 72, 76; 72, 77; 72, 78; 72, 79; 72, 80
def natsort_key(val, key, string_func, bytes_func, num_func): """ Key to sort strings and numbers naturally. It works by splitting the string into components of strings and numbers, and then converting the numbers into actual ints or floats. Parameters ---------- val : str | unicode | bytes | int | float | iterable key : callable | None A key to apply to the *val* before any other operations are performed. string_func : callable If *val* (or the output of *key* if given) is of type *str*, this function will be applied to it. The function must return a tuple. bytes_func : callable If *val* (or the output of *key* if given) is of type *bytes*, this function will be applied to it. The function must return a tuple. num_func : callable If *val* (or the output of *key* if given) is not of type *bytes*, *str*, nor is iterable, this function will be applied to it. The function must return a tuple. Returns ------- out : tuple The string split into its string and numeric components. It *always* starts with a string, and then alternates between numbers and strings (unless it was applied recursively, in which case it will return tuples of tuples, but the lowest-level tuples will then *always* start with a string etc.). See Also -------- parse_string_factory parse_bytes_factory parse_number_factory """ # Apply key if needed if key is not None: val = key(val) # Assume the input are strings, which is the most common case try: return string_func(val) except (TypeError, AttributeError): # If bytes type, use the bytes_func if type(val) in (bytes,): return bytes_func(val) # Otherwise, assume it is an iterable that must be parsed recursively. # Do not apply the key recursively. try: return tuple( natsort_key(x, None, string_func, bytes_func, num_func) for x in val ) # If that failed, it must be a number. except TypeError: return num_func(val)
0, module; 1, function_definition; 2, function_name:parse_number_factory; 3, parameters; 4, block; 5, identifier:alg; 6, identifier:sep; 7, identifier:pre_sep; 8, expression_statement; 9, expression_statement; 10, function_definition; 11, comment:# Return the function, possibly wrapping in tuple if PATH is selected.; 12, if_statement; 13, comment:""" Create a function that will format a number into a tuple. Parameters ---------- alg : ns enum Indicate how to format the *bytes*. sep : str The string character to be inserted before the number in the returned tuple. pre_sep : str In the event that *alg* contains ``UNGROUPLETTERS``, this string will be placed in a single-element tuple at the front of the returned nested tuple. Returns ------- func : callable A function that accepts numeric input (e.g. *int* or *float*) and returns a tuple containing the number with the leading string *sep*. Intended to be used as the *num_func* argument to *natsort_key*. See Also -------- natsort_key """; 14, assignment; 15, function_name:func; 16, parameters; 17, block; 18, boolean_operator; 19, block; 20, elif_clause; 21, elif_clause; 22, else_clause; 23, identifier:nan_replace; 24, conditional_expression:float("+inf") if alg & ns.NANLAST else float("-inf"); 25, identifier:val; 26, default_parameter; 27, default_parameter; 28, expression_statement; 29, return_statement; 30, boolean_operator; 31, binary_operator:alg & ns.LOCALEALPHA; 32, return_statement; 33, boolean_operator; 34, block; 35, binary_operator:alg & ns.PATH; 36, block; 37, block; 38, call; 39, binary_operator:alg & ns.NANLAST; 40, call; 41, identifier:_nan_replace; 42, identifier:nan_replace; 43, identifier:_sep; 44, identifier:sep; 45, comment:"""Given a number, place it in a tuple with a leading null string."""; 46, expression_list; 47, binary_operator:alg & ns.PATH; 48, binary_operator:alg & ns.UNGROUPLETTERS; 49, identifier:alg; 50, attribute; 51, lambda; 52, binary_operator:alg & ns.UNGROUPLETTERS; 53, binary_operator:alg & ns.LOCALEALPHA; 54, return_statement; 55, identifier:alg; 56, attribute; 57, return_statement; 58, return_statement; 59, identifier:float; 60, argument_list; 61, identifier:alg; 62, attribute; 63, identifier:float; 64, argument_list; 65, identifier:_sep; 66, conditional_expression:_nan_replace if val != val else val; 67, identifier:alg; 68, attribute; 69, identifier:alg; 70, attribute; 71, identifier:ns; 72, identifier:LOCALEALPHA; 73, lambda_parameters; 74, tuple; 75, identifier:alg; 76, attribute; 77, identifier:alg; 78, attribute; 79, lambda; 80, identifier:ns; 81, identifier:PATH; 82, lambda; 83, identifier:func; 84, string:"+inf"; 85, identifier:ns; 86, identifier:NANLAST; 87, string:"-inf"; 88, identifier:_nan_replace; 89, comparison_operator:val != val; 90, identifier:val; 91, identifier:ns; 92, identifier:PATH; 93, identifier:ns; 94, identifier:UNGROUPLETTERS; 95, identifier:x; 96, tuple; 97, identifier:ns; 98, identifier:UNGROUPLETTERS; 99, identifier:ns; 100, identifier:LOCALEALPHA; 101, lambda_parameters; 102, tuple; 103, lambda_parameters; 104, tuple; 105, identifier:val; 106, identifier:val; 107, tuple; 108, call; 109, identifier:x; 110, tuple; 111, call; 112, identifier:x; 113, call; 114, identifier:pre_sep; 115, identifier:func; 116, argument_list; 117, identifier:pre_sep; 118, identifier:func; 119, argument_list; 120, identifier:func; 121, argument_list; 122, identifier:x; 123, identifier:x; 124, identifier:x
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 8, 13; 9, 14; 10, 15; 10, 16; 10, 17; 12, 18; 12, 19; 12, 20; 12, 21; 12, 22; 14, 23; 14, 24; 16, 25; 16, 26; 16, 27; 17, 28; 17, 29; 18, 30; 18, 31; 19, 32; 20, 33; 20, 34; 21, 35; 21, 36; 22, 37; 24, 38; 24, 39; 24, 40; 26, 41; 26, 42; 27, 43; 27, 44; 28, 45; 29, 46; 30, 47; 30, 48; 31, 49; 31, 50; 32, 51; 33, 52; 33, 53; 34, 54; 35, 55; 35, 56; 36, 57; 37, 58; 38, 59; 38, 60; 39, 61; 39, 62; 40, 63; 40, 64; 46, 65; 46, 66; 47, 67; 47, 68; 48, 69; 48, 70; 50, 71; 50, 72; 51, 73; 51, 74; 52, 75; 52, 76; 53, 77; 53, 78; 54, 79; 56, 80; 56, 81; 57, 82; 58, 83; 60, 84; 62, 85; 62, 86; 64, 87; 66, 88; 66, 89; 66, 90; 68, 91; 68, 92; 70, 93; 70, 94; 73, 95; 74, 96; 76, 97; 76, 98; 78, 99; 78, 100; 79, 101; 79, 102; 82, 103; 82, 104; 89, 105; 89, 106; 96, 107; 96, 108; 101, 109; 102, 110; 102, 111; 103, 112; 104, 113; 107, 114; 108, 115; 108, 116; 110, 117; 111, 118; 111, 119; 113, 120; 113, 121; 116, 122; 119, 123; 121, 124
def parse_number_factory(alg, sep, pre_sep): """ Create a function that will format a number into a tuple. Parameters ---------- alg : ns enum Indicate how to format the *bytes*. sep : str The string character to be inserted before the number in the returned tuple. pre_sep : str In the event that *alg* contains ``UNGROUPLETTERS``, this string will be placed in a single-element tuple at the front of the returned nested tuple. Returns ------- func : callable A function that accepts numeric input (e.g. *int* or *float*) and returns a tuple containing the number with the leading string *sep*. Intended to be used as the *num_func* argument to *natsort_key*. See Also -------- natsort_key """ nan_replace = float("+inf") if alg & ns.NANLAST else float("-inf") def func(val, _nan_replace=nan_replace, _sep=sep): """Given a number, place it in a tuple with a leading null string.""" return _sep, _nan_replace if val != val else val # Return the function, possibly wrapping in tuple if PATH is selected. if alg & ns.PATH and alg & ns.UNGROUPLETTERS and alg & ns.LOCALEALPHA: return lambda x: (((pre_sep,), func(x)),) elif alg & ns.UNGROUPLETTERS and alg & ns.LOCALEALPHA: return lambda x: ((pre_sep,), func(x)) elif alg & ns.PATH: return lambda x: (func(x),) else: return func
0, module; 1, function_definition; 2, function_name:natsort_keygen; 3, parameters; 4, block; 5, default_parameter; 6, default_parameter; 7, expression_statement; 8, try_statement; 9, comment:# Add the NS_DUMB option if the locale library is broken.; 10, if_statement; 11, comment:# Set some variables that will be passed to the factory functions; 12, if_statement; 13, expression_statement; 14, comment:# Create the functions that will be used to split strings.; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, comment:# Create the high-level parsing functions for strings, bytes, and numbers.; 19, expression_statement; 20, if_statement; 21, expression_statement; 22, expression_statement; 23, comment:# Return the natsort key with the parsing path pre-chosen.; 24, return_statement; 25, identifier:key; 26, None; 27, identifier:alg; 28, attribute; 29, comment:""" Generate a key to sort strings and numbers naturally. This key is designed for use as the `key` argument to functions such as the `sorted` builtin. The user may customize the generated function with the arguments to `natsort_keygen`, including an optional `key` function. Parameters ---------- key : callable, optional A key used to manipulate the input value before parsing for numbers. It is **not** applied recursively. It should accept a single argument and return a single value. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.INT`. Returns ------- out : function A function that parses input for natural sorting that is suitable for passing as the `key` argument to functions such as `sorted`. See Also -------- natsorted natsort_key Examples -------- `natsort_keygen` is a convenient way to create a custom key to sort lists in-place (for example).:: >>> a = ['num5.10', 'num-3', 'num5.3', 'num2'] >>> a.sort(key=natsort_keygen(alg=ns.REAL)) >>> a [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3'] """; 30, block; 31, except_clause; 32, boolean_operator; 33, block; 34, binary_operator:alg & ns.NUMAFTER; 35, block; 36, else_clause; 37, assignment; 38, assignment; 39, assignment; 40, assignment; 41, assignment; 42, binary_operator:alg & ns.PATH; 43, block; 44, assignment; 45, assignment; 46, call; 47, identifier:ns; 48, identifier:DEFAULT; 49, expression_statement; 50, identifier:TypeError; 51, block; 52, binary_operator:alg & ns.LOCALEALPHA; 53, call; 54, expression_statement; 55, identifier:alg; 56, attribute; 57, if_statement; 58, expression_statement; 59, block; 60, identifier:regex; 61, call; 62, identifier:input_transform; 63, call; 64, identifier:component_transform; 65, call; 66, identifier:final_transform; 67, call; 68, identifier:string_func; 69, call; 70, identifier:alg; 71, attribute; 72, expression_statement; 73, identifier:bytes_func; 74, call; 75, identifier:num_func; 76, call; 77, identifier:partial; 78, argument_list; 79, binary_operator:ns.DEFAULT | alg; 80, expression_statement; 81, raise_statement; 82, identifier:alg; 83, attribute; 84, attribute; 85, argument_list; 86, augmented_assignment; 87, identifier:ns; 88, identifier:NUMAFTER; 89, binary_operator:alg & ns.LOCALEALPHA; 90, block; 91, else_clause; 92, assignment; 93, if_statement; 94, expression_statement; 95, attribute; 96, argument_list; 97, attribute; 98, argument_list; 99, attribute; 100, argument_list; 101, attribute; 102, argument_list; 103, attribute; 104, argument_list; 105, identifier:ns; 106, identifier:PATH; 107, assignment; 108, attribute; 109, argument_list; 110, attribute; 111, argument_list; 112, attribute; 113, keyword_argument; 114, keyword_argument; 115, keyword_argument; 116, keyword_argument; 117, attribute; 118, identifier:alg; 119, assignment; 120, call; 121, identifier:ns; 122, identifier:LOCALEALPHA; 123, attribute; 124, identifier:dumb_sort; 125, identifier:alg; 126, identifier:NS_DUMB; 127, identifier:alg; 128, attribute; 129, expression_statement; 130, block; 131, identifier:pre_sep; 132, attribute; 133, binary_operator:alg & ns.LOCALEALPHA; 134, block; 135, else_clause; 136, assignment; 137, identifier:utils; 138, identifier:regex_chooser; 139, identifier:alg; 140, identifier:utils; 141, identifier:input_string_transform_factory; 142, identifier:alg; 143, identifier:utils; 144, identifier:string_component_transform_factory; 145, identifier:alg; 146, identifier:utils; 147, identifier:final_data_transform_factory; 148, identifier:alg; 149, identifier:sep; 150, identifier:pre_sep; 151, identifier:utils; 152, identifier:parse_string_factory; 153, identifier:alg; 154, identifier:sep; 155, attribute; 156, identifier:input_transform; 157, identifier:component_transform; 158, identifier:final_transform; 159, identifier:string_func; 160, call; 161, identifier:utils; 162, identifier:parse_bytes_factory; 163, identifier:alg; 164, identifier:utils; 165, identifier:parse_number_factory; 166, identifier:alg; 167, identifier:sep; 168, identifier:pre_sep; 169, identifier:utils; 170, identifier:natsort_key; 171, identifier:key; 172, identifier:key; 173, identifier:string_func; 174, identifier:string_func; 175, identifier:bytes_func; 176, identifier:bytes_func; 177, identifier:num_func; 178, identifier:num_func; 179, identifier:ns; 180, identifier:DEFAULT; 181, identifier:msg; 182, string:"natsort_keygen: 'alg' argument must be from the enum 'ns'"; 183, identifier:ValueError; 184, argument_list; 185, attribute; 186, identifier:locale; 187, identifier:ns; 188, identifier:LOCALEALPHA; 189, assignment; 190, expression_statement; 191, attribute; 192, identifier:null_string_max; 193, identifier:alg; 194, attribute; 195, expression_statement; 196, block; 197, identifier:pre_sep; 198, attribute; 199, identifier:regex; 200, identifier:split; 201, attribute; 202, argument_list; 203, binary_operator:msg + ", got {}".format(py23_str(alg)); 204, identifier:natsort; 205, identifier:compat; 206, identifier:sep; 207, attribute; 208, assignment; 209, attribute; 210, identifier:locale; 211, identifier:ns; 212, identifier:LOCALEALPHA; 213, assignment; 214, expression_statement; 215, attribute; 216, identifier:null_string; 217, identifier:utils; 218, identifier:parse_path_factory; 219, identifier:string_func; 220, identifier:msg; 221, call; 222, attribute; 223, identifier:null_string_locale_max; 224, identifier:sep; 225, attribute; 226, identifier:natsort; 227, identifier:compat; 228, identifier:sep; 229, attribute; 230, assignment; 231, attribute; 232, identifier:locale; 233, attribute; 234, argument_list; 235, attribute; 236, identifier:locale; 237, attribute; 238, identifier:null_string_max; 239, attribute; 240, identifier:null_string_locale; 241, identifier:sep; 242, attribute; 243, identifier:natsort; 244, identifier:compat; 245, string:", got {}"; 246, identifier:format; 247, call; 248, identifier:natsort; 249, identifier:compat; 250, attribute; 251, identifier:locale; 252, attribute; 253, identifier:locale; 254, attribute; 255, identifier:null_string; 256, identifier:py23_str; 257, argument_list; 258, identifier:natsort; 259, identifier:compat; 260, identifier:natsort; 261, identifier:compat; 262, attribute; 263, identifier:locale; 264, identifier:alg; 265, identifier:natsort; 266, identifier:compat
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 5, 25; 5, 26; 6, 27; 6, 28; 7, 29; 8, 30; 8, 31; 10, 32; 10, 33; 12, 34; 12, 35; 12, 36; 13, 37; 15, 38; 16, 39; 17, 40; 19, 41; 20, 42; 20, 43; 21, 44; 22, 45; 24, 46; 28, 47; 28, 48; 30, 49; 31, 50; 31, 51; 32, 52; 32, 53; 33, 54; 34, 55; 34, 56; 35, 57; 35, 58; 36, 59; 37, 60; 37, 61; 38, 62; 38, 63; 39, 64; 39, 65; 40, 66; 40, 67; 41, 68; 41, 69; 42, 70; 42, 71; 43, 72; 44, 73; 44, 74; 45, 75; 45, 76; 46, 77; 46, 78; 49, 79; 51, 80; 51, 81; 52, 82; 52, 83; 53, 84; 53, 85; 54, 86; 56, 87; 56, 88; 57, 89; 57, 90; 57, 91; 58, 92; 59, 93; 59, 94; 61, 95; 61, 96; 63, 97; 63, 98; 65, 99; 65, 100; 67, 101; 67, 102; 69, 103; 69, 104; 71, 105; 71, 106; 72, 107; 74, 108; 74, 109; 76, 110; 76, 111; 78, 112; 78, 113; 78, 114; 78, 115; 78, 116; 79, 117; 79, 118; 80, 119; 81, 120; 83, 121; 83, 122; 84, 123; 84, 124; 86, 125; 86, 126; 89, 127; 89, 128; 90, 129; 91, 130; 92, 131; 92, 132; 93, 133; 93, 134; 93, 135; 94, 136; 95, 137; 95, 138; 96, 139; 97, 140; 97, 141; 98, 142; 99, 143; 99, 144; 100, 145; 101, 146; 101, 147; 102, 148; 102, 149; 102, 150; 103, 151; 103, 152; 104, 153; 104, 154; 104, 155; 104, 156; 104, 157; 104, 158; 107, 159; 107, 160; 108, 161; 108, 162; 109, 163; 110, 164; 110, 165; 111, 166; 111, 167; 111, 168; 112, 169; 112, 170; 113, 171; 113, 172; 114, 173; 114, 174; 115, 175; 115, 176; 116, 177; 116, 178; 117, 179; 117, 180; 119, 181; 119, 182; 120, 183; 120, 184; 123, 185; 123, 186; 128, 187; 128, 188; 129, 189; 130, 190; 132, 191; 132, 192; 133, 193; 133, 194; 134, 195; 135, 196; 136, 197; 136, 198; 155, 199; 155, 200; 160, 201; 160, 202; 184, 203; 185, 204; 185, 205; 189, 206; 189, 207; 190, 208; 191, 209; 191, 210; 194, 211; 194, 212; 195, 213; 196, 214; 198, 215; 198, 216; 201, 217; 201, 218; 202, 219; 203, 220; 203, 221; 207, 222; 207, 223; 208, 224; 208, 225; 209, 226; 209, 227; 213, 228; 213, 229; 214, 230; 215, 231; 215, 232; 221, 233; 221, 234; 222, 235; 222, 236; 225, 237; 225, 238; 229, 239; 229, 240; 230, 241; 230, 242; 231, 243; 231, 244; 233, 245; 233, 246; 234, 247; 235, 248; 235, 249; 237, 250; 237, 251; 239, 252; 239, 253; 242, 254; 242, 255; 247, 256; 247, 257; 250, 258; 250, 259; 252, 260; 252, 261; 254, 262; 254, 263; 257, 264; 262, 265; 262, 266
def natsort_keygen(key=None, alg=ns.DEFAULT): """ Generate a key to sort strings and numbers naturally. This key is designed for use as the `key` argument to functions such as the `sorted` builtin. The user may customize the generated function with the arguments to `natsort_keygen`, including an optional `key` function. Parameters ---------- key : callable, optional A key used to manipulate the input value before parsing for numbers. It is **not** applied recursively. It should accept a single argument and return a single value. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.INT`. Returns ------- out : function A function that parses input for natural sorting that is suitable for passing as the `key` argument to functions such as `sorted`. See Also -------- natsorted natsort_key Examples -------- `natsort_keygen` is a convenient way to create a custom key to sort lists in-place (for example).:: >>> a = ['num5.10', 'num-3', 'num5.3', 'num2'] >>> a.sort(key=natsort_keygen(alg=ns.REAL)) >>> a [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3'] """ try: ns.DEFAULT | alg except TypeError: msg = "natsort_keygen: 'alg' argument must be from the enum 'ns'" raise ValueError(msg + ", got {}".format(py23_str(alg))) # Add the NS_DUMB option if the locale library is broken. if alg & ns.LOCALEALPHA and natsort.compat.locale.dumb_sort(): alg |= NS_DUMB # Set some variables that will be passed to the factory functions if alg & ns.NUMAFTER: if alg & ns.LOCALEALPHA: sep = natsort.compat.locale.null_string_locale_max else: sep = natsort.compat.locale.null_string_max pre_sep = natsort.compat.locale.null_string_max else: if alg & ns.LOCALEALPHA: sep = natsort.compat.locale.null_string_locale else: sep = natsort.compat.locale.null_string pre_sep = natsort.compat.locale.null_string regex = utils.regex_chooser(alg) # Create the functions that will be used to split strings. input_transform = utils.input_string_transform_factory(alg) component_transform = utils.string_component_transform_factory(alg) final_transform = utils.final_data_transform_factory(alg, sep, pre_sep) # Create the high-level parsing functions for strings, bytes, and numbers. string_func = utils.parse_string_factory( alg, sep, regex.split, input_transform, component_transform, final_transform ) if alg & ns.PATH: string_func = utils.parse_path_factory(string_func) bytes_func = utils.parse_bytes_factory(alg) num_func = utils.parse_number_factory(alg, sep, pre_sep) # Return the natsort key with the parsing path pre-chosen. return partial( utils.natsort_key, key=key, string_func=string_func, bytes_func=bytes_func, num_func=num_func, )
0, module; 1, function_definition; 2, function_name:natsorted; 3, parameters; 4, block; 5, identifier:seq; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, expression_statement; 11, return_statement; 12, identifier:key; 13, None; 14, identifier:reverse; 15, False; 16, identifier:alg; 17, attribute; 18, comment:""" Sorts an iterable naturally. Parameters ---------- seq : iterable The input to sort. key : callable, optional A key used to determine how to sort each element of the iterable. It is **not** applied recursively. It should accept a single argument and return a single value. reverse : {{True, False}}, optional Return the list in reversed sorted order. The default is `False`. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.INT`. Returns ------- out: list The sorted input. See Also -------- natsort_keygen : Generates the key that makes natural sorting possible. realsorted : A wrapper for ``natsorted(seq, alg=ns.REAL)``. humansorted : A wrapper for ``natsorted(seq, alg=ns.LOCALE)``. index_natsorted : Returns the sorted indexes from `natsorted`. Examples -------- Use `natsorted` just like the builtin `sorted`:: >>> a = ['num3', 'num5', 'num2'] >>> natsorted(a) [{u}'num2', {u}'num3', {u}'num5'] """; 19, assignment; 20, call; 21, identifier:ns; 22, identifier:DEFAULT; 23, identifier:key; 24, call; 25, identifier:sorted; 26, argument_list; 27, identifier:natsort_keygen; 28, argument_list; 29, identifier:seq; 30, keyword_argument; 31, keyword_argument; 32, identifier:key; 33, identifier:alg; 34, identifier:reverse; 35, identifier:reverse; 36, identifier:key; 37, identifier:key
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 6, 12; 6, 13; 7, 14; 7, 15; 8, 16; 8, 17; 9, 18; 10, 19; 11, 20; 17, 21; 17, 22; 19, 23; 19, 24; 20, 25; 20, 26; 24, 27; 24, 28; 26, 29; 26, 30; 26, 31; 28, 32; 28, 33; 30, 34; 30, 35; 31, 36; 31, 37
def natsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ Sorts an iterable naturally. Parameters ---------- seq : iterable The input to sort. key : callable, optional A key used to determine how to sort each element of the iterable. It is **not** applied recursively. It should accept a single argument and return a single value. reverse : {{True, False}}, optional Return the list in reversed sorted order. The default is `False`. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.INT`. Returns ------- out: list The sorted input. See Also -------- natsort_keygen : Generates the key that makes natural sorting possible. realsorted : A wrapper for ``natsorted(seq, alg=ns.REAL)``. humansorted : A wrapper for ``natsorted(seq, alg=ns.LOCALE)``. index_natsorted : Returns the sorted indexes from `natsorted`. Examples -------- Use `natsorted` just like the builtin `sorted`:: >>> a = ['num3', 'num5', 'num2'] >>> natsorted(a) [{u}'num2', {u}'num3', {u}'num5'] """ key = natsort_keygen(key, alg) return sorted(seq, reverse=reverse, key=key)
0, module; 1, function_definition; 2, function_name:humansorted; 3, parameters; 4, block; 5, identifier:seq; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, return_statement; 11, identifier:key; 12, None; 13, identifier:reverse; 14, False; 15, identifier:alg; 16, attribute; 17, comment:""" Convenience function to properly sort non-numeric characters. This is a wrapper around ``natsorted(seq, alg=ns.LOCALE)``. Parameters ---------- seq : iterable The input to sort. key : callable, optional A key used to determine how to sort each element of the sequence. It is **not** applied recursively. It should accept a single argument and return a single value. reverse : {{True, False}}, optional Return the list in reversed sorted order. The default is `False`. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.LOCALE`. Returns ------- out : list The sorted input. See Also -------- index_humansorted : Returns the sorted indexes from `humansorted`. Notes ----- Please read :ref:`locale_issues` before using `humansorted`. Examples -------- Use `humansorted` just like the builtin `sorted`:: >>> a = ['Apple', 'Banana', 'apple', 'banana'] >>> natsorted(a) [{u}'Apple', {u}'Banana', {u}'apple', {u}'banana'] >>> humansorted(a) [{u}'apple', {u}'Apple', {u}'banana', {u}'Banana'] """; 18, call; 19, identifier:ns; 20, identifier:DEFAULT; 21, identifier:natsorted; 22, argument_list; 23, identifier:seq; 24, identifier:key; 25, identifier:reverse; 26, binary_operator:alg | ns.LOCALE; 27, identifier:alg; 28, attribute; 29, identifier:ns; 30, identifier:LOCALE
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 6, 11; 6, 12; 7, 13; 7, 14; 8, 15; 8, 16; 9, 17; 10, 18; 16, 19; 16, 20; 18, 21; 18, 22; 22, 23; 22, 24; 22, 25; 22, 26; 26, 27; 26, 28; 28, 29; 28, 30
def humansorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ Convenience function to properly sort non-numeric characters. This is a wrapper around ``natsorted(seq, alg=ns.LOCALE)``. Parameters ---------- seq : iterable The input to sort. key : callable, optional A key used to determine how to sort each element of the sequence. It is **not** applied recursively. It should accept a single argument and return a single value. reverse : {{True, False}}, optional Return the list in reversed sorted order. The default is `False`. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.LOCALE`. Returns ------- out : list The sorted input. See Also -------- index_humansorted : Returns the sorted indexes from `humansorted`. Notes ----- Please read :ref:`locale_issues` before using `humansorted`. Examples -------- Use `humansorted` just like the builtin `sorted`:: >>> a = ['Apple', 'Banana', 'apple', 'banana'] >>> natsorted(a) [{u}'Apple', {u}'Banana', {u}'apple', {u}'banana'] >>> humansorted(a) [{u}'apple', {u}'Apple', {u}'banana', {u}'Banana'] """ return natsorted(seq, key, reverse, alg | ns.LOCALE)
0, module; 1, function_definition; 2, function_name:realsorted; 3, parameters; 4, block; 5, identifier:seq; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, return_statement; 11, identifier:key; 12, None; 13, identifier:reverse; 14, False; 15, identifier:alg; 16, attribute; 17, comment:""" Convenience function to properly sort signed floats. A signed float in a string could be "a-5.7". This is a wrapper around ``natsorted(seq, alg=ns.REAL)``. The behavior of :func:`realsorted` for `natsort` version >= 4.0.0 was the default behavior of :func:`natsorted` for `natsort` version < 4.0.0. Parameters ---------- seq : iterable The input to sort. key : callable, optional A key used to determine how to sort each element of the sequence. It is **not** applied recursively. It should accept a single argument and return a single value. reverse : {{True, False}}, optional Return the list in reversed sorted order. The default is `False`. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.REAL`. Returns ------- out : list The sorted input. See Also -------- index_realsorted : Returns the sorted indexes from `realsorted`. Examples -------- Use `realsorted` just like the builtin `sorted`:: >>> a = ['num5.10', 'num-3', 'num5.3', 'num2'] >>> natsorted(a) [{u}'num2', {u}'num5.3', {u}'num5.10', {u}'num-3'] >>> realsorted(a) [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3'] """; 18, call; 19, identifier:ns; 20, identifier:DEFAULT; 21, identifier:natsorted; 22, argument_list; 23, identifier:seq; 24, identifier:key; 25, identifier:reverse; 26, binary_operator:alg | ns.REAL; 27, identifier:alg; 28, attribute; 29, identifier:ns; 30, identifier:REAL
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 6, 11; 6, 12; 7, 13; 7, 14; 8, 15; 8, 16; 9, 17; 10, 18; 16, 19; 16, 20; 18, 21; 18, 22; 22, 23; 22, 24; 22, 25; 22, 26; 26, 27; 26, 28; 28, 29; 28, 30
def realsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ Convenience function to properly sort signed floats. A signed float in a string could be "a-5.7". This is a wrapper around ``natsorted(seq, alg=ns.REAL)``. The behavior of :func:`realsorted` for `natsort` version >= 4.0.0 was the default behavior of :func:`natsorted` for `natsort` version < 4.0.0. Parameters ---------- seq : iterable The input to sort. key : callable, optional A key used to determine how to sort each element of the sequence. It is **not** applied recursively. It should accept a single argument and return a single value. reverse : {{True, False}}, optional Return the list in reversed sorted order. The default is `False`. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.REAL`. Returns ------- out : list The sorted input. See Also -------- index_realsorted : Returns the sorted indexes from `realsorted`. Examples -------- Use `realsorted` just like the builtin `sorted`:: >>> a = ['num5.10', 'num-3', 'num5.3', 'num2'] >>> natsorted(a) [{u}'num2', {u}'num5.3', {u}'num5.10', {u}'num-3'] >>> realsorted(a) [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3'] """ return natsorted(seq, key, reverse, alg | ns.REAL)
0, module; 1, function_definition; 2, function_name:index_natsorted; 3, parameters; 4, block; 5, identifier:seq; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, comment:# Pair the index and sequence together, then sort by element; 12, expression_statement; 13, expression_statement; 14, return_statement; 15, identifier:key; 16, None; 17, identifier:reverse; 18, False; 19, identifier:alg; 20, attribute; 21, comment:""" Determine the list of the indexes used to sort the input sequence. Sorts a sequence naturally, but returns a list of sorted the indexes and not the sorted list itself. This list of indexes can be used to sort multiple lists by the sorted order of the given sequence. Parameters ---------- seq : iterable The input to sort. key : callable, optional A key used to determine how to sort each element of the sequence. It is **not** applied recursively. It should accept a single argument and return a single value. reverse : {{True, False}}, optional Return the list in reversed sorted order. The default is `False`. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.INT`. Returns ------- out : tuple The ordered indexes of the input. See Also -------- natsorted order_by_index Examples -------- Use index_natsorted if you want to sort multiple lists by the sorted order of one list:: >>> a = ['num3', 'num5', 'num2'] >>> b = ['foo', 'bar', 'baz'] >>> index = index_natsorted(a) >>> index [2, 0, 1] >>> # Sort both lists by the sort order of a >>> order_by_index(a, index) [{u}'num2', {u}'num3', {u}'num5'] >>> order_by_index(b, index) [{u}'baz', {u}'foo', {u}'bar'] """; 22, comparison_operator:key is None; 23, block; 24, else_clause; 25, assignment; 26, call; 27, list_comprehension; 28, identifier:ns; 29, identifier:DEFAULT; 30, identifier:key; 31, None; 32, expression_statement; 33, block; 34, identifier:index_seq_pair; 35, list_comprehension; 36, attribute; 37, argument_list; 38, identifier:x; 39, for_in_clause; 40, assignment; 41, function_definition; 42, list; 43, for_in_clause; 44, identifier:index_seq_pair; 45, identifier:sort; 46, keyword_argument; 47, keyword_argument; 48, pattern_list; 49, identifier:index_seq_pair; 50, identifier:newkey; 51, call; 52, function_name:newkey; 53, parameters; 54, block; 55, identifier:x; 56, identifier:y; 57, pattern_list; 58, call; 59, identifier:reverse; 60, identifier:reverse; 61, identifier:key; 62, call; 63, identifier:x; 64, identifier:_; 65, identifier:itemgetter; 66, argument_list; 67, identifier:x; 68, return_statement; 69, identifier:x; 70, identifier:y; 71, identifier:enumerate; 72, argument_list; 73, identifier:natsort_keygen; 74, argument_list; 75, integer:1; 76, call; 77, identifier:seq; 78, identifier:newkey; 79, identifier:alg; 80, identifier:key; 81, argument_list; 82, call; 83, call; 84, argument_list; 85, identifier:itemgetter; 86, argument_list; 87, identifier:x; 88, integer:1
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 6, 15; 6, 16; 7, 17; 7, 18; 8, 19; 8, 20; 9, 21; 10, 22; 10, 23; 10, 24; 12, 25; 13, 26; 14, 27; 20, 28; 20, 29; 22, 30; 22, 31; 23, 32; 24, 33; 25, 34; 25, 35; 26, 36; 26, 37; 27, 38; 27, 39; 32, 40; 33, 41; 35, 42; 35, 43; 36, 44; 36, 45; 37, 46; 37, 47; 39, 48; 39, 49; 40, 50; 40, 51; 41, 52; 41, 53; 41, 54; 42, 55; 42, 56; 43, 57; 43, 58; 46, 59; 46, 60; 47, 61; 47, 62; 48, 63; 48, 64; 51, 65; 51, 66; 53, 67; 54, 68; 57, 69; 57, 70; 58, 71; 58, 72; 62, 73; 62, 74; 66, 75; 68, 76; 72, 77; 74, 78; 74, 79; 76, 80; 76, 81; 81, 82; 82, 83; 82, 84; 83, 85; 83, 86; 84, 87; 86, 88
def index_natsorted(seq, key=None, reverse=False, alg=ns.DEFAULT): """ Determine the list of the indexes used to sort the input sequence. Sorts a sequence naturally, but returns a list of sorted the indexes and not the sorted list itself. This list of indexes can be used to sort multiple lists by the sorted order of the given sequence. Parameters ---------- seq : iterable The input to sort. key : callable, optional A key used to determine how to sort each element of the sequence. It is **not** applied recursively. It should accept a single argument and return a single value. reverse : {{True, False}}, optional Return the list in reversed sorted order. The default is `False`. alg : ns enum, optional This option is used to control which algorithm `natsort` uses when sorting. For details into these options, please see the :class:`ns` class documentation. The default is `ns.INT`. Returns ------- out : tuple The ordered indexes of the input. See Also -------- natsorted order_by_index Examples -------- Use index_natsorted if you want to sort multiple lists by the sorted order of one list:: >>> a = ['num3', 'num5', 'num2'] >>> b = ['foo', 'bar', 'baz'] >>> index = index_natsorted(a) >>> index [2, 0, 1] >>> # Sort both lists by the sort order of a >>> order_by_index(a, index) [{u}'num2', {u}'num3', {u}'num5'] >>> order_by_index(b, index) [{u}'baz', {u}'foo', {u}'bar'] """ if key is None: newkey = itemgetter(1) else: def newkey(x): return key(itemgetter(1)(x)) # Pair the index and sequence together, then sort by element index_seq_pair = [[x, y] for x, y in enumerate(seq)] index_seq_pair.sort(reverse=reverse, key=natsort_keygen(newkey, alg)) return [x for x, _ in index_seq_pair]
0, module; 1, function_definition; 2, function_name:order_by_index; 3, parameters; 4, block; 5, identifier:seq; 6, identifier:index; 7, default_parameter; 8, expression_statement; 9, return_statement; 10, identifier:iter; 11, False; 12, comment:""" Order a given sequence by an index sequence. The output of `index_natsorted` is a sequence of integers (index) that correspond to how its input sequence **would** be sorted. The idea is that this index can be used to reorder multiple sequences by the sorted order of the first sequence. This function is a convenient wrapper to apply this ordering to a sequence. Parameters ---------- seq : sequence The sequence to order. index : iterable The iterable that indicates how to order `seq`. It should be the same length as `seq` and consist of integers only. iter : {{True, False}}, optional If `True`, the ordered sequence is returned as a iterator; otherwise it is returned as a list. The default is `False`. Returns ------- out : {{list, iterator}} The sequence ordered by `index`, as a `list` or as an iterator (depending on the value of `iter`). See Also -------- index_natsorted index_humansorted index_realsorted Examples -------- `order_by_index` is a convenience function that helps you apply the result of `index_natsorted`:: >>> a = ['num3', 'num5', 'num2'] >>> b = ['foo', 'bar', 'baz'] >>> index = index_natsorted(a) >>> index [2, 0, 1] >>> # Sort both lists by the sort order of a >>> order_by_index(a, index) [{u}'num2', {u}'num3', {u}'num5'] >>> order_by_index(b, index) [{u}'baz', {u}'foo', {u}'bar'] """; 13, conditional_expression:(seq[i] for i in index) if iter else [seq[i] for i in index]; 14, generator_expression; 15, identifier:iter; 16, list_comprehension; 17, subscript; 18, for_in_clause; 19, subscript; 20, for_in_clause; 21, identifier:seq; 22, identifier:i; 23, identifier:i; 24, identifier:index; 25, identifier:seq; 26, identifier:i; 27, identifier:i; 28, identifier:index
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 7, 10; 7, 11; 8, 12; 9, 13; 13, 14; 13, 15; 13, 16; 14, 17; 14, 18; 16, 19; 16, 20; 17, 21; 17, 22; 18, 23; 18, 24; 19, 25; 19, 26; 20, 27; 20, 28
def order_by_index(seq, index, iter=False): """ Order a given sequence by an index sequence. The output of `index_natsorted` is a sequence of integers (index) that correspond to how its input sequence **would** be sorted. The idea is that this index can be used to reorder multiple sequences by the sorted order of the first sequence. This function is a convenient wrapper to apply this ordering to a sequence. Parameters ---------- seq : sequence The sequence to order. index : iterable The iterable that indicates how to order `seq`. It should be the same length as `seq` and consist of integers only. iter : {{True, False}}, optional If `True`, the ordered sequence is returned as a iterator; otherwise it is returned as a list. The default is `False`. Returns ------- out : {{list, iterator}} The sequence ordered by `index`, as a `list` or as an iterator (depending on the value of `iter`). See Also -------- index_natsorted index_humansorted index_realsorted Examples -------- `order_by_index` is a convenience function that helps you apply the result of `index_natsorted`:: >>> a = ['num3', 'num5', 'num2'] >>> b = ['foo', 'bar', 'baz'] >>> index = index_natsorted(a) >>> index [2, 0, 1] >>> # Sort both lists by the sort order of a >>> order_by_index(a, index) [{u}'num2', {u}'num3', {u}'num5'] >>> order_by_index(b, index) [{u}'baz', {u}'foo', {u}'bar'] """ return (seq[i] for i in index) if iter else [seq[i] for i in index]
0, module; 1, function_definition; 2, function_name:find_records; 3, parameters; 4, block; 5, identifier:self; 6, identifier:collection_name; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, expression_statement; 13, expression_statement; 14, if_statement; 15, expression_statement; 16, if_statement; 17, return_statement; 18, identifier:query; 19, dictionary; 20, identifier:sort_by; 21, None; 22, identifier:sort_direction; 23, None; 24, identifier:start; 25, integer:0; 26, identifier:limit; 27, None; 28, comment:""" Return a cursor of records from the given MongoDB collection. :param collection_name: Name of the MongoDB collection to query. :param query: Standard MongoDB query. By default no restriction. :param sort_by: Name of a single field to sort by. :param sort_direction: The direction to sort, "asc" or "desc". :param start: Skip first n results. :param limit: The maximum number of results to return. :return: Cursor -- An iterable with results. :raise DataSourceError """; 29, assignment; 30, comparison_operator:sort_by is not None; 31, block; 32, assignment; 33, comparison_operator:limit is not None; 34, block; 35, call; 36, identifier:cursor; 37, call; 38, identifier:sort_by; 39, None; 40, expression_statement; 41, identifier:cursor; 42, call; 43, identifier:limit; 44, None; 45, expression_statement; 46, identifier:MongoDbCursor; 47, argument_list; 48, attribute; 49, argument_list; 50, assignment; 51, attribute; 52, argument_list; 53, assignment; 54, identifier:cursor; 55, call; 56, identifier:find; 57, identifier:query; 58, identifier:cursor; 59, call; 60, identifier:cursor; 61, identifier:skip; 62, identifier:start; 63, identifier:cursor; 64, call; 65, attribute; 66, argument_list; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, identifier:self; 72, identifier:_get_collection; 73, identifier:collection_name; 74, identifier:self; 75, identifier:_apply_sort; 76, identifier:cursor; 77, identifier:sort_by; 78, identifier:sort_direction; 79, identifier:cursor; 80, identifier:limit; 81, identifier:limit
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 7, 18; 7, 19; 8, 20; 8, 21; 9, 22; 9, 23; 10, 24; 10, 25; 11, 26; 11, 27; 12, 28; 13, 29; 14, 30; 14, 31; 15, 32; 16, 33; 16, 34; 17, 35; 29, 36; 29, 37; 30, 38; 30, 39; 31, 40; 32, 41; 32, 42; 33, 43; 33, 44; 34, 45; 35, 46; 35, 47; 37, 48; 37, 49; 40, 50; 42, 51; 42, 52; 45, 53; 47, 54; 48, 55; 48, 56; 49, 57; 50, 58; 50, 59; 51, 60; 51, 61; 52, 62; 53, 63; 53, 64; 55, 65; 55, 66; 59, 67; 59, 68; 64, 69; 64, 70; 65, 71; 65, 72; 66, 73; 67, 74; 67, 75; 68, 76; 68, 77; 68, 78; 69, 79; 69, 80; 70, 81
def find_records(self, collection_name, query={}, sort_by=None, sort_direction=None, start=0, limit=None): """ Return a cursor of records from the given MongoDB collection. :param collection_name: Name of the MongoDB collection to query. :param query: Standard MongoDB query. By default no restriction. :param sort_by: Name of a single field to sort by. :param sort_direction: The direction to sort, "asc" or "desc". :param start: Skip first n results. :param limit: The maximum number of results to return. :return: Cursor -- An iterable with results. :raise DataSourceError """ cursor = self._get_collection(collection_name).find(query) if sort_by is not None: cursor = self._apply_sort(cursor, sort_by, sort_direction) cursor = cursor.skip(start) if limit is not None: cursor = cursor.limit(limit) return MongoDbCursor(cursor)
0, module; 1, function_definition; 2, function_name:_apply_sort; 3, parameters; 4, block; 5, identifier:cursor; 6, identifier:sort_by; 7, identifier:sort_direction; 8, expression_statement; 9, if_statement; 10, return_statement; 11, comment:""" Apply sort to a cursor. :param cursor: The cursor to apply sort on. :param sort_by: The field name to sort by. :param sort_direction: The direction to sort, "asc" or "desc". :return: """; 12, boolean_operator; 13, block; 14, else_clause; 15, call; 16, comparison_operator:sort_direction is not None; 17, comparison_operator:sort_direction.lower() == "desc"; 18, expression_statement; 19, block; 20, attribute; 21, argument_list; 22, identifier:sort_direction; 23, None; 24, call; 25, string:"desc"; 26, assignment; 27, expression_statement; 28, identifier:cursor; 29, identifier:sort; 30, identifier:sort_by; 31, identifier:sort; 32, attribute; 33, argument_list; 34, identifier:sort; 35, attribute; 36, assignment; 37, identifier:sort_direction; 38, identifier:lower; 39, identifier:pymongo; 40, identifier:DESCENDING; 41, identifier:sort; 42, attribute; 43, identifier:pymongo; 44, identifier:ASCENDING
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 8, 11; 9, 12; 9, 13; 9, 14; 10, 15; 12, 16; 12, 17; 13, 18; 14, 19; 15, 20; 15, 21; 16, 22; 16, 23; 17, 24; 17, 25; 18, 26; 19, 27; 20, 28; 20, 29; 21, 30; 21, 31; 24, 32; 24, 33; 26, 34; 26, 35; 27, 36; 32, 37; 32, 38; 35, 39; 35, 40; 36, 41; 36, 42; 42, 43; 42, 44
def _apply_sort(cursor, sort_by, sort_direction): """ Apply sort to a cursor. :param cursor: The cursor to apply sort on. :param sort_by: The field name to sort by. :param sort_direction: The direction to sort, "asc" or "desc". :return: """ if sort_direction is not None and sort_direction.lower() == "desc": sort = pymongo.DESCENDING else: sort = pymongo.ASCENDING return cursor.sort(sort_by, sort)
0, module; 1, function_definition; 2, function_name:get_runs; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, expression_statement; 12, expression_statement; 13, function_definition; 14, expression_statement; 15, return_statement; 16, identifier:sort_by; 17, None; 18, identifier:sort_direction; 19, None; 20, identifier:start; 21, integer:0; 22, identifier:limit; 23, None; 24, identifier:query; 25, dictionary; 26, comment:""" Return all runs in the file store. If a run is corrupt, e.g. missing files, it is skipped. :param sort_by: NotImplemented :param sort_direction: NotImplemented :param start: NotImplemented :param limit: NotImplemented :param query: NotImplemented :return: FileStoreCursor """; 27, assignment; 28, function_name:run_iterator; 29, parameters; 30, block; 31, assignment; 32, call; 33, pair; 34, pair; 35, identifier:all_run_ids; 36, call; 37, expression_statement; 38, for_statement; 39, identifier:count; 40, call; 41, identifier:FileStoreCursor; 42, argument_list; 43, string:"type"; 44, string:"and"; 45, string:"filters"; 46, list; 47, attribute; 48, argument_list; 49, assignment; 50, identifier:id; 51, identifier:all_run_ids; 52, block; 53, identifier:len; 54, argument_list; 55, identifier:count; 56, call; 57, identifier:os; 58, identifier:listdir; 59, attribute; 60, identifier:blacklist; 61, call; 62, if_statement; 63, try_statement; 64, identifier:all_run_ids; 65, identifier:run_iterator; 66, argument_list; 67, identifier:self; 68, identifier:directory; 69, identifier:set; 70, argument_list; 71, comparison_operator:id in blacklist; 72, block; 73, block; 74, except_clause; 75, list; 76, identifier:id; 77, identifier:blacklist; 78, continue_statement; 79, expression_statement; 80, identifier:FileNotFoundError; 81, comment:# An incomplete experiment is a corrupt experiment.; 82, comment:# Skip it for now.; 83, comment:# TODO; 84, block; 85, string:"_sources"; 86, yield; 87, pass_statement; 88, call; 89, attribute; 90, argument_list; 91, identifier:self; 92, identifier:get; 93, identifier:id
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 6, 16; 6, 17; 7, 18; 7, 19; 8, 20; 8, 21; 9, 22; 9, 23; 10, 24; 10, 25; 11, 26; 12, 27; 13, 28; 13, 29; 13, 30; 14, 31; 15, 32; 25, 33; 25, 34; 27, 35; 27, 36; 30, 37; 30, 38; 31, 39; 31, 40; 32, 41; 32, 42; 33, 43; 33, 44; 34, 45; 34, 46; 36, 47; 36, 48; 37, 49; 38, 50; 38, 51; 38, 52; 40, 53; 40, 54; 42, 55; 42, 56; 47, 57; 47, 58; 48, 59; 49, 60; 49, 61; 52, 62; 52, 63; 54, 64; 56, 65; 56, 66; 59, 67; 59, 68; 61, 69; 61, 70; 62, 71; 62, 72; 63, 73; 63, 74; 70, 75; 71, 76; 71, 77; 72, 78; 73, 79; 74, 80; 74, 81; 74, 82; 74, 83; 74, 84; 75, 85; 79, 86; 84, 87; 86, 88; 88, 89; 88, 90; 89, 91; 89, 92; 90, 93
def get_runs(self, sort_by=None, sort_direction=None, start=0, limit=None, query={"type": "and", "filters": []}): """ Return all runs in the file store. If a run is corrupt, e.g. missing files, it is skipped. :param sort_by: NotImplemented :param sort_direction: NotImplemented :param start: NotImplemented :param limit: NotImplemented :param query: NotImplemented :return: FileStoreCursor """ all_run_ids = os.listdir(self.directory) def run_iterator(): blacklist = set(["_sources"]) for id in all_run_ids: if id in blacklist: continue try: yield self.get(id) except FileNotFoundError: # An incomplete experiment is a corrupt experiment. # Skip it for now. # TODO pass count = len(all_run_ids) return FileStoreCursor(count, run_iterator())
0, module; 1, function_definition; 2, function_name:get_runs; 3, parameters; 4, block; 5, expression_statement; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, if_statement; 15, expression_statement; 16, comment:# records_total should be the total size of the records in the database,; 17, comment:# not what was returned; 18, expression_statement; 19, expression_statement; 20, return_statement; 21, comment:"""Get all runs, sort it and return a response."""; 22, assignment; 23, assignment; 24, assignment; 25, assignment; 26, assignment; 27, assignment; 28, assignment; 29, assignment; 30, comparison_operator:order_column is not None; 31, block; 32, assignment; 33, assignment; 34, assignment; 35, call; 36, identifier:data; 37, subscript; 38, identifier:draw; 39, call; 40, identifier:start; 41, call; 42, identifier:length; 43, call; 44, identifier:length; 45, conditional_expression:length if length >= 0 else None; 46, identifier:order_column; 47, call; 48, identifier:order_dir; 49, call; 50, identifier:query; 51, call; 52, identifier:order_column; 53, None; 54, expression_statement; 55, if_statement; 56, identifier:runs; 57, call; 58, identifier:records_total; 59, call; 60, identifier:records_filtered; 61, call; 62, identifier:Response; 63, argument_list; 64, attribute; 65, string:"data"; 66, identifier:parse_int_arg; 67, argument_list; 68, identifier:parse_int_arg; 69, argument_list; 70, identifier:parse_int_arg; 71, argument_list; 72, identifier:length; 73, comparison_operator:length >= 0; 74, None; 75, attribute; 76, argument_list; 77, attribute; 78, argument_list; 79, identifier:parse_query_filter; 80, argument_list; 81, assignment; 82, comparison_operator:order_column == "hostname"; 83, block; 84, attribute; 85, argument_list; 86, attribute; 87, argument_list; 88, attribute; 89, argument_list; 90, call; 91, keyword_argument; 92, identifier:current_app; 93, identifier:config; 94, string:"draw"; 95, integer:1; 96, string:"start"; 97, integer:0; 98, string:"length"; 99, unary_operator; 100, identifier:length; 101, integer:0; 102, attribute; 103, identifier:get; 104, string:"order[0][column]"; 105, attribute; 106, identifier:get; 107, string:"order[0][dir]"; 108, identifier:order_column; 109, line_continuation:\; 110, call; 111, identifier:order_column; 112, string:"hostname"; 113, expression_statement; 114, call; 115, identifier:get_runs; 116, keyword_argument; 117, keyword_argument; 118, keyword_argument; 119, keyword_argument; 120, keyword_argument; 121, identifier:runs; 122, identifier:count; 123, identifier:runs; 124, identifier:count; 125, identifier:render_template; 126, argument_list; 127, identifier:mimetype; 128, string:"application/json"; 129, integer:1; 130, identifier:request; 131, identifier:args; 132, identifier:request; 133, identifier:args; 134, attribute; 135, argument_list; 136, assignment; 137, attribute; 138, argument_list; 139, identifier:start; 140, identifier:start; 141, identifier:limit; 142, identifier:length; 143, identifier:sort_by; 144, identifier:order_column; 145, identifier:sort_direction; 146, identifier:order_dir; 147, identifier:query; 148, identifier:query; 149, string:"api/runs.js"; 150, keyword_argument; 151, keyword_argument; 152, keyword_argument; 153, keyword_argument; 154, attribute; 155, identifier:get; 156, binary_operator:"columns[%d][name]" % int(order_column); 157, identifier:order_column; 158, string:"host.hostname"; 159, identifier:data; 160, identifier:get_run_dao; 161, identifier:runs; 162, identifier:runs; 163, identifier:draw; 164, identifier:draw; 165, identifier:recordsTotal; 166, identifier:records_total; 167, identifier:recordsFiltered; 168, identifier:records_filtered; 169, identifier:request; 170, identifier:args; 171, string:"columns[%d][name]"; 172, call; 173, identifier:int; 174, argument_list; 175, identifier:order_column
0, 1; 1, 2; 1, 3; 1, 4; 4, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 5, 21; 6, 22; 7, 23; 8, 24; 9, 25; 10, 26; 11, 27; 12, 28; 13, 29; 14, 30; 14, 31; 15, 32; 18, 33; 19, 34; 20, 35; 22, 36; 22, 37; 23, 38; 23, 39; 24, 40; 24, 41; 25, 42; 25, 43; 26, 44; 26, 45; 27, 46; 27, 47; 28, 48; 28, 49; 29, 50; 29, 51; 30, 52; 30, 53; 31, 54; 31, 55; 32, 56; 32, 57; 33, 58; 33, 59; 34, 60; 34, 61; 35, 62; 35, 63; 37, 64; 37, 65; 39, 66; 39, 67; 41, 68; 41, 69; 43, 70; 43, 71; 45, 72; 45, 73; 45, 74; 47, 75; 47, 76; 49, 77; 49, 78; 51, 79; 51, 80; 54, 81; 55, 82; 55, 83; 57, 84; 57, 85; 59, 86; 59, 87; 61, 88; 61, 89; 63, 90; 63, 91; 64, 92; 64, 93; 67, 94; 67, 95; 69, 96; 69, 97; 71, 98; 71, 99; 73, 100; 73, 101; 75, 102; 75, 103; 76, 104; 77, 105; 77, 106; 78, 107; 81, 108; 81, 109; 81, 110; 82, 111; 82, 112; 83, 113; 84, 114; 84, 115; 85, 116; 85, 117; 85, 118; 85, 119; 85, 120; 86, 121; 86, 122; 88, 123; 88, 124; 90, 125; 90, 126; 91, 127; 91, 128; 99, 129; 102, 130; 102, 131; 105, 132; 105, 133; 110, 134; 110, 135; 113, 136; 114, 137; 114, 138; 116, 139; 116, 140; 117, 141; 117, 142; 118, 143; 118, 144; 119, 145; 119, 146; 120, 147; 120, 148; 126, 149; 126, 150; 126, 151; 126, 152; 126, 153; 134, 154; 134, 155; 135, 156; 136, 157; 136, 158; 137, 159; 137, 160; 150, 161; 150, 162; 151, 163; 151, 164; 152, 165; 152, 166; 153, 167; 153, 168; 154, 169; 154, 170; 156, 171; 156, 172; 172, 173; 172, 174; 174, 175
def get_runs(): """Get all runs, sort it and return a response.""" data = current_app.config["data"] draw = parse_int_arg("draw", 1) start = parse_int_arg("start", 0) length = parse_int_arg("length", -1) length = length if length >= 0 else None order_column = request.args.get("order[0][column]") order_dir = request.args.get("order[0][dir]") query = parse_query_filter() if order_column is not None: order_column = \ request.args.get("columns[%d][name]" % int(order_column)) if order_column == "hostname": order_column = "host.hostname" runs = data.get_run_dao().get_runs( start=start, limit=length, sort_by=order_column, sort_direction=order_dir, query=query) # records_total should be the total size of the records in the database, # not what was returned records_total = runs.count() records_filtered = runs.count() return Response(render_template( "api/runs.js", runs=runs, draw=draw, recordsTotal=records_total, recordsFiltered=records_filtered), mimetype="application/json")
0, module; 1, function_definition; 2, function_name:generate_querystring; 3, parameters; 4, block; 5, identifier:params; 6, expression_statement; 7, if_statement; 8, expression_statement; 9, for_statement; 10, if_statement; 11, comment:""" Generate a querystring suitable for use in the v2 api. The Requests library doesn't know how to generate querystrings that encode dictionaries using square brackets: https://api.mollie.com/v2/methods?amount[value]=100.00&amount[currency]=USD Note: we use `sorted()` to work around a difference in iteration behaviour between Python 2 and 3. This makes the output predictable, and ordering of querystring parameters shouldn't matter. """; 12, not_operator; 13, block; 14, assignment; 15, pattern_list; 16, call; 17, block; 18, identifier:parts; 19, block; 20, identifier:params; 21, return_statement; 22, identifier:parts; 23, list; 24, identifier:param; 25, identifier:value; 26, identifier:sorted; 27, argument_list; 28, if_statement; 29, return_statement; 30, None; 31, call; 32, not_operator; 33, block; 34, else_clause; 35, call; 36, attribute; 37, argument_list; 38, call; 39, expression_statement; 40, comment:# encode dictionary with square brackets; 41, block; 42, attribute; 43, argument_list; 44, identifier:params; 45, identifier:items; 46, identifier:isinstance; 47, argument_list; 48, call; 49, for_statement; 50, string; 51, identifier:join; 52, identifier:parts; 53, identifier:value; 54, identifier:dict; 55, attribute; 56, argument_list; 57, pattern_list; 58, call; 59, block; 60, string_content:&; 61, identifier:parts; 62, identifier:append; 63, call; 64, identifier:key; 65, identifier:sub_value; 66, identifier:sorted; 67, argument_list; 68, expression_statement; 69, expression_statement; 70, identifier:urlencode; 71, argument_list; 72, call; 73, assignment; 74, call; 75, dictionary; 76, attribute; 77, argument_list; 78, identifier:composed; 79, call; 80, attribute; 81, argument_list; 82, pair; 83, identifier:value; 84, identifier:items; 85, attribute; 86, argument_list; 87, identifier:parts; 88, identifier:append; 89, call; 90, identifier:param; 91, identifier:value; 92, string; 93, identifier:format; 94, keyword_argument; 95, keyword_argument; 96, identifier:urlencode; 97, argument_list; 98, string_content:{param}[{key}]; 99, identifier:param; 100, identifier:param; 101, identifier:key; 102, identifier:key; 103, dictionary; 104, pair; 105, identifier:composed; 106, identifier:sub_value
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 7, 12; 7, 13; 8, 14; 9, 15; 9, 16; 9, 17; 10, 18; 10, 19; 12, 20; 13, 21; 14, 22; 14, 23; 15, 24; 15, 25; 16, 26; 16, 27; 17, 28; 19, 29; 21, 30; 27, 31; 28, 32; 28, 33; 28, 34; 29, 35; 31, 36; 31, 37; 32, 38; 33, 39; 34, 40; 34, 41; 35, 42; 35, 43; 36, 44; 36, 45; 38, 46; 38, 47; 39, 48; 41, 49; 42, 50; 42, 51; 43, 52; 47, 53; 47, 54; 48, 55; 48, 56; 49, 57; 49, 58; 49, 59; 50, 60; 55, 61; 55, 62; 56, 63; 57, 64; 57, 65; 58, 66; 58, 67; 59, 68; 59, 69; 63, 70; 63, 71; 67, 72; 68, 73; 69, 74; 71, 75; 72, 76; 72, 77; 73, 78; 73, 79; 74, 80; 74, 81; 75, 82; 76, 83; 76, 84; 79, 85; 79, 86; 80, 87; 80, 88; 81, 89; 82, 90; 82, 91; 85, 92; 85, 93; 86, 94; 86, 95; 89, 96; 89, 97; 92, 98; 94, 99; 94, 100; 95, 101; 95, 102; 97, 103; 103, 104; 104, 105; 104, 106
def generate_querystring(params): """ Generate a querystring suitable for use in the v2 api. The Requests library doesn't know how to generate querystrings that encode dictionaries using square brackets: https://api.mollie.com/v2/methods?amount[value]=100.00&amount[currency]=USD Note: we use `sorted()` to work around a difference in iteration behaviour between Python 2 and 3. This makes the output predictable, and ordering of querystring parameters shouldn't matter. """ if not params: return None parts = [] for param, value in sorted(params.items()): if not isinstance(value, dict): parts.append(urlencode({param: value})) else: # encode dictionary with square brackets for key, sub_value in sorted(value.items()): composed = '{param}[{key}]'.format(param=param, key=key) parts.append(urlencode({composed: sub_value})) if parts: return '&'.join(parts)
0, module; 1, function_definition; 2, function_name:filter_catalog; 3, parameters; 4, block; 5, identifier:catalog; 6, dictionary_splat_pattern; 7, expression_statement; 8, comment:# interpret input pars; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, comment:# sort by magnitude; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, if_statement; 18, comment:# apply limits, insuring no more than full catalog gets selected; 19, expression_statement; 20, expression_statement; 21, comment:# Extract sources identified by selection; 22, expression_statement; 23, return_statement; 24, identifier:kwargs; 25, comment:""" Create a new catalog selected from input based on photometry. Parameters ---------- bright_limit : float Fraction of catalog based on brightness that should be retained. Value of 1.00 means full catalog. max_bright : int Maximum number of sources to keep regardless of `bright_limit`. min_bright : int Minimum number of sources to keep regardless of `bright_limit`. colname : str Name of column to use for selection/sorting. Returns ------- new_catalog : `~astropy.table.Table` New table which only has the sources that meet the selection criteria. """; 26, assignment; 27, assignment; 28, assignment; 29, assignment; 30, assignment; 31, assignment; 32, assignment; 33, comparison_operator:max_bright is None; 34, block; 35, assignment; 36, assignment; 37, assignment; 38, identifier:new_catalog; 39, identifier:bright_limit; 40, call; 41, identifier:max_bright; 42, call; 43, identifier:min_bright; 44, call; 45, identifier:colname; 46, call; 47, identifier:phot_column; 48, subscript; 49, identifier:num_sources; 50, call; 51, identifier:sort_indx; 52, call; 53, identifier:max_bright; 54, None; 55, expression_statement; 56, identifier:limit_num; 57, call; 58, identifier:limit_num; 59, call; 60, identifier:new_catalog; 61, subscript; 62, attribute; 63, argument_list; 64, attribute; 65, argument_list; 66, attribute; 67, argument_list; 68, attribute; 69, argument_list; 70, identifier:catalog; 71, identifier:colname; 72, identifier:len; 73, argument_list; 74, attribute; 75, argument_list; 76, assignment; 77, identifier:max; 78, argument_list; 79, identifier:min; 80, argument_list; 81, identifier:catalog; 82, subscript; 83, identifier:kwargs; 84, identifier:get; 85, string; 86, float:1.00; 87, identifier:kwargs; 88, identifier:get; 89, string; 90, None; 91, identifier:kwargs; 92, identifier:get; 93, string; 94, integer:20; 95, identifier:kwargs; 96, identifier:get; 97, string; 98, string; 99, identifier:phot_column; 100, identifier:np; 101, identifier:argsort; 102, identifier:phot_column; 103, identifier:max_bright; 104, identifier:num_sources; 105, call; 106, identifier:min_bright; 107, identifier:max_bright; 108, identifier:limit_num; 109, identifier:num_sources; 110, identifier:sort_indx; 111, slice; 112, string_content:bright_limit; 113, string_content:max_bright; 114, string_content:min_bright; 115, string_content:colname; 116, string_content:vegamag; 117, identifier:int; 118, argument_list; 119, identifier:limit_num; 120, binary_operator:num_sources * bright_limit; 121, identifier:num_sources; 122, identifier:bright_limit
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 6, 24; 7, 25; 9, 26; 10, 27; 11, 28; 12, 29; 14, 30; 15, 31; 16, 32; 17, 33; 17, 34; 19, 35; 20, 36; 22, 37; 23, 38; 26, 39; 26, 40; 27, 41; 27, 42; 28, 43; 28, 44; 29, 45; 29, 46; 30, 47; 30, 48; 31, 49; 31, 50; 32, 51; 32, 52; 33, 53; 33, 54; 34, 55; 35, 56; 35, 57; 36, 58; 36, 59; 37, 60; 37, 61; 40, 62; 40, 63; 42, 64; 42, 65; 44, 66; 44, 67; 46, 68; 46, 69; 48, 70; 48, 71; 50, 72; 50, 73; 52, 74; 52, 75; 55, 76; 57, 77; 57, 78; 59, 79; 59, 80; 61, 81; 61, 82; 62, 83; 62, 84; 63, 85; 63, 86; 64, 87; 64, 88; 65, 89; 65, 90; 66, 91; 66, 92; 67, 93; 67, 94; 68, 95; 68, 96; 69, 97; 69, 98; 73, 99; 74, 100; 74, 101; 75, 102; 76, 103; 76, 104; 78, 105; 78, 106; 80, 107; 80, 108; 80, 109; 82, 110; 82, 111; 85, 112; 89, 113; 93, 114; 97, 115; 98, 116; 105, 117; 105, 118; 111, 119; 118, 120; 120, 121; 120, 122
def filter_catalog(catalog, **kwargs): """ Create a new catalog selected from input based on photometry. Parameters ---------- bright_limit : float Fraction of catalog based on brightness that should be retained. Value of 1.00 means full catalog. max_bright : int Maximum number of sources to keep regardless of `bright_limit`. min_bright : int Minimum number of sources to keep regardless of `bright_limit`. colname : str Name of column to use for selection/sorting. Returns ------- new_catalog : `~astropy.table.Table` New table which only has the sources that meet the selection criteria. """ # interpret input pars bright_limit = kwargs.get('bright_limit', 1.00) max_bright = kwargs.get('max_bright', None) min_bright = kwargs.get('min_bright', 20) colname = kwargs.get('colname', 'vegamag') # sort by magnitude phot_column = catalog[colname] num_sources = len(phot_column) sort_indx = np.argsort(phot_column) if max_bright is None: max_bright = num_sources # apply limits, insuring no more than full catalog gets selected limit_num = max(int(num_sources * bright_limit), min_bright) limit_num = min(max_bright, limit_num, num_sources) # Extract sources identified by selection new_catalog = catalog[sort_indx[:limit_num]] return new_catalog
0, module; 1, function_definition; 2, function_name:sort_timeseries; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, comment:# the time series is sorted by default; 9, if_statement; 10, expression_statement; 11, if_statement; 12, expression_statement; 13, expression_statement; 14, return_statement; 15, identifier:ascending; 16, True; 17, comment:"""Sorts the data points within the TimeSeries according to their occurrence inline. :param boolean ascending: Determines if the TimeSeries will be ordered ascending or descending. If this is set to descending once, the ordered parameter defined in :py:meth:`TimeSeries.__init__` will be set to False FOREVER. :return: Returns :py:obj:`self` for convenience. :rtype: TimeSeries """; 18, boolean_operator; 19, block; 20, assignment; 21, not_operator; 22, block; 23, call; 24, assignment; 25, identifier:self; 26, identifier:ascending; 27, attribute; 28, return_statement; 29, identifier:sortorder; 30, integer:1; 31, identifier:ascending; 32, expression_statement; 33, expression_statement; 34, attribute; 35, argument_list; 36, attribute; 37, identifier:ascending; 38, identifier:self; 39, identifier:_sorted; 40, assignment; 41, assignment; 42, attribute; 43, identifier:sort; 44, keyword_argument; 45, identifier:self; 46, identifier:_sorted; 47, identifier:sortorder; 48, unary_operator; 49, attribute; 50, False; 51, identifier:self; 52, identifier:_timeseriesData; 53, identifier:key; 54, lambda; 55, integer:1; 56, identifier:self; 57, identifier:_predefinedSorted; 58, lambda_parameters; 59, binary_operator:sortorder * i[0]; 60, identifier:i; 61, identifier:sortorder; 62, subscript; 63, identifier:i; 64, integer:0
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 6, 15; 6, 16; 7, 17; 9, 18; 9, 19; 10, 20; 11, 21; 11, 22; 12, 23; 13, 24; 14, 25; 18, 26; 18, 27; 19, 28; 20, 29; 20, 30; 21, 31; 22, 32; 22, 33; 23, 34; 23, 35; 24, 36; 24, 37; 27, 38; 27, 39; 32, 40; 33, 41; 34, 42; 34, 43; 35, 44; 36, 45; 36, 46; 40, 47; 40, 48; 41, 49; 41, 50; 42, 51; 42, 52; 44, 53; 44, 54; 48, 55; 49, 56; 49, 57; 54, 58; 54, 59; 58, 60; 59, 61; 59, 62; 62, 63; 62, 64
def sort_timeseries(self, ascending=True): """Sorts the data points within the TimeSeries according to their occurrence inline. :param boolean ascending: Determines if the TimeSeries will be ordered ascending or descending. If this is set to descending once, the ordered parameter defined in :py:meth:`TimeSeries.__init__` will be set to False FOREVER. :return: Returns :py:obj:`self` for convenience. :rtype: TimeSeries """ # the time series is sorted by default if ascending and self._sorted: return sortorder = 1 if not ascending: sortorder = -1 self._predefinedSorted = False self._timeseriesData.sort(key=lambda i: sortorder * i[0]) self._sorted = ascending return self
0, module; 1, function_definition; 2, function_name:sorted_timeseries; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, if_statement; 10, expression_statement; 11, expression_statement; 12, for_statement; 13, expression_statement; 14, return_statement; 15, identifier:ascending; 16, True; 17, comment:"""Returns a sorted copy of the TimeSeries, preserving the original one. As an assumption this new TimeSeries is not ordered anymore if a new value is added. :param boolean ascending: Determines if the TimeSeries will be ordered ascending or descending. :return: Returns a new TimeSeries instance sorted in the requested order. :rtype: TimeSeries """; 18, assignment; 19, not_operator; 20, block; 21, assignment; 22, assignment; 23, identifier:entry; 24, identifier:data; 25, block; 26, assignment; 27, identifier:newTS; 28, identifier:sortorder; 29, integer:1; 30, identifier:ascending; 31, expression_statement; 32, identifier:data; 33, call; 34, identifier:newTS; 35, call; 36, expression_statement; 37, attribute; 38, identifier:ascending; 39, assignment; 40, identifier:sorted; 41, argument_list; 42, identifier:TimeSeries; 43, argument_list; 44, call; 45, identifier:newTS; 46, identifier:_sorted; 47, identifier:sortorder; 48, unary_operator; 49, attribute; 50, keyword_argument; 51, attribute; 52, attribute; 53, argument_list; 54, integer:1; 55, identifier:self; 56, identifier:_timeseriesData; 57, identifier:key; 58, lambda; 59, identifier:self; 60, identifier:_normalized; 61, identifier:newTS; 62, identifier:add_entry; 63, list_splat; 64, lambda_parameters; 65, binary_operator:sortorder * i[0]; 66, identifier:entry; 67, identifier:i; 68, identifier:sortorder; 69, subscript; 70, identifier:i; 71, integer:0
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 6, 15; 6, 16; 7, 17; 8, 18; 9, 19; 9, 20; 10, 21; 11, 22; 12, 23; 12, 24; 12, 25; 13, 26; 14, 27; 18, 28; 18, 29; 19, 30; 20, 31; 21, 32; 21, 33; 22, 34; 22, 35; 25, 36; 26, 37; 26, 38; 31, 39; 33, 40; 33, 41; 35, 42; 35, 43; 36, 44; 37, 45; 37, 46; 39, 47; 39, 48; 41, 49; 41, 50; 43, 51; 44, 52; 44, 53; 48, 54; 49, 55; 49, 56; 50, 57; 50, 58; 51, 59; 51, 60; 52, 61; 52, 62; 53, 63; 58, 64; 58, 65; 63, 66; 64, 67; 65, 68; 65, 69; 69, 70; 69, 71
def sorted_timeseries(self, ascending=True): """Returns a sorted copy of the TimeSeries, preserving the original one. As an assumption this new TimeSeries is not ordered anymore if a new value is added. :param boolean ascending: Determines if the TimeSeries will be ordered ascending or descending. :return: Returns a new TimeSeries instance sorted in the requested order. :rtype: TimeSeries """ sortorder = 1 if not ascending: sortorder = -1 data = sorted(self._timeseriesData, key=lambda i: sortorder * i[0]) newTS = TimeSeries(self._normalized) for entry in data: newTS.add_entry(*entry) newTS._sorted = ascending return newTS
0, module; 1, function_definition; 2, function_name:_calculate_values_to_forecast; 3, parameters; 4, block; 5, identifier:self; 6, identifier:timeSeries; 7, expression_statement; 8, comment:# do not set anything, if it is not required; 9, if_statement; 10, comment:# check the TimeSeries for correctness; 11, if_statement; 12, if_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, comment:"""Calculates the number of values, that need to be forecasted to match the goal set in forecast_until. This sets the parameter "valuesToForecast" and should be called at the beginning of the :py:meth:`BaseMethod.execute` implementation. :param TimeSeries timeSeries: Should be a sorted and normalized TimeSeries instance. :raise: Raises a :py:exc:`ValueError` if the TimeSeries is either not normalized or sorted. """; 17, comparison_operator:self._forecastUntil is None; 18, block; 19, not_operator; 20, block; 21, not_operator; 22, block; 23, assignment; 24, assignment; 25, call; 26, attribute; 27, None; 28, return_statement; 29, call; 30, raise_statement; 31, call; 32, raise_statement; 33, identifier:timediff; 34, binary_operator:timeSeries[-1][0] - timeSeries[-2][0]; 35, identifier:forecastSpan; 36, binary_operator:self._forecastUntil - timeSeries[-1][0]; 37, attribute; 38, argument_list; 39, identifier:self; 40, identifier:_forecastUntil; 41, attribute; 42, argument_list; 43, call; 44, attribute; 45, argument_list; 46, call; 47, subscript; 48, subscript; 49, attribute; 50, subscript; 51, identifier:self; 52, identifier:set_parameter; 53, string:"valuesToForecast"; 54, binary_operator:int(forecastSpan / timediff) + 1; 55, identifier:timeSeries; 56, identifier:is_sorted; 57, identifier:ValueError; 58, argument_list; 59, identifier:timeSeries; 60, identifier:is_normalized; 61, identifier:ValueError; 62, argument_list; 63, subscript; 64, integer:0; 65, subscript; 66, integer:0; 67, identifier:self; 68, identifier:_forecastUntil; 69, subscript; 70, integer:0; 71, call; 72, integer:1; 73, string:"timeSeries has to be sorted."; 74, string:"timeSeries has to be normalized."; 75, identifier:timeSeries; 76, unary_operator; 77, identifier:timeSeries; 78, unary_operator; 79, identifier:timeSeries; 80, unary_operator; 81, identifier:int; 82, argument_list; 83, integer:1; 84, integer:2; 85, integer:1; 86, binary_operator:forecastSpan / timediff; 87, identifier:forecastSpan; 88, identifier:timediff
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 7, 16; 9, 17; 9, 18; 11, 19; 11, 20; 12, 21; 12, 22; 13, 23; 14, 24; 15, 25; 17, 26; 17, 27; 18, 28; 19, 29; 20, 30; 21, 31; 22, 32; 23, 33; 23, 34; 24, 35; 24, 36; 25, 37; 25, 38; 26, 39; 26, 40; 29, 41; 29, 42; 30, 43; 31, 44; 31, 45; 32, 46; 34, 47; 34, 48; 36, 49; 36, 50; 37, 51; 37, 52; 38, 53; 38, 54; 41, 55; 41, 56; 43, 57; 43, 58; 44, 59; 44, 60; 46, 61; 46, 62; 47, 63; 47, 64; 48, 65; 48, 66; 49, 67; 49, 68; 50, 69; 50, 70; 54, 71; 54, 72; 58, 73; 62, 74; 63, 75; 63, 76; 65, 77; 65, 78; 69, 79; 69, 80; 71, 81; 71, 82; 76, 83; 78, 84; 80, 85; 82, 86; 86, 87; 86, 88
def _calculate_values_to_forecast(self, timeSeries): """Calculates the number of values, that need to be forecasted to match the goal set in forecast_until. This sets the parameter "valuesToForecast" and should be called at the beginning of the :py:meth:`BaseMethod.execute` implementation. :param TimeSeries timeSeries: Should be a sorted and normalized TimeSeries instance. :raise: Raises a :py:exc:`ValueError` if the TimeSeries is either not normalized or sorted. """ # do not set anything, if it is not required if self._forecastUntil is None: return # check the TimeSeries for correctness if not timeSeries.is_sorted(): raise ValueError("timeSeries has to be sorted.") if not timeSeries.is_normalized(): raise ValueError("timeSeries has to be normalized.") timediff = timeSeries[-1][0] - timeSeries[-2][0] forecastSpan = self._forecastUntil - timeSeries[-1][0] self.set_parameter("valuesToForecast", int(forecastSpan / timediff) + 1)
0, module; 1, function_definition; 2, function_name:get_bucket; 3, parameters; 4, block; 5, identifier:self; 6, identifier:bucket; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, if_statement; 14, if_statement; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, return_statement; 20, identifier:marker; 21, None; 22, identifier:max_keys; 23, None; 24, identifier:prefix; 25, None; 26, comment:""" Get a list of all the objects in a bucket. @param bucket: The name of the bucket from which to retrieve objects. @type bucket: L{unicode} @param marker: If given, indicate a position in the overall results where the results of this call should begin. The first result is the first object that sorts greater than this marker. @type marker: L{bytes} or L{NoneType} @param max_keys: If given, the maximum number of objects to return. @type max_keys: L{int} or L{NoneType} @param prefix: If given, indicate that only objects with keys beginning with this value should be returned. @type prefix: L{bytes} or L{NoneType} @return: A L{Deferred} that fires with a L{BucketListing} describing the result. @see: U{http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html} """; 27, assignment; 28, comparison_operator:marker is not None; 29, block; 30, comparison_operator:max_keys is not None; 31, block; 32, comparison_operator:prefix is not None; 33, block; 34, identifier:args; 35, block; 36, else_clause; 37, assignment; 38, assignment; 39, call; 40, identifier:d; 41, identifier:args; 42, list; 43, identifier:marker; 44, None; 45, expression_statement; 46, identifier:max_keys; 47, None; 48, expression_statement; 49, identifier:prefix; 50, None; 51, expression_statement; 52, expression_statement; 53, block; 54, identifier:details; 55, call; 56, identifier:d; 57, call; 58, attribute; 59, argument_list; 60, call; 61, call; 62, call; 63, assignment; 64, expression_statement; 65, attribute; 66, argument_list; 67, attribute; 68, argument_list; 69, identifier:d; 70, identifier:addCallback; 71, attribute; 72, attribute; 73, argument_list; 74, attribute; 75, argument_list; 76, attribute; 77, argument_list; 78, identifier:object_name; 79, binary_operator:"?" + urlencode(args); 80, assignment; 81, identifier:self; 82, identifier:_details; 83, keyword_argument; 84, keyword_argument; 85, identifier:self; 86, identifier:_submit; 87, call; 88, identifier:self; 89, identifier:_parse_get_bucket; 90, identifier:args; 91, identifier:append; 92, tuple; 93, identifier:args; 94, identifier:append; 95, tuple; 96, identifier:args; 97, identifier:append; 98, tuple; 99, string:"?"; 100, call; 101, identifier:object_name; 102, None; 103, identifier:method; 104, string:b"GET"; 105, identifier:url_context; 106, call; 107, attribute; 108, argument_list; 109, string:"marker"; 110, identifier:marker; 111, string:"max-keys"; 112, binary_operator:"%d" % (max_keys,); 113, string:"prefix"; 114, identifier:prefix; 115, identifier:urlencode; 116, argument_list; 117, attribute; 118, argument_list; 119, identifier:self; 120, identifier:_query_factory; 121, identifier:details; 122, string:"%d"; 123, tuple; 124, identifier:args; 125, identifier:self; 126, identifier:_url_context; 127, keyword_argument; 128, keyword_argument; 129, identifier:max_keys; 130, identifier:bucket; 131, identifier:bucket; 132, identifier:object_name; 133, identifier:object_name
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 7, 20; 7, 21; 8, 22; 8, 23; 9, 24; 9, 25; 10, 26; 11, 27; 12, 28; 12, 29; 13, 30; 13, 31; 14, 32; 14, 33; 15, 34; 15, 35; 15, 36; 16, 37; 17, 38; 18, 39; 19, 40; 27, 41; 27, 42; 28, 43; 28, 44; 29, 45; 30, 46; 30, 47; 31, 48; 32, 49; 32, 50; 33, 51; 35, 52; 36, 53; 37, 54; 37, 55; 38, 56; 38, 57; 39, 58; 39, 59; 45, 60; 48, 61; 51, 62; 52, 63; 53, 64; 55, 65; 55, 66; 57, 67; 57, 68; 58, 69; 58, 70; 59, 71; 60, 72; 60, 73; 61, 74; 61, 75; 62, 76; 62, 77; 63, 78; 63, 79; 64, 80; 65, 81; 65, 82; 66, 83; 66, 84; 67, 85; 67, 86; 68, 87; 71, 88; 71, 89; 72, 90; 72, 91; 73, 92; 74, 93; 74, 94; 75, 95; 76, 96; 76, 97; 77, 98; 79, 99; 79, 100; 80, 101; 80, 102; 83, 103; 83, 104; 84, 105; 84, 106; 87, 107; 87, 108; 92, 109; 92, 110; 95, 111; 95, 112; 98, 113; 98, 114; 100, 115; 100, 116; 106, 117; 106, 118; 107, 119; 107, 120; 108, 121; 112, 122; 112, 123; 116, 124; 117, 125; 117, 126; 118, 127; 118, 128; 123, 129; 127, 130; 127, 131; 128, 132; 128, 133
def get_bucket(self, bucket, marker=None, max_keys=None, prefix=None): """ Get a list of all the objects in a bucket. @param bucket: The name of the bucket from which to retrieve objects. @type bucket: L{unicode} @param marker: If given, indicate a position in the overall results where the results of this call should begin. The first result is the first object that sorts greater than this marker. @type marker: L{bytes} or L{NoneType} @param max_keys: If given, the maximum number of objects to return. @type max_keys: L{int} or L{NoneType} @param prefix: If given, indicate that only objects with keys beginning with this value should be returned. @type prefix: L{bytes} or L{NoneType} @return: A L{Deferred} that fires with a L{BucketListing} describing the result. @see: U{http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html} """ args = [] if marker is not None: args.append(("marker", marker)) if max_keys is not None: args.append(("max-keys", "%d" % (max_keys,))) if prefix is not None: args.append(("prefix", prefix)) if args: object_name = "?" + urlencode(args) else: object_name = None details = self._details( method=b"GET", url_context=self._url_context(bucket=bucket, object_name=object_name), ) d = self._submit(self._query_factory(details)) d.addCallback(self._parse_get_bucket) return d
0, module; 1, function_definition; 2, function_name:findunique; 3, parameters; 4, block; 5, identifier:lst; 6, identifier:key; 7, expression_statement; 8, return_statement; 9, comment:""" Find all unique key values for items in lst. Parameters ---------- lst: list A list of composite dictionaries e.g. ``layers``, ``classes`` key: string The key name to search each dictionary in the list Returns ------- list A sorted Python list of unique keys in the list Example ------- To find all ``GROUP`` values for ``CLASS`` in a ``LAYER``:: s = ''' LAYER CLASS GROUP "group1" NAME "Class1" COLOR 0 0 0 END CLASS GROUP "group2" NAME "Class2" COLOR 0 0 0 END CLASS GROUP "group1" NAME "Class3" COLOR 0 0 0 END END ''' d = mappyfile.loads(s) groups = mappyfile.findunique(d["classes"], "group") assert groups == ["group1", "group2"] """; 10, call; 11, identifier:sorted; 12, argument_list; 13, call; 14, identifier:set; 15, argument_list; 16, list_comprehension; 17, subscript; 18, for_in_clause; 19, identifier:item; 20, call; 21, identifier:item; 22, identifier:lst; 23, attribute; 24, argument_list; 25, identifier:key; 26, identifier:lower
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 7, 9; 8, 10; 10, 11; 10, 12; 12, 13; 13, 14; 13, 15; 15, 16; 16, 17; 16, 18; 17, 19; 17, 20; 18, 21; 18, 22; 20, 23; 20, 24; 23, 25; 23, 26
def findunique(lst, key): """ Find all unique key values for items in lst. Parameters ---------- lst: list A list of composite dictionaries e.g. ``layers``, ``classes`` key: string The key name to search each dictionary in the list Returns ------- list A sorted Python list of unique keys in the list Example ------- To find all ``GROUP`` values for ``CLASS`` in a ``LAYER``:: s = ''' LAYER CLASS GROUP "group1" NAME "Class1" COLOR 0 0 0 END CLASS GROUP "group2" NAME "Class2" COLOR 0 0 0 END CLASS GROUP "group1" NAME "Class3" COLOR 0 0 0 END END ''' d = mappyfile.loads(s) groups = mappyfile.findunique(d["classes"], "group") assert groups == ["group1", "group2"] """ return sorted(set([item[key.lower()] for item in lst]))
0, module; 1, function_definition; 2, function_name:make_input_dataframe_by_entity; 3, parameters; 4, block; 5, identifier:tax_benefit_system; 6, identifier:nb_persons; 7, identifier:nb_groups; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, comment:#; 16, expression_statement; 17, expression_statement; 18, comment:# A legacy role is an index that every person within an entity has.; 19, comment:# For instance, the 'first_parent' has legacy role 0, the 'second_parent' 1, the first 'child' 2, the second 3, etc.; 20, expression_statement; 21, expression_statement; 22, for_statement; 23, for_statement; 24, return_statement; 25, comment:""" Generate a dictionnary of dataframes containing nb_persons persons spread in nb_groups groups. :param TaxBenefitSystem tax_benefit_system: the tax_benefit_system to use :param int nb_persons: the number of persons in the system :param int nb_groups: the number of collective entities in the system :returns: A dictionary whose keys are entities and values the corresponding data frames Example: >>> from openfisca_survey_manager.input_dataframe_generator import make_input_dataframe_by_entity >>> from openfisca_country_template import CountryTaxBenefitSystem >>> tbs = CountryTaxBenefitSystem() >>> input_dataframe_by_entity = make_input_dataframe_by_entity(tbs, 400, 100) >>> sorted(input_dataframe_by_entity['person'].columns.tolist()) ['household_id', 'household_legacy_role', 'household_role', 'person_id'] >>> sorted(input_dataframe_by_entity['household'].columns.tolist()) [] """; 26, assignment; 27, assignment; 28, assignment; 29, assignment; 30, assignment; 31, call; 32, assignment; 33, assignment; 34, assignment; 35, assignment; 36, identifier:id_person; 37, call; 38, block; 39, identifier:entity; 40, attribute; 41, block; 42, identifier:input_dataframe_by_entity; 43, identifier:input_dataframe_by_entity; 44, call; 45, identifier:person_entity; 46, subscript; 47, identifier:person_id; 48, call; 49, identifier:input_dataframe_by_entity; 50, call; 51, subscript; 52, call; 53, attribute; 54, argument_list; 55, identifier:adults; 56, binary_operator:[0] + sorted(random.sample(range(1, nb_persons), nb_groups - 1)); 57, identifier:members_entity_id; 58, call; 59, identifier:members_legacy_role; 60, call; 61, identifier:id_group; 62, unary_operator; 63, identifier:range; 64, argument_list; 65, if_statement; 66, expression_statement; 67, expression_statement; 68, identifier:tax_benefit_system; 69, identifier:entities; 70, if_statement; 71, expression_statement; 72, expression_statement; 73, expression_statement; 74, expression_statement; 75, expression_statement; 76, expression_statement; 77, expression_statement; 78, identifier:dict; 79, argument_list; 80, list_comprehension; 81, integer:0; 82, attribute; 83, argument_list; 84, identifier:dict; 85, argument_list; 86, identifier:input_dataframe_by_entity; 87, attribute; 88, attribute; 89, argument_list; 90, subscript; 91, identifier:set_index; 92, string; 93, list; 94, call; 95, attribute; 96, argument_list; 97, attribute; 98, argument_list; 99, integer:1; 100, identifier:nb_persons; 101, comparison_operator:id_person in adults; 102, block; 103, else_clause; 104, assignment; 105, assignment; 106, attribute; 107, block; 108, assignment; 109, assignment; 110, assignment; 111, assignment; 112, assignment; 113, assignment; 114, call; 115, identifier:entity; 116, for_in_clause; 117, if_clause; 118, identifier:np; 119, identifier:arange; 120, identifier:nb_persons; 121, identifier:person_entity; 122, identifier:key; 123, identifier:pd; 124, identifier:DataFrame; 125, dictionary; 126, identifier:input_dataframe_by_entity; 127, attribute; 128, string_content:person_id; 129, integer:0; 130, identifier:sorted; 131, argument_list; 132, identifier:np; 133, identifier:empty; 134, identifier:nb_persons; 135, keyword_argument; 136, identifier:np; 137, identifier:empty; 138, identifier:nb_persons; 139, keyword_argument; 140, identifier:id_person; 141, identifier:adults; 142, expression_statement; 143, expression_statement; 144, block; 145, subscript; 146, identifier:legacy_role; 147, subscript; 148, identifier:id_group; 149, identifier:entity; 150, identifier:is_person; 151, continue_statement; 152, identifier:key; 153, attribute; 154, identifier:person_dataframe; 155, subscript; 156, subscript; 157, identifier:members_entity_id; 158, subscript; 159, identifier:members_legacy_role; 160, subscript; 161, call; 162, subscript; 163, call; 164, attribute; 165, argument_list; 166, identifier:entity; 167, attribute; 168, attribute; 169, pair; 170, identifier:person_entity; 171, identifier:key; 172, call; 173, identifier:dtype; 174, identifier:int; 175, identifier:dtype; 176, identifier:int; 177, augmented_assignment; 178, assignment; 179, expression_statement; 180, identifier:members_legacy_role; 181, identifier:id_person; 182, identifier:members_entity_id; 183, identifier:id_person; 184, identifier:entity; 185, identifier:key; 186, identifier:input_dataframe_by_entity; 187, attribute; 188, identifier:person_dataframe; 189, binary_operator:key + '_id'; 190, identifier:person_dataframe; 191, binary_operator:key + '_legacy_role'; 192, identifier:person_dataframe; 193, binary_operator:key + '_role'; 194, attribute; 195, argument_list; 196, identifier:input_dataframe_by_entity; 197, identifier:key; 198, attribute; 199, argument_list; 200, subscript; 201, identifier:set_index; 202, binary_operator:key + '_id'; 203, keyword_argument; 204, identifier:tax_benefit_system; 205, identifier:entities; 206, identifier:entity; 207, identifier:is_person; 208, binary_operator:person_entity.key + '_id'; 209, identifier:person_id; 210, attribute; 211, argument_list; 212, identifier:id_group; 213, integer:1; 214, identifier:legacy_role; 215, integer:0; 216, assignment; 217, identifier:person_entity; 218, identifier:key; 219, identifier:key; 220, string; 221, identifier:key; 222, string; 223, identifier:key; 224, string; 225, identifier:np; 226, identifier:where; 227, comparison_operator:members_legacy_role == 0; 228, attribute; 229, attribute; 230, identifier:pd; 231, identifier:DataFrame; 232, dictionary; 233, identifier:input_dataframe_by_entity; 234, identifier:key; 235, identifier:key; 236, string; 237, identifier:inplace; 238, True; 239, attribute; 240, string; 241, identifier:random; 242, identifier:sample; 243, call; 244, binary_operator:nb_groups - 1; 245, identifier:legacy_role; 246, conditional_expression:2 if legacy_role == 0 else legacy_role + 1; 247, string_content:_id; 248, string_content:_legacy_role; 249, string_content:_role; 250, identifier:members_legacy_role; 251, integer:0; 252, subscript; 253, identifier:key; 254, subscript; 255, identifier:key; 256, pair; 257, string_content:_id; 258, identifier:person_entity; 259, identifier:key; 260, string_content:_id; 261, identifier:range; 262, argument_list; 263, identifier:nb_groups; 264, integer:1; 265, integer:2; 266, comparison_operator:legacy_role == 0; 267, binary_operator:legacy_role + 1; 268, attribute; 269, integer:0; 270, attribute; 271, unary_operator; 272, binary_operator:key + '_id'; 273, call; 274, integer:1; 275, identifier:nb_persons; 276, identifier:legacy_role; 277, integer:0; 278, identifier:legacy_role; 279, integer:1; 280, identifier:entity; 281, identifier:flattened_roles; 282, identifier:entity; 283, identifier:flattened_roles; 284, integer:1; 285, identifier:key; 286, string; 287, identifier:range; 288, argument_list; 289, string_content:_id; 290, identifier:nb_groups
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 8, 25; 9, 26; 10, 27; 11, 28; 12, 29; 13, 30; 14, 31; 16, 32; 17, 33; 20, 34; 21, 35; 22, 36; 22, 37; 22, 38; 23, 39; 23, 40; 23, 41; 24, 42; 26, 43; 26, 44; 27, 45; 27, 46; 28, 47; 28, 48; 29, 49; 29, 50; 30, 51; 30, 52; 31, 53; 31, 54; 32, 55; 32, 56; 33, 57; 33, 58; 34, 59; 34, 60; 35, 61; 35, 62; 37, 63; 37, 64; 38, 65; 38, 66; 38, 67; 40, 68; 40, 69; 41, 70; 41, 71; 41, 72; 41, 73; 41, 74; 41, 75; 41, 76; 41, 77; 44, 78; 44, 79; 46, 80; 46, 81; 48, 82; 48, 83; 50, 84; 50, 85; 51, 86; 51, 87; 52, 88; 52, 89; 53, 90; 53, 91; 54, 92; 56, 93; 56, 94; 58, 95; 58, 96; 60, 97; 60, 98; 62, 99; 64, 100; 65, 101; 65, 102; 65, 103; 66, 104; 67, 105; 70, 106; 70, 107; 71, 108; 72, 109; 73, 110; 74, 111; 75, 112; 76, 113; 77, 114; 80, 115; 80, 116; 80, 117; 82, 118; 82, 119; 83, 120; 87, 121; 87, 122; 88, 123; 88, 124; 89, 125; 90, 126; 90, 127; 92, 128; 93, 129; 94, 130; 94, 131; 95, 132; 95, 133; 96, 134; 96, 135; 97, 136; 97, 137; 98, 138; 98, 139; 101, 140; 101, 141; 102, 142; 102, 143; 103, 144; 104, 145; 104, 146; 105, 147; 105, 148; 106, 149; 106, 150; 107, 151; 108, 152; 108, 153; 109, 154; 109, 155; 110, 156; 110, 157; 111, 158; 111, 159; 112, 160; 112, 161; 113, 162; 113, 163; 114, 164; 114, 165; 116, 166; 116, 167; 117, 168; 125, 169; 127, 170; 127, 171; 131, 172; 135, 173; 135, 174; 139, 175; 139, 176; 142, 177; 143, 178; 144, 179; 145, 180; 145, 181; 147, 182; 147, 183; 153, 184; 153, 185; 155, 186; 155, 187; 156, 188; 156, 189; 158, 190; 158, 191; 160, 192; 160, 193; 161, 194; 161, 195; 162, 196; 162, 197; 163, 198; 163, 199; 164, 200; 164, 201; 165, 202; 165, 203; 167, 204; 167, 205; 168, 206; 168, 207; 169, 208; 169, 209; 172, 210; 172, 211; 177, 212; 177, 213; 178, 214; 178, 215; 179, 216; 187, 217; 187, 218; 189, 219; 189, 220; 191, 221; 191, 222; 193, 223; 193, 224; 194, 225; 194, 226; 195, 227; 195, 228; 195, 229; 198, 230; 198, 231; 199, 232; 200, 233; 200, 234; 202, 235; 202, 236; 203, 237; 203, 238; 208, 239; 208, 240; 210, 241; 210, 242; 211, 243; 211, 244; 216, 245; 216, 246; 220, 247; 222, 248; 224, 249; 227, 250; 227, 251; 228, 252; 228, 253; 229, 254; 229, 255; 232, 256; 236, 257; 239, 258; 239, 259; 240, 260; 243, 261; 243, 262; 244, 263; 244, 264; 246, 265; 246, 266; 246, 267; 252, 268; 252, 269; 254, 270; 254, 271; 256, 272; 256, 273; 262, 274; 262, 275; 266, 276; 266, 277; 267, 278; 267, 279; 268, 280; 268, 281; 270, 282; 270, 283; 271, 284; 272, 285; 272, 286; 273, 287; 273, 288; 286, 289; 288, 290
def make_input_dataframe_by_entity(tax_benefit_system, nb_persons, nb_groups): """ Generate a dictionnary of dataframes containing nb_persons persons spread in nb_groups groups. :param TaxBenefitSystem tax_benefit_system: the tax_benefit_system to use :param int nb_persons: the number of persons in the system :param int nb_groups: the number of collective entities in the system :returns: A dictionary whose keys are entities and values the corresponding data frames Example: >>> from openfisca_survey_manager.input_dataframe_generator import make_input_dataframe_by_entity >>> from openfisca_country_template import CountryTaxBenefitSystem >>> tbs = CountryTaxBenefitSystem() >>> input_dataframe_by_entity = make_input_dataframe_by_entity(tbs, 400, 100) >>> sorted(input_dataframe_by_entity['person'].columns.tolist()) ['household_id', 'household_legacy_role', 'household_role', 'person_id'] >>> sorted(input_dataframe_by_entity['household'].columns.tolist()) [] """ input_dataframe_by_entity = dict() person_entity = [entity for entity in tax_benefit_system.entities if entity.is_person][0] person_id = np.arange(nb_persons) input_dataframe_by_entity = dict() input_dataframe_by_entity[person_entity.key] = pd.DataFrame({ person_entity.key + '_id': person_id, }) input_dataframe_by_entity[person_entity.key].set_index('person_id') # adults = [0] + sorted(random.sample(range(1, nb_persons), nb_groups - 1)) members_entity_id = np.empty(nb_persons, dtype = int) # A legacy role is an index that every person within an entity has. # For instance, the 'first_parent' has legacy role 0, the 'second_parent' 1, the first 'child' 2, the second 3, etc. members_legacy_role = np.empty(nb_persons, dtype = int) id_group = -1 for id_person in range(nb_persons): if id_person in adults: id_group += 1 legacy_role = 0 else: legacy_role = 2 if legacy_role == 0 else legacy_role + 1 members_legacy_role[id_person] = legacy_role members_entity_id[id_person] = id_group for entity in tax_benefit_system.entities: if entity.is_person: continue key = entity.key person_dataframe = input_dataframe_by_entity[person_entity.key] person_dataframe[key + '_id'] = members_entity_id person_dataframe[key + '_legacy_role'] = members_legacy_role person_dataframe[key + '_role'] = np.where( members_legacy_role == 0, entity.flattened_roles[0].key, entity.flattened_roles[-1].key) input_dataframe_by_entity[key] = pd.DataFrame({ key + '_id': range(nb_groups) }) input_dataframe_by_entity[key].set_index(key + '_id', inplace = True) return input_dataframe_by_entity
0, module; 1, function_definition; 2, function_name:multi_sort; 3, parameters; 4, block; 5, identifier:remotes; 6, identifier:sort; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, comment:# Convert name to int if alpha is in sort.; 11, if_statement; 12, comment:# Parse versions if semver is in sort.; 13, if_statement; 14, comment:# Build sort_mapping dict.; 15, expression_statement; 16, for_statement; 17, comment:# Sort.; 18, expression_statement; 19, comment:"""Sort `remotes` in place. Allows sorting by multiple conditions. This is needed because Python 3 no longer supports sorting lists of multiple types. Sort keys must all be of the same type. Problem: the user expects versions to be sorted latest first and timelogical to be most recent first (when viewing the HTML documentation), yet expects alphabetical sorting to be A before Z. Solution: invert integers (dates and parsed versions). :param iter remotes: List of dicts from Versions().remotes. :param iter sort: What to sort by. May be one or more of: alpha, time, semver """; 20, assignment; 21, assignment; 22, comparison_operator:'alpha' in sort; 23, block; 24, comparison_operator:'semver' in sort; 25, block; 26, assignment; 27, pattern_list; 28, call; 29, block; 30, call; 31, identifier:exploded_alpha; 32, call; 33, identifier:exploded_semver; 34, call; 35, string; 36, identifier:sort; 37, expression_statement; 38, for_statement; 39, string; 40, identifier:sort; 41, expression_statement; 42, identifier:sort_mapping; 43, call; 44, identifier:i; 45, identifier:remote; 46, identifier:enumerate; 47, argument_list; 48, expression_statement; 49, for_statement; 50, expression_statement; 51, attribute; 52, argument_list; 53, identifier:list; 54, argument_list; 55, identifier:list; 56, argument_list; 57, string_content:alpha; 58, assignment; 59, identifier:name; 60, generator_expression; 61, block; 62, string_content:semver; 63, assignment; 64, identifier:dict; 65, argument_list; 66, identifier:remotes; 67, assignment; 68, identifier:sort_by; 69, identifier:sort; 70, block; 71, assignment; 72, identifier:remotes; 73, identifier:sort; 74, keyword_argument; 75, identifier:alpha_max_len; 76, call; 77, subscript; 78, for_in_clause; 79, expression_statement; 80, identifier:exploded_semver; 81, call; 82, identifier:key; 83, call; 84, if_statement; 85, subscript; 86, identifier:key; 87, identifier:key; 88, lambda; 89, identifier:max; 90, generator_expression; 91, identifier:r; 92, string; 93, identifier:r; 94, identifier:remotes; 95, call; 96, identifier:semvers; 97, generator_expression; 98, identifier:list; 99, argument_list; 100, comparison_operator:sort_by == 'alpha'; 101, block; 102, elif_clause; 103, elif_clause; 104, identifier:sort_mapping; 105, call; 106, lambda_parameters; 107, call; 108, call; 109, for_in_clause; 110, string_content:name; 111, attribute; 112, argument_list; 113, subscript; 114, for_in_clause; 115, identifier:sort_by; 116, string; 117, expression_statement; 118, comparison_operator:sort_by == 'time'; 119, block; 120, comparison_operator:sort_by == 'semver'; 121, block; 122, identifier:id; 123, argument_list; 124, identifier:k; 125, attribute; 126, argument_list; 127, identifier:len; 128, argument_list; 129, identifier:r; 130, identifier:remotes; 131, identifier:exploded_alpha; 132, identifier:append; 133, binary_operator:[ord(i) for i in name] + [0] * (alpha_max_len - len(name)); 134, identifier:r; 135, string; 136, identifier:r; 137, identifier:remotes; 138, string_content:alpha; 139, call; 140, identifier:sort_by; 141, string; 142, expression_statement; 143, identifier:sort_by; 144, string; 145, expression_statement; 146, identifier:remote; 147, identifier:sort_mapping; 148, identifier:get; 149, call; 150, subscript; 151, list_comprehension; 152, binary_operator:[0] * (alpha_max_len - len(name)); 153, string_content:name; 154, attribute; 155, argument_list; 156, string_content:time; 157, call; 158, string_content:semver; 159, call; 160, identifier:id; 161, argument_list; 162, identifier:r; 163, string; 164, call; 165, for_in_clause; 166, list; 167, parenthesized_expression; 168, identifier:key; 169, identifier:extend; 170, subscript; 171, attribute; 172, argument_list; 173, attribute; 174, argument_list; 175, identifier:k; 176, string_content:name; 177, identifier:ord; 178, argument_list; 179, identifier:i; 180, identifier:name; 181, integer:0; 182, binary_operator:alpha_max_len - len(name); 183, identifier:exploded_alpha; 184, identifier:i; 185, identifier:key; 186, identifier:append; 187, unary_operator; 188, identifier:key; 189, identifier:extend; 190, subscript; 191, identifier:i; 192, identifier:alpha_max_len; 193, call; 194, subscript; 195, identifier:exploded_semver; 196, identifier:i; 197, identifier:len; 198, argument_list; 199, identifier:remote; 200, string; 201, identifier:name; 202, string_content:date
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 7, 19; 8, 20; 9, 21; 11, 22; 11, 23; 13, 24; 13, 25; 15, 26; 16, 27; 16, 28; 16, 29; 18, 30; 20, 31; 20, 32; 21, 33; 21, 34; 22, 35; 22, 36; 23, 37; 23, 38; 24, 39; 24, 40; 25, 41; 26, 42; 26, 43; 27, 44; 27, 45; 28, 46; 28, 47; 29, 48; 29, 49; 29, 50; 30, 51; 30, 52; 32, 53; 32, 54; 34, 55; 34, 56; 35, 57; 37, 58; 38, 59; 38, 60; 38, 61; 39, 62; 41, 63; 43, 64; 43, 65; 47, 66; 48, 67; 49, 68; 49, 69; 49, 70; 50, 71; 51, 72; 51, 73; 52, 74; 58, 75; 58, 76; 60, 77; 60, 78; 61, 79; 63, 80; 63, 81; 67, 82; 67, 83; 70, 84; 71, 85; 71, 86; 74, 87; 74, 88; 76, 89; 76, 90; 77, 91; 77, 92; 78, 93; 78, 94; 79, 95; 81, 96; 81, 97; 83, 98; 83, 99; 84, 100; 84, 101; 84, 102; 84, 103; 85, 104; 85, 105; 88, 106; 88, 107; 90, 108; 90, 109; 92, 110; 95, 111; 95, 112; 97, 113; 97, 114; 100, 115; 100, 116; 101, 117; 102, 118; 102, 119; 103, 120; 103, 121; 105, 122; 105, 123; 106, 124; 107, 125; 107, 126; 108, 127; 108, 128; 109, 129; 109, 130; 111, 131; 111, 132; 112, 133; 113, 134; 113, 135; 114, 136; 114, 137; 116, 138; 117, 139; 118, 140; 118, 141; 119, 142; 120, 143; 120, 144; 121, 145; 123, 146; 125, 147; 125, 148; 126, 149; 128, 150; 133, 151; 133, 152; 135, 153; 139, 154; 139, 155; 141, 156; 142, 157; 144, 158; 145, 159; 149, 160; 149, 161; 150, 162; 150, 163; 151, 164; 151, 165; 152, 166; 152, 167; 154, 168; 154, 169; 155, 170; 157, 171; 157, 172; 159, 173; 159, 174; 161, 175; 163, 176; 164, 177; 164, 178; 165, 179; 165, 180; 166, 181; 167, 182; 170, 183; 170, 184; 171, 185; 171, 186; 172, 187; 173, 188; 173, 189; 174, 190; 178, 191; 182, 192; 182, 193; 187, 194; 190, 195; 190, 196; 193, 197; 193, 198; 194, 199; 194, 200; 198, 201; 200, 202
def multi_sort(remotes, sort): """Sort `remotes` in place. Allows sorting by multiple conditions. This is needed because Python 3 no longer supports sorting lists of multiple types. Sort keys must all be of the same type. Problem: the user expects versions to be sorted latest first and timelogical to be most recent first (when viewing the HTML documentation), yet expects alphabetical sorting to be A before Z. Solution: invert integers (dates and parsed versions). :param iter remotes: List of dicts from Versions().remotes. :param iter sort: What to sort by. May be one or more of: alpha, time, semver """ exploded_alpha = list() exploded_semver = list() # Convert name to int if alpha is in sort. if 'alpha' in sort: alpha_max_len = max(len(r['name']) for r in remotes) for name in (r['name'] for r in remotes): exploded_alpha.append([ord(i) for i in name] + [0] * (alpha_max_len - len(name))) # Parse versions if semver is in sort. if 'semver' in sort: exploded_semver = semvers(r['name'] for r in remotes) # Build sort_mapping dict. sort_mapping = dict() for i, remote in enumerate(remotes): key = list() for sort_by in sort: if sort_by == 'alpha': key.extend(exploded_alpha[i]) elif sort_by == 'time': key.append(-remote['date']) elif sort_by == 'semver': key.extend(exploded_semver[i]) sort_mapping[id(remote)] = key # Sort. remotes.sort(key=lambda k: sort_mapping.get(id(k)))
0, module; 1, function_definition; 2, function_name:get_params; 3, parameters; 4, block; 5, identifier:self; 6, identifier:ctx; 7, expression_statement; 8, expression_statement; 9, return_statement; 10, comment:"""Sort order of options before displaying. :param click.core.Context ctx: Click context. :return: super() return value. """; 11, call; 12, call; 13, attribute; 14, argument_list; 15, attribute; 16, argument_list; 17, attribute; 18, identifier:sort; 19, keyword_argument; 20, call; 21, identifier:get_params; 22, identifier:ctx; 23, identifier:self; 24, identifier:params; 25, identifier:key; 26, attribute; 27, identifier:super; 28, argument_list; 29, identifier:self; 30, identifier:custom_sort; 31, identifier:ClickGroup; 32, identifier:self
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 7, 10; 8, 11; 9, 12; 11, 13; 11, 14; 12, 15; 12, 16; 13, 17; 13, 18; 14, 19; 15, 20; 15, 21; 16, 22; 17, 23; 17, 24; 19, 25; 19, 26; 20, 27; 20, 28; 26, 29; 26, 30; 28, 31; 28, 32
def get_params(self, ctx): """Sort order of options before displaying. :param click.core.Context ctx: Click context. :return: super() return value. """ self.params.sort(key=self.custom_sort) return super(ClickGroup, self).get_params(ctx)
0, module; 1, function_definition; 2, function_name:configure_switch_entries; 3, parameters; 4, block; 5, identifier:self; 6, identifier:switch_ip; 7, identifier:port_bindings; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, comment:# Let's make these lists a set to exclude duplicates; 17, expression_statement; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, for_statement; 23, if_statement; 24, expression_statement; 25, expression_statement; 26, expression_statement; 27, if_statement; 28, expression_statement; 29, expression_statement; 30, expression_statement; 31, comment:"""Create a nexus switch entry in Nexus. The port_bindings is sorted by vlan_id, vni, port_id. When there is a change in vlan_id or vni, then vlan data is configured in Nexus device. Otherwise we check if there is a change in port_id where we configure the port with vlan trunk config. Called during switch replay event. """; 32, assignment; 33, assignment; 34, assignment; 35, assignment; 36, assignment; 37, call; 38, call; 39, assignment; 40, assignment; 41, assignment; 42, assignment; 43, assignment; 44, identifier:port; 45, identifier:port_bindings; 46, block; 47, identifier:pvlans; 48, block; 49, call; 50, call; 51, assignment; 52, identifier:vlans; 53, block; 54, call; 55, call; 56, call; 57, identifier:prev_vlan; 58, unary_operator; 59, identifier:prev_vni; 60, unary_operator; 61, identifier:prev_port; 62, None; 63, identifier:prev_native_vlan; 64, integer:0; 65, identifier:starttime; 66, call; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, identifier:vlans; 72, call; 73, identifier:pvlans; 74, call; 75, identifier:interface_count; 76, integer:0; 77, identifier:duplicate_port; 78, integer:0; 79, identifier:vlan_count; 80, integer:0; 81, if_statement; 82, expression_statement; 83, if_statement; 84, expression_statement; 85, expression_statement; 86, attribute; 87, argument_list; 88, attribute; 89, argument_list; 90, identifier:vlans; 91, call; 92, expression_statement; 93, expression_statement; 94, if_statement; 95, attribute; 96, argument_list; 97, attribute; 98, argument_list; 99, attribute; 100, argument_list; 101, integer:1; 102, integer:1; 103, attribute; 104, argument_list; 105, identifier:port_bindings; 106, identifier:sort; 107, keyword_argument; 108, attribute; 109, identifier:capture_and_print_timeshot; 110, identifier:starttime; 111, string:"replay_t2_aft_sort"; 112, keyword_argument; 113, identifier:set; 114, argument_list; 115, identifier:set; 116, argument_list; 117, call; 118, block; 119, assignment; 120, comparison_operator:port.port_id == prev_port; 121, block; 122, else_clause; 123, call; 124, call; 125, identifier:LOG; 126, identifier:debug; 127, string:"Replayed total %d ports for Switch %s"; 128, binary_operator:interface_count + 1; 129, identifier:switch_ip; 130, attribute; 131, identifier:capture_and_print_timeshot; 132, identifier:starttime; 133, string:"replay_part_1"; 134, keyword_argument; 135, identifier:list; 136, argument_list; 137, call; 138, assignment; 139, comparison_operator:vni == 0; 140, block; 141, else_clause; 142, identifier:self; 143, identifier:set_switch_ip_and_active_state; 144, identifier:switch_ip; 145, attribute; 146, identifier:self; 147, identifier:configure_next_batch_of_vlans; 148, identifier:switch_ip; 149, attribute; 150, identifier:capture_and_print_timeshot; 151, identifier:starttime; 152, string:"replay_part_2"; 153, keyword_argument; 154, identifier:time; 155, identifier:time; 156, identifier:key; 157, lambda; 158, identifier:self; 159, identifier:driver; 160, identifier:switch; 161, identifier:switch_ip; 162, attribute; 163, argument_list; 164, continue_statement; 165, pattern_list; 166, call; 167, attribute; 168, identifier:prev_port; 169, if_statement; 170, comment:# Different port - write out interface trunk on previous port; 171, block; 172, attribute; 173, argument_list; 174, attribute; 175, argument_list; 176, identifier:interface_count; 177, integer:1; 178, identifier:self; 179, identifier:driver; 180, identifier:switch; 181, identifier:switch_ip; 182, identifier:vlans; 183, attribute; 184, argument_list; 185, pattern_list; 186, subscript; 187, identifier:vni; 188, integer:0; 189, expression_statement; 190, block; 191, identifier:const; 192, identifier:SWITCH_RESTORE_S2; 193, identifier:self; 194, identifier:driver; 195, identifier:switch; 196, identifier:switch_ip; 197, lambda_parameters; 198, tuple; 199, identifier:nxos_db; 200, identifier:is_reserved_binding; 201, identifier:port; 202, identifier:auto_create; 203, identifier:auto_trunk; 204, attribute; 205, argument_list; 206, identifier:port; 207, identifier:port_id; 208, boolean_operator; 209, comment:# Same port/Same Vlan - skip duplicate; 210, block; 211, else_clause; 212, if_statement; 213, expression_statement; 214, expression_statement; 215, if_statement; 216, comment:# Start tracking new port; 217, if_statement; 218, if_statement; 219, expression_statement; 220, if_statement; 221, identifier:LOG; 222, identifier:debug; 223, concatenated_string; 224, identifier:switch_ip; 225, attribute; 226, identifier:vlan_count; 227, identifier:duplicate_port; 228, identifier:self; 229, identifier:_restore_port_binding; 230, identifier:switch_ip; 231, identifier:pvlans; 232, identifier:prev_port; 233, identifier:prev_native_vlan; 234, identifier:vlans; 235, identifier:sort; 236, identifier:vlan; 237, identifier:vni; 238, identifier:vlans; 239, integer:0; 240, call; 241, expression_statement; 242, identifier:x; 243, attribute; 244, attribute; 245, attribute; 246, identifier:self; 247, identifier:_gather_config_parms; 248, call; 249, attribute; 250, comparison_operator:port.vlan_id == prev_vlan; 251, comparison_operator:port.vni == prev_vni; 252, expression_statement; 253, continue_statement; 254, comment:# Same port/different Vlan - track it; 255, block; 256, identifier:prev_port; 257, block; 258, assignment; 259, assignment; 260, identifier:pvlans; 261, block; 262, identifier:auto_create; 263, block; 264, identifier:auto_trunk; 265, block; 266, assignment; 267, attribute; 268, block; 269, string:"Switch %s port %s replay summary: unique vlan "; 270, string:"count %d, duplicate port entries %d"; 271, identifier:port; 272, identifier:port_id; 273, attribute; 274, argument_list; 275, call; 276, identifier:x; 277, identifier:port_id; 278, identifier:x; 279, identifier:vlan_id; 280, identifier:x; 281, identifier:vni; 282, attribute; 283, argument_list; 284, identifier:port; 285, identifier:vlan_id; 286, attribute; 287, identifier:prev_vlan; 288, attribute; 289, identifier:prev_vni; 290, augmented_assignment; 291, expression_statement; 292, if_statement; 293, if_statement; 294, if_statement; 295, expression_statement; 296, expression_statement; 297, identifier:duplicate_port; 298, integer:0; 299, identifier:vlan_count; 300, integer:0; 301, expression_statement; 302, expression_statement; 303, expression_statement; 304, expression_statement; 305, expression_statement; 306, identifier:prev_port; 307, attribute; 308, identifier:port; 309, identifier:is_native; 310, expression_statement; 311, identifier:self; 312, identifier:_save_switch_vlan_range; 313, identifier:switch_ip; 314, identifier:vlans; 315, attribute; 316, argument_list; 317, identifier:nxos_db; 318, identifier:is_provider_vlan; 319, attribute; 320, identifier:port; 321, identifier:vlan_id; 322, identifier:port; 323, identifier:vni; 324, identifier:duplicate_port; 325, integer:1; 326, augmented_assignment; 327, identifier:auto_create; 328, block; 329, identifier:auto_trunk; 330, block; 331, attribute; 332, block; 333, augmented_assignment; 334, call; 335, call; 336, call; 337, assignment; 338, call; 339, call; 340, identifier:port; 341, identifier:port_id; 342, assignment; 343, identifier:self; 344, identifier:_save_switch_vxlan_range; 345, identifier:switch_ip; 346, identifier:vlans; 347, identifier:port; 348, identifier:vlan_id; 349, identifier:vlan_count; 350, integer:1; 351, expression_statement; 352, expression_statement; 353, identifier:port; 354, identifier:is_native; 355, expression_statement; 356, identifier:interface_count; 357, integer:1; 358, attribute; 359, argument_list; 360, attribute; 361, argument_list; 362, attribute; 363, argument_list; 364, identifier:prev_native_vlan; 365, integer:0; 366, attribute; 367, argument_list; 368, attribute; 369, argument_list; 370, identifier:prev_native_vlan; 371, attribute; 372, call; 373, call; 374, assignment; 375, identifier:LOG; 376, identifier:debug; 377, concatenated_string; 378, identifier:switch_ip; 379, identifier:prev_port; 380, identifier:vlan_count; 381, identifier:duplicate_port; 382, identifier:self; 383, identifier:_restore_port_binding; 384, identifier:switch_ip; 385, identifier:pvlans; 386, identifier:prev_port; 387, identifier:prev_native_vlan; 388, identifier:pvlans; 389, identifier:clear; 390, identifier:vlans; 391, identifier:add; 392, tuple; 393, identifier:pvlans; 394, identifier:add; 395, attribute; 396, identifier:port; 397, identifier:vlan_id; 398, attribute; 399, argument_list; 400, attribute; 401, argument_list; 402, identifier:prev_native_vlan; 403, attribute; 404, string:"Switch %s port %s replay summary: unique vlan "; 405, string:"count %d, duplicate port entries %d"; 406, attribute; 407, attribute; 408, identifier:port; 409, identifier:vlan_id; 410, identifier:vlans; 411, identifier:add; 412, tuple; 413, identifier:pvlans; 414, identifier:add; 415, attribute; 416, identifier:port; 417, identifier:vlan_id; 418, identifier:port; 419, identifier:vlan_id; 420, identifier:port; 421, identifier:vni; 422, attribute; 423, attribute; 424, identifier:port; 425, identifier:vlan_id; 426, identifier:port; 427, identifier:vlan_id; 428, identifier:port; 429, identifier:vni
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 4, 25; 4, 26; 4, 27; 4, 28; 4, 29; 4, 30; 8, 31; 9, 32; 10, 33; 11, 34; 12, 35; 13, 36; 14, 37; 15, 38; 17, 39; 18, 40; 19, 41; 20, 42; 21, 43; 22, 44; 22, 45; 22, 46; 23, 47; 23, 48; 24, 49; 25, 50; 26, 51; 27, 52; 27, 53; 28, 54; 29, 55; 30, 56; 32, 57; 32, 58; 33, 59; 33, 60; 34, 61; 34, 62; 35, 63; 35, 64; 36, 65; 36, 66; 37, 67; 37, 68; 38, 69; 38, 70; 39, 71; 39, 72; 40, 73; 40, 74; 41, 75; 41, 76; 42, 77; 42, 78; 43, 79; 43, 80; 46, 81; 46, 82; 46, 83; 48, 84; 48, 85; 49, 86; 49, 87; 50, 88; 50, 89; 51, 90; 51, 91; 53, 92; 53, 93; 53, 94; 54, 95; 54, 96; 55, 97; 55, 98; 56, 99; 56, 100; 58, 101; 60, 102; 66, 103; 66, 104; 67, 105; 67, 106; 68, 107; 69, 108; 69, 109; 70, 110; 70, 111; 70, 112; 72, 113; 72, 114; 74, 115; 74, 116; 81, 117; 81, 118; 82, 119; 83, 120; 83, 121; 83, 122; 84, 123; 85, 124; 86, 125; 86, 126; 87, 127; 87, 128; 87, 129; 88, 130; 88, 131; 89, 132; 89, 133; 89, 134; 91, 135; 91, 136; 92, 137; 93, 138; 94, 139; 94, 140; 94, 141; 95, 142; 95, 143; 96, 144; 96, 145; 97, 146; 97, 147; 98, 148; 99, 149; 99, 150; 100, 151; 100, 152; 100, 153; 103, 154; 103, 155; 107, 156; 107, 157; 108, 158; 108, 159; 112, 160; 112, 161; 117, 162; 117, 163; 118, 164; 119, 165; 119, 166; 120, 167; 120, 168; 121, 169; 122, 170; 122, 171; 123, 172; 123, 173; 124, 174; 124, 175; 128, 176; 128, 177; 130, 178; 130, 179; 134, 180; 134, 181; 136, 182; 137, 183; 137, 184; 138, 185; 138, 186; 139, 187; 139, 188; 140, 189; 141, 190; 145, 191; 145, 192; 149, 193; 149, 194; 153, 195; 153, 196; 157, 197; 157, 198; 162, 199; 162, 200; 163, 201; 165, 202; 165, 203; 166, 204; 166, 205; 167, 206; 167, 207; 169, 208; 169, 209; 169, 210; 169, 211; 171, 212; 171, 213; 171, 214; 171, 215; 171, 216; 171, 217; 171, 218; 171, 219; 171, 220; 172, 221; 172, 222; 173, 223; 173, 224; 173, 225; 173, 226; 173, 227; 174, 228; 174, 229; 175, 230; 175, 231; 175, 232; 175, 233; 183, 234; 183, 235; 185, 236; 185, 237; 186, 238; 186, 239; 189, 240; 190, 241; 197, 242; 198, 243; 198, 244; 198, 245; 204, 246; 204, 247; 205, 248; 205, 249; 208, 250; 208, 251; 210, 252; 210, 253; 211, 254; 211, 255; 212, 256; 212, 257; 213, 258; 214, 259; 215, 260; 215, 261; 217, 262; 217, 263; 218, 264; 218, 265; 219, 266; 220, 267; 220, 268; 223, 269; 223, 270; 225, 271; 225, 272; 240, 273; 240, 274; 241, 275; 243, 276; 243, 277; 244, 278; 244, 279; 245, 280; 245, 281; 248, 282; 248, 283; 249, 284; 249, 285; 250, 286; 250, 287; 251, 288; 251, 289; 252, 290; 255, 291; 255, 292; 255, 293; 255, 294; 257, 295; 257, 296; 258, 297; 258, 298; 259, 299; 259, 300; 261, 301; 261, 302; 261, 303; 263, 304; 265, 305; 266, 306; 266, 307; 267, 308; 267, 309; 268, 310; 273, 311; 273, 312; 274, 313; 274, 314; 275, 315; 275, 316; 282, 317; 282, 318; 283, 319; 286, 320; 286, 321; 288, 322; 288, 323; 290, 324; 290, 325; 291, 326; 292, 327; 292, 328; 293, 329; 293, 330; 294, 331; 294, 332; 295, 333; 296, 334; 301, 335; 302, 336; 303, 337; 304, 338; 305, 339; 307, 340; 307, 341; 310, 342; 315, 343; 315, 344; 316, 345; 316, 346; 319, 347; 319, 348; 326, 349; 326, 350; 328, 351; 330, 352; 331, 353; 331, 354; 332, 355; 333, 356; 333, 357; 334, 358; 334, 359; 335, 360; 335, 361; 336, 362; 336, 363; 337, 364; 337, 365; 338, 366; 338, 367; 339, 368; 339, 369; 342, 370; 342, 371; 351, 372; 352, 373; 355, 374; 358, 375; 358, 376; 359, 377; 359, 378; 359, 379; 359, 380; 359, 381; 360, 382; 360, 383; 361, 384; 361, 385; 361, 386; 361, 387; 362, 388; 362, 389; 366, 390; 366, 391; 367, 392; 368, 393; 368, 394; 369, 395; 371, 396; 371, 397; 372, 398; 372, 399; 373, 400; 373, 401; 374, 402; 374, 403; 377, 404; 377, 405; 392, 406; 392, 407; 395, 408; 395, 409; 398, 410; 398, 411; 399, 412; 400, 413; 400, 414; 401, 415; 403, 416; 403, 417; 406, 418; 406, 419; 407, 420; 407, 421; 412, 422; 412, 423; 415, 424; 415, 425; 422, 426; 422, 427; 423, 428; 423, 429
def configure_switch_entries(self, switch_ip, port_bindings): """Create a nexus switch entry in Nexus. The port_bindings is sorted by vlan_id, vni, port_id. When there is a change in vlan_id or vni, then vlan data is configured in Nexus device. Otherwise we check if there is a change in port_id where we configure the port with vlan trunk config. Called during switch replay event. """ prev_vlan = -1 prev_vni = -1 prev_port = None prev_native_vlan = 0 starttime = time.time() port_bindings.sort(key=lambda x: (x.port_id, x.vlan_id, x.vni)) self.driver.capture_and_print_timeshot( starttime, "replay_t2_aft_sort", switch=switch_ip) # Let's make these lists a set to exclude duplicates vlans = set() pvlans = set() interface_count = 0 duplicate_port = 0 vlan_count = 0 for port in port_bindings: if nxos_db.is_reserved_binding(port): continue auto_create, auto_trunk = self._gather_config_parms( nxos_db.is_provider_vlan(port.vlan_id), port.vlan_id) if port.port_id == prev_port: if port.vlan_id == prev_vlan and port.vni == prev_vni: # Same port/Same Vlan - skip duplicate duplicate_port += 1 continue else: # Same port/different Vlan - track it vlan_count += 1 if auto_create: vlans.add((port.vlan_id, port.vni)) if auto_trunk: pvlans.add(port.vlan_id) if port.is_native: prev_native_vlan = port.vlan_id else: # Different port - write out interface trunk on previous port if prev_port: interface_count += 1 LOG.debug("Switch %s port %s replay summary: unique vlan " "count %d, duplicate port entries %d", switch_ip, prev_port, vlan_count, duplicate_port) duplicate_port = 0 vlan_count = 0 if pvlans: self._restore_port_binding( switch_ip, pvlans, prev_port, prev_native_vlan) pvlans.clear() prev_native_vlan = 0 # Start tracking new port if auto_create: vlans.add((port.vlan_id, port.vni)) if auto_trunk: pvlans.add(port.vlan_id) prev_port = port.port_id if port.is_native: prev_native_vlan = port.vlan_id if pvlans: LOG.debug("Switch %s port %s replay summary: unique vlan " "count %d, duplicate port entries %d", switch_ip, port.port_id, vlan_count, duplicate_port) self._restore_port_binding( switch_ip, pvlans, prev_port, prev_native_vlan) LOG.debug("Replayed total %d ports for Switch %s", interface_count + 1, switch_ip) self.driver.capture_and_print_timeshot( starttime, "replay_part_1", switch=switch_ip) vlans = list(vlans) if vlans: vlans.sort() vlan, vni = vlans[0] if vni == 0: self._save_switch_vlan_range(switch_ip, vlans) else: self._save_switch_vxlan_range(switch_ip, vlans) self.set_switch_ip_and_active_state( switch_ip, const.SWITCH_RESTORE_S2) self.configure_next_batch_of_vlans(switch_ip) self.driver.capture_and_print_timeshot( starttime, "replay_part_2", switch=switch_ip)
0, module; 1, function_definition; 2, function_name:_sort_resources_per_hosting_device; 3, parameters; 4, block; 5, identifier:resources; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, return_statement; 10, comment:"""This function will sort the resources on hosting device. The sorting on hosting device is done by looking up the `hosting_device` attribute of the resource, and its `id`. :param resources: a dict with key of resource name :return dict sorted on the hosting device of input resource. Format: hosting_devices = { 'hd_id1' : {'routers':[routers], 'removed_routers':[routers], .... } 'hd_id2' : {'routers':[routers], .. } ....... } """; 11, assignment; 12, identifier:key; 13, call; 14, block; 15, identifier:hosting_devices; 16, identifier:hosting_devices; 17, dictionary; 18, attribute; 19, argument_list; 20, for_statement; 21, identifier:resources; 22, identifier:keys; 23, identifier:r; 24, boolean_operator; 25, block; 26, call; 27, list; 28, if_statement; 29, expression_statement; 30, expression_statement; 31, expression_statement; 32, attribute; 33, argument_list; 34, comparison_operator:r.get('hosting_device') is None; 35, block; 36, assignment; 37, call; 38, call; 39, identifier:resources; 40, identifier:get; 41, identifier:key; 42, call; 43, None; 44, continue_statement; 45, identifier:hd_id; 46, subscript; 47, attribute; 48, argument_list; 49, attribute; 50, argument_list; 51, attribute; 52, argument_list; 53, subscript; 54, string; 55, identifier:hosting_devices; 56, identifier:setdefault; 57, identifier:hd_id; 58, dictionary; 59, call; 60, identifier:append; 61, identifier:r; 62, identifier:r; 63, identifier:get; 64, string; 65, identifier:r; 66, string; 67, string_content:id; 68, attribute; 69, argument_list; 70, string_content:hosting_device; 71, string_content:hosting_device; 72, subscript; 73, identifier:setdefault; 74, identifier:key; 75, list; 76, identifier:hosting_devices; 77, identifier:hd_id
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 6, 10; 7, 11; 8, 12; 8, 13; 8, 14; 9, 15; 11, 16; 11, 17; 13, 18; 13, 19; 14, 20; 18, 21; 18, 22; 20, 23; 20, 24; 20, 25; 24, 26; 24, 27; 25, 28; 25, 29; 25, 30; 25, 31; 26, 32; 26, 33; 28, 34; 28, 35; 29, 36; 30, 37; 31, 38; 32, 39; 32, 40; 33, 41; 34, 42; 34, 43; 35, 44; 36, 45; 36, 46; 37, 47; 37, 48; 38, 49; 38, 50; 42, 51; 42, 52; 46, 53; 46, 54; 47, 55; 47, 56; 48, 57; 48, 58; 49, 59; 49, 60; 50, 61; 51, 62; 51, 63; 52, 64; 53, 65; 53, 66; 54, 67; 59, 68; 59, 69; 64, 70; 66, 71; 68, 72; 68, 73; 69, 74; 69, 75; 72, 76; 72, 77
def _sort_resources_per_hosting_device(resources): """This function will sort the resources on hosting device. The sorting on hosting device is done by looking up the `hosting_device` attribute of the resource, and its `id`. :param resources: a dict with key of resource name :return dict sorted on the hosting device of input resource. Format: hosting_devices = { 'hd_id1' : {'routers':[routers], 'removed_routers':[routers], .... } 'hd_id2' : {'routers':[routers], .. } ....... } """ hosting_devices = {} for key in resources.keys(): for r in resources.get(key) or []: if r.get('hosting_device') is None: continue hd_id = r['hosting_device']['id'] hosting_devices.setdefault(hd_id, {}) hosting_devices[hd_id].setdefault(key, []).append(r) return hosting_devices
0, module; 1, function_definition; 2, function_name:compute; 3, parameters; 4, block; 5, identifier:self; 6, identifier:t; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, expression_statement; 13, expression_statement; 14, if_statement; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, expression_statement; 23, expression_statement; 24, expression_statement; 25, identifier:yerr; 26, float:1.123e-12; 27, identifier:check_sorted; 28, True; 29, identifier:A; 30, None; 31, identifier:U; 32, None; 33, identifier:V; 34, None; 35, comment:""" Compute the extended form of the covariance matrix and factorize Args: x (array[n]): The independent coordinates of the data points. This array must be _sorted_ in ascending order. yerr (Optional[float or array[n]]): The measurement uncertainties for the data points at coordinates ``x``. These values will be added in quadrature to the diagonal of the covariance matrix. (default: ``1.123e-12``) check_sorted (bool): If ``True``, ``x`` will be checked to make sure that it is properly sorted. If ``False``, the coordinates will be assumed to be in the correct order. Raises: ValueError: For un-sorted data or mismatched dimensions. solver.LinAlgError: For non-positive definite matrices. """; 36, assignment; 37, boolean_operator; 38, block; 39, boolean_operator; 40, block; 41, assignment; 42, assignment; 43, assignment; 44, assignment; 45, assignment; 46, assignment; 47, assignment; 48, call; 49, assignment; 50, identifier:t; 51, call; 52, identifier:check_sorted; 53, call; 54, raise_statement; 55, identifier:check_sorted; 56, comparison_operator:len(t.shape) > 1; 57, raise_statement; 58, attribute; 59, identifier:t; 60, attribute; 61, call; 62, subscript; 63, identifier:yerr; 64, tuple_pattern; 65, attribute; 66, attribute; 67, conditional_expression:np.empty(0) if A is None else A; 68, attribute; 69, conditional_expression:np.empty((0, 0)) if U is None else U; 70, attribute; 71, conditional_expression:np.empty((0, 0)) if V is None else V; 72, attribute; 73, argument_list; 74, attribute; 75, False; 76, attribute; 77, argument_list; 78, attribute; 79, argument_list; 80, call; 81, call; 82, integer:1; 83, call; 84, identifier:self; 85, identifier:_t; 86, identifier:self; 87, identifier:_yerr; 88, attribute; 89, argument_list; 90, attribute; 91, slice; 92, identifier:alpha_real; 93, identifier:beta_real; 94, identifier:alpha_complex_real; 95, identifier:alpha_complex_imag; 96, identifier:beta_complex_real; 97, identifier:beta_complex_imag; 98, attribute; 99, identifier:coefficients; 100, identifier:self; 101, identifier:_A; 102, call; 103, comparison_operator:A is None; 104, identifier:A; 105, identifier:self; 106, identifier:_U; 107, call; 108, comparison_operator:U is None; 109, identifier:U; 110, identifier:self; 111, identifier:_V; 112, call; 113, comparison_operator:V is None; 114, identifier:V; 115, attribute; 116, identifier:compute; 117, attribute; 118, identifier:alpha_real; 119, identifier:beta_real; 120, identifier:alpha_complex_real; 121, identifier:alpha_complex_imag; 122, identifier:beta_complex_real; 123, identifier:beta_complex_imag; 124, attribute; 125, attribute; 126, attribute; 127, identifier:t; 128, binary_operator:self._yerr**2; 129, identifier:self; 130, identifier:dirty; 131, identifier:np; 132, identifier:atleast_1d; 133, identifier:t; 134, identifier:np; 135, identifier:any; 136, comparison_operator:np.diff(t) < 0.0; 137, identifier:ValueError; 138, argument_list; 139, identifier:len; 140, argument_list; 141, identifier:ValueError; 142, argument_list; 143, identifier:np; 144, identifier:empty_like; 145, attribute; 146, identifier:self; 147, identifier:_yerr; 148, identifier:self; 149, identifier:kernel; 150, attribute; 151, argument_list; 152, identifier:A; 153, None; 154, attribute; 155, argument_list; 156, identifier:U; 157, None; 158, attribute; 159, argument_list; 160, identifier:V; 161, None; 162, identifier:self; 163, identifier:solver; 164, attribute; 165, identifier:jitter; 166, identifier:self; 167, identifier:_A; 168, identifier:self; 169, identifier:_U; 170, identifier:self; 171, identifier:_V; 172, attribute; 173, integer:2; 174, call; 175, float:0.0; 176, string:"the input coordinates must be sorted"; 177, attribute; 178, string:"dimension mismatch"; 179, identifier:self; 180, identifier:_t; 181, identifier:np; 182, identifier:empty; 183, integer:0; 184, identifier:np; 185, identifier:empty; 186, tuple; 187, identifier:np; 188, identifier:empty; 189, tuple; 190, identifier:self; 191, identifier:kernel; 192, identifier:self; 193, identifier:_yerr; 194, attribute; 195, argument_list; 196, identifier:t; 197, identifier:shape; 198, integer:0; 199, integer:0; 200, integer:0; 201, integer:0; 202, identifier:np; 203, identifier:diff; 204, identifier:t
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 7, 25; 7, 26; 8, 27; 8, 28; 9, 29; 9, 30; 10, 31; 10, 32; 11, 33; 11, 34; 12, 35; 13, 36; 14, 37; 14, 38; 15, 39; 15, 40; 16, 41; 17, 42; 18, 43; 19, 44; 20, 45; 21, 46; 22, 47; 23, 48; 24, 49; 36, 50; 36, 51; 37, 52; 37, 53; 38, 54; 39, 55; 39, 56; 40, 57; 41, 58; 41, 59; 42, 60; 42, 61; 43, 62; 43, 63; 44, 64; 44, 65; 45, 66; 45, 67; 46, 68; 46, 69; 47, 70; 47, 71; 48, 72; 48, 73; 49, 74; 49, 75; 51, 76; 51, 77; 53, 78; 53, 79; 54, 80; 56, 81; 56, 82; 57, 83; 58, 84; 58, 85; 60, 86; 60, 87; 61, 88; 61, 89; 62, 90; 62, 91; 64, 92; 64, 93; 64, 94; 64, 95; 64, 96; 64, 97; 65, 98; 65, 99; 66, 100; 66, 101; 67, 102; 67, 103; 67, 104; 68, 105; 68, 106; 69, 107; 69, 108; 69, 109; 70, 110; 70, 111; 71, 112; 71, 113; 71, 114; 72, 115; 72, 116; 73, 117; 73, 118; 73, 119; 73, 120; 73, 121; 73, 122; 73, 123; 73, 124; 73, 125; 73, 126; 73, 127; 73, 128; 74, 129; 74, 130; 76, 131; 76, 132; 77, 133; 78, 134; 78, 135; 79, 136; 80, 137; 80, 138; 81, 139; 81, 140; 83, 141; 83, 142; 88, 143; 88, 144; 89, 145; 90, 146; 90, 147; 98, 148; 98, 149; 102, 150; 102, 151; 103, 152; 103, 153; 107, 154; 107, 155; 108, 156; 108, 157; 112, 158; 112, 159; 113, 160; 113, 161; 115, 162; 115, 163; 117, 164; 117, 165; 124, 166; 124, 167; 125, 168; 125, 169; 126, 170; 126, 171; 128, 172; 128, 173; 136, 174; 136, 175; 138, 176; 140, 177; 142, 178; 145, 179; 145, 180; 150, 181; 150, 182; 151, 183; 154, 184; 154, 185; 155, 186; 158, 187; 158, 188; 159, 189; 164, 190; 164, 191; 172, 192; 172, 193; 174, 194; 174, 195; 177, 196; 177, 197; 186, 198; 186, 199; 189, 200; 189, 201; 194, 202; 194, 203; 195, 204
def compute(self, t, yerr=1.123e-12, check_sorted=True, A=None, U=None, V=None): """ Compute the extended form of the covariance matrix and factorize Args: x (array[n]): The independent coordinates of the data points. This array must be _sorted_ in ascending order. yerr (Optional[float or array[n]]): The measurement uncertainties for the data points at coordinates ``x``. These values will be added in quadrature to the diagonal of the covariance matrix. (default: ``1.123e-12``) check_sorted (bool): If ``True``, ``x`` will be checked to make sure that it is properly sorted. If ``False``, the coordinates will be assumed to be in the correct order. Raises: ValueError: For un-sorted data or mismatched dimensions. solver.LinAlgError: For non-positive definite matrices. """ t = np.atleast_1d(t) if check_sorted and np.any(np.diff(t) < 0.0): raise ValueError("the input coordinates must be sorted") if check_sorted and len(t.shape) > 1: raise ValueError("dimension mismatch") self._t = t self._yerr = np.empty_like(self._t) self._yerr[:] = yerr (alpha_real, beta_real, alpha_complex_real, alpha_complex_imag, beta_complex_real, beta_complex_imag) = self.kernel.coefficients self._A = np.empty(0) if A is None else A self._U = np.empty((0, 0)) if U is None else U self._V = np.empty((0, 0)) if V is None else V self.solver.compute( self.kernel.jitter, alpha_real, beta_real, alpha_complex_real, alpha_complex_imag, beta_complex_real, beta_complex_imag, self._A, self._U, self._V, t, self._yerr**2 ) self.dirty = False
0, module; 1, function_definition; 2, function_name:group_dict; 3, parameters; 4, block; 5, identifier:items; 6, identifier:keyfunc; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, return_statement; 11, comment:"""Return a list defaultdict with ``items`` grouped by ``keyfunc``. >>> sorted(group_dict('eggs', lambda x: x).items()) [('e', ['e']), ('g', ['g', 'g']), ('s', ['s'])] """; 12, assignment; 13, identifier:i; 14, identifier:items; 15, block; 16, identifier:result; 17, identifier:result; 18, call; 19, expression_statement; 20, expression_statement; 21, attribute; 22, argument_list; 23, assignment; 24, call; 25, identifier:collections; 26, identifier:defaultdict; 27, identifier:list; 28, identifier:key; 29, call; 30, attribute; 31, argument_list; 32, identifier:keyfunc; 33, argument_list; 34, subscript; 35, identifier:append; 36, identifier:i; 37, identifier:i; 38, identifier:result; 39, identifier:key
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 7, 11; 8, 12; 9, 13; 9, 14; 9, 15; 10, 16; 12, 17; 12, 18; 15, 19; 15, 20; 18, 21; 18, 22; 19, 23; 20, 24; 21, 25; 21, 26; 22, 27; 23, 28; 23, 29; 24, 30; 24, 31; 29, 32; 29, 33; 30, 34; 30, 35; 31, 36; 33, 37; 34, 38; 34, 39
def group_dict(items, keyfunc): """Return a list defaultdict with ``items`` grouped by ``keyfunc``. >>> sorted(group_dict('eggs', lambda x: x).items()) [('e', ['e']), ('g', ['g', 'g']), ('s', ['s'])] """ result = collections.defaultdict(list) for i in items: key = keyfunc(i) result[key].append(i) return result
0, module; 1, function_definition; 2, function_name:_split_dict; 3, parameters; 4, block; 5, identifier:dic; 6, expression_statement; 7, expression_statement; 8, return_statement; 9, string; 10, assignment; 11, expression_list; 12, string_content:Split dict into sorted keys and values >>> _split_dict({'b': 2, 'a': 1}) (['a', 'b'], [1, 2]); 13, identifier:keys; 14, call; 15, identifier:keys; 16, list_comprehension; 17, identifier:sorted; 18, argument_list; 19, subscript; 20, for_in_clause; 21, call; 22, identifier:dic; 23, identifier:k; 24, identifier:k; 25, identifier:keys; 26, attribute; 27, argument_list; 28, identifier:dic; 29, identifier:keys
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 6, 9; 7, 10; 8, 11; 9, 12; 10, 13; 10, 14; 11, 15; 11, 16; 14, 17; 14, 18; 16, 19; 16, 20; 18, 21; 19, 22; 19, 23; 20, 24; 20, 25; 21, 26; 21, 27; 26, 28; 26, 29
def _split_dict(dic): '''Split dict into sorted keys and values >>> _split_dict({'b': 2, 'a': 1}) (['a', 'b'], [1, 2]) ''' keys = sorted(dic.keys()) return keys, [dic[k] for k in keys]
0, module; 1, function_definition; 2, function_name:monitor; 3, parameters; 4, block; 5, identifier:args; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, function_definition; 13, comment:# FIXME: this will generally return different column order between Python 2/3; 14, return_statement; 15, string; 16, assignment; 17, call; 18, assignment; 19, assignment; 20, assignment; 21, function_name:expander; 22, parameters; 23, block; 24, binary_operator:[header] + ['\t'.join( map(expander, v.values())) for v in statuses]; 25, string_content:Retrieve status of jobs submitted from a given workspace, as a list of TSV lines sorted by descending order of job submission date; 26, identifier:r; 27, call; 28, attribute; 29, argument_list; 30, identifier:statuses; 31, call; 32, identifier:header; 33, call; 34, identifier:expander; 35, lambda; 36, identifier:thing; 37, if_statement; 38, return_statement; 39, list; 40, list_comprehension; 41, attribute; 42, argument_list; 43, identifier:fapi; 44, identifier:_check_response_code; 45, identifier:r; 46, integer:200; 47, identifier:sorted; 48, argument_list; 49, attribute; 50, argument_list; 51, lambda_parameters; 52, call; 53, call; 54, block; 55, call; 56, identifier:header; 57, call; 58, for_in_clause; 59, identifier:fapi; 60, identifier:list_submissions; 61, attribute; 62, attribute; 63, call; 64, keyword_argument; 65, keyword_argument; 66, string; 67, identifier:join; 68, call; 69, identifier:v; 70, attribute; 71, argument_list; 72, identifier:isinstance; 73, argument_list; 74, expression_statement; 75, if_statement; 76, attribute; 77, argument_list; 78, attribute; 79, argument_list; 80, identifier:v; 81, identifier:statuses; 82, identifier:args; 83, identifier:project; 84, identifier:args; 85, identifier:workspace; 86, attribute; 87, argument_list; 88, identifier:key; 89, lambda; 90, identifier:reverse; 91, True; 92, string_content; 93, identifier:list; 94, argument_list; 95, string; 96, identifier:format; 97, identifier:v; 98, identifier:thing; 99, identifier:dict; 100, assignment; 101, identifier:entityType; 102, block; 103, string:"{0}"; 104, identifier:format; 105, identifier:thing; 106, string; 107, identifier:join; 108, call; 109, identifier:r; 110, identifier:json; 111, lambda_parameters; 112, subscript; 113, escape_sequence:\t; 114, call; 115, string_content:{0}; 116, identifier:entityType; 117, call; 118, return_statement; 119, string_content; 120, identifier:map; 121, argument_list; 122, identifier:k; 123, identifier:k; 124, string; 125, attribute; 126, argument_list; 127, attribute; 128, argument_list; 129, call; 130, escape_sequence:\t; 131, identifier:expander; 132, call; 133, string_content:submissionDate; 134, subscript; 135, identifier:keys; 136, identifier:thing; 137, identifier:get; 138, string:"entityType"; 139, None; 140, attribute; 141, argument_list; 142, attribute; 143, argument_list; 144, identifier:statuses; 145, integer:0; 146, string:"{0}:{1}"; 147, identifier:format; 148, identifier:entityType; 149, subscript; 150, identifier:v; 151, identifier:values; 152, identifier:thing; 153, string; 154, string_content:entityName
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 6, 15; 7, 16; 8, 17; 9, 18; 10, 19; 11, 20; 12, 21; 12, 22; 12, 23; 14, 24; 15, 25; 16, 26; 16, 27; 17, 28; 17, 29; 18, 30; 18, 31; 19, 32; 19, 33; 20, 34; 20, 35; 22, 36; 23, 37; 23, 38; 24, 39; 24, 40; 27, 41; 27, 42; 28, 43; 28, 44; 29, 45; 29, 46; 31, 47; 31, 48; 33, 49; 33, 50; 35, 51; 35, 52; 37, 53; 37, 54; 38, 55; 39, 56; 40, 57; 40, 58; 41, 59; 41, 60; 42, 61; 42, 62; 48, 63; 48, 64; 48, 65; 49, 66; 49, 67; 50, 68; 51, 69; 52, 70; 52, 71; 53, 72; 53, 73; 54, 74; 54, 75; 55, 76; 55, 77; 57, 78; 57, 79; 58, 80; 58, 81; 61, 82; 61, 83; 62, 84; 62, 85; 63, 86; 63, 87; 64, 88; 64, 89; 65, 90; 65, 91; 66, 92; 68, 93; 68, 94; 70, 95; 70, 96; 71, 97; 73, 98; 73, 99; 74, 100; 75, 101; 75, 102; 76, 103; 76, 104; 77, 105; 78, 106; 78, 107; 79, 108; 86, 109; 86, 110; 89, 111; 89, 112; 92, 113; 94, 114; 95, 115; 100, 116; 100, 117; 102, 118; 106, 119; 108, 120; 108, 121; 111, 122; 112, 123; 112, 124; 114, 125; 114, 126; 117, 127; 117, 128; 118, 129; 119, 130; 121, 131; 121, 132; 124, 133; 125, 134; 125, 135; 127, 136; 127, 137; 128, 138; 128, 139; 129, 140; 129, 141; 132, 142; 132, 143; 134, 144; 134, 145; 140, 146; 140, 147; 141, 148; 141, 149; 142, 150; 142, 151; 149, 152; 149, 153; 153, 154
def monitor(args): ''' Retrieve status of jobs submitted from a given workspace, as a list of TSV lines sorted by descending order of job submission date''' r = fapi.list_submissions(args.project, args.workspace) fapi._check_response_code(r, 200) statuses = sorted(r.json(), key=lambda k: k['submissionDate'], reverse=True) header = '\t'.join(list(statuses[0].keys())) expander = lambda v: '{0}'.format(v) def expander(thing): if isinstance(thing, dict): entityType = thing.get("entityType", None) if entityType: return "{0}:{1}".format(entityType, thing['entityName']) return "{0}".format(thing) # FIXME: this will generally return different column order between Python 2/3 return [header] + ['\t'.join( map(expander, v.values())) for v in statuses]
0, module; 1, function_definition; 2, function_name:tcsort; 3, parameters; 4, comment:# FIXME SUCH WOW SO INEFFICIENT O_O; 5, block; 6, identifier:item; 7, expression_statement; 8, return_statement; 9, comment:""" get len of transitive closure assume type items is tree... """; 10, binary_operator:len(item[1]) + sum(tcsort(kv) for kv in item[1].items()); 11, call; 12, call; 13, identifier:len; 14, argument_list; 15, identifier:sum; 16, generator_expression; 17, subscript; 18, call; 19, for_in_clause; 20, identifier:item; 21, integer:1; 22, identifier:tcsort; 23, argument_list; 24, identifier:kv; 25, call; 26, identifier:kv; 27, attribute; 28, argument_list; 29, subscript; 30, identifier:items; 31, identifier:item; 32, integer:1
0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 5, 7; 5, 8; 7, 9; 8, 10; 10, 11; 10, 12; 11, 13; 11, 14; 12, 15; 12, 16; 14, 17; 16, 18; 16, 19; 17, 20; 17, 21; 18, 22; 18, 23; 19, 24; 19, 25; 23, 26; 25, 27; 25, 28; 27, 29; 27, 30; 29, 31; 29, 32
def tcsort(item): # FIXME SUCH WOW SO INEFFICIENT O_O """ get len of transitive closure assume type items is tree... """ return len(item[1]) + sum(tcsort(kv) for kv in item[1].items())
0, module; 1, function_definition; 2, function_name:sortProperties; 3, parameters; 4, comment:# modified to sort objects using their global rank; 5, block; 6, identifier:self; 7, identifier:properties; 8, expression_statement; 9, comment:# Sort object lists; 10, for_statement; 11, comment:# Make sorted list of properties; 12, return_statement; 13, comment:"""Take a hash from predicate uris to lists of values. Sort the lists of values. Return a sorted list of properties."""; 14, pattern_list; 15, call; 16, block; 17, call; 18, identifier:prop; 19, identifier:objects; 20, attribute; 21, argument_list; 22, expression_statement; 23, identifier:sorted; 24, argument_list; 25, identifier:properties; 26, identifier:items; 27, call; 28, identifier:properties; 29, keyword_argument; 30, attribute; 31, argument_list; 32, identifier:key; 33, lambda; 34, identifier:objects; 35, identifier:sort; 36, keyword_argument; 37, lambda_parameters; 38, subscript; 39, identifier:key; 40, attribute; 41, identifier:p; 42, attribute; 43, identifier:p; 44, identifier:self; 45, identifier:_globalSortKey; 46, identifier:self; 47, identifier:predicate_rank
0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 3, 7; 5, 8; 5, 9; 5, 10; 5, 11; 5, 12; 8, 13; 10, 14; 10, 15; 10, 16; 12, 17; 14, 18; 14, 19; 15, 20; 15, 21; 16, 22; 17, 23; 17, 24; 20, 25; 20, 26; 22, 27; 24, 28; 24, 29; 27, 30; 27, 31; 29, 32; 29, 33; 30, 34; 30, 35; 31, 36; 33, 37; 33, 38; 36, 39; 36, 40; 37, 41; 38, 42; 38, 43; 40, 44; 40, 45; 42, 46; 42, 47
def sortProperties(self, properties): # modified to sort objects using their global rank """Take a hash from predicate uris to lists of values. Sort the lists of values. Return a sorted list of properties.""" # Sort object lists for prop, objects in properties.items(): objects.sort(key=self._globalSortKey) # Make sorted list of properties return sorted(properties, key=lambda p: self.predicate_rank[p])
0, module; 1, function_definition; 2, function_name:getSubOrder; 3, parameters; 4, block; 5, identifier:existing; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, function_definition; 10, for_statement; 11, expression_statement; 12, function_definition; 13, return_statement; 14, comment:""" Alpha sort by the full chain of parents. """; 15, assignment; 16, assignment; 17, function_name:getDepth; 18, parameters; 19, block; 20, identifier:id_; 21, identifier:existing; 22, block; 23, call; 24, function_name:key_; 25, parameters; 26, block; 27, call; 28, identifier:alpha; 29, subscript; 30, identifier:depths; 31, dictionary; 32, identifier:id_; 33, if_statement; 34, expression_statement; 35, identifier:print; 36, argument_list; 37, identifier:id_; 38, return_statement; 39, identifier:sorted; 40, argument_list; 41, call; 42, integer:0; 43, comparison_operator:id_ in depths; 44, block; 45, else_clause; 46, call; 47, call; 48, subscript; 49, identifier:depths; 50, keyword_argument; 51, identifier:list; 52, argument_list; 53, identifier:id_; 54, identifier:depths; 55, return_statement; 56, block; 57, identifier:getDepth; 58, argument_list; 59, identifier:sorted; 60, argument_list; 61, identifier:depths; 62, identifier:id_; 63, identifier:key; 64, identifier:key_; 65, call; 66, subscript; 67, if_statement; 68, identifier:id_; 69, call; 70, identifier:zip; 71, argument_list; 72, identifier:depths; 73, identifier:id_; 74, comparison_operator:id_ in existing; 75, block; 76, else_clause; 77, attribute; 78, argument_list; 79, list_splat; 80, identifier:id_; 81, identifier:existing; 82, expression_statement; 83, expression_statement; 84, return_statement; 85, block; 86, identifier:depths; 87, identifier:values; 88, call; 89, assignment; 90, assignment; 91, subscript; 92, return_statement; 93, identifier:sorted; 94, argument_list; 95, identifier:names_above; 96, call; 97, subscript; 98, binary_operator:names_above + [existing[id_]['rec']['label']]; 99, identifier:depths; 100, identifier:id_; 101, list; 102, generator_expression; 103, keyword_argument; 104, identifier:getDepth; 105, argument_list; 106, identifier:depths; 107, identifier:id_; 108, identifier:names_above; 109, list; 110, string; 111, tuple; 112, for_in_clause; 113, identifier:key; 114, lambda; 115, subscript; 116, subscript; 117, identifier:k; 118, subscript; 119, pattern_list; 120, call; 121, lambda_parameters; 122, subscript; 123, subscript; 124, string; 125, subscript; 126, string; 127, subscript; 128, string; 129, identifier:k; 130, identifier:v; 131, attribute; 132, argument_list; 133, identifier:a; 134, identifier:a; 135, integer:1; 136, identifier:existing; 137, identifier:id_; 138, string_content:sc; 139, subscript; 140, string; 141, string_content:label; 142, identifier:v; 143, string; 144, string_content:label; 145, identifier:existing; 146, identifier:items; 147, identifier:existing; 148, identifier:id_; 149, string_content:rec; 150, string_content:rec
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 6, 14; 7, 15; 8, 16; 9, 17; 9, 18; 9, 19; 10, 20; 10, 21; 10, 22; 11, 23; 12, 24; 12, 25; 12, 26; 13, 27; 15, 28; 15, 29; 16, 30; 16, 31; 18, 32; 19, 33; 22, 34; 23, 35; 23, 36; 25, 37; 26, 38; 27, 39; 27, 40; 29, 41; 29, 42; 33, 43; 33, 44; 33, 45; 34, 46; 36, 47; 38, 48; 40, 49; 40, 50; 41, 51; 41, 52; 43, 53; 43, 54; 44, 55; 45, 56; 46, 57; 46, 58; 47, 59; 47, 60; 48, 61; 48, 62; 50, 63; 50, 64; 52, 65; 55, 66; 56, 67; 58, 68; 60, 69; 65, 70; 65, 71; 66, 72; 66, 73; 67, 74; 67, 75; 67, 76; 69, 77; 69, 78; 71, 79; 74, 80; 74, 81; 75, 82; 75, 83; 75, 84; 76, 85; 77, 86; 77, 87; 79, 88; 82, 89; 83, 90; 84, 91; 85, 92; 88, 93; 88, 94; 89, 95; 89, 96; 90, 97; 90, 98; 91, 99; 91, 100; 92, 101; 94, 102; 94, 103; 96, 104; 96, 105; 97, 106; 97, 107; 98, 108; 98, 109; 101, 110; 102, 111; 102, 112; 103, 113; 103, 114; 105, 115; 109, 116; 111, 117; 111, 118; 112, 119; 112, 120; 114, 121; 114, 122; 115, 123; 115, 124; 116, 125; 116, 126; 118, 127; 118, 128; 119, 129; 119, 130; 120, 131; 120, 132; 121, 133; 122, 134; 122, 135; 123, 136; 123, 137; 124, 138; 125, 139; 125, 140; 126, 141; 127, 142; 127, 143; 128, 144; 131, 145; 131, 146; 139, 147; 139, 148; 140, 149; 143, 150
def getSubOrder(existing): """ Alpha sort by the full chain of parents. """ alpha = list(zip(*sorted(((k, v['rec']['label']) for k, v in existing.items()), key=lambda a: a[1])))[0] depths = {} def getDepth(id_): if id_ in depths: return depths[id_] else: if id_ in existing: names_above = getDepth(existing[id_]['sc']) depths[id_] = names_above + [existing[id_]['rec']['label']] return depths[id_] else: return [''] for id_ in existing: getDepth(id_) print(sorted(depths.values())) def key_(id_): return depths[id_] return sorted(depths, key=key_)
0, module; 1, function_definition; 2, function_name:insert_sort; 3, parameters; 4, block; 5, identifier:node; 6, identifier:target; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, comment:"""Insert node into sorted position in target tree. Uses sort function and language from target"""; 13, assignment; 14, assignment; 15, assignment; 16, identifier:child; 17, attribute; 18, block; 19, else_clause; 20, identifier:sort; 21, attribute; 22, identifier:lang; 23, attribute; 24, identifier:collator; 25, call; 26, identifier:target; 27, identifier:tree; 28, if_statement; 29, block; 30, identifier:target; 31, identifier:sort; 32, identifier:target; 33, identifier:lang; 34, attribute; 35, argument_list; 36, comparison_operator:collator.compare(sort(child) or '', sort(node) or '') > 0; 37, block; 38, expression_statement; 39, identifier:Collator; 40, identifier:createInstance; 41, conditional_expression:Locale(lang) if lang else Locale(); 42, call; 43, integer:0; 44, expression_statement; 45, break_statement; 46, call; 47, call; 48, identifier:lang; 49, call; 50, attribute; 51, argument_list; 52, call; 53, attribute; 54, argument_list; 55, identifier:Locale; 56, argument_list; 57, identifier:Locale; 58, argument_list; 59, identifier:collator; 60, identifier:compare; 61, boolean_operator; 62, boolean_operator; 63, attribute; 64, argument_list; 65, attribute; 66, identifier:append; 67, identifier:node; 68, identifier:lang; 69, call; 70, string; 71, call; 72, string; 73, identifier:child; 74, identifier:addprevious; 75, identifier:node; 76, identifier:target; 77, identifier:tree; 78, identifier:sort; 79, argument_list; 80, identifier:sort; 81, argument_list; 82, identifier:child; 83, identifier:node
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 7, 12; 8, 13; 9, 14; 10, 15; 11, 16; 11, 17; 11, 18; 11, 19; 13, 20; 13, 21; 14, 22; 14, 23; 15, 24; 15, 25; 17, 26; 17, 27; 18, 28; 19, 29; 21, 30; 21, 31; 23, 32; 23, 33; 25, 34; 25, 35; 28, 36; 28, 37; 29, 38; 34, 39; 34, 40; 35, 41; 36, 42; 36, 43; 37, 44; 37, 45; 38, 46; 41, 47; 41, 48; 41, 49; 42, 50; 42, 51; 44, 52; 46, 53; 46, 54; 47, 55; 47, 56; 49, 57; 49, 58; 50, 59; 50, 60; 51, 61; 51, 62; 52, 63; 52, 64; 53, 65; 53, 66; 54, 67; 56, 68; 61, 69; 61, 70; 62, 71; 62, 72; 63, 73; 63, 74; 64, 75; 65, 76; 65, 77; 69, 78; 69, 79; 71, 80; 71, 81; 79, 82; 81, 83
def insert_sort(node, target): """Insert node into sorted position in target tree. Uses sort function and language from target""" sort = target.sort lang = target.lang collator = Collator.createInstance(Locale(lang) if lang else Locale()) for child in target.tree: if collator.compare(sort(child) or '', sort(node) or '') > 0: child.addprevious(node) break else: target.tree.append(node)
0, module; 1, function_definition; 2, function_name:do_sort_by; 3, parameters; 4, block; 5, identifier:self; 6, identifier:element; 7, identifier:decl; 8, identifier:pseudo; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, comment:# Find current target, set its sort as well; 19, for_statement; 20, comment:"""Implement sort-by declaration - pre-match."""; 21, comparison_operator:',' in decl.value; 22, block; 23, else_clause; 24, assignment; 25, assignment; 26, assignment; 27, assignment; 28, assignment; 29, assignment; 30, assignment; 31, pattern_list; 32, line_continuation:\; 33, call; 34, block; 35, string; 36, attribute; 37, expression_statement; 38, block; 39, identifier:sort; 40, call; 41, identifier:step; 42, subscript; 43, identifier:target; 44, call; 45, attribute; 46, identifier:sort; 47, attribute; 48, subscript; 49, attribute; 50, False; 51, attribute; 52, None; 53, identifier:pos; 54, identifier:action; 55, identifier:enumerate; 56, argument_list; 57, if_statement; 58, string_content:,; 59, identifier:decl; 60, identifier:value; 61, assignment; 62, expression_statement; 63, expression_statement; 64, identifier:css_to_func; 65, argument_list; 66, attribute; 67, subscript; 68, attribute; 69, argument_list; 70, identifier:target; 71, identifier:sort; 72, identifier:target; 73, identifier:lang; 74, attribute; 75, string; 76, identifier:target; 77, identifier:isgroup; 78, identifier:target; 79, identifier:groupby; 80, call; 81, boolean_operator; 82, block; 83, pattern_list; 84, call; 85, assignment; 86, assignment; 87, call; 88, call; 89, attribute; 90, subscript; 91, identifier:self; 92, identifier:state; 93, attribute; 94, string; 95, identifier:self; 96, identifier:current_target; 97, identifier:self; 98, identifier:state; 99, string_content:lang; 100, identifier:reversed; 101, argument_list; 102, comparison_operator:action[0] == 'target'; 103, line_continuation:\; 104, comparison_operator:action[1].tree == element.etree_element; 105, expression_statement; 106, expression_statement; 107, expression_statement; 108, break_statement; 109, identifier:css; 110, identifier:flags; 111, identifier:split; 112, argument_list; 113, identifier:css; 114, attribute; 115, identifier:flags; 116, None; 117, identifier:serialize; 118, argument_list; 119, identifier:serialize; 120, argument_list; 121, identifier:self; 122, identifier:css_namespaces; 123, attribute; 124, string; 125, identifier:self; 126, identifier:state; 127, string_content:current_step; 128, subscript; 129, subscript; 130, string; 131, attribute; 132, attribute; 133, assignment; 134, assignment; 135, assignment; 136, attribute; 137, string; 138, identifier:decl; 139, identifier:value; 140, identifier:css; 141, boolean_operator; 142, identifier:self; 143, identifier:state; 144, string_content:lang; 145, identifier:step; 146, string; 147, identifier:action; 148, integer:0; 149, string_content:target; 150, subscript; 151, identifier:tree; 152, identifier:element; 153, identifier:etree_element; 154, attribute; 155, identifier:sort; 156, attribute; 157, False; 158, attribute; 159, None; 160, identifier:decl; 161, identifier:value; 162, string_content:,; 163, identifier:flags; 164, string; 165, string_content:actions; 166, identifier:action; 167, integer:1; 168, subscript; 169, identifier:sort; 170, subscript; 171, identifier:isgroup; 172, subscript; 173, identifier:groupby; 174, identifier:action; 175, integer:1; 176, identifier:action; 177, integer:1; 178, identifier:action; 179, integer:1
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 9, 20; 10, 21; 10, 22; 10, 23; 11, 24; 12, 25; 13, 26; 14, 27; 15, 28; 16, 29; 17, 30; 19, 31; 19, 32; 19, 33; 19, 34; 21, 35; 21, 36; 22, 37; 23, 38; 24, 39; 24, 40; 25, 41; 25, 42; 26, 43; 26, 44; 27, 45; 27, 46; 28, 47; 28, 48; 29, 49; 29, 50; 30, 51; 30, 52; 31, 53; 31, 54; 33, 55; 33, 56; 34, 57; 35, 58; 36, 59; 36, 60; 37, 61; 38, 62; 38, 63; 40, 64; 40, 65; 42, 66; 42, 67; 44, 68; 44, 69; 45, 70; 45, 71; 47, 72; 47, 73; 48, 74; 48, 75; 49, 76; 49, 77; 51, 78; 51, 79; 56, 80; 57, 81; 57, 82; 61, 83; 61, 84; 62, 85; 63, 86; 65, 87; 65, 88; 65, 89; 65, 90; 66, 91; 66, 92; 67, 93; 67, 94; 68, 95; 68, 96; 74, 97; 74, 98; 75, 99; 80, 100; 80, 101; 81, 102; 81, 103; 81, 104; 82, 105; 82, 106; 82, 107; 82, 108; 83, 109; 83, 110; 84, 111; 84, 112; 85, 113; 85, 114; 86, 115; 86, 116; 87, 117; 87, 118; 88, 119; 88, 120; 89, 121; 89, 122; 90, 123; 90, 124; 93, 125; 93, 126; 94, 127; 101, 128; 102, 129; 102, 130; 104, 131; 104, 132; 105, 133; 106, 134; 107, 135; 112, 136; 112, 137; 114, 138; 114, 139; 118, 140; 120, 141; 123, 142; 123, 143; 124, 144; 128, 145; 128, 146; 129, 147; 129, 148; 130, 149; 131, 150; 131, 151; 132, 152; 132, 153; 133, 154; 133, 155; 134, 156; 134, 157; 135, 158; 135, 159; 136, 160; 136, 161; 137, 162; 141, 163; 141, 164; 146, 165; 150, 166; 150, 167; 154, 168; 154, 169; 156, 170; 156, 171; 158, 172; 158, 173; 168, 174; 168, 175; 170, 176; 170, 177; 172, 178; 172, 179
def do_sort_by(self, element, decl, pseudo): """Implement sort-by declaration - pre-match.""" if ',' in decl.value: css, flags = split(decl.value, ',') else: css = decl.value flags = None sort = css_to_func(serialize(css), serialize(flags or ''), self.css_namespaces, self.state['lang']) step = self.state[self.state['current_step']] target = self.current_target() target.sort = sort target.lang = self.state['lang'] target.isgroup = False target.groupby = None # Find current target, set its sort as well for pos, action in \ enumerate(reversed(step['actions'])): if action[0] == 'target' and \ action[1].tree == element.etree_element: action[1].sort = sort action[1].isgroup = False action[1].groupby = None break
0, module; 1, function_definition; 2, function_name:keys; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, return_statement; 10, comment:""" Returns a sorted list of keys """; 11, assignment; 12, identifier:n; 13, call; 14, comment:# only append the valid keys; 15, block; 16, identifier:keys; 17, identifier:keys; 18, call; 19, identifier:range; 20, argument_list; 21, expression_statement; 22, if_statement; 23, identifier:list; 24, argument_list; 25, call; 26, assignment; 27, not_operator; 28, block; 29, identifier:len; 30, argument_list; 31, identifier:key; 32, call; 33, comparison_operator:key in ['', None]; 34, expression_statement; 35, identifier:self; 36, attribute; 37, argument_list; 38, identifier:key; 39, list; 40, call; 41, identifier:self; 42, identifier:get_value; 43, string; 44, None; 45, attribute; 46, argument_list; 47, identifier:keys; 48, identifier:append; 49, identifier:key
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 6, 10; 7, 11; 8, 12; 8, 13; 8, 14; 8, 15; 9, 16; 11, 17; 11, 18; 13, 19; 13, 20; 15, 21; 15, 22; 18, 23; 18, 24; 20, 25; 21, 26; 22, 27; 22, 28; 25, 29; 25, 30; 26, 31; 26, 32; 27, 33; 28, 34; 30, 35; 32, 36; 32, 37; 33, 38; 33, 39; 34, 40; 36, 41; 36, 42; 39, 43; 39, 44; 40, 45; 40, 46; 45, 47; 45, 48; 46, 49
def keys(self): """ Returns a sorted list of keys """ keys = list() for n in range(len(self)): # only append the valid keys key = self.get_value() if not key in ['', None]: keys.append(key) return keys
0, module; 1, function_definition; 2, function_name:set_item; 3, parameters; 4, block; 5, identifier:self; 6, identifier:key; 7, identifier:value; 8, expression_statement; 9, expression_statement; 10, comment:# if it exists, update; 11, if_statement; 12, comment:""" Sets the item by key, and refills the table sorted. """; 13, assignment; 14, comparison_operator:key in keys; 15, block; 16, comment:# otherwise we have to add an element; 17, else_clause; 18, identifier:keys; 19, call; 20, identifier:key; 21, identifier:keys; 22, expression_statement; 23, block; 24, identifier:list; 25, argument_list; 26, call; 27, expression_statement; 28, expression_statement; 29, call; 30, attribute; 31, argument_list; 32, call; 33, call; 34, attribute; 35, argument_list; 36, identifier:self; 37, identifier:set_value; 38, integer:1; 39, call; 40, call; 41, attribute; 42, argument_list; 43, attribute; 44, argument_list; 45, identifier:self; 46, identifier:keys; 47, attribute; 48, argument_list; 49, identifier:str; 50, argument_list; 51, identifier:self; 52, identifier:set_value; 53, integer:0; 54, call; 55, call; 56, identifier:self; 57, identifier:set_value; 58, integer:1; 59, binary_operator:len(self)-1; 60, call; 61, identifier:keys; 62, identifier:index; 63, identifier:key; 64, identifier:value; 65, identifier:len; 66, argument_list; 67, identifier:str; 68, argument_list; 69, call; 70, integer:1; 71, identifier:str; 72, argument_list; 73, identifier:self; 74, identifier:key; 75, identifier:len; 76, argument_list; 77, identifier:value; 78, identifier:self
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 8, 12; 9, 13; 11, 14; 11, 15; 11, 16; 11, 17; 13, 18; 13, 19; 14, 20; 14, 21; 15, 22; 17, 23; 19, 24; 19, 25; 22, 26; 23, 27; 23, 28; 25, 29; 26, 30; 26, 31; 27, 32; 28, 33; 29, 34; 29, 35; 30, 36; 30, 37; 31, 38; 31, 39; 31, 40; 32, 41; 32, 42; 33, 43; 33, 44; 34, 45; 34, 46; 39, 47; 39, 48; 40, 49; 40, 50; 41, 51; 41, 52; 42, 53; 42, 54; 42, 55; 43, 56; 43, 57; 44, 58; 44, 59; 44, 60; 47, 61; 47, 62; 48, 63; 50, 64; 54, 65; 54, 66; 55, 67; 55, 68; 59, 69; 59, 70; 60, 71; 60, 72; 66, 73; 68, 74; 69, 75; 69, 76; 72, 77; 76, 78
def set_item(self, key, value): """ Sets the item by key, and refills the table sorted. """ keys = list(self.keys()) # if it exists, update if key in keys: self.set_value(1,keys.index(key),str(value)) # otherwise we have to add an element else: self.set_value(0,len(self), str(key)) self.set_value(1,len(self)-1, str(value))
0, module; 1, function_definition; 2, function_name:insert_ordered; 3, parameters; 4, block; 5, identifier:value; 6, identifier:array; 7, expression_statement; 8, expression_statement; 9, comment:# search for the last array item that value is larger than; 10, for_statement; 11, expression_statement; 12, return_statement; 13, comment:""" This will insert the value into the array, keeping it sorted, and returning the index where it was inserted """; 14, assignment; 15, identifier:n; 16, call; 17, block; 18, call; 19, identifier:index; 20, identifier:index; 21, integer:0; 22, identifier:range; 23, argument_list; 24, if_statement; 25, attribute; 26, argument_list; 27, integer:0; 28, call; 29, comparison_operator:value >= array[n]; 30, block; 31, identifier:array; 32, identifier:insert; 33, identifier:index; 34, identifier:value; 35, identifier:len; 36, argument_list; 37, identifier:value; 38, subscript; 39, expression_statement; 40, identifier:array; 41, identifier:array; 42, identifier:n; 43, assignment; 44, identifier:index; 45, binary_operator:n+1; 46, identifier:n; 47, integer:1
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 7, 13; 8, 14; 10, 15; 10, 16; 10, 17; 11, 18; 12, 19; 14, 20; 14, 21; 16, 22; 16, 23; 17, 24; 18, 25; 18, 26; 23, 27; 23, 28; 24, 29; 24, 30; 25, 31; 25, 32; 26, 33; 26, 34; 28, 35; 28, 36; 29, 37; 29, 38; 30, 39; 36, 40; 38, 41; 38, 42; 39, 43; 43, 44; 43, 45; 45, 46; 45, 47
def insert_ordered(value, array): """ This will insert the value into the array, keeping it sorted, and returning the index where it was inserted """ index = 0 # search for the last array item that value is larger than for n in range(0,len(array)): if value >= array[n]: index = n+1 array.insert(index, value) return index
0, module; 1, function_definition; 2, function_name:remove_dataset; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, dictionary_splat_pattern; 8, expression_statement; 9, expression_statement; 10, comment:# Let's avoid deleting ALL parameters from the matching contexts; 11, if_statement; 12, comment:# let's handle deps if kind was passed; 13, expression_statement; 14, if_statement; 15, expression_statement; 16, if_statement; 17, expression_statement; 18, comment:# Let's avoid the possibility of deleting a single parameter; 19, expression_statement; 20, comment:# Let's also avoid the possibility of accidentally deleting system; 21, comment:# parameters, etc; 22, expression_statement; 23, comment:# ps = self.filter(**kwargs); 24, comment:# logger.info('removing {} parameters (this is not undoable)'.\; 25, comment:# format(len(ps))); 26, comment:# print "*** kwargs", kwargs, len(ps); 27, expression_statement; 28, comment:# not really sure why we need to call this twice, but it seems to do; 29, comment:# the trick; 30, expression_statement; 31, expression_statement; 32, comment:# TODO: check to make sure that trying to undo this; 33, comment:# will raise an error saying this is not undo-able; 34, expression_statement; 35, return_statement; 36, identifier:dataset; 37, None; 38, identifier:kwargs; 39, comment:""" Remove a dataset from the Bundle. This removes all matching Parameters from the dataset, model, and constraint contexts (by default if the context tag is not provided). You must provide some sort of filter or this will raise an Error (so that all Parameters are not accidentally removed). :parameter str dataset: name of the dataset :parameter **kwargs: any other tags to do the filter (except qualifier and dataset) :raises ValueError: if no filter is provided """; 40, call; 41, boolean_operator; 42, block; 43, assignment; 44, comparison_operator:kind is not None; 45, block; 46, assignment; 47, comparison_operator:dataset is None; 48, comment:# then let's find the list of datasets that match the filter,; 49, comment:# we'll then use dataset to do the removing. This avoids leaving; 50, comment:# pararameters behind that don't specifically match the filter; 51, comment:# (ie if kind is passed as 'rv' we still want to remove parameters; 52, comment:# with datasets that are RVs but belong to a different kind in; 53, comment:# another context like compute); 54, block; 55, assignment; 56, assignment; 57, call; 58, call; 59, call; 60, call; 61, call; 62, attribute; 63, argument_list; 64, comparison_operator:dataset is None; 65, not_operator; 66, raise_statement; 67, identifier:kind; 68, call; 69, identifier:kind; 70, None; 71, if_statement; 72, expression_statement; 73, for_statement; 74, expression_statement; 75, subscript; 76, identifier:kind; 77, identifier:dataset; 78, None; 79, expression_statement; 80, expression_statement; 81, subscript; 82, identifier:dataset; 83, subscript; 84, None; 85, attribute; 86, argument_list; 87, attribute; 88, argument_list; 89, attribute; 90, argument_list; 91, attribute; 92, argument_list; 93, attribute; 94, argument_list; 95, identifier:self; 96, identifier:_kwargs_checks; 97, identifier:kwargs; 98, identifier:dataset; 99, None; 100, call; 101, call; 102, attribute; 103, argument_list; 104, call; 105, block; 106, assignment; 107, identifier:kind_i; 108, identifier:kind; 109, block; 110, assignment; 111, identifier:kwargs; 112, string; 113, assignment; 114, assignment; 115, identifier:kwargs; 116, string; 117, identifier:kwargs; 118, string; 119, identifier:kwargs; 120, identifier:setdefault; 121, string; 122, list; 123, identifier:self; 124, identifier:remove_parameters_all; 125, dictionary_splat; 126, identifier:self; 127, identifier:remove_parameters_all; 128, dictionary_splat; 129, identifier:self; 130, identifier:_handle_dataset_selectparams; 131, identifier:self; 132, identifier:_add_history; 133, keyword_argument; 134, keyword_argument; 135, keyword_argument; 136, keyword_argument; 137, identifier:len; 138, argument_list; 139, identifier:ValueError; 140, argument_list; 141, identifier:kwargs; 142, identifier:get; 143, string; 144, None; 145, identifier:isinstance; 146, argument_list; 147, expression_statement; 148, identifier:kind_deps; 149, list; 150, expression_statement; 151, if_statement; 152, identifier:kind; 153, binary_operator:kind + kind_deps; 154, string_content:kind; 155, identifier:dataset; 156, attribute; 157, subscript; 158, None; 159, string_content:dataset; 160, string_content:qualifier; 161, string_content:context; 162, string; 163, string; 164, string; 165, string; 166, identifier:kwargs; 167, identifier:kwargs; 168, identifier:redo_func; 169, string; 170, identifier:redo_kwargs; 171, dictionary; 172, identifier:undo_func; 173, None; 174, identifier:undo_kwargs; 175, dictionary; 176, call; 177, string:"must provide some value to filter for datasets"; 178, string_content:kind; 179, identifier:kind; 180, identifier:str; 181, assignment; 182, assignment; 183, comparison_operator:dep not in kind; 184, block; 185, identifier:kind; 186, identifier:kind_deps; 187, call; 188, identifier:datasets; 189, identifier:kwargs; 190, string; 191, string_content:dataset; 192, string_content:model; 193, string_content:constraint; 194, string_content:compute; 195, string_content:remove_dataset; 196, pair; 197, attribute; 198, argument_list; 199, identifier:kind; 200, list; 201, identifier:dep; 202, call; 203, identifier:dep; 204, identifier:kind; 205, expression_statement; 206, attribute; 207, argument_list; 208, string_content:kind; 209, string; 210, identifier:dataset; 211, identifier:kwargs; 212, identifier:items; 213, identifier:kind; 214, attribute; 215, argument_list; 216, call; 217, identifier:self; 218, identifier:filter; 219, dictionary_splat; 220, string_content:dataset; 221, string; 222, identifier:format; 223, identifier:kind_i; 224, attribute; 225, argument_list; 226, identifier:kwargs; 227, string_content:{}_dep; 228, identifier:kind_deps; 229, identifier:append; 230, identifier:dep
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 4, 25; 4, 26; 4, 27; 4, 28; 4, 29; 4, 30; 4, 31; 4, 32; 4, 33; 4, 34; 4, 35; 6, 36; 6, 37; 7, 38; 8, 39; 9, 40; 11, 41; 11, 42; 13, 43; 14, 44; 14, 45; 15, 46; 16, 47; 16, 48; 16, 49; 16, 50; 16, 51; 16, 52; 16, 53; 16, 54; 17, 55; 19, 56; 22, 57; 27, 58; 30, 59; 31, 60; 34, 61; 40, 62; 40, 63; 41, 64; 41, 65; 42, 66; 43, 67; 43, 68; 44, 69; 44, 70; 45, 71; 45, 72; 45, 73; 45, 74; 46, 75; 46, 76; 47, 77; 47, 78; 54, 79; 54, 80; 55, 81; 55, 82; 56, 83; 56, 84; 57, 85; 57, 86; 58, 87; 58, 88; 59, 89; 59, 90; 60, 91; 60, 92; 61, 93; 61, 94; 62, 95; 62, 96; 63, 97; 64, 98; 64, 99; 65, 100; 66, 101; 68, 102; 68, 103; 71, 104; 71, 105; 72, 106; 73, 107; 73, 108; 73, 109; 74, 110; 75, 111; 75, 112; 79, 113; 80, 114; 81, 115; 81, 116; 83, 117; 83, 118; 85, 119; 85, 120; 86, 121; 86, 122; 87, 123; 87, 124; 88, 125; 89, 126; 89, 127; 90, 128; 91, 129; 91, 130; 93, 131; 93, 132; 94, 133; 94, 134; 94, 135; 94, 136; 100, 137; 100, 138; 101, 139; 101, 140; 102, 141; 102, 142; 103, 143; 103, 144; 104, 145; 104, 146; 105, 147; 106, 148; 106, 149; 109, 150; 109, 151; 110, 152; 110, 153; 112, 154; 113, 155; 113, 156; 114, 157; 114, 158; 116, 159; 118, 160; 121, 161; 122, 162; 122, 163; 122, 164; 122, 165; 125, 166; 128, 167; 133, 168; 133, 169; 134, 170; 134, 171; 135, 172; 135, 173; 136, 174; 136, 175; 138, 176; 140, 177; 143, 178; 146, 179; 146, 180; 147, 181; 150, 182; 151, 183; 151, 184; 153, 185; 153, 186; 156, 187; 156, 188; 157, 189; 157, 190; 162, 191; 163, 192; 164, 193; 165, 194; 169, 195; 171, 196; 176, 197; 176, 198; 181, 199; 181, 200; 182, 201; 182, 202; 183, 203; 183, 204; 184, 205; 187, 206; 187, 207; 190, 208; 196, 209; 196, 210; 197, 211; 197, 212; 200, 213; 202, 214; 202, 215; 205, 216; 206, 217; 206, 218; 207, 219; 209, 220; 214, 221; 214, 222; 215, 223; 216, 224; 216, 225; 219, 226; 221, 227; 224, 228; 224, 229; 225, 230
def remove_dataset(self, dataset=None, **kwargs): """ Remove a dataset from the Bundle. This removes all matching Parameters from the dataset, model, and constraint contexts (by default if the context tag is not provided). You must provide some sort of filter or this will raise an Error (so that all Parameters are not accidentally removed). :parameter str dataset: name of the dataset :parameter **kwargs: any other tags to do the filter (except qualifier and dataset) :raises ValueError: if no filter is provided """ self._kwargs_checks(kwargs) # Let's avoid deleting ALL parameters from the matching contexts if dataset is None and not len(kwargs.items()): raise ValueError("must provide some value to filter for datasets") # let's handle deps if kind was passed kind = kwargs.get('kind', None) if kind is not None: if isinstance(kind, str): kind = [kind] kind_deps = [] for kind_i in kind: dep = '{}_dep'.format(kind_i) if dep not in kind: kind_deps.append(dep) kind = kind + kind_deps kwargs['kind'] = kind if dataset is None: # then let's find the list of datasets that match the filter, # we'll then use dataset to do the removing. This avoids leaving # pararameters behind that don't specifically match the filter # (ie if kind is passed as 'rv' we still want to remove parameters # with datasets that are RVs but belong to a different kind in # another context like compute) dataset = self.filter(**kwargs).datasets kwargs['kind'] = None kwargs['dataset'] = dataset # Let's avoid the possibility of deleting a single parameter kwargs['qualifier'] = None # Let's also avoid the possibility of accidentally deleting system # parameters, etc kwargs.setdefault('context', ['dataset', 'model', 'constraint', 'compute']) # ps = self.filter(**kwargs) # logger.info('removing {} parameters (this is not undoable)'.\ # format(len(ps))) # print "*** kwargs", kwargs, len(ps) self.remove_parameters_all(**kwargs) # not really sure why we need to call this twice, but it seems to do # the trick self.remove_parameters_all(**kwargs) self._handle_dataset_selectparams() # TODO: check to make sure that trying to undo this # will raise an error saying this is not undo-able self._add_history(redo_func='remove_dataset', redo_kwargs={'dataset': dataset}, undo_func=None, undo_kwargs={}) return
0, module; 1, function_definition; 2, function_name:calls_sorted; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, function_definition; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, comment:# TODO: ugh, this is ugly. Test to find the optimal way to sort; 12, comment:# while still ending up with a list; 13, return_statement; 14, comment:""" calls sorted in z """; 15, function_name:_z; 16, parameters; 17, block; 18, assignment; 19, assignment; 20, assignment; 21, call; 22, identifier:call; 23, if_statement; 24, identifier:calls; 25, attribute; 26, identifier:zs; 27, call; 28, identifier:sorted_inds; 29, call; 30, attribute; 31, argument_list; 32, call; 33, block; 34, elif_clause; 35, else_clause; 36, identifier:self; 37, identifier:_calls; 38, attribute; 39, argument_list; 40, attribute; 41, argument_list; 42, identifier:_call; 43, identifier:make_callgroup; 44, call; 45, identifier:isinstance; 46, argument_list; 47, return_statement; 48, boolean_operator; 49, block; 50, comment:# put it at the back; 51, block; 52, identifier:np; 53, identifier:array; 54, list_comprehension; 55, identifier:zs; 56, identifier:argsort; 57, attribute; 58, argument_list; 59, attribute; 60, attribute; 61, call; 62, call; 63, call; 64, return_statement; 65, return_statement; 66, call; 67, for_in_clause; 68, subscript; 69, identifier:tolist; 70, attribute; 71, identifier:value; 72, identifier:np; 73, identifier:ndarray; 74, attribute; 75, argument_list; 76, identifier:isinstance; 77, argument_list; 78, identifier:isinstance; 79, argument_list; 80, attribute; 81, unary_operator; 82, identifier:_z; 83, argument_list; 84, identifier:c; 85, identifier:calls; 86, call; 87, identifier:sorted_inds; 88, identifier:call; 89, identifier:z; 90, identifier:np; 91, identifier:mean; 92, call; 93, attribute; 94, identifier:float; 95, attribute; 96, identifier:int; 97, attribute; 98, identifier:value; 99, attribute; 100, identifier:c; 101, attribute; 102, argument_list; 103, attribute; 104, argument_list; 105, attribute; 106, identifier:value; 107, attribute; 108, identifier:value; 109, identifier:call; 110, identifier:z; 111, identifier:np; 112, identifier:inf; 113, identifier:np; 114, identifier:array; 115, identifier:calls; 116, attribute; 117, identifier:flatten; 118, identifier:call; 119, identifier:z; 120, identifier:call; 121, identifier:z; 122, attribute; 123, identifier:value; 124, identifier:call; 125, identifier:z
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 6, 14; 7, 15; 7, 16; 7, 17; 8, 18; 9, 19; 10, 20; 13, 21; 16, 22; 17, 23; 18, 24; 18, 25; 19, 26; 19, 27; 20, 28; 20, 29; 21, 30; 21, 31; 23, 32; 23, 33; 23, 34; 23, 35; 25, 36; 25, 37; 27, 38; 27, 39; 29, 40; 29, 41; 30, 42; 30, 43; 31, 44; 32, 45; 32, 46; 33, 47; 34, 48; 34, 49; 35, 50; 35, 51; 38, 52; 38, 53; 39, 54; 40, 55; 40, 56; 44, 57; 44, 58; 46, 59; 46, 60; 47, 61; 48, 62; 48, 63; 49, 64; 51, 65; 54, 66; 54, 67; 57, 68; 57, 69; 59, 70; 59, 71; 60, 72; 60, 73; 61, 74; 61, 75; 62, 76; 62, 77; 63, 78; 63, 79; 64, 80; 65, 81; 66, 82; 66, 83; 67, 84; 67, 85; 68, 86; 68, 87; 70, 88; 70, 89; 74, 90; 74, 91; 75, 92; 77, 93; 77, 94; 79, 95; 79, 96; 80, 97; 80, 98; 81, 99; 83, 100; 86, 101; 86, 102; 92, 103; 92, 104; 93, 105; 93, 106; 95, 107; 95, 108; 97, 109; 97, 110; 99, 111; 99, 112; 101, 113; 101, 114; 102, 115; 103, 116; 103, 117; 105, 118; 105, 119; 107, 120; 107, 121; 116, 122; 116, 123; 122, 124; 122, 125
def calls_sorted(self): """ calls sorted in z """ def _z(call): if isinstance(call.z.value, np.ndarray): return np.mean(call.z.value.flatten()) elif isinstance(call.z.value, float) or isinstance(call.z.value, int): return call.z.value else: # put it at the back return -np.inf calls = self._calls zs = np.array([_z(c) for c in calls]) sorted_inds = zs.argsort() # TODO: ugh, this is ugly. Test to find the optimal way to sort # while still ending up with a list return _call.make_callgroup(np.array(calls)[sorted_inds].tolist())
0, module; 1, function_definition; 2, function_name:_filter_library_state; 3, parameters; 4, block; 5, identifier:self; 6, identifier:items; 7, expression_statement; 8, if_statement; 9, expression_statement; 10, comment:# If the hovered item is e.g. a connection, we need to get the parental state; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, if_statement; 15, comment:"""Filters out child elements of library state when they cannot be hovered Checks if hovered item is within a LibraryState * if not, the list is returned unfiltered * if so, STATE_SELECTION_INSIDE_LIBRARY_STATE_ENABLED is checked * if enabled, the library is selected (instead of the state copy) * if not, the upper most library is selected :param list items: Sorted list of items beneath the cursor :return: filtered items :rtype: list """; 16, not_operator; 17, block; 18, assignment; 19, assignment; 20, assignment; 21, assignment; 22, call; 23, comment:# select the library state instead of the library_root_state because it is hidden; 24, block; 25, else_clause; 26, identifier:items; 27, return_statement; 28, identifier:top_most_item; 29, subscript; 30, identifier:top_most_state_v; 31, conditional_expression:top_most_item if isinstance(top_most_item, StateView) else top_most_item.parent; 32, identifier:state; 33, attribute; 34, identifier:global_gui_config; 35, attribute; 36, attribute; 37, argument_list; 38, if_statement; 39, return_statement; 40, comment:# Find state_copy of uppermost LibraryState; 41, block; 42, identifier:items; 43, identifier:items; 44, integer:0; 45, identifier:top_most_item; 46, call; 47, attribute; 48, attribute; 49, identifier:state; 50, identifier:gui_helper_state_machine; 51, identifier:global_gui_config; 52, identifier:global_gui_config; 53, identifier:get_config_value; 54, string; 55, attribute; 56, block; 57, identifier:items; 58, expression_statement; 59, comment:# If the hovered element is a child of a library, make the library the hovered_item; 60, if_statement; 61, return_statement; 62, identifier:isinstance; 63, argument_list; 64, identifier:top_most_item; 65, identifier:parent; 66, identifier:top_most_state_v; 67, identifier:model; 68, string_content:STATE_SELECTION_INSIDE_LIBRARY_STATE_ENABLED; 69, identifier:state; 70, identifier:is_root_state_of_library; 71, expression_statement; 72, return_statement; 73, assignment; 74, identifier:library_root_state; 75, block; 76, identifier:items; 77, identifier:top_most_item; 78, identifier:StateView; 79, assignment; 80, call; 81, identifier:library_root_state; 82, call; 83, expression_statement; 84, expression_statement; 85, return_statement; 86, identifier:new_topmost_item; 87, call; 88, attribute; 89, argument_list; 90, attribute; 91, argument_list; 92, assignment; 93, assignment; 94, call; 95, attribute; 96, argument_list; 97, identifier:self; 98, identifier:dismiss_upper_items; 99, identifier:items; 100, identifier:new_topmost_item; 101, identifier:state; 102, identifier:get_uppermost_library_root_state; 103, identifier:library_state; 104, attribute; 105, identifier:library_state_v; 106, call; 107, attribute; 108, argument_list; 109, attribute; 110, identifier:get_view_for_core_element; 111, attribute; 112, identifier:library_root_state; 113, identifier:parent; 114, attribute; 115, argument_list; 116, identifier:self; 117, identifier:dismiss_upper_items; 118, identifier:items; 119, identifier:library_state_v; 120, attribute; 121, identifier:canvas; 122, identifier:state; 123, identifier:parent; 124, attribute; 125, identifier:get_view_for_core_element; 126, identifier:library_state; 127, identifier:self; 128, identifier:view; 129, attribute; 130, identifier:canvas; 131, identifier:self; 132, identifier:view
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 7, 15; 8, 16; 8, 17; 9, 18; 11, 19; 12, 20; 13, 21; 14, 22; 14, 23; 14, 24; 14, 25; 16, 26; 17, 27; 18, 28; 18, 29; 19, 30; 19, 31; 20, 32; 20, 33; 21, 34; 21, 35; 22, 36; 22, 37; 24, 38; 24, 39; 25, 40; 25, 41; 27, 42; 29, 43; 29, 44; 31, 45; 31, 46; 31, 47; 33, 48; 33, 49; 35, 50; 35, 51; 36, 52; 36, 53; 37, 54; 38, 55; 38, 56; 39, 57; 41, 58; 41, 59; 41, 60; 41, 61; 46, 62; 46, 63; 47, 64; 47, 65; 48, 66; 48, 67; 54, 68; 55, 69; 55, 70; 56, 71; 56, 72; 58, 73; 60, 74; 60, 75; 61, 76; 63, 77; 63, 78; 71, 79; 72, 80; 73, 81; 73, 82; 75, 83; 75, 84; 75, 85; 79, 86; 79, 87; 80, 88; 80, 89; 82, 90; 82, 91; 83, 92; 84, 93; 85, 94; 87, 95; 87, 96; 88, 97; 88, 98; 89, 99; 89, 100; 90, 101; 90, 102; 92, 103; 92, 104; 93, 105; 93, 106; 94, 107; 94, 108; 95, 109; 95, 110; 96, 111; 104, 112; 104, 113; 106, 114; 106, 115; 107, 116; 107, 117; 108, 118; 108, 119; 109, 120; 109, 121; 111, 122; 111, 123; 114, 124; 114, 125; 115, 126; 120, 127; 120, 128; 124, 129; 124, 130; 129, 131; 129, 132
def _filter_library_state(self, items): """Filters out child elements of library state when they cannot be hovered Checks if hovered item is within a LibraryState * if not, the list is returned unfiltered * if so, STATE_SELECTION_INSIDE_LIBRARY_STATE_ENABLED is checked * if enabled, the library is selected (instead of the state copy) * if not, the upper most library is selected :param list items: Sorted list of items beneath the cursor :return: filtered items :rtype: list """ if not items: return items top_most_item = items[0] # If the hovered item is e.g. a connection, we need to get the parental state top_most_state_v = top_most_item if isinstance(top_most_item, StateView) else top_most_item.parent state = top_most_state_v.model.state global_gui_config = gui_helper_state_machine.global_gui_config if global_gui_config.get_config_value('STATE_SELECTION_INSIDE_LIBRARY_STATE_ENABLED'): # select the library state instead of the library_root_state because it is hidden if state.is_root_state_of_library: new_topmost_item = self.view.canvas.get_view_for_core_element(state.parent) return self.dismiss_upper_items(items, new_topmost_item) return items else: # Find state_copy of uppermost LibraryState library_root_state = state.get_uppermost_library_root_state() # If the hovered element is a child of a library, make the library the hovered_item if library_root_state: library_state = library_root_state.parent library_state_v = self.view.canvas.get_view_for_core_element(library_state) return self.dismiss_upper_items(items, library_state_v) return items
0, module; 1, function_definition; 2, function_name:_filter_hovered_items; 3, parameters; 4, block; 5, identifier:self; 6, identifier:items; 7, identifier:event; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, expression_statement; 13, comment:# States/Names take precedence over connections if the connections are on the same hierarchy and if there is; 14, comment:# a port beneath the cursor; 15, expression_statement; 16, expression_statement; 17, if_statement; 18, comment:# NameView can only be hovered if it or its parent state is selected; 19, if_statement; 20, return_statement; 21, comment:"""Filters out items that cannot be hovered :param list items: Sorted list of items beneath the cursor :param Gtk.Event event: Motion event :return: filtered items :rtype: list """; 22, assignment; 23, not_operator; 24, block; 25, assignment; 26, assignment; 27, assignment; 28, assignment; 29, identifier:first_state_v; 30, comment:# There can be several connections above the state/name skip those and find the first non-connection-item; 31, block; 32, call; 33, block; 34, identifier:items; 35, identifier:items; 36, call; 37, identifier:items; 38, return_statement; 39, identifier:top_most_item; 40, subscript; 41, identifier:second_top_most_item; 42, conditional_expression:items[1] if len(items) > 1 else None; 43, identifier:first_state_v; 44, call; 45, identifier:first_state_v; 46, conditional_expression:first_state_v.parent if isinstance(first_state_v, NameView) else first_state_v; 47, for_statement; 48, comment:# Connections are only dismissed, if there is a port beneath the cursor. Search for ports here:; 49, expression_statement; 50, expression_statement; 51, expression_statement; 52, expression_statement; 53, for_statement; 54, if_statement; 55, identifier:isinstance; 56, argument_list; 57, expression_statement; 58, comment:# second item in the list must be the parent state of the NameView; 59, if_statement; 60, attribute; 61, argument_list; 62, identifier:items; 63, identifier:items; 64, integer:0; 65, subscript; 66, comparison_operator:len(items) > 1; 67, None; 68, identifier:next; 69, argument_list; 70, attribute; 71, call; 72, identifier:first_state_v; 73, identifier:item; 74, identifier:items; 75, block; 76, assignment; 77, assignment; 78, assignment; 79, assignment; 80, identifier:port_v; 81, identifier:state_ports; 82, block; 83, identifier:port_beneath_cursor; 84, block; 85, identifier:top_most_item; 86, identifier:NameView; 87, assignment; 88, boolean_operator; 89, block; 90, identifier:self; 91, identifier:_filter_library_state; 92, identifier:items; 93, identifier:items; 94, integer:1; 95, call; 96, integer:1; 97, call; 98, identifier:first_state_v; 99, identifier:parent; 100, identifier:isinstance; 101, argument_list; 102, if_statement; 103, break_statement; 104, identifier:port_beneath_cursor; 105, False; 106, identifier:state_ports; 107, call; 108, identifier:position; 109, call; 110, identifier:i2v_matrix; 111, call; 112, expression_statement; 113, expression_statement; 114, if_statement; 115, expression_statement; 116, expression_statement; 117, expression_statement; 118, identifier:state_v; 119, identifier:second_top_most_item; 120, comparison_operator:state_v not in self.view.selected_items; 121, comparison_operator:top_most_item not in self.view.selected_items; 122, expression_statement; 123, identifier:len; 124, argument_list; 125, identifier:filter; 126, argument_list; 127, identifier:first_state_v; 128, identifier:NameView; 129, call; 130, comment:# connection is on the same hierarchy level as the state/name, thus we dismiss it; 131, block; 132, attribute; 133, argument_list; 134, attribute; 135, argument_list; 136, attribute; 137, argument_list; 138, assignment; 139, assignment; 140, comparison_operator:view_distance == 0; 141, block; 142, assignment; 143, assignment; 144, assignment; 145, identifier:state_v; 146, attribute; 147, identifier:top_most_item; 148, attribute; 149, assignment; 150, identifier:items; 151, lambda; 152, identifier:items; 153, identifier:isinstance; 154, argument_list; 155, if_statement; 156, identifier:first_state_v; 157, identifier:get_all_ports; 158, call; 159, identifier:transform_point; 160, attribute; 161, attribute; 162, attribute; 163, identifier:get_matrix_i2v; 164, identifier:first_state_v; 165, identifier:item_distance; 166, subscript; 167, identifier:view_distance; 168, subscript; 169, identifier:view_distance; 170, integer:0; 171, expression_statement; 172, break_statement; 173, identifier:items; 174, call; 175, identifier:top_most_item; 176, subscript; 177, identifier:second_top_most_item; 178, conditional_expression:items[1] if len(items) > 1 else None; 179, attribute; 180, identifier:selected_items; 181, attribute; 182, identifier:selected_items; 183, identifier:items; 184, subscript; 185, lambda_parameters; 186, call; 187, identifier:item; 188, identifier:ConnectionView; 189, comparison_operator:self.view.canvas.get_parent(top_most_item) is not first_state_v; 190, block; 191, attribute; 192, argument_list; 193, identifier:event; 194, identifier:x; 195, identifier:event; 196, identifier:y; 197, identifier:self; 198, identifier:view; 199, call; 200, integer:1; 201, call; 202, integer:0; 203, assignment; 204, attribute; 205, argument_list; 206, identifier:items; 207, integer:0; 208, subscript; 209, comparison_operator:len(items) > 1; 210, None; 211, identifier:self; 212, identifier:view; 213, identifier:self; 214, identifier:view; 215, identifier:items; 216, slice; 217, identifier:item; 218, identifier:isinstance; 219, argument_list; 220, call; 221, identifier:first_state_v; 222, continue_statement; 223, attribute; 224, identifier:get_matrix_v2i; 225, identifier:first_state_v; 226, attribute; 227, argument_list; 228, attribute; 229, argument_list; 230, identifier:port_beneath_cursor; 231, True; 232, identifier:self; 233, identifier:dismiss_upper_items; 234, identifier:items; 235, identifier:item; 236, identifier:items; 237, integer:1; 238, call; 239, integer:1; 240, integer:1; 241, identifier:item; 242, tuple; 243, attribute; 244, argument_list; 245, identifier:self; 246, identifier:view; 247, attribute; 248, identifier:glue; 249, identifier:position; 250, identifier:i2v_matrix; 251, identifier:transform_distance; 252, identifier:item_distance; 253, integer:0; 254, identifier:len; 255, argument_list; 256, identifier:NameView; 257, identifier:StateView; 258, attribute; 259, identifier:get_parent; 260, identifier:top_most_item; 261, identifier:port_v; 262, identifier:port; 263, identifier:items; 264, attribute; 265, identifier:canvas; 266, identifier:self; 267, identifier:view
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 8, 21; 9, 22; 10, 23; 10, 24; 11, 25; 12, 26; 15, 27; 16, 28; 17, 29; 17, 30; 17, 31; 19, 32; 19, 33; 20, 34; 22, 35; 22, 36; 23, 37; 24, 38; 25, 39; 25, 40; 26, 41; 26, 42; 27, 43; 27, 44; 28, 45; 28, 46; 31, 47; 31, 48; 31, 49; 31, 50; 31, 51; 31, 52; 31, 53; 31, 54; 32, 55; 32, 56; 33, 57; 33, 58; 33, 59; 36, 60; 36, 61; 38, 62; 40, 63; 40, 64; 42, 65; 42, 66; 42, 67; 44, 68; 44, 69; 46, 70; 46, 71; 46, 72; 47, 73; 47, 74; 47, 75; 49, 76; 50, 77; 51, 78; 52, 79; 53, 80; 53, 81; 53, 82; 54, 83; 54, 84; 56, 85; 56, 86; 57, 87; 59, 88; 59, 89; 60, 90; 60, 91; 61, 92; 65, 93; 65, 94; 66, 95; 66, 96; 69, 97; 70, 98; 70, 99; 71, 100; 71, 101; 75, 102; 75, 103; 76, 104; 76, 105; 77, 106; 77, 107; 78, 108; 78, 109; 79, 110; 79, 111; 82, 112; 82, 113; 82, 114; 84, 115; 84, 116; 84, 117; 87, 118; 87, 119; 88, 120; 88, 121; 89, 122; 95, 123; 95, 124; 97, 125; 97, 126; 101, 127; 101, 128; 102, 129; 102, 130; 102, 131; 107, 132; 107, 133; 109, 134; 109, 135; 111, 136; 111, 137; 112, 138; 113, 139; 114, 140; 114, 141; 115, 142; 116, 143; 117, 144; 120, 145; 120, 146; 121, 147; 121, 148; 122, 149; 124, 150; 126, 151; 126, 152; 129, 153; 129, 154; 131, 155; 132, 156; 132, 157; 134, 158; 134, 159; 135, 160; 135, 161; 136, 162; 136, 163; 137, 164; 138, 165; 138, 166; 139, 167; 139, 168; 140, 169; 140, 170; 141, 171; 141, 172; 142, 173; 142, 174; 143, 175; 143, 176; 144, 177; 144, 178; 146, 179; 146, 180; 148, 181; 148, 182; 149, 183; 149, 184; 151, 185; 151, 186; 154, 187; 154, 188; 155, 189; 155, 190; 158, 191; 158, 192; 160, 193; 160, 194; 161, 195; 161, 196; 162, 197; 162, 198; 166, 199; 166, 200; 168, 201; 168, 202; 171, 203; 174, 204; 174, 205; 176, 206; 176, 207; 178, 208; 178, 209; 178, 210; 179, 211; 179, 212; 181, 213; 181, 214; 184, 215; 184, 216; 185, 217; 186, 218; 186, 219; 189, 220; 189, 221; 190, 222; 191, 223; 191, 224; 192, 225; 199, 226; 199, 227; 201, 228; 201, 229; 203, 230; 203, 231; 204, 232; 204, 233; 205, 234; 205, 235; 208, 236; 208, 237; 209, 238; 209, 239; 216, 240; 219, 241; 219, 242; 220, 243; 220, 244; 223, 245; 223, 246; 226, 247; 226, 248; 227, 249; 228, 250; 228, 251; 229, 252; 229, 253; 238, 254; 238, 255; 242, 256; 242, 257; 243, 258; 243, 259; 244, 260; 247, 261; 247, 262; 255, 263; 258, 264; 258, 265; 264, 266; 264, 267
def _filter_hovered_items(self, items, event): """Filters out items that cannot be hovered :param list items: Sorted list of items beneath the cursor :param Gtk.Event event: Motion event :return: filtered items :rtype: list """ items = self._filter_library_state(items) if not items: return items top_most_item = items[0] second_top_most_item = items[1] if len(items) > 1 else None # States/Names take precedence over connections if the connections are on the same hierarchy and if there is # a port beneath the cursor first_state_v = next(filter(lambda item: isinstance(item, (NameView, StateView)), items)) first_state_v = first_state_v.parent if isinstance(first_state_v, NameView) else first_state_v if first_state_v: # There can be several connections above the state/name skip those and find the first non-connection-item for item in items: if isinstance(item, ConnectionView): # connection is on the same hierarchy level as the state/name, thus we dismiss it if self.view.canvas.get_parent(top_most_item) is not first_state_v: continue break # Connections are only dismissed, if there is a port beneath the cursor. Search for ports here: port_beneath_cursor = False state_ports = first_state_v.get_all_ports() position = self.view.get_matrix_v2i(first_state_v).transform_point(event.x, event.y) i2v_matrix = self.view.get_matrix_i2v(first_state_v) for port_v in state_ports: item_distance = port_v.port.glue(position)[1] view_distance = i2v_matrix.transform_distance(item_distance, 0)[0] if view_distance == 0: port_beneath_cursor = True break if port_beneath_cursor: items = self.dismiss_upper_items(items, item) top_most_item = items[0] second_top_most_item = items[1] if len(items) > 1 else None # NameView can only be hovered if it or its parent state is selected if isinstance(top_most_item, NameView): state_v = second_top_most_item # second item in the list must be the parent state of the NameView if state_v not in self.view.selected_items and top_most_item not in self.view.selected_items: items = items[1:] return items
0, module; 1, function_definition; 2, function_name:compare_variables; 3, parameters; 4, block; 5, identifier:tree_model; 6, identifier:iter1; 7, identifier:iter2; 8, default_parameter; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, comment:# get key of first variable; 13, expression_statement; 14, comment:# get key of second variable; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, identifier:user_data; 20, None; 21, comment:"""Triggered upon updating the list of global variables Helper method to sort global variables alphabetically. :param tree_model: Tree model implementing the Gtk.TreeSortable interface. :param iter1: Points at a row. :param iter2: Points at a row. """; 22, assignment; 23, assignment; 24, assignment; 25, assignment; 26, assignment; 27, assignment; 28, comparison_operator:name1_as_bits == name2_as_bits; 29, block; 30, elif_clause; 31, else_clause; 32, identifier:path1; 33, subscript; 34, identifier:path2; 35, subscript; 36, identifier:name1; 37, subscript; 38, identifier:name2; 39, subscript; 40, identifier:name1_as_bits; 41, call; 42, identifier:name2_as_bits; 43, call; 44, identifier:name1_as_bits; 45, identifier:name2_as_bits; 46, return_statement; 47, comparison_operator:name1_as_bits > name2_as_bits; 48, block; 49, block; 50, call; 51, integer:0; 52, call; 53, integer:0; 54, subscript; 55, integer:0; 56, subscript; 57, integer:0; 58, attribute; 59, generator_expression; 60, attribute; 61, generator_expression; 62, integer:0; 63, identifier:name1_as_bits; 64, identifier:name2_as_bits; 65, return_statement; 66, return_statement; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, identifier:tree_model; 72, identifier:path1; 73, identifier:tree_model; 74, identifier:path2; 75, string; 76, identifier:join; 77, call; 78, for_in_clause; 79, string; 80, identifier:join; 81, call; 82, for_in_clause; 83, integer:1; 84, unary_operator; 85, identifier:tree_model; 86, identifier:get_path; 87, identifier:iter1; 88, identifier:tree_model; 89, identifier:get_path; 90, identifier:iter2; 91, string_content:; 92, identifier:format; 93, argument_list; 94, identifier:x; 95, identifier:name1; 96, string_content:; 97, identifier:format; 98, argument_list; 99, identifier:x; 100, identifier:name2; 101, integer:1; 102, call; 103, string; 104, call; 105, string; 106, identifier:ord; 107, argument_list; 108, string_content:b; 109, identifier:ord; 110, argument_list; 111, string_content:b; 112, identifier:x; 113, identifier:x
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 8, 19; 8, 20; 9, 21; 10, 22; 11, 23; 13, 24; 15, 25; 16, 26; 17, 27; 18, 28; 18, 29; 18, 30; 18, 31; 22, 32; 22, 33; 23, 34; 23, 35; 24, 36; 24, 37; 25, 38; 25, 39; 26, 40; 26, 41; 27, 42; 27, 43; 28, 44; 28, 45; 29, 46; 30, 47; 30, 48; 31, 49; 33, 50; 33, 51; 35, 52; 35, 53; 37, 54; 37, 55; 39, 56; 39, 57; 41, 58; 41, 59; 43, 60; 43, 61; 46, 62; 47, 63; 47, 64; 48, 65; 49, 66; 50, 67; 50, 68; 52, 69; 52, 70; 54, 71; 54, 72; 56, 73; 56, 74; 58, 75; 58, 76; 59, 77; 59, 78; 60, 79; 60, 80; 61, 81; 61, 82; 65, 83; 66, 84; 67, 85; 67, 86; 68, 87; 69, 88; 69, 89; 70, 90; 75, 91; 77, 92; 77, 93; 78, 94; 78, 95; 79, 96; 81, 97; 81, 98; 82, 99; 82, 100; 84, 101; 93, 102; 93, 103; 98, 104; 98, 105; 102, 106; 102, 107; 103, 108; 104, 109; 104, 110; 105, 111; 107, 112; 110, 113
def compare_variables(tree_model, iter1, iter2, user_data=None): """Triggered upon updating the list of global variables Helper method to sort global variables alphabetically. :param tree_model: Tree model implementing the Gtk.TreeSortable interface. :param iter1: Points at a row. :param iter2: Points at a row. """ path1 = tree_model.get_path(iter1)[0] path2 = tree_model.get_path(iter2)[0] # get key of first variable name1 = tree_model[path1][0] # get key of second variable name2 = tree_model[path2][0] name1_as_bits = ' '.join(format(ord(x), 'b') for x in name1) name2_as_bits = ' '.join(format(ord(x), 'b') for x in name2) if name1_as_bits == name2_as_bits: return 0 elif name1_as_bits > name2_as_bits: return 1 else: return -1
0, module; 1, function_definition; 2, function_name:load_hook_files; 3, parameters; 4, block; 5, identifier:pathname; 6, expression_statement; 7, global_statement; 8, if_statement; 9, for_statement; 10, comment:""" Loads files either defined as a glob or a single file path sorted by filenames. """; 11, identifier:hooks; 12, boolean_operator; 13, block; 14, else_clause; 15, identifier:path; 16, identifier:fsglob; 17, block; 18, comparison_operator:sys.version_info[0] > 2; 19, comparison_operator:sys.version_info[1] > 4; 20, expression_statement; 21, block; 22, expression_statement; 23, comment:# Append hooks file directory to the sys.path so submodules can be; 24, comment:# loaded too.; 25, if_statement; 26, expression_statement; 27, for_statement; 28, subscript; 29, integer:2; 30, subscript; 31, integer:4; 32, assignment; 33, expression_statement; 34, assignment; 35, comparison_operator:os.path.dirname(real_path) not in sys.path; 36, block; 37, assignment; 38, identifier:name; 39, call; 40, block; 41, attribute; 42, integer:0; 43, attribute; 44, integer:1; 45, identifier:fsglob; 46, call; 47, assignment; 48, identifier:real_path; 49, call; 50, call; 51, attribute; 52, expression_statement; 53, identifier:module; 54, call; 55, identifier:dir; 56, argument_list; 57, expression_statement; 58, if_statement; 59, identifier:sys; 60, identifier:version_info; 61, identifier:sys; 62, identifier:version_info; 63, identifier:sorted; 64, argument_list; 65, identifier:fsglob; 66, call; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, identifier:sys; 72, identifier:path; 73, call; 74, attribute; 75, argument_list; 76, identifier:module; 77, assignment; 78, boolean_operator; 79, block; 80, call; 81, identifier:sorted; 82, argument_list; 83, attribute; 84, identifier:realpath; 85, identifier:path; 86, attribute; 87, identifier:dirname; 88, identifier:real_path; 89, attribute; 90, argument_list; 91, identifier:imp; 92, identifier:load_source; 93, call; 94, identifier:real_path; 95, identifier:obj; 96, call; 97, call; 98, call; 99, expression_statement; 100, for_statement; 101, attribute; 102, argument_list; 103, call; 104, identifier:os; 105, identifier:path; 106, identifier:os; 107, identifier:path; 108, attribute; 109, identifier:append; 110, call; 111, attribute; 112, argument_list; 113, identifier:getattr; 114, argument_list; 115, identifier:hasattr; 116, argument_list; 117, identifier:callable; 118, argument_list; 119, assignment; 120, pattern_list; 121, identifier:func_hooks; 122, block; 123, identifier:glob; 124, identifier:iglob; 125, identifier:pathname; 126, keyword_argument; 127, attribute; 128, argument_list; 129, identifier:sys; 130, identifier:path; 131, attribute; 132, argument_list; 133, attribute; 134, identifier:basename; 135, identifier:path; 136, identifier:module; 137, identifier:name; 138, identifier:obj; 139, string; 140, identifier:obj; 141, identifier:func_hooks; 142, call; 143, identifier:hook; 144, identifier:name; 145, if_statement; 146, if_statement; 147, if_statement; 148, if_statement; 149, if_statement; 150, if_statement; 151, if_statement; 152, if_statement; 153, identifier:recursive; 154, True; 155, identifier:glob; 156, identifier:iglob; 157, identifier:pathname; 158, attribute; 159, identifier:dirname; 160, identifier:real_path; 161, identifier:os; 162, identifier:path; 163, string_content:dredd_hooks; 164, identifier:getattr; 165, argument_list; 166, comparison_operator:hook == BEFORE_ALL; 167, block; 168, comparison_operator:hook == AFTER_ALL; 169, block; 170, comparison_operator:hook == BEFORE_EACH; 171, block; 172, comparison_operator:hook == AFTER_EACH; 173, block; 174, comparison_operator:hook == BEFORE_EACH_VALIDATION; 175, block; 176, comparison_operator:hook == BEFORE_VALIDATION; 177, block; 178, comparison_operator:hook == BEFORE; 179, block; 180, comparison_operator:hook == AFTER; 181, block; 182, identifier:os; 183, identifier:path; 184, identifier:obj; 185, string; 186, identifier:hook; 187, identifier:BEFORE_ALL; 188, expression_statement; 189, identifier:hook; 190, identifier:AFTER_ALL; 191, expression_statement; 192, identifier:hook; 193, identifier:BEFORE_EACH; 194, expression_statement; 195, identifier:hook; 196, identifier:AFTER_EACH; 197, expression_statement; 198, identifier:hook; 199, identifier:BEFORE_EACH_VALIDATION; 200, expression_statement; 201, identifier:hook; 202, identifier:BEFORE_VALIDATION; 203, expression_statement; 204, identifier:hook; 205, identifier:BEFORE; 206, expression_statement; 207, identifier:hook; 208, identifier:AFTER; 209, expression_statement; 210, string_content:dredd_hooks; 211, call; 212, call; 213, call; 214, call; 215, call; 216, call; 217, call; 218, call; 219, attribute; 220, argument_list; 221, attribute; 222, argument_list; 223, attribute; 224, argument_list; 225, attribute; 226, argument_list; 227, attribute; 228, argument_list; 229, identifier:add_named_hook; 230, argument_list; 231, identifier:add_named_hook; 232, argument_list; 233, identifier:add_named_hook; 234, argument_list; 235, attribute; 236, identifier:append; 237, identifier:obj; 238, attribute; 239, identifier:append; 240, identifier:obj; 241, attribute; 242, identifier:append; 243, identifier:obj; 244, attribute; 245, identifier:append; 246, identifier:obj; 247, attribute; 248, identifier:append; 249, identifier:obj; 250, attribute; 251, identifier:obj; 252, identifier:name; 253, attribute; 254, identifier:obj; 255, identifier:name; 256, attribute; 257, identifier:obj; 258, identifier:name; 259, identifier:hooks; 260, identifier:_before_all; 261, identifier:hooks; 262, identifier:_after_all; 263, identifier:hooks; 264, identifier:_before_each; 265, identifier:hooks; 266, identifier:_after_each; 267, identifier:hooks; 268, identifier:_before_each_validation; 269, identifier:hooks; 270, identifier:_before_validation; 271, identifier:hooks; 272, identifier:_before; 273, identifier:hooks; 274, identifier:_after
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 6, 10; 7, 11; 8, 12; 8, 13; 8, 14; 9, 15; 9, 16; 9, 17; 12, 18; 12, 19; 13, 20; 14, 21; 17, 22; 17, 23; 17, 24; 17, 25; 17, 26; 17, 27; 18, 28; 18, 29; 19, 30; 19, 31; 20, 32; 21, 33; 22, 34; 25, 35; 25, 36; 26, 37; 27, 38; 27, 39; 27, 40; 28, 41; 28, 42; 30, 43; 30, 44; 32, 45; 32, 46; 33, 47; 34, 48; 34, 49; 35, 50; 35, 51; 36, 52; 37, 53; 37, 54; 39, 55; 39, 56; 40, 57; 40, 58; 41, 59; 41, 60; 43, 61; 43, 62; 46, 63; 46, 64; 47, 65; 47, 66; 49, 67; 49, 68; 50, 69; 50, 70; 51, 71; 51, 72; 52, 73; 54, 74; 54, 75; 56, 76; 57, 77; 58, 78; 58, 79; 64, 80; 66, 81; 66, 82; 67, 83; 67, 84; 68, 85; 69, 86; 69, 87; 70, 88; 73, 89; 73, 90; 74, 91; 74, 92; 75, 93; 75, 94; 77, 95; 77, 96; 78, 97; 78, 98; 79, 99; 79, 100; 80, 101; 80, 102; 82, 103; 83, 104; 83, 105; 86, 106; 86, 107; 89, 108; 89, 109; 90, 110; 93, 111; 93, 112; 96, 113; 96, 114; 97, 115; 97, 116; 98, 117; 98, 118; 99, 119; 100, 120; 100, 121; 100, 122; 101, 123; 101, 124; 102, 125; 102, 126; 103, 127; 103, 128; 108, 129; 108, 130; 110, 131; 110, 132; 111, 133; 111, 134; 112, 135; 114, 136; 114, 137; 116, 138; 116, 139; 118, 140; 119, 141; 119, 142; 120, 143; 120, 144; 122, 145; 122, 146; 122, 147; 122, 148; 122, 149; 122, 150; 122, 151; 122, 152; 126, 153; 126, 154; 127, 155; 127, 156; 128, 157; 131, 158; 131, 159; 132, 160; 133, 161; 133, 162; 139, 163; 142, 164; 142, 165; 145, 166; 145, 167; 146, 168; 146, 169; 147, 170; 147, 171; 148, 172; 148, 173; 149, 174; 149, 175; 150, 176; 150, 177; 151, 178; 151, 179; 152, 180; 152, 181; 158, 182; 158, 183; 165, 184; 165, 185; 166, 186; 166, 187; 167, 188; 168, 189; 168, 190; 169, 191; 170, 192; 170, 193; 171, 194; 172, 195; 172, 196; 173, 197; 174, 198; 174, 199; 175, 200; 176, 201; 176, 202; 177, 203; 178, 204; 178, 205; 179, 206; 180, 207; 180, 208; 181, 209; 185, 210; 188, 211; 191, 212; 194, 213; 197, 214; 200, 215; 203, 216; 206, 217; 209, 218; 211, 219; 211, 220; 212, 221; 212, 222; 213, 223; 213, 224; 214, 225; 214, 226; 215, 227; 215, 228; 216, 229; 216, 230; 217, 231; 217, 232; 218, 233; 218, 234; 219, 235; 219, 236; 220, 237; 221, 238; 221, 239; 222, 240; 223, 241; 223, 242; 224, 243; 225, 244; 225, 245; 226, 246; 227, 247; 227, 248; 228, 249; 230, 250; 230, 251; 230, 252; 232, 253; 232, 254; 232, 255; 234, 256; 234, 257; 234, 258; 235, 259; 235, 260; 238, 261; 238, 262; 241, 263; 241, 264; 244, 265; 244, 266; 247, 267; 247, 268; 250, 269; 250, 270; 253, 271; 253, 272; 256, 273; 256, 274
def load_hook_files(pathname): """ Loads files either defined as a glob or a single file path sorted by filenames. """ global hooks if sys.version_info[0] > 2 and sys.version_info[1] > 4: fsglob = sorted(glob.iglob(pathname, recursive=True)) else: fsglob = sorted(glob.iglob(pathname)) for path in fsglob: real_path = os.path.realpath(path) # Append hooks file directory to the sys.path so submodules can be # loaded too. if os.path.dirname(real_path) not in sys.path: sys.path.append(os.path.dirname(real_path)) module = imp.load_source(os.path.basename(path), real_path) for name in dir(module): obj = getattr(module, name) if hasattr(obj, 'dredd_hooks') and callable(obj): func_hooks = getattr(obj, 'dredd_hooks') for hook, name in func_hooks: if hook == BEFORE_ALL: hooks._before_all.append(obj) if hook == AFTER_ALL: hooks._after_all.append(obj) if hook == BEFORE_EACH: hooks._before_each.append(obj) if hook == AFTER_EACH: hooks._after_each.append(obj) if hook == BEFORE_EACH_VALIDATION: hooks._before_each_validation.append(obj) if hook == BEFORE_VALIDATION: add_named_hook(hooks._before_validation, obj, name) if hook == BEFORE: add_named_hook(hooks._before, obj, name) if hook == AFTER: add_named_hook(hooks._after, obj, name)
0, module; 1, function_definition; 2, function_name:sort; 3, parameters; 4, block; 5, identifier:imports; 6, default_parameter; 7, default_parameter; 8, dictionary_splat_pattern; 9, expression_statement; 10, if_statement; 11, if_statement; 12, comment:# Partition the imports; 13, expression_statement; 14, for_statement; 15, comment:# sort each of the segments; 16, for_statement; 17, return_statement; 18, identifier:separate; 19, True; 20, identifier:import_before_from; 21, True; 22, identifier:classify_kwargs; 23, comment:"""Sort import objects into groups. :param list imports: FromImport / ImportImport objects :param bool separate: Whether to classify and return separate segments of imports based on classification. :param bool import_before_from: Whether to sort `import ...` imports before `from ...` imports. For example: from os import path from aspy import refactor_imports import sys import pyramid separate = True, import_before_from = True import sys from os import path import pyramid from aspy import refactor_imports separate = True, import_before_from = False from os import path import sys import pyramid from aspy import refactor_imports separate = False, import_before_from = True import pyramid import sys from aspy import refactor_imports from os import path separate = False, import_before_from = False from aspy import refactor_imports from os import path import pyramid import sys """; 24, identifier:separate; 25, block; 26, else_clause; 27, identifier:import_before_from; 28, block; 29, else_clause; 30, assignment; 31, identifier:import_obj; 32, identifier:imports; 33, block; 34, pattern_list; 35, call; 36, block; 37, call; 38, function_definition; 39, expression_statement; 40, comment:# A little cheaty, this allows future imports to sort before others; 41, block; 42, function_definition; 43, block; 44, identifier:imports_partitioned; 45, call; 46, expression_statement; 47, identifier:segment_key; 48, identifier:val; 49, attribute; 50, argument_list; 51, expression_statement; 52, identifier:tuple; 53, generator_expression; 54, function_name:classify_func; 55, parameters; 56, block; 57, assignment; 58, function_definition; 59, expression_statement; 60, function_name:sort_within; 61, parameters; 62, block; 63, function_definition; 64, attribute; 65, argument_list; 66, call; 67, identifier:imports_partitioned; 68, identifier:items; 69, assignment; 70, call; 71, for_in_clause; 72, if_clause; 73, identifier:obj; 74, return_statement; 75, identifier:types; 76, attribute; 77, function_name:classify_func; 78, parameters; 79, block; 80, assignment; 81, identifier:obj; 82, return_statement; 83, function_name:sort_within; 84, parameters; 85, block; 86, identifier:collections; 87, identifier:defaultdict; 88, identifier:list; 89, attribute; 90, argument_list; 91, subscript; 92, call; 93, identifier:tuple; 94, argument_list; 95, identifier:key; 96, identifier:types; 97, comparison_operator:key in imports_partitioned; 98, call; 99, identifier:ImportType; 100, identifier:__all__; 101, identifier:obj; 102, return_statement; 103, identifier:types; 104, list; 105, binary_operator:(CLS_TO_INDEX[type(obj)],) + obj.sort_key; 106, identifier:obj; 107, return_statement; 108, subscript; 109, identifier:append; 110, identifier:import_obj; 111, identifier:imports_partitioned; 112, identifier:segment_key; 113, identifier:sorted; 114, argument_list; 115, subscript; 116, identifier:key; 117, identifier:imports_partitioned; 118, identifier:classify_import; 119, argument_list; 120, comparison_operator:classify_import( obj.import_statement.module, **classify_kwargs ) == ImportType.FUTURE; 121, True; 122, False; 123, tuple; 124, attribute; 125, call; 126, identifier:imports_partitioned; 127, call; 128, identifier:val; 129, keyword_argument; 130, identifier:imports_partitioned; 131, identifier:key; 132, attribute; 133, dictionary_splat; 134, call; 135, attribute; 136, subscript; 137, identifier:obj; 138, identifier:sort_key; 139, identifier:tuple; 140, argument_list; 141, identifier:classify_func; 142, argument_list; 143, identifier:key; 144, identifier:sort_within; 145, attribute; 146, identifier:module; 147, identifier:classify_kwargs; 148, identifier:classify_import; 149, argument_list; 150, identifier:ImportType; 151, identifier:FUTURE; 152, identifier:CLS_TO_INDEX; 153, call; 154, attribute; 155, identifier:import_obj; 156, identifier:obj; 157, identifier:import_statement; 158, attribute; 159, dictionary_splat; 160, identifier:type; 161, argument_list; 162, identifier:obj; 163, identifier:sort_key; 164, attribute; 165, identifier:module; 166, identifier:classify_kwargs; 167, identifier:obj; 168, identifier:obj; 169, identifier:import_statement
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 6, 18; 6, 19; 7, 20; 7, 21; 8, 22; 9, 23; 10, 24; 10, 25; 10, 26; 11, 27; 11, 28; 11, 29; 13, 30; 14, 31; 14, 32; 14, 33; 16, 34; 16, 35; 16, 36; 17, 37; 25, 38; 25, 39; 26, 40; 26, 41; 28, 42; 29, 43; 30, 44; 30, 45; 33, 46; 34, 47; 34, 48; 35, 49; 35, 50; 36, 51; 37, 52; 37, 53; 38, 54; 38, 55; 38, 56; 39, 57; 41, 58; 41, 59; 42, 60; 42, 61; 42, 62; 43, 63; 45, 64; 45, 65; 46, 66; 49, 67; 49, 68; 51, 69; 53, 70; 53, 71; 53, 72; 55, 73; 56, 74; 57, 75; 57, 76; 58, 77; 58, 78; 58, 79; 59, 80; 61, 81; 62, 82; 63, 83; 63, 84; 63, 85; 64, 86; 64, 87; 65, 88; 66, 89; 66, 90; 69, 91; 69, 92; 70, 93; 70, 94; 71, 95; 71, 96; 72, 97; 74, 98; 76, 99; 76, 100; 78, 101; 79, 102; 80, 103; 80, 104; 82, 105; 84, 106; 85, 107; 89, 108; 89, 109; 90, 110; 91, 111; 91, 112; 92, 113; 92, 114; 94, 115; 97, 116; 97, 117; 98, 118; 98, 119; 102, 120; 104, 121; 104, 122; 105, 123; 105, 124; 107, 125; 108, 126; 108, 127; 114, 128; 114, 129; 115, 130; 115, 131; 119, 132; 119, 133; 120, 134; 120, 135; 123, 136; 124, 137; 124, 138; 125, 139; 125, 140; 127, 141; 127, 142; 129, 143; 129, 144; 132, 145; 132, 146; 133, 147; 134, 148; 134, 149; 135, 150; 135, 151; 136, 152; 136, 153; 140, 154; 142, 155; 145, 156; 145, 157; 149, 158; 149, 159; 153, 160; 153, 161; 154, 162; 154, 163; 158, 164; 158, 165; 159, 166; 161, 167; 164, 168; 164, 169
def sort(imports, separate=True, import_before_from=True, **classify_kwargs): """Sort import objects into groups. :param list imports: FromImport / ImportImport objects :param bool separate: Whether to classify and return separate segments of imports based on classification. :param bool import_before_from: Whether to sort `import ...` imports before `from ...` imports. For example: from os import path from aspy import refactor_imports import sys import pyramid separate = True, import_before_from = True import sys from os import path import pyramid from aspy import refactor_imports separate = True, import_before_from = False from os import path import sys import pyramid from aspy import refactor_imports separate = False, import_before_from = True import pyramid import sys from aspy import refactor_imports from os import path separate = False, import_before_from = False from aspy import refactor_imports from os import path import pyramid import sys """ if separate: def classify_func(obj): return classify_import( obj.import_statement.module, **classify_kwargs ) types = ImportType.__all__ else: # A little cheaty, this allows future imports to sort before others def classify_func(obj): return classify_import( obj.import_statement.module, **classify_kwargs ) == ImportType.FUTURE types = [True, False] if import_before_from: def sort_within(obj): return (CLS_TO_INDEX[type(obj)],) + obj.sort_key else: def sort_within(obj): return tuple(obj.sort_key) # Partition the imports imports_partitioned = collections.defaultdict(list) for import_obj in imports: imports_partitioned[classify_func(import_obj)].append(import_obj) # sort each of the segments for segment_key, val in imports_partitioned.items(): imports_partitioned[segment_key] = sorted(val, key=sort_within) return tuple( tuple(imports_partitioned[key]) for key in types if key in imports_partitioned )
0, module; 1, function_definition; 2, function_name:sort_recursive; 3, parameters; 4, block; 5, identifier:data; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, return_statement; 10, comment:""" Recursively sorts all elements in a dictionary Args: data (dict): The dictionary to sort Returns: sorted_dict (OrderedDict): The sorted data dict """; 11, assignment; 12, identifier:i; 13, call; 14, block; 15, call; 16, identifier:newdict; 17, dictionary; 18, attribute; 19, argument_list; 20, if_statement; 21, identifier:OrderedDict; 22, argument_list; 23, identifier:data; 24, identifier:items; 25, comparison_operator:type(i[1]) is dict; 26, block; 27, else_clause; 28, call; 29, call; 30, identifier:dict; 31, expression_statement; 32, block; 33, identifier:sorted; 34, argument_list; 35, identifier:type; 36, argument_list; 37, assignment; 38, expression_statement; 39, call; 40, keyword_argument; 41, subscript; 42, subscript; 43, call; 44, assignment; 45, attribute; 46, argument_list; 47, identifier:key; 48, lambda; 49, identifier:i; 50, integer:1; 51, identifier:newdict; 52, subscript; 53, identifier:sort_recursive; 54, argument_list; 55, subscript; 56, subscript; 57, identifier:newdict; 58, identifier:items; 59, lambda_parameters; 60, tuple; 61, identifier:i; 62, integer:0; 63, subscript; 64, identifier:newdict; 65, subscript; 66, identifier:i; 67, integer:1; 68, identifier:item; 69, call; 70, subscript; 71, identifier:i; 72, integer:1; 73, identifier:i; 74, integer:0; 75, identifier:compare_type; 76, argument_list; 77, identifier:item; 78, integer:0; 79, call; 80, identifier:type; 81, argument_list; 82, subscript; 83, identifier:item; 84, integer:1
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 6, 10; 7, 11; 8, 12; 8, 13; 8, 14; 9, 15; 11, 16; 11, 17; 13, 18; 13, 19; 14, 20; 15, 21; 15, 22; 18, 23; 18, 24; 20, 25; 20, 26; 20, 27; 22, 28; 25, 29; 25, 30; 26, 31; 27, 32; 28, 33; 28, 34; 29, 35; 29, 36; 31, 37; 32, 38; 34, 39; 34, 40; 36, 41; 37, 42; 37, 43; 38, 44; 39, 45; 39, 46; 40, 47; 40, 48; 41, 49; 41, 50; 42, 51; 42, 52; 43, 53; 43, 54; 44, 55; 44, 56; 45, 57; 45, 58; 48, 59; 48, 60; 52, 61; 52, 62; 54, 63; 55, 64; 55, 65; 56, 66; 56, 67; 59, 68; 60, 69; 60, 70; 63, 71; 63, 72; 65, 73; 65, 74; 69, 75; 69, 76; 70, 77; 70, 78; 76, 79; 79, 80; 79, 81; 81, 82; 82, 83; 82, 84
def sort_recursive(data): """ Recursively sorts all elements in a dictionary Args: data (dict): The dictionary to sort Returns: sorted_dict (OrderedDict): The sorted data dict """ newdict = {} for i in data.items(): if type(i[1]) is dict: newdict[i[0]] = sort_recursive(i[1]) else: newdict[i[0]] = i[1] return OrderedDict(sorted(newdict.items(), key=lambda item: (compare_type(type(item[1])), item[0])))
0, module; 1, function_definition; 2, function_name:names_to_abbreviations; 3, parameters; 4, block; 5, identifier:reporters; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, expression_statement; 10, return_statement; 11, comment:"""Build a dict mapping names to their variations Something like: { "Atlantic Reporter": ['A.', 'A.2d'], } Note that the abbreviations are sorted by start date. """; 12, assignment; 13, pattern_list; 14, call; 15, block; 16, assignment; 17, identifier:sorted_names; 18, identifier:names; 19, dictionary; 20, identifier:reporter_key; 21, identifier:data_list; 22, attribute; 23, argument_list; 24, for_statement; 25, identifier:sorted_names; 26, call; 27, identifier:reporters; 28, identifier:items; 29, identifier:data; 30, identifier:data_list; 31, block; 32, identifier:OrderedDict; 33, argument_list; 34, expression_statement; 35, comment:# Sort abbreviations by start date of the edition; 36, expression_statement; 37, expression_statement; 38, expression_statement; 39, call; 40, assignment; 41, assignment; 42, assignment; 43, assignment; 44, identifier:sorted; 45, argument_list; 46, identifier:abbrevs; 47, call; 48, identifier:sort_func; 49, lambda; 50, identifier:abbrevs; 51, call; 52, subscript; 53, identifier:abbrevs; 54, call; 55, keyword_argument; 56, attribute; 57, argument_list; 58, lambda_parameters; 59, binary_operator:str(data['editions'][x]['start']) + x; 60, identifier:sorted; 61, argument_list; 62, identifier:names; 63, subscript; 64, attribute; 65, argument_list; 66, identifier:key; 67, lambda; 68, subscript; 69, identifier:keys; 70, identifier:x; 71, call; 72, identifier:x; 73, identifier:abbrevs; 74, keyword_argument; 75, identifier:data; 76, string; 77, identifier:names; 78, identifier:items; 79, lambda_parameters; 80, subscript; 81, identifier:data; 82, string; 83, identifier:str; 84, argument_list; 85, identifier:key; 86, identifier:sort_func; 87, string_content:name; 88, identifier:t; 89, identifier:t; 90, integer:0; 91, string_content:editions; 92, subscript; 93, subscript; 94, string; 95, subscript; 96, identifier:x; 97, string_content:start; 98, identifier:data; 99, string; 100, string_content:editions
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 7, 12; 8, 13; 8, 14; 8, 15; 9, 16; 10, 17; 12, 18; 12, 19; 13, 20; 13, 21; 14, 22; 14, 23; 15, 24; 16, 25; 16, 26; 22, 27; 22, 28; 24, 29; 24, 30; 24, 31; 26, 32; 26, 33; 31, 34; 31, 35; 31, 36; 31, 37; 31, 38; 33, 39; 34, 40; 36, 41; 37, 42; 38, 43; 39, 44; 39, 45; 40, 46; 40, 47; 41, 48; 41, 49; 42, 50; 42, 51; 43, 52; 43, 53; 45, 54; 45, 55; 47, 56; 47, 57; 49, 58; 49, 59; 51, 60; 51, 61; 52, 62; 52, 63; 54, 64; 54, 65; 55, 66; 55, 67; 56, 68; 56, 69; 58, 70; 59, 71; 59, 72; 61, 73; 61, 74; 63, 75; 63, 76; 64, 77; 64, 78; 67, 79; 67, 80; 68, 81; 68, 82; 71, 83; 71, 84; 74, 85; 74, 86; 76, 87; 79, 88; 80, 89; 80, 90; 82, 91; 84, 92; 92, 93; 92, 94; 93, 95; 93, 96; 94, 97; 95, 98; 95, 99; 99, 100
def names_to_abbreviations(reporters): """Build a dict mapping names to their variations Something like: { "Atlantic Reporter": ['A.', 'A.2d'], } Note that the abbreviations are sorted by start date. """ names = {} for reporter_key, data_list in reporters.items(): for data in data_list: abbrevs = data['editions'].keys() # Sort abbreviations by start date of the edition sort_func = lambda x: str(data['editions'][x]['start']) + x abbrevs = sorted(abbrevs, key=sort_func) names[data['name']] = abbrevs sorted_names = OrderedDict(sorted(names.items(), key=lambda t: t[0])) return sorted_names
0, module; 1, function_definition; 2, function_name:flatten; 3, parameters; 4, block; 5, identifier:weights; 6, expression_statement; 7, comment:# NOQA; 8, if_statement; 9, return_statement; 10, comment:""" Flatten weights into a long DataFrame. Parameters ---------- weights: pandas.DataFrame or dict A DataFrame of instrument weights with a MultiIndex where the top level contains pandas. Timestamps and the second level is instrument names. The columns consist of generic names. If dict is given this should be a dict of pandas.DataFrame in the above format, with keys for different root generics, e.g. 'CL' Returns ------- A long DataFrame of weights, where columns are "date", "contract", "generic" and "weight". If a dictionary is passed, DataFrame will contain additional colum "key" containing the key value and be sorted according to this key value. Example ------- >>> import pandas as pd >>> import mapping.util as util >>> vals = [[1, 0], [0, 1], [1, 0], [0, 1]] >>> widx = pd.MultiIndex.from_tuples([(pd.Timestamp('2015-01-03'), 'CLF5'), ... (pd.Timestamp('2015-01-03'), 'CLG5'), ... (pd.Timestamp('2015-01-04'), 'CLG5'), ... (pd.Timestamp('2015-01-04'), 'CLH5')]) >>> weights = pd.DataFrame(vals, index=widx, columns=["CL1", "CL2"]) >>> util.flatten(weights) """; 11, call; 12, block; 13, elif_clause; 14, else_clause; 15, identifier:wts; 16, identifier:isinstance; 17, argument_list; 18, expression_statement; 19, expression_statement; 20, call; 21, block; 22, block; 23, identifier:weights; 24, attribute; 25, assignment; 26, assignment; 27, identifier:isinstance; 28, argument_list; 29, expression_statement; 30, for_statement; 31, expression_statement; 32, raise_statement; 33, identifier:pd; 34, identifier:DataFrame; 35, identifier:wts; 36, call; 37, attribute; 38, list; 39, identifier:weights; 40, identifier:dict; 41, assignment; 42, identifier:key; 43, call; 44, block; 45, assignment; 46, call; 47, attribute; 48, argument_list; 49, identifier:wts; 50, identifier:columns; 51, string:"date"; 52, string:"contract"; 53, string:"generic"; 54, string:"weight"; 55, identifier:wts; 56, list; 57, identifier:sorted; 58, argument_list; 59, expression_statement; 60, expression_statement; 61, expression_statement; 62, expression_statement; 63, identifier:wts; 64, call; 65, identifier:ValueError; 66, argument_list; 67, call; 68, identifier:reset_index; 69, call; 70, assignment; 71, assignment; 72, assignment; 73, call; 74, attribute; 75, argument_list; 76, string:"weights must be pd.DataFrame or dict"; 77, attribute; 78, argument_list; 79, attribute; 80, argument_list; 81, identifier:wt; 82, call; 83, attribute; 84, list; 85, subscript; 86, identifier:key; 87, attribute; 88, argument_list; 89, call; 90, identifier:reset_index; 91, keyword_argument; 92, identifier:weights; 93, identifier:stack; 94, identifier:weights; 95, identifier:keys; 96, attribute; 97, argument_list; 98, identifier:wt; 99, identifier:columns; 100, string:"date"; 101, string:"contract"; 102, string:"generic"; 103, string:"weight"; 104, attribute; 105, slice; 106, string:"key"; 107, identifier:wts; 108, identifier:append; 109, identifier:wt; 110, attribute; 111, argument_list; 112, identifier:drop; 113, True; 114, call; 115, identifier:reset_index; 116, identifier:wt; 117, identifier:loc; 118, identifier:pd; 119, identifier:concat; 120, identifier:wts; 121, keyword_argument; 122, attribute; 123, argument_list; 124, identifier:axis; 125, integer:0; 126, subscript; 127, identifier:stack; 128, identifier:weights; 129, identifier:key
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 6, 10; 8, 11; 8, 12; 8, 13; 8, 14; 9, 15; 11, 16; 11, 17; 12, 18; 12, 19; 13, 20; 13, 21; 14, 22; 17, 23; 17, 24; 18, 25; 19, 26; 20, 27; 20, 28; 21, 29; 21, 30; 21, 31; 22, 32; 24, 33; 24, 34; 25, 35; 25, 36; 26, 37; 26, 38; 28, 39; 28, 40; 29, 41; 30, 42; 30, 43; 30, 44; 31, 45; 32, 46; 36, 47; 36, 48; 37, 49; 37, 50; 38, 51; 38, 52; 38, 53; 38, 54; 41, 55; 41, 56; 43, 57; 43, 58; 44, 59; 44, 60; 44, 61; 44, 62; 45, 63; 45, 64; 46, 65; 46, 66; 47, 67; 47, 68; 58, 69; 59, 70; 60, 71; 61, 72; 62, 73; 64, 74; 64, 75; 66, 76; 67, 77; 67, 78; 69, 79; 69, 80; 70, 81; 70, 82; 71, 83; 71, 84; 72, 85; 72, 86; 73, 87; 73, 88; 74, 89; 74, 90; 75, 91; 77, 92; 77, 93; 79, 94; 79, 95; 82, 96; 82, 97; 83, 98; 83, 99; 84, 100; 84, 101; 84, 102; 84, 103; 85, 104; 85, 105; 85, 106; 87, 107; 87, 108; 88, 109; 89, 110; 89, 111; 91, 112; 91, 113; 96, 114; 96, 115; 104, 116; 104, 117; 110, 118; 110, 119; 111, 120; 111, 121; 114, 122; 114, 123; 121, 124; 121, 125; 122, 126; 122, 127; 126, 128; 126, 129
def flatten(weights): """ Flatten weights into a long DataFrame. Parameters ---------- weights: pandas.DataFrame or dict A DataFrame of instrument weights with a MultiIndex where the top level contains pandas. Timestamps and the second level is instrument names. The columns consist of generic names. If dict is given this should be a dict of pandas.DataFrame in the above format, with keys for different root generics, e.g. 'CL' Returns ------- A long DataFrame of weights, where columns are "date", "contract", "generic" and "weight". If a dictionary is passed, DataFrame will contain additional colum "key" containing the key value and be sorted according to this key value. Example ------- >>> import pandas as pd >>> import mapping.util as util >>> vals = [[1, 0], [0, 1], [1, 0], [0, 1]] >>> widx = pd.MultiIndex.from_tuples([(pd.Timestamp('2015-01-03'), 'CLF5'), ... (pd.Timestamp('2015-01-03'), 'CLG5'), ... (pd.Timestamp('2015-01-04'), 'CLG5'), ... (pd.Timestamp('2015-01-04'), 'CLH5')]) >>> weights = pd.DataFrame(vals, index=widx, columns=["CL1", "CL2"]) >>> util.flatten(weights) """ # NOQA if isinstance(weights, pd.DataFrame): wts = weights.stack().reset_index() wts.columns = ["date", "contract", "generic", "weight"] elif isinstance(weights, dict): wts = [] for key in sorted(weights.keys()): wt = weights[key].stack().reset_index() wt.columns = ["date", "contract", "generic", "weight"] wt.loc[:, "key"] = key wts.append(wt) wts = pd.concat(wts, axis=0).reset_index(drop=True) else: raise ValueError("weights must be pd.DataFrame or dict") return wts
0, module; 1, function_definition; 2, function_name:calc_trades; 3, parameters; 4, block; 5, identifier:current_contracts; 6, identifier:desired_holdings; 7, identifier:trade_weights; 8, identifier:prices; 9, identifier:multipliers; 10, dictionary_splat_pattern; 11, expression_statement; 12, if_statement; 13, expression_statement; 14, for_statement; 15, if_statement; 16, expression_statement; 17, for_statement; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, return_statement; 23, identifier:kwargs; 24, comment:""" Calculate the number of tradeable contracts for rebalancing from a set of current contract holdings to a set of desired generic notional holdings based on prevailing prices and mapping from generics to tradeable instruments. Differences between current holdings and desired holdings are treated as 0. Zero trades are dropped. Parameters ---------- current_contracts: pandas.Series Series of current number of contracts held for tradeable instruments. Can pass 0 if all holdings are 0. desired_holdings: pandas.Series Series of desired holdings in base notional currency of generics. Index is generic contracts, these should be the same generics as in trade_weights. trade_weights: pandas.DataFrame or dict A pandas.DataFrame of loadings of generic contracts on tradeable instruments **for a given date**. The columns refer to generic contracts and the index is strings representing instrument names. If dict is given keys should be root generic names, e.g. 'CL', and values should be pandas.DataFrames of loadings. The union of all columns should be a superset of the desired_holdings.index prices: pandas.Series Series of instrument prices. Index is instrument name and values are number of contracts. Extra instrument prices will be ignored. multipliers: pandas.Series Series of instrument multipliers. Index is instrument name and values are the multiplier associated with the contract. multipliers.index should be a superset of mapped desired_holdings intruments. kwargs: key word arguments Key word arguments to be passed to to_contracts() Returns ------- A pandas.Series of instrument contract trades, lexigraphically sorted. Example ------- >>> import pandas as pd >>> import mapping.util as util >>> wts = pd.DataFrame([[0.5, 0], [0.5, 0.5], [0, 0.5]], ... index=["CLX16", "CLZ16", "CLF17"], ... columns=["CL1", "CL2"]) >>> desired_holdings = pd.Series([200000, -50000], index=["CL1", "CL2"]) >>> current_contracts = pd.Series([0, 1, 0], ... index=['CLX16', 'CLZ16', 'CLF17']) >>> prices = pd.Series([50.32, 50.41, 50.48], ... index=['CLX16', 'CLZ16', 'CLF17']) >>> multipliers = pd.Series([100, 100, 100], ... index=['CLX16', 'CLZ16', 'CLF17']) >>> trades = util.calc_trades(current_contracts, desired_holdings, wts, ... prices, multipliers) """; 25, not_operator; 26, block; 27, assignment; 28, identifier:key; 29, identifier:trade_weights; 30, block; 31, not_operator; 32, block; 33, assignment; 34, identifier:root_key; 35, identifier:trade_weights; 36, block; 37, assignment; 38, assignment; 39, assignment; 40, assignment; 41, identifier:trades; 42, call; 43, expression_statement; 44, identifier:generics; 45, list; 46, expression_statement; 47, call; 48, raise_statement; 49, identifier:desired_contracts; 50, list; 51, expression_statement; 52, expression_statement; 53, expression_statement; 54, expression_statement; 55, comment:# drop indexes where all non zero weights were in columns dropped above; 56, expression_statement; 57, expression_statement; 58, expression_statement; 59, expression_statement; 60, expression_statement; 61, identifier:desired_contracts; 62, call; 63, identifier:trades; 64, call; 65, identifier:trades; 66, subscript; 67, identifier:trades; 68, call; 69, identifier:isinstance; 70, argument_list; 71, assignment; 72, call; 73, attribute; 74, argument_list; 75, call; 76, assignment; 77, assignment; 78, assignment; 79, assignment; 80, assignment; 81, assignment; 82, assignment; 83, assignment; 84, call; 85, attribute; 86, argument_list; 87, attribute; 88, argument_list; 89, attribute; 90, comparison_operator:trades != 0; 91, attribute; 92, argument_list; 93, identifier:trade_weights; 94, identifier:dict; 95, identifier:trade_weights; 96, dictionary; 97, attribute; 98, argument_list; 99, call; 100, identifier:issubset; 101, call; 102, identifier:ValueError; 103, argument_list; 104, identifier:gnrc_weights; 105, subscript; 106, identifier:subset; 107, call; 108, identifier:gnrc_des_hlds; 109, subscript; 110, identifier:gnrc_weights; 111, subscript; 112, identifier:gnrc_weights; 113, subscript; 114, identifier:instr_des_hlds; 115, binary_operator:gnrc_des_hlds * gnrc_weights; 116, identifier:instr_des_hlds; 117, call; 118, identifier:wprices; 119, subscript; 120, attribute; 121, argument_list; 122, identifier:pd; 123, identifier:concat; 124, identifier:desired_contracts; 125, keyword_argument; 126, identifier:desired_contracts; 127, identifier:subtract; 128, identifier:current_contracts; 129, keyword_argument; 130, identifier:trades; 131, identifier:loc; 132, identifier:trades; 133, integer:0; 134, identifier:trades; 135, identifier:sort_index; 136, pair; 137, identifier:generics; 138, identifier:extend; 139, attribute; 140, identifier:set; 141, argument_list; 142, identifier:set; 143, argument_list; 144, call; 145, identifier:trade_weights; 146, identifier:root_key; 147, attribute; 148, argument_list; 149, attribute; 150, identifier:subset; 151, attribute; 152, slice; 153, identifier:subset; 154, attribute; 155, unary_operator; 156, identifier:gnrc_des_hlds; 157, identifier:gnrc_weights; 158, attribute; 159, argument_list; 160, attribute; 161, attribute; 162, identifier:desired_contracts; 163, identifier:append; 164, call; 165, identifier:axis; 166, integer:0; 167, identifier:fill_value; 168, integer:0; 169, string:""; 170, identifier:trade_weights; 171, subscript; 172, identifier:columns; 173, attribute; 174, identifier:generics; 175, attribute; 176, argument_list; 177, attribute; 178, identifier:intersection; 179, attribute; 180, identifier:desired_holdings; 181, identifier:loc; 182, identifier:gnrc_weights; 183, identifier:loc; 184, identifier:gnrc_weights; 185, identifier:loc; 186, call; 187, identifier:instr_des_hlds; 188, identifier:sum; 189, keyword_argument; 190, identifier:prices; 191, identifier:loc; 192, identifier:instr_des_hlds; 193, identifier:index; 194, identifier:to_contracts; 195, argument_list; 196, identifier:trade_weights; 197, identifier:key; 198, identifier:desired_holdings; 199, identifier:index; 200, concatenated_string; 201, identifier:format; 202, call; 203, call; 204, identifier:gnrc_weights; 205, identifier:columns; 206, identifier:desired_holdings; 207, identifier:index; 208, attribute; 209, argument_list; 210, identifier:axis; 211, integer:1; 212, identifier:instr_des_hlds; 213, identifier:wprices; 214, identifier:multipliers; 215, dictionary_splat; 216, string:"'desired_holdings.index' contains values which "; 217, string:"cannot be mapped to tradeables.\n"; 218, string:"Received: 'desired_holdings.index'\n {0}\n"; 219, string:"Expected in 'trade_weights' set of columns:\n {1}\n"; 220, identifier:sorted; 221, argument_list; 222, identifier:sorted; 223, argument_list; 224, parenthesized_expression; 225, identifier:all; 226, keyword_argument; 227, identifier:kwargs; 228, attribute; 229, identifier:generics; 230, comparison_operator:gnrc_weights == 0; 231, identifier:axis; 232, integer:1; 233, identifier:desired_holdings; 234, identifier:index; 235, identifier:gnrc_weights; 236, integer:0
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 10, 23; 11, 24; 12, 25; 12, 26; 13, 27; 14, 28; 14, 29; 14, 30; 15, 31; 15, 32; 16, 33; 17, 34; 17, 35; 17, 36; 18, 37; 19, 38; 20, 39; 21, 40; 22, 41; 25, 42; 26, 43; 27, 44; 27, 45; 30, 46; 31, 47; 32, 48; 33, 49; 33, 50; 36, 51; 36, 52; 36, 53; 36, 54; 36, 55; 36, 56; 36, 57; 36, 58; 36, 59; 36, 60; 37, 61; 37, 62; 38, 63; 38, 64; 39, 65; 39, 66; 40, 67; 40, 68; 42, 69; 42, 70; 43, 71; 46, 72; 47, 73; 47, 74; 48, 75; 51, 76; 52, 77; 53, 78; 54, 79; 56, 80; 57, 81; 58, 82; 59, 83; 60, 84; 62, 85; 62, 86; 64, 87; 64, 88; 66, 89; 66, 90; 68, 91; 68, 92; 70, 93; 70, 94; 71, 95; 71, 96; 72, 97; 72, 98; 73, 99; 73, 100; 74, 101; 75, 102; 75, 103; 76, 104; 76, 105; 77, 106; 77, 107; 78, 108; 78, 109; 79, 110; 79, 111; 80, 112; 80, 113; 81, 114; 81, 115; 82, 116; 82, 117; 83, 118; 83, 119; 84, 120; 84, 121; 85, 122; 85, 123; 86, 124; 86, 125; 87, 126; 87, 127; 88, 128; 88, 129; 89, 130; 89, 131; 90, 132; 90, 133; 91, 134; 91, 135; 96, 136; 97, 137; 97, 138; 98, 139; 99, 140; 99, 141; 101, 142; 101, 143; 103, 144; 105, 145; 105, 146; 107, 147; 107, 148; 109, 149; 109, 150; 111, 151; 111, 152; 111, 153; 113, 154; 113, 155; 115, 156; 115, 157; 117, 158; 117, 159; 119, 160; 119, 161; 120, 162; 120, 163; 121, 164; 125, 165; 125, 166; 129, 167; 129, 168; 136, 169; 136, 170; 139, 171; 139, 172; 141, 173; 143, 174; 144, 175; 144, 176; 147, 177; 147, 178; 148, 179; 149, 180; 149, 181; 151, 182; 151, 183; 154, 184; 154, 185; 155, 186; 158, 187; 158, 188; 159, 189; 160, 190; 160, 191; 161, 192; 161, 193; 164, 194; 164, 195; 171, 196; 171, 197; 173, 198; 173, 199; 175, 200; 175, 201; 176, 202; 176, 203; 177, 204; 177, 205; 179, 206; 179, 207; 186, 208; 186, 209; 189, 210; 189, 211; 195, 212; 195, 213; 195, 214; 195, 215; 200, 216; 200, 217; 200, 218; 200, 219; 202, 220; 202, 221; 203, 222; 203, 223; 208, 224; 208, 225; 209, 226; 215, 227; 221, 228; 223, 229; 224, 230; 226, 231; 226, 232; 228, 233; 228, 234; 230, 235; 230, 236
def calc_trades(current_contracts, desired_holdings, trade_weights, prices, multipliers, **kwargs): """ Calculate the number of tradeable contracts for rebalancing from a set of current contract holdings to a set of desired generic notional holdings based on prevailing prices and mapping from generics to tradeable instruments. Differences between current holdings and desired holdings are treated as 0. Zero trades are dropped. Parameters ---------- current_contracts: pandas.Series Series of current number of contracts held for tradeable instruments. Can pass 0 if all holdings are 0. desired_holdings: pandas.Series Series of desired holdings in base notional currency of generics. Index is generic contracts, these should be the same generics as in trade_weights. trade_weights: pandas.DataFrame or dict A pandas.DataFrame of loadings of generic contracts on tradeable instruments **for a given date**. The columns refer to generic contracts and the index is strings representing instrument names. If dict is given keys should be root generic names, e.g. 'CL', and values should be pandas.DataFrames of loadings. The union of all columns should be a superset of the desired_holdings.index prices: pandas.Series Series of instrument prices. Index is instrument name and values are number of contracts. Extra instrument prices will be ignored. multipliers: pandas.Series Series of instrument multipliers. Index is instrument name and values are the multiplier associated with the contract. multipliers.index should be a superset of mapped desired_holdings intruments. kwargs: key word arguments Key word arguments to be passed to to_contracts() Returns ------- A pandas.Series of instrument contract trades, lexigraphically sorted. Example ------- >>> import pandas as pd >>> import mapping.util as util >>> wts = pd.DataFrame([[0.5, 0], [0.5, 0.5], [0, 0.5]], ... index=["CLX16", "CLZ16", "CLF17"], ... columns=["CL1", "CL2"]) >>> desired_holdings = pd.Series([200000, -50000], index=["CL1", "CL2"]) >>> current_contracts = pd.Series([0, 1, 0], ... index=['CLX16', 'CLZ16', 'CLF17']) >>> prices = pd.Series([50.32, 50.41, 50.48], ... index=['CLX16', 'CLZ16', 'CLF17']) >>> multipliers = pd.Series([100, 100, 100], ... index=['CLX16', 'CLZ16', 'CLF17']) >>> trades = util.calc_trades(current_contracts, desired_holdings, wts, ... prices, multipliers) """ if not isinstance(trade_weights, dict): trade_weights = {"": trade_weights} generics = [] for key in trade_weights: generics.extend(trade_weights[key].columns) if not set(desired_holdings.index).issubset(set(generics)): raise ValueError("'desired_holdings.index' contains values which " "cannot be mapped to tradeables.\n" "Received: 'desired_holdings.index'\n {0}\n" "Expected in 'trade_weights' set of columns:\n {1}\n" .format(sorted(desired_holdings.index), sorted(generics))) desired_contracts = [] for root_key in trade_weights: gnrc_weights = trade_weights[root_key] subset = gnrc_weights.columns.intersection(desired_holdings.index) gnrc_des_hlds = desired_holdings.loc[subset] gnrc_weights = gnrc_weights.loc[:, subset] # drop indexes where all non zero weights were in columns dropped above gnrc_weights = gnrc_weights.loc[~(gnrc_weights == 0).all(axis=1)] instr_des_hlds = gnrc_des_hlds * gnrc_weights instr_des_hlds = instr_des_hlds.sum(axis=1) wprices = prices.loc[instr_des_hlds.index] desired_contracts.append(to_contracts(instr_des_hlds, wprices, multipliers, **kwargs)) desired_contracts = pd.concat(desired_contracts, axis=0) trades = desired_contracts.subtract(current_contracts, fill_value=0) trades = trades.loc[trades != 0] trades = trades.sort_index() return trades
0, module; 1, function_definition; 2, function_name:get_multiplier; 3, parameters; 4, block; 5, identifier:weights; 6, identifier:root_generic_multiplier; 7, expression_statement; 8, if_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, expression_statement; 13, expression_statement; 14, return_statement; 15, comment:""" Determine tradeable instrument multiplier based on generic asset multipliers and weights mapping from generics to tradeables. Parameters ---------- weights: pandas.DataFrame or dict A pandas.DataFrame of loadings of generic contracts on tradeable instruments **for a given date**. The columns are integers refering to generic number indexed from 0, e.g. [0, 1], and the index is strings representing instrument names. If dict is given keys should be generic instrument names, e.g. 'CL', and values should be pandas.DataFrames of loadings. The union of all indexes should be a superset of the instruments.index root_generic_multiplier: pandas.Series Series of multipliers for generic instruments lexigraphically sorted. If a dictionary of weights is given, root_generic_multiplier.index should correspond to the weights keys. Returns ------- A pandas.Series of multipliers for tradeable instruments. Examples -------- >>> import pandas as pd >>> import mapping.util as util >>> wts = pd.DataFrame([[0.5, 0], [0.5, 0.5], [0, 0.5]], ... index=["CLX16", "CLZ16", "CLF17"], ... columns=[0, 1]) >>> ast_mult = pd.Series([1000], index=["CL"]) >>> util.get_multiplier(wts, ast_mult) """; 16, boolean_operator; 17, block; 18, assignment; 19, assignment; 20, pattern_list; 21, call; 22, block; 23, assignment; 24, assignment; 25, identifier:imults; 26, comparison_operator:len(root_generic_multiplier) > 1; 27, not_operator; 28, raise_statement; 29, identifier:mults; 30, list; 31, identifier:intrs; 32, list; 33, identifier:ast; 34, identifier:multiplier; 35, attribute; 36, argument_list; 37, if_statement; 38, expression_statement; 39, expression_statement; 40, identifier:imults; 41, call; 42, identifier:imults; 43, call; 44, call; 45, integer:1; 46, call; 47, call; 48, identifier:root_generic_multiplier; 49, identifier:iteritems; 50, call; 51, block; 52, else_clause; 53, call; 54, call; 55, attribute; 56, argument_list; 57, attribute; 58, argument_list; 59, identifier:len; 60, argument_list; 61, identifier:isinstance; 62, argument_list; 63, identifier:ValueError; 64, argument_list; 65, identifier:isinstance; 66, argument_list; 67, expression_statement; 68, block; 69, attribute; 70, argument_list; 71, attribute; 72, argument_list; 73, identifier:pd; 74, identifier:Series; 75, identifier:mults; 76, identifier:intrs; 77, identifier:imults; 78, identifier:sort_index; 79, identifier:root_generic_multiplier; 80, identifier:weights; 81, identifier:dict; 82, concatenated_string; 83, identifier:weights; 84, identifier:dict; 85, assignment; 86, expression_statement; 87, identifier:mults; 88, identifier:extend; 89, call; 90, identifier:intrs; 91, identifier:extend; 92, identifier:weights_ast; 93, string:"For multiple generic instruments weights must be a "; 94, string:"dictionary"; 95, identifier:weights_ast; 96, attribute; 97, assignment; 98, attribute; 99, argument_list; 100, subscript; 101, identifier:index; 102, identifier:weights_ast; 103, attribute; 104, identifier:np; 105, identifier:repeat; 106, identifier:multiplier; 107, call; 108, identifier:weights; 109, identifier:ast; 110, identifier:weights; 111, identifier:index; 112, identifier:len; 113, argument_list; 114, identifier:weights_ast
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 7, 15; 8, 16; 8, 17; 9, 18; 10, 19; 11, 20; 11, 21; 11, 22; 12, 23; 13, 24; 14, 25; 16, 26; 16, 27; 17, 28; 18, 29; 18, 30; 19, 31; 19, 32; 20, 33; 20, 34; 21, 35; 21, 36; 22, 37; 22, 38; 22, 39; 23, 40; 23, 41; 24, 42; 24, 43; 26, 44; 26, 45; 27, 46; 28, 47; 35, 48; 35, 49; 37, 50; 37, 51; 37, 52; 38, 53; 39, 54; 41, 55; 41, 56; 43, 57; 43, 58; 44, 59; 44, 60; 46, 61; 46, 62; 47, 63; 47, 64; 50, 65; 50, 66; 51, 67; 52, 68; 53, 69; 53, 70; 54, 71; 54, 72; 55, 73; 55, 74; 56, 75; 56, 76; 57, 77; 57, 78; 60, 79; 62, 80; 62, 81; 64, 82; 66, 83; 66, 84; 67, 85; 68, 86; 69, 87; 69, 88; 70, 89; 71, 90; 71, 91; 72, 92; 82, 93; 82, 94; 85, 95; 85, 96; 86, 97; 89, 98; 89, 99; 96, 100; 96, 101; 97, 102; 97, 103; 98, 104; 98, 105; 99, 106; 99, 107; 100, 108; 100, 109; 103, 110; 103, 111; 107, 112; 107, 113; 113, 114
def get_multiplier(weights, root_generic_multiplier): """ Determine tradeable instrument multiplier based on generic asset multipliers and weights mapping from generics to tradeables. Parameters ---------- weights: pandas.DataFrame or dict A pandas.DataFrame of loadings of generic contracts on tradeable instruments **for a given date**. The columns are integers refering to generic number indexed from 0, e.g. [0, 1], and the index is strings representing instrument names. If dict is given keys should be generic instrument names, e.g. 'CL', and values should be pandas.DataFrames of loadings. The union of all indexes should be a superset of the instruments.index root_generic_multiplier: pandas.Series Series of multipliers for generic instruments lexigraphically sorted. If a dictionary of weights is given, root_generic_multiplier.index should correspond to the weights keys. Returns ------- A pandas.Series of multipliers for tradeable instruments. Examples -------- >>> import pandas as pd >>> import mapping.util as util >>> wts = pd.DataFrame([[0.5, 0], [0.5, 0.5], [0, 0.5]], ... index=["CLX16", "CLZ16", "CLF17"], ... columns=[0, 1]) >>> ast_mult = pd.Series([1000], index=["CL"]) >>> util.get_multiplier(wts, ast_mult) """ if len(root_generic_multiplier) > 1 and not isinstance(weights, dict): raise ValueError("For multiple generic instruments weights must be a " "dictionary") mults = [] intrs = [] for ast, multiplier in root_generic_multiplier.iteritems(): if isinstance(weights, dict): weights_ast = weights[ast].index else: weights_ast = weights.index mults.extend(np.repeat(multiplier, len(weights_ast))) intrs.extend(weights_ast) imults = pd.Series(mults, intrs) imults = imults.sort_index() return imults
0, module; 1, function_definition; 2, function_name:roller; 3, parameters; 4, block; 5, identifier:timestamps; 6, identifier:contract_dates; 7, identifier:get_weights; 8, dictionary_splat_pattern; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, comment:# for loop speedup only validate inputs the first function call to; 15, comment:# get_weights(); 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, expression_statement; 20, for_statement; 21, expression_statement; 22, return_statement; 23, identifier:kwargs; 24, comment:""" Calculate weight allocations to tradeable instruments for generic futures at a set of timestamps for a given root generic. Paramters --------- timestamps: iterable Sorted iterable of of pandas.Timestamps to calculate weights for contract_dates: pandas.Series Series with index of tradeable contract names and pandas.Timestamps representing the last date of the roll as values, sorted by values. Index must be unique and values must be strictly monotonic. get_weights: function A function which takes in a timestamp, contract_dates, validate_inputs and **kwargs. Returns a list of tuples consisting of the generic instrument name, the tradeable contract as a string, the weight on this contract as a float and the date as a pandas.Timestamp. kwargs: keyword arguments Arguements to pass to get_weights Return ------ A pandas.DataFrame with columns representing generics and a MultiIndex of date and contract. Values represent weights on tradeables for each generic. Examples -------- >>> import pandas as pd >>> import mapping.mappings as mappings >>> cols = pd.MultiIndex.from_product([["CL1", "CL2"], ['front', 'back']]) >>> idx = [-2, -1, 0] >>> trans = pd.DataFrame([[1.0, 0.0, 1.0, 0.0], [0.5, 0.5, 0.5, 0.5], ... [0.0, 1.0, 0.0, 1.0]], index=idx, columns=cols) >>> contract_dates = pd.Series([pd.Timestamp('2016-10-20'), ... pd.Timestamp('2016-11-21'), ... pd.Timestamp('2016-12-20')], ... index=['CLX16', 'CLZ16', 'CLF17']) >>> ts = pd.DatetimeIndex([pd.Timestamp('2016-10-18'), ... pd.Timestamp('2016-10-19'), ... pd.Timestamp('2016-10-19')]) >>> wts = mappings.roller(ts, contract_dates, mappings.static_transition, ... transition=trans) """; 25, assignment; 26, assignment; 27, call; 28, assignment; 29, assignment; 30, assignment; 31, call; 32, assignment; 33, identifier:ts; 34, subscript; 35, block; 36, assignment; 37, identifier:weights; 38, identifier:timestamps; 39, call; 40, identifier:contract_dates; 41, call; 42, identifier:_check_contract_dates; 43, argument_list; 44, identifier:weights; 45, list; 46, identifier:validate_inputs; 47, True; 48, identifier:ts; 49, subscript; 50, attribute; 51, argument_list; 52, identifier:validate_inputs; 53, False; 54, identifier:timestamps; 55, slice; 56, expression_statement; 57, identifier:weights; 58, call; 59, identifier:sorted; 60, argument_list; 61, attribute; 62, argument_list; 63, identifier:contract_dates; 64, identifier:timestamps; 65, integer:0; 66, identifier:weights; 67, identifier:extend; 68, call; 69, integer:1; 70, call; 71, identifier:aggregate_weights; 72, argument_list; 73, identifier:timestamps; 74, identifier:contract_dates; 75, identifier:sort_values; 76, identifier:get_weights; 77, argument_list; 78, attribute; 79, argument_list; 80, identifier:weights; 81, identifier:ts; 82, identifier:contract_dates; 83, keyword_argument; 84, dictionary_splat; 85, identifier:weights; 86, identifier:extend; 87, call; 88, identifier:validate_inputs; 89, identifier:validate_inputs; 90, identifier:kwargs; 91, identifier:get_weights; 92, argument_list; 93, identifier:ts; 94, identifier:contract_dates; 95, keyword_argument; 96, dictionary_splat; 97, identifier:validate_inputs; 98, identifier:validate_inputs; 99, identifier:kwargs
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 8, 23; 9, 24; 10, 25; 11, 26; 12, 27; 13, 28; 16, 29; 17, 30; 18, 31; 19, 32; 20, 33; 20, 34; 20, 35; 21, 36; 22, 37; 25, 38; 25, 39; 26, 40; 26, 41; 27, 42; 27, 43; 28, 44; 28, 45; 29, 46; 29, 47; 30, 48; 30, 49; 31, 50; 31, 51; 32, 52; 32, 53; 34, 54; 34, 55; 35, 56; 36, 57; 36, 58; 39, 59; 39, 60; 41, 61; 41, 62; 43, 63; 49, 64; 49, 65; 50, 66; 50, 67; 51, 68; 55, 69; 56, 70; 58, 71; 58, 72; 60, 73; 61, 74; 61, 75; 68, 76; 68, 77; 70, 78; 70, 79; 72, 80; 77, 81; 77, 82; 77, 83; 77, 84; 78, 85; 78, 86; 79, 87; 83, 88; 83, 89; 84, 90; 87, 91; 87, 92; 92, 93; 92, 94; 92, 95; 92, 96; 95, 97; 95, 98; 96, 99
def roller(timestamps, contract_dates, get_weights, **kwargs): """ Calculate weight allocations to tradeable instruments for generic futures at a set of timestamps for a given root generic. Paramters --------- timestamps: iterable Sorted iterable of of pandas.Timestamps to calculate weights for contract_dates: pandas.Series Series with index of tradeable contract names and pandas.Timestamps representing the last date of the roll as values, sorted by values. Index must be unique and values must be strictly monotonic. get_weights: function A function which takes in a timestamp, contract_dates, validate_inputs and **kwargs. Returns a list of tuples consisting of the generic instrument name, the tradeable contract as a string, the weight on this contract as a float and the date as a pandas.Timestamp. kwargs: keyword arguments Arguements to pass to get_weights Return ------ A pandas.DataFrame with columns representing generics and a MultiIndex of date and contract. Values represent weights on tradeables for each generic. Examples -------- >>> import pandas as pd >>> import mapping.mappings as mappings >>> cols = pd.MultiIndex.from_product([["CL1", "CL2"], ['front', 'back']]) >>> idx = [-2, -1, 0] >>> trans = pd.DataFrame([[1.0, 0.0, 1.0, 0.0], [0.5, 0.5, 0.5, 0.5], ... [0.0, 1.0, 0.0, 1.0]], index=idx, columns=cols) >>> contract_dates = pd.Series([pd.Timestamp('2016-10-20'), ... pd.Timestamp('2016-11-21'), ... pd.Timestamp('2016-12-20')], ... index=['CLX16', 'CLZ16', 'CLF17']) >>> ts = pd.DatetimeIndex([pd.Timestamp('2016-10-18'), ... pd.Timestamp('2016-10-19'), ... pd.Timestamp('2016-10-19')]) >>> wts = mappings.roller(ts, contract_dates, mappings.static_transition, ... transition=trans) """ timestamps = sorted(timestamps) contract_dates = contract_dates.sort_values() _check_contract_dates(contract_dates) weights = [] # for loop speedup only validate inputs the first function call to # get_weights() validate_inputs = True ts = timestamps[0] weights.extend(get_weights(ts, contract_dates, validate_inputs=validate_inputs, **kwargs)) validate_inputs = False for ts in timestamps[1:]: weights.extend(get_weights(ts, contract_dates, validate_inputs=validate_inputs, **kwargs)) weights = aggregate_weights(weights) return weights
0, module; 1, function_definition; 2, function_name:strictly_monotonic; 3, parameters; 4, block; 5, identifier:bb; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, comment:# subsequent equal entries have 0 in db; 11, expression_statement; 12, comment:# the first element is always unique (the second occurence is the dublicate); 13, expression_statement; 14, return_statement; 15, string; 16, assignment; 17, call; 18, assignment; 19, assignment; 20, assignment; 21, call; 22, string_content:bb is an index array which may have numerous double or triple occurrences of indices, such as for example the decay_index_pointer. This method removes all entries <= -, then all dublicates and finally returns a sorted list of indices.; 23, identifier:cc; 24, subscript; 25, attribute; 26, argument_list; 27, identifier:dc; 28, binary_operator:cc[1:]-cc[:-1]; 29, identifier:dc; 30, call; 31, identifier:dc_mask; 32, call; 33, attribute; 34, argument_list; 35, identifier:bb; 36, call; 37, identifier:cc; 38, identifier:sort; 39, subscript; 40, subscript; 41, attribute; 42, argument_list; 43, attribute; 44, argument_list; 45, call; 46, identifier:compressed; 47, attribute; 48, argument_list; 49, identifier:cc; 50, slice; 51, identifier:cc; 52, slice; 53, identifier:np; 54, identifier:insert; 55, identifier:dc; 56, integer:0; 57, integer:1; 58, attribute; 59, identifier:masked_equal; 60, identifier:dc; 61, integer:0; 62, attribute; 63, argument_list; 64, identifier:np; 65, identifier:where; 66, comparison_operator:bb>=0; 67, integer:1; 68, unary_operator; 69, identifier:np; 70, identifier:ma; 71, attribute; 72, identifier:array; 73, identifier:cc; 74, keyword_argument; 75, identifier:bb; 76, integer:0; 77, integer:1; 78, identifier:np; 79, identifier:ma; 80, identifier:mask; 81, attribute; 82, identifier:dc_mask; 83, identifier:mask
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 6, 15; 7, 16; 8, 17; 9, 18; 11, 19; 13, 20; 14, 21; 15, 22; 16, 23; 16, 24; 17, 25; 17, 26; 18, 27; 18, 28; 19, 29; 19, 30; 20, 31; 20, 32; 21, 33; 21, 34; 24, 35; 24, 36; 25, 37; 25, 38; 28, 39; 28, 40; 30, 41; 30, 42; 32, 43; 32, 44; 33, 45; 33, 46; 36, 47; 36, 48; 39, 49; 39, 50; 40, 51; 40, 52; 41, 53; 41, 54; 42, 55; 42, 56; 42, 57; 43, 58; 43, 59; 44, 60; 44, 61; 45, 62; 45, 63; 47, 64; 47, 65; 48, 66; 50, 67; 52, 68; 58, 69; 58, 70; 62, 71; 62, 72; 63, 73; 63, 74; 66, 75; 66, 76; 68, 77; 71, 78; 71, 79; 74, 80; 74, 81; 81, 82; 81, 83
def strictly_monotonic(bb): ''' bb is an index array which may have numerous double or triple occurrences of indices, such as for example the decay_index_pointer. This method removes all entries <= -, then all dublicates and finally returns a sorted list of indices. ''' cc=bb[np.where(bb>=0)] cc.sort() dc=cc[1:]-cc[:-1] # subsequent equal entries have 0 in db dc=np.insert(dc,0,1) # the first element is always unique (the second occurence is the dublicate) dc_mask=np.ma.masked_equal(dc,0) return np.ma.array(cc,mask=dc_mask.mask).compressed()
0, module; 1, function_definition; 2, function_name:_sort_shared_logical_disks; 3, parameters; 4, block; 5, identifier:logical_disks; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, comment:# Separate logical disks based on share_physical_disks value.; 10, comment:# 'logical_disks_shared' when share_physical_disks is True and; 11, comment:# 'logical_disks_nonshared' when share_physical_disks is False; 12, expression_statement; 13, expression_statement; 14, for_statement; 15, comment:# Separete logical disks with raid 1 from the 'logical_disks_shared' into; 16, comment:# 'logical_disks_shared_raid1' and remaining as; 17, comment:# 'logical_disks_shared_excl_raid1'.; 18, expression_statement; 19, expression_statement; 20, for_statement; 21, comment:# Sort the 'logical_disks_shared' in reverse order based on; 22, comment:# 'number_of_physical_disks' attribute, if provided, otherwise minimum; 23, comment:# disks required to create the logical volume.; 24, expression_statement; 25, comment:# Move RAID 1+0 to first in 'logical_disks_shared' when number of physical; 26, comment:# disks needed to create logical volume cannot be shared with odd number of; 27, comment:# disks and disks higher than that of RAID 1+0.; 28, expression_statement; 29, for_statement; 30, comment:# Final 'logical_disks_sorted' list should have non shared logical disks; 31, comment:# first, followed by shared logical disks with RAID 1, and finally by the; 32, comment:# shared logical disks sorted based on number of disks and RAID 1+0; 33, comment:# condition.; 34, expression_statement; 35, return_statement; 36, comment:"""Sort the logical disks based on the following conditions. When the share_physical_disks is True make sure we create the volume which needs more disks first. This avoids the situation of insufficient disks for some logical volume request. For example, - two logical disk with number of disks - LD1(3), LD2(4) - have 4 physical disks In this case, if we consider LD1 first then LD2 will fail since not enough disks available to create LD2. So follow a order for allocation when share_physical_disks is True. Also RAID1 can share only when there is logical volume with only 2 disks. So make sure we create RAID 1 first when share_physical_disks is True. And RAID 1+0 can share only when the logical volume with even number of disks. :param logical_disks: 'logical_disks' to be sorted for shared logical disks. :returns: the logical disks sorted based the above conditions. """; 37, assignment; 38, assignment; 39, assignment; 40, assignment; 41, identifier:x; 42, identifier:logical_disks; 43, block; 44, assignment; 45, assignment; 46, identifier:x; 47, identifier:logical_disks_shared; 48, block; 49, assignment; 50, assignment; 51, identifier:x; 52, identifier:logical_disks_shared; 53, block; 54, assignment; 55, identifier:logical_disks_sorted; 56, identifier:is_shared; 57, parenthesized_expression; 58, identifier:num_of_disks; 59, parenthesized_expression; 60, identifier:logical_disks_shared; 61, list; 62, identifier:logical_disks_nonshared; 63, list; 64, expression_statement; 65, expression_statement; 66, identifier:logical_disks_shared_raid1; 67, list; 68, identifier:logical_disks_shared_excl_raid1; 69, list; 70, expression_statement; 71, expression_statement; 72, identifier:logical_disks_shared; 73, call; 74, identifier:check; 75, True; 76, if_statement; 77, if_statement; 78, identifier:logical_disks_sorted; 79, parenthesized_expression; 80, lambda; 81, lambda; 82, assignment; 83, call; 84, assignment; 85, call; 86, identifier:sorted; 87, argument_list; 88, comparison_operator:x['raid_level'] == "1+0"; 89, block; 90, not_operator; 91, block; 92, binary_operator:logical_disks_nonshared + logical_disks_shared_raid1 + logical_disks_shared; 93, lambda_parameters; 94, conditional_expression:True if ('share_physical_disks' in x and x['share_physical_disks']) else False; 95, lambda_parameters; 96, conditional_expression:x['number_of_physical_disks'] if 'number_of_physical_disks' in x else constants.RAID_LEVEL_MIN_DISKS[x['raid_level']]; 97, identifier:target; 98, parenthesized_expression; 99, attribute; 100, argument_list; 101, identifier:target; 102, parenthesized_expression; 103, attribute; 104, argument_list; 105, identifier:logical_disks_shared_excl_raid1; 106, keyword_argument; 107, keyword_argument; 108, subscript; 109, string:"1+0"; 110, expression_statement; 111, for_statement; 112, identifier:check; 113, expression_statement; 114, expression_statement; 115, expression_statement; 116, binary_operator:logical_disks_nonshared + logical_disks_shared_raid1; 117, identifier:logical_disks_shared; 118, identifier:x; 119, True; 120, parenthesized_expression; 121, False; 122, identifier:x; 123, subscript; 124, comparison_operator:'number_of_physical_disks' in x; 125, subscript; 126, conditional_expression:logical_disks_shared if is_shared(x) else logical_disks_nonshared; 127, identifier:target; 128, identifier:append; 129, identifier:x; 130, conditional_expression:logical_disks_shared_raid1 if x['raid_level'] == '1' else logical_disks_shared_excl_raid1; 131, identifier:target; 132, identifier:append; 133, identifier:x; 134, identifier:reverse; 135, True; 136, identifier:key; 137, identifier:num_of_disks; 138, identifier:x; 139, string; 140, assignment; 141, identifier:y; 142, identifier:logical_disks_shared; 143, block; 144, call; 145, call; 146, assignment; 147, identifier:logical_disks_nonshared; 148, identifier:logical_disks_shared_raid1; 149, boolean_operator; 150, identifier:x; 151, string; 152, string; 153, identifier:x; 154, attribute; 155, subscript; 156, identifier:logical_disks_shared; 157, call; 158, identifier:logical_disks_nonshared; 159, identifier:logical_disks_shared_raid1; 160, comparison_operator:x['raid_level'] == '1'; 161, identifier:logical_disks_shared_excl_raid1; 162, string_content:raid_level; 163, identifier:x_num; 164, call; 165, if_statement; 166, attribute; 167, argument_list; 168, attribute; 169, argument_list; 170, identifier:check; 171, True; 172, comparison_operator:'share_physical_disks' in x; 173, subscript; 174, string_content:number_of_physical_disks; 175, string_content:number_of_physical_disks; 176, identifier:constants; 177, identifier:RAID_LEVEL_MIN_DISKS; 178, identifier:x; 179, string; 180, identifier:is_shared; 181, argument_list; 182, subscript; 183, string; 184, identifier:num_of_disks; 185, argument_list; 186, comparison_operator:y['raid_level'] != "1+0"; 187, block; 188, identifier:logical_disks_shared; 189, identifier:remove; 190, identifier:x; 191, identifier:logical_disks_shared; 192, identifier:insert; 193, integer:0; 194, identifier:x; 195, string; 196, identifier:x; 197, identifier:x; 198, string; 199, string_content:raid_level; 200, identifier:x; 201, identifier:x; 202, string; 203, string_content:1; 204, identifier:x; 205, subscript; 206, string:"1+0"; 207, expression_statement; 208, if_statement; 209, string_content:share_physical_disks; 210, string_content:share_physical_disks; 211, string_content:raid_level; 212, identifier:y; 213, string; 214, assignment; 215, comparison_operator:x_num < y_num; 216, block; 217, string_content:raid_level; 218, identifier:y_num; 219, call; 220, identifier:x_num; 221, identifier:y_num; 222, expression_statement; 223, if_statement; 224, identifier:num_of_disks; 225, argument_list; 226, assignment; 227, identifier:check; 228, block; 229, identifier:y; 230, identifier:check; 231, parenthesized_expression; 232, break_statement; 233, conditional_expression:True if y_num % 2 == 0 else False; 234, True; 235, comparison_operator:y_num % 2 == 0; 236, False; 237, binary_operator:y_num % 2; 238, integer:0; 239, identifier:y_num; 240, integer:2
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 4, 25; 4, 26; 4, 27; 4, 28; 4, 29; 4, 30; 4, 31; 4, 32; 4, 33; 4, 34; 4, 35; 6, 36; 7, 37; 8, 38; 12, 39; 13, 40; 14, 41; 14, 42; 14, 43; 18, 44; 19, 45; 20, 46; 20, 47; 20, 48; 24, 49; 28, 50; 29, 51; 29, 52; 29, 53; 34, 54; 35, 55; 37, 56; 37, 57; 38, 58; 38, 59; 39, 60; 39, 61; 40, 62; 40, 63; 43, 64; 43, 65; 44, 66; 44, 67; 45, 68; 45, 69; 48, 70; 48, 71; 49, 72; 49, 73; 50, 74; 50, 75; 53, 76; 53, 77; 54, 78; 54, 79; 57, 80; 59, 81; 64, 82; 65, 83; 70, 84; 71, 85; 73, 86; 73, 87; 76, 88; 76, 89; 77, 90; 77, 91; 79, 92; 80, 93; 80, 94; 81, 95; 81, 96; 82, 97; 82, 98; 83, 99; 83, 100; 84, 101; 84, 102; 85, 103; 85, 104; 87, 105; 87, 106; 87, 107; 88, 108; 88, 109; 89, 110; 89, 111; 90, 112; 91, 113; 91, 114; 91, 115; 92, 116; 92, 117; 93, 118; 94, 119; 94, 120; 94, 121; 95, 122; 96, 123; 96, 124; 96, 125; 98, 126; 99, 127; 99, 128; 100, 129; 102, 130; 103, 131; 103, 132; 104, 133; 106, 134; 106, 135; 107, 136; 107, 137; 108, 138; 108, 139; 110, 140; 111, 141; 111, 142; 111, 143; 113, 144; 114, 145; 115, 146; 116, 147; 116, 148; 120, 149; 123, 150; 123, 151; 124, 152; 124, 153; 125, 154; 125, 155; 126, 156; 126, 157; 126, 158; 130, 159; 130, 160; 130, 161; 139, 162; 140, 163; 140, 164; 143, 165; 144, 166; 144, 167; 145, 168; 145, 169; 146, 170; 146, 171; 149, 172; 149, 173; 151, 174; 152, 175; 154, 176; 154, 177; 155, 178; 155, 179; 157, 180; 157, 181; 160, 182; 160, 183; 164, 184; 164, 185; 165, 186; 165, 187; 166, 188; 166, 189; 167, 190; 168, 191; 168, 192; 169, 193; 169, 194; 172, 195; 172, 196; 173, 197; 173, 198; 179, 199; 181, 200; 182, 201; 182, 202; 183, 203; 185, 204; 186, 205; 186, 206; 187, 207; 187, 208; 195, 209; 198, 210; 202, 211; 205, 212; 205, 213; 207, 214; 208, 215; 208, 216; 213, 217; 214, 218; 214, 219; 215, 220; 215, 221; 216, 222; 216, 223; 219, 224; 219, 225; 222, 226; 223, 227; 223, 228; 225, 229; 226, 230; 226, 231; 228, 232; 231, 233; 233, 234; 233, 235; 233, 236; 235, 237; 235, 238; 237, 239; 237, 240
def _sort_shared_logical_disks(logical_disks): """Sort the logical disks based on the following conditions. When the share_physical_disks is True make sure we create the volume which needs more disks first. This avoids the situation of insufficient disks for some logical volume request. For example, - two logical disk with number of disks - LD1(3), LD2(4) - have 4 physical disks In this case, if we consider LD1 first then LD2 will fail since not enough disks available to create LD2. So follow a order for allocation when share_physical_disks is True. Also RAID1 can share only when there is logical volume with only 2 disks. So make sure we create RAID 1 first when share_physical_disks is True. And RAID 1+0 can share only when the logical volume with even number of disks. :param logical_disks: 'logical_disks' to be sorted for shared logical disks. :returns: the logical disks sorted based the above conditions. """ is_shared = (lambda x: True if ('share_physical_disks' in x and x['share_physical_disks']) else False) num_of_disks = (lambda x: x['number_of_physical_disks'] if 'number_of_physical_disks' in x else constants.RAID_LEVEL_MIN_DISKS[x['raid_level']]) # Separate logical disks based on share_physical_disks value. # 'logical_disks_shared' when share_physical_disks is True and # 'logical_disks_nonshared' when share_physical_disks is False logical_disks_shared = [] logical_disks_nonshared = [] for x in logical_disks: target = (logical_disks_shared if is_shared(x) else logical_disks_nonshared) target.append(x) # Separete logical disks with raid 1 from the 'logical_disks_shared' into # 'logical_disks_shared_raid1' and remaining as # 'logical_disks_shared_excl_raid1'. logical_disks_shared_raid1 = [] logical_disks_shared_excl_raid1 = [] for x in logical_disks_shared: target = (logical_disks_shared_raid1 if x['raid_level'] == '1' else logical_disks_shared_excl_raid1) target.append(x) # Sort the 'logical_disks_shared' in reverse order based on # 'number_of_physical_disks' attribute, if provided, otherwise minimum # disks required to create the logical volume. logical_disks_shared = sorted(logical_disks_shared_excl_raid1, reverse=True, key=num_of_disks) # Move RAID 1+0 to first in 'logical_disks_shared' when number of physical # disks needed to create logical volume cannot be shared with odd number of # disks and disks higher than that of RAID 1+0. check = True for x in logical_disks_shared: if x['raid_level'] == "1+0": x_num = num_of_disks(x) for y in logical_disks_shared: if y['raid_level'] != "1+0": y_num = num_of_disks(y) if x_num < y_num: check = (True if y_num % 2 == 0 else False) if check: break if not check: logical_disks_shared.remove(x) logical_disks_shared.insert(0, x) check = True # Final 'logical_disks_sorted' list should have non shared logical disks # first, followed by shared logical disks with RAID 1, and finally by the # shared logical disks sorted based on number of disks and RAID 1+0 # condition. logical_disks_sorted = (logical_disks_nonshared + logical_disks_shared_raid1 + logical_disks_shared) return logical_disks_sorted
0, module; 1, function_definition; 2, function_name:get_versions; 3, parameters; 4, block; 5, identifier:cls; 6, identifier:bucket; 7, identifier:key; 8, default_parameter; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, return_statement; 13, identifier:desc; 14, True; 15, comment:"""Fetch all versions of a specific object. :param bucket: The bucket (instance or id) to get the object from. :param key: Key of object. :param desc: Sort results desc if True, asc otherwise. :returns: The query to execute to fetch all versions. """; 16, assignment; 17, assignment; 18, call; 19, identifier:filters; 20, list; 21, identifier:order; 22, conditional_expression:cls.created.desc() if desc else cls.created.asc(); 23, attribute; 24, argument_list; 25, comparison_operator:cls.bucket_id == as_bucket_id(bucket); 26, comparison_operator:cls.key == key; 27, call; 28, identifier:desc; 29, call; 30, call; 31, identifier:order_by; 32, attribute; 33, identifier:order; 34, attribute; 35, call; 36, attribute; 37, identifier:key; 38, attribute; 39, argument_list; 40, attribute; 41, argument_list; 42, attribute; 43, argument_list; 44, identifier:cls; 45, identifier:key; 46, identifier:cls; 47, identifier:bucket_id; 48, identifier:as_bucket_id; 49, argument_list; 50, identifier:cls; 51, identifier:key; 52, attribute; 53, identifier:desc; 54, attribute; 55, identifier:asc; 56, attribute; 57, identifier:filter; 58, list_splat; 59, identifier:bucket; 60, identifier:cls; 61, identifier:created; 62, identifier:cls; 63, identifier:created; 64, identifier:cls; 65, identifier:query; 66, identifier:filters
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 8, 13; 8, 14; 9, 15; 10, 16; 11, 17; 12, 18; 16, 19; 16, 20; 17, 21; 17, 22; 18, 23; 18, 24; 20, 25; 20, 26; 22, 27; 22, 28; 22, 29; 23, 30; 23, 31; 24, 32; 24, 33; 25, 34; 25, 35; 26, 36; 26, 37; 27, 38; 27, 39; 29, 40; 29, 41; 30, 42; 30, 43; 32, 44; 32, 45; 34, 46; 34, 47; 35, 48; 35, 49; 36, 50; 36, 51; 38, 52; 38, 53; 40, 54; 40, 55; 42, 56; 42, 57; 43, 58; 49, 59; 52, 60; 52, 61; 54, 62; 54, 63; 56, 64; 56, 65; 58, 66
def get_versions(cls, bucket, key, desc=True): """Fetch all versions of a specific object. :param bucket: The bucket (instance or id) to get the object from. :param key: Key of object. :param desc: Sort results desc if True, asc otherwise. :returns: The query to execute to fetch all versions. """ filters = [ cls.bucket_id == as_bucket_id(bucket), cls.key == key, ] order = cls.created.desc() if desc else cls.created.asc() return cls.query.filter(*filters).order_by(cls.key, order)
0, module; 1, function_definition; 2, function_name:sort_by; 3, parameters; 4, block; 5, identifier:self; 6, list_splat_pattern; 7, expression_statement; 8, comment:# Support sorting by file_ids or keys.; 9, expression_statement; 10, comment:# self.record['_files'] = [{'key': files.get(id_, id_)} for id_ in ids]; 11, expression_statement; 12, expression_statement; 13, identifier:ids; 14, comment:"""Update files order. :param ids: List of ids specifying the final status of the list. """; 15, assignment; 16, assignment; 17, call; 18, identifier:files; 19, dictionary_comprehension; 20, attribute; 21, call; 22, attribute; 23, argument_list; 24, pair; 25, for_in_clause; 26, identifier:self; 27, identifier:filesmap; 28, identifier:OrderedDict; 29, argument_list; 30, identifier:self; 31, identifier:flush; 32, call; 33, attribute; 34, identifier:f_; 35, identifier:self; 36, list_comprehension; 37, identifier:str; 38, argument_list; 39, identifier:f_; 40, identifier:key; 41, tuple; 42, for_in_clause; 43, attribute; 44, call; 45, call; 46, identifier:id_; 47, identifier:ids; 48, identifier:f_; 49, identifier:file_id; 50, attribute; 51, argument_list; 52, attribute; 53, argument_list; 54, identifier:files; 55, identifier:get; 56, identifier:id_; 57, identifier:id_; 58, subscript; 59, identifier:dumps; 60, identifier:self; 61, call; 62, attribute; 63, argument_list; 64, identifier:files; 65, identifier:get; 66, identifier:id_; 67, identifier:id_
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 6, 13; 7, 14; 9, 15; 11, 16; 12, 17; 15, 18; 15, 19; 16, 20; 16, 21; 17, 22; 17, 23; 19, 24; 19, 25; 20, 26; 20, 27; 21, 28; 21, 29; 22, 30; 22, 31; 24, 32; 24, 33; 25, 34; 25, 35; 29, 36; 32, 37; 32, 38; 33, 39; 33, 40; 36, 41; 36, 42; 38, 43; 41, 44; 41, 45; 42, 46; 42, 47; 43, 48; 43, 49; 44, 50; 44, 51; 45, 52; 45, 53; 50, 54; 50, 55; 51, 56; 51, 57; 52, 58; 52, 59; 58, 60; 58, 61; 61, 62; 61, 63; 62, 64; 62, 65; 63, 66; 63, 67
def sort_by(self, *ids): """Update files order. :param ids: List of ids specifying the final status of the list. """ # Support sorting by file_ids or keys. files = {str(f_.file_id): f_.key for f_ in self} # self.record['_files'] = [{'key': files.get(id_, id_)} for id_ in ids] self.filesmap = OrderedDict([ (files.get(id_, id_), self[files.get(id_, id_)].dumps()) for id_ in ids ]) self.flush()
0, module; 1, function_definition; 2, function_name:sorted_files_from_bucket; 3, parameters; 4, block; 5, identifier:bucket; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, return_statement; 13, identifier:keys; 14, None; 15, comment:"""Return files from bucket sorted by given keys. :param bucket: :class:`~invenio_files_rest.models.Bucket` containing the files. :param keys: Keys order to be used. :returns: Sorted list of bucket items. """; 16, assignment; 17, assignment; 18, assignment; 19, assignment; 20, call; 21, identifier:keys; 22, boolean_operator; 23, identifier:total; 24, call; 25, identifier:sortby; 26, call; 27, identifier:values; 28, call; 29, identifier:sorted; 30, argument_list; 31, identifier:keys; 32, list; 33, identifier:len; 34, argument_list; 35, identifier:dict; 36, argument_list; 37, attribute; 38, argument_list; 39, identifier:values; 40, keyword_argument; 41, identifier:keys; 42, call; 43, call; 44, identifier:all; 45, identifier:key; 46, lambda; 47, identifier:zip; 48, argument_list; 49, attribute; 50, argument_list; 51, lambda_parameters; 52, call; 53, identifier:keys; 54, call; 55, identifier:ObjectVersion; 56, identifier:get_by_bucket; 57, identifier:bucket; 58, identifier:x; 59, attribute; 60, argument_list; 61, identifier:range; 62, argument_list; 63, identifier:sortby; 64, identifier:get; 65, attribute; 66, identifier:total; 67, identifier:total; 68, identifier:x; 69, identifier:key
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 6, 13; 6, 14; 7, 15; 8, 16; 9, 17; 10, 18; 11, 19; 12, 20; 16, 21; 16, 22; 17, 23; 17, 24; 18, 25; 18, 26; 19, 27; 19, 28; 20, 29; 20, 30; 22, 31; 22, 32; 24, 33; 24, 34; 26, 35; 26, 36; 28, 37; 28, 38; 30, 39; 30, 40; 34, 41; 36, 42; 37, 43; 37, 44; 40, 45; 40, 46; 42, 47; 42, 48; 43, 49; 43, 50; 46, 51; 46, 52; 48, 53; 48, 54; 49, 55; 49, 56; 50, 57; 51, 58; 52, 59; 52, 60; 54, 61; 54, 62; 59, 63; 59, 64; 60, 65; 60, 66; 62, 67; 65, 68; 65, 69
def sorted_files_from_bucket(bucket, keys=None): """Return files from bucket sorted by given keys. :param bucket: :class:`~invenio_files_rest.models.Bucket` containing the files. :param keys: Keys order to be used. :returns: Sorted list of bucket items. """ keys = keys or [] total = len(keys) sortby = dict(zip(keys, range(total))) values = ObjectVersion.get_by_bucket(bucket).all() return sorted(values, key=lambda x: sortby.get(x.key, total))
0, module; 1, function_definition; 2, function_name:sort_data; 3, parameters; 4, block; 5, identifier:x; 6, identifier:y; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, return_statement; 11, comment:"""Sort the data."""; 12, assignment; 13, assignment; 14, expression_list; 15, identifier:xy; 16, call; 17, pattern_list; 18, call; 19, identifier:x; 20, identifier:y; 21, identifier:sorted; 22, argument_list; 23, identifier:x; 24, identifier:y; 25, identifier:zip; 26, argument_list; 27, call; 28, list_splat; 29, identifier:zip; 30, argument_list; 31, identifier:xy; 32, identifier:x; 33, identifier:y
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 7, 11; 8, 12; 9, 13; 10, 14; 12, 15; 12, 16; 13, 17; 13, 18; 14, 19; 14, 20; 16, 21; 16, 22; 17, 23; 17, 24; 18, 25; 18, 26; 22, 27; 26, 28; 27, 29; 27, 30; 28, 31; 30, 32; 30, 33
def sort_data(x, y): """Sort the data.""" xy = sorted(zip(x, y)) x, y = zip(*xy) return x, y
0, module; 1, function_definition; 2, function_name:unsort_vector; 3, parameters; 4, block; 5, identifier:data; 6, identifier:indices_of_increasing; 7, expression_statement; 8, return_statement; 9, comment:"""Upermutate 1-D data that is sorted by indices_of_increasing."""; 10, call; 11, attribute; 12, argument_list; 13, identifier:numpy; 14, identifier:array; 15, list_comprehension; 16, subscript; 17, for_in_clause; 18, identifier:data; 19, call; 20, identifier:i; 21, call; 22, attribute; 23, argument_list; 24, identifier:range; 25, argument_list; 26, identifier:indices_of_increasing; 27, identifier:index; 28, identifier:i; 29, call; 30, identifier:len; 31, argument_list; 32, identifier:data
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 7, 9; 8, 10; 10, 11; 10, 12; 11, 13; 11, 14; 12, 15; 15, 16; 15, 17; 16, 18; 16, 19; 17, 20; 17, 21; 19, 22; 19, 23; 21, 24; 21, 25; 22, 26; 22, 27; 23, 28; 25, 29; 29, 30; 29, 31; 31, 32
def unsort_vector(data, indices_of_increasing): """Upermutate 1-D data that is sorted by indices_of_increasing.""" return numpy.array([data[indices_of_increasing.index(i)] for i in range(len(data))])
0, module; 1, function_definition; 2, function_name:_compute_sorted_indices; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, comment:# save in meaningful variable names; 10, expression_statement; 11, comment:# list (like self.y); 12, expression_statement; 13, comment:""" The smoothers need sorted data. This sorts it from the perspective of each column. if self._x[0][3] is the 9th-smallest value in self._x[0], then _xi_sorted[3] = 8 We only have to sort the data once. """; 14, assignment; 15, identifier:to_sort; 16, binary_operator:[self.y] + self.x; 17, block; 18, assignment; 19, assignment; 20, identifier:sorted_indices; 21, list; 22, list; 23, attribute; 24, expression_statement; 25, expression_statement; 26, expression_statement; 27, attribute; 28, subscript; 29, attribute; 30, subscript; 31, attribute; 32, identifier:self; 33, identifier:x; 34, assignment; 35, call; 36, call; 37, identifier:self; 38, identifier:_yi_sorted; 39, identifier:sorted_indices; 40, integer:0; 41, identifier:self; 42, identifier:_xi_sorted; 43, identifier:sorted_indices; 44, slice; 45, identifier:self; 46, identifier:y; 47, identifier:data_w_indices; 48, list_comprehension; 49, attribute; 50, argument_list; 51, attribute; 52, argument_list; 53, integer:1; 54, tuple; 55, for_in_clause; 56, identifier:data_w_indices; 57, identifier:sort; 58, identifier:sorted_indices; 59, identifier:append; 60, list_comprehension; 61, identifier:val; 62, identifier:i; 63, tuple_pattern; 64, call; 65, identifier:i; 66, for_in_clause; 67, identifier:i; 68, identifier:val; 69, identifier:enumerate; 70, argument_list; 71, pattern_list; 72, identifier:data_w_indices; 73, identifier:to_sort; 74, identifier:val; 75, identifier:i
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 6, 13; 7, 14; 8, 15; 8, 16; 8, 17; 10, 18; 12, 19; 14, 20; 14, 21; 16, 22; 16, 23; 17, 24; 17, 25; 17, 26; 18, 27; 18, 28; 19, 29; 19, 30; 22, 31; 23, 32; 23, 33; 24, 34; 25, 35; 26, 36; 27, 37; 27, 38; 28, 39; 28, 40; 29, 41; 29, 42; 30, 43; 30, 44; 31, 45; 31, 46; 34, 47; 34, 48; 35, 49; 35, 50; 36, 51; 36, 52; 44, 53; 48, 54; 48, 55; 49, 56; 49, 57; 51, 58; 51, 59; 52, 60; 54, 61; 54, 62; 55, 63; 55, 64; 60, 65; 60, 66; 63, 67; 63, 68; 64, 69; 64, 70; 66, 71; 66, 72; 70, 73; 71, 74; 71, 75
def _compute_sorted_indices(self): """ The smoothers need sorted data. This sorts it from the perspective of each column. if self._x[0][3] is the 9th-smallest value in self._x[0], then _xi_sorted[3] = 8 We only have to sort the data once. """ sorted_indices = [] for to_sort in [self.y] + self.x: data_w_indices = [(val, i) for (i, val) in enumerate(to_sort)] data_w_indices.sort() sorted_indices.append([i for val, i in data_w_indices]) # save in meaningful variable names self._yi_sorted = sorted_indices[0] # list (like self.y) self._xi_sorted = sorted_indices[1:]
0, module; 1, function_definition; 2, function_name:specify_data_set; 3, parameters; 4, block; 5, identifier:self; 6, identifier:x_input; 7, identifier:y_input; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, expression_statement; 13, identifier:sort_data; 14, False; 15, comment:""" Fully define data by lists of x values and y values. This will sort them by increasing x but remember how to unsort them for providing results. Parameters ---------- x_input : iterable list of floats that represent x y_input : iterable list of floats that represent y(x) for each x sort_data : bool, optional If true, the data will be sorted by increasing x values. """; 16, identifier:sort_data; 17, block; 18, else_clause; 19, assignment; 20, assignment; 21, expression_statement; 22, expression_statement; 23, expression_statement; 24, expression_statement; 25, if_statement; 26, block; 27, attribute; 28, identifier:x; 29, attribute; 30, identifier:y; 31, assignment; 32, assignment; 33, assignment; 34, assignment; 35, comparison_operator:len(set(self._original_index_of_xvalue)) != len(x); 36, block; 37, expression_statement; 38, identifier:self; 39, identifier:x; 40, identifier:self; 41, identifier:y; 42, identifier:xy; 43, call; 44, pattern_list; 45, call; 46, identifier:x_input_list; 47, call; 48, attribute; 49, list_comprehension; 50, call; 51, call; 52, raise_statement; 53, assignment; 54, identifier:sorted; 55, argument_list; 56, identifier:x; 57, identifier:y; 58, identifier:zip; 59, argument_list; 60, identifier:list; 61, argument_list; 62, identifier:self; 63, identifier:_original_index_of_xvalue; 64, call; 65, for_in_clause; 66, identifier:len; 67, argument_list; 68, identifier:len; 69, argument_list; 70, call; 71, pattern_list; 72, expression_list; 73, call; 74, list_splat; 75, identifier:x_input; 76, attribute; 77, argument_list; 78, identifier:xi; 79, identifier:x; 80, call; 81, identifier:x; 82, identifier:RuntimeError; 83, argument_list; 84, identifier:x; 85, identifier:y; 86, identifier:x_input; 87, identifier:y_input; 88, identifier:zip; 89, argument_list; 90, identifier:xy; 91, identifier:x_input_list; 92, identifier:index; 93, identifier:xi; 94, identifier:set; 95, argument_list; 96, string; 97, identifier:x_input; 98, identifier:y_input; 99, attribute; 100, string_content:There are some non-unique x-values; 101, identifier:self; 102, identifier:_original_index_of_xvalue
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 8, 13; 8, 14; 9, 15; 10, 16; 10, 17; 10, 18; 11, 19; 12, 20; 17, 21; 17, 22; 17, 23; 17, 24; 17, 25; 18, 26; 19, 27; 19, 28; 20, 29; 20, 30; 21, 31; 22, 32; 23, 33; 24, 34; 25, 35; 25, 36; 26, 37; 27, 38; 27, 39; 29, 40; 29, 41; 31, 42; 31, 43; 32, 44; 32, 45; 33, 46; 33, 47; 34, 48; 34, 49; 35, 50; 35, 51; 36, 52; 37, 53; 43, 54; 43, 55; 44, 56; 44, 57; 45, 58; 45, 59; 47, 60; 47, 61; 48, 62; 48, 63; 49, 64; 49, 65; 50, 66; 50, 67; 51, 68; 51, 69; 52, 70; 53, 71; 53, 72; 55, 73; 59, 74; 61, 75; 64, 76; 64, 77; 65, 78; 65, 79; 67, 80; 69, 81; 70, 82; 70, 83; 71, 84; 71, 85; 72, 86; 72, 87; 73, 88; 73, 89; 74, 90; 76, 91; 76, 92; 77, 93; 80, 94; 80, 95; 83, 96; 89, 97; 89, 98; 95, 99; 96, 100; 99, 101; 99, 102
def specify_data_set(self, x_input, y_input, sort_data=False): """ Fully define data by lists of x values and y values. This will sort them by increasing x but remember how to unsort them for providing results. Parameters ---------- x_input : iterable list of floats that represent x y_input : iterable list of floats that represent y(x) for each x sort_data : bool, optional If true, the data will be sorted by increasing x values. """ if sort_data: xy = sorted(zip(x_input, y_input)) x, y = zip(*xy) x_input_list = list(x_input) self._original_index_of_xvalue = [x_input_list.index(xi) for xi in x] if len(set(self._original_index_of_xvalue)) != len(x): raise RuntimeError('There are some non-unique x-values') else: x, y = x_input, y_input self.x = x self.y = y
0, module; 1, function_definition; 2, function_name:_aggregate; 3, parameters; 4, block; 5, identifier:data; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, if_statement; 13, expression_statement; 14, if_statement; 15, return_statement; 16, identifier:norm; 17, True; 18, identifier:sort_by; 19, string; 20, identifier:keys; 21, None; 22, string; 23, identifier:keys; 24, block; 25, else_clause; 26, assignment; 27, comparison_operator:sort_by == 'value'; 28, block; 29, else_clause; 30, assignment; 31, identifier:norm; 32, block; 33, else_clause; 34, expression_list; 35, string_content:value; 36, string_content:Counts the number of occurances of each item in 'data'. Inputs data: a list of values. norm: normalize the resulting counts (as percent) sort_by: how to sort the retured data. Options are 'value' and 'count'. Output a non-redundant list of values (from 'data') and a list of counts.; 37, expression_statement; 38, for_statement; 39, block; 40, identifier:vals; 41, list_comprehension; 42, identifier:sort_by; 43, string; 44, expression_statement; 45, block; 46, identifier:xs; 47, list_comprehension; 48, expression_statement; 49, expression_statement; 50, expression_statement; 51, block; 52, identifier:xs; 53, identifier:ys; 54, assignment; 55, identifier:d; 56, identifier:data; 57, block; 58, expression_statement; 59, for_statement; 60, tuple; 61, for_in_clause; 62, string_content:value; 63, call; 64, expression_statement; 65, subscript; 66, for_in_clause; 67, assignment; 68, assignment; 69, assignment; 70, expression_statement; 71, identifier:vdict; 72, dictionary_comprehension; 73, if_statement; 74, assignment; 75, identifier:d; 76, identifier:data; 77, block; 78, identifier:k; 79, identifier:v; 80, pattern_list; 81, call; 82, attribute; 83, argument_list; 84, call; 85, identifier:v; 86, integer:0; 87, identifier:v; 88, identifier:vals; 89, identifier:raw_y; 90, list_comprehension; 91, identifier:total_y; 92, call; 93, identifier:ys; 94, list_comprehension; 95, assignment; 96, pair; 97, for_in_clause; 98, comparison_operator:d in keys; 99, block; 100, identifier:vdict; 101, dictionary; 102, expression_statement; 103, identifier:k; 104, identifier:v; 105, attribute; 106, argument_list; 107, identifier:vals; 108, identifier:sort; 109, keyword_argument; 110, attribute; 111, argument_list; 112, subscript; 113, for_in_clause; 114, identifier:sum; 115, argument_list; 116, binary_operator:100. * y / total_y; 117, for_in_clause; 118, identifier:ys; 119, list_comprehension; 120, identifier:k; 121, integer:0; 122, identifier:k; 123, identifier:keys; 124, identifier:d; 125, identifier:keys; 126, expression_statement; 127, assignment; 128, identifier:vdict; 129, identifier:items; 130, identifier:key; 131, lambda; 132, identifier:vals; 133, identifier:sort; 134, keyword_argument; 135, identifier:v; 136, integer:1; 137, identifier:v; 138, identifier:vals; 139, identifier:raw_y; 140, binary_operator:100. * y; 141, identifier:total_y; 142, identifier:y; 143, identifier:raw_y; 144, subscript; 145, for_in_clause; 146, augmented_assignment; 147, subscript; 148, conditional_expression:vdict[d] + 1 if d in vdict else 1; 149, lambda_parameters; 150, subscript; 151, identifier:key; 152, lambda; 153, float:100.; 154, identifier:y; 155, identifier:v; 156, integer:1; 157, identifier:v; 158, identifier:vals; 159, subscript; 160, integer:1; 161, identifier:vdict; 162, identifier:d; 163, binary_operator:vdict[d] + 1; 164, comparison_operator:d in vdict; 165, integer:1; 166, identifier:x; 167, identifier:x; 168, integer:0; 169, lambda_parameters; 170, subscript; 171, identifier:vdict; 172, identifier:d; 173, subscript; 174, integer:1; 175, identifier:d; 176, identifier:vdict; 177, identifier:x; 178, identifier:x; 179, integer:1; 180, identifier:vdict; 181, identifier:d
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 6, 16; 6, 17; 7, 18; 7, 19; 8, 20; 8, 21; 9, 22; 10, 23; 10, 24; 10, 25; 11, 26; 12, 27; 12, 28; 12, 29; 13, 30; 14, 31; 14, 32; 14, 33; 15, 34; 19, 35; 22, 36; 24, 37; 24, 38; 25, 39; 26, 40; 26, 41; 27, 42; 27, 43; 28, 44; 29, 45; 30, 46; 30, 47; 32, 48; 32, 49; 32, 50; 33, 51; 34, 52; 34, 53; 37, 54; 38, 55; 38, 56; 38, 57; 39, 58; 39, 59; 41, 60; 41, 61; 43, 62; 44, 63; 45, 64; 47, 65; 47, 66; 48, 67; 49, 68; 50, 69; 51, 70; 54, 71; 54, 72; 57, 73; 58, 74; 59, 75; 59, 76; 59, 77; 60, 78; 60, 79; 61, 80; 61, 81; 63, 82; 63, 83; 64, 84; 65, 85; 65, 86; 66, 87; 66, 88; 67, 89; 67, 90; 68, 91; 68, 92; 69, 93; 69, 94; 70, 95; 72, 96; 72, 97; 73, 98; 73, 99; 74, 100; 74, 101; 77, 102; 80, 103; 80, 104; 81, 105; 81, 106; 82, 107; 82, 108; 83, 109; 84, 110; 84, 111; 90, 112; 90, 113; 92, 114; 92, 115; 94, 116; 94, 117; 95, 118; 95, 119; 96, 120; 96, 121; 97, 122; 97, 123; 98, 124; 98, 125; 99, 126; 102, 127; 105, 128; 105, 129; 109, 130; 109, 131; 110, 132; 110, 133; 111, 134; 112, 135; 112, 136; 113, 137; 113, 138; 115, 139; 116, 140; 116, 141; 117, 142; 117, 143; 119, 144; 119, 145; 126, 146; 127, 147; 127, 148; 131, 149; 131, 150; 134, 151; 134, 152; 140, 153; 140, 154; 144, 155; 144, 156; 145, 157; 145, 158; 146, 159; 146, 160; 147, 161; 147, 162; 148, 163; 148, 164; 148, 165; 149, 166; 150, 167; 150, 168; 152, 169; 152, 170; 159, 171; 159, 172; 163, 173; 163, 174; 164, 175; 164, 176; 169, 177; 170, 178; 170, 179; 173, 180; 173, 181
def _aggregate(data, norm=True, sort_by='value', keys=None): ''' Counts the number of occurances of each item in 'data'. Inputs data: a list of values. norm: normalize the resulting counts (as percent) sort_by: how to sort the retured data. Options are 'value' and 'count'. Output a non-redundant list of values (from 'data') and a list of counts. ''' if keys: vdict = {k: 0 for k in keys} for d in data: if d in keys: vdict[d] += 1 else: vdict = {} for d in data: vdict[d] = vdict[d] + 1 if d in vdict else 1 vals = [(k, v) for k, v in vdict.items()] if sort_by == 'value': vals.sort(key=lambda x: x[0]) else: vals.sort(key=lambda x: x[1]) xs = [v[0] for v in vals] if norm: raw_y = [v[1] for v in vals] total_y = sum(raw_y) ys = [100. * y / total_y for y in raw_y] else: ys = [v[1] for v in vals] return xs, ys
0, module; 1, function_definition; 2, function_name:list_files; 3, parameters; 4, block; 5, identifier:d; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, if_statement; 10, return_statement; 11, identifier:extension; 12, None; 13, string; 14, call; 15, block; 16, else_clause; 17, comparison_operator:extension is not None; 18, block; 19, identifier:files; 20, string_content:Lists files in a given directory. Args: d (str): Path to a directory. extension (str): If supplied, only files that contain the specificied extension will be returned. Default is ``False``, which returns all files in ``d``. Returns: list: A sorted list of file paths.; 21, attribute; 22, argument_list; 23, expression_statement; 24, expression_statement; 25, block; 26, identifier:extension; 27, None; 28, if_statement; 29, expression_statement; 30, attribute; 31, identifier:isdir; 32, identifier:d; 33, assignment; 34, assignment; 35, expression_statement; 36, comparison_operator:type(extension) in STR_TYPES; 37, block; 38, assignment; 39, identifier:os; 40, identifier:path; 41, identifier:expanded_dir; 42, call; 43, identifier:files; 44, call; 45, assignment; 46, call; 47, identifier:STR_TYPES; 48, expression_statement; 49, identifier:files; 50, list_comprehension; 51, attribute; 52, argument_list; 53, identifier:sorted; 54, argument_list; 55, identifier:files; 56, list; 57, identifier:type; 58, argument_list; 59, assignment; 60, identifier:f; 61, for_in_clause; 62, if_clause; 63, attribute; 64, identifier:expanduser; 65, identifier:d; 66, call; 67, identifier:d; 68, identifier:extension; 69, identifier:extension; 70, list; 71, identifier:f; 72, identifier:files; 73, call; 74, identifier:os; 75, identifier:path; 76, attribute; 77, argument_list; 78, identifier:extension; 79, identifier:any; 80, argument_list; 81, identifier:glob; 82, identifier:glob; 83, binary_operator:expanded_dir + '/*'; 84, list; 85, identifier:expanded_dir; 86, string; 87, comparison_operator:f.split('.')[-1] in extension; 88, comparison_operator:f.split('.')[-1].upper() in extension; 89, comparison_operator:f.split('.')[-1].lower() in extension; 90, string_content:/*; 91, subscript; 92, identifier:extension; 93, call; 94, identifier:extension; 95, call; 96, identifier:extension; 97, call; 98, unary_operator; 99, attribute; 100, argument_list; 101, attribute; 102, argument_list; 103, attribute; 104, argument_list; 105, integer:1; 106, subscript; 107, identifier:upper; 108, subscript; 109, identifier:lower; 110, identifier:f; 111, identifier:split; 112, string; 113, call; 114, unary_operator; 115, call; 116, unary_operator; 117, string_content:.; 118, attribute; 119, argument_list; 120, integer:1; 121, attribute; 122, argument_list; 123, integer:1; 124, identifier:f; 125, identifier:split; 126, string; 127, identifier:f; 128, identifier:split; 129, string; 130, string_content:.; 131, string_content:.
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 6, 12; 7, 13; 8, 14; 8, 15; 8, 16; 9, 17; 9, 18; 10, 19; 13, 20; 14, 21; 14, 22; 15, 23; 15, 24; 16, 25; 17, 26; 17, 27; 18, 28; 18, 29; 21, 30; 21, 31; 22, 32; 23, 33; 24, 34; 25, 35; 28, 36; 28, 37; 29, 38; 30, 39; 30, 40; 33, 41; 33, 42; 34, 43; 34, 44; 35, 45; 36, 46; 36, 47; 37, 48; 38, 49; 38, 50; 42, 51; 42, 52; 44, 53; 44, 54; 45, 55; 45, 56; 46, 57; 46, 58; 48, 59; 50, 60; 50, 61; 50, 62; 51, 63; 51, 64; 52, 65; 54, 66; 56, 67; 58, 68; 59, 69; 59, 70; 61, 71; 61, 72; 62, 73; 63, 74; 63, 75; 66, 76; 66, 77; 70, 78; 73, 79; 73, 80; 76, 81; 76, 82; 77, 83; 80, 84; 83, 85; 83, 86; 84, 87; 84, 88; 84, 89; 86, 90; 87, 91; 87, 92; 88, 93; 88, 94; 89, 95; 89, 96; 91, 97; 91, 98; 93, 99; 93, 100; 95, 101; 95, 102; 97, 103; 97, 104; 98, 105; 99, 106; 99, 107; 101, 108; 101, 109; 103, 110; 103, 111; 104, 112; 106, 113; 106, 114; 108, 115; 108, 116; 112, 117; 113, 118; 113, 119; 114, 120; 115, 121; 115, 122; 116, 123; 118, 124; 118, 125; 119, 126; 121, 127; 121, 128; 122, 129; 126, 130; 129, 131
def list_files(d, extension=None): ''' Lists files in a given directory. Args: d (str): Path to a directory. extension (str): If supplied, only files that contain the specificied extension will be returned. Default is ``False``, which returns all files in ``d``. Returns: list: A sorted list of file paths. ''' if os.path.isdir(d): expanded_dir = os.path.expanduser(d) files = sorted(glob.glob(expanded_dir + '/*')) else: files = [d, ] if extension is not None: if type(extension) in STR_TYPES: extension = [extension, ] files = [f for f in files if any([f.split('.')[-1] in extension, f.split('.')[-1].upper() in extension, f.split('.')[-1].lower() in extension])] return files
0, module; 1, function_definition; 2, function_name:get_collections; 3, parameters; 4, block; 5, identifier:db; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, if_statement; 13, if_statement; 14, return_statement; 15, identifier:collection; 16, None; 17, identifier:prefix; 18, None; 19, identifier:suffix; 20, None; 21, string; 22, comparison_operator:collection is not None; 23, block; 24, assignment; 25, comparison_operator:prefix is not None; 26, block; 27, comparison_operator:suffix is not None; 28, block; 29, call; 30, string_content:Returns a sorted list of collection names found in ``db``. Arguments: db (Database): A pymongo Database object. Can be obtained with ``get_db``. collection (str): Name of a collection. If the collection is present in the MongoDB database, a single-element list will be returned with the collecion name. If not, an empty list will be returned. This option is primarly included to allow for quick checking to see if a collection name is present. Default is None, which results in this option being ignored. prefix (str): If supplied, only collections that begin with ``prefix`` will be returned. suffix (str): If supplied, only collections that end with ``suffix`` will be returned. Returns: list: A sorted list of collection names.; 31, identifier:collection; 32, None; 33, return_statement; 34, identifier:collections; 35, call; 36, identifier:prefix; 37, None; 38, expression_statement; 39, identifier:suffix; 40, None; 41, expression_statement; 42, identifier:sorted; 43, argument_list; 44, list; 45, attribute; 46, argument_list; 47, assignment; 48, assignment; 49, identifier:collections; 50, identifier:collection; 51, identifier:db; 52, identifier:collection_names; 53, keyword_argument; 54, identifier:collections; 55, list_comprehension; 56, identifier:collections; 57, list_comprehension; 58, identifier:include_system_collections; 59, False; 60, identifier:c; 61, for_in_clause; 62, if_clause; 63, identifier:c; 64, for_in_clause; 65, if_clause; 66, identifier:c; 67, identifier:collections; 68, call; 69, identifier:c; 70, identifier:collections; 71, call; 72, attribute; 73, argument_list; 74, attribute; 75, argument_list; 76, identifier:c; 77, identifier:startswith; 78, identifier:prefix; 79, identifier:c; 80, identifier:endswith; 81, identifier:suffix
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 6, 15; 6, 16; 7, 17; 7, 18; 8, 19; 8, 20; 9, 21; 10, 22; 10, 23; 11, 24; 12, 25; 12, 26; 13, 27; 13, 28; 14, 29; 21, 30; 22, 31; 22, 32; 23, 33; 24, 34; 24, 35; 25, 36; 25, 37; 26, 38; 27, 39; 27, 40; 28, 41; 29, 42; 29, 43; 33, 44; 35, 45; 35, 46; 38, 47; 41, 48; 43, 49; 44, 50; 45, 51; 45, 52; 46, 53; 47, 54; 47, 55; 48, 56; 48, 57; 53, 58; 53, 59; 55, 60; 55, 61; 55, 62; 57, 63; 57, 64; 57, 65; 61, 66; 61, 67; 62, 68; 64, 69; 64, 70; 65, 71; 68, 72; 68, 73; 71, 74; 71, 75; 72, 76; 72, 77; 73, 78; 74, 79; 74, 80; 75, 81
def get_collections(db, collection=None, prefix=None, suffix=None): ''' Returns a sorted list of collection names found in ``db``. Arguments: db (Database): A pymongo Database object. Can be obtained with ``get_db``. collection (str): Name of a collection. If the collection is present in the MongoDB database, a single-element list will be returned with the collecion name. If not, an empty list will be returned. This option is primarly included to allow for quick checking to see if a collection name is present. Default is None, which results in this option being ignored. prefix (str): If supplied, only collections that begin with ``prefix`` will be returned. suffix (str): If supplied, only collections that end with ``suffix`` will be returned. Returns: list: A sorted list of collection names. ''' if collection is not None: return [collection, ] collections = db.collection_names(include_system_collections=False) if prefix is not None: collections = [c for c in collections if c.startswith(prefix)] if suffix is not None: collections = [c for c in collections if c.endswith(suffix)] return sorted(collections)
0, module; 1, function_definition; 2, function_name:leaf_nodes; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, comment:# Now contains all nodes that contain dependencies.; 8, expression_statement; 9, comment:# contains all nodes *without* any dependencies (leaf nodes); 10, return_statement; 11, comment:""" Return an interable of nodes with no edges pointing at them. This is helpful to find all nodes without dependencies. """; 12, assignment; 13, binary_operator:self.nodes - deps; 14, identifier:deps; 15, set_comprehension; 16, attribute; 17, identifier:deps; 18, identifier:item; 19, for_in_clause; 20, for_in_clause; 21, identifier:self; 22, identifier:nodes; 23, identifier:sublist; 24, call; 25, identifier:item; 26, identifier:sublist; 27, attribute; 28, argument_list; 29, attribute; 30, identifier:values; 31, identifier:self; 32, identifier:edges
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 8, 12; 10, 13; 12, 14; 12, 15; 13, 16; 13, 17; 15, 18; 15, 19; 15, 20; 16, 21; 16, 22; 19, 23; 19, 24; 20, 25; 20, 26; 24, 27; 24, 28; 27, 29; 27, 30; 29, 31; 29, 32
def leaf_nodes(self): """ Return an interable of nodes with no edges pointing at them. This is helpful to find all nodes without dependencies. """ # Now contains all nodes that contain dependencies. deps = {item for sublist in self.edges.values() for item in sublist} # contains all nodes *without* any dependencies (leaf nodes) return self.nodes - deps
0, module; 1, function_definition; 2, function_name:sort; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, while_statement; 8, comment:""" Return an iterable of nodes, toplogically sorted to correctly import dependencies before leaf nodes. """; 9, attribute; 10, block; 11, identifier:self; 12, identifier:nodes; 13, expression_statement; 14, for_statement; 15, if_statement; 16, assignment; 17, identifier:node; 18, call; 19, block; 20, not_operator; 21, block; 22, identifier:iterated; 23, False; 24, attribute; 25, argument_list; 26, expression_statement; 27, expression_statement; 28, expression_statement; 29, identifier:iterated; 30, raise_statement; 31, identifier:self; 32, identifier:leaf_nodes; 33, assignment; 34, call; 35, yield; 36, call; 37, identifier:iterated; 38, True; 39, attribute; 40, argument_list; 41, identifier:node; 42, identifier:CyclicGraphError; 43, argument_list; 44, identifier:self; 45, identifier:prune_node; 46, identifier:node; 47, string:"Sorting has found a cyclic graph."
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 6, 8; 7, 9; 7, 10; 9, 11; 9, 12; 10, 13; 10, 14; 10, 15; 13, 16; 14, 17; 14, 18; 14, 19; 15, 20; 15, 21; 16, 22; 16, 23; 18, 24; 18, 25; 19, 26; 19, 27; 19, 28; 20, 29; 21, 30; 24, 31; 24, 32; 26, 33; 27, 34; 28, 35; 30, 36; 33, 37; 33, 38; 34, 39; 34, 40; 35, 41; 36, 42; 36, 43; 39, 44; 39, 45; 40, 46; 43, 47
def sort(self): """ Return an iterable of nodes, toplogically sorted to correctly import dependencies before leaf nodes. """ while self.nodes: iterated = False for node in self.leaf_nodes(): iterated = True self.prune_node(node) yield node if not iterated: raise CyclicGraphError("Sorting has found a cyclic graph.")
0, module; 1, function_definition; 2, function_name:cycles; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, function_definition; 8, comment:# First, let's get a iterable of all known cycles.; 9, expression_statement; 10, expression_statement; 11, comment:# Now, let's go through and sift through the cycles, finding; 12, comment:# the shortest unique cycle known, ignoring cycles which contain; 13, comment:# already known cycles.; 14, for_statement; 15, comment:# And return that unique list.; 16, return_statement; 17, comment:""" Fairly expensive cycle detection algorithm. This method will return the shortest unique cycles that were detected. Debug usage may look something like: print("The following cycles were found:") for cycle in network.cycles(): print(" ", " -> ".join(cycle)) """; 18, function_name:walk_node; 19, parameters; 20, block; 21, assignment; 22, assignment; 23, identifier:cycle; 24, call; 25, block; 26, identifier:shortest; 27, identifier:node; 28, identifier:seen; 29, expression_statement; 30, if_statement; 31, expression_statement; 32, for_statement; 33, identifier:cycles; 34, call; 35, identifier:shortest; 36, call; 37, identifier:sorted; 38, argument_list; 39, for_statement; 40, comment:""" Walk each top-level node we know about, and recurse along the graph. """; 41, comparison_operator:node in seen; 42, block; 43, call; 44, identifier:edge; 45, subscript; 46, block; 47, attribute; 48, argument_list; 49, identifier:set; 50, argument_list; 51, identifier:cycles; 52, keyword_argument; 53, identifier:el; 54, identifier:shortest; 55, block; 56, else_clause; 57, identifier:node; 58, identifier:seen; 59, expression_statement; 60, return_statement; 61, attribute; 62, argument_list; 63, attribute; 64, identifier:node; 65, for_statement; 66, identifier:chain; 67, identifier:from_iterable; 68, generator_expression; 69, identifier:key; 70, identifier:len; 71, if_statement; 72, block; 73, yield; 74, identifier:seen; 75, identifier:add; 76, identifier:node; 77, identifier:self; 78, identifier:edges; 79, identifier:cycle; 80, call; 81, block; 82, call; 83, for_in_clause; 84, call; 85, block; 86, expression_statement; 87, tuple; 88, identifier:walk_node; 89, argument_list; 90, expression_statement; 91, identifier:walk_node; 92, argument_list; 93, identifier:node; 94, attribute; 95, attribute; 96, argument_list; 97, break_statement; 98, call; 99, identifier:node; 100, identifier:edge; 101, call; 102, yield; 103, identifier:node; 104, call; 105, identifier:self; 106, identifier:nodes; 107, call; 108, identifier:issubset; 109, call; 110, attribute; 111, argument_list; 112, identifier:set; 113, argument_list; 114, binary_operator:(node,) + cycle; 115, identifier:set; 116, argument_list; 117, identifier:set; 118, argument_list; 119, identifier:set; 120, argument_list; 121, identifier:shortest; 122, identifier:add; 123, identifier:cycle; 124, identifier:seen; 125, tuple; 126, identifier:cycle; 127, identifier:el; 128, identifier:cycle; 129, identifier:node
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 6, 17; 7, 18; 7, 19; 7, 20; 9, 21; 10, 22; 14, 23; 14, 24; 14, 25; 16, 26; 19, 27; 19, 28; 20, 29; 20, 30; 20, 31; 20, 32; 21, 33; 21, 34; 22, 35; 22, 36; 24, 37; 24, 38; 25, 39; 29, 40; 30, 41; 30, 42; 31, 43; 32, 44; 32, 45; 32, 46; 34, 47; 34, 48; 36, 49; 36, 50; 38, 51; 38, 52; 39, 53; 39, 54; 39, 55; 39, 56; 41, 57; 41, 58; 42, 59; 42, 60; 43, 61; 43, 62; 45, 63; 45, 64; 46, 65; 47, 66; 47, 67; 48, 68; 52, 69; 52, 70; 55, 71; 56, 72; 59, 73; 61, 74; 61, 75; 62, 76; 63, 77; 63, 78; 65, 79; 65, 80; 65, 81; 68, 82; 68, 83; 71, 84; 71, 85; 72, 86; 73, 87; 80, 88; 80, 89; 81, 90; 82, 91; 82, 92; 83, 93; 83, 94; 84, 95; 84, 96; 85, 97; 86, 98; 87, 99; 89, 100; 89, 101; 90, 102; 92, 103; 92, 104; 94, 105; 94, 106; 95, 107; 95, 108; 96, 109; 98, 110; 98, 111; 101, 112; 101, 113; 102, 114; 104, 115; 104, 116; 107, 117; 107, 118; 109, 119; 109, 120; 110, 121; 110, 122; 111, 123; 113, 124; 114, 125; 114, 126; 118, 127; 120, 128; 125, 129
def cycles(self): """ Fairly expensive cycle detection algorithm. This method will return the shortest unique cycles that were detected. Debug usage may look something like: print("The following cycles were found:") for cycle in network.cycles(): print(" ", " -> ".join(cycle)) """ def walk_node(node, seen): """ Walk each top-level node we know about, and recurse along the graph. """ if node in seen: yield (node,) return seen.add(node) for edge in self.edges[node]: for cycle in walk_node(edge, set(seen)): yield (node,) + cycle # First, let's get a iterable of all known cycles. cycles = chain.from_iterable( (walk_node(node, set()) for node in self.nodes)) shortest = set() # Now, let's go through and sift through the cycles, finding # the shortest unique cycle known, ignoring cycles which contain # already known cycles. for cycle in sorted(cycles, key=len): for el in shortest: if set(el).issubset(set(cycle)): break else: shortest.add(cycle) # And return that unique list. return shortest
0, module; 1, function_definition; 2, function_name:_prepare_imports; 3, parameters; 4, block; 5, identifier:self; 6, identifier:dicts; 7, expression_statement; 8, comment:# all pseudo parent ids we've seen; 9, expression_statement; 10, comment:# pseudo matches; 11, expression_statement; 12, comment:# get prepared imports from parent; 13, expression_statement; 14, comment:# collect parent pseudo_ids; 15, for_statement; 16, comment:# turn pseudo_ids into a tuple of dictionaries; 17, expression_statement; 18, comment:# loop over all data again, finding the pseudo ids true json id; 19, for_statement; 20, comment:# toposort the nodes so parents are imported first; 21, expression_statement; 22, expression_statement; 23, expression_statement; 24, for_statement; 25, comment:# resolve the sorted import order; 26, for_statement; 27, comment:# ensure all data made it into network (paranoid check, should never fail); 28, if_statement; 29, return_statement; 30, comment:""" an override for prepare imports that sorts the imports by parent_id dependencies """; 31, assignment; 32, assignment; 33, assignment; 34, pattern_list; 35, call; 36, block; 37, assignment; 38, pattern_list; 39, call; 40, comment:# check if this matches one of our ppids; 41, block; 42, assignment; 43, assignment; 44, assignment; 45, pattern_list; 46, call; 47, block; 48, identifier:jid; 49, call; 50, block; 51, comparison_operator:in_network != set(prepared.keys()); 52, comment:# pragma: no cover; 53, block; 54, identifier:import_order; 55, identifier:pseudo_ids; 56, call; 57, identifier:pseudo_matches; 58, dictionary; 59, identifier:prepared; 60, call; 61, identifier:_; 62, identifier:data; 63, attribute; 64, argument_list; 65, expression_statement; 66, if_statement; 67, identifier:pseudo_ids; 68, list_comprehension; 69, identifier:json_id; 70, identifier:data; 71, attribute; 72, argument_list; 73, for_statement; 74, identifier:network; 75, call; 76, identifier:in_network; 77, call; 78, identifier:import_order; 79, list; 80, identifier:json_id; 81, identifier:data; 82, attribute; 83, argument_list; 84, expression_statement; 85, comment:# resolve pseudo_ids to their json id before building the network; 86, if_statement; 87, expression_statement; 88, if_statement; 89, attribute; 90, argument_list; 91, expression_statement; 92, expression_statement; 93, identifier:in_network; 94, call; 95, raise_statement; 96, identifier:set; 97, argument_list; 98, identifier:dict; 99, argument_list; 100, identifier:prepared; 101, identifier:items; 102, assignment; 103, call; 104, block; 105, tuple; 106, for_in_clause; 107, identifier:prepared; 108, identifier:items; 109, pattern_list; 110, identifier:pseudo_ids; 111, block; 112, identifier:Network; 113, argument_list; 114, identifier:set; 115, argument_list; 116, identifier:prepared; 117, identifier:items; 118, assignment; 119, comparison_operator:parent_id in pseudo_matches; 120, block; 121, call; 122, identifier:parent_id; 123, comment:# Right. There's an import dep. We need to add the edge from; 124, comment:# the parent to the current node, so that we import the parent; 125, comment:# before the current node.; 126, block; 127, identifier:network; 128, identifier:sort; 129, call; 130, call; 131, identifier:set; 132, argument_list; 133, call; 134, call; 135, identifier:parent_id; 136, boolean_operator; 137, attribute; 138, argument_list; 139, expression_statement; 140, identifier:ppid; 141, call; 142, identifier:ppid; 143, identifier:pseudo_ids; 144, identifier:ppid; 145, identifier:spec; 146, expression_statement; 147, for_statement; 148, if_statement; 149, identifier:parent_id; 150, call; 151, identifier:parent_id; 152, identifier:pseudo_matches; 153, expression_statement; 154, attribute; 155, argument_list; 156, expression_statement; 157, attribute; 158, argument_list; 159, attribute; 160, argument_list; 161, call; 162, identifier:PupaInternalError; 163, argument_list; 164, attribute; 165, argument_list; 166, call; 167, string; 168, identifier:parent_id; 169, identifier:startswith; 170, string; 171, call; 172, identifier:get_pseudo_id; 173, argument_list; 174, assignment; 175, pattern_list; 176, call; 177, block; 178, identifier:match; 179, block; 180, attribute; 181, argument_list; 182, assignment; 183, identifier:network; 184, identifier:add_node; 185, identifier:json_id; 186, call; 187, identifier:import_order; 188, identifier:append; 189, tuple; 190, identifier:in_network; 191, identifier:add; 192, identifier:jid; 193, attribute; 194, argument_list; 195, string:"import is missing nodes in network set"; 196, call; 197, identifier:_prepare_imports; 198, identifier:dicts; 199, attribute; 200, argument_list; 201, string_content:~; 202, attribute; 203, argument_list; 204, identifier:ppid; 205, identifier:match; 206, True; 207, identifier:k; 208, identifier:v; 209, attribute; 210, argument_list; 211, if_statement; 212, if_statement; 213, expression_statement; 214, identifier:data; 215, identifier:get; 216, string; 217, None; 218, identifier:parent_id; 219, subscript; 220, attribute; 221, argument_list; 222, identifier:jid; 223, subscript; 224, identifier:prepared; 225, identifier:keys; 226, identifier:super; 227, argument_list; 228, identifier:data; 229, identifier:get; 230, string; 231, None; 232, identifier:pseudo_ids; 233, identifier:add; 234, identifier:parent_id; 235, identifier:spec; 236, identifier:items; 237, comparison_operator:data[k] != v; 238, block; 239, comparison_operator:ppid in pseudo_matches; 240, block; 241, assignment; 242, string_content:parent_id; 243, identifier:pseudo_matches; 244, identifier:parent_id; 245, identifier:network; 246, identifier:add_edge; 247, identifier:parent_id; 248, identifier:json_id; 249, identifier:prepared; 250, identifier:jid; 251, identifier:OrganizationImporter; 252, identifier:self; 253, string_content:parent_id; 254, subscript; 255, identifier:v; 256, expression_statement; 257, break_statement; 258, identifier:ppid; 259, identifier:pseudo_matches; 260, raise_statement; 261, subscript; 262, identifier:json_id; 263, identifier:data; 264, identifier:k; 265, assignment; 266, call; 267, identifier:pseudo_matches; 268, identifier:ppid; 269, identifier:match; 270, False; 271, identifier:UnresolvedIdError; 272, argument_list; 273, binary_operator:'multiple matches for pseudo id: ' + ppid; 274, string; 275, identifier:ppid; 276, string_content:multiple matches for pseudo id:
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 4, 25; 4, 26; 4, 27; 4, 28; 4, 29; 7, 30; 9, 31; 11, 32; 13, 33; 15, 34; 15, 35; 15, 36; 17, 37; 19, 38; 19, 39; 19, 40; 19, 41; 21, 42; 22, 43; 23, 44; 24, 45; 24, 46; 24, 47; 26, 48; 26, 49; 26, 50; 28, 51; 28, 52; 28, 53; 29, 54; 31, 55; 31, 56; 32, 57; 32, 58; 33, 59; 33, 60; 34, 61; 34, 62; 35, 63; 35, 64; 36, 65; 36, 66; 37, 67; 37, 68; 38, 69; 38, 70; 39, 71; 39, 72; 41, 73; 42, 74; 42, 75; 43, 76; 43, 77; 44, 78; 44, 79; 45, 80; 45, 81; 46, 82; 46, 83; 47, 84; 47, 85; 47, 86; 47, 87; 47, 88; 49, 89; 49, 90; 50, 91; 50, 92; 51, 93; 51, 94; 53, 95; 56, 96; 56, 97; 60, 98; 60, 99; 63, 100; 63, 101; 65, 102; 66, 103; 66, 104; 68, 105; 68, 106; 71, 107; 71, 108; 73, 109; 73, 110; 73, 111; 75, 112; 75, 113; 77, 114; 77, 115; 82, 116; 82, 117; 84, 118; 86, 119; 86, 120; 87, 121; 88, 122; 88, 123; 88, 124; 88, 125; 88, 126; 89, 127; 89, 128; 91, 129; 92, 130; 94, 131; 94, 132; 95, 133; 99, 134; 102, 135; 102, 136; 103, 137; 103, 138; 104, 139; 105, 140; 105, 141; 106, 142; 106, 143; 109, 144; 109, 145; 111, 146; 111, 147; 111, 148; 118, 149; 118, 150; 119, 151; 119, 152; 120, 153; 121, 154; 121, 155; 126, 156; 129, 157; 129, 158; 130, 159; 130, 160; 132, 161; 133, 162; 133, 163; 134, 164; 134, 165; 136, 166; 136, 167; 137, 168; 137, 169; 138, 170; 139, 171; 141, 172; 141, 173; 146, 174; 147, 175; 147, 176; 147, 177; 148, 178; 148, 179; 150, 180; 150, 181; 153, 182; 154, 183; 154, 184; 155, 185; 156, 186; 157, 187; 157, 188; 158, 189; 159, 190; 159, 191; 160, 192; 161, 193; 161, 194; 163, 195; 164, 196; 164, 197; 165, 198; 166, 199; 166, 200; 170, 201; 171, 202; 171, 203; 173, 204; 174, 205; 174, 206; 175, 207; 175, 208; 176, 209; 176, 210; 177, 211; 179, 212; 179, 213; 180, 214; 180, 215; 181, 216; 181, 217; 182, 218; 182, 219; 186, 220; 186, 221; 189, 222; 189, 223; 193, 224; 193, 225; 196, 226; 196, 227; 199, 228; 199, 229; 200, 230; 200, 231; 202, 232; 202, 233; 203, 234; 209, 235; 209, 236; 211, 237; 211, 238; 212, 239; 212, 240; 213, 241; 216, 242; 219, 243; 219, 244; 220, 245; 220, 246; 221, 247; 221, 248; 223, 249; 223, 250; 227, 251; 227, 252; 230, 253; 237, 254; 237, 255; 238, 256; 238, 257; 239, 258; 239, 259; 240, 260; 241, 261; 241, 262; 254, 263; 254, 264; 256, 265; 260, 266; 261, 267; 261, 268; 265, 269; 265, 270; 266, 271; 266, 272; 272, 273; 273, 274; 273, 275; 274, 276
def _prepare_imports(self, dicts): """ an override for prepare imports that sorts the imports by parent_id dependencies """ # all pseudo parent ids we've seen pseudo_ids = set() # pseudo matches pseudo_matches = {} # get prepared imports from parent prepared = dict(super(OrganizationImporter, self)._prepare_imports(dicts)) # collect parent pseudo_ids for _, data in prepared.items(): parent_id = data.get('parent_id', None) or '' if parent_id.startswith('~'): pseudo_ids.add(parent_id) # turn pseudo_ids into a tuple of dictionaries pseudo_ids = [(ppid, get_pseudo_id(ppid)) for ppid in pseudo_ids] # loop over all data again, finding the pseudo ids true json id for json_id, data in prepared.items(): # check if this matches one of our ppids for ppid, spec in pseudo_ids: match = True for k, v in spec.items(): if data[k] != v: match = False break if match: if ppid in pseudo_matches: raise UnresolvedIdError('multiple matches for pseudo id: ' + ppid) pseudo_matches[ppid] = json_id # toposort the nodes so parents are imported first network = Network() in_network = set() import_order = [] for json_id, data in prepared.items(): parent_id = data.get('parent_id', None) # resolve pseudo_ids to their json id before building the network if parent_id in pseudo_matches: parent_id = pseudo_matches[parent_id] network.add_node(json_id) if parent_id: # Right. There's an import dep. We need to add the edge from # the parent to the current node, so that we import the parent # before the current node. network.add_edge(parent_id, json_id) # resolve the sorted import order for jid in network.sort(): import_order.append((jid, prepared[jid])) in_network.add(jid) # ensure all data made it into network (paranoid check, should never fail) if in_network != set(prepared.keys()): # pragma: no cover raise PupaInternalError("import is missing nodes in network set") return import_order
0, module; 1, function_definition; 2, function_name:initial; 3, parameters; 4, block; 5, identifier:self; 6, identifier:request; 7, list_splat_pattern; 8, dictionary_splat_pattern; 9, expression_statement; 10, expression_statement; 11, assert_statement; 12, if_statement; 13, expression_statement; 14, identifier:args; 15, identifier:kwargs; 16, comment:""" Overrides DRF's `initial` in order to set the `_sorting_field` from corresponding property in view. Protected property is required in order to support overriding of `sorting_field` via `@property`, we do this after original `initial` has been ran in order to make sure that view has all its properties set up. """; 17, call; 18, not_operator; 19, call; 20, attribute; 21, block; 22, assignment; 23, attribute; 24, argument_list; 25, parenthesized_expression; 26, attribute; 27, argument_list; 28, identifier:self; 29, identifier:sorting_field; 30, expression_statement; 31, expression_statement; 32, attribute; 33, attribute; 34, call; 35, identifier:initial; 36, identifier:request; 37, list_splat; 38, dictionary_splat; 39, boolean_operator; 40, string; 41, line_continuation:\; 42, identifier:format; 43, attribute; 44, call; 45, assignment; 46, identifier:self; 47, identifier:_sorting_fields; 48, identifier:self; 49, identifier:sorting_fields; 50, identifier:super; 51, argument_list; 52, identifier:args; 53, identifier:kwargs; 54, attribute; 55, attribute; 56, string_content:{} should either define ``sorting_field`` or ``sorting_fields`` property, not both.; 57, attribute; 58, identifier:__name__; 59, attribute; 60, argument_list; 61, attribute; 62, list; 63, identifier:FlatMultipleModelMixin; 64, identifier:self; 65, identifier:self; 66, identifier:sorting_field; 67, identifier:self; 68, identifier:sorting_fields; 69, identifier:self; 70, identifier:__class__; 71, identifier:warnings; 72, identifier:warn; 73, string; 74, identifier:DeprecationWarning; 75, identifier:self; 76, identifier:sorting_fields; 77, attribute; 78, string_content:``sorting_field`` property is pending its deprecation. Use ``sorting_fields`` instead.; 79, identifier:self; 80, identifier:sorting_field
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 7, 14; 8, 15; 9, 16; 10, 17; 11, 18; 11, 19; 12, 20; 12, 21; 13, 22; 17, 23; 17, 24; 18, 25; 19, 26; 19, 27; 20, 28; 20, 29; 21, 30; 21, 31; 22, 32; 22, 33; 23, 34; 23, 35; 24, 36; 24, 37; 24, 38; 25, 39; 26, 40; 26, 41; 26, 42; 27, 43; 30, 44; 31, 45; 32, 46; 32, 47; 33, 48; 33, 49; 34, 50; 34, 51; 37, 52; 38, 53; 39, 54; 39, 55; 40, 56; 43, 57; 43, 58; 44, 59; 44, 60; 45, 61; 45, 62; 51, 63; 51, 64; 54, 65; 54, 66; 55, 67; 55, 68; 57, 69; 57, 70; 59, 71; 59, 72; 60, 73; 60, 74; 61, 75; 61, 76; 62, 77; 73, 78; 77, 79; 77, 80
def initial(self, request, *args, **kwargs): """ Overrides DRF's `initial` in order to set the `_sorting_field` from corresponding property in view. Protected property is required in order to support overriding of `sorting_field` via `@property`, we do this after original `initial` has been ran in order to make sure that view has all its properties set up. """ super(FlatMultipleModelMixin, self).initial(request, *args, **kwargs) assert not (self.sorting_field and self.sorting_fields), \ '{} should either define ``sorting_field`` or ``sorting_fields`` property, not both.' \ .format(self.__class__.__name__) if self.sorting_field: warnings.warn( '``sorting_field`` property is pending its deprecation. Use ``sorting_fields`` instead.', DeprecationWarning ) self.sorting_fields = [self.sorting_field] self._sorting_fields = self.sorting_fields
0, module; 1, function_definition; 2, function_name:prepare_sorting_fields; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, if_statement; 8, if_statement; 9, comment:""" Determine sorting direction and sorting field based on request query parameters and sorting options of self """; 10, comparison_operator:self.sorting_parameter_name in self.request.query_params; 11, comment:# Extract sorting parameter from query string; 12, block; 13, attribute; 14, comment:# Create a list of sorting parameters. Each parameter is a tuple: (field:str, descending:bool); 15, block; 16, attribute; 17, attribute; 18, expression_statement; 19, identifier:self; 20, identifier:_sorting_fields; 21, expression_statement; 22, identifier:self; 23, identifier:sorting_parameter_name; 24, attribute; 25, identifier:query_params; 26, assignment; 27, assignment; 28, identifier:self; 29, identifier:request; 30, attribute; 31, list_comprehension; 32, attribute; 33, list_comprehension; 34, identifier:self; 35, identifier:_sorting_fields; 36, call; 37, for_in_clause; 38, identifier:self; 39, identifier:_sorting_fields; 40, tuple; 41, for_in_clause; 42, attribute; 43, argument_list; 44, identifier:_; 45, call; 46, call; 47, comparison_operator:field[0] == '-'; 48, identifier:field; 49, attribute; 50, identifier:_; 51, identifier:strip; 52, attribute; 53, argument_list; 54, attribute; 55, argument_list; 56, subscript; 57, string; 58, identifier:self; 59, identifier:_sorting_fields; 60, call; 61, identifier:split; 62, string; 63, attribute; 64, identifier:get; 65, call; 66, call; 67, identifier:field; 68, integer:0; 69, string_content:-; 70, attribute; 71, argument_list; 72, string_content:,; 73, identifier:self; 74, identifier:sorting_fields_map; 75, attribute; 76, argument_list; 77, attribute; 78, argument_list; 79, attribute; 80, identifier:get; 81, attribute; 82, identifier:field; 83, identifier:lstrip; 84, string; 85, identifier:field; 86, identifier:lstrip; 87, string; 88, attribute; 89, identifier:query_params; 90, identifier:self; 91, identifier:sorting_parameter_name; 92, string_content:-; 93, string_content:-; 94, identifier:self; 95, identifier:request
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 6, 9; 7, 10; 7, 11; 7, 12; 8, 13; 8, 14; 8, 15; 10, 16; 10, 17; 12, 18; 13, 19; 13, 20; 15, 21; 16, 22; 16, 23; 17, 24; 17, 25; 18, 26; 21, 27; 24, 28; 24, 29; 26, 30; 26, 31; 27, 32; 27, 33; 30, 34; 30, 35; 31, 36; 31, 37; 32, 38; 32, 39; 33, 40; 33, 41; 36, 42; 36, 43; 37, 44; 37, 45; 40, 46; 40, 47; 41, 48; 41, 49; 42, 50; 42, 51; 45, 52; 45, 53; 46, 54; 46, 55; 47, 56; 47, 57; 49, 58; 49, 59; 52, 60; 52, 61; 53, 62; 54, 63; 54, 64; 55, 65; 55, 66; 56, 67; 56, 68; 57, 69; 60, 70; 60, 71; 62, 72; 63, 73; 63, 74; 65, 75; 65, 76; 66, 77; 66, 78; 70, 79; 70, 80; 71, 81; 75, 82; 75, 83; 76, 84; 77, 85; 77, 86; 78, 87; 79, 88; 79, 89; 81, 90; 81, 91; 84, 92; 87, 93; 88, 94; 88, 95
def prepare_sorting_fields(self): """ Determine sorting direction and sorting field based on request query parameters and sorting options of self """ if self.sorting_parameter_name in self.request.query_params: # Extract sorting parameter from query string self._sorting_fields = [ _.strip() for _ in self.request.query_params.get(self.sorting_parameter_name).split(',') ] if self._sorting_fields: # Create a list of sorting parameters. Each parameter is a tuple: (field:str, descending:bool) self._sorting_fields = [ (self.sorting_fields_map.get(field.lstrip('-'), field.lstrip('-')), field[0] == '-') for field in self._sorting_fields ]
0, module; 1, function_definition; 2, function_name:JSONList; 3, parameters; 4, block; 5, list_splat_pattern; 6, dictionary_splat_pattern; 7, expression_statement; 8, expression_statement; 9, try_statement; 10, return_statement; 11, identifier:args; 12, identifier:kwargs; 13, comment:"""Stores a list as JSON on database, with mutability support. If kwargs has a param `unique_sorted` (which evaluated to True), list values are made unique and sorted. """; 14, assignment; 15, block; 16, except_clause; 17, call; 18, identifier:type_; 19, identifier:JSON; 20, if_statement; 21, identifier:KeyError; 22, block; 23, attribute; 24, argument_list; 25, call; 26, block; 27, pass_statement; 28, identifier:MutationList; 29, identifier:as_mutable; 30, call; 31, attribute; 32, argument_list; 33, expression_statement; 34, identifier:type_; 35, argument_list; 36, identifier:kwargs; 37, identifier:pop; 38, string:"unique_sorted"; 39, assignment; 40, list_splat; 41, dictionary_splat; 42, identifier:type_; 43, identifier:JSONUniqueListType; 44, identifier:args; 45, identifier:kwargs
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 5, 11; 6, 12; 7, 13; 8, 14; 9, 15; 9, 16; 10, 17; 14, 18; 14, 19; 15, 20; 16, 21; 16, 22; 17, 23; 17, 24; 20, 25; 20, 26; 22, 27; 23, 28; 23, 29; 24, 30; 25, 31; 25, 32; 26, 33; 30, 34; 30, 35; 31, 36; 31, 37; 32, 38; 33, 39; 35, 40; 35, 41; 39, 42; 39, 43; 40, 44; 41, 45
def JSONList(*args, **kwargs): """Stores a list as JSON on database, with mutability support. If kwargs has a param `unique_sorted` (which evaluated to True), list values are made unique and sorted. """ type_ = JSON try: if kwargs.pop("unique_sorted"): type_ = JSONUniqueListType except KeyError: pass return MutationList.as_mutable(type_(*args, **kwargs))
0, module; 1, function_definition; 2, function_name:setup_coords; 3, parameters; 4, block; 5, default_parameter; 6, default_parameter; 7, default_parameter; 8, dictionary_splat_pattern; 9, expression_statement; 10, try_statement; 11, if_statement; 12, expression_statement; 13, for_statement; 14, expression_statement; 15, if_statement; 16, for_statement; 17, return_statement; 18, identifier:arr_names; 19, None; 20, identifier:sort; 21, list; 22, identifier:dims; 23, dictionary; 24, identifier:kwargs; 25, comment:""" Sets up the arr_names dictionary for the plot Parameters ---------- arr_names: string, list of strings or dictionary Set the unique array names of the resulting arrays and (optionally) dimensions. - if string: same as list of strings (see below). Strings may include {0} which will be replaced by a counter. - list of strings: those will be used for the array names. The final number of dictionaries in the return depend in this case on the `dims` and ``**furtherdims`` - dictionary: Then nothing happens and an :class:`OrderedDict` version of `arr_names` is returned. sort: list of strings This parameter defines how the dictionaries are ordered. It has no effect if `arr_names` is a dictionary (use a :class:`~collections.OrderedDict` for that). It can be a list of dimension strings matching to the dimensions in `dims` for the variable. dims: dict Keys must be variable names of dimensions (e.g. time, level, lat or lon) or 'name' for the variable name you want to choose. Values must be values of that dimension or iterables of the values (e.g. lists). Note that strings will be put into a list. For example dims = {'name': 't2m', 'time': 0} will result in one plot for the first time step, whereas dims = {'name': 't2m', 'time': [0, 1]} will result in two plots, one for the first (time == 0) and one for the second (time == 1) time step. ``**kwargs`` The same as `dims` (those will update what is specified in `dims`) Returns ------- ~collections.OrderedDict A mapping from the keys in `arr_names` and to dictionaries. Each dictionary corresponds defines the coordinates of one data array to load"""; 26, block; 27, except_clause; 28, comparison_operator:arr_names is None; 29, block; 30, elif_clause; 31, assignment; 32, pattern_list; 33, call; 34, block; 35, assignment; 36, identifier:sort; 37, block; 38, else_clause; 39, pattern_list; 40, call; 41, block; 42, call; 43, return_statement; 44, tuple; 45, comment:# ValueError for cyordereddict, TypeError for collections.OrderedDict; 46, block; 47, identifier:arr_names; 48, None; 49, expression_statement; 50, call; 51, block; 52, identifier:dims; 53, call; 54, identifier:key; 55, identifier:val; 56, attribute; 57, argument_list; 58, expression_statement; 59, identifier:sorted_dims; 60, call; 61, for_statement; 62, for_statement; 63, comment:# make sure, it is first sorted for the variable names; 64, block; 65, identifier:key; 66, identifier:val; 67, attribute; 68, argument_list; 69, expression_statement; 70, identifier:OrderedDict; 71, argument_list; 72, call; 73, identifier:ValueError; 74, identifier:TypeError; 75, pass_statement; 76, assignment; 77, identifier:isstring; 78, argument_list; 79, expression_statement; 80, identifier:OrderedDict; 81, argument_list; 82, identifier:six; 83, identifier:iteritems; 84, identifier:kwargs; 85, call; 86, identifier:OrderedDict; 87, argument_list; 88, identifier:key; 89, identifier:sort; 90, block; 91, pattern_list; 92, call; 93, block; 94, if_statement; 95, for_statement; 96, for_statement; 97, identifier:six; 98, identifier:iteritems; 99, identifier:sorted_dims; 100, assignment; 101, list_comprehension; 102, identifier:OrderedDict; 103, argument_list; 104, identifier:arr_names; 105, call; 106, identifier:arr_names; 107, assignment; 108, identifier:dims; 109, attribute; 110, argument_list; 111, expression_statement; 112, identifier:key; 113, identifier:val; 114, attribute; 115, argument_list; 116, expression_statement; 117, comparison_operator:'name' in dims; 118, block; 119, pattern_list; 120, call; 121, block; 122, pattern_list; 123, call; 124, block; 125, subscript; 126, call; 127, tuple; 128, for_in_clause; 129, identifier:arr_names; 130, identifier:repeat; 131, argument_list; 132, identifier:arr_names; 133, call; 134, identifier:dims; 135, identifier:setdefault; 136, identifier:key; 137, identifier:val; 138, assignment; 139, identifier:six; 140, identifier:iteritems; 141, identifier:dims; 142, assignment; 143, string; 144, identifier:dims; 145, expression_statement; 146, identifier:key; 147, identifier:val; 148, identifier:sorted; 149, argument_list; 150, expression_statement; 151, identifier:key; 152, identifier:val; 153, attribute; 154, argument_list; 155, expression_statement; 156, identifier:sorted_dims; 157, identifier:key; 158, identifier:iter; 159, argument_list; 160, call; 161, call; 162, pattern_list; 163, call; 164, string; 165, identifier:repeat; 166, argument_list; 167, subscript; 168, call; 169, subscript; 170, identifier:val; 171, string_content:name; 172, assignment; 173, call; 174, assignment; 175, identifier:six; 176, identifier:iteritems; 177, identifier:kwargs; 178, call; 179, call; 180, attribute; 181, argument_list; 182, identifier:dict; 183, argument_list; 184, identifier:i; 185, tuple_pattern; 186, identifier:enumerate; 187, argument_list; 188, string_content:arr{0}; 189, identifier:arr_names; 190, identifier:sorted_dims; 191, identifier:key; 192, attribute; 193, argument_list; 194, identifier:sorted_dims; 195, identifier:key; 196, subscript; 197, None; 198, attribute; 199, argument_list; 200, subscript; 201, identifier:val; 202, attribute; 203, argument_list; 204, identifier:safe_list; 205, argument_list; 206, identifier:arr_name; 207, identifier:format; 208, identifier:i; 209, call; 210, identifier:arr_name; 211, identifier:dim_tuple; 212, call; 213, identifier:dims; 214, identifier:pop; 215, identifier:key; 216, identifier:sorted_dims; 217, string; 218, identifier:dims; 219, identifier:items; 220, identifier:sorted_dims; 221, identifier:key; 222, identifier:sorted_dims; 223, identifier:setdefault; 224, identifier:key; 225, identifier:val; 226, identifier:val; 227, identifier:zip; 228, argument_list; 229, identifier:zip; 230, argument_list; 231, string_content:name; 232, call; 233, identifier:dim_tuple; 234, identifier:arr_names; 235, call; 236, attribute; 237, argument_list; 238, identifier:product; 239, argument_list; 240, identifier:sorted_dims; 241, identifier:keys; 242, list_splat; 243, call; 244, identifier:map; 245, argument_list; 246, identifier:list; 247, call; 248, attribute; 249, argument_list; 250, identifier:sorted_dims; 251, identifier:values
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 5, 18; 5, 19; 6, 20; 6, 21; 7, 22; 7, 23; 8, 24; 9, 25; 10, 26; 10, 27; 11, 28; 11, 29; 11, 30; 12, 31; 13, 32; 13, 33; 13, 34; 14, 35; 15, 36; 15, 37; 15, 38; 16, 39; 16, 40; 16, 41; 17, 42; 26, 43; 27, 44; 27, 45; 27, 46; 28, 47; 28, 48; 29, 49; 30, 50; 30, 51; 31, 52; 31, 53; 32, 54; 32, 55; 33, 56; 33, 57; 34, 58; 35, 59; 35, 60; 37, 61; 37, 62; 38, 63; 38, 64; 39, 65; 39, 66; 40, 67; 40, 68; 41, 69; 42, 70; 42, 71; 43, 72; 44, 73; 44, 74; 46, 75; 49, 76; 50, 77; 50, 78; 51, 79; 53, 80; 53, 81; 56, 82; 56, 83; 57, 84; 58, 85; 60, 86; 60, 87; 61, 88; 61, 89; 61, 90; 62, 91; 62, 92; 62, 93; 64, 94; 64, 95; 64, 96; 67, 97; 67, 98; 68, 99; 69, 100; 71, 101; 72, 102; 72, 103; 76, 104; 76, 105; 78, 106; 79, 107; 81, 108; 85, 109; 85, 110; 90, 111; 91, 112; 91, 113; 92, 114; 92, 115; 93, 116; 94, 117; 94, 118; 95, 119; 95, 120; 95, 121; 96, 122; 96, 123; 96, 124; 100, 125; 100, 126; 101, 127; 101, 128; 103, 129; 105, 130; 105, 131; 107, 132; 107, 133; 109, 134; 109, 135; 110, 136; 110, 137; 111, 138; 114, 139; 114, 140; 115, 141; 116, 142; 117, 143; 117, 144; 118, 145; 119, 146; 119, 147; 120, 148; 120, 149; 121, 150; 122, 151; 122, 152; 123, 153; 123, 154; 124, 155; 125, 156; 125, 157; 126, 158; 126, 159; 127, 160; 127, 161; 128, 162; 128, 163; 131, 164; 133, 165; 133, 166; 138, 167; 138, 168; 142, 169; 142, 170; 143, 171; 145, 172; 149, 173; 150, 174; 153, 175; 153, 176; 154, 177; 155, 178; 159, 179; 160, 180; 160, 181; 161, 182; 161, 183; 162, 184; 162, 185; 163, 186; 163, 187; 164, 188; 166, 189; 167, 190; 167, 191; 168, 192; 168, 193; 169, 194; 169, 195; 172, 196; 172, 197; 173, 198; 173, 199; 174, 200; 174, 201; 178, 202; 178, 203; 179, 204; 179, 205; 180, 206; 180, 207; 181, 208; 183, 209; 185, 210; 185, 211; 187, 212; 192, 213; 192, 214; 193, 215; 196, 216; 196, 217; 198, 218; 198, 219; 200, 220; 200, 221; 202, 222; 202, 223; 203, 224; 203, 225; 205, 226; 209, 227; 209, 228; 212, 229; 212, 230; 217, 231; 228, 232; 228, 233; 230, 234; 230, 235; 232, 236; 232, 237; 235, 238; 235, 239; 236, 240; 236, 241; 239, 242; 242, 243; 243, 244; 243, 245; 245, 246; 245, 247; 247, 248; 247, 249; 248, 250; 248, 251
def setup_coords(arr_names=None, sort=[], dims={}, **kwargs): """ Sets up the arr_names dictionary for the plot Parameters ---------- arr_names: string, list of strings or dictionary Set the unique array names of the resulting arrays and (optionally) dimensions. - if string: same as list of strings (see below). Strings may include {0} which will be replaced by a counter. - list of strings: those will be used for the array names. The final number of dictionaries in the return depend in this case on the `dims` and ``**furtherdims`` - dictionary: Then nothing happens and an :class:`OrderedDict` version of `arr_names` is returned. sort: list of strings This parameter defines how the dictionaries are ordered. It has no effect if `arr_names` is a dictionary (use a :class:`~collections.OrderedDict` for that). It can be a list of dimension strings matching to the dimensions in `dims` for the variable. dims: dict Keys must be variable names of dimensions (e.g. time, level, lat or lon) or 'name' for the variable name you want to choose. Values must be values of that dimension or iterables of the values (e.g. lists). Note that strings will be put into a list. For example dims = {'name': 't2m', 'time': 0} will result in one plot for the first time step, whereas dims = {'name': 't2m', 'time': [0, 1]} will result in two plots, one for the first (time == 0) and one for the second (time == 1) time step. ``**kwargs`` The same as `dims` (those will update what is specified in `dims`) Returns ------- ~collections.OrderedDict A mapping from the keys in `arr_names` and to dictionaries. Each dictionary corresponds defines the coordinates of one data array to load""" try: return OrderedDict(arr_names) except (ValueError, TypeError): # ValueError for cyordereddict, TypeError for collections.OrderedDict pass if arr_names is None: arr_names = repeat('arr{0}') elif isstring(arr_names): arr_names = repeat(arr_names) dims = OrderedDict(dims) for key, val in six.iteritems(kwargs): dims.setdefault(key, val) sorted_dims = OrderedDict() if sort: for key in sort: sorted_dims[key] = dims.pop(key) for key, val in six.iteritems(dims): sorted_dims[key] = val else: # make sure, it is first sorted for the variable names if 'name' in dims: sorted_dims['name'] = None for key, val in sorted(dims.items()): sorted_dims[key] = val for key, val in six.iteritems(kwargs): sorted_dims.setdefault(key, val) for key, val in six.iteritems(sorted_dims): sorted_dims[key] = iter(safe_list(val)) return OrderedDict([ (arr_name.format(i), dict(zip(sorted_dims.keys(), dim_tuple))) for i, (arr_name, dim_tuple) in enumerate(zip( arr_names, product( *map(list, sorted_dims.values()))))])
0, module; 1, function_definition; 2, function_name:get_tdata; 3, parameters; 4, block; 5, identifier:t_format; 6, identifier:files; 7, expression_statement; 8, function_definition; 9, import_statement; 10, import_from_statement; 11, expression_statement; 12, for_statement; 13, expression_statement; 14, expression_statement; 15, for_statement; 16, expression_statement; 17, comment:# sort according to time; 18, expression_statement; 19, expression_statement; 20, return_statement; 21, comment:""" Get the time information from file names Parameters ---------- t_format: str The string that can be used to get the time information in the files. Any numeric datetime format string (e.g. %Y, %m, %H) can be used, but not non-numeric strings like %b, etc. See [1]_ for the datetime format strings files: list of str The that contain the time informations Returns ------- pandas.Index The time coordinate list of str The file names as they are sorten in the returned index References ---------- .. [1] https://docs.python.org/2/library/datetime.html"""; 22, function_name:median; 23, parameters; 24, block; 25, dotted_name; 26, dotted_name; 27, dotted_name; 28, assignment; 29, pattern_list; 30, call; 31, block; 32, assignment; 33, assignment; 34, pattern_list; 35, call; 36, block; 37, assignment; 38, assignment; 39, assignment; 40, expression_list; 41, identifier:arr; 42, return_statement; 43, identifier:re; 44, identifier:pandas; 45, identifier:Index; 46, identifier:t_pattern; 47, identifier:t_format; 48, identifier:fmt; 49, identifier:patt; 50, attribute; 51, argument_list; 52, expression_statement; 53, identifier:t_pattern; 54, call; 55, identifier:time; 56, call; 57, identifier:i; 58, identifier:f; 59, identifier:enumerate; 60, argument_list; 61, expression_statement; 62, identifier:ind; 63, call; 64, identifier:files; 65, subscript; 66, identifier:time; 67, subscript; 68, call; 69, identifier:files; 70, binary_operator:arr.min() + (arr.max() - arr.min())/2; 71, identifier:t_patterns; 72, identifier:items; 73, assignment; 74, attribute; 75, argument_list; 76, identifier:list; 77, argument_list; 78, identifier:files; 79, assignment; 80, attribute; 81, argument_list; 82, call; 83, identifier:ind; 84, call; 85, identifier:ind; 86, identifier:to_datetime; 87, argument_list; 88, call; 89, binary_operator:(arr.max() - arr.min())/2; 90, identifier:t_pattern; 91, call; 92, identifier:re; 93, identifier:compile; 94, identifier:t_pattern; 95, call; 96, subscript; 97, call; 98, identifier:np; 99, identifier:argsort; 100, identifier:time; 101, attribute; 102, argument_list; 103, attribute; 104, argument_list; 105, call; 106, attribute; 107, argument_list; 108, parenthesized_expression; 109, integer:2; 110, attribute; 111, argument_list; 112, identifier:range; 113, argument_list; 114, identifier:time; 115, identifier:i; 116, identifier:median; 117, argument_list; 118, identifier:np; 119, identifier:array; 120, identifier:files; 121, identifier:np; 122, identifier:array; 123, identifier:time; 124, identifier:Index; 125, argument_list; 126, identifier:arr; 127, identifier:min; 128, binary_operator:arr.max() - arr.min(); 129, identifier:t_pattern; 130, identifier:replace; 131, identifier:fmt; 132, identifier:patt; 133, call; 134, call; 135, identifier:time; 136, keyword_argument; 137, call; 138, call; 139, identifier:len; 140, argument_list; 141, attribute; 142, argument_list; 143, identifier:name; 144, string; 145, attribute; 146, argument_list; 147, attribute; 148, argument_list; 149, identifier:files; 150, identifier:np; 151, identifier:array; 152, call; 153, string_content:time; 154, identifier:arr; 155, identifier:max; 156, identifier:arr; 157, identifier:min; 158, identifier:list; 159, argument_list; 160, call; 161, identifier:map; 162, argument_list; 163, lambda; 164, call; 165, lambda_parameters; 166, call; 167, attribute; 168, argument_list; 169, identifier:s; 170, attribute; 171, argument_list; 172, identifier:t_pattern; 173, identifier:findall; 174, identifier:f; 175, identifier:np; 176, identifier:datetime64; 177, call; 178, attribute; 179, argument_list; 180, attribute; 181, identifier:strptime; 182, identifier:s; 183, identifier:t_format; 184, identifier:dt; 185, identifier:datetime
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 7, 21; 8, 22; 8, 23; 8, 24; 9, 25; 10, 26; 10, 27; 11, 28; 12, 29; 12, 30; 12, 31; 13, 32; 14, 33; 15, 34; 15, 35; 15, 36; 16, 37; 18, 38; 19, 39; 20, 40; 23, 41; 24, 42; 25, 43; 26, 44; 27, 45; 28, 46; 28, 47; 29, 48; 29, 49; 30, 50; 30, 51; 31, 52; 32, 53; 32, 54; 33, 55; 33, 56; 34, 57; 34, 58; 35, 59; 35, 60; 36, 61; 37, 62; 37, 63; 38, 64; 38, 65; 39, 66; 39, 67; 40, 68; 40, 69; 42, 70; 50, 71; 50, 72; 52, 73; 54, 74; 54, 75; 56, 76; 56, 77; 60, 78; 61, 79; 63, 80; 63, 81; 65, 82; 65, 83; 67, 84; 67, 85; 68, 86; 68, 87; 70, 88; 70, 89; 73, 90; 73, 91; 74, 92; 74, 93; 75, 94; 77, 95; 79, 96; 79, 97; 80, 98; 80, 99; 81, 100; 82, 101; 82, 102; 84, 103; 84, 104; 87, 105; 88, 106; 88, 107; 89, 108; 89, 109; 91, 110; 91, 111; 95, 112; 95, 113; 96, 114; 96, 115; 97, 116; 97, 117; 101, 118; 101, 119; 102, 120; 103, 121; 103, 122; 104, 123; 105, 124; 105, 125; 106, 126; 106, 127; 108, 128; 110, 129; 110, 130; 111, 131; 111, 132; 113, 133; 117, 134; 125, 135; 125, 136; 128, 137; 128, 138; 133, 139; 133, 140; 134, 141; 134, 142; 136, 143; 136, 144; 137, 145; 137, 146; 138, 147; 138, 148; 140, 149; 141, 150; 141, 151; 142, 152; 144, 153; 145, 154; 145, 155; 147, 156; 147, 157; 152, 158; 152, 159; 159, 160; 160, 161; 160, 162; 162, 163; 162, 164; 163, 165; 163, 166; 164, 167; 164, 168; 165, 169; 166, 170; 166, 171; 167, 172; 167, 173; 168, 174; 170, 175; 170, 176; 171, 177; 177, 178; 177, 179; 178, 180; 178, 181; 179, 182; 179, 183; 180, 184; 180, 185
def get_tdata(t_format, files): """ Get the time information from file names Parameters ---------- t_format: str The string that can be used to get the time information in the files. Any numeric datetime format string (e.g. %Y, %m, %H) can be used, but not non-numeric strings like %b, etc. See [1]_ for the datetime format strings files: list of str The that contain the time informations Returns ------- pandas.Index The time coordinate list of str The file names as they are sorten in the returned index References ---------- .. [1] https://docs.python.org/2/library/datetime.html""" def median(arr): return arr.min() + (arr.max() - arr.min())/2 import re from pandas import Index t_pattern = t_format for fmt, patt in t_patterns.items(): t_pattern = t_pattern.replace(fmt, patt) t_pattern = re.compile(t_pattern) time = list(range(len(files))) for i, f in enumerate(files): time[i] = median(np.array(list(map( lambda s: np.datetime64(dt.datetime.strptime(s, t_format)), t_pattern.findall(f))))) ind = np.argsort(time) # sort according to time files = np.array(files)[ind] time = np.array(time)[ind] return to_datetime(Index(time, name='time')), files
0, module; 1, function_definition; 2, function_name:_set_and_filter; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, for_statement; 11, expression_statement; 12, for_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, return_statement; 17, comment:"""Filters the registered updates and sort out what is not needed This method filters out the formatoptions that have not changed, sets the new value and returns an iterable that is sorted by the priority (highest priority comes first) and dependencies Returns ------- list list of :class:`Formatoption` objects that have to be updated"""; 18, assignment; 19, assignment; 20, identifier:key; 21, attribute; 22, block; 23, pattern_list; 24, call; 25, block; 26, assignment; 27, identifier:fmto; 28, identifier:fmtos; 29, block; 30, assignment; 31, call; 32, call; 33, identifier:fmtos; 34, identifier:fmtos; 35, list; 36, identifier:seen; 37, call; 38, identifier:self; 39, identifier:_force; 40, expression_statement; 41, identifier:key; 42, identifier:value; 43, identifier:chain; 44, argument_list; 45, if_statement; 46, expression_statement; 47, expression_statement; 48, comment:# if the key is shared, a warning will be printed as long as; 49, comment:# this plotter is not also updating (for example due to a whole; 50, comment:# project update); 51, if_statement; 52, expression_statement; 53, if_statement; 54, identifier:fmtos; 55, call; 56, expression_statement; 57, attribute; 58, False; 59, attribute; 60, argument_list; 61, attribute; 62, argument_list; 63, identifier:set; 64, argument_list; 65, call; 66, call; 67, conditional_expression:six.iteritems( {key: getattr(self, key).default for key in self}) if self._todefault else (); 68, comparison_operator:key in seen; 69, block; 70, call; 71, assignment; 72, boolean_operator; 73, block; 74, else_clause; 75, assignment; 76, identifier:changed; 77, block; 78, attribute; 79, argument_list; 80, call; 81, identifier:self; 82, identifier:_todefault; 83, attribute; 84, identifier:clear; 85, attribute; 86, identifier:clear; 87, attribute; 88, argument_list; 89, attribute; 90, argument_list; 91, call; 92, attribute; 93, tuple; 94, identifier:key; 95, identifier:seen; 96, continue_statement; 97, attribute; 98, argument_list; 99, identifier:fmto; 100, call; 101, comparison_operator:key in self._shared; 102, comparison_operator:key not in self._force; 103, if_statement; 104, expression_statement; 105, block; 106, identifier:changed; 107, boolean_operator; 108, expression_statement; 109, identifier:self; 110, identifier:_insert_additionals; 111, identifier:fmtos; 112, identifier:seen; 113, attribute; 114, argument_list; 115, identifier:self; 116, identifier:_registered_updates; 117, identifier:self; 118, identifier:_force; 119, attribute; 120, identifier:setdefault; 121, identifier:key; 122, attribute; 123, identifier:six; 124, identifier:iteritems; 125, attribute; 126, attribute; 127, argument_list; 128, identifier:self; 129, identifier:_todefault; 130, identifier:seen; 131, identifier:add; 132, identifier:key; 133, identifier:getattr; 134, argument_list; 135, identifier:key; 136, attribute; 137, identifier:key; 138, attribute; 139, not_operator; 140, block; 141, assignment; 142, try_statement; 143, identifier:changed; 144, comparison_operator:key in self._force; 145, call; 146, attribute; 147, identifier:acquire; 148, identifier:self; 149, identifier:_registered_updates; 150, call; 151, identifier:value; 152, identifier:self; 153, identifier:_registered_updates; 154, identifier:six; 155, identifier:iteritems; 156, dictionary_comprehension; 157, identifier:self; 158, identifier:key; 159, identifier:self; 160, identifier:_shared; 161, identifier:self; 162, identifier:_force; 163, attribute; 164, expression_statement; 165, identifier:changed; 166, False; 167, block; 168, except_clause; 169, identifier:key; 170, attribute; 171, attribute; 172, argument_list; 173, identifier:fmto; 174, identifier:lock; 175, identifier:getattr; 176, argument_list; 177, pair; 178, for_in_clause; 179, attribute; 180, identifier:_updating; 181, call; 182, expression_statement; 183, as_pattern; 184, block; 185, identifier:self; 186, identifier:_force; 187, identifier:fmtos; 188, identifier:append; 189, identifier:fmto; 190, identifier:self; 191, identifier:key; 192, identifier:key; 193, attribute; 194, identifier:key; 195, identifier:self; 196, subscript; 197, identifier:plotter; 198, identifier:warn; 199, argument_list; 200, assignment; 201, identifier:Exception; 202, as_pattern_target; 203, expression_statement; 204, expression_statement; 205, raise_statement; 206, call; 207, identifier:default; 208, attribute; 209, identifier:key; 210, binary_operator:("%s formatoption is shared with another plotter." " Use the unshare method to enable the updating") % ( fmto.key); 211, keyword_argument; 212, identifier:changed; 213, call; 214, identifier:e; 215, call; 216, call; 217, identifier:e; 218, identifier:getattr; 219, argument_list; 220, identifier:self; 221, identifier:_shared; 222, parenthesized_expression; 223, parenthesized_expression; 224, identifier:logger; 225, attribute; 226, attribute; 227, argument_list; 228, attribute; 229, argument_list; 230, attribute; 231, argument_list; 232, identifier:self; 233, identifier:key; 234, concatenated_string; 235, attribute; 236, identifier:self; 237, identifier:logger; 238, identifier:fmto; 239, identifier:check_and_set; 240, identifier:value; 241, keyword_argument; 242, keyword_argument; 243, attribute; 244, identifier:pop; 245, identifier:key; 246, None; 247, attribute; 248, identifier:debug; 249, string; 250, identifier:key; 251, string:"%s formatoption is shared with another plotter."; 252, string:" Use the unshare method to enable the updating"; 253, identifier:fmto; 254, identifier:key; 255, identifier:todefault; 256, attribute; 257, identifier:validate; 258, not_operator; 259, identifier:self; 260, identifier:_registered_updates; 261, identifier:self; 262, identifier:logger; 263, string_content:Failed to set %s; 264, identifier:self; 265, identifier:_todefault; 266, attribute; 267, identifier:self; 268, identifier:no_validation
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 6, 17; 7, 18; 8, 19; 9, 20; 9, 21; 9, 22; 10, 23; 10, 24; 10, 25; 11, 26; 12, 27; 12, 28; 12, 29; 13, 30; 14, 31; 15, 32; 16, 33; 18, 34; 18, 35; 19, 36; 19, 37; 21, 38; 21, 39; 22, 40; 23, 41; 23, 42; 24, 43; 24, 44; 25, 45; 25, 46; 25, 47; 25, 48; 25, 49; 25, 50; 25, 51; 25, 52; 25, 53; 26, 54; 26, 55; 29, 56; 30, 57; 30, 58; 31, 59; 31, 60; 32, 61; 32, 62; 37, 63; 37, 64; 40, 65; 44, 66; 44, 67; 45, 68; 45, 69; 46, 70; 47, 71; 51, 72; 51, 73; 51, 74; 52, 75; 53, 76; 53, 77; 55, 78; 55, 79; 56, 80; 57, 81; 57, 82; 59, 83; 59, 84; 61, 85; 61, 86; 65, 87; 65, 88; 66, 89; 66, 90; 67, 91; 67, 92; 67, 93; 68, 94; 68, 95; 69, 96; 70, 97; 70, 98; 71, 99; 71, 100; 72, 101; 72, 102; 73, 103; 73, 104; 74, 105; 75, 106; 75, 107; 77, 108; 78, 109; 78, 110; 79, 111; 79, 112; 80, 113; 80, 114; 83, 115; 83, 116; 85, 117; 85, 118; 87, 119; 87, 120; 88, 121; 88, 122; 89, 123; 89, 124; 90, 125; 91, 126; 91, 127; 92, 128; 92, 129; 97, 130; 97, 131; 98, 132; 100, 133; 100, 134; 101, 135; 101, 136; 102, 137; 102, 138; 103, 139; 103, 140; 104, 141; 105, 142; 107, 143; 107, 144; 108, 145; 113, 146; 113, 147; 119, 148; 119, 149; 122, 150; 122, 151; 125, 152; 125, 153; 126, 154; 126, 155; 127, 156; 134, 157; 134, 158; 136, 159; 136, 160; 138, 161; 138, 162; 139, 163; 140, 164; 141, 165; 141, 166; 142, 167; 142, 168; 144, 169; 144, 170; 145, 171; 145, 172; 146, 173; 146, 174; 150, 175; 150, 176; 156, 177; 156, 178; 163, 179; 163, 180; 164, 181; 167, 182; 168, 183; 168, 184; 170, 185; 170, 186; 171, 187; 171, 188; 172, 189; 176, 190; 176, 191; 177, 192; 177, 193; 178, 194; 178, 195; 179, 196; 179, 197; 181, 198; 181, 199; 182, 200; 183, 201; 183, 202; 184, 203; 184, 204; 184, 205; 193, 206; 193, 207; 196, 208; 196, 209; 199, 210; 199, 211; 200, 212; 200, 213; 202, 214; 203, 215; 204, 216; 205, 217; 206, 218; 206, 219; 208, 220; 208, 221; 210, 222; 210, 223; 211, 224; 211, 225; 213, 226; 213, 227; 215, 228; 215, 229; 216, 230; 216, 231; 219, 232; 219, 233; 222, 234; 223, 235; 225, 236; 225, 237; 226, 238; 226, 239; 227, 240; 227, 241; 227, 242; 228, 243; 228, 244; 229, 245; 229, 246; 230, 247; 230, 248; 231, 249; 231, 250; 234, 251; 234, 252; 235, 253; 235, 254; 241, 255; 241, 256; 242, 257; 242, 258; 243, 259; 243, 260; 247, 261; 247, 262; 249, 263; 256, 264; 256, 265; 258, 266; 266, 267; 266, 268
def _set_and_filter(self): """Filters the registered updates and sort out what is not needed This method filters out the formatoptions that have not changed, sets the new value and returns an iterable that is sorted by the priority (highest priority comes first) and dependencies Returns ------- list list of :class:`Formatoption` objects that have to be updated""" fmtos = [] seen = set() for key in self._force: self._registered_updates.setdefault(key, getattr(self, key).value) for key, value in chain( six.iteritems(self._registered_updates), six.iteritems( {key: getattr(self, key).default for key in self}) if self._todefault else ()): if key in seen: continue seen.add(key) fmto = getattr(self, key) # if the key is shared, a warning will be printed as long as # this plotter is not also updating (for example due to a whole # project update) if key in self._shared and key not in self._force: if not self._shared[key].plotter._updating: warn(("%s formatoption is shared with another plotter." " Use the unshare method to enable the updating") % ( fmto.key), logger=self.logger) changed = False else: try: changed = fmto.check_and_set( value, todefault=self._todefault, validate=not self.no_validation) except Exception as e: self._registered_updates.pop(key, None) self.logger.debug('Failed to set %s', key) raise e changed = changed or key in self._force if changed: fmtos.append(fmto) fmtos = self._insert_additionals(fmtos, seen) for fmto in fmtos: fmto.lock.acquire() self._todefault = False self._registered_updates.clear() self._force.clear() return fmtos
0, module; 1, function_definition; 2, function_name:_sorted_by_priority; 3, parameters; 4, block; 5, identifier:self; 6, identifier:fmtos; 7, default_parameter; 8, expression_statement; 9, function_definition; 10, function_definition; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, while_statement; 16, identifier:changed; 17, None; 18, comment:"""Sort the formatoption objects by their priority and dependency Parameters ---------- fmtos: list list of :class:`Formatoption` instances changed: list the list of formatoption keys that have changed Yields ------ Formatoption The next formatoption as it comes by the sorting Warnings -------- The list `fmtos` is cleared by this method!"""; 19, function_name:pop_fmto; 20, parameters; 21, block; 22, function_name:get_children; 23, parameters; 24, block; 25, call; 26, assignment; 27, assignment; 28, call; 29, identifier:fmtos; 30, block; 31, identifier:key; 32, expression_statement; 33, delete_statement; 34, return_statement; 35, identifier:fmto; 36, identifier:parents_keys; 37, expression_statement; 38, for_statement; 39, attribute; 40, argument_list; 41, identifier:fmtos_keys; 42, list_comprehension; 43, attribute; 44, boolean_operator; 45, attribute; 46, argument_list; 47, delete_statement; 48, expression_statement; 49, comment:# first update children; 50, for_statement; 51, comment:# filter out if parent is in update list; 52, if_statement; 53, expression_statement; 54, assignment; 55, subscript; 56, call; 57, assignment; 58, identifier:key; 59, binary_operator:fmto.children + fmto.dependencies; 60, block; 61, identifier:fmtos; 62, identifier:sort; 63, keyword_argument; 64, keyword_argument; 65, attribute; 66, for_in_clause; 67, identifier:self; 68, identifier:_last_update; 69, identifier:changed; 70, subscript; 71, attribute; 72, identifier:debug; 73, string:"Update the formatoptions %s"; 74, identifier:fmtos_keys; 75, subscript; 76, assignment; 77, identifier:child_fmto; 78, call; 79, block; 80, call; 81, block; 82, yield; 83, identifier:idx; 84, call; 85, identifier:fmtos_keys; 86, identifier:idx; 87, attribute; 88, argument_list; 89, identifier:all_fmtos; 90, binary_operator:fmtos_keys + parents_keys; 91, attribute; 92, attribute; 93, if_statement; 94, expression_statement; 95, for_statement; 96, comment:# filter out if parent is in update list; 97, if_statement; 98, expression_statement; 99, identifier:key; 100, lambda; 101, identifier:reverse; 102, True; 103, identifier:fmto; 104, identifier:key; 105, identifier:fmto; 106, identifier:fmtos; 107, identifier:fmtos_keys; 108, slice; 109, identifier:self; 110, identifier:logger; 111, identifier:fmtos_keys; 112, integer:0; 113, identifier:fmto; 114, call; 115, identifier:get_children; 116, argument_list; 117, expression_statement; 118, identifier:any; 119, generator_expression; 120, continue_statement; 121, identifier:fmto; 122, attribute; 123, argument_list; 124, identifier:fmtos; 125, identifier:pop; 126, identifier:idx; 127, identifier:fmtos_keys; 128, identifier:parents_keys; 129, identifier:fmto; 130, identifier:children; 131, identifier:fmto; 132, identifier:dependencies; 133, comparison_operator:key not in fmtos_keys; 134, block; 135, assignment; 136, identifier:childs_child; 137, call; 138, block; 139, parenthesized_expression; 140, block; 141, yield; 142, lambda_parameters; 143, attribute; 144, attribute; 145, argument_list; 146, identifier:fmto; 147, list; 148, yield; 149, comparison_operator:key in fmtos_keys; 150, for_in_clause; 151, identifier:fmtos_keys; 152, identifier:index; 153, identifier:key; 154, identifier:key; 155, identifier:fmtos_keys; 156, continue_statement; 157, identifier:child_fmto; 158, call; 159, identifier:get_children; 160, argument_list; 161, expression_statement; 162, boolean_operator; 163, continue_statement; 164, identifier:child_fmto; 165, identifier:fmto; 166, identifier:fmto; 167, identifier:priority; 168, identifier:fmtos; 169, identifier:pop; 170, integer:0; 171, attribute; 172, identifier:child_fmto; 173, identifier:key; 174, identifier:fmtos_keys; 175, identifier:key; 176, attribute; 177, identifier:pop_fmto; 178, argument_list; 179, identifier:child_fmto; 180, binary_operator:parents_keys + [child_fmto.key]; 181, yield; 182, call; 183, comparison_operator:fmto.key in child_fmto.parents; 184, identifier:fmto; 185, identifier:key; 186, identifier:fmto; 187, identifier:parents; 188, identifier:key; 189, identifier:parents_keys; 190, list; 191, identifier:childs_child; 192, identifier:any; 193, generator_expression; 194, attribute; 195, attribute; 196, attribute; 197, comparison_operator:key in all_fmtos; 198, for_in_clause; 199, identifier:fmto; 200, identifier:key; 201, identifier:child_fmto; 202, identifier:parents; 203, identifier:child_fmto; 204, identifier:key; 205, identifier:key; 206, identifier:all_fmtos; 207, identifier:key; 208, attribute; 209, identifier:child_fmto; 210, identifier:parents
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 7, 16; 7, 17; 8, 18; 9, 19; 9, 20; 9, 21; 10, 22; 10, 23; 10, 24; 11, 25; 12, 26; 13, 27; 14, 28; 15, 29; 15, 30; 20, 31; 21, 32; 21, 33; 21, 34; 23, 35; 23, 36; 24, 37; 24, 38; 25, 39; 25, 40; 26, 41; 26, 42; 27, 43; 27, 44; 28, 45; 28, 46; 30, 47; 30, 48; 30, 49; 30, 50; 30, 51; 30, 52; 30, 53; 32, 54; 33, 55; 34, 56; 37, 57; 38, 58; 38, 59; 38, 60; 39, 61; 39, 62; 40, 63; 40, 64; 42, 65; 42, 66; 43, 67; 43, 68; 44, 69; 44, 70; 45, 71; 45, 72; 46, 73; 46, 74; 47, 75; 48, 76; 50, 77; 50, 78; 50, 79; 52, 80; 52, 81; 53, 82; 54, 83; 54, 84; 55, 85; 55, 86; 56, 87; 56, 88; 57, 89; 57, 90; 59, 91; 59, 92; 60, 93; 60, 94; 60, 95; 60, 96; 60, 97; 60, 98; 63, 99; 63, 100; 64, 101; 64, 102; 65, 103; 65, 104; 66, 105; 66, 106; 70, 107; 70, 108; 71, 109; 71, 110; 75, 111; 75, 112; 76, 113; 76, 114; 78, 115; 78, 116; 79, 117; 80, 118; 80, 119; 81, 120; 82, 121; 84, 122; 84, 123; 87, 124; 87, 125; 88, 126; 90, 127; 90, 128; 91, 129; 91, 130; 92, 131; 92, 132; 93, 133; 93, 134; 94, 135; 95, 136; 95, 137; 95, 138; 97, 139; 97, 140; 98, 141; 100, 142; 100, 143; 114, 144; 114, 145; 116, 146; 116, 147; 117, 148; 119, 149; 119, 150; 122, 151; 122, 152; 123, 153; 133, 154; 133, 155; 134, 156; 135, 157; 135, 158; 137, 159; 137, 160; 138, 161; 139, 162; 140, 163; 141, 164; 142, 165; 143, 166; 143, 167; 144, 168; 144, 169; 145, 170; 147, 171; 148, 172; 149, 173; 149, 174; 150, 175; 150, 176; 158, 177; 158, 178; 160, 179; 160, 180; 161, 181; 162, 182; 162, 183; 171, 184; 171, 185; 176, 186; 176, 187; 178, 188; 180, 189; 180, 190; 181, 191; 182, 192; 182, 193; 183, 194; 183, 195; 190, 196; 193, 197; 193, 198; 194, 199; 194, 200; 195, 201; 195, 202; 196, 203; 196, 204; 197, 205; 197, 206; 198, 207; 198, 208; 208, 209; 208, 210
def _sorted_by_priority(self, fmtos, changed=None): """Sort the formatoption objects by their priority and dependency Parameters ---------- fmtos: list list of :class:`Formatoption` instances changed: list the list of formatoption keys that have changed Yields ------ Formatoption The next formatoption as it comes by the sorting Warnings -------- The list `fmtos` is cleared by this method!""" def pop_fmto(key): idx = fmtos_keys.index(key) del fmtos_keys[idx] return fmtos.pop(idx) def get_children(fmto, parents_keys): all_fmtos = fmtos_keys + parents_keys for key in fmto.children + fmto.dependencies: if key not in fmtos_keys: continue child_fmto = pop_fmto(key) for childs_child in get_children( child_fmto, parents_keys + [child_fmto.key]): yield childs_child # filter out if parent is in update list if (any(key in all_fmtos for key in child_fmto.parents) or fmto.key in child_fmto.parents): continue yield child_fmto fmtos.sort(key=lambda fmto: fmto.priority, reverse=True) fmtos_keys = [fmto.key for fmto in fmtos] self._last_update = changed or fmtos_keys[:] self.logger.debug("Update the formatoptions %s", fmtos_keys) while fmtos: del fmtos_keys[0] fmto = fmtos.pop(0) # first update children for child_fmto in get_children(fmto, [fmto.key]): yield child_fmto # filter out if parent is in update list if any(key in fmtos_keys for key in fmto.parents): continue yield fmto
0, module; 1, function_definition; 2, function_name:sort_kwargs; 3, parameters; 4, block; 5, identifier:kwargs; 6, list_splat_pattern; 7, expression_statement; 8, return_statement; 9, identifier:param_lists; 10, comment:"""Function to sort keyword arguments and sort them into dictionaries This function returns dictionaries that contain the keyword arguments from `kwargs` corresponding given iterables in ``*params`` Parameters ---------- kwargs: dict Original dictionary ``*param_lists`` iterables of strings, each standing for a possible key in kwargs Returns ------- list len(params) + 1 dictionaries. Each dictionary contains the items of `kwargs` corresponding to the specified list in ``*param_lists``. The last dictionary contains the remaining items"""; 11, call; 12, identifier:chain; 13, argument_list; 14, generator_expression; 15, list; 16, dictionary_comprehension; 17, for_in_clause; 18, identifier:kwargs; 19, pair; 20, for_in_clause; 21, identifier:params; 22, call; 23, identifier:key; 24, call; 25, identifier:key; 26, call; 27, identifier:map; 28, argument_list; 29, attribute; 30, argument_list; 31, attribute; 32, argument_list; 33, identifier:set; 34, identifier:param_lists; 35, identifier:kwargs; 36, identifier:pop; 37, identifier:key; 38, identifier:params; 39, identifier:intersection; 40, identifier:kwargs
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 6, 9; 7, 10; 8, 11; 11, 12; 11, 13; 13, 14; 13, 15; 14, 16; 14, 17; 15, 18; 16, 19; 16, 20; 17, 21; 17, 22; 19, 23; 19, 24; 20, 25; 20, 26; 22, 27; 22, 28; 24, 29; 24, 30; 26, 31; 26, 32; 28, 33; 28, 34; 29, 35; 29, 36; 30, 37; 31, 38; 31, 39; 32, 40
def sort_kwargs(kwargs, *param_lists): """Function to sort keyword arguments and sort them into dictionaries This function returns dictionaries that contain the keyword arguments from `kwargs` corresponding given iterables in ``*params`` Parameters ---------- kwargs: dict Original dictionary ``*param_lists`` iterables of strings, each standing for a possible key in kwargs Returns ------- list len(params) + 1 dictionaries. Each dictionary contains the items of `kwargs` corresponding to the specified list in ``*param_lists``. The last dictionary contains the remaining items""" return chain( ({key: kwargs.pop(key) for key in params.intersection(kwargs)} for params in map(set, param_lists)), [kwargs])
0, module; 1, function_definition; 2, function_name:iterkeys; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, for_statement; 12, comment:"""Unsorted iterator over keys"""; 13, assignment; 14, assignment; 15, assignment; 16, identifier:key; 17, call; 18, block; 19, identifier:key; 20, call; 21, block; 22, identifier:patterns; 23, attribute; 24, identifier:replace; 25, attribute; 26, identifier:seen; 27, call; 28, attribute; 29, argument_list; 30, for_statement; 31, attribute; 32, argument_list; 33, if_statement; 34, identifier:self; 35, identifier:patterns; 36, identifier:self; 37, identifier:replace; 38, identifier:set; 39, argument_list; 40, identifier:six; 41, identifier:iterkeys; 42, attribute; 43, identifier:pattern; 44, identifier:patterns; 45, block; 46, identifier:DictMethods; 47, identifier:iterkeys; 48, identifier:self; 49, comparison_operator:key not in seen; 50, block; 51, identifier:self; 52, identifier:base; 53, expression_statement; 54, if_statement; 55, identifier:key; 56, identifier:seen; 57, expression_statement; 58, assignment; 59, identifier:m; 60, block; 61, yield; 62, identifier:m; 63, call; 64, expression_statement; 65, if_statement; 66, break_statement; 67, identifier:key; 68, attribute; 69, argument_list; 70, assignment; 71, comparison_operator:ret not in seen; 72, block; 73, identifier:pattern; 74, identifier:match; 75, identifier:key; 76, identifier:ret; 77, conditional_expression:m.group('key') if replace else m.group(); 78, identifier:ret; 79, identifier:seen; 80, expression_statement; 81, expression_statement; 82, call; 83, identifier:replace; 84, call; 85, call; 86, yield; 87, attribute; 88, argument_list; 89, attribute; 90, argument_list; 91, attribute; 92, argument_list; 93, identifier:ret; 94, identifier:m; 95, identifier:group; 96, string; 97, identifier:m; 98, identifier:group; 99, identifier:seen; 100, identifier:add; 101, identifier:ret; 102, string_content:key
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 6, 12; 7, 13; 8, 14; 9, 15; 10, 16; 10, 17; 10, 18; 11, 19; 11, 20; 11, 21; 13, 22; 13, 23; 14, 24; 14, 25; 15, 26; 15, 27; 17, 28; 17, 29; 18, 30; 20, 31; 20, 32; 21, 33; 23, 34; 23, 35; 25, 36; 25, 37; 27, 38; 27, 39; 28, 40; 28, 41; 29, 42; 30, 43; 30, 44; 30, 45; 31, 46; 31, 47; 32, 48; 33, 49; 33, 50; 42, 51; 42, 52; 45, 53; 45, 54; 49, 55; 49, 56; 50, 57; 53, 58; 54, 59; 54, 60; 57, 61; 58, 62; 58, 63; 60, 64; 60, 65; 60, 66; 61, 67; 63, 68; 63, 69; 64, 70; 65, 71; 65, 72; 68, 73; 68, 74; 69, 75; 70, 76; 70, 77; 71, 78; 71, 79; 72, 80; 72, 81; 77, 82; 77, 83; 77, 84; 80, 85; 81, 86; 82, 87; 82, 88; 84, 89; 84, 90; 85, 91; 85, 92; 86, 93; 87, 94; 87, 95; 88, 96; 89, 97; 89, 98; 91, 99; 91, 100; 92, 101; 96, 102
def iterkeys(self): """Unsorted iterator over keys""" patterns = self.patterns replace = self.replace seen = set() for key in six.iterkeys(self.base): for pattern in patterns: m = pattern.match(key) if m: ret = m.group('key') if replace else m.group() if ret not in seen: seen.add(ret) yield ret break for key in DictMethods.iterkeys(self): if key not in seen: yield key
0, module; 1, function_definition; 2, function_name:keys; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, return_statement; 10, comment:""" Return sorted list of keys. """; 11, assignment; 12, call; 13, identifier:k; 14, identifier:k; 15, call; 16, attribute; 17, argument_list; 18, identifier:list; 19, argument_list; 20, identifier:k; 21, identifier:sort; 22, call; 23, attribute; 24, argument_list; 25, identifier:dict; 26, identifier:keys; 27, identifier:self
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 6, 10; 7, 11; 8, 12; 9, 13; 11, 14; 11, 15; 12, 16; 12, 17; 15, 18; 15, 19; 16, 20; 16, 21; 19, 22; 22, 23; 22, 24; 23, 25; 23, 26; 24, 27
def keys(self): """ Return sorted list of keys. """ k = list(dict.keys(self)) k.sort() return k
0, module; 1, function_definition; 2, function_name:get_reports_page; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, expression_statement; 13, expression_statement; 14, comment:# explicitly compare to True and False to distinguish from None (which is treated as False in a conditional); 15, if_statement; 16, if_statement; 17, expression_statement; 18, expression_statement; 19, expression_statement; 20, comment:# create a Page object from the dict; 21, return_statement; 22, identifier:is_enclave; 23, None; 24, identifier:enclave_ids; 25, None; 26, identifier:tag; 27, None; 28, identifier:excluded_tags; 29, None; 30, identifier:from_time; 31, None; 32, identifier:to_time; 33, None; 34, comment:""" Retrieves a page of reports, filtering by time window, distribution type, enclave association, and tag. The results are sorted by updated time. This method does not take ``page_number`` and ``page_size`` parameters. Instead, each successive page must be found by adjusting the ``from_time`` and ``to_time`` parameters. Note: This endpoint will only return reports from a time window of maximum size of 2 weeks. If you give a time window larger than 2 weeks, it will pull reports starting at 2 weeks before the "to" date, through the "to" date. :param boolean is_enclave: restrict reports to specific distribution type (optional - by default all accessible reports are returned). :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific enclaves (optional - by default reports from all of user's enclaves are returned) :param list(str) tag: Name (or list of names) of tag(s) to filter reports by. Only reports containing ALL of these tags will be returned. :param list(str) excluded_tags: Reports containing ANY of these tags will be excluded from the results. :param int from_time: start of time window in milliseconds since epoch (optional) :param int to_time: end of time window in milliseconds since epoch (optional) :return: A |Page| of |Report| objects. """; 35, assignment; 36, identifier:is_enclave; 37, block; 38, elif_clause; 39, comparison_operator:enclave_ids is None; 40, block; 41, assignment; 42, assignment; 43, assignment; 44, identifier:result; 45, identifier:distribution_type; 46, None; 47, expression_statement; 48, not_operator; 49, block; 50, identifier:enclave_ids; 51, None; 52, expression_statement; 53, identifier:params; 54, dictionary; 55, identifier:resp; 56, call; 57, identifier:result; 58, call; 59, assignment; 60, identifier:is_enclave; 61, expression_statement; 62, assignment; 63, pair; 64, pair; 65, pair; 66, pair; 67, pair; 68, pair; 69, attribute; 70, argument_list; 71, attribute; 72, argument_list; 73, identifier:distribution_type; 74, attribute; 75, assignment; 76, identifier:enclave_ids; 77, attribute; 78, string; 79, identifier:from_time; 80, string; 81, identifier:to_time; 82, string; 83, identifier:distribution_type; 84, string; 85, identifier:enclave_ids; 86, string; 87, identifier:tag; 88, string; 89, identifier:excluded_tags; 90, attribute; 91, identifier:get; 92, string:"reports"; 93, keyword_argument; 94, identifier:Page; 95, identifier:from_dict; 96, call; 97, keyword_argument; 98, identifier:DistributionType; 99, identifier:ENCLAVE; 100, identifier:distribution_type; 101, attribute; 102, identifier:self; 103, identifier:enclave_ids; 104, string_content:from; 105, string_content:to; 106, string_content:distributionType; 107, string_content:enclaveIds; 108, string_content:tags; 109, string_content:excludedTags; 110, identifier:self; 111, identifier:_client; 112, identifier:params; 113, identifier:params; 114, attribute; 115, argument_list; 116, identifier:content_type; 117, identifier:Report; 118, identifier:DistributionType; 119, identifier:COMMUNITY; 120, identifier:resp; 121, identifier:json
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 6, 22; 6, 23; 7, 24; 7, 25; 8, 26; 8, 27; 9, 28; 9, 29; 10, 30; 10, 31; 11, 32; 11, 33; 12, 34; 13, 35; 15, 36; 15, 37; 15, 38; 16, 39; 16, 40; 17, 41; 18, 42; 19, 43; 21, 44; 35, 45; 35, 46; 37, 47; 38, 48; 38, 49; 39, 50; 39, 51; 40, 52; 41, 53; 41, 54; 42, 55; 42, 56; 43, 57; 43, 58; 47, 59; 48, 60; 49, 61; 52, 62; 54, 63; 54, 64; 54, 65; 54, 66; 54, 67; 54, 68; 56, 69; 56, 70; 58, 71; 58, 72; 59, 73; 59, 74; 61, 75; 62, 76; 62, 77; 63, 78; 63, 79; 64, 80; 64, 81; 65, 82; 65, 83; 66, 84; 66, 85; 67, 86; 67, 87; 68, 88; 68, 89; 69, 90; 69, 91; 70, 92; 70, 93; 71, 94; 71, 95; 72, 96; 72, 97; 74, 98; 74, 99; 75, 100; 75, 101; 77, 102; 77, 103; 78, 104; 80, 105; 82, 106; 84, 107; 86, 108; 88, 109; 90, 110; 90, 111; 93, 112; 93, 113; 96, 114; 96, 115; 97, 116; 97, 117; 101, 118; 101, 119; 114, 120; 114, 121
def get_reports_page(self, is_enclave=None, enclave_ids=None, tag=None, excluded_tags=None, from_time=None, to_time=None): """ Retrieves a page of reports, filtering by time window, distribution type, enclave association, and tag. The results are sorted by updated time. This method does not take ``page_number`` and ``page_size`` parameters. Instead, each successive page must be found by adjusting the ``from_time`` and ``to_time`` parameters. Note: This endpoint will only return reports from a time window of maximum size of 2 weeks. If you give a time window larger than 2 weeks, it will pull reports starting at 2 weeks before the "to" date, through the "to" date. :param boolean is_enclave: restrict reports to specific distribution type (optional - by default all accessible reports are returned). :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific enclaves (optional - by default reports from all of user's enclaves are returned) :param list(str) tag: Name (or list of names) of tag(s) to filter reports by. Only reports containing ALL of these tags will be returned. :param list(str) excluded_tags: Reports containing ANY of these tags will be excluded from the results. :param int from_time: start of time window in milliseconds since epoch (optional) :param int to_time: end of time window in milliseconds since epoch (optional) :return: A |Page| of |Report| objects. """ distribution_type = None # explicitly compare to True and False to distinguish from None (which is treated as False in a conditional) if is_enclave: distribution_type = DistributionType.ENCLAVE elif not is_enclave: distribution_type = DistributionType.COMMUNITY if enclave_ids is None: enclave_ids = self.enclave_ids params = { 'from': from_time, 'to': to_time, 'distributionType': distribution_type, 'enclaveIds': enclave_ids, 'tags': tag, 'excludedTags': excluded_tags } resp = self._client.get("reports", params=params) result = Page.from_dict(resp.json(), content_type=Report) # create a Page object from the dict return result
0, module; 1, function_definition; 2, function_name:hashify_targets; 3, parameters; 4, type; 5, block; 6, typed_parameter; 7, identifier:build_context; 8, identifier:list; 9, expression_statement; 10, return_statement; 11, identifier:targets; 12, type; 13, comment:"""Return sorted hashes of `targets`."""; 14, call; 15, identifier:list; 16, identifier:sorted; 17, generator_expression; 18, call; 19, for_in_clause; 20, attribute; 21, argument_list; 22, identifier:target_name; 23, call; 24, subscript; 25, identifier:hash; 26, identifier:build_context; 27, identifier:listify; 28, argument_list; 29, attribute; 30, identifier:target_name; 31, identifier:targets; 32, identifier:build_context; 33, identifier:targets
0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 3, 7; 4, 8; 5, 9; 5, 10; 6, 11; 6, 12; 9, 13; 10, 14; 12, 15; 14, 16; 14, 17; 17, 18; 17, 19; 18, 20; 18, 21; 19, 22; 19, 23; 20, 24; 20, 25; 21, 26; 23, 27; 23, 28; 24, 29; 24, 30; 28, 31; 29, 32; 29, 33
def hashify_targets(targets: list, build_context) -> list: """Return sorted hashes of `targets`.""" return sorted(build_context.targets[target_name].hash(build_context) for target_name in listify(targets))
0, module; 1, function_definition; 2, function_name:stable_reverse_topological_sort; 3, parameters; 4, block; 5, identifier:graph; 6, expression_statement; 7, if_statement; 8, comment:# nonrecursive version; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, comment:"""Return a list of nodes in topological sort order. This topological sort is a **unique** permutation of the nodes such that an edge from u to v implies that u appears before v in the topological sort order. Parameters ---------- graph : NetworkX digraph A directed graph Raises ------ NetworkXError Topological sort is defined for directed graphs only. If the graph G is undirected, a NetworkXError is raised. NetworkXUnfeasible If G is not a directed acyclic graph (DAG) no topological sort exists and a NetworkXUnfeasible exception is raised. Notes ----- - This algorithm is based on a description and proof in The Algorithm Design Manual [1]_ . - This implementation is modified from networkx 1.11 implementation [2]_ to achieve stability, support only reverse (allows yielding instead of returning a list), and remove the `nbunch` argument (had no use for it). See also -------- is_directed_acyclic_graph References ---------- .. [1] Skiena, S. S. The Algorithm Design Manual (Springer-Verlag, 1998). http://www.amazon.com/exec/obidos/ASIN/0387948600/ref=ase_thealgorithmrepo/ .. [2] networkx on GitHub https://github.com/networkx/networkx/blob/8358afac209c00b7feb3e81c901098852a9413b3/networkx/algorithms/dag.py#L88-L168 """; 13, not_operator; 14, block; 15, assignment; 16, assignment; 17, identifier:v; 18, call; 19, block; 20, call; 21, raise_statement; 22, identifier:seen; 23, call; 24, identifier:explored; 25, call; 26, identifier:sorted; 27, argument_list; 28, if_statement; 29, expression_statement; 30, comment:# nodes yet to look at; 31, while_statement; 32, attribute; 33, argument_list; 34, call; 35, identifier:set; 36, argument_list; 37, identifier:set; 38, argument_list; 39, call; 40, comparison_operator:v in explored; 41, block; 42, assignment; 43, identifier:fringe; 44, block; 45, identifier:graph; 46, identifier:is_directed; 47, attribute; 48, argument_list; 49, attribute; 50, argument_list; 51, identifier:v; 52, identifier:explored; 53, continue_statement; 54, identifier:fringe; 55, list; 56, expression_statement; 57, comment:# depth first search; 58, if_statement; 59, expression_statement; 60, comment:# mark as seen; 61, comment:# Check successors for cycles and for new nodes; 62, expression_statement; 63, for_statement; 64, if_statement; 65, identifier:networkx; 66, identifier:NetworkXError; 67, string; 68, identifier:graph; 69, identifier:nodes; 70, identifier:v; 71, assignment; 72, comparison_operator:w in explored; 73, comment:# already looked down this branch; 74, block; 75, call; 76, assignment; 77, identifier:n; 78, call; 79, block; 80, identifier:new_nodes; 81, comment:# Add new_nodes to fringe; 82, block; 83, else_clause; 84, string_content:Topological sort not defined on undirected graphs.; 85, identifier:w; 86, subscript; 87, identifier:w; 88, identifier:explored; 89, expression_statement; 90, continue_statement; 91, attribute; 92, argument_list; 93, identifier:new_nodes; 94, list; 95, identifier:sorted; 96, argument_list; 97, if_statement; 98, expression_statement; 99, comment:# No new nodes so w is fully explored; 100, block; 101, identifier:fringe; 102, unary_operator; 103, call; 104, identifier:seen; 105, identifier:add; 106, identifier:w; 107, subscript; 108, comparison_operator:n not in explored; 109, block; 110, call; 111, expression_statement; 112, expression_statement; 113, expression_statement; 114, integer:1; 115, attribute; 116, argument_list; 117, identifier:graph; 118, identifier:w; 119, identifier:n; 120, identifier:explored; 121, if_statement; 122, expression_statement; 123, attribute; 124, argument_list; 125, call; 126, yield; 127, call; 128, identifier:fringe; 129, identifier:pop; 130, comparison_operator:n in seen; 131, comment:# CYCLE!! OH NOOOO!!; 132, block; 133, call; 134, identifier:fringe; 135, identifier:extend; 136, identifier:new_nodes; 137, attribute; 138, argument_list; 139, identifier:w; 140, attribute; 141, argument_list; 142, identifier:n; 143, identifier:seen; 144, raise_statement; 145, attribute; 146, argument_list; 147, identifier:explored; 148, identifier:add; 149, identifier:w; 150, identifier:fringe; 151, identifier:pop; 152, call; 153, identifier:new_nodes; 154, identifier:append; 155, identifier:n; 156, attribute; 157, argument_list; 158, identifier:networkx; 159, identifier:NetworkXUnfeasible; 160, string; 161, string_content:Graph contains a cycle.
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 6, 12; 7, 13; 7, 14; 9, 15; 10, 16; 11, 17; 11, 18; 11, 19; 13, 20; 14, 21; 15, 22; 15, 23; 16, 24; 16, 25; 18, 26; 18, 27; 19, 28; 19, 29; 19, 30; 19, 31; 20, 32; 20, 33; 21, 34; 23, 35; 23, 36; 25, 37; 25, 38; 27, 39; 28, 40; 28, 41; 29, 42; 31, 43; 31, 44; 32, 45; 32, 46; 34, 47; 34, 48; 39, 49; 39, 50; 40, 51; 40, 52; 41, 53; 42, 54; 42, 55; 44, 56; 44, 57; 44, 58; 44, 59; 44, 60; 44, 61; 44, 62; 44, 63; 44, 64; 47, 65; 47, 66; 48, 67; 49, 68; 49, 69; 55, 70; 56, 71; 58, 72; 58, 73; 58, 74; 59, 75; 62, 76; 63, 77; 63, 78; 63, 79; 64, 80; 64, 81; 64, 82; 64, 83; 67, 84; 71, 85; 71, 86; 72, 87; 72, 88; 74, 89; 74, 90; 75, 91; 75, 92; 76, 93; 76, 94; 78, 95; 78, 96; 79, 97; 82, 98; 83, 99; 83, 100; 86, 101; 86, 102; 89, 103; 91, 104; 91, 105; 92, 106; 96, 107; 97, 108; 97, 109; 98, 110; 100, 111; 100, 112; 100, 113; 102, 114; 103, 115; 103, 116; 107, 117; 107, 118; 108, 119; 108, 120; 109, 121; 109, 122; 110, 123; 110, 124; 111, 125; 112, 126; 113, 127; 115, 128; 115, 129; 121, 130; 121, 131; 121, 132; 122, 133; 123, 134; 123, 135; 124, 136; 125, 137; 125, 138; 126, 139; 127, 140; 127, 141; 130, 142; 130, 143; 132, 144; 133, 145; 133, 146; 137, 147; 137, 148; 138, 149; 140, 150; 140, 151; 144, 152; 145, 153; 145, 154; 146, 155; 152, 156; 152, 157; 156, 158; 156, 159; 157, 160; 160, 161
def stable_reverse_topological_sort(graph): """Return a list of nodes in topological sort order. This topological sort is a **unique** permutation of the nodes such that an edge from u to v implies that u appears before v in the topological sort order. Parameters ---------- graph : NetworkX digraph A directed graph Raises ------ NetworkXError Topological sort is defined for directed graphs only. If the graph G is undirected, a NetworkXError is raised. NetworkXUnfeasible If G is not a directed acyclic graph (DAG) no topological sort exists and a NetworkXUnfeasible exception is raised. Notes ----- - This algorithm is based on a description and proof in The Algorithm Design Manual [1]_ . - This implementation is modified from networkx 1.11 implementation [2]_ to achieve stability, support only reverse (allows yielding instead of returning a list), and remove the `nbunch` argument (had no use for it). See also -------- is_directed_acyclic_graph References ---------- .. [1] Skiena, S. S. The Algorithm Design Manual (Springer-Verlag, 1998). http://www.amazon.com/exec/obidos/ASIN/0387948600/ref=ase_thealgorithmrepo/ .. [2] networkx on GitHub https://github.com/networkx/networkx/blob/8358afac209c00b7feb3e81c901098852a9413b3/networkx/algorithms/dag.py#L88-L168 """ if not graph.is_directed(): raise networkx.NetworkXError( 'Topological sort not defined on undirected graphs.') # nonrecursive version seen = set() explored = set() for v in sorted(graph.nodes()): if v in explored: continue fringe = [v] # nodes yet to look at while fringe: w = fringe[-1] # depth first search if w in explored: # already looked down this branch fringe.pop() continue seen.add(w) # mark as seen # Check successors for cycles and for new nodes new_nodes = [] for n in sorted(graph[w]): if n not in explored: if n in seen: # CYCLE!! OH NOOOO!! raise networkx.NetworkXUnfeasible( 'Graph contains a cycle.') new_nodes.append(n) if new_nodes: # Add new_nodes to fringe fringe.extend(new_nodes) else: # No new nodes so w is fully explored explored.add(w) yield w fringe.pop()
0, module; 1, function_definition; 2, function_name:walk_target_deps_topological_order; 3, parameters; 4, block; 5, identifier:self; 6, typed_parameter; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, identifier:target; 11, type; 12, comment:"""Generate all dependencies of `target` by topological sort order."""; 13, assignment; 14, identifier:dep_name; 15, call; 16, block; 17, identifier:Target; 18, identifier:all_deps; 19, call; 20, identifier:topological_sort; 21, argument_list; 22, if_statement; 23, identifier:get_descendants; 24, argument_list; 25, attribute; 26, comparison_operator:dep_name in all_deps; 27, block; 28, attribute; 29, attribute; 30, identifier:self; 31, identifier:target_graph; 32, identifier:dep_name; 33, identifier:all_deps; 34, expression_statement; 35, identifier:self; 36, identifier:target_graph; 37, identifier:target; 38, identifier:name; 39, yield; 40, subscript; 41, attribute; 42, identifier:dep_name; 43, identifier:self; 44, identifier:targets
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 6, 10; 6, 11; 7, 12; 8, 13; 9, 14; 9, 15; 9, 16; 11, 17; 13, 18; 13, 19; 15, 20; 15, 21; 16, 22; 19, 23; 19, 24; 21, 25; 22, 26; 22, 27; 24, 28; 24, 29; 25, 30; 25, 31; 26, 32; 26, 33; 27, 34; 28, 35; 28, 36; 29, 37; 29, 38; 34, 39; 39, 40; 40, 41; 40, 42; 41, 43; 41, 44
def walk_target_deps_topological_order(self, target: Target): """Generate all dependencies of `target` by topological sort order.""" all_deps = get_descendants(self.target_graph, target.name) for dep_name in topological_sort(self.target_graph): if dep_name in all_deps: yield self.targets[dep_name]
0, module; 1, function_definition; 2, function_name:_print_message; 3, parameters; 4, block; 5, identifier:self; 6, identifier:prefix; 7, identifier:message; 8, default_parameter; 9, expression_statement; 10, comment:# Load up the standard output.; 11, expression_statement; 12, comment:# We have some extra stuff for verbose mode.; 13, if_statement; 14, comment:# Send the final output to the handler to be rendered.; 15, expression_statement; 16, identifier:verbose; 17, True; 18, string; 19, assignment; 20, identifier:verbose; 21, block; 22, call; 23, string_content:Prints a message and takes care of all sorts of nasty code; 24, identifier:output; 25, list; 26, expression_statement; 27, comment:# Detailed problem description.; 28, if_statement; 29, if_statement; 30, if_statement; 31, comment:# Show the user what tier we're on; 32, expression_statement; 33, comment:# If file information is available, output that as well.; 34, expression_statement; 35, if_statement; 36, comment:# If there is a line number, that gets put on the end.; 37, if_statement; 38, if_statement; 39, if_statement; 40, comment:# Stick it in with the standard items.; 41, expression_statement; 42, expression_statement; 43, attribute; 44, argument_list; 45, string; 46, identifier:prefix; 47, subscript; 48, assignment; 49, subscript; 50, block; 51, call; 52, block; 53, call; 54, block; 55, call; 56, assignment; 57, boolean_operator; 58, block; 59, subscript; 60, block; 61, boolean_operator; 62, block; 63, call; 64, block; 65, call; 66, call; 67, attribute; 68, identifier:write; 69, call; 70, string_content; 71, identifier:message; 72, string; 73, identifier:verbose_output; 74, list; 75, identifier:message; 76, string; 77, expression_statement; 78, attribute; 79, argument_list; 80, expression_statement; 81, attribute; 82, argument_list; 83, expression_statement; 84, expression_statement; 85, attribute; 86, argument_list; 87, identifier:files; 88, subscript; 89, comparison_operator:files is not None; 90, comparison_operator:files != ''; 91, expression_statement; 92, comment:# Nested files (subpackes) are stored in a list.; 93, if_statement; 94, identifier:message; 95, string; 96, expression_statement; 97, subscript; 98, comparison_operator:message['column'] != 0; 99, expression_statement; 100, attribute; 101, argument_list; 102, expression_statement; 103, expression_statement; 104, attribute; 105, argument_list; 106, attribute; 107, argument_list; 108, identifier:self; 109, identifier:handler; 110, attribute; 111, argument_list; 112, escape_sequence:\n; 113, string_content:message; 114, string_content:description; 115, call; 116, identifier:message; 117, identifier:get; 118, string; 119, call; 120, identifier:message; 121, identifier:get; 122, string; 123, call; 124, call; 125, identifier:verbose_output; 126, identifier:append; 127, binary_operator:'\tTier:\t%d' % message['tier']; 128, identifier:message; 129, string; 130, identifier:files; 131, None; 132, identifier:files; 133, string; 134, assignment; 135, comparison_operator:type(files) is list; 136, block; 137, else_clause; 138, string_content:line; 139, call; 140, identifier:message; 141, string; 142, subscript; 143, integer:0; 144, call; 145, identifier:message; 146, identifier:get; 147, string; 148, call; 149, call; 150, identifier:output; 151, identifier:append; 152, string; 153, identifier:output; 154, identifier:append; 155, call; 156, string; 157, identifier:join; 158, call; 159, attribute; 160, argument_list; 161, string_content:signing_severity; 162, attribute; 163, argument_list; 164, string_content:signing_help; 165, attribute; 166, argument_list; 167, attribute; 168, argument_list; 169, string; 170, subscript; 171, string_content:file; 172, identifier:fmsg; 173, string; 174, call; 175, identifier:list; 176, if_statement; 177, expression_statement; 178, block; 179, attribute; 180, argument_list; 181, string_content:column; 182, identifier:message; 183, string; 184, attribute; 185, argument_list; 186, string_content:context; 187, attribute; 188, argument_list; 189, attribute; 190, argument_list; 191, string_content; 192, attribute; 193, argument_list; 194, identifier:map; 195, argument_list; 196, identifier:verbose_output; 197, identifier:append; 198, call; 199, identifier:verbose_output; 200, identifier:append; 201, parenthesized_expression; 202, identifier:verbose_output; 203, identifier:append; 204, string; 205, identifier:verbose_output; 206, identifier:append; 207, call; 208, string_content; 209, identifier:message; 210, string; 211, string_content; 212, identifier:type; 213, argument_list; 214, comparison_operator:files[-1] == ''; 215, block; 216, call; 217, expression_statement; 218, identifier:verbose_output; 219, identifier:append; 220, binary_operator:'\tLine:\t%s' % message['line']; 221, string_content:column; 222, identifier:verbose_output; 223, identifier:append; 224, binary_operator:'\tColumn:\t%d' % message['column']; 225, identifier:verbose_output; 226, identifier:append; 227, string; 228, identifier:verbose_output; 229, identifier:extend; 230, list_comprehension; 231, escape_sequence:\n; 232, string; 233, identifier:join; 234, identifier:verbose_output; 235, attribute; 236, identifier:output; 237, attribute; 238, argument_list; 239, binary_operator:'\tAutomated signing severity: %s' % message['signing_severity']; 240, string_content; 241, attribute; 242, argument_list; 243, escape_sequence:\t; 244, escape_sequence:\t; 245, string_content:tier; 246, escape_sequence:\t; 247, escape_sequence:\t; 248, identifier:files; 249, subscript; 250, string; 251, expression_statement; 252, attribute; 253, argument_list; 254, call; 255, string; 256, subscript; 257, string; 258, subscript; 259, string_content; 260, parenthesized_expression; 261, for_in_clause; 262, string_content; 263, identifier:unicodehelper; 264, identifier:decode; 265, identifier:self; 266, identifier:_flatten_list; 267, subscript; 268, string; 269, subscript; 270, escape_sequence:\t; 271, identifier:self; 272, identifier:_flatten_list; 273, subscript; 274, identifier:files; 275, unary_operator; 276, assignment; 277, identifier:verbose_output; 278, identifier:append; 279, binary_operator:fmsg % ' > '.join(files); 280, attribute; 281, argument_list; 282, string_content; 283, identifier:message; 284, string; 285, string_content; 286, identifier:message; 287, string; 288, escape_sequence:\t; 289, conditional_expression:'\t> %s' % x if x is not None else '\t>' + ('-' * 20); 290, identifier:x; 291, subscript; 292, escape_sequence:\n; 293, identifier:message; 294, string; 295, string_content; 296, identifier:message; 297, string; 298, identifier:message; 299, string; 300, integer:1; 301, subscript; 302, string; 303, identifier:fmsg; 304, call; 305, identifier:verbose_output; 306, identifier:append; 307, binary_operator:fmsg % files; 308, escape_sequence:\t; 309, escape_sequence:\t; 310, string_content:line; 311, escape_sequence:\t; 312, escape_sequence:\t; 313, string_content:column; 314, binary_operator:'\t> %s' % x; 315, comparison_operator:x is not None; 316, binary_operator:'\t>' + ('-' * 20); 317, identifier:message; 318, string; 319, string_content:description; 320, escape_sequence:\t; 321, string_content:signing_severity; 322, string_content:signing_help; 323, identifier:files; 324, unary_operator; 325, string_content:(none); 326, attribute; 327, argument_list; 328, identifier:fmsg; 329, identifier:files; 330, string; 331, identifier:x; 332, identifier:x; 333, None; 334, string; 335, parenthesized_expression; 336, string_content:context; 337, integer:1; 338, string; 339, identifier:join; 340, identifier:files; 341, string_content; 342, string_content; 343, binary_operator:'-' * 20; 344, string_content:>; 345, escape_sequence:\t; 346, escape_sequence:\t; 347, string; 348, integer:20; 349, string_content:-
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 8, 16; 8, 17; 9, 18; 11, 19; 13, 20; 13, 21; 15, 22; 18, 23; 19, 24; 19, 25; 21, 26; 21, 27; 21, 28; 21, 29; 21, 30; 21, 31; 21, 32; 21, 33; 21, 34; 21, 35; 21, 36; 21, 37; 21, 38; 21, 39; 21, 40; 21, 41; 21, 42; 22, 43; 22, 44; 25, 45; 25, 46; 25, 47; 26, 48; 28, 49; 28, 50; 29, 51; 29, 52; 30, 53; 30, 54; 32, 55; 34, 56; 35, 57; 35, 58; 37, 59; 37, 60; 38, 61; 38, 62; 39, 63; 39, 64; 41, 65; 42, 66; 43, 67; 43, 68; 44, 69; 45, 70; 47, 71; 47, 72; 48, 73; 48, 74; 49, 75; 49, 76; 50, 77; 51, 78; 51, 79; 52, 80; 53, 81; 53, 82; 54, 83; 54, 84; 55, 85; 55, 86; 56, 87; 56, 88; 57, 89; 57, 90; 58, 91; 58, 92; 58, 93; 59, 94; 59, 95; 60, 96; 61, 97; 61, 98; 62, 99; 63, 100; 63, 101; 64, 102; 64, 103; 65, 104; 65, 105; 66, 106; 66, 107; 67, 108; 67, 109; 69, 110; 69, 111; 70, 112; 72, 113; 76, 114; 77, 115; 78, 116; 78, 117; 79, 118; 80, 119; 81, 120; 81, 121; 82, 122; 83, 123; 84, 124; 85, 125; 85, 126; 86, 127; 88, 128; 88, 129; 89, 130; 89, 131; 90, 132; 90, 133; 91, 134; 93, 135; 93, 136; 93, 137; 95, 138; 96, 139; 97, 140; 97, 141; 98, 142; 98, 143; 99, 144; 100, 145; 100, 146; 101, 147; 102, 148; 103, 149; 104, 150; 104, 151; 105, 152; 106, 153; 106, 154; 107, 155; 110, 156; 110, 157; 111, 158; 115, 159; 115, 160; 118, 161; 119, 162; 119, 163; 122, 164; 123, 165; 123, 166; 124, 167; 124, 168; 127, 169; 127, 170; 129, 171; 134, 172; 134, 173; 135, 174; 135, 175; 136, 176; 136, 177; 137, 178; 139, 179; 139, 180; 141, 181; 142, 182; 142, 183; 144, 184; 144, 185; 147, 186; 148, 187; 148, 188; 149, 189; 149, 190; 152, 191; 155, 192; 155, 193; 158, 194; 158, 195; 159, 196; 159, 197; 160, 198; 162, 199; 162, 200; 163, 201; 165, 202; 165, 203; 166, 204; 167, 205; 167, 206; 168, 207; 169, 208; 170, 209; 170, 210; 173, 211; 174, 212; 174, 213; 176, 214; 176, 215; 177, 216; 178, 217; 179, 218; 179, 219; 180, 220; 183, 221; 184, 222; 184, 223; 185, 224; 187, 225; 187, 226; 188, 227; 189, 228; 189, 229; 190, 230; 191, 231; 192, 232; 192, 233; 193, 234; 195, 235; 195, 236; 198, 237; 198, 238; 201, 239; 204, 240; 207, 241; 207, 242; 208, 243; 208, 244; 210, 245; 211, 246; 211, 247; 213, 248; 214, 249; 214, 250; 215, 251; 216, 252; 216, 253; 217, 254; 220, 255; 220, 256; 224, 257; 224, 258; 227, 259; 230, 260; 230, 261; 232, 262; 235, 263; 235, 264; 237, 265; 237, 266; 238, 267; 239, 268; 239, 269; 240, 270; 241, 271; 241, 272; 242, 273; 249, 274; 249, 275; 251, 276; 252, 277; 252, 278; 253, 279; 254, 280; 254, 281; 255, 282; 256, 283; 256, 284; 257, 285; 258, 286; 258, 287; 259, 288; 260, 289; 261, 290; 261, 291; 262, 292; 267, 293; 267, 294; 268, 295; 269, 296; 269, 297; 273, 298; 273, 299; 275, 300; 276, 301; 276, 302; 279, 303; 279, 304; 280, 305; 280, 306; 281, 307; 282, 308; 282, 309; 284, 310; 285, 311; 285, 312; 287, 313; 289, 314; 289, 315; 289, 316; 291, 317; 291, 318; 294, 319; 295, 320; 297, 321; 299, 322; 301, 323; 301, 324; 302, 325; 304, 326; 304, 327; 307, 328; 307, 329; 314, 330; 314, 331; 315, 332; 315, 333; 316, 334; 316, 335; 318, 336; 324, 337; 326, 338; 326, 339; 327, 340; 330, 341; 334, 342; 335, 343; 338, 344; 341, 345; 342, 346; 343, 347; 343, 348; 347, 349
def _print_message(self, prefix, message, verbose=True): 'Prints a message and takes care of all sorts of nasty code' # Load up the standard output. output = ['\n', prefix, message['message']] # We have some extra stuff for verbose mode. if verbose: verbose_output = [] # Detailed problem description. if message['description']: verbose_output.append( self._flatten_list(message['description'])) if message.get('signing_severity'): verbose_output.append( ('\tAutomated signing severity: %s' % message['signing_severity'])) if message.get('signing_help'): verbose_output.append( '\tSuggestions for passing automated signing:') verbose_output.append( self._flatten_list(message['signing_help'])) # Show the user what tier we're on verbose_output.append('\tTier:\t%d' % message['tier']) # If file information is available, output that as well. files = message['file'] if files is not None and files != '': fmsg = '\tFile:\t%s' # Nested files (subpackes) are stored in a list. if type(files) is list: if files[-1] == '': files[-1] = '(none)' verbose_output.append(fmsg % ' > '.join(files)) else: verbose_output.append(fmsg % files) # If there is a line number, that gets put on the end. if message['line']: verbose_output.append('\tLine:\t%s' % message['line']) if message['column'] and message['column'] != 0: verbose_output.append('\tColumn:\t%d' % message['column']) if message.get('context'): verbose_output.append('\tContext:') verbose_output.extend([('\t> %s' % x if x is not None else '\t>' + ('-' * 20)) for x in message['context']]) # Stick it in with the standard items. output.append('\n') output.append('\n'.join(verbose_output)) # Send the final output to the handler to be rendered. self.handler.write(u''.join(map(unicodehelper.decode, output)))
0, module; 1, function_definition; 2, function_name:finish; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, expression_statement; 13, comment:# If we have any items, add them. Items are optional here.; 14, if_statement; 15, if_statement; 16, comment:# Attempt to set a view_mode if given; 17, if_statement; 18, comment:# Finalize the directory items; 19, expression_statement; 20, comment:# Return the cached list of all the list items that were added; 21, return_statement; 22, identifier:items; 23, None; 24, identifier:sort_methods; 25, None; 26, identifier:succeeded; 27, True; 28, identifier:update_listing; 29, False; 30, identifier:cache_to_disc; 31, True; 32, identifier:view_mode; 33, None; 34, string; 35, identifier:items; 36, block; 37, identifier:sort_methods; 38, block; 39, comparison_operator:view_mode is not None; 40, comment:# First check if we were given an integer or parseable integer; 41, block; 42, call; 43, attribute; 44, string_content:Adds the provided items to the XBMC interface. :param items: an iterable of items where each item is either a dictionary with keys/values suitable for passing to :meth:`xbmcswift2.ListItem.from_dict` or an instance of :class:`xbmcswift2.ListItem`. :param sort_methods: a list of valid XBMC sort_methods. Each item in the list can either be a sort method or a tuple of ``sort_method, label2_mask``. See :meth:`add_sort_method` for more detail concerning valid sort_methods. Example call with sort_methods:: sort_methods = ['label', 'title', ('date', '%D')] plugin.finish(items, sort_methods=sort_methods) :param view_mode: can either be an integer (or parseable integer string) corresponding to a view_mode or the name of a type of view. Currrently the only view type supported is 'thumbnail'. :returns: a list of all ListItems added to the XBMC interface.; 45, expression_statement; 46, for_statement; 47, identifier:view_mode; 48, None; 49, try_statement; 50, if_statement; 51, attribute; 52, argument_list; 53, identifier:self; 54, identifier:added_items; 55, call; 56, identifier:sort_method; 57, identifier:sort_methods; 58, block; 59, block; 60, except_clause; 61, comparison_operator:view_mode_id is not None; 62, block; 63, identifier:self; 64, identifier:end_of_directory; 65, identifier:succeeded; 66, identifier:update_listing; 67, identifier:cache_to_disc; 68, attribute; 69, argument_list; 70, if_statement; 71, expression_statement; 72, identifier:ValueError; 73, comment:# Attempt to lookup a view mode; 74, block; 75, identifier:view_mode_id; 76, None; 77, expression_statement; 78, identifier:self; 79, identifier:add_items; 80, identifier:items; 81, boolean_operator; 82, block; 83, else_clause; 84, assignment; 85, expression_statement; 86, call; 87, not_operator; 88, call; 89, expression_statement; 90, block; 91, identifier:view_mode_id; 92, call; 93, assignment; 94, attribute; 95, argument_list; 96, call; 97, identifier:hasattr; 98, argument_list; 99, call; 100, expression_statement; 101, identifier:int; 102, argument_list; 103, identifier:view_mode_id; 104, call; 105, identifier:self; 106, identifier:set_view_mode; 107, identifier:view_mode_id; 108, identifier:isinstance; 109, argument_list; 110, identifier:sort_method; 111, string; 112, attribute; 113, argument_list; 114, call; 115, identifier:view_mode; 116, attribute; 117, argument_list; 118, identifier:sort_method; 119, identifier:basestring; 120, string_content:__len__; 121, identifier:self; 122, identifier:add_sort_method; 123, list_splat; 124, attribute; 125, argument_list; 126, identifier:self; 127, identifier:get_view_mode_id; 128, identifier:view_mode; 129, identifier:sort_method; 130, identifier:self; 131, identifier:add_sort_method; 132, identifier:sort_method
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 6, 22; 6, 23; 7, 24; 7, 25; 8, 26; 8, 27; 9, 28; 9, 29; 10, 30; 10, 31; 11, 32; 11, 33; 12, 34; 14, 35; 14, 36; 15, 37; 15, 38; 17, 39; 17, 40; 17, 41; 19, 42; 21, 43; 34, 44; 36, 45; 38, 46; 39, 47; 39, 48; 41, 49; 41, 50; 42, 51; 42, 52; 43, 53; 43, 54; 45, 55; 46, 56; 46, 57; 46, 58; 49, 59; 49, 60; 50, 61; 50, 62; 51, 63; 51, 64; 52, 65; 52, 66; 52, 67; 55, 68; 55, 69; 58, 70; 59, 71; 60, 72; 60, 73; 60, 74; 61, 75; 61, 76; 62, 77; 68, 78; 68, 79; 69, 80; 70, 81; 70, 82; 70, 83; 71, 84; 74, 85; 77, 86; 81, 87; 81, 88; 82, 89; 83, 90; 84, 91; 84, 92; 85, 93; 86, 94; 86, 95; 87, 96; 88, 97; 88, 98; 89, 99; 90, 100; 92, 101; 92, 102; 93, 103; 93, 104; 94, 105; 94, 106; 95, 107; 96, 108; 96, 109; 98, 110; 98, 111; 99, 112; 99, 113; 100, 114; 102, 115; 104, 116; 104, 117; 109, 118; 109, 119; 111, 120; 112, 121; 112, 122; 113, 123; 114, 124; 114, 125; 116, 126; 116, 127; 117, 128; 123, 129; 124, 130; 124, 131; 125, 132
def finish(self, items=None, sort_methods=None, succeeded=True, update_listing=False, cache_to_disc=True, view_mode=None): '''Adds the provided items to the XBMC interface. :param items: an iterable of items where each item is either a dictionary with keys/values suitable for passing to :meth:`xbmcswift2.ListItem.from_dict` or an instance of :class:`xbmcswift2.ListItem`. :param sort_methods: a list of valid XBMC sort_methods. Each item in the list can either be a sort method or a tuple of ``sort_method, label2_mask``. See :meth:`add_sort_method` for more detail concerning valid sort_methods. Example call with sort_methods:: sort_methods = ['label', 'title', ('date', '%D')] plugin.finish(items, sort_methods=sort_methods) :param view_mode: can either be an integer (or parseable integer string) corresponding to a view_mode or the name of a type of view. Currrently the only view type supported is 'thumbnail'. :returns: a list of all ListItems added to the XBMC interface. ''' # If we have any items, add them. Items are optional here. if items: self.add_items(items) if sort_methods: for sort_method in sort_methods: if not isinstance(sort_method, basestring) and hasattr(sort_method, '__len__'): self.add_sort_method(*sort_method) else: self.add_sort_method(sort_method) # Attempt to set a view_mode if given if view_mode is not None: # First check if we were given an integer or parseable integer try: view_mode_id = int(view_mode) except ValueError: # Attempt to lookup a view mode view_mode_id = self.get_view_mode_id(view_mode) if view_mode_id is not None: self.set_view_mode(view_mode_id) # Finalize the directory items self.end_of_directory(succeeded, update_listing, cache_to_disc) # Return the cached list of all the list items that were added return self.added_items
0, module; 1, function_definition; 2, function_name:followingPrefix; 3, parameters; 4, block; 5, identifier:prefix; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, while_statement; 10, if_statement; 11, expression_statement; 12, expression_statement; 13, return_statement; 14, comment:"""Returns a String that sorts just after all Strings beginning with a prefix"""; 15, assignment; 16, assignment; 17, parenthesized_expression; 18, block; 19, parenthesized_expression; 20, block; 21, assignment; 22, assignment; 23, call; 24, identifier:prefixBytes; 25, call; 26, identifier:changeIndex; 27, binary_operator:len(prefixBytes) - 1; 28, boolean_operator; 29, expression_statement; 30, comparison_operator:changeIndex < 0; 31, return_statement; 32, identifier:newBytes; 33, call; 34, subscript; 35, binary_operator:newBytes[changeIndex] + 1; 36, attribute; 37, argument_list; 38, identifier:array; 39, argument_list; 40, call; 41, integer:1; 42, comparison_operator:changeIndex >= 0; 43, comparison_operator:prefixBytes[changeIndex] == 0xff; 44, assignment; 45, identifier:changeIndex; 46, integer:0; 47, None; 48, identifier:array; 49, argument_list; 50, identifier:newBytes; 51, identifier:changeIndex; 52, subscript; 53, integer:1; 54, identifier:newBytes; 55, identifier:tostring; 56, string; 57, identifier:prefix; 58, identifier:len; 59, argument_list; 60, identifier:changeIndex; 61, integer:0; 62, subscript; 63, integer:0xff; 64, identifier:changeIndex; 65, binary_operator:changeIndex - 1; 66, string; 67, subscript; 68, identifier:newBytes; 69, identifier:changeIndex; 70, string_content:B; 71, identifier:prefixBytes; 72, identifier:prefixBytes; 73, identifier:changeIndex; 74, identifier:changeIndex; 75, integer:1; 76, string_content:B; 77, identifier:prefix; 78, slice; 79, integer:0; 80, binary_operator:changeIndex + 1; 81, identifier:changeIndex; 82, integer:1
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 6, 14; 7, 15; 8, 16; 9, 17; 9, 18; 10, 19; 10, 20; 11, 21; 12, 22; 13, 23; 15, 24; 15, 25; 16, 26; 16, 27; 17, 28; 18, 29; 19, 30; 20, 31; 21, 32; 21, 33; 22, 34; 22, 35; 23, 36; 23, 37; 25, 38; 25, 39; 27, 40; 27, 41; 28, 42; 28, 43; 29, 44; 30, 45; 30, 46; 31, 47; 33, 48; 33, 49; 34, 50; 34, 51; 35, 52; 35, 53; 36, 54; 36, 55; 39, 56; 39, 57; 40, 58; 40, 59; 42, 60; 42, 61; 43, 62; 43, 63; 44, 64; 44, 65; 49, 66; 49, 67; 52, 68; 52, 69; 56, 70; 59, 71; 62, 72; 62, 73; 65, 74; 65, 75; 66, 76; 67, 77; 67, 78; 78, 79; 78, 80; 80, 81; 80, 82
def followingPrefix(prefix): """Returns a String that sorts just after all Strings beginning with a prefix""" prefixBytes = array('B', prefix) changeIndex = len(prefixBytes) - 1 while (changeIndex >= 0 and prefixBytes[changeIndex] == 0xff ): changeIndex = changeIndex - 1; if(changeIndex < 0): return None newBytes = array('B', prefix[0:changeIndex + 1]) newBytes[changeIndex] = newBytes[changeIndex] + 1 return newBytes.tostring()
0, module; 1, function_definition; 2, function_name:sorted_maybe_numeric; 3, parameters; 4, block; 5, identifier:x; 6, expression_statement; 7, expression_statement; 8, if_statement; 9, comment:""" Sorts x with numeric semantics if all keys are nonnegative integers. Otherwise uses standard string sorting. """; 10, assignment; 11, identifier:all_numeric; 12, block; 13, else_clause; 14, identifier:all_numeric; 15, call; 16, return_statement; 17, block; 18, identifier:all; 19, argument_list; 20, call; 21, return_statement; 22, call; 23, identifier:sorted; 24, argument_list; 25, call; 26, identifier:map; 27, argument_list; 28, identifier:x; 29, keyword_argument; 30, identifier:sorted; 31, argument_list; 32, attribute; 33, identifier:x; 34, identifier:key; 35, identifier:int; 36, identifier:x; 37, identifier:str; 38, identifier:isdigit
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 6, 9; 7, 10; 8, 11; 8, 12; 8, 13; 10, 14; 10, 15; 12, 16; 13, 17; 15, 18; 15, 19; 16, 20; 17, 21; 19, 22; 20, 23; 20, 24; 21, 25; 22, 26; 22, 27; 24, 28; 24, 29; 25, 30; 25, 31; 27, 32; 27, 33; 29, 34; 29, 35; 31, 36; 32, 37; 32, 38
def sorted_maybe_numeric(x): """ Sorts x with numeric semantics if all keys are nonnegative integers. Otherwise uses standard string sorting. """ all_numeric = all(map(str.isdigit, x)) if all_numeric: return sorted(x, key=int) else: return sorted(x)
0, module; 1, function_definition; 2, function_name:sort_by; 3, parameters; 4, block; 5, identifier:self; 6, identifier:sf; 7, expression_statement; 8, expression_statement; 9, return_statement; 10, comment:""" Determines how to sort search results. Available sorting methods are sort.SCORE, sort.COMMENTS, sort.HEIGHT, sort.RELEVANCE, sort.CREATED_AT, and sort.RANDOM; default is sort.CREATED_AT. """; 11, assignment; 12, call; 13, identifier:params; 14, call; 15, attribute; 16, argument_list; 17, identifier:join_params; 18, argument_list; 19, identifier:self; 20, identifier:__class__; 21, dictionary_splat; 22, attribute; 23, dictionary; 24, identifier:params; 25, identifier:self; 26, identifier:parameters; 27, pair; 28, string:"sf"; 29, identifier:sf
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 7, 10; 8, 11; 9, 12; 11, 13; 11, 14; 12, 15; 12, 16; 14, 17; 14, 18; 15, 19; 15, 20; 16, 21; 18, 22; 18, 23; 21, 24; 22, 25; 22, 26; 23, 27; 27, 28; 27, 29
def sort_by(self, sf): """ Determines how to sort search results. Available sorting methods are sort.SCORE, sort.COMMENTS, sort.HEIGHT, sort.RELEVANCE, sort.CREATED_AT, and sort.RANDOM; default is sort.CREATED_AT. """ params = join_params(self.parameters, {"sf": sf}) return self.__class__(**params)
0, module; 1, function_definition; 2, function_name:get_contributor_sort_value; 3, parameters; 4, block; 5, identifier:self; 6, identifier:obj; 7, expression_statement; 8, expression_statement; 9, if_statement; 10, return_statement; 11, comment:"""Generate display name for contributor."""; 12, assignment; 13, boolean_operator; 14, block; 15, else_clause; 16, call; 17, identifier:user; 18, attribute; 19, attribute; 20, attribute; 21, expression_statement; 22, block; 23, attribute; 24, argument_list; 25, identifier:obj; 26, identifier:contributor; 27, identifier:user; 28, identifier:first_name; 29, identifier:user; 30, identifier:last_name; 31, assignment; 32, expression_statement; 33, call; 34, identifier:lower; 35, identifier:contributor; 36, call; 37, assignment; 38, attribute; 39, argument_list; 40, attribute; 41, argument_list; 42, identifier:contributor; 43, attribute; 44, identifier:contributor; 45, identifier:strip; 46, identifier:user; 47, identifier:get_full_name; 48, identifier:user; 49, identifier:username
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 7, 11; 8, 12; 9, 13; 9, 14; 9, 15; 10, 16; 12, 17; 12, 18; 13, 19; 13, 20; 14, 21; 15, 22; 16, 23; 16, 24; 18, 25; 18, 26; 19, 27; 19, 28; 20, 29; 20, 30; 21, 31; 22, 32; 23, 33; 23, 34; 31, 35; 31, 36; 32, 37; 33, 38; 33, 39; 36, 40; 36, 41; 37, 42; 37, 43; 38, 44; 38, 45; 40, 46; 40, 47; 43, 48; 43, 49
def get_contributor_sort_value(self, obj): """Generate display name for contributor.""" user = obj.contributor if user.first_name or user.last_name: contributor = user.get_full_name() else: contributor = user.username return contributor.strip().lower()
0, module; 1, function_definition; 2, function_name:_gen_cache_key_for_slice; 3, parameters; 4, block; 5, identifier:url_dict; 6, identifier:start_int; 7, identifier:total_int; 8, identifier:authn_subj_list; 9, expression_statement; 10, comment:# logging.debug('Gen key. result_record_count={}'.format(result_record_count)); 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, return_statement; 17, comment:"""Generate cache key for the REST URL the client is currently accessing or is expected to access in order to get the slice starting at the given ``start_int`` of a multi-slice result set. When used for finding the key to check in the current call, ``start_int`` is 0, or the start that was passed in the current call. When used for finding the key to set for the anticipated call, ``start_int`` is current ``start_int`` + ``count_int``, the number of objects the current call will return. The URL for the slice is the same as for the current slice, except that the `start` query parameter has been increased by the number of items returned in the current slice. Except for advancing the start value and potentially adjusting the desired slice size, it doesn't make sense for the client to change the REST URL during slicing, but such queries are supported. They will, however, trigger potentially expensive database queries to find the current slice position. To support adjustments in desired slice size during slicing, the count is not used when generating the key. The active subjects are used in the key in order to prevent potential security issues if authenticated subjects change during slicing. The url_dict is normalized by encoding it to a JSON string with sorted keys. A hash of the JSON is used for better distribution in a hash map and to avoid the 256 bytes limit on keys in some caches. """; 18, assignment; 19, call; 20, call; 21, assignment; 22, call; 23, call; 24, identifier:key_url_dict; 25, call; 26, attribute; 27, argument_list; 28, attribute; 29, argument_list; 30, identifier:key_json; 31, call; 32, attribute; 33, argument_list; 34, attribute; 35, argument_list; 36, attribute; 37, argument_list; 38, subscript; 39, identifier:pop; 40, string; 41, None; 42, subscript; 43, identifier:pop; 44, string; 45, None; 46, attribute; 47, argument_list; 48, identifier:logging; 49, identifier:debug; 50, call; 51, call; 52, identifier:hexdigest; 53, identifier:copy; 54, identifier:deepcopy; 55, identifier:url_dict; 56, identifier:key_url_dict; 57, string; 58, string_content:start; 59, identifier:key_url_dict; 60, string; 61, string_content:count; 62, attribute; 63, identifier:serialize_to_normalized_compact_json; 64, dictionary; 65, attribute; 66, argument_list; 67, attribute; 68, argument_list; 69, string_content:query; 70, string_content:query; 71, identifier:d1_common; 72, identifier:util; 73, pair; 74, pair; 75, pair; 76, pair; 77, string; 78, identifier:format; 79, identifier:key_json; 80, identifier:hashlib; 81, identifier:sha256; 82, call; 83, string; 84, identifier:key_url_dict; 85, string; 86, identifier:start_int; 87, string; 88, identifier:total_int; 89, string; 90, identifier:authn_subj_list; 91, string_content:key_json={}; 92, attribute; 93, argument_list; 94, string_content:url_dict; 95, string_content:start; 96, string_content:total; 97, string_content:subject; 98, identifier:key_json; 99, identifier:encode; 100, string; 101, string_content:utf-8
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 9, 17; 11, 18; 12, 19; 13, 20; 14, 21; 15, 22; 16, 23; 18, 24; 18, 25; 19, 26; 19, 27; 20, 28; 20, 29; 21, 30; 21, 31; 22, 32; 22, 33; 23, 34; 23, 35; 25, 36; 25, 37; 26, 38; 26, 39; 27, 40; 27, 41; 28, 42; 28, 43; 29, 44; 29, 45; 31, 46; 31, 47; 32, 48; 32, 49; 33, 50; 34, 51; 34, 52; 36, 53; 36, 54; 37, 55; 38, 56; 38, 57; 40, 58; 42, 59; 42, 60; 44, 61; 46, 62; 46, 63; 47, 64; 50, 65; 50, 66; 51, 67; 51, 68; 57, 69; 60, 70; 62, 71; 62, 72; 64, 73; 64, 74; 64, 75; 64, 76; 65, 77; 65, 78; 66, 79; 67, 80; 67, 81; 68, 82; 73, 83; 73, 84; 74, 85; 74, 86; 75, 87; 75, 88; 76, 89; 76, 90; 77, 91; 82, 92; 82, 93; 83, 94; 85, 95; 87, 96; 89, 97; 92, 98; 92, 99; 93, 100; 100, 101
def _gen_cache_key_for_slice(url_dict, start_int, total_int, authn_subj_list): """Generate cache key for the REST URL the client is currently accessing or is expected to access in order to get the slice starting at the given ``start_int`` of a multi-slice result set. When used for finding the key to check in the current call, ``start_int`` is 0, or the start that was passed in the current call. When used for finding the key to set for the anticipated call, ``start_int`` is current ``start_int`` + ``count_int``, the number of objects the current call will return. The URL for the slice is the same as for the current slice, except that the `start` query parameter has been increased by the number of items returned in the current slice. Except for advancing the start value and potentially adjusting the desired slice size, it doesn't make sense for the client to change the REST URL during slicing, but such queries are supported. They will, however, trigger potentially expensive database queries to find the current slice position. To support adjustments in desired slice size during slicing, the count is not used when generating the key. The active subjects are used in the key in order to prevent potential security issues if authenticated subjects change during slicing. The url_dict is normalized by encoding it to a JSON string with sorted keys. A hash of the JSON is used for better distribution in a hash map and to avoid the 256 bytes limit on keys in some caches. """ # logging.debug('Gen key. result_record_count={}'.format(result_record_count)) key_url_dict = copy.deepcopy(url_dict) key_url_dict['query'].pop('start', None) key_url_dict['query'].pop('count', None) key_json = d1_common.util.serialize_to_normalized_compact_json( { 'url_dict': key_url_dict, 'start': start_int, 'total': total_int, 'subject': authn_subj_list, } ) logging.debug('key_json={}'.format(key_json)) return hashlib.sha256(key_json.encode('utf-8')).hexdigest()
0, module; 1, function_definition; 2, function_name:normalize; 3, parameters; 4, block; 5, identifier:rp_pyxb; 6, expression_statement; 7, comment:# noinspection PyMissingOrEmptyDocstring; 8, function_definition; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, comment:"""Normalize a ReplicationPolicy PyXB type in place. The preferred and blocked lists are sorted alphabetically. As blocked nodes override preferred nodes, and any node present in both lists is removed from the preferred list. Args: rp_pyxb : ReplicationPolicy PyXB object The object will be normalized in place. """; 13, function_name:sort; 14, parameters; 15, block; 16, assignment; 17, call; 18, call; 19, identifier:r; 20, identifier:a; 21, expression_statement; 22, attribute; 23, binary_operator:set(_get_attr_or_list(rp_pyxb, 'pref')) - set( _get_attr_or_list(rp_pyxb, 'block') ); 24, identifier:sort; 25, argument_list; 26, identifier:sort; 27, argument_list; 28, call; 29, identifier:rp_pyxb; 30, identifier:preferredMemberNode; 31, call; 32, call; 33, identifier:rp_pyxb; 34, string; 35, identifier:rp_pyxb; 36, string; 37, attribute; 38, argument_list; 39, identifier:set; 40, argument_list; 41, identifier:set; 42, argument_list; 43, string_content:block; 44, string_content:pref; 45, attribute; 46, identifier:sort_value_list_pyxb; 47, call; 48, call; 49, call; 50, identifier:d1_common; 51, identifier:xml; 52, identifier:_get_attr_or_list; 53, argument_list; 54, identifier:_get_attr_or_list; 55, argument_list; 56, identifier:_get_attr_or_list; 57, argument_list; 58, identifier:r; 59, identifier:a; 60, identifier:rp_pyxb; 61, string; 62, identifier:rp_pyxb; 63, string; 64, string_content:pref; 65, string_content:block
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 6, 12; 8, 13; 8, 14; 8, 15; 9, 16; 10, 17; 11, 18; 14, 19; 14, 20; 15, 21; 16, 22; 16, 23; 17, 24; 17, 25; 18, 26; 18, 27; 21, 28; 22, 29; 22, 30; 23, 31; 23, 32; 25, 33; 25, 34; 27, 35; 27, 36; 28, 37; 28, 38; 31, 39; 31, 40; 32, 41; 32, 42; 34, 43; 36, 44; 37, 45; 37, 46; 38, 47; 40, 48; 42, 49; 45, 50; 45, 51; 47, 52; 47, 53; 48, 54; 48, 55; 49, 56; 49, 57; 53, 58; 53, 59; 55, 60; 55, 61; 57, 62; 57, 63; 61, 64; 63, 65
def normalize(rp_pyxb): """Normalize a ReplicationPolicy PyXB type in place. The preferred and blocked lists are sorted alphabetically. As blocked nodes override preferred nodes, and any node present in both lists is removed from the preferred list. Args: rp_pyxb : ReplicationPolicy PyXB object The object will be normalized in place. """ # noinspection PyMissingOrEmptyDocstring def sort(r, a): d1_common.xml.sort_value_list_pyxb(_get_attr_or_list(r, a)) rp_pyxb.preferredMemberNode = set(_get_attr_or_list(rp_pyxb, 'pref')) - set( _get_attr_or_list(rp_pyxb, 'block') ) sort(rp_pyxb, 'block') sort(rp_pyxb, 'pref')
0, module; 1, function_definition; 2, function_name:normalize; 3, parameters; 4, block; 5, identifier:body_part_tup; 6, expression_statement; 7, return_statement; 8, comment:"""Normalize a tuple of BodyPart objects to a string. Normalization is done by sorting the body_parts by the Content- Disposition headers, which is typically on the form, ``form-data; name="name_of_part``. """; 9, call; 10, attribute; 11, argument_list; 12, string; 13, identifier:join; 14, list_comprehension; 15, string_content; 16, call; 17, for_in_clause; 18, escape_sequence:\n; 19, escape_sequence:\n; 20, attribute; 21, argument_list; 22, identifier:p; 23, call; 24, string; 25, identifier:format; 26, call; 27, attribute; 28, identifier:sorted; 29, argument_list; 30, string_content; 31, identifier:str; 32, argument_list; 33, identifier:p; 34, identifier:text; 35, identifier:body_part_tup; 36, keyword_argument; 37, escape_sequence:\n; 38, escape_sequence:\n; 39, subscript; 40, attribute; 41, identifier:key; 42, lambda; 43, attribute; 44, string; 45, identifier:p; 46, identifier:encoding; 47, lambda_parameters; 48, subscript; 49, identifier:p; 50, identifier:headers; 51, string_content:Content-Disposition; 52, identifier:p; 53, attribute; 54, string; 55, identifier:p; 56, identifier:headers; 57, string_content:Content-Disposition
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 6, 8; 7, 9; 9, 10; 9, 11; 10, 12; 10, 13; 11, 14; 12, 15; 14, 16; 14, 17; 15, 18; 15, 19; 16, 20; 16, 21; 17, 22; 17, 23; 20, 24; 20, 25; 21, 26; 21, 27; 23, 28; 23, 29; 24, 30; 26, 31; 26, 32; 27, 33; 27, 34; 29, 35; 29, 36; 30, 37; 30, 38; 32, 39; 32, 40; 36, 41; 36, 42; 39, 43; 39, 44; 40, 45; 40, 46; 42, 47; 42, 48; 43, 49; 43, 50; 44, 51; 47, 52; 48, 53; 48, 54; 53, 55; 53, 56; 54, 57
def normalize(body_part_tup,): """Normalize a tuple of BodyPart objects to a string. Normalization is done by sorting the body_parts by the Content- Disposition headers, which is typically on the form, ``form-data; name="name_of_part``. """ return '\n\n'.join( [ '{}\n\n{}'.format( str(p.headers[b'Content-Disposition'], p.encoding), p.text ) for p in sorted( body_part_tup, key=lambda p: p.headers[b'Content-Disposition'] ) ] )
0, module; 1, function_definition; 2, function_name:save_json; 3, parameters; 4, block; 5, identifier:py_obj; 6, identifier:json_path; 7, expression_statement; 8, with_statement; 9, comment:"""Serialize a native object to JSON and save it normalized, pretty printed to a file. The JSON string is normalized by sorting any dictionary keys. Args: py_obj: object Any object that can be represented in JSON. Some types, such as datetimes are automatically converted to strings. json_path: str File path to which to write the JSON file. E.g.: The path must exist. The filename will normally end with ".json". See Also: ToJsonCompatibleTypes() """; 10, with_clause; 11, block; 12, with_item; 13, expression_statement; 14, as_pattern; 15, call; 16, call; 17, as_pattern_target; 18, attribute; 19, argument_list; 20, identifier:open; 21, argument_list; 22, identifier:f; 23, identifier:f; 24, identifier:write; 25, call; 26, identifier:json_path; 27, string; 28, keyword_argument; 29, identifier:serialize_to_normalized_pretty_json; 30, argument_list; 31, string_content:w; 32, identifier:encoding; 33, string; 34, identifier:py_obj; 35, string_content:utf-8
0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 7, 9; 8, 10; 8, 11; 10, 12; 11, 13; 12, 14; 13, 15; 14, 16; 14, 17; 15, 18; 15, 19; 16, 20; 16, 21; 17, 22; 18, 23; 18, 24; 19, 25; 21, 26; 21, 27; 21, 28; 25, 29; 25, 30; 27, 31; 28, 32; 28, 33; 30, 34; 33, 35
def save_json(py_obj, json_path): """Serialize a native object to JSON and save it normalized, pretty printed to a file. The JSON string is normalized by sorting any dictionary keys. Args: py_obj: object Any object that can be represented in JSON. Some types, such as datetimes are automatically converted to strings. json_path: str File path to which to write the JSON file. E.g.: The path must exist. The filename will normally end with ".json". See Also: ToJsonCompatibleTypes() """ with open(json_path, 'w', encoding='utf-8') as f: f.write(serialize_to_normalized_pretty_json(py_obj))