sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:contigs_to_positions; 3, parameters; 3, 4; 3, 5; 4, identifier:contigs; 5, default_parameter; 5, 6; 5, 7; 6, identifier:binning; 7, integer:10000; 8, block; 8, 9; 8, 11; 8, 20; 8, 24; 8, 67; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:positions; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:np; 17, identifier:zeros_like; 18, argument_list; 18, 19; 19, identifier:contigs; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:index; 23, integer:0; 24, for_statement; 24, 25; 24, 28; 24, 34; 25, pattern_list; 25, 26; 25, 27; 26, identifier:_; 27, identifier:chunk; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:itertools; 31, identifier:groubpy; 32, argument_list; 32, 33; 33, identifier:contigs; 34, block; 34, 35; 34, 42; 34, 63; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:l; 38, call; 38, 39; 38, 40; 39, identifier:len; 40, argument_list; 40, 41; 41, identifier:chunk; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 52; 44, subscript; 44, 45; 44, 46; 45, identifier:positions; 46, slice; 46, 47; 46, 48; 46, 49; 47, identifier:index; 48, colon; 49, binary_operator:+; 49, 50; 49, 51; 50, identifier:index; 51, identifier:l; 52, binary_operator:*; 52, 53; 52, 62; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:np; 56, identifier:arange; 57, argument_list; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:list; 60, argument_list; 60, 61; 61, identifier:chunk; 62, identifier:binning; 63, expression_statement; 63, 64; 64, augmented_assignment:+=; 64, 65; 64, 66; 65, identifier:index; 66, identifier:l; 67, return_statement; 67, 68; 68, identifier:positions
def contigs_to_positions(contigs, binning=10000): """Build positions from contig labels From a list of contig labels and a binning parameter, build a list of positions that's essentially a concatenation of linspaces with step equal to the binning. Parameters ---------- contigs : list or array_like The list of contig labels, must be sorted. binning : int, optional The step for the list of positions. Default is 10000. Returns ------- positions : numpy.ndarray The piece-wise sorted list of positions """ positions = np.zeros_like(contigs) index = 0 for _, chunk in itertools.groubpy(contigs): l = len(chunk) positions[index : index + l] = np.arange(list(chunk)) * binning index += l return positions
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:find_nearest; 3, parameters; 3, 4; 3, 5; 4, identifier:sorted_list; 5, identifier:x; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 15; 9, 20; 9, 33; 10, comparison_operator:<=; 10, 11; 10, 12; 11, identifier:x; 12, subscript; 12, 13; 12, 14; 13, identifier:sorted_list; 14, integer:0; 15, block; 15, 16; 16, return_statement; 16, 17; 17, subscript; 17, 18; 17, 19; 18, identifier:sorted_list; 19, integer:0; 20, elif_clause; 20, 21; 20, 27; 21, comparison_operator:>=; 21, 22; 21, 23; 22, identifier:x; 23, subscript; 23, 24; 23, 25; 24, identifier:sorted_list; 25, unary_operator:-; 25, 26; 26, integer:1; 27, block; 27, 28; 28, return_statement; 28, 29; 29, subscript; 29, 30; 29, 31; 30, identifier:sorted_list; 31, unary_operator:-; 31, 32; 32, integer:1; 33, else_clause; 33, 34; 34, block; 34, 35; 34, 43; 34, 51; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:lower; 38, call; 38, 39; 38, 40; 39, identifier:find_le; 40, argument_list; 40, 41; 40, 42; 41, identifier:sorted_list; 42, identifier:x; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:upper; 46, call; 46, 47; 46, 48; 47, identifier:find_ge; 48, argument_list; 48, 49; 48, 50; 49, identifier:sorted_list; 50, identifier:x; 51, if_statement; 51, 52; 51, 61; 51, 64; 52, comparison_operator:>; 52, 53; 52, 57; 53, parenthesized_expression; 53, 54; 54, binary_operator:-; 54, 55; 54, 56; 55, identifier:x; 56, identifier:lower; 57, parenthesized_expression; 57, 58; 58, binary_operator:-; 58, 59; 58, 60; 59, identifier:upper; 60, identifier:x; 61, block; 61, 62; 62, return_statement; 62, 63; 63, identifier:upper; 64, else_clause; 64, 65; 65, block; 65, 66; 66, return_statement; 66, 67; 67, identifier:lower
def find_nearest(sorted_list, x): """ Find the nearest item of x from sorted array. :type array: list :param array: an iterable object that support inex :param x: a comparable value note: for finding the nearest item from a descending array, I recommend find_nearest(sorted_list[::-1], x). Because the built-in list[::-1] method is super fast. Usage:: >>> find_nearest([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 5.1) 5 **中文文档** 在正序数组中, 返回最接近x的数。 """ if x <= sorted_list[0]: return sorted_list[0] elif x >= sorted_list[-1]: return sorted_list[-1] else: lower = find_le(sorted_list, x) upper = find_ge(sorted_list, x) if (x - lower) > (upper - x): return upper else: return lower
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:execute; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:input_params; 5, identifier:engine; 6, default_parameter; 6, 7; 6, 8; 7, identifier:cwd; 8, None; 9, block; 9, 10; 9, 12; 9, 33; 9, 51; 9, 52; 9, 56; 9, 71; 9, 72; 9, 76; 9, 84; 9, 104; 9, 105; 9, 111; 9, 121; 9, 122; 9, 126; 9, 152; 9, 161; 9, 186; 9, 187; 9, 205; 10, expression_statement; 10, 11; 11, comment; 12, try_statement; 12, 13; 12, 23; 13, block; 13, 14; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:taskengine_exe; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:config; 20, identifier:get; 21, argument_list; 21, 22; 22, string:'engine'; 23, except_clause; 23, 24; 23, 25; 24, identifier:NoConfigOptionError; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:TaskEngineNotFoundError; 29, argument_list; 29, 30; 30, binary_operator:+; 30, 31; 30, 32; 31, string:"Task Engine config option not set."; 32, string:"\nPlease verify the 'engine' configuration setting."; 33, if_statement; 33, 34; 33, 43; 34, not_operator; 34, 35; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:os; 39, identifier:path; 40, identifier:exists; 41, argument_list; 41, 42; 42, identifier:taskengine_exe; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:TaskEngineNotFoundError; 47, argument_list; 47, 48; 48, binary_operator:+; 48, 49; 48, 50; 49, string:"Task Engine executable not found."; 50, string:"\nPlease verify the 'engine' configuration setting."; 51, comment; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:engine_args; 55, None; 56, try_statement; 56, 57; 56, 67; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:engine_args; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:config; 64, identifier:get; 65, argument_list; 65, 66; 66, string:'engine-args'; 67, except_clause; 67, 68; 67, 69; 68, identifier:NoConfigOptionError; 69, block; 69, 70; 70, pass_statement; 71, comment; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:environment; 75, None; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:config_environment; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:config; 82, identifier:get_environment; 83, argument_list; 84, if_statement; 84, 85; 84, 86; 85, identifier:config_environment; 86, block; 86, 87; 86, 97; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:environment; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:os; 94, identifier:environ; 95, identifier:copy; 96, argument_list; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:environment; 101, identifier:update; 102, argument_list; 102, 103; 103, identifier:config_environment; 104, comment; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:args; 108, list:[taskengine_exe, engine]; 108, 109; 108, 110; 109, identifier:taskengine_exe; 110, identifier:engine; 111, if_statement; 111, 112; 111, 113; 112, identifier:engine_args; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:args; 118, identifier:append; 119, argument_list; 119, 120; 120, identifier:engine_args; 121, comment; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:startupinfo; 125, None; 126, if_statement; 126, 127; 126, 135; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:sys; 131, identifier:platform; 132, identifier:startswith; 133, argument_list; 133, 134; 134, string:'win'; 135, block; 135, 136; 135, 144; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:startupinfo; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:subprocess; 142, identifier:STARTUPINFO; 143, argument_list; 144, expression_statement; 144, 145; 145, augmented_assignment:|=; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:startupinfo; 148, identifier:dwFlags; 149, attribute; 149, 150; 149, 151; 150, identifier:subprocess; 151, identifier:STARTF_USESHOWWINDOW; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:input_json; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:json; 158, identifier:dumps; 159, argument_list; 159, 160; 160, identifier:input_params; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:process; 164, call; 164, 165; 164, 166; 165, identifier:Popen; 166, argument_list; 166, 167; 166, 168; 166, 171; 166, 174; 166, 177; 166, 180; 166, 183; 167, identifier:args; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:stdout; 170, identifier:PIPE; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:stdin; 173, identifier:PIPE; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:stderr; 176, identifier:PIPE; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:cwd; 179, identifier:cwd; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:env; 182, identifier:environment; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:startupinfo; 185, identifier:startupinfo; 186, comment; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, pattern_list; 189, 190; 189, 191; 190, identifier:stdout; 191, identifier:stderr; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:process; 195, identifier:communicate; 196, argument_list; 196, 197; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:input; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:input_json; 202, identifier:encode; 203, argument_list; 203, 204; 204, string:'utf-8'; 205, if_statement; 205, 206; 205, 211; 205, 241; 206, comparison_operator:!=; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:process; 209, identifier:returncode; 210, integer:0; 211, block; 211, 212; 212, if_statement; 212, 213; 212, 216; 212, 227; 213, comparison_operator:!=; 213, 214; 213, 215; 214, identifier:stderr; 215, string:''; 216, block; 216, 217; 217, raise_statement; 217, 218; 218, call; 218, 219; 218, 220; 219, identifier:TaskEngineExecutionError; 220, argument_list; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:stderr; 224, identifier:decode; 225, argument_list; 225, 226; 226, string:'utf-8'; 227, else_clause; 227, 228; 228, block; 228, 229; 229, raise_statement; 229, 230; 230, call; 230, 231; 230, 232; 231, identifier:TaskEngineExecutionError; 232, argument_list; 232, 233; 233, binary_operator:+; 233, 234; 233, 235; 234, string:'Task Engine exited with code: '; 235, call; 235, 236; 235, 237; 236, identifier:str; 237, argument_list; 237, 238; 238, attribute; 238, 239; 238, 240; 239, identifier:process; 240, identifier:returncode; 241, else_clause; 241, 242; 242, block; 242, 243; 243, return_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:json; 247, identifier:loads; 248, argument_list; 248, 249; 248, 255; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:stdout; 252, identifier:decode; 253, argument_list; 253, 254; 254, string:'utf-8'; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:object_pairs_hook; 257, identifier:OrderedDict
def execute(input_params, engine, cwd=None): """ Execute a task with the provided input parameters :param input_params: Python dictionary containg all input parameters. This will be converted to JSON before being passed to the task engine. :param engine: String specifying Task Engine type to run (ENVI, IDL, etc.) :param cwd: Optionally specify the current working directory to be used when spawning the task engine. :return: A python dictionary representing the results JSON string generated by the Task Engine. """ try: taskengine_exe = config.get('engine') except NoConfigOptionError: raise TaskEngineNotFoundError( "Task Engine config option not set." + "\nPlease verify the 'engine' configuration setting.") if not os.path.exists(taskengine_exe): raise TaskEngineNotFoundError( "Task Engine executable not found." + "\nPlease verify the 'engine' configuration setting.") # Get any arguments for the taskengine engine_args = None try: engine_args = config.get('engine-args') except NoConfigOptionError: pass # Get environment overrides if they exist environment = None config_environment = config.get_environment() if config_environment: environment = os.environ.copy() environment.update(config_environment) # Build up the args vector for popen args = [taskengine_exe, engine] if engine_args: args.append(engine_args) # Hide the Console Window on Windows OS startupinfo = None if sys.platform.startswith('win'): startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW input_json = json.dumps(input_params) process = Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE, cwd=cwd, env=environment, startupinfo=startupinfo) # taskengine output is in UTF8. Encode/Decode to UTF8 stdout, stderr = process.communicate(input=input_json.encode('utf-8')) if process.returncode != 0: if stderr != '': raise TaskEngineExecutionError(stderr.decode('utf-8')) else: raise TaskEngineExecutionError( 'Task Engine exited with code: ' + str(process.returncode)) else: return json.loads(stdout.decode('utf-8'), object_pairs_hook=OrderedDict)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:merge_fasta; 3, parameters; 3, 4; 3, 5; 4, identifier:fasta_file; 5, identifier:output_dir; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 50; 6, 101; 6, 129; 6, 130; 6, 150; 6, 160; 6, 161; 6, 167; 6, 280; 6, 281; 6, 307; 6, 316; 6, 328; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:chunk_lexicographic_order; 13, parameters; 13, 14; 14, identifier:chunk; 15, block; 15, 16; 15, 18; 15, 27; 15, 36; 15, 43; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:chunk_fields; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:chunk; 24, identifier:split; 25, argument_list; 25, 26; 26, string:"_"; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:chunk_name; 30, subscript; 30, 31; 30, 32; 31, identifier:chunk_fields; 32, slice; 32, 33; 32, 34; 33, colon; 34, unary_operator:-; 34, 35; 35, integer:1; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:chunk_id; 39, subscript; 39, 40; 39, 41; 40, identifier:chunk_fields; 41, unary_operator:-; 41, 42; 42, integer:1; 43, return_statement; 43, 44; 44, tuple; 44, 45; 44, 46; 45, identifier:chunk_name; 46, call; 46, 47; 46, 48; 47, identifier:int; 48, argument_list; 48, 49; 49, identifier:chunk_id; 50, function_definition; 50, 51; 50, 52; 50, 55; 51, function_name:are_consecutive; 52, parameters; 52, 53; 52, 54; 53, identifier:chunk1; 54, identifier:chunk2; 55, block; 55, 56; 56, if_statement; 56, 57; 56, 62; 56, 65; 57, comparison_operator:in; 57, 58; 57, 59; 58, None; 59, set; 59, 60; 59, 61; 60, identifier:chunk1; 61, identifier:chunk2; 62, block; 62, 63; 63, return_statement; 63, 64; 64, False; 65, else_clause; 65, 66; 66, block; 66, 67; 66, 74; 66, 81; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:ord1; 70, call; 70, 71; 70, 72; 71, identifier:chunk_lexicographic_order; 72, argument_list; 72, 73; 73, identifier:chunk1; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:ord2; 77, call; 77, 78; 77, 79; 78, identifier:chunk_lexicographic_order; 79, argument_list; 79, 80; 80, identifier:chunk2; 81, return_statement; 81, 82; 82, boolean_operator:and; 82, 83; 82, 91; 83, parenthesized_expression; 83, 84; 84, comparison_operator:==; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:ord1; 87, integer:0; 88, subscript; 88, 89; 88, 90; 89, identifier:ord2; 90, integer:0; 91, parenthesized_expression; 91, 92; 92, comparison_operator:==; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:ord1; 95, integer:1; 96, binary_operator:+; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:ord2; 99, integer:1; 100, integer:1; 101, function_definition; 101, 102; 101, 103; 101, 105; 102, function_name:consecutiveness; 103, parameters; 103, 104; 104, identifier:key_chunk_pair; 105, block; 105, 106; 105, 108; 105, 114; 105, 123; 106, expression_statement; 106, 107; 107, comment; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, pattern_list; 110, 111; 110, 112; 111, identifier:key; 112, identifier:chunk; 113, identifier:key_chunk_pair; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, pattern_list; 116, 117; 116, 118; 117, identifier:chunk_name; 118, identifier:chunk_id; 119, call; 119, 120; 119, 121; 120, identifier:chunk_lexicographic_order; 121, argument_list; 121, 122; 122, identifier:chunk; 123, return_statement; 123, 124; 124, tuple; 124, 125; 124, 126; 125, identifier:chunk_name; 126, binary_operator:-; 126, 127; 126, 128; 127, identifier:chunk_id; 128, identifier:key; 129, comment; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:genome; 133, dictionary_comprehension; 133, 134; 133, 141; 134, pair; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:record; 137, identifier:id; 138, attribute; 138, 139; 138, 140; 139, identifier:record; 140, identifier:seq; 141, for_in_clause; 141, 142; 141, 143; 142, identifier:record; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:SeqIO; 146, identifier:parse; 147, argument_list; 147, 148; 147, 149; 148, identifier:fasta_file; 149, string:"fasta"; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:sorted_ids; 153, call; 153, 154; 153, 155; 154, identifier:sorted; 155, argument_list; 155, 156; 155, 157; 156, identifier:genome; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:key; 159, identifier:chunk_lexicographic_order; 160, comment; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:new_genome; 164, call; 164, 165; 164, 166; 165, identifier:dict; 166, argument_list; 167, for_statement; 167, 168; 167, 171; 167, 181; 168, pattern_list; 168, 169; 168, 170; 169, identifier:_; 170, identifier:g; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:itertools; 174, identifier:groupby; 175, argument_list; 175, 176; 175, 180; 176, call; 176, 177; 176, 178; 177, identifier:enumerate; 178, argument_list; 178, 179; 179, identifier:sorted_ids; 180, identifier:consecutiveness; 181, block; 181, 182; 181, 195; 181, 202; 181, 208; 181, 212; 181, 234; 181, 255; 181, 274; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:chunk_range; 185, call; 185, 186; 185, 187; 186, identifier:map; 187, argument_list; 187, 188; 187, 194; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:operator; 191, identifier:itemgetter; 192, argument_list; 192, 193; 193, integer:1; 194, identifier:g; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:first_chunk; 198, call; 198, 199; 198, 200; 199, identifier:next; 200, argument_list; 200, 201; 201, identifier:chunk_range; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:my_sequence; 205, subscript; 205, 206; 205, 207; 206, identifier:genome; 207, identifier:first_chunk; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:my_chunk; 211, None; 212, while_statement; 212, 213; 212, 214; 213, string:"Reading chunk range"; 214, block; 214, 215; 215, try_statement; 215, 216; 215, 230; 216, block; 216, 217; 216, 224; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:my_chunk; 220, call; 220, 221; 220, 222; 221, identifier:next; 222, argument_list; 222, 223; 223, identifier:chunk_range; 224, expression_statement; 224, 225; 225, augmented_assignment:+=; 225, 226; 225, 227; 226, identifier:my_sequence; 227, subscript; 227, 228; 227, 229; 228, identifier:genome; 229, identifier:my_chunk; 230, except_clause; 230, 231; 230, 232; 231, identifier:StopIteration; 232, block; 232, 233; 233, break_statement; 234, try_statement; 234, 235; 234, 248; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:last_chunk_id; 239, subscript; 239, 240; 239, 246; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:my_chunk; 243, identifier:split; 244, argument_list; 244, 245; 245, string:"_"; 246, unary_operator:-; 246, 247; 247, integer:1; 248, except_clause; 248, 249; 248, 250; 249, identifier:AttributeError; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:last_chunk_id; 254, string:""; 255, if_statement; 255, 256; 255, 257; 255, 268; 256, identifier:last_chunk_id; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:new_chunk_id; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, string:"{}_{}"; 264, identifier:format; 265, argument_list; 265, 266; 265, 267; 266, identifier:first_chunk; 267, identifier:last_chunk_id; 268, else_clause; 268, 269; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:new_chunk_id; 273, identifier:first_chunk; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 279; 276, subscript; 276, 277; 276, 278; 277, identifier:new_genome; 278, identifier:new_chunk_id; 279, identifier:my_sequence; 280, comment; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:base_name; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, string:"."; 287, identifier:join; 288, argument_list; 288, 289; 289, subscript; 289, 290; 289, 303; 290, call; 290, 291; 290, 301; 291, attribute; 291, 292; 291, 300; 292, call; 292, 293; 292, 298; 293, attribute; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:os; 296, identifier:path; 297, identifier:basename; 298, argument_list; 298, 299; 299, identifier:fasta_file; 300, identifier:split; 301, argument_list; 301, 302; 302, string:"."; 303, slice; 303, 304; 303, 305; 304, colon; 305, unary_operator:-; 305, 306; 306, integer:1; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:output_name; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, string:"{}_merged.fa"; 313, identifier:format; 314, argument_list; 314, 315; 315, identifier:base_name; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:merged_core_file; 319, call; 319, 320; 319, 325; 320, attribute; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:os; 323, identifier:path; 324, identifier:join; 325, argument_list; 325, 326; 325, 327; 326, identifier:output_dir; 327, identifier:output_name; 328, with_statement; 328, 329; 328, 339; 329, with_clause; 329, 330; 330, with_item; 330, 331; 331, as_pattern; 331, 332; 331, 337; 332, call; 332, 333; 332, 334; 333, identifier:open; 334, argument_list; 334, 335; 334, 336; 335, identifier:merged_core_file; 336, string:"w"; 337, as_pattern_target; 337, 338; 338, identifier:output_handle; 339, block; 339, 340; 340, for_statement; 340, 341; 340, 342; 340, 349; 341, identifier:my_id; 342, call; 342, 343; 342, 344; 343, identifier:sorted; 344, argument_list; 344, 345; 344, 346; 345, identifier:new_genome; 346, keyword_argument; 346, 347; 346, 348; 347, identifier:key; 348, identifier:chunk_lexicographic_order; 349, block; 349, 350; 349, 362; 350, expression_statement; 350, 351; 351, call; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:output_handle; 354, identifier:write; 355, argument_list; 355, 356; 356, call; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, string:">{}\n"; 359, identifier:format; 360, argument_list; 360, 361; 361, identifier:my_id; 362, expression_statement; 362, 363; 363, call; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:output_handle; 366, identifier:write; 367, argument_list; 367, 368; 368, call; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, string:"{}\n"; 371, identifier:format; 372, argument_list; 372, 373; 373, subscript; 373, 374; 373, 375; 374, identifier:new_genome; 375, identifier:my_id
def merge_fasta(fasta_file, output_dir): """Merge chunks into complete FASTA bins Merge bin chunks by appending consecutive chunks to one another. Parameters ---------- fasta_file : file, str or pathlib.Path The FASTA file containing the chunks to merge. output_dir : str or pathlib.Path The output directory to write the merged FASTA bin into. """ # First, define some functions for ordering chunks and detecting # consecutive chunk sequences def chunk_lexicographic_order(chunk): """A quick callback to sort chunk ids lexicographically (first on original names alphabetically, then on relative position on the original contig) """ chunk_fields = chunk.split("_") chunk_name = chunk_fields[:-1] chunk_id = chunk_fields[-1] return (chunk_name, int(chunk_id)) def are_consecutive(chunk1, chunk2): if None in {chunk1, chunk2}: return False else: ord1 = chunk_lexicographic_order(chunk1) ord2 = chunk_lexicographic_order(chunk2) return (ord1[0] == ord2[0]) and (ord1[1] == ord2[1] + 1) def consecutiveness(key_chunk_pair): """A callback for the groupby magic below """ key, chunk = key_chunk_pair chunk_name, chunk_id = chunk_lexicographic_order(chunk) return (chunk_name, chunk_id - key) # Read chunks and sort them genome = { record.id: record.seq for record in SeqIO.parse(fasta_file, "fasta") } sorted_ids = sorted(genome, key=chunk_lexicographic_order) # Identify consecutive ranges and merge them new_genome = dict() for _, g in itertools.groupby(enumerate(sorted_ids), consecutiveness): chunk_range = map(operator.itemgetter(1), g) first_chunk = next(chunk_range) my_sequence = genome[first_chunk] my_chunk = None while "Reading chunk range": try: my_chunk = next(chunk_range) my_sequence += genome[my_chunk] except StopIteration: break try: last_chunk_id = my_chunk.split("_")[-1] except AttributeError: last_chunk_id = "" if last_chunk_id: new_chunk_id = "{}_{}".format(first_chunk, last_chunk_id) else: new_chunk_id = first_chunk new_genome[new_chunk_id] = my_sequence # Write the result base_name = ".".join(os.path.basename(fasta_file).split(".")[:-1]) output_name = "{}_merged.fa".format(base_name) merged_core_file = os.path.join(output_dir, output_name) with open(merged_core_file, "w") as output_handle: for my_id in sorted(new_genome, key=chunk_lexicographic_order): output_handle.write(">{}\n".format(my_id)) output_handle.write("{}\n".format(new_genome[my_id]))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:print_block; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 11; 4, identifier:self; 5, identifier:section_key; 6, default_parameter; 6, 7; 6, 8; 7, identifier:f; 8, attribute; 8, 9; 8, 10; 9, identifier:sys; 10, identifier:stdout; 11, default_parameter; 11, 12; 11, 13; 12, identifier:file_format; 13, string:"mwtab"; 14, block; 14, 15; 14, 17; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 17, 431; 18, comparison_operator:==; 18, 19; 18, 20; 19, identifier:file_format; 20, string:"mwtab"; 21, block; 21, 22; 22, for_statement; 22, 23; 22, 26; 22, 33; 23, pattern_list; 23, 24; 23, 25; 24, identifier:key; 25, identifier:value; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, subscript; 28, 29; 28, 30; 29, identifier:self; 30, identifier:section_key; 31, identifier:items; 32, argument_list; 33, block; 33, 34; 33, 46; 33, 88; 34, if_statement; 34, 35; 34, 44; 35, boolean_operator:and; 35, 36; 35, 39; 36, comparison_operator:==; 36, 37; 36, 38; 37, identifier:section_key; 38, string:"METABOLOMICS WORKBENCH"; 39, comparison_operator:not; 39, 40; 39, 41; 40, identifier:key; 41, tuple; 41, 42; 41, 43; 42, string:"VERSION"; 43, string:"CREATED_ON"; 44, block; 44, 45; 45, continue_statement; 46, if_statement; 46, 47; 46, 52; 46, 62; 46, 77; 47, comparison_operator:in; 47, 48; 47, 49; 48, identifier:key; 49, tuple; 49, 50; 49, 51; 50, string:"VERSION"; 51, string:"CREATED_ON"; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:cw; 56, binary_operator:-; 56, 57; 56, 58; 57, integer:20; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, identifier:key; 62, elif_clause; 62, 63; 62, 67; 63, comparison_operator:in; 63, 64; 63, 65; 64, identifier:key; 65, tuple; 65, 66; 66, string:"SUBJECT_SAMPLE_FACTORS"; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:cw; 71, binary_operator:-; 71, 72; 71, 73; 72, integer:33; 73, call; 73, 74; 73, 75; 74, identifier:len; 75, argument_list; 75, 76; 76, identifier:key; 77, else_clause; 77, 78; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:cw; 82, binary_operator:-; 82, 83; 82, 84; 83, integer:30; 84, call; 84, 85; 84, 86; 85, identifier:len; 86, argument_list; 86, 87; 87, identifier:key; 88, if_statement; 88, 89; 88, 92; 88, 128; 88, 163; 88, 185; 88, 262; 88, 403; 89, comparison_operator:in; 89, 90; 89, 91; 90, string:"\n"; 91, identifier:value; 92, block; 92, 93; 93, for_statement; 93, 94; 93, 95; 93, 101; 94, identifier:line; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:value; 98, identifier:split; 99, argument_list; 99, 100; 100, string:"\n"; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:print; 105, argument_list; 105, 106; 105, 125; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, string:"{}{}{}\t{}"; 109, identifier:format; 110, argument_list; 110, 111; 110, 120; 110, 121; 110, 124; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:prefixes; 116, identifier:get; 117, argument_list; 117, 118; 117, 119; 118, identifier:section_key; 119, string:""; 120, identifier:key; 121, binary_operator:*; 121, 122; 121, 123; 122, identifier:cw; 123, string:" "; 124, identifier:line; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:file; 127, identifier:f; 128, elif_clause; 128, 129; 128, 132; 129, comparison_operator:==; 129, 130; 129, 131; 130, identifier:key; 131, string:"SUBJECT_SAMPLE_FACTORS"; 132, block; 132, 133; 133, for_statement; 133, 134; 133, 135; 133, 136; 134, identifier:factor; 135, identifier:value; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:print; 140, argument_list; 140, 141; 140, 160; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, string:"{}{}\t{}"; 144, identifier:format; 145, argument_list; 145, 146; 145, 147; 145, 150; 146, identifier:key; 147, binary_operator:*; 147, 148; 147, 149; 148, identifier:cw; 149, string:" "; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, string:"\t"; 153, identifier:join; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:factor; 158, identifier:values; 159, argument_list; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:file; 162, identifier:f; 163, elif_clause; 163, 164; 163, 170; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:key; 167, identifier:endswith; 168, argument_list; 168, 169; 169, string:":UNITS"; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:print; 174, argument_list; 174, 175; 174, 182; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, string:"{}\t{}"; 178, identifier:format; 179, argument_list; 179, 180; 179, 181; 180, identifier:key; 181, identifier:value; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:file; 184, identifier:f; 185, elif_clause; 185, 186; 185, 192; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:key; 189, identifier:endswith; 190, argument_list; 190, 191; 191, string:"_RESULTS_FILE"; 192, block; 192, 193; 193, if_statement; 193, 194; 193, 199; 193, 234; 194, call; 194, 195; 194, 196; 195, identifier:isinstance; 196, argument_list; 196, 197; 196, 198; 197, identifier:value; 198, identifier:dict; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 203; 202, identifier:print; 203, argument_list; 203, 204; 203, 231; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, string:"{}{} \t{}\t{}:{}"; 207, identifier:format; 208, argument_list; 208, 209; 208, 218; 209, call; 209, 210; 209, 215; 210, attribute; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:self; 213, identifier:prefixes; 214, identifier:get; 215, argument_list; 215, 216; 215, 217; 216, identifier:section_key; 217, string:""; 218, list_splat; 218, 219; 219, list_comprehension; 219, 220; 219, 221; 219, 228; 220, identifier:i; 221, for_in_clause; 221, 222; 221, 223; 222, identifier:pair; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:value; 226, identifier:items; 227, argument_list; 228, for_in_clause; 228, 229; 228, 230; 229, identifier:i; 230, identifier:pair; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:file; 233, identifier:f; 234, else_clause; 234, 235; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:print; 239, argument_list; 239, 240; 239, 259; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, string:"{}{}{}\t{}"; 243, identifier:format; 244, argument_list; 244, 245; 244, 254; 244, 255; 244, 258; 245, call; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:self; 249, identifier:prefixes; 250, identifier:get; 251, argument_list; 251, 252; 251, 253; 252, identifier:section_key; 253, string:""; 254, identifier:key; 255, binary_operator:*; 255, 256; 255, 257; 256, identifier:cw; 257, string:" "; 258, identifier:value; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:file; 261, identifier:f; 262, elif_clause; 262, 263; 262, 269; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:key; 266, identifier:endswith; 267, argument_list; 267, 268; 268, string:"_START"; 269, block; 269, 270; 269, 274; 269, 289; 269, 297; 269, 395; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:start_key; 273, identifier:key; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:end_key; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, string:"{}{}"; 280, identifier:format; 281, argument_list; 281, 282; 281, 288; 282, subscript; 282, 283; 282, 284; 283, identifier:start_key; 284, slice; 284, 285; 284, 286; 285, colon; 286, unary_operator:-; 286, 287; 287, integer:5; 288, string:"END"; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 292; 291, identifier:print; 292, argument_list; 292, 293; 292, 294; 293, identifier:start_key; 294, keyword_argument; 294, 295; 294, 296; 295, identifier:file; 296, identifier:f; 297, for_statement; 297, 298; 297, 299; 297, 300; 298, identifier:data_key; 299, identifier:value; 300, block; 300, 301; 301, if_statement; 301, 302; 301, 307; 301, 333; 301, 363; 302, comparison_operator:in; 302, 303; 302, 304; 303, identifier:data_key; 304, tuple; 304, 305; 304, 306; 305, string:"Samples"; 306, string:"Factors"; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 311; 310, identifier:print; 311, argument_list; 311, 312; 311, 330; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, string:"{}\t{}"; 315, identifier:format; 316, argument_list; 316, 317; 316, 318; 317, identifier:data_key; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, string:"\t"; 321, identifier:join; 322, argument_list; 322, 323; 323, subscript; 323, 324; 323, 329; 324, subscript; 324, 325; 324, 328; 325, subscript; 325, 326; 325, 327; 326, identifier:self; 327, identifier:section_key; 328, identifier:key; 329, identifier:data_key; 330, keyword_argument; 330, 331; 330, 332; 331, identifier:file; 332, identifier:f; 333, elif_clause; 333, 334; 333, 338; 334, comparison_operator:in; 334, 335; 334, 336; 335, identifier:data_key; 336, tuple; 336, 337; 337, string:"Fields"; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 342; 341, identifier:print; 342, argument_list; 342, 343; 342, 360; 343, call; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, string:"{}"; 346, identifier:format; 347, argument_list; 347, 348; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, string:"\t"; 351, identifier:join; 352, argument_list; 352, 353; 353, subscript; 353, 354; 353, 359; 354, subscript; 354, 355; 354, 358; 355, subscript; 355, 356; 355, 357; 356, identifier:self; 357, identifier:section_key; 358, identifier:key; 359, identifier:data_key; 360, keyword_argument; 360, 361; 360, 362; 361, identifier:file; 362, identifier:f; 363, elif_clause; 363, 364; 363, 367; 364, comparison_operator:==; 364, 365; 364, 366; 365, identifier:data_key; 366, string:"DATA"; 367, block; 367, 368; 368, for_statement; 368, 369; 368, 370; 368, 377; 369, identifier:data; 370, subscript; 370, 371; 370, 376; 371, subscript; 371, 372; 371, 375; 372, subscript; 372, 373; 372, 374; 373, identifier:self; 374, identifier:section_key; 375, identifier:key; 376, identifier:data_key; 377, block; 377, 378; 378, expression_statement; 378, 379; 379, call; 379, 380; 379, 381; 380, identifier:print; 381, argument_list; 381, 382; 381, 392; 382, call; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, string:"\t"; 385, identifier:join; 386, argument_list; 386, 387; 387, call; 387, 388; 387, 391; 388, attribute; 388, 389; 388, 390; 389, identifier:data; 390, identifier:values; 391, argument_list; 392, keyword_argument; 392, 393; 392, 394; 393, identifier:file; 394, identifier:f; 395, expression_statement; 395, 396; 396, call; 396, 397; 396, 398; 397, identifier:print; 398, argument_list; 398, 399; 398, 400; 399, identifier:end_key; 400, keyword_argument; 400, 401; 400, 402; 401, identifier:file; 402, identifier:f; 403, else_clause; 403, 404; 404, block; 404, 405; 405, expression_statement; 405, 406; 406, call; 406, 407; 406, 408; 407, identifier:print; 408, argument_list; 408, 409; 408, 428; 409, call; 409, 410; 409, 413; 410, attribute; 410, 411; 410, 412; 411, string:"{}{}{}\t{}"; 412, identifier:format; 413, argument_list; 413, 414; 413, 423; 413, 424; 413, 427; 414, call; 414, 415; 414, 420; 415, attribute; 415, 416; 415, 419; 416, attribute; 416, 417; 416, 418; 417, identifier:self; 418, identifier:prefixes; 419, identifier:get; 420, argument_list; 420, 421; 420, 422; 421, identifier:section_key; 422, string:""; 423, identifier:key; 424, binary_operator:*; 424, 425; 424, 426; 425, identifier:cw; 426, string:" "; 427, identifier:value; 428, keyword_argument; 428, 429; 428, 430; 429, identifier:file; 430, identifier:f; 431, elif_clause; 431, 432; 431, 435; 432, comparison_operator:==; 432, 433; 432, 434; 433, identifier:file_format; 434, string:"json"; 435, block; 435, 436; 436, expression_statement; 436, 437; 437, call; 437, 438; 437, 439; 438, identifier:print; 439, argument_list; 439, 440; 439, 454; 440, call; 440, 441; 440, 444; 441, attribute; 441, 442; 441, 443; 442, identifier:json; 443, identifier:dumps; 444, argument_list; 444, 445; 444, 448; 444, 451; 445, subscript; 445, 446; 445, 447; 446, identifier:self; 447, identifier:section_key; 448, keyword_argument; 448, 449; 448, 450; 449, identifier:sort_keys; 450, False; 451, keyword_argument; 451, 452; 451, 453; 452, identifier:indent; 453, integer:4; 454, keyword_argument; 454, 455; 454, 456; 455, identifier:file; 456, identifier:f
def print_block(self, section_key, f=sys.stdout, file_format="mwtab"): """Print `mwtab` section into a file or stdout. :param str section_key: Section name. :param io.StringIO f: writable file-like stream. :param str file_format: Format to use: `mwtab` or `json`. :return: None :rtype: :py:obj:`None` """ if file_format == "mwtab": for key, value in self[section_key].items(): if section_key == "METABOLOMICS WORKBENCH" and key not in ("VERSION", "CREATED_ON"): continue if key in ("VERSION", "CREATED_ON"): cw = 20 - len(key) elif key in ("SUBJECT_SAMPLE_FACTORS", ): cw = 33 - len(key) else: cw = 30 - len(key) if "\n" in value: for line in value.split("\n"): print("{}{}{}\t{}".format(self.prefixes.get(section_key, ""), key, cw * " ", line), file=f) elif key == "SUBJECT_SAMPLE_FACTORS": for factor in value: print("{}{}\t{}".format(key, cw * " ", "\t".join(factor.values())), file=f) elif key.endswith(":UNITS"): print("{}\t{}".format(key, value), file=f) elif key.endswith("_RESULTS_FILE"): if isinstance(value, dict): print("{}{} \t{}\t{}:{}".format(self.prefixes.get(section_key, ""), *[i for pair in value.items() for i in pair]), file=f) else: print("{}{}{}\t{}".format(self.prefixes.get(section_key, ""), key, cw * " ", value), file=f) elif key.endswith("_START"): start_key = key end_key = "{}{}".format(start_key[:-5], "END") print(start_key, file=f) for data_key in value: if data_key in ("Samples", "Factors"): print("{}\t{}".format(data_key, "\t".join(self[section_key][key][data_key])), file=f) elif data_key in ("Fields", ): print("{}".format("\t".join(self[section_key][key][data_key])), file=f) elif data_key == "DATA": for data in self[section_key][key][data_key]: print("\t".join(data.values()), file=f) print(end_key, file=f) else: print("{}{}{}\t{}".format(self.prefixes.get(section_key, ""), key, cw * " ", value), file=f) elif file_format == "json": print(json.dumps(self[section_key], sort_keys=False, indent=4), file=f)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:retry; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:ExceptionToCheck; 5, default_parameter; 5, 6; 5, 7; 6, identifier:tries; 7, integer:4; 8, default_parameter; 8, 9; 8, 10; 9, identifier:delay; 10, integer:3; 11, default_parameter; 11, 12; 11, 13; 12, identifier:backoff; 13, integer:2; 14, default_parameter; 14, 15; 14, 16; 15, identifier:status_codes; 16, list:[]; 17, default_parameter; 17, 18; 17, 19; 18, identifier:logger; 19, None; 20, block; 20, 21; 20, 23; 20, 37; 20, 46; 20, 56; 20, 70; 20, 175; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 31; 24, boolean_operator:or; 24, 25; 24, 28; 25, comparison_operator:is; 25, 26; 25, 27; 26, identifier:backoff; 27, None; 28, comparison_operator:<=; 28, 29; 28, 30; 29, identifier:backoff; 30, integer:0; 31, block; 31, 32; 32, raise_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:ValueError; 35, argument_list; 35, 36; 36, string:"backoff must be a number greater than 0"; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:tries; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:math; 43, identifier:floor; 44, argument_list; 44, 45; 45, identifier:tries; 46, if_statement; 46, 47; 46, 50; 47, comparison_operator:<; 47, 48; 47, 49; 48, identifier:tries; 49, integer:0; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:ValueError; 54, argument_list; 54, 55; 55, string:"tries must be a number 0 or greater"; 56, if_statement; 56, 57; 56, 64; 57, boolean_operator:or; 57, 58; 57, 61; 58, comparison_operator:is; 58, 59; 58, 60; 59, identifier:delay; 60, None; 61, comparison_operator:<=; 61, 62; 61, 63; 62, identifier:delay; 63, integer:0; 64, block; 64, 65; 65, raise_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:ValueError; 68, argument_list; 68, 69; 69, string:"delay must be a number greater than 0"; 70, function_definition; 70, 71; 70, 72; 70, 74; 71, function_name:deco_retry; 72, parameters; 72, 73; 73, identifier:f; 74, block; 74, 75; 74, 173; 75, function_definition; 75, 76; 75, 77; 75, 82; 76, function_name:f_retry; 77, parameters; 77, 78; 77, 80; 78, list_splat_pattern; 78, 79; 79, identifier:args; 80, dictionary_splat_pattern; 80, 81; 81, identifier:kwargs; 82, block; 82, 83; 82, 91; 82, 165; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, pattern_list; 85, 86; 85, 87; 86, identifier:mtries; 87, identifier:mdelay; 88, expression_list; 88, 89; 88, 90; 89, identifier:tries; 90, identifier:delay; 91, while_statement; 91, 92; 91, 95; 92, comparison_operator:>; 92, 93; 92, 94; 93, identifier:mtries; 94, integer:1; 95, block; 95, 96; 96, try_statement; 96, 97; 96, 106; 97, block; 97, 98; 98, return_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:f; 101, argument_list; 101, 102; 101, 104; 102, list_splat; 102, 103; 103, identifier:args; 104, dictionary_splat; 104, 105; 105, identifier:kwargs; 106, except_clause; 106, 107; 106, 111; 107, as_pattern; 107, 108; 107, 109; 108, identifier:ExceptionToCheck; 109, as_pattern_target; 109, 110; 110, identifier:err; 111, block; 111, 112; 111, 133; 111, 150; 111, 157; 111, 161; 112, if_statement; 112, 113; 112, 131; 113, parenthesized_expression; 113, 114; 114, boolean_operator:and; 114, 115; 114, 126; 115, boolean_operator:and; 115, 116; 115, 122; 116, comparison_operator:is; 116, 117; 116, 121; 117, call; 117, 118; 117, 119; 118, identifier:type; 119, argument_list; 119, 120; 120, identifier:err; 121, identifier:DataFailureException; 122, call; 122, 123; 122, 124; 123, identifier:len; 124, argument_list; 124, 125; 125, identifier:status_codes; 126, comparison_operator:not; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:err; 129, identifier:status; 130, identifier:status_codes; 131, block; 131, 132; 132, raise_statement; 133, if_statement; 133, 134; 133, 135; 134, identifier:logger; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:logger; 140, identifier:warning; 141, argument_list; 141, 142; 142, binary_operator:%; 142, 143; 142, 144; 143, string:'%s: %s, Retrying in %s seconds.'; 144, tuple; 144, 145; 144, 148; 144, 149; 145, attribute; 145, 146; 145, 147; 146, identifier:f; 147, identifier:__name__; 148, identifier:err; 149, identifier:mdelay; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:time; 154, identifier:sleep; 155, argument_list; 155, 156; 156, identifier:mdelay; 157, expression_statement; 157, 158; 158, augmented_assignment:-=; 158, 159; 158, 160; 159, identifier:mtries; 160, integer:1; 161, expression_statement; 161, 162; 162, augmented_assignment:*=; 162, 163; 162, 164; 163, identifier:mdelay; 164, identifier:backoff; 165, return_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:f; 168, argument_list; 168, 169; 168, 171; 169, list_splat; 169, 170; 170, identifier:args; 171, dictionary_splat; 171, 172; 172, identifier:kwargs; 173, return_statement; 173, 174; 174, identifier:f_retry; 175, return_statement; 175, 176; 176, identifier:deco_retry
def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, status_codes=[], logger=None): """ Decorator function for retrying the decorated function, using an exponential or fixed backoff. Original: https://wiki.python.org/moin/PythonDecoratorLibrary#Retry ExceptionToCheck: the exception to check. Can be a tuple of exceptions to check tries: number of times to try (not retry) before giving up delay: initial delay between tries in seconds backoff: backoff multiplier status_codes: list of http status codes to check for retrying, only applies when ExceptionToCheck is a DataFailureException logger: logging.Logger instance """ if backoff is None or backoff <= 0: raise ValueError("backoff must be a number greater than 0") tries = math.floor(tries) if tries < 0: raise ValueError("tries must be a number 0 or greater") if delay is None or delay <= 0: raise ValueError("delay must be a number greater than 0") def deco_retry(f): def f_retry(*args, **kwargs): mtries, mdelay = tries, delay while mtries > 1: try: return f(*args, **kwargs) except ExceptionToCheck as err: if (type(err) is DataFailureException and len(status_codes) and err.status not in status_codes): raise if logger: logger.warning('%s: %s, Retrying in %s seconds.' % ( f.__name__, err, mdelay)) time.sleep(mdelay) mtries -= 1 mdelay *= backoff return f(*args, **kwargs) return f_retry return deco_retry
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:lcopt_bw2_autosetup; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 4, default_parameter; 4, 5; 4, 6; 5, identifier:ei_username; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:ei_password; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:write_config; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:ecoinvent_version; 15, string:'3.3'; 16, default_parameter; 16, 17; 16, 18; 17, identifier:ecoinvent_system_model; 18, string:"cutoff"; 19, default_parameter; 19, 20; 19, 21; 20, identifier:overwrite; 21, False; 22, block; 22, 23; 22, 25; 22, 41; 22, 47; 22, 48; 22, 82; 22, 88; 22, 89; 22, 161; 22, 182; 22, 183; 22, 229; 22, 241; 22, 255; 22, 274; 22, 314; 22, 315; 22, 429; 22, 454; 22, 463; 23, expression_statement; 23, 24; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:ei_name; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, string:"Ecoinvent{}_{}_{}"; 31, identifier:format; 32, argument_list; 32, 33; 32, 40; 33, list_splat; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:ecoinvent_version; 37, identifier:split; 38, argument_list; 38, 39; 39, string:'.'; 40, identifier:ecoinvent_system_model; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:config; 44, call; 44, 45; 44, 46; 45, identifier:check_for_config; 46, argument_list; 47, comment; 48, if_statement; 48, 49; 48, 52; 49, comparison_operator:is; 49, 50; 49, 51; 50, identifier:config; 51, None; 52, block; 52, 53; 52, 57; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:config; 56, identifier:DEFAULT_CONFIG; 57, with_statement; 57, 58; 57, 70; 58, with_clause; 58, 59; 59, with_item; 59, 60; 60, as_pattern; 60, 61; 60, 68; 61, call; 61, 62; 61, 63; 62, identifier:open; 63, argument_list; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:storage; 66, identifier:config_file; 67, string:"w"; 68, as_pattern_target; 68, 69; 69, identifier:cfg; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:yaml; 75, identifier:dump; 76, argument_list; 76, 77; 76, 78; 76, 79; 77, identifier:config; 78, identifier:cfg; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:default_flow_style; 81, False; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:store_option; 85, attribute; 85, 86; 85, 87; 86, identifier:storage; 87, identifier:project_type; 88, comment; 89, if_statement; 89, 90; 89, 93; 89, 129; 90, comparison_operator:==; 90, 91; 90, 92; 91, identifier:store_option; 92, string:'single'; 93, block; 93, 94; 93, 100; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:project_name; 97, attribute; 97, 98; 97, 99; 98, identifier:storage; 99, identifier:single_project_name; 100, if_statement; 100, 101; 100, 105; 101, call; 101, 102; 101, 103; 102, identifier:bw2_project_exists; 103, argument_list; 103, 104; 104, identifier:project_name; 105, block; 105, 106; 105, 115; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:bw2; 111, identifier:projects; 112, identifier:set_current; 113, argument_list; 113, 114; 114, identifier:project_name; 115, if_statement; 115, 116; 115, 125; 115, 126; 116, boolean_operator:and; 116, 117; 116, 122; 117, comparison_operator:in; 117, 118; 117, 119; 118, identifier:ei_name; 119, attribute; 119, 120; 119, 121; 120, identifier:bw2; 121, identifier:databases; 122, comparison_operator:==; 122, 123; 122, 124; 123, identifier:overwrite; 124, False; 125, comment; 126, block; 126, 127; 127, return_statement; 127, 128; 128, True; 129, else_clause; 129, 130; 129, 131; 130, comment; 131, block; 131, 132; 131, 138; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:project_name; 135, binary_operator:+; 135, 136; 135, 137; 136, identifier:DEFAULT_PROJECT_STEM; 137, identifier:ei_name; 138, if_statement; 138, 139; 138, 143; 139, call; 139, 140; 139, 141; 140, identifier:bw2_project_exists; 141, argument_list; 141, 142; 142, identifier:project_name; 143, block; 143, 144; 144, if_statement; 144, 145; 144, 146; 145, identifier:overwrite; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:bw2; 152, identifier:projects; 153, identifier:delete_project; 154, argument_list; 154, 155; 154, 158; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:name; 157, identifier:project_name; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:delete_dir; 160, True; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:auto_ecoinvent; 164, call; 164, 165; 164, 166; 165, identifier:partial; 166, argument_list; 166, 167; 166, 170; 166, 173; 166, 176; 166, 179; 167, attribute; 167, 168; 167, 169; 168, identifier:eidl; 169, identifier:get_ecoinvent; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:db_name; 172, identifier:ei_name; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:auto_write; 175, True; 176, keyword_argument; 176, 177; 176, 178; 177, identifier:version; 178, identifier:ecoinvent_version; 179, keyword_argument; 179, 180; 179, 181; 180, identifier:system_model; 181, identifier:ecoinvent_system_model; 182, comment; 183, if_statement; 183, 184; 183, 187; 184, comparison_operator:is; 184, 185; 184, 186; 185, identifier:config; 186, None; 187, block; 187, 188; 188, if_statement; 188, 189; 188, 192; 189, comparison_operator:in; 189, 190; 189, 191; 190, string:"ecoinvent"; 191, identifier:config; 192, block; 192, 193; 192, 209; 192, 225; 193, if_statement; 193, 194; 193, 197; 194, comparison_operator:is; 194, 195; 194, 196; 195, identifier:ei_username; 196, None; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:ei_username; 201, call; 201, 202; 201, 207; 202, attribute; 202, 203; 202, 206; 203, subscript; 203, 204; 203, 205; 204, identifier:config; 205, string:'ecoinvent'; 206, identifier:get; 207, argument_list; 207, 208; 208, string:'username'; 209, if_statement; 209, 210; 209, 213; 210, comparison_operator:is; 210, 211; 210, 212; 211, identifier:ei_password; 212, None; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:ei_password; 217, call; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, subscript; 219, 220; 219, 221; 220, identifier:config; 221, string:'ecoinvent'; 222, identifier:get; 223, argument_list; 223, 224; 224, string:'password'; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:write_config; 228, False; 229, if_statement; 229, 230; 229, 233; 230, comparison_operator:is; 230, 231; 230, 232; 231, identifier:ei_username; 232, None; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:ei_username; 237, call; 237, 238; 237, 239; 238, identifier:input; 239, argument_list; 239, 240; 240, string:'ecoinvent username: '; 241, if_statement; 241, 242; 241, 245; 242, comparison_operator:is; 242, 243; 242, 244; 243, identifier:ei_password; 244, None; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:ei_password; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:getpass; 252, identifier:getpass; 253, argument_list; 253, 254; 254, string:'ecoinvent password: '; 255, if_statement; 255, 256; 255, 259; 256, comparison_operator:is; 256, 257; 256, 258; 257, identifier:write_config; 258, None; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:write_config; 263, comparison_operator:in; 263, 264; 263, 268; 264, call; 264, 265; 264, 266; 265, identifier:input; 266, argument_list; 266, 267; 267, string:'store username and password on this computer? y/[n]'; 268, list:['y', 'Y', 'yes', 'YES', 'Yes']; 268, 269; 268, 270; 268, 271; 268, 272; 268, 273; 269, string:'y'; 270, string:'Y'; 271, string:'yes'; 272, string:'YES'; 273, string:'Yes'; 274, if_statement; 274, 275; 274, 276; 275, identifier:write_config; 276, block; 276, 277; 276, 289; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 282; 279, subscript; 279, 280; 279, 281; 280, identifier:config; 281, string:'ecoinvent'; 282, dictionary; 282, 283; 282, 286; 283, pair; 283, 284; 283, 285; 284, string:'username'; 285, identifier:ei_username; 286, pair; 286, 287; 286, 288; 287, string:'password'; 288, identifier:ei_password; 289, with_statement; 289, 290; 289, 302; 290, with_clause; 290, 291; 291, with_item; 291, 292; 292, as_pattern; 292, 293; 292, 300; 293, call; 293, 294; 293, 295; 294, identifier:open; 295, argument_list; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:storage; 298, identifier:config_file; 299, string:"w"; 300, as_pattern_target; 300, 301; 301, identifier:cfg; 302, block; 302, 303; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:yaml; 307, identifier:dump; 308, argument_list; 308, 309; 308, 310; 308, 311; 309, identifier:config; 310, identifier:cfg; 311, keyword_argument; 311, 312; 311, 313; 312, identifier:default_flow_style; 313, False; 314, comment; 315, if_statement; 315, 316; 315, 319; 315, 388; 316, comparison_operator:==; 316, 317; 316, 318; 317, identifier:store_option; 318, string:'single'; 319, block; 319, 320; 320, if_statement; 320, 321; 320, 325; 320, 335; 321, call; 321, 322; 321, 323; 322, identifier:bw2_project_exists; 323, argument_list; 323, 324; 324, identifier:project_name; 325, block; 325, 326; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 333; 328, attribute; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:bw2; 331, identifier:projects; 332, identifier:set_current; 333, argument_list; 333, 334; 334, identifier:project_name; 335, else_clause; 335, 336; 336, block; 336, 337; 337, if_statement; 337, 338; 337, 343; 337, 359; 338, not_operator; 338, 339; 339, call; 339, 340; 339, 341; 340, identifier:bw2_project_exists; 341, argument_list; 341, 342; 342, identifier:DEFAULT_BIOSPHERE_PROJECT; 343, block; 343, 344; 343, 353; 344, expression_statement; 344, 345; 345, call; 345, 346; 345, 351; 346, attribute; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:bw2; 349, identifier:projects; 350, identifier:set_current; 351, argument_list; 351, 352; 352, identifier:project_name; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:bw2; 357, identifier:bw2setup; 358, argument_list; 359, else_clause; 359, 360; 360, block; 360, 361; 360, 370; 360, 376; 361, expression_statement; 361, 362; 362, call; 362, 363; 362, 368; 363, attribute; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:bw2; 366, identifier:projects; 367, identifier:set_current; 368, argument_list; 368, 369; 369, identifier:DEFAULT_BIOSPHERE_PROJECT; 370, expression_statement; 370, 371; 371, call; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:bw2; 374, identifier:create_core_migrations; 375, argument_list; 376, expression_statement; 376, 377; 377, call; 377, 378; 377, 383; 378, attribute; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:bw2; 381, identifier:projects; 382, identifier:copy_project; 383, argument_list; 383, 384; 383, 385; 384, identifier:project_name; 385, keyword_argument; 385, 386; 385, 387; 386, identifier:switch; 387, True; 388, else_clause; 388, 389; 388, 390; 389, comment; 390, block; 390, 391; 390, 402; 390, 411; 390, 417; 391, if_statement; 391, 392; 391, 397; 392, not_operator; 392, 393; 393, call; 393, 394; 393, 395; 394, identifier:bw2_project_exists; 395, argument_list; 395, 396; 396, identifier:DEFAULT_BIOSPHERE_PROJECT; 397, block; 397, 398; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 401; 400, identifier:lcopt_biosphere_setup; 401, argument_list; 402, expression_statement; 402, 403; 403, call; 403, 404; 403, 409; 404, attribute; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, identifier:bw2; 407, identifier:projects; 408, identifier:set_current; 409, argument_list; 409, 410; 410, identifier:DEFAULT_BIOSPHERE_PROJECT; 411, expression_statement; 411, 412; 412, call; 412, 413; 412, 416; 413, attribute; 413, 414; 413, 415; 414, identifier:bw2; 415, identifier:create_core_migrations; 416, argument_list; 417, expression_statement; 417, 418; 418, call; 418, 419; 418, 424; 419, attribute; 419, 420; 419, 423; 420, attribute; 420, 421; 420, 422; 421, identifier:bw2; 422, identifier:projects; 423, identifier:copy_project; 424, argument_list; 424, 425; 424, 426; 425, identifier:project_name; 426, keyword_argument; 426, 427; 426, 428; 427, identifier:switch; 428, True; 429, if_statement; 429, 430; 429, 437; 429, 448; 430, boolean_operator:and; 430, 431; 430, 434; 431, comparison_operator:is; 431, 432; 431, 433; 432, identifier:ei_username; 433, None; 434, comparison_operator:is; 434, 435; 434, 436; 435, identifier:ei_password; 436, None; 437, block; 437, 438; 438, expression_statement; 438, 439; 439, call; 439, 440; 439, 441; 440, identifier:auto_ecoinvent; 441, argument_list; 441, 442; 441, 445; 442, keyword_argument; 442, 443; 442, 444; 443, identifier:username; 444, identifier:ei_username; 445, keyword_argument; 445, 446; 445, 447; 446, identifier:password; 447, identifier:ei_password; 448, else_clause; 448, 449; 449, block; 449, 450; 450, expression_statement; 450, 451; 451, call; 451, 452; 451, 453; 452, identifier:auto_ecoinvent; 453, argument_list; 454, expression_statement; 454, 455; 455, call; 455, 456; 455, 457; 456, identifier:write_search_index; 457, argument_list; 457, 458; 457, 459; 457, 460; 458, identifier:project_name; 459, identifier:ei_name; 460, keyword_argument; 460, 461; 460, 462; 461, identifier:overwrite; 462, identifier:overwrite; 463, return_statement; 463, 464; 464, True
def lcopt_bw2_autosetup(ei_username=None, ei_password=None, write_config=None, ecoinvent_version='3.3', ecoinvent_system_model = "cutoff", overwrite=False): """ Utility function to automatically set up brightway2 to work correctly with lcopt. It requires a valid username and password to login to the ecoinvent website. These can be entered directly into the function using the keyword arguments `ei_username` and `ei_password` or entered interactively by using no arguments. `ecoinvent_version` needs to be a string representation of a valid ecoinvent database, at time of writing these are "3.01", "3.1", "3.2", "3.3", "3.4" `ecoinvent_system_model` needs to be one of "cutoff", "apos", "consequential" To overwrite an existing version, set overwrite=True """ ei_name = "Ecoinvent{}_{}_{}".format(*ecoinvent_version.split('.'), ecoinvent_system_model) config = check_for_config() # If, for some reason, there's no config file, write the defaults if config is None: config = DEFAULT_CONFIG with open(storage.config_file, "w") as cfg: yaml.dump(config, cfg, default_flow_style=False) store_option = storage.project_type # Check if there's already a project set up that matches the current configuration if store_option == 'single': project_name = storage.single_project_name if bw2_project_exists(project_name): bw2.projects.set_current(project_name) if ei_name in bw2.databases and overwrite == False: #print ('{} is already set up'.format(ei_name)) return True else: # default to 'unique' project_name = DEFAULT_PROJECT_STEM + ei_name if bw2_project_exists(project_name): if overwrite: bw2.projects.delete_project(name=project_name, delete_dir=True) auto_ecoinvent = partial(eidl.get_ecoinvent,db_name=ei_name, auto_write=True, version=ecoinvent_version, system_model=ecoinvent_system_model) # check for a config file (lcopt_config.yml) if config is not None: if "ecoinvent" in config: if ei_username is None: ei_username = config['ecoinvent'].get('username') if ei_password is None: ei_password = config['ecoinvent'].get('password') write_config = False if ei_username is None: ei_username = input('ecoinvent username: ') if ei_password is None: ei_password = getpass.getpass('ecoinvent password: ') if write_config is None: write_config = input('store username and password on this computer? y/[n]') in ['y', 'Y', 'yes', 'YES', 'Yes'] if write_config: config['ecoinvent'] = { 'username': ei_username, 'password': ei_password } with open(storage.config_file, "w") as cfg: yaml.dump(config, cfg, default_flow_style=False) # no need to keep running bw2setup - we can just copy a blank project which has been set up before if store_option == 'single': if bw2_project_exists(project_name): bw2.projects.set_current(project_name) else: if not bw2_project_exists(DEFAULT_BIOSPHERE_PROJECT): bw2.projects.set_current(project_name) bw2.bw2setup() else: bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT) bw2.create_core_migrations() bw2.projects.copy_project(project_name, switch=True) else: #if store_option == 'unique': if not bw2_project_exists(DEFAULT_BIOSPHERE_PROJECT): lcopt_biosphere_setup() bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT) bw2.create_core_migrations() bw2.projects.copy_project(project_name, switch=True) if ei_username is not None and ei_password is not None: auto_ecoinvent(username=ei_username, password=ei_password) else: auto_ecoinvent() write_search_index(project_name, ei_name, overwrite=overwrite) return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:lcopt_bw2_forwast_setup; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 4, default_parameter; 4, 5; 4, 6; 5, identifier:use_autodownload; 6, True; 7, default_parameter; 7, 8; 7, 9; 8, identifier:forwast_path; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:db_name; 12, identifier:FORWAST_PROJECT_NAME; 13, default_parameter; 13, 14; 13, 15; 14, identifier:overwrite; 15, False; 16, block; 16, 17; 16, 19; 16, 45; 16, 164; 16, 173; 17, expression_statement; 17, 18; 18, comment; 19, if_statement; 19, 20; 19, 21; 19, 29; 19, 38; 20, identifier:use_autodownload; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:forwast_filepath; 25, call; 25, 26; 25, 27; 26, identifier:forwast_autodownload; 27, argument_list; 27, 28; 28, identifier:FORWAST_URL; 29, elif_clause; 29, 30; 29, 33; 30, comparison_operator:is; 30, 31; 30, 32; 31, identifier:forwast_path; 32, None; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:forwast_filepath; 37, identifier:forwast_path; 38, else_clause; 38, 39; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:ValueError; 43, argument_list; 43, 44; 44, string:'Need a path if not using autodownload'; 45, if_statement; 45, 46; 45, 51; 45, 90; 46, comparison_operator:==; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:storage; 49, identifier:project_type; 50, string:'single'; 51, block; 51, 52; 51, 58; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:db_name; 55, attribute; 55, 56; 55, 57; 56, identifier:storage; 57, identifier:single_project_name; 58, if_statement; 58, 59; 58, 63; 58, 73; 59, call; 59, 60; 59, 61; 60, identifier:bw2_project_exists; 61, argument_list; 61, 62; 62, identifier:db_name; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:bw2; 69, identifier:projects; 70, identifier:set_current; 71, argument_list; 71, 72; 72, identifier:db_name; 73, else_clause; 73, 74; 74, block; 74, 75; 74, 84; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:bw2; 80, identifier:projects; 81, identifier:set_current; 82, argument_list; 82, 83; 83, identifier:db_name; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:bw2; 88, identifier:bw2setup; 89, argument_list; 90, else_clause; 90, 91; 91, block; 91, 92; 91, 125; 91, 126; 91, 137; 91, 146; 91, 152; 92, if_statement; 92, 93; 92, 98; 93, comparison_operator:in; 93, 94; 93, 95; 94, identifier:db_name; 95, attribute; 95, 96; 95, 97; 96, identifier:bw2; 97, identifier:projects; 98, block; 98, 99; 99, if_statement; 99, 100; 99, 101; 99, 116; 100, identifier:overwrite; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:bw2; 107, identifier:projects; 108, identifier:delete_project; 109, argument_list; 109, 110; 109, 113; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:name; 112, identifier:db_name; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:delete_dir; 115, True; 116, else_clause; 116, 117; 117, block; 117, 118; 117, 123; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:print; 121, argument_list; 121, 122; 122, string:'Looks like bw2 is already set up for the FORWAST database - if you want to overwrite the existing version run lcopt.utils.lcopt_bw2_forwast_setup in a python shell using overwrite = True'; 123, return_statement; 123, 124; 124, False; 125, comment; 126, if_statement; 126, 127; 126, 132; 127, not_operator; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:bw2_project_exists; 130, argument_list; 130, 131; 131, identifier:DEFAULT_BIOSPHERE_PROJECT; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:lcopt_biosphere_setup; 136, argument_list; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 144; 139, attribute; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:bw2; 142, identifier:projects; 143, identifier:set_current; 144, argument_list; 144, 145; 145, identifier:DEFAULT_BIOSPHERE_PROJECT; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:bw2; 150, identifier:create_core_migrations; 151, argument_list; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:bw2; 157, identifier:projects; 158, identifier:copy_project; 159, argument_list; 159, 160; 159, 161; 160, identifier:db_name; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:switch; 163, True; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 171; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:bw2; 169, identifier:BW2Package; 170, identifier:import_file; 171, argument_list; 171, 172; 172, identifier:forwast_filepath; 173, return_statement; 173, 174; 174, True
def lcopt_bw2_forwast_setup(use_autodownload=True, forwast_path=None, db_name=FORWAST_PROJECT_NAME, overwrite=False): """ Utility function to set up brightway2 to work correctly with lcopt using the FORWAST database instead of ecoinvent By default it'll try and download the forwast database as a .bw2package file from lca-net If you've downloaded the forwast .bw2package file already you can set use_autodownload=False and forwast_path to point to the downloaded file To overwrite an existing version, set overwrite=True """ if use_autodownload: forwast_filepath = forwast_autodownload(FORWAST_URL) elif forwast_path is not None: forwast_filepath = forwast_path else: raise ValueError('Need a path if not using autodownload') if storage.project_type == 'single': db_name = storage.single_project_name if bw2_project_exists(db_name): bw2.projects.set_current(db_name) else: bw2.projects.set_current(db_name) bw2.bw2setup() else: if db_name in bw2.projects: if overwrite: bw2.projects.delete_project(name=db_name, delete_dir=True) else: print('Looks like bw2 is already set up for the FORWAST database - if you want to overwrite the existing version run lcopt.utils.lcopt_bw2_forwast_setup in a python shell using overwrite = True') return False # no need to keep running bw2setup - we can just copy a blank project which has been set up before if not bw2_project_exists(DEFAULT_BIOSPHERE_PROJECT): lcopt_biosphere_setup() bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT) bw2.create_core_migrations() bw2.projects.copy_project(db_name, switch=True) bw2.BW2Package.import_file(forwast_filepath) return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:_validate_samples_factors; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:mwtabfile; 5, default_parameter; 5, 6; 5, 7; 6, identifier:validate_samples; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:validate_factors; 10, True; 11, block; 11, 12; 11, 14; 11, 28; 11, 42; 11, 93; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:from_subject_samples; 17, set_comprehension; 17, 18; 17, 21; 18, subscript; 18, 19; 18, 20; 19, identifier:i; 20, string:"local_sample_id"; 21, for_in_clause; 21, 22; 21, 23; 22, identifier:i; 23, subscript; 23, 24; 23, 27; 24, subscript; 24, 25; 24, 26; 25, identifier:mwtabfile; 26, string:"SUBJECT_SAMPLE_FACTORS"; 27, string:"SUBJECT_SAMPLE_FACTORS"; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:from_subject_factors; 31, set_comprehension; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:i; 34, string:"factors"; 35, for_in_clause; 35, 36; 35, 37; 36, identifier:i; 37, subscript; 37, 38; 37, 41; 38, subscript; 38, 39; 38, 40; 39, identifier:mwtabfile; 40, string:"SUBJECT_SAMPLE_FACTORS"; 41, string:"SUBJECT_SAMPLE_FACTORS"; 42, if_statement; 42, 43; 42, 44; 43, identifier:validate_samples; 44, block; 44, 45; 44, 67; 45, if_statement; 45, 46; 45, 49; 46, comparison_operator:in; 46, 47; 46, 48; 47, string:"MS_METABOLITE_DATA"; 48, identifier:mwtabfile; 49, block; 49, 50; 49, 63; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:from_metabolite_data_samples; 53, call; 53, 54; 53, 55; 54, identifier:set; 55, argument_list; 55, 56; 56, subscript; 56, 57; 56, 62; 57, subscript; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:mwtabfile; 60, string:"MS_METABOLITE_DATA"; 61, string:"MS_METABOLITE_DATA_START"; 62, string:"Samples"; 63, assert_statement; 63, 64; 64, comparison_operator:==; 64, 65; 64, 66; 65, identifier:from_subject_samples; 66, identifier:from_metabolite_data_samples; 67, if_statement; 67, 68; 67, 71; 68, comparison_operator:in; 68, 69; 68, 70; 69, string:"NMR_BINNED_DATA"; 70, identifier:mwtabfile; 71, block; 71, 72; 71, 89; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:from_nmr_binned_data_samples; 75, call; 75, 76; 75, 77; 76, identifier:set; 77, argument_list; 77, 78; 78, subscript; 78, 79; 78, 86; 79, subscript; 79, 80; 79, 85; 80, subscript; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:mwtabfile; 83, string:"NMR_BINNED_DATA"; 84, string:"NMR_BINNED_DATA_START"; 85, string:"Fields"; 86, slice; 86, 87; 86, 88; 87, integer:1; 88, colon; 89, assert_statement; 89, 90; 90, comparison_operator:==; 90, 91; 90, 92; 91, identifier:from_subject_samples; 92, identifier:from_nmr_binned_data_samples; 93, if_statement; 93, 94; 93, 95; 94, identifier:validate_factors; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 100; 97, comparison_operator:in; 97, 98; 97, 99; 98, string:"MS_METABOLITE_DATA"; 99, identifier:mwtabfile; 100, block; 100, 101; 100, 114; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:from_metabolite_data_factors; 104, call; 104, 105; 104, 106; 105, identifier:set; 106, argument_list; 106, 107; 107, subscript; 107, 108; 107, 113; 108, subscript; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:mwtabfile; 111, string:"MS_METABOLITE_DATA"; 112, string:"MS_METABOLITE_DATA_START"; 113, string:"Factors"; 114, assert_statement; 114, 115; 115, comparison_operator:==; 115, 116; 115, 117; 116, identifier:from_subject_factors; 117, identifier:from_metabolite_data_factors
def _validate_samples_factors(mwtabfile, validate_samples=True, validate_factors=True): """Validate ``Samples`` and ``Factors`` identifiers across the file. :param mwtabfile: Instance of :class:`~mwtab.mwtab.MWTabFile`. :type mwtabfile: :class:`~mwtab.mwtab.MWTabFile` :return: None :rtype: :py:obj:`None` """ from_subject_samples = {i["local_sample_id"] for i in mwtabfile["SUBJECT_SAMPLE_FACTORS"]["SUBJECT_SAMPLE_FACTORS"]} from_subject_factors = {i["factors"] for i in mwtabfile["SUBJECT_SAMPLE_FACTORS"]["SUBJECT_SAMPLE_FACTORS"]} if validate_samples: if "MS_METABOLITE_DATA" in mwtabfile: from_metabolite_data_samples = set(mwtabfile["MS_METABOLITE_DATA"]["MS_METABOLITE_DATA_START"]["Samples"]) assert from_subject_samples == from_metabolite_data_samples if "NMR_BINNED_DATA" in mwtabfile: from_nmr_binned_data_samples = set(mwtabfile["NMR_BINNED_DATA"]["NMR_BINNED_DATA_START"]["Fields"][1:]) assert from_subject_samples == from_nmr_binned_data_samples if validate_factors: if "MS_METABOLITE_DATA" in mwtabfile: from_metabolite_data_factors = set(mwtabfile["MS_METABOLITE_DATA"]["MS_METABOLITE_DATA_START"]["Factors"]) assert from_subject_factors == from_metabolite_data_factors
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:convert; 3, parameters; 3, 4; 4, identifier:schema; 5, block; 5, 6; 5, 8; 5, 9; 5, 24; 5, 155; 5, 186; 5, 235; 5, 280; 5, 299; 5, 350; 5, 381; 5, 396; 5, 408; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 17; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 14; 13, identifier:schema; 14, attribute; 14, 15; 14, 16; 15, identifier:vol; 16, identifier:Schema; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:schema; 21, attribute; 21, 22; 21, 23; 22, identifier:schema; 23, identifier:schema; 24, if_statement; 24, 25; 24, 30; 25, call; 25, 26; 25, 27; 26, identifier:isinstance; 27, argument_list; 27, 28; 27, 29; 28, identifier:schema; 29, identifier:Mapping; 30, block; 30, 31; 30, 35; 30, 153; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:val; 34, list:[]; 35, for_statement; 35, 36; 35, 39; 35, 44; 36, pattern_list; 36, 37; 36, 38; 37, identifier:key; 38, identifier:value; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:schema; 42, identifier:items; 43, argument_list; 44, block; 44, 45; 44, 49; 44, 76; 44, 83; 44, 89; 44, 100; 44, 146; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:description; 48, None; 49, if_statement; 49, 50; 49, 57; 49, 70; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:key; 54, attribute; 54, 55; 54, 56; 55, identifier:vol; 56, identifier:Marker; 57, block; 57, 58; 57, 64; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:pkey; 61, attribute; 61, 62; 61, 63; 62, identifier:key; 63, identifier:schema; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:description; 67, attribute; 67, 68; 67, 69; 68, identifier:key; 69, identifier:description; 70, else_clause; 70, 71; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:pkey; 75, identifier:key; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:pval; 79, call; 79, 80; 79, 81; 80, identifier:convert; 81, argument_list; 81, 82; 82, identifier:value; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:pval; 87, string:'name'; 88, identifier:pkey; 89, if_statement; 89, 90; 89, 93; 90, comparison_operator:is; 90, 91; 90, 92; 91, identifier:description; 92, None; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, subscript; 96, 97; 96, 98; 97, identifier:pval; 98, string:'description'; 99, identifier:description; 100, if_statement; 100, 101; 100, 112; 101, call; 101, 102; 101, 103; 102, identifier:isinstance; 103, argument_list; 103, 104; 103, 105; 104, identifier:key; 105, tuple; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:vol; 108, identifier:Required; 109, attribute; 109, 110; 109, 111; 110, identifier:vol; 111, identifier:Optional; 112, block; 112, 113; 112, 127; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 126; 115, subscript; 115, 116; 115, 117; 116, identifier:pval; 117, call; 117, 118; 117, 125; 118, attribute; 118, 119; 118, 124; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:key; 122, identifier:__class__; 123, identifier:__name__; 124, identifier:lower; 125, argument_list; 126, True; 127, if_statement; 127, 128; 127, 135; 128, comparison_operator:is; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:key; 131, identifier:default; 132, attribute; 132, 133; 132, 134; 133, identifier:vol; 134, identifier:UNDEFINED; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:pval; 140, string:'default'; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:key; 144, identifier:default; 145, argument_list; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:val; 150, identifier:append; 151, argument_list; 151, 152; 152, identifier:pval; 153, return_statement; 153, 154; 154, identifier:val; 155, if_statement; 155, 156; 155, 163; 156, call; 156, 157; 156, 158; 157, identifier:isinstance; 158, argument_list; 158, 159; 158, 160; 159, identifier:schema; 160, attribute; 160, 161; 160, 162; 161, identifier:vol; 162, identifier:All; 163, block; 163, 164; 163, 168; 163, 184; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:val; 167, dictionary; 168, for_statement; 168, 169; 168, 170; 168, 173; 169, identifier:validator; 170, attribute; 170, 171; 170, 172; 171, identifier:schema; 172, identifier:validators; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:val; 178, identifier:update; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:convert; 182, argument_list; 182, 183; 183, identifier:validator; 184, return_statement; 184, 185; 185, identifier:val; 186, if_statement; 186, 187; 186, 198; 187, call; 187, 188; 187, 189; 188, identifier:isinstance; 189, argument_list; 189, 190; 189, 191; 190, identifier:schema; 191, tuple; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:vol; 194, identifier:Clamp; 195, attribute; 195, 196; 195, 197; 196, identifier:vol; 197, identifier:Range; 198, block; 198, 199; 198, 203; 198, 218; 198, 233; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:val; 202, dictionary; 203, if_statement; 203, 204; 203, 209; 204, comparison_operator:is; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:schema; 207, identifier:min; 208, None; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, subscript; 212, 213; 212, 214; 213, identifier:val; 214, string:'valueMin'; 215, attribute; 215, 216; 215, 217; 216, identifier:schema; 217, identifier:min; 218, if_statement; 218, 219; 218, 224; 219, comparison_operator:is; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:schema; 222, identifier:max; 223, None; 224, block; 224, 225; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, subscript; 227, 228; 227, 229; 228, identifier:val; 229, string:'valueMax'; 230, attribute; 230, 231; 230, 232; 231, identifier:schema; 232, identifier:max; 233, return_statement; 233, 234; 234, identifier:val; 235, if_statement; 235, 236; 235, 243; 236, call; 236, 237; 236, 238; 237, identifier:isinstance; 238, argument_list; 238, 239; 238, 240; 239, identifier:schema; 240, attribute; 240, 241; 240, 242; 241, identifier:vol; 242, identifier:Length; 243, block; 243, 244; 243, 248; 243, 263; 243, 278; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:val; 247, dictionary; 248, if_statement; 248, 249; 248, 254; 249, comparison_operator:is; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:schema; 252, identifier:min; 253, None; 254, block; 254, 255; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 260; 257, subscript; 257, 258; 257, 259; 258, identifier:val; 259, string:'lengthMin'; 260, attribute; 260, 261; 260, 262; 261, identifier:schema; 262, identifier:min; 263, if_statement; 263, 264; 263, 269; 264, comparison_operator:is; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:schema; 267, identifier:max; 268, None; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:val; 274, string:'lengthMax'; 275, attribute; 275, 276; 275, 277; 276, identifier:schema; 277, identifier:max; 278, return_statement; 278, 279; 279, identifier:val; 280, if_statement; 280, 281; 280, 288; 281, call; 281, 282; 281, 283; 282, identifier:isinstance; 283, argument_list; 283, 284; 283, 285; 284, identifier:schema; 285, attribute; 285, 286; 285, 287; 286, identifier:vol; 287, identifier:Datetime; 288, block; 288, 289; 289, return_statement; 289, 290; 290, dictionary; 290, 291; 290, 294; 291, pair; 291, 292; 291, 293; 292, string:'type'; 293, string:'datetime'; 294, pair; 294, 295; 294, 296; 295, string:'format'; 296, attribute; 296, 297; 296, 298; 297, identifier:schema; 298, identifier:format; 299, if_statement; 299, 300; 299, 307; 300, call; 300, 301; 300, 302; 301, identifier:isinstance; 302, argument_list; 302, 303; 302, 304; 303, identifier:schema; 304, attribute; 304, 305; 304, 306; 305, identifier:vol; 306, identifier:In; 307, block; 307, 308; 307, 334; 308, if_statement; 308, 309; 308, 316; 309, call; 309, 310; 309, 311; 310, identifier:isinstance; 311, argument_list; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:schema; 314, identifier:container; 315, identifier:Mapping; 316, block; 316, 317; 317, return_statement; 317, 318; 318, dictionary; 318, 319; 318, 322; 319, pair; 319, 320; 319, 321; 320, string:'type'; 321, string:'select'; 322, pair; 322, 323; 322, 324; 323, string:'options'; 324, call; 324, 325; 324, 326; 325, identifier:list; 326, argument_list; 326, 327; 327, call; 327, 328; 327, 333; 328, attribute; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:schema; 331, identifier:container; 332, identifier:items; 333, argument_list; 334, return_statement; 334, 335; 335, dictionary; 335, 336; 335, 339; 336, pair; 336, 337; 336, 338; 337, string:'type'; 338, string:'select'; 339, pair; 339, 340; 339, 341; 340, string:'options'; 341, list_comprehension; 341, 342; 341, 345; 342, tuple; 342, 343; 342, 344; 343, identifier:item; 344, identifier:item; 345, for_in_clause; 345, 346; 345, 347; 346, identifier:item; 347, attribute; 347, 348; 347, 349; 348, identifier:schema; 349, identifier:container; 350, if_statement; 350, 351; 350, 369; 351, comparison_operator:in; 351, 352; 351, 353; 352, identifier:schema; 353, tuple; 353, 354; 353, 357; 353, 360; 353, 363; 353, 366; 354, attribute; 354, 355; 354, 356; 355, identifier:vol; 356, identifier:Lower; 357, attribute; 357, 358; 357, 359; 358, identifier:vol; 359, identifier:Upper; 360, attribute; 360, 361; 360, 362; 361, identifier:vol; 362, identifier:Capitalize; 363, attribute; 363, 364; 363, 365; 364, identifier:vol; 365, identifier:Title; 366, attribute; 366, 367; 366, 368; 367, identifier:vol; 368, identifier:Strip; 369, block; 369, 370; 370, return_statement; 370, 371; 371, dictionary; 371, 372; 372, pair; 372, 373; 372, 380; 373, call; 373, 374; 373, 379; 374, attribute; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:schema; 377, identifier:__name__; 378, identifier:lower; 379, argument_list; 380, True; 381, if_statement; 381, 382; 381, 389; 382, call; 382, 383; 382, 384; 383, identifier:isinstance; 384, argument_list; 384, 385; 384, 386; 385, identifier:schema; 386, attribute; 386, 387; 386, 388; 387, identifier:vol; 388, identifier:Coerce; 389, block; 389, 390; 390, expression_statement; 390, 391; 391, assignment; 391, 392; 391, 393; 392, identifier:schema; 393, attribute; 393, 394; 393, 395; 394, identifier:schema; 395, identifier:type; 396, if_statement; 396, 397; 396, 400; 397, comparison_operator:in; 397, 398; 397, 399; 398, identifier:schema; 399, identifier:TYPES_MAP; 400, block; 400, 401; 401, return_statement; 401, 402; 402, dictionary; 402, 403; 403, pair; 403, 404; 403, 405; 404, string:'type'; 405, subscript; 405, 406; 405, 407; 406, identifier:TYPES_MAP; 407, identifier:schema; 408, raise_statement; 408, 409; 409, call; 409, 410; 409, 411; 410, identifier:ValueError; 411, argument_list; 411, 412; 412, call; 412, 413; 412, 416; 413, attribute; 413, 414; 413, 415; 414, string:'Unable to convert schema: {}'; 415, identifier:format; 416, argument_list; 416, 417; 417, identifier:schema
def convert(schema): """Convert a voluptuous schema to a dictionary.""" # pylint: disable=too-many-return-statements,too-many-branches if isinstance(schema, vol.Schema): schema = schema.schema if isinstance(schema, Mapping): val = [] for key, value in schema.items(): description = None if isinstance(key, vol.Marker): pkey = key.schema description = key.description else: pkey = key pval = convert(value) pval['name'] = pkey if description is not None: pval['description'] = description if isinstance(key, (vol.Required, vol.Optional)): pval[key.__class__.__name__.lower()] = True if key.default is not vol.UNDEFINED: pval['default'] = key.default() val.append(pval) return val if isinstance(schema, vol.All): val = {} for validator in schema.validators: val.update(convert(validator)) return val if isinstance(schema, (vol.Clamp, vol.Range)): val = {} if schema.min is not None: val['valueMin'] = schema.min if schema.max is not None: val['valueMax'] = schema.max return val if isinstance(schema, vol.Length): val = {} if schema.min is not None: val['lengthMin'] = schema.min if schema.max is not None: val['lengthMax'] = schema.max return val if isinstance(schema, vol.Datetime): return { 'type': 'datetime', 'format': schema.format, } if isinstance(schema, vol.In): if isinstance(schema.container, Mapping): return { 'type': 'select', 'options': list(schema.container.items()), } return { 'type': 'select', 'options': [(item, item) for item in schema.container] } if schema in (vol.Lower, vol.Upper, vol.Capitalize, vol.Title, vol.Strip): return { schema.__name__.lower(): True, } if isinstance(schema, vol.Coerce): schema = schema.type if schema in TYPES_MAP: return {'type': TYPES_MAP[schema]} raise ValueError('Unable to convert schema: {}'.format(schema))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_jsmin; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 21; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:theA; 13, string:'\n'; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_action; 19, argument_list; 19, 20; 20, integer:3; 21, while_statement; 21, 22; 21, 27; 22, comparison_operator:!=; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:theA; 26, string:'\000'; 27, block; 27, 28; 28, if_statement; 28, 29; 28, 34; 28, 59; 28, 125; 29, comparison_operator:==; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:theA; 33, string:' '; 34, block; 34, 35; 35, if_statement; 35, 36; 35, 42; 35, 50; 36, call; 36, 37; 36, 38; 37, identifier:isAlphanum; 38, argument_list; 38, 39; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:theB; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:_action; 48, argument_list; 48, 49; 49, integer:1; 50, else_clause; 50, 51; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_action; 57, argument_list; 57, 58; 58, integer:2; 59, elif_clause; 59, 60; 59, 65; 60, comparison_operator:==; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:theA; 64, string:'\n'; 65, block; 65, 66; 66, if_statement; 66, 67; 66, 77; 66, 85; 66, 99; 67, comparison_operator:in; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:theB; 71, list:['{', '[', '(', '+', '-']; 71, 72; 71, 73; 71, 74; 71, 75; 71, 76; 72, string:'{'; 73, string:'['; 74, string:'('; 75, string:'+'; 76, string:'-'; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:_action; 83, argument_list; 83, 84; 84, integer:1; 85, elif_clause; 85, 86; 85, 91; 86, comparison_operator:==; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:theB; 90, string:' '; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:_action; 97, argument_list; 97, 98; 98, integer:3; 99, else_clause; 99, 100; 100, block; 100, 101; 101, if_statement; 101, 102; 101, 108; 101, 116; 102, call; 102, 103; 102, 104; 103, identifier:isAlphanum; 104, argument_list; 104, 105; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:theB; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:_action; 114, argument_list; 114, 115; 115, integer:1; 116, else_clause; 116, 117; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:_action; 123, argument_list; 123, 124; 124, integer:2; 125, else_clause; 125, 126; 126, block; 126, 127; 127, if_statement; 127, 128; 127, 133; 127, 158; 127, 212; 128, comparison_operator:==; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:theB; 132, string:' '; 133, block; 133, 134; 134, if_statement; 134, 135; 134, 141; 134, 149; 135, call; 135, 136; 135, 137; 136, identifier:isAlphanum; 137, argument_list; 137, 138; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:theA; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:_action; 147, argument_list; 147, 148; 148, integer:1; 149, else_clause; 149, 150; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_action; 156, argument_list; 156, 157; 157, integer:3; 158, elif_clause; 158, 159; 158, 164; 159, comparison_operator:==; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:theB; 163, string:'\n'; 164, block; 164, 165; 165, if_statement; 165, 166; 165, 178; 165, 186; 166, comparison_operator:in; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:theA; 170, list:['}', ']', ')', '+', '-', '"', '\'']; 170, 171; 170, 172; 170, 173; 170, 174; 170, 175; 170, 176; 170, 177; 171, string:'}'; 172, string:']'; 173, string:')'; 174, string:'+'; 175, string:'-'; 176, string:'"'; 177, string:'\''; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:_action; 184, argument_list; 184, 185; 185, integer:1; 186, else_clause; 186, 187; 187, block; 187, 188; 188, if_statement; 188, 189; 188, 195; 188, 203; 189, call; 189, 190; 189, 191; 190, identifier:isAlphanum; 191, argument_list; 191, 192; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:theA; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:_action; 201, argument_list; 201, 202; 202, integer:1; 203, else_clause; 203, 204; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:_action; 210, argument_list; 210, 211; 211, integer:3; 212, else_clause; 212, 213; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:_action; 219, argument_list; 219, 220; 220, integer:1
def _jsmin(self): """Copy the input to the output, deleting the characters which are insignificant to JavaScript. Comments will be removed. Tabs will be replaced with spaces. Carriage returns will be replaced with linefeeds. Most spaces and linefeeds will be removed. """ self.theA = '\n' self._action(3) while self.theA != '\000': if self.theA == ' ': if isAlphanum(self.theB): self._action(1) else: self._action(2) elif self.theA == '\n': if self.theB in ['{', '[', '(', '+', '-']: self._action(1) elif self.theB == ' ': self._action(3) else: if isAlphanum(self.theB): self._action(1) else: self._action(2) else: if self.theB == ' ': if isAlphanum(self.theA): self._action(1) else: self._action(3) elif self.theB == '\n': if self.theA in ['}', ']', ')', '+', '-', '"', '\'']: self._action(1) else: if isAlphanum(self.theA): self._action(1) else: self._action(3) else: self._action(1)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:tokenizer; 3, parameters; 3, 4; 4, identifier:text; 5, block; 5, 6; 5, 8; 5, 20; 5, 441; 5, 448; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:stream; 11, call; 11, 12; 11, 13; 12, identifier:deque; 13, argument_list; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:text; 17, identifier:split; 18, argument_list; 18, 19; 19, string:"\n"; 20, while_statement; 20, 21; 20, 27; 21, comparison_operator:>; 21, 22; 21, 26; 22, call; 22, 23; 22, 24; 23, identifier:len; 24, argument_list; 24, 25; 25, identifier:stream; 26, integer:0; 27, block; 27, 28; 27, 36; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:line; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:stream; 34, identifier:popleft; 35, argument_list; 36, if_statement; 36, 37; 36, 43; 36, 90; 36, 105; 36, 127; 36, 153; 36, 190; 36, 264; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:line; 40, identifier:startswith; 41, argument_list; 41, 42; 42, string:"#METABOLOMICS WORKBENCH"; 43, block; 43, 44; 43, 51; 43, 58; 44, expression_statement; 44, 45; 45, yield; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:KeyValue; 48, argument_list; 48, 49; 48, 50; 49, string:"#METABOLOMICS WORKBENCH"; 50, string:"\n"; 51, expression_statement; 51, 52; 52, yield; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:KeyValue; 55, argument_list; 55, 56; 55, 57; 56, string:"HEADER"; 57, identifier:line; 58, for_statement; 58, 59; 58, 60; 58, 66; 59, identifier:identifier; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:line; 63, identifier:split; 64, argument_list; 64, 65; 65, string:" "; 66, block; 66, 67; 67, if_statement; 67, 68; 67, 71; 68, comparison_operator:in; 68, 69; 68, 70; 69, string:":"; 70, identifier:identifier; 71, block; 71, 72; 71, 83; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, pattern_list; 74, 75; 74, 76; 75, identifier:key; 76, identifier:value; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:identifier; 80, identifier:split; 81, argument_list; 81, 82; 82, string:":"; 83, expression_statement; 83, 84; 84, yield; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:KeyValue; 87, argument_list; 87, 88; 87, 89; 88, identifier:key; 89, identifier:value; 90, elif_clause; 90, 91; 90, 97; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:line; 94, identifier:startswith; 95, argument_list; 95, 96; 96, string:"#ANALYSIS TYPE"; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, yield; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:KeyValue; 102, argument_list; 102, 103; 102, 104; 103, string:"HEADER"; 104, identifier:line; 105, elif_clause; 105, 106; 105, 112; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:line; 109, identifier:startswith; 110, argument_list; 110, 111; 111, string:"#SUBJECT_SAMPLE_FACTORS:"; 112, block; 112, 113; 112, 120; 113, expression_statement; 113, 114; 114, yield; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:KeyValue; 117, argument_list; 117, 118; 117, 119; 118, string:"#ENDSECTION"; 119, string:"\n"; 120, expression_statement; 120, 121; 121, yield; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:KeyValue; 124, argument_list; 124, 125; 124, 126; 125, string:"#SUBJECT_SAMPLE_FACTORS"; 126, string:"\n"; 127, elif_clause; 127, 128; 127, 134; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:line; 131, identifier:startswith; 132, argument_list; 132, 133; 133, string:"#"; 134, block; 134, 135; 134, 142; 135, expression_statement; 135, 136; 136, yield; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:KeyValue; 139, argument_list; 139, 140; 139, 141; 140, string:"#ENDSECTION"; 141, string:"\n"; 142, expression_statement; 142, 143; 143, yield; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:KeyValue; 146, argument_list; 146, 147; 146, 152; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:line; 150, identifier:strip; 151, argument_list; 152, string:"\n"; 153, elif_clause; 153, 154; 153, 160; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:line; 157, identifier:startswith; 158, argument_list; 158, 159; 159, string:"SUBJECT_SAMPLE_FACTORS"; 160, block; 160, 161; 160, 175; 160, 176; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 169; 163, pattern_list; 163, 164; 163, 165; 163, 166; 163, 167; 163, 168; 164, identifier:key; 165, identifier:subject_type; 166, identifier:local_sample_id; 167, identifier:factors; 168, identifier:additional_sample_data; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:line; 172, identifier:split; 173, argument_list; 173, 174; 174, string:"\t"; 175, comment; 176, expression_statement; 176, 177; 177, yield; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:SubjectSampleFactors; 180, argument_list; 180, 181; 180, 186; 180, 187; 180, 188; 180, 189; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:key; 184, identifier:strip; 185, argument_list; 186, identifier:subject_type; 187, identifier:local_sample_id; 188, identifier:factors; 189, identifier:additional_sample_data; 190, elif_clause; 190, 191; 190, 197; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:line; 194, identifier:endswith; 195, argument_list; 195, 196; 196, string:"_START"; 197, block; 197, 198; 197, 205; 198, expression_statement; 198, 199; 199, yield; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:KeyValue; 202, argument_list; 202, 203; 202, 204; 203, identifier:line; 204, string:"\n"; 205, while_statement; 205, 206; 205, 213; 206, not_operator; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:line; 210, identifier:endswith; 211, argument_list; 211, 212; 212, string:"_END"; 213, block; 213, 214; 213, 222; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:line; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:stream; 220, identifier:popleft; 221, argument_list; 222, if_statement; 222, 223; 222, 229; 222, 241; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:line; 226, identifier:endswith; 227, argument_list; 227, 228; 228, string:"_END"; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, yield; 231, 232; 232, call; 232, 233; 232, 234; 233, identifier:KeyValue; 234, argument_list; 234, 235; 234, 240; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:line; 238, identifier:strip; 239, argument_list; 240, string:"\n"; 241, else_clause; 241, 242; 242, block; 242, 243; 242, 252; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:data; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:line; 249, identifier:split; 250, argument_list; 250, 251; 251, string:"\t"; 252, expression_statement; 252, 253; 253, yield; 253, 254; 254, call; 254, 255; 254, 256; 255, identifier:KeyValue; 256, argument_list; 256, 257; 256, 260; 257, subscript; 257, 258; 257, 259; 258, identifier:data; 259, integer:0; 260, call; 260, 261; 260, 262; 261, identifier:tuple; 262, argument_list; 262, 263; 263, identifier:data; 264, else_clause; 264, 265; 265, block; 265, 266; 266, if_statement; 266, 267; 266, 268; 267, identifier:line; 268, block; 268, 269; 269, if_statement; 269, 270; 269, 283; 269, 359; 270, boolean_operator:or; 270, 271; 270, 277; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:line; 274, identifier:startswith; 275, argument_list; 275, 276; 276, string:"MS:MS_RESULTS_FILE"; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:line; 280, identifier:startswith; 281, argument_list; 281, 282; 282, string:"NM:NMR_RESULTS_FILE"; 283, block; 283, 284; 284, try_statement; 284, 285; 284, 330; 285, block; 285, 286; 285, 298; 285, 313; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 292; 288, pattern_list; 288, 289; 288, 290; 288, 291; 289, identifier:key; 290, identifier:value; 291, identifier:extra; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:line; 295, identifier:split; 296, argument_list; 296, 297; 297, string:"\t"; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 303; 300, pattern_list; 300, 301; 300, 302; 301, identifier:extra_key; 302, identifier:extra_value; 303, call; 303, 304; 303, 311; 304, attribute; 304, 305; 304, 310; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:extra; 308, identifier:strip; 309, argument_list; 310, identifier:split; 311, argument_list; 311, 312; 312, string:":"; 313, expression_statement; 313, 314; 314, yield; 314, 315; 315, call; 315, 316; 315, 317; 316, identifier:KeyValueExtra; 317, argument_list; 317, 318; 317, 327; 317, 328; 317, 329; 318, subscript; 318, 319; 318, 324; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:key; 322, identifier:strip; 323, argument_list; 324, slice; 324, 325; 324, 326; 325, integer:3; 326, colon; 327, identifier:value; 328, identifier:extra_key; 329, identifier:extra_value; 330, except_clause; 330, 331; 330, 332; 331, identifier:ValueError; 332, block; 332, 333; 332, 344; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 338; 335, pattern_list; 335, 336; 335, 337; 336, identifier:key; 337, identifier:value; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:line; 341, identifier:split; 342, argument_list; 342, 343; 343, string:"\t"; 344, expression_statement; 344, 345; 345, yield; 345, 346; 346, call; 346, 347; 346, 348; 347, identifier:KeyValue; 348, argument_list; 348, 349; 348, 358; 349, subscript; 349, 350; 349, 355; 350, call; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, identifier:key; 353, identifier:strip; 354, argument_list; 355, slice; 355, 356; 355, 357; 356, integer:3; 357, colon; 358, identifier:value; 359, else_clause; 359, 360; 360, block; 360, 361; 361, try_statement; 361, 362; 361, 428; 362, block; 362, 363; 362, 374; 363, expression_statement; 363, 364; 364, assignment; 364, 365; 364, 368; 365, pattern_list; 365, 366; 365, 367; 366, identifier:key; 367, identifier:value; 368, call; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:line; 371, identifier:split; 372, argument_list; 372, 373; 373, string:"\t"; 374, if_statement; 374, 375; 374, 378; 374, 415; 375, comparison_operator:in; 375, 376; 375, 377; 376, string:":"; 377, identifier:key; 378, block; 378, 379; 379, if_statement; 379, 380; 379, 386; 379, 398; 380, call; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:key; 383, identifier:startswith; 384, argument_list; 384, 385; 385, string:"MS_METABOLITE_DATA:UNITS"; 386, block; 386, 387; 387, expression_statement; 387, 388; 388, yield; 388, 389; 389, call; 389, 390; 389, 391; 390, identifier:KeyValue; 391, argument_list; 391, 392; 391, 397; 392, call; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:key; 395, identifier:strip; 396, argument_list; 397, identifier:value; 398, else_clause; 398, 399; 399, block; 399, 400; 400, expression_statement; 400, 401; 401, yield; 401, 402; 402, call; 402, 403; 402, 404; 403, identifier:KeyValue; 404, argument_list; 404, 405; 404, 414; 405, subscript; 405, 406; 405, 411; 406, call; 406, 407; 406, 410; 407, attribute; 407, 408; 407, 409; 408, identifier:key; 409, identifier:strip; 410, argument_list; 411, slice; 411, 412; 411, 413; 412, integer:3; 413, colon; 414, identifier:value; 415, else_clause; 415, 416; 416, block; 416, 417; 417, expression_statement; 417, 418; 418, yield; 418, 419; 419, call; 419, 420; 419, 421; 420, identifier:KeyValue; 421, argument_list; 421, 422; 421, 427; 422, call; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:key; 425, identifier:strip; 426, argument_list; 427, identifier:value; 428, except_clause; 428, 429; 428, 430; 429, identifier:ValueError; 430, block; 430, 431; 430, 440; 431, expression_statement; 431, 432; 432, call; 432, 433; 432, 434; 433, identifier:print; 434, argument_list; 434, 435; 434, 436; 435, string:"LINE WITH ERROR:\n\t"; 436, call; 436, 437; 436, 438; 437, identifier:repr; 438, argument_list; 438, 439; 439, identifier:line; 440, raise_statement; 441, expression_statement; 441, 442; 442, yield; 442, 443; 443, call; 443, 444; 443, 445; 444, identifier:KeyValue; 445, argument_list; 445, 446; 445, 447; 446, string:"#ENDSECTION"; 447, string:"\n"; 448, expression_statement; 448, 449; 449, yield; 449, 450; 450, call; 450, 451; 450, 452; 451, identifier:KeyValue; 452, argument_list; 452, 453; 452, 454; 453, string:"!#ENDFILE"; 454, string:"\n"
def tokenizer(text): """A lexical analyzer for the `mwtab` formatted files. :param str text: `mwtab` formatted text. :return: Tuples of data. :rtype: py:class:`~collections.namedtuple` """ stream = deque(text.split("\n")) while len(stream) > 0: line = stream.popleft() if line.startswith("#METABOLOMICS WORKBENCH"): yield KeyValue("#METABOLOMICS WORKBENCH", "\n") yield KeyValue("HEADER", line) for identifier in line.split(" "): if ":" in identifier: key, value = identifier.split(":") yield KeyValue(key, value) elif line.startswith("#ANALYSIS TYPE"): yield KeyValue("HEADER", line) elif line.startswith("#SUBJECT_SAMPLE_FACTORS:"): yield KeyValue("#ENDSECTION", "\n") yield KeyValue("#SUBJECT_SAMPLE_FACTORS", "\n") elif line.startswith("#"): yield KeyValue("#ENDSECTION", "\n") yield KeyValue(line.strip(), "\n") elif line.startswith("SUBJECT_SAMPLE_FACTORS"): key, subject_type, local_sample_id, factors, additional_sample_data = line.split("\t") # factors = [dict([[i.strip() for i in f.split(":")]]) for f in factors.split("|")] yield SubjectSampleFactors(key.strip(), subject_type, local_sample_id, factors, additional_sample_data) elif line.endswith("_START"): yield KeyValue(line, "\n") while not line.endswith("_END"): line = stream.popleft() if line.endswith("_END"): yield KeyValue(line.strip(), "\n") else: data = line.split("\t") yield KeyValue(data[0], tuple(data)) else: if line: if line.startswith("MS:MS_RESULTS_FILE") or line.startswith("NM:NMR_RESULTS_FILE"): try: key, value, extra = line.split("\t") extra_key, extra_value = extra.strip().split(":") yield KeyValueExtra(key.strip()[3:], value, extra_key, extra_value) except ValueError: key, value = line.split("\t") yield KeyValue(key.strip()[3:], value) else: try: key, value = line.split("\t") if ":" in key: if key.startswith("MS_METABOLITE_DATA:UNITS"): yield KeyValue(key.strip(), value) else: yield KeyValue(key.strip()[3:], value) else: yield KeyValue(key.strip(), value) except ValueError: print("LINE WITH ERROR:\n\t", repr(line)) raise yield KeyValue("#ENDSECTION", "\n") yield KeyValue("!#ENDFILE", "\n")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_proxy; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:url; 6, default_parameter; 6, 7; 6, 8; 7, identifier:urlparams; 8, None; 9, block; 9, 10; 9, 12; 9, 30; 9, 37; 9, 41; 9, 61; 9, 62; 9, 73; 9, 117; 9, 126; 9, 127; 9, 135; 9, 143; 9, 147; 9, 185; 9, 186; 9, 194; 9, 200; 9, 201; 9, 202; 10, expression_statement; 10, 11; 11, comment; 12, for_statement; 12, 13; 12, 16; 12, 23; 13, pattern_list; 13, 14; 13, 15; 14, identifier:k; 15, identifier:v; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:request; 20, identifier:params; 21, identifier:iteritems; 22, argument_list; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, subscript; 26, 27; 26, 28; 27, identifier:urlparams; 28, identifier:k; 29, identifier:v; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:query; 33, call; 33, 34; 33, 35; 34, identifier:urlencode; 35, argument_list; 35, 36; 36, identifier:urlparams; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:full_url; 40, identifier:url; 41, if_statement; 41, 42; 41, 43; 42, identifier:query; 43, block; 43, 44; 43, 57; 44, if_statement; 44, 45; 44, 52; 45, not_operator; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:full_url; 49, identifier:endswith; 50, argument_list; 50, 51; 51, string:"?"; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, augmented_assignment:+=; 54, 55; 54, 56; 55, identifier:full_url; 56, string:"?"; 57, expression_statement; 57, 58; 58, augmented_assignment:+=; 58, 59; 58, 60; 59, identifier:full_url; 60, identifier:query; 61, comment; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:req; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:urllib2; 68, identifier:Request; 69, argument_list; 69, 70; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:url; 72, identifier:full_url; 73, for_statement; 73, 74; 73, 75; 73, 78; 74, identifier:header; 75, attribute; 75, 76; 75, 77; 76, identifier:request; 77, identifier:headers; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 87; 79, 103; 80, comparison_operator:==; 80, 81; 80, 86; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:header; 84, identifier:lower; 85, argument_list; 86, string:"host"; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:req; 92, identifier:add_header; 93, argument_list; 93, 94; 93, 95; 94, identifier:header; 95, subscript; 95, 96; 95, 102; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:urlparse; 99, identifier:urlparse; 100, argument_list; 100, 101; 101, identifier:url; 102, integer:1; 103, else_clause; 103, 104; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:req; 109, identifier:add_header; 110, argument_list; 110, 111; 110, 112; 111, identifier:header; 112, subscript; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:request; 115, identifier:headers; 116, identifier:header; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:res; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:urllib2; 123, identifier:urlopen; 124, argument_list; 124, 125; 125, identifier:req; 126, comment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:i; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:res; 133, identifier:info; 134, argument_list; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:response; 139, identifier:status; 140, attribute; 140, 141; 140, 142; 141, identifier:res; 142, identifier:code; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:got_content_length; 146, False; 147, for_statement; 147, 148; 147, 149; 147, 150; 147, 151; 148, identifier:header; 149, identifier:i; 150, comment; 151, block; 151, 152; 151, 162; 151, 175; 152, if_statement; 152, 153; 152, 160; 153, comparison_operator:==; 153, 154; 153, 159; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:header; 157, identifier:lower; 158, argument_list; 159, string:"transfer-encoding"; 160, block; 160, 161; 161, continue_statement; 162, if_statement; 162, 163; 162, 170; 163, comparison_operator:==; 163, 164; 163, 169; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:header; 167, identifier:lower; 168, argument_list; 169, string:"content-length"; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:got_content_length; 174, True; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 182; 177, subscript; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:response; 180, identifier:headers; 181, identifier:header; 182, subscript; 182, 183; 182, 184; 183, identifier:i; 184, identifier:header; 185, comment; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:result; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:res; 192, identifier:read; 193, argument_list; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:res; 198, identifier:close; 199, argument_list; 200, comment; 201, comment; 202, return_statement; 202, 203; 203, identifier:result
def _proxy(self, url, urlparams=None): """Do the actual action of proxying the call. """ for k,v in request.params.iteritems(): urlparams[k]=v query = urlencode(urlparams) full_url = url if query: if not full_url.endswith("?"): full_url += "?" full_url += query # build the request with its headers req = urllib2.Request(url=full_url) for header in request.headers: if header.lower() == "host": req.add_header(header, urlparse.urlparse(url)[1]) else: req.add_header(header, request.headers[header]) res = urllib2.urlopen(req) # add response headers i = res.info() response.status = res.code got_content_length = False for header in i: # We don't support serving the result as chunked if header.lower() == "transfer-encoding": continue if header.lower() == "content-length": got_content_length = True response.headers[header] = i[header] # return the result result = res.read() res.close() #if not got_content_length: # response.headers['content-length'] = str(len(result)) return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:attempt_open_query_permutations; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:url; 5, identifier:orig_file_path; 6, identifier:is_header_file; 7, block; 7, 8; 7, 10; 7, 22; 7, 23; 7, 51; 7, 52; 7, 113; 7, 122; 7, 138; 7, 144; 7, 150; 7, 159; 7, 160; 7, 175; 7, 189; 7, 190; 7, 206; 7, 207; 7, 230; 7, 231; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:directory; 13, binary_operator:+; 13, 14; 13, 21; 14, call; 14, 15; 14, 16; 15, identifier:dirname; 16, argument_list; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:convert_to_platform_safe; 19, argument_list; 19, 20; 20, identifier:orig_file_path; 21, string:"/"; 22, comment; 23, try_statement; 23, 24; 23, 47; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:filenames; 28, list_comprehension; 28, 29; 28, 30; 28, 38; 29, identifier:f; 30, for_in_clause; 30, 31; 30, 32; 31, identifier:f; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:os; 35, identifier:listdir; 36, argument_list; 36, 37; 37, identifier:directory; 38, if_clause; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:isfile; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:join; 44, argument_list; 44, 45; 44, 46; 45, identifier:directory; 46, identifier:f; 47, except_clause; 47, 48; 47, 49; 48, identifier:OSError; 49, block; 49, 50; 50, return_statement; 51, comment; 52, if_statement; 52, 53; 52, 54; 52, 84; 53, identifier:is_header_file; 54, block; 54, 55; 54, 67; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:filenames; 58, list_comprehension; 58, 59; 58, 60; 58, 63; 59, identifier:f; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:f; 62, identifier:filenames; 63, if_clause; 63, 64; 64, comparison_operator:in; 64, 65; 64, 66; 65, string:".http-headers"; 66, identifier:f; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:filenames; 70, list_comprehension; 70, 71; 70, 72; 70, 75; 71, identifier:f; 72, for_in_clause; 72, 73; 72, 74; 73, identifier:f; 74, identifier:filenames; 75, if_clause; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:_compare_file_name; 78, argument_list; 78, 79; 78, 82; 78, 83; 79, binary_operator:+; 79, 80; 79, 81; 80, identifier:orig_file_path; 81, string:".http-headers"; 82, identifier:directory; 83, identifier:f; 84, else_clause; 84, 85; 85, block; 85, 86; 85, 98; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:filenames; 89, list_comprehension; 89, 90; 89, 91; 89, 94; 90, identifier:f; 91, for_in_clause; 91, 92; 91, 93; 92, identifier:f; 93, identifier:filenames; 94, if_clause; 94, 95; 95, comparison_operator:not; 95, 96; 95, 97; 96, string:".http-headers"; 97, identifier:f; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:filenames; 101, list_comprehension; 101, 102; 101, 103; 101, 106; 102, identifier:f; 103, for_in_clause; 103, 104; 103, 105; 104, identifier:f; 105, identifier:filenames; 106, if_clause; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:_compare_file_name; 109, argument_list; 109, 110; 109, 111; 109, 112; 110, identifier:orig_file_path; 111, identifier:directory; 112, identifier:f; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:url_parts; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:url; 119, identifier:split; 120, argument_list; 120, 121; 121, string:"/"; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:url_parts; 125, call; 125, 126; 125, 136; 126, attribute; 126, 127; 126, 135; 127, subscript; 127, 128; 127, 129; 128, identifier:url_parts; 129, binary_operator:-; 129, 130; 129, 134; 130, call; 130, 131; 130, 132; 131, identifier:len; 132, argument_list; 132, 133; 133, identifier:url_parts; 134, integer:1; 135, identifier:split; 136, argument_list; 136, 137; 137, string:"?"; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:base; 141, subscript; 141, 142; 141, 143; 142, identifier:url_parts; 143, integer:0; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:params; 147, subscript; 147, 148; 147, 149; 148, identifier:url_parts; 149, integer:1; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:params; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:params; 156, identifier:split; 157, argument_list; 157, 158; 158, string:"&"; 159, comment; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:filenames; 163, list_comprehension; 163, 164; 163, 165; 163, 168; 164, identifier:f; 165, for_in_clause; 165, 166; 165, 167; 166, identifier:f; 167, identifier:filenames; 168, if_clause; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:f; 172, identifier:startswith; 173, argument_list; 173, 174; 174, identifier:base; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:params; 178, list_comprehension; 178, 179; 178, 186; 179, call; 179, 180; 179, 181; 180, identifier:convert_to_platform_safe; 181, argument_list; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:unquote; 184, argument_list; 184, 185; 185, identifier:p; 186, for_in_clause; 186, 187; 186, 188; 187, identifier:p; 188, identifier:params; 189, comment; 190, for_statement; 190, 191; 190, 192; 190, 193; 191, identifier:param; 192, identifier:params; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:filenames; 197, list_comprehension; 197, 198; 197, 199; 197, 202; 198, identifier:f; 199, for_in_clause; 199, 200; 199, 201; 200, identifier:f; 201, identifier:filenames; 202, if_clause; 202, 203; 203, comparison_operator:in; 203, 204; 203, 205; 204, identifier:param; 205, identifier:f; 206, comment; 207, if_statement; 207, 208; 207, 214; 208, comparison_operator:==; 208, 209; 208, 213; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:filenames; 213, integer:1; 214, block; 214, 215; 214, 225; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:path; 218, call; 218, 219; 218, 220; 219, identifier:join; 220, argument_list; 220, 221; 220, 222; 221, identifier:directory; 222, subscript; 222, 223; 222, 224; 223, identifier:filenames; 224, integer:0; 225, return_statement; 225, 226; 226, call; 226, 227; 226, 228; 227, identifier:open_file; 228, argument_list; 228, 229; 229, identifier:path; 230, comment; 231, if_statement; 231, 232; 231, 238; 232, comparison_operator:>; 232, 233; 232, 237; 233, call; 233, 234; 233, 235; 234, identifier:len; 235, argument_list; 235, 236; 236, identifier:filenames; 237, integer:1; 238, block; 238, 239; 239, raise_statement; 239, 240; 240, call; 240, 241; 240, 242; 241, identifier:DataFailureException; 242, argument_list; 242, 243; 242, 244; 242, 247; 243, identifier:url; 244, binary_operator:+; 244, 245; 244, 246; 245, string:"Multiple mock data files matched the "; 246, string:"parameters provided!"; 247, integer:404
def attempt_open_query_permutations(url, orig_file_path, is_header_file): """ Attempt to open a given mock data file with different permutations of the query parameters """ directory = dirname(convert_to_platform_safe(orig_file_path)) + "/" # get all filenames in directory try: filenames = [f for f in os.listdir(directory) if isfile(join(directory, f))] except OSError: return # ensure that there are not extra parameters on any files if is_header_file: filenames = [f for f in filenames if ".http-headers" in f] filenames = [f for f in filenames if _compare_file_name(orig_file_path + ".http-headers", directory, f)] else: filenames = [f for f in filenames if ".http-headers" not in f] filenames = [f for f in filenames if _compare_file_name(orig_file_path, directory, f)] url_parts = url.split("/") url_parts = url_parts[len(url_parts) - 1].split("?") base = url_parts[0] params = url_parts[1] params = params.split("&") # check to ensure that the base url matches filenames = [f for f in filenames if f.startswith(base)] params = [convert_to_platform_safe(unquote(p)) for p in params] # ensure that all parameters are there for param in params: filenames = [f for f in filenames if param in f] # if we only have one file, return it if len(filenames) == 1: path = join(directory, filenames[0]) return open_file(path) # if there is more than one file, raise an exception if len(filenames) > 1: raise DataFailureException(url, "Multiple mock data files matched the " + "parameters provided!", 404)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:search_databases; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:search_term; 6, default_parameter; 6, 7; 6, 8; 7, identifier:location; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:markets_only; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:databases_to_search; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:allow_internal; 17, False; 18, block; 18, 19; 18, 21; 18, 25; 18, 69; 18, 109; 18, 117; 18, 118; 18, 124; 18, 143; 18, 164; 18, 176; 18, 183; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:dict_list; 24, list:[]; 25, if_statement; 25, 26; 25, 27; 26, identifier:allow_internal; 27, block; 27, 28; 27, 32; 27, 62; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:internal_dict; 31, dictionary; 32, for_statement; 32, 33; 32, 36; 32, 45; 33, pattern_list; 33, 34; 33, 35; 34, identifier:k; 35, identifier:v; 36, call; 36, 37; 36, 44; 37, attribute; 37, 38; 37, 43; 38, subscript; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:database; 42, string:'items'; 43, identifier:items; 44, argument_list; 45, block; 45, 46; 46, if_statement; 46, 47; 46, 55; 47, comparison_operator:==; 47, 48; 47, 54; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:v; 51, identifier:get; 52, argument_list; 52, 53; 53, string:'lcopt_type'; 54, string:'intermediate'; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:internal_dict; 60, identifier:k; 61, identifier:v; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:dict_list; 66, identifier:append; 67, argument_list; 67, 68; 68, identifier:internal_dict; 69, if_statement; 69, 70; 69, 73; 69, 74; 69, 75; 69, 88; 70, comparison_operator:is; 70, 71; 70, 72; 71, identifier:databases_to_search; 72, None; 73, comment; 74, comment; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, augmented_assignment:+=; 77, 78; 77, 79; 78, identifier:dict_list; 79, list_comprehension; 79, 80; 79, 83; 80, subscript; 80, 81; 80, 82; 81, identifier:x; 82, string:'items'; 83, for_in_clause; 83, 84; 83, 85; 84, identifier:x; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:external_databases; 88, else_clause; 88, 89; 88, 90; 89, comment; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, augmented_assignment:+=; 92, 93; 92, 94; 93, identifier:dict_list; 94, list_comprehension; 94, 95; 94, 98; 94, 103; 95, subscript; 95, 96; 95, 97; 96, identifier:x; 97, string:'items'; 98, for_in_clause; 98, 99; 98, 100; 99, identifier:x; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:external_databases; 103, if_clause; 103, 104; 104, comparison_operator:in; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:x; 107, string:'name'; 108, identifier:databases_to_search; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:data; 112, call; 112, 113; 112, 114; 113, identifier:Dictionaries; 114, argument_list; 114, 115; 115, list_splat; 115, 116; 116, identifier:dict_list; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:query; 121, call; 121, 122; 121, 123; 122, identifier:Query; 123, argument_list; 124, if_statement; 124, 125; 124, 126; 125, identifier:markets_only; 126, block; 126, 127; 126, 136; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:market_filter; 130, call; 130, 131; 130, 132; 131, identifier:Filter; 132, argument_list; 132, 133; 132, 134; 132, 135; 133, string:"name"; 134, string:"has"; 135, string:"market for"; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:query; 140, identifier:add; 141, argument_list; 141, 142; 142, identifier:market_filter; 143, if_statement; 143, 144; 143, 147; 144, comparison_operator:is; 144, 145; 144, 146; 145, identifier:location; 146, None; 147, block; 147, 148; 147, 157; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:location_filter; 151, call; 151, 152; 151, 153; 152, identifier:Filter; 153, argument_list; 153, 154; 153, 155; 153, 156; 154, string:"location"; 155, string:"is"; 156, identifier:location; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:query; 161, identifier:add; 162, argument_list; 162, 163; 163, identifier:location_filter; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:query; 168, identifier:add; 169, argument_list; 169, 170; 170, call; 170, 171; 170, 172; 171, identifier:Filter; 172, argument_list; 172, 173; 172, 174; 172, 175; 173, string:"name"; 174, string:"ihas"; 175, identifier:search_term; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:result; 179, call; 179, 180; 179, 181; 180, identifier:query; 181, argument_list; 181, 182; 182, identifier:data; 183, return_statement; 183, 184; 184, identifier:result
def search_databases(self, search_term, location=None, markets_only=False, databases_to_search=None, allow_internal=False): """ Search external databases linked to your lcopt model. To restrict the search to particular databases (e.g. technosphere or biosphere only) use a list of database names in the ``database_to_search`` variable """ dict_list = [] if allow_internal: internal_dict = {} for k, v in self.database['items'].items(): if v.get('lcopt_type') == 'intermediate': internal_dict[k] = v dict_list.append(internal_dict) if databases_to_search is None: #Search all of the databases available #data = Dictionaries(self.database['items'], *[x['items'] for x in self.external_databases]) dict_list += [x['items'] for x in self.external_databases] else: #data = Dictionaries(self.database['items'], *[x['items'] for x in self.external_databases if x['name'] in databases_to_search]) dict_list += [x['items'] for x in self.external_databases if x['name'] in databases_to_search] data = Dictionaries(*dict_list) #data = Dictionaries(self.database['items'], *[x['items'] for x in self.external_databases if x['name'] in databases_to_search]) query = Query() if markets_only: market_filter = Filter("name", "has", "market for") query.add(market_filter) if location is not None: location_filter = Filter("location", "is", location) query.add(location_filter) query.add(Filter("name", "ihas", search_term)) result = query(data) return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:create_parameter_map; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 26; 5, 30; 5, 52; 5, 178; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:names; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:modelInstance; 15, identifier:names; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:db; 19, subscript; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:modelInstance; 24, identifier:database; 25, string:'items'; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:parameter_map; 29, dictionary; 30, function_definition; 30, 31; 30, 32; 30, 34; 31, function_name:get_names_index; 32, parameters; 32, 33; 33, identifier:my_thing; 34, block; 34, 35; 35, return_statement; 35, 36; 36, subscript; 36, 37; 36, 51; 37, list_comprehension; 37, 38; 37, 39; 37, 47; 38, identifier:i; 39, for_in_clause; 39, 40; 39, 43; 40, pattern_list; 40, 41; 40, 42; 41, identifier:i; 42, identifier:x; 43, call; 43, 44; 43, 45; 44, identifier:enumerate; 45, argument_list; 45, 46; 46, identifier:names; 47, if_clause; 47, 48; 48, comparison_operator:==; 48, 49; 48, 50; 49, identifier:x; 50, identifier:my_thing; 51, integer:0; 52, for_statement; 52, 53; 52, 56; 52, 61; 53, pattern_list; 53, 54; 53, 55; 54, identifier:k; 55, identifier:this_item; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:db; 59, identifier:items; 60, argument_list; 61, block; 61, 62; 62, if_statement; 62, 63; 62, 68; 63, comparison_operator:==; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:this_item; 66, string:'type'; 67, string:'process'; 68, block; 68, 69; 68, 89; 68, 107; 68, 118; 68, 133; 68, 147; 68, 168; 68, 169; 68, 170; 68, 171; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:production_id; 72, subscript; 72, 73; 72, 88; 73, list_comprehension; 73, 74; 73, 77; 73, 82; 74, subscript; 74, 75; 74, 76; 75, identifier:x; 76, string:'input'; 77, for_in_clause; 77, 78; 77, 79; 78, identifier:x; 79, subscript; 79, 80; 79, 81; 80, identifier:this_item; 81, string:'exchanges'; 82, if_clause; 82, 83; 83, comparison_operator:==; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:x; 86, string:'type'; 87, string:'production'; 88, integer:0; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:input_ids; 92, list_comprehension; 92, 93; 92, 96; 92, 101; 93, subscript; 93, 94; 93, 95; 94, identifier:x; 95, string:'input'; 96, for_in_clause; 96, 97; 96, 98; 97, identifier:x; 98, subscript; 98, 99; 98, 100; 99, identifier:this_item; 100, string:'exchanges'; 101, if_clause; 101, 102; 102, comparison_operator:==; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:x; 105, string:'type'; 106, string:'technosphere'; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:production_index; 110, call; 110, 111; 110, 112; 111, identifier:get_names_index; 112, argument_list; 112, 113; 113, subscript; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:db; 116, identifier:production_id; 117, string:'name'; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:input_indexes; 121, list_comprehension; 121, 122; 121, 130; 122, call; 122, 123; 122, 124; 123, identifier:get_names_index; 124, argument_list; 124, 125; 125, subscript; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:db; 128, identifier:x; 129, string:'name'; 130, for_in_clause; 130, 131; 130, 132; 131, identifier:x; 132, identifier:input_ids; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:parameter_ids; 136, list_comprehension; 136, 137; 136, 144; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, string:'n_p_{}_{}'; 140, identifier:format; 141, argument_list; 141, 142; 141, 143; 142, identifier:x; 143, identifier:production_index; 144, for_in_clause; 144, 145; 144, 146; 145, identifier:x; 146, identifier:input_indexes; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:parameter_map_items; 150, dictionary_comprehension; 150, 151; 150, 160; 151, pair; 151, 152; 151, 157; 152, tuple; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:input_ids; 155, identifier:n; 156, identifier:k; 157, subscript; 157, 158; 157, 159; 158, identifier:parameter_ids; 159, identifier:n; 160, for_in_clause; 160, 161; 160, 164; 161, pattern_list; 161, 162; 161, 163; 162, identifier:n; 163, identifier:x; 164, call; 164, 165; 164, 166; 165, identifier:enumerate; 166, argument_list; 166, 167; 167, identifier:input_ids; 168, comment; 169, comment; 170, comment; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:parameter_map; 175, identifier:update; 176, argument_list; 176, 177; 177, identifier:parameter_map_items; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:self; 182, identifier:parameter_map; 183, identifier:parameter_map
def create_parameter_map(self): """ Creates a parameter map which takes a tuple of the exchange 'from' and exchange 'to' codes and returns the parameter name for that exchange """ names = self.modelInstance.names db = self.modelInstance.database['items'] parameter_map = {} def get_names_index(my_thing): return[i for i, x in enumerate(names) if x == my_thing][0] for k, this_item in db.items(): if this_item['type'] == 'process': production_id = [x['input'] for x in this_item['exchanges'] if x['type'] == 'production'][0] input_ids = [x['input'] for x in this_item['exchanges'] if x['type'] == 'technosphere'] production_index = get_names_index(db[production_id]['name']) input_indexes = [get_names_index(db[x]['name']) for x in input_ids] parameter_ids = ['n_p_{}_{}'.format(x, production_index) for x in input_indexes] parameter_map_items = {(input_ids[n], k): parameter_ids[n] for n, x in enumerate(input_ids)} #check = [self.modelInstance.params[x]['description'] for x in parameter_ids] #print(check) #print(parameter_map_items) parameter_map.update(parameter_map_items) self.parameter_map = parameter_map
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:apply; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 4, default_parameter; 4, 5; 4, 6; 5, identifier:config_override; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:tag_override; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:rollback; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:skip_missing; 15, None; 16, block; 16, 17; 16, 19; 16, 20; 16, 27; 16, 33; 16, 34; 16, 45; 16, 262; 17, expression_statement; 17, 18; 18, comment; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:config; 23, call; 23, 24; 23, 25; 24, identifier:get_config; 25, argument_list; 25, 26; 26, identifier:config_override; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:databases; 30, subscript; 30, 31; 30, 32; 31, identifier:config; 32, string:'databases'; 33, comment; 34, if_statement; 34, 35; 34, 39; 35, boolean_operator:and; 35, 36; 35, 37; 36, identifier:rollback; 37, not_operator; 37, 38; 38, identifier:tag_override; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:RuntimeError; 43, argument_list; 43, 44; 44, string:'To rollback a migration you need to specify the database tag with `--tag`'; 45, for_statement; 45, 46; 45, 47; 45, 51; 45, 52; 46, identifier:tag; 47, call; 47, 48; 47, 49; 48, identifier:sorted; 49, argument_list; 49, 50; 50, identifier:databases; 51, comment; 52, block; 52, 53; 52, 61; 52, 62; 52, 74; 52, 86; 52, 98; 52, 106; 52, 114; 52, 122; 52, 133; 52, 145; 52, 157; 52, 158; 52, 181; 52, 182; 52, 200; 52, 201; 52, 211; 52, 251; 52, 252; 53, if_statement; 53, 54; 53, 59; 54, boolean_operator:and; 54, 55; 54, 56; 55, identifier:tag_override; 56, comparison_operator:!=; 56, 57; 56, 58; 57, identifier:tag_override; 58, identifier:tag; 59, block; 59, 60; 60, continue_statement; 61, comment; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:engine; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:databases; 69, identifier:tag; 70, identifier:get; 71, argument_list; 71, 72; 71, 73; 72, string:'engine'; 73, string:'mysql'; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:host; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:databases; 81, identifier:tag; 82, identifier:get; 83, argument_list; 83, 84; 83, 85; 84, string:'host'; 85, string:'localhost'; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:port; 89, call; 89, 90; 89, 95; 90, attribute; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:databases; 93, identifier:tag; 94, identifier:get; 95, argument_list; 95, 96; 95, 97; 96, string:'port'; 97, integer:3306; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:user; 101, subscript; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:databases; 104, identifier:tag; 105, string:'user'; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:password; 109, subscript; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:databases; 112, identifier:tag; 113, string:'password'; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:db; 117, subscript; 117, 118; 117, 121; 118, subscript; 118, 119; 118, 120; 119, identifier:databases; 120, identifier:tag; 121, string:'db'; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:path; 125, call; 125, 126; 125, 127; 126, identifier:add_slash; 127, argument_list; 127, 128; 128, subscript; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:databases; 131, identifier:tag; 132, string:'path'; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:pre_migration; 136, call; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:databases; 140, identifier:tag; 141, identifier:get; 142, argument_list; 142, 143; 142, 144; 143, string:'pre_migration'; 144, None; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:post_migration; 148, call; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:databases; 152, identifier:tag; 153, identifier:get; 154, argument_list; 154, 155; 154, 156; 155, string:'post_migration'; 156, None; 157, comment; 158, if_statement; 158, 159; 158, 160; 158, 173; 159, identifier:skip_missing; 160, block; 160, 161; 161, try_statement; 161, 162; 161, 169; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:check_exists; 166, argument_list; 166, 167; 166, 168; 167, identifier:path; 168, string:'dir'; 169, except_clause; 169, 170; 169, 171; 170, identifier:RuntimeError; 171, block; 171, 172; 172, continue_statement; 173, else_clause; 173, 174; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:check_exists; 178, argument_list; 178, 179; 178, 180; 179, identifier:path; 180, string:'dir'; 181, comment; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:connection; 185, call; 185, 186; 185, 187; 186, identifier:get_connection; 187, argument_list; 187, 188; 187, 189; 187, 190; 187, 191; 187, 192; 187, 193; 187, 194; 188, identifier:engine; 189, identifier:host; 190, identifier:user; 191, identifier:port; 192, identifier:password; 193, identifier:db; 194, call; 194, 195; 194, 196; 195, identifier:get_ssl; 196, argument_list; 196, 197; 197, subscript; 197, 198; 197, 199; 198, identifier:databases; 199, identifier:tag; 200, comment; 201, if_statement; 201, 202; 201, 203; 202, identifier:pre_migration; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 207; 206, identifier:run_migration; 207, argument_list; 207, 208; 207, 209; 207, 210; 208, identifier:connection; 209, identifier:pre_migration; 210, identifier:engine; 211, if_statement; 211, 212; 211, 213; 211, 232; 212, identifier:rollback; 213, block; 213, 214; 213, 224; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:print; 217, argument_list; 217, 218; 218, binary_operator:%; 218, 219; 218, 220; 219, string:' * Rolling back %s (`%s` on %s)'; 220, tuple; 220, 221; 220, 222; 220, 223; 221, identifier:tag; 222, identifier:db; 223, identifier:engine; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:rollback_migration; 227, argument_list; 227, 228; 227, 229; 227, 230; 227, 231; 228, identifier:engine; 229, identifier:connection; 230, identifier:path; 231, identifier:rollback; 232, else_clause; 232, 233; 233, block; 233, 234; 233, 244; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:print; 237, argument_list; 237, 238; 238, binary_operator:%; 238, 239; 238, 240; 239, string:' * Applying migrations for %s (`%s` on %s)'; 240, tuple; 240, 241; 240, 242; 240, 243; 241, identifier:tag; 242, identifier:db; 243, identifier:engine; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 247; 246, identifier:apply_migrations; 247, argument_list; 247, 248; 247, 249; 247, 250; 248, identifier:engine; 249, identifier:connection; 250, identifier:path; 251, comment; 252, if_statement; 252, 253; 252, 254; 253, identifier:post_migration; 254, block; 254, 255; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 258; 257, identifier:run_migration; 258, argument_list; 258, 259; 258, 260; 258, 261; 259, identifier:connection; 260, identifier:post_migration; 261, identifier:engine; 262, return_statement; 262, 263; 263, True
def apply(config_override=None, tag_override=None, rollback=None, skip_missing=None): """ Look thru migrations and apply them """ # Load config config = get_config(config_override) databases = config['databases'] # If we are rolling back, ensure that we have a database tag if rollback and not tag_override: raise RuntimeError( 'To rollback a migration you need to specify the database tag with `--tag`') for tag in sorted(databases): # If a tag is specified, skip other tags if tag_override and tag_override != tag: continue # Set vars engine = databases[tag].get('engine', 'mysql') host = databases[tag].get('host', 'localhost') port = databases[tag].get('port', 3306) user = databases[tag]['user'] password = databases[tag]['password'] db = databases[tag]['db'] path = add_slash(databases[tag]['path']) pre_migration = databases[tag].get('pre_migration', None) post_migration = databases[tag].get('post_migration', None) # Check if the migration path exists if skip_missing: try: check_exists(path, 'dir') except RuntimeError: continue else: check_exists(path, 'dir') # Get database connection connection = get_connection( engine, host, user, port, password, db, get_ssl(databases[tag])) # Run pre migration queries if pre_migration: run_migration(connection, pre_migration, engine) if rollback: print(' * Rolling back %s (`%s` on %s)' % (tag, db, engine)) rollback_migration(engine, connection, path, rollback) else: print(' * Applying migrations for %s (`%s` on %s)' % (tag, db, engine)) apply_migrations(engine, connection, path) # Run post migration queries if post_migration: run_migration(connection, post_migration, engine) return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_configure_common; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:self; 5, identifier:prefix; 6, identifier:fallback_level; 7, identifier:fallback_format; 8, identifier:handler_name; 9, identifier:handler; 10, default_parameter; 10, 11; 10, 12; 11, identifier:custom_args; 12, string:''; 13, block; 13, 14; 13, 16; 13, 17; 13, 33; 13, 49; 13, 60; 13, 71; 13, 72; 13, 73; 13, 82; 13, 89; 13, 96; 13, 105; 13, 131; 13, 132; 13, 148; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:log_level; 20, call; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:config; 25, identifier:get_option; 26, argument_list; 26, 27; 26, 28; 26, 31; 26, 32; 27, string:'LOGGING'; 28, binary_operator:+; 28, 29; 28, 30; 29, identifier:prefix; 30, string:'log_level'; 31, None; 32, identifier:fallback_level; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:log_format_name; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:config; 41, identifier:get_option; 42, argument_list; 42, 43; 42, 44; 42, 47; 42, 48; 43, string:'LOGGING'; 44, binary_operator:+; 44, 45; 44, 46; 45, identifier:prefix; 46, string:'log_format'; 47, None; 48, None; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:log_format; 52, conditional_expression:if; 52, 53; 52, 58; 52, 59; 53, attribute; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:ReportingFormats; 56, identifier:log_format_name; 57, identifier:value; 58, identifier:log_format_name; 59, identifier:fallback_format; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:log_format; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:log_format; 66, identifier:format; 67, argument_list; 67, 68; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:custom_args; 70, identifier:custom_args; 71, comment; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:formatter; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:logging; 79, identifier:Formatter; 80, argument_list; 80, 81; 81, identifier:log_format; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:handler; 86, identifier:setFormatter; 87, argument_list; 87, 88; 88, identifier:formatter; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:handler; 93, identifier:setLevel; 94, argument_list; 94, 95; 95, identifier:log_level; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:logger; 102, identifier:addHandler; 103, argument_list; 103, 104; 104, identifier:handler; 105, if_statement; 105, 106; 105, 120; 105, 121; 106, not_operator; 106, 107; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:logger; 112, identifier:isEnabledFor; 113, argument_list; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:logging; 117, identifier:getLevelName; 118, argument_list; 118, 119; 119, identifier:log_level; 120, comment; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:logger; 128, identifier:setLevel; 129, argument_list; 129, 130; 130, identifier:log_level; 131, comment; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:log_info; 138, identifier:append; 139, argument_list; 139, 140; 140, binary_operator:+; 140, 141; 140, 144; 141, binary_operator:+; 141, 142; 141, 143; 142, identifier:handler_name; 143, string:' @ '; 144, call; 144, 145; 144, 146; 145, identifier:str; 146, argument_list; 146, 147; 147, identifier:log_level; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:log_handlers; 154, identifier:append; 155, argument_list; 155, 156; 156, identifier:handler
def _configure_common( self, prefix, fallback_level, fallback_format, handler_name, handler, custom_args='' ): """commom configuration code Args: prefix (str): A prefix for the `log_level` and `log_format` keys to use with the config. #FIXME: Hacky, add separate sections for each logger config? fallback_level (str): Fallback/minimum log level, for if config does not have one. fallback_format (str): Fallback format for if it's not in the config. handler_name (str): Handler used in debug messages. handler (str): The handler to configure and use. custom_args (str): special ID to include in messages """ ## Retrieve settings from config ## log_level = self.config.get_option( 'LOGGING', prefix + 'log_level', None, fallback_level ) log_format_name = self.config.get_option( 'LOGGING', prefix + 'log_format', None, None ) log_format = ReportingFormats[log_format_name].value if log_format_name else fallback_format log_format = log_format.format(custom_args=custom_args) # should work even if no {custom_args} ## Attach handlers/formatter ## formatter = logging.Formatter(log_format) handler.setFormatter(formatter) handler.setLevel(log_level) self.logger.addHandler(handler) if not self.logger.isEnabledFor(logging.getLevelName(log_level)): # make sure logger level is not lower than handler level self.logger.setLevel(log_level) ## Save info about handler created ## self.log_info.append(handler_name + ' @ ' + str(log_level)) self.log_handlers.append(handler)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:get_file; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:self; 5, identifier:name; 6, identifier:save_to; 7, default_parameter; 7, 8; 7, 9; 8, identifier:add_to_cache; 9, True; 10, default_parameter; 10, 11; 10, 12; 11, identifier:force_refresh; 12, False; 13, default_parameter; 13, 14; 13, 15; 14, identifier:_lock_exclusive; 15, False; 16, block; 16, 17; 16, 19; 16, 28; 16, 32; 16, 71; 16, 79; 16, 87; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 24; 21, pattern_list; 21, 22; 21, 23; 22, identifier:uname; 23, identifier:version; 24, call; 24, 25; 24, 26; 25, identifier:split_name; 26, argument_list; 26, 27; 27, identifier:name; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:lock; 31, None; 32, if_statement; 32, 33; 32, 36; 32, 65; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:local_store; 36, block; 36, 37; 36, 48; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:lock; 40, call; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:lock_manager; 45, identifier:lock_for; 46, argument_list; 46, 47; 47, identifier:uname; 48, if_statement; 48, 49; 48, 50; 48, 57; 49, identifier:_lock_exclusive; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:lock; 55, identifier:lock_exclusive; 56, argument_list; 57, else_clause; 57, 58; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:lock; 63, identifier:lock_shared; 64, argument_list; 65, else_clause; 65, 66; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:add_to_cache; 70, False; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:t; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:time; 77, identifier:time; 78, argument_list; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:logger; 83, identifier:debug; 84, argument_list; 84, 85; 84, 86; 85, string:' downloading %s'; 86, identifier:name; 87, try_statement; 87, 88; 87, 215; 88, block; 88, 89; 88, 151; 88, 208; 89, if_statement; 89, 90; 89, 102; 90, boolean_operator:or; 90, 91; 90, 95; 91, not_operator; 91, 92; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:remote_store; 95, parenthesized_expression; 95, 96; 96, boolean_operator:and; 96, 97; 96, 100; 97, comparison_operator:is; 97, 98; 97, 99; 98, identifier:version; 99, None; 100, not_operator; 100, 101; 101, identifier:force_refresh; 102, block; 102, 103; 103, try_statement; 103, 104; 103, 129; 104, block; 104, 105; 105, if_statement; 105, 106; 105, 118; 106, boolean_operator:and; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:local_store; 110, call; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:local_store; 115, identifier:exists; 116, argument_list; 116, 117; 117, identifier:name; 118, block; 118, 119; 119, return_statement; 119, 120; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:local_store; 125, identifier:get_file; 126, argument_list; 126, 127; 126, 128; 127, identifier:name; 128, identifier:save_to; 129, except_clause; 129, 130; 129, 131; 130, identifier:Exception; 131, block; 131, 132; 132, if_statement; 132, 133; 132, 136; 132, 148; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:remote_store; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:logger; 141, identifier:warning; 142, argument_list; 142, 143; 142, 144; 142, 145; 143, string:"Error getting '%s' from local store"; 144, identifier:name; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:exc_info; 147, True; 148, else_clause; 148, 149; 149, block; 149, 150; 150, raise_statement; 151, if_statement; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:remote_store; 155, block; 155, 156; 155, 183; 155, 195; 155, 206; 156, if_statement; 156, 157; 156, 161; 157, boolean_operator:and; 157, 158; 157, 160; 158, not_operator; 158, 159; 159, identifier:_lock_exclusive; 160, identifier:add_to_cache; 161, block; 161, 162; 161, 171; 162, if_statement; 162, 163; 162, 164; 163, identifier:lock; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:lock; 169, identifier:unlock; 170, argument_list; 171, return_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:get_file; 176, argument_list; 176, 177; 176, 178; 176, 179; 176, 180; 177, identifier:name; 178, identifier:save_to; 179, identifier:add_to_cache; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:_lock_exclusive; 182, True; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:vname; 186, call; 186, 187; 186, 192; 187, attribute; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:self; 190, identifier:remote_store; 191, identifier:get_file; 192, argument_list; 192, 193; 192, 194; 193, identifier:name; 194, identifier:save_to; 195, if_statement; 195, 196; 195, 197; 196, identifier:add_to_cache; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:_add_to_cache; 203, argument_list; 203, 204; 203, 205; 204, identifier:vname; 205, identifier:save_to; 206, return_statement; 206, 207; 207, identifier:vname; 208, raise_statement; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:FiletrackerError; 211, argument_list; 211, 212; 212, binary_operator:%; 212, 213; 212, 214; 213, string:"File not available: %s"; 214, identifier:name; 215, finally_clause; 215, 216; 216, block; 216, 217; 216, 226; 217, if_statement; 217, 218; 217, 219; 218, identifier:lock; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:lock; 224, identifier:close; 225, argument_list; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:logger; 230, identifier:debug; 231, argument_list; 231, 232; 231, 233; 231, 234; 232, string:' processed %s in %.2fs'; 233, identifier:name; 234, binary_operator:-; 234, 235; 234, 240; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:time; 238, identifier:time; 239, argument_list; 240, identifier:t
def get_file(self, name, save_to, add_to_cache=True, force_refresh=False, _lock_exclusive=False): """Retrieves file identified by ``name``. The file is saved as ``save_to``. If ``add_to_cache`` is ``True``, the file is added to the local store. If ``force_refresh`` is ``True``, local cache is not examined if a remote store is configured. If a remote store is configured, but ``name`` does not contain a version, the local data store is not used, as we cannot guarantee that the version there is fresh. Local data store implemented in :class:`LocalDataStore` tries to not copy the entire file to ``save_to`` if possible, but instead uses hardlinking. Therefore you should not modify the file if you don't want to totally blow something. This method returns the full versioned name of the retrieved file. """ uname, version = split_name(name) lock = None if self.local_store: lock = self.lock_manager.lock_for(uname) if _lock_exclusive: lock.lock_exclusive() else: lock.lock_shared() else: add_to_cache = False t = time.time() logger.debug(' downloading %s', name) try: if not self.remote_store or (version is not None and not force_refresh): try: if self.local_store and self.local_store.exists(name): return self.local_store.get_file(name, save_to) except Exception: if self.remote_store: logger.warning("Error getting '%s' from local store", name, exc_info=True) else: raise if self.remote_store: if not _lock_exclusive and add_to_cache: if lock: lock.unlock() return self.get_file(name, save_to, add_to_cache, _lock_exclusive=True) vname = self.remote_store.get_file(name, save_to) if add_to_cache: self._add_to_cache(vname, save_to) return vname raise FiletrackerError("File not available: %s" % name) finally: if lock: lock.close() logger.debug(' processed %s in %.2fs', name, time.time() - t)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:get_stream; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:name; 6, default_parameter; 6, 7; 6, 8; 7, identifier:force_refresh; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:serve_from_cache; 11, False; 12, block; 12, 13; 12, 15; 12, 24; 12, 28; 12, 50; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 20; 17, pattern_list; 17, 18; 17, 19; 18, identifier:uname; 19, identifier:version; 20, call; 20, 21; 20, 22; 21, identifier:split_name; 22, argument_list; 22, 23; 23, identifier:name; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:lock; 27, None; 28, if_statement; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:local_store; 32, block; 32, 33; 32, 44; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:lock; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:lock_manager; 41, identifier:lock_for; 42, argument_list; 42, 43; 43, identifier:uname; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:lock; 48, identifier:lock_shared; 49, argument_list; 50, try_statement; 50, 51; 50, 215; 51, block; 51, 52; 51, 113; 51, 208; 52, if_statement; 52, 53; 52, 65; 53, boolean_operator:or; 53, 54; 53, 58; 54, not_operator; 54, 55; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:remote_store; 58, parenthesized_expression; 58, 59; 59, boolean_operator:and; 59, 60; 59, 63; 60, comparison_operator:is; 60, 61; 60, 62; 61, identifier:version; 62, None; 63, not_operator; 63, 64; 64, identifier:force_refresh; 65, block; 65, 66; 66, try_statement; 66, 67; 66, 91; 67, block; 67, 68; 68, if_statement; 68, 69; 68, 81; 69, boolean_operator:and; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:local_store; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:local_store; 78, identifier:exists; 79, argument_list; 79, 80; 80, identifier:name; 81, block; 81, 82; 82, return_statement; 82, 83; 83, call; 83, 84; 83, 89; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:local_store; 88, identifier:get_stream; 89, argument_list; 89, 90; 90, identifier:name; 91, except_clause; 91, 92; 91, 93; 92, identifier:Exception; 93, block; 93, 94; 94, if_statement; 94, 95; 94, 98; 94, 110; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:remote_store; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:logger; 103, identifier:warning; 104, argument_list; 104, 105; 104, 106; 104, 107; 105, string:"Error getting '%s' from local store"; 106, identifier:name; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:exc_info; 109, True; 110, else_clause; 110, 111; 111, block; 111, 112; 112, raise_statement; 113, if_statement; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:remote_store; 117, block; 117, 118; 117, 199; 118, if_statement; 118, 119; 118, 124; 119, boolean_operator:and; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:local_store; 123, identifier:serve_from_cache; 124, block; 124, 125; 124, 152; 124, 190; 125, if_statement; 125, 126; 125, 129; 126, comparison_operator:is; 126, 127; 126, 128; 127, identifier:version; 128, None; 129, block; 129, 130; 129, 141; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:version; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:remote_store; 138, identifier:file_version; 139, argument_list; 139, 140; 140, identifier:name; 141, if_statement; 141, 142; 141, 143; 142, identifier:version; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:name; 147, call; 147, 148; 147, 149; 148, identifier:versioned_name; 149, argument_list; 149, 150; 149, 151; 150, identifier:uname; 151, identifier:version; 152, if_statement; 152, 153; 152, 164; 153, boolean_operator:or; 153, 154; 153, 155; 154, identifier:force_refresh; 155, not_operator; 155, 156; 156, call; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:local_store; 161, identifier:exists; 162, argument_list; 162, 163; 163, identifier:name; 164, block; 164, 165; 164, 178; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 170; 167, tuple_pattern; 167, 168; 167, 169; 168, identifier:stream; 169, identifier:vname; 170, call; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:remote_store; 175, identifier:get_stream; 176, argument_list; 176, 177; 177, identifier:name; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:name; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:local_store; 186, identifier:add_stream; 187, argument_list; 187, 188; 187, 189; 188, identifier:vname; 189, identifier:stream; 190, return_statement; 190, 191; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:local_store; 196, identifier:get_stream; 197, argument_list; 197, 198; 198, identifier:name; 199, return_statement; 199, 200; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:remote_store; 205, identifier:get_stream; 206, argument_list; 206, 207; 207, identifier:name; 208, raise_statement; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:FiletrackerError; 211, argument_list; 211, 212; 212, binary_operator:%; 212, 213; 212, 214; 213, string:"File not available: %s"; 214, identifier:name; 215, finally_clause; 215, 216; 216, block; 216, 217; 217, if_statement; 217, 218; 217, 219; 218, identifier:lock; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:lock; 224, identifier:close; 225, argument_list
def get_stream(self, name, force_refresh=False, serve_from_cache=False): """Retrieves file identified by ``name`` in streaming mode. Works like :meth:`get_file`, except that returns a tuple (file-like object, versioned name). When both remote_store and local_store are present, serve_from_cache can be used to ensure that the file will be downloaded and served from a local cache. If a full version is specified and the file exists in the cache a file will be always served locally. """ uname, version = split_name(name) lock = None if self.local_store: lock = self.lock_manager.lock_for(uname) lock.lock_shared() try: if not self.remote_store or (version is not None and not force_refresh): try: if self.local_store and self.local_store.exists(name): return self.local_store.get_stream(name) except Exception: if self.remote_store: logger.warning("Error getting '%s' from local store", name, exc_info=True) else: raise if self.remote_store: if self.local_store and serve_from_cache: if version is None: version = self.remote_store.file_version(name) if version: name = versioned_name(uname, version) if force_refresh or not self.local_store.exists(name): (stream, vname) = self.remote_store.get_stream(name) name = self.local_store.add_stream(vname, stream) return self.local_store.get_stream(name) return self.remote_store.get_stream(name) raise FiletrackerError("File not available: %s" % name) finally: if lock: lock.close()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:put_file; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:self; 5, identifier:name; 6, identifier:filename; 7, default_parameter; 7, 8; 7, 9; 8, identifier:to_local_store; 9, True; 10, default_parameter; 10, 11; 10, 12; 11, identifier:to_remote_store; 12, True; 13, default_parameter; 13, 14; 13, 15; 14, identifier:compress_hint; 15, True; 16, block; 16, 17; 16, 19; 16, 33; 16, 38; 16, 42; 16, 64; 16, 130; 17, expression_statement; 17, 18; 18, comment; 19, if_statement; 19, 20; 19, 25; 20, boolean_operator:and; 20, 21; 20, 23; 21, not_operator; 21, 22; 22, identifier:to_local_store; 23, not_operator; 23, 24; 24, identifier:to_remote_store; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:ValueError; 29, argument_list; 29, 30; 30, concatenated_string; 30, 31; 30, 32; 31, string:"Neither to_local_store nor to_remote_store set "; 32, string:"in a call to filetracker.Client.put_file"; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:check_name; 36, argument_list; 36, 37; 37, identifier:name; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:lock; 41, None; 42, if_statement; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:local_store; 46, block; 46, 47; 46, 58; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:lock; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:lock_manager; 55, identifier:lock_for; 56, argument_list; 56, 57; 57, identifier:name; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:lock; 62, identifier:lock_exclusive; 63, argument_list; 64, try_statement; 64, 65; 64, 119; 65, block; 65, 66; 65, 91; 66, if_statement; 66, 67; 66, 78; 67, boolean_operator:and; 67, 68; 67, 75; 68, parenthesized_expression; 68, 69; 69, boolean_operator:or; 69, 70; 69, 71; 70, identifier:to_local_store; 71, not_operator; 71, 72; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:remote_store; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:local_store; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:versioned_name; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:local_store; 87, identifier:add_file; 88, argument_list; 88, 89; 88, 90; 89, identifier:name; 90, identifier:filename; 91, if_statement; 91, 92; 91, 103; 92, boolean_operator:and; 92, 93; 92, 100; 93, parenthesized_expression; 93, 94; 94, boolean_operator:or; 94, 95; 94, 96; 95, identifier:to_remote_store; 96, not_operator; 96, 97; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:local_store; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:remote_store; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:versioned_name; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:remote_store; 112, identifier:add_file; 113, argument_list; 113, 114; 113, 115; 113, 116; 114, identifier:name; 115, identifier:filename; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:compress_hint; 118, identifier:compress_hint; 119, finally_clause; 119, 120; 120, block; 120, 121; 121, if_statement; 121, 122; 121, 123; 122, identifier:lock; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:lock; 128, identifier:close; 129, argument_list; 130, return_statement; 130, 131; 131, identifier:versioned_name
def put_file(self, name, filename, to_local_store=True, to_remote_store=True, compress_hint=True): """Adds file ``filename`` to the filetracker under the name ``name``. If the file already exists, a new version is created. In practice if the store does not support versioning, the file is overwritten. The file may be added to local store only (if ``to_remote_store`` is ``False``), to remote store only (if ``to_local_store`` is ``False``) or both. If only one store is configured, the values of ``to_local_store`` and ``to_remote_store`` are ignored. Local data store implemented in :class:`LocalDataStore` tries to not directly copy the data to the final cache destination, but uses hardlinking. Therefore you should not modify the file in-place later as this would be disastrous. If ``compress_hint`` is set to False, file is compressed on the server, instead of the client. This is generally not recommended, unless you know what you're doing. """ if not to_local_store and not to_remote_store: raise ValueError("Neither to_local_store nor to_remote_store set " "in a call to filetracker.Client.put_file") check_name(name) lock = None if self.local_store: lock = self.lock_manager.lock_for(name) lock.lock_exclusive() try: if (to_local_store or not self.remote_store) and self.local_store: versioned_name = self.local_store.add_file(name, filename) if (to_remote_store or not self.local_store) and self.remote_store: versioned_name = self.remote_store.add_file( name, filename, compress_hint=compress_hint) finally: if lock: lock.close() return versioned_name
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:check; 3, parameters; 3, 4; 3, 5; 4, identifier:operations; 5, default_parameter; 5, 6; 5, 7; 6, identifier:loud; 7, False; 8, block; 8, 9; 8, 11; 8, 19; 8, 23; 8, 27; 8, 56; 8, 167; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 14; 12, not_operator; 12, 13; 13, identifier:CHECKERS; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:load_checkers; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:roll_call; 22, list:[]; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:everything_ok; 26, True; 27, if_statement; 27, 28; 27, 31; 28, boolean_operator:and; 28, 29; 28, 30; 29, identifier:loud; 30, identifier:operations; 31, block; 31, 32; 31, 36; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:title; 35, string:"Preflyt Checklist"; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:sys; 41, identifier:stderr; 42, identifier:write; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, string:"{}\n{}\n"; 47, identifier:format; 48, argument_list; 48, 49; 48, 50; 49, identifier:title; 50, binary_operator:*; 50, 51; 50, 52; 51, string:"="; 52, call; 52, 53; 52, 54; 53, identifier:len; 54, argument_list; 54, 55; 55, identifier:title; 56, for_statement; 56, 57; 56, 58; 56, 59; 57, identifier:operation; 58, identifier:operations; 59, block; 59, 60; 59, 75; 59, 83; 59, 103; 59, 111; 59, 121; 59, 129; 59, 145; 60, if_statement; 60, 61; 60, 69; 61, comparison_operator:not; 61, 62; 61, 68; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:operation; 65, identifier:get; 66, argument_list; 66, 67; 67, string:'checker'; 68, identifier:CHECKERS; 69, block; 69, 70; 70, raise_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:CheckerNotFoundError; 73, argument_list; 73, 74; 74, identifier:operation; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:checker_cls; 78, subscript; 78, 79; 78, 80; 79, identifier:CHECKERS; 80, subscript; 80, 81; 80, 82; 81, identifier:operation; 82, string:'checker'; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:args; 86, dictionary_comprehension; 86, 87; 86, 90; 86, 99; 87, pair; 87, 88; 87, 89; 88, identifier:k; 89, identifier:v; 90, for_in_clause; 90, 91; 90, 94; 91, pattern_list; 91, 92; 91, 93; 92, identifier:k; 93, identifier:v; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:operation; 97, identifier:items; 98, argument_list; 99, if_clause; 99, 100; 100, comparison_operator:!=; 100, 101; 100, 102; 101, identifier:k; 102, string:'checker'; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:checker; 106, call; 106, 107; 106, 108; 107, identifier:checker_cls; 108, argument_list; 108, 109; 109, dictionary_splat; 109, 110; 110, identifier:args; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, pattern_list; 113, 114; 113, 115; 114, identifier:success; 115, identifier:message; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:checker; 119, identifier:check; 120, argument_list; 121, if_statement; 121, 122; 121, 124; 122, not_operator; 122, 123; 123, identifier:success; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:everything_ok; 128, False; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:roll_call; 133, identifier:append; 134, argument_list; 134, 135; 135, dictionary; 135, 136; 135, 139; 135, 142; 136, pair; 136, 137; 136, 138; 137, string:"check"; 138, identifier:operation; 139, pair; 139, 140; 139, 141; 140, string:"success"; 141, identifier:success; 142, pair; 142, 143; 142, 144; 143, string:"message"; 144, identifier:message; 145, if_statement; 145, 146; 145, 147; 146, identifier:loud; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:sys; 153, identifier:stderr; 154, identifier:write; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, string:" {}\n"; 159, identifier:format; 160, argument_list; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:pformat_check; 163, argument_list; 163, 164; 163, 165; 163, 166; 164, identifier:success; 165, identifier:operation; 166, identifier:message; 167, return_statement; 167, 168; 168, expression_list; 168, 169; 168, 170; 169, identifier:everything_ok; 170, identifier:roll_call
def check(operations, loud=False): """Check all the things :param operations: The operations to check :param loud: `True` if checkers should prettyprint their status to stderr. `False` otherwise. :returns: A tuple of overall success, and a detailed execution log for all the operations """ if not CHECKERS: load_checkers() roll_call = [] everything_ok = True if loud and operations: title = "Preflyt Checklist" sys.stderr.write("{}\n{}\n".format(title, "=" * len(title))) for operation in operations: if operation.get('checker') not in CHECKERS: raise CheckerNotFoundError(operation) checker_cls = CHECKERS[operation['checker']] args = {k: v for k, v in operation.items() if k != 'checker'} checker = checker_cls(**args) success, message = checker.check() if not success: everything_ok = False roll_call.append({"check": operation, "success": success, "message": message}) if loud: sys.stderr.write(" {}\n".format(pformat_check(success, operation, message))) return everything_ok, roll_call
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:deci2sexa; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:deci; 5, default_parameter; 5, 6; 5, 7; 6, identifier:pre; 7, integer:3; 8, default_parameter; 8, 9; 8, 10; 9, identifier:trunc; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:lower; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:upper; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:b; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:upper_trim; 22, False; 23, block; 23, 24; 23, 26; 23, 51; 23, 55; 23, 72; 23, 82; 23, 94; 23, 100; 23, 101; 23, 107; 23, 134; 23, 141; 23, 142; 23, 143; 23, 158; 23, 171; 23, 178; 23, 185; 23, 209; 23, 226; 23, 233; 23, 234; 24, expression_statement; 24, 25; 25, comment; 26, if_statement; 26, 27; 26, 34; 27, boolean_operator:and; 27, 28; 27, 31; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:lower; 30, None; 31, comparison_operator:is; 31, 32; 31, 33; 32, identifier:upper; 33, None; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:deci; 38, call; 38, 39; 38, 40; 39, identifier:normalize; 40, argument_list; 40, 41; 40, 42; 40, 45; 40, 48; 41, identifier:deci; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:lower; 44, identifier:lower; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:upper; 47, identifier:upper; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:b; 50, identifier:b; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:sign; 54, integer:1; 55, if_statement; 55, 56; 55, 59; 56, comparison_operator:<; 56, 57; 56, 58; 57, identifier:deci; 58, integer:0; 59, block; 59, 60; 59, 67; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:deci; 63, call; 63, 64; 63, 65; 64, identifier:abs; 65, argument_list; 65, 66; 66, identifier:deci; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:sign; 70, unary_operator:-; 70, 71; 71, integer:1; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, pattern_list; 74, 75; 74, 76; 75, identifier:hd; 76, identifier:f1; 77, call; 77, 78; 77, 79; 78, identifier:divmod; 79, argument_list; 79, 80; 79, 81; 80, identifier:deci; 81, integer:1; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, pattern_list; 84, 85; 84, 86; 85, identifier:mm; 86, identifier:f2; 87, call; 87, 88; 87, 89; 88, identifier:divmod; 89, argument_list; 89, 90; 89, 93; 90, binary_operator:*; 90, 91; 90, 92; 91, identifier:f1; 92, float:60.0; 93, integer:1; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:sf; 97, binary_operator:*; 97, 98; 97, 99; 98, identifier:f2; 99, float:60.0; 100, comment; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:fp; 104, binary_operator:**; 104, 105; 104, 106; 105, integer:10; 106, identifier:pre; 107, if_statement; 107, 108; 107, 109; 107, 122; 108, identifier:trunc; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 115; 112, pattern_list; 112, 113; 112, 114; 113, identifier:ss; 114, identifier:_; 115, call; 115, 116; 115, 117; 116, identifier:divmod; 117, argument_list; 117, 118; 117, 121; 118, binary_operator:*; 118, 119; 118, 120; 119, identifier:sf; 120, identifier:fp; 121, integer:1; 122, else_clause; 122, 123; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:ss; 127, call; 127, 128; 127, 129; 128, identifier:round; 129, argument_list; 129, 130; 129, 133; 130, binary_operator:*; 130, 131; 130, 132; 131, identifier:sf; 132, identifier:fp; 133, integer:0; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:ss; 137, call; 137, 138; 137, 139; 138, identifier:int; 139, argument_list; 139, 140; 140, identifier:ss; 141, comment; 142, comment; 143, if_statement; 143, 144; 143, 149; 144, comparison_operator:==; 144, 145; 144, 146; 145, identifier:ss; 146, binary_operator:*; 146, 147; 146, 148; 147, integer:60; 148, identifier:fp; 149, block; 149, 150; 149, 154; 150, expression_statement; 150, 151; 151, augmented_assignment:+=; 151, 152; 151, 153; 152, identifier:mm; 153, integer:1; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:ss; 157, integer:0; 158, if_statement; 158, 159; 158, 162; 159, comparison_operator:==; 159, 160; 159, 161; 160, identifier:mm; 161, integer:60; 162, block; 162, 163; 162, 167; 163, expression_statement; 163, 164; 164, augmented_assignment:+=; 164, 165; 164, 166; 165, identifier:hd; 166, integer:1; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:mm; 170, integer:0; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:hd; 174, call; 174, 175; 174, 176; 175, identifier:int; 176, argument_list; 176, 177; 177, identifier:hd; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:mm; 181, call; 181, 182; 181, 183; 182, identifier:int; 183, argument_list; 183, 184; 184, identifier:mm; 185, if_statement; 185, 186; 185, 195; 185, 196; 186, boolean_operator:and; 186, 187; 186, 194; 187, boolean_operator:and; 187, 188; 187, 191; 188, comparison_operator:is; 188, 189; 188, 190; 189, identifier:lower; 190, None; 191, comparison_operator:is; 191, 192; 191, 193; 192, identifier:upper; 193, None; 194, identifier:upper_trim; 195, comment; 196, block; 196, 197; 197, if_statement; 197, 198; 197, 201; 198, comparison_operator:==; 198, 199; 198, 200; 199, identifier:hd; 200, identifier:upper; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:hd; 205, call; 205, 206; 205, 207; 206, identifier:int; 207, argument_list; 207, 208; 208, identifier:lower; 209, if_statement; 209, 210; 209, 221; 210, boolean_operator:and; 210, 211; 210, 218; 211, boolean_operator:and; 211, 212; 211, 215; 212, comparison_operator:==; 212, 213; 212, 214; 213, identifier:hd; 214, integer:0; 215, comparison_operator:==; 215, 216; 215, 217; 216, identifier:mm; 217, integer:0; 218, comparison_operator:==; 218, 219; 218, 220; 219, identifier:ss; 220, integer:0; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:sign; 225, integer:1; 226, expression_statement; 226, 227; 227, augmented_assignment:/=; 227, 228; 227, 229; 228, identifier:ss; 229, call; 229, 230; 229, 231; 230, identifier:float; 231, argument_list; 231, 232; 232, identifier:fp; 233, comment; 234, return_statement; 234, 235; 235, tuple; 235, 236; 235, 237; 235, 238; 235, 239; 236, identifier:sign; 237, identifier:hd; 238, identifier:mm; 239, identifier:ss
def deci2sexa(deci, pre=3, trunc=False, lower=None, upper=None, b=False, upper_trim=False): """Returns the sexagesimal representation of a decimal number. Parameters ---------- deci : float Decimal number to be converted into sexagesimal. If `lower` and `upper` are given then the number is normalized to the given range before converting to sexagesimal. pre : int The last part of the sexagesimal number is rounded to these many decimal places. This can be negative. Default is 3. trunc : bool If True then the last part of the sexagesimal number is truncated and not rounded to `pre` decimal places. Default is False. lower : int Lower bound of range to which number is to be normalized. upper : int Upper bound of range to which number is to be normalized. b : bool Affects type of normalization. See docstring for `normalize`. upper_trim : bool If `lower` and `upper` are given and this is True, then if the first part of the sexagesimal number is equal to `upper`, it is replaced with `lower` (value used is int(lower)). This converts numbers such as "24 00 00.000" to "00 00 00.000". Default value is False. Returns ------- s : 4 element tuple; (int, int, int, float) A tuple of sign and the three parts of the sexagesimal number. Sign is 1 for positive and -1 for negative values. The sign applies to the whole angle and not to any single part, i.e., all parts are positive and the sign multiplies the angle. The first and second parts of the sexagesimal number are integers and the last part is a float. Notes ----- The given decimal number `deci` is converted into a sexagesimal number. The last part of the sexagesimal number is rounded to `pre` number of decimal points. If `trunc == True` then instead of rounding, the last part is truncated. If `lower` and `upper` are given then the number is normalized to the given range before converting into sexagesimal format. The `b` argument determines the type of normalization. See docstring of the `normalize` function for details. If `upper_trim` is True then, if after convertion to sexagesimal the first part is equal to `upper`, it is replaced with `lower` (value used is int(lower)). This is useful in cases where numbers such as "24 00 00.00" needs to be converted into "00 00 00.00" The returned sign, first element of tuple, applies to the whole number and not just to a single part. Examples -------- >>> deci2sexa(-11.2345678) (-1, 11, 14, 4.444) >>> deci2sexa(-11.2345678, pre=5) (-1, 11, 14, 4.44408) >>> deci2sexa(-11.2345678, pre=4) (-1, 11, 14, 4.4441) >>> deci2sexa(-11.2345678, pre=4, trunc=True) (-1, 11, 14, 4.444) >>> deci2sexa(-11.2345678, pre=1) (-1, 11, 14, 4.4) >>> deci2sexa(-11.2345678, pre=0) (-1, 11, 14, 4.0) >>> deci2sexa(-11.2345678, pre=-1) (-1, 11, 14, 0.0) >>> x = 23+59/60.0+59.99999/3600.0 To 3 decimal places, this number is 24 or 0 hours. >>> deci2sexa(x, pre=3, lower=0, upper=24, upper_trim=True) (1, 0, 0, 0.0) >>> deci2sexa(x, pre=3, lower=0, upper=24, upper_trim=False) (1, 24, 0, 0.0) To 5 decimal places, we get back the full value. >>> deci2sexa(x, pre=5, lower=0, upper=24, upper_trim=True) (1, 23, 59, 59.99999) """ if lower is not None and upper is not None: deci = normalize(deci, lower=lower, upper=upper, b=b) sign = 1 if deci < 0: deci = abs(deci) sign = -1 hd, f1 = divmod(deci, 1) mm, f2 = divmod(f1 * 60.0, 1) sf = f2 * 60.0 # Find the seconds part to required precision. fp = 10 ** pre if trunc: ss, _ = divmod(sf * fp, 1) else: ss = round(sf * fp, 0) ss = int(ss) # If ss is 60 to given precision then update mm, and if necessary # hd. if ss == 60 * fp: mm += 1 ss = 0 if mm == 60: hd += 1 mm = 0 hd = int(hd) mm = int(mm) if lower is not None and upper is not None and upper_trim: # For example 24h0m0s => 0h0m0s. if hd == upper: hd = int(lower) if hd == 0 and mm == 0 and ss == 0: sign = 1 ss /= float(fp) # hd and mm parts are integer values but of type float return (sign, hd, mm, ss)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:phmsdms; 3, parameters; 3, 4; 4, identifier:hmsdms; 5, block; 5, 6; 5, 8; 5, 12; 5, 16; 5, 17; 5, 18; 5, 19; 5, 20; 5, 21; 5, 22; 5, 23; 5, 32; 5, 33; 5, 42; 5, 50; 5, 59; 5, 66; 5, 147; 5, 448; 5, 449; 5, 477; 5, 487; 5, 503; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:units; 11, None; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:sign; 15, None; 16, comment; 17, comment; 18, comment; 19, comment; 20, comment; 21, comment; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:pattern1; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:re; 29, identifier:compile; 30, argument_list; 30, 31; 31, string:r"([-+]?[0-9]*\.?[0-9]+[^0-9\-+]*)"; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:pattern2; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:re; 39, identifier:compile; 40, argument_list; 40, 41; 41, string:r"([-+]?[0-9]*\.?[0-9]+)"; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:hmsdms; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:hmsdms; 48, identifier:lower; 49, argument_list; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:hdlist; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:pattern1; 56, identifier:findall; 57, argument_list; 57, 58; 58, identifier:hmsdms; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:parts; 62, list:[None, None, None]; 62, 63; 62, 64; 62, 65; 63, None; 64, None; 65, None; 66, function_definition; 66, 67; 66, 68; 66, 69; 66, 70; 66, 71; 66, 72; 66, 73; 66, 74; 67, function_name:_fill_right_not_none; 68, parameters; 69, comment; 70, comment; 71, comment; 72, comment; 73, comment; 74, block; 74, 75; 74, 82; 74, 97; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:rp; 78, call; 78, 79; 78, 80; 79, identifier:reversed; 80, argument_list; 80, 81; 81, identifier:parts; 82, for_statement; 82, 83; 82, 86; 82, 90; 83, pattern_list; 83, 84; 83, 85; 84, identifier:i; 85, identifier:j; 86, call; 86, 87; 86, 88; 87, identifier:enumerate; 88, argument_list; 88, 89; 89, identifier:rp; 90, block; 90, 91; 91, if_statement; 91, 92; 91, 95; 92, comparison_operator:is; 92, 93; 92, 94; 93, identifier:j; 94, None; 95, block; 95, 96; 96, break_statement; 97, if_statement; 97, 98; 97, 101; 97, 102; 97, 108; 97, 119; 98, comparison_operator:==; 98, 99; 98, 100; 99, identifier:i; 100, integer:0; 101, comment; 102, block; 102, 103; 103, raise_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:ValueError; 106, argument_list; 106, 107; 107, string:"Invalid string."; 108, elif_clause; 108, 109; 108, 112; 109, comparison_operator:==; 109, 110; 109, 111; 110, identifier:i; 111, integer:1; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:parts; 117, integer:2; 118, identifier:v; 119, elif_clause; 119, 120; 119, 123; 119, 124; 119, 125; 120, comparison_operator:==; 120, 121; 120, 122; 121, identifier:i; 122, integer:2; 123, comment; 124, comment; 125, block; 125, 126; 126, if_statement; 126, 127; 126, 132; 126, 139; 127, comparison_operator:is; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:parts; 130, integer:0; 131, None; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:parts; 137, integer:0; 138, identifier:v; 139, else_clause; 139, 140; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:parts; 145, integer:1; 146, identifier:v; 147, for_statement; 147, 148; 147, 149; 147, 150; 148, identifier:valun; 149, identifier:hdlist; 150, block; 150, 151; 150, 440; 151, try_statement; 151, 152; 151, 153; 151, 167; 152, comment; 153, block; 153, 154; 153, 161; 153, 162; 153, 163; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:v; 157, call; 157, 158; 157, 159; 158, identifier:float; 159, argument_list; 159, 160; 160, identifier:valun; 161, comment; 162, comment; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:_fill_right_not_none; 166, argument_list; 167, except_clause; 167, 168; 167, 169; 167, 170; 167, 171; 168, identifier:ValueError; 169, comment; 170, comment; 171, block; 171, 172; 171, 216; 171, 260; 171, 300; 171, 340; 171, 376; 171, 412; 172, if_statement; 172, 173; 172, 180; 173, boolean_operator:or; 173, 174; 173, 177; 174, comparison_operator:in; 174, 175; 174, 176; 175, string:"hh"; 176, identifier:valun; 177, comparison_operator:in; 177, 178; 177, 179; 178, string:"h"; 179, identifier:valun; 180, block; 180, 181; 180, 190; 180, 212; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:m; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:pattern2; 187, identifier:search; 188, argument_list; 188, 189; 189, identifier:valun; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 195; 192, subscript; 192, 193; 192, 194; 193, identifier:parts; 194, integer:0; 195, call; 195, 196; 195, 197; 196, identifier:float; 197, argument_list; 197, 198; 198, subscript; 198, 199; 198, 200; 199, identifier:valun; 200, slice; 200, 201; 200, 206; 200, 207; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:m; 204, identifier:start; 205, argument_list; 206, colon; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:m; 210, identifier:end; 211, argument_list; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:units; 215, string:"hours"; 216, if_statement; 216, 217; 216, 224; 217, boolean_operator:or; 217, 218; 217, 221; 218, comparison_operator:in; 218, 219; 218, 220; 219, string:"dd"; 220, identifier:valun; 221, comparison_operator:in; 221, 222; 221, 223; 222, string:"d"; 223, identifier:valun; 224, block; 224, 225; 224, 234; 224, 256; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:m; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:pattern2; 231, identifier:search; 232, argument_list; 232, 233; 233, identifier:valun; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 239; 236, subscript; 236, 237; 236, 238; 237, identifier:parts; 238, integer:0; 239, call; 239, 240; 239, 241; 240, identifier:float; 241, argument_list; 241, 242; 242, subscript; 242, 243; 242, 244; 243, identifier:valun; 244, slice; 244, 245; 244, 250; 244, 251; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:m; 248, identifier:start; 249, argument_list; 250, colon; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:m; 254, identifier:end; 255, argument_list; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:units; 259, string:"degrees"; 260, if_statement; 260, 261; 260, 268; 261, boolean_operator:or; 261, 262; 261, 265; 262, comparison_operator:in; 262, 263; 262, 264; 263, string:"mm"; 264, identifier:valun; 265, comparison_operator:in; 265, 266; 265, 267; 266, string:"m"; 267, identifier:valun; 268, block; 268, 269; 268, 278; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:m; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:pattern2; 275, identifier:search; 276, argument_list; 276, 277; 277, identifier:valun; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 283; 280, subscript; 280, 281; 280, 282; 281, identifier:parts; 282, integer:1; 283, call; 283, 284; 283, 285; 284, identifier:float; 285, argument_list; 285, 286; 286, subscript; 286, 287; 286, 288; 287, identifier:valun; 288, slice; 288, 289; 288, 294; 288, 295; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:m; 292, identifier:start; 293, argument_list; 294, colon; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:m; 298, identifier:end; 299, argument_list; 300, if_statement; 300, 301; 300, 308; 301, boolean_operator:or; 301, 302; 301, 305; 302, comparison_operator:in; 302, 303; 302, 304; 303, string:"ss"; 304, identifier:valun; 305, comparison_operator:in; 305, 306; 305, 307; 306, string:"s"; 307, identifier:valun; 308, block; 308, 309; 308, 318; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:m; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:pattern2; 315, identifier:search; 316, argument_list; 316, 317; 317, identifier:valun; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 323; 320, subscript; 320, 321; 320, 322; 321, identifier:parts; 322, integer:2; 323, call; 323, 324; 323, 325; 324, identifier:float; 325, argument_list; 325, 326; 326, subscript; 326, 327; 326, 328; 327, identifier:valun; 328, slice; 328, 329; 328, 334; 328, 335; 329, call; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:m; 332, identifier:start; 333, argument_list; 334, colon; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:m; 338, identifier:end; 339, argument_list; 340, if_statement; 340, 341; 340, 344; 341, comparison_operator:in; 341, 342; 341, 343; 342, string:"'"; 343, identifier:valun; 344, block; 344, 345; 344, 354; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:m; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:pattern2; 351, identifier:search; 352, argument_list; 352, 353; 353, identifier:valun; 354, expression_statement; 354, 355; 355, assignment; 355, 356; 355, 359; 356, subscript; 356, 357; 356, 358; 357, identifier:parts; 358, integer:1; 359, call; 359, 360; 359, 361; 360, identifier:float; 361, argument_list; 361, 362; 362, subscript; 362, 363; 362, 364; 363, identifier:valun; 364, slice; 364, 365; 364, 370; 364, 371; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:m; 368, identifier:start; 369, argument_list; 370, colon; 371, call; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:m; 374, identifier:end; 375, argument_list; 376, if_statement; 376, 377; 376, 380; 377, comparison_operator:in; 377, 378; 377, 379; 378, string:'"'; 379, identifier:valun; 380, block; 380, 381; 380, 390; 381, expression_statement; 381, 382; 382, assignment; 382, 383; 382, 384; 383, identifier:m; 384, call; 384, 385; 384, 388; 385, attribute; 385, 386; 385, 387; 386, identifier:pattern2; 387, identifier:search; 388, argument_list; 388, 389; 389, identifier:valun; 390, expression_statement; 390, 391; 391, assignment; 391, 392; 391, 395; 392, subscript; 392, 393; 392, 394; 393, identifier:parts; 394, integer:2; 395, call; 395, 396; 395, 397; 396, identifier:float; 397, argument_list; 397, 398; 398, subscript; 398, 399; 398, 400; 399, identifier:valun; 400, slice; 400, 401; 400, 406; 400, 407; 401, call; 401, 402; 401, 405; 402, attribute; 402, 403; 402, 404; 403, identifier:m; 404, identifier:start; 405, argument_list; 406, colon; 407, call; 407, 408; 407, 411; 408, attribute; 408, 409; 408, 410; 409, identifier:m; 410, identifier:end; 411, argument_list; 412, if_statement; 412, 413; 412, 416; 412, 417; 412, 418; 413, comparison_operator:in; 413, 414; 413, 415; 414, string:":"; 415, identifier:valun; 416, comment; 417, comment; 418, block; 418, 419; 418, 429; 418, 436; 419, expression_statement; 419, 420; 420, assignment; 420, 421; 420, 422; 421, identifier:v; 422, call; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:valun; 425, identifier:replace; 426, argument_list; 426, 427; 426, 428; 427, string:":"; 428, string:""; 429, expression_statement; 429, 430; 430, assignment; 430, 431; 430, 432; 431, identifier:v; 432, call; 432, 433; 432, 434; 433, identifier:float; 434, argument_list; 434, 435; 435, identifier:v; 436, expression_statement; 436, 437; 437, call; 437, 438; 437, 439; 438, identifier:_fill_right_not_none; 439, argument_list; 440, if_statement; 440, 441; 440, 443; 441, not_operator; 441, 442; 442, identifier:units; 443, block; 443, 444; 444, expression_statement; 444, 445; 445, assignment; 445, 446; 445, 447; 446, identifier:units; 447, string:"degrees"; 448, comment; 449, for_statement; 449, 450; 449, 451; 449, 452; 450, identifier:i; 451, identifier:parts; 452, block; 452, 453; 453, if_statement; 453, 454; 453, 459; 454, boolean_operator:and; 454, 455; 454, 456; 455, identifier:i; 456, comparison_operator:<; 456, 457; 456, 458; 457, identifier:i; 458, float:0.0; 459, block; 459, 460; 460, if_statement; 460, 461; 460, 464; 460, 470; 461, comparison_operator:is; 461, 462; 461, 463; 462, identifier:sign; 463, None; 464, block; 464, 465; 465, expression_statement; 465, 466; 466, assignment; 466, 467; 466, 468; 467, identifier:sign; 468, unary_operator:-; 468, 469; 469, integer:1; 470, else_clause; 470, 471; 471, block; 471, 472; 472, raise_statement; 472, 473; 473, call; 473, 474; 473, 475; 474, identifier:ValueError; 475, argument_list; 475, 476; 476, string:"Only one number can be negative."; 477, if_statement; 477, 478; 477, 481; 477, 482; 478, comparison_operator:is; 478, 479; 478, 480; 479, identifier:sign; 480, None; 481, comment; 482, block; 482, 483; 483, expression_statement; 483, 484; 484, assignment; 484, 485; 484, 486; 485, identifier:sign; 486, integer:1; 487, expression_statement; 487, 488; 488, assignment; 488, 489; 488, 490; 489, identifier:vals; 490, list_comprehension; 490, 491; 490, 500; 491, conditional_expression:if; 491, 492; 491, 496; 491, 499; 492, call; 492, 493; 492, 494; 493, identifier:abs; 494, argument_list; 494, 495; 495, identifier:i; 496, comparison_operator:is; 496, 497; 496, 498; 497, identifier:i; 498, None; 499, float:0.0; 500, for_in_clause; 500, 501; 500, 502; 501, identifier:i; 502, identifier:parts; 503, return_statement; 503, 504; 504, call; 504, 505; 504, 506; 505, identifier:dict; 506, argument_list; 506, 507; 506, 510; 506, 513; 506, 516; 507, keyword_argument; 507, 508; 507, 509; 508, identifier:sign; 509, identifier:sign; 510, keyword_argument; 510, 511; 510, 512; 511, identifier:units; 512, identifier:units; 513, keyword_argument; 513, 514; 513, 515; 514, identifier:vals; 515, identifier:vals; 516, keyword_argument; 516, 517; 516, 518; 517, identifier:parts; 518, identifier:parts
def phmsdms(hmsdms): """Parse a string containing a sexagesimal number. This can handle several types of delimiters and will process reasonably valid strings. See examples. Parameters ---------- hmsdms : str String containing a sexagesimal number. Returns ------- d : dict parts : a 3 element list of floats The three parts of the sexagesimal number that were identified. vals : 3 element list of floats The numerical values of the three parts of the sexagesimal number. sign : int Sign of the sexagesimal number; 1 for positive and -1 for negative. units : {"degrees", "hours"} The units of the sexagesimal number. This is infered from the characters present in the string. If it a pure number then units is "degrees". Examples -------- >>> phmsdms("12") == { ... 'parts': [12.0, None, None], ... 'sign': 1, ... 'units': 'degrees', ... 'vals': [12.0, 0.0, 0.0] ... } True >>> phmsdms("12h") == { ... 'parts': [12.0, None, None], ... 'sign': 1, ... 'units': 'hours', ... 'vals': [12.0, 0.0, 0.0] ... } True >>> phmsdms("12d13m14.56") == { ... 'parts': [12.0, 13.0, 14.56], ... 'sign': 1, ... 'units': 'degrees', ... 'vals': [12.0, 13.0, 14.56] ... } True >>> phmsdms("12d13m14.56") == { ... 'parts': [12.0, 13.0, 14.56], ... 'sign': 1, ... 'units': 'degrees', ... 'vals': [12.0, 13.0, 14.56] ... } True >>> phmsdms("12d14.56ss") == { ... 'parts': [12.0, None, 14.56], ... 'sign': 1, ... 'units': 'degrees', ... 'vals': [12.0, 0.0, 14.56] ... } True >>> phmsdms("14.56ss") == { ... 'parts': [None, None, 14.56], ... 'sign': 1, ... 'units': 'degrees', ... 'vals': [0.0, 0.0, 14.56] ... } True >>> phmsdms("12h13m12.4s") == { ... 'parts': [12.0, 13.0, 12.4], ... 'sign': 1, ... 'units': 'hours', ... 'vals': [12.0, 13.0, 12.4] ... } True >>> phmsdms("12:13:12.4s") == { ... 'parts': [12.0, 13.0, 12.4], ... 'sign': 1, ... 'units': 'degrees', ... 'vals': [12.0, 13.0, 12.4] ... } True But `phmsdms("12:13mm:12.4s")` will not work. """ units = None sign = None # Floating point regex: # http://www.regular-expressions.info/floatingpoint.html # # pattern1: find a decimal number (int or float) and any # characters following it upto the next decimal number. [^0-9\-+]* # => keep gathering elements until we get to a digit, a - or a # +. These three indicates the possible start of the next number. pattern1 = re.compile(r"([-+]?[0-9]*\.?[0-9]+[^0-9\-+]*)") # pattern2: find decimal number (int or float) in string. pattern2 = re.compile(r"([-+]?[0-9]*\.?[0-9]+)") hmsdms = hmsdms.lower() hdlist = pattern1.findall(hmsdms) parts = [None, None, None] def _fill_right_not_none(): # Find the pos. where parts is not None. Next value must # be inserted to the right of this. If this is 2 then we have # already filled seconds part, raise exception. If this is 1 # then fill 2. If this is 0 fill 1. If none of these then fill # 0. rp = reversed(parts) for i, j in enumerate(rp): if j is not None: break if i == 0: # Seconds part already filled. raise ValueError("Invalid string.") elif i == 1: parts[2] = v elif i == 2: # Either parts[0] is None so fill it, or it is filled # and hence fill parts[1]. if parts[0] is None: parts[0] = v else: parts[1] = v for valun in hdlist: try: # See if this is pure number. v = float(valun) # Sexagesimal part cannot be determined. So guess it by # seeing which all parts have already been identified. _fill_right_not_none() except ValueError: # Not a pure number. Infer sexagesimal part from the # suffix. if "hh" in valun or "h" in valun: m = pattern2.search(valun) parts[0] = float(valun[m.start():m.end()]) units = "hours" if "dd" in valun or "d" in valun: m = pattern2.search(valun) parts[0] = float(valun[m.start():m.end()]) units = "degrees" if "mm" in valun or "m" in valun: m = pattern2.search(valun) parts[1] = float(valun[m.start():m.end()]) if "ss" in valun or "s" in valun: m = pattern2.search(valun) parts[2] = float(valun[m.start():m.end()]) if "'" in valun: m = pattern2.search(valun) parts[1] = float(valun[m.start():m.end()]) if '"' in valun: m = pattern2.search(valun) parts[2] = float(valun[m.start():m.end()]) if ":" in valun: # Sexagesimal part cannot be determined. So guess it by # seeing which all parts have already been identified. v = valun.replace(":", "") v = float(v) _fill_right_not_none() if not units: units = "degrees" # Find sign. Only the first identified part can have a -ve sign. for i in parts: if i and i < 0.0: if sign is None: sign = -1 else: raise ValueError("Only one number can be negative.") if sign is None: # None of these are negative. sign = 1 vals = [abs(i) if i is not None else 0.0 for i in parts] return dict(sign=sign, units=units, vals=vals, parts=parts)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:pposition; 3, parameters; 3, 4; 3, 5; 4, identifier:hd; 5, default_parameter; 5, 6; 5, 7; 6, identifier:details; 7, False; 8, block; 8, 9; 8, 11; 8, 12; 8, 13; 8, 23; 8, 38; 8, 39; 8, 166; 8, 198; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:p; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:re; 19, identifier:split; 20, argument_list; 20, 21; 20, 22; 21, string:r"[^\d\-+.]*"; 22, identifier:hd; 23, if_statement; 23, 24; 23, 32; 24, comparison_operator:not; 24, 25; 24, 29; 25, call; 25, 26; 25, 27; 26, identifier:len; 27, argument_list; 27, 28; 28, identifier:p; 29, list:[2, 6]; 29, 30; 29, 31; 30, integer:2; 31, integer:6; 32, block; 32, 33; 33, raise_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:ValueError; 36, argument_list; 36, 37; 37, string:"Input must contain either 2 or 6 numbers."; 38, comment; 39, if_statement; 39, 40; 39, 46; 39, 84; 39, 85; 40, comparison_operator:==; 40, 41; 40, 45; 41, call; 41, 42; 41, 43; 42, identifier:len; 43, argument_list; 43, 44; 44, identifier:p; 45, integer:2; 46, block; 46, 47; 46, 65; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, pattern_list; 49, 50; 49, 51; 50, identifier:x; 51, identifier:y; 52, expression_list; 52, 53; 52, 59; 53, call; 53, 54; 53, 55; 54, identifier:float; 55, argument_list; 55, 56; 56, subscript; 56, 57; 56, 58; 57, identifier:p; 58, integer:0; 59, call; 59, 60; 59, 61; 60, identifier:float; 61, argument_list; 61, 62; 62, subscript; 62, 63; 62, 64; 63, identifier:p; 64, integer:1; 65, if_statement; 65, 66; 65, 67; 66, identifier:details; 67, block; 67, 68; 67, 72; 67, 78; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:numvals; 71, integer:2; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:raw_x; 75, subscript; 75, 76; 75, 77; 76, identifier:p; 77, integer:0; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:raw_y; 81, subscript; 81, 82; 81, 83; 82, identifier:p; 83, integer:1; 84, comment; 85, elif_clause; 85, 86; 85, 92; 86, comparison_operator:==; 86, 87; 86, 91; 87, call; 87, 88; 87, 89; 88, identifier:len; 89, argument_list; 89, 90; 90, identifier:p; 91, integer:6; 92, block; 92, 93; 92, 109; 92, 122; 92, 138; 92, 151; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:x_p; 96, call; 96, 97; 96, 98; 97, identifier:phmsdms; 98, argument_list; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, string:" "; 102, identifier:join; 103, argument_list; 103, 104; 104, subscript; 104, 105; 104, 106; 105, identifier:p; 106, slice; 106, 107; 106, 108; 107, colon; 108, integer:3; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:x; 112, call; 112, 113; 112, 114; 113, identifier:sexa2deci; 114, argument_list; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:x_p; 117, string:'sign'; 118, list_splat; 118, 119; 119, subscript; 119, 120; 119, 121; 120, identifier:x_p; 121, string:'vals'; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:y_p; 125, call; 125, 126; 125, 127; 126, identifier:phmsdms; 127, argument_list; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, string:" "; 131, identifier:join; 132, argument_list; 132, 133; 133, subscript; 133, 134; 133, 135; 134, identifier:p; 135, slice; 135, 136; 135, 137; 136, integer:3; 137, colon; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:y; 141, call; 141, 142; 141, 143; 142, identifier:sexa2deci; 143, argument_list; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:y_p; 146, string:'sign'; 147, list_splat; 147, 148; 148, subscript; 148, 149; 148, 150; 149, identifier:y_p; 150, string:'vals'; 151, if_statement; 151, 152; 151, 153; 152, identifier:details; 153, block; 153, 154; 153, 158; 153, 162; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:raw_x; 157, identifier:x_p; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:raw_y; 161, identifier:y_p; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:numvals; 165, integer:6; 166, if_statement; 166, 167; 166, 168; 166, 190; 167, identifier:details; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:result; 172, call; 172, 173; 172, 174; 173, identifier:dict; 174, argument_list; 174, 175; 174, 178; 174, 181; 174, 184; 174, 187; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:x; 177, identifier:x; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:y; 180, identifier:y; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:numvals; 183, identifier:numvals; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:raw_x; 186, identifier:raw_x; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:raw_y; 189, identifier:raw_y; 190, else_clause; 190, 191; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:result; 195, expression_list; 195, 196; 195, 197; 196, identifier:x; 197, identifier:y; 198, return_statement; 198, 199; 199, identifier:result
def pposition(hd, details=False): """Parse string into angular position. A string containing 2 or 6 numbers is parsed, and the numbers are converted into decimal numbers. In the former case the numbers are assumed to be floats. In the latter case, the numbers are assumed to be sexagesimal. Parameters ---------- hd: str String containing 2 or 6 numbers. The numbers can be spearated with character or characters other than ".", "-", "+". The string must contain either 2 or 6 numbers. details: bool The detailed result from parsing the string is returned. See "Returns" section below. Default is False. Returns ------- x: (float, float) or dict A tuple containing decimal equivalents of the parsed numbers. If the string contains 6 numbers then they are assumed be sexagesimal components. If ``details`` is True then a dictionary with the following keys is returned: x: float The first number. y: float The second number numvals: int Number of items parsed; 2 or 6. raw_x: dict The result returned by ``phmsdms`` for the first number. raw_y: dict The result returned by ``phmsdms`` for the second number. It is up to the user to interpret the units of the numbers returned. Raises ------ ValueError: The exception is raised if the string cannot be interpreted as a sequence of 2 or 6 numbers. Examples -------- The position of M100 reported by SIMBAD is "12 22 54.899 +15 49 20.57". This can be easily parsed in the following manner. >>> from angles import pposition >>> ra, de = pposition("12 22 54.899 +15 49 20.57") >>> ra 12.38191638888889 >>> de 15.822380555555556 """ # :TODO: split two angles based on user entered separator and process each part separately. # Split at any character other than a digit, ".", "-", and "+". p = re.split(r"[^\d\-+.]*", hd) if len(p) not in [2, 6]: raise ValueError("Input must contain either 2 or 6 numbers.") # Two floating point numbers if string has 2 numbers. if len(p) == 2: x, y = float(p[0]), float(p[1]) if details: numvals = 2 raw_x = p[0] raw_y = p[1] # Two sexagesimal numbers if string has 6 numbers. elif len(p) == 6: x_p = phmsdms(" ".join(p[:3])) x = sexa2deci(x_p['sign'], *x_p['vals']) y_p = phmsdms(" ".join(p[3:])) y = sexa2deci(y_p['sign'], *y_p['vals']) if details: raw_x = x_p raw_y = y_p numvals = 6 if details: result = dict(x=x, y=y, numvals=numvals, raw_x=raw_x, raw_y=raw_y) else: result = x, y return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 68; 2, function_name:Create; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 3, 32; 3, 35; 3, 38; 3, 41; 3, 44; 3, 47; 3, 50; 3, 53; 3, 56; 3, 59; 3, 62; 3, 65; 4, identifier:name; 5, identifier:template; 6, identifier:group_id; 7, identifier:network_id; 8, default_parameter; 8, 9; 8, 10; 9, identifier:cpu; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:memory; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:alias; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:password; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:ip_address; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:storage_type; 25, string:"standard"; 26, default_parameter; 26, 27; 26, 28; 27, identifier:type; 28, string:"standard"; 29, default_parameter; 29, 30; 29, 31; 30, identifier:primary_dns; 31, None; 32, default_parameter; 32, 33; 32, 34; 33, identifier:secondary_dns; 34, None; 35, default_parameter; 35, 36; 35, 37; 36, identifier:additional_disks; 37, list:[]; 38, default_parameter; 38, 39; 38, 40; 39, identifier:custom_fields; 40, list:[]; 41, default_parameter; 41, 42; 41, 43; 42, identifier:ttl; 43, None; 44, default_parameter; 44, 45; 44, 46; 45, identifier:managed_os; 46, False; 47, default_parameter; 47, 48; 47, 49; 48, identifier:description; 49, None; 50, default_parameter; 50, 51; 50, 52; 51, identifier:source_server_password; 52, None; 53, default_parameter; 53, 54; 53, 55; 54, identifier:cpu_autoscale_policy_id; 55, None; 56, default_parameter; 56, 57; 56, 58; 57, identifier:anti_affinity_policy_id; 58, None; 59, default_parameter; 59, 60; 59, 61; 60, identifier:packages; 61, list:[]; 62, default_parameter; 62, 63; 62, 64; 63, identifier:configuration_id; 64, None; 65, default_parameter; 65, 66; 65, 67; 66, identifier:session; 67, None; 68, block; 68, 69; 68, 71; 68, 90; 68, 98; 68, 197; 68, 224; 68, 249; 68, 262; 68, 277; 68, 302; 68, 303; 68, 304; 68, 305; 68, 306; 68, 307; 68, 338; 68, 401; 69, expression_statement; 69, 70; 70, comment; 71, if_statement; 71, 72; 71, 74; 72, not_operator; 72, 73; 73, identifier:alias; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:alias; 78, call; 78, 79; 78, 86; 79, attribute; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:clc; 83, identifier:v2; 84, identifier:Account; 85, identifier:GetAlias; 86, argument_list; 86, 87; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:session; 89, identifier:session; 90, if_statement; 90, 91; 90, 93; 91, not_operator; 91, 92; 92, identifier:description; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:description; 97, identifier:name; 98, if_statement; 98, 99; 98, 106; 99, comparison_operator:!=; 99, 100; 99, 105; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:type; 103, identifier:lower; 104, argument_list; 105, string:"baremetal"; 106, block; 106, 107; 106, 133; 106, 165; 107, if_statement; 107, 108; 107, 113; 108, boolean_operator:or; 108, 109; 108, 111; 109, not_operator; 109, 110; 110, identifier:cpu; 111, not_operator; 111, 112; 112, identifier:memory; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:group; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:clc; 121, identifier:v2; 122, identifier:Group; 123, argument_list; 123, 124; 123, 127; 123, 130; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:id; 126, identifier:group_id; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:alias; 129, identifier:alias; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:session; 132, identifier:session; 133, if_statement; 133, 134; 133, 143; 133, 153; 134, boolean_operator:and; 134, 135; 134, 137; 135, not_operator; 135, 136; 136, identifier:cpu; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:group; 140, identifier:Defaults; 141, argument_list; 141, 142; 142, string:"cpu"; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:cpu; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:group; 150, identifier:Defaults; 151, argument_list; 151, 152; 152, string:"cpu"; 153, elif_clause; 153, 154; 153, 156; 154, not_operator; 154, 155; 155, identifier:cpu; 156, block; 156, 157; 157, raise_statement; 157, 158; 158, parenthesized_expression; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:clc; 162, identifier:CLCException; 163, argument_list; 163, 164; 164, string:"No default CPU defined"; 165, if_statement; 165, 166; 165, 175; 165, 185; 166, boolean_operator:and; 166, 167; 166, 169; 167, not_operator; 167, 168; 168, identifier:memory; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:group; 172, identifier:Defaults; 173, argument_list; 173, 174; 174, string:"memory"; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:memory; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:group; 182, identifier:Defaults; 183, argument_list; 183, 184; 184, string:"memory"; 185, elif_clause; 185, 186; 185, 188; 186, not_operator; 186, 187; 187, identifier:memory; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, parenthesized_expression; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:clc; 194, identifier:CLCException; 195, argument_list; 195, 196; 196, string:"No default Memory defined"; 197, if_statement; 197, 198; 197, 215; 198, boolean_operator:and; 198, 199; 198, 206; 199, comparison_operator:==; 199, 200; 199, 205; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:type; 203, identifier:lower; 204, argument_list; 205, string:"standard"; 206, comparison_operator:not; 206, 207; 206, 212; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:storage_type; 210, identifier:lower; 211, argument_list; 212, tuple; 212, 213; 212, 214; 213, string:"standard"; 214, string:"premium"; 215, block; 215, 216; 216, raise_statement; 216, 217; 217, parenthesized_expression; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:clc; 221, identifier:CLCException; 222, argument_list; 222, 223; 223, string:"Invalid type/storage_type combo"; 224, if_statement; 224, 225; 224, 240; 225, boolean_operator:and; 225, 226; 225, 233; 226, comparison_operator:==; 226, 227; 226, 232; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:type; 230, identifier:lower; 231, argument_list; 232, string:"hyperscale"; 233, comparison_operator:!=; 233, 234; 233, 239; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:storage_type; 237, identifier:lower; 238, argument_list; 239, string:"hyperscale"; 240, block; 240, 241; 241, raise_statement; 241, 242; 242, parenthesized_expression; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:clc; 246, identifier:CLCException; 247, argument_list; 247, 248; 248, string:"Invalid type/storage_type combo"; 249, if_statement; 249, 250; 249, 257; 250, comparison_operator:==; 250, 251; 250, 256; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:type; 254, identifier:lower; 255, argument_list; 256, string:"baremetal"; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:type; 261, string:"bareMetal"; 262, if_statement; 262, 263; 262, 268; 263, boolean_operator:and; 263, 264; 263, 265; 264, identifier:ttl; 265, comparison_operator:<=; 265, 266; 265, 267; 266, identifier:ttl; 267, integer:3600; 268, block; 268, 269; 269, raise_statement; 269, 270; 270, parenthesized_expression; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:clc; 274, identifier:CLCException; 275, argument_list; 275, 276; 276, string:"ttl must be greater than 3600 seconds"; 277, if_statement; 277, 278; 277, 279; 278, identifier:ttl; 279, block; 279, 280; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 283; 282, identifier:ttl; 283, call; 283, 284; 283, 291; 284, attribute; 284, 285; 284, 290; 285, attribute; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:clc; 288, identifier:v2; 289, identifier:time_utils; 290, identifier:SecondsToZuluTS; 291, argument_list; 291, 292; 292, binary_operator:+; 292, 293; 292, 301; 293, call; 293, 294; 293, 295; 294, identifier:int; 295, argument_list; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:time; 299, identifier:time; 300, argument_list; 301, identifier:ttl; 302, comment; 303, comment; 304, comment; 305, comment; 306, comment; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:payload; 310, dictionary; 310, 311; 310, 314; 310, 317; 310, 320; 310, 323; 310, 326; 310, 329; 310, 332; 310, 335; 311, pair; 311, 312; 311, 313; 312, string:'name'; 313, identifier:name; 314, pair; 314, 315; 314, 316; 315, string:'description'; 316, identifier:description; 317, pair; 317, 318; 317, 319; 318, string:'groupId'; 319, identifier:group_id; 320, pair; 320, 321; 320, 322; 321, string:'primaryDNS'; 322, identifier:primary_dns; 323, pair; 323, 324; 323, 325; 324, string:'secondaryDNS'; 325, identifier:secondary_dns; 326, pair; 326, 327; 326, 328; 327, string:'networkId'; 328, identifier:network_id; 329, pair; 329, 330; 329, 331; 330, string:'password'; 331, identifier:password; 332, pair; 332, 333; 332, 334; 333, string:'type'; 334, identifier:type; 335, pair; 335, 336; 335, 337; 336, string:'customFields'; 337, identifier:custom_fields; 338, if_statement; 338, 339; 338, 342; 338, 356; 339, comparison_operator:==; 339, 340; 339, 341; 340, identifier:type; 341, string:'bareMetal'; 342, block; 342, 343; 343, expression_statement; 343, 344; 344, call; 344, 345; 344, 348; 345, attribute; 345, 346; 345, 347; 346, identifier:payload; 347, identifier:update; 348, argument_list; 348, 349; 349, dictionary; 349, 350; 349, 353; 350, pair; 350, 351; 350, 352; 351, string:'configurationId'; 352, identifier:configuration_id; 353, pair; 353, 354; 353, 355; 354, string:'osType'; 355, identifier:template; 356, else_clause; 356, 357; 357, block; 357, 358; 358, expression_statement; 358, 359; 359, call; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:payload; 362, identifier:update; 363, argument_list; 363, 364; 364, dictionary; 364, 365; 364, 368; 364, 371; 364, 374; 364, 377; 364, 380; 364, 383; 364, 386; 364, 389; 364, 392; 364, 395; 364, 398; 365, pair; 365, 366; 365, 367; 366, string:'sourceServerId'; 367, identifier:template; 368, pair; 368, 369; 368, 370; 369, string:'isManagedOS'; 370, identifier:managed_os; 371, pair; 371, 372; 371, 373; 372, string:'ipAddress'; 373, identifier:ip_address; 374, pair; 374, 375; 374, 376; 375, string:'sourceServerPassword'; 376, identifier:source_server_password; 377, pair; 377, 378; 377, 379; 378, string:'cpu'; 379, identifier:cpu; 380, pair; 380, 381; 380, 382; 381, string:'cpuAutoscalePolicyId'; 382, identifier:cpu_autoscale_policy_id; 383, pair; 383, 384; 383, 385; 384, string:'memoryGB'; 385, identifier:memory; 386, pair; 386, 387; 386, 388; 387, string:'storageType'; 388, identifier:storage_type; 389, pair; 389, 390; 389, 391; 390, string:'antiAffinityPolicyId'; 391, identifier:anti_affinity_policy_id; 392, pair; 392, 393; 392, 394; 393, string:'additionalDisks'; 394, identifier:additional_disks; 395, pair; 395, 396; 395, 397; 396, string:'ttl'; 397, identifier:ttl; 398, pair; 398, 399; 398, 400; 399, string:'packages'; 400, identifier:packages; 401, return_statement; 401, 402; 402, call; 402, 403; 402, 408; 403, attribute; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:clc; 406, identifier:v2; 407, identifier:Requests; 408, argument_list; 408, 409; 408, 432; 408, 435; 409, call; 409, 410; 409, 417; 410, attribute; 410, 411; 410, 416; 411, attribute; 411, 412; 411, 415; 412, attribute; 412, 413; 412, 414; 413, identifier:clc; 414, identifier:v2; 415, identifier:API; 416, identifier:Call; 417, argument_list; 417, 418; 417, 419; 417, 423; 417, 429; 418, string:'POST'; 419, binary_operator:%; 419, 420; 419, 421; 420, string:'servers/%s'; 421, parenthesized_expression; 421, 422; 422, identifier:alias; 423, call; 423, 424; 423, 427; 424, attribute; 424, 425; 424, 426; 425, identifier:json; 426, identifier:dumps; 427, argument_list; 427, 428; 428, identifier:payload; 429, keyword_argument; 429, 430; 429, 431; 430, identifier:session; 431, identifier:session; 432, keyword_argument; 432, 433; 432, 434; 433, identifier:alias; 434, identifier:alias; 435, keyword_argument; 435, 436; 435, 437; 436, identifier:session; 437, identifier:session
def Create(name,template,group_id,network_id,cpu=None,memory=None,alias=None,password=None,ip_address=None, storage_type="standard",type="standard",primary_dns=None,secondary_dns=None, additional_disks=[],custom_fields=[],ttl=None,managed_os=False,description=None, source_server_password=None,cpu_autoscale_policy_id=None,anti_affinity_policy_id=None, packages=[],configuration_id=None,session=None): """Creates a new server. https://www.centurylinkcloud.com/api-docs/v2/#servers-create-server cpu and memory are optional and if not provided we pull from the default server size values associated with the provided group_id. Set ttl as number of seconds before server is to be terminated. Must be >3600 >>> d = clc.v2.Datacenter() >>> clc.v2.Server.Create(name="api2",cpu=1,memory=1, group_id=d.Groups().Get("Default Group").id, template=d.Templates().Search("centos-6-64")[0].id, network_id=d.Networks().networks[0].id).WaitUntilComplete() 0 """ if not alias: alias = clc.v2.Account.GetAlias(session=session) if not description: description = name if type.lower() != "baremetal": if not cpu or not memory: group = clc.v2.Group(id=group_id,alias=alias,session=session) if not cpu and group.Defaults("cpu"): cpu = group.Defaults("cpu") elif not cpu: raise(clc.CLCException("No default CPU defined")) if not memory and group.Defaults("memory"): memory = group.Defaults("memory") elif not memory: raise(clc.CLCException("No default Memory defined")) if type.lower() == "standard" and storage_type.lower() not in ("standard","premium"): raise(clc.CLCException("Invalid type/storage_type combo")) if type.lower() == "hyperscale" and storage_type.lower() != "hyperscale": raise(clc.CLCException("Invalid type/storage_type combo")) if type.lower() == "baremetal": type = "bareMetal" if ttl and ttl<=3600: raise(clc.CLCException("ttl must be greater than 3600 seconds")) if ttl: ttl = clc.v2.time_utils.SecondsToZuluTS(int(time.time())+ttl) # TODO - validate custom_fields as a list of dicts with an id and a value key # TODO - validate template exists # TODO - validate additional_disks as a list of dicts with a path, sizeGB, and type (partitioned,raw) keys # TODO - validate addition_disks path not in template reserved paths # TODO - validate antiaffinity policy id set only with type=hyperscale payload = { 'name': name, 'description': description, 'groupId': group_id, 'primaryDNS': primary_dns, 'secondaryDNS': secondary_dns, 'networkId': network_id, 'password': password, 'type': type, 'customFields': custom_fields } if type == 'bareMetal': payload.update({'configurationId': configuration_id, 'osType': template}) else: payload.update({'sourceServerId': template, 'isManagedOS': managed_os, 'ipAddress': ip_address, 'sourceServerPassword': source_server_password, 'cpu': cpu, 'cpuAutoscalePolicyId': cpu_autoscale_policy_id, 'memoryGB': memory, 'storageType': storage_type, 'antiAffinityPolicyId': anti_affinity_policy_id, 'additionalDisks': additional_disks, 'ttl': ttl, 'packages': packages}) return clc.v2.Requests(clc.v2.API.Call('POST','servers/%s' % (alias), json.dumps(payload), session=session), alias=alias, session=session)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 66; 2, function_name:Clone; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 3, 36; 3, 39; 3, 42; 3, 45; 3, 48; 3, 51; 3, 54; 3, 57; 3, 60; 3, 63; 4, identifier:self; 5, identifier:network_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:cpu; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:memory; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:group_id; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:alias; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:password; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:ip_address; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:storage_type; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:type; 32, None; 33, default_parameter; 33, 34; 33, 35; 34, identifier:primary_dns; 35, None; 36, default_parameter; 36, 37; 36, 38; 37, identifier:secondary_dns; 38, None; 39, default_parameter; 39, 40; 39, 41; 40, identifier:custom_fields; 41, None; 42, default_parameter; 42, 43; 42, 44; 43, identifier:ttl; 44, None; 45, default_parameter; 45, 46; 45, 47; 46, identifier:managed_os; 47, False; 48, default_parameter; 48, 49; 48, 50; 49, identifier:description; 50, None; 51, default_parameter; 51, 52; 51, 53; 52, identifier:source_server_password; 53, None; 54, default_parameter; 54, 55; 54, 56; 55, identifier:cpu_autoscale_policy_id; 56, None; 57, default_parameter; 57, 58; 57, 59; 58, identifier:anti_affinity_policy_id; 59, None; 60, default_parameter; 60, 61; 60, 62; 61, identifier:packages; 62, list:[]; 63, default_parameter; 63, 64; 63, 65; 64, identifier:count; 65, integer:1; 66, block; 66, 67; 66, 69; 66, 94; 66, 95; 66, 105; 66, 115; 66, 125; 66, 135; 66, 149; 66, 158; 66, 168; 66, 178; 66, 188; 66, 205; 66, 215; 66, 216; 66, 217; 66, 218; 66, 222; 66, 314; 67, expression_statement; 67, 68; 68, comment; 69, if_statement; 69, 70; 69, 72; 70, not_operator; 70, 71; 71, identifier:name; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:name; 76, call; 76, 77; 76, 92; 77, attribute; 77, 78; 77, 91; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:re; 81, identifier:search; 82, argument_list; 82, 83; 82, 88; 83, binary_operator:%; 83, 84; 83, 85; 84, string:"%s(.+)\d{2}$"; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:alias; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:name; 91, identifier:group; 92, argument_list; 92, 93; 93, integer:1; 94, comment; 95, if_statement; 95, 96; 95, 98; 96, not_operator; 96, 97; 97, identifier:cpu; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:cpu; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:cpu; 105, if_statement; 105, 106; 105, 108; 106, not_operator; 106, 107; 107, identifier:memory; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:memory; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:memory; 115, if_statement; 115, 116; 115, 118; 116, not_operator; 116, 117; 117, identifier:group_id; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:group_id; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:group_id; 125, if_statement; 125, 126; 125, 128; 126, not_operator; 126, 127; 127, identifier:alias; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:alias; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:alias; 135, if_statement; 135, 136; 135, 138; 136, not_operator; 136, 137; 137, identifier:source_server_password; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:source_server_password; 142, subscript; 142, 143; 142, 148; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:Credentials; 147, argument_list; 148, string:'password'; 149, if_statement; 149, 150; 149, 152; 150, not_operator; 150, 151; 151, identifier:password; 152, block; 152, 153; 152, 157; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:password; 156, identifier:source_server_password; 157, block:# is this the expected behavior?; 158, if_statement; 158, 159; 158, 161; 159, not_operator; 159, 160; 160, identifier:storage_type; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:storage_type; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:storage_type; 168, if_statement; 168, 169; 168, 171; 169, not_operator; 169, 170; 170, identifier:type; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:type; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:type; 178, if_statement; 178, 179; 178, 181; 179, not_operator; 179, 180; 180, identifier:storage_type; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:storage_type; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:storage_type; 188, if_statement; 188, 189; 188, 198; 189, boolean_operator:and; 189, 190; 189, 192; 190, not_operator; 190, 191; 191, identifier:custom_fields; 192, call; 192, 193; 192, 194; 193, identifier:len; 194, argument_list; 194, 195; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:custom_fields; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:custom_fields; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:custom_fields; 205, if_statement; 205, 206; 205, 208; 206, not_operator; 206, 207; 207, identifier:description; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:description; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:description; 215, comment; 216, comment; 217, comment; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:requests_lst; 221, list:[]; 222, for_statement; 222, 223; 222, 224; 222, 229; 223, identifier:i; 224, call; 224, 225; 224, 226; 225, identifier:range; 226, argument_list; 226, 227; 226, 228; 227, integer:0; 228, identifier:count; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:requests_lst; 234, identifier:append; 235, argument_list; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:Server; 239, identifier:Create; 240, argument_list; 240, 241; 240, 242; 240, 245; 240, 248; 240, 251; 240, 254; 240, 257; 240, 262; 240, 265; 240, 268; 240, 271; 240, 274; 240, 277; 240, 280; 240, 283; 240, 286; 240, 289; 240, 292; 240, 295; 240, 298; 240, 301; 240, 304; 240, 309; 241, line_continuation:\; 242, keyword_argument; 242, 243; 242, 244; 243, identifier:name; 244, identifier:name; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:cpu; 247, identifier:cpu; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:memory; 250, identifier:memory; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:group_id; 253, identifier:group_id; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:network_id; 256, identifier:network_id; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:alias; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:alias; 262, keyword_argument; 262, 263; 262, 264; 263, identifier:password; 264, identifier:password; 265, keyword_argument; 265, 266; 265, 267; 266, identifier:ip_address; 267, identifier:ip_address; 268, keyword_argument; 268, 269; 268, 270; 269, identifier:storage_type; 270, identifier:storage_type; 271, keyword_argument; 271, 272; 271, 273; 272, identifier:type; 273, identifier:type; 274, keyword_argument; 274, 275; 274, 276; 275, identifier:primary_dns; 276, identifier:primary_dns; 277, keyword_argument; 277, 278; 277, 279; 278, identifier:secondary_dns; 279, identifier:secondary_dns; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:custom_fields; 282, identifier:custom_fields; 283, keyword_argument; 283, 284; 283, 285; 284, identifier:ttl; 285, identifier:ttl; 286, keyword_argument; 286, 287; 286, 288; 287, identifier:managed_os; 288, identifier:managed_os; 289, keyword_argument; 289, 290; 289, 291; 290, identifier:description; 291, identifier:description; 292, keyword_argument; 292, 293; 292, 294; 293, identifier:source_server_password; 294, identifier:source_server_password; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:cpu_autoscale_policy_id; 297, identifier:cpu_autoscale_policy_id; 298, keyword_argument; 298, 299; 298, 300; 299, identifier:anti_affinity_policy_id; 300, identifier:anti_affinity_policy_id; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:packages; 303, identifier:packages; 304, keyword_argument; 304, 305; 304, 306; 305, identifier:template; 306, attribute; 306, 307; 306, 308; 307, identifier:self; 308, identifier:id; 309, keyword_argument; 309, 310; 309, 311; 310, identifier:session; 311, attribute; 311, 312; 311, 313; 312, identifier:self; 313, identifier:session; 314, return_statement; 314, 315; 315, parenthesized_expression; 315, 316; 316, call; 316, 317; 316, 318; 317, identifier:sum; 318, argument_list; 318, 319; 319, identifier:requests_lst
def Clone(self,network_id,name=None,cpu=None,memory=None,group_id=None,alias=None,password=None,ip_address=None, storage_type=None,type=None,primary_dns=None,secondary_dns=None, custom_fields=None,ttl=None,managed_os=False,description=None, source_server_password=None,cpu_autoscale_policy_id=None,anti_affinity_policy_id=None, packages=[],count=1): """Creates one or more clones of existing server. https://www.centurylinkcloud.com/api-docs/v2/#servers-create-server Set ttl as number of seconds before server is to be terminated. * - network_id is currently a required parameter. This will change to optional once APIs are available to return the network id of self. * - if no password is supplied will reuse the password of self. Is this the expected behavior? Take note there is no way to for a system generated password with this pattern since we cannot leave as None * - any DNS settings from self aren't propogated to clone since they are unknown at system level and the clone process will touch them * - no change to the disk layout we will clone all * - clone will not replicate managed OS setting from self so this must be explicitly set >>> d = clc.v2.Datacenter() >>> clc.v2.Server(alias='BTDI',id='WA1BTDIAPI207').Clone(network_id=d.Networks().networks[0].id,count=2) 0 """ if not name: name = re.search("%s(.+)\d{2}$" % self.alias,self.name).group(1) #if not description and self.description: description = self.description if not cpu: cpu = self.cpu if not memory: memory = self.memory if not group_id: group_id = self.group_id if not alias: alias = self.alias if not source_server_password: source_server_password = self.Credentials()['password'] if not password: password = source_server_password # is this the expected behavior? if not storage_type: storage_type = self.storage_type if not type: type = self.type if not storage_type: storage_type = self.storage_type if not custom_fields and len(self.custom_fields): custom_fields = self.custom_fields if not description: description = self.description # TODO - #if not cpu_autoscale_policy_id: cpu_autoscale_policy_id = # TODO - #if not anti_affinity_policy_id: anti_affinity_policy_id = # TODO - need to get network_id of self, not currently exposed via API :( requests_lst = [] for i in range(0,count): requests_lst.append(Server.Create( \ name=name,cpu=cpu,memory=memory,group_id=group_id,network_id=network_id,alias=self.alias, password=password,ip_address=ip_address,storage_type=storage_type,type=type, primary_dns=primary_dns,secondary_dns=secondary_dns, custom_fields=custom_fields,ttl=ttl,managed_os=managed_os,description=description, source_server_password=source_server_password,cpu_autoscale_policy_id=cpu_autoscale_policy_id, anti_affinity_policy_id=anti_affinity_policy_id,packages=packages, template=self.id,session=self.session)) return(sum(requests_lst))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:Call; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:method; 5, identifier:url; 6, default_parameter; 6, 7; 6, 8; 7, identifier:payload; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:session; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:debug; 14, False; 15, block; 15, 16; 15, 18; 15, 61; 15, 70; 15, 71; 15, 72; 15, 105; 15, 119; 15, 145; 15, 194; 15, 227; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 22; 18, 35; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:session; 21, None; 22, block; 22, 23; 22, 29; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:token; 26, subscript; 26, 27; 26, 28; 27, identifier:session; 28, string:'token'; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:http_session; 32, subscript; 32, 33; 32, 34; 33, identifier:session; 34, string:'http_session'; 35, else_clause; 35, 36; 36, block; 36, 37; 36, 49; 36, 55; 37, if_statement; 37, 38; 37, 42; 38, not_operator; 38, 39; 39, attribute; 39, 40; 39, 41; 40, identifier:clc; 41, identifier:_LOGIN_TOKEN_V2; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:API; 47, identifier:_Login; 48, argument_list; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:token; 52, attribute; 52, 53; 52, 54; 53, identifier:clc; 54, identifier:_LOGIN_TOKEN_V2; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:http_session; 58, attribute; 58, 59; 58, 60; 59, identifier:clc; 60, identifier:_REQUESTS_SESSION; 61, if_statement; 61, 62; 61, 65; 62, comparison_operator:is; 62, 63; 62, 64; 63, identifier:payload; 64, None; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:payload; 69, dictionary; 70, comment; 71, comment; 72, if_statement; 72, 73; 72, 78; 72, 91; 73, comparison_operator:==; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:url; 76, integer:0; 77, string:'/'; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:fq_url; 82, binary_operator:%; 82, 83; 82, 84; 83, string:"%s%s"; 84, tuple; 84, 85; 84, 90; 85, attribute; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:clc; 88, identifier:defaults; 89, identifier:ENDPOINT_URL_V2; 90, identifier:url; 91, else_clause; 91, 92; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:fq_url; 96, binary_operator:%; 96, 97; 96, 98; 97, string:"%s/v2/%s"; 98, tuple; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:clc; 102, identifier:defaults; 103, identifier:ENDPOINT_URL_V2; 104, identifier:url; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 112; 107, attribute; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:http_session; 110, identifier:headers; 111, identifier:update; 112, argument_list; 112, 113; 113, dictionary; 113, 114; 114, pair; 114, 115; 114, 116; 115, string:'Authorization'; 116, binary_operator:%; 116, 117; 116, 118; 117, string:"Bearer %s"; 118, identifier:token; 119, if_statement; 119, 120; 119, 125; 119, 135; 120, call; 120, 121; 120, 122; 121, identifier:isinstance; 122, argument_list; 122, 123; 122, 124; 123, identifier:payload; 124, identifier:basestring; 125, block; 125, 126; 125, 134; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 133; 128, subscript; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:http_session; 131, identifier:headers; 132, string:'content-type'; 133, string:"Application/json"; 134, block:# added for server ops with str payload; 135, else_clause; 135, 136; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 144; 139, subscript; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:http_session; 142, identifier:headers; 143, string:'content-type'; 144, string:"application/x-www-form-urlencoded"; 145, if_statement; 145, 146; 145, 149; 145, 171; 146, comparison_operator:==; 146, 147; 146, 148; 147, identifier:method; 148, string:"GET"; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:r; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:http_session; 156, identifier:request; 157, argument_list; 157, 158; 157, 159; 157, 160; 157, 163; 158, identifier:method; 159, identifier:fq_url; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:params; 162, identifier:payload; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:verify; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:API; 168, identifier:_ResourcePath; 169, argument_list; 169, 170; 170, string:'clc/cacert.pem'; 171, else_clause; 171, 172; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:r; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:http_session; 179, identifier:request; 180, argument_list; 180, 181; 180, 182; 180, 183; 180, 186; 181, identifier:method; 182, identifier:fq_url; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:data; 185, identifier:payload; 186, keyword_argument; 186, 187; 186, 188; 187, identifier:verify; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:API; 191, identifier:_ResourcePath; 192, argument_list; 192, 193; 193, string:'clc/cacert.pem'; 194, if_statement; 194, 195; 194, 196; 195, identifier:debug; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:API; 201, identifier:_DebugRequest; 202, argument_list; 202, 203; 202, 224; 203, keyword_argument; 203, 204; 203, 205; 204, identifier:request; 205, call; 205, 206; 205, 223; 206, attribute; 206, 207; 206, 222; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:requests; 210, identifier:Request; 211, argument_list; 211, 212; 211, 213; 211, 214; 211, 217; 212, identifier:method; 213, identifier:fq_url; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:data; 216, identifier:payload; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:headers; 219, attribute; 219, 220; 219, 221; 220, identifier:http_session; 221, identifier:headers; 222, identifier:prepare; 223, argument_list; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:response; 226, identifier:r; 227, if_statement; 227, 228; 227, 239; 227, 254; 228, boolean_operator:and; 228, 229; 228, 234; 229, comparison_operator:>=; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:r; 232, identifier:status_code; 233, integer:200; 234, comparison_operator:<; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:r; 237, identifier:status_code; 238, integer:300; 239, block; 239, 240; 240, try_statement; 240, 241; 240, 249; 241, block; 241, 242; 242, return_statement; 242, 243; 243, parenthesized_expression; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:r; 247, identifier:json; 248, argument_list; 249, except_clause; 249, 250; 250, block; 250, 251; 251, return_statement; 251, 252; 252, parenthesized_expression; 252, 253; 253, dictionary; 254, else_clause; 254, 255; 255, block; 255, 256; 256, try_statement; 256, 257; 256, 310; 256, 316; 257, block; 257, 258; 257, 281; 257, 289; 257, 299; 257, 307; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:e; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:clc; 264, identifier:APIFailedResponse; 265, argument_list; 265, 266; 266, binary_operator:%; 266, 267; 266, 268; 267, string:"Response code %s. %s %s %s"; 268, tuple; 268, 269; 268, 272; 268, 279; 268, 280; 269, attribute; 269, 270; 269, 271; 270, identifier:r; 271, identifier:status_code; 272, subscript; 272, 273; 272, 278; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:r; 276, identifier:json; 277, argument_list; 278, string:'message'; 279, identifier:method; 280, identifier:fq_url; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:e; 285, identifier:response_status_code; 286, attribute; 286, 287; 286, 288; 287, identifier:r; 288, identifier:status_code; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:e; 293, identifier:response_json; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:r; 297, identifier:json; 298, argument_list; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:e; 303, identifier:response_text; 304, attribute; 304, 305; 304, 306; 305, identifier:r; 306, identifier:text; 307, raise_statement; 307, 308; 308, parenthesized_expression; 308, 309; 309, identifier:e; 310, except_clause; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:clc; 313, identifier:APIFailedResponse; 314, block; 314, 315; 315, raise_statement; 316, except_clause; 316, 317; 317, block; 317, 318; 317, 337; 317, 345; 317, 351; 317, 352; 317, 360; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:e; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:clc; 324, identifier:APIFailedResponse; 325, argument_list; 325, 326; 326, binary_operator:%; 326, 327; 326, 328; 327, string:"Response code %s. %s. %s %s"; 328, tuple; 328, 329; 328, 332; 328, 335; 328, 336; 329, attribute; 329, 330; 329, 331; 330, identifier:r; 331, identifier:status_code; 332, attribute; 332, 333; 332, 334; 333, identifier:r; 334, identifier:text; 335, identifier:method; 336, identifier:fq_url; 337, expression_statement; 337, 338; 338, assignment; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:e; 341, identifier:response_status_code; 342, attribute; 342, 343; 342, 344; 343, identifier:r; 344, identifier:status_code; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:e; 349, identifier:response_json; 350, dictionary; 351, comment; 352, expression_statement; 352, 353; 353, assignment; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:e; 356, identifier:response_text; 357, attribute; 357, 358; 357, 359; 358, identifier:r; 359, identifier:text; 360, raise_statement; 360, 361; 361, parenthesized_expression; 361, 362; 362, identifier:e
def Call(method,url,payload=None,session=None,debug=False): """Execute v2 API call. :param url: URL paths associated with the API call :param payload: dict containing all parameters to submit with POST call :returns: decoded API json result """ if session is not None: token = session['token'] http_session = session['http_session'] else: if not clc._LOGIN_TOKEN_V2: API._Login() token = clc._LOGIN_TOKEN_V2 http_session = clc._REQUESTS_SESSION if payload is None: payload = {} # If executing refs provided in API they are abs paths, # Else refs we build in the sdk are relative if url[0]=='/': fq_url = "%s%s" % (clc.defaults.ENDPOINT_URL_V2,url) else: fq_url = "%s/v2/%s" % (clc.defaults.ENDPOINT_URL_V2,url) http_session.headers.update({'Authorization': "Bearer %s" % token}) if isinstance(payload, basestring): http_session.headers['content-type'] = "Application/json" # added for server ops with str payload else: http_session.headers['content-type'] = "application/x-www-form-urlencoded" if method=="GET": r = http_session.request(method,fq_url, params=payload, verify=API._ResourcePath('clc/cacert.pem')) else: r = http_session.request(method,fq_url, data=payload, verify=API._ResourcePath('clc/cacert.pem')) if debug: API._DebugRequest(request=requests.Request(method,fq_url,data=payload,headers=http_session.headers).prepare(), response=r) if r.status_code>=200 and r.status_code<300: try: return(r.json()) except: return({}) else: try: e = clc.APIFailedResponse("Response code %s. %s %s %s" % (r.status_code,r.json()['message'],method,fq_url)) e.response_status_code = r.status_code e.response_json = r.json() e.response_text = r.text raise(e) except clc.APIFailedResponse: raise except: e = clc.APIFailedResponse("Response code %s. %s. %s %s" % (r.status_code,r.text,method,fq_url)) e.response_status_code = r.status_code e.response_json = {} # or should this be None? e.response_text = r.text raise(e)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:processFiles; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 12; 5, 348; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:to_process; 11, list:[]; 12, for_statement; 12, 13; 12, 14; 12, 17; 13, identifier:filename; 14, subscript; 14, 15; 14, 16; 15, identifier:args; 16, string:'filenames'; 17, block; 17, 18; 17, 24; 17, 58; 17, 67; 17, 92; 17, 111; 17, 130; 17, 143; 17, 149; 17, 205; 17, 224; 17, 230; 17, 335; 17, 341; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:file; 21, call; 21, 22; 21, 23; 22, identifier:dict; 23, argument_list; 24, if_statement; 24, 25; 24, 28; 24, 50; 25, subscript; 25, 26; 25, 27; 26, identifier:args; 27, string:'include'; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, subscript; 31, 32; 31, 33; 32, identifier:file; 33, string:'include'; 34, binary_operator:+; 34, 35; 34, 36; 35, identifier:INCLUDE_STRING; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, string:''; 39, identifier:join; 40, argument_list; 40, 41; 41, list_comprehension; 41, 42; 41, 45; 42, binary_operator:+; 42, 43; 42, 44; 43, string:'-I'; 44, identifier:item; 45, for_in_clause; 45, 46; 45, 47; 46, identifier:item; 47, subscript; 47, 48; 47, 49; 48, identifier:args; 49, string:'include'; 50, else_clause; 50, 51; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:file; 56, string:'include'; 57, identifier:INCLUDE_STRING; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 63; 60, subscript; 60, 61; 60, 62; 61, identifier:file; 62, string:'file_path'; 63, call; 63, 64; 63, 65; 64, identifier:getPath; 65, argument_list; 65, 66; 66, identifier:filename; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 72; 68, 73; 69, subscript; 69, 70; 69, 71; 70, identifier:file; 71, string:'file_base_name'; 72, line_continuation:\; 73, subscript; 73, 74; 73, 91; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:os; 78, identifier:path; 79, identifier:splitext; 80, argument_list; 80, 81; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:os; 85, identifier:path; 86, identifier:basename; 87, argument_list; 87, 88; 88, subscript; 88, 89; 88, 90; 89, identifier:file; 90, string:'file_path'; 91, integer:0; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 101; 94, pattern_list; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:file; 97, string:'no_extension'; 98, subscript; 98, 99; 98, 100; 99, identifier:file; 100, string:'extension'; 101, call; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:os; 105, identifier:path; 106, identifier:splitext; 107, argument_list; 107, 108; 108, subscript; 108, 109; 108, 110; 109, identifier:file; 110, string:'file_path'; 111, if_statement; 111, 112; 111, 117; 112, comparison_operator:not; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:file; 115, string:'extension'; 116, identifier:CYTHONIZABLE_FILE_EXTS; 117, block; 117, 118; 118, raise_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:CytherError; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, string:"The file '{}' is not a designated cython file"; 125, identifier:format; 126, argument_list; 126, 127; 127, subscript; 127, 128; 127, 129; 128, identifier:file; 129, string:'file_path'; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:base_path; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:os; 137, identifier:path; 138, identifier:dirname; 139, argument_list; 139, 140; 140, subscript; 140, 141; 140, 142; 141, identifier:file; 142, string:'file_path'; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:local_build; 146, subscript; 146, 147; 146, 148; 147, identifier:args; 148, string:'local'; 149, if_statement; 149, 150; 149, 152; 149, 193; 150, not_operator; 150, 151; 151, identifier:local_build; 152, block; 152, 153; 152, 165; 152, 175; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:cache_name; 156, call; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:os; 160, identifier:path; 161, identifier:join; 162, argument_list; 162, 163; 162, 164; 163, identifier:base_path; 164, string:'__cythercache__'; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:os; 169, identifier:makedirs; 170, argument_list; 170, 171; 170, 172; 171, identifier:cache_name; 172, keyword_argument; 172, 173; 172, 174; 173, identifier:exist_ok; 174, True; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:file; 179, string:'c_name'; 180, binary_operator:+; 180, 181; 180, 192; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:os; 185, identifier:path; 186, identifier:join; 187, argument_list; 187, 188; 187, 189; 188, identifier:cache_name; 189, subscript; 189, 190; 189, 191; 190, identifier:file; 191, string:'file_base_name'; 192, string:'.c'; 193, else_clause; 193, 194; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 200; 197, subscript; 197, 198; 197, 199; 198, identifier:file; 199, string:'c_name'; 200, binary_operator:+; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:file; 203, string:'no_extension'; 204, string:'.c'; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 210; 207, subscript; 207, 208; 207, 209; 208, identifier:file; 209, string:'object_file_name'; 210, binary_operator:+; 210, 211; 210, 223; 211, subscript; 211, 212; 211, 222; 212, call; 212, 213; 212, 218; 213, attribute; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:os; 216, identifier:path; 217, identifier:splitext; 218, argument_list; 218, 219; 219, subscript; 219, 220; 219, 221; 220, identifier:file; 221, string:'c_name'; 222, integer:0; 223, string:'.o'; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:output_name; 227, subscript; 227, 228; 227, 229; 228, identifier:args; 229, string:'output_name'; 230, if_statement; 230, 231; 230, 234; 230, 245; 230, 323; 231, subscript; 231, 232; 231, 233; 232, identifier:args; 233, string:'watch'; 234, block; 234, 235; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:file; 239, string:'output_name'; 240, binary_operator:+; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:file; 243, string:'no_extension'; 244, identifier:DEFAULT_OUTPUT_EXTENSION; 245, elif_clause; 245, 246; 245, 247; 246, identifier:output_name; 247, block; 247, 248; 248, if_statement; 248, 249; 248, 266; 248, 273; 249, boolean_operator:and; 249, 250; 249, 258; 250, call; 250, 251; 250, 256; 251, attribute; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:os; 254, identifier:path; 255, identifier:exists; 256, argument_list; 256, 257; 257, identifier:output_name; 258, call; 258, 259; 258, 264; 259, attribute; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:os; 262, identifier:path; 263, identifier:isfile; 264, argument_list; 264, 265; 265, identifier:output_name; 266, block; 266, 267; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 272; 269, subscript; 269, 270; 269, 271; 270, identifier:file; 271, string:'output_name'; 272, identifier:output_name; 273, else_clause; 273, 274; 274, block; 274, 275; 274, 286; 274, 298; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:dirname; 278, call; 278, 279; 278, 284; 279, attribute; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:os; 282, identifier:path; 283, identifier:dirname; 284, argument_list; 284, 285; 285, identifier:output_name; 286, if_statement; 286, 287; 286, 289; 287, not_operator; 287, 288; 288, identifier:dirname; 289, block; 289, 290; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:dirname; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:os; 296, identifier:getcwd; 297, argument_list; 298, if_statement; 298, 299; 298, 307; 298, 314; 299, call; 299, 300; 299, 305; 300, attribute; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:os; 303, identifier:path; 304, identifier:exists; 305, argument_list; 305, 306; 306, identifier:dirname; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 313; 310, subscript; 310, 311; 310, 312; 311, identifier:file; 312, string:'output_name'; 313, identifier:output_name; 314, else_clause; 314, 315; 315, block; 315, 316; 316, raise_statement; 316, 317; 317, call; 317, 318; 317, 319; 318, identifier:CytherError; 319, argument_list; 319, 320; 320, concatenated_string; 320, 321; 320, 322; 321, string:'The directory specified to write'; 322, string:'the output file in does not exist'; 323, else_clause; 323, 324; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 330; 327, subscript; 327, 328; 327, 329; 328, identifier:file; 329, string:'output_name'; 330, binary_operator:+; 330, 331; 330, 334; 331, subscript; 331, 332; 331, 333; 332, identifier:file; 333, string:'no_extension'; 334, identifier:DEFAULT_OUTPUT_EXTENSION; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 340; 337, subscript; 337, 338; 337, 339; 338, identifier:file; 339, string:'stamp_if_error'; 340, integer:0; 341, expression_statement; 341, 342; 342, call; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:to_process; 345, identifier:append; 346, argument_list; 346, 347; 347, identifier:file; 348, return_statement; 348, 349; 349, identifier:to_process
def processFiles(args): """ Generates and error checks each file's information before the compilation actually starts """ to_process = [] for filename in args['filenames']: file = dict() if args['include']: file['include'] = INCLUDE_STRING + ''.join( ['-I' + item for item in args['include']]) else: file['include'] = INCLUDE_STRING file['file_path'] = getPath(filename) file['file_base_name'] = \ os.path.splitext(os.path.basename(file['file_path']))[0] file['no_extension'], file['extension'] = os.path.splitext( file['file_path']) if file['extension'] not in CYTHONIZABLE_FILE_EXTS: raise CytherError( "The file '{}' is not a designated cython file".format( file['file_path'])) base_path = os.path.dirname(file['file_path']) local_build = args['local'] if not local_build: cache_name = os.path.join(base_path, '__cythercache__') os.makedirs(cache_name, exist_ok=True) file['c_name'] = os.path.join(cache_name, file['file_base_name']) + '.c' else: file['c_name'] = file['no_extension'] + '.c' file['object_file_name'] = os.path.splitext(file['c_name'])[0] + '.o' output_name = args['output_name'] if args['watch']: file['output_name'] = file['no_extension']+DEFAULT_OUTPUT_EXTENSION elif output_name: if os.path.exists(output_name) and os.path.isfile(output_name): file['output_name'] = output_name else: dirname = os.path.dirname(output_name) if not dirname: dirname = os.getcwd() if os.path.exists(dirname): file['output_name'] = output_name else: raise CytherError('The directory specified to write' 'the output file in does not exist') else: file['output_name'] = file['no_extension']+DEFAULT_OUTPUT_EXTENSION file['stamp_if_error'] = 0 to_process.append(file) return to_process
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:main; 3, parameters; 4, block; 4, 5; 4, 7; 4, 21; 4, 34; 4, 48; 4, 70; 4, 97; 4, 125; 4, 147; 4, 170; 4, 178; 4, 219; 4, 235; 4, 259; 4, 272; 4, 273; 4, 274; 4, 275; 4, 276; 4, 277; 4, 278; 4, 279; 4, 280; 4, 281; 4, 282; 4, 283; 4, 284; 4, 285; 4, 296; 4, 297; 4, 319; 5, expression_statement; 5, 6; 6, comment; 7, expression_statement; 7, 8; 8, assignment; 8, 9; 8, 10; 9, identifier:parser; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:argparse; 13, identifier:ArgumentParser; 14, argument_list; 14, 15; 14, 18; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:description; 17, string:'DistanceClassifier for classification based on distance measure in feature space.'; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:add_help; 20, False; 21, expression_statement; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:parser; 25, identifier:add_argument; 26, argument_list; 26, 27; 26, 28; 26, 31; 27, string:'INPUT_FILE'; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:type; 30, identifier:str; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:help; 33, string:'Data file to perform DistanceClassifier on; ensure that the class label column is labeled as "class".'; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:parser; 38, identifier:add_argument; 39, argument_list; 39, 40; 39, 41; 39, 42; 39, 45; 40, string:'-h'; 41, string:'--help'; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:action; 44, string:'help'; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:help; 47, string:'Show this help message and exit.'; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:parser; 52, identifier:add_argument; 53, argument_list; 53, 54; 53, 55; 53, 58; 53, 61; 53, 64; 53, 67; 54, string:'-is'; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:action; 57, string:'store'; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:dest; 60, string:'INPUT_SEPARATOR'; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:default; 63, string:'\t'; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:type; 66, identifier:str; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:help; 69, string:'Character used to separate columns in the input file.'; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:parser; 74, identifier:add_argument; 75, argument_list; 75, 76; 75, 77; 75, 80; 75, 83; 75, 86; 75, 91; 75, 94; 76, string:'-d'; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:action; 79, string:'store'; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:dest; 82, string:'D'; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:default; 85, string:'mahalanobis'; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:choices; 88, list:['mahalanobis','euclidean']; 88, 89; 88, 90; 89, string:'mahalanobis'; 90, string:'euclidean'; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:type; 93, identifier:str; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:help; 96, string:'Distance metric to use.'; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:parser; 101, identifier:add_argument; 102, argument_list; 102, 103; 102, 104; 102, 107; 102, 110; 102, 113; 102, 119; 102, 122; 103, string:'-v'; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:action; 106, string:'store'; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:dest; 109, string:'VERBOSITY'; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:default; 112, integer:1; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:choices; 115, list:[0, 1, 2]; 115, 116; 115, 117; 115, 118; 116, integer:0; 117, integer:1; 118, integer:2; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:type; 121, identifier:int; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:help; 124, string:'How much information DistanceClassifier communicates while it is running: 0 = none, 1 = minimal, 2 = all.'; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:parser; 129, identifier:add_argument; 130, argument_list; 130, 131; 130, 132; 130, 135; 130, 138; 130, 141; 130, 144; 131, string:'-s'; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:action; 134, string:'store'; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:dest; 137, string:'RANDOM_STATE'; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:default; 140, integer:0; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:type; 143, identifier:int; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:help; 146, string:'Random state for train/test split.'; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:parser; 151, identifier:add_argument; 152, argument_list; 152, 153; 152, 154; 152, 157; 152, 167; 153, string:'--version'; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:action; 156, string:'version'; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:version; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, string:'DistanceClassifier {version}'; 162, identifier:format; 163, argument_list; 163, 164; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:version; 166, identifier:__version__; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:help; 169, string:'Show DistanceClassifier\'s version number and exit.'; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:args; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:parser; 176, identifier:parse_args; 177, argument_list; 178, if_statement; 178, 179; 178, 184; 179, comparison_operator:>=; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:args; 182, identifier:VERBOSITY; 183, integer:2; 184, block; 184, 185; 184, 190; 184, 214; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:print; 188, argument_list; 188, 189; 189, string:'\nDistanceClassifier settings:'; 190, for_statement; 190, 191; 190, 192; 190, 198; 191, identifier:arg; 192, call; 192, 193; 192, 194; 193, identifier:sorted; 194, argument_list; 194, 195; 195, attribute; 195, 196; 195, 197; 196, identifier:args; 197, identifier:__dict__; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:print; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, string:'{}\t=\t{}'; 206, identifier:format; 207, argument_list; 207, 208; 207, 209; 208, identifier:arg; 209, subscript; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:args; 212, identifier:__dict__; 213, identifier:arg; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:print; 217, argument_list; 217, 218; 218, string:''; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:input_data; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:pd; 225, identifier:read_csv; 226, argument_list; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:args; 229, identifier:INPUT_FILE; 230, keyword_argument; 230, 231; 230, 232; 231, identifier:sep; 232, attribute; 232, 233; 232, 234; 233, identifier:args; 234, identifier:INPUT_SEPARATOR; 235, if_statement; 235, 236; 235, 243; 236, comparison_operator:in; 236, 237; 236, 238; 237, string:'Class'; 238, attribute; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:input_data; 241, identifier:columns; 242, identifier:values; 243, block; 243, 244; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:input_data; 248, identifier:rename; 249, argument_list; 249, 250; 249, 256; 250, keyword_argument; 250, 251; 250, 252; 251, identifier:columns; 252, dictionary; 252, 253; 253, pair; 253, 254; 253, 255; 254, string:'Label'; 255, string:'label'; 256, keyword_argument; 256, 257; 256, 258; 257, identifier:inplace; 258, True; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:RANDOM_STATE; 262, conditional_expression:if; 262, 263; 262, 266; 262, 271; 263, attribute; 263, 264; 263, 265; 264, identifier:args; 265, identifier:RANDOM_STATE; 266, comparison_operator:>; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:args; 269, identifier:RANDOM_STATE; 270, integer:0; 271, None; 272, comment; 273, comment; 274, comment; 275, comment; 276, comment; 277, comment; 278, comment; 279, comment; 280, comment; 281, comment; 282, comment; 283, comment; 284, comment; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:dc; 288, call; 288, 289; 288, 290; 289, identifier:DistanceClassifier; 290, argument_list; 290, 291; 291, keyword_argument; 291, 292; 291, 293; 292, identifier:d; 293, attribute; 293, 294; 293, 295; 294, identifier:args; 295, identifier:D; 296, comment; 297, expression_statement; 297, 298; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:dc; 301, identifier:fit; 302, argument_list; 302, 303; 302, 314; 303, attribute; 303, 304; 303, 313; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:input_data; 307, identifier:drop; 308, argument_list; 308, 309; 308, 310; 309, string:'label'; 310, keyword_argument; 310, 311; 310, 312; 311, identifier:axis; 312, integer:1; 313, identifier:values; 314, attribute; 314, 315; 314, 318; 315, subscript; 315, 316; 315, 317; 316, identifier:input_data; 317, string:'label'; 318, identifier:values; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 322; 321, identifier:print; 322, argument_list; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:dc; 326, identifier:score; 327, argument_list; 327, 328; 327, 339; 328, attribute; 328, 329; 328, 338; 329, call; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:input_data; 332, identifier:drop; 333, argument_list; 333, 334; 333, 335; 334, string:'label'; 335, keyword_argument; 335, 336; 335, 337; 336, identifier:axis; 337, integer:1; 338, identifier:values; 339, attribute; 339, 340; 339, 343; 340, subscript; 340, 341; 340, 342; 341, identifier:input_data; 342, string:'label'; 343, identifier:values
def main(): """Main function that is called when DistanceClassifier is run on the command line""" parser = argparse.ArgumentParser(description='DistanceClassifier for classification based on distance measure in feature space.', add_help=False) parser.add_argument('INPUT_FILE', type=str, help='Data file to perform DistanceClassifier on; ensure that the class label column is labeled as "class".') parser.add_argument('-h', '--help', action='help', help='Show this help message and exit.') parser.add_argument('-is', action='store', dest='INPUT_SEPARATOR', default='\t', type=str, help='Character used to separate columns in the input file.') parser.add_argument('-d', action='store', dest='D', default='mahalanobis',choices = ['mahalanobis','euclidean'], type=str, help='Distance metric to use.') parser.add_argument('-v', action='store', dest='VERBOSITY', default=1, choices=[0, 1, 2], type=int, help='How much information DistanceClassifier communicates while it is running: 0 = none, 1 = minimal, 2 = all.') parser.add_argument('-s', action='store', dest='RANDOM_STATE', default=0, type=int, help='Random state for train/test split.') parser.add_argument('--version', action='version', version='DistanceClassifier {version}'.format(version=__version__), help='Show DistanceClassifier\'s version number and exit.') args = parser.parse_args() if args.VERBOSITY >= 2: print('\nDistanceClassifier settings:') for arg in sorted(args.__dict__): print('{}\t=\t{}'.format(arg, args.__dict__[arg])) print('') input_data = pd.read_csv(args.INPUT_FILE, sep=args.INPUT_SEPARATOR) if 'Class' in input_data.columns.values: input_data.rename(columns={'Label': 'label'}, inplace=True) RANDOM_STATE = args.RANDOM_STATE if args.RANDOM_STATE > 0 else None # # training_indices, testing_indices = train_test_split(input_data.index, # stratify=input_data['label'].values, # train_size=0.75, # test_size=0.25, # random_state=RANDOM_STATE) # # training_features = input_data.loc[training_indices].drop('label', axis=1).values # training_classes = input_data.loc[training_indices, 'label'].values # # testing_features = input_data.loc[testing_indices].drop('label', axis=1).values # testing_classes = input_data.loc[testing_indices, 'label'].values # Run and evaluate DistanceClassifier on the training and testing data dc = DistanceClassifier(d = args.D) # dc.fit(training_features, training_classes) dc.fit(input_data.drop('label',axis=1).values, input_data['label'].values) print(dc.score(input_data.drop('label',axis=1).values, input_data['label'].values))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:delete; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:name; 6, identifier:version; 7, default_parameter; 7, 8; 7, 9; 8, identifier:_lock; 9, True; 10, block; 10, 11; 10, 13; 10, 22; 10, 46; 10, 314; 10, 322; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:link_path; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_link_path; 20, argument_list; 20, 21; 21, identifier:name; 22, if_statement; 22, 23; 22, 24; 22, 38; 23, identifier:_lock; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:file_lock; 28, call; 28, 29; 28, 30; 29, identifier:_exclusive_lock; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_lock_path; 35, argument_list; 35, 36; 35, 37; 36, string:'links'; 37, identifier:name; 38, else_clause; 38, 39; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:file_lock; 43, call; 43, 44; 43, 45; 44, identifier:_no_lock; 45, argument_list; 46, with_statement; 46, 47; 46, 50; 47, with_clause; 47, 48; 48, with_item; 48, 49; 49, identifier:file_lock; 50, block; 50, 51; 50, 59; 50, 68; 50, 91; 50, 100; 50, 306; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:logger; 55, identifier:debug; 56, argument_list; 56, 57; 56, 58; 57, string:'Acquired or inherited lock for link %s.'; 58, identifier:name; 59, if_statement; 59, 60; 59, 65; 60, not_operator; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:_path_exists; 63, argument_list; 63, 64; 64, identifier:link_path; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, identifier:FiletrackerFileNotFoundError; 68, if_statement; 68, 69; 68, 75; 69, comparison_operator:>; 69, 70; 69, 74; 70, call; 70, 71; 70, 72; 71, identifier:_file_version; 72, argument_list; 72, 73; 73, identifier:link_path; 74, identifier:version; 75, block; 75, 76; 75, 89; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:logger; 80, identifier:info; 81, argument_list; 81, 82; 81, 83; 81, 84; 81, 85; 82, string:'Tried to delete newer version of %s (%d < %d), ignoring.'; 83, identifier:name; 84, identifier:version; 85, call; 85, 86; 85, 87; 86, identifier:_file_version; 87, argument_list; 87, 88; 88, identifier:link_path; 89, return_statement; 89, 90; 90, False; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:digest; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_digest_for_link; 98, argument_list; 98, 99; 99, identifier:name; 100, with_statement; 100, 101; 100, 113; 101, with_clause; 101, 102; 102, with_item; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:_exclusive_lock; 105, argument_list; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:_lock_path; 110, argument_list; 110, 111; 110, 112; 111, string:'blobs'; 112, identifier:digest; 113, block; 113, 114; 113, 122; 113, 126; 113, 269; 113, 276; 113, 283; 113, 291; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:logger; 118, identifier:debug; 119, argument_list; 119, 120; 119, 121; 120, string:'Acquired lock for blob %s.'; 121, identifier:digest; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:should_delete_blob; 125, False; 126, with_statement; 126, 127; 126, 137; 127, with_clause; 127, 128; 128, with_item; 128, 129; 129, as_pattern; 129, 130; 129, 135; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:_db_transaction; 134, argument_list; 135, as_pattern_target; 135, 136; 136, identifier:txn; 137, block; 137, 138; 137, 145; 137, 153; 137, 167; 137, 177; 137, 184; 137, 262; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:logger; 142, identifier:debug; 143, argument_list; 143, 144; 144, string:'Started DB transaction (deleting link).'; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:digest_bytes; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:digest; 151, identifier:encode; 152, argument_list; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:link_count; 156, call; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:db; 161, identifier:get; 162, argument_list; 162, 163; 162, 164; 163, identifier:digest_bytes; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:txn; 166, identifier:txn; 167, if_statement; 167, 168; 167, 171; 168, comparison_operator:is; 168, 169; 168, 170; 169, identifier:link_count; 170, None; 171, block; 171, 172; 172, raise_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:RuntimeError; 175, argument_list; 175, 176; 176, string:"File exists but has no key in db"; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:link_count; 180, call; 180, 181; 180, 182; 181, identifier:int; 182, argument_list; 182, 183; 183, identifier:link_count; 184, if_statement; 184, 185; 184, 188; 184, 234; 185, comparison_operator:==; 185, 186; 185, 187; 186, identifier:link_count; 187, integer:1; 188, block; 188, 189; 188, 197; 188, 209; 188, 230; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:logger; 193, identifier:debug; 194, argument_list; 194, 195; 194, 196; 195, string:'Deleting last link to blob %s.'; 196, identifier:digest; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 204; 199, attribute; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:db; 203, identifier:delete; 204, argument_list; 204, 205; 204, 206; 205, identifier:digest_bytes; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:txn; 208, identifier:txn; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 216; 211, attribute; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:db; 215, identifier:delete; 216, argument_list; 216, 217; 216, 227; 217, call; 217, 218; 217, 226; 218, attribute; 218, 219; 218, 225; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, string:'{}:logical_size'; 222, identifier:format; 223, argument_list; 223, 224; 224, identifier:digest; 225, identifier:encode; 226, argument_list; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:txn; 229, identifier:txn; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:should_delete_blob; 233, True; 234, else_clause; 234, 235; 235, block; 235, 236; 235, 249; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:new_count; 239, call; 239, 240; 239, 248; 240, attribute; 240, 241; 240, 247; 241, call; 241, 242; 241, 243; 242, identifier:str; 243, argument_list; 243, 244; 244, binary_operator:-; 244, 245; 244, 246; 245, identifier:link_count; 246, integer:1; 247, identifier:encode; 248, argument_list; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 256; 251, attribute; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:db; 255, identifier:put; 256, argument_list; 256, 257; 256, 258; 256, 259; 257, identifier:digest_bytes; 258, identifier:new_count; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:txn; 261, identifier:txn; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:logger; 266, identifier:debug; 267, argument_list; 267, 268; 268, string:'Committing DB transaction (deleting link).'; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:logger; 273, identifier:debug; 274, argument_list; 274, 275; 275, string:'Committed DB transaction (deleting link).'; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:os; 280, identifier:unlink; 281, argument_list; 281, 282; 282, identifier:link_path; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:logger; 287, identifier:debug; 288, argument_list; 288, 289; 288, 290; 289, string:'Deleted link %s.'; 290, identifier:name; 291, if_statement; 291, 292; 291, 293; 292, identifier:should_delete_blob; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:os; 298, identifier:unlink; 299, argument_list; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:self; 303, identifier:_blob_path; 304, argument_list; 304, 305; 305, identifier:digest; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:logger; 310, identifier:debug; 311, argument_list; 311, 312; 311, 313; 312, string:'Released lock for blob %s.'; 313, identifier:digest; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:logger; 318, identifier:debug; 319, argument_list; 319, 320; 319, 321; 320, string:'Released (or gave back) lock for link %s.'; 321, identifier:name; 322, return_statement; 322, 323; 323, True
def delete(self, name, version, _lock=True): """Removes a file from the storage. Args: name: name of the file being deleted. May contain slashes that are treated as path separators. version: file "version" that is meant to be deleted If the file that is stored has newer version than provided, it will not be deleted. lock: whether or not to acquire locks This is for internal use only, normal users should always leave it set to True. Returns whether or not the file has been deleted. """ link_path = self._link_path(name) if _lock: file_lock = _exclusive_lock(self._lock_path('links', name)) else: file_lock = _no_lock() with file_lock: logger.debug('Acquired or inherited lock for link %s.', name) if not _path_exists(link_path): raise FiletrackerFileNotFoundError if _file_version(link_path) > version: logger.info( 'Tried to delete newer version of %s (%d < %d), ignoring.', name, version, _file_version(link_path)) return False digest = self._digest_for_link(name) with _exclusive_lock(self._lock_path('blobs', digest)): logger.debug('Acquired lock for blob %s.', digest) should_delete_blob = False with self._db_transaction() as txn: logger.debug('Started DB transaction (deleting link).') digest_bytes = digest.encode() link_count = self.db.get(digest_bytes, txn=txn) if link_count is None: raise RuntimeError("File exists but has no key in db") link_count = int(link_count) if link_count == 1: logger.debug('Deleting last link to blob %s.', digest) self.db.delete(digest_bytes, txn=txn) self.db.delete( '{}:logical_size'.format(digest).encode(), txn=txn) should_delete_blob = True else: new_count = str(link_count - 1).encode() self.db.put(digest_bytes, new_count, txn=txn) logger.debug('Committing DB transaction (deleting link).') logger.debug('Committed DB transaction (deleting link).') os.unlink(link_path) logger.debug('Deleted link %s.', name) if should_delete_blob: os.unlink(self._blob_path(digest)) logger.debug('Released lock for blob %s.', digest) logger.debug('Released (or gave back) lock for link %s.', name) return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:wait_socks; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:sock_events; 5, default_parameter; 5, 6; 5, 7; 6, identifier:inmask; 7, integer:1; 8, default_parameter; 8, 9; 8, 10; 9, identifier:outmask; 10, integer:2; 11, default_parameter; 11, 12; 11, 13; 12, identifier:timeout; 13, None; 14, block; 14, 15; 14, 17; 14, 21; 14, 60; 14, 65; 14, 69; 14, 73; 14, 137; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:results; 20, list:[]; 21, for_statement; 21, 22; 21, 25; 21, 26; 22, pattern_list; 22, 23; 22, 24; 23, identifier:sock; 24, identifier:mask; 25, identifier:sock_events; 26, block; 26, 27; 27, if_statement; 27, 28; 27, 37; 28, call; 28, 29; 28, 30; 29, identifier:isinstance; 30, argument_list; 30, 31; 30, 32; 31, identifier:sock; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:zmq; 35, identifier:backend; 36, identifier:Socket; 37, block; 37, 38; 37, 48; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:mask; 41, call; 41, 42; 41, 43; 42, identifier:_check_events; 43, argument_list; 43, 44; 43, 45; 43, 46; 43, 47; 44, identifier:sock; 45, identifier:mask; 46, identifier:inmask; 47, identifier:outmask; 48, if_statement; 48, 49; 48, 50; 49, identifier:mask; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:results; 55, identifier:append; 56, argument_list; 56, 57; 57, tuple; 57, 58; 57, 59; 58, identifier:sock; 59, identifier:mask; 60, if_statement; 60, 61; 60, 62; 61, identifier:results; 62, block; 62, 63; 63, return_statement; 63, 64; 64, identifier:results; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:fd_map; 68, dictionary; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:fd_events; 72, list:[]; 73, for_statement; 73, 74; 73, 77; 73, 78; 74, pattern_list; 74, 75; 74, 76; 75, identifier:sock; 76, identifier:mask; 77, identifier:sock_events; 78, block; 78, 79; 78, 122; 78, 128; 79, if_statement; 79, 80; 79, 89; 79, 101; 79, 112; 80, call; 80, 81; 80, 82; 81, identifier:isinstance; 82, argument_list; 82, 83; 82, 84; 83, identifier:sock; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:zmq; 87, identifier:backend; 88, identifier:Socket; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:fd; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:sock; 96, identifier:getsockopt; 97, argument_list; 97, 98; 98, attribute; 98, 99; 98, 100; 99, identifier:zmq; 100, identifier:FD; 101, elif_clause; 101, 102; 101, 107; 102, call; 102, 103; 102, 104; 103, identifier:isinstance; 104, argument_list; 104, 105; 104, 106; 105, identifier:sock; 106, identifier:int; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:fd; 111, identifier:sock; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:fd; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:sock; 120, identifier:fileno; 121, argument_list; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:fd_map; 126, identifier:fd; 127, identifier:sock; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:fd_events; 132, identifier:append; 133, argument_list; 133, 134; 134, tuple; 134, 135; 134, 136; 135, identifier:fd; 136, identifier:mask; 137, while_statement; 137, 138; 137, 139; 138, integer:1; 139, block; 139, 140; 139, 148; 139, 160; 139, 167; 139, 171; 139, 218; 139, 223; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:started; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:time; 146, identifier:time; 147, argument_list; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:active; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:descriptor; 154, identifier:wait_fds; 155, argument_list; 155, 156; 155, 157; 155, 158; 155, 159; 156, identifier:fd_events; 157, identifier:inmask; 158, identifier:outmask; 159, identifier:timeout; 160, if_statement; 160, 161; 160, 163; 160, 164; 161, not_operator; 161, 162; 162, identifier:active; 163, comment; 164, block; 164, 165; 165, return_statement; 165, 166; 166, list:[]; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:results; 170, list:[]; 171, for_statement; 171, 172; 171, 175; 171, 176; 172, pattern_list; 172, 173; 172, 174; 173, identifier:fd; 174, identifier:mask; 175, identifier:active; 176, block; 176, 177; 176, 183; 176, 209; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:sock; 180, subscript; 180, 181; 180, 182; 181, identifier:fd_map; 182, identifier:fd; 183, if_statement; 183, 184; 183, 193; 184, call; 184, 185; 184, 186; 185, identifier:isinstance; 186, argument_list; 186, 187; 186, 188; 187, identifier:sock; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:zmq; 191, identifier:backend; 192, identifier:Socket; 193, block; 193, 194; 193, 204; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:mask; 197, call; 197, 198; 197, 199; 198, identifier:_check_events; 199, argument_list; 199, 200; 199, 201; 199, 202; 199, 203; 200, identifier:sock; 201, identifier:mask; 202, identifier:inmask; 203, identifier:outmask; 204, if_statement; 204, 205; 204, 207; 205, not_operator; 205, 206; 206, identifier:mask; 207, block; 207, 208; 208, continue_statement; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:results; 213, identifier:append; 214, argument_list; 214, 215; 215, tuple; 215, 216; 215, 217; 216, identifier:sock; 217, identifier:mask; 218, if_statement; 218, 219; 218, 220; 219, identifier:results; 220, block; 220, 221; 221, return_statement; 221, 222; 222, identifier:results; 223, expression_statement; 223, 224; 224, augmented_assignment:-=; 224, 225; 224, 226; 225, identifier:timeout; 226, binary_operator:-; 226, 227; 226, 232; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:time; 230, identifier:time; 231, argument_list; 232, identifier:started
def wait_socks(sock_events, inmask=1, outmask=2, timeout=None): """wait on a combination of zeromq sockets, normal sockets, and fds .. note:: this method can block it will return once there is relevant activity on any of the descriptors or sockets, or the timeout expires :param sock_events: two-tuples, the first item is either a zeromq socket, a socket, or a file descriptor, and the second item is a mask made up of the inmask and/or the outmask bitwise-ORd together :type sock_events: list :param inmask: the mask to use for readable events (default 1) :type inmask: int :param outmask: the mask to use for writable events (default 2) :type outmask: int :param timeout: the maximum time to block before raising an exception :type timeout: int, float or None :returns: a list of two-tuples, each has one of the first elements from ``sock_events``, the second element is the event mask of the activity that was detected (made up on inmask and/or outmask bitwise-ORd together) """ results = [] for sock, mask in sock_events: if isinstance(sock, zmq.backend.Socket): mask = _check_events(sock, mask, inmask, outmask) if mask: results.append((sock, mask)) if results: return results fd_map = {} fd_events = [] for sock, mask in sock_events: if isinstance(sock, zmq.backend.Socket): fd = sock.getsockopt(zmq.FD) elif isinstance(sock, int): fd = sock else: fd = sock.fileno() fd_map[fd] = sock fd_events.append((fd, mask)) while 1: started = time.time() active = descriptor.wait_fds(fd_events, inmask, outmask, timeout) if not active: # timed out return [] results = [] for fd, mask in active: sock = fd_map[fd] if isinstance(sock, zmq.backend.Socket): mask = _check_events(sock, mask, inmask, outmask) if not mask: continue results.append((sock, mask)) if results: return results timeout -= time.time() - started
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:sync_ldap_user_membership; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:user; 6, identifier:ldap_groups; 7, block; 7, 8; 7, 10; 7, 14; 7, 28; 7, 35; 7, 39; 7, 43; 7, 47; 7, 53; 7, 57; 7, 250; 7, 251; 7, 252; 7, 288; 7, 320; 7, 321; 7, 327; 7, 333; 7, 339; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:groupname_field; 13, string:'name'; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:actualGroups; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:user; 21, identifier:groups; 22, identifier:values_list; 23, argument_list; 23, 24; 23, 25; 24, string:'name'; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:flat; 27, True; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:user_Membership_total; 31, call; 31, 32; 31, 33; 32, identifier:len; 33, argument_list; 33, 34; 34, identifier:ldap_groups; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:user_Membership_added; 38, integer:0; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:user_Membership_deleted; 42, integer:0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:user_Membership_errors; 46, integer:0; 47, expression_statement; 47, 48; 48, augmented_assignment:+=; 48, 49; 48, 50; 49, identifier:ldap_groups; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:conf_LDAP_SYNC_GROUP_MEMBERSHIP_ADD_DEFAULT; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:ldap_name_groups; 56, list:[]; 57, for_statement; 57, 58; 57, 61; 57, 62; 58, pattern_list; 58, 59; 58, 60; 59, identifier:cname; 60, identifier:ldap_attributes; 61, identifier:ldap_groups; 62, block; 62, 63; 62, 67; 62, 101; 62, 133; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:defaults; 66, dictionary; 67, try_statement; 67, 68; 67, 96; 68, block; 68, 69; 69, for_statement; 69, 70; 69, 73; 69, 78; 70, pattern_list; 70, 71; 70, 72; 71, identifier:name; 72, identifier:attribute; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:ldap_attributes; 76, identifier:items; 77, argument_list; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 88; 81, subscript; 81, 82; 81, 83; 82, identifier:defaults; 83, subscript; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:conf_LDAP_SYNC_GROUP_ATTRIBUTES; 87, identifier:name; 88, call; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:attribute; 92, integer:0; 93, identifier:decode; 94, argument_list; 94, 95; 95, string:'utf-8'; 96, except_clause; 96, 97; 96, 98; 96, 99; 97, identifier:AttributeError; 98, comment; 99, block; 99, 100; 100, continue_statement; 101, try_statement; 101, 102; 101, 116; 102, block; 102, 103; 102, 109; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:groupname; 106, subscript; 106, 107; 106, 108; 107, identifier:defaults; 108, identifier:groupname_field; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:ldap_name_groups; 113, identifier:append; 114, argument_list; 114, 115; 115, identifier:groupname; 116, except_clause; 116, 117; 116, 118; 117, identifier:KeyError; 118, block; 118, 119; 118, 128; 118, 132; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:logger; 123, identifier:warning; 124, argument_list; 124, 125; 125, binary_operator:%; 125, 126; 125, 127; 126, string:"Group is missing a required attribute '%s'"; 127, identifier:groupname_field; 128, expression_statement; 128, 129; 129, augmented_assignment:+=; 129, 130; 129, 131; 130, identifier:user_Membership_errors; 131, integer:1; 132, continue_statement; 133, if_statement; 133, 134; 133, 138; 134, parenthesized_expression; 134, 135; 135, comparison_operator:not; 135, 136; 135, 137; 136, identifier:groupname; 137, identifier:actualGroups; 138, block; 138, 139; 138, 151; 138, 152; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:kwargs; 142, dictionary; 142, 143; 142, 148; 143, pair; 143, 144; 143, 147; 144, binary_operator:+; 144, 145; 144, 146; 145, identifier:groupname_field; 146, string:'__iexact'; 147, identifier:groupname; 148, pair; 148, 149; 148, 150; 149, string:'defaults'; 150, identifier:defaults; 151, comment; 152, try_statement; 152, 153; 152, 193; 152, 199; 152, 222; 153, block; 153, 154; 154, if_statement; 154, 155; 154, 159; 154, 174; 155, parenthesized_expression; 155, 156; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:conf_LDAP_SYNC_GROUP_MEMBERSHIP_CREATE_IF_NOT_EXISTS; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 165; 162, pattern_list; 162, 163; 162, 164; 163, identifier:group; 164, identifier:created; 165, call; 165, 166; 165, 171; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:Group; 169, identifier:objects; 170, identifier:get_or_create; 171, argument_list; 171, 172; 172, dictionary_splat; 172, 173; 173, identifier:kwargs; 174, else_clause; 174, 175; 175, block; 175, 176; 175, 189; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:group; 179, call; 179, 180; 179, 185; 180, attribute; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:Group; 183, identifier:objects; 184, identifier:get; 185, argument_list; 185, 186; 186, keyword_argument; 186, 187; 186, 188; 187, identifier:name; 188, identifier:groupname; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:created; 192, False; 193, except_clause; 193, 194; 193, 196; 193, 197; 194, parenthesized_expression; 194, 195; 195, identifier:ObjectDoesNotExist; 196, comment; 197, block; 197, 198; 198, continue_statement; 199, except_clause; 199, 200; 199, 206; 200, as_pattern; 200, 201; 200, 204; 201, tuple; 201, 202; 201, 203; 202, identifier:IntegrityError; 203, identifier:DataError; 204, as_pattern_target; 204, 205; 205, identifier:e; 206, block; 206, 207; 206, 218; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:logger; 211, identifier:error; 212, argument_list; 212, 213; 213, binary_operator:%; 213, 214; 213, 215; 214, string:"Error creating group %s: %s"; 215, tuple; 215, 216; 215, 217; 216, identifier:groupname; 217, identifier:e; 218, expression_statement; 218, 219; 219, augmented_assignment:+=; 219, 220; 219, 221; 220, identifier:user_Membership_errors; 221, integer:1; 222, else_clause; 222, 223; 223, block; 223, 224; 223, 236; 223, 237; 223, 246; 224, if_statement; 224, 225; 224, 226; 225, identifier:created; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:logger; 231, identifier:debug; 232, argument_list; 232, 233; 233, binary_operator:%; 233, 234; 233, 235; 234, string:"Created group %s"; 235, identifier:groupname; 236, comment; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 244; 239, attribute; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:group; 242, identifier:user_set; 243, identifier:add; 244, argument_list; 244, 245; 245, identifier:user; 246, expression_statement; 246, 247; 247, augmented_assignment:+=; 247, 248; 247, 249; 248, identifier:user_Membership_added; 249, integer:1; 250, comment; 251, comment; 252, for_statement; 252, 253; 252, 254; 252, 255; 253, identifier:check_group; 254, identifier:actualGroups; 255, block; 255, 256; 256, if_statement; 256, 257; 256, 261; 257, parenthesized_expression; 257, 258; 258, comparison_operator:not; 258, 259; 258, 260; 259, identifier:check_group; 260, identifier:ldap_name_groups; 261, block; 261, 262; 261, 275; 261, 284; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:group; 265, call; 265, 266; 265, 271; 266, attribute; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:Group; 269, identifier:objects; 270, identifier:get; 271, argument_list; 271, 272; 272, keyword_argument; 272, 273; 272, 274; 273, identifier:name; 274, identifier:check_group; 275, expression_statement; 275, 276; 276, call; 276, 277; 276, 282; 277, attribute; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:group; 280, identifier:user_set; 281, identifier:remove; 282, argument_list; 282, 283; 283, identifier:user; 284, expression_statement; 284, 285; 285, augmented_assignment:+=; 285, 286; 285, 287; 286, identifier:user_Membership_deleted; 287, integer:1; 288, if_statement; 288, 289; 288, 299; 289, parenthesized_expression; 289, 290; 290, boolean_operator:or; 290, 291; 290, 295; 291, parenthesized_expression; 291, 292; 292, comparison_operator:>; 292, 293; 292, 294; 293, identifier:user_Membership_deleted; 294, integer:0; 295, parenthesized_expression; 295, 296; 296, comparison_operator:>; 296, 297; 296, 298; 297, identifier:user_Membership_added; 298, integer:0; 299, block; 299, 300; 299, 306; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:group; 304, identifier:save; 305, argument_list; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:logger; 310, identifier:info; 311, argument_list; 311, 312; 312, binary_operator:%; 312, 313; 312, 314; 313, string:"Group membership for user %s synchronized: %d Added, %d Removed"; 314, tuple; 314, 315; 314, 318; 314, 319; 315, attribute; 315, 316; 315, 317; 316, identifier:user; 317, identifier:username; 318, identifier:user_Membership_added; 319, identifier:user_Membership_deleted; 320, comment; 321, expression_statement; 321, 322; 322, augmented_assignment:+=; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:self; 325, identifier:stats_membership_total; 326, identifier:user_Membership_total; 327, expression_statement; 327, 328; 328, augmented_assignment:+=; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:self; 331, identifier:stats_membership_added; 332, identifier:user_Membership_added; 333, expression_statement; 333, 334; 334, augmented_assignment:+=; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:self; 337, identifier:stats_membership_deleted; 338, identifier:user_Membership_deleted; 339, expression_statement; 339, 340; 340, augmented_assignment:+=; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:self; 343, identifier:stats_membership_errors; 344, identifier:user_Membership_errors
def sync_ldap_user_membership(self, user, ldap_groups): """Synchronize LDAP membership to Django membership""" groupname_field = 'name' actualGroups = user.groups.values_list('name', flat=True) user_Membership_total = len(ldap_groups) user_Membership_added = 0 user_Membership_deleted = 0 user_Membership_errors = 0 ldap_groups += self.conf_LDAP_SYNC_GROUP_MEMBERSHIP_ADD_DEFAULT ldap_name_groups = [] for cname, ldap_attributes in ldap_groups: defaults = {} try: for name, attribute in ldap_attributes.items(): defaults[self.conf_LDAP_SYNC_GROUP_ATTRIBUTES[name]] = attribute[0].decode('utf-8') except AttributeError: # In some cases attrs is a list instead of a dict; skip these invalid groups continue try: groupname = defaults[groupname_field] ldap_name_groups.append(groupname) except KeyError: logger.warning("Group is missing a required attribute '%s'" % groupname_field) user_Membership_errors += 1 continue if (groupname not in actualGroups): kwargs = { groupname_field + '__iexact': groupname, 'defaults': defaults, } #Adding Group Membership try: if (self.conf_LDAP_SYNC_GROUP_MEMBERSHIP_CREATE_IF_NOT_EXISTS): group, created = Group.objects.get_or_create(**kwargs) else: group = Group.objects.get(name=groupname) created = False except (ObjectDoesNotExist): #Doesn't exist and not autocreate groups, we pass the error continue except (IntegrityError, DataError) as e: logger.error("Error creating group %s: %s" % (groupname, e)) user_Membership_errors += 1 else: if created: logger.debug("Created group %s" % groupname) #Now well assign the user group.user_set.add(user) user_Membership_added += 1 #Default Primary Group: Temporary is fixed #removing group membership for check_group in actualGroups: if (check_group not in ldap_name_groups): group = Group.objects.get(name=check_group) group.user_set.remove(user) user_Membership_deleted += 1 if ((user_Membership_deleted > 0) or (user_Membership_added > 0)): group.save() logger.info("Group membership for user %s synchronized: %d Added, %d Removed" % (user.username, user_Membership_added, user_Membership_deleted)) #Return statistics self.stats_membership_total += user_Membership_total self.stats_membership_added += user_Membership_added self.stats_membership_deleted += user_Membership_deleted self.stats_membership_errors += user_Membership_errors
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:get; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:block; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:timeout; 10, None; 11, block; 11, 12; 11, 14; 11, 115; 11, 141; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 19; 15, not_operator; 15, 16; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_data; 19, block; 19, 20; 19, 28; 19, 36; 19, 51; 19, 64; 19, 75; 19, 85; 20, if_statement; 20, 21; 20, 23; 21, not_operator; 21, 22; 22, identifier:block; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:Empty; 27, argument_list; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:current; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:compat; 34, identifier:getcurrent; 35, argument_list; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:waketime; 39, conditional_expression:if; 39, 40; 39, 41; 39, 44; 40, None; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:timeout; 43, None; 44, binary_operator:+; 44, 45; 44, 50; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:time; 48, identifier:time; 49, argument_list; 50, identifier:timeout; 51, if_statement; 51, 52; 51, 55; 52, comparison_operator:is; 52, 53; 52, 54; 53, identifier:timeout; 54, None; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:scheduler; 60, identifier:schedule_at; 61, argument_list; 61, 62; 61, 63; 62, identifier:waketime; 63, identifier:current; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:_waiters; 70, identifier:append; 71, argument_list; 71, 72; 72, tuple; 72, 73; 72, 74; 73, identifier:current; 74, identifier:waketime; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 84; 77, attribute; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:scheduler; 81, identifier:state; 82, identifier:mainloop; 83, identifier:switch; 84, argument_list; 85, if_statement; 85, 86; 85, 89; 86, comparison_operator:is; 86, 87; 86, 88; 87, identifier:timeout; 88, None; 89, block; 89, 90; 90, if_statement; 90, 91; 90, 99; 91, not_operator; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:scheduler; 95, identifier:_remove_timer; 96, argument_list; 96, 97; 96, 98; 97, identifier:waketime; 98, identifier:current; 99, block; 99, 100; 99, 111; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:_waiters; 106, identifier:remove; 107, argument_list; 107, 108; 108, tuple; 108, 109; 108, 110; 109, identifier:current; 110, identifier:waketime; 111, raise_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:Empty; 114, argument_list; 115, if_statement; 115, 116; 115, 125; 116, boolean_operator:and; 116, 117; 116, 122; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:full; 121, argument_list; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:_waiters; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:scheduler; 130, identifier:schedule; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 140; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:_waiters; 138, identifier:popleft; 139, argument_list; 140, integer:0; 141, return_statement; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:_get; 146, argument_list
def get(self, block=True, timeout=None): """get an item out of the queue .. note:: if `block` is ``True`` (the default) and the queue is :meth`empty`, this method will block the current coroutine until something has been :meth:`put`. :param block: whether to block if there is no data yet available (default ``True``) :type block: bool :param timeout: the maximum time in seconds to block waiting for data. with the default of ``None``, can wait indefinitely. this is unused if `block` is ``False``. :type timeout: int, float or None :raises: :class:`Empty` if there is no data in the queue and block is ``False``, or `timeout` expires :returns: something that was previously :meth:`put` in the queue """ if not self._data: if not block: raise Empty() current = compat.getcurrent() waketime = None if timeout is None else time.time() + timeout if timeout is not None: scheduler.schedule_at(waketime, current) self._waiters.append((current, waketime)) scheduler.state.mainloop.switch() if timeout is not None: if not scheduler._remove_timer(waketime, current): self._waiters.remove((current, waketime)) raise Empty() if self.full() and self._waiters: scheduler.schedule(self._waiters.popleft()[0]) return self._get()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:put; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:item; 6, default_parameter; 6, 7; 6, 8; 7, identifier:block; 8, True; 9, default_parameter; 9, 10; 9, 11; 10, identifier:timeout; 11, None; 12, block; 12, 13; 12, 15; 12, 117; 12, 144; 12, 158; 12, 164; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 21; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:full; 20, argument_list; 21, block; 21, 22; 21, 30; 21, 38; 21, 53; 21, 66; 21, 77; 21, 87; 22, if_statement; 22, 23; 22, 25; 23, not_operator; 23, 24; 24, identifier:block; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:Full; 29, argument_list; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:current; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:compat; 36, identifier:getcurrent; 37, argument_list; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:waketime; 41, conditional_expression:if; 41, 42; 41, 43; 41, 46; 42, None; 43, comparison_operator:is; 43, 44; 43, 45; 44, identifier:timeout; 45, None; 46, binary_operator:+; 46, 47; 46, 52; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:time; 50, identifier:time; 51, argument_list; 52, identifier:timeout; 53, if_statement; 53, 54; 53, 57; 54, comparison_operator:is; 54, 55; 54, 56; 55, identifier:timeout; 56, None; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:scheduler; 62, identifier:schedule_at; 63, argument_list; 63, 64; 63, 65; 64, identifier:waketime; 65, identifier:current; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 73; 68, attribute; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:_waiters; 72, identifier:append; 73, argument_list; 73, 74; 74, tuple; 74, 75; 74, 76; 75, identifier:current; 76, identifier:waketime; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 86; 79, attribute; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:scheduler; 83, identifier:state; 84, identifier:mainloop; 85, identifier:switch; 86, argument_list; 87, if_statement; 87, 88; 87, 91; 88, comparison_operator:is; 88, 89; 88, 90; 89, identifier:timeout; 90, None; 91, block; 91, 92; 92, if_statement; 92, 93; 92, 101; 93, not_operator; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:scheduler; 97, identifier:_remove_timer; 98, argument_list; 98, 99; 98, 100; 99, identifier:waketime; 100, identifier:current; 101, block; 101, 102; 101, 113; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:_waiters; 108, identifier:remove; 109, argument_list; 109, 110; 110, tuple; 110, 111; 110, 112; 111, identifier:current; 112, identifier:waketime; 113, raise_statement; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:Full; 116, argument_list; 117, if_statement; 117, 118; 117, 128; 118, boolean_operator:and; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_waiters; 122, not_operator; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:full; 127, argument_list; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:scheduler; 133, identifier:schedule; 134, argument_list; 134, 135; 135, subscript; 135, 136; 135, 143; 136, call; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:_waiters; 141, identifier:popleft; 142, argument_list; 143, integer:0; 144, if_statement; 144, 145; 144, 149; 145, not_operator; 145, 146; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:_open_tasks; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_jobs_done; 156, identifier:clear; 157, argument_list; 158, expression_statement; 158, 159; 159, augmented_assignment:+=; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:_open_tasks; 163, integer:1; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:_put; 169, argument_list; 169, 170; 170, identifier:item
def put(self, item, block=True, timeout=None): """put an item into the queue .. note:: if the queue was created with a `maxsize` and it is currently :meth:`full`, this method will block the calling coroutine until another coroutine :meth:`get`\ s an item. :param item: the object to put into the queue, can be any type :param block: whether to block if the queue is already :meth:`full` (default ``True``) :type block: bool :param timeout: the maximum time in seconds to block waiting. with the default of ``None``, it can wait indefinitely. this is unused if `block` is ``False``. :type timeout: int, float or None :raises: :class:`Full` if the queue is :meth:`full` and `block` is ``False``, or if `timeout` expires. """ if self.full(): if not block: raise Full() current = compat.getcurrent() waketime = None if timeout is None else time.time() + timeout if timeout is not None: scheduler.schedule_at(waketime, current) self._waiters.append((current, waketime)) scheduler.state.mainloop.switch() if timeout is not None: if not scheduler._remove_timer(waketime, current): self._waiters.remove((current, waketime)) raise Full() if self._waiters and not self.full(): scheduler.schedule(self._waiters.popleft()[0]) if not self._open_tasks: self._jobs_done.clear() self._open_tasks += 1 self._put(item)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_compress_tokens; 3, parameters; 3, 4; 4, identifier:tokens; 5, block; 5, 6; 5, 8; 5, 12; 5, 58; 5, 65; 5, 69; 5, 165; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:recorder; 11, None; 12, function_definition; 12, 13; 12, 14; 12, 17; 13, function_name:_edge_case_stray_end_quoted; 14, parameters; 14, 15; 14, 16; 15, identifier:tokens; 16, identifier:index; 17, block; 17, 18; 17, 20; 17, 21; 17, 22; 17, 23; 17, 24; 18, expression_statement; 18, 19; 19, comment; 20, comment; 21, comment; 22, comment; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, subscript; 26, 27; 26, 28; 27, identifier:tokens; 28, identifier:index; 29, call; 29, 30; 29, 31; 30, identifier:Token; 31, argument_list; 31, 32; 31, 37; 31, 44; 31, 51; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:type; 34, attribute; 34, 35; 34, 36; 35, identifier:TokenType; 36, identifier:UnquotedLiteral; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:content; 39, attribute; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:tokens; 42, identifier:index; 43, identifier:content; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:line; 46, attribute; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:tokens; 49, identifier:index; 50, identifier:line; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:col; 53, attribute; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:tokens; 56, identifier:index; 57, identifier:col; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:tokens_len; 61, call; 61, 62; 61, 63; 62, identifier:len; 63, argument_list; 63, 64; 64, identifier:tokens; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:index; 68, integer:0; 69, with_statement; 69, 70; 69, 78; 70, with_clause; 70, 71; 71, with_item; 71, 72; 72, as_pattern; 72, 73; 72, 76; 73, call; 73, 74; 73, 75; 74, identifier:_EdgeCaseStrayParens; 75, argument_list; 76, as_pattern_target; 76, 77; 77, identifier:edge_case_stray_parens; 78, block; 78, 79; 78, 89; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:edge_cases; 82, list:[ (_is_paren_type, edge_case_stray_parens), (_is_end_quoted_type, _edge_case_stray_end_quoted), ]; 82, 83; 82, 86; 83, tuple; 83, 84; 83, 85; 84, identifier:_is_paren_type; 85, identifier:edge_case_stray_parens; 86, tuple; 86, 87; 86, 88; 87, identifier:_is_end_quoted_type; 88, identifier:_edge_case_stray_end_quoted; 89, while_statement; 89, 90; 89, 93; 90, comparison_operator:<; 90, 91; 90, 92; 91, identifier:index; 92, identifier:tokens_len; 93, block; 93, 94; 93, 103; 93, 161; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:recorder; 97, call; 97, 98; 97, 99; 98, identifier:_find_recorder; 99, argument_list; 99, 100; 99, 101; 99, 102; 100, identifier:recorder; 101, identifier:tokens; 102, identifier:index; 103, if_statement; 103, 104; 103, 107; 103, 108; 103, 136; 104, comparison_operator:is; 104, 105; 104, 106; 105, identifier:recorder; 106, None; 107, comment; 108, block; 108, 109; 108, 120; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:result; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:recorder; 115, identifier:consume_token; 116, argument_list; 116, 117; 116, 118; 116, 119; 117, identifier:tokens; 118, identifier:index; 119, identifier:tokens_len; 120, if_statement; 120, 121; 120, 124; 121, comparison_operator:is; 121, 122; 121, 123; 122, identifier:result; 123, None; 124, block; 124, 125; 124, 132; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 131; 127, tuple_pattern; 127, 128; 127, 129; 127, 130; 128, identifier:index; 129, identifier:tokens_len; 130, identifier:tokens; 131, identifier:result; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:recorder; 135, None; 136, else_clause; 136, 137; 136, 138; 137, comment; 138, block; 138, 139; 139, for_statement; 139, 140; 139, 143; 139, 144; 140, pattern_list; 140, 141; 140, 142; 141, identifier:matcher; 142, identifier:handler; 143, identifier:edge_cases; 144, block; 144, 145; 145, if_statement; 145, 146; 145, 154; 146, call; 146, 147; 146, 148; 147, identifier:matcher; 148, argument_list; 148, 149; 149, attribute; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:tokens; 152, identifier:index; 153, identifier:type; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:handler; 158, argument_list; 158, 159; 158, 160; 159, identifier:tokens; 160, identifier:index; 161, expression_statement; 161, 162; 162, augmented_assignment:+=; 162, 163; 162, 164; 163, identifier:index; 164, integer:1; 165, return_statement; 165, 166; 166, identifier:tokens
def _compress_tokens(tokens): """Paste multi-line strings, comments, RST etc together. This function works by iterating over each over the _RECORDERS to determine if we should start recording a token sequence for pasting together. If it finds one, then we keep recording until that recorder is done and returns a pasted together token sequence. Keep going until we reach the end of the sequence. The sequence is modified in place, so any function that modifies it must return its new length. This is also why we use a while loop here. """ recorder = None def _edge_case_stray_end_quoted(tokens, index): """Convert stray end_quoted_literals to unquoted_literals.""" # In this case, "tokenize" the matched token into what it would # have looked like had the last quote not been there. Put the # last quote on the end of the final token and call it an # unquoted_literal tokens[index] = Token(type=TokenType.UnquotedLiteral, content=tokens[index].content, line=tokens[index].line, col=tokens[index].col) tokens_len = len(tokens) index = 0 with _EdgeCaseStrayParens() as edge_case_stray_parens: edge_cases = [ (_is_paren_type, edge_case_stray_parens), (_is_end_quoted_type, _edge_case_stray_end_quoted), ] while index < tokens_len: recorder = _find_recorder(recorder, tokens, index) if recorder is not None: # Do recording result = recorder.consume_token(tokens, index, tokens_len) if result is not None: (index, tokens_len, tokens) = result recorder = None else: # Handle edge cases for matcher, handler in edge_cases: if matcher(tokens[index].type): handler(tokens, index) index += 1 return tokens
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_parse_dependencies; 3, parameters; 3, 4; 4, identifier:string; 5, block; 5, 6; 5, 8; 5, 17; 5, 26; 5, 33; 5, 37; 5, 41; 5, 72; 5, 87; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:contents; 11, call; 11, 12; 11, 13; 12, identifier:_get_contents_between; 13, argument_list; 13, 14; 13, 15; 13, 16; 14, identifier:string; 15, string:'('; 16, string:')'; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:unsorted_dependencies; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:contents; 23, identifier:split; 24, argument_list; 24, 25; 25, string:','; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:_check_parameters; 29, argument_list; 29, 30; 29, 31; 30, identifier:unsorted_dependencies; 31, tuple; 31, 32; 32, string:'?'; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:buildable_dependencies; 36, list:[]; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:given_dependencies; 40, list:[]; 41, for_statement; 41, 42; 41, 43; 41, 44; 42, identifier:dependency; 43, identifier:unsorted_dependencies; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 51; 45, 63; 46, comparison_operator:==; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:dependency; 49, integer:0; 50, string:'?'; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:given_dependencies; 56, identifier:append; 57, argument_list; 57, 58; 58, subscript; 58, 59; 58, 60; 59, identifier:dependency; 60, slice; 60, 61; 60, 62; 61, integer:1; 62, colon; 63, else_clause; 63, 64; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:buildable_dependencies; 69, identifier:append; 70, argument_list; 70, 71; 71, identifier:dependency; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:string; 75, subscript; 75, 76; 75, 77; 76, identifier:string; 77, slice; 77, 78; 77, 86; 78, binary_operator:+; 78, 79; 78, 85; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:string; 82, identifier:index; 83, argument_list; 83, 84; 84, string:')'; 85, integer:1; 86, colon; 87, return_statement; 87, 88; 88, expression_list; 88, 89; 88, 90; 88, 91; 89, identifier:buildable_dependencies; 90, identifier:given_dependencies; 91, identifier:string
def _parse_dependencies(string): """ This function actually parses the dependencies are sorts them into the buildable and given dependencies """ contents = _get_contents_between(string, '(', ')') unsorted_dependencies = contents.split(',') _check_parameters(unsorted_dependencies, ('?',)) buildable_dependencies = [] given_dependencies = [] for dependency in unsorted_dependencies: if dependency[0] == '?': given_dependencies.append(dependency[1:]) else: buildable_dependencies.append(dependency) string = string[string.index(')') + 1:] return buildable_dependencies, given_dependencies, string
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:wait_fds; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:fd_events; 5, default_parameter; 5, 6; 5, 7; 6, identifier:inmask; 7, integer:1; 8, default_parameter; 8, 9; 8, 10; 9, identifier:outmask; 10, integer:2; 11, default_parameter; 11, 12; 11, 13; 12, identifier:timeout; 13, None; 14, block; 14, 15; 14, 17; 14, 25; 14, 29; 14, 33; 14, 37; 14, 88; 14, 155; 14, 201; 14, 229; 14, 244; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:current; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:compat; 23, identifier:getcurrent; 24, argument_list; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:activated; 28, dictionary; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:poll_regs; 32, dictionary; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:callback_refs; 36, dictionary; 37, function_definition; 37, 38; 37, 39; 37, 42; 38, function_name:activate; 39, parameters; 39, 40; 39, 41; 40, identifier:fd; 41, identifier:event; 42, block; 42, 43; 42, 73; 42, 74; 42, 82; 43, if_statement; 43, 44; 43, 50; 43, 51; 43, 52; 44, boolean_operator:and; 44, 45; 44, 47; 45, not_operator; 45, 46; 46, identifier:activated; 47, comparison_operator:!=; 47, 48; 47, 49; 48, identifier:timeout; 49, integer:0; 50, comment; 51, comment; 52, block; 52, 53; 52, 60; 52, 61; 52, 62; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:scheduler; 57, identifier:schedule; 58, argument_list; 58, 59; 59, identifier:current; 60, comment; 61, comment; 62, if_statement; 62, 63; 62, 64; 63, identifier:timeout; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:scheduler; 69, identifier:_remove_timer; 70, argument_list; 70, 71; 70, 72; 71, identifier:waketime; 72, identifier:current; 73, comment; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:activated; 78, identifier:setdefault; 79, argument_list; 79, 80; 79, 81; 80, identifier:fd; 81, integer:0; 82, expression_statement; 82, 83; 83, augmented_assignment:|=; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:activated; 86, identifier:fd; 87, identifier:event; 88, for_statement; 88, 89; 88, 92; 88, 93; 89, pattern_list; 89, 90; 89, 91; 90, identifier:fd; 91, identifier:events; 92, identifier:fd_events; 93, block; 93, 94; 93, 98; 93, 102; 93, 118; 93, 134; 93, 142; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:readable; 97, None; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:writable; 101, None; 102, if_statement; 102, 103; 102, 106; 103, binary_operator:&; 103, 104; 103, 105; 104, identifier:events; 105, identifier:inmask; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:readable; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:functools; 113, identifier:partial; 114, argument_list; 114, 115; 114, 116; 114, 117; 115, identifier:activate; 116, identifier:fd; 117, identifier:inmask; 118, if_statement; 118, 119; 118, 122; 119, binary_operator:&; 119, 120; 119, 121; 120, identifier:events; 121, identifier:outmask; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:writable; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:functools; 129, identifier:partial; 130, argument_list; 130, 131; 130, 132; 130, 133; 131, identifier:activate; 132, identifier:fd; 133, identifier:outmask; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:callback_refs; 138, identifier:fd; 139, tuple; 139, 140; 139, 141; 140, identifier:readable; 141, identifier:writable; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:poll_regs; 146, identifier:fd; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:scheduler; 150, identifier:_register_fd; 151, argument_list; 151, 152; 151, 153; 151, 154; 152, identifier:fd; 153, identifier:readable; 154, identifier:writable; 155, if_statement; 155, 156; 155, 157; 155, 158; 155, 176; 155, 188; 156, identifier:timeout; 157, comment; 158, block; 158, 159; 158, 169; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:waketime; 162, binary_operator:+; 162, 163; 162, 168; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:time; 166, identifier:time; 167, argument_list; 168, identifier:timeout; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:scheduler; 173, identifier:pause_until; 174, argument_list; 174, 175; 175, identifier:waketime; 176, elif_clause; 176, 177; 176, 180; 176, 181; 177, comparison_operator:==; 177, 178; 177, 179; 178, identifier:timeout; 179, integer:0; 180, comment; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:scheduler; 186, identifier:pause; 187, argument_list; 188, else_clause; 188, 189; 188, 190; 189, comment; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 200; 193, attribute; 193, 194; 193, 199; 194, attribute; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:scheduler; 197, identifier:state; 198, identifier:mainloop; 199, identifier:switch; 200, argument_list; 201, for_statement; 201, 202; 201, 205; 201, 210; 202, pattern_list; 202, 203; 202, 204; 203, identifier:fd; 204, identifier:reg; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:poll_regs; 208, identifier:iteritems; 209, argument_list; 210, block; 210, 211; 210, 219; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 216; 213, pattern_list; 213, 214; 213, 215; 214, identifier:readable; 215, identifier:writable; 216, subscript; 216, 217; 216, 218; 217, identifier:callback_refs; 218, identifier:fd; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:scheduler; 223, identifier:_unregister_fd; 224, argument_list; 224, 225; 224, 226; 224, 227; 224, 228; 225, identifier:fd; 226, identifier:readable; 227, identifier:writable; 228, identifier:reg; 229, if_statement; 229, 230; 229, 235; 230, attribute; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:scheduler; 233, identifier:state; 234, identifier:interrupted; 235, block; 235, 236; 236, raise_statement; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:IOError; 239, argument_list; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:errno; 242, identifier:EINTR; 243, string:"interrupted system call"; 244, return_statement; 244, 245; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:activated; 248, identifier:items; 249, argument_list
def wait_fds(fd_events, inmask=1, outmask=2, timeout=None): """wait for the first of a number of file descriptors to have activity .. note:: this method can block it will return once there is relevant activity on the file descriptors, or the timeout expires :param fd_events: two-tuples, each one a file descriptor and a mask made up of the inmask and/or the outmask bitwise-ORd together :type fd_events: list :param inmask: the mask to use for readable events (default 1) :type inmask: int :param outmask: the mask to use for writable events (default 2) :type outmask: int :param timeout: the maximum time to wait before raising an exception (default None) :type timeout: int, float or None :returns: a list of two-tuples, each is a file descriptor and an event mask (made up of inmask and/or outmask bitwise-ORd together) representing readable and writable events """ current = compat.getcurrent() activated = {} poll_regs = {} callback_refs = {} def activate(fd, event): if not activated and timeout != 0: # this is the first invocation of `activated` for a blocking # `wait_fds` call, so re-schedule the blocked coroutine scheduler.schedule(current) # if there was a timeout then also have to pull # the coroutine from the timed_paused structure if timeout: scheduler._remove_timer(waketime, current) # in any case, set the event information activated.setdefault(fd, 0) activated[fd] |= event for fd, events in fd_events: readable = None writable = None if events & inmask: readable = functools.partial(activate, fd, inmask) if events & outmask: writable = functools.partial(activate, fd, outmask) callback_refs[fd] = (readable, writable) poll_regs[fd] = scheduler._register_fd(fd, readable, writable) if timeout: # real timeout value, schedule ourself `timeout` seconds in the future waketime = time.time() + timeout scheduler.pause_until(waketime) elif timeout == 0: # timeout == 0, only pause for 1 loop iteration scheduler.pause() else: # timeout is None, it's up to _hit_poller->activate to bring us back scheduler.state.mainloop.switch() for fd, reg in poll_regs.iteritems(): readable, writable = callback_refs[fd] scheduler._unregister_fd(fd, readable, writable, reg) if scheduler.state.interrupted: raise IOError(errno.EINTR, "interrupted system call") return activated.items()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:processAndSetDefaults; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 20; 5, 43; 5, 51; 5, 59; 5, 77; 5, 78; 5, 79; 5, 80; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 14; 10, not_operator; 10, 11; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:input; 14, block; 14, 15; 15, raise_statement; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:ValueError; 18, argument_list; 18, 19; 19, identifier:NO_INPUT_FILE; 20, if_statement; 20, 21; 20, 25; 20, 26; 20, 39; 21, not_operator; 21, 22; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:output; 25, comment; 26, block; 26, 27; 26, 37; 26, 38; 27, if_statement; 27, 28; 27, 32; 28, not_operator; 28, 29; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:build_directory; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:File; 36, argument_list; 37, pass_statement; 38, comment; 39, else_clause; 39, 40; 40, block; 40, 41; 40, 42; 41, pass_statement; 42, comment; 43, if_statement; 43, 44; 43, 48; 44, not_operator; 44, 45; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:build_directory; 48, block; 48, 49; 48, 50; 49, pass_statement; 50, comment; 51, for_statement; 51, 52; 51, 53; 51, 56; 52, identifier:dependency; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:given_dependencies; 56, block; 56, 57; 56, 58; 57, pass_statement; 58, comment; 59, if_statement; 59, 60; 59, 71; 60, comparison_operator:!=; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:output_format; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:output; 69, identifier:getType; 70, argument_list; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:ValueError; 75, argument_list; 75, 76; 76, string:""; 77, comment; 78, comment; 79, comment; 80, return_statement
def processAndSetDefaults(self): """ The heart of the 'Instruction' object. This method will make sure that all fields not entered will be defaulted to a correct value. Also checks for incongruities in the data entered, if it was by the user. """ # INPUT, OUTPUT, GIVEN + BUILDABLE DEPS if not self.input: raise ValueError(NO_INPUT_FILE) if not self.output: # Build directory must exist, right? if not self.build_directory: File() pass # Can it be built? / reference self.output_format for this else: pass # if it is not congruent with other info provided if not self.build_directory: pass # Initialize it for dependency in self.given_dependencies: pass # Check if the dependcy exists if self.output_format != self.output.getType(): raise ValueError("") # Given dependencies must actually exist! # output_name must be at a lower extenion level than input_name # The build directory return
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:schedule; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:target; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:args; 9, tuple; 10, default_parameter; 10, 11; 10, 12; 11, identifier:kwargs; 12, None; 13, block; 13, 14; 13, 16; 13, 39; 13, 69; 13, 78; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:target; 19, None; 20, block; 20, 21; 20, 37; 21, function_definition; 21, 22; 21, 23; 21, 25; 22, function_name:decorator; 23, parameters; 23, 24; 24, identifier:target; 25, block; 25, 26; 26, return_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:schedule; 29, argument_list; 29, 30; 29, 31; 29, 34; 30, identifier:target; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:args; 33, identifier:args; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:kwargs; 36, identifier:kwargs; 37, return_statement; 37, 38; 38, identifier:decorator; 39, if_statement; 39, 40; 39, 53; 39, 58; 40, boolean_operator:or; 40, 41; 40, 48; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 45; 44, identifier:target; 45, attribute; 45, 46; 45, 47; 46, identifier:compat; 47, identifier:greenlet; 48, comparison_operator:is; 48, 49; 48, 50; 49, identifier:target; 50, attribute; 50, 51; 50, 52; 51, identifier:compat; 52, identifier:main_greenlet; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:glet; 57, identifier:target; 58, else_clause; 58, 59; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:glet; 63, call; 63, 64; 63, 65; 64, identifier:greenlet; 65, argument_list; 65, 66; 65, 67; 65, 68; 66, identifier:target; 67, identifier:args; 68, identifier:kwargs; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:state; 74, identifier:paused; 75, identifier:append; 76, argument_list; 76, 77; 77, identifier:glet; 78, return_statement; 78, 79; 79, identifier:target
def schedule(target=None, args=(), kwargs=None): """insert a greenlet into the scheduler If provided a function, it is wrapped in a new greenlet :param target: what to schedule :type target: function or greenlet :param args: arguments for the function (only used if ``target`` is a function) :type args: tuple :param kwargs: keyword arguments for the function (only used if ``target`` is a function) :type kwargs: dict or None :returns: the ``target`` argument This function can also be used as a decorator, either preloading ``args`` and/or ``kwargs`` or not: >>> @schedule >>> def f(): ... print 'hello from f' >>> @schedule(args=('world',)) >>> def f(name): ... print 'hello %s' % name """ if target is None: def decorator(target): return schedule(target, args=args, kwargs=kwargs) return decorator if isinstance(target, compat.greenlet) or target is compat.main_greenlet: glet = target else: glet = greenlet(target, args, kwargs) state.paused.append(glet) return target
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:schedule_at; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:unixtime; 5, default_parameter; 5, 6; 5, 7; 6, identifier:target; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:args; 10, tuple; 11, default_parameter; 11, 12; 11, 13; 12, identifier:kwargs; 13, None; 14, block; 14, 15; 14, 17; 14, 41; 14, 71; 14, 81; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:target; 20, None; 21, block; 21, 22; 21, 39; 22, function_definition; 22, 23; 22, 24; 22, 26; 23, function_name:decorator; 24, parameters; 24, 25; 25, identifier:target; 26, block; 26, 27; 27, return_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:schedule_at; 30, argument_list; 30, 31; 30, 32; 30, 33; 30, 36; 31, identifier:unixtime; 32, identifier:target; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:args; 35, identifier:args; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:kwargs; 38, identifier:kwargs; 39, return_statement; 39, 40; 40, identifier:decorator; 41, if_statement; 41, 42; 41, 55; 41, 60; 42, boolean_operator:or; 42, 43; 42, 50; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:target; 47, attribute; 47, 48; 47, 49; 48, identifier:compat; 49, identifier:greenlet; 50, comparison_operator:is; 50, 51; 50, 52; 51, identifier:target; 52, attribute; 52, 53; 52, 54; 53, identifier:compat; 54, identifier:main_greenlet; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:glet; 59, identifier:target; 60, else_clause; 60, 61; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:glet; 65, call; 65, 66; 65, 67; 66, identifier:greenlet; 67, argument_list; 67, 68; 67, 69; 67, 70; 68, identifier:target; 69, identifier:args; 70, identifier:kwargs; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:state; 76, identifier:timed_paused; 77, identifier:insert; 78, argument_list; 78, 79; 78, 80; 79, identifier:unixtime; 80, identifier:glet; 81, return_statement; 81, 82; 82, identifier:target
def schedule_at(unixtime, target=None, args=(), kwargs=None): """insert a greenlet into the scheduler to be run at a set time If provided a function, it is wrapped in a new greenlet :param unixtime: the unix timestamp at which the new greenlet should be started :type unixtime: int or float :param target: what to schedule :type target: function or greenlet :param args: arguments for the function (only used if ``target`` is a function) :type args: tuple :param kwargs: keyword arguments for the function (only used if ``target`` is a function) :type kwargs: dict or None :returns: the ``target`` argument This function can also be used as a decorator: >>> @schedule_at(1296423834) >>> def f(): ... print 'hello from f' and args/kwargs can also be preloaded: >>> @schedule_at(1296423834, args=('world',)) >>> def f(name): ... print 'hello %s' % name """ if target is None: def decorator(target): return schedule_at(unixtime, target, args=args, kwargs=kwargs) return decorator if isinstance(target, compat.greenlet) or target is compat.main_greenlet: glet = target else: glet = greenlet(target, args, kwargs) state.timed_paused.insert(unixtime, glet) return target
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:schedule_recurring; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:interval; 5, default_parameter; 5, 6; 5, 7; 6, identifier:target; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:maxtimes; 10, integer:0; 11, default_parameter; 11, 12; 11, 13; 12, identifier:starting_at; 13, integer:0; 14, default_parameter; 14, 15; 14, 16; 15, identifier:args; 16, tuple; 17, default_parameter; 17, 18; 17, 19; 18, identifier:kwargs; 19, None; 20, block; 20, 21; 20, 23; 20, 33; 20, 55; 20, 59; 20, 90; 20, 134; 20, 140; 20, 151; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:starting_at; 26, boolean_operator:or; 26, 27; 26, 28; 27, identifier:starting_at; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:time; 31, identifier:time; 32, argument_list; 33, if_statement; 33, 34; 33, 37; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:target; 36, None; 37, block; 37, 38; 37, 53; 38, function_definition; 38, 39; 38, 40; 38, 42; 39, function_name:decorator; 40, parameters; 40, 41; 41, identifier:target; 42, block; 42, 43; 43, return_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:schedule_recurring; 46, argument_list; 46, 47; 46, 48; 46, 49; 46, 50; 46, 51; 46, 52; 47, identifier:interval; 48, identifier:target; 49, identifier:maxtimes; 50, identifier:starting_at; 51, identifier:args; 52, identifier:kwargs; 53, return_statement; 53, 54; 54, identifier:decorator; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:func; 58, identifier:target; 59, if_statement; 59, 60; 59, 73; 60, boolean_operator:or; 60, 61; 60, 68; 61, call; 61, 62; 61, 63; 62, identifier:isinstance; 63, argument_list; 63, 64; 63, 65; 64, identifier:target; 65, attribute; 65, 66; 65, 67; 66, identifier:compat; 67, identifier:greenlet; 68, comparison_operator:is; 68, 69; 68, 70; 69, identifier:target; 70, attribute; 70, 71; 70, 72; 71, identifier:compat; 72, identifier:main_greenlet; 73, block; 73, 74; 73, 84; 74, if_statement; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:target; 77, identifier:dead; 78, block; 78, 79; 79, raise_statement; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:TypeError; 82, argument_list; 82, 83; 83, string:"can't schedule a dead greenlet"; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:func; 87, attribute; 87, 88; 87, 89; 88, identifier:target; 89, identifier:run; 90, function_definition; 90, 91; 90, 92; 90, 95; 90, 96; 90, 97; 91, function_name:run_and_schedule_one; 92, parameters; 92, 93; 92, 94; 93, identifier:tstamp; 94, identifier:count; 95, comment; 96, comment; 97, block; 97, 98; 98, if_statement; 98, 99; 98, 105; 99, boolean_operator:or; 99, 100; 99, 102; 100, not_operator; 100, 101; 101, identifier:maxtimes; 102, comparison_operator:<; 102, 103; 102, 104; 103, identifier:count; 104, identifier:maxtimes; 105, block; 105, 106; 105, 110; 105, 121; 106, expression_statement; 106, 107; 107, augmented_assignment:+=; 107, 108; 107, 109; 108, identifier:tstamp; 109, identifier:interval; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:func; 113, argument_list; 113, 114; 113, 116; 114, list_splat; 114, 115; 115, identifier:args; 116, dictionary_splat; 116, 117; 117, parenthesized_expression; 117, 118; 118, boolean_operator:or; 118, 119; 118, 120; 119, identifier:kwargs; 120, dictionary; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:schedule_at; 124, argument_list; 124, 125; 124, 126; 124, 127; 125, identifier:tstamp; 126, identifier:run_and_schedule_one; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:args; 129, tuple; 129, 130; 129, 131; 130, identifier:tstamp; 131, binary_operator:+; 131, 132; 131, 133; 132, identifier:count; 133, integer:1; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:firstrun; 137, binary_operator:+; 137, 138; 137, 139; 138, identifier:starting_at; 139, identifier:interval; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:schedule_at; 143, argument_list; 143, 144; 143, 145; 143, 146; 144, identifier:firstrun; 145, identifier:run_and_schedule_one; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:args; 148, tuple; 148, 149; 148, 150; 149, identifier:firstrun; 150, integer:0; 151, return_statement; 151, 152; 152, identifier:target
def schedule_recurring(interval, target=None, maxtimes=0, starting_at=0, args=(), kwargs=None): """insert a greenlet into the scheduler to run regularly at an interval If provided a function, it is wrapped in a new greenlet :param interval: the number of seconds between invocations :type interval: int or float :param target: what to schedule :type target: function or greenlet :param maxtimes: if provided, do not run more than ``maxtimes`` iterations :type maxtimes: int :param starting_at: the unix timestamp of when to schedule it for the first time (defaults to the time of the ``schedule_recurring`` call) :type starting_at: int or float :param args: arguments for the function :type args: tuple :param kwargs: keyword arguments for the function :type kwargs: dict or None :returns: the ``target`` argument This function can also be used as a decorator: >>> @schedule_recurring(30) >>> def f(): ... print "the regular 'hello' from f" and args/kwargs can also be preloaded: >>> @schedule_recurring(30, args=('world',)) >>> def f(name): ... print 'the regular hello %s' % name """ starting_at = starting_at or time.time() if target is None: def decorator(target): return schedule_recurring( interval, target, maxtimes, starting_at, args, kwargs) return decorator func = target if isinstance(target, compat.greenlet) or target is compat.main_greenlet: if target.dead: raise TypeError("can't schedule a dead greenlet") func = target.run def run_and_schedule_one(tstamp, count): # pass in the time scheduled instead of just checking # time.time() so that delays don't add up if not maxtimes or count < maxtimes: tstamp += interval func(*args, **(kwargs or {})) schedule_at(tstamp, run_and_schedule_one, args=(tstamp, count + 1)) firstrun = starting_at + interval schedule_at(firstrun, run_and_schedule_one, args=(firstrun, 0)) return target
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:assert_output; 3, parameters; 3, 4; 3, 5; 4, identifier:output; 5, identifier:assert_equal; 6, block; 6, 7; 6, 9; 6, 16; 6, 23; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:sorted_output; 12, call; 12, 13; 12, 14; 13, identifier:sorted; 14, argument_list; 14, 15; 15, identifier:output; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:sorted_assert; 19, call; 19, 20; 19, 21; 20, identifier:sorted; 21, argument_list; 21, 22; 22, identifier:assert_equal; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:!=; 24, 25; 24, 26; 25, identifier:sorted_output; 26, identifier:sorted_assert; 27, block; 27, 28; 28, raise_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:ValueError; 31, argument_list; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:ASSERT_ERROR; 35, identifier:format; 36, argument_list; 36, 37; 36, 38; 37, identifier:sorted_output; 38, identifier:sorted_assert
def assert_output(output, assert_equal): """ Check that two outputs have the same contents as one another, even if they aren't sorted yet """ sorted_output = sorted(output) sorted_assert = sorted(assert_equal) if sorted_output != sorted_assert: raise ValueError(ASSERT_ERROR.format(sorted_output, sorted_assert))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:get_input; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:prompt; 5, identifier:check; 6, keyword_separator; 7, default_parameter; 7, 8; 7, 9; 8, identifier:redo_prompt; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:repeat_prompt; 12, False; 13, block; 13, 14; 13, 16; 13, 28; 13, 32; 13, 58; 13, 72; 13, 97; 13, 144; 13, 151; 13, 177; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 22; 17, call; 17, 18; 17, 19; 18, identifier:isinstance; 19, argument_list; 19, 20; 19, 21; 20, identifier:check; 21, identifier:str; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:check; 26, tuple; 26, 27; 27, identifier:check; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:to_join; 31, list:[]; 32, for_statement; 32, 33; 32, 34; 32, 35; 33, identifier:item; 34, identifier:check; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 38; 36, 49; 37, identifier:item; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:to_join; 43, identifier:append; 44, argument_list; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:str; 47, argument_list; 47, 48; 48, identifier:item; 49, else_clause; 49, 50; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:to_join; 55, identifier:append; 56, argument_list; 56, 57; 57, string:"''"; 58, expression_statement; 58, 59; 59, augmented_assignment:+=; 59, 60; 59, 61; 60, identifier:prompt; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, string:" [{}]: "; 64, identifier:format; 65, argument_list; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, string:'/'; 69, identifier:join; 70, argument_list; 70, 71; 71, identifier:to_join; 72, if_statement; 72, 73; 72, 74; 72, 79; 73, identifier:repeat_prompt; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:redo_prompt; 78, identifier:prompt; 79, elif_clause; 79, 80; 79, 82; 80, not_operator; 80, 81; 81, identifier:redo_prompt; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:redo_prompt; 86, call; 86, 87; 86, 92; 87, attribute; 87, 88; 87, 91; 88, concatenated_string; 88, 89; 88, 90; 89, string:"Incorrect input, please choose from {}: "; 90, string:""; 91, identifier:format; 92, argument_list; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:str; 95, argument_list; 95, 96; 96, identifier:check; 97, if_statement; 97, 98; 97, 102; 97, 113; 97, 129; 98, call; 98, 99; 98, 100; 99, identifier:callable; 100, argument_list; 100, 101; 101, identifier:check; 102, block; 102, 103; 103, function_definition; 103, 104; 103, 105; 103, 107; 104, function_name:_checker; 105, parameters; 105, 106; 106, identifier:r; 107, block; 107, 108; 108, return_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:check; 111, argument_list; 111, 112; 112, identifier:r; 113, elif_clause; 113, 114; 113, 119; 114, call; 114, 115; 114, 116; 115, identifier:isinstance; 116, argument_list; 116, 117; 116, 118; 117, identifier:check; 118, identifier:tuple; 119, block; 119, 120; 120, function_definition; 120, 121; 120, 122; 120, 124; 121, function_name:_checker; 122, parameters; 122, 123; 123, identifier:r; 124, block; 124, 125; 125, return_statement; 125, 126; 126, comparison_operator:in; 126, 127; 126, 128; 127, identifier:r; 128, identifier:check; 129, else_clause; 129, 130; 130, block; 130, 131; 131, raise_statement; 131, 132; 132, call; 132, 133; 132, 134; 133, identifier:ValueError; 134, argument_list; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:RESPONSES_ERROR; 138, identifier:format; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:type; 142, argument_list; 142, 143; 143, identifier:check; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:response; 147, call; 147, 148; 147, 149; 148, identifier:input; 149, argument_list; 149, 150; 150, identifier:prompt; 151, while_statement; 151, 152; 151, 157; 152, not_operator; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:_checker; 155, argument_list; 155, 156; 156, identifier:response; 157, block; 157, 158; 157, 167; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:print; 161, argument_list; 161, 162; 161, 163; 162, identifier:response; 163, call; 163, 164; 163, 165; 164, identifier:type; 165, argument_list; 165, 166; 166, identifier:response; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:response; 170, call; 170, 171; 170, 172; 171, identifier:input; 172, argument_list; 172, 173; 173, conditional_expression:if; 173, 174; 173, 175; 173, 176; 174, identifier:redo_prompt; 175, identifier:redo_prompt; 176, identifier:prompt; 177, return_statement; 177, 178; 178, identifier:response
def get_input(prompt, check, *, redo_prompt=None, repeat_prompt=False): """ Ask the user to input something on the terminal level, check their response and ask again if they didn't answer correctly """ if isinstance(check, str): check = (check,) to_join = [] for item in check: if item: to_join.append(str(item)) else: to_join.append("''") prompt += " [{}]: ".format('/'.join(to_join)) if repeat_prompt: redo_prompt = prompt elif not redo_prompt: redo_prompt = "Incorrect input, please choose from {}: " \ "".format(str(check)) if callable(check): def _checker(r): return check(r) elif isinstance(check, tuple): def _checker(r): return r in check else: raise ValueError(RESPONSES_ERROR.format(type(check))) response = input(prompt) while not _checker(response): print(response, type(response)) response = input(redo_prompt if redo_prompt else prompt) return response
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:install_build_requires; 3, parameters; 3, 4; 4, identifier:pkg_targets; 5, block; 5, 6; 5, 8; 5, 47; 5, 107; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 15; 9, function_name:pip_install; 10, parameters; 10, 11; 10, 12; 11, identifier:pkg_name; 12, default_parameter; 12, 13; 12, 14; 13, identifier:pkg_vers; 14, None; 15, block; 15, 16; 15, 27; 15, 31; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:pkg_name_version; 19, conditional_expression:if; 19, 20; 19, 25; 19, 26; 20, binary_operator:%; 20, 21; 20, 22; 21, string:'%s==%s'; 22, tuple; 22, 23; 22, 24; 23, identifier:pkg_name; 24, identifier:pkg_vers; 25, identifier:pkg_vers; 26, identifier:pkg_name; 27, print_statement; 27, 28; 28, binary_operator:%; 28, 29; 28, 30; 29, string:'[WARNING] %s not found, attempting to install using a raw "pip install" call!'; 30, identifier:pkg_name_version; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 46; 33, attribute; 33, 34; 33, 45; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:subprocess; 37, identifier:Popen; 38, argument_list; 38, 39; 38, 42; 39, binary_operator:%; 39, 40; 39, 41; 40, string:'pip install %s'; 41, identifier:pkg_name_version; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:shell; 44, True; 45, identifier:communicate; 46, argument_list; 47, function_definition; 47, 48; 47, 49; 47, 51; 48, function_name:get_pkg_info; 49, parameters; 49, 50; 50, identifier:pkg; 51, block; 51, 52; 51, 54; 51, 62; 51, 103; 52, expression_statement; 52, 53; 53, comment; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, pattern_list; 56, 57; 56, 58; 57, identifier:pkg_name; 58, identifier:pkg_vers; 59, expression_list; 59, 60; 59, 61; 60, None; 61, None; 62, if_statement; 62, 63; 62, 66; 62, 78; 63, comparison_operator:in; 63, 64; 63, 65; 64, string:'=='; 65, identifier:pkg; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 72; 69, pattern_list; 69, 70; 69, 71; 70, identifier:pkg_name; 71, identifier:pkg_vers; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:pkg; 75, identifier:split; 76, argument_list; 76, 77; 77, string:'=='; 78, else_clause; 78, 79; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:pkg_name; 83, subscript; 83, 84; 83, 102; 84, call; 84, 85; 84, 100; 85, attribute; 85, 86; 85, 99; 86, call; 86, 87; 86, 96; 87, attribute; 87, 88; 87, 95; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:pkg; 91, identifier:replace; 92, argument_list; 92, 93; 92, 94; 93, string:'>'; 94, string:''; 95, identifier:replace; 96, argument_list; 96, 97; 96, 98; 97, string:'<'; 98, string:''; 99, identifier:split; 100, argument_list; 100, 101; 101, string:'='; 102, integer:0; 103, return_statement; 103, 104; 104, expression_list; 104, 105; 104, 106; 105, identifier:pkg_name; 106, identifier:pkg_vers; 107, for_statement; 107, 108; 107, 109; 107, 110; 108, identifier:pkg; 109, identifier:pkg_targets; 110, block; 110, 111; 110, 120; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, pattern_list; 113, 114; 113, 115; 114, identifier:pkg_name; 115, identifier:pkg_vers; 116, call; 116, 117; 116, 118; 117, identifier:get_pkg_info; 118, argument_list; 118, 119; 119, identifier:pkg; 120, try_statement; 120, 121; 120, 169; 121, block; 121, 122; 121, 133; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:pkg_name_version; 125, conditional_expression:if; 125, 126; 125, 131; 125, 132; 126, binary_operator:%; 126, 127; 126, 128; 127, string:'%s==%s'; 128, tuple; 128, 129; 128, 130; 129, identifier:pkg_name; 130, identifier:pkg_vers; 131, identifier:pkg_vers; 132, identifier:pkg_name; 133, if_statement; 133, 134; 133, 135; 133, 160; 134, identifier:pkg_vers; 135, block; 135, 136; 135, 149; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:version; 139, call; 139, 140; 139, 141; 140, identifier:getattr; 141, argument_list; 141, 142; 141, 148; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:importlib; 145, identifier:import_module; 146, argument_list; 146, 147; 147, identifier:pkg_name; 148, string:'__version__'; 149, if_statement; 149, 150; 149, 153; 150, comparison_operator:!=; 150, 151; 150, 152; 151, identifier:version; 152, identifier:pkg_vers; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:pip_install; 157, argument_list; 157, 158; 157, 159; 158, identifier:pkg_name; 159, identifier:pkg_vers; 160, else_clause; 160, 161; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:importlib; 166, identifier:import_module; 167, argument_list; 167, 168; 168, identifier:pkg_name; 169, except_clause; 169, 170; 169, 171; 170, identifier:ImportError; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:pip_install; 175, argument_list; 175, 176; 175, 177; 176, identifier:pkg_name; 177, identifier:pkg_vers
def install_build_requires(pkg_targets): """Iterate through build_requires list and pip install if package is not present accounting for version""" def pip_install(pkg_name, pkg_vers=None): pkg_name_version = '%s==%s' % (pkg_name, pkg_vers) if pkg_vers else pkg_name print '[WARNING] %s not found, attempting to install using a raw "pip install" call!' % pkg_name_version subprocess.Popen('pip install %s' % pkg_name_version, shell=True).communicate() def get_pkg_info(pkg): """Get package name and version given a build_requires element""" pkg_name, pkg_vers = None, None if '==' in pkg: pkg_name, pkg_vers = pkg.split('==') else: pkg_name = pkg.replace('>', '').replace('<', '').split('=')[0] return pkg_name, pkg_vers for pkg in pkg_targets: pkg_name, pkg_vers = get_pkg_info(pkg) try: pkg_name_version = '%s==%s' % (pkg_name, pkg_vers) if pkg_vers else pkg_name if pkg_vers: version = getattr(importlib.import_module(pkg_name), '__version__') if version != pkg_vers: pip_install(pkg_name, pkg_vers) else: importlib.import_module(pkg_name) except ImportError: pip_install(pkg_name, pkg_vers)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:PackagePublishUI; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:package; 5, identifier:type; 6, identifier:visibility; 7, block; 7, 8; 7, 10; 7, 11; 7, 24; 7, 37; 7, 99; 7, 100; 7, 121; 7, 252; 7, 277; 7, 278; 7, 282; 7, 324; 7, 345; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:linux_lst; 14, dictionary; 14, 15; 15, pair; 15, 16; 15, 17; 16, string:'L'; 17, dictionary; 17, 18; 17, 21; 18, pair; 18, 19; 18, 20; 19, string:'selected'; 20, False; 21, pair; 21, 22; 21, 23; 22, string:'Description'; 23, string:'All Linux'; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:windows_lst; 27, dictionary; 27, 28; 28, pair; 28, 29; 28, 30; 29, string:'W'; 30, dictionary; 30, 31; 30, 34; 31, pair; 31, 32; 31, 33; 32, string:'selected'; 33, False; 34, pair; 34, 35; 34, 36; 35, string:'Description'; 36, string:'All Windows'; 37, for_statement; 37, 38; 37, 39; 37, 48; 38, identifier:r; 39, call; 39, 40; 39, 47; 40, attribute; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:clc; 44, identifier:v1; 45, identifier:Server; 46, identifier:GetTemplates; 47, argument_list; 48, block; 48, 49; 48, 55; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 54; 51, subscript; 51, 52; 51, 53; 52, identifier:r; 53, string:'selected'; 54, False; 55, if_statement; 55, 56; 55, 65; 55, 77; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:re; 59, identifier:search; 60, argument_list; 60, 61; 60, 62; 61, string:"Windows"; 62, subscript; 62, 63; 62, 64; 63, identifier:r; 64, string:'Description'; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 76; 68, subscript; 68, 69; 68, 70; 69, identifier:windows_lst; 70, call; 70, 71; 70, 72; 71, identifier:str; 72, argument_list; 72, 73; 73, subscript; 73, 74; 73, 75; 74, identifier:r; 75, string:'OperatingSystem'; 76, identifier:r; 77, elif_clause; 77, 78; 77, 87; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:re; 81, identifier:search; 82, argument_list; 82, 83; 82, 84; 83, string:"CentOS|RedHat|Ubuntu"; 84, subscript; 84, 85; 84, 86; 85, identifier:r; 86, string:'Description'; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 98; 90, subscript; 90, 91; 90, 92; 91, identifier:linux_lst; 92, call; 92, 93; 92, 94; 93, identifier:str; 94, argument_list; 94, 95; 95, subscript; 95, 96; 95, 97; 96, identifier:r; 97, string:'OperatingSystem'; 98, identifier:r; 99, comment; 100, if_statement; 100, 101; 100, 106; 101, comparison_operator:==; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:os; 104, identifier:name; 105, string:'posix'; 106, block; 106, 107; 106, 115; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:scr; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:curses; 113, identifier:initscr; 114, argument_list; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:curses; 119, identifier:cbreak; 120, argument_list; 121, while_statement; 121, 122; 121, 123; 122, True; 123, block; 123, 124; 123, 154; 124, if_statement; 124, 125; 124, 130; 124, 142; 125, comparison_operator:==; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:os; 128, identifier:name; 129, string:'posix'; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:c; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:Blueprint; 137, identifier:_DrawPublishPackageUIPosix; 138, argument_list; 138, 139; 138, 140; 138, 141; 139, identifier:scr; 140, identifier:linux_lst; 141, identifier:windows_lst; 142, else_clause; 142, 143; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:c; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:Blueprint; 150, identifier:_DrawPublishPackageUI; 151, argument_list; 151, 152; 151, 153; 152, identifier:linux_lst; 153, identifier:windows_lst; 154, if_statement; 154, 155; 154, 162; 154, 164; 154, 190; 154, 216; 154, 234; 155, comparison_operator:==; 155, 156; 155, 161; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:c; 159, identifier:lower; 160, argument_list; 161, string:'q'; 162, block; 162, 163; 163, break_statement; 164, elif_clause; 164, 165; 164, 172; 165, comparison_operator:==; 165, 166; 165, 171; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:c; 169, identifier:lower; 170, argument_list; 171, string:'l'; 172, block; 172, 173; 173, for_statement; 173, 174; 173, 175; 173, 176; 174, identifier:l; 175, identifier:linux_lst; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 184; 179, subscript; 179, 180; 179, 183; 180, subscript; 180, 181; 180, 182; 181, identifier:linux_lst; 182, identifier:l; 183, string:'selected'; 184, not_operator; 184, 185; 185, subscript; 185, 186; 185, 189; 186, subscript; 186, 187; 186, 188; 187, identifier:linux_lst; 188, identifier:l; 189, string:'selected'; 190, elif_clause; 190, 191; 190, 198; 191, comparison_operator:==; 191, 192; 191, 197; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:c; 195, identifier:lower; 196, argument_list; 197, string:'w'; 198, block; 198, 199; 199, for_statement; 199, 200; 199, 201; 199, 202; 200, identifier:l; 201, identifier:windows_lst; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 210; 205, subscript; 205, 206; 205, 209; 206, subscript; 206, 207; 206, 208; 207, identifier:windows_lst; 208, identifier:l; 209, string:'selected'; 210, not_operator; 210, 211; 211, subscript; 211, 212; 211, 215; 212, subscript; 212, 213; 212, 214; 213, identifier:windows_lst; 214, identifier:l; 215, string:'selected'; 216, elif_clause; 216, 217; 216, 220; 217, comparison_operator:in; 217, 218; 217, 219; 218, identifier:c; 219, identifier:linux_lst; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 228; 223, subscript; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:linux_lst; 226, identifier:c; 227, string:'selected'; 228, not_operator; 228, 229; 229, subscript; 229, 230; 229, 233; 230, subscript; 230, 231; 230, 232; 231, identifier:linux_lst; 232, identifier:c; 233, string:'selected'; 234, elif_clause; 234, 235; 234, 238; 235, comparison_operator:in; 235, 236; 235, 237; 236, identifier:c; 237, identifier:windows_lst; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 246; 241, subscript; 241, 242; 241, 245; 242, subscript; 242, 243; 242, 244; 243, identifier:windows_lst; 244, identifier:c; 245, string:'selected'; 246, not_operator; 246, 247; 247, subscript; 247, 248; 247, 251; 248, subscript; 248, 249; 248, 250; 249, identifier:windows_lst; 250, identifier:c; 251, string:'selected'; 252, if_statement; 252, 253; 252, 258; 253, comparison_operator:==; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:os; 256, identifier:name; 257, string:'posix'; 258, block; 258, 259; 258, 265; 258, 271; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:curses; 263, identifier:nocbreak; 264, argument_list; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:curses; 269, identifier:echo; 270, argument_list; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:curses; 275, identifier:endwin; 276, argument_list; 277, comment; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:ids; 281, list:[]; 282, for_statement; 282, 283; 282, 284; 282, 302; 283, identifier:l; 284, call; 284, 285; 284, 301; 285, attribute; 285, 286; 285, 300; 286, call; 286, 287; 286, 288; 287, identifier:dict; 288, argument_list; 288, 289; 289, binary_operator:+; 289, 290; 289, 295; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:linux_lst; 293, identifier:items; 294, argument_list; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:windows_lst; 298, identifier:items; 299, argument_list; 300, identifier:values; 301, argument_list; 302, block; 302, 303; 303, if_statement; 303, 304; 303, 311; 304, boolean_operator:and; 304, 305; 304, 308; 305, subscript; 305, 306; 305, 307; 306, identifier:l; 307, string:'selected'; 308, comparison_operator:in; 308, 309; 308, 310; 309, string:'OperatingSystem'; 310, identifier:l; 311, block; 311, 312; 312, expression_statement; 312, 313; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:ids; 316, identifier:append; 317, argument_list; 317, 318; 318, call; 318, 319; 318, 320; 319, identifier:str; 320, argument_list; 320, 321; 321, subscript; 321, 322; 321, 323; 322, identifier:l; 323, string:'OperatingSystem'; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 333; 326, attribute; 326, 327; 326, 332; 327, attribute; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:clc; 330, identifier:v1; 331, identifier:output; 332, identifier:Status; 333, argument_list; 333, 334; 333, 335; 333, 336; 334, string:'SUCCESS'; 335, integer:2; 336, binary_operator:%; 336, 337; 336, 338; 337, string:'Selected operating system IDs: %s'; 338, parenthesized_expression; 338, 339; 339, call; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, string:" "; 342, identifier:join; 343, argument_list; 343, 344; 344, identifier:ids; 345, return_statement; 345, 346; 346, parenthesized_expression; 346, 347; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:Blueprint; 350, identifier:PackagePublish; 351, argument_list; 351, 352; 351, 353; 351, 354; 351, 355; 352, identifier:package; 353, identifier:type; 354, identifier:visibility; 355, identifier:ids
def PackagePublishUI(package,type,visibility): """Publishes a Blueprint Package for use within the Blueprint Designer after interactive OS selection. Interactive selection of one or more operating systems by name. :param package: path to zip file containing package.manifest and supporting scripts :param classification: package type (System, Script, Software) :param visibility: package visibility filter (Public, Private, Shared) """ # fetch OS list linux_lst = {'L': {'selected': False, 'Description': 'All Linux'}} windows_lst = {'W': {'selected': False, 'Description': 'All Windows'}} for r in clc.v1.Server.GetTemplates(): r['selected'] = False if re.search("Windows",r['Description']): windows_lst[str(r['OperatingSystem'])] = r elif re.search("CentOS|RedHat|Ubuntu",r['Description']): linux_lst[str(r['OperatingSystem'])] = r # Get selections if os.name=='posix': scr = curses.initscr() curses.cbreak(); while True: if os.name=='posix': c = Blueprint._DrawPublishPackageUIPosix(scr,linux_lst,windows_lst) else: c = Blueprint._DrawPublishPackageUI(linux_lst,windows_lst) if c.lower() == 'q': break elif c.lower() == 'l': for l in linux_lst: linux_lst[l]['selected'] = not linux_lst[l]['selected'] elif c.lower() == 'w': for l in windows_lst: windows_lst[l]['selected'] = not windows_lst[l]['selected'] elif c in linux_lst: linux_lst[c]['selected'] = not linux_lst[c]['selected'] elif c in windows_lst: windows_lst[c]['selected'] = not windows_lst[c]['selected'] if os.name=='posix': curses.nocbreak(); curses.echo(); curses.endwin() # Extract selections ids = [] for l in dict(linux_lst.items()+windows_lst.items()).values(): if l['selected'] and 'OperatingSystem' in l: ids.append(str(l['OperatingSystem'])) clc.v1.output.Status('SUCCESS',2,'Selected operating system IDs: %s' % (" ".join(ids))) return(Blueprint.PackagePublish(package,type,visibility,ids))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_construct_select_query; 3, parameters; 3, 4; 4, dictionary_splat_pattern; 4, 5; 5, identifier:filter_definition; 6, block; 6, 7; 6, 9; 6, 18; 6, 28; 6, 38; 6, 48; 6, 60; 6, 111; 6, 230; 6, 239; 6, 254; 6, 269; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:table_name; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:filter_definition; 15, identifier:pop; 16, argument_list; 16, 17; 17, string:'table'; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:distinct; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:filter_definition; 24, identifier:pop; 25, argument_list; 25, 26; 25, 27; 26, string:'distinct'; 27, False; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:select_count; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:filter_definition; 34, identifier:pop; 35, argument_list; 35, 36; 35, 37; 36, string:'count'; 37, False; 38, if_statement; 38, 39; 38, 42; 39, boolean_operator:and; 39, 40; 39, 41; 40, identifier:distinct; 41, identifier:select_count; 42, block; 42, 43; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:UnsupportedDefinitionError; 46, argument_list; 46, 47; 47, string:'SELECT (DISTINCT ...) is not supported'; 48, if_statement; 48, 49; 48, 54; 49, boolean_operator:and; 49, 50; 49, 51; 50, identifier:select_count; 51, comparison_operator:in; 51, 52; 51, 53; 52, string:'select'; 53, identifier:filter_definition; 54, block; 54, 55; 55, raise_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:UnsupportedDefinitionError; 58, argument_list; 58, 59; 59, string:'SELECT COUNT(columns) is not supported'; 60, if_statement; 60, 61; 60, 64; 61, comparison_operator:in; 61, 62; 61, 63; 62, string:'joins'; 63, identifier:filter_definition; 64, block; 64, 65; 64, 74; 64, 89; 64, 95; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:join_definitions; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:filter_definition; 71, identifier:pop; 72, argument_list; 72, 73; 73, string:'joins'; 74, if_statement; 74, 75; 74, 83; 75, not_operator; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:isinstance; 78, argument_list; 78, 79; 78, 80; 79, identifier:join_definitions; 80, tuple; 80, 81; 80, 82; 81, identifier:tuple; 82, identifier:list; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:join_definitions; 87, tuple; 87, 88; 88, identifier:join_definitions; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:filter_definition; 93, string:'joins'; 94, list:[]; 95, for_statement; 95, 96; 95, 97; 95, 98; 96, identifier:join_def; 97, identifier:join_definitions; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:filter_definition; 104, string:'joins'; 105, identifier:append; 106, argument_list; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:_expand_join; 109, argument_list; 109, 110; 110, identifier:join_def; 111, if_statement; 111, 112; 111, 115; 112, comparison_operator:in; 112, 113; 112, 114; 113, string:'where'; 114, identifier:filter_definition; 115, block; 115, 116; 116, for_statement; 116, 117; 116, 120; 116, 127; 117, pattern_list; 117, 118; 117, 119; 118, identifier:key; 119, identifier:value; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:filter_definition; 124, string:'where'; 125, identifier:items; 126, argument_list; 127, block; 127, 128; 128, if_statement; 128, 129; 128, 133; 128, 134; 128, 174; 129, call; 129, 130; 129, 131; 130, identifier:is_filter_query; 131, argument_list; 131, 132; 132, identifier:value; 133, comment; 134, block; 134, 135; 134, 144; 134, 154; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:sub_query; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:value; 141, identifier:pop; 142, argument_list; 142, 143; 143, identifier:DEFAULT_FILTER_KEY; 144, if_statement; 144, 145; 144, 146; 145, identifier:value; 146, block; 146, 147; 147, raise_statement; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:ParsingInputError; 150, argument_list; 150, 151; 151, binary_operator:%; 151, 152; 151, 153; 152, string:"Unknown keys for sub-query provided: %s"; 153, identifier:value; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 161; 156, subscript; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:filter_definition; 159, string:'where'; 160, identifier:key; 161, call; 161, 162; 161, 163; 162, identifier:mosql_raw; 163, argument_list; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, string:'( {} )'; 167, identifier:format; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:_construct_select_query; 171, argument_list; 171, 172; 172, dictionary_splat; 172, 173; 173, identifier:sub_query; 174, elif_clause; 174, 175; 174, 198; 174, 199; 175, boolean_operator:and; 175, 176; 175, 188; 176, boolean_operator:and; 176, 177; 176, 182; 177, call; 177, 178; 177, 179; 178, identifier:isinstance; 179, argument_list; 179, 180; 179, 181; 180, identifier:value; 181, identifier:str; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:value; 185, identifier:startswith; 186, argument_list; 186, 187; 187, string:'$'; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:QUERY_REFERENCE; 191, identifier:fullmatch; 192, argument_list; 192, 193; 193, subscript; 193, 194; 193, 195; 194, identifier:value; 195, slice; 195, 196; 195, 197; 196, integer:1; 197, colon; 198, comment; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 207; 202, subscript; 202, 203; 202, 206; 203, subscript; 203, 204; 203, 205; 204, identifier:filter_definition; 205, string:'where'; 206, identifier:key; 207, call; 207, 208; 207, 209; 208, identifier:mosql_raw; 209, argument_list; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, string:'"{}"'; 213, identifier:format; 214, argument_list; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, string:'"."'; 218, identifier:join; 219, argument_list; 219, 220; 220, call; 220, 221; 220, 228; 221, attribute; 221, 222; 221, 227; 222, subscript; 222, 223; 222, 224; 223, identifier:value; 224, slice; 224, 225; 224, 226; 225, integer:1; 226, colon; 227, identifier:split; 228, argument_list; 228, 229; 229, string:'.'; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:raw_select; 233, call; 233, 234; 233, 235; 234, identifier:select; 235, argument_list; 235, 236; 235, 237; 236, identifier:table_name; 237, dictionary_splat; 237, 238; 238, identifier:filter_definition; 239, if_statement; 239, 240; 239, 241; 239, 242; 240, identifier:distinct; 241, comment; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:raw_select; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:raw_select; 249, identifier:replace; 250, argument_list; 250, 251; 250, 252; 250, 253; 251, string:'SELECT'; 252, string:'SELECT DISTINCT'; 253, integer:1; 254, if_statement; 254, 255; 254, 256; 254, 257; 255, identifier:select_count; 256, comment; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:raw_select; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:raw_select; 264, identifier:replace; 265, argument_list; 265, 266; 265, 267; 265, 268; 266, string:'SELECT *'; 267, string:'SELECT COUNT(*)'; 268, integer:1; 269, return_statement; 269, 270; 270, identifier:raw_select
def _construct_select_query(**filter_definition): """Return SELECT statement that will be used as a filter. :param filter_definition: definition of a filter that should be used for SELECT construction :return: """ table_name = filter_definition.pop('table') distinct = filter_definition.pop('distinct', False) select_count = filter_definition.pop('count', False) if distinct and select_count: raise UnsupportedDefinitionError('SELECT (DISTINCT ...) is not supported') if select_count and 'select' in filter_definition: raise UnsupportedDefinitionError('SELECT COUNT(columns) is not supported') if 'joins' in filter_definition: join_definitions = filter_definition.pop('joins') if not isinstance(join_definitions, (tuple, list)): join_definitions = (join_definitions,) filter_definition['joins'] = [] for join_def in join_definitions: filter_definition['joins'].append(_expand_join(join_def)) if 'where' in filter_definition: for key, value in filter_definition['where'].items(): if is_filter_query(value): # We can do it recursively here sub_query = value.pop(DEFAULT_FILTER_KEY) if value: raise ParsingInputError("Unknown keys for sub-query provided: %s" % value) filter_definition['where'][key] = mosql_raw('( {} )'.format(_construct_select_query(**sub_query))) elif isinstance(value, str) and value.startswith('$') and QUERY_REFERENCE.fullmatch(value[1:]): # Make sure we construct correct query with escaped table name and escaped column for sub-queries filter_definition['where'][key] = mosql_raw('"{}"'.format('"."'.join(value[1:].split('.')))) raw_select = select(table_name, **filter_definition) if distinct: # Note that we want to limit replace to the current SELECT, not affect nested ones raw_select = raw_select.replace('SELECT', 'SELECT DISTINCT', 1) if select_count: # Note that we want to limit replace to the current SELECT, not affect nested ones raw_select = raw_select.replace('SELECT *', 'SELECT COUNT(*)', 1) return raw_select
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:encode; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:df; 5, default_parameter; 5, 6; 5, 7; 6, identifier:encoding; 7, string:'utf8'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:verbosity; 10, integer:1; 11, block; 11, 12; 11, 14; 11, 41; 11, 42; 11, 318; 11, 327; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 18; 14, 19; 15, comparison_operator:>; 15, 16; 15, 17; 16, identifier:verbosity; 17, integer:0; 18, comment; 19, block; 19, 20; 19, 35; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:pbar; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:progressbar; 26, identifier:ProgressBar; 27, argument_list; 27, 28; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:maxval; 30, subscript; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:df; 33, identifier:shape; 34, integer:1; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:pbar; 39, identifier:start; 40, argument_list; 41, comment; 42, for_statement; 42, 43; 42, 46; 42, 52; 43, pattern_list; 43, 44; 43, 45; 44, identifier:colnum; 45, identifier:col; 46, call; 46, 47; 46, 48; 47, identifier:enumerate; 48, argument_list; 48, 49; 49, attribute; 49, 50; 49, 51; 50, identifier:df; 51, identifier:columns; 52, block; 52, 53; 53, if_statement; 53, 54; 53, 63; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 60; 57, subscript; 57, 58; 57, 59; 58, identifier:df; 59, identifier:col; 60, attribute; 60, 61; 60, 62; 61, identifier:pd; 62, identifier:Series; 63, block; 63, 64; 63, 74; 63, 312; 63, 313; 63, 314; 63, 315; 63, 316; 63, 317; 64, if_statement; 64, 65; 64, 66; 65, identifier:verbosity; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:pbar; 71, identifier:update; 72, argument_list; 72, 73; 73, identifier:colnum; 74, if_statement; 74, 75; 74, 114; 75, boolean_operator:and; 75, 76; 75, 101; 76, comparison_operator:in; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:df; 80, identifier:col; 81, identifier:dtype; 82, tuple; 82, 83; 82, 89; 82, 95; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:np; 86, identifier:dtype; 87, argument_list; 87, 88; 88, string:'object'; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:np; 92, identifier:dtype; 93, argument_list; 93, 94; 94, string:'U'; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:np; 98, identifier:dtype; 99, argument_list; 99, 100; 100, string:'S'; 101, call; 101, 102; 101, 103; 102, identifier:any; 103, generator_expression; 103, 104; 103, 109; 104, call; 104, 105; 104, 106; 105, identifier:isinstance; 106, argument_list; 106, 107; 106, 108; 107, identifier:obj; 108, identifier:basestring; 109, for_in_clause; 109, 110; 109, 111; 110, identifier:obj; 111, subscript; 111, 112; 111, 113; 112, identifier:df; 113, identifier:col; 114, block; 114, 115; 114, 134; 114, 144; 114, 305; 114, 311; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:strmask; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:np; 121, identifier:array; 122, argument_list; 122, 123; 123, list_comprehension; 123, 124; 123, 129; 124, call; 124, 125; 124, 126; 125, identifier:isinstance; 126, argument_list; 126, 127; 126, 128; 127, identifier:obj; 128, identifier:basestring; 129, for_in_clause; 129, 130; 129, 131; 130, identifier:obj; 131, subscript; 131, 132; 131, 133; 132, identifier:df; 133, identifier:col; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:series; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:df; 141, identifier:col; 142, identifier:copy; 143, argument_list; 144, try_statement; 144, 145; 144, 168; 144, 202; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, subscript; 148, 149; 148, 150; 149, identifier:series; 150, identifier:strmask; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:np; 155, identifier:char; 156, identifier:encode; 157, argument_list; 157, 158; 158, call; 158, 159; 158, 166; 159, attribute; 159, 160; 159, 165; 160, attribute; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:series; 163, identifier:strmask; 164, identifier:values; 165, identifier:astype; 166, argument_list; 166, 167; 167, string:'U'; 168, except_clause; 168, 169; 168, 170; 169, identifier:TypeError; 170, block; 170, 171; 170, 201; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:print; 174, argument_list; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, string:"Unable to convert {} elements starting at position {} in column {}"; 178, identifier:format; 179, argument_list; 179, 180; 179, 184; 179, 200; 180, call; 180, 181; 180, 182; 181, identifier:sum; 182, argument_list; 182, 183; 183, identifier:strmask; 184, subscript; 184, 185; 184, 197; 185, list_comprehension; 185, 186; 185, 187; 185, 195; 186, identifier:i; 187, for_in_clause; 187, 188; 187, 191; 188, pattern_list; 188, 189; 188, 190; 189, identifier:i; 190, identifier:b; 191, call; 191, 192; 191, 193; 192, identifier:enumerate; 193, argument_list; 193, 194; 194, identifier:strmask; 195, if_clause; 195, 196; 196, identifier:b; 197, slice; 197, 198; 197, 199; 198, colon; 199, integer:1; 200, identifier:col; 201, raise_statement; 202, except_clause; 202, 203; 202, 206; 203, tuple; 203, 204; 203, 205; 204, identifier:UnicodeDecodeError; 205, identifier:UnicodeEncodeError; 206, block; 206, 207; 207, try_statement; 207, 208; 207, 231; 207, 232; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:series; 213, identifier:strmask; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:np; 217, identifier:array; 218, argument_list; 218, 219; 219, list_comprehension; 219, 220; 219, 226; 220, call; 220, 221; 220, 222; 221, identifier:eval; 222, argument_list; 222, 223; 222, 224; 222, 225; 223, identifier:s; 224, dictionary; 225, dictionary; 226, for_in_clause; 226, 227; 226, 228; 227, identifier:s; 228, subscript; 228, 229; 228, 230; 229, identifier:series; 230, identifier:strmask; 231, comment; 232, except_clause; 232, 233; 232, 237; 233, tuple; 233, 234; 233, 235; 233, 236; 234, identifier:SyntaxError; 235, identifier:UnicodeDecodeError; 236, identifier:UnicodeEncodeError; 237, block; 237, 238; 237, 242; 237, 288; 237, 289; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:newseries; 241, list:[]; 242, for_statement; 242, 243; 242, 244; 242, 247; 243, identifier:s; 244, subscript; 244, 245; 244, 246; 245, identifier:series; 246, identifier:strmask; 247, block; 247, 248; 248, try_statement; 248, 249; 248, 260; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, augmented_assignment:+=; 251, 252; 251, 253; 252, identifier:newseries; 253, list:[s.encode('utf8')]; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:s; 257, identifier:encode; 258, argument_list; 258, 259; 259, string:'utf8'; 260, except_clause; 260, 261; 261, block; 261, 262; 261, 279; 261, 280; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 265; 264, identifier:print; 265, argument_list; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, string:u'Had trouble encoding {} so used repr to turn it into {}'; 269, identifier:format; 270, argument_list; 270, 271; 270, 272; 271, identifier:s; 272, call; 272, 273; 272, 274; 273, identifier:repr; 274, argument_list; 274, 275; 275, call; 275, 276; 275, 277; 276, identifier:transcode_unicode; 277, argument_list; 277, 278; 278, identifier:s; 279, comment; 280, expression_statement; 280, 281; 281, augmented_assignment:+=; 281, 282; 281, 283; 282, identifier:newseries; 283, list:[transcode_unicode(s)]; 283, 284; 284, call; 284, 285; 284, 286; 285, identifier:transcode_unicode; 286, argument_list; 286, 287; 287, identifier:s; 288, comment; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 294; 291, subscript; 291, 292; 291, 293; 292, identifier:series; 293, identifier:strmask; 294, call; 294, 295; 294, 303; 295, attribute; 295, 296; 295, 302; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:np; 299, identifier:array; 300, argument_list; 300, 301; 301, identifier:newseries; 302, identifier:astype; 303, argument_list; 303, 304; 304, string:'O'; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 310; 307, subscript; 307, 308; 307, 309; 308, identifier:df; 309, identifier:col; 310, identifier:series; 311, comment; 312, comment; 313, comment; 314, comment; 315, comment; 316, comment; 317, comment; 318, if_statement; 318, 319; 318, 320; 319, identifier:verbosity; 320, block; 320, 321; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:pbar; 325, identifier:finish; 326, argument_list; 327, return_statement; 327, 328; 328, identifier:df
def encode(df, encoding='utf8', verbosity=1): """If you try to encode each element individually with python, this would take days!""" if verbosity > 0: # pbar_i = 0 pbar = progressbar.ProgressBar(maxval=df.shape[1]) pbar.start() # encode strings as UTF-8 so they'll work in python2 and python3 for colnum, col in enumerate(df.columns): if isinstance(df[col], pd.Series): if verbosity: pbar.update(colnum) if df[col].dtype in (np.dtype('object'), np.dtype('U'), np.dtype('S')) and any(isinstance(obj, basestring) for obj in df[col]): strmask = np.array([isinstance(obj, basestring) for obj in df[col]]) series = df[col].copy() try: series[strmask] = np.char.encode(series[strmask].values.astype('U')) except TypeError: print("Unable to convert {} elements starting at position {} in column {}".format( sum(strmask), [i for i, b in enumerate(strmask) if b][:1], col)) raise except (UnicodeDecodeError, UnicodeEncodeError): try: series[strmask] = np.array([eval(s, {}, {}) for s in series[strmask]]) # FIXME: do something different for unicode and decode errors except (SyntaxError, UnicodeDecodeError, UnicodeEncodeError): newseries = [] for s in series[strmask]: try: newseries += [s.encode('utf8')] except: print(u'Had trouble encoding {} so used repr to turn it into {}'.format(s, repr(transcode_unicode(s)))) # strip all unicode chars are convert to ASCII str newseries += [transcode_unicode(s)] # for dtype('U'): UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 207: ordinal not in r series[strmask] = np.array(newseries).astype('O') df[col] = series # df[col] = np.array([x.encode('utf8') if isinstance(x, unicode) else x for x in df[col]]) # WARNING: this takes DAYS for only 100k tweets! # series = df[col].copy() # for i, value in series.iteritems(): # if isinstance(value, basestring): # series[i] = str(value.encode(encoding)) # df[col] = series if verbosity: pbar.finish() return df
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:data_worker; 3, parameters; 3, 4; 4, dictionary_splat_pattern; 4, 5; 5, identifier:kwargs; 6, block; 6, 7; 6, 9; 6, 183; 6, 187; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 9, 176; 10, comparison_operator:is; 10, 11; 10, 12; 11, identifier:kwargs; 12, None; 13, block; 13, 14; 13, 32; 13, 50; 13, 68; 13, 96; 13, 139; 14, if_statement; 14, 15; 14, 18; 14, 25; 15, comparison_operator:in; 15, 16; 15, 17; 16, string:"function"; 17, identifier:kwargs; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:function; 22, subscript; 22, 23; 22, 24; 23, identifier:kwargs; 24, string:"function"; 25, else_clause; 25, 26; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:Exception; 30, argument_list; 30, 31; 31, string:"Invalid arguments, no function specified"; 32, if_statement; 32, 33; 32, 36; 32, 43; 33, comparison_operator:in; 33, 34; 33, 35; 34, string:"input"; 35, identifier:kwargs; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:input_queue; 40, subscript; 40, 41; 40, 42; 41, identifier:kwargs; 42, string:"input"; 43, else_clause; 43, 44; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:Exception; 48, argument_list; 48, 49; 49, string:"Invalid Arguments, no input queue"; 50, if_statement; 50, 51; 50, 54; 50, 61; 51, comparison_operator:in; 51, 52; 51, 53; 52, string:"output"; 53, identifier:kwargs; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:output_map; 58, subscript; 58, 59; 58, 60; 59, identifier:kwargs; 60, string:"output"; 61, else_clause; 61, 62; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:Exception; 66, argument_list; 66, 67; 67, string:"Invalid Arguments, no output map"; 68, if_statement; 68, 69; 68, 72; 68, 82; 69, comparison_operator:in; 69, 70; 69, 71; 70, string:"token"; 71, identifier:kwargs; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:argsdict; 76, dictionary; 76, 77; 77, pair; 77, 78; 77, 79; 78, string:"quandl_token"; 79, subscript; 79, 80; 79, 81; 80, identifier:kwargs; 81, string:"token"; 82, else_clause; 82, 83; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 90; 85, comparison_operator:in; 85, 86; 85, 87; 86, string:"Quandl"; 87, attribute; 87, 88; 87, 89; 88, identifier:function; 89, identifier:__module__; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:Exception; 94, argument_list; 94, 95; 95, string:"Invalid Arguments, no Quandl token"; 96, if_statement; 96, 97; 96, 105; 96, 125; 97, comparison_operator:in; 97, 98; 97, 104; 98, parenthesized_expression; 98, 99; 99, boolean_operator:and; 99, 100; 99, 103; 100, boolean_operator:and; 100, 101; 100, 102; 101, string:"source"; 102, string:"begin"; 103, string:"end"; 104, identifier:kwargs; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:argsdict; 109, dictionary; 109, 110; 109, 115; 109, 120; 110, pair; 110, 111; 110, 112; 111, string:"data_source"; 112, subscript; 112, 113; 112, 114; 113, identifier:kwargs; 114, string:"source"; 115, pair; 115, 116; 115, 117; 116, string:"begin"; 117, subscript; 117, 118; 117, 119; 118, identifier:kwargs; 119, string:"begin"; 120, pair; 120, 121; 120, 122; 121, string:"end"; 122, subscript; 122, 123; 122, 124; 123, identifier:kwargs; 124, string:"end"; 125, else_clause; 125, 126; 126, block; 126, 127; 127, if_statement; 127, 128; 127, 133; 128, comparison_operator:in; 128, 129; 128, 130; 129, string:"pandas.io.data"; 130, attribute; 130, 131; 130, 132; 131, identifier:function; 132, identifier:__module__; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:Exception; 137, argument_list; 137, 138; 138, string:"Invalid Arguments, no pandas data source specified"; 139, if_statement; 139, 140; 139, 152; 139, 162; 140, boolean_operator:and; 140, 141; 140, 145; 141, parenthesized_expression; 141, 142; 142, comparison_operator:in; 142, 143; 142, 144; 143, string:"source"; 144, identifier:kwargs; 145, parenthesized_expression; 145, 146; 146, comparison_operator:not; 146, 147; 146, 151; 147, parenthesized_expression; 147, 148; 148, boolean_operator:and; 148, 149; 148, 150; 149, string:"begin"; 150, string:"end"; 151, identifier:kwargs; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:argsdict; 156, dictionary; 156, 157; 157, pair; 157, 158; 157, 159; 158, string:"data_source"; 159, subscript; 159, 160; 159, 161; 160, identifier:kwargs; 161, string:"source"; 162, else_clause; 162, 163; 163, block; 163, 164; 164, if_statement; 164, 165; 164, 170; 165, comparison_operator:in; 165, 166; 165, 167; 166, string:"pandas.io.data"; 167, attribute; 167, 168; 167, 169; 168, identifier:function; 169, identifier:__module__; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:Exception; 174, argument_list; 174, 175; 175, string:"Invalid Arguments, no pandas data source specified"; 176, else_clause; 176, 177; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 181; 180, identifier:Exception; 181, argument_list; 181, 182; 182, string:"Invalid Arguments"; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:retries; 186, integer:5; 187, while_statement; 187, 188; 187, 194; 188, not_operator; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:input_queue; 192, identifier:empty; 193, argument_list; 194, block; 194, 195; 194, 203; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:data_key; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:input_queue; 201, identifier:get; 202, argument_list; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:get_data; 206, argument_list; 206, 207; 206, 208; 206, 209; 206, 210; 206, 211; 207, identifier:function; 208, identifier:data_key; 209, identifier:output_map; 210, identifier:retries; 211, identifier:argsdict
def data_worker(**kwargs): """ Function to be spawned concurrently, consume data keys from input queue, and push the resulting dataframes to output map """ if kwargs is not None: if "function" in kwargs: function = kwargs["function"] else: Exception("Invalid arguments, no function specified") if "input" in kwargs: input_queue = kwargs["input"] else: Exception("Invalid Arguments, no input queue") if "output" in kwargs: output_map = kwargs["output"] else: Exception("Invalid Arguments, no output map") if "token" in kwargs: argsdict = {"quandl_token": kwargs["token"]} else: if "Quandl" in function.__module__: Exception("Invalid Arguments, no Quandl token") if ("source" and "begin" and "end") in kwargs: argsdict = {"data_source": kwargs["source"], "begin": kwargs["begin"], "end": kwargs["end"]} else: if "pandas.io.data" in function.__module__: Exception("Invalid Arguments, no pandas data source specified") if ("source" in kwargs) and (("begin" and "end") not in kwargs): argsdict = {"data_source": kwargs["source"]} else: if "pandas.io.data" in function.__module__: Exception("Invalid Arguments, no pandas data source specified") else: Exception("Invalid Arguments") retries = 5 while not input_queue.empty(): data_key = input_queue.get() get_data(function, data_key, output_map, retries, argsdict)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:makeproperty; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:ns; 5, default_parameter; 5, 6; 5, 7; 6, identifier:cls; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:name; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:docstring; 13, string:''; 14, default_parameter; 14, 15; 14, 16; 15, identifier:descendant; 16, True; 17, block; 17, 18; 17, 20; 17, 115; 17, 254; 17, 321; 18, expression_statement; 18, 19; 19, comment; 20, function_definition; 20, 21; 20, 22; 20, 24; 21, function_name:get_property; 22, parameters; 22, 23; 23, identifier:self; 24, block; 24, 25; 24, 27; 24, 52; 24, 68; 25, expression_statement; 25, 26; 26, comment; 27, if_statement; 27, 28; 27, 31; 27, 40; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:cls; 30, None; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:xpath; 35, binary_operator:%; 35, 36; 35, 37; 36, string:'%s:%s'; 37, tuple; 37, 38; 37, 39; 38, identifier:ns; 39, identifier:name; 40, else_clause; 40, 41; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:xpath; 45, binary_operator:%; 45, 46; 45, 47; 46, string:'%s:%s'; 47, tuple; 47, 48; 47, 49; 48, identifier:ns; 49, attribute; 49, 50; 49, 51; 50, identifier:cls; 51, identifier:__name__; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:xpath; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:_node; 60, identifier:xpath; 61, argument_list; 61, 62; 61, 63; 62, identifier:xpath; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:namespaces; 65, attribute; 65, 66; 65, 67; 66, identifier:SLDNode; 67, identifier:_nsmap; 68, if_statement; 68, 69; 68, 75; 68, 111; 69, comparison_operator:==; 69, 70; 69, 74; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, identifier:xpath; 74, integer:1; 75, block; 75, 76; 76, if_statement; 76, 77; 76, 80; 76, 87; 77, comparison_operator:is; 77, 78; 77, 79; 78, identifier:cls; 79, None; 80, block; 80, 81; 81, return_statement; 81, 82; 82, attribute; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:xpath; 85, integer:0; 86, identifier:text; 87, else_clause; 87, 88; 88, block; 88, 89; 88, 98; 88, 109; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:elem; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:cls; 95, identifier:__new__; 96, argument_list; 96, 97; 97, identifier:cls; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:cls; 102, identifier:__init__; 103, argument_list; 103, 104; 103, 105; 103, 106; 104, identifier:elem; 105, identifier:self; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:descendant; 108, identifier:descendant; 109, return_statement; 109, 110; 110, identifier:elem; 111, else_clause; 111, 112; 112, block; 112, 113; 113, return_statement; 113, 114; 114, None; 115, function_definition; 115, 116; 115, 117; 115, 120; 116, function_name:set_property; 117, parameters; 117, 118; 117, 119; 118, identifier:self; 119, identifier:value; 120, block; 120, 121; 120, 123; 120, 148; 120, 164; 121, expression_statement; 121, 122; 122, comment; 123, if_statement; 123, 124; 123, 127; 123, 136; 124, comparison_operator:is; 124, 125; 124, 126; 125, identifier:cls; 126, None; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:xpath; 131, binary_operator:%; 131, 132; 131, 133; 132, string:'%s:%s'; 133, tuple; 133, 134; 133, 135; 134, identifier:ns; 135, identifier:name; 136, else_clause; 136, 137; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:xpath; 141, binary_operator:%; 141, 142; 141, 143; 142, string:'%s:%s'; 143, tuple; 143, 144; 143, 145; 144, identifier:ns; 145, attribute; 145, 146; 145, 147; 146, identifier:cls; 147, identifier:__name__; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:xpath; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_node; 156, identifier:xpath; 157, argument_list; 157, 158; 157, 159; 158, identifier:xpath; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:namespaces; 161, attribute; 161, 162; 161, 163; 162, identifier:SLDNode; 163, identifier:_nsmap; 164, if_statement; 164, 165; 164, 171; 164, 195; 165, comparison_operator:==; 165, 166; 165, 170; 166, call; 166, 167; 166, 168; 167, identifier:len; 168, argument_list; 168, 169; 169, identifier:xpath; 170, integer:1; 171, block; 171, 172; 172, if_statement; 172, 173; 172, 176; 172, 185; 173, comparison_operator:is; 173, 174; 173, 175; 174, identifier:cls; 175, None; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 184; 179, attribute; 179, 180; 179, 183; 180, subscript; 180, 181; 180, 182; 181, identifier:xpath; 182, integer:0; 183, identifier:text; 184, identifier:value; 185, else_clause; 185, 186; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, subscript; 189, 190; 189, 191; 190, identifier:xpath; 191, integer:0; 192, attribute; 192, 193; 192, 194; 193, identifier:value; 194, identifier:_node; 195, else_clause; 195, 196; 196, block; 196, 197; 197, if_statement; 197, 198; 197, 201; 197, 241; 198, comparison_operator:is; 198, 199; 198, 200; 199, identifier:cls; 200, None; 201, block; 201, 202; 201, 226; 201, 232; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:elem; 205, call; 205, 206; 205, 211; 206, attribute; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:_node; 210, identifier:makeelement; 211, argument_list; 211, 212; 211, 221; 212, binary_operator:%; 212, 213; 212, 214; 213, string:'{%s}%s'; 214, tuple; 214, 215; 214, 220; 215, subscript; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:SLDNode; 218, identifier:_nsmap; 219, identifier:ns; 220, identifier:name; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:nsmap; 223, attribute; 223, 224; 223, 225; 224, identifier:SLDNode; 225, identifier:_nsmap; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:elem; 230, identifier:text; 231, identifier:value; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 239; 234, attribute; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:self; 237, identifier:_node; 238, identifier:append; 239, argument_list; 239, 240; 240, identifier:elem; 241, else_clause; 241, 242; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 250; 245, attribute; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:_node; 249, identifier:append; 250, argument_list; 250, 251; 251, attribute; 251, 252; 251, 253; 252, identifier:value; 253, identifier:_node; 254, function_definition; 254, 255; 254, 256; 254, 258; 255, function_name:del_property; 256, parameters; 256, 257; 257, identifier:self; 258, block; 258, 259; 258, 261; 258, 286; 258, 302; 259, expression_statement; 259, 260; 260, comment; 261, if_statement; 261, 262; 261, 265; 261, 274; 262, comparison_operator:is; 262, 263; 262, 264; 263, identifier:cls; 264, None; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:xpath; 269, binary_operator:%; 269, 270; 269, 271; 270, string:'%s:%s'; 271, tuple; 271, 272; 271, 273; 272, identifier:ns; 273, identifier:name; 274, else_clause; 274, 275; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:xpath; 279, binary_operator:%; 279, 280; 279, 281; 280, string:'%s:%s'; 281, tuple; 281, 282; 281, 283; 282, identifier:ns; 283, attribute; 283, 284; 283, 285; 284, identifier:cls; 285, identifier:__name__; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:xpath; 289, call; 289, 290; 289, 295; 290, attribute; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:self; 293, identifier:_node; 294, identifier:xpath; 295, argument_list; 295, 296; 295, 297; 296, identifier:xpath; 297, keyword_argument; 297, 298; 297, 299; 298, identifier:namespaces; 299, attribute; 299, 300; 299, 301; 300, identifier:SLDNode; 301, identifier:_nsmap; 302, if_statement; 302, 303; 302, 309; 303, comparison_operator:==; 303, 304; 303, 308; 304, call; 304, 305; 304, 306; 305, identifier:len; 306, argument_list; 306, 307; 307, identifier:xpath; 308, integer:1; 309, block; 309, 310; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 317; 312, attribute; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:self; 315, identifier:_node; 316, identifier:remove; 317, argument_list; 317, 318; 318, subscript; 318, 319; 318, 320; 319, identifier:xpath; 320, integer:0; 321, return_statement; 321, 322; 322, call; 322, 323; 322, 324; 323, identifier:property; 324, argument_list; 324, 325; 324, 326; 324, 327; 324, 328; 325, identifier:get_property; 326, identifier:set_property; 327, identifier:del_property; 328, identifier:docstring
def makeproperty(ns, cls=None, name=None, docstring='', descendant=True): """ Make a property on an instance of an SLDNode. If cls is omitted, the property is assumed to be a text node, with no corresponding class object. If name is omitted, the property is assumed to be a complex node, with a corresponding class wrapper. @type ns: string @param ns: The namespace of this property's node. @type cls: class @param cls: Optional. The class of the child property. @type name: string @param name: Optional. The name of the child property. @type docstring: string @param docstring: Optional. The docstring to attach to the new property. @type descendant: boolean @param descendant: Does this element descend from the parent, or is it a sibling? @rtype: property attribute @return: A property attribute for this named property. """ def get_property(self): """ A generic property getter. """ if cls is None: xpath = '%s:%s' % (ns, name) else: xpath = '%s:%s' % (ns, cls.__name__) xpath = self._node.xpath(xpath, namespaces=SLDNode._nsmap) if len(xpath) == 1: if cls is None: return xpath[0].text else: elem = cls.__new__(cls) cls.__init__(elem, self, descendant=descendant) return elem else: return None def set_property(self, value): """ A generic property setter. """ if cls is None: xpath = '%s:%s' % (ns, name) else: xpath = '%s:%s' % (ns, cls.__name__) xpath = self._node.xpath(xpath, namespaces=SLDNode._nsmap) if len(xpath) == 1: if cls is None: xpath[0].text = value else: xpath[0] = value._node else: if cls is None: elem = self._node.makeelement('{%s}%s' % (SLDNode._nsmap[ns], name), nsmap=SLDNode._nsmap) elem.text = value self._node.append(elem) else: self._node.append(value._node) def del_property(self): """ A generic property deleter. """ if cls is None: xpath = '%s:%s' % (ns, name) else: xpath = '%s:%s' % (ns, cls.__name__) xpath = self._node.xpath(xpath, namespaces=SLDNode._nsmap) if len(xpath) == 1: self._node.remove(xpath[0]) return property(get_property, set_property, del_property, docstring)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_parse_from_string; 3, parameters; 3, 4; 4, identifier:string_input; 5, block; 5, 6; 5, 8; 5, 9; 5, 25; 5, 50; 5, 153; 5, 170; 5, 229; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:read_lines; 12, call; 12, 13; 12, 14; 13, identifier:list; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:filter; 17, argument_list; 17, 18; 17, 19; 18, None; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:string_input; 22, identifier:split; 23, argument_list; 23, 24; 24, string:'\n'; 25, if_statement; 25, 26; 25, 34; 25, 44; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, subscript; 28, 29; 28, 30; 29, identifier:read_lines; 30, integer:0; 31, identifier:startswith; 32, argument_list; 32, 33; 33, string:'#'; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:comment; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:read_lines; 41, identifier:pop; 42, argument_list; 42, 43; 43, integer:0; 44, else_clause; 44, 45; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:comment; 49, string:''; 50, if_statement; 50, 51; 50, 57; 50, 58; 50, 74; 51, comparison_operator:>; 51, 52; 51, 56; 52, call; 52, 53; 52, 54; 53, identifier:len; 54, argument_list; 54, 55; 55, identifier:read_lines; 56, integer:1; 57, comment; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:order; 62, call; 62, 63; 62, 64; 63, identifier:int; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:math; 68, identifier:sqrt; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, identifier:read_lines; 74, else_clause; 74, 75; 74, 76; 75, comment; 76, block; 76, 77; 76, 99; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:order; 80, call; 80, 81; 80, 82; 81, identifier:int; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:math; 86, identifier:sqrt; 87, argument_list; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:math; 91, identifier:sqrt; 92, argument_list; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, subscript; 96, 97; 96, 98; 97, identifier:read_lines; 98, integer:0; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:read_lines; 102, call; 102, 103; 102, 104; 103, identifier:filter; 104, argument_list; 104, 105; 104, 117; 105, lambda; 105, 106; 105, 108; 106, lambda_parameters; 106, 107; 107, identifier:x; 108, comparison_operator:==; 108, 109; 108, 113; 109, call; 109, 110; 109, 111; 110, identifier:len; 111, argument_list; 111, 112; 112, identifier:x; 113, parenthesized_expression; 113, 114; 114, binary_operator:**; 114, 115; 114, 116; 115, identifier:order; 116, integer:2; 117, list_comprehension; 117, 118; 117, 131; 117, 144; 118, subscript; 118, 119; 118, 122; 119, subscript; 119, 120; 119, 121; 120, identifier:read_lines; 121, integer:0; 122, slice; 122, 123; 122, 124; 122, 125; 123, identifier:i; 124, colon; 125, parenthesized_expression; 125, 126; 126, binary_operator:+; 126, 127; 126, 128; 127, identifier:i; 128, binary_operator:**; 128, 129; 128, 130; 129, identifier:order; 130, integer:2; 131, for_in_clause; 131, 132; 131, 133; 132, identifier:i; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:utils; 136, identifier:range_; 137, argument_list; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:len; 140, argument_list; 140, 141; 141, subscript; 141, 142; 141, 143; 142, identifier:read_lines; 143, integer:0; 144, if_clause; 144, 145; 145, comparison_operator:==; 145, 146; 145, 152; 146, binary_operator:%; 146, 147; 146, 148; 147, identifier:i; 148, parenthesized_expression; 148, 149; 149, binary_operator:**; 149, 150; 149, 151; 150, identifier:order; 151, integer:2; 152, integer:0; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:matrix; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:utils; 159, identifier:get_list_of_lists; 160, argument_list; 160, 161; 160, 164; 160, 167; 161, binary_operator:**; 161, 162; 161, 163; 162, identifier:order; 163, integer:2; 164, binary_operator:**; 164, 165; 164, 166; 165, identifier:order; 166, integer:2; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:fill_with; 169, integer:0; 170, for_statement; 170, 171; 170, 174; 170, 178; 171, pattern_list; 171, 172; 171, 173; 172, identifier:i; 173, identifier:line; 174, call; 174, 175; 174, 176; 175, identifier:enumerate; 176, argument_list; 176, 177; 177, identifier:read_lines; 178, block; 178, 179; 178, 187; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:line; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:line; 185, identifier:strip; 186, argument_list; 187, for_statement; 187, 188; 187, 191; 187, 195; 188, pattern_list; 188, 189; 188, 190; 189, identifier:j; 190, identifier:value; 191, call; 191, 192; 191, 193; 192, identifier:enumerate; 193, argument_list; 193, 194; 194, identifier:line; 195, block; 195, 196; 196, if_statement; 196, 197; 196, 207; 196, 219; 197, boolean_operator:and; 197, 198; 197, 203; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:value; 201, identifier:isdigit; 202, argument_list; 203, call; 203, 204; 203, 205; 204, identifier:int; 205, argument_list; 205, 206; 206, identifier:value; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 215; 210, subscript; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:matrix; 213, identifier:i; 214, identifier:j; 215, call; 215, 216; 215, 217; 216, identifier:int; 217, argument_list; 217, 218; 218, identifier:value; 219, else_clause; 219, 220; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 228; 223, subscript; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:matrix; 226, identifier:i; 227, identifier:j; 228, integer:0; 229, return_statement; 229, 230; 230, expression_list; 230, 231; 230, 232; 230, 233; 231, identifier:order; 232, identifier:comment; 233, identifier:matrix
def _parse_from_string(string_input): """Parses a Sudoku instance from string input. :param string_input: A string containing the Sudoku to parse. :type string_input: str :return: The parsed Sudoku. :rtype: :py:class:`dlxsudoku.sudoku.Sudoku` """ # Check if comment line is present. read_lines = list(filter(None, string_input.split('\n'))) if read_lines[0].startswith('#'): comment = read_lines.pop(0) else: comment = '' if len(read_lines) > 1: # Assume that Sudoku is defined over several rows. order = int(math.sqrt(len(read_lines))) else: # Sudoku is defined on one line. order = int(math.sqrt(math.sqrt(len(read_lines[0])))) read_lines = filter(lambda x: len(x) == (order ** 2), [read_lines[0][i:(i + order ** 2)] for i in utils.range_(len(read_lines[0])) if i % (order ** 2) == 0]) matrix = utils.get_list_of_lists( order ** 2, order ** 2, fill_with=0) for i, line in enumerate(read_lines): line = line.strip() for j, value in enumerate(line): if value.isdigit() and int(value): matrix[i][j] = int(value) else: matrix[i][j] = 0 return order, comment, matrix
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:solve; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:verbose; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:allow_brute_force; 10, True; 11, block; 11, 12; 11, 14; 11, 162; 12, expression_statement; 12, 13; 13, comment; 14, while_statement; 14, 15; 14, 19; 14, 20; 15, not_operator; 15, 16; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:is_solved; 19, comment; 20, block; 20, 21; 20, 27; 20, 28; 20, 44; 20, 45; 20, 46; 20, 47; 21, expression_statement; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:_update; 26, argument_list; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:singles_found; 31, boolean_operator:or; 31, 32; 31, 39; 32, boolean_operator:or; 32, 33; 32, 34; 33, False; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_fill_naked_singles; 38, argument_list; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_fill_hidden_singles; 43, argument_list; 44, comment; 45, comment; 46, comment; 47, if_statement; 47, 48; 47, 50; 48, not_operator; 48, 49; 49, identifier:singles_found; 50, block; 50, 51; 51, if_statement; 51, 52; 51, 53; 51, 150; 52, identifier:allow_brute_force; 53, block; 53, 54; 53, 58; 53, 140; 53, 149; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:solution; 57, None; 58, try_statement; 58, 59; 58, 96; 58, 120; 58, 131; 59, block; 59, 60; 59, 74; 59, 82; 59, 89; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:dlxs; 63, call; 63, 64; 63, 65; 64, identifier:DancingLinksSolver; 65, argument_list; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:copy; 69, identifier:deepcopy; 70, argument_list; 70, 71; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:_matrix; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:solutions; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:dlxs; 80, identifier:solve; 81, argument_list; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:solution; 85, call; 85, 86; 85, 87; 86, identifier:next; 87, argument_list; 87, 88; 88, identifier:solutions; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:more_solutions; 92, call; 92, 93; 92, 94; 93, identifier:next; 94, argument_list; 94, 95; 95, identifier:solutions; 96, except_clause; 96, 97; 96, 101; 97, as_pattern; 97, 98; 97, 99; 98, identifier:StopIteration; 99, as_pattern_target; 99, 100; 100, identifier:e; 101, block; 101, 102; 102, if_statement; 102, 103; 102, 106; 102, 113; 103, comparison_operator:is; 103, 104; 103, 105; 104, identifier:solution; 105, None; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_matrix; 112, identifier:solution; 113, else_clause; 113, 114; 114, block; 114, 115; 115, raise_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:SudokuHasNoSolutionError; 118, argument_list; 118, 119; 119, string:"Dancing Links solver could not find any solution."; 120, except_clause; 120, 121; 120, 125; 121, as_pattern; 121, 122; 121, 123; 122, identifier:Exception; 123, as_pattern_target; 123, 124; 124, identifier:e; 125, block; 125, 126; 126, raise_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:SudokuHasNoSolutionError; 129, argument_list; 129, 130; 130, string:"Brute Force method failed."; 131, else_clause; 131, 132; 131, 133; 131, 134; 132, comment; 133, comment; 134, block; 134, 135; 135, raise_statement; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:SudokuHasMultipleSolutionsError; 138, argument_list; 138, 139; 139, string:"This Sudoku has multiple solutions!"; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:solution_steps; 146, identifier:append; 147, argument_list; 147, 148; 148, string:"BRUTE FORCE - Dancing Links"; 149, break_statement; 150, else_clause; 150, 151; 151, block; 151, 152; 151, 157; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:print; 155, argument_list; 155, 156; 156, identifier:self; 157, raise_statement; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:SudokuTooDifficultError; 160, argument_list; 160, 161; 161, string:"This Sudoku requires more advanced methods!"; 162, if_statement; 162, 163; 162, 164; 163, identifier:verbose; 164, block; 164, 165; 164, 181; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:print; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, string:"Sudoku solved in {0} iterations!\n{1}"; 172, identifier:format; 173, argument_list; 173, 174; 173, 180; 174, call; 174, 175; 174, 176; 175, identifier:len; 176, argument_list; 176, 177; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:solution_steps; 180, identifier:self; 181, for_statement; 181, 182; 181, 183; 181, 186; 182, identifier:step; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:solution_steps; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:print; 190, argument_list; 190, 191; 191, identifier:step
def solve(self, verbose=False, allow_brute_force=True): """Solve the Sudoku. :param verbose: If the steps used for solving the Sudoku should be printed. Default is `False` :type verbose: bool :param allow_brute_force: If Dancing Links Brute Force method should be used if necessary. Default is `True` :type allow_brute_force: bool """ while not self.is_solved: # Update possibles arrays. self._update() # See if any position can be singled out. singles_found = False or self._fill_naked_singles() or self._fill_hidden_singles() # If singles_found is False, then no new uniquely defined cells were found # and this solver cannot solve the Sudoku. We either use brute force or throw an error. # Else, if singles_found is True, run another iteration to see if new singles have shown up. if not singles_found: if allow_brute_force: solution = None try: dlxs = DancingLinksSolver(copy.deepcopy(self._matrix)) solutions = dlxs.solve() solution = next(solutions) more_solutions = next(solutions) except StopIteration as e: if solution is not None: self._matrix = solution else: raise SudokuHasNoSolutionError("Dancing Links solver could not find any solution.") except Exception as e: raise SudokuHasNoSolutionError("Brute Force method failed.") else: # We end up here if the second `next(solutions)` works, # i.e. if multiple solutions exist. raise SudokuHasMultipleSolutionsError("This Sudoku has multiple solutions!") self.solution_steps.append("BRUTE FORCE - Dancing Links") break else: print(self) raise SudokuTooDifficultError("This Sudoku requires more advanced methods!") if verbose: print("Sudoku solved in {0} iterations!\n{1}".format(len(self.solution_steps), self)) for step in self.solution_steps: print(step)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_fill_hidden_singles; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 355; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 18; 9, identifier:i; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:utils; 13, identifier:range_; 14, argument_list; 14, 15; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:side; 18, block; 18, 19; 18, 32; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:box_i; 22, binary_operator:*; 22, 23; 22, 29; 23, parenthesized_expression; 23, 24; 24, binary_operator://; 24, 25; 24, 26; 25, identifier:i; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:order; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:order; 32, for_statement; 32, 33; 32, 34; 32, 42; 33, identifier:j; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:utils; 37, identifier:range_; 38, argument_list; 38, 39; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:side; 42, block; 42, 43; 42, 56; 42, 57; 42, 67; 42, 68; 42, 78; 42, 110; 42, 156; 42, 157; 42, 167; 42, 199; 42, 245; 42, 246; 42, 256; 42, 309; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:box_j; 46, binary_operator:*; 46, 47; 46, 53; 47, parenthesized_expression; 47, 48; 48, binary_operator://; 48, 49; 48, 50; 49, identifier:j; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:order; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:order; 56, comment; 57, if_statement; 57, 58; 57, 65; 58, comparison_operator:>; 58, 59; 58, 64; 59, subscript; 59, 60; 59, 63; 60, subscript; 60, 61; 60, 62; 61, identifier:self; 62, identifier:i; 63, identifier:j; 64, integer:0; 65, block; 65, 66; 66, continue_statement; 67, comment; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:p; 71, subscript; 71, 72; 71, 77; 72, subscript; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:_possibles; 76, identifier:i; 77, identifier:j; 78, for_statement; 78, 79; 78, 80; 78, 88; 79, identifier:k; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:utils; 83, identifier:range_; 84, argument_list; 84, 85; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:side; 88, block; 88, 89; 88, 95; 89, if_statement; 89, 90; 89, 93; 90, comparison_operator:==; 90, 91; 90, 92; 91, identifier:k; 92, identifier:j; 93, block; 93, 94; 94, continue_statement; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:p; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:p; 101, identifier:difference; 102, argument_list; 102, 103; 103, subscript; 103, 104; 103, 109; 104, subscript; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:_possibles; 108, identifier:i; 109, identifier:k; 110, if_statement; 110, 111; 110, 117; 110, 118; 111, comparison_operator:==; 111, 112; 111, 116; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, identifier:p; 116, integer:1; 117, comment; 118, block; 118, 119; 118, 132; 118, 154; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:set_cell; 124, argument_list; 124, 125; 124, 126; 124, 127; 125, identifier:i; 126, identifier:j; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:p; 130, identifier:pop; 131, argument_list; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:solution_steps; 138, identifier:append; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:_format_step; 144, argument_list; 144, 145; 144, 146; 144, 149; 145, string:"HIDDEN-ROW"; 146, tuple; 146, 147; 146, 148; 147, identifier:i; 148, identifier:j; 149, subscript; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:self; 152, identifier:i; 153, identifier:j; 154, return_statement; 154, 155; 155, True; 156, comment; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:p; 160, subscript; 160, 161; 160, 166; 161, subscript; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:_possibles; 165, identifier:i; 166, identifier:j; 167, for_statement; 167, 168; 167, 169; 167, 177; 168, identifier:k; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:utils; 172, identifier:range_; 173, argument_list; 173, 174; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:side; 177, block; 177, 178; 177, 184; 178, if_statement; 178, 179; 178, 182; 179, comparison_operator:==; 179, 180; 179, 181; 180, identifier:k; 181, identifier:i; 182, block; 182, 183; 183, continue_statement; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:p; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:p; 190, identifier:difference; 191, argument_list; 191, 192; 192, subscript; 192, 193; 192, 198; 193, subscript; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:_possibles; 197, identifier:k; 198, identifier:j; 199, if_statement; 199, 200; 199, 206; 199, 207; 200, comparison_operator:==; 200, 201; 200, 205; 201, call; 201, 202; 201, 203; 202, identifier:len; 203, argument_list; 203, 204; 204, identifier:p; 205, integer:1; 206, comment; 207, block; 207, 208; 207, 221; 207, 243; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:self; 212, identifier:set_cell; 213, argument_list; 213, 214; 213, 215; 213, 216; 214, identifier:i; 215, identifier:j; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:p; 219, identifier:pop; 220, argument_list; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 228; 223, attribute; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:self; 226, identifier:solution_steps; 227, identifier:append; 228, argument_list; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:_format_step; 233, argument_list; 233, 234; 233, 235; 233, 238; 234, string:"HIDDEN-COL"; 235, tuple; 235, 236; 235, 237; 236, identifier:i; 237, identifier:j; 238, subscript; 238, 239; 238, 242; 239, subscript; 239, 240; 239, 241; 240, identifier:self; 241, identifier:i; 242, identifier:j; 243, return_statement; 243, 244; 244, True; 245, comment; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:p; 249, subscript; 249, 250; 249, 255; 250, subscript; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:self; 253, identifier:_possibles; 254, identifier:i; 255, identifier:j; 256, for_statement; 256, 257; 256, 258; 256, 269; 257, identifier:k; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:utils; 261, identifier:range_; 262, argument_list; 262, 263; 262, 264; 263, identifier:box_i; 264, binary_operator:+; 264, 265; 264, 266; 265, identifier:box_i; 266, attribute; 266, 267; 266, 268; 267, identifier:self; 268, identifier:order; 269, block; 269, 270; 270, for_statement; 270, 271; 270, 272; 270, 283; 271, identifier:kk; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:utils; 275, identifier:range_; 276, argument_list; 276, 277; 276, 278; 277, identifier:box_j; 278, binary_operator:+; 278, 279; 278, 280; 279, identifier:box_j; 280, attribute; 280, 281; 280, 282; 281, identifier:self; 282, identifier:order; 283, block; 283, 284; 283, 294; 284, if_statement; 284, 285; 284, 292; 285, boolean_operator:and; 285, 286; 285, 289; 286, comparison_operator:==; 286, 287; 286, 288; 287, identifier:k; 288, identifier:i; 289, comparison_operator:==; 289, 290; 289, 291; 290, identifier:kk; 291, identifier:j; 292, block; 292, 293; 293, continue_statement; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:p; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:p; 300, identifier:difference; 301, argument_list; 301, 302; 302, subscript; 302, 303; 302, 308; 303, subscript; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:self; 306, identifier:_possibles; 307, identifier:k; 308, identifier:kk; 309, if_statement; 309, 310; 309, 316; 309, 317; 310, comparison_operator:==; 310, 311; 310, 315; 311, call; 311, 312; 311, 313; 312, identifier:len; 313, argument_list; 313, 314; 314, identifier:p; 315, integer:1; 316, comment; 317, block; 317, 318; 317, 331; 317, 353; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:self; 322, identifier:set_cell; 323, argument_list; 323, 324; 323, 325; 323, 326; 324, identifier:i; 325, identifier:j; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:p; 329, identifier:pop; 330, argument_list; 331, expression_statement; 331, 332; 332, call; 332, 333; 332, 338; 333, attribute; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:self; 336, identifier:solution_steps; 337, identifier:append; 338, argument_list; 338, 339; 339, call; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:self; 342, identifier:_format_step; 343, argument_list; 343, 344; 343, 345; 343, 348; 344, string:"HIDDEN-BOX"; 345, tuple; 345, 346; 345, 347; 346, identifier:i; 347, identifier:j; 348, subscript; 348, 349; 348, 352; 349, subscript; 349, 350; 349, 351; 350, identifier:self; 351, identifier:i; 352, identifier:j; 353, return_statement; 353, 354; 354, True; 355, return_statement; 355, 356; 356, False
def _fill_hidden_singles(self): """Look for hidden singles, i.e. cells with only one unique possible value in row, column or box. :return: If any Hidden Single has been found. :rtype: bool """ for i in utils.range_(self.side): box_i = (i // self.order) * self.order for j in utils.range_(self.side): box_j = (j // self.order) * self.order # Skip if this cell is determined already. if self[i][j] > 0: continue # Look for hidden single in rows. p = self._possibles[i][j] for k in utils.range_(self.side): if k == j: continue p = p.difference(self._possibles[i][k]) if len(p) == 1: # Found a hidden single in a row! self.set_cell(i, j, p.pop()) self.solution_steps.append(self._format_step("HIDDEN-ROW", (i, j), self[i][j])) return True # Look for hidden single in columns p = self._possibles[i][j] for k in utils.range_(self.side): if k == i: continue p = p.difference(self._possibles[k][j]) if len(p) == 1: # Found a hidden single in a column! self.set_cell(i, j, p.pop()) self.solution_steps.append(self._format_step("HIDDEN-COL", (i, j), self[i][j])) return True # Look for hidden single in box p = self._possibles[i][j] for k in utils.range_(box_i, box_i + self.order): for kk in utils.range_(box_j, box_j + self.order): if k == i and kk == j: continue p = p.difference(self._possibles[k][kk]) if len(p) == 1: # Found a hidden single in a box! self.set_cell(i, j, p.pop()) self.solution_steps.append(self._format_step("HIDDEN-BOX", (i, j), self[i][j])) return True return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:request; 6, default_parameter; 6, 7; 6, 8; 7, identifier:reverse; 8, False; 9, block; 9, 10; 9, 12; 9, 29; 9, 37; 9, 48; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:field; 15, call; 15, 16; 15, 25; 16, attribute; 16, 17; 16, 24; 17, attribute; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:model; 22, identifier:_meta; 23, identifier:fields; 24, identifier:get; 25, argument_list; 25, 26; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:columns_sort; 29, if_statement; 29, 30; 29, 32; 30, not_operator; 30, 31; 31, identifier:field; 32, block; 32, 33; 33, return_statement; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:collection; 37, if_statement; 37, 38; 37, 39; 38, identifier:reverse; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:field; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:field; 46, identifier:desc; 47, argument_list; 48, return_statement; 48, 49; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:collection; 54, identifier:order_by; 55, argument_list; 55, 56; 56, identifier:field
def sort(self, request, reverse=False): """Sort current collection.""" field = self.model._meta.fields.get(self.columns_sort) if not field: return self.collection if reverse: field = field.desc() return self.collection.order_by(field)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:process_docstring; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:app; 5, identifier:what; 6, identifier:name; 7, identifier:obj; 8, identifier:options; 9, identifier:lines; 10, block; 10, 11; 10, 13; 10, 22; 10, 54; 10, 62; 10, 70; 10, 82; 10, 204; 10, 211; 10, 221; 10, 236; 10, 250; 10, 251; 10, 341; 10, 375; 10, 376; 10, 380; 10, 403; 10, 409; 10, 438; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:aliases; 16, call; 16, 17; 16, 18; 17, identifier:getattr; 18, argument_list; 18, 19; 18, 20; 18, 21; 19, identifier:app; 20, string:"_sigaliases"; 21, None; 22, if_statement; 22, 23; 22, 26; 23, comparison_operator:is; 23, 24; 23, 25; 24, identifier:aliases; 25, None; 26, block; 26, 27; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:==; 28, 29; 28, 30; 29, identifier:what; 30, string:"module"; 31, block; 31, 32; 31, 48; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:aliases; 35, call; 35, 36; 35, 37; 36, identifier:get_aliases; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 47; 39, attribute; 39, 40; 39, 46; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:inspect; 43, identifier:getsource; 44, argument_list; 44, 45; 45, identifier:obj; 46, identifier:splitlines; 47, argument_list; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:app; 52, identifier:_sigaliases; 53, identifier:aliases; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:sig_marker; 57, binary_operator:+; 57, 58; 57, 61; 58, binary_operator:+; 58, 59; 58, 60; 59, string:":"; 60, identifier:SIG_FIELD; 61, string:":"; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:is_class; 65, comparison_operator:in; 65, 66; 65, 67; 66, identifier:what; 67, tuple; 67, 68; 67, 69; 68, string:"class"; 69, string:"exception"; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:signature; 73, call; 73, 74; 73, 75; 74, identifier:extract_signature; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, string:"\n"; 79, identifier:join; 80, argument_list; 80, 81; 81, identifier:lines; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:signature; 85, None; 86, block; 86, 87; 86, 92; 86, 100; 86, 106; 86, 118; 86, 152; 86, 158; 86, 162; 86, 192; 87, if_statement; 87, 88; 87, 90; 88, not_operator; 88, 89; 89, identifier:is_class; 90, block; 90, 91; 91, return_statement; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:init_method; 95, call; 95, 96; 95, 97; 96, identifier:getattr; 97, argument_list; 97, 98; 97, 99; 98, identifier:obj; 99, string:"__init__"; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:init_doc; 103, attribute; 103, 104; 103, 105; 104, identifier:init_method; 105, identifier:__doc__; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:init_lines; 109, subscript; 109, 110; 109, 115; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:init_doc; 113, identifier:splitlines; 114, argument_list; 115, slice; 115, 116; 115, 117; 116, integer:1; 117, colon; 118, if_statement; 118, 119; 118, 125; 119, comparison_operator:>; 119, 120; 119, 124; 120, call; 120, 121; 120, 122; 121, identifier:len; 122, argument_list; 122, 123; 123, identifier:init_lines; 124, integer:1; 125, block; 125, 126; 125, 144; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:init_doc; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:textwrap; 132, identifier:dedent; 133, argument_list; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, string:"\n"; 137, identifier:join; 138, argument_list; 138, 139; 139, subscript; 139, 140; 139, 141; 140, identifier:init_lines; 141, slice; 141, 142; 141, 143; 142, integer:1; 143, colon; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:init_lines; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:init_doc; 150, identifier:splitlines; 151, argument_list; 152, if_statement; 152, 153; 152, 156; 153, comparison_operator:not; 153, 154; 153, 155; 154, identifier:sig_marker; 155, identifier:init_doc; 156, block; 156, 157; 157, return_statement; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:sig_started; 161, False; 162, for_statement; 162, 163; 162, 164; 162, 165; 163, identifier:line; 164, identifier:init_lines; 165, block; 165, 166; 165, 182; 166, if_statement; 166, 167; 166, 177; 167, call; 167, 168; 167, 175; 168, attribute; 168, 169; 168, 174; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:line; 172, identifier:lstrip; 173, argument_list; 174, identifier:startswith; 175, argument_list; 175, 176; 176, identifier:sig_marker; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:sig_started; 181, True; 182, if_statement; 182, 183; 182, 184; 183, identifier:sig_started; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:lines; 189, identifier:append; 190, argument_list; 190, 191; 191, identifier:line; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:signature; 195, call; 195, 196; 195, 197; 196, identifier:extract_signature; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, string:"\n"; 201, identifier:join; 202, argument_list; 202, 203; 203, identifier:lines; 204, if_statement; 204, 205; 204, 206; 205, identifier:is_class; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:obj; 210, identifier:init_method; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 217; 213, pattern_list; 213, 214; 213, 215; 213, 216; 214, identifier:param_types; 215, identifier:rtype; 216, identifier:_; 217, call; 217, 218; 217, 219; 218, identifier:parse_signature; 219, argument_list; 219, 220; 220, identifier:signature; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:param_names; 224, list_comprehension; 224, 225; 224, 226; 225, identifier:p; 226, for_in_clause; 226, 227; 226, 228; 227, identifier:p; 228, attribute; 228, 229; 228, 235; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:inspect; 232, identifier:signature; 233, argument_list; 233, 234; 234, identifier:obj; 235, identifier:parameters; 236, if_statement; 236, 237; 236, 245; 237, boolean_operator:and; 237, 238; 237, 239; 238, identifier:is_class; 239, parenthesized_expression; 239, 240; 240, comparison_operator:==; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:param_names; 243, integer:0; 244, string:"self"; 245, block; 245, 246; 246, delete_statement; 246, 247; 247, subscript; 247, 248; 247, 249; 248, identifier:param_names; 249, integer:0; 250, comment; 251, if_statement; 251, 252; 251, 261; 252, comparison_operator:==; 252, 253; 252, 257; 253, call; 253, 254; 253, 255; 254, identifier:len; 255, argument_list; 255, 256; 256, identifier:param_names; 257, call; 257, 258; 257, 259; 258, identifier:len; 259, argument_list; 259, 260; 260, identifier:param_types; 261, block; 261, 262; 262, for_statement; 262, 263; 262, 266; 262, 271; 263, pattern_list; 263, 264; 263, 265; 264, identifier:name; 265, identifier:type_; 266, call; 266, 267; 266, 268; 267, identifier:zip; 268, argument_list; 268, 269; 268, 270; 269, identifier:param_names; 270, identifier:param_types; 271, block; 271, 272; 271, 281; 271, 290; 271, 307; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:find; 275, binary_operator:%; 275, 276; 275, 277; 276, string:":param %(name)s:"; 277, dictionary; 277, 278; 278, pair; 278, 279; 278, 280; 279, string:"name"; 280, identifier:name; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:alias; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:aliases; 287, identifier:get; 288, argument_list; 288, 289; 289, identifier:type_; 290, if_statement; 290, 291; 290, 294; 291, comparison_operator:is; 291, 292; 291, 293; 292, identifier:alias; 293, None; 294, block; 294, 295; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:type_; 298, binary_operator:%; 298, 299; 298, 300; 299, string:"*%(type)s* :sup:`%(alias)s`"; 300, dictionary; 300, 301; 300, 304; 301, pair; 301, 302; 301, 303; 302, string:"type"; 303, identifier:type_; 304, pair; 304, 305; 304, 306; 305, string:"alias"; 306, identifier:alias; 307, for_statement; 307, 308; 307, 311; 307, 315; 308, pattern_list; 308, 309; 308, 310; 309, identifier:i; 310, identifier:line; 311, call; 311, 312; 311, 313; 312, identifier:enumerate; 313, argument_list; 313, 314; 314, identifier:lines; 315, block; 315, 316; 316, if_statement; 316, 317; 316, 323; 317, call; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:line; 320, identifier:startswith; 321, argument_list; 321, 322; 322, identifier:find; 323, block; 323, 324; 323, 340; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:lines; 328, identifier:insert; 329, argument_list; 329, 330; 329, 331; 330, identifier:i; 331, binary_operator:%; 331, 332; 331, 333; 332, string:":type %(name)s: %(type)s"; 333, dictionary; 333, 334; 333, 337; 334, pair; 334, 335; 334, 336; 335, string:"name"; 336, identifier:name; 337, pair; 337, 338; 337, 339; 338, string:"type"; 339, identifier:type_; 340, break_statement; 341, if_statement; 341, 342; 341, 344; 342, not_operator; 342, 343; 343, identifier:is_class; 344, block; 344, 345; 345, for_statement; 345, 346; 345, 349; 345, 353; 346, pattern_list; 346, 347; 346, 348; 347, identifier:i; 348, identifier:line; 349, call; 349, 350; 349, 351; 350, identifier:enumerate; 351, argument_list; 351, 352; 352, identifier:lines; 353, block; 353, 354; 354, if_statement; 354, 355; 354, 363; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:line; 358, identifier:startswith; 359, argument_list; 359, 360; 360, tuple; 360, 361; 360, 362; 361, string:":return:"; 362, string:":returns:"; 363, block; 363, 364; 363, 374; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:lines; 368, identifier:insert; 369, argument_list; 369, 370; 369, 371; 370, identifier:i; 371, binary_operator:+; 371, 372; 371, 373; 372, string:":rtype: "; 373, identifier:rtype; 374, break_statement; 375, comment; 376, expression_statement; 376, 377; 377, assignment; 377, 378; 377, 379; 378, identifier:sig_start; 379, integer:0; 380, while_statement; 380, 381; 380, 387; 381, comparison_operator:<; 381, 382; 381, 383; 382, identifier:sig_start; 383, call; 383, 384; 383, 385; 384, identifier:len; 385, argument_list; 385, 386; 386, identifier:lines; 387, block; 387, 388; 387, 399; 388, if_statement; 388, 389; 388, 397; 389, call; 389, 390; 389, 395; 390, attribute; 390, 391; 390, 394; 391, subscript; 391, 392; 391, 393; 392, identifier:lines; 393, identifier:sig_start; 394, identifier:startswith; 395, argument_list; 395, 396; 396, identifier:sig_marker; 397, block; 397, 398; 398, break_statement; 399, expression_statement; 399, 400; 400, augmented_assignment:+=; 400, 401; 400, 402; 401, identifier:sig_start; 402, integer:1; 403, expression_statement; 403, 404; 404, assignment; 404, 405; 404, 406; 405, identifier:sig_end; 406, binary_operator:+; 406, 407; 406, 408; 407, identifier:sig_start; 408, integer:1; 409, while_statement; 409, 410; 409, 416; 410, comparison_operator:<; 410, 411; 410, 412; 411, identifier:sig_end; 412, call; 412, 413; 412, 414; 413, identifier:len; 414, argument_list; 414, 415; 415, identifier:lines; 416, block; 416, 417; 416, 434; 417, if_statement; 417, 418; 417, 432; 418, boolean_operator:or; 418, 419; 418, 424; 419, parenthesized_expression; 419, 420; 420, not_operator; 420, 421; 421, subscript; 421, 422; 421, 423; 422, identifier:lines; 423, identifier:sig_end; 424, parenthesized_expression; 424, 425; 425, comparison_operator:!=; 425, 426; 425, 431; 426, subscript; 426, 427; 426, 430; 427, subscript; 427, 428; 427, 429; 428, identifier:lines; 429, identifier:sig_end; 430, integer:0; 431, string:" "; 432, block; 432, 433; 433, break_statement; 434, expression_statement; 434, 435; 435, augmented_assignment:+=; 435, 436; 435, 437; 436, identifier:sig_end; 437, integer:1; 438, for_statement; 438, 439; 438, 440; 438, 448; 439, identifier:i; 440, call; 440, 441; 440, 442; 441, identifier:reversed; 442, argument_list; 442, 443; 443, call; 443, 444; 443, 445; 444, identifier:range; 445, argument_list; 445, 446; 445, 447; 446, identifier:sig_start; 447, identifier:sig_end; 448, block; 448, 449; 449, delete_statement; 449, 450; 450, subscript; 450, 451; 450, 452; 451, identifier:lines; 452, identifier:i
def process_docstring(app, what, name, obj, options, lines): """Modify the docstring before generating documentation. This will insert type declarations for parameters and return type into the docstring, and remove the signature field so that it will be excluded from the generated document. """ aliases = getattr(app, "_sigaliases", None) if aliases is None: if what == "module": aliases = get_aliases(inspect.getsource(obj).splitlines()) app._sigaliases = aliases sig_marker = ":" + SIG_FIELD + ":" is_class = what in ("class", "exception") signature = extract_signature("\n".join(lines)) if signature is None: if not is_class: return init_method = getattr(obj, "__init__") init_doc = init_method.__doc__ init_lines = init_doc.splitlines()[1:] if len(init_lines) > 1: init_doc = textwrap.dedent("\n".join(init_lines[1:])) init_lines = init_doc.splitlines() if sig_marker not in init_doc: return sig_started = False for line in init_lines: if line.lstrip().startswith(sig_marker): sig_started = True if sig_started: lines.append(line) signature = extract_signature("\n".join(lines)) if is_class: obj = init_method param_types, rtype, _ = parse_signature(signature) param_names = [p for p in inspect.signature(obj).parameters] if is_class and (param_names[0] == "self"): del param_names[0] # if something goes wrong, don't insert parameter types if len(param_names) == len(param_types): for name, type_ in zip(param_names, param_types): find = ":param %(name)s:" % {"name": name} alias = aliases.get(type_) if alias is not None: type_ = "*%(type)s* :sup:`%(alias)s`" % {"type": type_, "alias": alias} for i, line in enumerate(lines): if line.startswith(find): lines.insert(i, ":type %(name)s: %(type)s" % {"name": name, "type": type_}) break if not is_class: for i, line in enumerate(lines): if line.startswith((":return:", ":returns:")): lines.insert(i, ":rtype: " + rtype) break # remove the signature field sig_start = 0 while sig_start < len(lines): if lines[sig_start].startswith(sig_marker): break sig_start += 1 sig_end = sig_start + 1 while sig_end < len(lines): if (not lines[sig_end]) or (lines[sig_end][0] != " "): break sig_end += 1 for i in reversed(range(sig_start, sig_end)): del lines[i]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:main; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:argv; 6, None; 7, block; 7, 8; 7, 10; 7, 19; 7, 34; 7, 47; 7, 70; 7, 87; 7, 103; 7, 116; 7, 127; 7, 140; 7, 141; 7, 164; 7, 177; 7, 205; 7, 209; 7, 317; 7, 334; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:parser; 13, call; 13, 14; 13, 15; 14, identifier:ArgumentParser; 15, argument_list; 15, 16; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:prog; 18, string:"pygenstub"; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:parser; 23, identifier:add_argument; 24, argument_list; 24, 25; 24, 26; 24, 29; 25, string:"--version"; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:action; 28, string:"version"; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:version; 31, binary_operator:+; 31, 32; 31, 33; 32, string:"%(prog)s "; 33, identifier:__version__; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:parser; 38, identifier:add_argument; 39, argument_list; 39, 40; 39, 41; 39, 44; 40, string:"files"; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:nargs; 43, string:"*"; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:help; 46, string:"generate stubs for given files"; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:parser; 51, identifier:add_argument; 52, argument_list; 52, 53; 52, 54; 52, 55; 52, 58; 52, 61; 52, 64; 52, 67; 53, string:"-m"; 54, string:"--module"; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:action; 57, string:"append"; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:metavar; 60, string:"MODULE"; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:dest; 63, string:"modules"; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:default; 66, list:[]; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:help; 69, string:"generate stubs for given modules"; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:parser; 74, identifier:add_argument; 75, argument_list; 75, 76; 75, 77; 75, 78; 75, 81; 75, 84; 76, string:"-o"; 77, string:"--output"; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:metavar; 80, string:"PATH"; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:dest; 83, string:"out_dir"; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:help; 86, string:"change the output directory"; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:parser; 91, identifier:add_argument; 92, argument_list; 92, 93; 92, 94; 92, 97; 92, 100; 93, string:"--generic"; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:action; 96, string:"store_true"; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:default; 99, False; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:help; 102, string:"generate generic stubs"; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:parser; 107, identifier:add_argument; 108, argument_list; 108, 109; 108, 110; 108, 113; 109, string:"--debug"; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:action; 112, string:"store_true"; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:help; 115, string:"enable debug messages"; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:argv; 119, conditional_expression:if; 119, 120; 119, 121; 119, 124; 120, identifier:argv; 121, comparison_operator:is; 121, 122; 121, 123; 122, identifier:argv; 123, None; 124, attribute; 124, 125; 124, 126; 125, identifier:sys; 126, identifier:argv; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:arguments; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:parser; 133, identifier:parse_args; 134, argument_list; 134, 135; 135, subscript; 135, 136; 135, 137; 136, identifier:argv; 137, slice; 137, 138; 137, 139; 138, integer:1; 139, colon; 140, comment; 141, if_statement; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:arguments; 144, identifier:debug; 145, block; 145, 146; 145, 157; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:logging; 150, identifier:basicConfig; 151, argument_list; 151, 152; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:level; 154, attribute; 154, 155; 154, 156; 155, identifier:logging; 156, identifier:DEBUG; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:_logger; 161, identifier:debug; 162, argument_list; 162, 163; 163, string:"running in debug mode"; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:out_dir; 167, conditional_expression:if; 167, 168; 167, 171; 167, 176; 168, attribute; 168, 169; 168, 170; 169, identifier:arguments; 170, identifier:out_dir; 171, comparison_operator:is; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:arguments; 174, identifier:out_dir; 175, None; 176, string:""; 177, if_statement; 177, 178; 177, 192; 178, boolean_operator:and; 178, 179; 178, 183; 179, parenthesized_expression; 179, 180; 180, comparison_operator:==; 180, 181; 180, 182; 181, identifier:out_dir; 182, string:""; 183, parenthesized_expression; 183, 184; 184, comparison_operator:>; 184, 185; 184, 191; 185, call; 185, 186; 185, 187; 186, identifier:len; 187, argument_list; 187, 188; 188, attribute; 188, 189; 188, 190; 189, identifier:arguments; 190, identifier:modules; 191, integer:0; 192, block; 192, 193; 192, 198; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 196; 195, identifier:print; 196, argument_list; 196, 197; 197, string:"Output directory must be given when generating stubs for modules."; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:sys; 202, identifier:exit; 203, argument_list; 203, 204; 204, integer:1; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:modules; 208, list:[]; 209, for_statement; 209, 210; 209, 211; 209, 214; 210, identifier:path; 211, attribute; 211, 212; 211, 213; 212, identifier:arguments; 213, identifier:files; 214, block; 214, 215; 214, 241; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:paths; 218, conditional_expression:if; 218, 219; 218, 228; 218, 236; 219, call; 219, 220; 219, 226; 220, attribute; 220, 221; 220, 225; 221, call; 221, 222; 221, 223; 222, identifier:Path; 223, argument_list; 223, 224; 224, identifier:path; 225, identifier:glob; 226, argument_list; 226, 227; 227, string:"**/*.py"; 228, call; 228, 229; 228, 235; 229, attribute; 229, 230; 229, 234; 230, call; 230, 231; 230, 232; 231, identifier:Path; 232, argument_list; 232, 233; 233, identifier:path; 234, identifier:is_dir; 235, argument_list; 236, list:[Path(path)]; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:Path; 239, argument_list; 239, 240; 240, identifier:path; 241, for_statement; 241, 242; 241, 243; 241, 244; 242, identifier:source; 243, identifier:paths; 244, block; 244, 245; 244, 272; 244, 295; 244, 308; 245, if_statement; 245, 246; 245, 259; 246, call; 246, 247; 246, 253; 247, attribute; 247, 248; 247, 252; 248, call; 248, 249; 248, 250; 249, identifier:str; 250, argument_list; 250, 251; 251, identifier:source; 252, identifier:startswith; 253, argument_list; 253, 254; 254, attribute; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:os; 257, identifier:path; 258, identifier:pardir; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:source; 263, call; 263, 264; 263, 271; 264, attribute; 264, 265; 264, 270; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:source; 268, identifier:absolute; 269, argument_list; 270, identifier:resolve; 271, argument_list; 272, if_statement; 272, 273; 272, 283; 273, boolean_operator:and; 273, 274; 273, 278; 274, parenthesized_expression; 274, 275; 275, comparison_operator:!=; 275, 276; 275, 277; 276, identifier:out_dir; 277, string:""; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:source; 281, identifier:is_absolute; 282, argument_list; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:source; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:source; 290, identifier:relative_to; 291, argument_list; 291, 292; 292, attribute; 292, 293; 292, 294; 293, identifier:source; 294, identifier:root; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:destination; 298, call; 298, 299; 298, 300; 299, identifier:Path; 300, argument_list; 300, 301; 300, 302; 301, identifier:out_dir; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:source; 305, identifier:with_suffix; 306, argument_list; 306, 307; 307, string:".pyi"; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:modules; 312, identifier:append; 313, argument_list; 313, 314; 314, tuple; 314, 315; 314, 316; 315, identifier:source; 316, identifier:destination; 317, for_statement; 317, 318; 317, 319; 317, 322; 318, identifier:mod_name; 319, attribute; 319, 320; 319, 321; 320, identifier:arguments; 321, identifier:modules; 322, block; 322, 323; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:modules; 327, identifier:extend; 328, argument_list; 328, 329; 329, call; 329, 330; 329, 331; 330, identifier:get_pkg_paths; 331, argument_list; 331, 332; 331, 333; 332, identifier:mod_name; 333, identifier:out_dir; 334, for_statement; 334, 335; 334, 338; 334, 339; 335, pattern_list; 335, 336; 335, 337; 336, identifier:source; 337, identifier:destination; 338, identifier:modules; 339, block; 339, 340; 339, 349; 339, 369; 339, 402; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:_logger; 344, identifier:info; 345, argument_list; 345, 346; 345, 347; 345, 348; 346, string:"generating stub for %s to path %s"; 347, identifier:source; 348, identifier:destination; 349, with_statement; 349, 350; 349, 360; 350, with_clause; 350, 351; 351, with_item; 351, 352; 352, as_pattern; 352, 353; 352, 358; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:source; 356, identifier:open; 357, argument_list; 358, as_pattern_target; 358, 359; 359, identifier:f; 360, block; 360, 361; 361, expression_statement; 361, 362; 362, assignment; 362, 363; 362, 364; 363, identifier:code; 364, call; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:f; 367, identifier:read; 368, argument_list; 369, try_statement; 369, 370; 369, 383; 370, block; 370, 371; 371, expression_statement; 371, 372; 372, assignment; 372, 373; 372, 374; 373, identifier:stub; 374, call; 374, 375; 374, 376; 375, identifier:get_stub; 376, argument_list; 376, 377; 376, 378; 377, identifier:code; 378, keyword_argument; 378, 379; 378, 380; 379, identifier:generic; 380, attribute; 380, 381; 380, 382; 381, identifier:arguments; 382, identifier:generic; 383, except_clause; 383, 384; 383, 388; 384, as_pattern; 384, 385; 384, 386; 385, identifier:Exception; 386, as_pattern_target; 386, 387; 387, identifier:e; 388, block; 388, 389; 388, 401; 389, expression_statement; 389, 390; 390, call; 390, 391; 390, 392; 391, identifier:print; 392, argument_list; 392, 393; 392, 394; 392, 395; 392, 396; 393, identifier:source; 394, string:"-"; 395, identifier:e; 396, keyword_argument; 396, 397; 396, 398; 397, identifier:file; 398, attribute; 398, 399; 398, 400; 399, identifier:sys; 400, identifier:stderr; 401, continue_statement; 402, if_statement; 402, 403; 402, 406; 403, comparison_operator:!=; 403, 404; 403, 405; 404, identifier:stub; 405, string:""; 406, block; 406, 407; 406, 428; 407, if_statement; 407, 408; 407, 416; 408, not_operator; 408, 409; 409, call; 409, 410; 409, 415; 410, attribute; 410, 411; 410, 414; 411, attribute; 411, 412; 411, 413; 412, identifier:destination; 413, identifier:parent; 414, identifier:exists; 415, argument_list; 416, block; 416, 417; 417, expression_statement; 417, 418; 418, call; 418, 419; 418, 424; 419, attribute; 419, 420; 419, 423; 420, attribute; 420, 421; 420, 422; 421, identifier:destination; 422, identifier:parent; 423, identifier:mkdir; 424, argument_list; 424, 425; 425, keyword_argument; 425, 426; 425, 427; 426, identifier:parents; 427, True; 428, with_statement; 428, 429; 428, 440; 429, with_clause; 429, 430; 430, with_item; 430, 431; 431, as_pattern; 431, 432; 431, 438; 432, call; 432, 433; 432, 436; 433, attribute; 433, 434; 433, 435; 434, identifier:destination; 435, identifier:open; 436, argument_list; 436, 437; 437, string:"w"; 438, as_pattern_target; 438, 439; 439, identifier:f; 440, block; 440, 441; 441, expression_statement; 441, 442; 442, call; 442, 443; 442, 446; 443, attribute; 443, 444; 443, 445; 444, identifier:f; 445, identifier:write; 446, argument_list; 446, 447; 447, binary_operator:+; 447, 448; 447, 453; 448, binary_operator:+; 448, 449; 448, 452; 449, binary_operator:+; 449, 450; 449, 451; 450, string:"# "; 451, identifier:EDIT_WARNING; 452, string:"\n\n"; 453, identifier:stub
def main(argv=None): """Start the command line interface.""" parser = ArgumentParser(prog="pygenstub") parser.add_argument("--version", action="version", version="%(prog)s " + __version__) parser.add_argument("files", nargs="*", help="generate stubs for given files") parser.add_argument( "-m", "--module", action="append", metavar="MODULE", dest="modules", default=[], help="generate stubs for given modules", ) parser.add_argument( "-o", "--output", metavar="PATH", dest="out_dir", help="change the output directory" ) parser.add_argument( "--generic", action="store_true", default=False, help="generate generic stubs" ) parser.add_argument("--debug", action="store_true", help="enable debug messages") argv = argv if argv is not None else sys.argv arguments = parser.parse_args(argv[1:]) # set debug mode if arguments.debug: logging.basicConfig(level=logging.DEBUG) _logger.debug("running in debug mode") out_dir = arguments.out_dir if arguments.out_dir is not None else "" if (out_dir == "") and (len(arguments.modules) > 0): print("Output directory must be given when generating stubs for modules.") sys.exit(1) modules = [] for path in arguments.files: paths = Path(path).glob("**/*.py") if Path(path).is_dir() else [Path(path)] for source in paths: if str(source).startswith(os.path.pardir): source = source.absolute().resolve() if (out_dir != "") and source.is_absolute(): source = source.relative_to(source.root) destination = Path(out_dir, source.with_suffix(".pyi")) modules.append((source, destination)) for mod_name in arguments.modules: modules.extend(get_pkg_paths(mod_name, out_dir)) for source, destination in modules: _logger.info("generating stub for %s to path %s", source, destination) with source.open() as f: code = f.read() try: stub = get_stub(code, generic=arguments.generic) except Exception as e: print(source, "-", e, file=sys.stderr) continue if stub != "": if not destination.parent.exists(): destination.parent.mkdir(parents=True) with destination.open("w") as f: f.write("# " + EDIT_WARNING + "\n\n" + stub)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_code; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 40; 5, 44; 5, 83; 5, 113; 5, 119; 5, 210; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:stub; 11, list:[]; 12, for_statement; 12, 13; 12, 14; 12, 17; 13, identifier:deco; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:decorators; 17, block; 17, 18; 18, if_statement; 18, 19; 18, 30; 19, boolean_operator:or; 19, 20; 19, 24; 20, parenthesized_expression; 20, 21; 21, comparison_operator:in; 21, 22; 21, 23; 22, identifier:deco; 23, identifier:DECORATORS; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:deco; 27, identifier:endswith; 28, argument_list; 28, 29; 29, string:".setter"; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:stub; 35, identifier:append; 36, argument_list; 36, 37; 37, binary_operator:+; 37, 38; 37, 39; 38, string:"@"; 39, identifier:deco; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:parameters; 43, list:[]; 44, for_statement; 44, 45; 44, 49; 44, 52; 45, pattern_list; 45, 46; 45, 47; 45, 48; 46, identifier:name; 47, identifier:type_; 48, identifier:has_default; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:parameters; 52, block; 52, 53; 52, 76; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:decl; 56, binary_operator:%; 56, 57; 56, 58; 57, string:"%(n)s%(t)s%(d)s"; 58, dictionary; 58, 59; 58, 62; 58, 70; 59, pair; 59, 60; 59, 61; 60, string:"n"; 61, identifier:name; 62, pair; 62, 63; 62, 64; 63, string:"t"; 64, conditional_expression:if; 64, 65; 64, 68; 64, 69; 65, binary_operator:+; 65, 66; 65, 67; 66, string:": "; 67, identifier:type_; 68, identifier:type_; 69, string:""; 70, pair; 70, 71; 70, 72; 71, string:"d"; 72, conditional_expression:if; 72, 73; 72, 74; 72, 75; 73, string:" = ..."; 74, identifier:has_default; 75, string:""; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:parameters; 80, identifier:append; 81, argument_list; 81, 82; 82, identifier:decl; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:slots; 86, dictionary; 86, 87; 86, 95; 86, 100; 86, 108; 87, pair; 87, 88; 87, 89; 88, string:"a"; 89, conditional_expression:if; 89, 90; 89, 91; 89, 94; 90, string:"async "; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:_async; 94, string:""; 95, pair; 95, 96; 95, 97; 96, string:"n"; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:name; 100, pair; 100, 101; 100, 102; 101, string:"p"; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, string:", "; 105, identifier:join; 106, argument_list; 106, 107; 107, identifier:parameters; 108, pair; 108, 109; 108, 110; 109, string:"r"; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:rtype; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:prototype; 116, binary_operator:%; 116, 117; 116, 118; 117, string:"%(a)sdef %(n)s(%(p)s) -> %(r)s: ..."; 118, identifier:slots; 119, if_statement; 119, 120; 119, 126; 119, 134; 119, 175; 120, comparison_operator:<=; 120, 121; 120, 125; 121, call; 121, 122; 121, 123; 122, identifier:len; 123, argument_list; 123, 124; 124, identifier:prototype; 125, identifier:LINE_LENGTH_LIMIT; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:stub; 131, identifier:append; 132, argument_list; 132, 133; 133, identifier:prototype; 134, elif_clause; 134, 135; 134, 145; 135, comparison_operator:<=; 135, 136; 135, 144; 136, call; 136, 137; 136, 138; 137, identifier:len; 138, argument_list; 138, 139; 139, binary_operator:+; 139, 140; 139, 141; 140, identifier:INDENT; 141, subscript; 141, 142; 141, 143; 142, identifier:slots; 143, string:"p"; 144, identifier:LINE_LENGTH_LIMIT; 145, block; 145, 146; 145, 155; 145, 166; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:stub; 150, identifier:append; 151, argument_list; 151, 152; 152, binary_operator:%; 152, 153; 152, 154; 153, string:"%(a)sdef %(n)s("; 154, identifier:slots; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:stub; 159, identifier:append; 160, argument_list; 160, 161; 161, binary_operator:+; 161, 162; 161, 163; 162, identifier:INDENT; 163, subscript; 163, 164; 163, 165; 164, identifier:slots; 165, string:"p"; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:stub; 170, identifier:append; 171, argument_list; 171, 172; 172, binary_operator:%; 172, 173; 172, 174; 173, string:") -> %(r)s: ..."; 174, identifier:slots; 175, else_clause; 175, 176; 176, block; 176, 177; 176, 186; 176, 201; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:stub; 181, identifier:append; 182, argument_list; 182, 183; 183, binary_operator:%; 183, 184; 183, 185; 184, string:"%(a)sdef %(n)s("; 185, identifier:slots; 186, for_statement; 186, 187; 186, 188; 186, 189; 187, identifier:param; 188, identifier:parameters; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:stub; 194, identifier:append; 195, argument_list; 195, 196; 196, binary_operator:+; 196, 197; 196, 200; 197, binary_operator:+; 197, 198; 197, 199; 198, identifier:INDENT; 199, identifier:param; 200, string:","; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:stub; 205, identifier:append; 206, argument_list; 206, 207; 207, binary_operator:%; 207, 208; 207, 209; 208, string:") -> %(r)s: ..."; 209, identifier:slots; 210, return_statement; 210, 211; 211, identifier:stub
def get_code(self): """Get the stub code for this function. :sig: () -> List[str] :return: Lines of stub code for this function. """ stub = [] for deco in self.decorators: if (deco in DECORATORS) or deco.endswith(".setter"): stub.append("@" + deco) parameters = [] for name, type_, has_default in self.parameters: decl = "%(n)s%(t)s%(d)s" % { "n": name, "t": ": " + type_ if type_ else "", "d": " = ..." if has_default else "", } parameters.append(decl) slots = { "a": "async " if self._async else "", "n": self.name, "p": ", ".join(parameters), "r": self.rtype, } prototype = "%(a)sdef %(n)s(%(p)s) -> %(r)s: ..." % slots if len(prototype) <= LINE_LENGTH_LIMIT: stub.append(prototype) elif len(INDENT + slots["p"]) <= LINE_LENGTH_LIMIT: stub.append("%(a)sdef %(n)s(" % slots) stub.append(INDENT + slots["p"]) stub.append(") -> %(r)s: ..." % slots) else: stub.append("%(a)sdef %(n)s(" % slots) for param in parameters: stub.append(INDENT + param + ",") stub.append(") -> %(r)s: ..." % slots) return stub
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:query_metric_stats; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:self; 5, identifier:metric_type; 6, default_parameter; 6, 7; 6, 8; 7, identifier:metric_id; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:start; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:end; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:bucketDuration; 17, None; 18, dictionary_splat_pattern; 18, 19; 19, identifier:query_options; 20, block; 20, 21; 20, 23; 20, 53; 20, 83; 20, 113; 20, 162; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:start; 26, None; 27, block; 27, 28; 28, if_statement; 28, 29; 28, 35; 28, 45; 29, comparison_operator:is; 29, 30; 29, 34; 30, call; 30, 31; 30, 32; 31, identifier:type; 32, argument_list; 32, 33; 33, identifier:start; 34, identifier:datetime; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 41; 38, subscript; 38, 39; 38, 40; 39, identifier:query_options; 40, string:'start'; 41, call; 41, 42; 41, 43; 42, identifier:datetime_to_time_millis; 43, argument_list; 43, 44; 44, identifier:start; 45, else_clause; 45, 46; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:query_options; 51, string:'start'; 52, identifier:start; 53, if_statement; 53, 54; 53, 57; 54, comparison_operator:is; 54, 55; 54, 56; 55, identifier:end; 56, None; 57, block; 57, 58; 58, if_statement; 58, 59; 58, 65; 58, 75; 59, comparison_operator:is; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:type; 62, argument_list; 62, 63; 63, identifier:end; 64, identifier:datetime; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:query_options; 70, string:'end'; 71, call; 71, 72; 71, 73; 72, identifier:datetime_to_time_millis; 73, argument_list; 73, 74; 74, identifier:end; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:query_options; 81, string:'end'; 82, identifier:end; 83, if_statement; 83, 84; 83, 87; 84, comparison_operator:is; 84, 85; 84, 86; 85, identifier:bucketDuration; 86, None; 87, block; 87, 88; 88, if_statement; 88, 89; 88, 95; 88, 105; 89, comparison_operator:is; 89, 90; 89, 94; 90, call; 90, 91; 90, 92; 91, identifier:type; 92, argument_list; 92, 93; 93, identifier:bucketDuration; 94, identifier:timedelta; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:query_options; 100, string:'bucketDuration'; 101, call; 101, 102; 101, 103; 102, identifier:timedelta_to_duration; 103, argument_list; 103, 104; 104, identifier:bucketDuration; 105, else_clause; 105, 106; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:query_options; 111, string:'bucketDuration'; 112, identifier:bucketDuration; 113, if_statement; 113, 114; 113, 117; 113, 133; 114, comparison_operator:is; 114, 115; 114, 116; 115, identifier:metric_id; 116, None; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:url; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:_get_metrics_stats_url; 125, argument_list; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:_get_metrics_single_url; 130, argument_list; 130, 131; 130, 132; 131, identifier:metric_type; 132, identifier:metric_id; 133, else_clause; 133, 134; 134, block; 134, 135; 134, 148; 135, if_statement; 135, 136; 135, 142; 136, comparison_operator:<; 136, 137; 136, 141; 137, call; 137, 138; 137, 139; 138, identifier:len; 139, argument_list; 139, 140; 140, identifier:query_options; 141, integer:0; 142, block; 142, 143; 143, raise_statement; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:HawkularError; 146, argument_list; 146, 147; 147, string:'Tags are required when querying without metric_id'; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:url; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:_get_metrics_stats_url; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:_get_url; 160, argument_list; 160, 161; 161, identifier:metric_type; 162, return_statement; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:_get; 167, argument_list; 167, 168; 167, 169; 168, identifier:url; 169, dictionary_splat; 169, 170; 170, identifier:query_options
def query_metric_stats(self, metric_type, metric_id=None, start=None, end=None, bucketDuration=None, **query_options): """ Query for metric aggregates from the server. This is called buckets in the Hawkular-Metrics documentation. :param metric_type: MetricType to be matched (required) :param metric_id: Exact string matching metric id or None for tags matching only :param start: Milliseconds since epoch or datetime instance :param end: Milliseconds since epoch or datetime instance :param bucketDuration: The timedelta or duration of buckets. Can be a string presentation or timedelta object :param query_options: For possible query_options, see the Hawkular-Metrics documentation. """ if start is not None: if type(start) is datetime: query_options['start'] = datetime_to_time_millis(start) else: query_options['start'] = start if end is not None: if type(end) is datetime: query_options['end'] = datetime_to_time_millis(end) else: query_options['end'] = end if bucketDuration is not None: if type(bucketDuration) is timedelta: query_options['bucketDuration'] = timedelta_to_duration(bucketDuration) else: query_options['bucketDuration'] = bucketDuration if metric_id is not None: url = self._get_metrics_stats_url(self._get_metrics_single_url(metric_type, metric_id)) else: if len(query_options) < 0: raise HawkularError('Tags are required when querying without metric_id') url = self._get_metrics_stats_url(self._get_url(metric_type)) return self._get(url, **query_options)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 25; 2, function_name:find_files; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 4, default_parameter; 4, 5; 4, 6; 5, identifier:path; 6, string:''; 7, default_parameter; 7, 8; 7, 9; 8, identifier:ext; 9, string:''; 10, default_parameter; 10, 11; 10, 12; 11, identifier:level; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:typ; 15, identifier:list; 16, default_parameter; 16, 17; 16, 18; 17, identifier:dirs; 18, False; 19, default_parameter; 19, 20; 19, 21; 20, identifier:files; 21, True; 22, default_parameter; 22, 23; 22, 24; 23, identifier:verbosity; 24, integer:0; 25, block; 25, 26; 25, 28; 25, 50; 26, expression_statement; 26, 27; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:gen; 31, call; 31, 32; 31, 33; 32, identifier:generate_files; 33, argument_list; 33, 34; 33, 35; 33, 38; 33, 41; 33, 44; 33, 47; 34, identifier:path; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:ext; 37, identifier:ext; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:level; 40, identifier:level; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:dirs; 43, identifier:dirs; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:files; 46, identifier:files; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:verbosity; 49, identifier:verbosity; 50, if_statement; 50, 51; 50, 60; 50, 73; 50, 83; 51, call; 51, 52; 51, 53; 52, identifier:isinstance; 53, argument_list; 53, 54; 53, 57; 54, call; 54, 55; 54, 56; 55, identifier:typ; 56, argument_list; 57, attribute; 57, 58; 57, 59; 58, identifier:collections; 59, identifier:Mapping; 60, block; 60, 61; 61, return_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:typ; 64, generator_expression; 64, 65; 64, 70; 65, tuple; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:ff; 68, string:'path'; 69, identifier:ff; 70, for_in_clause; 70, 71; 70, 72; 71, identifier:ff; 72, identifier:gen; 73, elif_clause; 73, 74; 73, 77; 74, comparison_operator:is; 74, 75; 74, 76; 75, identifier:typ; 76, None; 77, block; 77, 78; 78, return_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:typ; 81, argument_list; 81, 82; 82, identifier:gen; 83, else_clause; 83, 84; 84, block; 84, 85; 85, return_statement; 85, 86; 86, identifier:gen
def find_files(path='', ext='', level=None, typ=list, dirs=False, files=True, verbosity=0): """ Recursively find all files in the indicated directory Filter by the indicated file name extension (ext) Args: path (str): Root/base path to search. ext (str): File name extension. Only file paths that ".endswith()" this string will be returned level (int, optional): Depth of file tree to halt recursion at. None = full recursion to as deep as it goes 0 = nonrecursive, just provide a list of files at the root level of the tree 1 = one level of depth deeper in the tree typ (type): output type (default: list). if a mapping type is provided the keys will be the full paths (unique) dirs (bool): Whether to yield dir paths along with file paths (default: False) files (bool): Whether to yield file paths (default: True) `dirs=True`, `files=False` is equivalent to `ls -d` Returns: list of dicts: dict keys are { 'path', 'name', 'bytes', 'created', 'modified', 'accessed', 'permissions' } path (str): Full, absolute paths to file beneath the indicated directory and ending with `ext` name (str): File name only (everythin after the last slash in the path) size (int): File size in bytes created (datetime): File creation timestamp from file system modified (datetime): File modification timestamp from file system accessed (datetime): File access timestamp from file system permissions (int): File permissions bytes as a chown-style integer with a maximum of 4 digits type (str): One of 'file', 'dir', 'symlink->file', 'symlink->dir', 'symlink->broken' e.g.: 777 or 1755 Examples: >>> 'util.py' in [d['name'] for d in find_files(os.path.dirname(__file__), ext='.py', level=0)] True >>> (d for d in find_files(os.path.dirname(__file__), ext='.py') if d['name'] == 'util.py').next()['size'] > 1000 True There should be an __init__ file in the same directory as this script. And it should be at the top of the list. >>> sorted(d['name'] for d in find_files(os.path.dirname(__file__), ext='.py', level=0))[0] '__init__.py' >>> all(d['type'] in ('file','dir','symlink->file','symlink->dir','mount-point->file','mount-point->dir','block-device', 'symlink->broken','pipe','special','socket','unknown') for d in find_files(level=1, files=True, dirs=True)) True >>> os.path.join(os.path.dirname(__file__), '__init__.py') in find_files( ... os.path.dirname(__file__), ext='.py', level=0, typ=dict) True """ gen = generate_files(path, ext=ext, level=level, dirs=dirs, files=files, verbosity=verbosity) if isinstance(typ(), collections.Mapping): return typ((ff['path'], ff) for ff in gen) elif typ is not None: return typ(gen) else: return gen
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:limitted_dump; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 4, default_parameter; 4, 5; 4, 6; 5, identifier:cursor; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:twitter; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:path; 12, string:'tweets.json'; 13, default_parameter; 13, 14; 13, 15; 14, identifier:limit; 15, integer:450; 16, default_parameter; 16, 17; 16, 18; 17, identifier:rate; 18, identifier:TWITTER_SEARCH_RATE_LIMIT; 19, default_parameter; 19, 20; 19, 21; 20, identifier:indent; 21, unary_operator:-; 21, 22; 22, integer:1; 23, block; 23, 24; 23, 26; 23, 36; 23, 42; 23, 59; 23, 68; 23, 77; 23, 78; 24, expression_statement; 24, 25; 25, comment; 26, if_statement; 26, 27; 26, 29; 27, not_operator; 27, 28; 28, identifier:twitter; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:twitter; 33, call; 33, 34; 33, 35; 34, identifier:get_twitter; 35, argument_list; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:cursor; 39, boolean_operator:or; 39, 40; 39, 41; 40, identifier:cursor; 41, string:'python'; 42, if_statement; 42, 43; 42, 48; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:cursor; 47, identifier:basestring; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:cursor; 52, call; 52, 53; 52, 54; 53, identifier:get_cursor; 54, argument_list; 54, 55; 54, 56; 55, identifier:twitter; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:search; 58, identifier:cursor; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:newline; 62, conditional_expression:if; 62, 63; 62, 64; 62, 67; 63, string:'\n'; 64, comparison_operator:is; 64, 65; 64, 66; 65, identifier:indent; 66, None; 67, string:''; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:<; 69, 70; 69, 71; 70, identifier:indent; 71, integer:0; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:indent; 76, None; 77, comment; 78, with_statement; 78, 79; 78, 98; 79, with_clause; 79, 80; 80, with_item; 80, 81; 81, as_pattern; 81, 82; 81, 96; 82, parenthesized_expression; 82, 83; 83, conditional_expression:if; 83, 84; 83, 89; 83, 95; 84, call; 84, 85; 84, 86; 85, identifier:open; 86, argument_list; 86, 87; 86, 88; 87, identifier:path; 88, string:'w'; 89, not_operator; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:isinstance; 92, argument_list; 92, 93; 92, 94; 93, identifier:path; 94, identifier:file; 95, identifier:path; 96, as_pattern_target; 96, 97; 97, identifier:f; 98, block; 98, 99; 98, 106; 98, 182; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:f; 103, identifier:write; 104, argument_list; 104, 105; 105, string:'[\n'; 106, for_statement; 106, 107; 106, 110; 106, 114; 107, pattern_list; 107, 108; 107, 109; 108, identifier:i; 109, identifier:obj; 110, call; 110, 111; 110, 112; 111, identifier:enumerate; 112, argument_list; 112, 113; 113, identifier:cursor; 114, block; 114, 115; 114, 130; 114, 149; 114, 161; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:f; 119, identifier:write; 120, argument_list; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:json; 124, identifier:dumps; 125, argument_list; 125, 126; 125, 127; 126, identifier:obj; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:indent; 129, identifier:indent; 130, if_statement; 130, 131; 130, 136; 130, 146; 131, comparison_operator:<; 131, 132; 131, 133; 132, identifier:i; 133, binary_operator:-; 133, 134; 133, 135; 134, identifier:limit; 135, integer:1; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:f; 141, identifier:write; 142, argument_list; 142, 143; 143, binary_operator:+; 143, 144; 143, 145; 144, string:','; 145, identifier:newline; 146, else_clause; 146, 147; 147, block; 147, 148; 148, break_statement; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:remaining; 152, call; 152, 153; 152, 154; 153, identifier:int; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:twitter; 158, identifier:get_lastfunction_header; 159, argument_list; 159, 160; 160, string:'x-rate-limit-remaining'; 161, if_statement; 161, 162; 161, 165; 161, 173; 162, comparison_operator:>; 162, 163; 162, 164; 163, identifier:remaining; 164, integer:0; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:sleep; 169, argument_list; 169, 170; 170, binary_operator:/; 170, 171; 170, 172; 171, float:1.; 172, identifier:rate; 173, else_clause; 173, 174; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:sleep; 178, argument_list; 178, 179; 179, binary_operator:*; 179, 180; 179, 181; 180, integer:15; 181, integer:60; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:f; 186, identifier:write; 187, argument_list; 187, 188; 188, string:'\n]\n'
def limitted_dump(cursor=None, twitter=None, path='tweets.json', limit=450, rate=TWITTER_SEARCH_RATE_LIMIT, indent=-1): """Dump a limitted number of json.dump-able objects to the indicated file rate (int): Number of queries per 15 minute twitter window """ if not twitter: twitter = get_twitter() cursor = cursor or 'python' if isinstance(cursor, basestring): cursor = get_cursor(twitter, search=cursor) newline = '\n' if indent is not None else '' if indent < 0: indent = None # TODO: keep track of T0 for the optimal "reset" sleep duration with (open(path, 'w') if not isinstance(path, file) else path) as f: f.write('[\n') for i, obj in enumerate(cursor): f.write(json.dumps(obj, indent=indent)) if i < limit - 1: f.write(',' + newline) else: break remaining = int(twitter.get_lastfunction_header('x-rate-limit-remaining')) if remaining > 0: sleep(1. / rate) else: sleep(15 * 60) f.write('\n]\n')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:request; 6, default_parameter; 6, 7; 6, 8; 7, identifier:reverse; 8, False; 9, block; 9, 10; 9, 12; 10, expression_statement; 10, 11; 11, comment; 12, return_statement; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:sorted; 15, argument_list; 15, 16; 15, 19; 15, 32; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:collection; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:key; 21, lambda; 21, 22; 21, 24; 22, lambda_parameters; 22, 23; 23, identifier:o; 24, call; 24, 25; 24, 26; 25, identifier:getattr; 26, argument_list; 26, 27; 26, 28; 26, 31; 27, identifier:o; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:columns_sort; 31, integer:0; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:reverse; 34, identifier:reverse
async def sort(self, request, reverse=False): """Sort collection.""" return sorted( self.collection, key=lambda o: getattr(o, self.columns_sort, 0), reverse=reverse)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_request; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:self; 5, identifier:path; 6, identifier:key; 7, identifier:data; 8, identifier:method; 9, identifier:key_is_cik; 10, default_parameter; 10, 11; 10, 12; 11, identifier:extra_headers; 12, dictionary; 13, block; 13, 14; 13, 16; 13, 57; 13, 61; 13, 78; 13, 89; 13, 95; 13, 102; 13, 118; 13, 126; 13, 141; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 20; 16, 47; 17, comparison_operator:==; 17, 18; 17, 19; 18, identifier:method; 19, string:'GET'; 20, block; 20, 21; 20, 43; 21, if_statement; 21, 22; 21, 28; 21, 37; 22, comparison_operator:>; 22, 23; 22, 27; 23, call; 23, 24; 23, 25; 24, identifier:len; 25, argument_list; 25, 26; 26, identifier:data; 27, integer:0; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:url; 32, binary_operator:+; 32, 33; 32, 36; 33, binary_operator:+; 33, 34; 33, 35; 34, identifier:path; 35, string:'?'; 36, identifier:data; 37, else_clause; 37, 38; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:url; 42, identifier:path; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:body; 46, None; 47, else_clause; 47, 48; 48, block; 48, 49; 48, 53; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:url; 52, identifier:path; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:body; 56, identifier:data; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:headers; 60, dictionary; 61, if_statement; 61, 62; 61, 63; 61, 70; 62, identifier:key_is_cik; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:headers; 68, string:'X-Exosite-CIK'; 69, identifier:key; 70, else_clause; 70, 71; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:headers; 76, string:'X-Exosite-Token'; 77, identifier:key; 78, if_statement; 78, 79; 78, 82; 79, comparison_operator:==; 79, 80; 79, 81; 80, identifier:method; 81, string:'POST'; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:headers; 87, string:'Content-Type'; 88, string:'application/x-www-form-urlencoded; charset=utf-8'; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:headers; 93, string:'Accept'; 94, string:'text/plain, text/csv, application/x-www-form-urlencoded'; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:headers; 99, identifier:update; 100, argument_list; 100, 101; 101, identifier:extra_headers; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 107; 104, pattern_list; 104, 105; 104, 106; 105, identifier:body; 106, identifier:response; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_onephttp; 112, identifier:request; 113, argument_list; 113, 114; 113, 115; 113, 116; 113, 117; 114, identifier:method; 115, identifier:url; 116, identifier:body; 117, identifier:headers; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:pr; 121, call; 121, 122; 121, 123; 122, identifier:ProvisionResponse; 123, argument_list; 123, 124; 123, 125; 124, identifier:body; 125, identifier:response; 126, if_statement; 126, 127; 126, 135; 127, boolean_operator:and; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:_raise_api_exceptions; 131, not_operator; 131, 132; 132, attribute; 132, 133; 132, 134; 133, identifier:pr; 134, identifier:isok; 135, block; 135, 136; 136, raise_statement; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:ProvisionException; 139, argument_list; 139, 140; 140, identifier:pr; 141, return_statement; 141, 142; 142, identifier:pr
def _request(self, path, key, data, method, key_is_cik, extra_headers={}): """Generically shared HTTP request method. Args: path: The API endpoint to interact with. key: A string for the key used by the device for the API. Either a CIK or token. data: A string for the pre-encoded data to be sent with this request. method: A string denoting the HTTP verb to use for the request (e.g. 'GET', 'POST') key_is_cik: Whether or not the device key used is a CIK or token. extra_headers: A dictionary of extra headers to include with the request. Returns: A ProvisionResponse containing the result of the HTTP request. """ if method == 'GET': if len(data) > 0: url = path + '?' + data else: url = path body = None else: url = path body = data headers = {} if key_is_cik: headers['X-Exosite-CIK'] = key else: headers['X-Exosite-Token'] = key if method == 'POST': headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8' headers['Accept'] = 'text/plain, text/csv, application/x-www-form-urlencoded' headers.update(extra_headers) body, response = self._onephttp.request(method, url, body, headers) pr = ProvisionResponse(body, response) if self._raise_api_exceptions and not pr.isok: raise ProvisionException(pr) return pr
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:parse_intervals; 3, parameters; 3, 4; 3, 5; 4, identifier:path; 5, default_parameter; 5, 6; 5, 7; 6, identifier:as_context; 7, False; 8, block; 8, 9; 8, 11; 8, 59; 8, 81; 8, 90; 8, 96; 8, 105; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 14; 12, function_name:_regions_from_range; 13, parameters; 14, block; 14, 15; 15, if_statement; 15, 16; 15, 17; 15, 49; 16, identifier:as_context; 17, block; 17, 18; 17, 39; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:ctxs; 21, call; 21, 22; 21, 23; 22, identifier:list; 23, argument_list; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:set; 26, argument_list; 26, 27; 27, subscript; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:pf; 30, identifier:lines; 31, slice; 31, 32; 31, 35; 31, 36; 32, binary_operator:-; 32, 33; 32, 34; 33, identifier:start; 34, integer:1; 35, colon; 36, binary_operator:-; 36, 37; 36, 38; 37, identifier:stop; 38, integer:1; 39, return_statement; 39, 40; 40, list_comprehension; 40, 41; 40, 46; 41, call; 41, 42; 41, 43; 42, identifier:ContextInterval; 43, argument_list; 43, 44; 43, 45; 44, identifier:filename; 45, identifier:ctx; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:ctx; 48, identifier:ctxs; 49, else_clause; 49, 50; 50, block; 50, 51; 51, return_statement; 51, 52; 52, list:[LineInterval(filename, start, stop)]; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:LineInterval; 55, argument_list; 55, 56; 55, 57; 55, 58; 56, identifier:filename; 57, identifier:start; 58, identifier:stop; 59, if_statement; 59, 60; 59, 63; 59, 75; 60, comparison_operator:in; 60, 61; 60, 62; 61, string:':'; 62, identifier:path; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, pattern_list; 66, 67; 66, 68; 67, identifier:path; 68, identifier:subpath; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:path; 72, identifier:split; 73, argument_list; 73, 74; 74, string:':'; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:subpath; 80, string:''; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:pf; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:PythonFile; 87, identifier:from_modulename; 88, argument_list; 88, 89; 89, identifier:path; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:filename; 93, attribute; 93, 94; 93, 95; 94, identifier:pf; 95, identifier:filename; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:rng; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:NUMBER_RE; 102, identifier:match; 103, argument_list; 103, 104; 104, identifier:subpath; 105, if_statement; 105, 106; 105, 107; 105, 108; 105, 136; 105, 169; 106, identifier:rng; 107, comment; 108, block; 108, 109; 108, 124; 108, 132; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, pattern_list; 111, 112; 111, 113; 112, identifier:start; 113, identifier:stop; 114, call; 114, 115; 114, 116; 115, identifier:map; 116, argument_list; 116, 117; 116, 118; 117, identifier:int; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:rng; 121, identifier:groups; 122, argument_list; 122, 123; 123, integer:0; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:stop; 127, boolean_operator:or; 127, 128; 127, 129; 128, identifier:stop; 129, binary_operator:+; 129, 130; 129, 131; 130, identifier:start; 131, integer:1; 132, return_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:_regions_from_range; 135, argument_list; 136, elif_clause; 136, 137; 136, 139; 136, 140; 137, not_operator; 137, 138; 138, identifier:subpath; 139, comment; 140, block; 140, 141; 140, 153; 140, 165; 141, if_statement; 141, 142; 141, 143; 142, identifier:as_context; 143, block; 143, 144; 144, return_statement; 144, 145; 145, list:[ContextInterval(filename, pf.prefix)]; 145, 146; 146, call; 146, 147; 146, 148; 147, identifier:ContextInterval; 148, argument_list; 148, 149; 148, 150; 149, identifier:filename; 150, attribute; 150, 151; 150, 152; 151, identifier:pf; 152, identifier:prefix; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, pattern_list; 155, 156; 155, 157; 156, identifier:start; 157, identifier:stop; 158, expression_list; 158, 159; 158, 160; 159, integer:1; 160, binary_operator:+; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:pf; 163, identifier:line_count; 164, integer:1; 165, return_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:_regions_from_range; 168, argument_list; 169, else_clause; 169, 170; 169, 171; 170, comment; 171, block; 171, 172; 171, 182; 171, 200; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:context; 175, binary_operator:+; 175, 176; 175, 181; 176, binary_operator:+; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:pf; 179, identifier:prefix; 180, string:':'; 181, identifier:subpath; 182, if_statement; 182, 183; 182, 188; 183, comparison_operator:not; 183, 184; 183, 185; 184, identifier:context; 185, attribute; 185, 186; 185, 187; 186, identifier:pf; 187, identifier:lines; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:ValueError; 192, argument_list; 192, 193; 193, binary_operator:%; 193, 194; 193, 195; 194, string:"%s is not a valid context for %s"; 195, tuple; 195, 196; 195, 197; 196, identifier:context; 197, attribute; 197, 198; 197, 199; 198, identifier:pf; 199, identifier:prefix; 200, if_statement; 200, 201; 200, 202; 200, 210; 201, identifier:as_context; 202, block; 202, 203; 203, return_statement; 203, 204; 204, list:[ContextInterval(filename, context)]; 204, 205; 205, call; 205, 206; 205, 207; 206, identifier:ContextInterval; 207, argument_list; 207, 208; 207, 209; 208, identifier:filename; 209, identifier:context; 210, else_clause; 210, 211; 211, block; 211, 212; 211, 223; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 217; 214, pattern_list; 214, 215; 214, 216; 215, identifier:start; 216, identifier:stop; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:pf; 220, identifier:context_range; 221, argument_list; 221, 222; 222, identifier:context; 223, return_statement; 223, 224; 224, list:[LineInterval(filename, start, stop)]; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:LineInterval; 227, argument_list; 227, 228; 227, 229; 227, 230; 228, identifier:filename; 229, identifier:start; 230, identifier:stop
def parse_intervals(path, as_context=False): """ Parse path strings into a collection of Intervals. `path` is a string describing a region in a file. It's format is dotted.module.name:[line | start-stop | context] `dotted.module.name` is a python module `line` is a single line number in the module (1-offset) `start-stop` is a right-open interval of line numbers `context` is a '.' delimited, nested name of a class or function. For example FooClass.method_a.inner_method identifies the innermost function in code like class FooClass: def method_a(self): def inner_method(): pass Parameters ---------- path : str Region description (see above) as_context : bool (optional, default=False) If `True`, return `ContextInterval`s instead of `LineInterval`s. If `path` provides a line number or range, the result will include all contexts that intersect this line range. Returns ------- list of `Interval`s """ def _regions_from_range(): if as_context: ctxs = list(set(pf.lines[start - 1: stop - 1])) return [ ContextInterval(filename, ctx) for ctx in ctxs ] else: return [LineInterval(filename, start, stop)] if ':' in path: path, subpath = path.split(':') else: subpath = '' pf = PythonFile.from_modulename(path) filename = pf.filename rng = NUMBER_RE.match(subpath) if rng: # specified a line or line range start, stop = map(int, rng.groups(0)) stop = stop or start + 1 return _regions_from_range() elif not subpath: # asked for entire module if as_context: return [ContextInterval(filename, pf.prefix)] start, stop = 1, pf.line_count + 1 return _regions_from_range() else: # specified a context name context = pf.prefix + ':' + subpath if context not in pf.lines: raise ValueError("%s is not a valid context for %s" % (context, pf.prefix)) if as_context: return [ContextInterval(filename, context)] else: start, stop = pf.context_range(context) return [LineInterval(filename, start, stop)]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:graphiter; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:graph; 6, identifier:target; 7, default_parameter; 7, 8; 7, 9; 8, identifier:ascendants; 9, integer:0; 10, default_parameter; 10, 11; 10, 12; 11, identifier:descendants; 12, integer:1; 13, block; 13, 14; 13, 16; 13, 22; 13, 31; 13, 37; 13, 46; 13, 53; 13, 140; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:asc; 19, binary_operator:+; 19, 20; 19, 21; 20, integer:0; 21, identifier:ascendants; 22, if_statement; 22, 23; 22, 26; 23, comparison_operator:!=; 23, 24; 23, 25; 24, identifier:asc; 25, integer:0; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, augmented_assignment:-=; 28, 29; 28, 30; 29, identifier:asc; 30, integer:1; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:desc; 34, binary_operator:+; 34, 35; 34, 36; 35, integer:0; 36, identifier:descendants; 37, if_statement; 37, 38; 37, 41; 38, comparison_operator:!=; 38, 39; 38, 40; 39, identifier:desc; 40, integer:0; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, augmented_assignment:-=; 43, 44; 43, 45; 44, identifier:desc; 45, integer:1; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:t; 49, call; 49, 50; 49, 51; 50, identifier:str; 51, argument_list; 51, 52; 52, identifier:target; 53, if_statement; 53, 54; 53, 65; 54, boolean_operator:and; 54, 55; 54, 58; 55, comparison_operator:!=; 55, 56; 55, 57; 56, identifier:descendants; 57, integer:0; 58, comparison_operator:is; 58, 59; 58, 64; 59, subscript; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:downwards; 63, identifier:t; 64, True; 65, block; 65, 66; 65, 74; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 73; 68, subscript; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:downwards; 72, identifier:t; 73, False; 74, for_statement; 74, 75; 74, 78; 74, 84; 75, pattern_list; 75, 76; 75, 77; 76, identifier:pred; 77, identifier:obj; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:graph; 81, identifier:predicate_objects; 82, argument_list; 82, 83; 83, identifier:target; 84, block; 84, 85; 84, 97; 84, 107; 84, 108; 85, if_statement; 85, 86; 85, 95; 86, boolean_operator:and; 86, 87; 86, 90; 87, comparison_operator:==; 87, 88; 87, 89; 88, identifier:desc; 89, integer:0; 90, call; 90, 91; 90, 92; 91, identifier:isinstance; 92, argument_list; 92, 93; 92, 94; 93, identifier:obj; 94, identifier:BNode; 95, block; 95, 96; 96, continue_statement; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:add; 102, argument_list; 102, 103; 103, tuple; 103, 104; 103, 105; 103, 106; 104, identifier:target; 105, identifier:pred; 106, identifier:obj; 107, comment; 108, if_statement; 108, 109; 108, 123; 109, boolean_operator:and; 109, 110; 109, 113; 110, comparison_operator:!=; 110, 111; 110, 112; 111, identifier:desc; 112, integer:0; 113, comparison_operator:is; 113, 114; 113, 122; 114, subscript; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:downwards; 118, call; 118, 119; 118, 120; 119, identifier:str; 120, argument_list; 120, 121; 121, identifier:obj; 122, True; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:graphiter; 129, argument_list; 129, 130; 129, 131; 129, 134; 129, 137; 130, identifier:graph; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:target; 133, identifier:obj; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:ascendants; 136, integer:0; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:descendants; 139, identifier:desc; 140, if_statement; 140, 141; 140, 152; 141, boolean_operator:and; 141, 142; 141, 145; 142, comparison_operator:!=; 142, 143; 142, 144; 143, identifier:ascendants; 144, integer:0; 145, comparison_operator:is; 145, 146; 145, 151; 146, subscript; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:updwards; 150, identifier:t; 151, True; 152, block; 152, 153; 152, 161; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 160; 155, subscript; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:updwards; 159, identifier:t; 160, False; 161, for_statement; 161, 162; 161, 165; 161, 173; 162, pattern_list; 162, 163; 162, 164; 163, identifier:s; 164, identifier:p; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:graph; 168, identifier:subject_predicates; 169, argument_list; 169, 170; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:object; 172, identifier:target; 173, block; 173, 174; 173, 186; 173, 196; 173, 197; 174, if_statement; 174, 175; 174, 184; 175, boolean_operator:and; 175, 176; 175, 179; 176, comparison_operator:==; 176, 177; 176, 178; 177, identifier:desc; 178, integer:0; 179, call; 179, 180; 179, 181; 180, identifier:isinstance; 181, argument_list; 181, 182; 181, 183; 182, identifier:s; 183, identifier:BNode; 184, block; 184, 185; 185, continue_statement; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:self; 190, identifier:add; 191, argument_list; 191, 192; 192, tuple; 192, 193; 192, 194; 192, 195; 193, identifier:s; 194, identifier:p; 195, identifier:target; 196, comment; 197, if_statement; 197, 198; 197, 212; 198, boolean_operator:and; 198, 199; 198, 202; 199, comparison_operator:!=; 199, 200; 199, 201; 200, identifier:asc; 201, integer:0; 202, comparison_operator:is; 202, 203; 202, 211; 203, subscript; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:updwards; 207, call; 207, 208; 207, 209; 208, identifier:str; 209, argument_list; 209, 210; 210, identifier:s; 211, True; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:graphiter; 218, argument_list; 218, 219; 218, 220; 218, 223; 218, 226; 219, identifier:graph; 220, keyword_argument; 220, 221; 220, 222; 221, identifier:target; 222, identifier:s; 223, keyword_argument; 223, 224; 223, 225; 224, identifier:ascendants; 225, identifier:asc; 226, keyword_argument; 226, 227; 226, 228; 227, identifier:descendants; 228, integer:0
def graphiter(self, graph, target, ascendants=0, descendants=1): """ Iter on a graph to finds object connected :param graph: Graph to serialize :type graph: Graph :param target: Node to iterate over :type target: Node :param ascendants: Number of level to iter over upwards (-1 = No Limit) :param descendants: Number of level to iter over downwards (-1 = No limit) :return: """ asc = 0 + ascendants if asc != 0: asc -= 1 desc = 0 + descendants if desc != 0: desc -= 1 t = str(target) if descendants != 0 and self.downwards[t] is True: self.downwards[t] = False for pred, obj in graph.predicate_objects(target): if desc == 0 and isinstance(obj, BNode): continue self.add((target, pred, obj)) # Retrieve triples about the object if desc != 0 and self.downwards[str(obj)] is True: self.graphiter(graph, target=obj, ascendants=0, descendants=desc) if ascendants != 0 and self.updwards[t] is True: self.updwards[t] = False for s, p in graph.subject_predicates(object=target): if desc == 0 and isinstance(s, BNode): continue self.add((s, p, target)) # Retrieve triples about the parent as object if asc != 0 and self.updwards[str(s)] is True: self.graphiter(graph, target=s, ascendants=asc, descendants=0)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:map_aliases_to_device_objects; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 47; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:all_devices; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:get_all_devices_in_portal; 15, argument_list; 16, for_statement; 16, 17; 16, 18; 16, 19; 17, identifier:dev_o; 18, identifier:all_devices; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:dev_o; 24, string:'portals_aliases'; 25, subscript; 25, 26; 25, 44; 26, subscript; 26, 27; 26, 43; 27, subscript; 27, 28; 27, 42; 28, subscript; 28, 29; 28, 41; 29, subscript; 29, 30; 29, 40; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:get_portal_by_name; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:portal_name; 39, argument_list; 40, integer:2; 41, integer:1; 42, string:'info'; 43, string:'aliases'; 44, subscript; 44, 45; 44, 46; 45, identifier:dev_o; 46, string:'rid'; 47, return_statement; 47, 48; 48, identifier:all_devices
def map_aliases_to_device_objects(self): """ A device object knows its rid, but not its alias. A portal object knows its device rids and aliases. This function adds an 'portals_aliases' key to all of the device objects so they can be sorted by alias. """ all_devices = self.get_all_devices_in_portal() for dev_o in all_devices: dev_o['portals_aliases'] = self.get_portal_by_name( self.portal_name() )[2][1]['info']['aliases'][ dev_o['rid'] ] return all_devices
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:print_sorted_device_list; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:device_list; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort_key; 10, string:'sn'; 11, block; 11, 12; 11, 14; 11, 27; 11, 31; 11, 208; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:dev_list; 17, conditional_expression:if; 17, 18; 17, 19; 17, 22; 18, identifier:device_list; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:device_list; 21, None; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:get_all_devices_in_portal; 26, argument_list; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:sorted_dev_list; 30, list:[]; 31, if_statement; 31, 32; 31, 35; 31, 80; 31, 143; 31, 192; 32, comparison_operator:==; 32, 33; 32, 34; 33, identifier:sort_key; 34, string:'sn'; 35, block; 35, 36; 35, 52; 35, 59; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:sort_keys; 39, list_comprehension; 39, 40; 39, 43; 39, 46; 40, subscript; 40, 41; 40, 42; 41, identifier:k; 42, identifier:sort_key; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:k; 45, identifier:dev_list; 46, if_clause; 46, 47; 47, comparison_operator:is; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:k; 50, identifier:sort_key; 51, None; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:sort_keys; 55, call; 55, 56; 55, 57; 56, identifier:sorted; 57, argument_list; 57, 58; 58, identifier:sort_keys; 59, for_statement; 59, 60; 59, 61; 59, 62; 60, identifier:key; 61, identifier:sort_keys; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:sorted_dev_list; 67, identifier:extend; 68, argument_list; 68, 69; 69, list_comprehension; 69, 70; 69, 71; 69, 74; 70, identifier:d; 71, for_in_clause; 71, 72; 71, 73; 72, identifier:d; 73, identifier:dev_list; 74, if_clause; 74, 75; 75, comparison_operator:==; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:d; 78, string:'sn'; 79, identifier:key; 80, elif_clause; 80, 81; 80, 84; 81, comparison_operator:==; 81, 82; 81, 83; 82, identifier:sort_key; 83, string:'name'; 84, block; 84, 85; 84, 110; 84, 117; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:sort_keys; 88, list_comprehension; 88, 89; 88, 96; 88, 97; 88, 100; 89, subscript; 89, 90; 89, 95; 90, subscript; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:k; 93, string:'info'; 94, string:'description'; 95, identifier:sort_key; 96, line_continuation:\; 97, for_in_clause; 97, 98; 97, 99; 98, identifier:k; 99, identifier:dev_list; 100, if_clause; 100, 101; 101, comparison_operator:is; 101, 102; 101, 109; 102, subscript; 102, 103; 102, 108; 103, subscript; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:k; 106, string:'info'; 107, string:'description'; 108, identifier:sort_key; 109, None; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:sort_keys; 113, call; 113, 114; 113, 115; 114, identifier:sorted; 115, argument_list; 115, 116; 116, identifier:sort_keys; 117, for_statement; 117, 118; 117, 119; 117, 120; 118, identifier:key; 119, identifier:sort_keys; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:sorted_dev_list; 125, identifier:extend; 126, argument_list; 126, 127; 127, list_comprehension; 127, 128; 127, 129; 127, 132; 127, 133; 128, identifier:d; 129, for_in_clause; 129, 130; 129, 131; 130, identifier:d; 131, identifier:dev_list; 132, line_continuation:\; 133, if_clause; 133, 134; 134, comparison_operator:==; 134, 135; 134, 142; 135, subscript; 135, 136; 135, 141; 136, subscript; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:d; 139, string:'info'; 140, string:'description'; 141, identifier:sort_key; 142, identifier:key; 143, elif_clause; 143, 144; 143, 147; 144, comparison_operator:==; 144, 145; 144, 146; 145, identifier:sort_key; 146, string:'portals_aliases'; 147, block; 147, 148; 147, 164; 147, 171; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:sort_keys; 151, list_comprehension; 151, 152; 151, 155; 151, 158; 152, subscript; 152, 153; 152, 154; 153, identifier:k; 154, identifier:sort_key; 155, for_in_clause; 155, 156; 155, 157; 156, identifier:k; 157, identifier:dev_list; 158, if_clause; 158, 159; 159, comparison_operator:is; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:k; 162, identifier:sort_key; 163, None; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:sort_keys; 167, call; 167, 168; 167, 169; 168, identifier:sorted; 169, argument_list; 169, 170; 170, identifier:sort_keys; 171, for_statement; 171, 172; 171, 173; 171, 174; 172, identifier:key; 173, identifier:sort_keys; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:sorted_dev_list; 179, identifier:extend; 180, argument_list; 180, 181; 181, list_comprehension; 181, 182; 181, 183; 181, 186; 182, identifier:d; 183, for_in_clause; 183, 184; 183, 185; 184, identifier:d; 185, identifier:dev_list; 186, if_clause; 186, 187; 187, comparison_operator:==; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:d; 190, identifier:sort_key; 191, identifier:key; 192, else_clause; 192, 193; 193, block; 193, 194; 193, 204; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:print; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, string:"Sort key {!r} not recognized."; 201, identifier:format; 202, argument_list; 202, 203; 203, identifier:sort_key; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:sort_keys; 207, None; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:self; 212, identifier:print_device_list; 213, argument_list; 213, 214; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:device_list; 216, identifier:sorted_dev_list
def print_sorted_device_list(self, device_list=None, sort_key='sn'): """ Takes in a sort key and prints the device list according to that sort. Default sorts on serial number. Current supported sort options are: - name - sn - portals_aliases Can take optional device object list. """ dev_list = device_list if device_list is not None else self.get_all_devices_in_portal() sorted_dev_list = [] if sort_key == 'sn': sort_keys = [ k[sort_key] for k in dev_list if k[sort_key] is not None ] sort_keys = sorted(sort_keys) for key in sort_keys: sorted_dev_list.extend([ d for d in dev_list if d['sn'] == key ]) elif sort_key == 'name': sort_keys = [ k['info']['description'][sort_key]\ for k in dev_list if k['info']['description'][sort_key] is not None ] sort_keys = sorted(sort_keys) for key in sort_keys: sorted_dev_list.extend( [ d for d in dev_list\ if d['info']['description'][sort_key] == key ] ) elif sort_key == 'portals_aliases': sort_keys = [ k[sort_key] for k in dev_list if k[sort_key] is not None ] sort_keys = sorted(sort_keys) for key in sort_keys: sorted_dev_list.extend([ d for d in dev_list if d[sort_key] == key ]) else: print("Sort key {!r} not recognized.".format(sort_key)) sort_keys = None self.print_device_list(device_list=sorted_dev_list)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:enrich; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:columns; 6, block; 6, 7; 6, 9; 6, 26; 6, 27; 6, 28; 6, 41; 6, 45; 6, 53; 6, 156; 6, 172; 7, expression_statement; 7, 8; 8, comment; 9, for_statement; 9, 10; 9, 11; 9, 12; 10, identifier:column; 11, identifier:columns; 12, block; 12, 13; 13, if_statement; 13, 14; 13, 21; 14, comparison_operator:not; 14, 15; 14, 16; 15, identifier:column; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:data; 20, identifier:columns; 21, block; 21, 22; 22, return_statement; 22, 23; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:data; 26, comment; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:first_column; 31, call; 31, 32; 31, 33; 32, identifier:list; 33, argument_list; 33, 34; 34, subscript; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:data; 38, subscript; 38, 39; 38, 40; 39, identifier:columns; 40, integer:0; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:count; 44, integer:0; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:append_df; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:pandas; 51, identifier:DataFrame; 52, argument_list; 53, for_statement; 53, 54; 53, 55; 53, 56; 54, identifier:cell; 55, identifier:first_column; 56, block; 56, 57; 56, 150; 57, if_statement; 57, 58; 57, 64; 57, 65; 57, 66; 58, comparison_operator:>=; 58, 59; 58, 63; 59, call; 59, 60; 59, 61; 60, identifier:len; 61, argument_list; 61, 62; 62, identifier:cell; 63, integer:1; 64, comment; 65, comment; 66, block; 66, 67; 66, 75; 66, 76; 66, 93; 66, 94; 66, 115; 66, 130; 66, 142; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:df; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:pandas; 73, identifier:DataFrame; 74, argument_list; 75, comment; 76, for_statement; 76, 77; 76, 78; 76, 79; 77, identifier:column; 78, identifier:columns; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 85; 82, subscript; 82, 83; 82, 84; 83, identifier:df; 84, identifier:column; 85, subscript; 85, 86; 85, 91; 85, 92; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:data; 90, identifier:loc; 91, identifier:count; 92, identifier:column; 93, comment; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:extra_df; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:pandas; 100, identifier:DataFrame; 101, argument_list; 101, 102; 102, binary_operator:*; 102, 103; 102, 111; 103, list:[self.data.loc[count]]; 103, 104; 104, subscript; 104, 105; 104, 110; 105, attribute; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:data; 109, identifier:loc; 110, identifier:count; 111, call; 111, 112; 111, 113; 112, identifier:len; 113, argument_list; 113, 114; 114, identifier:df; 115, for_statement; 115, 116; 115, 117; 115, 118; 116, identifier:column; 117, identifier:columns; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:extra_df; 123, identifier:column; 124, call; 124, 125; 124, 126; 125, identifier:list; 126, argument_list; 126, 127; 127, subscript; 127, 128; 127, 129; 128, identifier:df; 129, identifier:column; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:append_df; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:append_df; 136, identifier:append; 137, argument_list; 137, 138; 137, 139; 138, identifier:extra_df; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:ignore_index; 141, True; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:extra_df; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:pandas; 148, identifier:DataFrame; 149, argument_list; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:count; 153, binary_operator:+; 153, 154; 153, 155; 154, identifier:count; 155, integer:1; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:data; 161, call; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:data; 166, identifier:append; 167, argument_list; 167, 168; 167, 169; 168, identifier:append_df; 169, keyword_argument; 169, 170; 169, 171; 170, identifier:ignore_index; 171, True; 172, return_statement; 172, 173; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:data
def enrich(self, columns): """ This method appends at the end of the dataframe as many rows as items are found in the list of elemnents in the provided columns. This assumes that the length of the lists for the several specified columns is the same. As an example, for the row A {"C1":"V1", "C2":field1, "C3":field2, "C4":field3} we have three cells with a list of four elements each of them: * field1: [1,2,3,4] * field2: ["a", "b", "c", "d"] * field3: [1.1, 2.2, 3.3, 4.4] This method converts each of the elements of each cell in a new row keeping the columns name: {"C1":"V1", "C2":1, "C3":"a", "C4":1.1} {"C1":"V1", "C2":2, "C3":"b", "C4":2.2} {"C1":"V1", "C2":3, "C3":"c", "C4":3.3} {"C1":"V1", "C2":4, "C3":"d", "C4":4.4} :param columns: list of strings :rtype pandas.DataFrame """ for column in columns: if column not in self.data.columns: return self.data # Looking for the rows with columns with lists of more # than one element first_column = list(self.data[columns[0]]) count = 0 append_df = pandas.DataFrame() for cell in first_column: if len(cell) >= 1: # Interested in those lists with more # than one element df = pandas.DataFrame() # Create a dataframe of N rows from the list for column in columns: df[column] = self.data.loc[count, column] # Repeat the original rows N times extra_df = pandas.DataFrame([self.data.loc[count]] * len(df)) for column in columns: extra_df[column] = list(df[column]) append_df = append_df.append(extra_df, ignore_index=True) extra_df = pandas.DataFrame() count = count + 1 self.data = self.data.append(append_df, ignore_index=True) return self.data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:parse_metadata; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:obj; 6, identifier:xml; 7, block; 7, 8; 7, 10; 7, 47; 7, 84; 7, 101; 7, 102; 7, 134; 7, 140; 8, expression_statement; 8, 9; 9, comment; 10, for_statement; 10, 11; 10, 12; 10, 21; 11, identifier:child; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:xml; 15, identifier:xpath; 16, argument_list; 16, 17; 16, 18; 17, string:"ti:description"; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:namespaces; 20, identifier:XPATH_NAMESPACES; 21, block; 21, 22; 21, 31; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:lg; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:child; 28, identifier:get; 29, argument_list; 29, 30; 30, string:"{http://www.w3.org/XML/1998/namespace}lang"; 31, if_statement; 31, 32; 31, 35; 32, comparison_operator:is; 32, 33; 32, 34; 33, identifier:lg; 34, None; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:obj; 40, identifier:set_cts_property; 41, argument_list; 41, 42; 41, 43; 41, 46; 42, string:"description"; 43, attribute; 43, 44; 43, 45; 44, identifier:child; 45, identifier:text; 46, identifier:lg; 47, for_statement; 47, 48; 47, 49; 47, 58; 48, identifier:child; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:xml; 52, identifier:xpath; 53, argument_list; 53, 54; 53, 55; 54, string:"ti:label"; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:namespaces; 57, identifier:XPATH_NAMESPACES; 58, block; 58, 59; 58, 68; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:lg; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:child; 65, identifier:get; 66, argument_list; 66, 67; 67, string:"{http://www.w3.org/XML/1998/namespace}lang"; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:lg; 71, None; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:obj; 77, identifier:set_cts_property; 78, argument_list; 78, 79; 78, 80; 78, 83; 79, string:"label"; 80, attribute; 80, 81; 80, 82; 81, identifier:child; 82, identifier:text; 83, identifier:lg; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:obj; 88, identifier:citation; 89, call; 89, 90; 89, 95; 90, attribute; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:cls; 93, identifier:CLASS_CITATION; 94, identifier:ingest; 95, argument_list; 95, 96; 95, 97; 95, 100; 96, identifier:xml; 97, attribute; 97, 98; 97, 99; 98, identifier:obj; 99, identifier:citation; 100, string:"ti:online/ti:citationMapping/ti:citation"; 101, comment; 102, for_statement; 102, 103; 102, 104; 102, 113; 103, identifier:child; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:xml; 107, identifier:xpath; 108, argument_list; 108, 109; 108, 110; 109, string:"ti:about"; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:namespaces; 112, identifier:XPATH_NAMESPACES; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:obj; 118, identifier:set_link; 119, argument_list; 119, 120; 119, 128; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:RDF_NAMESPACES; 124, identifier:CTS; 125, identifier:term; 126, argument_list; 126, 127; 127, string:"about"; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:child; 131, identifier:get; 132, argument_list; 132, 133; 133, string:'urn'; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:_parse_structured_metadata; 137, argument_list; 137, 138; 137, 139; 138, identifier:obj; 139, identifier:xml; 140, expression_statement; 140, 141; 141, comment
def parse_metadata(cls, obj, xml): """ Parse a resource to feed the object :param obj: Obj to set metadata of :type obj: XmlCtsTextMetadata :param xml: An xml representation object :type xml: lxml.etree._Element """ for child in xml.xpath("ti:description", namespaces=XPATH_NAMESPACES): lg = child.get("{http://www.w3.org/XML/1998/namespace}lang") if lg is not None: obj.set_cts_property("description", child.text, lg) for child in xml.xpath("ti:label", namespaces=XPATH_NAMESPACES): lg = child.get("{http://www.w3.org/XML/1998/namespace}lang") if lg is not None: obj.set_cts_property("label", child.text, lg) obj.citation = cls.CLASS_CITATION.ingest(xml, obj.citation, "ti:online/ti:citationMapping/ti:citation") # Added for commentary for child in xml.xpath("ti:about", namespaces=XPATH_NAMESPACES): obj.set_link(RDF_NAMESPACES.CTS.term("about"), child.get('urn')) _parse_structured_metadata(obj, xml) """ online = xml.xpath("ti:online", namespaces=NS) if len(online) > 0: online = online[0] obj.docname = online.get("docname") for validate in online.xpath("ti:validate", namespaces=NS): obj.validate = validate.get("schema") for namespaceMapping in online.xpath("ti:namespaceMapping", namespaces=NS): obj.metadata["namespaceMapping"][namespaceMapping.get("abbreviation")] = namespaceMapping.get("nsURI") """
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:setup; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:app; 6, block; 6, 7; 6, 9; 6, 18; 6, 26; 6, 27; 6, 48; 6, 71; 6, 97; 6, 162; 6, 187; 6, 188; 6, 238; 6, 256; 6, 311; 6, 340; 6, 341; 6, 362; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 16; 11, attribute; 11, 12; 11, 15; 12, call; 12, 13; 12, 14; 13, identifier:super; 14, argument_list; 15, identifier:setup; 16, argument_list; 16, 17; 17, identifier:app; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:handlers; 23, call; 23, 24; 23, 25; 24, identifier:OrderedDict; 25, argument_list; 26, comment; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 40; 29, attribute; 29, 30; 29, 39; 30, attribute; 30, 31; 30, 38; 31, attribute; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:app; 35, identifier:ps; 36, identifier:jinja2; 37, identifier:cfg; 38, identifier:template_folders; 39, identifier:append; 40, argument_list; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:op; 44, identifier:join; 45, argument_list; 45, 46; 45, 47; 46, identifier:PLUGIN_ROOT; 47, string:'templates'; 48, decorated_definition; 48, 49; 48, 57; 49, decorator; 49, 50; 50, attribute; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:app; 54, identifier:ps; 55, identifier:jinja2; 56, identifier:filter; 57, function_definition; 57, 58; 57, 59; 57, 65; 58, function_name:admtest; 59, parameters; 59, 60; 59, 61; 59, 62; 60, identifier:value; 61, identifier:a; 62, default_parameter; 62, 63; 62, 64; 63, identifier:b; 64, None; 65, block; 65, 66; 66, return_statement; 66, 67; 67, conditional_expression:if; 67, 68; 67, 69; 67, 70; 68, identifier:a; 69, identifier:value; 70, identifier:b; 71, decorated_definition; 71, 72; 71, 80; 72, decorator; 72, 73; 73, attribute; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:app; 77, identifier:ps; 78, identifier:jinja2; 79, identifier:filter; 80, function_definition; 80, 81; 80, 82; 80, 88; 81, function_name:admeq; 82, parameters; 82, 83; 82, 84; 82, 85; 83, identifier:a; 84, identifier:b; 85, default_parameter; 85, 86; 85, 87; 86, identifier:result; 87, True; 88, block; 88, 89; 89, return_statement; 89, 90; 90, conditional_expression:if; 90, 91; 90, 92; 90, 95; 91, identifier:result; 92, comparison_operator:==; 92, 93; 92, 94; 93, identifier:a; 94, identifier:b; 95, not_operator; 95, 96; 96, identifier:result; 97, decorated_definition; 97, 98; 97, 106; 98, decorator; 98, 99; 99, attribute; 99, 100; 99, 105; 100, attribute; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:app; 103, identifier:ps; 104, identifier:jinja2; 105, identifier:register; 106, function_definition; 106, 107; 106, 108; 106, 111; 107, function_name:admurl; 108, parameters; 108, 109; 108, 110; 109, identifier:request; 110, identifier:prefix; 111, block; 111, 112; 111, 138; 111, 149; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:qs; 115, dictionary_comprehension; 115, 116; 115, 119; 115, 130; 116, pair; 116, 117; 116, 118; 117, identifier:k; 118, identifier:v; 119, for_in_clause; 119, 120; 119, 123; 120, pattern_list; 120, 121; 120, 122; 121, identifier:k; 122, identifier:v; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:request; 127, identifier:query; 128, identifier:items; 129, argument_list; 130, if_clause; 130, 131; 131, not_operator; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:k; 135, identifier:startswith; 136, argument_list; 136, 137; 137, identifier:prefix; 138, if_statement; 138, 139; 138, 141; 139, not_operator; 139, 140; 140, identifier:qs; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:qs; 145, dictionary; 145, 146; 146, pair; 146, 147; 146, 148; 147, string:'ap'; 148, integer:0; 149, return_statement; 149, 150; 150, binary_operator:%; 150, 151; 150, 152; 151, string:"%s?%s"; 152, tuple; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:request; 155, identifier:path; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:urlparse; 159, identifier:urlencode; 160, argument_list; 160, 161; 161, identifier:qs; 162, if_statement; 162, 163; 162, 170; 163, comparison_operator:is; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:cfg; 168, identifier:name; 169, None; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:cfg; 177, identifier:name; 178, binary_operator:%; 178, 179; 178, 180; 179, string:"%s admin"; 180, call; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:app; 184, identifier:name; 185, identifier:title; 186, argument_list; 187, comment; 188, if_statement; 188, 189; 188, 198; 189, not_operator; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:callable; 192, argument_list; 192, 193; 193, attribute; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:cfg; 197, identifier:home; 198, block; 198, 199; 198, 230; 199, function_definition; 199, 200; 199, 201; 199, 203; 200, function_name:admin_home; 201, parameters; 201, 202; 202, identifier:request; 203, block; 203, 204; 203, 212; 204, expression_statement; 204, 205; 205, yield; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:authorize; 210, argument_list; 210, 211; 211, identifier:request; 212, return_statement; 212, 213; 213, call; 213, 214; 213, 221; 214, attribute; 214, 215; 214, 220; 215, attribute; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:app; 218, identifier:ps; 219, identifier:jinja2; 220, identifier:render; 221, argument_list; 221, 222; 221, 227; 222, attribute; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:self; 225, identifier:cfg; 226, identifier:template_home; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:active; 229, None; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:cfg; 236, identifier:home; 237, identifier:admin_home; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 250; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:app; 243, identifier:register; 244, argument_list; 244, 245; 245, attribute; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:cfg; 249, identifier:prefix; 250, argument_list; 250, 251; 251, attribute; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:cfg; 255, identifier:home; 256, if_statement; 256, 257; 256, 263; 257, not_operator; 257, 258; 258, attribute; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:cfg; 262, identifier:i18n; 263, block; 263, 264; 263, 310; 264, expression_statement; 264, 265; 265, call; 265, 266; 265, 277; 266, attribute; 266, 267; 266, 276; 267, attribute; 267, 268; 267, 275; 268, attribute; 268, 269; 268, 274; 269, attribute; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:app; 272, identifier:ps; 273, identifier:jinja2; 274, identifier:env; 275, identifier:globals; 276, identifier:update; 277, argument_list; 277, 278; 278, dictionary; 278, 279; 278, 285; 278, 291; 279, pair; 279, 280; 279, 281; 280, string:'_'; 281, lambda; 281, 282; 281, 284; 282, lambda_parameters; 282, 283; 283, identifier:s; 284, identifier:s; 285, pair; 285, 286; 285, 287; 286, string:'gettext'; 287, lambda; 287, 288; 287, 290; 288, lambda_parameters; 288, 289; 289, identifier:s; 290, identifier:s; 291, pair; 291, 292; 291, 293; 292, string:'ngettext'; 293, lambda; 293, 294; 293, 298; 294, lambda_parameters; 294, 295; 294, 296; 294, 297; 295, identifier:s; 296, identifier:p; 297, identifier:n; 298, subscript; 298, 299; 298, 309; 299, parenthesized_expression; 299, 300; 300, boolean_operator:or; 300, 301; 300, 307; 301, boolean_operator:and; 301, 302; 301, 305; 302, comparison_operator:!=; 302, 303; 302, 304; 303, identifier:n; 304, integer:1; 305, tuple; 305, 306; 306, identifier:p; 307, tuple; 307, 308; 308, identifier:s; 309, integer:0; 310, return_statement; 311, if_statement; 311, 312; 311, 328; 312, boolean_operator:or; 312, 313; 312, 318; 313, comparison_operator:not; 313, 314; 313, 315; 314, string:'babel'; 315, attribute; 315, 316; 315, 317; 316, identifier:app; 317, identifier:ps; 318, not_operator; 318, 319; 319, call; 319, 320; 319, 321; 320, identifier:isinstance; 321, argument_list; 321, 322; 321, 327; 322, attribute; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:app; 325, identifier:ps; 326, identifier:babel; 327, identifier:BPlugin; 328, block; 328, 329; 329, raise_statement; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:PluginException; 332, argument_list; 332, 333; 333, binary_operator:%; 333, 334; 333, 335; 334, string:'Plugin `%s` requires for plugin `%s` to be installed to the application.'; 335, tuple; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:self; 338, identifier:name; 339, identifier:BPlugin; 340, comment; 341, expression_statement; 341, 342; 342, call; 342, 343; 342, 354; 343, attribute; 343, 344; 343, 353; 344, attribute; 344, 345; 344, 352; 345, attribute; 345, 346; 345, 351; 346, attribute; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:app; 349, identifier:ps; 350, identifier:babel; 351, identifier:cfg; 352, identifier:locales_dirs; 353, identifier:append; 354, argument_list; 354, 355; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:op; 358, identifier:join; 359, argument_list; 359, 360; 359, 361; 360, identifier:PLUGIN_ROOT; 361, string:'locales'; 362, if_statement; 362, 363; 362, 371; 363, not_operator; 363, 364; 364, attribute; 364, 365; 364, 370; 365, attribute; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:app; 368, identifier:ps; 369, identifier:babel; 370, identifier:locale_selector_func; 371, block; 371, 372; 372, expression_statement; 372, 373; 373, assignment; 373, 374; 373, 381; 374, attribute; 374, 375; 374, 380; 375, attribute; 375, 376; 375, 379; 376, attribute; 376, 377; 376, 378; 377, identifier:app; 378, identifier:ps; 379, identifier:babel; 380, identifier:locale_selector_func; 381, attribute; 381, 382; 381, 387; 382, attribute; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, identifier:app; 385, identifier:ps; 386, identifier:babel; 387, identifier:select_locale_by_request
def setup(self, app): """ Initialize the application. """ super().setup(app) self.handlers = OrderedDict() # Connect admin templates app.ps.jinja2.cfg.template_folders.append(op.join(PLUGIN_ROOT, 'templates')) @app.ps.jinja2.filter def admtest(value, a, b=None): return a if value else b @app.ps.jinja2.filter def admeq(a, b, result=True): return result if a == b else not result @app.ps.jinja2.register def admurl(request, prefix): qs = {k: v for k, v in request.query.items() if not k.startswith(prefix)} if not qs: qs = {'ap': 0} return "%s?%s" % (request.path, urlparse.urlencode(qs)) if self.cfg.name is None: self.cfg.name = "%s admin" % app.name.title() # Register a base view if not callable(self.cfg.home): def admin_home(request): yield from self.authorize(request) return app.ps.jinja2.render(self.cfg.template_home, active=None) self.cfg.home = admin_home app.register(self.cfg.prefix)(self.cfg.home) if not self.cfg.i18n: app.ps.jinja2.env.globals.update({ '_': lambda s: s, 'gettext': lambda s: s, 'ngettext': lambda s, p, n: (n != 1 and (p,) or (s,))[0], }) return if 'babel' not in app.ps or not isinstance(app.ps.babel, BPlugin): raise PluginException( 'Plugin `%s` requires for plugin `%s` to be installed to the application.' % ( self.name, BPlugin)) # Connect admin locales app.ps.babel.cfg.locales_dirs.append(op.join(PLUGIN_ROOT, 'locales')) if not app.ps.babel.locale_selector_func: app.ps.babel.locale_selector_func = app.ps.babel.select_locale_by_request
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:passageLoop; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:parent; 5, identifier:new_tree; 6, identifier:xpath1; 7, default_parameter; 7, 8; 7, 9; 8, identifier:xpath2; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:preceding_siblings; 12, False; 13, default_parameter; 13, 14; 13, 15; 14, identifier:following_siblings; 15, False; 16, block; 16, 17; 16, 19; 16, 28; 16, 411; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 24; 21, pattern_list; 21, 22; 21, 23; 22, identifier:current_1; 23, identifier:queue_1; 24, call; 24, 25; 24, 26; 25, identifier:__formatXpath__; 26, argument_list; 26, 27; 27, identifier:xpath1; 28, if_statement; 28, 29; 28, 32; 28, 33; 28, 161; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:xpath2; 31, None; 32, comment; 33, block; 33, 34; 33, 44; 33, 53; 33, 57; 33, 66; 33, 67; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 39; 36, pattern_list; 36, 37; 36, 38; 37, identifier:result_1; 38, identifier:loop; 39, call; 39, 40; 39, 41; 40, identifier:performXpath; 41, argument_list; 41, 42; 41, 43; 42, identifier:parent; 43, identifier:current_1; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:loop; 47, True; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:queue_1; 52, identifier:xpath1; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:central; 56, None; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:has_no_queue; 60, comparison_operator:==; 60, 61; 60, 65; 61, call; 61, 62; 61, 63; 62, identifier:len; 63, argument_list; 63, 64; 64, identifier:queue_1; 65, integer:0; 66, comment; 67, if_statement; 67, 68; 67, 71; 68, boolean_operator:or; 68, 69; 68, 70; 69, identifier:preceding_siblings; 70, identifier:following_siblings; 71, block; 71, 72; 72, for_statement; 72, 73; 72, 74; 72, 78; 73, identifier:sibling; 74, call; 74, 75; 74, 76; 75, identifier:xmliter; 76, argument_list; 76, 77; 77, identifier:parent; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 83; 79, 128; 79, 145; 80, comparison_operator:==; 80, 81; 80, 82; 81, identifier:sibling; 82, identifier:result_1; 83, block; 83, 84; 83, 88; 83, 89; 83, 102; 83, 103; 83, 104; 83, 122; 83, 123; 83, 124; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:central; 87, True; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:child; 92, call; 92, 93; 92, 94; 93, identifier:copyNode; 94, argument_list; 94, 95; 94, 96; 94, 99; 95, identifier:result_1; 96, keyword_argument; 96, 97; 96, 98; 97, identifier:children; 98, identifier:has_no_queue; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:parent; 101, identifier:new_tree; 102, comment; 103, comment; 104, if_statement; 104, 105; 104, 107; 105, not_operator; 105, 106; 106, identifier:has_no_queue; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:passageLoop; 111, argument_list; 111, 112; 111, 113; 111, 114; 111, 115; 111, 116; 111, 119; 112, identifier:result_1; 113, identifier:child; 114, identifier:queue_1; 115, None; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:preceding_siblings; 118, identifier:preceding_siblings; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:following_siblings; 121, identifier:following_siblings; 122, comment; 123, comment; 124, if_statement; 124, 125; 124, 126; 125, identifier:preceding_siblings; 126, block; 126, 127; 127, break_statement; 128, elif_clause; 128, 129; 128, 133; 129, boolean_operator:and; 129, 130; 129, 132; 130, not_operator; 130, 131; 131, identifier:central; 132, identifier:preceding_siblings; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:copyNode; 137, argument_list; 137, 138; 137, 139; 137, 142; 138, identifier:sibling; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:parent; 141, identifier:new_tree; 142, keyword_argument; 142, 143; 142, 144; 143, identifier:children; 144, True; 145, elif_clause; 145, 146; 145, 149; 146, boolean_operator:and; 146, 147; 146, 148; 147, identifier:central; 148, identifier:following_siblings; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 153; 152, identifier:copyNode; 153, argument_list; 153, 154; 153, 155; 153, 158; 154, identifier:sibling; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:parent; 157, identifier:new_tree; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:children; 160, True; 161, else_clause; 161, 162; 162, block; 162, 163; 162, 173; 162, 217; 162, 247; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 168; 165, pattern_list; 165, 166; 165, 167; 166, identifier:result_1; 167, identifier:loop; 168, call; 168, 169; 168, 170; 169, identifier:performXpath; 170, argument_list; 170, 171; 170, 172; 171, identifier:parent; 172, identifier:current_1; 173, if_statement; 173, 174; 173, 177; 173, 206; 174, comparison_operator:is; 174, 175; 174, 176; 175, identifier:loop; 176, True; 177, block; 177, 178; 177, 182; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:queue_1; 181, identifier:xpath1; 182, if_statement; 182, 183; 182, 186; 182, 195; 183, comparison_operator:==; 183, 184; 183, 185; 184, identifier:xpath2; 185, identifier:xpath1; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, pattern_list; 189, 190; 189, 191; 190, identifier:current_2; 191, identifier:queue_2; 192, expression_list; 192, 193; 192, 194; 193, identifier:current_1; 194, identifier:queue_1; 195, else_clause; 195, 196; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, pattern_list; 199, 200; 199, 201; 200, identifier:current_2; 201, identifier:queue_2; 202, call; 202, 203; 202, 204; 203, identifier:__formatXpath__; 204, argument_list; 204, 205; 205, identifier:xpath2; 206, else_clause; 206, 207; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, pattern_list; 210, 211; 210, 212; 211, identifier:current_2; 212, identifier:queue_2; 213, call; 213, 214; 213, 215; 214, identifier:__formatXpath__; 215, argument_list; 215, 216; 216, identifier:xpath2; 217, if_statement; 217, 218; 217, 221; 217, 241; 218, comparison_operator:!=; 218, 219; 218, 220; 219, identifier:xpath1; 220, identifier:xpath2; 221, block; 221, 222; 221, 232; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 227; 224, pattern_list; 224, 225; 224, 226; 225, identifier:result_2; 226, identifier:loop; 227, call; 227, 228; 227, 229; 228, identifier:performXpath; 229, argument_list; 229, 230; 229, 231; 230, identifier:parent; 231, identifier:current_2; 232, if_statement; 232, 233; 232, 236; 233, comparison_operator:is; 233, 234; 233, 235; 234, identifier:loop; 235, True; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:queue_2; 240, identifier:xpath2; 241, else_clause; 241, 242; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:result_2; 246, identifier:result_1; 247, if_statement; 247, 248; 247, 251; 247, 286; 248, comparison_operator:==; 248, 249; 248, 250; 249, identifier:result_1; 250, identifier:result_2; 251, block; 251, 252; 251, 261; 251, 274; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:has_no_queue; 255, comparison_operator:==; 255, 256; 255, 260; 256, call; 256, 257; 256, 258; 257, identifier:len; 258, argument_list; 258, 259; 259, identifier:queue_1; 260, integer:0; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:child; 264, call; 264, 265; 264, 266; 265, identifier:copyNode; 266, argument_list; 266, 267; 266, 268; 266, 271; 267, identifier:result_1; 268, keyword_argument; 268, 269; 268, 270; 269, identifier:children; 270, identifier:has_no_queue; 271, keyword_argument; 271, 272; 271, 273; 272, identifier:parent; 273, identifier:new_tree; 274, if_statement; 274, 275; 274, 277; 275, not_operator; 275, 276; 276, identifier:has_no_queue; 277, block; 277, 278; 278, expression_statement; 278, 279; 279, call; 279, 280; 279, 281; 280, identifier:passageLoop; 281, argument_list; 281, 282; 281, 283; 281, 284; 281, 285; 282, identifier:result_1; 283, identifier:child; 284, identifier:queue_1; 285, identifier:queue_2; 286, else_clause; 286, 287; 287, block; 287, 288; 287, 292; 287, 293; 287, 374; 287, 383; 287, 396; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:start; 291, False; 292, comment; 293, for_statement; 293, 294; 293, 295; 293, 299; 293, 300; 293, 301; 294, identifier:sibling; 295, call; 295, 296; 295, 297; 296, identifier:xmliter; 297, argument_list; 297, 298; 298, identifier:parent; 299, comment; 300, comment; 301, block; 301, 302; 302, if_statement; 302, 303; 302, 304; 302, 305; 302, 306; 302, 326; 302, 327; 302, 328; 303, identifier:start; 304, comment; 305, comment; 306, block; 306, 307; 307, if_statement; 307, 308; 307, 311; 307, 313; 308, comparison_operator:==; 308, 309; 308, 310; 309, identifier:sibling; 310, identifier:result_2; 311, block; 311, 312; 312, break_statement; 313, else_clause; 313, 314; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, call; 316, 317; 316, 318; 317, identifier:copyNode; 318, argument_list; 318, 319; 318, 320; 318, 323; 319, identifier:sibling; 320, keyword_argument; 320, 321; 320, 322; 321, identifier:parent; 322, identifier:new_tree; 323, keyword_argument; 323, 324; 323, 325; 324, identifier:children; 325, True; 326, comment; 327, comment; 328, elif_clause; 328, 329; 328, 332; 329, comparison_operator:==; 329, 330; 329, 331; 330, identifier:sibling; 331, identifier:result_1; 332, block; 332, 333; 332, 337; 332, 346; 332, 359; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:start; 336, True; 337, expression_statement; 337, 338; 338, assignment; 338, 339; 338, 340; 339, identifier:has_no_queue_1; 340, comparison_operator:==; 340, 341; 340, 345; 341, call; 341, 342; 341, 343; 342, identifier:len; 343, argument_list; 343, 344; 344, identifier:queue_1; 345, integer:0; 346, expression_statement; 346, 347; 347, assignment; 347, 348; 347, 349; 348, identifier:node; 349, call; 349, 350; 349, 351; 350, identifier:copyNode; 351, argument_list; 351, 352; 351, 353; 351, 356; 352, identifier:sibling; 353, keyword_argument; 353, 354; 353, 355; 354, identifier:children; 355, identifier:has_no_queue_1; 356, keyword_argument; 356, 357; 356, 358; 357, identifier:parent; 358, identifier:new_tree; 359, if_statement; 359, 360; 359, 362; 360, not_operator; 360, 361; 361, identifier:has_no_queue_1; 362, block; 362, 363; 363, expression_statement; 363, 364; 364, call; 364, 365; 364, 366; 365, identifier:passageLoop; 366, argument_list; 366, 367; 366, 368; 366, 369; 366, 370; 366, 371; 367, identifier:sibling; 368, identifier:node; 369, identifier:queue_1; 370, None; 371, keyword_argument; 371, 372; 371, 373; 372, identifier:following_siblings; 373, True; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 377; 376, identifier:continue_loop; 377, comparison_operator:==; 377, 378; 377, 382; 378, call; 378, 379; 378, 380; 379, identifier:len; 380, argument_list; 380, 381; 381, identifier:queue_2; 382, integer:0; 383, expression_statement; 383, 384; 384, assignment; 384, 385; 384, 386; 385, identifier:node; 386, call; 386, 387; 386, 388; 387, identifier:copyNode; 388, argument_list; 388, 389; 388, 390; 388, 393; 389, identifier:result_2; 390, keyword_argument; 390, 391; 390, 392; 391, identifier:children; 392, identifier:continue_loop; 393, keyword_argument; 393, 394; 393, 395; 394, identifier:parent; 395, identifier:new_tree; 396, if_statement; 396, 397; 396, 399; 397, not_operator; 397, 398; 398, identifier:continue_loop; 399, block; 399, 400; 400, expression_statement; 400, 401; 401, call; 401, 402; 401, 403; 402, identifier:passageLoop; 403, argument_list; 403, 404; 403, 405; 403, 406; 403, 407; 403, 408; 404, identifier:result_2; 405, identifier:node; 406, identifier:queue_2; 407, None; 408, keyword_argument; 408, 409; 408, 410; 409, identifier:preceding_siblings; 410, True; 411, return_statement; 411, 412; 412, identifier:new_tree
def passageLoop(parent, new_tree, xpath1, xpath2=None, preceding_siblings=False, following_siblings=False): """ Loop over passages to construct and increment new tree given a parent and XPaths :param parent: Parent on which to perform xpath :param new_tree: Parent on which to add nodes :param xpath1: List of xpath elements :type xpath1: [str] :param xpath2: List of xpath elements :type xpath2: [str] :param preceding_siblings: Append preceding siblings of XPath 1/2 match to the tree :param following_siblings: Append following siblings of XPath 1/2 match to the tree :return: Newly incremented tree """ current_1, queue_1 = __formatXpath__(xpath1) if xpath2 is None: # In case we need what is following or preceding our node result_1, loop = performXpath(parent, current_1) if loop is True: queue_1 = xpath1 central = None has_no_queue = len(queue_1) == 0 # For each sibling, when we need them in the context of a range if preceding_siblings or following_siblings: for sibling in xmliter(parent): if sibling == result_1: central = True # We copy the node we looked for (Result_1) child = copyNode(result_1, children=has_no_queue, parent=new_tree) # if we don't have children # we loop over the passage child if not has_no_queue: passageLoop( result_1, child, queue_1, None, preceding_siblings=preceding_siblings, following_siblings=following_siblings ) # If we were waiting for preceding_siblings, we break it off # As we don't need to go further if preceding_siblings: break elif not central and preceding_siblings: copyNode(sibling, parent=new_tree, children=True) elif central and following_siblings: copyNode(sibling, parent=new_tree, children=True) else: result_1, loop = performXpath(parent, current_1) if loop is True: queue_1 = xpath1 if xpath2 == xpath1: current_2, queue_2 = current_1, queue_1 else: current_2, queue_2 = __formatXpath__(xpath2) else: current_2, queue_2 = __formatXpath__(xpath2) if xpath1 != xpath2: result_2, loop = performXpath(parent, current_2) if loop is True: queue_2 = xpath2 else: result_2 = result_1 if result_1 == result_2: has_no_queue = len(queue_1) == 0 child = copyNode(result_1, children=has_no_queue, parent=new_tree) if not has_no_queue: passageLoop( result_1, child, queue_1, queue_2 ) else: start = False # For each sibling for sibling in xmliter(parent): # If we have found start # We copy the node because we are between start and end if start: # If we are at the end # We break the copy if sibling == result_2: break else: copyNode(sibling, parent=new_tree, children=True) # If this is start # Then we copy it and initiate star elif sibling == result_1: start = True has_no_queue_1 = len(queue_1) == 0 node = copyNode(sibling, children=has_no_queue_1, parent=new_tree) if not has_no_queue_1: passageLoop(sibling, node, queue_1, None, following_siblings=True) continue_loop = len(queue_2) == 0 node = copyNode(result_2, children=continue_loop, parent=new_tree) if not continue_loop: passageLoop(result_2, node, queue_2, None, preceding_siblings=True) return new_tree
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 1, 21; 2, function_name:cancellable_wait; 3, parameters; 3, 4; 3, 5; 3, 14; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 8; 6, list_splat_pattern; 6, 7; 7, identifier:awaitables; 8, type; 8, 9; 9, generic_type; 9, 10; 9, 11; 10, identifier:Awaitable; 11, type_parameter; 11, 12; 12, type; 12, 13; 13, identifier:_R; 14, typed_default_parameter; 14, 15; 14, 16; 14, 18; 15, identifier:timeout; 16, type; 16, 17; 17, identifier:float; 18, None; 19, type; 19, 20; 20, identifier:_R; 21, block; 21, 22; 21, 24; 21, 49; 21, 96; 21, 106; 21, 114; 21, 145; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:futures; 27, list_comprehension; 27, 28; 27, 39; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:asyncio; 31, identifier:ensure_future; 32, argument_list; 32, 33; 32, 34; 33, identifier:a; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:loop; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:loop; 39, for_in_clause; 39, 40; 39, 41; 40, identifier:a; 41, binary_operator:+; 41, 42; 41, 43; 42, identifier:awaitables; 43, tuple; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:wait; 48, argument_list; 49, try_statement; 49, 50; 49, 76; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 56; 53, pattern_list; 53, 54; 53, 55; 54, identifier:done; 55, identifier:pending; 56, await; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:asyncio; 60, identifier:wait; 61, argument_list; 61, 62; 61, 63; 61, 66; 61, 71; 62, identifier:futures; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:timeout; 65, identifier:timeout; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:return_when; 68, attribute; 68, 69; 68, 70; 69, identifier:asyncio; 70, identifier:FIRST_COMPLETED; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:loop; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:loop; 76, except_clause; 76, 77; 76, 82; 76, 83; 76, 84; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:asyncio; 80, identifier:futures; 81, identifier:CancelledError; 82, comment; 83, comment; 84, block; 84, 85; 84, 95; 85, for_statement; 85, 86; 85, 87; 85, 88; 86, identifier:future; 87, identifier:futures; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:future; 93, identifier:cancel; 94, argument_list; 95, raise_statement; 96, for_statement; 96, 97; 96, 98; 96, 99; 97, identifier:task; 98, identifier:pending; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:task; 104, identifier:cancel; 105, argument_list; 106, if_statement; 106, 107; 106, 109; 107, not_operator; 107, 108; 108, identifier:done; 109, block; 109, 110; 110, raise_statement; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:TimeoutError; 113, argument_list; 114, if_statement; 114, 115; 114, 120; 114, 121; 114, 122; 115, comparison_operator:is; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:triggered_token; 119, None; 120, comment; 121, comment; 122, block; 122, 123; 122, 133; 123, for_statement; 123, 124; 123, 125; 123, 126; 124, identifier:task; 125, identifier:done; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:task; 131, identifier:exception; 132, argument_list; 133, raise_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:OperationCancelled; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, string:"Cancellation requested by {} token"; 140, identifier:format; 141, argument_list; 141, 142; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:triggered_token; 145, return_statement; 145, 146; 146, call; 146, 147; 146, 154; 147, attribute; 147, 148; 147, 153; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:done; 151, identifier:pop; 152, argument_list; 153, identifier:result; 154, argument_list
async def cancellable_wait(self, *awaitables: Awaitable[_R], timeout: float = None) -> _R: """ Wait for the first awaitable to complete, unless we timeout or the token is triggered. Returns the result of the first awaitable to complete. Raises TimeoutError if we timeout or `~cancel_token.exceptions.OperationCancelled` if the cancel token is triggered. All pending futures are cancelled before returning. """ futures = [asyncio.ensure_future(a, loop=self.loop) for a in awaitables + (self.wait(),)] try: done, pending = await asyncio.wait( futures, timeout=timeout, return_when=asyncio.FIRST_COMPLETED, loop=self.loop, ) except asyncio.futures.CancelledError: # Since we use return_when=asyncio.FIRST_COMPLETED above, we can be sure none of our # futures will be done here, so we don't need to check if any is done before cancelling. for future in futures: future.cancel() raise for task in pending: task.cancel() if not done: raise TimeoutError() if self.triggered_token is not None: # We've been asked to cancel so we don't care about our future, but we must # consume its exception or else asyncio will emit warnings. for task in done: task.exception() raise OperationCancelled( "Cancellation requested by {} token".format(self.triggered_token) ) return done.pop().result()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_tweets_count_times; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:twitter; 5, identifier:count; 6, default_parameter; 6, 7; 6, 8; 7, identifier:query; 8, None; 9, block; 9, 10; 9, 12; 9, 13; 9, 24; 9, 30; 9, 34; 9, 38; 9, 215; 9, 223; 9, 224; 9, 233; 9, 251; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 18; 15, pattern_list; 15, 16; 15, 17; 16, identifier:oldest_id; 17, identifier:newest_id; 18, call; 18, 19; 18, 20; 19, identifier:_get_oldest_id; 20, argument_list; 20, 21; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:query; 23, identifier:query; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:newest_id; 27, boolean_operator:or; 27, 28; 27, 29; 28, identifier:newest_id; 29, identifier:oldest_id; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:all_tweets; 33, list:[]; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:i; 37, integer:0; 38, while_statement; 38, 39; 38, 42; 39, comparison_operator:<; 39, 40; 39, 41; 40, identifier:i; 41, identifier:count; 42, block; 42, 43; 42, 47; 42, 48; 42, 98; 42, 107; 42, 116; 42, 155; 42, 162; 42, 163; 42, 173; 42, 183; 42, 197; 43, expression_statement; 43, 44; 44, augmented_assignment:+=; 44, 45; 44, 46; 45, identifier:i; 46, integer:1; 47, comment; 48, if_statement; 48, 49; 48, 52; 48, 73; 49, comparison_operator:<=; 49, 50; 49, 51; 50, identifier:oldest_id; 51, identifier:newest_id; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:tweets; 56, call; 56, 57; 56, 58; 57, identifier:get_tweets; 58, argument_list; 58, 59; 58, 62; 58, 67; 58, 70; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:query; 61, identifier:query; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:max_id; 64, binary_operator:-; 64, 65; 64, 66; 65, identifier:oldest_id; 66, integer:1; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:count; 69, identifier:TWEETS_PER_SEARCH; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:twitter; 72, identifier:twitter; 73, else_clause; 73, 74; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:tweets; 78, call; 78, 79; 78, 80; 79, identifier:get_tweets; 80, argument_list; 80, 81; 80, 84; 80, 89; 80, 92; 80, 95; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:query; 83, identifier:query; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:max_id; 86, binary_operator:-; 86, 87; 86, 88; 87, identifier:oldest_id; 88, integer:1; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:since_id; 91, identifier:newest_id; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:count; 94, identifier:TWEETS_PER_SEARCH; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:twitter; 97, identifier:twitter; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:rate_limit_remaining; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:twitter; 104, identifier:get_lastfunction_header; 105, argument_list; 105, 106; 106, string:'x-rate-limit-remaining'; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:rate_limit_reset; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:twitter; 113, identifier:get_lastfunction_header; 114, argument_list; 114, 115; 115, string:'x-rate-limit-reset'; 116, if_statement; 116, 117; 116, 122; 116, 123; 116, 141; 117, not_operator; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:len; 120, argument_list; 120, 121; 121, identifier:tweets; 122, comment; 123, block; 123, 124; 123, 140; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:oldest_id; 127, binary_operator:+; 127, 128; 127, 129; 128, identifier:oldest_id; 129, binary_operator:*; 129, 130; 129, 139; 130, parenthesized_expression; 130, 131; 131, binary_operator:+; 131, 132; 131, 138; 132, binary_operator:-; 132, 133; 132, 137; 133, parenthesized_expression; 133, 134; 134, boolean_operator:or; 134, 135; 134, 136; 135, identifier:newest_id; 136, identifier:oldest_id; 137, identifier:oldest_id; 138, integer:1; 139, integer:10000; 140, break_statement; 141, elif_clause; 141, 142; 141, 147; 141, 148; 142, call; 142, 143; 142, 144; 143, identifier:isinstance; 144, argument_list; 144, 145; 144, 146; 145, identifier:tweets; 146, identifier:dict; 147, comment; 148, block; 148, 149; 148, 154; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:print; 152, argument_list; 152, 153; 153, identifier:tweets; 154, break_statement; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:all_tweets; 159, identifier:extend; 160, argument_list; 160, 161; 161, identifier:tweets; 162, comment; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:tweet_ids; 166, set_comprehension; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:t; 169, string:'id'; 170, for_in_clause; 170, 171; 170, 172; 171, identifier:t; 172, identifier:tweets; 173, if_statement; 173, 174; 173, 175; 174, identifier:oldest_id; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:tweet_ids; 180, identifier:add; 181, argument_list; 181, 182; 182, identifier:oldest_id; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, pattern_list; 185, 186; 185, 187; 186, identifier:oldest_id; 187, identifier:newest_id; 188, expression_list; 188, 189; 188, 193; 189, call; 189, 190; 189, 191; 190, identifier:min; 191, argument_list; 191, 192; 192, identifier:tweet_ids; 193, call; 193, 194; 193, 195; 194, identifier:max; 195, argument_list; 195, 196; 196, identifier:tweet_ids; 197, if_statement; 197, 198; 197, 201; 198, comparison_operator:==; 198, 199; 198, 200; 199, identifier:rate_limit_remaining; 200, integer:1; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:time; 206, identifier:sleep; 207, argument_list; 207, 208; 208, binary_operator:-; 208, 209; 208, 210; 209, identifier:rate_limit_reset; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:time; 213, identifier:time; 214, argument_list; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:save_tweets; 218, argument_list; 218, 219; 218, 220; 219, identifier:all_tweets; 220, keyword_argument; 220, 221; 220, 222; 221, identifier:query; 222, identifier:query; 223, comment; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:_set_oldest_id; 227, argument_list; 227, 228; 227, 229; 227, 230; 228, identifier:oldest_id; 229, identifier:newest_id; 230, keyword_argument; 230, 231; 230, 232; 231, identifier:query; 232, identifier:query; 233, if_statement; 233, 234; 233, 240; 234, comparison_operator:==; 234, 235; 234, 239; 235, call; 235, 236; 235, 237; 236, identifier:len; 237, argument_list; 237, 238; 238, identifier:all_tweets; 239, integer:0; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:os; 245, identifier:remove; 246, argument_list; 246, 247; 247, call; 247, 248; 247, 249; 248, identifier:make_oldest_id_path; 249, argument_list; 249, 250; 250, identifier:query; 251, return_statement; 251, 252; 252, expression_list; 252, 253; 252, 257; 253, call; 253, 254; 253, 255; 254, identifier:len; 255, argument_list; 255, 256; 256, identifier:all_tweets; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:twitter; 260, identifier:get_lastfunction_header; 261, argument_list; 261, 262; 262, string:'x-rate-limit-remaining'
def get_tweets_count_times(twitter, count, query=None): r""" hits the twitter api `count` times and grabs tweets for the indicated query""" # get id to start from oldest_id, newest_id = _get_oldest_id(query=query) newest_id = newest_id or oldest_id all_tweets = [] i = 0 while i < count: i += 1 # use search api to request 100 tweets. Twitter returns the most recent (max_id) first if oldest_id <= newest_id: tweets = get_tweets(query=query, max_id=oldest_id - 1, count=TWEETS_PER_SEARCH, twitter=twitter) else: tweets = get_tweets(query=query, max_id=oldest_id - 1, since_id=newest_id, count=TWEETS_PER_SEARCH, twitter=twitter) rate_limit_remaining = twitter.get_lastfunction_header('x-rate-limit-remaining') rate_limit_reset = twitter.get_lastfunction_header('x-rate-limit-reset') if not len(tweets): # not rate limitted, just no tweets returned by query oldest_id = oldest_id + ((newest_id or oldest_id) - oldest_id + 1) * 10000 break elif isinstance(tweets, dict): # rate limit hit, or other twython response error print(tweets) break all_tweets.extend(tweets) # determine new oldest id tweet_ids = {t['id'] for t in tweets} if oldest_id: tweet_ids.add(oldest_id) oldest_id, newest_id = min(tweet_ids), max(tweet_ids) if rate_limit_remaining == 1: time.sleep(rate_limit_reset - time.time()) save_tweets(all_tweets, query=query) # set id to start from for next time _set_oldest_id(oldest_id, newest_id, query=query) if len(all_tweets) == 0: os.remove(make_oldest_id_path(query)) return len(all_tweets), twitter.get_lastfunction_header('x-rate-limit-remaining')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:register; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:cls; 5, identifier:code; 6, identifier:name; 7, default_parameter; 7, 8; 7, 9; 8, identifier:hash_name; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:hash_new; 12, None; 13, block; 13, 14; 13, 16; 13, 28; 13, 29; 13, 46; 13, 74; 13, 75; 13, 89; 13, 90; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 22; 17, not_operator; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:_is_app_specific_func; 20, argument_list; 20, 21; 21, identifier:code; 22, block; 22, 23; 23, raise_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:ValueError; 26, argument_list; 26, 27; 27, string:"only application-specific functions can be registered"; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:name_mapping_data; 32, list:[ # (mapping, name in mapping, error if existing) (cls._func_from_name, name, "function name is already registered for a different function"), (cls._func_from_hash, hash_name, "hashlib name is already registered for a different function")]; 32, 33; 32, 34; 32, 40; 33, comment; 34, tuple; 34, 35; 34, 38; 34, 39; 35, attribute; 35, 36; 35, 37; 36, identifier:cls; 37, identifier:_func_from_name; 38, identifier:name; 39, string:"function name is already registered for a different function"; 40, tuple; 40, 41; 40, 44; 40, 45; 41, attribute; 41, 42; 41, 43; 42, identifier:cls; 43, identifier:_func_from_hash; 44, identifier:hash_name; 45, string:"hashlib name is already registered for a different function"; 46, for_statement; 46, 47; 46, 51; 46, 52; 47, tuple_pattern; 47, 48; 47, 49; 47, 50; 48, identifier:mapping; 49, identifier:nameinmap; 50, identifier:errmsg; 51, identifier:name_mapping_data; 52, block; 52, 53; 52, 63; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:existing_func; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:mapping; 59, identifier:get; 60, argument_list; 60, 61; 60, 62; 61, identifier:nameinmap; 62, identifier:code; 63, if_statement; 63, 64; 63, 67; 64, comparison_operator:!=; 64, 65; 64, 66; 65, identifier:existing_func; 66, identifier:code; 67, block; 67, 68; 68, raise_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:ValueError; 71, argument_list; 71, 72; 71, 73; 72, identifier:errmsg; 73, identifier:existing_func; 74, comment; 75, if_statement; 75, 76; 75, 81; 76, comparison_operator:in; 76, 77; 76, 78; 77, identifier:code; 78, attribute; 78, 79; 78, 80; 79, identifier:cls; 80, identifier:_func_hash; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:cls; 86, identifier:unregister; 87, argument_list; 87, 88; 88, identifier:code; 89, comment; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:cls; 94, identifier:_do_register; 95, argument_list; 95, 96; 95, 97; 95, 98; 95, 99; 96, identifier:code; 97, identifier:name; 98, identifier:hash_name; 99, identifier:hash_new
def register(cls, code, name, hash_name=None, hash_new=None): """Add an application-specific function to the registry. Registers a function with the given `code` (an integer) and `name` (a string, which is added both with only hyphens and only underscores), as well as an optional `hash_name` and `hash_new` constructor for hashlib compatibility. If the application-specific function is already registered, the related data is replaced. Registering a function with a `code` not in the application-specific range (0x00-0xff) or with names already registered for a different function raises a `ValueError`. >>> import hashlib >>> FuncReg.register(0x05, 'md-5', 'md5', hashlib.md5) >>> FuncReg.get('md-5') == FuncReg.get('md_5') == 0x05 True >>> hashobj = FuncReg.hash_from_func(0x05) >>> hashobj.name == 'md5' True >>> FuncReg.func_from_hash(hashobj) == 0x05 True >>> FuncReg.reset() >>> 0x05 in FuncReg False """ if not _is_app_specific_func(code): raise ValueError( "only application-specific functions can be registered") # Check already registered name in different mappings. name_mapping_data = [ # (mapping, name in mapping, error if existing) (cls._func_from_name, name, "function name is already registered for a different function"), (cls._func_from_hash, hash_name, "hashlib name is already registered for a different function")] for (mapping, nameinmap, errmsg) in name_mapping_data: existing_func = mapping.get(nameinmap, code) if existing_func != code: raise ValueError(errmsg, existing_func) # Unregister if existing to ensure no orphan entries. if code in cls._func_hash: cls.unregister(code) # Proceed to registration. cls._do_register(code, name, hash_name, hash_new)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 25; 2, function_name:compile_vocab; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:docs; 5, default_parameter; 5, 6; 5, 7; 6, identifier:limit; 7, float:1e6; 8, default_parameter; 8, 9; 8, 10; 9, identifier:verbose; 10, integer:0; 11, default_parameter; 11, 12; 11, 13; 12, identifier:tokenizer; 13, call; 13, 14; 13, 15; 14, identifier:Tokenizer; 15, argument_list; 15, 16; 15, 19; 15, 22; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:stem; 18, None; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:lower; 21, None; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:strip; 24, None; 25, block; 25, 26; 25, 28; 25, 35; 25, 41; 25, 69; 25, 176; 26, expression_statement; 26, 27; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:tokenizer; 31, call; 31, 32; 31, 33; 32, identifier:make_tokenizer; 33, argument_list; 33, 34; 34, identifier:tokenizer; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:d; 38, call; 38, 39; 38, 40; 39, identifier:Dictionary; 40, argument_list; 41, try_statement; 41, 42; 41, 63; 42, block; 42, 43; 42, 55; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:limit; 46, call; 46, 47; 46, 48; 47, identifier:min; 48, argument_list; 48, 49; 48, 50; 49, identifier:limit; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:docs; 53, identifier:count; 54, argument_list; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:docs; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:docs; 61, identifier:iterator; 62, argument_list; 63, except_clause; 63, 64; 63, 67; 64, tuple; 64, 65; 64, 66; 65, identifier:AttributeError; 66, identifier:TypeError; 67, block; 67, 68; 68, pass_statement; 69, for_statement; 69, 70; 69, 73; 69, 77; 69, 78; 69, 79; 70, pattern_list; 70, 71; 70, 72; 71, identifier:i; 72, identifier:doc; 73, call; 73, 74; 73, 75; 74, identifier:enumerate; 75, argument_list; 75, 76; 76, identifier:docs; 77, comment; 78, comment; 79, block; 79, 80; 79, 128; 79, 134; 79, 148; 80, try_statement; 80, 81; 80, 82; 80, 91; 81, comment; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:doc; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:doc; 89, identifier:values; 90, argument_list; 91, except_clause; 91, 92; 91, 93; 91, 94; 92, identifier:AttributeError; 93, comment; 94, block; 94, 95; 95, if_statement; 95, 96; 95, 102; 95, 119; 96, not_operator; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:isinstance; 99, argument_list; 99, 100; 99, 101; 100, identifier:doc; 101, identifier:str; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:doc; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, string:' '; 109, identifier:join; 110, argument_list; 110, 111; 111, list_comprehension; 111, 112; 111, 116; 112, call; 112, 113; 112, 114; 113, identifier:str; 114, argument_list; 114, 115; 115, identifier:v; 116, for_in_clause; 116, 117; 116, 118; 117, identifier:v; 118, identifier:doc; 119, else_clause; 119, 120; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:doc; 124, call; 124, 125; 124, 126; 125, identifier:str; 126, argument_list; 126, 127; 127, identifier:doc; 128, if_statement; 128, 129; 128, 132; 129, comparison_operator:>=; 129, 130; 129, 131; 130, identifier:i; 131, identifier:limit; 132, block; 132, 133; 133, break_statement; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:d; 138, identifier:add_documents; 139, argument_list; 139, 140; 140, list:[list(tokenizer(doc))]; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:list; 143, argument_list; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:tokenizer; 146, argument_list; 146, 147; 147, identifier:doc; 148, if_statement; 148, 149; 148, 155; 149, boolean_operator:and; 149, 150; 149, 151; 150, identifier:verbose; 151, not_operator; 151, 152; 152, binary_operator:%; 152, 153; 152, 154; 153, identifier:i; 154, integer:100; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:log; 160, identifier:info; 161, argument_list; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, string:'{}: {}'; 165, identifier:format; 166, argument_list; 166, 167; 166, 168; 167, identifier:i; 168, subscript; 168, 169; 168, 173; 169, call; 169, 170; 169, 171; 170, identifier:repr; 171, argument_list; 171, 172; 172, identifier:d; 173, slice; 173, 174; 173, 175; 174, colon; 175, integer:120; 176, return_statement; 176, 177; 177, identifier:d
def compile_vocab(docs, limit=1e6, verbose=0, tokenizer=Tokenizer(stem=None, lower=None, strip=None)): """Get the set of words used anywhere in a sequence of documents and assign an integer id This vectorizer is much faster than the scikit-learn version (and only requires low/constant RAM ?). >>> gen = ('label: ' + chr(ord('A') + i % 3)*3 for i in range(11)) >>> d = compile_vocab(gen, verbose=0) >>> d <gensim.corpora.dictionary.Dictionary ...> >>> print(d) Dictionary(4 unique tokens: [u'AAA', u'BBB', u'CCC', u'label']) >>> sorted(d.token2id.values()) [0, 1, 2, 3] >>> sorted(d.token2id.keys()) [u'AAA', u'BBB', u'CCC', u'label'] """ tokenizer = make_tokenizer(tokenizer) d = Dictionary() try: limit = min(limit, docs.count()) docs = docs.iterator() except (AttributeError, TypeError): pass for i, doc in enumerate(docs): # if isinstance(doc, (tuple, list)) and len(doc) == 2 and isinstance(doc[1], int): # doc, score = docs try: # in case docs is a values() queryset (dicts of records in a DB table) doc = doc.values() except AttributeError: # doc already is a values_list if not isinstance(doc, str): doc = ' '.join([str(v) for v in doc]) else: doc = str(doc) if i >= limit: break d.add_documents([list(tokenizer(doc))]) if verbose and not i % 100: log.info('{}: {}'.format(i, repr(d)[:120])) return d
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parse; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:resource; 6, block; 6, 7; 6, 9; 6, 13; 6, 17; 6, 21; 6, 63; 6, 120; 6, 144; 6, 145; 6, 159; 6, 160; 6, 167; 6, 177; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:textgroups; 12, list:[]; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:texts; 16, list:[]; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:invalids; 20, list:[]; 21, for_statement; 21, 22; 21, 23; 21, 24; 22, identifier:folder; 23, identifier:resource; 24, block; 24, 25; 24, 39; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:cts_files; 28, call; 28, 29; 28, 30; 29, identifier:glob; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, string:"{base_folder}/data/*/__cts__.xml"; 34, identifier:format; 35, argument_list; 35, 36; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:base_folder; 38, identifier:folder; 39, for_statement; 39, 40; 39, 41; 39, 42; 40, identifier:cts_file; 41, identifier:cts_files; 42, block; 42, 43; 42, 54; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 48; 45, pattern_list; 45, 46; 45, 47; 46, identifier:textgroup; 47, identifier:cts_file; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:_parse_textgroup; 52, argument_list; 52, 53; 53, identifier:cts_file; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:textgroups; 58, identifier:append; 59, argument_list; 59, 60; 60, tuple; 60, 61; 60, 62; 61, identifier:textgroup; 62, identifier:cts_file; 63, for_statement; 63, 64; 63, 67; 63, 68; 64, pattern_list; 64, 65; 64, 66; 65, identifier:textgroup; 66, identifier:cts_textgroup_file; 67, identifier:textgroups; 68, block; 68, 69; 68, 90; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:cts_work_files; 72, call; 72, 73; 72, 74; 73, identifier:glob; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, string:"{parent}/*/__cts__.xml"; 78, identifier:format; 79, argument_list; 79, 80; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:parent; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:os; 86, identifier:path; 87, identifier:dirname; 88, argument_list; 88, 89; 89, identifier:cts_textgroup_file; 90, for_statement; 90, 91; 90, 92; 90, 93; 91, identifier:cts_work_file; 92, identifier:cts_work_files; 93, block; 93, 94; 93, 107; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 100; 96, pattern_list; 96, 97; 96, 98; 96, 99; 97, identifier:_; 98, identifier:parsed_texts; 99, identifier:directory; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:_parse_work; 104, argument_list; 104, 105; 104, 106; 105, identifier:cts_work_file; 106, identifier:textgroup; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:texts; 111, identifier:extend; 112, argument_list; 112, 113; 113, list_comprehension; 113, 114; 113, 117; 114, tuple; 114, 115; 114, 116; 115, identifier:text; 116, identifier:directory; 117, for_in_clause; 117, 118; 117, 119; 118, identifier:text; 119, identifier:parsed_texts; 120, for_statement; 120, 121; 120, 124; 120, 125; 120, 126; 121, pattern_list; 121, 122; 121, 123; 122, identifier:text; 123, identifier:directory; 124, identifier:texts; 125, comment; 126, block; 126, 127; 127, if_statement; 127, 128; 127, 136; 128, not_operator; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:_parse_text; 133, argument_list; 133, 134; 133, 135; 134, identifier:text; 135, identifier:directory; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:invalids; 141, identifier:append; 142, argument_list; 142, 143; 143, identifier:text; 144, comment; 145, for_statement; 145, 146; 145, 149; 145, 150; 146, pattern_list; 146, 147; 146, 148; 147, identifier:textgroup; 148, identifier:textgroup_path; 149, identifier:textgroups; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_dispatch_container; 156, argument_list; 156, 157; 156, 158; 157, identifier:textgroup; 158, identifier:textgroup_path; 159, comment; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:_clean_invalids; 165, argument_list; 165, 166; 166, identifier:invalids; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:inventory; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:dispatcher; 176, identifier:collection; 177, return_statement; 177, 178; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:inventory
def parse(self, resource): """ Parse a list of directories and reads it into a collection :param resource: List of folders :return: An inventory resource and a list of CtsTextMetadata metadata-objects """ textgroups = [] texts = [] invalids = [] for folder in resource: cts_files = glob("{base_folder}/data/*/__cts__.xml".format(base_folder=folder)) for cts_file in cts_files: textgroup, cts_file = self._parse_textgroup(cts_file) textgroups.append((textgroup, cts_file)) for textgroup, cts_textgroup_file in textgroups: cts_work_files = glob("{parent}/*/__cts__.xml".format(parent=os.path.dirname(cts_textgroup_file))) for cts_work_file in cts_work_files: _, parsed_texts, directory = self._parse_work(cts_work_file, textgroup) texts.extend([(text, directory) for text in parsed_texts]) for text, directory in texts: # If text_id is not none, the text parsing errored if not self._parse_text(text, directory): invalids.append(text) # Dispatching routine for textgroup, textgroup_path in textgroups: self._dispatch_container(textgroup, textgroup_path) # Clean invalids if there was a need self._clean_invalids(invalids) self.inventory = self.dispatcher.collection return self.inventory
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:generate_address_label; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 24; 5, 40; 5, 58; 5, 83; 5, 97; 5, 98; 5, 131; 5, 132; 5, 142; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 9, attribute; 9, 10; 9, 11; 10, identifier:self; 11, identifier:organisation_name; 12, block; 12, 13; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:address_label; 19, identifier:append; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:organisation_name; 24, if_statement; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:department_name; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:address_label; 35, identifier:append; 36, argument_list; 36, 37; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:department_name; 40, if_statement; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:po_box_number; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:address_label; 51, identifier:append; 52, argument_list; 52, 53; 53, binary_operator:+; 53, 54; 53, 55; 54, string:'PO Box '; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:po_box_number; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:elements; 61, list:[ self.sub_building_name, self.building_name, self.building_number, self.dependent_thoroughfare, self.thoroughfare, self.double_dependent_locality, self.dependent_locality, ]; 61, 62; 61, 65; 61, 68; 61, 71; 61, 74; 61, 77; 61, 80; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:sub_building_name; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:building_name; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:building_number; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:dependent_thoroughfare; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:thoroughfare; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:double_dependent_locality; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:dependent_locality; 83, for_statement; 83, 84; 83, 85; 83, 86; 84, identifier:element; 85, identifier:elements; 86, block; 86, 87; 87, if_statement; 87, 88; 87, 89; 88, identifier:element; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:_append_to_label; 95, argument_list; 95, 96; 96, identifier:element; 97, comment; 98, if_statement; 98, 99; 98, 107; 99, comparison_operator:<; 99, 100; 99, 106; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:address_label; 106, integer:7; 107, block; 107, 108; 108, for_statement; 108, 109; 108, 110; 108, 121; 109, identifier:i; 110, call; 110, 111; 110, 112; 111, identifier:range; 112, argument_list; 112, 113; 113, binary_operator:-; 113, 114; 113, 115; 114, integer:7; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:address_label; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:address_label; 128, identifier:append; 129, argument_list; 129, 130; 130, string:''; 131, comment; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 139; 134, subscript; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:address_label; 138, integer:5; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:post_town; 142, return_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, string:", "; 146, identifier:join; 147, argument_list; 147, 148; 148, list_comprehension; 148, 149; 148, 150; 148, 155; 149, identifier:f; 150, for_in_clause; 150, 151; 150, 152; 151, identifier:f; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:address_label; 155, if_clause; 155, 156; 156, identifier:f
def generate_address_label(self): """Construct a list for address label. Non-empty premises elements are appended to the address label in the order of organisation_name, department_name, po_box_number (which must be prepended with 'PO Box', sub_building_name, building_name, building_number, then the rest of the elements except for Town and Postcode because we want them in their own fields. This isn't strict address label but we're probably loading them into a database. """ if self.organisation_name: self.address_label.append(self.organisation_name) if self.department_name: self.address_label.append(self.department_name) if self.po_box_number: self.address_label.append('PO Box ' + self.po_box_number) elements = [ self.sub_building_name, self.building_name, self.building_number, self.dependent_thoroughfare, self.thoroughfare, self.double_dependent_locality, self.dependent_locality, ] for element in elements: if element: self._append_to_label(element) # pad label to length of 7 if not already if len(self.address_label) < 7: for i in range(7 - len(self.address_label)): self.address_label.append('') # finally, add post town self.address_label[5] = self.post_town return ", ".join([f for f in self.address_label if f])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:verify_token; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:token; 5, identifier:public_key_or_address; 6, default_parameter; 6, 7; 6, 8; 7, identifier:signing_algorithm; 8, string:"ES256K"; 9, block; 9, 10; 9, 12; 9, 19; 9, 25; 9, 35; 9, 47; 9, 57; 9, 69; 9, 79; 9, 90; 9, 97; 9, 104; 9, 111; 9, 186; 9, 217; 9, 223; 9, 242; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:decoded_token; 15, call; 15, 16; 15, 17; 16, identifier:decode_token; 17, argument_list; 17, 18; 18, identifier:token; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:decoded_token_payload; 22, subscript; 22, 23; 22, 24; 23, identifier:decoded_token; 24, string:"payload"; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:not; 26, 27; 26, 28; 27, string:"subject"; 28, identifier:decoded_token_payload; 29, block; 29, 30; 30, raise_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:ValueError; 33, argument_list; 33, 34; 34, string:"Token doesn't have a subject"; 35, if_statement; 35, 36; 35, 41; 36, comparison_operator:not; 36, 37; 36, 38; 37, string:"publicKey"; 38, subscript; 38, 39; 38, 40; 39, identifier:decoded_token_payload; 40, string:"subject"; 41, block; 41, 42; 42, raise_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:ValueError; 45, argument_list; 45, 46; 46, string:"Token doesn't have a subject public key"; 47, if_statement; 47, 48; 47, 51; 48, comparison_operator:not; 48, 49; 48, 50; 49, string:"issuer"; 50, identifier:decoded_token_payload; 51, block; 51, 52; 52, raise_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:ValueError; 55, argument_list; 55, 56; 56, string:"Token doesn't have an issuer"; 57, if_statement; 57, 58; 57, 63; 58, comparison_operator:not; 58, 59; 58, 60; 59, string:"publicKey"; 60, subscript; 60, 61; 60, 62; 61, identifier:decoded_token_payload; 62, string:"issuer"; 63, block; 63, 64; 64, raise_statement; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:ValueError; 67, argument_list; 67, 68; 68, string:"Token doesn't have an issuer public key"; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:not; 70, 71; 70, 72; 71, string:"claim"; 72, identifier:decoded_token_payload; 73, block; 73, 74; 74, raise_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:ValueError; 77, argument_list; 77, 78; 78, string:"Token doesn't have a claim"; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:issuer_public_key; 82, call; 82, 83; 82, 84; 83, identifier:str; 84, argument_list; 84, 85; 85, subscript; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:decoded_token_payload; 88, string:"issuer"; 89, string:"publicKey"; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:public_key_object; 93, call; 93, 94; 93, 95; 94, identifier:ECPublicKey; 95, argument_list; 95, 96; 96, identifier:issuer_public_key; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:compressed_public_key; 100, call; 100, 101; 100, 102; 101, identifier:compress; 102, argument_list; 102, 103; 103, identifier:issuer_public_key; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:decompressed_public_key; 107, call; 107, 108; 107, 109; 108, identifier:decompress; 109, argument_list; 109, 110; 110, identifier:issuer_public_key; 111, if_statement; 111, 112; 111, 119; 111, 145; 111, 179; 112, comparison_operator:==; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:public_key_object; 115, identifier:_type; 116, attribute; 116, 117; 116, 118; 117, identifier:PubkeyType; 118, identifier:compressed; 119, block; 119, 120; 119, 128; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:compressed_address; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:public_key_object; 126, identifier:address; 127, argument_list; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:uncompressed_address; 131, call; 131, 132; 131, 133; 132, identifier:bin_hash160_to_address; 133, argument_list; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:bin_hash160; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:decompress; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:public_key_object; 143, identifier:to_bin; 144, argument_list; 145, elif_clause; 145, 146; 145, 153; 146, comparison_operator:==; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:public_key_object; 149, identifier:_type; 150, attribute; 150, 151; 150, 152; 151, identifier:PubkeyType; 152, identifier:uncompressed; 153, block; 153, 154; 153, 171; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:compressed_address; 157, call; 157, 158; 157, 159; 158, identifier:bin_hash160_to_address; 159, argument_list; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:bin_hash160; 162, argument_list; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:compress; 165, argument_list; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:public_key_object; 169, identifier:to_bin; 170, argument_list; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:uncompressed_address; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:public_key_object; 177, identifier:address; 178, argument_list; 179, else_clause; 179, 180; 180, block; 180, 181; 181, raise_statement; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:ValueError; 184, argument_list; 184, 185; 185, string:"Invalid issuer public key format"; 186, if_statement; 186, 187; 186, 190; 186, 192; 186, 198; 186, 204; 186, 210; 187, comparison_operator:==; 187, 188; 187, 189; 188, identifier:public_key_or_address; 189, identifier:compressed_public_key; 190, block; 190, 191; 191, pass_statement; 192, elif_clause; 192, 193; 192, 196; 193, comparison_operator:==; 193, 194; 193, 195; 194, identifier:public_key_or_address; 195, identifier:decompressed_public_key; 196, block; 196, 197; 197, pass_statement; 198, elif_clause; 198, 199; 198, 202; 199, comparison_operator:==; 199, 200; 199, 201; 200, identifier:public_key_or_address; 201, identifier:compressed_address; 202, block; 202, 203; 203, pass_statement; 204, elif_clause; 204, 205; 204, 208; 205, comparison_operator:==; 205, 206; 205, 207; 206, identifier:public_key_or_address; 207, identifier:uncompressed_address; 208, block; 208, 209; 209, pass_statement; 210, else_clause; 210, 211; 211, block; 211, 212; 212, raise_statement; 212, 213; 213, call; 213, 214; 213, 215; 214, identifier:ValueError; 215, argument_list; 215, 216; 216, string:"Token public key doesn't match the verifying value"; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:token_verifier; 220, call; 220, 221; 220, 222; 221, identifier:TokenVerifier; 222, argument_list; 223, if_statement; 223, 224; 223, 236; 224, not_operator; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:token_verifier; 228, identifier:verify; 229, argument_list; 229, 230; 229, 231; 230, identifier:token; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:public_key_object; 234, identifier:to_pem; 235, argument_list; 236, block; 236, 237; 237, raise_statement; 237, 238; 238, call; 238, 239; 238, 240; 239, identifier:ValueError; 240, argument_list; 240, 241; 241, string:"Token was not signed by the issuer public key"; 242, return_statement; 242, 243; 243, identifier:decoded_token
def verify_token(token, public_key_or_address, signing_algorithm="ES256K"): """ A function for validating an individual token. """ decoded_token = decode_token(token) decoded_token_payload = decoded_token["payload"] if "subject" not in decoded_token_payload: raise ValueError("Token doesn't have a subject") if "publicKey" not in decoded_token_payload["subject"]: raise ValueError("Token doesn't have a subject public key") if "issuer" not in decoded_token_payload: raise ValueError("Token doesn't have an issuer") if "publicKey" not in decoded_token_payload["issuer"]: raise ValueError("Token doesn't have an issuer public key") if "claim" not in decoded_token_payload: raise ValueError("Token doesn't have a claim") issuer_public_key = str(decoded_token_payload["issuer"]["publicKey"]) public_key_object = ECPublicKey(issuer_public_key) compressed_public_key = compress(issuer_public_key) decompressed_public_key = decompress(issuer_public_key) if public_key_object._type == PubkeyType.compressed: compressed_address = public_key_object.address() uncompressed_address = bin_hash160_to_address( bin_hash160( decompress(public_key_object.to_bin()) ) ) elif public_key_object._type == PubkeyType.uncompressed: compressed_address = bin_hash160_to_address( bin_hash160( compress(public_key_object.to_bin()) ) ) uncompressed_address = public_key_object.address() else: raise ValueError("Invalid issuer public key format") if public_key_or_address == compressed_public_key: pass elif public_key_or_address == decompressed_public_key: pass elif public_key_or_address == compressed_address: pass elif public_key_or_address == uncompressed_address: pass else: raise ValueError("Token public key doesn't match the verifying value") token_verifier = TokenVerifier() if not token_verifier.verify(token, public_key_object.to_pem()): raise ValueError("Token was not signed by the issuer public key") return decoded_token
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_translations; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:locale; 6, block; 6, 7; 6, 9; 6, 18; 6, 31; 6, 35; 6, 133; 6, 146; 6, 150; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:locale; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:normalize_locale; 16, argument_list; 16, 17; 17, identifier:locale; 18, if_statement; 18, 19; 18, 24; 19, comparison_operator:in; 19, 20; 19, 21; 20, identifier:locale; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:translations; 24, block; 24, 25; 25, return_statement; 25, 26; 26, subscript; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:translations; 30, identifier:locale; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:translations; 34, dictionary; 35, for_statement; 35, 36; 35, 37; 35, 40; 36, identifier:path; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:dirs; 40, block; 40, 41; 40, 58; 40, 70; 40, 78; 40, 86; 40, 95; 40, 103; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:file; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:os; 48, identifier:path; 49, identifier:join; 50, argument_list; 50, 51; 50, 52; 51, identifier:path; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, string:'{}.py'; 55, identifier:format; 56, argument_list; 56, 57; 57, identifier:locale; 58, if_statement; 58, 59; 58, 68; 59, not_operator; 59, 60; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:os; 64, identifier:path; 65, identifier:isfile; 66, argument_list; 66, 67; 67, identifier:file; 68, block; 68, 69; 69, continue_statement; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:loader; 73, call; 73, 74; 73, 75; 74, identifier:SourceFileLoader; 75, argument_list; 75, 76; 75, 77; 76, identifier:locale; 77, identifier:file; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:locale_dict; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:loader; 84, identifier:load_module; 85, argument_list; 86, if_statement; 86, 87; 86, 93; 87, not_operator; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:hasattr; 90, argument_list; 90, 91; 90, 92; 91, identifier:locale_dict; 92, string:'translations'; 93, block; 93, 94; 94, continue_statement; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:language; 98, call; 98, 99; 98, 100; 99, identifier:getattr; 100, argument_list; 100, 101; 100, 102; 101, identifier:locale_dict; 102, string:'translations'; 103, if_statement; 103, 104; 103, 105; 103, 110; 104, identifier:translations; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:translations; 109, identifier:language; 110, else_clause; 110, 111; 111, block; 111, 112; 111, 129; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:merged; 115, call; 115, 116; 115, 117; 116, identifier:dict; 117, argument_list; 117, 118; 118, binary_operator:|; 118, 119; 118, 124; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:translations; 122, identifier:items; 123, argument_list; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:language; 127, identifier:items; 128, argument_list; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:translations; 132, identifier:merged; 133, if_statement; 133, 134; 133, 135; 134, identifier:translations; 135, block; 135, 136; 135, 144; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 143; 138, subscript; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:translations; 142, identifier:locale; 143, identifier:translations; 144, return_statement; 144, 145; 145, identifier:translations; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:err; 149, string:'No translations found for locale [{}]'; 150, raise_statement; 150, 151; 151, call; 151, 152; 151, 153; 152, identifier:NoTranslations; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:err; 157, identifier:format; 158, argument_list; 158, 159; 159, identifier:locale
def get_translations(self, locale): """ Get translation dictionary Returns a dictionary for locale or raises an exception if such can't be located. If a dictionary for locale was previously loaded returns that, otherwise goes through registered locations and merges any found custom dictionaries with defaults. :param locale: str, locale to load translations :return: dict, translations dictionary """ locale = self.normalize_locale(locale) if locale in self.translations: return self.translations[locale] translations = {} for path in self.dirs: file = os.path.join(path, '{}.py'.format(locale)) if not os.path.isfile(file): continue loader = SourceFileLoader(locale, file) locale_dict = loader.load_module() if not hasattr(locale_dict, 'translations'): continue language = getattr(locale_dict, 'translations') if translations: translations = language else: merged = dict(translations.items() | language.items()) translations = merged if translations: self.translations[locale] = translations return translations err = 'No translations found for locale [{}]' raise NoTranslations(err.format(locale))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:process_request; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:session; 6, block; 6, 7; 6, 9; 6, 17; 6, 30; 6, 238; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:debugger; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:debugger; 16, argument_list; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:debugger_session_id; 20, conditional_expression:if; 20, 21; 20, 26; 20, 29; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:debugger; 24, identifier:session_id; 25, argument_list; 26, comparison_operator:is; 26, 27; 26, 28; 27, identifier:debugger; 28, None; 29, None; 30, try_statement; 30, 31; 30, 213; 31, block; 31, 32; 31, 40; 31, 63; 31, 199; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:request; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:session; 38, identifier:read_request; 39, argument_list; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:debugger_session_id; 43, None; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:debugger; 49, identifier:request; 50, argument_list; 50, 51; 50, 52; 50, 53; 50, 58; 51, identifier:debugger_session_id; 52, identifier:request; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:session; 56, identifier:protocol_version; 57, argument_list; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:session; 61, identifier:protocol; 62, argument_list; 63, try_statement; 63, 64; 63, 152; 64, block; 64, 65; 64, 79; 64, 92; 64, 139; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:target_route; 68, call; 68, 69; 68, 76; 69, attribute; 69, 70; 69, 75; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:route_map; 74, argument_list; 75, identifier:route; 76, argument_list; 76, 77; 76, 78; 77, identifier:request; 78, identifier:self; 79, if_statement; 79, 80; 79, 83; 80, comparison_operator:is; 80, 81; 80, 82; 81, identifier:debugger_session_id; 82, None; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:debugger; 88, identifier:target_route; 89, argument_list; 89, 90; 89, 91; 90, identifier:debugger_session_id; 91, identifier:target_route; 92, if_statement; 92, 93; 92, 96; 92, 107; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:target_route; 95, None; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:response; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:execute; 104, argument_list; 104, 105; 104, 106; 105, identifier:request; 106, identifier:target_route; 107, else_clause; 107, 108; 108, block; 108, 109; 108, 121; 108, 128; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:presenter_cls; 112, call; 112, 113; 112, 120; 113, attribute; 113, 114; 113, 119; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:route_map; 118, argument_list; 119, identifier:error_presenter; 120, argument_list; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:presenter; 124, call; 124, 125; 124, 126; 125, identifier:presenter_cls; 126, argument_list; 126, 127; 127, identifier:request; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:response; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:presenter; 134, identifier:error_code; 135, argument_list; 135, 136; 136, keyword_argument; 136, 137; 136, 138; 137, identifier:code; 138, integer:404; 139, if_statement; 139, 140; 139, 143; 140, comparison_operator:is; 140, 141; 140, 142; 141, identifier:debugger_session_id; 142, None; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:debugger; 148, identifier:response; 149, argument_list; 149, 150; 149, 151; 150, identifier:debugger_session_id; 151, identifier:response; 152, except_clause; 152, 153; 152, 157; 153, as_pattern; 153, 154; 153, 155; 154, identifier:Exception; 155, as_pattern_target; 155, 156; 156, identifier:e; 157, block; 157, 158; 157, 171; 157, 183; 157, 190; 158, if_statement; 158, 159; 158, 162; 159, comparison_operator:is; 159, 160; 159, 161; 160, identifier:debugger_session_id; 161, None; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:debugger; 167, identifier:exception; 168, argument_list; 168, 169; 168, 170; 169, identifier:debugger_session_id; 170, identifier:e; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:presenter_cls; 174, call; 174, 175; 174, 182; 175, attribute; 175, 176; 175, 181; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:route_map; 180, argument_list; 181, identifier:error_presenter; 182, argument_list; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:presenter; 186, call; 186, 187; 186, 188; 187, identifier:presenter_cls; 188, argument_list; 188, 189; 189, identifier:request; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:response; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:presenter; 196, identifier:exception_error; 197, argument_list; 197, 198; 198, identifier:e; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:session; 203, identifier:write_response; 204, argument_list; 204, 205; 204, 206; 204, 207; 205, identifier:request; 206, identifier:response; 207, list_splat; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:response; 211, identifier:__pushed_responses__; 212, argument_list; 213, except_clause; 213, 214; 213, 218; 214, as_pattern; 214, 215; 214, 216; 215, identifier:Exception; 216, as_pattern_target; 216, 217; 217, identifier:e; 218, block; 218, 219; 218, 232; 219, if_statement; 219, 220; 219, 223; 220, comparison_operator:is; 220, 221; 220, 222; 221, identifier:debugger_session_id; 222, None; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:debugger; 228, identifier:exception; 229, argument_list; 229, 230; 229, 231; 230, identifier:debugger_session_id; 231, identifier:e; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:session; 236, identifier:session_close; 237, argument_list; 238, if_statement; 238, 239; 238, 242; 239, comparison_operator:is; 239, 240; 239, 241; 240, identifier:debugger_session_id; 241, None; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:debugger; 247, identifier:finalize; 248, argument_list; 248, 249; 249, identifier:debugger_session_id
def process_request(self, session): """ Process single request from the given session :param session: session for reading requests and writing responses :return: None """ debugger = self.debugger() debugger_session_id = debugger.session_id() if debugger is not None else None try: request = session.read_request() if debugger_session_id is not None: debugger.request( debugger_session_id, request, session.protocol_version(), session.protocol() ) try: target_route = self.route_map().route(request, self) if debugger_session_id is not None: debugger.target_route(debugger_session_id, target_route) if target_route is not None: response = self.execute(request, target_route) else: presenter_cls = self.route_map().error_presenter() presenter = presenter_cls(request) response = presenter.error_code(code=404) if debugger_session_id is not None: debugger.response(debugger_session_id, response) except Exception as e: if debugger_session_id is not None: debugger.exception(debugger_session_id, e) presenter_cls = self.route_map().error_presenter() presenter = presenter_cls(request) response = presenter.exception_error(e) session.write_response(request, response, *response.__pushed_responses__()) except Exception as e: if debugger_session_id is not None: debugger.exception(debugger_session_id, e) session.session_close() if debugger_session_id is not None: debugger.finalize(debugger_session_id)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:find_point_in_section_list; 3, parameters; 3, 4; 3, 5; 4, identifier:point; 5, identifier:section_list; 6, block; 6, 7; 6, 9; 6, 25; 6, 67; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 22; 10, boolean_operator:or; 10, 11; 10, 16; 11, comparison_operator:<; 11, 12; 11, 13; 12, identifier:point; 13, subscript; 13, 14; 13, 15; 14, identifier:section_list; 15, integer:0; 16, comparison_operator:>; 16, 17; 16, 18; 17, identifier:point; 18, subscript; 18, 19; 18, 20; 19, identifier:section_list; 20, unary_operator:-; 20, 21; 21, integer:1; 22, block; 22, 23; 23, return_statement; 23, 24; 24, None; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:in; 26, 27; 26, 28; 27, identifier:point; 28, identifier:section_list; 29, block; 29, 30; 29, 43; 29, 54; 29, 63; 30, if_statement; 30, 31; 30, 37; 31, comparison_operator:==; 31, 32; 31, 33; 32, identifier:point; 33, subscript; 33, 34; 33, 35; 34, identifier:section_list; 35, unary_operator:-; 35, 36; 36, integer:1; 37, block; 37, 38; 38, return_statement; 38, 39; 39, subscript; 39, 40; 39, 41; 40, identifier:section_list; 41, unary_operator:-; 41, 42; 42, integer:2; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:ind; 46, binary_operator:-; 46, 47; 46, 53; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:section_list; 50, identifier:bisect; 51, argument_list; 51, 52; 52, identifier:point; 53, integer:1; 54, if_statement; 54, 55; 54, 58; 55, comparison_operator:==; 55, 56; 55, 57; 56, identifier:ind; 57, integer:0; 58, block; 58, 59; 59, return_statement; 59, 60; 60, subscript; 60, 61; 60, 62; 61, identifier:section_list; 62, integer:0; 63, return_statement; 63, 64; 64, subscript; 64, 65; 64, 66; 65, identifier:section_list; 66, identifier:ind; 67, try_statement; 67, 68; 67, 84; 68, block; 68, 69; 68, 78; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:ind; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:section_list; 75, identifier:bisect; 76, argument_list; 76, 77; 77, identifier:point; 78, return_statement; 78, 79; 79, subscript; 79, 80; 79, 81; 80, identifier:section_list; 81, binary_operator:-; 81, 82; 81, 83; 82, identifier:ind; 83, integer:1; 84, except_clause; 84, 85; 84, 86; 85, identifier:IndexError; 86, block; 86, 87; 87, return_statement; 87, 88; 88, None
def find_point_in_section_list(point, section_list): """Returns the start of the section the given point belongs to. The given list is assumed to contain start points of consecutive sections, except for the final point, assumed to be the end point of the last section. For example, the list [5, 8, 30, 31] is interpreted as the following list of sections: [5-8), [8-30), [30-31], so the points -32, 4.5, 32 and 100 all match no section, while 5 and 7.5 match [5-8) and so for them the function returns 5, and 30, 30.7 and 31 all match [30-31]. Parameters --------- point : float The point for which to match a section. section_list : sortedcontainers.SortedList A list of start points of consecutive sections. Returns ------- float The start of the section the given point belongs to. None if no match was found. Example ------- >>> from sortedcontainers import SortedList >>> seclist = SortedList([5, 8, 30, 31]) >>> find_point_in_section_list(4, seclist) >>> find_point_in_section_list(5, seclist) 5 >>> find_point_in_section_list(27, seclist) 8 >>> find_point_in_section_list(31, seclist) 30 """ if point < section_list[0] or point > section_list[-1]: return None if point in section_list: if point == section_list[-1]: return section_list[-2] ind = section_list.bisect(point)-1 if ind == 0: return section_list[0] return section_list[ind] try: ind = section_list.bisect(point) return section_list[ind-1] except IndexError: return None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:find_range_ix_in_section_list; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:start; 5, identifier:end; 6, identifier:section_list; 7, block; 7, 8; 7, 10; 7, 28; 7, 51; 7, 76; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 23; 11, boolean_operator:or; 11, 12; 11, 18; 12, comparison_operator:>; 12, 13; 12, 14; 13, identifier:start; 14, subscript; 14, 15; 14, 16; 15, identifier:section_list; 16, unary_operator:-; 16, 17; 17, integer:1; 18, comparison_operator:<; 18, 19; 18, 20; 19, identifier:end; 20, subscript; 20, 21; 20, 22; 21, identifier:section_list; 22, integer:0; 23, block; 23, 24; 24, return_statement; 24, 25; 25, list:[0, 0]; 25, 26; 25, 27; 26, integer:0; 27, integer:0; 28, if_statement; 28, 29; 28, 34; 28, 41; 29, comparison_operator:<; 29, 30; 29, 31; 30, identifier:start; 31, subscript; 31, 32; 31, 33; 32, identifier:section_list; 33, integer:0; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:start_section; 38, subscript; 38, 39; 38, 40; 39, identifier:section_list; 40, integer:0; 41, else_clause; 41, 42; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:start_section; 46, call; 46, 47; 46, 48; 47, identifier:find_point_in_section_list; 48, argument_list; 48, 49; 48, 50; 49, identifier:start; 50, identifier:section_list; 51, if_statement; 51, 52; 51, 58; 51, 66; 52, comparison_operator:>; 52, 53; 52, 54; 53, identifier:end; 54, subscript; 54, 55; 54, 56; 55, identifier:section_list; 56, unary_operator:-; 56, 57; 57, integer:1; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:end_section; 62, subscript; 62, 63; 62, 64; 63, identifier:section_list; 64, unary_operator:-; 64, 65; 65, integer:2; 66, else_clause; 66, 67; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:end_section; 71, call; 71, 72; 71, 73; 72, identifier:find_point_in_section_list; 73, argument_list; 73, 74; 73, 75; 74, identifier:end; 75, identifier:section_list; 76, return_statement; 76, 77; 77, list:[ section_list.index(start_section), section_list.index(end_section)+1]; 77, 78; 77, 84; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:section_list; 81, identifier:index; 82, argument_list; 82, 83; 83, identifier:start_section; 84, binary_operator:+; 84, 85; 84, 91; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:section_list; 88, identifier:index; 89, argument_list; 89, 90; 90, identifier:end_section; 91, integer:1
def find_range_ix_in_section_list(start, end, section_list): """Returns the index range all sections belonging to the given range. The given list is assumed to contain start points of consecutive sections, except for the final point, assumed to be the end point of the last section. For example, the list [5, 8, 30, 31] is interpreted as the following list of sections: [5-8), [8-30), [30-31]. As such, this function will return [5,8] for the range (7,9) and [5,8,30] while for (7, 30). Parameters --------- start : float The start of the desired range. end : float The end of the desired range. section_list : sortedcontainers.SortedList A list of start points of consecutive sections. Returns ------- iterable The index range of all sections belonging to the given range. Example ------- >>> from sortedcontainers import SortedList >>> seclist = SortedList([5, 8, 30, 31]) >>> find_range_ix_in_section_list(3, 4, seclist) [0, 0] >>> find_range_ix_in_section_list(6, 7, seclist) [0, 1] >>> find_range_ix_in_section_list(7, 9, seclist) [0, 2] >>> find_range_ix_in_section_list(7, 30, seclist) [0, 3] >>> find_range_ix_in_section_list(7, 321, seclist) [0, 3] >>> find_range_ix_in_section_list(4, 321, seclist) [0, 3] """ if start > section_list[-1] or end < section_list[0]: return [0, 0] if start < section_list[0]: start_section = section_list[0] else: start_section = find_point_in_section_list(start, section_list) if end > section_list[-1]: end_section = section_list[-2] else: end_section = find_point_in_section_list(end, section_list) return [ section_list.index(start_section), section_list.index(end_section)+1]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:find_range_in_section_list; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:start; 5, identifier:end; 6, identifier:section_list; 7, block; 7, 8; 7, 10; 7, 19; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:ind; 13, call; 13, 14; 13, 15; 14, identifier:find_range_ix_in_section_list; 15, argument_list; 15, 16; 15, 17; 15, 18; 16, identifier:start; 17, identifier:end; 18, identifier:section_list; 19, return_statement; 19, 20; 20, subscript; 20, 21; 20, 22; 21, identifier:section_list; 22, slice; 22, 23; 22, 26; 22, 27; 23, subscript; 23, 24; 23, 25; 24, identifier:ind; 25, integer:0; 26, colon; 27, subscript; 27, 28; 27, 29; 28, identifier:ind; 29, integer:1
def find_range_in_section_list(start, end, section_list): """Returns all sections belonging to the given range. The given list is assumed to contain start points of consecutive sections, except for the final point, assumed to be the end point of the last section. For example, the list [5, 8, 30, 31] is interpreted as the following list of sections: [5-8), [8-30), [30-31]. As such, this function will return [5,8] for the range (7,9) and [5,8,30] while for (7, 30). Parameters --------- start : float The start of the desired range. end : float The end of the desired range. section_list : sortedcontainers.SortedList A list of start points of consecutive sections. Returns ------- iterable The starting points of all sections belonging to the given range. Example ------- >>> from sortedcontainers import SortedList >>> seclist = SortedList([5, 8, 30, 31]) >>> find_range_in_section_list(3, 4, seclist) [] >>> find_range_in_section_list(6, 7, seclist) [5] >>> find_range_in_section_list(7, 9, seclist) [5, 8] >>> find_range_in_section_list(7, 30, seclist) [5, 8, 30] >>> find_range_in_section_list(7, 321, seclist) [5, 8, 30] >>> find_range_in_section_list(4, 321, seclist) [5, 8, 30] """ ind = find_range_ix_in_section_list(start, end, section_list) return section_list[ind[0]: ind[1]]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:find_range_ix_in_point_list; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:start; 5, identifier:end; 6, identifier:point_list; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, return_statement; 10, 11; 11, list:[point_list.bisect_left(start), point_list.bisect_right(end)]; 11, 12; 11, 18; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:point_list; 15, identifier:bisect_left; 16, argument_list; 16, 17; 17, identifier:start; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:point_list; 21, identifier:bisect_right; 22, argument_list; 22, 23; 23, identifier:end
def find_range_ix_in_point_list(start, end, point_list): """Returns the index range all points inside the given range. Parameters --------- start : float The start of the desired range. end : float The end of the desired range. point_list : sortedcontainers.SortedList A list of points. Returns ------- iterable The index range of all points inside the given range. Example ------- >>> from sortedcontainers import SortedList >>> point_list = SortedList([5, 8, 15]) >>> find_range_ix_in_point_list(3, 4, point_list) [0, 0] >>> find_range_ix_in_point_list(3, 7, point_list) [0, 1] >>> find_range_ix_in_point_list(3, 8, point_list) [0, 2] >>> find_range_ix_in_point_list(4, 15, point_list) [0, 3] >>> find_range_ix_in_point_list(4, 321, point_list) [0, 3] >>> find_range_ix_in_point_list(6, 321, point_list) [1, 3] """ return [point_list.bisect_left(start), point_list.bisect_right(end)]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:check; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:type_spec; 6, identifier:arg_name; 7, identifier:decorated_function; 8, block; 8, 9; 8, 11; 8, 42; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:raise_exception; 13, parameters; 13, 14; 14, identifier:x_spec; 15, block; 15, 16; 15, 29; 15, 37; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:exc_text; 19, binary_operator:%; 19, 20; 19, 21; 20, string:'Argument "%s" for function "%s" has invalid type'; 21, tuple; 21, 22; 21, 23; 22, identifier:arg_name; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:Verifier; 26, identifier:function_name; 27, argument_list; 27, 28; 28, identifier:decorated_function; 29, expression_statement; 29, 30; 30, augmented_assignment:+=; 30, 31; 30, 32; 31, identifier:exc_text; 32, binary_operator:%; 32, 33; 32, 34; 33, string:' (%s should be %s)'; 34, tuple; 34, 35; 34, 36; 35, identifier:x_spec; 36, identifier:type_spec; 37, raise_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:TypeError; 40, argument_list; 40, 41; 41, identifier:exc_text; 42, if_statement; 42, 43; 42, 51; 42, 157; 42, 188; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:type_spec; 47, tuple; 47, 48; 47, 49; 47, 50; 48, identifier:tuple; 49, identifier:list; 50, identifier:set; 51, block; 51, 52; 51, 74; 52, for_statement; 52, 53; 52, 54; 52, 55; 53, identifier:single_type; 54, identifier:type_spec; 55, block; 55, 56; 56, if_statement; 56, 57; 56, 68; 57, boolean_operator:and; 57, 58; 57, 62; 58, parenthesized_expression; 58, 59; 59, comparison_operator:is; 59, 60; 59, 61; 60, identifier:single_type; 61, None; 62, comparison_operator:is; 62, 63; 62, 67; 63, call; 63, 64; 63, 65; 64, identifier:isclass; 65, argument_list; 65, 66; 66, identifier:single_type; 67, False; 68, block; 68, 69; 69, raise_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:RuntimeError; 72, argument_list; 72, 73; 73, string:'Invalid specification. Must be type or tuple/list/set of types'; 74, if_statement; 74, 75; 74, 78; 74, 127; 75, comparison_operator:in; 75, 76; 75, 77; 76, None; 77, identifier:type_spec; 78, block; 78, 79; 78, 95; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:type_spec; 82, call; 82, 83; 82, 84; 83, identifier:tuple; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:filter; 87, argument_list; 87, 88; 87, 94; 88, lambda; 88, 89; 88, 91; 89, lambda_parameters; 89, 90; 90, identifier:x; 91, comparison_operator:is; 91, 92; 91, 93; 92, identifier:x; 93, None; 94, identifier:type_spec; 95, return_statement; 95, 96; 96, lambda; 96, 97; 96, 99; 97, lambda_parameters; 97, 98; 98, identifier:x; 99, conditional_expression:if; 99, 100; 99, 101; 99, 115; 99, 116; 100, None; 101, boolean_operator:or; 101, 102; 101, 105; 102, comparison_operator:is; 102, 103; 102, 104; 103, identifier:x; 104, None; 105, comparison_operator:is; 105, 106; 105, 114; 106, call; 106, 107; 106, 108; 107, identifier:isinstance; 108, argument_list; 108, 109; 108, 110; 109, identifier:x; 110, call; 110, 111; 110, 112; 111, identifier:tuple; 112, argument_list; 112, 113; 113, identifier:type_spec; 114, True; 115, line_continuation:\; 116, call; 116, 117; 116, 118; 117, identifier:raise_exception; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:str; 121, argument_list; 121, 122; 122, parenthesized_expression; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:type; 125, argument_list; 125, 126; 126, identifier:x; 127, else_clause; 127, 128; 128, block; 128, 129; 129, return_statement; 129, 130; 130, lambda; 130, 131; 130, 133; 131, lambda_parameters; 131, 132; 132, identifier:x; 133, conditional_expression:if; 133, 134; 133, 135; 133, 145; 133, 146; 134, None; 135, comparison_operator:is; 135, 136; 135, 144; 136, call; 136, 137; 136, 138; 137, identifier:isinstance; 138, argument_list; 138, 139; 138, 140; 139, identifier:x; 140, call; 140, 141; 140, 142; 141, identifier:tuple; 142, argument_list; 142, 143; 143, identifier:type_spec; 144, True; 145, line_continuation:\; 146, call; 146, 147; 146, 148; 147, identifier:raise_exception; 148, argument_list; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:str; 151, argument_list; 151, 152; 152, parenthesized_expression; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:type; 155, argument_list; 155, 156; 156, identifier:x; 157, elif_clause; 157, 158; 157, 162; 158, call; 158, 159; 158, 160; 159, identifier:isclass; 160, argument_list; 160, 161; 161, identifier:type_spec; 162, block; 162, 163; 163, return_statement; 163, 164; 164, lambda; 164, 165; 164, 167; 165, lambda_parameters; 165, 166; 166, identifier:x; 167, conditional_expression:if; 167, 168; 167, 169; 167, 176; 167, 177; 168, None; 169, comparison_operator:is; 169, 170; 169, 175; 170, call; 170, 171; 170, 172; 171, identifier:isinstance; 172, argument_list; 172, 173; 172, 174; 173, identifier:x; 174, identifier:type_spec; 175, True; 176, line_continuation:\; 177, call; 177, 178; 177, 179; 178, identifier:raise_exception; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:str; 182, argument_list; 182, 183; 183, parenthesized_expression; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:type; 186, argument_list; 186, 187; 187, identifier:x; 188, else_clause; 188, 189; 189, block; 189, 190; 190, raise_statement; 190, 191; 191, call; 191, 192; 191, 193; 192, identifier:RuntimeError; 193, argument_list; 193, 194; 194, string:'Invalid specification. Must be type or tuple/list/set of types'
def check(self, type_spec, arg_name, decorated_function): """ Return callable that checks function parameter for type validity. Checks parameter if it is instance of specified class or classes :param type_spec: type or list/tuple/set of types :param arg_name: function parameter name :param decorated_function: target function :return: function """ def raise_exception(x_spec): exc_text = 'Argument "%s" for function "%s" has invalid type' % ( arg_name, Verifier.function_name(decorated_function) ) exc_text += ' (%s should be %s)' % (x_spec, type_spec) raise TypeError(exc_text) if isinstance(type_spec, (tuple, list, set)): for single_type in type_spec: if (single_type is not None) and isclass(single_type) is False: raise RuntimeError( 'Invalid specification. Must be type or tuple/list/set of types' ) if None in type_spec: type_spec = tuple(filter(lambda x: x is not None, type_spec)) return lambda x: None if x is None or isinstance(x, tuple(type_spec)) is True else \ raise_exception(str((type(x)))) else: return lambda x: None if isinstance(x, tuple(type_spec)) is True else \ raise_exception(str((type(x)))) elif isclass(type_spec): return lambda x: None if isinstance(x, type_spec) is True else \ raise_exception(str((type(x)))) else: raise RuntimeError('Invalid specification. Must be type or tuple/list/set of types')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:check; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:type_spec; 6, identifier:arg_name; 7, identifier:decorated_function; 8, block; 8, 9; 8, 11; 8, 40; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:raise_exception; 13, parameters; 13, 14; 14, identifier:text_spec; 15, block; 15, 16; 15, 29; 15, 35; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:exc_text; 19, binary_operator:%; 19, 20; 19, 21; 20, string:'Argument "%s" for function "%s" has invalid type'; 21, tuple; 21, 22; 21, 23; 22, identifier:arg_name; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:Verifier; 26, identifier:function_name; 27, argument_list; 27, 28; 28, identifier:decorated_function; 29, expression_statement; 29, 30; 30, augmented_assignment:+=; 30, 31; 30, 32; 31, identifier:exc_text; 32, binary_operator:%; 32, 33; 32, 34; 33, string:' (%s)'; 34, identifier:text_spec; 35, raise_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:TypeError; 38, argument_list; 38, 39; 39, identifier:exc_text; 40, if_statement; 40, 41; 40, 49; 40, 158; 40, 193; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 45; 44, identifier:type_spec; 45, tuple; 45, 46; 45, 47; 45, 48; 46, identifier:tuple; 47, identifier:list; 48, identifier:set; 49, block; 49, 50; 49, 72; 50, for_statement; 50, 51; 50, 52; 50, 53; 51, identifier:single_type; 52, identifier:type_spec; 53, block; 53, 54; 54, if_statement; 54, 55; 54, 66; 55, boolean_operator:and; 55, 56; 55, 60; 56, parenthesized_expression; 56, 57; 57, comparison_operator:is; 57, 58; 57, 59; 58, identifier:single_type; 59, None; 60, comparison_operator:is; 60, 61; 60, 65; 61, call; 61, 62; 61, 63; 62, identifier:isclass; 63, argument_list; 63, 64; 64, identifier:single_type; 65, False; 66, block; 66, 67; 67, raise_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:RuntimeError; 70, argument_list; 70, 71; 71, string:'Invalid specification. Must be type or tuple/list/set of types'; 72, if_statement; 72, 73; 72, 76; 72, 127; 73, comparison_operator:in; 73, 74; 73, 75; 74, None; 75, identifier:type_spec; 76, block; 76, 77; 76, 93; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:type_spec; 80, call; 80, 81; 80, 82; 81, identifier:tuple; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:filter; 85, argument_list; 85, 86; 85, 92; 86, lambda; 86, 87; 86, 89; 87, lambda_parameters; 87, 88; 88, identifier:x; 89, comparison_operator:is; 89, 90; 89, 91; 90, identifier:x; 91, None; 92, identifier:type_spec; 93, return_statement; 93, 94; 94, lambda; 94, 95; 94, 97; 95, lambda_parameters; 95, 96; 96, identifier:x; 97, conditional_expression:if; 97, 98; 97, 99; 97, 100; 97, 119; 97, 120; 98, None; 99, line_continuation:\; 100, boolean_operator:or; 100, 101; 100, 104; 101, comparison_operator:is; 101, 102; 101, 103; 102, identifier:x; 103, None; 104, parenthesized_expression; 104, 105; 105, boolean_operator:and; 105, 106; 105, 112; 106, comparison_operator:is; 106, 107; 106, 111; 107, call; 107, 108; 107, 109; 108, identifier:isclass; 109, argument_list; 109, 110; 110, identifier:x; 111, True; 112, comparison_operator:is; 112, 113; 112, 118; 113, call; 113, 114; 113, 115; 114, identifier:issubclass; 115, argument_list; 115, 116; 115, 117; 116, identifier:x; 117, identifier:type_spec; 118, True; 119, line_continuation:\; 120, call; 120, 121; 120, 122; 121, identifier:raise_exception; 122, argument_list; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:str; 125, argument_list; 125, 126; 126, identifier:x; 127, else_clause; 127, 128; 128, block; 128, 129; 129, return_statement; 129, 130; 130, lambda; 130, 131; 130, 133; 131, lambda_parameters; 131, 132; 132, identifier:x; 133, conditional_expression:if; 133, 134; 133, 135; 133, 150; 133, 151; 134, None; 135, parenthesized_expression; 135, 136; 136, boolean_operator:and; 136, 137; 136, 143; 137, comparison_operator:is; 137, 138; 137, 142; 138, call; 138, 139; 138, 140; 139, identifier:isclass; 140, argument_list; 140, 141; 141, identifier:x; 142, True; 143, comparison_operator:is; 143, 144; 143, 149; 144, call; 144, 145; 144, 146; 145, identifier:issubclass; 146, argument_list; 146, 147; 146, 148; 147, identifier:x; 148, identifier:type_spec; 149, True; 150, line_continuation:\; 151, call; 151, 152; 151, 153; 152, identifier:raise_exception; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:str; 156, argument_list; 156, 157; 157, identifier:x; 158, elif_clause; 158, 159; 158, 163; 159, call; 159, 160; 159, 161; 160, identifier:isclass; 161, argument_list; 161, 162; 162, identifier:type_spec; 163, block; 163, 164; 164, return_statement; 164, 165; 165, lambda; 165, 166; 165, 168; 166, lambda_parameters; 166, 167; 167, identifier:x; 168, conditional_expression:if; 168, 169; 168, 170; 168, 185; 168, 186; 169, None; 170, parenthesized_expression; 170, 171; 171, boolean_operator:and; 171, 172; 171, 178; 172, comparison_operator:is; 172, 173; 172, 177; 173, call; 173, 174; 173, 175; 174, identifier:isclass; 175, argument_list; 175, 176; 176, identifier:x; 177, True; 178, comparison_operator:is; 178, 179; 178, 184; 179, call; 179, 180; 179, 181; 180, identifier:issubclass; 181, argument_list; 181, 182; 181, 183; 182, identifier:x; 183, identifier:type_spec; 184, True; 185, line_continuation:\; 186, call; 186, 187; 186, 188; 187, identifier:raise_exception; 188, argument_list; 188, 189; 189, call; 189, 190; 189, 191; 190, identifier:str; 191, argument_list; 191, 192; 192, identifier:x; 193, else_clause; 193, 194; 194, block; 194, 195; 195, raise_statement; 195, 196; 196, call; 196, 197; 196, 198; 197, identifier:RuntimeError; 198, argument_list; 198, 199; 199, string:'Invalid specification. Must be type or tuple/list/set of types'
def check(self, type_spec, arg_name, decorated_function): """ Return callable that checks function parameter for class validity. Checks parameter if it is class or subclass of specified class or classes :param type_spec: type or list/tuple/set of types :param arg_name: function parameter name :param decorated_function: target function :return: function """ def raise_exception(text_spec): exc_text = 'Argument "%s" for function "%s" has invalid type' % ( arg_name, Verifier.function_name(decorated_function) ) exc_text += ' (%s)' % text_spec raise TypeError(exc_text) if isinstance(type_spec, (tuple, list, set)): for single_type in type_spec: if (single_type is not None) and isclass(single_type) is False: raise RuntimeError( 'Invalid specification. Must be type or tuple/list/set of types' ) if None in type_spec: type_spec = tuple(filter(lambda x: x is not None, type_spec)) return lambda x: None if \ x is None or (isclass(x) is True and issubclass(x, type_spec) is True) else \ raise_exception(str(x)) else: return lambda x: None if (isclass(x) is True and issubclass(x, type_spec) is True) else \ raise_exception(str(x)) elif isclass(type_spec): return lambda x: None if (isclass(x) is True and issubclass(x, type_spec) is True) else \ raise_exception(str(x)) else: raise RuntimeError('Invalid specification. Must be type or tuple/list/set of types')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:check; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:value_spec; 6, identifier:arg_name; 7, identifier:decorated_function; 8, block; 8, 9; 8, 11; 8, 40; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:raise_exception; 13, parameters; 13, 14; 14, identifier:text_spec; 15, block; 15, 16; 15, 29; 15, 35; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:exc_text; 19, binary_operator:%; 19, 20; 19, 21; 20, string:'Argument "%s" for function "%s" has invalid value'; 21, tuple; 21, 22; 21, 23; 22, identifier:arg_name; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:Verifier; 26, identifier:function_name; 27, argument_list; 27, 28; 28, identifier:decorated_function; 29, expression_statement; 29, 30; 30, augmented_assignment:+=; 30, 31; 30, 32; 31, identifier:exc_text; 32, binary_operator:%; 32, 33; 32, 34; 33, string:' (%s)'; 34, identifier:text_spec; 35, raise_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:ValueError; 38, argument_list; 38, 39; 39, identifier:exc_text; 40, if_statement; 40, 41; 40, 49; 40, 94; 40, 119; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 45; 44, identifier:value_spec; 45, tuple; 45, 46; 45, 47; 45, 48; 46, identifier:tuple; 47, identifier:list; 48, identifier:set; 49, block; 49, 50; 49, 67; 49, 92; 50, for_statement; 50, 51; 50, 52; 50, 53; 51, identifier:single_value; 52, identifier:value_spec; 53, block; 53, 54; 54, if_statement; 54, 55; 54, 61; 55, comparison_operator:is; 55, 56; 55, 60; 56, call; 56, 57; 56, 58; 57, identifier:isfunction; 58, argument_list; 58, 59; 59, identifier:single_value; 60, False; 61, block; 61, 62; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:RuntimeError; 65, argument_list; 65, 66; 66, string:'Invalid specification. Must be function or tuple/list/set of functions'; 67, function_definition; 67, 68; 67, 69; 67, 71; 68, function_name:check; 69, parameters; 69, 70; 70, identifier:x; 71, block; 71, 72; 72, for_statement; 72, 73; 72, 74; 72, 75; 73, identifier:f; 74, identifier:value_spec; 75, block; 75, 76; 76, if_statement; 76, 77; 76, 83; 77, comparison_operator:is; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:f; 80, argument_list; 80, 81; 81, identifier:x; 82, True; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:raise_exception; 87, argument_list; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:str; 90, argument_list; 90, 91; 91, identifier:x; 92, return_statement; 92, 93; 93, identifier:check; 94, elif_clause; 94, 95; 94, 99; 95, call; 95, 96; 95, 97; 96, identifier:isfunction; 97, argument_list; 97, 98; 98, identifier:value_spec; 99, block; 99, 100; 100, return_statement; 100, 101; 101, lambda; 101, 102; 101, 104; 102, lambda_parameters; 102, 103; 103, identifier:x; 104, conditional_expression:if; 104, 105; 104, 106; 104, 112; 105, None; 106, comparison_operator:is; 106, 107; 106, 111; 107, call; 107, 108; 107, 109; 108, identifier:value_spec; 109, argument_list; 109, 110; 110, identifier:x; 111, True; 112, call; 112, 113; 112, 114; 113, identifier:raise_exception; 114, argument_list; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:str; 117, argument_list; 117, 118; 118, identifier:x; 119, else_clause; 119, 120; 120, block; 120, 121; 121, raise_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:RuntimeError; 124, argument_list; 124, 125; 125, string:'Invalid specification. Must be function or tuple/list/set of functions'
def check(self, value_spec, arg_name, decorated_function): """ Return callable that checks function parameter for value validity. Checks parameter if its value passes specified restrictions. :param value_spec: function or list/tuple/set of functions. Each function must accept one parameter and \ must return True or False if it passed restrictions or not. :param arg_name: function parameter name :param decorated_function: target function :return: function """ def raise_exception(text_spec): exc_text = 'Argument "%s" for function "%s" has invalid value' % ( arg_name, Verifier.function_name(decorated_function) ) exc_text += ' (%s)' % text_spec raise ValueError(exc_text) if isinstance(value_spec, (tuple, list, set)): for single_value in value_spec: if isfunction(single_value) is False: raise RuntimeError( 'Invalid specification. Must be function or tuple/list/set of functions' ) def check(x): for f in value_spec: if f(x) is not True: raise_exception(str(x)) return check elif isfunction(value_spec): return lambda x: None if value_spec(x) is True else raise_exception(str(x)) else: raise RuntimeError('Invalid specification. Must be function or tuple/list/set of functions')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:cache_control; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:validator; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:storage; 9, None; 10, block; 10, 11; 10, 13; 10, 23; 10, 32; 10, 43; 10, 130; 11, expression_statement; 11, 12; 12, comment; 13, function_definition; 13, 14; 13, 15; 13, 20; 14, function_name:default_validator; 15, parameters; 15, 16; 15, 18; 16, list_splat_pattern; 16, 17; 17, identifier:args; 18, dictionary_splat_pattern; 18, 19; 19, identifier:kwargs; 20, block; 20, 21; 21, return_statement; 21, 22; 22, True; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:validator; 26, None; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:validator; 31, identifier:default_validator; 32, if_statement; 32, 33; 32, 36; 33, comparison_operator:is; 33, 34; 33, 35; 34, identifier:storage; 35, None; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:storage; 40, call; 40, 41; 40, 42; 41, identifier:WGlobalSingletonCacheStorage; 42, argument_list; 43, function_definition; 43, 44; 43, 45; 43, 47; 44, function_name:first_level_decorator; 45, parameters; 45, 46; 46, identifier:decorated_function; 47, block; 47, 48; 47, 122; 48, function_definition; 48, 49; 48, 50; 48, 56; 49, function_name:second_level_decorator; 50, parameters; 50, 51; 50, 52; 50, 54; 51, identifier:original_function; 52, list_splat_pattern; 52, 53; 53, identifier:args; 54, dictionary_splat_pattern; 54, 55; 55, identifier:kwargs; 56, block; 56, 57; 56, 68; 56, 81; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:validator_check; 60, call; 60, 61; 60, 62; 61, identifier:validator; 62, argument_list; 62, 63; 62, 64; 62, 66; 63, identifier:original_function; 64, list_splat; 64, 65; 65, identifier:args; 66, dictionary_splat; 66, 67; 67, identifier:kwargs; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:cache_entry; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:storage; 74, identifier:get_cache; 75, argument_list; 75, 76; 75, 77; 75, 79; 76, identifier:original_function; 77, list_splat; 77, 78; 78, identifier:args; 79, dictionary_splat; 79, 80; 80, identifier:kwargs; 81, if_statement; 81, 82; 81, 91; 81, 116; 82, boolean_operator:or; 82, 83; 82, 86; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:validator_check; 85, True; 86, comparison_operator:is; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:cache_entry; 89, identifier:has_value; 90, False; 91, block; 91, 92; 91, 102; 91, 114; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:result; 95, call; 95, 96; 95, 97; 96, identifier:original_function; 97, argument_list; 97, 98; 97, 100; 98, list_splat; 98, 99; 99, identifier:args; 100, dictionary_splat; 100, 101; 101, identifier:kwargs; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:storage; 106, identifier:put; 107, argument_list; 107, 108; 107, 109; 107, 110; 107, 112; 108, identifier:result; 109, identifier:original_function; 110, list_splat; 110, 111; 111, identifier:args; 112, dictionary_splat; 112, 113; 113, identifier:kwargs; 114, return_statement; 114, 115; 115, identifier:result; 116, else_clause; 116, 117; 117, block; 117, 118; 118, return_statement; 118, 119; 119, attribute; 119, 120; 119, 121; 120, identifier:cache_entry; 121, identifier:cached_value; 122, return_statement; 122, 123; 123, call; 123, 124; 123, 128; 124, call; 124, 125; 124, 126; 125, identifier:decorator; 126, argument_list; 126, 127; 127, identifier:second_level_decorator; 128, argument_list; 128, 129; 129, identifier:decorated_function; 130, return_statement; 130, 131; 131, identifier:first_level_decorator
def cache_control(validator=None, storage=None): """ Decorator that is used for caching result. :param validator: function, that has following signature (decorated_function, \*args, \*\*kwargs), where \ decorated_function - original function, args - function arguments, kwargs - function keyword arguments. \ This function must return True if cache is valid (old result must be use if it there is one), or False - to \ generate and to store new result. So function that always return True can be used as singleton. And function \ that always return False won't cache anything at all. By default (if no validator is specified), it presumes \ that cache is always valid. :param storage: storage that is used for caching results. see :class:`.WCacheStorage` class. :return: decorated function """ def default_validator(*args, **kwargs): return True if validator is None: validator = default_validator if storage is None: storage = WGlobalSingletonCacheStorage() def first_level_decorator(decorated_function): def second_level_decorator(original_function, *args, **kwargs): validator_check = validator(original_function, *args, **kwargs) cache_entry = storage.get_cache(original_function, *args, **kwargs) if validator_check is not True or cache_entry.has_value is False: result = original_function(*args, **kwargs) storage.put(result, original_function, *args, **kwargs) return result else: return cache_entry.cached_value return decorator(second_level_decorator)(decorated_function) return first_level_decorator
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 1, 14; 2, function_name:parse_int_string; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:int_string; 6, type; 6, 7; 7, identifier:str; 8, type; 8, 9; 9, generic_type; 9, 10; 9, 11; 10, identifier:List; 11, type_parameter; 11, 12; 12, type; 12, 13; 13, identifier:int; 14, block; 14, 15; 14, 17; 14, 34; 14, 44; 14, 54; 14, 63; 14, 75; 14, 170; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:cleaned; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, string:" "; 23, identifier:join; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 33; 26, attribute; 26, 27; 26, 32; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:int_string; 30, identifier:strip; 31, argument_list; 32, identifier:split; 33, argument_list; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:cleaned; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:cleaned; 40, identifier:replace; 41, argument_list; 41, 42; 41, 43; 42, string:" - "; 43, string:"-"; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:cleaned; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:cleaned; 50, identifier:replace; 51, argument_list; 51, 52; 51, 53; 52, string:","; 53, string:" "; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:tokens; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:cleaned; 60, identifier:split; 61, argument_list; 61, 62; 62, string:" "; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 64, 72; 65, identifier:indices; 66, type; 66, 67; 67, generic_type; 67, 68; 67, 69; 68, identifier:Set; 69, type_parameter; 69, 70; 70, type; 70, 71; 71, identifier:int; 72, call; 72, 73; 72, 74; 73, identifier:set; 74, argument_list; 75, for_statement; 75, 76; 75, 77; 75, 78; 76, identifier:token; 77, identifier:tokens; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 83; 79, 146; 80, comparison_operator:in; 80, 81; 80, 82; 81, string:"-"; 82, identifier:token; 83, block; 83, 84; 83, 93; 83, 109; 83, 118; 83, 129; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:endpoints; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:token; 90, identifier:split; 91, argument_list; 91, 92; 92, string:"-"; 93, if_statement; 93, 94; 93, 100; 94, comparison_operator:!=; 94, 95; 94, 99; 95, call; 95, 96; 95, 97; 96, identifier:len; 97, argument_list; 97, 98; 98, identifier:endpoints; 99, integer:2; 100, block; 100, 101; 100, 108; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:LOG; 105, identifier:info; 106, argument_list; 106, 107; 107, string:f"Dropping '{token}' as invalid - weird range."; 108, continue_statement; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:start; 112, call; 112, 113; 112, 114; 113, identifier:int; 114, argument_list; 114, 115; 115, subscript; 115, 116; 115, 117; 116, identifier:endpoints; 117, integer:0; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:end; 121, binary_operator:+; 121, 122; 121, 128; 122, call; 122, 123; 122, 124; 123, identifier:int; 124, argument_list; 124, 125; 125, subscript; 125, 126; 125, 127; 126, identifier:endpoints; 127, integer:1; 128, integer:1; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:indices; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:indices; 135, identifier:union; 136, argument_list; 136, 137; 136, 138; 137, identifier:indices; 138, call; 138, 139; 138, 140; 139, identifier:set; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:range; 143, argument_list; 143, 144; 143, 145; 144, identifier:start; 145, identifier:end; 146, else_clause; 146, 147; 147, block; 147, 148; 148, try_statement; 148, 149; 148, 160; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:indices; 154, identifier:add; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:int; 158, argument_list; 158, 159; 159, identifier:token; 160, except_clause; 160, 161; 160, 162; 161, identifier:ValueError; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:LOG; 167, identifier:info; 168, argument_list; 168, 169; 169, string:f"Dropping '{token}' as invalid - not an int."; 170, return_statement; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:list; 173, argument_list; 173, 174; 174, identifier:indices
def parse_int_string(int_string: str) -> List[int]: """ Given a string like "1 23 4-8 32 1", return a unique list of those integers in the string and the integers in the ranges in the string. Non-numbers ignored. Not necessarily sorted """ cleaned = " ".join(int_string.strip().split()) cleaned = cleaned.replace(" - ", "-") cleaned = cleaned.replace(",", " ") tokens = cleaned.split(" ") indices: Set[int] = set() for token in tokens: if "-" in token: endpoints = token.split("-") if len(endpoints) != 2: LOG.info(f"Dropping '{token}' as invalid - weird range.") continue start = int(endpoints[0]) end = int(endpoints[1]) + 1 indices = indices.union(indices, set(range(start, end))) else: try: indices.add(int(token)) except ValueError: LOG.info(f"Dropping '{token}' as invalid - not an int.") return list(indices)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:locations_within; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:a; 5, identifier:b; 6, identifier:tolerance; 7, block; 7, 8; 7, 10; 7, 14; 7, 15; 7, 22; 7, 85; 7, 104; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:ret; 13, string:''; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:b; 18, call; 18, 19; 18, 20; 19, identifier:dict; 20, argument_list; 20, 21; 21, identifier:b; 22, for_statement; 22, 23; 22, 26; 22, 31; 23, tuple_pattern; 23, 24; 23, 25; 24, identifier:key; 25, identifier:value; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:a; 29, identifier:items; 30, argument_list; 31, block; 31, 32; 31, 44; 31, 81; 32, if_statement; 32, 33; 32, 36; 33, comparison_operator:not; 33, 34; 33, 35; 34, identifier:key; 35, identifier:b; 36, block; 36, 37; 37, raise_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:ValueError; 40, argument_list; 40, 41; 41, binary_operator:+; 41, 42; 41, 43; 42, string:"b does not have the key: "; 43, identifier:key; 44, if_statement; 44, 45; 44, 61; 45, comparison_operator:>; 45, 46; 45, 60; 46, call; 46, 47; 46, 48; 47, identifier:abs; 48, argument_list; 48, 49; 49, binary_operator:-; 49, 50; 49, 54; 50, call; 50, 51; 50, 52; 51, identifier:int; 52, argument_list; 52, 53; 53, identifier:value; 54, call; 54, 55; 54, 56; 55, identifier:int; 56, argument_list; 56, 57; 57, subscript; 57, 58; 57, 59; 58, identifier:b; 59, identifier:key; 60, identifier:tolerance; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, augmented_assignment:+=; 63, 64; 63, 65; 64, identifier:ret; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, string:'key {0} differs: {1} {2}'; 68, identifier:format; 69, argument_list; 69, 70; 69, 71; 69, 75; 70, identifier:key; 71, call; 71, 72; 71, 73; 72, identifier:int; 73, argument_list; 73, 74; 74, identifier:value; 75, call; 75, 76; 75, 77; 76, identifier:int; 77, argument_list; 77, 78; 78, subscript; 78, 79; 78, 80; 79, identifier:b; 80, identifier:key; 81, delete_statement; 81, 82; 82, subscript; 82, 83; 82, 84; 83, identifier:b; 84, identifier:key; 85, if_statement; 85, 86; 85, 87; 86, identifier:b; 87, block; 87, 88; 88, raise_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:ValueError; 91, argument_list; 91, 92; 92, binary_operator:+; 92, 93; 92, 94; 93, string:"keys in b not seen in a: "; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, string:", "; 97, identifier:join; 98, argument_list; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:b; 102, identifier:keys; 103, argument_list; 104, return_statement; 104, 105; 105, identifier:ret
def locations_within(a, b, tolerance): """ Verifies whether two positions are the same. A tolerance value determines how close the two positions must be to be considered "same". The two locations must be dictionaries that have the same keys. If a key is pesent in one but not in the other, this is an error. The values must be integers or anything that can be converted to an integer through ``int``. (If somehow you need floating point precision, this is not the function for you.) Do not rely on this function to determine whether two object have the same keys. If the function finds the locations to be within tolerances, then the two objects have the same keys. Otherwise, you cannot infer anything regarding the keys because the function will return as soon as it knows that the two locations are **not** within tolerance. :param a: First position. :type a: :class:`dict` :param b: Second position. :type b: :class:`dict` :param tolerance: The tolerance within which the two positions must be. :return: An empty string if the comparison is successful. Otherwise, the string contains a description of the differences. :rtype: :class:`str` :raises ValueError: When a key is present in one object but not the other. """ ret = '' # Clone b so that we can destroy it. b = dict(b) for (key, value) in a.items(): if key not in b: raise ValueError("b does not have the key: " + key) if abs(int(value) - int(b[key])) > tolerance: ret += 'key {0} differs: {1} {2}'.format(key, int(value), int(b[key])) del b[key] if b: raise ValueError("keys in b not seen in a: " + ", ".join(b.keys())) return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:persistence2stats; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:rev_docs; 5, default_parameter; 5, 6; 5, 7; 6, identifier:min_persisted; 7, integer:5; 8, default_parameter; 8, 9; 8, 10; 9, identifier:min_visible; 10, integer:1209600; 11, default_parameter; 11, 12; 11, 13; 12, identifier:include; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:exclude; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:verbose; 19, False; 20, block; 20, 21; 20, 23; 20, 34; 20, 41; 20, 48; 20, 60; 20, 72; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:rev_docs; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:mwxml; 30, identifier:utilities; 31, identifier:normalize; 32, argument_list; 32, 33; 33, identifier:rev_docs; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:min_persisted; 37, call; 37, 38; 37, 39; 38, identifier:int; 39, argument_list; 39, 40; 40, identifier:min_persisted; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:min_visible; 44, call; 44, 45; 44, 46; 45, identifier:int; 46, argument_list; 46, 47; 47, identifier:min_visible; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:include; 51, conditional_expression:if; 51, 52; 51, 53; 51, 56; 52, identifier:include; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:include; 55, None; 56, lambda; 56, 57; 56, 59; 57, lambda_parameters; 57, 58; 58, identifier:t; 59, True; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:exclude; 63, conditional_expression:if; 63, 64; 63, 65; 63, 68; 64, identifier:exclude; 65, comparison_operator:is; 65, 66; 65, 67; 66, identifier:exclude; 67, None; 68, lambda; 68, 69; 68, 71; 69, lambda_parameters; 69, 70; 70, identifier:t; 71, False; 72, for_statement; 72, 73; 72, 74; 72, 75; 73, identifier:rev_doc; 74, identifier:rev_docs; 75, block; 75, 76; 75, 82; 75, 110; 75, 135; 75, 299; 75, 319; 75, 328; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:persistence_doc; 79, subscript; 79, 80; 79, 81; 80, identifier:rev_doc; 81, string:'persistence'; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:stats_doc; 85, dictionary; 85, 86; 85, 89; 85, 92; 85, 95; 85, 98; 85, 101; 85, 104; 85, 107; 86, pair; 86, 87; 86, 88; 87, string:'tokens_added'; 88, integer:0; 89, pair; 89, 90; 89, 91; 90, string:'persistent_tokens'; 91, integer:0; 92, pair; 92, 93; 92, 94; 93, string:'non_self_persistent_tokens'; 94, integer:0; 95, pair; 95, 96; 95, 97; 96, string:'sum_log_persisted'; 97, integer:0; 98, pair; 98, 99; 98, 100; 99, string:'sum_log_non_self_persisted'; 100, integer:0; 101, pair; 101, 102; 101, 103; 102, string:'sum_log_seconds_visible'; 103, integer:0; 104, pair; 104, 105; 104, 106; 105, string:'censored'; 106, False; 107, pair; 107, 108; 107, 109; 108, string:'non_self_censored'; 109, False; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:filtered_docs; 113, generator_expression; 113, 114; 113, 115; 113, 120; 114, identifier:t; 115, for_in_clause; 115, 116; 115, 117; 116, identifier:t; 117, subscript; 117, 118; 117, 119; 118, identifier:persistence_doc; 119, string:'tokens'; 120, if_clause; 120, 121; 121, boolean_operator:and; 121, 122; 121, 128; 122, call; 122, 123; 122, 124; 123, identifier:include; 124, argument_list; 124, 125; 125, subscript; 125, 126; 125, 127; 126, identifier:t; 127, string:'text'; 128, not_operator; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:exclude; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 134; 133, identifier:t; 134, string:'text'; 135, for_statement; 135, 136; 135, 137; 135, 138; 136, identifier:token_doc; 137, identifier:filtered_docs; 138, block; 138, 139; 138, 159; 138, 165; 138, 178; 138, 192; 138, 206; 138, 207; 139, if_statement; 139, 140; 139, 141; 140, identifier:verbose; 141, block; 141, 142; 141, 151; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:sys; 147, identifier:stderr; 148, identifier:write; 149, argument_list; 149, 150; 150, string:"."; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 158; 153, attribute; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:sys; 156, identifier:stderr; 157, identifier:flush; 158, argument_list; 159, expression_statement; 159, 160; 160, augmented_assignment:+=; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:stats_doc; 163, string:'tokens_added'; 164, integer:1; 165, expression_statement; 165, 166; 166, augmented_assignment:+=; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:stats_doc; 169, string:'sum_log_persisted'; 170, call; 170, 171; 170, 172; 171, identifier:log; 172, argument_list; 172, 173; 173, binary_operator:+; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:token_doc; 176, string:'persisted'; 177, integer:1; 178, expression_statement; 178, 179; 179, augmented_assignment:+=; 179, 180; 179, 183; 179, 184; 180, subscript; 180, 181; 180, 182; 181, identifier:stats_doc; 182, string:'sum_log_non_self_persisted'; 183, line_continuation:\; 184, call; 184, 185; 184, 186; 185, identifier:log; 186, argument_list; 186, 187; 187, binary_operator:+; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:token_doc; 190, string:'non_self_persisted'; 191, integer:1; 192, expression_statement; 192, 193; 193, augmented_assignment:+=; 193, 194; 193, 197; 193, 198; 194, subscript; 194, 195; 194, 196; 195, identifier:stats_doc; 196, string:'sum_log_seconds_visible'; 197, line_continuation:\; 198, call; 198, 199; 198, 200; 199, identifier:log; 200, argument_list; 200, 201; 201, binary_operator:+; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:token_doc; 204, string:'seconds_visible'; 205, integer:1; 206, comment; 207, if_statement; 207, 208; 207, 213; 207, 226; 208, comparison_operator:>=; 208, 209; 208, 212; 209, subscript; 209, 210; 209, 211; 210, identifier:token_doc; 211, string:'seconds_visible'; 212, identifier:min_visible; 213, block; 213, 214; 213, 220; 214, expression_statement; 214, 215; 215, augmented_assignment:+=; 215, 216; 215, 219; 216, subscript; 216, 217; 216, 218; 217, identifier:stats_doc; 218, string:'persistent_tokens'; 219, integer:1; 220, expression_statement; 220, 221; 221, augmented_assignment:+=; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:stats_doc; 224, string:'non_self_persistent_tokens'; 225, integer:1; 226, else_clause; 226, 227; 226, 228; 227, comment; 228, block; 228, 229; 228, 240; 228, 251; 228, 252; 229, expression_statement; 229, 230; 230, augmented_assignment:+=; 230, 231; 230, 234; 230, 235; 231, subscript; 231, 232; 231, 233; 232, identifier:stats_doc; 233, string:'persistent_tokens'; 234, line_continuation:\; 235, comparison_operator:>=; 235, 236; 235, 239; 236, subscript; 236, 237; 236, 238; 237, identifier:token_doc; 238, string:'persisted'; 239, identifier:min_persisted; 240, expression_statement; 240, 241; 241, augmented_assignment:+=; 241, 242; 241, 245; 241, 246; 242, subscript; 242, 243; 242, 244; 243, identifier:stats_doc; 244, string:'non_self_persistent_tokens'; 245, line_continuation:\; 246, comparison_operator:>=; 246, 247; 246, 250; 247, subscript; 247, 248; 247, 249; 248, identifier:token_doc; 249, string:'non_self_persisted'; 250, identifier:min_persisted; 251, comment; 252, if_statement; 252, 253; 252, 258; 252, 271; 253, comparison_operator:<; 253, 254; 253, 257; 254, subscript; 254, 255; 254, 256; 255, identifier:persistence_doc; 256, string:'seconds_possible'; 257, identifier:min_visible; 258, block; 258, 259; 258, 265; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 264; 261, subscript; 261, 262; 261, 263; 262, identifier:stats_doc; 263, string:'censored'; 264, True; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 270; 267, subscript; 267, 268; 267, 269; 268, identifier:stats_doc; 269, string:'non_self_censored'; 270, True; 271, else_clause; 271, 272; 272, block; 272, 273; 272, 286; 273, if_statement; 273, 274; 273, 279; 274, comparison_operator:<; 274, 275; 274, 278; 275, subscript; 275, 276; 275, 277; 276, identifier:persistence_doc; 277, string:'revisions_processed'; 278, identifier:min_persisted; 279, block; 279, 280; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 285; 282, subscript; 282, 283; 282, 284; 283, identifier:stats_doc; 284, string:'censored'; 285, True; 286, if_statement; 286, 287; 286, 292; 287, comparison_operator:<; 287, 288; 287, 291; 288, subscript; 288, 289; 288, 290; 289, identifier:persistence_doc; 290, string:'non_self_processed'; 291, identifier:min_persisted; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 298; 295, subscript; 295, 296; 295, 297; 296, identifier:stats_doc; 297, string:'non_self_censored'; 298, True; 299, if_statement; 299, 300; 299, 301; 300, identifier:verbose; 301, block; 301, 302; 301, 311; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 309; 304, attribute; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:sys; 307, identifier:stderr; 308, identifier:write; 309, argument_list; 309, 310; 310, string:"\n"; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 318; 313, attribute; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:sys; 316, identifier:stderr; 317, identifier:flush; 318, argument_list; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 326; 321, attribute; 321, 322; 321, 325; 322, subscript; 322, 323; 322, 324; 323, identifier:rev_doc; 324, string:'persistence'; 325, identifier:update; 326, argument_list; 326, 327; 327, identifier:stats_doc; 328, expression_statement; 328, 329; 329, yield; 329, 330; 330, identifier:rev_doc
def persistence2stats(rev_docs, min_persisted=5, min_visible=1209600, include=None, exclude=None, verbose=False): """ Processes a sorted and page-partitioned sequence of revision documents into and adds statistics to the 'persistence' field each token "added" in the revision persisted through future revisions. :Parameters: rev_docs : `iterable` ( `dict` ) JSON documents of revision data containing a 'diff' field as generated by ``dump2diffs``. It's assumed that rev_docs are partitioned by page and otherwise in chronological order. window_size : `int` The size of the window of revisions from which persistence data will be generated. min_persisted : `int` The minimum future revisions that a token must persist in order to be considered "persistent". min_visible : `int` The minimum number of seconds that a token must be visible in order to be considered "persistent". include : `func` A function that returns `True` when a token should be included in statistical processing exclude : `str` | `re.SRE_Pattern` A function that returns `True` when a token should *not* be included in statistical processing (Takes precedence over 'include') verbose : `bool` Prints out dots and stuff to stderr :Returns: A generator of rev_docs with a 'persistence' field containing statistics about individual tokens. """ rev_docs = mwxml.utilities.normalize(rev_docs) min_persisted = int(min_persisted) min_visible = int(min_visible) include = include if include is not None else lambda t: True exclude = exclude if exclude is not None else lambda t: False for rev_doc in rev_docs: persistence_doc = rev_doc['persistence'] stats_doc = { 'tokens_added': 0, 'persistent_tokens': 0, 'non_self_persistent_tokens': 0, 'sum_log_persisted': 0, 'sum_log_non_self_persisted': 0, 'sum_log_seconds_visible': 0, 'censored': False, 'non_self_censored': False } filtered_docs = (t for t in persistence_doc['tokens'] if include(t['text']) and not exclude(t['text'])) for token_doc in filtered_docs: if verbose: sys.stderr.write(".") sys.stderr.flush() stats_doc['tokens_added'] += 1 stats_doc['sum_log_persisted'] += log(token_doc['persisted'] + 1) stats_doc['sum_log_non_self_persisted'] += \ log(token_doc['non_self_persisted'] + 1) stats_doc['sum_log_seconds_visible'] += \ log(token_doc['seconds_visible'] + 1) # Look for time threshold if token_doc['seconds_visible'] >= min_visible: stats_doc['persistent_tokens'] += 1 stats_doc['non_self_persistent_tokens'] += 1 else: # Look for review threshold stats_doc['persistent_tokens'] += \ token_doc['persisted'] >= min_persisted stats_doc['non_self_persistent_tokens'] += \ token_doc['non_self_persisted'] >= min_persisted # Check for censoring if persistence_doc['seconds_possible'] < min_visible: stats_doc['censored'] = True stats_doc['non_self_censored'] = True else: if persistence_doc['revisions_processed'] < min_persisted: stats_doc['censored'] = True if persistence_doc['non_self_processed'] < min_persisted: stats_doc['non_self_censored'] = True if verbose: sys.stderr.write("\n") sys.stderr.flush() rev_doc['persistence'].update(stats_doc) yield rev_doc
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:update_index; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 109; 5, 115; 5, 116; 5, 120; 5, 189; 5, 211; 5, 217; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:idx; 12, dictionary; 13, for_statement; 13, 14; 13, 17; 13, 38; 14, pattern_list; 14, 15; 14, 16; 15, identifier:_; 16, identifier:p; 17, call; 17, 18; 17, 19; 18, identifier:sorted; 19, argument_list; 19, 20; 19, 27; 20, call; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:permissions; 25, identifier:items; 26, argument_list; 27, keyword_argument; 27, 28; 27, 29; 28, identifier:key; 29, lambda; 29, 30; 29, 32; 30, lambda_parameters; 30, 31; 31, identifier:x; 32, call; 32, 33; 32, 34; 33, identifier:str; 34, argument_list; 34, 35; 35, subscript; 35, 36; 35, 37; 36, identifier:x; 37, integer:0; 38, block; 38, 39; 38, 43; 38, 49; 38, 95; 38, 103; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:branch; 42, identifier:idx; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:parent_p; 46, attribute; 46, 47; 46, 48; 47, identifier:const; 48, identifier:PERM_DENY; 49, for_statement; 49, 50; 49, 51; 49, 56; 50, identifier:k; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:p; 54, identifier:namespace; 55, identifier:keys; 56, block; 56, 57; 56, 83; 56, 89; 57, if_statement; 57, 58; 57, 62; 58, not_operator; 58, 59; 59, comparison_operator:in; 59, 60; 59, 61; 60, identifier:k; 61, identifier:branch; 62, block; 62, 63; 62, 72; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:branch; 67, identifier:k; 68, dictionary; 68, 69; 69, pair; 69, 70; 69, 71; 70, string:"__"; 71, identifier:parent_p; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:branch; 77, identifier:k; 78, identifier:update; 79, argument_list; 79, 80; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:__implicit; 82, True; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:branch; 86, subscript; 86, 87; 86, 88; 87, identifier:branch; 88, identifier:k; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:parent_p; 92, subscript; 92, 93; 92, 94; 93, identifier:branch; 94, string:"__"; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:branch; 99, string:"__"; 100, attribute; 100, 101; 100, 102; 101, identifier:p; 102, identifier:value; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:branch; 107, string:"__implicit"; 108, False; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:index; 114, identifier:idx; 115, comment; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:ramap; 119, dictionary; 120, function_definition; 120, 121; 120, 122; 120, 124; 121, function_name:update_ramap; 122, parameters; 122, 123; 123, identifier:branch_idx; 124, block; 124, 125; 124, 132; 124, 163; 124, 187; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:r; 128, dictionary; 128, 129; 129, pair; 129, 130; 129, 131; 130, string:"__"; 131, False; 132, for_statement; 132, 133; 132, 136; 132, 144; 133, pattern_list; 133, 134; 133, 135; 134, identifier:k; 135, identifier:v; 136, call; 136, 137; 136, 138; 137, identifier:list; 138, argument_list; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:branch_idx; 142, identifier:items; 143, argument_list; 144, block; 144, 145; 145, if_statement; 145, 146; 145, 153; 146, boolean_operator:and; 146, 147; 146, 150; 147, comparison_operator:!=; 147, 148; 147, 149; 148, identifier:k; 149, string:"__"; 150, comparison_operator:!=; 150, 151; 150, 152; 151, identifier:k; 152, string:"__implicit"; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:r; 158, identifier:k; 159, call; 159, 160; 159, 161; 160, identifier:update_ramap; 161, argument_list; 161, 162; 162, identifier:v; 163, if_statement; 163, 164; 163, 180; 164, boolean_operator:and; 164, 165; 164, 170; 165, comparison_operator:is; 165, 166; 165, 169; 166, subscript; 166, 167; 166, 168; 167, identifier:branch_idx; 168, string:"__"; 169, None; 170, comparison_operator:!=; 170, 171; 170, 179; 171, parenthesized_expression; 171, 172; 172, binary_operator:&; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:branch_idx; 175, string:"__"; 176, attribute; 176, 177; 176, 178; 177, identifier:const; 178, identifier:PERM_READ; 179, integer:0; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 186; 183, subscript; 183, 184; 183, 185; 184, identifier:r; 185, string:"__"; 186, True; 187, return_statement; 187, 188; 188, identifier:r; 189, for_statement; 189, 190; 189, 193; 189, 201; 190, pattern_list; 190, 191; 190, 192; 191, identifier:k; 192, identifier:v; 193, call; 193, 194; 193, 195; 194, identifier:list; 195, argument_list; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:idx; 199, identifier:items; 200, argument_list; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:ramap; 206, identifier:k; 207, call; 207, 208; 207, 209; 208, identifier:update_ramap; 209, argument_list; 209, 210; 210, identifier:v; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:read_access_map; 216, identifier:ramap; 217, return_statement; 217, 218; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:index
def update_index(self): """ Regenerates the permission index for this set Called everytime a rule is added / removed / modified in the set """ # update index idx = {} for _, p in sorted(self.permissions.items(), key=lambda x: str(x[0])): branch = idx parent_p = const.PERM_DENY for k in p.namespace.keys: if not k in branch: branch[k] = {"__": parent_p} branch[k].update(__implicit=True) branch = branch[k] parent_p = branch["__"] branch["__"] = p.value branch["__implicit"] = False self.index = idx # update read access map ramap = {} def update_ramap(branch_idx): r = {"__": False} for k, v in list(branch_idx.items()): if k != "__" and k != "__implicit": r[k] = update_ramap(v) if branch_idx["__"] is not None and (branch_idx["__"] & const.PERM_READ) != 0: r["__"] = True return r for k, v in list(idx.items()): ramap[k] = update_ramap(v) self.read_access_map = ramap return self.index
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:type_check; 3, parameters; 3, 4; 4, identifier:func_handle; 5, block; 5, 6; 5, 8; 5, 119; 5, 346; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 14; 8, 15; 8, 16; 8, 17; 8, 18; 8, 19; 9, function_name:checkType; 10, parameters; 10, 11; 10, 12; 10, 13; 11, identifier:var_name; 12, identifier:var_val; 13, identifier:annot; 14, comment; 15, comment; 16, comment; 17, comment; 18, comment; 19, block; 19, 20; 19, 95; 19, 96; 20, if_statement; 20, 21; 20, 24; 20, 25; 20, 84; 21, comparison_operator:in; 21, 22; 21, 23; 22, identifier:var_name; 23, identifier:annot; 24, comment; 25, block; 25, 26; 25, 32; 25, 33; 25, 34; 25, 35; 25, 36; 25, 37; 25, 38; 25, 39; 25, 40; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:var_anno; 29, subscript; 29, 30; 29, 31; 30, identifier:annot; 31, identifier:var_name; 32, comment; 33, comment; 34, comment; 35, comment; 36, comment; 37, comment; 38, comment; 39, comment; 40, if_statement; 40, 41; 40, 44; 40, 49; 40, 68; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:var_val; 43, None; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:type_ok; 48, True; 49, elif_clause; 49, 50; 49, 57; 50, parenthesized_expression; 50, 51; 51, comparison_operator:is; 51, 52; 51, 56; 52, call; 52, 53; 52, 54; 53, identifier:type; 54, argument_list; 54, 55; 55, identifier:var_val; 56, identifier:bool; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:type_ok; 61, parenthesized_expression; 61, 62; 62, comparison_operator:in; 62, 63; 62, 67; 63, call; 63, 64; 63, 65; 64, identifier:type; 65, argument_list; 65, 66; 66, identifier:var_val; 67, identifier:var_anno; 68, else_clause; 68, 69; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:type_ok; 73, comparison_operator:in; 73, 74; 73, 75; 74, True; 75, list_comprehension; 75, 76; 75, 81; 76, call; 76, 77; 76, 78; 77, identifier:isinstance; 78, argument_list; 78, 79; 78, 80; 79, identifier:var_val; 80, identifier:_; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:_; 83, identifier:var_anno; 84, else_clause; 84, 85; 84, 86; 85, comment; 86, block; 86, 87; 86, 91; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:var_anno; 90, string:'Unspecified'; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:type_ok; 94, True; 95, comment; 96, if_statement; 96, 97; 96, 99; 97, not_operator; 97, 98; 98, identifier:type_ok; 99, block; 99, 100; 99, 113; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:args; 103, tuple; 103, 104; 103, 105; 103, 108; 103, 109; 104, identifier:var_name; 105, attribute; 105, 106; 105, 107; 106, identifier:func_handle; 107, identifier:__name__; 108, identifier:var_anno; 109, call; 109, 110; 109, 111; 110, identifier:type; 111, argument_list; 111, 112; 112, identifier:var_val; 113, raise_statement; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:QtmacsArgumentError; 116, argument_list; 116, 117; 117, list_splat; 117, 118; 118, identifier:args; 119, decorated_definition; 119, 120; 119, 127; 120, decorator; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:functools; 124, identifier:wraps; 125, argument_list; 125, 126; 126, identifier:func_handle; 127, function_definition; 127, 128; 127, 129; 127, 134; 127, 135; 127, 136; 128, function_name:wrapper; 129, parameters; 129, 130; 129, 132; 130, list_splat_pattern; 130, 131; 131, identifier:args; 132, dictionary_splat_pattern; 132, 133; 133, identifier:kwds; 134, comment; 135, comment; 136, block; 136, 137; 136, 146; 136, 147; 136, 148; 136, 152; 136, 193; 136, 194; 136, 195; 136, 196; 136, 197; 136, 251; 136, 252; 136, 259; 136, 260; 136, 261; 136, 290; 136, 291; 136, 338; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:argspec; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:inspect; 143, identifier:getfullargspec; 144, argument_list; 144, 145; 145, identifier:func_handle; 146, comment; 147, comment; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:annot; 151, dictionary; 152, for_statement; 152, 153; 152, 156; 152, 163; 153, pattern_list; 153, 154; 153, 155; 154, identifier:key; 155, identifier:val; 156, call; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:argspec; 160, identifier:annotations; 161, identifier:items; 162, argument_list; 163, block; 163, 164; 164, if_statement; 164, 165; 164, 176; 164, 183; 165, boolean_operator:or; 165, 166; 165, 171; 166, call; 166, 167; 166, 168; 167, identifier:isinstance; 168, argument_list; 168, 169; 168, 170; 169, identifier:val; 170, identifier:tuple; 171, call; 171, 172; 171, 173; 172, identifier:isinstance; 173, argument_list; 173, 174; 173, 175; 174, identifier:val; 175, identifier:list; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:annot; 181, identifier:key; 182, identifier:val; 183, else_clause; 183, 184; 184, block; 184, 185; 184, 192; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:annot; 189, identifier:key; 190, expression_list; 190, 191; 191, identifier:val; 192, comment; 193, comment; 194, comment; 195, comment; 196, comment; 197, if_statement; 197, 198; 197, 203; 197, 219; 198, comparison_operator:is; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:argspec; 201, identifier:defaults; 202, None; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:defaults; 207, call; 207, 208; 207, 209; 208, identifier:tuple; 209, argument_list; 209, 210; 210, binary_operator:*; 210, 211; 210, 213; 211, list:[None]; 211, 212; 212, None; 213, call; 213, 214; 213, 215; 214, identifier:len; 215, argument_list; 215, 216; 216, attribute; 216, 217; 216, 218; 217, identifier:argspec; 218, identifier:args; 219, else_clause; 219, 220; 220, block; 220, 221; 220, 237; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:num_none; 224, binary_operator:-; 224, 225; 224, 231; 225, call; 225, 226; 225, 227; 226, identifier:len; 227, argument_list; 227, 228; 228, attribute; 228, 229; 228, 230; 229, identifier:argspec; 230, identifier:args; 231, call; 231, 232; 231, 233; 232, identifier:len; 233, argument_list; 233, 234; 234, attribute; 234, 235; 234, 236; 235, identifier:argspec; 236, identifier:defaults; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:defaults; 240, binary_operator:+; 240, 241; 240, 248; 241, call; 241, 242; 241, 243; 242, identifier:tuple; 243, argument_list; 243, 244; 244, binary_operator:*; 244, 245; 244, 247; 245, list:[None]; 245, 246; 246, None; 247, identifier:num_none; 248, attribute; 248, 249; 248, 250; 249, identifier:argspec; 250, identifier:defaults; 251, comment; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:ofs; 255, call; 255, 256; 255, 257; 256, identifier:len; 257, argument_list; 257, 258; 258, identifier:args; 259, comment; 260, comment; 261, for_statement; 261, 262; 261, 265; 261, 275; 261, 276; 262, pattern_list; 262, 263; 262, 264; 263, identifier:idx; 264, identifier:var_name; 265, call; 265, 266; 265, 267; 266, identifier:enumerate; 267, argument_list; 267, 268; 268, subscript; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:argspec; 271, identifier:args; 272, slice; 272, 273; 272, 274; 273, colon; 274, identifier:ofs; 275, comment; 276, block; 276, 277; 276, 283; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:var_val; 280, subscript; 280, 281; 280, 282; 281, identifier:args; 282, identifier:idx; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 286; 285, identifier:checkType; 286, argument_list; 286, 287; 286, 288; 286, 289; 287, identifier:var_name; 288, identifier:var_val; 289, identifier:annot; 290, comment; 291, for_statement; 291, 292; 291, 295; 291, 305; 291, 306; 291, 307; 291, 308; 291, 309; 292, pattern_list; 292, 293; 292, 294; 293, identifier:idx; 294, identifier:var_name; 295, call; 295, 296; 295, 297; 296, identifier:enumerate; 297, argument_list; 297, 298; 298, subscript; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:argspec; 301, identifier:args; 302, slice; 302, 303; 302, 304; 303, identifier:ofs; 304, colon; 305, comment; 306, comment; 307, comment; 308, comment; 309, block; 309, 310; 309, 331; 310, if_statement; 310, 311; 310, 314; 310, 321; 311, comparison_operator:in; 311, 312; 311, 313; 312, identifier:var_name; 313, identifier:kwds; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:var_val; 318, subscript; 318, 319; 318, 320; 319, identifier:kwds; 320, identifier:var_name; 321, else_clause; 321, 322; 322, block; 322, 323; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:var_val; 326, subscript; 326, 327; 326, 328; 327, identifier:defaults; 328, binary_operator:+; 328, 329; 328, 330; 329, identifier:idx; 330, identifier:ofs; 331, expression_statement; 331, 332; 332, call; 332, 333; 332, 334; 333, identifier:checkType; 334, argument_list; 334, 335; 334, 336; 334, 337; 335, identifier:var_name; 336, identifier:var_val; 337, identifier:annot; 338, return_statement; 338, 339; 339, call; 339, 340; 339, 341; 340, identifier:func_handle; 341, argument_list; 341, 342; 341, 344; 342, list_splat; 342, 343; 343, identifier:args; 344, dictionary_splat; 344, 345; 345, identifier:kwds; 346, return_statement; 346, 347; 347, identifier:wrapper
def type_check(func_handle): """ Ensure arguments have the type specified in the annotation signature. Example:: def foo(a, b:str, c:int =0, d:(int, list)=None): pass This function accepts an arbitrary parameter for ``a``, a string for ``b``, an integer for ``c`` which defaults to 0, and either an integer or a list for ``d`` and defaults to ``None``. The decorator does not check return types and considers derived classes as valid (ie. the type check uses the Python native ``isinstance`` to do its job). For instance, if the function is defined as:: @type_check def foo(a: QtGui.QWidget): pass then the following two calls will both succeed:: foo(QtGui.QWidget()) foo(QtGui.QTextEdit()) because ``QTextEdit`` inherits ``QWidget``. .. note:: the check is skipped if the value (either passed or by default) is **None**. |Raises| * **QtmacsArgumentError** if at least one argument has an invalid type. """ def checkType(var_name, var_val, annot): # Retrieve the annotation for this variable and determine # if the type of that variable matches with the annotation. # This annotation is stored in the dictionary ``annot`` # but contains only variables for such an annotation exists, # hence the if/else branch. if var_name in annot: # Fetch the type-annotation of the variable. var_anno = annot[var_name] # Skip the type check if the variable is none, otherwise # check if it is a derived class. The only exception from # the latter rule are binary values, because in Python # # >> isinstance(False, int) # True # # and warrants a special check. if var_val is None: type_ok = True elif (type(var_val) is bool): type_ok = (type(var_val) in var_anno) else: type_ok = True in [isinstance(var_val, _) for _ in var_anno] else: # Variable without annotation are compatible by assumption. var_anno = 'Unspecified' type_ok = True # If the check failed then raise a QtmacsArgumentError. if not type_ok: args = (var_name, func_handle.__name__, var_anno, type(var_val)) raise QtmacsArgumentError(*args) @functools.wraps(func_handle) def wrapper(*args, **kwds): # Retrieve information about all arguments passed to the function, # as well as their annotations in the function signature. argspec = inspect.getfullargspec(func_handle) # Convert all variable annotations that were not specified as a # tuple or list into one, eg. str --> will become (str,) annot = {} for key, val in argspec.annotations.items(): if isinstance(val, tuple) or isinstance(val, list): annot[key] = val else: annot[key] = val, # Note the trailing colon! # Prefix the argspec.defaults tuple with **None** elements to make # its length equal to the number of variables (for sanity in the # code below). Since **None** types are always ignored by this # decorator this change is neutral. if argspec.defaults is None: defaults = tuple([None] * len(argspec.args)) else: num_none = len(argspec.args) - len(argspec.defaults) defaults = tuple([None] * num_none) + argspec.defaults # Shorthand for the number of unnamed arguments. ofs = len(args) # Process the unnamed arguments. These are always the first ``ofs`` # elements in argspec.args. for idx, var_name in enumerate(argspec.args[:ofs]): # Look up the value in the ``args`` variable. var_val = args[idx] checkType(var_name, var_val, annot) # Process the named- and default arguments. for idx, var_name in enumerate(argspec.args[ofs:]): # Extract the argument value. If it was passed to the # function as a named (ie. keyword) argument then extract # it from ``kwds``, otherwise look it up in the tuple with # the default values. if var_name in kwds: var_val = kwds[var_name] else: var_val = defaults[idx + ofs] checkType(var_name, var_val, annot) return func_handle(*args, **kwds) return wrapper
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:qteProcessKey; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:event; 6, identifier:targetObj; 7, block; 7, 8; 7, 10; 7, 11; 7, 21; 7, 28; 7, 39; 7, 40; 7, 41; 7, 77; 7, 78; 7, 87; 7, 88; 7, 96; 7, 127; 7, 128; 7, 129; 7, 130; 7, 131; 7, 144; 7, 145; 7, 154; 7, 363; 7, 364; 7, 365; 7, 366; 7, 377; 7, 384; 7, 395; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:msgObj; 14, call; 14, 15; 14, 16; 15, identifier:QtmacsMessage; 16, argument_list; 16, 17; 16, 20; 17, tuple; 17, 18; 17, 19; 18, identifier:targetObj; 19, identifier:event; 20, None; 21, expression_statement; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:msgObj; 25, identifier:setSignalName; 26, argument_list; 26, 27; 27, string:'qtesigKeypressed'; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 37; 30, attribute; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:qteMain; 35, identifier:qtesigKeypressed; 36, identifier:emit; 37, argument_list; 37, 38; 38, identifier:msgObj; 39, comment; 40, comment; 41, if_statement; 41, 42; 41, 74; 42, comparison_operator:in; 42, 43; 42, 48; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:event; 46, identifier:key; 47, argument_list; 48, tuple; 48, 49; 48, 54; 48, 59; 48, 64; 48, 69; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:QtCore; 52, identifier:Qt; 53, identifier:Key_Shift; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:QtCore; 57, identifier:Qt; 58, identifier:Key_Control; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:QtCore; 62, identifier:Qt; 63, identifier:Key_Meta; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:QtCore; 67, identifier:Qt; 68, identifier:Key_Alt; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:QtCore; 72, identifier:Qt; 73, identifier:Key_AltGr; 74, block; 74, 75; 75, return_statement; 75, 76; 76, False; 77, comment; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_keysequence; 84, identifier:appendQKeyEvent; 85, argument_list; 85, 86; 86, identifier:event; 87, comment; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:isRegisteredWidget; 91, call; 91, 92; 91, 93; 92, identifier:hasattr; 93, argument_list; 93, 94; 93, 95; 94, identifier:targetObj; 95, string:'_qteAdmin'; 96, if_statement; 96, 97; 96, 106; 96, 115; 97, boolean_operator:and; 97, 98; 97, 99; 98, identifier:isRegisteredWidget; 99, call; 99, 100; 99, 101; 100, identifier:hasattr; 101, argument_list; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:targetObj; 104, identifier:_qteAdmin; 105, string:'keyMap'; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:keyMap; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:targetObj; 113, identifier:_qteAdmin; 114, identifier:keyMap; 115, else_clause; 115, 116; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:keyMap; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:qteMain; 125, identifier:_qteGlobalKeyMapByReference; 126, argument_list; 127, comment; 128, comment; 129, comment; 130, comment; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, tuple_pattern; 133, 134; 133, 135; 134, identifier:macroName; 135, identifier:isPartialMatch; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:keyMap; 139, identifier:match; 140, argument_list; 140, 141; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:_keysequence; 144, comment; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:keyseq_copy; 148, call; 148, 149; 148, 150; 149, identifier:QtmacsKeysequence; 150, argument_list; 150, 151; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:_keysequence; 154, if_statement; 154, 155; 154, 156; 154, 157; 154, 158; 154, 159; 154, 248; 155, identifier:isPartialMatch; 156, comment; 157, comment; 158, comment; 159, block; 159, 160; 160, if_statement; 160, 161; 160, 164; 160, 165; 160, 192; 161, comparison_operator:is; 161, 162; 161, 163; 162, identifier:macroName; 163, None; 164, comment; 165, block; 165, 166; 165, 174; 165, 181; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:msgObj; 169, call; 169, 170; 169, 171; 170, identifier:QtmacsMessage; 171, argument_list; 171, 172; 171, 173; 172, identifier:keyseq_copy; 173, None; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:msgObj; 178, identifier:setSignalName; 179, argument_list; 179, 180; 180, string:'qtesigKeyseqPartial'; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 190; 183, attribute; 183, 184; 183, 189; 184, attribute; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:qteMain; 188, identifier:qtesigKeyseqPartial; 189, identifier:emit; 190, argument_list; 190, 191; 191, identifier:msgObj; 192, else_clause; 192, 193; 192, 194; 193, comment; 194, block; 194, 195; 194, 211; 194, 212; 194, 222; 194, 229; 194, 240; 195, if_statement; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:_qteFlagRunMacro; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 207; 202, attribute; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:qteMain; 206, identifier:qteRunMacro; 207, argument_list; 207, 208; 207, 209; 207, 210; 208, identifier:macroName; 209, identifier:targetObj; 210, identifier:keyseq_copy; 211, comment; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:msgObj; 215, call; 215, 216; 215, 217; 216, identifier:QtmacsMessage; 217, argument_list; 217, 218; 217, 221; 218, tuple; 218, 219; 218, 220; 219, identifier:macroName; 220, identifier:keyseq_copy; 221, None; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:msgObj; 226, identifier:setSignalName; 227, argument_list; 227, 228; 228, string:'qtesigKeyseqComplete'; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 238; 231, attribute; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:qteMain; 236, identifier:qtesigKeyseqComplete; 237, identifier:emit; 238, argument_list; 238, 239; 239, identifier:msgObj; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 247; 242, attribute; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:_keysequence; 246, identifier:reset; 247, argument_list; 248, else_clause; 248, 249; 249, block; 249, 250; 249, 355; 250, if_statement; 250, 251; 250, 252; 250, 253; 250, 254; 250, 255; 250, 330; 251, identifier:isRegisteredWidget; 252, comment; 253, comment; 254, comment; 255, block; 255, 256; 255, 264; 255, 274; 255, 284; 255, 293; 255, 304; 255, 312; 255, 319; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:tmp; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:keyseq_copy; 262, identifier:toString; 263, argument_list; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:tmp; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:tmp; 270, identifier:replace; 271, argument_list; 271, 272; 271, 273; 272, string:'<'; 273, string:'&lt;'; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:tmp; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:tmp; 280, identifier:replace; 281, argument_list; 281, 282; 281, 283; 282, string:'>'; 283, string:'&gt;'; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:msg; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, string:'No macro is bound to <b>{}</b>.'; 290, identifier:format; 291, argument_list; 291, 292; 292, identifier:tmp; 293, expression_statement; 293, 294; 294, call; 294, 295; 294, 302; 295, attribute; 295, 296; 295, 301; 296, attribute; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:self; 299, identifier:qteMain; 300, identifier:qteLogger; 301, identifier:warning; 302, argument_list; 302, 303; 303, identifier:msg; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:msgObj; 307, call; 307, 308; 307, 309; 308, identifier:QtmacsMessage; 309, argument_list; 309, 310; 309, 311; 310, identifier:keyseq_copy; 311, None; 312, expression_statement; 312, 313; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:msgObj; 316, identifier:setSignalName; 317, argument_list; 317, 318; 318, string:'qtesigKeyseqInvalid'; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 328; 321, attribute; 321, 322; 321, 327; 322, attribute; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:self; 325, identifier:qteMain; 326, identifier:qtesigKeyseqInvalid; 327, identifier:emit; 328, argument_list; 328, 329; 329, identifier:msgObj; 330, else_clause; 330, 331; 330, 332; 330, 333; 330, 334; 330, 335; 330, 336; 331, comment; 332, comment; 333, comment; 334, comment; 335, comment; 336, block; 336, 337; 337, if_statement; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:self; 340, identifier:_qteFlagRunMacro; 341, block; 341, 342; 342, expression_statement; 342, 343; 343, call; 343, 344; 343, 349; 344, attribute; 344, 345; 344, 348; 345, attribute; 345, 346; 345, 347; 346, identifier:self; 347, identifier:qteMain; 348, identifier:qteRunMacro; 349, argument_list; 349, 350; 349, 353; 349, 354; 350, attribute; 350, 351; 350, 352; 351, identifier:self; 352, identifier:QtDelivery; 353, identifier:targetObj; 354, identifier:keyseq_copy; 355, expression_statement; 355, 356; 356, call; 356, 357; 356, 362; 357, attribute; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:self; 360, identifier:_keysequence; 361, identifier:reset; 362, argument_list; 363, comment; 364, comment; 365, comment; 366, expression_statement; 366, 367; 367, assignment; 367, 368; 367, 369; 368, identifier:msgObj; 369, call; 369, 370; 369, 371; 370, identifier:QtmacsMessage; 371, argument_list; 371, 372; 371, 376; 372, tuple; 372, 373; 372, 374; 372, 375; 373, identifier:targetObj; 374, identifier:keyseq_copy; 375, identifier:macroName; 376, None; 377, expression_statement; 377, 378; 378, call; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:msgObj; 381, identifier:setSignalName; 382, argument_list; 382, 383; 383, string:'qtesigKeyparsed'; 384, expression_statement; 384, 385; 385, call; 385, 386; 385, 393; 386, attribute; 386, 387; 386, 392; 387, attribute; 387, 388; 387, 391; 388, attribute; 388, 389; 388, 390; 389, identifier:self; 390, identifier:qteMain; 391, identifier:qtesigKeyparsed; 392, identifier:emit; 393, argument_list; 393, 394; 394, identifier:msgObj; 395, return_statement; 395, 396; 396, identifier:isPartialMatch
def qteProcessKey(self, event, targetObj): """ If the key completes a valid key sequence then queue the associated macro. |Args| * ``targetObj`` (**QObject**): the source of the event (see Qt documentation). * ``event_qt`` (**QKeyEvent**): information about the key event (see Qt documentation). |Returns| * **Bool**: **True** if there was at least a partial match and **False** if the key sequence was invalid. |Raises| * **None** """ # Announce the key and targeted Qtmacs widget. msgObj = QtmacsMessage((targetObj, event), None) msgObj.setSignalName('qtesigKeypressed') self.qteMain.qtesigKeypressed.emit(msgObj) # Ignore standalone <Shift>, <Ctrl>, <Win>, <Alt>, and <AltGr> # events. if event.key() in (QtCore.Qt.Key_Shift, QtCore.Qt.Key_Control, QtCore.Qt.Key_Meta, QtCore.Qt.Key_Alt, QtCore.Qt.Key_AltGr): return False # Add the latest key stroke to the current key sequence. self._keysequence.appendQKeyEvent(event) # Determine if the widget was registered with qteAddWidget isRegisteredWidget = hasattr(targetObj, '_qteAdmin') if isRegisteredWidget and hasattr(targetObj._qteAdmin, 'keyMap'): keyMap = targetObj._qteAdmin.keyMap else: keyMap = self.qteMain._qteGlobalKeyMapByReference() # See if there is a match with an entry from the key map of # the current object. If ``isPartialMatch`` is True then the # key sequence is potentially incomplete, but not invalid. # If ``macroName`` is not **None** then it is indeed complete. (macroName, isPartialMatch) = keyMap.match(self._keysequence) # Make a convenience copy of the key sequence. keyseq_copy = QtmacsKeysequence(self._keysequence) if isPartialMatch: # Reset the key combination history if a valid macro was # found so that the next key that arrives starts a new key # sequence. if macroName is None: # Report a partially completed key-sequence. msgObj = QtmacsMessage(keyseq_copy, None) msgObj.setSignalName('qtesigKeyseqPartial') self.qteMain.qtesigKeyseqPartial.emit(msgObj) else: # Execute the macro if requested. if self._qteFlagRunMacro: self.qteMain.qteRunMacro(macroName, targetObj, keyseq_copy) # Announce that the key sequence lead to a valid macro. msgObj = QtmacsMessage((macroName, keyseq_copy), None) msgObj.setSignalName('qtesigKeyseqComplete') self.qteMain.qtesigKeyseqComplete.emit(msgObj) self._keysequence.reset() else: if isRegisteredWidget: # Announce (and log) that the key sequence is invalid. However, # format the key string to Html first, eg. "<ctrl>-x i" to # "<b>&lt;Ctrl&gt;+x i</b>". tmp = keyseq_copy.toString() tmp = tmp.replace('<', '&lt;') tmp = tmp.replace('>', '&gt;') msg = 'No macro is bound to <b>{}</b>.'.format(tmp) self.qteMain.qteLogger.warning(msg) msgObj = QtmacsMessage(keyseq_copy, None) msgObj.setSignalName('qtesigKeyseqInvalid') self.qteMain.qtesigKeyseqInvalid.emit(msgObj) else: # If we are in this branch then the widet is part of the # Qtmacs widget hierachy yet was not registered with # the qteAddWidget method. In this case use the QtDelivery # macro to pass on whatever the event was (assuming # macro processing is enabled). if self._qteFlagRunMacro: self.qteMain.qteRunMacro( self.QtDelivery, targetObj, keyseq_copy) self._keysequence.reset() # Announce that Qtmacs has processed another key event. The # outcome of this processing is communicated along with the # signal. msgObj = QtmacsMessage((targetObj, keyseq_copy, macroName), None) msgObj.setSignalName('qtesigKeyparsed') self.qteMain.qtesigKeyparsed.emit(msgObj) return isPartialMatch
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:timerEvent; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:event; 6, block; 6, 7; 6, 9; 6, 20; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:killTimer; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:event; 18, identifier:timerId; 19, argument_list; 20, if_statement; 20, 21; 20, 30; 20, 31; 20, 180; 20, 195; 21, comparison_operator:==; 21, 22; 21, 27; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:event; 25, identifier:timerId; 26, argument_list; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_qteTimerRunMacro; 30, comment; 31, block; 31, 32; 31, 38; 31, 39; 31, 40; 31, 41; 31, 42; 31, 43; 31, 44; 31, 45; 31, 46; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:_qteTimerRunMacro; 37, None; 38, comment; 39, comment; 40, comment; 41, comment; 42, comment; 43, comment; 44, comment; 45, comment; 46, while_statement; 46, 47; 46, 48; 47, True; 48, block; 48, 49; 48, 174; 49, if_statement; 49, 50; 49, 58; 49, 82; 49, 162; 50, comparison_operator:>; 50, 51; 50, 57; 51, call; 51, 52; 51, 53; 52, identifier:len; 53, argument_list; 53, 54; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_qteMacroQueue; 57, integer:0; 58, block; 58, 59; 58, 73; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 65; 61, tuple_pattern; 61, 62; 61, 63; 61, 64; 62, identifier:macroName; 63, identifier:qteWidget; 64, identifier:event; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:_qteMacroQueue; 70, identifier:pop; 71, argument_list; 71, 72; 72, integer:0; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:_qteRunQueuedMacro; 78, argument_list; 78, 79; 78, 80; 78, 81; 79, identifier:macroName; 80, identifier:qteWidget; 81, identifier:event; 82, elif_clause; 82, 83; 82, 91; 82, 92; 82, 93; 82, 94; 82, 95; 82, 96; 83, comparison_operator:>; 83, 84; 83, 90; 84, call; 84, 85; 84, 86; 85, identifier:len; 86, argument_list; 86, 87; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:_qteKeyEmulationQueue; 90, integer:0; 91, comment; 92, comment; 93, comment; 94, comment; 95, comment; 96, block; 96, 97; 96, 139; 96, 140; 96, 141; 96, 152; 97, if_statement; 97, 98; 97, 103; 97, 112; 98, comparison_operator:is; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:_qteActiveApplet; 102, None; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:receiver; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:qteActiveWindow; 111, argument_list; 112, else_clause; 112, 113; 113, block; 113, 114; 114, if_statement; 114, 115; 114, 122; 114, 129; 115, comparison_operator:is; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:_qteActiveApplet; 120, identifier:_qteActiveWidget; 121, None; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:receiver; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:_qteActiveApplet; 129, else_clause; 129, 130; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:receiver; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:_qteActiveApplet; 138, identifier:_qteActiveWidget; 139, comment; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:keysequence; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:_qteKeyEmulationQueue; 149, identifier:pop; 150, argument_list; 150, 151; 151, integer:0; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:_qteEventFilter; 158, identifier:eventFilter; 159, argument_list; 159, 160; 159, 161; 160, identifier:receiver; 161, identifier:keysequence; 162, else_clause; 162, 163; 162, 164; 162, 165; 162, 166; 163, comment; 164, comment; 165, comment; 166, block; 166, 167; 166, 173; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:_qteFocusManager; 172, argument_list; 173, break_statement; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:_qteFocusManager; 179, argument_list; 180, elif_clause; 180, 181; 180, 190; 180, 191; 180, 192; 180, 193; 181, comparison_operator:==; 181, 182; 181, 187; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:event; 185, identifier:timerId; 186, argument_list; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:debugTimer; 190, comment; 191, comment; 192, comment; 193, block; 193, 194; 194, pass_statement; 195, else_clause; 195, 196; 195, 197; 196, comment; 197, block; 197, 198; 197, 203; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 201; 200, identifier:print; 201, argument_list; 201, 202; 202, string:'Unknown timer ID'; 203, pass_statement
def timerEvent(self, event): """ Trigger the focus manager and work off all queued macros. The main purpose of using this timer event is to postpone updating the visual layout of Qtmacs until all macro code has been fully executed. Furthermore, this GUI update needs to happen in between any two macros. This method will trigger itself until all macros in the queue were executed. |Args| * ``event`` (**QTimerEvent**): Qt native event description. |Returns| * **None** |Raises| * **None** """ self.killTimer(event.timerId()) if event.timerId() == self._qteTimerRunMacro: # Declare the macro execution timer event handled. self._qteTimerRunMacro = None # If we are in this branch then the focus manager was just # executed, the event loop has updated all widgets and # cleared out all signals, and there is at least one macro # in the macro queue and/or at least one key to emulate # in the key queue. Execute the macros/keys and trigger # the focus manager after each. The macro queue is cleared # out first and the keys are only emulated if no more # macros are left. while True: if len(self._qteMacroQueue) > 0: (macroName, qteWidget, event) = self._qteMacroQueue.pop(0) self._qteRunQueuedMacro(macroName, qteWidget, event) elif len(self._qteKeyEmulationQueue) > 0: # Determine the recipient of the event. This can # be, in order of preference, the active widget in # the active applet, or just the active applet (if # it has no widget inside), or the active window # (if no applets are available). if self._qteActiveApplet is None: receiver = self.qteActiveWindow() else: if self._qteActiveApplet._qteActiveWidget is None: receiver = self._qteActiveApplet else: receiver = self._qteActiveApplet._qteActiveWidget # Call the event filter directly and trigger the focus # manager again. keysequence = self._qteKeyEmulationQueue.pop(0) self._qteEventFilter.eventFilter(receiver, keysequence) else: # If we are in this branch then no more macros are left # to run. So trigger the focus manager one more time # and then leave the while-loop. self._qteFocusManager() break self._qteFocusManager() elif event.timerId() == self.debugTimer: #win = self.qteNextWindow() #self.qteMakeWindowActive(win) #self.debugTimer = self.startTimer(1000) pass else: # Should not happen. print('Unknown timer ID') pass
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 37; 2, function_name:qteNextApplet; 3, parameters; 3, 4; 3, 5; 3, 10; 3, 17; 3, 22; 3, 27; 3, 32; 4, identifier:self; 5, typed_default_parameter; 5, 6; 5, 7; 5, 9; 6, identifier:numSkip; 7, type; 7, 8; 8, identifier:int; 9, integer:1; 10, typed_default_parameter; 10, 11; 10, 12; 10, 16; 11, identifier:ofsApp; 12, type; 12, 13; 13, tuple; 13, 14; 13, 15; 14, identifier:QtmacsApplet; 15, identifier:str; 16, None; 17, typed_default_parameter; 17, 18; 17, 19; 17, 21; 18, identifier:skipInvisible; 19, type; 19, 20; 20, identifier:bool; 21, True; 22, typed_default_parameter; 22, 23; 22, 24; 22, 26; 23, identifier:skipVisible; 24, type; 24, 25; 25, identifier:bool; 26, False; 27, typed_default_parameter; 27, 28; 27, 29; 27, 31; 28, identifier:skipMiniApplet; 29, type; 29, 30; 30, identifier:bool; 31, True; 32, typed_default_parameter; 32, 33; 32, 34; 32, 36; 33, identifier:windowObj; 34, type; 34, 35; 35, identifier:QtmacsWindow; 36, None; 37, block; 37, 38; 37, 40; 37, 41; 37, 42; 37, 43; 37, 44; 37, 60; 37, 61; 37, 73; 37, 74; 37, 75; 37, 76; 37, 83; 37, 84; 37, 93; 37, 94; 37, 95; 37, 135; 37, 136; 37, 137; 37, 155; 37, 156; 37, 157; 37, 176; 37, 177; 37, 187; 37, 188; 37, 199; 37, 325; 37, 326; 37, 327; 37, 339; 37, 340; 37, 341; 38, expression_statement; 38, 39; 39, comment; 40, comment; 41, comment; 42, comment; 43, comment; 44, if_statement; 44, 45; 44, 50; 45, call; 45, 46; 45, 47; 46, identifier:isinstance; 47, argument_list; 47, 48; 47, 49; 48, identifier:ofsApp; 49, identifier:str; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:ofsApp; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:qteGetAppletHandle; 58, argument_list; 58, 59; 59, identifier:ofsApp; 60, comment; 61, if_statement; 61, 62; 61, 70; 62, comparison_operator:==; 62, 63; 62, 69; 63, call; 63, 64; 63, 65; 64, identifier:len; 65, argument_list; 65, 66; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:_qteAppletList; 69, integer:0; 70, block; 70, 71; 71, return_statement; 71, 72; 72, None; 73, comment; 74, comment; 75, comment; 76, if_statement; 76, 77; 76, 80; 77, boolean_operator:and; 77, 78; 77, 79; 78, identifier:skipVisible; 79, identifier:skipInvisible; 80, block; 80, 81; 81, return_statement; 81, 82; 82, None; 83, comment; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:appList; 87, call; 87, 88; 87, 89; 88, identifier:list; 89, argument_list; 89, 90; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:_qteAppletList; 93, comment; 94, comment; 95, if_statement; 95, 96; 95, 97; 96, identifier:skipInvisible; 97, block; 97, 98; 97, 112; 97, 113; 97, 114; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:appList; 101, list_comprehension; 101, 102; 101, 103; 101, 106; 102, identifier:app; 103, for_in_clause; 103, 104; 103, 105; 104, identifier:app; 105, identifier:appList; 106, if_clause; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:app; 110, identifier:qteIsVisible; 111, argument_list; 112, comment; 113, comment; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:windowObj; 117, None; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:appList; 122, list_comprehension; 122, 123; 122, 124; 122, 127; 123, identifier:app; 124, for_in_clause; 124, 125; 124, 126; 125, identifier:app; 126, identifier:appList; 127, if_clause; 127, 128; 128, comparison_operator:==; 128, 129; 128, 134; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:app; 132, identifier:qteParentWindow; 133, argument_list; 134, identifier:windowObj; 135, comment; 136, comment; 137, if_statement; 137, 138; 137, 139; 138, identifier:skipVisible; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:appList; 143, list_comprehension; 143, 144; 143, 145; 143, 148; 144, identifier:app; 145, for_in_clause; 145, 146; 145, 147; 146, identifier:app; 147, identifier:appList; 148, if_clause; 148, 149; 149, not_operator; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:app; 153, identifier:qteIsVisible; 154, argument_list; 155, comment; 156, comment; 157, if_statement; 157, 158; 157, 159; 158, identifier:skipMiniApplet; 159, block; 159, 160; 160, if_statement; 160, 161; 160, 166; 161, comparison_operator:in; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:_qteMiniApplet; 165, identifier:appList; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:appList; 171, identifier:remove; 172, argument_list; 172, 173; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:_qteMiniApplet; 176, comment; 177, if_statement; 177, 178; 177, 184; 178, comparison_operator:==; 178, 179; 178, 183; 179, call; 179, 180; 179, 181; 180, identifier:len; 181, argument_list; 181, 182; 182, identifier:appList; 183, integer:0; 184, block; 184, 185; 185, return_statement; 185, 186; 186, None; 187, comment; 188, if_statement; 188, 189; 188, 192; 189, comparison_operator:is; 189, 190; 189, 191; 190, identifier:ofsApp; 191, None; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:ofsApp; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:_qteActiveApplet; 199, if_statement; 199, 200; 199, 205; 199, 206; 199, 207; 199, 315; 200, comparison_operator:in; 200, 201; 200, 202; 201, identifier:ofsApp; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:_qteAppletList; 205, comment; 206, comment; 207, block; 207, 208; 208, if_statement; 208, 209; 208, 212; 208, 213; 208, 223; 209, comparison_operator:in; 209, 210; 209, 211; 210, identifier:ofsApp; 211, identifier:appList; 212, comment; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:ofsIdx; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:appList; 220, identifier:index; 221, argument_list; 221, 222; 222, identifier:ofsApp; 223, else_clause; 223, 224; 223, 225; 223, 226; 223, 227; 224, comment; 225, comment; 226, comment; 227, block; 227, 228; 227, 239; 227, 249; 227, 259; 227, 260; 227, 277; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:ofsIdx; 231, call; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:_qteAppletList; 236, identifier:index; 237, argument_list; 237, 238; 238, identifier:ofsApp; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:glob_list; 242, subscript; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:_qteAppletList; 246, slice; 246, 247; 246, 248; 247, identifier:ofsIdx; 248, colon; 249, expression_statement; 249, 250; 250, augmented_assignment:+=; 250, 251; 250, 252; 251, identifier:glob_list; 252, subscript; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:_qteAppletList; 256, slice; 256, 257; 256, 258; 257, colon; 258, identifier:ofsIdx; 259, comment; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:ofsIdx; 263, list_comprehension; 263, 264; 263, 270; 263, 273; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:appList; 267, identifier:index; 268, argument_list; 268, 269; 269, identifier:_; 270, for_in_clause; 270, 271; 270, 272; 271, identifier:_; 272, identifier:glob_list; 273, if_clause; 273, 274; 274, comparison_operator:in; 274, 275; 274, 276; 275, identifier:_; 276, identifier:appList; 277, if_statement; 277, 278; 277, 284; 277, 306; 278, comparison_operator:==; 278, 279; 278, 283; 279, call; 279, 280; 279, 281; 280, identifier:len; 281, argument_list; 281, 282; 282, identifier:ofsIdx; 283, integer:0; 284, block; 284, 285; 284, 292; 284, 304; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:msg; 288, parenthesized_expression; 288, 289; 289, concatenated_string; 289, 290; 289, 291; 290, string:'No match between global and local applet list'; 291, string:' --> Bug.'; 292, expression_statement; 292, 293; 293, call; 293, 294; 293, 299; 294, attribute; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:self; 297, identifier:qteLogger; 298, identifier:error; 299, argument_list; 299, 300; 299, 301; 300, identifier:msg; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:stack_info; 303, True; 304, return_statement; 304, 305; 305, None; 306, else_clause; 306, 307; 306, 308; 307, comment; 308, block; 308, 309; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:ofsIdx; 312, subscript; 312, 313; 312, 314; 313, identifier:ofsIdx; 314, integer:0; 315, else_clause; 315, 316; 315, 317; 315, 318; 315, 319; 315, 320; 316, comment; 317, comment; 318, comment; 319, comment; 320, block; 320, 321; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:ofsIdx; 324, integer:0; 325, comment; 326, comment; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:ofsIdx; 330, binary_operator:%; 330, 331; 330, 335; 331, parenthesized_expression; 331, 332; 332, binary_operator:+; 332, 333; 332, 334; 333, identifier:ofsIdx; 334, identifier:numSkip; 335, call; 335, 336; 335, 337; 336, identifier:len; 337, argument_list; 337, 338; 338, identifier:appList; 339, comment; 340, comment; 341, return_statement; 341, 342; 342, subscript; 342, 343; 342, 344; 343, identifier:appList; 344, identifier:ofsIdx
def qteNextApplet(self, numSkip: int=1, ofsApp: (QtmacsApplet, str)=None, skipInvisible: bool=True, skipVisible: bool=False, skipMiniApplet: bool=True, windowObj: QtmacsWindow=None): """ Return the next applet in cyclic order. If ``ofsApp=None`` then start cycling at the currently active applet. If ``ofsApp`` does not fit the selection criteria, then the cycling starts at the next applet in cyclic order that does. The returned applet is ``numSkip`` items in cyclic order away from the offset applet. If ``numSkip`` is positive traverse the applet list forwards, otherwise backwards. The method supports the following Boolean selection criteria: * ``skipInvisible``: ignore all invisible applets. * ``skipVisible``: ignore all visible applets. * ``skipMiniApplet``: ignore the mini applet applet. The ``ofsApp`` parameter can either be an instance of ``QtmacsApplet`` or a string denoting an applet ID. In the latter case the ``qteGetAppletHandle`` method is used to fetch the respective applet instance. |Args| * ``numSkip`` (**int**): number of applets to skip. * ``ofsApp`` (**QtmacsApplet**, **str**): applet from where to start counting. * ``skipInvisible`` (**bool**): whether or not to skip currently not shown applets. * ``skipVisible`` (**bool**): whether or not to skip currently shown applets. * ``skipMiniApplet`` (**bool**): whether or not to skip the mini applet. * ``windowObj`` (**QtmacsWindow**): the window to use when looking for applets. If **None**, then search in all windows. |Returns| * **QtmacsApplet**: either the next applet that fits the criteria, or **None** if no such applet exists. |Raises| * **QtmacsArgumentError** if at least one argument has an invalid type. """ # If ``applet`` was specified by its ID (ie. a string) then # fetch the associated ``QtmacsApplet`` instance. If # ``applet`` is already an instance of ``QtmacsApplet`` then # use it directly. if isinstance(ofsApp, str): ofsApp = self.qteGetAppletHandle(ofsApp) # Return immediately if the applet list is empty. if len(self._qteAppletList) == 0: return None # Sanity check: if the user requests applets that are neither # visible nor invisible then return immediately because no # such applet can possibly exist. if skipVisible and skipInvisible: return None # Make a copy of the applet list. appList = list(self._qteAppletList) # Remove all invisible applets from the list if the # skipInvisible flag is set. if skipInvisible: appList = [app for app in appList if app.qteIsVisible()] # From the list of (now guaranteed visible) applets remove # all those that are not in the specified window. if windowObj is not None: appList = [app for app in appList if app.qteParentWindow() == windowObj] # Remove all visible applets from the list if the # skipInvisible flag is set. if skipVisible: appList = [app for app in appList if not app.qteIsVisible()] # If the mini-buffer is to be skipped remove it (if a custom # mini applet even exists). if skipMiniApplet: if self._qteMiniApplet in appList: appList.remove(self._qteMiniApplet) # Return immediately if no applet satisfied all criteria. if len(appList) == 0: return None # If no offset applet was given use the currently active one. if ofsApp is None: ofsApp = self._qteActiveApplet if ofsApp in self._qteAppletList: # Determine if the offset applet is part of the pruned # list. if ofsApp in appList: # Yes: determine its index in the list. ofsIdx = appList.index(ofsApp) else: # No: traverse all applets until one is found that is # also part of the pruned list (start at ofsIdx). Then # determine its index in the list. ofsIdx = self._qteAppletList.index(ofsApp) glob_list = self._qteAppletList[ofsIdx:] glob_list += self._qteAppletList[:ofsIdx] # Compile the intersection between the global and pruned list. ofsIdx = [appList.index(_) for _ in glob_list if _ in appList] if len(ofsIdx) == 0: msg = ('No match between global and local applet list' ' --> Bug.') self.qteLogger.error(msg, stack_info=True) return None else: # Pick the first match. ofsIdx = ofsIdx[0] else: # The offset applet does not exist, eg. because the user # supplied a handle that does not point to an applet or # we are called from qteKillApplet to replace the just # removed (and active) applet. ofsIdx = 0 # Compute the index of the next applet and wrap around the # list if necessary. ofsIdx = (ofsIdx + numSkip) % len(appList) # Return a handle to the applet that meets the specified # criteria. return appList[ofsIdx]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:_qteRunQueuedMacro; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 16; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:macroName; 7, type; 7, 8; 8, identifier:str; 9, typed_default_parameter; 9, 10; 9, 11; 9, 15; 10, identifier:widgetObj; 11, type; 11, 12; 12, attribute; 12, 13; 12, 14; 13, identifier:QtGui; 14, identifier:QWidget; 15, None; 16, typed_default_parameter; 16, 17; 16, 18; 16, 20; 17, identifier:keysequence; 18, type; 18, 19; 19, identifier:QtmacsKeysequence; 20, None; 21, block; 21, 22; 21, 24; 21, 25; 21, 32; 21, 33; 21, 34; 21, 70; 21, 71; 21, 81; 21, 82; 21, 120; 21, 121; 21, 122; 21, 133; 21, 134; 21, 135; 21, 164; 21, 165; 22, expression_statement; 22, 23; 23, comment; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:app; 28, call; 28, 29; 28, 30; 29, identifier:qteGetAppletFromWidget; 30, argument_list; 30, 31; 31, identifier:widgetObj; 32, comment; 33, comment; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:app; 37, None; 38, block; 38, 39; 39, if_statement; 39, 40; 39, 46; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:sip; 43, identifier:isdeleted; 44, argument_list; 44, 45; 45, identifier:app; 46, block; 46, 47; 46, 51; 46, 60; 46, 69; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:msg; 50, string:'Ignored macro <b>{}</b> because it targeted a'; 51, expression_statement; 51, 52; 52, augmented_assignment:+=; 52, 53; 52, 54; 53, identifier:msg; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, string:' nonexistent applet.'; 57, identifier:format; 58, argument_list; 58, 59; 59, identifier:macroName; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:qteLogger; 66, identifier:warning; 67, argument_list; 67, 68; 68, identifier:msg; 69, return_statement; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:macroObj; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:qteGetMacroObject; 78, argument_list; 78, 79; 78, 80; 79, identifier:macroName; 80, identifier:widgetObj; 81, comment; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:macroObj; 85, None; 86, block; 86, 87; 86, 91; 86, 110; 86, 119; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:msg; 90, string:'No <b>{}</b>-macro compatible with {}:{}-type applet'; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:msg; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:msg; 97, identifier:format; 98, argument_list; 98, 99; 98, 100; 98, 105; 99, identifier:macroName; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:app; 103, identifier:qteAppletSignature; 104, argument_list; 105, attribute; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:widgetObj; 108, identifier:_qteAdmin; 109, identifier:widgetSignature; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:qteLogger; 116, identifier:warning; 117, argument_list; 117, 118; 118, identifier:msg; 119, return_statement; 120, comment; 121, comment; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:qteDefVar; 127, argument_list; 127, 128; 127, 129; 127, 130; 128, string:'last_key_sequence'; 129, identifier:keysequence; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:doc; 132, string:"Last valid key sequence that triggered a macro."; 133, comment; 134, comment; 135, if_statement; 135, 136; 135, 139; 135, 150; 136, comparison_operator:is; 136, 137; 136, 138; 137, identifier:app; 138, None; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:macroObj; 144, identifier:qteApplet; 145, assignment; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:macroObj; 148, identifier:qteWidget; 149, None; 150, else_clause; 150, 151; 151, block; 151, 152; 151, 158; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:macroObj; 156, identifier:qteApplet; 157, identifier:app; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:macroObj; 162, identifier:qteWidget; 163, identifier:widgetObj; 164, comment; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:macroObj; 169, identifier:qtePrepareToRun; 170, argument_list
def _qteRunQueuedMacro(self, macroName: str, widgetObj: QtGui.QWidget=None, keysequence: QtmacsKeysequence=None): """ Execute the next macro in the macro queue. This method is triggered by the ``timerEvent`` in conjunction with the focus manager to ensure the event loop updates the GUI in between any two macros. .. warning:: Never call this method directly. |Args| * ``macroName`` (**str**): name of macro * ``widgetObj`` (**QWidget**): widget (if any) for which the macro applies * ``keysequence* (**QtmacsKeysequence**): key sequence that triggered the macro. |Returns| * **None** |Raises| * **QtmacsArgumentError** if at least one argument has an invalid type. """ # Fetch the applet holding the widget (this may be None). app = qteGetAppletFromWidget(widgetObj) # Double check that the applet still exists, unless there is # no applet (can happen when the windows are empty). if app is not None: if sip.isdeleted(app): msg = 'Ignored macro <b>{}</b> because it targeted a' msg += ' nonexistent applet.'.format(macroName) self.qteLogger.warning(msg) return # Fetch a signature compatible macro object. macroObj = self.qteGetMacroObject(macroName, widgetObj) # Log an error if no compatible macro was found. if macroObj is None: msg = 'No <b>{}</b>-macro compatible with {}:{}-type applet' msg = msg.format(macroName, app.qteAppletSignature(), widgetObj._qteAdmin.widgetSignature) self.qteLogger.warning(msg) return # Update the 'last_key_sequence' variable in case the macros, # or slots triggered by that macro, have access to it. self.qteDefVar('last_key_sequence', keysequence, doc="Last valid key sequence that triggered a macro.") # Set some variables in the macro object for convenient access # from inside the macro. if app is None: macroObj.qteApplet = macroObj.qteWidget = None else: macroObj.qteApplet = app macroObj.qteWidget = widgetObj # Run the macro and trigger the focus manager. macroObj.qtePrepareToRun()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:qteNewApplet; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 14; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:appletName; 7, type; 7, 8; 8, identifier:str; 9, typed_default_parameter; 9, 10; 9, 11; 9, 13; 10, identifier:appletID; 11, type; 11, 12; 12, identifier:str; 13, None; 14, typed_default_parameter; 14, 15; 14, 16; 14, 18; 15, identifier:windowObj; 16, type; 16, 17; 17, identifier:QtmacsWindow; 18, None; 19, block; 19, 20; 19, 22; 19, 23; 19, 58; 19, 59; 19, 99; 19, 100; 19, 101; 19, 134; 19, 135; 19, 136; 19, 176; 19, 177; 19, 215; 19, 216; 19, 264; 19, 265; 19, 275; 19, 276; 19, 277; 19, 278; 19, 279; 19, 318; 19, 319; 19, 320; 19, 321; 19, 328; 19, 329; 19, 341; 19, 342; 20, expression_statement; 20, 21; 21, comment; 22, comment; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:windowObj; 26, None; 27, block; 27, 28; 27, 36; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:windowObj; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:qteActiveWindow; 35, argument_list; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:windowObj; 39, None; 40, block; 40, 41; 40, 45; 40, 57; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:msg; 44, string:'Cannot determine the currently active window.'; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:qteLogger; 51, identifier:error; 52, argument_list; 52, 53; 52, 54; 53, identifier:msg; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:stack_info; 56, True; 57, return_statement; 58, comment; 59, if_statement; 59, 60; 59, 63; 60, comparison_operator:is; 60, 61; 60, 62; 61, identifier:appletID; 62, None; 63, block; 63, 64; 63, 68; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:cnt; 67, integer:0; 68, while_statement; 68, 69; 68, 70; 69, True; 70, block; 70, 71; 70, 82; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:appletID; 74, binary_operator:+; 74, 75; 74, 78; 75, binary_operator:+; 75, 76; 75, 77; 76, identifier:appletName; 77, string:'_'; 78, call; 78, 79; 78, 80; 79, identifier:str; 80, argument_list; 80, 81; 81, identifier:cnt; 82, if_statement; 82, 83; 82, 91; 82, 93; 83, comparison_operator:is; 83, 84; 83, 90; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:qteGetAppletHandle; 88, argument_list; 88, 89; 89, identifier:appletID; 90, None; 91, block; 91, 92; 92, break_statement; 93, else_clause; 93, 94; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, augmented_assignment:+=; 96, 97; 96, 98; 97, identifier:cnt; 98, integer:1; 99, comment; 100, comment; 101, if_statement; 101, 102; 101, 110; 102, comparison_operator:is; 102, 103; 102, 109; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:qteGetAppletHandle; 107, argument_list; 107, 108; 108, identifier:appletID; 109, None; 110, block; 110, 111; 110, 120; 110, 132; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:msg; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, string:'Applet with ID <b>{}</b> already exists'; 117, identifier:format; 118, argument_list; 118, 119; 119, identifier:appletID; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:qteLogger; 126, identifier:error; 127, argument_list; 127, 128; 127, 129; 128, identifier:msg; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:stack_info; 131, True; 132, return_statement; 132, 133; 133, None; 134, comment; 135, comment; 136, if_statement; 136, 137; 136, 142; 136, 166; 137, comparison_operator:not; 137, 138; 137, 139; 138, identifier:appletName; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:_qteRegistryApplets; 142, block; 142, 143; 142, 152; 142, 164; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:msg; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, string:'Unknown applet <b>{}</b>'; 149, identifier:format; 150, argument_list; 150, 151; 151, identifier:appletName; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:qteLogger; 158, identifier:error; 159, argument_list; 159, 160; 159, 161; 160, identifier:msg; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:stack_info; 163, True; 164, return_statement; 164, 165; 165, None; 166, else_clause; 166, 167; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:cls; 171, subscript; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:_qteRegistryApplets; 175, identifier:appletName; 176, comment; 177, try_statement; 177, 178; 177, 186; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:app; 182, call; 182, 183; 182, 184; 183, identifier:cls; 184, argument_list; 184, 185; 185, identifier:appletID; 186, except_clause; 186, 187; 186, 188; 187, identifier:Exception; 188, block; 188, 189; 188, 198; 188, 213; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:msg; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, string:'Applet <b>{}</b> has a faulty constructor.'; 195, identifier:format; 196, argument_list; 196, 197; 197, identifier:appletID; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:qteLogger; 204, identifier:exception; 205, argument_list; 205, 206; 205, 207; 205, 210; 206, identifier:msg; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:exc_info; 209, True; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:stack_info; 212, True; 213, return_statement; 213, 214; 214, None; 215, comment; 216, if_statement; 216, 217; 216, 224; 217, comparison_operator:is; 217, 218; 217, 223; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:app; 221, identifier:qteAppletSignature; 222, argument_list; 223, None; 224, block; 224, 225; 224, 238; 224, 242; 224, 246; 224, 250; 224, 262; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:msg; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, string:'Cannot add applet <b>{}</b> '; 231, identifier:format; 232, argument_list; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:app; 236, identifier:qteAppletID; 237, argument_list; 238, expression_statement; 238, 239; 239, augmented_assignment:+=; 239, 240; 239, 241; 240, identifier:msg; 241, string:'because it has not applet signature.'; 242, expression_statement; 242, 243; 243, augmented_assignment:+=; 243, 244; 243, 245; 244, identifier:msg; 245, string:' Use self.qteSetAppletSignature in the constructor'; 246, expression_statement; 246, 247; 247, augmented_assignment:+=; 247, 248; 247, 249; 248, identifier:msg; 249, string:' of the class to fix this.'; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 257; 252, attribute; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:qteLogger; 256, identifier:error; 257, argument_list; 257, 258; 257, 259; 258, identifier:msg; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:stack_info; 261, True; 262, return_statement; 262, 263; 263, None; 264, comment; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 272; 267, attribute; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:_qteAppletList; 271, identifier:insert; 272, argument_list; 272, 273; 272, 274; 273, integer:0; 274, identifier:app; 275, comment; 276, comment; 277, comment; 278, comment; 279, if_statement; 279, 280; 279, 287; 280, comparison_operator:is; 280, 281; 280, 286; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:app; 284, identifier:layout; 285, argument_list; 286, None; 287, block; 287, 288; 287, 296; 287, 311; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:appLayout; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:QtGui; 294, identifier:QHBoxLayout; 295, argument_list; 296, for_statement; 296, 297; 296, 298; 296, 303; 297, identifier:handle; 298, attribute; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:app; 301, identifier:_qteAdmin; 302, identifier:widgetList; 303, block; 303, 304; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:appLayout; 308, identifier:addWidget; 309, argument_list; 309, 310; 310, identifier:handle; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:app; 315, identifier:setLayout; 316, argument_list; 316, 317; 317, identifier:appLayout; 318, comment; 319, comment; 320, comment; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:app; 325, identifier:qteReparent; 326, argument_list; 326, 327; 327, None; 328, comment; 329, expression_statement; 329, 330; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:self; 333, identifier:qteRunHook; 334, argument_list; 334, 335; 334, 336; 335, string:'init'; 336, call; 336, 337; 336, 338; 337, identifier:QtmacsMessage; 338, argument_list; 338, 339; 338, 340; 339, None; 340, identifier:app; 341, comment; 342, return_statement; 342, 343; 343, identifier:app
def qteNewApplet(self, appletName: str, appletID: str=None, windowObj: QtmacsWindow=None): """ Create a new instance of ``appletName`` and assign it the ``appletID``. This method creates a new instance of ``appletName``, as registered by the ``qteRegisterApplet`` method. If an applet with ``appletID`` already exists then the method does nothing and returns **None**, otherwise the newly created instance. If ``appletID`` is **None** then the method will create an applet with the next unique ID that fits the format ``appletName_0``, eg. 'RichEditor_0', 'RichEditor_1', etc. .. note:: The applet is not automatically made visible. |Args| * ``appletName`` (**str**): name of applet to create (eg. 'LogViewer') * ``appletID`` (**str**): unique applet identifier. * ``windowObj`` (**QtmacsWindow**): the window in which the applet should be created. |Returns| * **QtmacsApplet**: applet handle or **None** if no applet was created. |Raises| * **QtmacsArgumentError** if at least one argument has an invalid type. """ # Use the currently active window if none was specified. if windowObj is None: windowObj = self.qteActiveWindow() if windowObj is None: msg = 'Cannot determine the currently active window.' self.qteLogger.error(msg, stack_info=True) return # Determine an automatic applet ID if none was provided. if appletID is None: cnt = 0 while True: appletID = appletName + '_' + str(cnt) if self.qteGetAppletHandle(appletID) is None: break else: cnt += 1 # Return immediately if an applet with the same ID already # exists. if self.qteGetAppletHandle(appletID) is not None: msg = 'Applet with ID <b>{}</b> already exists'.format(appletID) self.qteLogger.error(msg, stack_info=True) return None # Verify that the requested applet class was registered # beforehand and fetch it. if appletName not in self._qteRegistryApplets: msg = 'Unknown applet <b>{}</b>'.format(appletName) self.qteLogger.error(msg, stack_info=True) return None else: cls = self._qteRegistryApplets[appletName] # Try to instantiate the class. try: app = cls(appletID) except Exception: msg = 'Applet <b>{}</b> has a faulty constructor.'.format(appletID) self.qteLogger.exception(msg, exc_info=True, stack_info=True) return None # Ensure the applet class has an applet signature. if app.qteAppletSignature() is None: msg = 'Cannot add applet <b>{}</b> '.format(app.qteAppletID()) msg += 'because it has not applet signature.' msg += ' Use self.qteSetAppletSignature in the constructor' msg += ' of the class to fix this.' self.qteLogger.error(msg, stack_info=True) return None # Add the applet to the list of instantiated Qtmacs applets. self._qteAppletList.insert(0, app) # If the new applet does not yet have an internal layout then # arrange all its children automatically. The layout used for # this is horizontal and the widgets are added in the order in # which they were registered with Qtmacs. if app.layout() is None: appLayout = QtGui.QHBoxLayout() for handle in app._qteAdmin.widgetList: appLayout.addWidget(handle) app.setLayout(appLayout) # Initially, the window does not have a parent. A parent will # be assigned automatically once the applet is made visible, # in which case it is re-parented into a QtmacsSplitter. app.qteReparent(None) # Emit the init hook for this applet. self.qteRunHook('init', QtmacsMessage(None, app)) # Return applet handle. return app
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:qteAddMiniApplet; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:appletObj; 7, type; 7, 8; 8, identifier:QtmacsApplet; 9, block; 9, 10; 9, 12; 9, 13; 9, 14; 9, 36; 9, 37; 9, 38; 9, 39; 9, 78; 9, 79; 9, 80; 9, 81; 9, 82; 9, 90; 9, 96; 9, 97; 9, 103; 9, 111; 9, 112; 9, 120; 9, 128; 9, 130; 9, 131; 9, 132; 9, 144; 9, 145; 9, 146; 9, 157; 9, 166; 9, 167; 9, 168; 9, 181; 9, 190; 9, 199; 9, 200; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, comment; 14, if_statement; 14, 15; 14, 20; 15, comparison_operator:is; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_qteMiniApplet; 19, None; 20, block; 20, 21; 20, 25; 20, 34; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:msg; 24, string:'Cannot replace mini applet more than once.'; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:qteLogger; 31, identifier:warning; 32, argument_list; 32, 33; 33, identifier:msg; 34, return_statement; 34, 35; 35, False; 36, comment; 37, comment; 38, comment; 39, if_statement; 39, 40; 39, 47; 40, comparison_operator:is; 40, 41; 40, 46; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:appletObj; 44, identifier:layout; 45, argument_list; 46, None; 47, block; 47, 48; 47, 56; 47, 71; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:appLayout; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:QtGui; 54, identifier:QHBoxLayout; 55, argument_list; 56, for_statement; 56, 57; 56, 58; 56, 63; 57, identifier:handle; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:appletObj; 61, identifier:_qteAdmin; 62, identifier:widgetList; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:appLayout; 68, identifier:addWidget; 69, argument_list; 69, 70; 70, identifier:handle; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:appletObj; 75, identifier:setLayout; 76, argument_list; 76, 77; 77, identifier:appLayout; 78, comment; 79, comment; 80, comment; 81, comment; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 89; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:appletObj; 87, identifier:_qteAdmin; 88, identifier:isMiniApplet; 89, True; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:_qteMiniApplet; 95, identifier:appletObj; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:app; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:_qteActiveApplet; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:appWin; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:qteActiveWindow; 110, argument_list; 111, comment; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_qteMiniApplet; 118, identifier:_qteCallingApplet; 119, identifier:app; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:_qteMiniApplet; 126, identifier:_qteCallingWindow; 127, identifier:appWin; 128, delete_statement; 128, 129; 129, identifier:app; 130, comment; 131, comment; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:_qteAppletList; 138, identifier:insert; 139, argument_list; 139, 140; 139, 141; 140, integer:0; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:_qteMiniApplet; 144, comment; 145, comment; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 153; 148, attribute; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:appWin; 151, identifier:qteLayoutSplitter; 152, identifier:addWidget; 153, argument_list; 153, 154; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:_qteMiniApplet; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 164; 159, attribute; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:_qteMiniApplet; 163, identifier:show; 164, argument_list; 164, 165; 165, True; 166, comment; 167, comment; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:wid; 171, call; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:_qteMiniApplet; 176, identifier:qteNextWidget; 177, argument_list; 177, 178; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:numSkip; 180, integer:0; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:_qteMiniApplet; 187, identifier:qteMakeWidgetActive; 188, argument_list; 188, 189; 189, identifier:wid; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:qteMakeAppletActive; 195, argument_list; 195, 196; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:_qteMiniApplet; 199, comment; 200, return_statement; 200, 201; 201, True
def qteAddMiniApplet(self, appletObj: QtmacsApplet): """ Install ``appletObj`` as the mini applet in the window layout. At any given point there can ever only be one mini applet in the entire Qtmacs application, irrespective of how many windows are open. Note that this method does nothing if a custom mini applet is already active. Use ``qteKillMiniApplet`` to remove that one first before installing a new one. |Args| * ``appletObj`` (**QtmacsApplet**): the new mini applet. |Returns| * **bool**: if **True** the mini applet was installed successfully. |Raises| * **QtmacsArgumentError** if at least one argument has an invalid type. """ # Do nothing if a custom mini applet has already been # installed. if self._qteMiniApplet is not None: msg = 'Cannot replace mini applet more than once.' self.qteLogger.warning(msg) return False # Arrange all registered widgets inside this applet # automatically if the mini applet object did not install its # own layout. if appletObj.layout() is None: appLayout = QtGui.QHBoxLayout() for handle in appletObj._qteAdmin.widgetList: appLayout.addWidget(handle) appletObj.setLayout(appLayout) # Now that we have decided to install this mini applet, keep a # reference to it and set the mini applet flag in the # applet. This flag is necessary for some methods to separate # conventional applets from mini applets. appletObj._qteAdmin.isMiniApplet = True self._qteMiniApplet = appletObj # Shorthands. app = self._qteActiveApplet appWin = self.qteActiveWindow() # Remember which window and applet spawned this mini applet. self._qteMiniApplet._qteCallingApplet = app self._qteMiniApplet._qteCallingWindow = appWin del app # Add the mini applet to the applet registry, ie. for most # purposes the mini applet is treated like any other applet. self._qteAppletList.insert(0, self._qteMiniApplet) # Add the mini applet to the respective splitter in the window # layout and show it. appWin.qteLayoutSplitter.addWidget(self._qteMiniApplet) self._qteMiniApplet.show(True) # Give focus to first focusable widget in the mini applet # applet (if one exists) wid = self._qteMiniApplet.qteNextWidget(numSkip=0) self._qteMiniApplet.qteMakeWidgetActive(wid) self.qteMakeAppletActive(self._qteMiniApplet) # Mini applet was successfully installed. return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:qteKillMiniApplet; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 17; 5, 18; 5, 45; 5, 196; 5, 197; 5, 205; 5, 213; 5, 214; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 15; 10, comparison_operator:is; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_qteMiniApplet; 14, None; 15, block; 15, 16; 16, return_statement; 17, comment; 18, if_statement; 18, 19; 18, 28; 19, not_operator; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:qteIsMiniApplet; 24, argument_list; 24, 25; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:_qteMiniApplet; 28, block; 28, 29; 28, 36; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:msg; 32, parenthesized_expression; 32, 33; 33, concatenated_string; 33, 34; 33, 35; 34, string:'Mini applet does not have its mini applet flag set.'; 35, string:' Ignored.'; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:qteLogger; 42, identifier:warning; 43, argument_list; 43, 44; 44, identifier:msg; 45, if_statement; 45, 46; 45, 53; 45, 54; 45, 55; 45, 69; 46, comparison_operator:not; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:_qteMiniApplet; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_qteAppletList; 53, comment; 54, comment; 55, block; 55, 56; 55, 60; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:msg; 59, string:'Custom mini applet not in applet list --> Bug.'; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:qteLogger; 66, identifier:warning; 67, argument_list; 67, 68; 68, identifier:msg; 69, else_clause; 69, 70; 69, 71; 70, comment; 71, block; 71, 72; 71, 104; 71, 105; 71, 113; 71, 114; 71, 115; 71, 116; 71, 117; 71, 128; 71, 185; 72, try_statement; 72, 73; 72, 82; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:_qteMiniApplet; 80, identifier:qteToBeKilled; 81, argument_list; 82, except_clause; 82, 83; 82, 84; 83, identifier:Exception; 84, block; 84, 85; 84, 89; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:msg; 88, string:'qteToBeKilledRoutine is faulty'; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:qteLogger; 95, identifier:exception; 96, argument_list; 96, 97; 96, 98; 96, 101; 97, identifier:msg; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:exc_info; 100, True; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:stack_info; 103, True; 104, comment; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:win; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_qteMiniApplet; 112, identifier:_qteCallingWindow; 113, comment; 114, comment; 115, comment; 116, comment; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:app; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:qteNextApplet; 124, argument_list; 124, 125; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:windowObj; 127, identifier:win; 128, if_statement; 128, 129; 128, 132; 128, 133; 128, 134; 128, 142; 129, comparison_operator:is; 129, 130; 129, 131; 130, identifier:app; 131, None; 132, comment; 133, comment; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:qteMakeAppletActive; 140, argument_list; 140, 141; 141, identifier:app; 142, else_clause; 142, 143; 142, 144; 142, 145; 143, comment; 144, comment; 145, block; 145, 146; 145, 160; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:app; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:qteNextApplet; 153, argument_list; 153, 154; 153, 157; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:skipInvisible; 156, False; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:skipVisible; 159, True; 160, if_statement; 160, 161; 160, 164; 160, 165; 160, 166; 160, 174; 161, comparison_operator:is; 161, 162; 161, 163; 162, identifier:app; 163, None; 164, comment; 165, comment; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:qteMakeAppletActive; 172, argument_list; 172, 173; 173, identifier:app; 174, else_clause; 174, 175; 174, 176; 174, 177; 174, 178; 175, comment; 176, comment; 177, comment; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:_qteActiveApplet; 184, None; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 192; 187, attribute; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:self; 190, identifier:_qteAppletList; 191, identifier:remove; 192, argument_list; 192, 193; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:_qteMiniApplet; 196, comment; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 204; 199, attribute; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:_qteMiniApplet; 203, identifier:close; 204, argument_list; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 212; 207, attribute; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:_qteMiniApplet; 211, identifier:deleteLater; 212, argument_list; 213, comment; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:_qteMiniApplet; 219, None
def qteKillMiniApplet(self): """ Remove the mini applet. If a different applet is to be restored/focused then call ``qteMakeAppletActive`` for that applet *after* calling this method. |Args| * **None** |Returns| * **None** |Raises| * **None** """ # Sanity check: is the handle valid? if self._qteMiniApplet is None: return # Sanity check: is it really a mini applet? if not self.qteIsMiniApplet(self._qteMiniApplet): msg = ('Mini applet does not have its mini applet flag set.' ' Ignored.') self.qteLogger.warning(msg) if self._qteMiniApplet not in self._qteAppletList: # Something is wrong because the mini applet is not part # of the applet list. msg = 'Custom mini applet not in applet list --> Bug.' self.qteLogger.warning(msg) else: # Inform the mini applet that it is about to be killed. try: self._qteMiniApplet.qteToBeKilled() except Exception: msg = 'qteToBeKilledRoutine is faulty' self.qteLogger.exception(msg, exc_info=True, stack_info=True) # Shorthands to calling window. win = self._qteMiniApplet._qteCallingWindow # We need to move the focus from the mini applet back to a # regular applet. Therefore, first look for the next # visible applet in the current window (ie. the last one # that was made active). app = self.qteNextApplet(windowObj=win) if app is not None: # Found another (visible or invisible) applet --> make # it active/visible. self.qteMakeAppletActive(app) else: # No visible applet available in this window --> look # for an invisible one. app = self.qteNextApplet(skipInvisible=False, skipVisible=True) if app is not None: # Found an invisible applet --> make it # active/visible. self.qteMakeAppletActive(app) else: # There is no other visible applet in this window. # The focus manager will therefore make a new applet # active. self._qteActiveApplet = None self._qteAppletList.remove(self._qteMiniApplet) # Close the mini applet applet and schedule it for deletion. self._qteMiniApplet.close() self._qteMiniApplet.deleteLater() # Clear the handle to the mini applet. self._qteMiniApplet = None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:qteSplitApplet; 3, parameters; 3, 4; 3, 5; 3, 12; 3, 17; 4, identifier:self; 5, typed_default_parameter; 5, 6; 5, 7; 5, 11; 6, identifier:applet; 7, type; 7, 8; 8, tuple; 8, 9; 8, 10; 9, identifier:QtmacsApplet; 10, identifier:str; 11, None; 12, typed_default_parameter; 12, 13; 12, 14; 12, 16; 13, identifier:splitHoriz; 14, type; 14, 15; 15, identifier:bool; 16, True; 17, typed_default_parameter; 17, 18; 17, 19; 17, 21; 18, identifier:windowObj; 19, type; 19, 20; 20, identifier:QtmacsWindow; 21, None; 22, block; 22, 23; 22, 25; 22, 26; 22, 27; 22, 28; 22, 29; 22, 51; 22, 52; 22, 87; 22, 88; 22, 109; 22, 142; 22, 143; 22, 144; 22, 145; 22, 161; 22, 162; 22, 163; 22, 194; 22, 195; 22, 196; 22, 197; 22, 198; 22, 199; 22, 200; 22, 201; 22, 202; 22, 216; 22, 217; 22, 218; 22, 219; 22, 231; 22, 267; 22, 268; 22, 269; 22, 270; 22, 302; 22, 303; 22, 304; 22, 305; 22, 306; 22, 307; 22, 316; 22, 317; 22, 318; 22, 319; 22, 320; 22, 321; 22, 322; 22, 323; 22, 324; 22, 332; 22, 339; 22, 346; 22, 353; 22, 361; 22, 362; 22, 363; 22, 364; 22, 365; 22, 371; 23, expression_statement; 23, 24; 24, comment; 25, comment; 26, comment; 27, comment; 28, comment; 29, if_statement; 29, 30; 29, 35; 29, 45; 30, call; 30, 31; 30, 32; 31, identifier:isinstance; 32, argument_list; 32, 33; 32, 34; 33, identifier:applet; 34, identifier:str; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:newAppObj; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:qteGetAppletHandle; 43, argument_list; 43, 44; 44, identifier:applet; 45, else_clause; 45, 46; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:newAppObj; 50, identifier:applet; 51, comment; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:windowObj; 55, None; 56, block; 56, 57; 56, 65; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:windowObj; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:qteActiveWindow; 64, argument_list; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:windowObj; 68, None; 69, block; 69, 70; 69, 74; 69, 86; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:msg; 73, string:'Cannot determine the currently active window.'; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:qteLogger; 80, identifier:error; 81, argument_list; 81, 82; 81, 83; 82, identifier:msg; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:stack_info; 85, True; 86, return_statement; 87, comment; 88, if_statement; 88, 89; 88, 90; 88, 99; 89, identifier:splitHoriz; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:splitOrientation; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:QtCore; 97, identifier:Qt; 98, identifier:Horizontal; 99, else_clause; 99, 100; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:splitOrientation; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:QtCore; 107, identifier:Qt; 108, identifier:Vertical; 109, if_statement; 109, 110; 109, 113; 109, 114; 109, 115; 109, 130; 110, comparison_operator:is; 110, 111; 110, 112; 111, identifier:newAppObj; 112, None; 113, comment; 114, comment; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:newAppObj; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:qteNextApplet; 123, argument_list; 123, 124; 123, 127; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:skipVisible; 126, True; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:skipInvisible; 129, False; 130, else_clause; 130, 131; 130, 132; 131, comment; 132, block; 132, 133; 133, if_statement; 133, 134; 133, 139; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:newAppObj; 137, identifier:qteIsVisible; 138, argument_list; 139, block; 139, 140; 140, return_statement; 140, 141; 141, False; 142, comment; 143, comment; 144, comment; 145, if_statement; 145, 146; 145, 149; 146, comparison_operator:is; 146, 147; 146, 148; 147, identifier:newAppObj; 148, None; 149, block; 149, 150; 149, 159; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:qteLogger; 156, identifier:warning; 157, argument_list; 157, 158; 158, string:'All applets are already visible.'; 159, return_statement; 159, 160; 160, False; 161, comment; 162, comment; 163, if_statement; 163, 164; 163, 173; 164, comparison_operator:==; 164, 165; 164, 172; 165, call; 165, 166; 165, 171; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:windowObj; 169, identifier:qteAppletSplitter; 170, identifier:count; 171, argument_list; 172, integer:0; 173, block; 173, 174; 173, 183; 173, 192; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 181; 176, attribute; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:windowObj; 179, identifier:qteAppletSplitter; 180, identifier:qteAddWidget; 181, argument_list; 181, 182; 182, identifier:newAppObj; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 190; 185, attribute; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:windowObj; 188, identifier:qteAppletSplitter; 189, identifier:setOrientation; 190, argument_list; 190, 191; 191, identifier:splitOrientation; 192, return_statement; 192, 193; 193, True; 194, comment; 195, comment; 196, comment; 197, comment; 198, comment; 199, comment; 200, comment; 201, comment; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:curApp; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:qteNextApplet; 209, argument_list; 209, 210; 209, 213; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:numSkip; 212, integer:0; 213, keyword_argument; 213, 214; 213, 215; 214, identifier:windowObj; 215, identifier:windowObj; 216, comment; 217, comment; 218, comment; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:split; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:self; 225, identifier:_qteFindAppletInSplitter; 226, argument_list; 226, 227; 226, 228; 227, identifier:curApp; 228, attribute; 228, 229; 228, 230; 229, identifier:windowObj; 230, identifier:qteAppletSplitter; 231, if_statement; 231, 232; 231, 235; 232, comparison_operator:is; 232, 233; 232, 234; 233, identifier:split; 234, None; 235, block; 235, 236; 235, 240; 235, 253; 235, 265; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:msg; 239, string:'Active applet <b>{}</b> not in the layout.'; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:msg; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:msg; 246, identifier:format; 247, argument_list; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:curApp; 251, identifier:qteAppletID; 252, argument_list; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 260; 255, attribute; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:qteLogger; 259, identifier:error; 260, argument_list; 260, 261; 260, 262; 261, identifier:msg; 262, keyword_argument; 262, 263; 262, 264; 263, identifier:stack_info; 264, True; 265, return_statement; 265, 266; 266, False; 267, comment; 268, comment; 269, comment; 270, if_statement; 270, 271; 270, 276; 271, comparison_operator:is; 271, 272; 271, 273; 272, identifier:split; 273, attribute; 273, 274; 273, 275; 274, identifier:windowObj; 275, identifier:qteAppletSplitter; 276, block; 276, 277; 277, if_statement; 277, 278; 277, 285; 278, comparison_operator:==; 278, 279; 278, 284; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:split; 282, identifier:count; 283, argument_list; 284, integer:1; 285, block; 285, 286; 285, 293; 285, 300; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:split; 290, identifier:qteAddWidget; 291, argument_list; 291, 292; 292, identifier:newAppObj; 293, expression_statement; 293, 294; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:split; 297, identifier:setOrientation; 298, argument_list; 298, 299; 299, identifier:splitOrientation; 300, return_statement; 300, 301; 301, True; 302, comment; 303, comment; 304, comment; 305, comment; 306, comment; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:curAppIdx; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:split; 313, identifier:indexOf; 314, argument_list; 314, 315; 315, identifier:curApp; 316, comment; 317, comment; 318, comment; 319, comment; 320, comment; 321, comment; 322, comment; 323, comment; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 327; 326, identifier:newSplit; 327, call; 327, 328; 327, 329; 328, identifier:QtmacsSplitter; 329, argument_list; 329, 330; 329, 331; 330, identifier:splitOrientation; 331, identifier:windowObj; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:curApp; 336, identifier:setParent; 337, argument_list; 337, 338; 338, None; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:newSplit; 343, identifier:qteAddWidget; 344, argument_list; 344, 345; 345, identifier:curApp; 346, expression_statement; 346, 347; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:newSplit; 350, identifier:qteAddWidget; 351, argument_list; 351, 352; 352, identifier:newAppObj; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:split; 357, identifier:insertWidget; 358, argument_list; 358, 359; 358, 360; 359, identifier:curAppIdx; 360, identifier:newSplit; 361, comment; 362, comment; 363, comment; 364, comment; 365, expression_statement; 365, 366; 366, call; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, identifier:split; 369, identifier:qteAdjustWidgetSizes; 370, argument_list; 371, return_statement; 371, 372; 372, True
def qteSplitApplet(self, applet: (QtmacsApplet, str)=None, splitHoriz: bool=True, windowObj: QtmacsWindow=None): """ Reveal ``applet`` by splitting the space occupied by the current applet. If ``applet`` is already visible then the method does nothing. Furthermore, this method does not change the focus, ie. the currently active applet will remain active. If ``applet`` is **None** then the next invisible applet will be shown. If ``windowObj`` is **None** then the currently active window will be used. The ``applet`` parameter can either be an instance of ``QtmacsApplet`` or a string denoting an applet ID. In the latter case the ``qteGetAppletHandle`` method is used to fetch the respective applet instance. |Args| * ``applet`` (**QtmacsApplet**, **str**): the applet to reveal. * ``splitHoriz`` (**bool**): whether to split horizontally or vertically. * ``windowObj`` (**QtmacsWindow**): the window in which to reveal ``applet``. |Returns| * **bool**: if **True**, ``applet`` was revealed. |Raises| * **QtmacsArgumentError** if at least one argument has an invalid type. """ # If ``newAppObj`` was specified by its ID (ie. a string) then # fetch the associated ``QtmacsApplet`` instance. If # ``newAppObj`` is already an instance of ``QtmacsApplet`` # then use it directly. if isinstance(applet, str): newAppObj = self.qteGetAppletHandle(applet) else: newAppObj = applet # Use the currently active window if none was specified. if windowObj is None: windowObj = self.qteActiveWindow() if windowObj is None: msg = 'Cannot determine the currently active window.' self.qteLogger.error(msg, stack_info=True) return # Convert ``splitHoriz`` to the respective Qt constant. if splitHoriz: splitOrientation = QtCore.Qt.Horizontal else: splitOrientation = QtCore.Qt.Vertical if newAppObj is None: # If no new applet was specified use the next available # invisible applet. newAppObj = self.qteNextApplet(skipVisible=True, skipInvisible=False) else: # Do nothing if the new applet is already visible. if newAppObj.qteIsVisible(): return False # If we still have not found an applet then there are no # invisible applets left to show. Therefore, splitting makes # no sense. if newAppObj is None: self.qteLogger.warning('All applets are already visible.') return False # If the root splitter is empty then add the new applet and # return immediately. if windowObj.qteAppletSplitter.count() == 0: windowObj.qteAppletSplitter.qteAddWidget(newAppObj) windowObj.qteAppletSplitter.setOrientation(splitOrientation) return True # ------------------------------------------------------------ # The root splitter contains at least one widget, if we got # this far. # ------------------------------------------------------------ # Shorthand to last active applet in the current window. Query # this applet with qteNextApplet method because # self._qteActiveApplet may be a mini applet, and we are only # interested in genuine applets. curApp = self.qteNextApplet(numSkip=0, windowObj=windowObj) # Get a reference to the splitter in which the currently # active applet lives. This may be the root splitter, or one # of its child splitters. split = self._qteFindAppletInSplitter( curApp, windowObj.qteAppletSplitter) if split is None: msg = 'Active applet <b>{}</b> not in the layout.' msg = msg.format(curApp.qteAppletID()) self.qteLogger.error(msg, stack_info=True) return False # If 'curApp' lives in the root splitter, and the root # splitter contains only a single element, then simply add the # new applet as the second element and return. if split is windowObj.qteAppletSplitter: if split.count() == 1: split.qteAddWidget(newAppObj) split.setOrientation(splitOrientation) return True # ------------------------------------------------------------ # The splitter (root or not) contains two widgets, if we got # this far. # ------------------------------------------------------------ # Determine the index of the applet inside the splitter. curAppIdx = split.indexOf(curApp) # Create a new splitter and populate it with 'curApp' and the # previously invisible ``newAppObj``. Then insert this new splitter at # the position where the old applet was taken from. Note: widgets are # inserted with ``qteAddWidget`` (because they are ``QtmacsApplet`` # instances), whereas splitters are added with ``insertWidget``, NOT # ``qteInsertWidget``. The reason is that splitters do not require the # extra TLC necessary for applets in terms of how and where to show # them. newSplit = QtmacsSplitter(splitOrientation, windowObj) curApp.setParent(None) newSplit.qteAddWidget(curApp) newSplit.qteAddWidget(newAppObj) split.insertWidget(curAppIdx, newSplit) # Adjust the size of two widgets in ``split`` (ie. ``newSplit`` and # whatever other widget) to take up equal space. The same adjusment is # made for ``newSplit``, but there the ``qteAddWidget`` methods have # already taken care of it. split.qteAdjustWidgetSizes() return True