sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:write_file; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:content; 6, default_parameter; 6, 7; 6, 8; 7, identifier:filepath; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:filename; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:indent; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:keys_to_write; 17, None; 18, block; 18, 19; 18, 21; 18, 30; 18, 39; 18, 80; 18, 90; 18, 104; 18, 129; 19, expression_statement; 19, 20; 20, string:''' Write a Python dictionary as JSON to a file. :param content: Dictionary of key-value pairs to save to a file :param filepath: Path where the file is to be created :param filename: Name of the file to be created :param indent: You can use this to specify indent level for pretty printing the file :param keys_to_write: array of keys that are to be picked from data and written to file. Default is None, when all data is written to file. :return: Path of file used :raises OSError, EnvironmentError, ValueError '''; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:path; 24, conditional_expression:if; 24, 25; 24, 26; 24, 27; 25, identifier:filepath; 26, identifier:filepath; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:filepath; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:name; 33, conditional_expression:if; 33, 34; 33, 35; 33, 36; 34, identifier:filename; 35, identifier:filename; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:filename; 39, if_statement; 39, 40; 39, 49; 40, not_operator; 40, 41; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:os; 45, identifier:path; 46, identifier:exists; 47, argument_list; 47, 48; 48, identifier:path; 49, block; 49, 50; 50, try_statement; 50, 51; 50, 59; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:os; 56, identifier:makedirs; 57, argument_list; 57, 58; 58, identifier:path; 59, except_clause; 59, 60; 59, 64; 60, as_pattern; 60, 61; 60, 62; 61, identifier:OSError; 62, as_pattern_target; 62, 63; 63, identifier:error; 64, block; 64, 65; 64, 79; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:logger; 71, identifier:error; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, string:"Error while creating directory: {}"; 76, identifier:format; 77, argument_list; 77, 78; 78, identifier:error; 79, raise_statement; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:name; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:_ends_with; 87, argument_list; 87, 88; 87, 89; 88, identifier:name; 89, string:".json"; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:path; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:_ends_with; 97, argument_list; 97, 98; 97, 99; 98, identifier:path; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:os; 102, identifier:path; 103, identifier:sep; 104, if_statement; 104, 105; 104, 106; 104, 123; 105, identifier:keys_to_write; 106, block; 106, 107; 106, 111; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:data_to_write; 110, dictionary; 111, for_statement; 111, 112; 111, 113; 111, 114; 112, identifier:key; 113, identifier:keys_to_write; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:data_to_write; 119, identifier:key; 120, subscript; 120, 121; 120, 122; 121, identifier:content; 122, identifier:key; 123, else_clause; 123, 124; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:data_to_write; 128, identifier:content; 129, try_statement; 129, 130; 129, 159; 129, 180; 130, block; 130, 131; 130, 138; 130, 149; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:indent; 134, conditional_expression:if; 134, 135; 134, 136; 134, 137; 135, identifier:indent; 136, identifier:indent; 137, integer:2; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:_write_json; 143, argument_list; 143, 144; 143, 145; 143, 146; 143, 147; 143, 148; 144, identifier:path; 145, identifier:name; 146, string:'w'; 147, identifier:data_to_write; 148, identifier:indent; 149, return_statement; 149, 150; 150, call; 150, 151; 150, 156; 151, attribute; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:os; 154, identifier:path; 155, identifier:join; 156, argument_list; 156, 157; 156, 158; 157, identifier:path; 158, identifier:name; 159, except_clause; 159, 160; 159, 164; 160, as_pattern; 160, 161; 160, 162; 161, identifier:EnvironmentError; 162, as_pattern_target; 162, 163; 163, identifier:error; 164, block; 164, 165; 164, 179; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 172; 167, attribute; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:logger; 171, identifier:error; 172, argument_list; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, string:"Error while opening or writing to file: {}"; 176, identifier:format; 177, argument_list; 177, 178; 178, identifier:error; 179, raise_statement; 180, except_clause; 180, 181; 180, 182; 181, identifier:ValueError; 182, block; 182, 183; 183, raise_statement
def write_file(self, content, filepath=None, filename=None, indent=None, keys_to_write=None): ''' Write a Python dictionary as JSON to a file. :param content: Dictionary of key-value pairs to save to a file :param filepath: Path where the file is to be created :param filename: Name of the file to be created :param indent: You can use this to specify indent level for pretty printing the file :param keys_to_write: array of keys that are to be picked from data and written to file. Default is None, when all data is written to file. :return: Path of file used :raises OSError, EnvironmentError, ValueError ''' path = filepath if filepath else self.filepath name = filename if filename else self.filename if not os.path.exists(path): try: os.makedirs(path) except OSError as error: self.logger.error("Error while creating directory: {}".format(error)) raise name = self._ends_with(name, ".json") path = self._ends_with(path, os.path.sep) if keys_to_write: data_to_write = {} for key in keys_to_write: data_to_write[key] = content[key] else: data_to_write = content try: indent = indent if indent else 2 self._write_json(path, name, 'w', data_to_write, indent) return os.path.join(path, name) except EnvironmentError as error: self.logger.error("Error while opening or writing to file: {}".format(error)) raise except ValueError: raise
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:write_values; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:data; 6, default_parameter; 6, 7; 6, 8; 7, identifier:filepath; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:filename; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:indent; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:keys_to_write; 17, None; 18, block; 18, 19; 18, 21; 18, 30; 18, 39; 18, 49; 18, 63; 18, 114; 18, 139; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:name; 24, conditional_expression:if; 24, 25; 24, 26; 24, 27; 25, identifier:filename; 26, identifier:filename; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:filename; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:path; 33, conditional_expression:if; 33, 34; 33, 35; 33, 36; 34, identifier:filepath; 35, identifier:filepath; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:filepath; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:name; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:_ends_with; 46, argument_list; 46, 47; 46, 48; 47, identifier:name; 48, string:".json"; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:path; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:_ends_with; 56, argument_list; 56, 57; 56, 58; 57, identifier:path; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:os; 61, identifier:path; 62, identifier:sep; 63, if_statement; 63, 64; 63, 75; 64, not_operator; 64, 65; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:os; 69, identifier:path; 70, identifier:isfile; 71, argument_list; 71, 72; 72, binary_operator:+; 72, 73; 72, 74; 73, identifier:path; 74, identifier:name; 75, block; 75, 76; 76, try_statement; 76, 77; 76, 89; 76, 110; 77, block; 77, 78; 78, return_statement; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:write_file; 83, argument_list; 83, 84; 83, 85; 83, 86; 83, 87; 83, 88; 84, identifier:data; 85, identifier:path; 86, identifier:name; 87, identifier:indent; 88, identifier:keys_to_write; 89, except_clause; 89, 90; 89, 94; 90, as_pattern; 90, 91; 90, 92; 91, identifier:EnvironmentError; 92, as_pattern_target; 92, 93; 93, identifier:error; 94, block; 94, 95; 94, 109; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:logger; 101, identifier:error; 102, argument_list; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, string:"Error while opening or writing to file: {}"; 106, identifier:format; 107, argument_list; 107, 108; 108, identifier:error; 109, raise_statement; 110, except_clause; 110, 111; 110, 112; 111, identifier:ValueError; 112, block; 112, 113; 113, raise_statement; 114, if_statement; 114, 115; 114, 116; 114, 133; 115, identifier:keys_to_write; 116, block; 116, 117; 116, 121; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:data_to_write; 120, dictionary; 121, for_statement; 121, 122; 121, 123; 121, 124; 122, identifier:key; 123, identifier:keys_to_write; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 130; 127, subscript; 127, 128; 127, 129; 128, identifier:data_to_write; 129, identifier:key; 130, subscript; 130, 131; 130, 132; 131, identifier:data; 132, identifier:key; 133, else_clause; 133, 134; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:data_to_write; 138, identifier:data; 139, try_statement; 139, 140; 139, 255; 139, 276; 140, block; 140, 141; 140, 213; 140, 226; 140, 234; 140, 245; 141, with_statement; 141, 142; 141, 154; 142, with_clause; 142, 143; 143, with_item; 143, 144; 144, as_pattern; 144, 145; 144, 152; 145, call; 145, 146; 145, 147; 146, identifier:open; 147, argument_list; 147, 148; 147, 151; 148, binary_operator:+; 148, 149; 148, 150; 149, identifier:path; 150, identifier:name; 151, string:'r'; 152, as_pattern_target; 152, 153; 153, identifier:fil; 154, block; 154, 155; 154, 164; 154, 178; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:output; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:json; 161, identifier:load; 162, argument_list; 162, 163; 163, identifier:fil; 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:self; 169, identifier:logger; 170, identifier:info; 171, argument_list; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, string:"Read contents of {}"; 175, identifier:format; 176, argument_list; 176, 177; 177, identifier:filename; 178, for_statement; 178, 179; 178, 180; 178, 181; 179, identifier:key; 180, identifier:data_to_write; 181, block; 181, 182; 182, try_statement; 182, 183; 182, 192; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 189; 186, subscript; 186, 187; 186, 188; 187, identifier:output; 188, identifier:key; 189, subscript; 189, 190; 189, 191; 190, identifier:data_to_write; 191, identifier:key; 192, except_clause; 192, 193; 192, 197; 193, as_pattern; 193, 194; 193, 195; 194, identifier:TypeError; 195, as_pattern_target; 195, 196; 196, identifier:error; 197, block; 197, 198; 197, 212; 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:logger; 204, identifier:error; 205, argument_list; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, string:"File contents could not be serialized into a dict. {}"; 209, identifier:format; 210, argument_list; 210, 211; 211, identifier:error; 212, raise_statement; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:_write_json; 218, argument_list; 218, 219; 218, 220; 218, 223; 218, 224; 218, 225; 219, identifier:path; 220, binary_operator:+; 220, 221; 220, 222; 221, identifier:name; 222, string:".temp"; 223, string:"w"; 224, identifier:output; 225, identifier:indent; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:FileUtils; 230, identifier:remove_file; 231, argument_list; 231, 232; 231, 233; 232, identifier:name; 233, identifier:path; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:FileUtils; 238, identifier:rename_file; 239, argument_list; 239, 240; 239, 243; 239, 244; 240, binary_operator:+; 240, 241; 240, 242; 241, identifier:name; 242, string:'.temp'; 243, identifier:name; 244, identifier:path; 245, return_statement; 245, 246; 246, call; 246, 247; 246, 252; 247, attribute; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:os; 250, identifier:path; 251, identifier:join; 252, argument_list; 252, 253; 252, 254; 253, identifier:path; 254, identifier:name; 255, except_clause; 255, 256; 255, 260; 256, as_pattern; 256, 257; 256, 258; 257, identifier:EnvironmentError; 258, as_pattern_target; 258, 259; 259, identifier:error; 260, block; 260, 261; 260, 275; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 268; 263, attribute; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:self; 266, identifier:logger; 267, identifier:error; 268, argument_list; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, string:"Error while writing to, opening or reading the file: {}"; 272, identifier:format; 273, argument_list; 273, 274; 274, identifier:error; 275, raise_statement; 276, except_clause; 276, 277; 276, 281; 277, as_pattern; 277, 278; 277, 279; 278, identifier:ValueError; 279, as_pattern_target; 279, 280; 280, identifier:error; 281, block; 281, 282; 281, 296; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 289; 284, attribute; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:logger; 288, identifier:error; 289, argument_list; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, string:"File could not be decoded to JSON. It might be empty? {}"; 293, identifier:format; 294, argument_list; 294, 295; 295, identifier:error; 296, try_statement; 296, 297; 296, 319; 297, block; 297, 298; 297, 309; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:self; 302, identifier:_write_json; 303, argument_list; 303, 304; 303, 305; 303, 306; 303, 307; 303, 308; 304, identifier:path; 305, identifier:name; 306, string:"w"; 307, identifier:data_to_write; 308, identifier:indent; 309, return_statement; 309, 310; 310, call; 310, 311; 310, 316; 311, attribute; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:os; 314, identifier:path; 315, identifier:join; 316, argument_list; 316, 317; 316, 318; 317, identifier:path; 318, identifier:name; 319, except_clause; 319, 320; 319, 321; 320, identifier:EnvironmentError; 321, block; 321, 322; 322, raise_statement
def write_values(self, data, filepath=None, filename=None, indent=None, keys_to_write=None): """ Tries to write extra content to a JSON file. Creates filename.temp with updated content, removes the old file and finally renames the .temp to match the old file. This is in effort to preserve the data in case of some weird errors cause problems. :param filepath: Path to file :param filename: Name of file :param data: Data to write as a dictionary :param indent: indent level for pretty printing the resulting file :param keys_to_write: array of keys that are to be picked from data and written to file. Default is None, when all data is written to file. :return: Path to file used :raises EnvironmentError ValueError """ name = filename if filename else self.filename path = filepath if filepath else self.filepath name = self._ends_with(name, ".json") path = self._ends_with(path, os.path.sep) if not os.path.isfile(path + name): try: return self.write_file(data, path, name, indent, keys_to_write) except EnvironmentError as error: self.logger.error("Error while opening or writing to file: {}".format(error)) raise except ValueError: raise if keys_to_write: data_to_write = {} for key in keys_to_write: data_to_write[key] = data[key] else: data_to_write = data try: with open(path + name, 'r') as fil: output = json.load(fil) self.logger.info("Read contents of {}".format(filename)) for key in data_to_write: try: output[key] = data_to_write[key] except TypeError as error: self.logger.error( "File contents could not be serialized into a dict. {}".format(error)) raise self._write_json(path, name + ".temp", "w", output, indent) FileUtils.remove_file(name, path) FileUtils.rename_file(name + '.temp', name, path) return os.path.join(path, name) except EnvironmentError as error: self.logger.error( "Error while writing to, opening or reading the file: {}".format(error)) raise except ValueError as error: self.logger.error( "File could not be decoded to JSON. It might be empty? {}".format(error)) try: self._write_json(path, name, "w", data_to_write, indent) return os.path.join(path, name) except EnvironmentError: raise
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_resolve_requirements; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:requirements; 6, block; 6, 7; 6, 9; 6, 26; 6, 39; 6, 50; 6, 54; 6, 65; 6, 75; 6, 86; 6, 87; 6, 189; 6, 249; 6, 255; 7, expression_statement; 7, 8; 8, comment; 9, try_statement; 9, 10; 9, 21; 10, block; 10, 11; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:dut_count; 14, subscript; 14, 15; 14, 20; 15, subscript; 15, 16; 15, 19; 16, subscript; 16, 17; 16, 18; 17, identifier:requirements; 18, string:"duts"; 19, string:"*"; 20, string:"count"; 21, except_clause; 21, 22; 21, 23; 22, identifier:KeyError; 23, block; 23, 24; 24, return_statement; 24, 25; 25, list:[]; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:default_values; 29, dictionary; 29, 30; 29, 33; 29, 36; 30, pair; 30, 31; 30, 32; 31, string:"type"; 32, string:"hardware"; 33, pair; 33, 34; 33, 35; 34, string:"allowed_platforms"; 35, list:[]; 36, pair; 36, 37; 36, 38; 37, string:"nick"; 38, None; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:default_values; 43, identifier:update; 44, argument_list; 44, 45; 45, subscript; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:requirements; 48, string:"duts"; 49, string:"*"; 50, delete_statement; 50, 51; 51, subscript; 51, 52; 51, 53; 52, identifier:default_values; 53, string:"count"; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:dut_keys; 57, call; 57, 58; 57, 59; 58, identifier:list; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:default_values; 63, identifier:keys; 64, argument_list; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:dut_keys; 69, identifier:extend; 70, argument_list; 70, 71; 71, list:["application", "location", "subtype"]; 71, 72; 71, 73; 71, 74; 72, string:"application"; 73, string:"location"; 74, string:"subtype"; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:dut_requirements; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:__generate_indexed_requirements; 82, argument_list; 82, 83; 82, 84; 82, 85; 83, identifier:dut_count; 84, identifier:default_values; 85, identifier:requirements; 86, comment; 87, for_statement; 87, 88; 87, 89; 87, 96; 88, identifier:key; 89, call; 89, 90; 89, 95; 90, attribute; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:requirements; 93, string:"duts"; 94, identifier:keys; 95, argument_list; 96, block; 96, 97; 96, 106; 96, 116; 97, if_statement; 97, 98; 97, 104; 98, not_operator; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:isinstance; 101, argument_list; 101, 102; 101, 103; 102, identifier:key; 103, identifier:string_types; 104, block; 104, 105; 105, continue_statement; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:match; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:re; 112, identifier:search; 113, argument_list; 113, 114; 113, 115; 114, string:r'([\d]{1,})\.\.([\d]{1,})'; 115, identifier:key; 116, if_statement; 116, 117; 116, 118; 117, identifier:match; 118, block; 118, 119; 118, 131; 118, 143; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:first_dut_idx; 122, call; 122, 123; 122, 124; 123, identifier:int; 124, argument_list; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:match; 128, identifier:group; 129, argument_list; 129, 130; 130, integer:1; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:last_dut_idx; 134, call; 134, 135; 134, 136; 135, identifier:int; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:match; 140, identifier:group; 141, argument_list; 141, 142; 142, integer:2; 143, for_statement; 143, 144; 143, 145; 143, 152; 144, identifier:i; 145, call; 145, 146; 145, 147; 146, identifier:range; 147, argument_list; 147, 148; 147, 149; 148, identifier:first_dut_idx; 149, binary_operator:+; 149, 150; 149, 151; 150, identifier:last_dut_idx; 151, integer:1; 152, block; 152, 153; 153, for_statement; 153, 154; 153, 155; 153, 156; 154, identifier:k; 155, identifier:dut_keys; 156, block; 156, 157; 157, if_statement; 157, 158; 157, 165; 158, comparison_operator:in; 158, 159; 158, 160; 159, identifier:k; 160, subscript; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:requirements; 163, string:"duts"; 164, identifier:key; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 175; 168, attribute; 168, 169; 168, 174; 169, subscript; 169, 170; 169, 171; 170, identifier:dut_requirements; 171, binary_operator:-; 171, 172; 171, 173; 172, identifier:i; 173, integer:1; 174, identifier:set; 175, argument_list; 175, 176; 175, 177; 176, identifier:k; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:copy; 180, identifier:copy; 181, argument_list; 181, 182; 182, subscript; 182, 183; 182, 188; 183, subscript; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:requirements; 186, string:"duts"; 187, identifier:key; 188, identifier:k; 189, for_statement; 189, 190; 189, 193; 189, 197; 190, pattern_list; 190, 191; 190, 192; 191, identifier:idx; 192, identifier:req; 193, call; 193, 194; 193, 195; 194, identifier:enumerate; 195, argument_list; 195, 196; 196, identifier:dut_requirements; 197, block; 197, 198; 197, 237; 198, if_statement; 198, 199; 198, 209; 199, call; 199, 200; 199, 201; 200, identifier:isinstance; 201, argument_list; 201, 202; 201, 208; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:req; 205, identifier:get; 206, argument_list; 206, 207; 207, string:"nick"; 208, identifier:string_types; 209, block; 209, 210; 209, 219; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:nick; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:req; 216, identifier:get; 217, argument_list; 217, 218; 218, string:"nick"; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:req; 223, identifier:set; 224, argument_list; 224, 225; 224, 226; 225, string:"nick"; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:ResourceConfig; 229, identifier:__replace_base_variables; 230, argument_list; 230, 231; 230, 232; 230, 236; 231, identifier:nick; 232, call; 232, 233; 232, 234; 233, identifier:len; 234, argument_list; 234, 235; 235, identifier:dut_requirements; 236, identifier:idx; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:self; 241, identifier:_solve_location; 242, argument_list; 242, 243; 242, 244; 242, 248; 243, identifier:req; 244, call; 244, 245; 244, 246; 245, identifier:len; 246, argument_list; 246, 247; 247, identifier:dut_requirements; 248, identifier:idx; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:self; 253, identifier:_dut_requirements; 254, identifier:dut_requirements; 255, return_statement; 255, 256; 256, None
def _resolve_requirements(self, requirements): """ Internal method for resolving requirements into resource configurations. :param requirements: Resource requirements from test case configuration as dictionary. :return: Empty list if dut_count cannot be resolved, or nothing """ try: dut_count = requirements["duts"]["*"]["count"] except KeyError: return [] default_values = { "type": "hardware", "allowed_platforms": [], "nick": None, } default_values.update(requirements["duts"]["*"]) del default_values["count"] dut_keys = list(default_values.keys()) dut_keys.extend(["application", "location", "subtype"]) dut_requirements = self.__generate_indexed_requirements(dut_count, default_values, requirements) # Match groups of duts defined with 1..40 notation. for key in requirements["duts"].keys(): if not isinstance(key, string_types): continue match = re.search(r'([\d]{1,})\.\.([\d]{1,})', key) if match: first_dut_idx = int(match.group(1)) last_dut_idx = int(match.group(2)) for i in range(first_dut_idx, last_dut_idx+1): for k in dut_keys: if k in requirements["duts"][key]: dut_requirements[i-1].set(k, copy.copy(requirements["duts"][key][k])) for idx, req in enumerate(dut_requirements): if isinstance(req.get("nick"), string_types): nick = req.get("nick") req.set("nick", ResourceConfig.__replace_base_variables(nick, len(dut_requirements), idx)) self._solve_location(req, len(dut_requirements), idx) self._dut_requirements = dut_requirements return None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:flash; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:binary_location; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:forceflash; 10, None; 11, block; 11, 12; 11, 14; 11, 32; 11, 77; 11, 78; 11, 100; 11, 101; 11, 109; 11, 118; 11, 135; 11, 276; 11, 295; 11, 303; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 17; 15, not_operator; 15, 16; 16, identifier:Flash; 17, block; 17, 18; 17, 27; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:logger; 24, identifier:error; 25, argument_list; 25, 26; 26, string:"Mbed-flasher not installed!"; 27, raise_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:ImportError; 30, argument_list; 30, 31; 31, string:"Mbed-flasher not installed!"; 32, try_statement; 32, 33; 32, 34; 32, 46; 33, comment; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:build; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:Build; 43, identifier:init; 44, argument_list; 44, 45; 45, identifier:binary_location; 46, except_clause; 46, 47; 46, 51; 47, as_pattern; 47, 48; 47, 49; 48, identifier:NotImplementedError; 49, as_pattern_target; 49, 50; 50, identifier:error; 51, block; 51, 52; 51, 63; 51, 72; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:logger; 58, identifier:error; 59, argument_list; 59, 60; 60, concatenated_string; 60, 61; 60, 62; 61, string:"Build initialization failed. "; 62, string:"Check your build location."; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:logger; 69, identifier:debug; 70, argument_list; 70, 71; 71, identifier:error; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:DutConnectionError; 75, argument_list; 75, 76; 76, identifier:error; 77, comment; 78, if_statement; 78, 79; 78, 88; 79, not_operator; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_flash_needed; 84, argument_list; 84, 85; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:forceflash; 87, identifier:forceflash; 88, block; 88, 89; 88, 98; 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:logger; 95, identifier:info; 96, argument_list; 96, 97; 97, string:"Skipping flash, not needed."; 98, return_statement; 98, 99; 99, True; 100, comment; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:logger; 104, call; 104, 105; 104, 106; 105, identifier:get_external_logger; 106, argument_list; 106, 107; 106, 108; 107, string:"mbed-flasher"; 108, string:"FLS"; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:flasher; 112, call; 112, 113; 112, 114; 113, identifier:Flash; 114, argument_list; 114, 115; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:logger; 117, identifier:logger; 118, if_statement; 118, 119; 118, 123; 119, not_operator; 119, 120; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:device; 123, block; 123, 124; 123, 133; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 131; 126, attribute; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:logger; 130, identifier:error; 131, argument_list; 131, 132; 132, string:"Trying to flash device but device is not there?"; 133, return_statement; 133, 134; 134, False; 135, try_statement; 135, 136; 135, 206; 136, block; 136, 137; 136, 147; 136, 161; 136, 175; 136, 186; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:buildfile; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:build; 145, identifier:get_file; 146, argument_list; 147, if_statement; 147, 148; 147, 150; 148, not_operator; 148, 149; 149, identifier:buildfile; 150, block; 150, 151; 151, raise_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:DutConnectionError; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, string:"Binary {} not found"; 158, identifier:format; 159, argument_list; 159, 160; 160, identifier:buildfile; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:logger; 167, identifier:info; 168, argument_list; 168, 169; 168, 170; 169, string:'Flashing dev: %s'; 170, subscript; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:device; 174, string:'target_id'; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:target_id; 178, call; 178, 179; 178, 184; 179, attribute; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:self; 182, identifier:device; 183, identifier:get; 184, argument_list; 184, 185; 185, string:"target_id"; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:retcode; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:flasher; 192, identifier:flash; 193, argument_list; 193, 194; 193, 197; 193, 200; 194, keyword_argument; 194, 195; 194, 196; 195, identifier:build; 196, identifier:buildfile; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:target_id; 199, identifier:target_id; 200, keyword_argument; 200, 201; 200, 202; 201, identifier:device_mapping_table; 202, list:[self.device]; 202, 203; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:device; 206, except_clause; 206, 207; 206, 211; 207, as_pattern; 207, 208; 207, 209; 208, identifier:FLASHER_ERRORS; 209, as_pattern_target; 209, 210; 210, identifier:error; 211, block; 211, 212; 211, 244; 211, 271; 212, if_statement; 212, 213; 212, 218; 212, 228; 213, comparison_operator:==; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:error; 216, identifier:__class__; 217, identifier:NotImplementedError; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:self; 224, identifier:logger; 225, identifier:error; 226, argument_list; 226, 227; 227, string:"Flashing not supported for this platform!"; 228, elif_clause; 228, 229; 228, 234; 229, comparison_operator:==; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:error; 232, identifier:__class__; 233, identifier:SyntaxError; 234, block; 234, 235; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 242; 237, attribute; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:logger; 241, identifier:error; 242, argument_list; 242, 243; 243, string:"target_id required by mbed-flasher!"; 244, if_statement; 244, 245; 244, 248; 245, comparison_operator:is; 245, 246; 245, 247; 246, identifier:FlashError; 247, None; 248, block; 248, 249; 249, if_statement; 249, 250; 249, 255; 250, comparison_operator:==; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:error; 253, identifier:__class__; 254, identifier:FlashError; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 263; 258, attribute; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:logger; 262, identifier:error; 263, argument_list; 263, 264; 263, 265; 263, 268; 264, string:"Flasher raised the following error: %s Error code: %i"; 265, attribute; 265, 266; 265, 267; 266, identifier:error; 267, identifier:message; 268, attribute; 268, 269; 268, 270; 269, identifier:error; 270, identifier:return_code; 271, raise_statement; 271, 272; 272, call; 272, 273; 272, 274; 273, identifier:DutConnectionError; 274, argument_list; 274, 275; 275, identifier:error; 276, if_statement; 276, 277; 276, 280; 277, comparison_operator:==; 277, 278; 277, 279; 278, identifier:retcode; 279, integer:0; 280, block; 280, 281; 280, 293; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 288; 283, attribute; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:dutinformation; 287, identifier:build_binary_sha1; 288, attribute; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:self; 291, identifier:build; 292, identifier:sha1; 293, return_statement; 293, 294; 294, True; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 302; 297, attribute; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:self; 300, identifier:dutinformation; 301, identifier:build_binary_sha1; 302, None; 303, return_statement; 303, 304; 304, False
def flash(self, binary_location=None, forceflash=None): """ Flash a binary to the target device using mbed-flasher. :param binary_location: Binary to flash to device. :param forceflash: Not used. :return: False if an unknown error was encountered during flashing. True if flasher retcode == 0 :raises: ImportError if mbed-flasher not installed. :raises: DutConnectionError if flashing fails. """ if not Flash: self.logger.error("Mbed-flasher not installed!") raise ImportError("Mbed-flasher not installed!") try: # create build object self.build = Build.init(binary_location) except NotImplementedError as error: self.logger.error("Build initialization failed. " "Check your build location.") self.logger.debug(error) raise DutConnectionError(error) # check if need to flash - depend on forceflash -option if not self._flash_needed(forceflash=forceflash): self.logger.info("Skipping flash, not needed.") return True # initialize mbed-flasher with proper logger logger = get_external_logger("mbed-flasher", "FLS") flasher = Flash(logger=logger) if not self.device: self.logger.error("Trying to flash device but device is not there?") return False try: buildfile = self.build.get_file() if not buildfile: raise DutConnectionError("Binary {} not found".format(buildfile)) self.logger.info('Flashing dev: %s', self.device['target_id']) target_id = self.device.get("target_id") retcode = flasher.flash(build=buildfile, target_id=target_id, device_mapping_table=[self.device]) except FLASHER_ERRORS as error: if error.__class__ == NotImplementedError: self.logger.error("Flashing not supported for this platform!") elif error.__class__ == SyntaxError: self.logger.error("target_id required by mbed-flasher!") if FlashError is not None: if error.__class__ == FlashError: self.logger.error("Flasher raised the following error: %s Error code: %i", error.message, error.return_code) raise DutConnectionError(error) if retcode == 0: self.dutinformation.build_binary_sha1 = self.build.sha1 return True self.dutinformation.build_binary_sha1 = None return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:open_connection; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 20; 5, 48; 5, 66; 5, 84; 5, 199; 5, 253; 5, 266; 5, 267; 5, 285; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, comparison_operator:is; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:readthread; 13, None; 14, block; 14, 15; 15, raise_statement; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:DutConnectionError; 18, argument_list; 18, 19; 19, string:"Trying to open serial port which was already open"; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:logger; 26, identifier:info; 27, argument_list; 27, 28; 27, 42; 28, binary_operator:%; 28, 29; 28, 32; 29, concatenated_string; 29, 30; 29, 31; 30, string:"Open Connection "; 31, string:"for '%s' using '%s' baudrate: %d"; 32, tuple; 32, 33; 32, 36; 32, 39; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:dut_name; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:comport; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:serial_baudrate; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:extra; 44, dictionary; 44, 45; 45, pair; 45, 46; 45, 47; 46, string:'type'; 47, string:'<->'; 48, if_statement; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:serial_xonxoff; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:logger; 59, identifier:debug; 60, argument_list; 60, 61; 61, binary_operator:%; 61, 62; 61, 63; 62, string:"Use software flow control for dut: %s"; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:dut_name; 66, if_statement; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:serial_rtscts; 70, block; 70, 71; 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:self; 76, identifier:logger; 77, identifier:debug; 78, argument_list; 78, 79; 79, binary_operator:%; 79, 80; 79, 81; 80, string:"Use hardware flow control for dut: %s"; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:dut_name; 84, try_statement; 84, 85; 84, 153; 84, 176; 85, block; 85, 86; 85, 97; 85, 107; 85, 117; 85, 127; 85, 137; 85, 145; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:port; 91, call; 91, 92; 91, 93; 92, identifier:EnhancedSerial; 93, argument_list; 93, 94; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:comport; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:port; 103, identifier:baudrate; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:serial_baudrate; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 114; 109, attribute; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:port; 113, identifier:timeout; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:serial_timeout; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 124; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:port; 123, identifier:xonxoff; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:serial_xonxoff; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:port; 133, identifier:rtscts; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:serial_rtscts; 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:self; 142, identifier:port; 143, identifier:flushInput; 144, argument_list; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:port; 151, identifier:flushOutput; 152, argument_list; 153, except_clause; 153, 154; 153, 158; 154, as_pattern; 154, 155; 154, 156; 155, identifier:SerialException; 156, as_pattern_target; 156, 157; 157, identifier:err; 158, block; 158, 159; 158, 168; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:logger; 165, identifier:warning; 166, argument_list; 166, 167; 167, identifier:err; 168, raise_statement; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:DutConnectionError; 171, argument_list; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:str; 174, argument_list; 174, 175; 175, identifier:err; 176, except_clause; 176, 177; 176, 181; 177, as_pattern; 177, 178; 177, 179; 178, identifier:ValueError; 179, as_pattern_target; 179, 180; 180, identifier:err; 181, block; 181, 182; 181, 191; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 189; 184, attribute; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:logger; 188, identifier:warning; 189, argument_list; 189, 190; 190, identifier:err; 191, raise_statement; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:ValueError; 194, argument_list; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:str; 197, argument_list; 197, 198; 198, identifier:err; 199, if_statement; 199, 200; 199, 203; 199, 236; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:ch_mode; 203, block; 203, 204; 203, 227; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 211; 206, attribute; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:logger; 210, identifier:info; 211, argument_list; 211, 212; 211, 221; 212, binary_operator:%; 212, 213; 212, 214; 213, string:"Use chunk-mode with size %d, delay: %.3f when write data"; 214, tuple; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:ch_mode_chunk_size; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:ch_mode_ch_delay; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:extra; 223, dictionary; 223, 224; 224, pair; 224, 225; 224, 226; 225, string:'type'; 226, string:'<->'; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:time; 231, identifier:sleep; 232, argument_list; 232, 233; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:ch_mode_start_delay; 236, else_clause; 236, 237; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 245; 240, attribute; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:logger; 244, identifier:info; 245, argument_list; 245, 246; 245, 247; 246, string:"Use normal serial write mode"; 247, keyword_argument; 247, 248; 247, 249; 248, identifier:extra; 249, dictionary; 249, 250; 250, pair; 250, 251; 250, 252; 251, string:'type'; 252, string:'<->'; 253, if_statement; 253, 254; 253, 259; 254, attribute; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:params; 258, identifier:reset; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:self; 264, identifier:reset; 265, argument_list; 266, comment; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:self; 271, identifier:readthread; 272, call; 272, 273; 272, 274; 273, identifier:Thread; 274, argument_list; 274, 275; 274, 280; 275, keyword_argument; 275, 276; 275, 277; 276, identifier:name; 277, attribute; 277, 278; 277, 279; 278, identifier:self; 279, identifier:name; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:target; 282, attribute; 282, 283; 282, 284; 283, identifier:self; 284, identifier:run; 285, expression_statement; 285, 286; 286, call; 286, 287; 286, 292; 287, attribute; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:self; 290, identifier:readthread; 291, identifier:start; 292, argument_list
def open_connection(self): """ Open serial port connection. :return: Nothing :raises: DutConnectionError if serial port was already open or a SerialException occurs. ValueError if EnhancedSerial __init__ or value setters raise ValueError """ if self.readthread is not None: raise DutConnectionError("Trying to open serial port which was already open") self.logger.info("Open Connection " "for '%s' using '%s' baudrate: %d" % (self.dut_name, self.comport, self.serial_baudrate), extra={'type': '<->'}) if self.serial_xonxoff: self.logger.debug("Use software flow control for dut: %s" % self.dut_name) if self.serial_rtscts: self.logger.debug("Use hardware flow control for dut: %s" % self.dut_name) try: self.port = EnhancedSerial(self.comport) self.port.baudrate = self.serial_baudrate self.port.timeout = self.serial_timeout self.port.xonxoff = self.serial_xonxoff self.port.rtscts = self.serial_rtscts self.port.flushInput() self.port.flushOutput() except SerialException as err: self.logger.warning(err) raise DutConnectionError(str(err)) except ValueError as err: self.logger.warning(err) raise ValueError(str(err)) if self.ch_mode: self.logger.info("Use chunk-mode with size %d, delay: %.3f when write data" % (self.ch_mode_chunk_size, self.ch_mode_ch_delay), extra={'type': '<->'}) time.sleep(self.ch_mode_start_delay) else: self.logger.info("Use normal serial write mode", extra={'type': '<->'}) if self.params.reset: self.reset() # Start the serial reading thread self.readthread = Thread(name=self.name, target=self.run) self.readthread.start()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:print_info; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 25; 5, 29; 5, 33; 5, 51; 5, 73; 5, 190; 5, 224; 5, 233; 5, 242; 5, 249; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:table; 11, call; 11, 12; 11, 13; 12, identifier:PrettyTable; 13, argument_list; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:start_string; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, string:"DutSerial {} \n"; 20, identifier:format; 21, argument_list; 21, 22; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:name; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:row; 28, list:[]; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:info_string; 32, string:""; 33, if_statement; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:config; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:info_string; 41, binary_operator:+; 41, 42; 41, 43; 42, identifier:info_string; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, string:"Configuration for this DUT:\n\n {} \n"; 46, identifier:format; 47, argument_list; 47, 48; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:config; 51, if_statement; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:comport; 55, block; 55, 56; 55, 64; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:table; 60, identifier:add_column; 61, argument_list; 61, 62; 61, 63; 62, string:"COM port"; 63, list:[]; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:row; 68, identifier:append; 69, argument_list; 69, 70; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:comport; 73, if_statement; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:port; 77, block; 77, 78; 77, 106; 77, 134; 77, 162; 78, if_statement; 78, 79; 78, 86; 79, call; 79, 80; 79, 81; 80, identifier:hasattr; 81, argument_list; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:port; 85, string:"baudrate"; 86, block; 86, 87; 86, 95; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:table; 91, identifier:add_column; 92, argument_list; 92, 93; 92, 94; 93, string:"Baudrate"; 94, list:[]; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:row; 99, identifier:append; 100, argument_list; 100, 101; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:port; 105, identifier:baudrate; 106, if_statement; 106, 107; 106, 114; 107, call; 107, 108; 107, 109; 108, identifier:hasattr; 109, argument_list; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:port; 113, string:"xonxoff"; 114, block; 114, 115; 114, 123; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:table; 119, identifier:add_column; 120, argument_list; 120, 121; 120, 122; 121, string:"XON/XOFF"; 122, list:[]; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:row; 127, identifier:append; 128, argument_list; 128, 129; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:port; 133, identifier:xonxoff; 134, if_statement; 134, 135; 134, 142; 135, call; 135, 136; 135, 137; 136, identifier:hasattr; 137, argument_list; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:port; 141, string:"timeout"; 142, block; 142, 143; 142, 151; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:table; 147, identifier:add_column; 148, argument_list; 148, 149; 148, 150; 149, string:"Timeout"; 150, list:[]; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:row; 155, identifier:append; 156, argument_list; 156, 157; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:port; 161, identifier:timeout; 162, if_statement; 162, 163; 162, 170; 163, call; 163, 164; 163, 165; 164, identifier:hasattr; 165, argument_list; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:port; 169, string:"rtscts"; 170, block; 170, 171; 170, 179; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:table; 175, identifier:add_column; 176, argument_list; 176, 177; 176, 178; 177, string:"RTSCTS"; 178, list:[]; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:row; 183, identifier:append; 184, argument_list; 184, 185; 185, attribute; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:port; 189, identifier:rtscts; 190, if_statement; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:location; 194, block; 194, 195; 194, 203; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:table; 199, identifier:add_column; 200, argument_list; 200, 201; 200, 202; 201, string:"Location"; 202, list:[]; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:row; 207, identifier:append; 208, argument_list; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, string:"X = {}, Y = {}"; 212, identifier:format; 213, argument_list; 213, 214; 213, 219; 214, attribute; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:location; 218, identifier:x_coord; 219, attribute; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:self; 222, identifier:location; 223, identifier:y_coord; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 231; 226, attribute; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:logger; 230, identifier:info; 231, argument_list; 231, 232; 232, identifier:start_string; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 240; 235, attribute; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:logger; 239, identifier:debug; 240, argument_list; 240, 241; 241, identifier:info_string; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:table; 246, identifier:add_row; 247, argument_list; 247, 248; 248, identifier:row; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 252; 251, identifier:print; 252, argument_list; 252, 253; 253, identifier:table
def print_info(self): """ Prints Dut information nicely formatted into a table. """ table = PrettyTable() start_string = "DutSerial {} \n".format(self.name) row = [] info_string = "" if self.config: info_string = info_string + "Configuration for this DUT:\n\n {} \n".format(self.config) if self.comport: table.add_column("COM port", []) row.append(self.comport) if self.port: if hasattr(self.port, "baudrate"): table.add_column("Baudrate", []) row.append(self.port.baudrate) if hasattr(self.port, "xonxoff"): table.add_column("XON/XOFF", []) row.append(self.port.xonxoff) if hasattr(self.port, "timeout"): table.add_column("Timeout", []) row.append(self.port.timeout) if hasattr(self.port, "rtscts"): table.add_column("RTSCTS", []) row.append(self.port.rtscts) if self.location: table.add_column("Location", []) row.append("X = {}, Y = {}".format(self.location.x_coord, self.location.y_coord)) self.logger.info(start_string) self.logger.debug(info_string) table.add_row(row) print(table)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:check; 3, parameters; 3, 4; 4, identifier:labels; 5, block; 5, 6; 5, 8; 5, 9; 5, 22; 5, 34; 5, 56; 5, 57; 5, 65; 5, 77; 5, 97; 5, 134; 5, 135; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 16; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 15; 14, identifier:labels; 15, identifier:list; 16, block; 16, 17; 17, raise_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:IOError; 20, argument_list; 20, 21; 21, string:'labels are not in a list'; 22, if_statement; 22, 23; 22, 28; 23, not_operator; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:len; 26, argument_list; 26, 27; 27, identifier:labels; 28, block; 28, 29; 29, raise_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:IOError; 32, argument_list; 32, 33; 33, string:'the labels list is empty'; 34, if_statement; 34, 35; 34, 50; 35, not_operator; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:all; 38, argument_list; 38, 39; 39, list_comprehension; 39, 40; 39, 47; 40, call; 40, 41; 40, 42; 41, identifier:isinstance; 42, argument_list; 42, 43; 42, 44; 43, identifier:l; 44, attribute; 44, 45; 44, 46; 45, identifier:np; 46, identifier:ndarray; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:l; 49, identifier:labels; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:IOError; 54, argument_list; 54, 55; 55, string:'all labels must be numpy arrays'; 56, comment; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:ndim; 60, attribute; 60, 61; 60, 64; 61, subscript; 61, 62; 61, 63; 62, identifier:labels; 63, integer:0; 64, identifier:ndim; 65, if_statement; 65, 66; 65, 71; 66, comparison_operator:not; 66, 67; 66, 68; 67, identifier:ndim; 68, list:[1, 2]; 68, 69; 68, 70; 69, integer:1; 70, integer:2; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:IOError; 75, argument_list; 75, 76; 76, string:'labels dimension must be 1 or 2'; 77, if_statement; 77, 78; 77, 91; 78, not_operator; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:all; 81, argument_list; 81, 82; 82, list_comprehension; 82, 83; 82, 88; 83, comparison_operator:==; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:l; 86, identifier:ndim; 87, identifier:ndim; 88, for_in_clause; 88, 89; 88, 90; 89, identifier:l; 90, identifier:labels; 91, block; 91, 92; 92, raise_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:IOError; 95, argument_list; 95, 96; 96, string:'all labels dimensions must be equal'; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:==; 98, 99; 98, 100; 99, identifier:ndim; 100, integer:2; 101, block; 101, 102; 101, 112; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:shape1; 105, subscript; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:labels; 109, integer:0; 110, identifier:shape; 111, integer:1; 112, if_statement; 112, 113; 112, 128; 113, not_operator; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:all; 116, argument_list; 116, 117; 117, list_comprehension; 117, 118; 117, 125; 118, comparison_operator:==; 118, 119; 118, 124; 119, subscript; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:l; 122, identifier:shape; 123, integer:1; 124, identifier:shape1; 125, for_in_clause; 125, 126; 125, 127; 126, identifier:l; 127, identifier:labels; 128, block; 128, 129; 129, raise_statement; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:IOError; 132, argument_list; 132, 133; 133, string:'all labels must have same shape on 2nd dim'; 134, comment; 135, for_statement; 135, 136; 135, 137; 135, 138; 136, identifier:label; 137, identifier:labels; 138, block; 138, 139; 138, 163; 138, 164; 138, 165; 138, 176; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:index; 142, parenthesized_expression; 142, 143; 143, conditional_expression:if; 143, 144; 143, 150; 143, 155; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:np; 147, identifier:argsort; 148, argument_list; 148, 149; 149, identifier:label; 150, comparison_operator:==; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:label; 153, identifier:ndim; 154, integer:1; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:np; 158, identifier:lexsort; 159, argument_list; 159, 160; 160, attribute; 160, 161; 160, 162; 161, identifier:label; 162, identifier:T; 163, comment; 164, comment; 165, assert_statement; 165, 166; 166, comparison_operator:==; 166, 167; 166, 171; 167, call; 167, 168; 167, 169; 168, identifier:len; 169, argument_list; 169, 170; 170, identifier:index; 171, subscript; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:label; 174, identifier:shape; 175, integer:0; 176, if_statement; 176, 177; 176, 198; 177, not_operator; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:all; 180, generator_expression; 180, 181; 180, 186; 181, comparison_operator:==; 181, 182; 181, 183; 182, identifier:n; 183, subscript; 183, 184; 183, 185; 184, identifier:index; 185, identifier:n; 186, for_in_clause; 186, 187; 186, 188; 187, identifier:n; 188, call; 188, 189; 188, 190; 189, identifier:range; 190, argument_list; 190, 191; 191, binary_operator:-; 191, 192; 191, 197; 192, subscript; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:label; 195, identifier:shape; 196, integer:0; 197, integer:1; 198, block; 198, 199; 199, raise_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:IOError; 202, argument_list; 202, 203; 203, string:'labels are not sorted in increasing order'
def check(labels): """Raise IOError if labels are not correct `labels` must be a list of sorted numpy arrays of equal dimensions (must be 1D or 2D). In the case of 2D labels, the second axis must have the same shape for all labels. """ # type checking if not isinstance(labels, list): raise IOError('labels are not in a list') if not len(labels): raise IOError('the labels list is empty') if not all([isinstance(l, np.ndarray) for l in labels]): raise IOError('all labels must be numpy arrays') # dimension checking ndim = labels[0].ndim if ndim not in [1, 2]: raise IOError('labels dimension must be 1 or 2') if not all([l.ndim == ndim for l in labels]): raise IOError('all labels dimensions must be equal') if ndim == 2: shape1 = labels[0].shape[1] if not all([l.shape[1] == shape1 for l in labels]): raise IOError('all labels must have same shape on 2nd dim') # sort checking for label in labels: index = (np.argsort(label) if label.ndim == 1 else np.lexsort(label.T)) # print label, index # print len(index), label.shape[0] assert len(index) == label.shape[0] if not all(n == index[n] for n in range(label.shape[0]-1)): raise IOError('labels are not sorted in increasing order')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:write; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 4, identifier:filename; 5, identifier:groupname; 6, identifier:items; 7, identifier:times; 8, identifier:features; 9, default_parameter; 9, 10; 9, 11; 10, identifier:properties; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:dformat; 14, string:'dense'; 15, default_parameter; 15, 16; 15, 17; 16, identifier:chunk_size; 17, string:'auto'; 18, default_parameter; 18, 19; 18, 20; 19, identifier:sparsity; 20, float:0.1; 21, default_parameter; 21, 22; 21, 23; 22, identifier:mode; 23, string:'a'; 24, block; 24, 25; 24, 27; 24, 28; 24, 37; 24, 55; 24, 56; 25, expression_statement; 25, 26; 26, comment; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:sparsity; 31, conditional_expression:if; 31, 32; 31, 33; 31, 36; 32, identifier:sparsity; 33, comparison_operator:==; 33, 34; 33, 35; 34, identifier:dformat; 35, string:'sparse'; 36, None; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:data; 40, call; 40, 41; 40, 42; 41, identifier:Data; 42, argument_list; 42, 43; 42, 44; 42, 45; 42, 46; 42, 49; 42, 52; 43, identifier:items; 44, identifier:times; 45, identifier:features; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:properties; 48, identifier:properties; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:sparsity; 51, identifier:sparsity; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:check; 54, True; 55, comment; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 67; 58, attribute; 58, 59; 58, 66; 59, call; 59, 60; 59, 61; 60, identifier:Writer; 61, argument_list; 61, 62; 61, 63; 62, identifier:filename; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:chunk_size; 65, identifier:chunk_size; 66, identifier:write; 67, argument_list; 67, 68; 67, 69; 67, 70; 68, identifier:data; 69, identifier:groupname; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:append; 72, True
def write(filename, groupname, items, times, features, properties=None, dformat='dense', chunk_size='auto', sparsity=0.1, mode='a'): """Write h5features data in a HDF5 file. This function is a wrapper to the Writer class. It has three purposes: * Check parameters for errors (see details below), * Create Items, Times and Features objects * Send them to the Writer. :param str filename: HDF5 file to be writted, potentially serving as a container for many small files. If the file does not exist, it is created. If the file is already a valid HDF5 file, try to append the data in it. :param str groupname: Name of the group to write the data in, or to append the data to if the group already exists in the file. :param items: List of files from which the features where extracted. Items must not contain duplicates. :type items: list of str :param times: Time value for the features array. Elements of a 1D array are considered as the center of the time window associated with the features. A 2D array must have 2 columns corresponding to the begin and end timestamps of the features time window. :type times: list of 1D or 2D numpy arrays :param features: Features should have time along the lines and features along the columns (accomodating row-major storage in hdf5 files). :type features: list of 2D numpy arrays :param properties: Optional. Properties associated with each item. Properties describe the features associated with each item in a dictionnary. It can store parameters or fields recorded by the user. :type properties: list of dictionnaries :param str dformat: Optional. Which format to store the features into (sparse or dense). Default is dense. :param float chunk_size: Optional. In Mo, tuning parameter corresponding to the size of a chunk in the h5file. By default the chunk size is guessed automatically. Tis parameter is ignored if the file already exists. :param float sparsity: Optional. Tuning parameter corresponding to the expected proportion (in [0, 1]) of non-zeros elements on average in a single frame. :param char mode: Optional. The mode for overwriting an existing file, 'a' to append data to the file, 'w' to overwrite it :raise IOError: if the filename is not valid or parameters are inconsistent. :raise NotImplementedError: if dformat == 'sparse' """ # Prepare the data, raise on error sparsity = sparsity if dformat == 'sparse' else None data = Data(items, times, features, properties=properties, sparsity=sparsity, check=True) # Write all that stuff in the HDF5 file's specified group Writer(filename, chunk_size=chunk_size).write(data, groupname, append=True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:read; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:from_item; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:to_item; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:from_time; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:to_time; 16, None; 17, block; 17, 18; 17, 20; 17, 21; 17, 42; 17, 57; 17, 58; 17, 59; 17, 76; 17, 89; 17, 102; 17, 111; 17, 120; 17, 130; 17, 131; 17, 143; 17, 144; 17, 204; 17, 205; 17, 266; 17, 281; 17, 307; 18, expression_statement; 18, 19; 19, comment; 20, comment; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:to_item; 24, None; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:to_item; 29, conditional_expression:if; 29, 30; 29, 38; 29, 41; 30, subscript; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:items; 35, identifier:data; 36, unary_operator:-; 36, 37; 37, integer:1; 38, comparison_operator:is; 38, 39; 38, 40; 39, identifier:from_item; 40, None; 41, identifier:from_item; 42, if_statement; 42, 43; 42, 46; 43, comparison_operator:is; 43, 44; 43, 45; 44, identifier:from_item; 45, None; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:from_item; 50, subscript; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:items; 55, identifier:data; 56, integer:0; 57, comment; 58, comment; 59, if_statement; 59, 60; 59, 70; 60, not_operator; 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:items; 66, identifier:is_valid_interval; 67, argument_list; 67, 68; 67, 69; 68, identifier:from_item; 69, identifier:to_item; 70, block; 70, 71; 71, raise_statement; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:IOError; 74, argument_list; 74, 75; 75, string:'cannot read items: not a valid interval'; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:from_idx; 79, call; 79, 80; 79, 87; 80, attribute; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:items; 85, identifier:data; 86, identifier:index; 87, argument_list; 87, 88; 88, identifier:from_item; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:to_idx; 92, call; 92, 93; 92, 100; 93, attribute; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:items; 98, identifier:data; 99, identifier:index; 100, argument_list; 100, 101; 101, identifier:to_item; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:from_pos; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:_get_item_position; 109, argument_list; 109, 110; 110, identifier:from_idx; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:to_pos; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_get_item_position; 118, argument_list; 118, 119; 119, identifier:to_idx; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:lower; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:_get_from_time; 127, argument_list; 127, 128; 127, 129; 128, identifier:from_time; 129, identifier:from_pos; 130, comment; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:upper; 134, binary_operator:+; 134, 135; 134, 142; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:_get_to_time; 139, argument_list; 139, 140; 139, 141; 140, identifier:to_time; 141, identifier:to_pos; 142, integer:1; 143, comment; 144, if_statement; 144, 145; 144, 150; 144, 156; 145, comparison_operator:==; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:dformat; 149, string:'sparse'; 150, block; 150, 151; 151, raise_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:NotImplementedError; 154, argument_list; 154, 155; 155, string:'Reading sparse features not implemented'; 156, else_clause; 156, 157; 157, block; 157, 158; 157, 193; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:features; 161, parenthesized_expression; 161, 162; 162, conditional_expression:if; 162, 163; 162, 177; 162, 182; 163, attribute; 163, 164; 163, 176; 164, subscript; 164, 165; 164, 170; 164, 172; 165, subscript; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:group; 169, string:'features'; 170, slice; 170, 171; 171, colon; 172, slice; 172, 173; 172, 174; 172, 175; 173, identifier:lower; 174, colon; 175, identifier:upper; 176, identifier:T; 177, comparison_operator:==; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:version; 181, string:'0.1'; 182, subscript; 182, 183; 182, 188; 182, 192; 183, subscript; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:group; 187, string:'features'; 188, slice; 188, 189; 188, 190; 188, 191; 189, identifier:lower; 190, colon; 191, identifier:upper; 192, ellipsis:...; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:labels; 196, subscript; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:_labels_group; 200, slice; 200, 201; 200, 202; 200, 203; 201, identifier:lower; 202, colon; 203, identifier:upper; 204, comment; 205, if_statement; 205, 206; 205, 209; 205, 220; 205, 221; 206, comparison_operator:==; 206, 207; 206, 208; 207, identifier:to_idx; 208, identifier:from_idx; 209, block; 209, 210; 209, 215; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:features; 213, list:[features]; 213, 214; 214, identifier:features; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:labels; 218, list:[labels]; 218, 219; 219, identifier:labels; 220, comment; 221, else_clause; 221, 222; 222, block; 222, 223; 222, 240; 222, 253; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:item_ends; 226, binary_operator:+; 226, 227; 226, 239; 227, binary_operator:-; 227, 228; 227, 236; 228, subscript; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:_index; 232, slice; 232, 233; 232, 234; 232, 235; 233, identifier:from_idx; 234, colon; 235, identifier:to_idx; 236, subscript; 236, 237; 236, 238; 237, identifier:from_pos; 238, integer:0; 239, integer:1; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:features; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:np; 246, identifier:split; 247, argument_list; 247, 248; 247, 249; 247, 250; 248, identifier:features; 249, identifier:item_ends; 250, keyword_argument; 250, 251; 250, 252; 251, identifier:axis; 252, integer:0; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:labels; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:np; 259, identifier:split; 260, argument_list; 260, 261; 260, 262; 260, 263; 261, identifier:labels; 262, identifier:item_ends; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:axis; 265, integer:0; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:items; 269, subscript; 269, 270; 269, 275; 270, attribute; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:self; 273, identifier:items; 274, identifier:data; 275, slice; 275, 276; 275, 277; 275, 278; 276, identifier:from_idx; 277, colon; 278, binary_operator:+; 278, 279; 278, 280; 279, identifier:to_idx; 280, integer:1; 281, if_statement; 281, 282; 281, 287; 281, 292; 282, comparison_operator:is; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:self; 285, identifier:properties; 286, None; 287, block; 287, 288; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:properties; 291, None; 292, else_clause; 292, 293; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:properties; 297, subscript; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:self; 300, identifier:properties; 301, slice; 301, 302; 301, 303; 301, 304; 302, identifier:from_idx; 303, colon; 304, binary_operator:+; 304, 305; 304, 306; 305, identifier:to_idx; 306, integer:1; 307, return_statement; 307, 308; 308, call; 308, 309; 308, 310; 309, identifier:Data; 310, argument_list; 310, 311; 310, 312; 310, 313; 310, 314; 310, 317; 311, identifier:items; 312, identifier:labels; 313, identifier:features; 314, keyword_argument; 314, 315; 314, 316; 315, identifier:properties; 316, identifier:properties; 317, keyword_argument; 317, 318; 317, 319; 318, identifier:check; 319, False
def read(self, from_item=None, to_item=None, from_time=None, to_time=None): """Retrieve requested data coordinates from the h5features index. :param str from_item: Optional. Read the data starting from this item. (defaults to the first stored item) :param str to_item: Optional. Read the data until reaching the item. (defaults to from_item if it was specified and to the last stored item otherwise). :param float from_time: Optional. (defaults to the beginning time in from_item) The specified times are included in the output. :param float to_time: Optional. (defaults to the ending time in to_item) the specified times are included in the output. :return: An instance of h5features.Data read from the file. """ # handling default arguments if to_item is None: to_item = self.items.data[-1] if from_item is None else from_item if from_item is None: from_item = self.items.data[0] # index coordinates of from/to_item. TODO optimize because we # have 4 accesses to list.index() where 2 are enougth. if not self.items.is_valid_interval(from_item, to_item): raise IOError('cannot read items: not a valid interval') from_idx = self.items.data.index(from_item) to_idx = self.items.data.index(to_item) from_pos = self._get_item_position(from_idx) to_pos = self._get_item_position(to_idx) lower = self._get_from_time(from_time, from_pos) # upper included with +1 upper = self._get_to_time(to_time, to_pos) + 1 # Step 2: access actual data if self.dformat == 'sparse': raise NotImplementedError( 'Reading sparse features not implemented') else: features = (self.group['features'][:, lower:upper].T if self.version == '0.1' else self.group['features'][lower:upper, ...]) labels = self._labels_group[lower:upper] # If we read a single item if to_idx == from_idx: features = [features] labels = [labels] # Several items case: split them from the index else: item_ends = self._index[from_idx:to_idx] - from_pos[0] + 1 features = np.split(features, item_ends, axis=0) labels = np.split(labels, item_ends, axis=0) items = self.items.data[from_idx:to_idx + 1] if self.properties is None: properties = None else: properties = self.properties[from_idx:to_idx + 1] return Data( items, labels, features, properties=properties, check=False)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse; 3, parameters; 3, 4; 4, identifier:s; 5, block; 5, 6; 5, 8; 5, 34; 5, 50; 5, 384; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:d; 11, call; 11, 12; 11, 13; 12, identifier:dict; 13, argument_list; 13, 14; 13, 17; 13, 20; 13, 23; 13, 26; 13, 29; 14, keyword_argument; 14, 15; 14, 16; 15, identifier:ninputs; 16, None; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:noutputs; 19, None; 20, keyword_argument; 20, 21; 20, 22; 21, identifier:input_labels; 22, None; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:output_labels; 25, None; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:intype; 28, None; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:cover; 31, call; 31, 32; 31, 33; 32, identifier:set; 33, argument_list; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:lines; 37, list_comprehension; 37, 38; 37, 43; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:line; 41, identifier:strip; 42, argument_list; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:line; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:s; 48, identifier:splitlines; 49, argument_list; 50, for_statement; 50, 51; 50, 54; 50, 61; 50, 62; 51, pattern_list; 51, 52; 51, 53; 52, identifier:i; 53, identifier:line; 54, call; 54, 55; 54, 56; 55, identifier:enumerate; 56, argument_list; 56, 57; 56, 58; 57, identifier:lines; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:start; 60, integer:1; 61, comment; 62, block; 62, 63; 62, 75; 62, 76; 62, 85; 62, 117; 62, 118; 62, 127; 62, 159; 62, 160; 62, 169; 62, 173; 62, 174; 62, 183; 62, 216; 62, 217; 62, 226; 62, 259; 62, 260; 62, 269; 62, 300; 62, 301; 62, 310; 62, 359; 62, 360; 62, 369; 62, 373; 63, if_statement; 63, 64; 63, 73; 64, boolean_operator:or; 64, 65; 64, 67; 65, not_operator; 65, 66; 66, identifier:line; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:_COMMENT; 70, identifier:match; 71, argument_list; 71, 72; 72, identifier:line; 73, block; 73, 74; 74, continue_statement; 75, comment; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:m_in; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:_NINS; 82, identifier:match; 83, argument_list; 83, 84; 84, identifier:line; 85, if_statement; 85, 86; 85, 87; 86, identifier:m_in; 87, block; 87, 88; 88, if_statement; 88, 89; 88, 94; 88, 110; 89, comparison_operator:is; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:d; 92, string:'ninputs'; 93, None; 94, block; 94, 95; 94, 109; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:d; 99, string:'ninputs'; 100, call; 100, 101; 100, 102; 101, identifier:int; 102, argument_list; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:m_in; 106, identifier:group; 107, argument_list; 107, 108; 108, integer:1; 109, continue_statement; 110, else_clause; 110, 111; 111, block; 111, 112; 112, raise_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:Error; 115, argument_list; 115, 116; 116, string:".i declared more than once"; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:m_out; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:_NOUTS; 124, identifier:match; 125, argument_list; 125, 126; 126, identifier:line; 127, if_statement; 127, 128; 127, 129; 128, identifier:m_out; 129, block; 129, 130; 130, if_statement; 130, 131; 130, 136; 130, 152; 131, comparison_operator:is; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:d; 134, string:'noutputs'; 135, None; 136, block; 136, 137; 136, 151; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:d; 141, string:'noutputs'; 142, call; 142, 143; 142, 144; 143, identifier:int; 144, argument_list; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:m_out; 148, identifier:group; 149, argument_list; 149, 150; 150, integer:1; 151, continue_statement; 152, else_clause; 152, 153; 153, block; 153, 154; 154, raise_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:Error; 157, argument_list; 157, 158; 158, string:".o declared more than once"; 159, comment; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:m_prod; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:_PROD; 166, identifier:match; 167, argument_list; 167, 168; 168, identifier:line; 169, if_statement; 169, 170; 169, 171; 170, identifier:m_prod; 171, block; 171, 172; 172, continue_statement; 173, comment; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:m_ilb; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:_ILB; 180, identifier:match; 181, argument_list; 181, 182; 182, identifier:line; 183, if_statement; 183, 184; 183, 185; 184, identifier:m_ilb; 185, block; 185, 186; 186, if_statement; 186, 187; 186, 192; 186, 209; 187, comparison_operator:is; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:d; 190, string:'input_labels'; 191, None; 192, block; 192, 193; 192, 208; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:d; 197, string:'input_labels'; 198, call; 198, 199; 198, 207; 199, attribute; 199, 200; 199, 206; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:m_ilb; 203, identifier:group; 204, argument_list; 204, 205; 205, integer:1; 206, identifier:split; 207, argument_list; 208, continue_statement; 209, else_clause; 209, 210; 210, block; 210, 211; 211, raise_statement; 211, 212; 212, call; 212, 213; 212, 214; 213, identifier:Error; 214, argument_list; 214, 215; 215, string:".ilb declared more than once"; 216, comment; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:m_ob; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:_OB; 223, identifier:match; 224, argument_list; 224, 225; 225, identifier:line; 226, if_statement; 226, 227; 226, 228; 227, identifier:m_ob; 228, block; 228, 229; 229, if_statement; 229, 230; 229, 235; 229, 252; 230, comparison_operator:is; 230, 231; 230, 234; 231, subscript; 231, 232; 231, 233; 232, identifier:d; 233, string:'output_labels'; 234, None; 235, block; 235, 236; 235, 251; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 241; 238, subscript; 238, 239; 238, 240; 239, identifier:d; 240, string:'output_labels'; 241, call; 241, 242; 241, 250; 242, attribute; 242, 243; 242, 249; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:m_ob; 246, identifier:group; 247, argument_list; 247, 248; 248, integer:1; 249, identifier:split; 250, argument_list; 251, continue_statement; 252, else_clause; 252, 253; 253, block; 253, 254; 254, raise_statement; 254, 255; 255, call; 255, 256; 255, 257; 256, identifier:Error; 257, argument_list; 257, 258; 258, string:".ob declared more than once"; 259, comment; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:m_type; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:_TYPE; 266, identifier:match; 267, argument_list; 267, 268; 268, identifier:line; 269, if_statement; 269, 270; 269, 271; 270, identifier:m_type; 271, block; 271, 272; 272, if_statement; 272, 273; 272, 278; 272, 293; 273, comparison_operator:is; 273, 274; 273, 277; 274, subscript; 274, 275; 274, 276; 275, identifier:d; 276, string:'intype'; 277, None; 278, block; 278, 279; 278, 292; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 284; 281, subscript; 281, 282; 281, 283; 282, identifier:d; 283, string:'intype'; 284, subscript; 284, 285; 284, 286; 285, identifier:_TYPES; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:m_type; 289, identifier:group; 290, argument_list; 290, 291; 291, integer:1; 292, continue_statement; 293, else_clause; 293, 294; 294, block; 294, 295; 295, raise_statement; 295, 296; 296, call; 296, 297; 296, 298; 297, identifier:Error; 298, argument_list; 298, 299; 299, string:".type declared more tha once"; 300, comment; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:m_cube; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:_CUBE; 307, identifier:match; 308, argument_list; 308, 309; 309, identifier:line; 310, if_statement; 310, 311; 310, 312; 311, identifier:m_cube; 312, block; 312, 313; 312, 323; 312, 335; 312, 347; 312, 358; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 318; 315, pattern_list; 315, 316; 315, 317; 316, identifier:inputs; 317, identifier:outputs; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:m_cube; 321, identifier:groups; 322, argument_list; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:invec; 326, call; 326, 327; 326, 328; 327, identifier:tuple; 328, generator_expression; 328, 329; 328, 332; 329, subscript; 329, 330; 329, 331; 330, identifier:_INCODE; 331, identifier:c; 332, for_in_clause; 332, 333; 332, 334; 333, identifier:c; 334, identifier:inputs; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 338; 337, identifier:outvec; 338, call; 338, 339; 338, 340; 339, identifier:tuple; 340, generator_expression; 340, 341; 340, 344; 341, subscript; 341, 342; 341, 343; 342, identifier:_OUTCODE; 343, identifier:c; 344, for_in_clause; 344, 345; 344, 346; 345, identifier:c; 346, identifier:outputs; 347, expression_statement; 347, 348; 348, call; 348, 349; 348, 354; 349, attribute; 349, 350; 349, 353; 350, subscript; 350, 351; 350, 352; 351, identifier:d; 352, string:'cover'; 353, identifier:add; 354, argument_list; 354, 355; 355, tuple; 355, 356; 355, 357; 356, identifier:invec; 357, identifier:outvec; 358, continue_statement; 359, comment; 360, expression_statement; 360, 361; 361, assignment; 361, 362; 361, 363; 362, identifier:m_end; 363, call; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:_END; 366, identifier:match; 367, argument_list; 367, 368; 368, identifier:line; 369, if_statement; 369, 370; 369, 371; 370, identifier:m_end; 371, block; 371, 372; 372, continue_statement; 373, raise_statement; 373, 374; 374, call; 374, 375; 374, 376; 375, identifier:Error; 376, argument_list; 376, 377; 377, call; 377, 378; 377, 381; 378, attribute; 378, 379; 378, 380; 379, string:"syntax error on line {}: {}"; 380, identifier:format; 381, argument_list; 381, 382; 381, 383; 382, identifier:i; 383, identifier:line; 384, return_statement; 384, 385; 385, identifier:d
def parse(s): """ Parse an input string in PLA format, and return an intermediate representation dict. Parameters ---------- s : str String containing a PLA. Returns ------- A dict with all PLA information: =============== ============ ================================= Key Value type Value description =============== ============ ================================= ninputs int Number of inputs noutputs int Number of outputs input_labels list Input variable names output_labels list Output function names intype int Cover type: {F, R, FD, FR, DR, FDR} cover set Implicant table =============== ============ ================================= """ d = dict(ninputs=None, noutputs=None, input_labels=None, output_labels=None, intype=None, cover=set()) lines = [line.strip() for line in s.splitlines()] for i, line in enumerate(lines, start=1): # skip comments if not line or _COMMENT.match(line): continue # .i m_in = _NINS.match(line) if m_in: if d['ninputs'] is None: d['ninputs'] = int(m_in.group(1)) continue else: raise Error(".i declared more than once") # .o m_out = _NOUTS.match(line) if m_out: if d['noutputs'] is None: d['noutputs'] = int(m_out.group(1)) continue else: raise Error(".o declared more than once") # ignore .p m_prod = _PROD.match(line) if m_prod: continue # .ilb m_ilb = _ILB.match(line) if m_ilb: if d['input_labels'] is None: d['input_labels'] = m_ilb.group(1).split() continue else: raise Error(".ilb declared more than once") # .ob m_ob = _OB.match(line) if m_ob: if d['output_labels'] is None: d['output_labels'] = m_ob.group(1).split() continue else: raise Error(".ob declared more than once") # .type m_type = _TYPE.match(line) if m_type: if d['intype'] is None: d['intype'] = _TYPES[m_type.group(1)] continue else: raise Error(".type declared more tha once") # cube m_cube = _CUBE.match(line) if m_cube: inputs, outputs = m_cube.groups() invec = tuple(_INCODE[c] for c in inputs) outvec = tuple(_OUTCODE[c] for c in outputs) d['cover'].add((invec, outvec)) continue # ignore .e m_end = _END.match(line) if m_end: continue raise Error("syntax error on line {}: {}".format(i, line)) return d
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:var; 3, parameters; 3, 4; 3, 5; 4, identifier:name; 5, default_parameter; 5, 6; 5, 7; 6, identifier:index; 7, None; 8, block; 8, 9; 8, 11; 8, 18; 8, 53; 8, 62; 8, 92; 8, 147; 8, 196; 8, 225; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:tname; 14, call; 14, 15; 14, 16; 15, identifier:type; 16, argument_list; 16, 17; 17, identifier:name; 18, if_statement; 18, 19; 18, 22; 18, 28; 18, 37; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:tname; 21, identifier:str; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:names; 26, tuple; 26, 27; 27, identifier:name; 28, elif_clause; 28, 29; 28, 32; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:tname; 31, identifier:tuple; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:names; 36, identifier:name; 37, else_clause; 37, 38; 38, block; 38, 39; 38, 43; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:fstr; 42, string:"expected name to be a str or tuple, got {0.__name__}"; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:TypeError; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:fstr; 50, identifier:format; 51, argument_list; 51, 52; 52, identifier:tname; 53, if_statement; 53, 54; 53, 56; 54, not_operator; 54, 55; 55, identifier:names; 56, block; 56, 57; 57, raise_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:ValueError; 60, argument_list; 60, 61; 61, string:"expected at least one name"; 62, for_statement; 62, 63; 62, 64; 62, 65; 63, identifier:name; 64, identifier:names; 65, block; 65, 66; 65, 73; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:tname; 69, call; 69, 70; 69, 71; 70, identifier:type; 71, argument_list; 71, 72; 72, identifier:name; 73, if_statement; 73, 74; 73, 77; 74, comparison_operator:is; 74, 75; 74, 76; 75, identifier:tname; 76, identifier:str; 77, block; 77, 78; 77, 82; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:fstr; 81, string:"expected name to be a str, got {0.__name__}"; 82, raise_statement; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:TypeError; 85, argument_list; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:fstr; 89, identifier:format; 90, argument_list; 90, 91; 91, identifier:tname; 92, if_statement; 92, 93; 92, 96; 92, 103; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:index; 95, None; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:indices; 100, call; 100, 101; 100, 102; 101, identifier:tuple; 102, argument_list; 103, else_clause; 103, 104; 104, block; 104, 105; 104, 112; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:tindex; 108, call; 108, 109; 108, 110; 109, identifier:type; 110, argument_list; 110, 111; 111, identifier:index; 112, if_statement; 112, 113; 112, 116; 112, 122; 112, 131; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:tindex; 115, identifier:int; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:indices; 120, tuple; 120, 121; 121, identifier:index; 122, elif_clause; 122, 123; 122, 126; 123, comparison_operator:is; 123, 124; 123, 125; 124, identifier:tindex; 125, identifier:tuple; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:indices; 130, identifier:index; 131, else_clause; 131, 132; 132, block; 132, 133; 132, 137; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:fstr; 136, string:"expected index to be an int or tuple, got {0.__name__}"; 137, raise_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:TypeError; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:fstr; 144, identifier:format; 145, argument_list; 145, 146; 146, identifier:tindex; 147, for_statement; 147, 148; 147, 149; 147, 150; 148, identifier:index; 149, identifier:indices; 150, block; 150, 151; 150, 158; 150, 177; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:tindex; 154, call; 154, 155; 154, 156; 155, identifier:type; 156, argument_list; 156, 157; 157, identifier:index; 158, if_statement; 158, 159; 158, 162; 159, comparison_operator:is; 159, 160; 159, 161; 160, identifier:tindex; 161, identifier:int; 162, block; 162, 163; 162, 167; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:fstr; 166, string:"expected index to be an int, got {0.__name__}"; 167, raise_statement; 167, 168; 168, call; 168, 169; 168, 170; 169, identifier:TypeError; 170, argument_list; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:fstr; 174, identifier:format; 175, argument_list; 175, 176; 176, identifier:tindex; 177, if_statement; 177, 178; 177, 181; 178, comparison_operator:<; 178, 179; 178, 180; 179, identifier:index; 180, integer:0; 181, block; 181, 182; 181, 186; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:fstr; 185, string:"expected index to be >= 0, got {}"; 186, raise_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:ValueError; 189, argument_list; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:fstr; 193, identifier:format; 194, argument_list; 194, 195; 195, identifier:index; 196, try_statement; 196, 197; 196, 206; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:v; 201, subscript; 201, 202; 201, 203; 202, identifier:VARIABLES; 203, tuple; 203, 204; 203, 205; 204, identifier:names; 205, identifier:indices; 206, except_clause; 206, 207; 206, 208; 207, identifier:KeyError; 208, block; 208, 209; 208, 217; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:v; 212, call; 212, 213; 212, 214; 213, identifier:Variable; 214, argument_list; 214, 215; 214, 216; 215, identifier:names; 216, identifier:indices; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 224; 219, subscript; 219, 220; 219, 221; 220, identifier:VARIABLES; 221, tuple; 221, 222; 221, 223; 222, identifier:names; 223, identifier:indices; 224, identifier:v; 225, return_statement; 225, 226; 226, identifier:v
def var(name, index=None): """Return a unique Variable instance. .. note:: Do **NOT** call this function directly. Instead, use one of the concrete implementations: * :func:`pyeda.boolalg.bdd.bddvar` * :func:`pyeda.boolalg.expr.exprvar`, * :func:`pyeda.boolalg.table.ttvar`. """ tname = type(name) if tname is str: names = (name, ) elif tname is tuple: names = name else: fstr = "expected name to be a str or tuple, got {0.__name__}" raise TypeError(fstr.format(tname)) if not names: raise ValueError("expected at least one name") for name in names: tname = type(name) if tname is not str: fstr = "expected name to be a str, got {0.__name__}" raise TypeError(fstr.format(tname)) if index is None: indices = tuple() else: tindex = type(index) if tindex is int: indices = (index, ) elif tindex is tuple: indices = index else: fstr = "expected index to be an int or tuple, got {0.__name__}" raise TypeError(fstr.format(tindex)) for index in indices: tindex = type(index) if tindex is not int: fstr = "expected index to be an int, got {0.__name__}" raise TypeError(fstr.format(tindex)) if index < 0: fstr = "expected index to be >= 0, got {}" raise ValueError(fstr.format(index)) try: v = VARIABLES[(names, indices)] except KeyError: v = Variable(names, indices) VARIABLES[(names, indices)] = v return v
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_itemize; 3, parameters; 3, 4; 4, identifier:objs; 5, block; 5, 6; 5, 8; 5, 23; 5, 37; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 17; 9, not_operator; 9, 10; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 14; 13, identifier:objs; 14, attribute; 14, 15; 14, 16; 15, identifier:collections; 16, identifier:Sequence; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:TypeError; 21, argument_list; 21, 22; 22, string:"expected a sequence of Function"; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:isseq; 26, list_comprehension; 26, 27; 26, 34; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 31; 30, identifier:obj; 31, attribute; 31, 32; 31, 33; 32, identifier:collections; 33, identifier:Sequence; 34, for_in_clause; 34, 35; 34, 36; 35, identifier:obj; 36, identifier:objs; 37, if_statement; 37, 38; 37, 43; 37, 124; 37, 217; 38, not_operator; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:any; 41, argument_list; 41, 42; 42, identifier:isseq; 43, block; 43, 44; 43, 48; 43, 110; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:ftype; 47, None; 48, for_statement; 48, 49; 48, 50; 48, 51; 49, identifier:obj; 50, identifier:objs; 51, block; 51, 52; 52, if_statement; 52, 53; 52, 56; 52, 97; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:ftype; 55, None; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 63; 57, 68; 57, 79; 57, 90; 58, call; 58, 59; 58, 60; 59, identifier:isinstance; 60, argument_list; 60, 61; 60, 62; 61, identifier:obj; 62, identifier:BinaryDecisionDiagram; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:ftype; 67, identifier:BinaryDecisionDiagram; 68, elif_clause; 68, 69; 68, 74; 69, call; 69, 70; 69, 71; 70, identifier:isinstance; 71, argument_list; 71, 72; 71, 73; 72, identifier:obj; 73, identifier:Expression; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:ftype; 78, identifier:Expression; 79, elif_clause; 79, 80; 79, 85; 80, call; 80, 81; 80, 82; 81, identifier:isinstance; 82, argument_list; 82, 83; 82, 84; 83, identifier:obj; 84, identifier:TruthTable; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:ftype; 89, identifier:TruthTable; 90, else_clause; 90, 91; 91, block; 91, 92; 92, raise_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:TypeError; 95, argument_list; 95, 96; 96, string:"expected valid Function inputs"; 97, elif_clause; 97, 98; 97, 104; 98, not_operator; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:isinstance; 101, argument_list; 101, 102; 101, 103; 102, identifier:obj; 103, identifier:ftype; 104, block; 104, 105; 105, raise_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:ValueError; 108, argument_list; 108, 109; 109, string:"expected uniform Function types"; 110, return_statement; 110, 111; 111, expression_list; 111, 112; 111, 116; 111, 123; 112, call; 112, 113; 112, 114; 113, identifier:list; 114, argument_list; 114, 115; 115, identifier:objs; 116, tuple; 116, 117; 117, tuple; 117, 118; 117, 119; 118, integer:0; 119, call; 119, 120; 119, 121; 120, identifier:len; 121, argument_list; 121, 122; 122, identifier:objs; 123, identifier:ftype; 124, elif_clause; 124, 125; 124, 129; 125, call; 125, 126; 125, 127; 126, identifier:all; 127, argument_list; 127, 128; 128, identifier:isseq; 129, block; 129, 130; 129, 136; 129, 140; 129, 144; 129, 200; 129, 212; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:items; 133, call; 133, 134; 133, 135; 134, identifier:list; 135, argument_list; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:shape; 139, None; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:ftype; 143, None; 144, for_statement; 144, 145; 144, 146; 144, 147; 145, identifier:obj; 146, identifier:objs; 147, block; 147, 148; 147, 158; 147, 177; 147, 196; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 154; 150, pattern_list; 150, 151; 150, 152; 150, 153; 151, identifier:_items; 152, identifier:_shape; 153, identifier:_ftype; 154, call; 154, 155; 154, 156; 155, identifier:_itemize; 156, argument_list; 156, 157; 157, identifier:obj; 158, if_statement; 158, 159; 158, 162; 158, 167; 159, comparison_operator:is; 159, 160; 159, 161; 160, identifier:shape; 161, None; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:shape; 166, identifier:_shape; 167, elif_clause; 167, 168; 167, 171; 168, comparison_operator:!=; 168, 169; 168, 170; 169, identifier:shape; 170, identifier:_shape; 171, block; 171, 172; 172, raise_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:ValueError; 175, argument_list; 175, 176; 176, string:"expected uniform farray dimensions"; 177, if_statement; 177, 178; 177, 181; 177, 186; 178, comparison_operator:is; 178, 179; 178, 180; 179, identifier:ftype; 180, None; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:ftype; 185, identifier:_ftype; 186, elif_clause; 186, 187; 186, 190; 187, comparison_operator:!=; 187, 188; 187, 189; 188, identifier:ftype; 189, identifier:_ftype; 190, block; 190, 191; 191, raise_statement; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:ValueError; 194, argument_list; 194, 195; 195, string:"expected uniform Function types"; 196, expression_statement; 196, 197; 197, augmented_assignment:+=; 197, 198; 197, 199; 198, identifier:items; 199, identifier:_items; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:shape; 203, binary_operator:+; 203, 204; 203, 211; 204, tuple; 204, 205; 205, tuple; 205, 206; 205, 207; 206, integer:0; 207, call; 207, 208; 207, 209; 208, identifier:len; 209, argument_list; 209, 210; 210, identifier:objs; 211, identifier:shape; 212, return_statement; 212, 213; 213, expression_list; 213, 214; 213, 215; 213, 216; 214, identifier:items; 215, identifier:shape; 216, identifier:ftype; 217, else_clause; 217, 218; 218, block; 218, 219; 219, raise_statement; 219, 220; 220, call; 220, 221; 220, 222; 221, identifier:ValueError; 222, argument_list; 222, 223; 223, string:"expected uniform farray dimensions"
def _itemize(objs): """Recursive helper function for farray.""" if not isinstance(objs, collections.Sequence): raise TypeError("expected a sequence of Function") isseq = [isinstance(obj, collections.Sequence) for obj in objs] if not any(isseq): ftype = None for obj in objs: if ftype is None: if isinstance(obj, BinaryDecisionDiagram): ftype = BinaryDecisionDiagram elif isinstance(obj, Expression): ftype = Expression elif isinstance(obj, TruthTable): ftype = TruthTable else: raise TypeError("expected valid Function inputs") elif not isinstance(obj, ftype): raise ValueError("expected uniform Function types") return list(objs), ((0, len(objs)), ), ftype elif all(isseq): items = list() shape = None ftype = None for obj in objs: _items, _shape, _ftype = _itemize(obj) if shape is None: shape = _shape elif shape != _shape: raise ValueError("expected uniform farray dimensions") if ftype is None: ftype = _ftype elif ftype != _ftype: raise ValueError("expected uniform Function types") items += _items shape = ((0, len(objs)), ) + shape return items, shape, ftype else: raise ValueError("expected uniform farray dimensions")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_filtdim; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:items; 5, identifier:shape; 6, identifier:dim; 7, identifier:nsl; 8, block; 8, 9; 8, 11; 8, 25; 8, 32; 8, 38; 8, 39; 8, 55; 8, 56; 8, 65; 8, 66; 8, 72; 8, 350; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:normshape; 14, call; 14, 15; 14, 16; 15, identifier:tuple; 16, generator_expression; 16, 17; 16, 20; 17, binary_operator:-; 17, 18; 17, 19; 18, identifier:stop; 19, identifier:start; 20, for_in_clause; 20, 21; 20, 24; 21, pattern_list; 21, 22; 21, 23; 22, identifier:start; 23, identifier:stop; 24, identifier:shape; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:nsl_type; 28, call; 28, 29; 28, 30; 29, identifier:type; 30, argument_list; 30, 31; 31, identifier:nsl; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:newitems; 35, call; 35, 36; 35, 37; 36, identifier:list; 37, argument_list; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:num; 42, call; 42, 43; 42, 44; 43, identifier:reduce; 44, argument_list; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:operator; 47, identifier:mul; 48, subscript; 48, 49; 48, 50; 49, identifier:normshape; 50, slice; 50, 51; 50, 52; 51, colon; 52, binary_operator:+; 52, 53; 52, 54; 53, identifier:dim; 54, integer:1; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:size; 59, binary_operator://; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:items; 64, identifier:num; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:n; 69, subscript; 69, 70; 69, 71; 70, identifier:normshape; 71, identifier:dim; 72, if_statement; 72, 73; 72, 76; 72, 124; 72, 207; 72, 208; 73, comparison_operator:is; 73, 74; 73, 75; 74, identifier:nsl_type; 75, identifier:int; 76, block; 76, 77; 76, 107; 76, 108; 77, for_statement; 77, 78; 77, 79; 77, 83; 78, identifier:i; 79, call; 79, 80; 79, 81; 80, identifier:range; 81, argument_list; 81, 82; 82, identifier:num; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 90; 85, comparison_operator:==; 85, 86; 85, 89; 86, binary_operator:%; 86, 87; 86, 88; 87, identifier:i; 88, identifier:n; 89, identifier:nsl; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, augmented_assignment:+=; 92, 93; 92, 94; 93, identifier:newitems; 94, subscript; 94, 95; 94, 96; 95, identifier:items; 96, slice; 96, 97; 96, 100; 96, 101; 97, binary_operator:*; 97, 98; 97, 99; 98, identifier:size; 99, identifier:i; 100, colon; 101, binary_operator:*; 101, 102; 101, 103; 102, identifier:size; 103, parenthesized_expression; 103, 104; 104, binary_operator:+; 104, 105; 104, 106; 105, identifier:i; 106, integer:1; 107, comment; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:newshape; 111, binary_operator:+; 111, 112; 111, 117; 112, subscript; 112, 113; 112, 114; 113, identifier:shape; 114, slice; 114, 115; 114, 116; 115, colon; 116, identifier:dim; 117, subscript; 117, 118; 117, 119; 118, identifier:shape; 119, slice; 119, 120; 119, 123; 120, binary_operator:+; 120, 121; 120, 122; 121, identifier:dim; 122, integer:1; 123, colon; 124, elif_clause; 124, 125; 124, 128; 125, comparison_operator:is; 125, 126; 125, 127; 126, identifier:nsl_type; 127, identifier:slice; 128, block; 128, 129; 128, 165; 128, 166; 128, 174; 128, 188; 129, for_statement; 129, 130; 129, 131; 129, 135; 130, identifier:i; 131, call; 131, 132; 131, 133; 132, identifier:range; 133, argument_list; 133, 134; 134, identifier:num; 135, block; 135, 136; 136, if_statement; 136, 137; 136, 148; 137, comparison_operator:<=; 137, 138; 137, 141; 137, 145; 138, attribute; 138, 139; 138, 140; 139, identifier:nsl; 140, identifier:start; 141, parenthesized_expression; 141, 142; 142, binary_operator:%; 142, 143; 142, 144; 143, identifier:i; 144, identifier:n; 145, attribute; 145, 146; 145, 147; 146, identifier:nsl; 147, identifier:stop; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, augmented_assignment:+=; 150, 151; 150, 152; 151, identifier:newitems; 152, subscript; 152, 153; 152, 154; 153, identifier:items; 154, slice; 154, 155; 154, 158; 154, 159; 155, binary_operator:*; 155, 156; 155, 157; 156, identifier:size; 157, identifier:i; 158, colon; 159, binary_operator:*; 159, 160; 159, 161; 160, identifier:size; 161, parenthesized_expression; 161, 162; 162, binary_operator:+; 162, 163; 162, 164; 163, identifier:i; 164, integer:1; 165, comment; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:offset; 169, subscript; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:shape; 172, identifier:dim; 173, integer:0; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:redim; 177, tuple; 177, 178; 177, 183; 178, binary_operator:+; 178, 179; 178, 180; 179, identifier:offset; 180, attribute; 180, 181; 180, 182; 181, identifier:nsl; 182, identifier:start; 183, binary_operator:+; 183, 184; 183, 185; 184, identifier:offset; 185, attribute; 185, 186; 185, 187; 186, identifier:nsl; 187, identifier:stop; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:newshape; 191, binary_operator:+; 191, 192; 191, 200; 192, binary_operator:+; 192, 193; 192, 198; 193, subscript; 193, 194; 193, 195; 194, identifier:shape; 195, slice; 195, 196; 195, 197; 196, colon; 197, identifier:dim; 198, tuple; 198, 199; 199, identifier:redim; 200, subscript; 200, 201; 200, 202; 201, identifier:shape; 202, slice; 202, 203; 202, 206; 203, binary_operator:+; 203, 204; 203, 205; 204, identifier:dim; 205, integer:1; 206, colon; 207, comment; 208, else_clause; 208, 209; 209, block; 209, 210; 209, 241; 209, 254; 209, 281; 209, 333; 209, 334; 210, if_statement; 210, 211; 210, 219; 211, comparison_operator:<; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:nsl; 214, identifier:size; 215, call; 215, 216; 215, 217; 216, identifier:clog2; 217, argument_list; 217, 218; 218, identifier:n; 219, block; 219, 220; 219, 224; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:fstr; 223, string:"expected dim {} select to have >= {} bits, got {}"; 224, raise_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:ValueError; 227, argument_list; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:fstr; 231, identifier:format; 232, argument_list; 232, 233; 232, 234; 232, 238; 233, identifier:dim; 234, call; 234, 235; 234, 236; 235, identifier:clog2; 236, argument_list; 236, 237; 237, identifier:n; 238, attribute; 238, 239; 238, 240; 239, identifier:nsl; 240, identifier:size; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:groups; 244, list_comprehension; 244, 245; 244, 248; 245, call; 245, 246; 245, 247; 246, identifier:list; 247, argument_list; 248, for_in_clause; 248, 249; 248, 250; 249, identifier:_; 250, call; 250, 251; 250, 252; 251, identifier:range; 252, argument_list; 252, 253; 253, identifier:n; 254, for_statement; 254, 255; 254, 256; 254, 260; 255, identifier:i; 256, call; 256, 257; 256, 258; 257, identifier:range; 258, argument_list; 258, 259; 259, identifier:num; 260, block; 260, 261; 261, expression_statement; 261, 262; 262, augmented_assignment:+=; 262, 263; 262, 268; 263, subscript; 263, 264; 263, 265; 264, identifier:groups; 265, binary_operator:%; 265, 266; 265, 267; 266, identifier:i; 267, identifier:n; 268, subscript; 268, 269; 268, 270; 269, identifier:items; 270, slice; 270, 271; 270, 274; 270, 275; 271, binary_operator:*; 271, 272; 271, 273; 272, identifier:size; 273, identifier:i; 274, colon; 275, binary_operator:*; 275, 276; 275, 277; 276, identifier:size; 277, parenthesized_expression; 277, 278; 278, binary_operator:+; 278, 279; 278, 280; 279, identifier:i; 280, integer:1; 281, for_statement; 281, 282; 281, 283; 281, 288; 282, identifier:muxins; 283, call; 283, 284; 283, 285; 284, identifier:zip; 285, argument_list; 285, 286; 286, list_splat; 286, 287; 287, identifier:groups; 288, block; 288, 289; 288, 300; 288, 320; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:it; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:boolfunc; 295, identifier:iter_terms; 296, argument_list; 296, 297; 297, attribute; 297, 298; 297, 299; 298, identifier:nsl; 299, identifier:_items; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:xs; 303, list_comprehension; 303, 304; 303, 317; 304, call; 304, 305; 304, 306; 305, identifier:reduce; 306, argument_list; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:operator; 309, identifier:and_; 310, binary_operator:+; 310, 311; 310, 313; 311, tuple; 311, 312; 312, identifier:muxin; 313, call; 313, 314; 313, 315; 314, identifier:next; 315, argument_list; 315, 316; 316, identifier:it; 317, for_in_clause; 317, 318; 317, 319; 318, identifier:muxin; 319, identifier:muxins; 320, expression_statement; 320, 321; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:newitems; 324, identifier:append; 325, argument_list; 325, 326; 326, call; 326, 327; 326, 328; 327, identifier:reduce; 328, argument_list; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:operator; 331, identifier:or_; 332, identifier:xs; 333, comment; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 337; 336, identifier:newshape; 337, binary_operator:+; 337, 338; 337, 343; 338, subscript; 338, 339; 338, 340; 339, identifier:shape; 340, slice; 340, 341; 340, 342; 341, colon; 342, identifier:dim; 343, subscript; 343, 344; 343, 345; 344, identifier:shape; 345, slice; 345, 346; 345, 349; 346, binary_operator:+; 346, 347; 346, 348; 347, identifier:dim; 348, integer:1; 349, colon; 350, return_statement; 350, 351; 351, expression_list; 351, 352; 351, 353; 352, identifier:newitems; 353, identifier:newshape
def _filtdim(items, shape, dim, nsl): """Return items, shape filtered by a dimension slice.""" normshape = tuple(stop - start for start, stop in shape) nsl_type = type(nsl) newitems = list() # Number of groups num = reduce(operator.mul, normshape[:dim+1]) # Size of each group size = len(items) // num # Size of the dimension n = normshape[dim] if nsl_type is int: for i in range(num): if i % n == nsl: newitems += items[size*i:size*(i+1)] # Collapse dimension newshape = shape[:dim] + shape[dim+1:] elif nsl_type is slice: for i in range(num): if nsl.start <= (i % n) < nsl.stop: newitems += items[size*i:size*(i+1)] # Reshape dimension offset = shape[dim][0] redim = (offset + nsl.start, offset + nsl.stop) newshape = shape[:dim] + (redim, ) + shape[dim+1:] # farray else: if nsl.size < clog2(n): fstr = "expected dim {} select to have >= {} bits, got {}" raise ValueError(fstr.format(dim, clog2(n), nsl.size)) groups = [list() for _ in range(n)] for i in range(num): groups[i % n] += items[size*i:size*(i+1)] for muxins in zip(*groups): it = boolfunc.iter_terms(nsl._items) xs = [reduce(operator.and_, (muxin, ) + next(it)) for muxin in muxins] newitems.append(reduce(operator.or_, xs)) # Collapse dimension newshape = shape[:dim] + shape[dim+1:] return newitems, newshape
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 1, 10; 2, function_name:parse; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:text; 6, type; 6, 7; 7, identifier:str; 8, type; 8, 9; 9, identifier:Docstring; 10, block; 10, 11; 10, 13; 10, 19; 10, 25; 10, 26; 10, 35; 10, 36; 10, 45; 10, 82; 10, 83; 10, 93; 10, 103; 10, 153; 10, 154; 10, 166; 10, 172; 10, 176; 10, 211; 10, 230; 10, 234; 10, 278; 10, 284; 10, 285; 10, 488; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:ret; 16, call; 16, 17; 16, 18; 17, identifier:Docstring; 18, argument_list; 19, if_statement; 19, 20; 19, 22; 20, not_operator; 20, 21; 21, identifier:text; 22, block; 22, 23; 23, return_statement; 23, 24; 24, identifier:ret; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:text; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:inspect; 32, identifier:cleandoc; 33, argument_list; 33, 34; 34, identifier:text; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:match; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:_titles_re; 42, identifier:search; 43, argument_list; 43, 44; 44, identifier:text; 45, if_statement; 45, 46; 45, 47; 45, 72; 46, identifier:match; 47, block; 47, 48; 47, 60; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:desc_chunk; 51, subscript; 51, 52; 51, 53; 52, identifier:text; 53, slice; 53, 54; 53, 55; 54, colon; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:match; 58, identifier:start; 59, argument_list; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:meta_chunk; 63, subscript; 63, 64; 63, 65; 64, identifier:text; 65, slice; 65, 66; 65, 71; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:match; 69, identifier:start; 70, argument_list; 71, colon; 72, else_clause; 72, 73; 73, block; 73, 74; 73, 78; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:desc_chunk; 77, identifier:text; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:meta_chunk; 81, string:""; 82, comment; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:parts; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:desc_chunk; 89, identifier:split; 90, argument_list; 90, 91; 90, 92; 91, string:"\n"; 92, integer:1; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:ret; 97, identifier:short_description; 98, boolean_operator:or; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:parts; 101, integer:0; 102, None; 103, if_statement; 103, 104; 103, 110; 104, comparison_operator:>; 104, 105; 104, 109; 105, call; 105, 106; 105, 107; 106, identifier:len; 107, argument_list; 107, 108; 108, identifier:parts; 109, integer:1; 110, block; 110, 111; 110, 119; 110, 130; 110, 141; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:long_desc_chunk; 114, boolean_operator:or; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:parts; 117, integer:1; 118, string:""; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:ret; 123, identifier:blank_after_short_description; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:long_desc_chunk; 127, identifier:startswith; 128, argument_list; 128, 129; 129, string:"\n"; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:ret; 134, identifier:blank_after_long_description; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:long_desc_chunk; 138, identifier:endswith; 139, argument_list; 139, 140; 140, string:"\n\n"; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:ret; 145, identifier:long_description; 146, boolean_operator:or; 146, 147; 146, 152; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:long_desc_chunk; 150, identifier:strip; 151, argument_list; 152, None; 153, comment; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:matches; 157, call; 157, 158; 157, 159; 158, identifier:list; 159, argument_list; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:_titles_re; 163, identifier:finditer; 164, argument_list; 164, 165; 165, identifier:meta_chunk; 166, if_statement; 166, 167; 166, 169; 167, not_operator; 167, 168; 168, identifier:matches; 169, block; 169, 170; 170, return_statement; 170, 171; 171, identifier:ret; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:splits; 175, list:[]; 176, for_statement; 176, 177; 176, 178; 176, 187; 177, identifier:j; 178, call; 178, 179; 178, 180; 179, identifier:range; 180, argument_list; 180, 181; 181, binary_operator:-; 181, 182; 181, 186; 182, call; 182, 183; 182, 184; 183, identifier:len; 184, argument_list; 184, 185; 185, identifier:matches; 186, integer:1; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:splits; 192, identifier:append; 193, argument_list; 193, 194; 194, tuple; 194, 195; 194, 202; 195, call; 195, 196; 195, 201; 196, attribute; 196, 197; 196, 200; 197, subscript; 197, 198; 197, 199; 198, identifier:matches; 199, identifier:j; 200, identifier:end; 201, argument_list; 202, call; 202, 203; 202, 210; 203, attribute; 203, 204; 203, 209; 204, subscript; 204, 205; 204, 206; 205, identifier:matches; 206, binary_operator:+; 206, 207; 206, 208; 207, identifier:j; 208, integer:1; 209, identifier:start; 210, argument_list; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:splits; 215, identifier:append; 216, argument_list; 216, 217; 217, tuple; 217, 218; 217, 226; 218, call; 218, 219; 218, 225; 219, attribute; 219, 220; 219, 224; 220, subscript; 220, 221; 220, 222; 221, identifier:matches; 222, unary_operator:-; 222, 223; 223, integer:1; 224, identifier:end; 225, argument_list; 226, call; 226, 227; 226, 228; 227, identifier:len; 228, argument_list; 228, 229; 229, identifier:meta_chunk; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:chunks; 233, dictionary; 234, for_statement; 234, 235; 234, 240; 234, 244; 235, pattern_list; 235, 236; 235, 237; 236, identifier:j; 237, tuple_pattern; 237, 238; 237, 239; 238, identifier:start; 239, identifier:end; 240, call; 240, 241; 240, 242; 241, identifier:enumerate; 242, argument_list; 242, 243; 243, identifier:splits; 244, block; 244, 245; 244, 256; 244, 262; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:title; 248, call; 248, 249; 248, 254; 249, attribute; 249, 250; 249, 253; 250, subscript; 250, 251; 250, 252; 251, identifier:matches; 252, identifier:j; 253, identifier:group; 254, argument_list; 254, 255; 255, integer:1; 256, if_statement; 256, 257; 256, 260; 257, comparison_operator:not; 257, 258; 257, 259; 258, identifier:title; 259, identifier:_valid; 260, block; 260, 261; 261, continue_statement; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 267; 264, subscript; 264, 265; 264, 266; 265, identifier:chunks; 266, identifier:title; 267, call; 267, 268; 267, 276; 268, attribute; 268, 269; 268, 275; 269, subscript; 269, 270; 269, 271; 270, identifier:meta_chunk; 271, slice; 271, 272; 271, 273; 271, 274; 272, identifier:start; 273, colon; 274, identifier:end; 275, identifier:strip; 276, argument_list; 276, 277; 277, string:"\n"; 278, if_statement; 278, 279; 278, 281; 279, not_operator; 279, 280; 280, identifier:chunks; 281, block; 281, 282; 282, return_statement; 282, 283; 283, identifier:ret; 284, comment; 285, for_statement; 285, 286; 285, 289; 285, 294; 285, 295; 286, pattern_list; 286, 287; 286, 288; 287, identifier:title; 288, identifier:chunk; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:chunks; 292, identifier:items; 293, argument_list; 294, comment; 295, block; 295, 296; 295, 306; 295, 315; 295, 323; 295, 324; 295, 356; 295, 357; 295, 365; 295, 383; 295, 392; 295, 396; 295, 431; 295, 450; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 299; 298, identifier:indent_match; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:re; 302, identifier:search; 303, argument_list; 303, 304; 303, 305; 304, string:r"^\s+"; 305, identifier:chunk; 306, if_statement; 306, 307; 306, 309; 307, not_operator; 307, 308; 308, identifier:indent_match; 309, block; 309, 310; 310, raise_statement; 310, 311; 311, call; 311, 312; 311, 313; 312, identifier:ParseError; 313, argument_list; 313, 314; 314, string:f'Can\'t infer indent from "{chunk}"'; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:indent; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:indent_match; 321, identifier:group; 322, argument_list; 323, comment; 324, if_statement; 324, 325; 324, 332; 325, comparison_operator:in; 325, 326; 325, 329; 326, subscript; 326, 327; 326, 328; 327, identifier:_sections; 328, identifier:title; 329, tuple; 329, 330; 329, 331; 330, string:"returns"; 331, string:"yields"; 332, block; 332, 333; 332, 342; 332, 355; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:part; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:inspect; 339, identifier:cleandoc; 340, argument_list; 340, 341; 341, identifier:chunk; 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:ret; 347, identifier:meta; 348, identifier:append; 349, argument_list; 349, 350; 350, call; 350, 351; 350, 352; 351, identifier:_build_meta; 352, argument_list; 352, 353; 352, 354; 353, identifier:part; 354, identifier:title; 355, continue_statement; 356, comment; 357, expression_statement; 357, 358; 358, assignment; 358, 359; 358, 360; 359, identifier:_re; 360, binary_operator:+; 360, 361; 360, 364; 361, binary_operator:+; 361, 362; 361, 363; 362, string:"^"; 363, identifier:indent; 364, string:r"(?=\S)"; 365, expression_statement; 365, 366; 366, assignment; 366, 367; 366, 368; 367, identifier:c_matches; 368, call; 368, 369; 368, 370; 369, identifier:list; 370, argument_list; 370, 371; 371, call; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:re; 374, identifier:finditer; 375, argument_list; 375, 376; 375, 377; 375, 378; 376, identifier:_re; 377, identifier:chunk; 378, keyword_argument; 378, 379; 378, 380; 379, identifier:flags; 380, attribute; 380, 381; 380, 382; 381, identifier:re; 382, identifier:M; 383, if_statement; 383, 384; 383, 386; 384, not_operator; 384, 385; 385, identifier:c_matches; 386, block; 386, 387; 387, raise_statement; 387, 388; 388, call; 388, 389; 388, 390; 389, identifier:ParseError; 390, argument_list; 390, 391; 391, string:f'No specification for "{title}": "{chunk}"'; 392, expression_statement; 392, 393; 393, assignment; 393, 394; 393, 395; 394, identifier:c_splits; 395, list:[]; 396, for_statement; 396, 397; 396, 398; 396, 407; 397, identifier:j; 398, call; 398, 399; 398, 400; 399, identifier:range; 400, argument_list; 400, 401; 401, binary_operator:-; 401, 402; 401, 406; 402, call; 402, 403; 402, 404; 403, identifier:len; 404, argument_list; 404, 405; 405, identifier:c_matches; 406, integer:1; 407, block; 407, 408; 408, expression_statement; 408, 409; 409, call; 409, 410; 409, 413; 410, attribute; 410, 411; 410, 412; 411, identifier:c_splits; 412, identifier:append; 413, argument_list; 413, 414; 414, tuple; 414, 415; 414, 422; 415, call; 415, 416; 415, 421; 416, attribute; 416, 417; 416, 420; 417, subscript; 417, 418; 417, 419; 418, identifier:c_matches; 419, identifier:j; 420, identifier:end; 421, argument_list; 422, call; 422, 423; 422, 430; 423, attribute; 423, 424; 423, 429; 424, subscript; 424, 425; 424, 426; 425, identifier:c_matches; 426, binary_operator:+; 426, 427; 426, 428; 427, identifier:j; 428, integer:1; 429, identifier:start; 430, argument_list; 431, expression_statement; 431, 432; 432, call; 432, 433; 432, 436; 433, attribute; 433, 434; 433, 435; 434, identifier:c_splits; 435, identifier:append; 436, argument_list; 436, 437; 437, tuple; 437, 438; 437, 446; 438, call; 438, 439; 438, 445; 439, attribute; 439, 440; 439, 444; 440, subscript; 440, 441; 440, 442; 441, identifier:c_matches; 442, unary_operator:-; 442, 443; 443, integer:1; 444, identifier:end; 445, argument_list; 446, call; 446, 447; 446, 448; 447, identifier:len; 448, argument_list; 448, 449; 449, identifier:chunk; 450, for_statement; 450, 451; 450, 456; 450, 460; 451, pattern_list; 451, 452; 451, 453; 452, identifier:j; 453, tuple_pattern; 453, 454; 453, 455; 454, identifier:start; 455, identifier:end; 456, call; 456, 457; 456, 458; 457, identifier:enumerate; 458, argument_list; 458, 459; 459, identifier:c_splits; 460, block; 460, 461; 460, 475; 461, expression_statement; 461, 462; 462, assignment; 462, 463; 462, 464; 463, identifier:part; 464, call; 464, 465; 464, 473; 465, attribute; 465, 466; 465, 472; 466, subscript; 466, 467; 466, 468; 467, identifier:chunk; 468, slice; 468, 469; 468, 470; 468, 471; 469, identifier:start; 470, colon; 471, identifier:end; 472, identifier:strip; 473, argument_list; 473, 474; 474, string:"\n"; 475, expression_statement; 475, 476; 476, call; 476, 477; 476, 482; 477, attribute; 477, 478; 477, 481; 478, attribute; 478, 479; 478, 480; 479, identifier:ret; 480, identifier:meta; 481, identifier:append; 482, argument_list; 482, 483; 483, call; 483, 484; 483, 485; 484, identifier:_build_meta; 485, argument_list; 485, 486; 485, 487; 486, identifier:part; 487, identifier:title; 488, return_statement; 488, 489; 489, identifier:ret
def parse(text: str) -> Docstring: """ Parse the Google-style docstring into its components. :returns: parsed docstring """ ret = Docstring() if not text: return ret # Clean according to PEP-0257 text = inspect.cleandoc(text) # Find first title and split on its position match = _titles_re.search(text) if match: desc_chunk = text[: match.start()] meta_chunk = text[match.start() :] else: desc_chunk = text meta_chunk = "" # Break description into short and long parts parts = desc_chunk.split("\n", 1) ret.short_description = parts[0] or None if len(parts) > 1: long_desc_chunk = parts[1] or "" ret.blank_after_short_description = long_desc_chunk.startswith("\n") ret.blank_after_long_description = long_desc_chunk.endswith("\n\n") ret.long_description = long_desc_chunk.strip() or None # Split by sections determined by titles matches = list(_titles_re.finditer(meta_chunk)) if not matches: return ret splits = [] for j in range(len(matches) - 1): splits.append((matches[j].end(), matches[j + 1].start())) splits.append((matches[-1].end(), len(meta_chunk))) chunks = {} for j, (start, end) in enumerate(splits): title = matches[j].group(1) if title not in _valid: continue chunks[title] = meta_chunk[start:end].strip("\n") if not chunks: return ret # Add elements from each chunk for title, chunk in chunks.items(): # Determine indent indent_match = re.search(r"^\s+", chunk) if not indent_match: raise ParseError(f'Can\'t infer indent from "{chunk}"') indent = indent_match.group() # Check for returns/yeilds (only one element) if _sections[title] in ("returns", "yields"): part = inspect.cleandoc(chunk) ret.meta.append(_build_meta(part, title)) continue # Split based on lines which have exactly that indent _re = "^" + indent + r"(?=\S)" c_matches = list(re.finditer(_re, chunk, flags=re.M)) if not c_matches: raise ParseError(f'No specification for "{title}": "{chunk}"') c_splits = [] for j in range(len(c_matches) - 1): c_splits.append((c_matches[j].end(), c_matches[j + 1].start())) c_splits.append((c_matches[-1].end(), len(chunk))) for j, (start, end) in enumerate(c_splits): part = chunk[start:end].strip("\n") ret.meta.append(_build_meta(part, title)) return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:generate; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:tool; 6, default_parameter; 6, 7; 6, 8; 7, identifier:copied; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:copy; 11, False; 12, block; 12, 13; 12, 15; 12, 16; 12, 20; 12, 41; 12, 45; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:tools; 19, list:[]; 20, if_statement; 20, 21; 20, 23; 20, 34; 21, not_operator; 21, 22; 22, identifier:tool; 23, block; 23, 24; 23, 31; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:logger; 28, identifier:info; 29, argument_list; 29, 30; 30, string:"Workspace supports one tool for all projects within."; 31, return_statement; 31, 32; 32, unary_operator:-; 32, 33; 33, integer:1; 34, else_clause; 34, 35; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:tools; 39, list:[tool]; 39, 40; 40, identifier:tool; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:result; 44, integer:0; 45, for_statement; 45, 46; 45, 47; 45, 48; 46, identifier:export_tool; 47, identifier:tools; 48, block; 48, 49; 48, 60; 48, 71; 48, 75; 48, 129; 48, 130; 48, 156; 48, 181; 48, 276; 48, 277; 48, 293; 48, 301; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:tool_export; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, call; 54, 55; 54, 56; 55, identifier:ToolsSupported; 56, argument_list; 57, identifier:get_tool; 58, argument_list; 58, 59; 59, identifier:export_tool; 60, if_statement; 60, 61; 60, 64; 61, comparison_operator:is; 61, 62; 61, 63; 62, identifier:tool_export; 63, None; 64, block; 64, 65; 64, 70; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:result; 68, unary_operator:-; 68, 69; 69, integer:1; 70, continue_statement; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:project_export_dir_overwrite; 74, False; 75, if_statement; 75, 76; 75, 87; 75, 96; 76, comparison_operator:!=; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:settings; 81, identifier:export_location_format; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:settings; 86, identifier:DEFAULT_EXPORT_LOCATION_FORMAT; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:location_format; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:settings; 95, identifier:export_location_format; 96, else_clause; 96, 97; 97, block; 97, 98; 98, if_statement; 98, 99; 98, 104; 98, 119; 99, comparison_operator:in; 99, 100; 99, 101; 100, string:'export_dir'; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:workspace_settings; 104, block; 104, 105; 104, 115; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:location_format; 108, subscript; 108, 109; 108, 114; 109, subscript; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:workspace_settings; 113, string:'export_dir'; 114, integer:0; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:project_export_dir_overwrite; 118, True; 119, else_clause; 119, 120; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:location_format; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:settings; 128, identifier:export_location_format; 129, comment; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:location; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, call; 135, 136; 135, 137; 136, identifier:PartialFormatter; 137, argument_list; 138, identifier:format; 139, argument_list; 139, 140; 139, 141; 140, identifier:location_format; 141, dictionary_splat; 141, 142; 142, dictionary; 142, 143; 142, 148; 142, 151; 143, pair; 143, 144; 143, 145; 144, string:'project_name'; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:name; 148, pair; 148, 149; 148, 150; 149, string:'tool'; 150, identifier:tool; 151, pair; 151, 152; 151, 153; 152, string:'workspace_name'; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:name; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:workspace_dic; 159, dictionary; 159, 160; 159, 163; 160, pair; 160, 161; 160, 162; 161, string:'projects'; 162, list:[]; 163, pair; 163, 164; 163, 165; 164, string:'settings'; 165, dictionary; 165, 166; 165, 171; 166, pair; 166, 167; 166, 168; 167, string:'name'; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:name; 171, pair; 171, 172; 171, 173; 172, string:'path'; 173, call; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:os; 177, identifier:path; 178, identifier:normpath; 179, argument_list; 179, 180; 180, identifier:location; 181, for_statement; 181, 182; 181, 183; 181, 186; 182, identifier:project; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:projects; 186, block; 186, 187; 186, 197; 186, 210; 186, 211; 186, 212; 186, 220; 186, 229; 186, 239; 186, 257; 186, 258; 186, 267; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:generated_files; 190, dictionary; 190, 191; 190, 194; 191, pair; 191, 192; 191, 193; 192, string:'projects'; 193, list:[]; 194, pair; 194, 195; 194, 196; 195, string:'workspaces'; 196, list:[]; 197, if_statement; 197, 198; 197, 199; 198, identifier:project_export_dir_overwrite; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 209; 202, subscript; 202, 203; 202, 208; 203, subscript; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:project; 206, identifier:project; 207, string:'common'; 208, string:'export_dir'; 209, identifier:location; 210, comment; 211, comment; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:project; 216, identifier:_fill_export_dict; 217, argument_list; 217, 218; 217, 219; 218, identifier:export_tool; 219, identifier:copied; 220, if_statement; 220, 221; 220, 222; 221, identifier:copy; 222, block; 222, 223; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:project; 227, identifier:_copy_sources_to_generated_destination; 228, argument_list; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 238; 231, subscript; 231, 232; 231, 237; 232, subscript; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:project; 235, identifier:project; 236, string:'export'; 237, string:'singular'; 238, False; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:files; 242, call; 242, 243; 242, 256; 243, attribute; 243, 244; 243, 255; 244, call; 244, 245; 244, 246; 245, identifier:tool_export; 246, argument_list; 246, 247; 246, 252; 247, subscript; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:project; 250, identifier:project; 251, string:'export'; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:settings; 255, identifier:export_project; 256, argument_list; 257, comment; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 265; 260, attribute; 260, 261; 260, 264; 261, subscript; 261, 262; 261, 263; 262, identifier:workspace_dic; 263, string:'projects'; 264, identifier:append; 265, argument_list; 265, 266; 266, identifier:files; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 274; 269, attribute; 269, 270; 269, 273; 270, subscript; 270, 271; 270, 272; 271, identifier:generated_files; 272, string:'projects'; 273, identifier:append; 274, argument_list; 274, 275; 275, identifier:files; 276, comment; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 282; 279, subscript; 279, 280; 279, 281; 280, identifier:generated_files; 281, string:'workspaces'; 282, call; 282, 283; 282, 292; 283, attribute; 283, 284; 283, 291; 284, call; 284, 285; 284, 286; 285, identifier:tool_export; 286, argument_list; 286, 287; 286, 288; 287, identifier:workspace_dic; 288, attribute; 288, 289; 288, 290; 289, identifier:self; 290, identifier:settings; 291, identifier:export_workspace; 292, argument_list; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 300; 295, subscript; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:generated_files; 299, identifier:export_tool; 300, identifier:generated_files; 301, return_statement; 301, 302; 302, identifier:result
def generate(self, tool, copied=False, copy=False): """ Generates a workspace """ # copied - already done by external script, copy - do actual copy tools = [] if not tool: logger.info("Workspace supports one tool for all projects within.") return -1 else: tools = [tool] result = 0 for export_tool in tools: tool_export = ToolsSupported().get_tool(export_tool) if tool_export is None: result = -1 continue project_export_dir_overwrite = False if self.settings.export_location_format != self.settings.DEFAULT_EXPORT_LOCATION_FORMAT: location_format = self.settings.export_location_format else: if 'export_dir' in self.workspace_settings: location_format = self.workspace_settings['export_dir'][0] project_export_dir_overwrite = True else: location_format = self.settings.export_location_format # substitute all of the different dynamic values location = PartialFormatter().format(location_format, **{ 'project_name': self.name, 'tool': tool, 'workspace_name': self.name }) workspace_dic = { 'projects': [], 'settings': { 'name': self.name, 'path': os.path.normpath(location), }, } for project in self.projects: generated_files = { 'projects' : [], 'workspaces': [], } if project_export_dir_overwrite: project.project['common']['export_dir'] = location # Merge all dics, copy sources if required, correct output dir. This happens here # because we need tool to set proper path (tool might be used as string template) project._fill_export_dict(export_tool, copied) if copy: project._copy_sources_to_generated_destination() project.project['export']['singular'] = False files = tool_export(project.project['export'], self.settings).export_project() # we gather all generated files, needed for workspace files workspace_dic['projects'].append(files) generated_files['projects'].append(files) # all projects are genereated, now generate workspace files generated_files['workspaces'] = tool_export(workspace_dic, self.settings).export_workspace() self.generated_files[export_tool] = generated_files return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_send_request; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:request; 6, block; 6, 7; 6, 9; 6, 18; 6, 33; 6, 42; 6, 48; 6, 72; 6, 81; 6, 111; 6, 118; 6, 155; 6, 161; 6, 192; 6, 246; 6, 254; 6, 262; 6, 268; 6, 304; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:headers; 12, dictionary; 12, 13; 13, pair; 13, 14; 13, 15; 14, string:"X-Experience-API-Version"; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:version; 18, if_statement; 18, 19; 18, 24; 19, comparison_operator:is; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:auth; 23, None; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 30; 27, subscript; 27, 28; 27, 29; 28, identifier:headers; 29, string:"Authorization"; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:auth; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:headers; 37, identifier:update; 38, argument_list; 38, 39; 39, attribute; 39, 40; 39, 41; 40, identifier:request; 41, identifier:headers; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:params; 45, attribute; 45, 46; 45, 47; 46, identifier:request; 47, identifier:query_params; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:params; 51, dictionary_comprehension; 51, 52; 51, 65; 52, pair; 52, 53; 52, 54; 53, identifier:k; 54, call; 54, 55; 54, 63; 55, attribute; 55, 56; 55, 62; 56, call; 56, 57; 56, 58; 57, identifier:unicode; 58, argument_list; 58, 59; 59, subscript; 59, 60; 59, 61; 60, identifier:params; 61, identifier:k; 62, identifier:encode; 63, argument_list; 63, 64; 64, string:'utf-8'; 65, for_in_clause; 65, 66; 65, 67; 66, identifier:k; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:params; 70, identifier:keys; 71, argument_list; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:params; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:urllib; 78, identifier:urlencode; 79, argument_list; 79, 80; 80, identifier:params; 81, if_statement; 81, 82; 81, 90; 81, 97; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:request; 86, identifier:resource; 87, identifier:startswith; 88, argument_list; 88, 89; 89, string:'http'; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:url; 94, attribute; 94, 95; 94, 96; 95, identifier:request; 96, identifier:resource; 97, else_clause; 97, 98; 98, block; 98, 99; 98, 105; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:url; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:endpoint; 105, expression_statement; 105, 106; 106, augmented_assignment:+=; 106, 107; 106, 108; 107, identifier:url; 108, attribute; 108, 109; 108, 110; 109, identifier:request; 110, identifier:resource; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:parsed; 114, call; 114, 115; 114, 116; 115, identifier:urlparse; 116, argument_list; 116, 117; 117, identifier:url; 118, if_statement; 118, 119; 118, 124; 118, 139; 119, comparison_operator:==; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:parsed; 122, identifier:scheme; 123, string:"https"; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:web_req; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:httplib; 131, identifier:HTTPSConnection; 132, argument_list; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:parsed; 135, identifier:hostname; 136, attribute; 136, 137; 136, 138; 137, identifier:parsed; 138, identifier:port; 139, else_clause; 139, 140; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:web_req; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:httplib; 147, identifier:HTTPConnection; 148, argument_list; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:parsed; 151, identifier:hostname; 152, attribute; 152, 153; 152, 154; 153, identifier:parsed; 154, identifier:port; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:path; 158, attribute; 158, 159; 158, 160; 159, identifier:parsed; 160, identifier:path; 161, if_statement; 161, 162; 161, 169; 162, boolean_operator:or; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:parsed; 165, identifier:query; 166, attribute; 166, 167; 166, 168; 167, identifier:parsed; 168, identifier:path; 169, block; 169, 170; 169, 174; 169, 185; 170, expression_statement; 170, 171; 171, augmented_assignment:+=; 171, 172; 171, 173; 172, identifier:path; 173, string:"?"; 174, if_statement; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:parsed; 177, identifier:query; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, augmented_assignment:+=; 180, 181; 180, 182; 181, identifier:path; 182, attribute; 182, 183; 182, 184; 183, identifier:parsed; 184, identifier:query; 185, if_statement; 185, 186; 185, 187; 186, identifier:params; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, augmented_assignment:+=; 189, 190; 189, 191; 190, identifier:path; 191, identifier:params; 192, if_statement; 192, 193; 192, 204; 192, 227; 193, boolean_operator:and; 193, 194; 193, 199; 194, call; 194, 195; 194, 196; 195, identifier:hasattr; 196, argument_list; 196, 197; 196, 198; 197, identifier:request; 198, string:"content"; 199, comparison_operator:is; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:request; 202, identifier:content; 203, None; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:web_req; 209, identifier:request; 210, argument_list; 210, 211; 210, 216; 210, 219; 210, 224; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:method; 213, attribute; 213, 214; 213, 215; 214, identifier:request; 215, identifier:method; 216, keyword_argument; 216, 217; 216, 218; 217, identifier:url; 218, identifier:path; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:body; 221, attribute; 221, 222; 221, 223; 222, identifier:request; 223, identifier:content; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:headers; 226, identifier:headers; 227, else_clause; 227, 228; 228, block; 228, 229; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:web_req; 233, identifier:request; 234, argument_list; 234, 235; 234, 240; 234, 243; 235, keyword_argument; 235, 236; 235, 237; 236, identifier:method; 237, attribute; 237, 238; 237, 239; 238, identifier:request; 239, identifier:method; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:url; 242, identifier:path; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:headers; 245, identifier:headers; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:response; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:web_req; 252, identifier:getresponse; 253, argument_list; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:data; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:response; 260, identifier:read; 261, argument_list; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:web_req; 266, identifier:close; 267, argument_list; 268, if_statement; 268, 269; 268, 293; 268, 298; 269, parenthesized_expression; 269, 270; 270, boolean_operator:or; 270, 271; 270, 277; 271, comparison_operator:<=; 271, 272; 271, 273; 271, 276; 272, integer:200; 273, attribute; 273, 274; 273, 275; 274, identifier:response; 275, identifier:status; 276, integer:300; 277, parenthesized_expression; 277, 278; 278, boolean_operator:and; 278, 279; 278, 290; 279, boolean_operator:and; 279, 280; 279, 285; 280, comparison_operator:==; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:response; 283, identifier:status; 284, integer:404; 285, call; 285, 286; 285, 287; 286, identifier:hasattr; 287, argument_list; 287, 288; 287, 289; 288, identifier:request; 289, string:"ignore404"; 290, attribute; 290, 291; 290, 292; 291, identifier:request; 292, identifier:ignore404; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:success; 297, True; 298, else_clause; 298, 299; 299, block; 299, 300; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:success; 303, False; 304, return_statement; 304, 305; 305, call; 305, 306; 305, 307; 306, identifier:LRSResponse; 307, argument_list; 307, 308; 307, 311; 307, 314; 307, 317; 308, keyword_argument; 308, 309; 308, 310; 309, identifier:success; 310, identifier:success; 311, keyword_argument; 311, 312; 311, 313; 312, identifier:request; 313, identifier:request; 314, keyword_argument; 314, 315; 314, 316; 315, identifier:response; 316, identifier:response; 317, keyword_argument; 317, 318; 317, 319; 318, identifier:data; 319, identifier:data
def _send_request(self, request): """Establishes connection and returns http response based off of request. :param request: HTTPRequest object :type request: :class:`tincan.http_request.HTTPRequest` :returns: LRS Response object :rtype: :class:`tincan.lrs_response.LRSResponse` """ headers = {"X-Experience-API-Version": self.version} if self.auth is not None: headers["Authorization"] = self.auth headers.update(request.headers) params = request.query_params params = {k: unicode(params[k]).encode('utf-8') for k in params.keys()} params = urllib.urlencode(params) if request.resource.startswith('http'): url = request.resource else: url = self.endpoint url += request.resource parsed = urlparse(url) if parsed.scheme == "https": web_req = httplib.HTTPSConnection(parsed.hostname, parsed.port) else: web_req = httplib.HTTPConnection(parsed.hostname, parsed.port) path = parsed.path if parsed.query or parsed.path: path += "?" if parsed.query: path += parsed.query if params: path += params if hasattr(request, "content") and request.content is not None: web_req.request( method=request.method, url=path, body=request.content, headers=headers, ) else: web_req.request( method=request.method, url=path, headers=headers, ) response = web_req.getresponse() data = response.read() web_req.close() if (200 <= response.status < 300 or (response.status == 404 and hasattr(request, "ignore404") and request.ignore404)): success = True else: success = False return LRSResponse( success=success, request=request, response=response, data=data, )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:query_statements; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:query; 6, block; 6, 7; 6, 9; 6, 13; 6, 26; 6, 87; 6, 99; 6, 105; 6, 114; 6, 132; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:params; 12, dictionary; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:param_keys; 16, list:[ "registration", "since", "until", "limit", "ascending", "related_activities", "related_agents", "format", "attachments", ]; 16, 17; 16, 18; 16, 19; 16, 20; 16, 21; 16, 22; 16, 23; 16, 24; 16, 25; 17, string:"registration"; 18, string:"since"; 19, string:"until"; 20, string:"limit"; 21, string:"ascending"; 22, string:"related_activities"; 23, string:"related_agents"; 24, string:"format"; 25, string:"attachments"; 26, for_statement; 26, 27; 26, 30; 26, 35; 27, pattern_list; 27, 28; 27, 29; 28, identifier:k; 29, identifier:v; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:query; 33, identifier:iteritems; 34, argument_list; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:v; 39, None; 40, block; 40, 41; 41, if_statement; 41, 42; 41, 45; 41, 59; 41, 76; 42, comparison_operator:==; 42, 43; 42, 44; 43, identifier:k; 44, string:"agent"; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:params; 50, identifier:k; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:v; 54, identifier:to_json; 55, argument_list; 55, 56; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:version; 59, elif_clause; 59, 60; 59, 67; 60, boolean_operator:or; 60, 61; 60, 64; 61, comparison_operator:==; 61, 62; 61, 63; 62, identifier:k; 63, string:"verb"; 64, comparison_operator:==; 64, 65; 64, 66; 65, identifier:k; 66, string:"activity"; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:params; 72, identifier:k; 73, attribute; 73, 74; 73, 75; 74, identifier:v; 75, identifier:id; 76, elif_clause; 76, 77; 76, 80; 77, comparison_operator:in; 77, 78; 77, 79; 78, identifier:k; 79, identifier:param_keys; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:params; 85, identifier:k; 86, identifier:v; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:request; 90, call; 90, 91; 90, 92; 91, identifier:HTTPRequest; 92, argument_list; 92, 93; 92, 96; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:method; 95, string:"GET"; 96, keyword_argument; 96, 97; 96, 98; 97, identifier:resource; 98, string:"statements"; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:request; 103, identifier:query_params; 104, identifier:params; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:lrs_response; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_send_request; 112, argument_list; 112, 113; 113, identifier:request; 114, if_statement; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:lrs_response; 117, identifier:success; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:lrs_response; 123, identifier:content; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:StatementsResult; 127, identifier:from_json; 128, argument_list; 128, 129; 129, attribute; 129, 130; 129, 131; 130, identifier:lrs_response; 131, identifier:data; 132, return_statement; 132, 133; 133, identifier:lrs_response
def query_statements(self, query): """Query the LRS for statements with specified parameters :param query: Dictionary of query parameters and their values :type query: dict :return: LRS Response object with the returned StatementsResult object as content :rtype: :class:`tincan.lrs_response.LRSResponse` .. note:: Optional query parameters are\n **statementId:** (*str*) ID of the Statement to fetch **voidedStatementId:** (*str*) ID of the voided Statement to fetch **agent:** (*Agent* |*Group*) Filter to return Statements for which the specified Agent or Group is the Actor **verb:** (*Verb id IRI*) Filter to return Statements matching the verb id **activity:** (*Activity id IRI*) Filter to return Statements for which the specified Activity is the Object **registration:** (*UUID*) Filter to return Statements matching the specified registration ID **related_activities:** (*bool*) Include Statements for which the Object, Context Activities or any Sub-Statement properties match the specified Activity **related_agents:** (*bool*) Include Statements for which the Actor, Object, Authority, Instructor, Team, or any Sub-Statement properties match the specified Agent **since:** (*datetime*) Filter to return Statements stored since the specified datetime **until:** (*datetime*) Filter to return Statements stored at or before the specified datetime **limit:** (*positive int*) Allow <limit> Statements to be returned. 0 indicates the maximum supported by the LRS **format:** (*str* {"ids"|"exact"|"canonical"}) Manipulates how the LRS handles importing and returning the statements **attachments:** (*bool*) If true, the LRS will use multipart responses and include all attachment data per Statement returned. Otherwise, application/json is used and no attachment information will be returned **ascending:** (*bool*) If true, the LRS will return results in ascending order of stored time (oldest first) """ params = {} param_keys = [ "registration", "since", "until", "limit", "ascending", "related_activities", "related_agents", "format", "attachments", ] for k, v in query.iteritems(): if v is not None: if k == "agent": params[k] = v.to_json(self.version) elif k == "verb" or k == "activity": params[k] = v.id elif k in param_keys: params[k] = v request = HTTPRequest( method="GET", resource="statements" ) request.query_params = params lrs_response = self._send_request(request) if lrs_response.success: lrs_response.content = StatementsResult.from_json(lrs_response.data) return lrs_response
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:retrieve_state; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:activity; 6, identifier:agent; 7, identifier:state_id; 8, default_parameter; 8, 9; 8, 10; 9, identifier:registration; 10, None; 11, block; 11, 12; 11, 14; 11, 29; 11, 44; 11, 59; 11, 83; 11, 96; 11, 105; 11, 214; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 21; 15, not_operator; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:isinstance; 18, argument_list; 18, 19; 18, 20; 19, identifier:activity; 20, identifier:Activity; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:activity; 25, call; 25, 26; 25, 27; 26, identifier:Activity; 27, argument_list; 27, 28; 28, identifier:activity; 29, if_statement; 29, 30; 29, 36; 30, not_operator; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:isinstance; 33, argument_list; 33, 34; 33, 35; 34, identifier:agent; 35, identifier:Agent; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:agent; 40, call; 40, 41; 40, 42; 41, identifier:Agent; 42, argument_list; 42, 43; 43, identifier:agent; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:request; 47, call; 47, 48; 47, 49; 48, identifier:HTTPRequest; 49, argument_list; 49, 50; 49, 53; 49, 56; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:method; 52, string:"GET"; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:resource; 55, string:"activities/state"; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:ignore404; 58, True; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:request; 63, identifier:query_params; 64, dictionary; 64, 65; 64, 70; 64, 80; 65, pair; 65, 66; 65, 67; 66, string:"activityId"; 67, attribute; 67, 68; 67, 69; 68, identifier:activity; 69, identifier:id; 70, pair; 70, 71; 70, 72; 71, string:"agent"; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:agent; 75, identifier:to_json; 76, argument_list; 76, 77; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:version; 80, pair; 80, 81; 80, 82; 81, string:"stateId"; 82, identifier:state_id; 83, if_statement; 83, 84; 83, 87; 84, comparison_operator:is; 84, 85; 84, 86; 85, identifier:registration; 86, None; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 95; 90, subscript; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:request; 93, identifier:query_params; 94, string:"registration"; 95, identifier:registration; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:lrs_response; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:_send_request; 103, argument_list; 103, 104; 104, identifier:request; 105, if_statement; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:lrs_response; 108, identifier:success; 109, block; 109, 110; 109, 130; 109, 141; 109, 151; 109, 170; 109, 189; 109, 208; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:doc; 113, call; 113, 114; 113, 115; 114, identifier:StateDocument; 115, argument_list; 115, 116; 115, 119; 115, 124; 115, 127; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:id; 118, identifier:state_id; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:content; 121, attribute; 121, 122; 121, 123; 122, identifier:lrs_response; 123, identifier:data; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:activity; 126, identifier:activity; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:agent; 129, identifier:agent; 130, if_statement; 130, 131; 130, 134; 131, comparison_operator:is; 131, 132; 131, 133; 132, identifier:registration; 133, None; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:doc; 139, identifier:registration; 140, identifier:registration; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:headers; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:lrs_response; 148, identifier:response; 149, identifier:getheaders; 150, argument_list; 151, if_statement; 151, 152; 151, 161; 152, boolean_operator:and; 152, 153; 152, 156; 153, comparison_operator:in; 153, 154; 153, 155; 154, string:"lastModified"; 155, identifier:headers; 156, comparison_operator:is; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:headers; 159, string:"lastModified"; 160, None; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:doc; 166, identifier:timestamp; 167, subscript; 167, 168; 167, 169; 168, identifier:headers; 169, string:"lastModified"; 170, if_statement; 170, 171; 170, 180; 171, boolean_operator:and; 171, 172; 171, 175; 172, comparison_operator:in; 172, 173; 172, 174; 173, string:"contentType"; 174, identifier:headers; 175, comparison_operator:is; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:headers; 178, string:"contentType"; 179, None; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:doc; 185, identifier:content_type; 186, subscript; 186, 187; 186, 188; 187, identifier:headers; 188, string:"contentType"; 189, if_statement; 189, 190; 189, 199; 190, boolean_operator:and; 190, 191; 190, 194; 191, comparison_operator:in; 191, 192; 191, 193; 192, string:"etag"; 193, identifier:headers; 194, comparison_operator:is; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:headers; 197, string:"etag"; 198, None; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:doc; 204, identifier:etag; 205, subscript; 205, 206; 205, 207; 206, identifier:headers; 207, string:"etag"; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:lrs_response; 212, identifier:content; 213, identifier:doc; 214, return_statement; 214, 215; 215, identifier:lrs_response
def retrieve_state(self, activity, agent, state_id, registration=None): """Retrieve state from LRS with the provided parameters :param activity: Activity object of desired state :type activity: :class:`tincan.activity.Activity` :param agent: Agent object of desired state :type agent: :class:`tincan.agent.Agent` :param state_id: UUID of desired state :type state_id: str | unicode :param registration: registration UUID of desired state :type registration: str | unicode :return: LRS Response object with retrieved state document as content :rtype: :class:`tincan.lrs_response.LRSResponse` """ if not isinstance(activity, Activity): activity = Activity(activity) if not isinstance(agent, Agent): agent = Agent(agent) request = HTTPRequest( method="GET", resource="activities/state", ignore404=True ) request.query_params = { "activityId": activity.id, "agent": agent.to_json(self.version), "stateId": state_id } if registration is not None: request.query_params["registration"] = registration lrs_response = self._send_request(request) if lrs_response.success: doc = StateDocument( id=state_id, content=lrs_response.data, activity=activity, agent=agent ) if registration is not None: doc.registration = registration headers = lrs_response.response.getheaders() if "lastModified" in headers and headers["lastModified"] is not None: doc.timestamp = headers["lastModified"] if "contentType" in headers and headers["contentType"] is not None: doc.content_type = headers["contentType"] if "etag" in headers and headers["etag"] is not None: doc.etag = headers["etag"] lrs_response.content = doc return lrs_response
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:as_version; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:version; 7, attribute; 7, 8; 7, 9; 8, identifier:Version; 9, identifier:latest; 10, block; 10, 11; 10, 13; 10, 237; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 20; 13, 199; 14, not_operator; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:isinstance; 17, argument_list; 17, 18; 17, 19; 18, identifier:self; 19, identifier:list; 20, block; 20, 21; 20, 25; 20, 190; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:result; 24, dictionary; 25, for_statement; 25, 26; 25, 29; 25, 48; 26, pattern_list; 26, 27; 26, 28; 27, identifier:k; 28, identifier:v; 29, conditional_expression:if; 29, 30; 29, 35; 29, 40; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:iteritems; 34, argument_list; 35, call; 35, 36; 35, 37; 36, identifier:isinstance; 37, argument_list; 37, 38; 37, 39; 38, identifier:self; 39, identifier:dict; 40, call; 40, 41; 40, 47; 41, attribute; 41, 42; 41, 46; 42, call; 42, 43; 42, 44; 43, identifier:vars; 44, argument_list; 44, 45; 45, identifier:self; 46, identifier:iteritems; 47, argument_list; 48, block; 48, 49; 48, 61; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:k; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_props_corrected; 57, identifier:get; 58, argument_list; 58, 59; 58, 60; 59, identifier:k; 60, identifier:k; 61, if_statement; 61, 62; 61, 67; 61, 79; 61, 128; 61, 146; 61, 164; 61, 182; 62, call; 62, 63; 62, 64; 63, identifier:isinstance; 64, argument_list; 64, 65; 64, 66; 65, identifier:v; 66, identifier:SerializableBase; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:result; 72, identifier:k; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:v; 76, identifier:as_version; 77, argument_list; 77, 78; 78, identifier:version; 79, elif_clause; 79, 80; 79, 85; 80, call; 80, 81; 80, 82; 81, identifier:isinstance; 82, argument_list; 82, 83; 82, 84; 83, identifier:v; 84, identifier:list; 85, block; 85, 86; 85, 92; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, subscript; 88, 89; 88, 90; 89, identifier:result; 90, identifier:k; 91, list:[]; 92, for_statement; 92, 93; 92, 94; 92, 95; 93, identifier:val; 94, identifier:v; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 102; 96, 117; 97, call; 97, 98; 97, 99; 98, identifier:isinstance; 99, argument_list; 99, 100; 99, 101; 100, identifier:val; 101, identifier:SerializableBase; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 110; 105, attribute; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:result; 108, identifier:k; 109, identifier:append; 110, argument_list; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:val; 114, identifier:as_version; 115, argument_list; 115, 116; 116, identifier:version; 117, else_clause; 117, 118; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:result; 124, identifier:k; 125, identifier:append; 126, argument_list; 126, 127; 127, identifier:val; 128, elif_clause; 128, 129; 128, 136; 129, call; 129, 130; 129, 131; 130, identifier:isinstance; 131, argument_list; 131, 132; 131, 133; 132, identifier:v; 133, attribute; 133, 134; 133, 135; 134, identifier:uuid; 135, identifier:UUID; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:result; 141, identifier:k; 142, call; 142, 143; 142, 144; 143, identifier:unicode; 144, argument_list; 144, 145; 145, identifier:v; 146, elif_clause; 146, 147; 146, 154; 147, call; 147, 148; 147, 149; 148, identifier:isinstance; 149, argument_list; 149, 150; 149, 151; 150, identifier:v; 151, attribute; 151, 152; 151, 153; 152, identifier:datetime; 153, identifier:timedelta; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:result; 159, identifier:k; 160, call; 160, 161; 160, 162; 161, identifier:jsonify_timedelta; 162, argument_list; 162, 163; 163, identifier:v; 164, elif_clause; 164, 165; 164, 172; 165, call; 165, 166; 165, 167; 166, identifier:isinstance; 167, argument_list; 167, 168; 167, 169; 168, identifier:v; 169, attribute; 169, 170; 169, 171; 170, identifier:datetime; 171, identifier:datetime; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:result; 177, identifier:k; 178, call; 178, 179; 178, 180; 179, identifier:jsonify_datetime; 180, argument_list; 180, 181; 181, identifier:v; 182, else_clause; 182, 183; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 189; 186, subscript; 186, 187; 186, 188; 187, identifier:result; 188, identifier:k; 189, identifier:v; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:result; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:_filter_none; 197, argument_list; 197, 198; 198, identifier:result; 199, else_clause; 199, 200; 200, block; 200, 201; 200, 205; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:result; 204, list:[]; 205, for_statement; 205, 206; 205, 207; 205, 208; 206, identifier:v; 207, identifier:self; 208, block; 208, 209; 209, if_statement; 209, 210; 209, 215; 209, 228; 210, call; 210, 211; 210, 212; 211, identifier:isinstance; 212, argument_list; 212, 213; 212, 214; 213, identifier:v; 214, identifier:SerializableBase; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:result; 220, identifier:append; 221, argument_list; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:v; 225, identifier:as_version; 226, argument_list; 226, 227; 227, identifier:version; 228, else_clause; 228, 229; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:result; 234, identifier:append; 235, argument_list; 235, 236; 236, identifier:v; 237, return_statement; 237, 238; 238, identifier:result
def as_version(self, version=Version.latest): """Returns a dict that has been modified based on versioning in order to be represented in JSON properly A class should overload as_version(self, version) implementation in order to tailor a more specific representation :param version: the relevant version. This allows for variance between versions :type version: str | unicode """ if not isinstance(self, list): result = {} for k, v in self.iteritems() if isinstance(self, dict) else vars(self).iteritems(): k = self._props_corrected.get(k, k) if isinstance(v, SerializableBase): result[k] = v.as_version(version) elif isinstance(v, list): result[k] = [] for val in v: if isinstance(val, SerializableBase): result[k].append(val.as_version(version)) else: result[k].append(val) elif isinstance(v, uuid.UUID): result[k] = unicode(v) elif isinstance(v, datetime.timedelta): result[k] = jsonify_timedelta(v) elif isinstance(v, datetime.datetime): result[k] = jsonify_datetime(v) else: result[k] = v result = self._filter_none(result) else: result = [] for v in self: if isinstance(v, SerializableBase): result.append(v.as_version(version)) else: result.append(v) return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:xrun; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 19; 4, identifier:command; 5, identifier:options; 6, default_parameter; 6, 7; 6, 8; 7, identifier:log; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:_log_container_as_started; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:logfile; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:timeout; 17, unary_operator:-; 17, 18; 18, integer:1; 19, default_parameter; 19, 20; 19, 21; 20, identifier:kill_callback; 21, None; 22, block; 22, 23; 22, 25; 22, 44; 22, 72; 22, 100; 22, 110; 22, 118; 22, 126; 22, 132; 23, expression_statement; 23, 24; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:cmd; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, string:" "; 31, identifier:join; 32, argument_list; 32, 33; 33, binary_operator:+; 33, 34; 33, 36; 34, list:[command]; 34, 35; 35, identifier:command; 36, call; 36, 37; 36, 38; 37, identifier:list; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:map; 41, argument_list; 41, 42; 41, 43; 42, identifier:str; 43, identifier:options; 44, function_definition; 44, 45; 44, 46; 44, 48; 45, function_name:_print_info; 46, parameters; 46, 47; 47, identifier:msg; 48, block; 48, 49; 48, 55; 49, if_statement; 49, 50; 49, 53; 50, comparison_operator:is; 50, 51; 50, 52; 51, identifier:msg; 52, None; 53, block; 53, 54; 54, return_statement; 55, if_statement; 55, 56; 55, 57; 55, 65; 56, identifier:log; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:log; 62, identifier:info; 63, argument_list; 63, 64; 64, identifier:msg; 65, else_clause; 65, 66; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:print; 70, argument_list; 70, 71; 71, identifier:msg; 72, function_definition; 72, 73; 72, 74; 72, 76; 73, function_name:_print_warn; 74, parameters; 74, 75; 75, identifier:msg; 76, block; 76, 77; 76, 83; 77, if_statement; 77, 78; 77, 81; 78, comparison_operator:is; 78, 79; 78, 80; 79, identifier:msg; 80, None; 81, block; 81, 82; 82, return_statement; 83, if_statement; 83, 84; 83, 85; 83, 93; 84, identifier:log; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:log; 90, identifier:warn; 91, argument_list; 91, 92; 92, identifier:msg; 93, else_clause; 93, 94; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 98; 97, identifier:print; 98, argument_list; 98, 99; 99, identifier:msg; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:_print_info; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, string:u"Running: {0:s}"; 107, identifier:format; 108, argument_list; 108, 109; 109, identifier:cmd; 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:sys; 115, identifier:stdout; 116, identifier:flush; 117, argument_list; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:starttime; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:time; 124, identifier:time; 125, argument_list; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:process; 129, assignment; 129, 130; 129, 131; 130, identifier:p; 131, None; 132, try_statement; 132, 133; 132, 363; 133, block; 133, 134; 133, 166; 133, 342; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:foutname; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:os; 141, identifier:path; 142, identifier:join; 143, argument_list; 143, 144; 143, 145; 144, string:"/tmp"; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, string:"stimela_output_{0:s}_{1:f}"; 148, identifier:format; 149, argument_list; 149, 150; 149, 165; 150, call; 150, 151; 150, 164; 151, attribute; 151, 152; 151, 163; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:hashlib; 155, identifier:md5; 156, argument_list; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:cmd; 160, identifier:encode; 161, argument_list; 161, 162; 162, string:'utf-8'; 163, identifier:hexdigest; 164, argument_list; 165, identifier:starttime; 166, with_statement; 166, 167; 166, 177; 167, with_clause; 167, 168; 168, with_item; 168, 169; 169, as_pattern; 169, 170; 169, 175; 170, call; 170, 171; 170, 172; 171, identifier:open; 172, argument_list; 172, 173; 172, 174; 173, identifier:foutname; 174, string:"w+"; 175, as_pattern_target; 175, 176; 176, identifier:fout; 177, block; 177, 178; 177, 198; 177, 276; 177, 294; 177, 335; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:p; 181, assignment; 181, 182; 181, 183; 182, identifier:process; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:subprocess; 186, identifier:Popen; 187, argument_list; 187, 188; 187, 189; 187, 192; 187, 195; 188, identifier:cmd; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:stderr; 191, identifier:fout; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:stdout; 194, identifier:fout; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:shell; 197, True; 198, function_definition; 198, 199; 198, 200; 198, 202; 199, function_name:clock_killer; 200, parameters; 200, 201; 201, identifier:p; 202, block; 202, 203; 203, while_statement; 203, 204; 203, 216; 204, boolean_operator:and; 204, 205; 204, 212; 205, comparison_operator:is; 205, 206; 205, 211; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:process; 209, identifier:poll; 210, argument_list; 211, None; 212, parenthesized_expression; 212, 213; 213, comparison_operator:>=; 213, 214; 213, 215; 214, identifier:timeout; 215, integer:0; 216, block; 216, 217; 216, 225; 216, 269; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:currenttime; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:time; 223, identifier:time; 224, argument_list; 225, if_statement; 225, 226; 225, 232; 225, 248; 226, parenthesized_expression; 226, 227; 227, comparison_operator:<; 227, 228; 227, 231; 228, binary_operator:-; 228, 229; 228, 230; 229, identifier:currenttime; 230, identifier:starttime; 231, identifier:timeout; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, boolean_operator:and; 234, 235; 234, 236; 235, identifier:DEBUG; 236, call; 236, 237; 236, 238; 237, identifier:_print_warn; 238, argument_list; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, string:u"Clock Reaper: has been running for {0:f}, must finish in {1:f}"; 242, identifier:format; 243, argument_list; 243, 244; 243, 247; 244, binary_operator:-; 244, 245; 244, 246; 245, identifier:currenttime; 246, identifier:starttime; 247, identifier:timeout; 248, else_clause; 248, 249; 249, block; 249, 250; 249, 260; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:_print_warn; 253, argument_list; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, string:u"Clock Reaper: Timeout reached for '{0:s}'... sending the KILL signal"; 257, identifier:format; 258, argument_list; 258, 259; 259, identifier:cmd; 260, expression_statement; 260, 261; 261, boolean_operator:and; 261, 262; 261, 266; 262, parenthesized_expression; 262, 263; 263, comparison_operator:is; 263, 264; 263, 265; 264, identifier:kill_callback; 265, None; 266, call; 266, 267; 266, 268; 267, identifier:kill_callback; 268, argument_list; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:time; 273, identifier:sleep; 274, argument_list; 274, 275; 275, identifier:INTERRUPT_TIME; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 293; 278, attribute; 278, 279; 278, 292; 279, call; 279, 280; 279, 281; 280, identifier:Thread; 281, argument_list; 281, 282; 281, 285; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:target; 284, identifier:clock_killer; 285, keyword_argument; 285, 286; 285, 287; 286, identifier:args; 287, call; 287, 288; 287, 289; 288, identifier:tuple; 289, argument_list; 289, 290; 290, list:[p]; 290, 291; 291, identifier:p; 292, identifier:start; 293, argument_list; 294, while_statement; 294, 295; 294, 303; 295, parenthesized_expression; 295, 296; 296, comparison_operator:is; 296, 297; 296, 302; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:process; 300, identifier:poll; 301, argument_list; 302, None; 303, block; 303, 304; 303, 312; 303, 326; 303, 333; 303, 334; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:currenttime; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:time; 310, identifier:time; 311, argument_list; 312, expression_statement; 312, 313; 313, boolean_operator:and; 313, 314; 313, 315; 314, identifier:DEBUG; 315, call; 315, 316; 315, 317; 316, identifier:_print_info; 317, argument_list; 317, 318; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, string:u"God mode on: has been running for {0:f}"; 321, identifier:format; 322, argument_list; 322, 323; 323, binary_operator:-; 323, 324; 323, 325; 324, identifier:currenttime; 325, identifier:starttime; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:time; 330, identifier:sleep; 331, argument_list; 331, 332; 332, identifier:INTERRUPT_TIME; 333, comment; 334, comment; 335, assert_statement; 335, 336; 335, 341; 336, call; 336, 337; 336, 338; 337, identifier:hasattr; 338, argument_list; 338, 339; 338, 340; 339, identifier:process; 340, string:"returncode"; 341, string:"No returncode after termination!"; 342, with_statement; 342, 343; 342, 353; 343, with_clause; 343, 344; 344, with_item; 344, 345; 345, as_pattern; 345, 346; 345, 351; 346, call; 346, 347; 346, 348; 347, identifier:open; 348, argument_list; 348, 349; 348, 350; 349, identifier:foutname; 350, string:"r"; 351, as_pattern_target; 351, 352; 352, identifier:fout; 353, block; 353, 354; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 357; 356, identifier:_print_info; 357, argument_list; 357, 358; 358, call; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:fout; 361, identifier:read; 362, argument_list; 363, finally_clause; 363, 364; 364, block; 364, 365; 365, if_statement; 365, 366; 365, 374; 366, boolean_operator:and; 366, 367; 366, 371; 367, parenthesized_expression; 367, 368; 368, comparison_operator:is; 368, 369; 368, 370; 369, identifier:process; 370, None; 371, attribute; 371, 372; 371, 373; 372, identifier:process; 373, identifier:returncode; 374, block; 374, 375; 375, raise_statement; 375, 376; 376, call; 376, 377; 376, 378; 377, identifier:StimelaCabRuntimeError; 378, argument_list; 378, 379; 379, binary_operator:%; 379, 380; 379, 381; 380, string:'%s: returns errr code %d'; 381, tuple; 381, 382; 381, 383; 382, identifier:command; 383, attribute; 383, 384; 383, 385; 384, identifier:process; 385, identifier:returncode
def xrun(command, options, log=None, _log_container_as_started=False, logfile=None, timeout=-1, kill_callback=None): """ Run something on command line. Example: _run("ls", ["-lrt", "../"]) """ cmd = " ".join([command] + list(map(str, options)) ) def _print_info(msg): if msg is None: return if log: log.info(msg) else: print(msg) def _print_warn(msg): if msg is None: return if log: log.warn(msg) else: print(msg) _print_info(u"Running: {0:s}".format(cmd)) sys.stdout.flush() starttime = time.time() process = p = None try: foutname = os.path.join("/tmp", "stimela_output_{0:s}_{1:f}".format(hashlib.md5(cmd.encode('utf-8')).hexdigest(), starttime)) with open(foutname, "w+") as fout: p = process = subprocess.Popen(cmd, stderr=fout, stdout=fout, shell=True) def clock_killer(p): while process.poll() is None and (timeout >= 0): currenttime = time.time() if (currenttime - starttime < timeout): DEBUG and _print_warn(u"Clock Reaper: has been running for {0:f}, must finish in {1:f}".format(currenttime - starttime, timeout)) else: _print_warn(u"Clock Reaper: Timeout reached for '{0:s}'... sending the KILL signal".format(cmd)) (kill_callback is not None) and kill_callback() time.sleep(INTERRUPT_TIME) Thread(target=clock_killer, args=tuple([p])).start() while (process.poll() is None): currenttime = time.time() DEBUG and _print_info(u"God mode on: has been running for {0:f}".format(currenttime - starttime)) time.sleep(INTERRUPT_TIME) # this is probably not ideal as it interrupts the process every few seconds, #check whether there is an alternative with a callback assert hasattr(process, "returncode"), "No returncode after termination!" with open(foutname, "r") as fout: _print_info(fout.read()) finally: if (process is not None) and process.returncode: raise StimelaCabRuntimeError('%s: returns errr code %d' % (command, process.returncode))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:to_items; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:func; 7, identifier:str; 8, block; 8, 9; 8, 11; 9, expression_statement; 9, 10; 10, comment; 11, return_statement; 11, 12; 12, list_comprehension; 12, 13; 12, 23; 13, tuple; 13, 14; 13, 15; 14, identifier:key; 15, call; 15, 16; 15, 17; 16, identifier:func; 17, argument_list; 17, 18; 18, subscript; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:kwargs; 22, identifier:key; 23, for_in_clause; 23, 24; 23, 25; 24, identifier:key; 25, call; 25, 26; 25, 27; 26, identifier:sorted; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:kwargs; 33, identifier:keys; 34, argument_list
def to_items(self, func=str): """ Contruct a list of dictionary items. The items are normalized using: - A sort function by key (for consistent results) - A transformation function for values The transformation function will default to `str`, which is a good choice when encoding values as part of a response; this requires that complex types (UUID, Enum, etc.) have a valid string encoding. The transformation function should be set to `identity` in cases where raw values are desired; this is normally necessary when passing page data to controller functions as kwargs. """ return [ (key, func(self.kwargs[key])) for key in sorted(self.kwargs.keys()) ]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_apply_sub_frames; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:documents; 6, identifier:subs; 7, block; 7, 8; 7, 10; 7, 11; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, for_statement; 11, 12; 11, 15; 11, 20; 11, 21; 12, pattern_list; 12, 13; 12, 14; 13, identifier:path; 14, identifier:projection; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:subs; 18, identifier:items; 19, argument_list; 20, comment; 21, block; 21, 22; 21, 26; 21, 30; 21, 65; 21, 66; 21, 70; 21, 246; 21, 247; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:sub; 25, None; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:expect_map; 29, False; 30, if_statement; 30, 31; 30, 34; 30, 44; 30, 62; 31, comparison_operator:in; 31, 32; 31, 33; 32, string:'$sub'; 33, identifier:projection; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:sub; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:projection; 41, identifier:pop; 42, argument_list; 42, 43; 43, string:'$sub'; 44, elif_clause; 44, 45; 44, 48; 45, comparison_operator:in; 45, 46; 45, 47; 46, string:'$sub.'; 47, identifier:projection; 48, block; 48, 49; 48, 58; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:sub; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:projection; 55, identifier:pop; 56, argument_list; 56, 57; 57, string:'$sub.'; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:expect_map; 61, True; 62, else_clause; 62, 63; 63, block; 63, 64; 64, continue_statement; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:raw_subs; 69, list:[]; 70, for_statement; 70, 71; 70, 72; 70, 73; 71, identifier:document; 72, identifier:documents; 73, block; 73, 74; 73, 84; 73, 90; 73, 209; 73, 213; 73, 222; 73, 237; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:value; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:cls; 80, identifier:_path_to_value; 81, argument_list; 81, 82; 81, 83; 82, identifier:path; 83, identifier:document; 84, if_statement; 84, 85; 84, 88; 85, comparison_operator:is; 85, 86; 85, 87; 86, identifier:value; 87, None; 88, block; 88, 89; 89, continue_statement; 90, if_statement; 90, 91; 90, 96; 90, 173; 90, 202; 91, call; 91, 92; 91, 93; 92, identifier:isinstance; 93, argument_list; 93, 94; 93, 95; 94, identifier:value; 95, identifier:dict; 96, block; 96, 97; 97, if_statement; 97, 98; 97, 99; 97, 100; 97, 156; 97, 157; 98, identifier:expect_map; 99, comment; 100, block; 100, 101; 100, 109; 101, expression_statement; 101, 102; 102, augmented_assignment:+=; 102, 103; 102, 104; 103, identifier:raw_subs; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:value; 107, identifier:values; 108, argument_list; 109, for_statement; 109, 110; 109, 113; 109, 118; 110, pattern_list; 110, 111; 110, 112; 111, identifier:k; 112, identifier:v; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:value; 116, identifier:items; 117, argument_list; 118, block; 118, 119; 119, if_statement; 119, 120; 119, 125; 119, 145; 120, call; 120, 121; 120, 122; 121, identifier:isinstance; 122, argument_list; 122, 123; 122, 124; 123, identifier:v; 124, identifier:list; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:value; 130, identifier:k; 131, list_comprehension; 131, 132; 131, 136; 131, 139; 132, call; 132, 133; 132, 134; 133, identifier:sub; 134, argument_list; 134, 135; 135, identifier:u; 136, for_in_clause; 136, 137; 136, 138; 137, identifier:u; 138, identifier:v; 139, if_clause; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:isinstance; 142, argument_list; 142, 143; 142, 144; 143, identifier:u; 144, identifier:dict; 145, else_clause; 145, 146; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:value; 151, identifier:k; 152, call; 152, 153; 152, 154; 153, identifier:sub; 154, argument_list; 154, 155; 155, identifier:v; 156, comment; 157, else_clause; 157, 158; 158, block; 158, 159; 158, 166; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:raw_subs; 163, identifier:append; 164, argument_list; 164, 165; 165, identifier:value; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:value; 169, call; 169, 170; 169, 171; 170, identifier:sub; 171, argument_list; 171, 172; 172, identifier:value; 173, elif_clause; 173, 174; 173, 179; 173, 180; 174, call; 174, 175; 174, 176; 175, identifier:isinstance; 176, argument_list; 176, 177; 176, 178; 177, identifier:value; 178, identifier:list; 179, comment; 180, block; 180, 181; 180, 185; 181, expression_statement; 181, 182; 182, augmented_assignment:+=; 182, 183; 182, 184; 183, identifier:raw_subs; 184, identifier:value; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:value; 188, list_comprehension; 188, 189; 188, 193; 188, 196; 189, call; 189, 190; 189, 191; 190, identifier:sub; 191, argument_list; 191, 192; 192, identifier:v; 193, for_in_clause; 193, 194; 193, 195; 194, identifier:v; 195, identifier:value; 196, if_clause; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:isinstance; 199, argument_list; 199, 200; 199, 201; 200, identifier:v; 201, identifier:dict; 202, else_clause; 202, 203; 203, block; 203, 204; 204, raise_statement; 204, 205; 205, call; 205, 206; 205, 207; 206, identifier:TypeError; 207, argument_list; 207, 208; 208, string:'Not a supported sub-frame type'; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:child_document; 212, identifier:document; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:keys; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:cls; 219, identifier:_path_to_keys; 220, argument_list; 220, 221; 221, identifier:path; 222, for_statement; 222, 223; 222, 224; 222, 230; 223, identifier:key; 224, subscript; 224, 225; 224, 226; 225, identifier:keys; 226, slice; 226, 227; 226, 228; 227, colon; 228, unary_operator:-; 228, 229; 229, integer:1; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:child_document; 234, subscript; 234, 235; 234, 236; 235, identifier:child_document; 236, identifier:key; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 245; 239, subscript; 239, 240; 239, 241; 240, identifier:child_document; 241, subscript; 241, 242; 241, 243; 242, identifier:keys; 243, unary_operator:-; 243, 244; 244, integer:1; 245, identifier:value; 246, comment; 247, if_statement; 247, 248; 247, 249; 248, identifier:projection; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:sub; 254, identifier:_apply_projection; 255, argument_list; 255, 256; 255, 257; 256, identifier:raw_subs; 257, identifier:projection
def _apply_sub_frames(cls, documents, subs): """Convert embedded documents to sub-frames for one or more documents""" # Dereference each reference for path, projection in subs.items(): # Get the SubFrame class we'll use to wrap the embedded document sub = None expect_map = False if '$sub' in projection: sub = projection.pop('$sub') elif '$sub.' in projection: sub = projection.pop('$sub.') expect_map = True else: continue # Add sub-frames to the documents raw_subs = [] for document in documents: value = cls._path_to_value(path, document) if value is None: continue if isinstance(value, dict): if expect_map: # Dictionary of embedded documents raw_subs += value.values() for k, v in value.items(): if isinstance(v ,list): value[k] = [ sub(u) for u in v if isinstance(u, dict)] else: value[k] = sub(v) # Single embedded document else: raw_subs.append(value) value = sub(value) elif isinstance(value, list): # List of embedded documents raw_subs += value value = [sub(v) for v in value if isinstance(v, dict)] else: raise TypeError('Not a supported sub-frame type') child_document = document keys = cls._path_to_keys(path) for key in keys[:-1]: child_document = child_document[key] child_document[keys[-1]] = value # Apply the projection to the list of sub frames if projection: sub._apply_projection(raw_subs, projection)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_dereference; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:documents; 6, identifier:references; 7, block; 7, 8; 7, 10; 7, 11; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, for_statement; 11, 12; 11, 15; 11, 20; 11, 21; 12, pattern_list; 12, 13; 12, 14; 13, identifier:path; 14, identifier:projection; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:references; 18, identifier:items; 19, argument_list; 20, comment; 21, block; 21, 22; 21, 28; 21, 29; 21, 35; 21, 95; 21, 96; 21, 105; 21, 126; 21, 138; 21, 139; 22, if_statement; 22, 23; 22, 26; 23, comparison_operator:not; 23, 24; 23, 25; 24, string:'$ref'; 25, identifier:projection; 26, block; 26, 27; 27, continue_statement; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:ids; 32, call; 32, 33; 32, 34; 33, identifier:set; 34, argument_list; 35, for_statement; 35, 36; 35, 37; 35, 38; 36, identifier:document; 37, identifier:documents; 38, block; 38, 39; 38, 49; 38, 54; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:value; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:cls; 45, identifier:_path_to_value; 46, argument_list; 46, 47; 46, 48; 47, identifier:path; 48, identifier:document; 49, if_statement; 49, 50; 49, 52; 50, not_operator; 50, 51; 51, identifier:value; 52, block; 52, 53; 53, continue_statement; 54, if_statement; 54, 55; 54, 60; 54, 68; 54, 86; 55, call; 55, 56; 55, 57; 56, identifier:isinstance; 57, argument_list; 57, 58; 57, 59; 58, identifier:value; 59, identifier:list; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:ids; 65, identifier:update; 66, argument_list; 66, 67; 67, identifier:value; 68, elif_clause; 68, 69; 68, 74; 69, call; 69, 70; 69, 71; 70, identifier:isinstance; 71, argument_list; 71, 72; 71, 73; 72, identifier:value; 73, identifier:dict; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:ids; 79, identifier:update; 80, argument_list; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:value; 84, identifier:values; 85, argument_list; 86, else_clause; 86, 87; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:ids; 92, identifier:add; 93, argument_list; 93, 94; 94, identifier:value; 95, comment; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:ref; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:projection; 102, identifier:pop; 103, argument_list; 103, 104; 104, string:'$ref'; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:frames; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:ref; 111, identifier:many; 112, argument_list; 112, 113; 112, 123; 113, dictionary; 113, 114; 114, pair; 114, 115; 114, 116; 115, string:'_id'; 116, dictionary; 116, 117; 117, pair; 117, 118; 117, 119; 118, string:'$in'; 119, call; 119, 120; 119, 121; 120, identifier:list; 121, argument_list; 121, 122; 122, identifier:ids; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:projection; 125, identifier:projection; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:frames; 129, dictionary_comprehension; 129, 130; 129, 135; 130, pair; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:f; 133, identifier:_id; 134, identifier:f; 135, for_in_clause; 135, 136; 135, 137; 136, identifier:f; 137, identifier:frames; 138, comment; 139, for_statement; 139, 140; 139, 141; 139, 142; 140, identifier:document; 141, identifier:documents; 142, block; 142, 143; 142, 153; 142, 158; 142, 221; 142, 225; 142, 234; 142, 249; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:value; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:cls; 149, identifier:_path_to_value; 150, argument_list; 150, 151; 150, 152; 151, identifier:path; 152, identifier:document; 153, if_statement; 153, 154; 153, 156; 154, not_operator; 154, 155; 155, identifier:value; 156, block; 156, 157; 157, continue_statement; 158, if_statement; 158, 159; 158, 164; 158, 165; 158, 180; 158, 209; 159, call; 159, 160; 159, 161; 160, identifier:isinstance; 161, argument_list; 161, 162; 161, 163; 162, identifier:value; 163, identifier:list; 164, comment; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:value; 169, list_comprehension; 169, 170; 169, 173; 169, 176; 170, subscript; 170, 171; 170, 172; 171, identifier:frames; 172, identifier:id; 173, for_in_clause; 173, 174; 173, 175; 174, identifier:id; 175, identifier:value; 176, if_clause; 176, 177; 177, comparison_operator:in; 177, 178; 177, 179; 178, identifier:id; 179, identifier:frames; 180, elif_clause; 180, 181; 180, 186; 180, 187; 181, call; 181, 182; 181, 183; 182, identifier:isinstance; 183, argument_list; 183, 184; 183, 185; 184, identifier:value; 185, identifier:dict; 186, comment; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:value; 191, dictionary_comprehension; 191, 192; 191, 200; 192, pair; 192, 193; 192, 194; 193, identifier:key; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:frames; 197, identifier:get; 198, argument_list; 198, 199; 199, identifier:id; 200, for_in_clause; 200, 201; 200, 204; 201, pattern_list; 201, 202; 201, 203; 202, identifier:key; 203, identifier:id; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:value; 207, identifier:items; 208, argument_list; 209, else_clause; 209, 210; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:value; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:frames; 217, identifier:get; 218, argument_list; 218, 219; 218, 220; 219, identifier:value; 220, None; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:child_document; 224, identifier:document; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:keys; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:cls; 231, identifier:_path_to_keys; 232, argument_list; 232, 233; 233, identifier:path; 234, for_statement; 234, 235; 234, 236; 234, 242; 235, identifier:key; 236, subscript; 236, 237; 236, 238; 237, identifier:keys; 238, slice; 238, 239; 238, 240; 239, colon; 240, unary_operator:-; 240, 241; 241, integer:1; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:child_document; 246, subscript; 246, 247; 246, 248; 247, identifier:child_document; 248, identifier:key; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 257; 251, subscript; 251, 252; 251, 253; 252, identifier:child_document; 253, subscript; 253, 254; 253, 255; 254, identifier:keys; 255, unary_operator:-; 255, 256; 256, integer:1; 257, identifier:value
def _dereference(cls, documents, references): """Dereference one or more documents""" # Dereference each reference for path, projection in references.items(): # Check there is a $ref in the projection, else skip it if '$ref' not in projection: continue # Collect Ids of documents to dereference ids = set() for document in documents: value = cls._path_to_value(path, document) if not value: continue if isinstance(value, list): ids.update(value) elif isinstance(value, dict): ids.update(value.values()) else: ids.add(value) # Find the referenced documents ref = projection.pop('$ref') frames = ref.many( {'_id': {'$in': list(ids)}}, projection=projection ) frames = {f._id: f for f in frames} # Add dereferenced frames to the document for document in documents: value = cls._path_to_value(path, document) if not value: continue if isinstance(value, list): # List of references value = [frames[id] for id in value if id in frames] elif isinstance(value, dict): # Dictionary of references value = {key: frames.get(id) for key, id in value.items()} else: value = frames.get(value, None) child_document = document keys = cls._path_to_keys(path) for key in keys[:-1]: child_document = child_document[key] child_document[keys[-1]] = value
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:p; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:i; 5, identifier:sample_size; 6, identifier:weights; 7, block; 7, 8; 7, 10; 7, 11; 7, 17; 7, 24; 7, 25; 7, 26; 7, 33; 7, 37; 7, 38; 7, 42; 7, 139; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:weight_i; 14, subscript; 14, 15; 14, 16; 15, identifier:weights; 16, identifier:i; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:weights_sum; 20, call; 20, 21; 20, 22; 21, identifier:sum; 22, argument_list; 22, 23; 23, identifier:weights; 24, comment; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:other_weights; 29, call; 29, 30; 29, 31; 30, identifier:list; 31, argument_list; 31, 32; 32, identifier:weights; 33, delete_statement; 33, 34; 34, subscript; 34, 35; 34, 36; 35, identifier:other_weights; 36, identifier:i; 37, comment; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:probability_of_i; 41, integer:0; 42, for_statement; 42, 43; 42, 44; 42, 49; 42, 50; 43, identifier:picks; 44, call; 44, 45; 44, 46; 45, identifier:range; 46, argument_list; 46, 47; 46, 48; 47, integer:0; 48, identifier:sample_size; 49, comment; 50, block; 50, 51; 50, 64; 50, 65; 50, 69; 50, 131; 50, 132; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:permutations; 54, call; 54, 55; 54, 56; 55, identifier:list; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:itertools; 60, identifier:permutations; 61, argument_list; 61, 62; 61, 63; 62, identifier:other_weights; 63, identifier:picks; 64, comment; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:permutation_probabilities; 68, list:[]; 69, for_statement; 69, 70; 69, 71; 69, 72; 69, 73; 70, identifier:permutation; 71, identifier:permutations; 72, comment; 73, block; 73, 74; 73, 78; 73, 82; 73, 101; 73, 102; 73, 109; 73, 110; 73, 124; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:pick_probabilities; 77, list:[]; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:pick_weight_sum; 81, identifier:weights_sum; 82, for_statement; 82, 83; 82, 84; 82, 85; 83, identifier:pick; 84, identifier:permutation; 85, block; 85, 86; 85, 95; 85, 96; 85, 97; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:pick_probabilities; 90, identifier:append; 91, argument_list; 91, 92; 92, binary_operator:/; 92, 93; 92, 94; 93, identifier:pick; 94, identifier:pick_weight_sum; 95, comment; 96, comment; 97, expression_statement; 97, 98; 98, augmented_assignment:-=; 98, 99; 98, 100; 99, identifier:pick_weight_sum; 100, identifier:pick; 101, comment; 102, expression_statement; 102, 103; 103, augmented_assignment:+=; 103, 104; 103, 105; 104, identifier:pick_probabilities; 105, list:[weight_i / pick_weight_sum]; 105, 106; 106, binary_operator:/; 106, 107; 106, 108; 107, identifier:weight_i; 108, identifier:pick_weight_sum; 109, comment; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:permutation_probability; 113, call; 113, 114; 113, 115; 114, identifier:reduce; 115, argument_list; 115, 116; 115, 123; 116, lambda; 116, 117; 116, 120; 117, lambda_parameters; 117, 118; 117, 119; 118, identifier:x; 119, identifier:y; 120, binary_operator:*; 120, 121; 120, 122; 121, identifier:x; 122, identifier:y; 123, identifier:pick_probabilities; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:permutation_probabilities; 128, identifier:append; 129, argument_list; 129, 130; 130, identifier:permutation_probability; 131, comment; 132, expression_statement; 132, 133; 133, augmented_assignment:+=; 133, 134; 133, 135; 134, identifier:probability_of_i; 135, call; 135, 136; 135, 137; 136, identifier:sum; 137, argument_list; 137, 138; 138, identifier:permutation_probabilities; 139, return_statement; 139, 140; 140, identifier:probability_of_i
def p(i, sample_size, weights): """ Given a weighted set and sample size return the probabilty that the weight `i` will be present in the sample. Created to test the output of the `SomeOf` maker class. The math was provided by Andy Blackshaw - thank you dad :) """ # Determine the initial pick values weight_i = weights[i] weights_sum = sum(weights) # Build a list of weights that don't contain the weight `i`. This list will # be used to build the possible picks before weight `i`. other_weights = list(weights) del other_weights[i] # Calculate the probability probability_of_i = 0 for picks in range(0, sample_size): # Build the list of possible permutations for this pick in the sample permutations = list(itertools.permutations(other_weights, picks)) # Calculate the probability for this permutation permutation_probabilities = [] for permutation in permutations: # Calculate the probability for each pick in the permutation pick_probabilities = [] pick_weight_sum = weights_sum for pick in permutation: pick_probabilities.append(pick / pick_weight_sum) # Each time we pick we update the sum of the weight the next # pick is from. pick_weight_sum -= pick # Add the probability of picking i as the last pick pick_probabilities += [weight_i / pick_weight_sum] # Multiply all the probabilities for the permutation together permutation_probability = reduce( lambda x, y: x * y, pick_probabilities ) permutation_probabilities.append(permutation_probability) # Add together all the probabilities for all permutations together probability_of_i += sum(permutation_probabilities) return probability_of_i
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:diff_to_html; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, identifier:details; 6, block; 6, 7; 6, 9; 6, 13; 6, 14; 6, 20; 6, 68; 6, 69; 6, 82; 6, 145; 6, 146; 6, 159; 6, 263; 6, 264; 6, 277; 6, 340; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:changes; 12, list:[]; 13, comment; 14, if_statement; 14, 15; 14, 17; 15, not_operator; 15, 16; 16, identifier:details; 17, block; 17, 18; 18, return_statement; 18, 19; 19, string:''; 20, function_definition; 20, 21; 20, 22; 20, 24; 21, function_name:_frame; 22, parameters; 22, 23; 23, identifier:value; 24, block; 24, 25; 24, 27; 24, 63; 25, expression_statement; 25, 26; 26, comment; 27, if_statement; 27, 28; 27, 37; 27, 42; 28, boolean_operator:and; 28, 29; 28, 34; 29, call; 29, 30; 29, 31; 30, identifier:isinstance; 31, argument_list; 31, 32; 31, 33; 32, identifier:value; 33, identifier:dict; 34, comparison_operator:in; 34, 35; 34, 36; 35, string:'_str'; 36, identifier:value; 37, block; 37, 38; 38, return_statement; 38, 39; 39, subscript; 39, 40; 39, 41; 40, identifier:value; 41, string:'_str'; 42, elif_clause; 42, 43; 42, 48; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:value; 47, identifier:list; 48, block; 48, 49; 49, return_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, string:', '; 53, identifier:join; 54, argument_list; 54, 55; 55, list_comprehension; 55, 56; 55, 60; 56, call; 56, 57; 56, 58; 57, identifier:_frame; 58, argument_list; 58, 59; 59, identifier:v; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:v; 62, identifier:value; 63, return_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:str; 66, argument_list; 66, 67; 67, identifier:value; 68, comment; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:fields; 72, call; 72, 73; 72, 74; 73, identifier:sorted; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:details; 78, identifier:get; 79, argument_list; 79, 80; 79, 81; 80, string:'additions'; 81, dictionary; 82, for_statement; 82, 83; 82, 84; 82, 85; 83, identifier:field; 84, identifier:fields; 85, block; 85, 86; 85, 97; 85, 120; 85, 138; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:new_value; 89, call; 89, 90; 89, 91; 90, identifier:_frame; 91, argument_list; 91, 92; 92, subscript; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:details; 95, string:'additions'; 96, identifier:field; 97, if_statement; 97, 98; 97, 103; 98, call; 98, 99; 98, 100; 99, identifier:isinstance; 100, argument_list; 100, 101; 100, 102; 101, identifier:new_value; 102, identifier:list; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:new_value; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, string:', '; 110, identifier:join; 111, argument_list; 111, 112; 112, list_comprehension; 112, 113; 112, 117; 113, call; 113, 114; 113, 115; 114, identifier:_frame; 115, argument_list; 115, 116; 116, identifier:v; 117, for_in_clause; 117, 118; 117, 119; 118, identifier:v; 119, identifier:new_value; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:change; 123, call; 123, 124; 123, 131; 124, attribute; 124, 125; 124, 130; 125, subscript; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:cls; 128, identifier:_templates; 129, string:'add'; 130, identifier:format; 131, argument_list; 131, 132; 131, 135; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:field; 134, identifier:field; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:new_value; 137, identifier:new_value; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:changes; 142, identifier:append; 143, argument_list; 143, 144; 144, identifier:change; 145, comment; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:fields; 149, call; 149, 150; 149, 151; 150, identifier:sorted; 151, argument_list; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:details; 155, identifier:get; 156, argument_list; 156, 157; 156, 158; 157, string:'updates'; 158, dictionary; 159, for_statement; 159, 160; 159, 161; 159, 162; 160, identifier:field; 161, identifier:fields; 162, block; 162, 163; 162, 176; 162, 199; 162, 212; 162, 235; 162, 256; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:original_value; 166, call; 166, 167; 166, 168; 167, identifier:_frame; 168, argument_list; 168, 169; 169, subscript; 169, 170; 169, 175; 170, subscript; 170, 171; 170, 174; 171, subscript; 171, 172; 171, 173; 172, identifier:details; 173, string:'updates'; 174, identifier:field; 175, integer:0; 176, if_statement; 176, 177; 176, 182; 177, call; 177, 178; 177, 179; 178, identifier:isinstance; 179, argument_list; 179, 180; 179, 181; 180, identifier:original_value; 181, identifier:list; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:original_value; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, string:', '; 189, identifier:join; 190, argument_list; 190, 191; 191, list_comprehension; 191, 192; 191, 196; 192, call; 192, 193; 192, 194; 193, identifier:_frame; 194, argument_list; 194, 195; 195, identifier:v; 196, for_in_clause; 196, 197; 196, 198; 197, identifier:v; 198, identifier:original_value; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:new_value; 202, call; 202, 203; 202, 204; 203, identifier:_frame; 204, argument_list; 204, 205; 205, subscript; 205, 206; 205, 211; 206, subscript; 206, 207; 206, 210; 207, subscript; 207, 208; 207, 209; 208, identifier:details; 209, string:'updates'; 210, identifier:field; 211, integer:1; 212, if_statement; 212, 213; 212, 218; 213, call; 213, 214; 213, 215; 214, identifier:isinstance; 215, argument_list; 215, 216; 215, 217; 216, identifier:new_value; 217, identifier:list; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:new_value; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, string:', '; 225, identifier:join; 226, argument_list; 226, 227; 227, list_comprehension; 227, 228; 227, 232; 228, call; 228, 229; 228, 230; 229, identifier:_frame; 230, argument_list; 230, 231; 231, identifier:v; 232, for_in_clause; 232, 233; 232, 234; 233, identifier:v; 234, identifier:new_value; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:change; 238, call; 238, 239; 238, 246; 239, attribute; 239, 240; 239, 245; 240, subscript; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:cls; 243, identifier:_templates; 244, string:'update'; 245, identifier:format; 246, argument_list; 246, 247; 246, 250; 246, 253; 247, keyword_argument; 247, 248; 247, 249; 248, identifier:field; 249, identifier:field; 250, keyword_argument; 250, 251; 250, 252; 251, identifier:original_value; 252, identifier:original_value; 253, keyword_argument; 253, 254; 253, 255; 254, identifier:new_value; 255, identifier:new_value; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:changes; 260, identifier:append; 261, argument_list; 261, 262; 262, identifier:change; 263, comment; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:fields; 267, call; 267, 268; 267, 269; 268, identifier:sorted; 269, argument_list; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:details; 273, identifier:get; 274, argument_list; 274, 275; 274, 276; 275, string:'deletions'; 276, dictionary; 277, for_statement; 277, 278; 277, 279; 277, 280; 278, identifier:field; 279, identifier:fields; 280, block; 280, 281; 280, 292; 280, 315; 280, 333; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:original_value; 284, call; 284, 285; 284, 286; 285, identifier:_frame; 286, argument_list; 286, 287; 287, subscript; 287, 288; 287, 291; 288, subscript; 288, 289; 288, 290; 289, identifier:details; 290, string:'deletions'; 291, identifier:field; 292, if_statement; 292, 293; 292, 298; 293, call; 293, 294; 293, 295; 294, identifier:isinstance; 295, argument_list; 295, 296; 295, 297; 296, identifier:original_value; 297, identifier:list; 298, block; 298, 299; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:original_value; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, string:', '; 305, identifier:join; 306, argument_list; 306, 307; 307, list_comprehension; 307, 308; 307, 312; 308, call; 308, 309; 308, 310; 309, identifier:_frame; 310, argument_list; 310, 311; 311, identifier:v; 312, for_in_clause; 312, 313; 312, 314; 313, identifier:v; 314, identifier:original_value; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:change; 318, call; 318, 319; 318, 326; 319, attribute; 319, 320; 319, 325; 320, subscript; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:cls; 323, identifier:_templates; 324, string:'delete'; 325, identifier:format; 326, argument_list; 326, 327; 326, 330; 327, keyword_argument; 327, 328; 327, 329; 328, identifier:field; 329, identifier:field; 330, keyword_argument; 330, 331; 330, 332; 331, identifier:original_value; 332, identifier:original_value; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:changes; 337, identifier:append; 338, argument_list; 338, 339; 339, identifier:change; 340, return_statement; 340, 341; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, string:'\n'; 344, identifier:join; 345, argument_list; 345, 346; 346, identifier:changes
def diff_to_html(cls, details): """Return an entry's details in HTML format""" changes = [] # Check that there are details to convert to HMTL if not details: return '' def _frame(value): """ Handle converted `Frame` references where the human identifier is stored against the `_str` key. """ if isinstance(value, dict) and '_str' in value: return value['_str'] elif isinstance(value, list): return ', '.join([_frame(v) for v in value]) return str(value) # Additions fields = sorted(details.get('additions', {})) for field in fields: new_value = _frame(details['additions'][field]) if isinstance(new_value, list): new_value = ', '.join([_frame(v) for v in new_value]) change = cls._templates['add'].format( field=field, new_value=new_value ) changes.append(change) # Updates fields = sorted(details.get('updates', {})) for field in fields: original_value = _frame(details['updates'][field][0]) if isinstance(original_value, list): original_value = ', '.join([_frame(v) for v in original_value]) new_value = _frame(details['updates'][field][1]) if isinstance(new_value, list): new_value = ', '.join([_frame(v) for v in new_value]) change = cls._templates['update'].format( field=field, original_value=original_value, new_value=new_value ) changes.append(change) # Deletions fields = sorted(details.get('deletions', {})) for field in fields: original_value = _frame(details['deletions'][field]) if isinstance(original_value, list): original_value = ', '.join([_frame(v) for v in original_value]) change = cls._templates['delete'].format( field=field, original_value=original_value ) changes.append(change) return '\n'.join(changes)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:SortBy; 3, parameters; 3, 4; 4, list_splat_pattern; 4, 5; 5, identifier:qs; 6, block; 6, 7; 6, 9; 6, 13; 6, 56; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:sort; 12, list:[]; 13, for_statement; 13, 14; 13, 15; 13, 16; 14, identifier:q; 15, identifier:qs; 16, block; 16, 17; 17, if_statement; 17, 18; 17, 26; 17, 43; 18, call; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:q; 22, identifier:_path; 23, identifier:endswith; 24, argument_list; 24, 25; 25, string:'.desc'; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:sort; 31, identifier:append; 32, argument_list; 32, 33; 33, tuple; 33, 34; 33, 42; 34, subscript; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:q; 37, identifier:_path; 38, slice; 38, 39; 38, 40; 39, colon; 40, unary_operator:-; 40, 41; 41, integer:5; 42, identifier:DESCENDING; 43, else_clause; 43, 44; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:sort; 49, identifier:append; 50, argument_list; 50, 51; 51, tuple; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:q; 54, identifier:_path; 55, identifier:ASCENDING; 56, return_statement; 56, 57; 57, identifier:sort
def SortBy(*qs): """Convert a list of Q objects into list of sort instructions""" sort = [] for q in qs: if q._path.endswith('.desc'): sort.append((q._path[:-5], DESCENDING)) else: sort.append((q._path, ASCENDING)) return sort
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:refresh; 3, parameters; 4, block; 4, 5; 4, 7; 4, 11; 4, 53; 4, 67; 4, 74; 4, 143; 5, expression_statement; 5, 6; 6, comment; 7, expression_statement; 7, 8; 8, assignment; 8, 9; 8, 10; 9, identifier:override_files; 10, list:[]; 11, for_statement; 11, 12; 11, 13; 11, 18; 12, identifier:stack; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:traceback; 16, identifier:extract_stack; 17, argument_list; 18, block; 18, 19; 18, 40; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:f; 22, call; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:os; 26, identifier:path; 27, identifier:join; 28, argument_list; 28, 29; 28, 39; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:os; 33, identifier:path; 34, identifier:dirname; 35, argument_list; 35, 36; 36, subscript; 36, 37; 36, 38; 37, identifier:stack; 38, integer:0; 39, identifier:OVERRIDE_FILE; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:not; 41, 42; 41, 43; 42, identifier:f; 43, identifier:override_files; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:override_files; 49, identifier:insert; 50, argument_list; 50, 51; 50, 52; 51, integer:0; 52, identifier:f; 53, if_statement; 53, 54; 53, 57; 54, comparison_operator:in; 54, 55; 54, 56; 55, identifier:OVERRIDE_FILE; 56, identifier:override_files; 57, block; 57, 58; 58, delete_statement; 58, 59; 59, subscript; 59, 60; 59, 61; 60, identifier:override_files; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:override_files; 64, identifier:index; 65, argument_list; 65, 66; 66, identifier:OVERRIDE_FILE; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:override_files; 71, identifier:append; 72, argument_list; 72, 73; 73, identifier:OVERRIDE_FILE; 74, function_definition; 74, 75; 74, 76; 74, 78; 75, function_name:import_path; 76, parameters; 76, 77; 77, identifier:path; 78, block; 78, 79; 78, 105; 78, 109; 78, 121; 78, 132; 78, 141; 79, if_statement; 79, 80; 79, 88; 80, comparison_operator:<; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:sys; 83, identifier:version_info; 84, tuple; 84, 85; 84, 86; 84, 87; 85, integer:3; 86, integer:5; 87, integer:0; 88, block; 88, 89; 88, 95; 89, import_from_statement; 89, 90; 89, 93; 90, dotted_name; 90, 91; 90, 92; 91, identifier:importlib; 92, identifier:machinery; 93, dotted_name; 93, 94; 94, identifier:SourceFileLoader; 95, return_statement; 95, 96; 96, call; 96, 97; 96, 104; 97, attribute; 97, 98; 97, 103; 98, call; 98, 99; 98, 100; 99, identifier:SourceFileLoader; 100, argument_list; 100, 101; 100, 102; 101, identifier:__name__; 102, identifier:path; 103, identifier:load_module; 104, argument_list; 105, import_statement; 105, 106; 106, dotted_name; 106, 107; 106, 108; 107, identifier:importlib; 108, identifier:util; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:spec; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:importlib; 116, identifier:util; 117, identifier:spec_from_file_location; 118, argument_list; 118, 119; 118, 120; 119, identifier:__name__; 120, identifier:path; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:module; 124, call; 124, 125; 124, 130; 125, attribute; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:importlib; 128, identifier:util; 129, identifier:module_from_spec; 130, argument_list; 130, 131; 131, identifier:spec; 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:spec; 137, identifier:loader; 138, identifier:exec_module; 139, argument_list; 139, 140; 140, identifier:module; 141, return_statement; 141, 142; 142, identifier:module; 143, for_statement; 143, 144; 143, 145; 143, 146; 144, identifier:override_file; 145, identifier:override_files; 146, block; 146, 147; 146, 159; 146, 166; 147, if_statement; 147, 148; 147, 157; 148, not_operator; 148, 149; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:os; 153, identifier:path; 154, identifier:isfile; 155, argument_list; 155, 156; 156, identifier:override_file; 157, block; 157, 158; 158, continue_statement; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:mod; 162, call; 162, 163; 162, 164; 163, identifier:import_path; 164, argument_list; 164, 165; 165, identifier:override_file; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, call; 169, 170; 169, 171; 170, identifier:globals; 171, argument_list; 172, identifier:update; 173, argument_list; 173, 174; 174, dictionary_comprehension; 174, 175; 174, 182; 174, 188; 175, pair; 175, 176; 175, 177; 176, identifier:n; 177, call; 177, 178; 177, 179; 178, identifier:getattr; 179, argument_list; 179, 180; 179, 181; 180, identifier:mod; 181, identifier:n; 182, for_in_clause; 182, 183; 182, 184; 183, identifier:n; 184, call; 184, 185; 184, 186; 185, identifier:dir; 186, argument_list; 186, 187; 187, identifier:mod; 188, if_clause; 188, 189; 189, not_operator; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:n; 193, identifier:startswith; 194, argument_list; 194, 195; 195, string:"__"
def refresh(): """Scan over all the involved directories and load configs from them.""" override_files = [] for stack in traceback.extract_stack(): f = os.path.join(os.path.dirname(stack[0]), OVERRIDE_FILE) if f not in override_files: override_files.insert(0, f) if OVERRIDE_FILE in override_files: del override_files[override_files.index(OVERRIDE_FILE)] override_files.append(OVERRIDE_FILE) def import_path(path): if sys.version_info < (3, 5, 0): from importlib.machinery import SourceFileLoader return SourceFileLoader(__name__, path).load_module() import importlib.util spec = importlib.util.spec_from_file_location(__name__, path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module for override_file in override_files: if not os.path.isfile(override_file): continue mod = import_path(override_file) globals().update({n: getattr(mod, n) for n in dir(mod) if not n.startswith("__")})
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:setup; 3, parameters; 3, 4; 3, 14; 3, 18; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:level; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:Union; 9, type_parameter; 9, 10; 9, 12; 10, type; 10, 11; 11, identifier:str; 12, type; 12, 13; 13, identifier:int; 14, typed_parameter; 14, 15; 14, 16; 15, identifier:structured; 16, type; 16, 17; 17, identifier:bool; 18, typed_default_parameter; 18, 19; 18, 20; 18, 22; 19, identifier:config_path; 20, type; 20, 21; 21, identifier:str; 22, None; 23, block; 23, 24; 23, 26; 23, 28; 23, 32; 23, 48; 23, 91; 23, 114; 23, 115; 23, 116; 23, 122; 23, 129; 23, 193; 23, 201; 23, 208; 24, expression_statement; 24, 25; 25, comment; 26, global_statement; 26, 27; 27, identifier:logs_are_structured; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:logs_are_structured; 31, identifier:structured; 32, if_statement; 32, 33; 32, 39; 33, not_operator; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:isinstance; 36, argument_list; 36, 37; 36, 38; 37, identifier:level; 38, identifier:int; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:level; 43, subscript; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:logging; 46, identifier:_nameToLevel; 47, identifier:level; 48, function_definition; 48, 49; 48, 50; 48, 52; 49, function_name:ensure_utf8_stream; 50, parameters; 50, 51; 51, identifier:stream; 52, block; 52, 53; 52, 89; 53, if_statement; 53, 54; 53, 68; 54, boolean_operator:and; 54, 55; 54, 63; 55, not_operator; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:isinstance; 58, argument_list; 58, 59; 58, 60; 59, identifier:stream; 60, attribute; 60, 61; 60, 62; 61, identifier:io; 62, identifier:StringIO; 63, call; 63, 64; 63, 65; 64, identifier:hasattr; 65, argument_list; 65, 66; 65, 67; 66, identifier:stream; 67, string:"buffer"; 68, block; 68, 69; 68, 83; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:stream; 72, call; 72, 73; 72, 79; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:codecs; 76, identifier:getwriter; 77, argument_list; 77, 78; 78, string:"utf-8"; 79, argument_list; 79, 80; 80, attribute; 80, 81; 80, 82; 81, identifier:stream; 82, identifier:buffer; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:stream; 87, identifier:encoding; 88, string:"utf-8"; 89, return_statement; 89, 90; 90, identifier:stream; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 100; 93, pattern_list; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:sys; 96, identifier:stdout; 97, attribute; 97, 98; 97, 99; 98, identifier:sys; 99, identifier:stderr; 100, generator_expression; 100, 101; 100, 105; 101, call; 101, 102; 101, 103; 102, identifier:ensure_utf8_stream; 103, argument_list; 103, 104; 104, identifier:s; 105, for_in_clause; 105, 106; 105, 107; 106, identifier:s; 107, tuple; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:sys; 110, identifier:stdout; 111, attribute; 111, 112; 111, 113; 112, identifier:sys; 113, identifier:stderr; 114, comment; 115, comment; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:logging; 120, identifier:basicConfig; 121, argument_list; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:logging; 126, identifier:setLogRecordFactory; 127, argument_list; 127, 128; 128, identifier:NumpyLogRecord; 129, if_statement; 129, 130; 129, 142; 130, boolean_operator:and; 130, 131; 130, 134; 131, comparison_operator:is; 131, 132; 131, 133; 132, identifier:config_path; 133, None; 134, call; 134, 135; 134, 140; 135, attribute; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:os; 138, identifier:path; 139, identifier:isfile; 140, argument_list; 140, 141; 141, identifier:config_path; 142, block; 142, 143; 142, 163; 143, with_statement; 143, 144; 143, 153; 144, with_clause; 144, 145; 145, with_item; 145, 146; 146, as_pattern; 146, 147; 146, 151; 147, call; 147, 148; 147, 149; 148, identifier:open; 149, argument_list; 149, 150; 150, identifier:config_path; 151, as_pattern_target; 151, 152; 152, identifier:fh; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:config; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:yaml; 160, identifier:safe_load; 161, argument_list; 161, 162; 162, identifier:fh; 163, for_statement; 163, 164; 163, 167; 163, 172; 164, pattern_list; 164, 165; 164, 166; 165, identifier:key; 166, identifier:val; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:config; 170, identifier:items; 171, argument_list; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 183; 175, attribute; 175, 176; 175, 182; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:logging; 179, identifier:getLogger; 180, argument_list; 180, 181; 181, identifier:key; 182, identifier:setLevel; 183, argument_list; 183, 184; 184, call; 184, 185; 184, 190; 185, attribute; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:logging; 188, identifier:_nameToLevel; 189, identifier:get; 190, argument_list; 190, 191; 190, 192; 191, identifier:val; 192, identifier:level; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:root; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:logging; 199, identifier:getLogger; 200, argument_list; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:root; 205, identifier:setLevel; 206, argument_list; 206, 207; 207, identifier:level; 208, if_statement; 208, 209; 208, 211; 208, 245; 209, not_operator; 209, 210; 210, identifier:structured; 211, block; 211, 212; 212, if_statement; 212, 213; 212, 227; 213, boolean_operator:and; 213, 214; 213, 220; 214, not_operator; 214, 215; 215, attribute; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:sys; 218, identifier:stdin; 219, identifier:closed; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:sys; 224, identifier:stdout; 225, identifier:isatty; 226, argument_list; 227, block; 227, 228; 227, 236; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:handler; 231, subscript; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:root; 234, identifier:handlers; 235, integer:0; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:handler; 240, identifier:setFormatter; 241, argument_list; 241, 242; 242, call; 242, 243; 242, 244; 243, identifier:AwesomeFormatter; 244, argument_list; 245, else_clause; 245, 246; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 254; 249, subscript; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:root; 252, identifier:handlers; 253, integer:0; 254, call; 254, 255; 254, 256; 255, identifier:StructuredHandler; 256, argument_list; 256, 257; 257, identifier:level
def setup(level: Union[str, int], structured: bool, config_path: str = None): """ Make stdout and stderr unicode friendly in case of misconfigured \ environments, initializes the logging, structured logging and \ enables colored logs if it is appropriate. :param level: The global logging level. :param structured: Output JSON logs to stdout. :param config_path: Path to a yaml file that configures the level of output of the loggers. \ Root logger level is set through the level argument and will override any \ root configuration found in the conf file. :return: None """ global logs_are_structured logs_are_structured = structured if not isinstance(level, int): level = logging._nameToLevel[level] def ensure_utf8_stream(stream): if not isinstance(stream, io.StringIO) and hasattr(stream, "buffer"): stream = codecs.getwriter("utf-8")(stream.buffer) stream.encoding = "utf-8" return stream sys.stdout, sys.stderr = (ensure_utf8_stream(s) for s in (sys.stdout, sys.stderr)) # basicConfig is only called to make sure there is at least one handler for the root logger. # All the output level setting is down right afterwards. logging.basicConfig() logging.setLogRecordFactory(NumpyLogRecord) if config_path is not None and os.path.isfile(config_path): with open(config_path) as fh: config = yaml.safe_load(fh) for key, val in config.items(): logging.getLogger(key).setLevel(logging._nameToLevel.get(val, level)) root = logging.getLogger() root.setLevel(level) if not structured: if not sys.stdin.closed and sys.stdout.isatty(): handler = root.handlers[0] handler.setFormatter(AwesomeFormatter()) else: root.handlers[0] = StructuredHandler(level)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:getMessage; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 41; 5, 150; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 18; 8, 30; 9, call; 9, 10; 9, 11; 10, identifier:isinstance; 11, argument_list; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:msg; 15, attribute; 15, 16; 15, 17; 16, identifier:numpy; 17, identifier:ndarray; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:msg; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:array2string; 26, argument_list; 26, 27; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:msg; 30, else_clause; 30, 31; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:msg; 35, call; 35, 36; 35, 37; 36, identifier:str; 37, argument_list; 37, 38; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:msg; 41, if_statement; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:args; 45, block; 45, 46; 45, 52; 45, 144; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:a2s; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:array2string; 52, if_statement; 52, 53; 52, 60; 52, 92; 52, 126; 53, call; 53, 54; 53, 55; 54, identifier:isinstance; 55, argument_list; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:args; 59, identifier:Dict; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:args; 64, dictionary_comprehension; 64, 65; 64, 81; 65, pair; 65, 66; 65, 67; 66, identifier:k; 67, parenthesized_expression; 67, 68; 68, conditional_expression:if; 68, 69; 68, 73; 68, 80; 69, call; 69, 70; 69, 71; 70, identifier:a2s; 71, argument_list; 71, 72; 72, identifier:v; 73, call; 73, 74; 73, 75; 74, identifier:isinstance; 75, argument_list; 75, 76; 75, 77; 76, identifier:v; 77, attribute; 77, 78; 77, 79; 78, identifier:numpy; 79, identifier:ndarray; 80, identifier:v; 81, for_in_clause; 81, 82; 81, 85; 82, tuple_pattern; 82, 83; 82, 84; 83, identifier:k; 84, identifier:v; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:args; 90, identifier:items; 91, argument_list; 92, elif_clause; 92, 93; 92, 100; 93, call; 93, 94; 93, 95; 94, identifier:isinstance; 95, argument_list; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:args; 99, identifier:Sequence; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:args; 104, call; 104, 105; 104, 106; 105, identifier:tuple; 106, generator_expression; 106, 107; 106, 121; 107, parenthesized_expression; 107, 108; 108, conditional_expression:if; 108, 109; 108, 113; 108, 120; 109, call; 109, 110; 109, 111; 110, identifier:a2s; 111, argument_list; 111, 112; 112, identifier:a; 113, call; 113, 114; 113, 115; 114, identifier:isinstance; 115, argument_list; 115, 116; 115, 117; 116, identifier:a; 117, attribute; 117, 118; 117, 119; 118, identifier:numpy; 119, identifier:ndarray; 120, identifier:a; 121, for_in_clause; 121, 122; 121, 123; 122, identifier:a; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:args; 126, else_clause; 126, 127; 127, block; 127, 128; 128, raise_statement; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:TypeError; 131, argument_list; 131, 132; 132, binary_operator:%; 132, 133; 132, 134; 133, string:"Unexpected input '%s' with type '%s'"; 134, tuple; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:args; 138, call; 138, 139; 138, 140; 139, identifier:type; 140, argument_list; 140, 141; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:args; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:msg; 147, binary_operator:%; 147, 148; 147, 149; 148, identifier:msg; 149, identifier:args; 150, return_statement; 150, 151; 151, identifier:msg
def getMessage(self): """ Return the message for this LogRecord. Return the message for this LogRecord after merging any user-supplied \ arguments with the message. """ if isinstance(self.msg, numpy.ndarray): msg = self.array2string(self.msg) else: msg = str(self.msg) if self.args: a2s = self.array2string if isinstance(self.args, Dict): args = {k: (a2s(v) if isinstance(v, numpy.ndarray) else v) for (k, v) in self.args.items()} elif isinstance(self.args, Sequence): args = tuple((a2s(a) if isinstance(a, numpy.ndarray) else a) for a in self.args) else: raise TypeError("Unexpected input '%s' with type '%s'" % (self.args, type(self.args))) msg = msg % args return msg
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:toposorted; 3, parameters; 3, 4; 3, 5; 4, identifier:nodes; 5, identifier:edges; 6, block; 6, 7; 6, 9; 6, 16; 6, 23; 6, 53; 6, 64; 6, 68; 6, 141; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:incoming; 12, call; 12, 13; 12, 14; 13, identifier:defaultdict; 14, argument_list; 14, 15; 15, identifier:set; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:outgoing; 19, call; 19, 20; 19, 21; 20, identifier:defaultdict; 21, argument_list; 21, 22; 22, identifier:set; 23, for_statement; 23, 24; 23, 25; 23, 26; 24, identifier:edge; 25, identifier:edges; 26, block; 26, 27; 26, 40; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 36; 29, attribute; 29, 30; 29, 35; 30, subscript; 30, 31; 30, 32; 31, identifier:incoming; 32, attribute; 32, 33; 32, 34; 33, identifier:edge; 34, identifier:to_id; 35, identifier:add; 36, argument_list; 36, 37; 37, attribute; 37, 38; 37, 39; 38, identifier:edge; 39, identifier:from_id; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 49; 42, attribute; 42, 43; 42, 48; 43, subscript; 43, 44; 43, 45; 44, identifier:outgoing; 45, attribute; 45, 46; 45, 47; 46, identifier:edge; 47, identifier:from_id; 48, identifier:add; 49, argument_list; 49, 50; 50, attribute; 50, 51; 50, 52; 51, identifier:edge; 52, identifier:to_id; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:working_set; 56, call; 56, 57; 56, 58; 57, identifier:list; 58, argument_list; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:nodes; 62, identifier:values; 63, argument_list; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:results; 67, list:[]; 68, while_statement; 68, 69; 68, 70; 69, identifier:working_set; 70, block; 70, 71; 70, 75; 70, 121; 70, 137; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:remaining; 74, list:[]; 75, for_statement; 75, 76; 75, 77; 75, 78; 76, identifier:node; 77, identifier:working_set; 78, block; 78, 79; 78, 95; 78, 102; 79, if_statement; 79, 80; 79, 85; 79, 86; 80, subscript; 80, 81; 80, 82; 81, identifier:incoming; 82, attribute; 82, 83; 82, 84; 83, identifier:node; 84, identifier:id; 85, comment; 86, block; 86, 87; 86, 94; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:remaining; 91, identifier:append; 92, argument_list; 92, 93; 93, identifier:node; 94, continue_statement; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:results; 99, identifier:append; 100, argument_list; 100, 101; 101, identifier:node; 102, for_statement; 102, 103; 102, 104; 102, 109; 103, identifier:child; 104, subscript; 104, 105; 104, 106; 105, identifier:outgoing; 106, attribute; 106, 107; 106, 108; 107, identifier:node; 108, identifier:id; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:incoming; 115, identifier:child; 116, identifier:remove; 117, argument_list; 117, 118; 118, attribute; 118, 119; 118, 120; 119, identifier:node; 120, identifier:id; 121, if_statement; 121, 122; 121, 131; 122, comparison_operator:==; 122, 123; 122, 127; 123, call; 123, 124; 123, 125; 124, identifier:len; 125, argument_list; 125, 126; 126, identifier:working_set; 127, call; 127, 128; 127, 129; 128, identifier:len; 129, argument_list; 129, 130; 130, identifier:remaining; 131, block; 131, 132; 132, raise_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:Exception; 135, argument_list; 135, 136; 136, string:"Cycle detected"; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:working_set; 140, identifier:remaining; 141, return_statement; 141, 142; 142, identifier:results
def toposorted(nodes, edges): """ Perform a topological sort on the input resources. The topological sort uses Kahn's algorithm, which is a stable sort and will preserve this ordering; note that a DFS will produce a worst case ordering from the perspective of batching. """ incoming = defaultdict(set) outgoing = defaultdict(set) for edge in edges: incoming[edge.to_id].add(edge.from_id) outgoing[edge.from_id].add(edge.to_id) working_set = list(nodes.values()) results = [] while working_set: remaining = [] for node in working_set: if incoming[node.id]: # node still has incoming edges remaining.append(node) continue results.append(node) for child in outgoing[node.id]: incoming[child].remove(node.id) if len(working_set) == len(remaining): raise Exception("Cycle detected") working_set = remaining return results
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:proc; 3, parameters; 3, 4; 3, 5; 4, identifier:ctx; 5, identifier:files; 6, block; 6, 7; 6, 9; 6, 43; 6, 53; 6, 65; 6, 76; 7, expression_statement; 7, 8; 8, string:'''Process calculated structures'''; 9, function_definition; 9, 10; 9, 11; 9, 14; 10, function_name:calc_reader; 11, parameters; 11, 12; 11, 13; 12, identifier:fn; 13, identifier:verb; 14, block; 14, 15; 14, 34; 15, if_statement; 15, 16; 15, 17; 16, identifier:verb; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:echo; 21, argument_list; 21, 22; 21, 28; 21, 31; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, string:'Reading: {:<60s}\r'; 25, identifier:format; 26, argument_list; 26, 27; 27, identifier:fn; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:nl; 30, False; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:err; 33, True; 34, return_statement; 34, 35; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:ase; 39, identifier:io; 40, identifier:read; 41, argument_list; 41, 42; 42, identifier:fn; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:action; 46, subscript; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:ctx; 50, identifier:parent; 51, identifier:params; 52, string:'action'; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:systems; 56, list_comprehension; 56, 57; 56, 62; 57, call; 57, 58; 57, 59; 58, identifier:calc_reader; 59, argument_list; 59, 60; 59, 61; 60, identifier:calc; 61, identifier:verbose; 62, for_in_clause; 62, 63; 62, 64; 63, identifier:calc; 64, identifier:files; 65, if_statement; 65, 66; 65, 67; 66, identifier:verbose; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:echo; 71, argument_list; 71, 72; 71, 73; 72, string:''; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:err; 75, True; 76, if_statement; 76, 77; 76, 80; 76, 306; 77, comparison_operator:==; 77, 78; 77, 79; 78, identifier:action; 79, string:'cij'; 80, block; 80, 81; 80, 99; 80, 111; 80, 115; 80, 259; 80, 302; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:cij; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:elastic; 87, identifier:get_elastic_tensor; 88, argument_list; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:systems; 91, integer:0; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:systems; 94, subscript; 94, 95; 94, 96; 95, identifier:systems; 96, slice; 96, 97; 96, 98; 97, integer:1; 98, colon; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:msv; 102, call; 102, 103; 102, 110; 103, attribute; 103, 104; 103, 109; 104, subscript; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:cij; 107, integer:1; 108, integer:3; 109, identifier:max; 110, argument_list; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:eps; 114, float:1e-4; 115, if_statement; 115, 116; 115, 117; 116, identifier:verbose; 117, block; 117, 118; 117, 127; 117, 156; 117, 184; 117, 189; 117, 219; 117, 224; 117, 250; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:echo; 121, argument_list; 121, 122; 122, binary_operator:+; 122, 123; 122, 124; 123, string:'Cij solution\n'; 124, binary_operator:*; 124, 125; 124, 126; 125, integer:30; 126, string:'-'; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:echo; 130, argument_list; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, string:' Solution rank: {:2d}{}'; 134, identifier:format; 135, argument_list; 135, 136; 135, 141; 136, subscript; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:cij; 139, integer:1; 140, integer:2; 141, conditional_expression:if; 141, 142; 141, 143; 141, 155; 142, string:' (undetermined)'; 143, comparison_operator:<; 143, 144; 143, 149; 144, subscript; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:cij; 147, integer:1; 148, integer:2; 149, call; 149, 150; 149, 151; 150, identifier:len; 151, argument_list; 151, 152; 152, subscript; 152, 153; 152, 154; 153, identifier:cij; 154, integer:0; 155, string:''; 156, if_statement; 156, 157; 156, 169; 157, comparison_operator:==; 157, 158; 157, 163; 158, subscript; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:cij; 161, integer:1; 162, integer:2; 163, call; 163, 164; 163, 165; 164, identifier:len; 165, argument_list; 165, 166; 166, subscript; 166, 167; 166, 168; 167, identifier:cij; 168, integer:0; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:echo; 173, argument_list; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, string:' Square of residuals: {:7.2g}'; 177, identifier:format; 178, argument_list; 178, 179; 179, subscript; 179, 180; 179, 183; 180, subscript; 180, 181; 180, 182; 181, identifier:cij; 182, integer:1; 183, integer:1; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 187; 186, identifier:echo; 187, argument_list; 187, 188; 188, string:' Relative singular values:'; 189, for_statement; 189, 190; 189, 191; 189, 198; 190, identifier:sv; 191, binary_operator:/; 191, 192; 191, 197; 192, subscript; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:cij; 195, integer:1; 196, integer:3; 197, identifier:msv; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:echo; 202, argument_list; 202, 203; 202, 216; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, string:'{:7.4f}{}'; 206, identifier:format; 207, argument_list; 207, 208; 207, 209; 208, identifier:sv; 209, conditional_expression:if; 209, 210; 209, 211; 209, 215; 210, string:'* '; 211, comparison_operator:<; 211, 212; 211, 214; 212, parenthesized_expression; 212, 213; 213, identifier:sv; 214, identifier:eps; 215, string:' '; 216, keyword_argument; 216, 217; 216, 218; 217, identifier:nl; 218, False; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 222; 221, identifier:echo; 222, argument_list; 222, 223; 223, string:'\n\nElastic tensor (GPa):'; 224, for_statement; 224, 225; 224, 226; 224, 236; 225, identifier:dsc; 226, call; 226, 227; 226, 232; 227, attribute; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:elastic; 230, identifier:elastic; 231, identifier:get_cij_order; 232, argument_list; 232, 233; 233, subscript; 233, 234; 233, 235; 234, identifier:systems; 235, integer:0; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 240; 239, identifier:echo; 240, argument_list; 240, 241; 240, 247; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, string:'{: >7s} '; 244, identifier:format; 245, argument_list; 245, 246; 246, identifier:dsc; 247, keyword_argument; 247, 248; 247, 249; 248, identifier:nl; 249, False; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:echo; 253, argument_list; 253, 254; 254, binary_operator:+; 254, 255; 254, 256; 255, string:'\n'; 256, binary_operator:*; 256, 257; 256, 258; 257, integer:30; 258, string:'-'; 259, for_statement; 259, 260; 259, 263; 259, 276; 260, pattern_list; 260, 261; 260, 262; 261, identifier:c; 262, identifier:sv; 263, call; 263, 264; 263, 265; 264, identifier:zip; 265, argument_list; 265, 266; 265, 269; 266, subscript; 266, 267; 266, 268; 267, identifier:cij; 268, integer:0; 269, binary_operator:/; 269, 270; 269, 275; 270, subscript; 270, 271; 270, 274; 271, subscript; 271, 272; 271, 273; 272, identifier:cij; 273, integer:1; 274, integer:3; 275, identifier:msv; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 280; 279, identifier:echo; 280, argument_list; 280, 281; 280, 299; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, string:'{:7.2f}{}'; 284, identifier:format; 285, argument_list; 285, 286; 285, 293; 286, binary_operator:/; 286, 287; 286, 288; 287, identifier:c; 288, attribute; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:ase; 291, identifier:units; 292, identifier:GPa; 293, conditional_expression:if; 293, 294; 293, 295; 293, 298; 294, string:'* '; 295, comparison_operator:<; 295, 296; 295, 297; 296, identifier:sv; 297, identifier:eps; 298, string:' '; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:nl; 301, False; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 305; 304, identifier:echo; 305, argument_list; 306, elif_clause; 306, 307; 306, 310; 307, comparison_operator:==; 307, 308; 307, 309; 308, identifier:action; 309, string:'eos'; 310, block; 310, 311; 310, 329; 310, 339; 310, 352; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:eos; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:elastic; 317, identifier:get_BM_EOS; 318, argument_list; 318, 319; 318, 322; 319, subscript; 319, 320; 319, 321; 320, identifier:systems; 321, integer:0; 322, keyword_argument; 322, 323; 322, 324; 323, identifier:systems; 324, subscript; 324, 325; 324, 326; 325, identifier:systems; 326, slice; 326, 327; 326, 328; 327, integer:1; 328, colon; 329, expression_statement; 329, 330; 330, augmented_assignment:/=; 330, 331; 330, 334; 331, subscript; 331, 332; 331, 333; 332, identifier:eos; 333, integer:1; 334, attribute; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:ase; 337, identifier:units; 338, identifier:GPa; 339, if_statement; 339, 340; 339, 341; 340, identifier:verbose; 341, block; 341, 342; 342, expression_statement; 342, 343; 343, call; 343, 344; 343, 345; 344, identifier:echo; 345, argument_list; 345, 346; 346, binary_operator:%; 346, 347; 346, 348; 347, string:'# %7s (A^3) %7s (GPa) %7s'; 348, tuple; 348, 349; 348, 350; 348, 351; 349, string:"V0"; 350, string:"B0"; 351, string:"B0'"; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 355; 354, identifier:echo; 355, argument_list; 355, 356; 356, binary_operator:%; 356, 357; 356, 358; 357, string:' %7.2f %7.2f %7.2f'; 358, call; 358, 359; 358, 360; 359, identifier:tuple; 360, argument_list; 360, 361; 361, identifier:eos
def proc(ctx, files): '''Process calculated structures''' def calc_reader(fn, verb): if verb: echo('Reading: {:<60s}\r'.format(fn), nl=False, err=True) return ase.io.read(fn) action = ctx.parent.params['action'] systems = [calc_reader(calc, verbose) for calc in files] if verbose : echo('', err=True) if action == 'cij': cij = elastic.get_elastic_tensor(systems[0], systems=systems[1:]) msv = cij[1][3].max() eps = 1e-4 if verbose: echo('Cij solution\n'+30*'-') echo(' Solution rank: {:2d}{}'.format( cij[1][2], ' (undetermined)' if cij[1][2] < len(cij[0]) else '')) if cij[1][2] == len(cij[0]): echo(' Square of residuals: {:7.2g}'.format(cij[1][1])) echo(' Relative singular values:') for sv in cij[1][3]/msv: echo('{:7.4f}{}'.format( sv, '* ' if (sv) < eps else ' '), nl=False) echo('\n\nElastic tensor (GPa):') for dsc in elastic.elastic.get_cij_order(systems[0]): echo('{: >7s} '.format(dsc), nl=False) echo('\n'+30*'-') for c, sv in zip(cij[0], cij[1][3]/msv): echo('{:7.2f}{}'.format( c/ase.units.GPa, '* ' if sv < eps else ' '), nl=False) echo() elif action == 'eos': eos = elastic.get_BM_EOS(systems[0], systems=systems[1:]) eos[1] /= ase.units.GPa if verbose: echo('# %7s (A^3) %7s (GPa) %7s' % ("V0", "B0", "B0'")) echo(' %7.2f %7.2f %7.2f' % tuple(eos))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:sample; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:self; 5, identifier:bqm; 6, default_parameter; 6, 7; 6, 8; 7, identifier:init_solution; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:tenure; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:scale_factor; 14, integer:1; 15, default_parameter; 15, 16; 15, 17; 16, identifier:timeout; 17, integer:20; 18, default_parameter; 18, 19; 18, 20; 19, identifier:num_reads; 20, integer:1; 21, block; 21, 22; 21, 24; 21, 25; 21, 26; 21, 117; 21, 138; 21, 160; 21, 173; 21, 188; 21, 201; 21, 211; 21, 222; 21, 223; 21, 227; 21, 231; 21, 317; 21, 336; 21, 348; 22, expression_statement; 22, 23; 23, comment; 24, comment; 25, comment; 26, if_statement; 26, 27; 26, 30; 26, 111; 27, comparison_operator:is; 27, 28; 27, 29; 28, identifier:init_solution; 29, None; 30, block; 30, 31; 30, 46; 30, 61; 30, 83; 31, if_statement; 31, 32; 31, 40; 32, not_operator; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:isinstance; 35, argument_list; 35, 36; 35, 37; 36, identifier:init_solution; 37, attribute; 37, 38; 37, 39; 38, identifier:dimod; 39, identifier:SampleSet; 40, block; 40, 41; 41, raise_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:TypeError; 44, argument_list; 44, 45; 45, string:"'init_solution' should be a 'dimod.SampleSet' instance"; 46, if_statement; 46, 47; 46, 55; 47, comparison_operator:<; 47, 48; 47, 54; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:init_solution; 53, identifier:record; 54, integer:1; 55, block; 55, 56; 56, raise_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:ValueError; 59, argument_list; 59, 60; 60, string:"'init_solution' should contain at least one sample"; 61, if_statement; 61, 62; 61, 77; 62, comparison_operator:!=; 62, 63; 62, 73; 63, call; 63, 64; 63, 65; 64, identifier:len; 65, argument_list; 65, 66; 66, attribute; 66, 67; 66, 72; 67, subscript; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:init_solution; 70, identifier:record; 71, integer:0; 72, identifier:sample; 73, call; 73, 74; 73, 75; 74, identifier:len; 75, argument_list; 75, 76; 76, identifier:bqm; 77, block; 77, 78; 78, raise_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:ValueError; 81, argument_list; 81, 82; 82, string:"'init_solution' sample dimension different from BQM"; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:init_sample; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:_bqm_sample_to_tabu_sample; 90, argument_list; 90, 91; 90, 108; 91, attribute; 91, 92; 91, 107; 92, subscript; 92, 93; 92, 106; 93, attribute; 93, 94; 93, 105; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:init_solution; 97, identifier:change_vartype; 98, argument_list; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:dimod; 101, identifier:BINARY; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:inplace; 104, False; 105, identifier:record; 106, integer:0; 107, identifier:sample; 108, attribute; 108, 109; 108, 110; 109, identifier:bqm; 110, identifier:binary; 111, else_clause; 111, 112; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:init_sample; 116, None; 117, if_statement; 117, 118; 117, 120; 118, not_operator; 118, 119; 119, identifier:bqm; 120, block; 120, 121; 121, return_statement; 121, 122; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:dimod; 126, identifier:SampleSet; 127, identifier:from_samples; 128, argument_list; 128, 129; 128, 130; 128, 133; 129, list:[]; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:energy; 132, integer:0; 133, keyword_argument; 133, 134; 133, 135; 134, identifier:vartype; 135, attribute; 135, 136; 135, 137; 136, identifier:bqm; 137, identifier:vartype; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:tenure; 141, None; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:tenure; 146, call; 146, 147; 146, 148; 147, identifier:max; 148, argument_list; 148, 149; 148, 159; 149, call; 149, 150; 149, 151; 150, identifier:min; 151, argument_list; 151, 152; 151, 153; 152, integer:20; 153, binary_operator://; 153, 154; 153, 158; 154, call; 154, 155; 154, 156; 155, identifier:len; 156, argument_list; 156, 157; 157, identifier:bqm; 158, integer:4; 159, integer:0; 160, if_statement; 160, 161; 160, 167; 161, not_operator; 161, 162; 162, call; 162, 163; 162, 164; 163, identifier:isinstance; 164, argument_list; 164, 165; 164, 166; 165, identifier:tenure; 166, identifier:int; 167, block; 167, 168; 168, raise_statement; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:TypeError; 171, argument_list; 171, 172; 172, string:"'tenure' should be an integer in range [0, num_vars - 1]"; 173, if_statement; 173, 174; 173, 182; 174, not_operator; 174, 175; 175, comparison_operator:<=; 175, 176; 175, 177; 175, 178; 176, integer:0; 177, identifier:tenure; 178, call; 178, 179; 178, 180; 179, identifier:len; 180, argument_list; 180, 181; 181, identifier:bqm; 182, block; 182, 183; 183, raise_statement; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:ValueError; 186, argument_list; 186, 187; 187, string:"'tenure' should be an integer in range [0, num_vars - 1]"; 188, if_statement; 188, 189; 188, 195; 189, not_operator; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:isinstance; 192, argument_list; 192, 193; 192, 194; 193, identifier:num_reads; 194, identifier:int; 195, block; 195, 196; 196, raise_statement; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:TypeError; 199, argument_list; 199, 200; 200, string:"'num_reads' should be a positive integer"; 201, if_statement; 201, 202; 201, 205; 202, comparison_operator:<; 202, 203; 202, 204; 203, identifier:num_reads; 204, integer:1; 205, block; 205, 206; 206, raise_statement; 206, 207; 207, call; 207, 208; 207, 209; 208, identifier:ValueError; 209, argument_list; 209, 210; 210, string:"'num_reads' should be a positive integer"; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:qubo; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:_bqm_to_tabu_qubo; 218, argument_list; 218, 219; 219, attribute; 219, 220; 219, 221; 220, identifier:bqm; 221, identifier:binary; 222, comment; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:samples; 226, list:[]; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:energies; 230, list:[]; 231, for_statement; 231, 232; 231, 233; 231, 237; 232, identifier:_; 233, call; 233, 234; 233, 235; 234, identifier:range; 235, argument_list; 235, 236; 236, identifier:num_reads; 237, block; 237, 238; 237, 262; 237, 273; 237, 292; 237, 303; 237, 310; 238, if_statement; 238, 239; 238, 242; 239, comparison_operator:is; 239, 240; 239, 241; 240, identifier:init_sample; 241, None; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:init_sample; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:self; 249, identifier:_bqm_sample_to_tabu_sample; 250, argument_list; 250, 251; 250, 259; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:_random_sample; 255, argument_list; 255, 256; 256, attribute; 256, 257; 256, 258; 257, identifier:bqm; 258, identifier:binary; 259, attribute; 259, 260; 259, 261; 260, identifier:bqm; 261, identifier:binary; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:r; 265, call; 265, 266; 265, 267; 266, identifier:TabuSearch; 267, argument_list; 267, 268; 267, 269; 267, 270; 267, 271; 267, 272; 268, identifier:qubo; 269, identifier:init_sample; 270, identifier:tenure; 271, identifier:scale_factor; 272, identifier:timeout; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:sample; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:self; 279, identifier:_tabu_sample_to_bqm_sample; 280, argument_list; 280, 281; 280, 289; 281, call; 281, 282; 281, 283; 282, identifier:list; 283, argument_list; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:r; 287, identifier:bestSolution; 288, argument_list; 289, attribute; 289, 290; 289, 291; 290, identifier:bqm; 291, identifier:binary; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:energy; 295, call; 295, 296; 295, 301; 296, attribute; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:bqm; 299, identifier:binary; 300, identifier:energy; 301, argument_list; 301, 302; 302, identifier:sample; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:samples; 307, identifier:append; 308, argument_list; 308, 309; 309, identifier:sample; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:energies; 314, identifier:append; 315, argument_list; 315, 316; 316, identifier:energy; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:response; 320, call; 320, 321; 320, 326; 321, attribute; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:dimod; 324, identifier:SampleSet; 325, identifier:from_samples; 326, argument_list; 326, 327; 326, 328; 326, 331; 327, identifier:samples; 328, keyword_argument; 328, 329; 328, 330; 329, identifier:energy; 330, identifier:energies; 331, keyword_argument; 331, 332; 331, 333; 332, identifier:vartype; 333, attribute; 333, 334; 333, 335; 334, identifier:dimod; 335, identifier:BINARY; 336, expression_statement; 336, 337; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:response; 340, identifier:change_vartype; 341, argument_list; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:bqm; 344, identifier:vartype; 345, keyword_argument; 345, 346; 345, 347; 346, identifier:inplace; 347, True; 348, return_statement; 348, 349; 349, identifier:response
def sample(self, bqm, init_solution=None, tenure=None, scale_factor=1, timeout=20, num_reads=1): """Run a tabu search on a given binary quadratic model. Args: bqm (:obj:`~dimod.BinaryQuadraticModel`): The binary quadratic model (BQM) to be sampled. init_solution (:obj:`~dimod.SampleSet`, optional): Single sample that sets an initial state for all the problem variables. Default is a random initial state. tenure (int, optional): Tabu tenure, which is the length of the tabu list, or number of recently explored solutions kept in memory. Default is a quarter of the number of problem variables up to a maximum value of 20. scale_factor (number, optional): Scaling factor for linear and quadratic biases in the BQM. Internally, the BQM is converted to a QUBO matrix, and elements are stored as long ints using ``internal_q = long int (q * scale_factor)``. timeout (int, optional): Total running time in milliseconds. num_reads (int, optional): Number of reads. Each run of the tabu algorithm generates a sample. Returns: :obj:`~dimod.SampleSet`: A `dimod` :obj:`.~dimod.SampleSet` object. Examples: This example provides samples for a two-variable QUBO model. >>> from tabu import TabuSampler >>> import dimod >>> sampler = TabuSampler() >>> Q = {(0, 0): -1, (1, 1): -1, (0, 1): 2} >>> bqm = dimod.BinaryQuadraticModel.from_qubo(Q, offset=0.0) >>> samples = sampler.sample(bqm) >>> samples.record[0].energy -1.0 """ # input checking and defaults calculation # TODO: one "read" per sample in init_solution sampleset if init_solution is not None: if not isinstance(init_solution, dimod.SampleSet): raise TypeError("'init_solution' should be a 'dimod.SampleSet' instance") if len(init_solution.record) < 1: raise ValueError("'init_solution' should contain at least one sample") if len(init_solution.record[0].sample) != len(bqm): raise ValueError("'init_solution' sample dimension different from BQM") init_sample = self._bqm_sample_to_tabu_sample( init_solution.change_vartype(dimod.BINARY, inplace=False).record[0].sample, bqm.binary) else: init_sample = None if not bqm: return dimod.SampleSet.from_samples([], energy=0, vartype=bqm.vartype) if tenure is None: tenure = max(min(20, len(bqm) // 4), 0) if not isinstance(tenure, int): raise TypeError("'tenure' should be an integer in range [0, num_vars - 1]") if not 0 <= tenure < len(bqm): raise ValueError("'tenure' should be an integer in range [0, num_vars - 1]") if not isinstance(num_reads, int): raise TypeError("'num_reads' should be a positive integer") if num_reads < 1: raise ValueError("'num_reads' should be a positive integer") qubo = self._bqm_to_tabu_qubo(bqm.binary) # run Tabu search samples = [] energies = [] for _ in range(num_reads): if init_sample is None: init_sample = self._bqm_sample_to_tabu_sample(self._random_sample(bqm.binary), bqm.binary) r = TabuSearch(qubo, init_sample, tenure, scale_factor, timeout) sample = self._tabu_sample_to_bqm_sample(list(r.bestSolution()), bqm.binary) energy = bqm.binary.energy(sample) samples.append(sample) energies.append(energy) response = dimod.SampleSet.from_samples( samples, energy=energies, vartype=dimod.BINARY) response.change_vartype(bqm.vartype, inplace=True) return response
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:mmi_to_raster; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:force_flag; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:algorithm; 10, identifier:USE_ASCII; 11, block; 11, 12; 11, 14; 11, 21; 11, 30; 11, 75; 11, 76; 11, 92; 11, 262; 11, 263; 11, 270; 11, 271; 11, 316; 11, 324; 11, 332; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:LOGGER; 18, identifier:debug; 19, argument_list; 19, 20; 20, string:'mmi_to_raster requested.'; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:algorithm; 24, None; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:algorithm; 29, identifier:USE_ASCII; 30, if_statement; 30, 31; 30, 34; 30, 55; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:algorithm_name; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:tif_path; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:os; 42, identifier:path; 43, identifier:join; 44, argument_list; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:output_dir; 48, binary_operator:%; 48, 49; 48, 50; 49, string:'%s-%s.tif'; 50, tuple; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:output_basename; 54, identifier:algorithm; 55, else_clause; 55, 56; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:tif_path; 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:join; 66, argument_list; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:output_dir; 70, binary_operator:%; 70, 71; 70, 72; 71, string:'%s.tif'; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:output_basename; 75, comment; 76, if_statement; 76, 77; 76, 89; 77, boolean_operator:and; 77, 78; 77, 86; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:os; 82, identifier:path; 83, identifier:exists; 84, argument_list; 84, 85; 85, identifier:tif_path; 86, comparison_operator:is; 86, 87; 86, 88; 87, identifier:force_flag; 88, True; 89, block; 89, 90; 90, return_statement; 90, 91; 91, identifier:tif_path; 92, if_statement; 92, 93; 92, 96; 92, 97; 92, 149; 93, comparison_operator:==; 93, 94; 93, 95; 94, identifier:algorithm; 95, identifier:USE_ASCII; 96, comment; 97, block; 97, 98; 97, 107; 97, 108; 97, 132; 97, 141; 97, 142; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:ascii_path; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:mmi_to_ascii; 105, argument_list; 105, 106; 106, True; 107, comment; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:command; 111, parenthesized_expression; 111, 112; 112, binary_operator:%; 112, 113; 112, 117; 113, parenthesized_expression; 113, 114; 114, concatenated_string; 114, 115; 114, 116; 115, string:'%(gdal_translate)s -a_srs EPSG:4326 '; 116, string:'"%(ascii)s" "%(tif)s"'; 117, dictionary; 117, 118; 117, 126; 117, 129; 118, pair; 118, 119; 118, 120; 119, string:'gdal_translate'; 120, subscript; 120, 121; 120, 125; 121, call; 121, 122; 121, 123; 122, identifier:which; 123, argument_list; 123, 124; 124, string:'gdal_translate'; 125, integer:0; 126, pair; 126, 127; 126, 128; 127, string:'ascii'; 128, identifier:ascii_path; 129, pair; 129, 130; 129, 131; 130, string:'tif'; 131, identifier:tif_path; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:LOGGER; 136, identifier:info; 137, argument_list; 137, 138; 138, binary_operator:%; 138, 139; 138, 140; 139, string:'Created this gdal command:\n%s'; 140, identifier:command; 141, comment; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:_run_command; 147, argument_list; 147, 148; 148, identifier:command; 149, else_clause; 149, 150; 149, 151; 149, 152; 150, comment; 151, comment; 152, block; 152, 153; 152, 162; 152, 163; 152, 164; 152, 165; 152, 174; 152, 233; 152, 242; 152, 243; 152, 250; 152, 251; 152, 252; 152, 253; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:vrt_path; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:mmi_to_vrt; 160, argument_list; 160, 161; 161, identifier:force_flag; 162, comment; 163, comment; 164, comment; 165, if_statement; 165, 166; 165, 169; 166, comparison_operator:in; 166, 167; 166, 168; 167, identifier:INVDIST; 168, identifier:algorithm; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:algorithm; 173, string:'invdist:power=2.0:smoothing=1.0'; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:command; 177, parenthesized_expression; 177, 178; 178, binary_operator:%; 178, 179; 178, 185; 179, parenthesized_expression; 179, 180; 180, concatenated_string; 180, 181; 180, 182; 180, 183; 180, 184; 181, string:'%(gdal_grid)s -a %(alg)s -zfield "mmi" -txe %(xMin)s '; 182, string:'%(xMax)s -tye %(yMin)s %(yMax)s -outsize %(dimX)i '; 183, string:'%(dimY)i -of GTiff -ot Float16 -a_srs EPSG:4326 -l mmi '; 184, string:'"%(vrt)s" "%(tif)s"'; 185, dictionary; 185, 186; 185, 194; 185, 197; 185, 202; 185, 207; 185, 212; 185, 217; 185, 222; 185, 227; 185, 230; 186, pair; 186, 187; 186, 188; 187, string:'gdal_grid'; 188, subscript; 188, 189; 188, 193; 189, call; 189, 190; 189, 191; 190, identifier:which; 191, argument_list; 191, 192; 192, string:'gdal_grid'; 193, integer:0; 194, pair; 194, 195; 194, 196; 195, string:'alg'; 196, identifier:algorithm; 197, pair; 197, 198; 197, 199; 198, string:'xMin'; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:x_minimum; 202, pair; 202, 203; 202, 204; 203, string:'xMax'; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:x_maximum; 207, pair; 207, 208; 207, 209; 208, string:'yMin'; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:y_minimum; 212, pair; 212, 213; 212, 214; 213, string:'yMax'; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:y_maximum; 217, pair; 217, 218; 217, 219; 218, string:'dimX'; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:columns; 222, pair; 222, 223; 222, 224; 223, string:'dimY'; 224, attribute; 224, 225; 224, 226; 225, identifier:self; 226, identifier:rows; 227, pair; 227, 228; 227, 229; 228, string:'vrt'; 229, identifier:vrt_path; 230, pair; 230, 231; 230, 232; 231, string:'tif'; 232, identifier:tif_path; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:LOGGER; 237, identifier:info; 238, argument_list; 238, 239; 239, binary_operator:%; 239, 240; 239, 241; 240, string:'Created this gdal command:\n%s'; 241, identifier:command; 242, comment; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:self; 247, identifier:_run_command; 248, argument_list; 248, 249; 249, identifier:command; 250, comment; 251, comment; 252, comment; 253, if_statement; 253, 254; 253, 257; 254, comparison_operator:in; 254, 255; 254, 256; 255, identifier:INVDIST; 256, identifier:algorithm; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:algorithm; 261, string:'invdist'; 262, comment; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:self; 267, identifier:create_keyword_file; 268, argument_list; 268, 269; 269, identifier:algorithm; 270, comment; 271, if_statement; 271, 272; 271, 275; 271, 296; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:algorithm_name; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:qml_path; 279, call; 279, 280; 279, 285; 280, attribute; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:os; 283, identifier:path; 284, identifier:join; 285, argument_list; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:self; 288, identifier:output_dir; 289, binary_operator:%; 289, 290; 289, 291; 290, string:'%s-%s.qml'; 291, tuple; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:self; 294, identifier:output_basename; 295, identifier:algorithm; 296, else_clause; 296, 297; 297, block; 297, 298; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:qml_path; 301, call; 301, 302; 301, 307; 302, attribute; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:os; 305, identifier:path; 306, identifier:join; 307, argument_list; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:output_dir; 311, binary_operator:%; 311, 312; 311, 313; 312, string:'%s.qml'; 313, attribute; 313, 314; 313, 315; 314, identifier:self; 315, identifier:output_basename; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:qml_source_path; 319, call; 319, 320; 319, 321; 320, identifier:resources_path; 321, argument_list; 321, 322; 321, 323; 322, string:'converter_data'; 323, string:'mmi.qml'; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:shutil; 328, identifier:copyfile; 329, argument_list; 329, 330; 329, 331; 330, identifier:qml_source_path; 331, identifier:qml_path; 332, return_statement; 332, 333; 333, identifier:tif_path
def mmi_to_raster(self, force_flag=False, algorithm=USE_ASCII): """Convert the grid.xml's mmi column to a raster using gdal_grid. A geotiff file will be created. Unfortunately no python bindings exist for doing this so we are going to do it using a shell call. .. see also:: http://www.gdal.org/gdal_grid.html Example of the gdal_grid call we generate:: gdal_grid -zfield "mmi" -a invdist:power=2.0:smoothing=1.0 \ -txe 126.29 130.29 -tye 0.802 4.798 -outsize 400 400 -of GTiff \ -ot Float16 -l mmi mmi.vrt mmi.tif .. note:: It is assumed that gdal_grid is in your path. :param force_flag: Whether to force the regeneration of the output file. Defaults to False. :type force_flag: bool :param algorithm: Which re-sampling algorithm to use. valid options are 'nearest' (for nearest neighbour), 'invdist' (for inverse distance), 'average' (for moving average). Defaults to 'nearest' if not specified. Note that passing re-sampling alg parameters is currently not supported. If None is passed it will be replaced with 'use_ascii'. 'use_ascii' algorithm will convert the mmi grid to ascii file then convert it to raster using gdal_translate. :type algorithm: str :returns: Path to the resulting tif file. :rtype: str .. note:: For interest you can also make quite beautiful smoothed raster using this: gdal_grid -zfield "mmi" -a_srs EPSG:4326 -a invdist:power=2.0:smoothing=1.0 -txe 122.45 126.45 -tye -2.21 1.79 -outsize 400 400 -of GTiff -ot Float16 -l mmi mmi.vrt mmi-trippy.tif """ LOGGER.debug('mmi_to_raster requested.') if algorithm is None: algorithm = USE_ASCII if self.algorithm_name: tif_path = os.path.join( self.output_dir, '%s-%s.tif' % ( self.output_basename, algorithm)) else: tif_path = os.path.join( self.output_dir, '%s.tif' % self.output_basename) # short circuit if the tif is already created. if os.path.exists(tif_path) and force_flag is not True: return tif_path if algorithm == USE_ASCII: # Convert to ascii ascii_path = self.mmi_to_ascii(True) # Creating command to convert to tif command = ( ( '%(gdal_translate)s -a_srs EPSG:4326 ' '"%(ascii)s" "%(tif)s"' ) % { 'gdal_translate': which('gdal_translate')[0], 'ascii': ascii_path, 'tif': tif_path } ) LOGGER.info('Created this gdal command:\n%s' % command) # Now run GDAL warp scottie... self._run_command(command) else: # Ensure the vrt mmi file exists (it will generate csv too if # needed) vrt_path = self.mmi_to_vrt(force_flag) # now generate the tif using default nearest neighbour # interpolation options. This gives us the same output as the # mmi.grd generated by the earthquake server. if INVDIST in algorithm: algorithm = 'invdist:power=2.0:smoothing=1.0' command = ( ( '%(gdal_grid)s -a %(alg)s -zfield "mmi" -txe %(xMin)s ' '%(xMax)s -tye %(yMin)s %(yMax)s -outsize %(dimX)i ' '%(dimY)i -of GTiff -ot Float16 -a_srs EPSG:4326 -l mmi ' '"%(vrt)s" "%(tif)s"' ) % { 'gdal_grid': which('gdal_grid')[0], 'alg': algorithm, 'xMin': self.x_minimum, 'xMax': self.x_maximum, 'yMin': self.y_minimum, 'yMax': self.y_maximum, 'dimX': self.columns, 'dimY': self.rows, 'vrt': vrt_path, 'tif': tif_path } ) LOGGER.info('Created this gdal command:\n%s' % command) # Now run GDAL warp scottie... self._run_command(command) # We will use keywords file name with simple algorithm name since # it will raise an error in windows related to having double # colon in path if INVDIST in algorithm: algorithm = 'invdist' # copy the keywords file from fixtures for this layer self.create_keyword_file(algorithm) # Lastly copy over the standard qml (QGIS Style file) for the mmi.tif if self.algorithm_name: qml_path = os.path.join( self.output_dir, '%s-%s.qml' % ( self.output_basename, algorithm)) else: qml_path = os.path.join( self.output_dir, '%s.qml' % self.output_basename) qml_source_path = resources_path('converter_data', 'mmi.qml') shutil.copyfile(qml_source_path, qml_path) return tif_path
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:create_keyword_file; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:algorithm; 6, block; 6, 7; 6, 9; 6, 15; 6, 16; 6, 23; 6, 38; 6, 45; 6, 60; 6, 64; 6, 94; 6, 194; 6, 215; 6, 216; 6, 220; 6, 245; 6, 256; 6, 307; 6, 352; 6, 353; 6, 381; 6, 387; 6, 395; 6, 407; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:keyword_io; 12, call; 12, 13; 12, 14; 13, identifier:KeywordIO; 14, argument_list; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:mmi_default_classes; 19, call; 19, 20; 19, 21; 20, identifier:default_classification_thresholds; 21, argument_list; 21, 22; 22, identifier:earthquake_mmi_scale; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:mmi_default_threshold; 26, dictionary; 26, 27; 27, pair; 27, 28; 27, 31; 28, subscript; 28, 29; 28, 30; 29, identifier:earthquake_mmi_scale; 30, string:'key'; 31, dictionary; 31, 32; 31, 35; 32, pair; 32, 33; 32, 34; 33, string:'active'; 34, True; 35, pair; 35, 36; 35, 37; 36, string:'classes'; 37, identifier:mmi_default_classes; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:generic_default_classes; 41, call; 41, 42; 41, 43; 42, identifier:default_classification_thresholds; 43, argument_list; 43, 44; 44, identifier:generic_hazard_classes; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:generic_default_threshold; 48, dictionary; 48, 49; 49, pair; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:generic_hazard_classes; 52, string:'key'; 53, dictionary; 53, 54; 53, 57; 54, pair; 54, 55; 54, 56; 55, string:'active'; 56, True; 57, pair; 57, 58; 57, 59; 58, string:'classes'; 59, identifier:generic_default_classes; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:threshold_keyword; 63, dictionary; 64, for_statement; 64, 65; 64, 66; 64, 67; 64, 68; 65, identifier:exposure; 66, identifier:exposure_all; 67, comment; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 75; 69, 84; 70, comparison_operator:in; 70, 71; 70, 72; 71, identifier:exposure; 72, subscript; 72, 73; 72, 74; 73, identifier:earthquake_mmi_scale; 74, string:'exposures'; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 83; 78, subscript; 78, 79; 78, 80; 79, identifier:threshold_keyword; 80, subscript; 80, 81; 80, 82; 81, identifier:exposure; 82, string:'key'; 83, identifier:mmi_default_threshold; 84, else_clause; 84, 85; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 93; 88, subscript; 88, 89; 88, 90; 89, identifier:threshold_keyword; 90, subscript; 90, 91; 90, 92; 91, identifier:exposure; 92, string:'key'; 93, identifier:generic_default_threshold; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:extra_keywords; 97, dictionary; 97, 98; 97, 105; 97, 112; 97, 119; 97, 126; 97, 133; 97, 140; 97, 152; 97, 159; 97, 166; 97, 173; 97, 180; 97, 187; 98, pair; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:extra_keyword_earthquake_latitude; 101, string:'key'; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:latitude; 105, pair; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:extra_keyword_earthquake_longitude; 108, string:'key'; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:longitude; 112, pair; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:extra_keyword_earthquake_magnitude; 115, string:'key'; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:magnitude; 119, pair; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:extra_keyword_earthquake_depth; 122, string:'key'; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:depth; 126, pair; 126, 127; 126, 130; 127, subscript; 127, 128; 127, 129; 128, identifier:extra_keyword_earthquake_description; 129, string:'key'; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:description; 133, pair; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:extra_keyword_earthquake_location; 136, string:'key'; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:location; 140, pair; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:extra_keyword_earthquake_event_time; 143, string:'key'; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:time; 149, identifier:strftime; 150, argument_list; 150, 151; 151, string:'%Y-%m-%dT%H:%M:%S'; 152, pair; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:extra_keyword_time_zone; 155, string:'key'; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:time_zone; 159, pair; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:extra_keyword_earthquake_x_minimum; 162, string:'key'; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:x_minimum; 166, pair; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:extra_keyword_earthquake_x_maximum; 169, string:'key'; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:x_maximum; 173, pair; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:extra_keyword_earthquake_y_minimum; 176, string:'key'; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:y_minimum; 180, pair; 180, 181; 180, 184; 181, subscript; 181, 182; 181, 183; 182, identifier:extra_keyword_earthquake_y_maximum; 183, string:'key'; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:y_maximum; 187, pair; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:extra_keyword_earthquake_event_id; 190, string:'key'; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:event_id; 194, for_statement; 194, 195; 194, 198; 194, 208; 195, pattern_list; 195, 196; 195, 197; 196, identifier:key; 197, identifier:value; 198, call; 198, 199; 198, 200; 199, identifier:list; 200, argument_list; 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:extra_keywords; 206, identifier:items; 207, argument_list; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:extra_keywords; 213, identifier:key; 214, identifier:value; 215, comment; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:empty_keys; 219, list:[]; 220, for_statement; 220, 221; 220, 224; 220, 232; 221, pattern_list; 221, 222; 221, 223; 222, identifier:key; 223, identifier:value; 224, call; 224, 225; 224, 226; 225, identifier:list; 226, argument_list; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:extra_keywords; 230, identifier:items; 231, argument_list; 232, block; 232, 233; 233, if_statement; 233, 234; 233, 237; 234, comparison_operator:is; 234, 235; 234, 236; 235, identifier:value; 236, None; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:empty_keys; 242, identifier:append; 243, argument_list; 243, 244; 244, identifier:key; 245, for_statement; 245, 246; 245, 247; 245, 248; 246, identifier:empty_key; 247, identifier:empty_keys; 248, block; 248, 249; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:extra_keywords; 253, identifier:pop; 254, argument_list; 254, 255; 255, identifier:empty_key; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:keywords; 259, dictionary; 259, 260; 259, 265; 259, 270; 259, 273; 259, 278; 259, 283; 259, 288; 259, 293; 259, 298; 259, 301; 259, 304; 260, pair; 260, 261; 260, 262; 261, string:'hazard'; 262, subscript; 262, 263; 262, 264; 263, identifier:hazard_earthquake; 264, string:'key'; 265, pair; 265, 266; 265, 267; 266, string:'hazard_category'; 267, subscript; 267, 268; 267, 269; 268, identifier:hazard_category_single_event; 269, string:'key'; 270, pair; 270, 271; 270, 272; 271, string:'keyword_version'; 272, identifier:inasafe_keyword_version; 273, pair; 273, 274; 273, 275; 274, string:'layer_geometry'; 275, subscript; 275, 276; 275, 277; 276, identifier:layer_geometry_raster; 277, string:'key'; 278, pair; 278, 279; 278, 280; 279, string:'layer_mode'; 280, subscript; 280, 281; 280, 282; 281, identifier:layer_mode_continuous; 282, string:'key'; 283, pair; 283, 284; 283, 285; 284, string:'layer_purpose'; 285, subscript; 285, 286; 285, 287; 286, identifier:layer_purpose_hazard; 287, string:'key'; 288, pair; 288, 289; 288, 290; 289, string:'continuous_hazard_unit'; 290, subscript; 290, 291; 290, 292; 291, identifier:unit_mmi; 292, string:'key'; 293, pair; 293, 294; 293, 295; 294, string:'classification'; 295, subscript; 295, 296; 295, 297; 296, identifier:earthquake_mmi_scale; 297, string:'key'; 298, pair; 298, 299; 298, 300; 299, string:'thresholds'; 300, identifier:threshold_keyword; 301, pair; 301, 302; 301, 303; 302, string:'extra_keywords'; 303, identifier:extra_keywords; 304, pair; 304, 305; 304, 306; 305, string:'active_band'; 306, integer:1; 307, if_statement; 307, 308; 307, 311; 307, 332; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:algorithm_name; 311, block; 311, 312; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:layer_path; 315, call; 315, 316; 315, 321; 316, attribute; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:os; 319, identifier:path; 320, identifier:join; 321, argument_list; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:self; 324, identifier:output_dir; 325, binary_operator:%; 325, 326; 325, 327; 326, string:'%s-%s.tif'; 327, tuple; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:self; 330, identifier:output_basename; 331, identifier:algorithm; 332, else_clause; 332, 333; 333, block; 333, 334; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 337; 336, identifier:layer_path; 337, call; 337, 338; 337, 343; 338, attribute; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:os; 341, identifier:path; 342, identifier:join; 343, argument_list; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:self; 346, identifier:output_dir; 347, binary_operator:%; 347, 348; 347, 349; 348, string:'%s.tif'; 349, attribute; 349, 350; 349, 351; 350, identifier:self; 351, identifier:output_basename; 352, comment; 353, if_statement; 353, 354; 353, 366; 353, 373; 354, comparison_operator:==; 354, 355; 354, 365; 355, call; 355, 356; 355, 357; 356, identifier:len; 357, argument_list; 357, 358; 358, call; 358, 359; 358, 364; 359, attribute; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:self; 362, identifier:title; 363, identifier:strip; 364, argument_list; 365, integer:0; 366, block; 366, 367; 367, expression_statement; 367, 368; 368, assignment; 368, 369; 368, 370; 369, identifier:keyword_title; 370, attribute; 370, 371; 370, 372; 371, identifier:self; 372, identifier:output_basename; 373, else_clause; 373, 374; 374, block; 374, 375; 375, expression_statement; 375, 376; 376, assignment; 376, 377; 376, 378; 377, identifier:keyword_title; 378, attribute; 378, 379; 378, 380; 379, identifier:self; 380, identifier:title; 381, expression_statement; 381, 382; 382, assignment; 382, 383; 382, 386; 383, subscript; 383, 384; 383, 385; 384, identifier:keywords; 385, string:'title'; 386, identifier:keyword_title; 387, expression_statement; 387, 388; 388, assignment; 388, 389; 388, 390; 389, identifier:hazard_layer; 390, call; 390, 391; 390, 392; 391, identifier:QgsRasterLayer; 392, argument_list; 392, 393; 392, 394; 393, identifier:layer_path; 394, identifier:keyword_title; 395, if_statement; 395, 396; 395, 402; 396, not_operator; 396, 397; 397, call; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, identifier:hazard_layer; 400, identifier:isValid; 401, argument_list; 402, block; 402, 403; 403, raise_statement; 403, 404; 404, call; 404, 405; 404, 406; 405, identifier:InvalidLayerError; 406, argument_list; 407, expression_statement; 407, 408; 408, call; 408, 409; 408, 412; 409, attribute; 409, 410; 409, 411; 410, identifier:keyword_io; 411, identifier:write_keywords; 412, argument_list; 412, 413; 412, 414; 413, identifier:hazard_layer; 414, identifier:keywords
def create_keyword_file(self, algorithm): """Create keyword file for the raster file created. Basically copy a template from keyword file in converter data and add extra keyword (usually a title) :param algorithm: Which re-sampling algorithm to use. valid options are 'nearest' (for nearest neighbour), 'invdist' (for inverse distance), 'average' (for moving average). Defaults to 'nearest' if not specified. Note that passing re-sampling alg parameters is currently not supported. If None is passed it will be replaced with 'nearest'. :type algorithm: str """ keyword_io = KeywordIO() # Set thresholds for each exposure mmi_default_classes = default_classification_thresholds( earthquake_mmi_scale ) mmi_default_threshold = { earthquake_mmi_scale['key']: { 'active': True, 'classes': mmi_default_classes } } generic_default_classes = default_classification_thresholds( generic_hazard_classes ) generic_default_threshold = { generic_hazard_classes['key']: { 'active': True, 'classes': generic_default_classes } } threshold_keyword = {} for exposure in exposure_all: # Not all exposure is supported by earthquake_mmi_scale if exposure in earthquake_mmi_scale['exposures']: threshold_keyword[exposure['key']] = mmi_default_threshold else: threshold_keyword[ exposure['key']] = generic_default_threshold extra_keywords = { extra_keyword_earthquake_latitude['key']: self.latitude, extra_keyword_earthquake_longitude['key']: self.longitude, extra_keyword_earthquake_magnitude['key']: self.magnitude, extra_keyword_earthquake_depth['key']: self.depth, extra_keyword_earthquake_description['key']: self.description, extra_keyword_earthquake_location['key']: self.location, extra_keyword_earthquake_event_time['key']: self.time.strftime( '%Y-%m-%dT%H:%M:%S'), extra_keyword_time_zone['key']: self.time_zone, extra_keyword_earthquake_x_minimum['key']: self.x_minimum, extra_keyword_earthquake_x_maximum['key']: self.x_maximum, extra_keyword_earthquake_y_minimum['key']: self.y_minimum, extra_keyword_earthquake_y_maximum['key']: self.y_maximum, extra_keyword_earthquake_event_id['key']: self.event_id } for key, value in list(self.extra_keywords.items()): extra_keywords[key] = value # Delete empty element. empty_keys = [] for key, value in list(extra_keywords.items()): if value is None: empty_keys.append(key) for empty_key in empty_keys: extra_keywords.pop(empty_key) keywords = { 'hazard': hazard_earthquake['key'], 'hazard_category': hazard_category_single_event['key'], 'keyword_version': inasafe_keyword_version, 'layer_geometry': layer_geometry_raster['key'], 'layer_mode': layer_mode_continuous['key'], 'layer_purpose': layer_purpose_hazard['key'], 'continuous_hazard_unit': unit_mmi['key'], 'classification': earthquake_mmi_scale['key'], 'thresholds': threshold_keyword, 'extra_keywords': extra_keywords, 'active_band': 1 } if self.algorithm_name: layer_path = os.path.join( self.output_dir, '%s-%s.tif' % ( self.output_basename, algorithm)) else: layer_path = os.path.join( self.output_dir, '%s.tif' % self.output_basename) # append title and source to the keywords file if len(self.title.strip()) == 0: keyword_title = self.output_basename else: keyword_title = self.title keywords['title'] = keyword_title hazard_layer = QgsRasterLayer(layer_path, keyword_title) if not hazard_layer.isValid(): raise InvalidLayerError() keyword_io.write_keywords(hazard_layer, keywords)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_check_value_mapping; 3, parameters; 3, 4; 3, 5; 4, identifier:layer; 5, default_parameter; 5, 6; 5, 7; 6, identifier:exposure_key; 7, None; 8, block; 8, 9; 8, 11; 8, 26; 8, 35; 8, 85; 8, 92; 8, 139; 8, 146; 8, 150; 8, 170; 8, 174; 8, 192; 8, 204; 8, 233; 8, 241; 8, 249; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:index; 14, call; 14, 15; 14, 22; 15, attribute; 15, 16; 15, 21; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:layer; 19, identifier:fields; 20, argument_list; 21, identifier:lookupField; 22, argument_list; 22, 23; 23, subscript; 23, 24; 23, 25; 24, identifier:exposure_type_field; 25, string:'field_name'; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:unique_exposure; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:layer; 32, identifier:uniqueValues; 33, argument_list; 33, 34; 34, identifier:index; 35, if_statement; 35, 36; 35, 45; 35, 72; 36, comparison_operator:==; 36, 37; 36, 42; 37, subscript; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:layer; 40, identifier:keywords; 41, string:'layer_purpose'; 42, subscript; 42, 43; 42, 44; 43, identifier:layer_purpose_hazard; 44, string:'key'; 45, block; 45, 46; 45, 62; 46, if_statement; 46, 47; 46, 49; 47, not_operator; 47, 48; 48, identifier:exposure_key; 49, block; 49, 50; 49, 57; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:message; 53, call; 53, 54; 53, 55; 54, identifier:tr; 55, argument_list; 55, 56; 56, string:'Hazard value mapping missing exposure key.'; 57, raise_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:InvalidKeywordsForProcessingAlgorithm; 60, argument_list; 60, 61; 61, identifier:message; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:value_map; 65, call; 65, 66; 65, 67; 66, identifier:active_thresholds_value_maps; 67, argument_list; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:layer; 70, identifier:keywords; 71, identifier:exposure_key; 72, else_clause; 72, 73; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:value_map; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:layer; 81, identifier:keywords; 82, identifier:get; 83, argument_list; 83, 84; 84, string:'value_map'; 85, if_statement; 85, 86; 85, 88; 85, 89; 86, not_operator; 86, 87; 87, identifier:value_map; 88, comment; 89, block; 89, 90; 90, return_statement; 90, 91; 91, identifier:layer; 92, if_statement; 92, 93; 92, 102; 92, 129; 93, comparison_operator:==; 93, 94; 93, 99; 94, subscript; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:layer; 97, identifier:keywords; 98, string:'layer_purpose'; 99, subscript; 99, 100; 99, 101; 100, identifier:layer_purpose_hazard; 101, string:'key'; 102, block; 102, 103; 102, 119; 103, if_statement; 103, 104; 103, 106; 104, not_operator; 104, 105; 105, identifier:exposure_key; 106, block; 106, 107; 106, 114; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:message; 110, call; 110, 111; 110, 112; 111, identifier:tr; 112, argument_list; 112, 113; 113, string:'Hazard classification is missing exposure key.'; 114, raise_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:InvalidKeywordsForProcessingAlgorithm; 117, argument_list; 117, 118; 118, identifier:message; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:classification; 122, call; 122, 123; 122, 124; 123, identifier:active_classification; 124, argument_list; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:layer; 127, identifier:keywords; 128, identifier:exposure_key; 129, else_clause; 129, 130; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:classification; 134, subscript; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:layer; 137, identifier:keywords; 138, string:'classification'; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:exposure_classification; 142, call; 142, 143; 142, 144; 143, identifier:definition; 144, argument_list; 144, 145; 145, identifier:classification; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:other; 149, None; 150, if_statement; 150, 151; 150, 158; 151, comparison_operator:!=; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:exposure_classification; 154, string:'key'; 155, subscript; 155, 156; 155, 157; 156, identifier:data_driven_classes; 157, string:'key'; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:other; 162, subscript; 162, 163; 162, 169; 163, subscript; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:exposure_classification; 166, string:'classes'; 167, unary_operator:-; 167, 168; 168, integer:1; 169, string:'key'; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:exposure_mapped; 173, list:[]; 174, for_statement; 174, 175; 174, 176; 174, 184; 175, identifier:group; 176, call; 176, 177; 176, 178; 177, identifier:list; 178, argument_list; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:value_map; 182, identifier:values; 183, argument_list; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:exposure_mapped; 189, identifier:extend; 190, argument_list; 190, 191; 191, identifier:group; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:diff; 195, call; 195, 196; 195, 197; 196, identifier:list; 197, argument_list; 197, 198; 198, binary_operator:-; 198, 199; 198, 200; 199, identifier:unique_exposure; 200, call; 200, 201; 200, 202; 201, identifier:set; 202, argument_list; 202, 203; 203, identifier:exposure_mapped; 204, if_statement; 204, 205; 204, 215; 204, 225; 205, comparison_operator:in; 205, 206; 205, 207; 206, identifier:other; 207, call; 207, 208; 207, 209; 208, identifier:list; 209, argument_list; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:value_map; 213, identifier:keys; 214, argument_list; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, subscript; 219, 220; 219, 221; 220, identifier:value_map; 221, identifier:other; 222, identifier:extend; 223, argument_list; 223, 224; 224, identifier:diff; 225, else_clause; 225, 226; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 232; 229, subscript; 229, 230; 229, 231; 230, identifier:value_map; 231, identifier:other; 232, identifier:diff; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 240; 235, subscript; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:layer; 238, identifier:keywords; 239, string:'value_map'; 240, identifier:value_map; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 248; 243, subscript; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:layer; 246, identifier:keywords; 247, string:'classification'; 248, identifier:classification; 249, return_statement; 249, 250; 250, identifier:layer
def _check_value_mapping(layer, exposure_key=None): """Loop over the exposure type field and check if the value map is correct. :param layer: The layer :type layer: QgsVectorLayer :param exposure_key: The exposure key. :type exposure_key: str """ index = layer.fields().lookupField(exposure_type_field['field_name']) unique_exposure = layer.uniqueValues(index) if layer.keywords['layer_purpose'] == layer_purpose_hazard['key']: if not exposure_key: message = tr('Hazard value mapping missing exposure key.') raise InvalidKeywordsForProcessingAlgorithm(message) value_map = active_thresholds_value_maps(layer.keywords, exposure_key) else: value_map = layer.keywords.get('value_map') if not value_map: # The exposure do not have a value_map, we can skip the layer. return layer if layer.keywords['layer_purpose'] == layer_purpose_hazard['key']: if not exposure_key: message = tr('Hazard classification is missing exposure key.') raise InvalidKeywordsForProcessingAlgorithm(message) classification = active_classification(layer.keywords, exposure_key) else: classification = layer.keywords['classification'] exposure_classification = definition(classification) other = None if exposure_classification['key'] != data_driven_classes['key']: other = exposure_classification['classes'][-1]['key'] exposure_mapped = [] for group in list(value_map.values()): exposure_mapped.extend(group) diff = list(unique_exposure - set(exposure_mapped)) if other in list(value_map.keys()): value_map[other].extend(diff) else: value_map[other] = diff layer.keywords['value_map'] = value_map layer.keywords['classification'] = classification return layer
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:clean_inasafe_fields; 3, parameters; 3, 4; 4, identifier:layer; 5, block; 5, 6; 5, 8; 5, 12; 5, 13; 5, 91; 5, 92; 5, 93; 5, 94; 5, 118; 5, 132; 5, 133; 5, 137; 5, 189; 5, 190; 5, 201; 5, 205; 5, 206; 5, 249; 5, 255; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:fields; 11, list:[]; 12, comment; 13, if_statement; 13, 14; 13, 23; 13, 40; 13, 41; 13, 68; 13, 69; 14, comparison_operator:==; 14, 15; 14, 20; 15, subscript; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:layer; 18, identifier:keywords; 19, string:'layer_purpose'; 20, subscript; 20, 21; 20, 22; 21, identifier:layer_purpose_exposure; 22, string:'key'; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:fields; 27, call; 27, 28; 27, 29; 28, identifier:get_fields; 29, argument_list; 29, 30; 29, 35; 30, subscript; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:layer; 33, identifier:keywords; 34, string:'layer_purpose'; 35, subscript; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:layer; 38, identifier:keywords; 39, string:'exposure'; 40, comment; 41, elif_clause; 41, 42; 41, 51; 42, comparison_operator:==; 42, 43; 42, 48; 43, subscript; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:layer; 46, identifier:keywords; 47, string:'layer_purpose'; 48, subscript; 48, 49; 48, 50; 49, identifier:layer_purpose_hazard; 50, string:'key'; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:fields; 55, call; 55, 56; 55, 57; 56, identifier:get_fields; 57, argument_list; 57, 58; 57, 63; 58, subscript; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:layer; 61, identifier:keywords; 62, string:'layer_purpose'; 63, subscript; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:layer; 66, identifier:keywords; 67, string:'hazard'; 68, comment; 69, elif_clause; 69, 70; 69, 79; 70, comparison_operator:==; 70, 71; 70, 76; 71, subscript; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:layer; 74, identifier:keywords; 75, string:'layer_purpose'; 76, subscript; 76, 77; 76, 78; 77, identifier:layer_purpose_aggregation; 78, string:'key'; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:fields; 83, call; 83, 84; 83, 85; 84, identifier:get_fields; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:layer; 89, identifier:keywords; 90, string:'layer_purpose'; 91, comment; 92, comment; 93, comment; 94, try_statement; 94, 95; 94, 114; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 106; 97, subscript; 97, 98; 97, 103; 98, subscript; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:layer; 101, identifier:keywords; 102, string:'inasafe_fields'; 103, subscript; 103, 104; 103, 105; 104, identifier:displaced_field; 105, string:'key'; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:fields; 111, identifier:append; 112, argument_list; 112, 113; 113, identifier:displaced_field; 114, except_clause; 114, 115; 114, 116; 115, identifier:KeyError; 116, block; 116, 117; 117, pass_statement; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:expected_fields; 121, dictionary_comprehension; 121, 122; 121, 129; 122, pair; 122, 123; 122, 126; 123, subscript; 123, 124; 123, 125; 124, identifier:field; 125, string:'key'; 126, subscript; 126, 127; 126, 128; 127, identifier:field; 128, string:'field_name'; 129, for_in_clause; 129, 130; 129, 131; 130, identifier:field; 131, identifier:fields; 132, comment; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:new_keywords; 136, dictionary; 137, for_statement; 137, 138; 137, 141; 137, 156; 138, pattern_list; 138, 139; 138, 140; 139, identifier:key; 140, identifier:val; 141, call; 141, 142; 141, 143; 142, identifier:list; 143, argument_list; 143, 144; 144, call; 144, 145; 144, 155; 145, attribute; 145, 146; 145, 154; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:layer; 150, identifier:keywords; 151, identifier:get; 152, argument_list; 152, 153; 153, string:'inasafe_fields'; 154, identifier:items; 155, argument_list; 156, block; 156, 157; 157, if_statement; 157, 158; 157, 161; 158, comparison_operator:in; 158, 159; 158, 160; 159, identifier:key; 160, identifier:expected_fields; 161, block; 161, 162; 161, 174; 161, 181; 162, if_statement; 162, 163; 162, 168; 163, call; 163, 164; 163, 165; 164, identifier:isinstance; 165, argument_list; 165, 166; 165, 167; 166, identifier:val; 167, identifier:str; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:val; 172, list:[val]; 172, 173; 173, identifier:val; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 177; 176, identifier:sum_fields; 177, argument_list; 177, 178; 177, 179; 177, 180; 178, identifier:layer; 179, identifier:key; 180, identifier:val; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 186; 183, subscript; 183, 184; 183, 185; 184, identifier:new_keywords; 185, identifier:key; 186, subscript; 186, 187; 186, 188; 187, identifier:expected_fields; 188, identifier:key; 189, comment; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 199; 192, attribute; 192, 193; 192, 198; 193, subscript; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:layer; 196, identifier:keywords; 197, string:'inasafe_fields'; 198, identifier:update; 199, argument_list; 199, 200; 200, identifier:new_keywords; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:to_remove; 204, list:[]; 205, comment; 206, for_statement; 206, 207; 206, 208; 206, 217; 207, identifier:field; 208, call; 208, 209; 208, 216; 209, attribute; 209, 210; 209, 215; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:layer; 213, identifier:fields; 214, argument_list; 215, identifier:toList; 216, argument_list; 217, block; 217, 218; 218, if_statement; 218, 219; 218, 237; 219, comparison_operator:not; 219, 220; 219, 225; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:field; 223, identifier:name; 224, argument_list; 225, call; 225, 226; 225, 227; 226, identifier:list; 227, argument_list; 227, 228; 228, call; 228, 229; 228, 236; 229, attribute; 229, 230; 229, 235; 230, subscript; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:layer; 233, identifier:keywords; 234, string:'inasafe_fields'; 235, identifier:values; 236, argument_list; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:to_remove; 242, identifier:append; 243, argument_list; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:field; 247, identifier:name; 248, argument_list; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 252; 251, identifier:remove_fields; 252, argument_list; 252, 253; 252, 254; 253, identifier:layer; 254, identifier:to_remove; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:LOGGER; 259, identifier:debug; 260, argument_list; 260, 261; 261, binary_operator:%; 261, 262; 261, 263; 262, string:'Fields which have been removed from %s : %s'; 263, tuple; 263, 264; 263, 269; 264, subscript; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:layer; 267, identifier:keywords; 268, string:'layer_purpose'; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, string:' '; 272, identifier:join; 273, argument_list; 273, 274; 274, identifier:to_remove
def clean_inasafe_fields(layer): """Clean inasafe_fields based on keywords. 1. Must use standard field names. 2. Sum up list of fields' value and put in the standard field name. 3. Remove un-used fields. :param layer: The layer :type layer: QgsVectorLayer """ fields = [] # Exposure if layer.keywords['layer_purpose'] == layer_purpose_exposure['key']: fields = get_fields( layer.keywords['layer_purpose'], layer.keywords['exposure']) # Hazard elif layer.keywords['layer_purpose'] == layer_purpose_hazard['key']: fields = get_fields( layer.keywords['layer_purpose'], layer.keywords['hazard']) # Aggregation elif layer.keywords['layer_purpose'] == layer_purpose_aggregation['key']: fields = get_fields( layer.keywords['layer_purpose']) # Add displaced_field definition to expected_fields # for minimum needs calculator. # If there is no displaced_field keyword, then pass try: if layer.keywords['inasafe_fields'][displaced_field['key']]: fields.append(displaced_field) except KeyError: pass expected_fields = {field['key']: field['field_name'] for field in fields} # Convert the field name and sum up if needed new_keywords = {} for key, val in list(layer.keywords.get('inasafe_fields').items()): if key in expected_fields: if isinstance(val, str): val = [val] sum_fields(layer, key, val) new_keywords[key] = expected_fields[key] # Houra, InaSAFE keywords match our concepts ! layer.keywords['inasafe_fields'].update(new_keywords) to_remove = [] # Remove unnecessary fields (the one that is not in the inasafe_fields) for field in layer.fields().toList(): if field.name() not in list(layer.keywords['inasafe_fields'].values()): to_remove.append(field.name()) remove_fields(layer, to_remove) LOGGER.debug( 'Fields which have been removed from %s : %s' % (layer.keywords['layer_purpose'], ' '.join(to_remove)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_size_is_needed; 3, parameters; 3, 4; 4, identifier:layer; 5, block; 5, 6; 5, 8; 5, 19; 5, 26; 5, 36; 5, 44; 5, 58; 5, 59; 5, 60; 5, 68; 5, 78; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:exposure; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:layer; 15, identifier:keywords; 16, identifier:get; 17, argument_list; 17, 18; 18, string:'exposure'; 19, if_statement; 19, 20; 19, 22; 19, 23; 20, not_operator; 20, 21; 21, identifier:exposure; 22, comment; 23, block; 23, 24; 24, return_statement; 24, 25; 25, False; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:indivisible_exposure_keys; 29, list_comprehension; 29, 30; 29, 33; 30, subscript; 30, 31; 30, 32; 31, identifier:f; 32, string:'key'; 33, for_in_clause; 33, 34; 33, 35; 34, identifier:f; 35, identifier:indivisible_exposure; 36, if_statement; 36, 37; 36, 40; 36, 41; 37, comparison_operator:in; 37, 38; 37, 39; 38, identifier:exposure; 39, identifier:indivisible_exposure_keys; 40, comment; 41, block; 41, 42; 42, return_statement; 42, 43; 43, False; 44, if_statement; 44, 45; 44, 54; 44, 55; 45, comparison_operator:==; 45, 46; 45, 51; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:layer; 49, identifier:geometryType; 50, argument_list; 51, attribute; 51, 52; 51, 53; 52, identifier:QgsWkbTypes; 53, identifier:PointGeometry; 54, comment; 55, block; 55, 56; 56, return_statement; 56, 57; 57, False; 58, comment; 59, comment; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:fields; 63, subscript; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:layer; 66, identifier:keywords; 67, string:'inasafe_fields'; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:absolute_field_keys; 71, list_comprehension; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:f; 74, string:'key'; 75, for_in_clause; 75, 76; 75, 77; 76, identifier:f; 77, identifier:count_fields; 78, for_statement; 78, 79; 78, 80; 78, 81; 78, 89; 79, identifier:field; 80, identifier:fields; 81, block; 81, 82; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:in; 83, 84; 83, 85; 84, identifier:field; 85, identifier:absolute_field_keys; 86, block; 86, 87; 87, return_statement; 87, 88; 88, True; 89, else_clause; 89, 90; 90, block; 90, 91; 91, return_statement; 91, 92; 92, False
def _size_is_needed(layer): """Checker if we need the size field. :param layer: The layer to test. :type layer: QgsVectorLayer :return: If we need the size field. :rtype: bool """ exposure = layer.keywords.get('exposure') if not exposure: # The layer is not an exposure. return False indivisible_exposure_keys = [f['key'] for f in indivisible_exposure] if exposure in indivisible_exposure_keys: # The exposure is not divisible, We don't need to compute the size. return False if layer.geometryType() == QgsWkbTypes.PointGeometry: # The exposure is a point layer. We don't need to compute the size. return False # The layer is divisible and not a point layer. # We need to check if some fields are absolute. fields = layer.keywords['inasafe_fields'] absolute_field_keys = [f['key'] for f in count_fields] for field in fields: if field in absolute_field_keys: return True else: return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_remove_features; 3, parameters; 3, 4; 4, identifier:layer; 5, block; 5, 6; 5, 8; 5, 9; 5, 17; 5, 28; 5, 36; 5, 44; 5, 45; 5, 46; 5, 57; 5, 70; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:layer_purpose; 12, subscript; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:layer; 15, identifier:keywords; 16, string:'layer_purpose'; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:layer_subcategory; 20, call; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:layer; 24, identifier:keywords; 25, identifier:get; 26, argument_list; 26, 27; 27, identifier:layer_purpose; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:compulsory_field; 31, call; 31, 32; 31, 33; 32, identifier:get_compulsory_fields; 33, argument_list; 33, 34; 33, 35; 34, identifier:layer_purpose; 35, identifier:layer_subcategory; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:inasafe_fields; 39, subscript; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:layer; 42, identifier:keywords; 43, string:'inasafe_fields'; 44, comment; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:field_names; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:inasafe_fields; 52, identifier:get; 53, argument_list; 53, 54; 54, subscript; 54, 55; 54, 56; 55, identifier:compulsory_field; 56, string:'key'; 57, if_statement; 57, 58; 57, 64; 58, not_operator; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:isinstance; 61, argument_list; 61, 62; 61, 63; 62, identifier:field_names; 63, identifier:list; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:field_names; 68, list:[field_names]; 68, 69; 69, identifier:field_names; 70, for_statement; 70, 71; 70, 72; 70, 73; 71, identifier:field_name; 72, identifier:field_names; 73, block; 73, 74; 73, 93; 73, 106; 73, 112; 73, 125; 73, 131; 73, 135; 73, 313; 73, 319; 74, if_statement; 74, 75; 74, 77; 75, not_operator; 75, 76; 76, identifier:field_name; 77, block; 77, 78; 77, 88; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:message; 81, binary_operator:%; 81, 82; 81, 83; 82, string:'Keyword %s is missing from %s'; 83, tuple; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:compulsory_field; 86, string:'key'; 87, identifier:layer_purpose; 88, raise_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:InvalidKeywordsForProcessingAlgorithm; 91, argument_list; 91, 92; 92, identifier:message; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:index; 96, call; 96, 97; 96, 104; 97, attribute; 97, 98; 97, 103; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:layer; 101, identifier:fields; 102, argument_list; 103, identifier:lookupField; 104, argument_list; 104, 105; 105, identifier:field_name; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:request; 109, call; 109, 110; 109, 111; 110, identifier:QgsFeatureRequest; 111, argument_list; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:request; 116, identifier:setSubsetOfAttributes; 117, argument_list; 117, 118; 117, 120; 118, list:[field_name]; 118, 119; 119, identifier:field_name; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:layer; 123, identifier:fields; 124, argument_list; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:layer; 129, identifier:startEditing; 130, argument_list; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:i; 134, integer:0; 135, for_statement; 135, 136; 135, 137; 135, 143; 136, identifier:feature; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:layer; 140, identifier:getFeatures; 141, argument_list; 141, 142; 142, identifier:request; 143, block; 143, 144; 143, 154; 143, 240; 143, 241; 143, 249; 143, 269; 143, 270; 143, 293; 143, 294; 143, 311; 143, 312; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:feat_attr; 147, subscript; 147, 148; 147, 153; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:feature; 151, identifier:attributes; 152, argument_list; 153, identifier:index; 154, if_statement; 154, 155; 154, 172; 155, parenthesized_expression; 155, 156; 156, boolean_operator:or; 156, 157; 156, 160; 157, comparison_operator:is; 157, 158; 157, 159; 158, identifier:feat_attr; 159, None; 160, parenthesized_expression; 160, 161; 161, boolean_operator:and; 161, 162; 161, 167; 162, call; 162, 163; 162, 164; 163, identifier:hasattr; 164, argument_list; 164, 165; 164, 166; 165, identifier:feat_attr; 166, string:'isNull'; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:feat_attr; 170, identifier:isNull; 171, argument_list; 172, block; 172, 173; 173, if_statement; 173, 174; 173, 177; 173, 178; 173, 194; 173, 220; 174, comparison_operator:==; 174, 175; 174, 176; 175, identifier:layer_purpose; 176, string:'hazard'; 177, comment; 178, block; 178, 179; 178, 190; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:layer; 183, identifier:deleteFeature; 184, argument_list; 184, 185; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:feature; 188, identifier:id; 189, argument_list; 190, expression_statement; 190, 191; 191, augmented_assignment:+=; 191, 192; 191, 193; 192, identifier:i; 193, integer:1; 194, elif_clause; 194, 195; 194, 198; 194, 199; 195, comparison_operator:==; 195, 196; 195, 197; 196, identifier:layer_purpose; 197, string:'aggregation'; 198, comment; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:layer; 204, identifier:changeAttributeValue; 205, argument_list; 205, 206; 205, 211; 205, 212; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:feature; 209, identifier:id; 210, argument_list; 211, identifier:index; 212, call; 212, 213; 212, 214; 213, identifier:str; 214, argument_list; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:feature; 218, identifier:id; 219, argument_list; 220, elif_clause; 220, 221; 220, 224; 220, 225; 220, 226; 221, comparison_operator:==; 221, 222; 221, 223; 222, identifier:layer_purpose; 223, string:'exposure'; 224, comment; 225, comment; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:layer; 231, identifier:changeAttributeValue; 232, argument_list; 232, 233; 232, 238; 232, 239; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:feature; 236, identifier:id; 237, argument_list; 238, identifier:index; 239, string:''; 240, comment; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:geometry; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:feature; 247, identifier:geometry; 248, argument_list; 249, if_statement; 249, 250; 249, 252; 250, not_operator; 250, 251; 251, identifier:geometry; 252, block; 252, 253; 252, 264; 252, 268; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:layer; 257, identifier:deleteFeature; 258, argument_list; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:feature; 262, identifier:id; 263, argument_list; 264, expression_statement; 264, 265; 265, augmented_assignment:+=; 265, 266; 265, 267; 266, identifier:i; 267, integer:1; 268, continue_statement; 269, comment; 270, if_statement; 270, 271; 270, 276; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:geometry; 274, identifier:isEmpty; 275, argument_list; 276, block; 276, 277; 276, 288; 276, 292; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:layer; 281, identifier:deleteFeature; 282, argument_list; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:feature; 286, identifier:id; 287, argument_list; 288, expression_statement; 288, 289; 289, augmented_assignment:+=; 289, 290; 289, 291; 290, identifier:i; 291, integer:1; 292, continue_statement; 293, comment; 294, if_statement; 294, 295; 294, 301; 294, 302; 294, 303; 294, 304; 294, 305; 294, 306; 294, 307; 294, 308; 294, 309; 295, not_operator; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:geometry; 299, identifier:isGeosValid; 300, argument_list; 301, comment; 302, comment; 303, comment; 304, comment; 305, comment; 306, comment; 307, comment; 308, comment; 309, block; 309, 310; 310, pass_statement; 311, comment; 312, comment; 313, expression_statement; 313, 314; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:layer; 317, identifier:commitChanges; 318, argument_list; 319, if_statement; 319, 320; 319, 321; 319, 337; 320, identifier:i; 321, block; 321, 322; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:LOGGER; 326, identifier:critical; 327, argument_list; 327, 328; 328, binary_operator:%; 328, 329; 328, 330; 329, string:'Features which have been removed from %s : %s'; 330, tuple; 330, 331; 330, 336; 331, subscript; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:layer; 334, identifier:keywords; 335, string:'layer_purpose'; 336, identifier:i; 337, else_clause; 337, 338; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:LOGGER; 343, identifier:info; 344, argument_list; 344, 345; 345, binary_operator:%; 345, 346; 345, 349; 346, concatenated_string; 346, 347; 346, 348; 347, string:'No feature has been removed from %s during the vector layer '; 348, string:'preparation'; 349, subscript; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:layer; 352, identifier:keywords; 353, string:'layer_purpose'
def _remove_features(layer): """Remove features which do not have information for InaSAFE or an invalid geometry. :param layer: The vector layer. :type layer: QgsVectorLayer """ # Get the layer purpose of the layer. layer_purpose = layer.keywords['layer_purpose'] layer_subcategory = layer.keywords.get(layer_purpose) compulsory_field = get_compulsory_fields(layer_purpose, layer_subcategory) inasafe_fields = layer.keywords['inasafe_fields'] # Compulsory fields can be list of field name or single field name. # We need to iterate through all of them field_names = inasafe_fields.get(compulsory_field['key']) if not isinstance(field_names, list): field_names = [field_names] for field_name in field_names: if not field_name: message = 'Keyword %s is missing from %s' % ( compulsory_field['key'], layer_purpose) raise InvalidKeywordsForProcessingAlgorithm(message) index = layer.fields().lookupField(field_name) request = QgsFeatureRequest() request.setSubsetOfAttributes([field_name], layer.fields()) layer.startEditing() i = 0 for feature in layer.getFeatures(request): feat_attr = feature.attributes()[index] if (feat_attr is None or (hasattr(feat_attr, 'isNull') and feat_attr.isNull())): if layer_purpose == 'hazard': # Remove the feature if the hazard is null. layer.deleteFeature(feature.id()) i += 1 elif layer_purpose == 'aggregation': # Put the ID if the value is null. layer.changeAttributeValue( feature.id(), index, str(feature.id())) elif layer_purpose == 'exposure': # Put an empty value, the value mapping will take care of # it in the 'other' group. layer.changeAttributeValue( feature.id(), index, '') # Check if there is en empty geometry. geometry = feature.geometry() if not geometry: layer.deleteFeature(feature.id()) i += 1 continue # Check if the geometry is empty. if geometry.isEmpty(): layer.deleteFeature(feature.id()) i += 1 continue # Check if the geometry is valid. if not geometry.isGeosValid(): # polygonize can produce some invalid geometries # For instance a polygon like this, sharing a same point : # _______ # | ___|__ # | |__| | # |________| # layer.deleteFeature(feature.id()) # i += 1 pass # TODO We need to add more tests # like checking if the value is in the value_mapping. layer.commitChanges() if i: LOGGER.critical( 'Features which have been removed from %s : %s' % (layer.keywords['layer_purpose'], i)) else: LOGGER.info( 'No feature has been removed from %s during the vector layer ' 'preparation' % layer.keywords['layer_purpose'])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_profiles; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:overwrite; 7, False; 8, block; 8, 9; 8, 11; 8, 73; 8, 74; 8, 75; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 16; 12, function_name:sort_by_locale; 13, parameters; 13, 14; 13, 15; 14, identifier:unsorted_profiles; 15, identifier:locale; 16, block; 16, 17; 16, 19; 16, 26; 16, 36; 16, 40; 16, 44; 16, 69; 17, expression_statement; 17, 18; 18, comment; 19, if_statement; 19, 20; 19, 23; 20, comparison_operator:is; 20, 21; 20, 22; 21, identifier:locale; 22, None; 23, block; 23, 24; 24, return_statement; 24, 25; 25, identifier:unsorted_profiles; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:locale; 29, binary_operator:%; 29, 30; 29, 31; 30, string:'_%s'; 31, subscript; 31, 32; 31, 33; 32, identifier:locale; 33, slice; 33, 34; 33, 35; 34, colon; 35, integer:2; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:profiles_our_locale; 39, list:[]; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:profiles_remaining; 43, list:[]; 44, for_statement; 44, 45; 44, 46; 44, 47; 45, identifier:profile_name; 46, identifier:unsorted_profiles; 47, block; 47, 48; 48, if_statement; 48, 49; 48, 52; 48, 60; 49, comparison_operator:in; 49, 50; 49, 51; 50, identifier:locale; 51, identifier:profile_name; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:profiles_our_locale; 57, identifier:append; 58, argument_list; 58, 59; 59, identifier:profile_name; 60, else_clause; 60, 61; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:profiles_remaining; 66, identifier:append; 67, argument_list; 67, 68; 68, identifier:profile_name; 69, return_statement; 69, 70; 70, binary_operator:+; 70, 71; 70, 72; 71, identifier:profiles_our_locale; 72, identifier:profiles_remaining; 73, comment; 74, comment; 75, if_statement; 75, 76; 75, 80; 75, 87; 76, not_operator; 76, 77; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:root_directory; 80, block; 80, 81; 80, 85; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:profiles; 84, list:[]; 85, return_statement; 85, 86; 86, identifier:profiles; 87, else_clause; 87, 88; 88, block; 88, 89; 88, 103; 88, 110; 88, 128; 88, 129; 88, 181; 88, 182; 88, 183; 88, 190; 88, 217; 88, 227; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:locale_minimum_needs_dir; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:os; 96, identifier:path; 97, identifier:join; 98, argument_list; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:root_directory; 102, string:'minimum_needs'; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:path_name; 106, call; 106, 107; 106, 108; 107, identifier:resources_path; 108, argument_list; 108, 109; 109, string:'minimum_needs'; 110, if_statement; 110, 111; 110, 120; 111, not_operator; 111, 112; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:os; 116, identifier:path; 117, identifier:exists; 118, argument_list; 118, 119; 119, identifier:locale_minimum_needs_dir; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:os; 125, identifier:makedirs; 126, argument_list; 126, 127; 127, identifier:locale_minimum_needs_dir; 128, comment; 129, for_statement; 129, 130; 129, 131; 129, 137; 130, identifier:file_name; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:os; 134, identifier:listdir; 135, argument_list; 135, 136; 136, identifier:path_name; 137, block; 137, 138; 137, 150; 137, 162; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:source_file; 141, call; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:os; 145, identifier:path; 146, identifier:join; 147, argument_list; 147, 148; 147, 149; 148, identifier:path_name; 149, identifier:file_name; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:destination_file; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:os; 157, identifier:path; 158, identifier:join; 159, argument_list; 159, 160; 159, 161; 160, identifier:locale_minimum_needs_dir; 161, identifier:file_name; 162, if_statement; 162, 163; 162, 174; 163, boolean_operator:or; 163, 164; 163, 173; 164, not_operator; 164, 165; 165, call; 165, 166; 165, 171; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:os; 169, identifier:path; 170, identifier:exists; 171, argument_list; 171, 172; 172, identifier:destination_file; 173, identifier:overwrite; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:copy; 178, argument_list; 178, 179; 178, 180; 179, identifier:source_file; 180, identifier:destination_file; 181, comment; 182, comment; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:move_old_profile; 188, argument_list; 188, 189; 189, identifier:locale_minimum_needs_dir; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:profiles; 193, list_comprehension; 193, 194; 193, 200; 193, 208; 194, subscript; 194, 195; 194, 196; 195, identifier:profile; 196, slice; 196, 197; 196, 198; 197, colon; 198, unary_operator:-; 198, 199; 199, integer:5; 200, for_in_clause; 200, 201; 200, 202; 201, identifier:profile; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:os; 205, identifier:listdir; 206, argument_list; 206, 207; 207, identifier:locale_minimum_needs_dir; 208, if_clause; 208, 209; 209, comparison_operator:==; 209, 210; 209, 216; 210, subscript; 210, 211; 210, 212; 211, identifier:profile; 212, slice; 212, 213; 212, 215; 213, unary_operator:-; 213, 214; 214, integer:5; 215, colon; 216, string:'.json'; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:profiles; 220, call; 220, 221; 220, 222; 221, identifier:sort_by_locale; 222, argument_list; 222, 223; 222, 224; 223, identifier:profiles; 224, attribute; 224, 225; 224, 226; 225, identifier:self; 226, identifier:locale; 227, return_statement; 227, 228; 228, identifier:profiles
def get_profiles(self, overwrite=False): """Get all the minimum needs profiles. :returns: The minimum needs by name. :rtype: list """ def sort_by_locale(unsorted_profiles, locale): """Sort the profiles by language settings. The profiles that are in the same language as the QGIS' locale will be sorted out first. :param unsorted_profiles: The user profiles profiles :type unsorted_profiles: list :param locale: The language settings string :type locale: str :returns: Ordered profiles :rtype: list """ if locale is None: return unsorted_profiles locale = '_%s' % locale[:2] profiles_our_locale = [] profiles_remaining = [] for profile_name in unsorted_profiles: if locale in profile_name: profiles_our_locale.append(profile_name) else: profiles_remaining.append(profile_name) return profiles_our_locale + profiles_remaining # We ignore empty root_directory to avoid load min needs profile # to test directory when test is running. if not self.root_directory: profiles = [] return profiles else: locale_minimum_needs_dir = os.path.join( self.root_directory, 'minimum_needs') path_name = resources_path('minimum_needs') if not os.path.exists(locale_minimum_needs_dir): os.makedirs(locale_minimum_needs_dir) # load default min needs profile for file_name in os.listdir(path_name): source_file = os.path.join(path_name, file_name) destination_file = os.path.join( locale_minimum_needs_dir, file_name) if not os.path.exists(destination_file) or overwrite: copy(source_file, destination_file) # move old min needs profile under user profile to inasafe # subdirectory self.move_old_profile(locale_minimum_needs_dir) profiles = [ profile[:-5] for profile in os.listdir(locale_minimum_needs_dir) if profile[-5:] == '.json'] profiles = sort_by_locale(profiles, self.locale) return profiles
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sorted_keywords_by_order; 3, parameters; 3, 4; 3, 5; 4, identifier:keywords; 5, identifier:order; 6, block; 6, 7; 6, 9; 6, 10; 6, 32; 6, 38; 6, 65; 6, 85; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, for_statement; 10, 11; 10, 14; 10, 22; 11, pattern_list; 11, 12; 11, 13; 12, identifier:key; 13, identifier:value; 14, call; 14, 15; 14, 16; 15, identifier:list; 16, argument_list; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:keywords; 20, identifier:items; 21, argument_list; 22, block; 22, 23; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:value; 26, None; 27, block; 27, 28; 28, delete_statement; 28, 29; 29, subscript; 29, 30; 29, 31; 30, identifier:keywords; 31, identifier:key; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:ordered_keywords; 35, call; 35, 36; 35, 37; 36, identifier:OrderedDict; 37, argument_list; 38, for_statement; 38, 39; 38, 40; 38, 41; 39, identifier:key; 40, identifier:order; 41, block; 41, 42; 42, if_statement; 42, 43; 42, 53; 43, comparison_operator:in; 43, 44; 43, 45; 44, identifier:key; 45, call; 45, 46; 45, 47; 46, identifier:list; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:keywords; 51, identifier:keys; 52, argument_list; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:ordered_keywords; 58, identifier:key; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:keywords; 62, identifier:get; 63, argument_list; 63, 64; 64, identifier:key; 65, for_statement; 65, 66; 65, 67; 65, 68; 66, identifier:keyword; 67, identifier:keywords; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:not; 70, 71; 70, 72; 71, identifier:keyword; 72, identifier:order; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:ordered_keywords; 78, identifier:keyword; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:keywords; 82, identifier:get; 83, argument_list; 83, 84; 84, identifier:keyword; 85, return_statement; 85, 86; 86, identifier:ordered_keywords
def sorted_keywords_by_order(keywords, order): """Sort keywords based on defined order. :param keywords: Keyword to be sorted. :type keywords: dict :param order: Ordered list of key. :type order: list :return: Ordered dictionary based on order list. :rtype: OrderedDict """ # we need to delete item with no value for key, value in list(keywords.items()): if value is None: del keywords[key] ordered_keywords = OrderedDict() for key in order: if key in list(keywords.keys()): ordered_keywords[key] = keywords.get(key) for keyword in keywords: if keyword not in order: ordered_keywords[keyword] = keywords.get(keyword) return ordered_keywords
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_layer_modes; 3, parameters; 3, 4; 4, identifier:subcategory; 5, block; 5, 6; 5, 8; 5, 17; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:layer_modes; 11, subscript; 11, 12; 11, 16; 12, call; 12, 13; 12, 14; 13, identifier:definition; 14, argument_list; 14, 15; 15, identifier:subcategory; 16, string:'layer_modes'; 17, return_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:sorted; 20, argument_list; 20, 21; 20, 22; 21, identifier:layer_modes; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:key; 24, lambda; 24, 25; 24, 27; 25, lambda_parameters; 25, 26; 26, identifier:k; 27, subscript; 27, 28; 27, 29; 28, identifier:k; 29, string:'key'
def get_layer_modes(subcategory): """Return all sorted layer modes from exposure or hazard. :param subcategory: Hazard or Exposure key. :type subcategory: str :returns: List of layer modes definition. :rtype: list """ layer_modes = definition(subcategory)['layer_modes'] return sorted(layer_modes, key=lambda k: k['key'])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:add_ordered_combo_item; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:combo; 5, identifier:text; 6, default_parameter; 6, 7; 6, 8; 7, identifier:data; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:count_selected_features; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:icon; 14, None; 15, block; 15, 16; 15, 18; 15, 41; 15, 49; 15, 109; 15, 110; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:count_selected_features; 21, None; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, augmented_assignment:+=; 24, 25; 24, 26; 25, identifier:text; 26, binary_operator:+; 26, 27; 26, 40; 27, binary_operator:+; 27, 28; 27, 29; 28, string:' ('; 29, call; 29, 30; 29, 36; 30, attribute; 30, 31; 30, 35; 31, call; 31, 32; 31, 33; 32, identifier:tr; 33, argument_list; 33, 34; 34, string:'{count} selected features'; 35, identifier:format; 36, argument_list; 36, 37; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:count; 39, identifier:count_selected_features; 40, string:')'; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:size; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:combo; 47, identifier:count; 48, argument_list; 49, for_statement; 49, 50; 49, 51; 49, 56; 50, identifier:combo_index; 51, call; 51, 52; 51, 53; 52, identifier:range; 53, argument_list; 53, 54; 53, 55; 54, integer:0; 55, identifier:size; 56, block; 56, 57; 56, 66; 56, 67; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:item_text; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:combo; 63, identifier:itemText; 64, argument_list; 64, 65; 65, identifier:combo_index; 66, comment; 67, if_statement; 67, 68; 67, 83; 68, comparison_operator:<; 68, 69; 68, 82; 69, call; 69, 70; 69, 71; 70, identifier:cmp; 71, argument_list; 71, 72; 71, 77; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:text; 75, identifier:lower; 76, argument_list; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:item_text; 80, identifier:lower; 81, argument_list; 82, integer:0; 83, block; 83, 84; 83, 108; 84, if_statement; 84, 85; 84, 86; 84, 97; 85, identifier:icon; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:combo; 91, identifier:insertItem; 92, argument_list; 92, 93; 92, 94; 92, 95; 92, 96; 93, identifier:combo_index; 94, identifier:icon; 95, identifier:text; 96, identifier:data; 97, else_clause; 97, 98; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:combo; 103, identifier:insertItem; 104, argument_list; 104, 105; 104, 106; 104, 107; 105, identifier:combo_index; 106, identifier:text; 107, identifier:data; 108, return_statement; 109, comment; 110, if_statement; 110, 111; 110, 112; 110, 123; 111, identifier:icon; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:combo; 117, identifier:insertItem; 118, argument_list; 118, 119; 118, 120; 118, 121; 118, 122; 119, identifier:size; 120, identifier:icon; 121, identifier:text; 122, identifier:data; 123, else_clause; 123, 124; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:combo; 129, identifier:insertItem; 130, argument_list; 130, 131; 130, 132; 130, 133; 131, identifier:size; 132, identifier:text; 133, identifier:data
def add_ordered_combo_item( combo, text, data=None, count_selected_features=None, icon=None): """Add a combo item ensuring that all items are listed alphabetically. Although QComboBox allows you to set an InsertAlphabetically enum this only has effect when a user interactively adds combo items to an editable combo. This we have this little function to ensure that combos are always sorted alphabetically. :param combo: Combo box receiving the new item. :type combo: QComboBox :param text: Display text for the combo. :type text: str :param data: Optional UserRole data to be associated with the item. :type data: QVariant, str :param count_selected_features: A count to display if the layer has some selected features. Default to None, nothing will be displayed. :type count_selected_features: None, int :param icon: Icon to display in the combobox. :type icon: QIcon """ if count_selected_features is not None: text += ' (' + tr('{count} selected features').format( count=count_selected_features) + ')' size = combo.count() for combo_index in range(0, size): item_text = combo.itemText(combo_index) # see if text alphabetically precedes item_text if cmp(text.lower(), item_text.lower()) < 0: if icon: combo.insertItem(combo_index, icon, text, data) else: combo.insertItem(combo_index, text, data) return # otherwise just add it to the end if icon: combo.insertItem(size, icon, text, data) else: combo.insertItem(size, text, data)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 17; 10, attribute; 10, 11; 10, 16; 11, call; 11, 12; 11, 13; 12, identifier:super; 13, argument_list; 13, 14; 13, 15; 14, identifier:JSSObjectList; 15, identifier:self; 16, identifier:sort; 17, argument_list; 17, 18; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:key; 20, lambda; 20, 21; 20, 23; 21, lambda_parameters; 21, 22; 22, identifier:k; 23, attribute; 23, 24; 23, 25; 24, identifier:k; 25, identifier:id
def sort(self): """Sort list elements by ID.""" super(JSSObjectList, self).sort(key=lambda k: k.id)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_by_name; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 17; 10, attribute; 10, 11; 10, 16; 11, call; 11, 12; 11, 13; 12, identifier:super; 13, argument_list; 13, 14; 13, 15; 14, identifier:JSSObjectList; 15, identifier:self; 16, identifier:sort; 17, argument_list; 17, 18; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:key; 20, lambda; 20, 21; 20, 23; 21, lambda_parameters; 21, 22; 22, identifier:k; 23, attribute; 23, 24; 23, 25; 24, identifier:k; 25, identifier:name
def sort_by_name(self): """Sort list elements by name.""" super(JSSObjectList, self).sort(key=lambda k: k.name)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:find_sorted_task_dependencies; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:task; 5, identifier:task_name; 6, identifier:task_id; 7, block; 7, 8; 7, 10; 7, 23; 7, 56; 7, 85; 7, 93; 7, 100; 7, 112; 7, 116; 7, 117; 7, 118; 7, 127; 7, 135; 7, 147; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:log; 14, identifier:info; 15, argument_list; 15, 16; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, string:"find_sorted_task_dependencies {} {}"; 19, identifier:format; 20, argument_list; 20, 21; 20, 22; 21, identifier:task_name; 22, identifier:task_id; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:cot_input_dependencies; 26, list_comprehension; 26, 27; 26, 33; 27, call; 27, 28; 27, 29; 28, identifier:_craft_dependency_tuple; 29, argument_list; 29, 30; 29, 31; 29, 32; 30, identifier:task_name; 31, identifier:task_type; 32, identifier:task_id; 33, for_in_clause; 33, 34; 33, 37; 34, pattern_list; 34, 35; 34, 36; 35, identifier:task_type; 36, identifier:task_id; 37, call; 37, 38; 37, 55; 38, attribute; 38, 39; 38, 54; 39, call; 39, 40; 39, 51; 40, attribute; 40, 41; 40, 50; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, subscript; 43, 44; 43, 45; 44, identifier:task; 45, string:'extra'; 46, identifier:get; 47, argument_list; 47, 48; 47, 49; 48, string:'chainOfTrust'; 49, dictionary; 50, identifier:get; 51, argument_list; 51, 52; 51, 53; 52, string:'inputs'; 53, dictionary; 54, identifier:items; 55, argument_list; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:upstream_artifacts_dependencies; 59, list_comprehension; 59, 60; 59, 70; 60, call; 60, 61; 60, 62; 61, identifier:_craft_dependency_tuple; 62, argument_list; 62, 63; 62, 64; 62, 67; 63, identifier:task_name; 64, subscript; 64, 65; 64, 66; 65, identifier:artifact_dict; 66, string:'taskType'; 67, subscript; 67, 68; 67, 69; 68, identifier:artifact_dict; 69, string:'taskId'; 70, for_in_clause; 70, 71; 70, 72; 71, identifier:artifact_dict; 72, call; 72, 73; 72, 82; 73, attribute; 73, 74; 73, 81; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:task; 77, identifier:get; 78, argument_list; 78, 79; 78, 80; 79, string:'payload'; 80, dictionary; 81, identifier:get; 82, argument_list; 82, 83; 82, 84; 83, string:'upstreamArtifacts'; 84, list:[]; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:dependencies; 88, list:[*cot_input_dependencies, *upstream_artifacts_dependencies]; 88, 89; 88, 91; 89, list_splat; 89, 90; 90, identifier:cot_input_dependencies; 91, list_splat; 91, 92; 92, identifier:upstream_artifacts_dependencies; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:dependencies; 96, call; 96, 97; 96, 98; 97, identifier:_sort_dependencies_by_name_then_task_id; 98, argument_list; 98, 99; 99, identifier:dependencies; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:parent_task_id; 103, boolean_operator:or; 103, 104; 103, 108; 104, call; 104, 105; 104, 106; 105, identifier:get_parent_task_id; 106, argument_list; 106, 107; 107, identifier:task; 108, call; 108, 109; 108, 110; 109, identifier:get_decision_task_id; 110, argument_list; 110, 111; 111, identifier:task; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:parent_task_type; 115, string:'parent'; 116, comment; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:parent_tuple; 121, call; 121, 122; 121, 123; 122, identifier:_craft_dependency_tuple; 123, argument_list; 123, 124; 123, 125; 123, 126; 124, identifier:task_name; 125, identifier:parent_task_type; 126, identifier:parent_task_id; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:dependencies; 131, identifier:insert; 132, argument_list; 132, 133; 132, 134; 133, integer:0; 134, identifier:parent_tuple; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:log; 139, identifier:info; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, string:'found dependencies: {}'; 144, identifier:format; 145, argument_list; 145, 146; 146, identifier:dependencies; 147, return_statement; 147, 148; 148, identifier:dependencies
def find_sorted_task_dependencies(task, task_name, task_id): """Find the taskIds of the chain of trust dependencies of a given task. Args: task (dict): the task definition to inspect. task_name (str): the name of the task, for logging and naming children. task_id (str): the taskId of the task. Returns: list: tuples associating dependent task ``name`` to dependent task ``taskId``. """ log.info("find_sorted_task_dependencies {} {}".format(task_name, task_id)) cot_input_dependencies = [ _craft_dependency_tuple(task_name, task_type, task_id) for task_type, task_id in task['extra'].get('chainOfTrust', {}).get('inputs', {}).items() ] upstream_artifacts_dependencies = [ _craft_dependency_tuple(task_name, artifact_dict['taskType'], artifact_dict['taskId']) for artifact_dict in task.get('payload', {}).get('upstreamArtifacts', []) ] dependencies = [*cot_input_dependencies, *upstream_artifacts_dependencies] dependencies = _sort_dependencies_by_name_then_task_id(dependencies) parent_task_id = get_parent_task_id(task) or get_decision_task_id(task) parent_task_type = 'parent' # make sure we deal with the decision task first, or we may populate # signing:build0:decision before signing:decision parent_tuple = _craft_dependency_tuple(task_name, parent_task_type, parent_task_id) dependencies.insert(0, parent_tuple) log.info('found dependencies: {}'.format(dependencies)) return dependencies
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_all_artifacts_per_task_id; 3, parameters; 3, 4; 3, 5; 4, identifier:chain; 5, identifier:upstream_artifacts; 6, block; 6, 7; 6, 9; 6, 13; 6, 80; 6, 104; 6, 105; 6, 127; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:all_artifacts_per_task_id; 12, dictionary; 13, for_statement; 13, 14; 13, 15; 13, 18; 13, 19; 14, identifier:link; 15, attribute; 15, 16; 15, 17; 16, identifier:chain; 17, identifier:links; 18, comment; 19, block; 19, 20; 19, 42; 19, 43; 20, if_statement; 20, 21; 20, 26; 21, comparison_operator:in; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:link; 24, identifier:task_type; 25, identifier:PARENT_TASK_TYPES; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:add_enumerable_item_to_dict; 30, argument_list; 30, 31; 30, 34; 30, 39; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:dict_; 33, identifier:all_artifacts_per_task_id; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:key; 36, attribute; 36, 37; 36, 38; 37, identifier:link; 38, identifier:task_id; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:item; 41, string:'public/task-graph.json'; 42, comment; 43, if_statement; 43, 44; 43, 49; 44, comparison_operator:in; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:link; 47, identifier:task_type; 48, identifier:DECISION_TASK_TYPES; 49, block; 49, 50; 49, 65; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:add_enumerable_item_to_dict; 53, argument_list; 53, 54; 53, 57; 53, 62; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:dict_; 56, identifier:all_artifacts_per_task_id; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:key; 59, attribute; 59, 60; 59, 61; 60, identifier:link; 61, identifier:task_id; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:item; 64, string:'public/actions.json'; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:add_enumerable_item_to_dict; 68, argument_list; 68, 69; 68, 72; 68, 77; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:dict_; 71, identifier:all_artifacts_per_task_id; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:key; 74, attribute; 74, 75; 74, 76; 75, identifier:link; 76, identifier:task_id; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:item; 79, string:'public/parameters.yml'; 80, if_statement; 80, 81; 80, 82; 81, identifier:upstream_artifacts; 82, block; 82, 83; 83, for_statement; 83, 84; 83, 85; 83, 86; 84, identifier:upstream_dict; 85, identifier:upstream_artifacts; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:add_enumerable_item_to_dict; 90, argument_list; 90, 91; 90, 94; 90, 99; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:dict_; 93, identifier:all_artifacts_per_task_id; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:key; 96, subscript; 96, 97; 96, 98; 97, identifier:upstream_dict; 98, string:'taskId'; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:item; 101, subscript; 101, 102; 101, 103; 102, identifier:upstream_dict; 103, string:'paths'; 104, comment; 105, for_statement; 105, 106; 105, 109; 105, 114; 106, pattern_list; 106, 107; 106, 108; 107, identifier:task_id; 108, identifier:paths; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:all_artifacts_per_task_id; 112, identifier:items; 113, argument_list; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:all_artifacts_per_task_id; 119, identifier:task_id; 120, call; 120, 121; 120, 122; 121, identifier:sorted; 122, argument_list; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:set; 125, argument_list; 125, 126; 126, identifier:paths; 127, return_statement; 127, 128; 128, identifier:all_artifacts_per_task_id
def get_all_artifacts_per_task_id(chain, upstream_artifacts): """Return every artifact to download, including the Chain Of Trust Artifacts. Args: chain (ChainOfTrust): the chain of trust object upstream_artifacts: the list of upstream artifact definitions Returns: dict: sorted list of paths to downloaded artifacts ordered by taskId """ all_artifacts_per_task_id = {} for link in chain.links: # Download task-graph.json for decision+action task cot verification if link.task_type in PARENT_TASK_TYPES: add_enumerable_item_to_dict( dict_=all_artifacts_per_task_id, key=link.task_id, item='public/task-graph.json' ) # Download actions.json for decision+action task cot verification if link.task_type in DECISION_TASK_TYPES: add_enumerable_item_to_dict( dict_=all_artifacts_per_task_id, key=link.task_id, item='public/actions.json' ) add_enumerable_item_to_dict( dict_=all_artifacts_per_task_id, key=link.task_id, item='public/parameters.yml' ) if upstream_artifacts: for upstream_dict in upstream_artifacts: add_enumerable_item_to_dict( dict_=all_artifacts_per_task_id, key=upstream_dict['taskId'], item=upstream_dict['paths'] ) # Avoid duplicate paths per task_id for task_id, paths in all_artifacts_per_task_id.items(): all_artifacts_per_task_id[task_id] = sorted(set(paths)) return all_artifacts_per_task_id
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_upstream_artifacts_full_paths_per_task_id; 3, parameters; 3, 4; 4, identifier:context; 5, block; 5, 6; 5, 8; 5, 18; 5, 32; 5, 39; 5, 43; 5, 47; 5, 124; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:upstream_artifacts; 11, subscript; 11, 12; 11, 17; 12, subscript; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:context; 15, identifier:task; 16, string:'payload'; 17, string:'upstreamArtifacts'; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:task_ids_and_relative_paths; 21, list_comprehension; 21, 22; 21, 29; 22, tuple; 22, 23; 22, 26; 23, subscript; 23, 24; 23, 25; 24, identifier:artifact_definition; 25, string:'taskId'; 26, subscript; 26, 27; 26, 28; 27, identifier:artifact_definition; 28, string:'paths'; 29, for_in_clause; 29, 30; 29, 31; 30, identifier:artifact_definition; 31, identifier:upstream_artifacts; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:optional_artifacts_per_task_id; 35, call; 35, 36; 35, 37; 36, identifier:get_optional_artifacts_per_task_id; 37, argument_list; 37, 38; 38, identifier:upstream_artifacts; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:upstream_artifacts_full_paths_per_task_id; 42, dictionary; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:failed_paths_per_task_id; 46, dictionary; 47, for_statement; 47, 48; 47, 51; 47, 52; 48, pattern_list; 48, 49; 48, 50; 49, identifier:task_id; 50, identifier:paths; 51, identifier:task_ids_and_relative_paths; 52, block; 52, 53; 53, for_statement; 53, 54; 53, 55; 53, 56; 54, identifier:path; 55, identifier:paths; 56, block; 56, 57; 57, try_statement; 57, 58; 57, 81; 58, block; 58, 59; 58, 68; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:path_to_add; 62, call; 62, 63; 62, 64; 63, identifier:get_and_check_single_upstream_artifact_full_path; 64, argument_list; 64, 65; 64, 66; 64, 67; 65, identifier:context; 66, identifier:task_id; 67, identifier:path; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:add_enumerable_item_to_dict; 71, argument_list; 71, 72; 71, 75; 71, 78; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:dict_; 74, identifier:upstream_artifacts_full_paths_per_task_id; 75, keyword_argument; 75, 76; 75, 77; 76, identifier:key; 77, identifier:task_id; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:item; 80, identifier:path_to_add; 81, except_clause; 81, 82; 81, 83; 82, identifier:ScriptWorkerTaskException; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 94; 84, 121; 85, comparison_operator:in; 85, 86; 85, 87; 86, identifier:path; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:optional_artifacts_per_task_id; 90, identifier:get; 91, argument_list; 91, 92; 91, 93; 92, identifier:task_id; 93, list:[]; 94, block; 94, 95; 94, 108; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:log; 99, identifier:warning; 100, argument_list; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, string:'Optional artifact "{}" of task "{}" not found'; 104, identifier:format; 105, argument_list; 105, 106; 105, 107; 106, identifier:path; 107, identifier:task_id; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:add_enumerable_item_to_dict; 111, argument_list; 111, 112; 111, 115; 111, 118; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:dict_; 114, identifier:failed_paths_per_task_id; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:key; 117, identifier:task_id; 118, keyword_argument; 118, 119; 118, 120; 119, identifier:item; 120, identifier:path; 121, else_clause; 121, 122; 122, block; 122, 123; 123, raise_statement; 124, return_statement; 124, 125; 125, expression_list; 125, 126; 125, 127; 126, identifier:upstream_artifacts_full_paths_per_task_id; 127, identifier:failed_paths_per_task_id
def get_upstream_artifacts_full_paths_per_task_id(context): """List the downloaded upstream artifacts. Args: context (scriptworker.context.Context): the scriptworker context. Returns: dict, dict: lists of the paths to upstream artifacts, sorted by task_id. First dict represents the existing upstream artifacts. The second one maps the optional artifacts that couldn't be downloaded Raises: scriptworker.exceptions.ScriptWorkerTaskException: when an artifact doesn't exist. """ upstream_artifacts = context.task['payload']['upstreamArtifacts'] task_ids_and_relative_paths = [ (artifact_definition['taskId'], artifact_definition['paths']) for artifact_definition in upstream_artifacts ] optional_artifacts_per_task_id = get_optional_artifacts_per_task_id(upstream_artifacts) upstream_artifacts_full_paths_per_task_id = {} failed_paths_per_task_id = {} for task_id, paths in task_ids_and_relative_paths: for path in paths: try: path_to_add = get_and_check_single_upstream_artifact_full_path(context, task_id, path) add_enumerable_item_to_dict( dict_=upstream_artifacts_full_paths_per_task_id, key=task_id, item=path_to_add ) except ScriptWorkerTaskException: if path in optional_artifacts_per_task_id.get(task_id, []): log.warning('Optional artifact "{}" of task "{}" not found'.format(path, task_id)) add_enumerable_item_to_dict( dict_=failed_paths_per_task_id, key=task_id, item=path ) else: raise return upstream_artifacts_full_paths_per_task_id, failed_paths_per_task_id
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_PreparedData; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:order_by; 7, tuple; 8, block; 8, 9; 8, 11; 8, 19; 8, 28; 8, 68; 8, 177; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 14; 12, not_operator; 12, 13; 13, identifier:order_by; 14, block; 14, 15; 15, return_statement; 15, 16; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:__data; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:sorted_data; 22, subscript; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:__data; 26, slice; 26, 27; 27, colon; 28, if_statement; 28, 29; 28, 62; 29, boolean_operator:or; 29, 30; 29, 37; 30, call; 30, 31; 30, 32; 31, identifier:isinstance; 32, argument_list; 32, 33; 32, 34; 33, identifier:order_by; 34, attribute; 34, 35; 34, 36; 35, identifier:six; 36, identifier:string_types; 37, parenthesized_expression; 37, 38; 38, boolean_operator:and; 38, 39; 38, 51; 39, boolean_operator:and; 39, 40; 39, 45; 40, call; 40, 41; 40, 42; 41, identifier:isinstance; 42, argument_list; 42, 43; 42, 44; 43, identifier:order_by; 44, identifier:tuple; 45, comparison_operator:==; 45, 46; 45, 50; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, identifier:order_by; 50, integer:2; 51, comparison_operator:in; 51, 52; 51, 59; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:order_by; 56, integer:1; 57, identifier:lower; 58, argument_list; 59, list:["asc", "desc"]; 59, 60; 59, 61; 60, string:"asc"; 61, string:"desc"; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:order_by; 66, tuple; 66, 67; 67, identifier:order_by; 68, for_statement; 68, 69; 68, 70; 68, 74; 69, identifier:key; 70, call; 70, 71; 70, 72; 71, identifier:reversed; 72, argument_list; 72, 73; 73, identifier:order_by; 74, block; 74, 75; 75, if_statement; 75, 76; 75, 83; 75, 103; 75, 168; 76, call; 76, 77; 76, 78; 77, identifier:isinstance; 78, argument_list; 78, 79; 78, 80; 79, identifier:key; 80, attribute; 80, 81; 80, 82; 81, identifier:six; 82, identifier:string_types; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:sorted_data; 88, identifier:sort; 89, argument_list; 89, 90; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:key; 92, lambda; 92, 93; 92, 95; 93, lambda_parameters; 93, 94; 94, identifier:x; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:x; 99, integer:0; 100, identifier:get; 101, argument_list; 101, 102; 102, identifier:key; 103, elif_clause; 103, 104; 103, 131; 104, parenthesized_expression; 104, 105; 105, boolean_operator:and; 105, 106; 105, 120; 106, boolean_operator:and; 106, 107; 106, 114; 107, call; 107, 108; 107, 109; 108, identifier:isinstance; 109, argument_list; 109, 110; 109, 111; 110, identifier:key; 111, tuple; 111, 112; 111, 113; 112, identifier:list; 113, identifier:tuple; 114, comparison_operator:==; 114, 115; 114, 119; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, identifier:key; 119, integer:2; 120, comparison_operator:in; 120, 121; 120, 128; 121, call; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, subscript; 123, 124; 123, 125; 124, identifier:key; 125, integer:1; 126, identifier:lower; 127, argument_list; 128, tuple; 128, 129; 128, 130; 129, string:"asc"; 130, string:"desc"; 131, block; 131, 132; 131, 148; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:key_func; 135, lambda; 135, 136; 135, 138; 136, lambda_parameters; 136, 137; 137, identifier:x; 138, call; 138, 139; 138, 144; 139, attribute; 139, 140; 139, 143; 140, subscript; 140, 141; 140, 142; 141, identifier:x; 142, integer:0; 143, identifier:get; 144, argument_list; 144, 145; 145, subscript; 145, 146; 145, 147; 146, identifier:key; 147, integer:0; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:sorted_data; 152, identifier:sort; 153, argument_list; 153, 154; 153, 157; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:key; 156, identifier:key_func; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:reverse; 159, comparison_operator:!=; 159, 160; 159, 167; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:key; 164, integer:1; 165, identifier:lower; 166, argument_list; 167, string:"asc"; 168, else_clause; 168, 169; 169, block; 169, 170; 170, raise_statement; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:DataTableException; 173, argument_list; 173, 174; 174, concatenated_string; 174, 175; 174, 176; 175, string:"Expected tuple with second value: "; 176, string:"'asc' or 'desc'"; 177, return_statement; 177, 178; 178, identifier:sorted_data
def _PreparedData(self, order_by=()): """Prepares the data for enumeration - sorting it by order_by. Args: order_by: Optional. Specifies the name of the column(s) to sort by, and (optionally) which direction to sort in. Default sort direction is asc. Following formats are accepted: "string_col_name" -- For a single key in default (asc) order. ("string_col_name", "asc|desc") -- For a single key. [("col_1","asc|desc"), ("col_2","asc|desc")] -- For more than one column, an array of tuples of (col_name, "asc|desc"). Returns: The data sorted by the keys given. Raises: DataTableException: Sort direction not in 'asc' or 'desc' """ if not order_by: return self.__data sorted_data = self.__data[:] if isinstance(order_by, six.string_types) or ( isinstance(order_by, tuple) and len(order_by) == 2 and order_by[1].lower() in ["asc", "desc"]): order_by = (order_by,) for key in reversed(order_by): if isinstance(key, six.string_types): sorted_data.sort(key=lambda x: x[0].get(key)) elif (isinstance(key, (list, tuple)) and len(key) == 2 and key[1].lower() in ("asc", "desc")): key_func = lambda x: x[0].get(key[0]) sorted_data.sort(key=key_func, reverse=key[1].lower() != "asc") else: raise DataTableException("Expected tuple with second value: " "'asc' or 'desc'") return sorted_data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:ToJSCode; 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:columns_order; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:order_by; 11, tuple; 12, block; 12, 13; 12, 15; 12, 21; 12, 38; 12, 55; 12, 56; 12, 62; 12, 82; 12, 83; 12, 154; 12, 167; 12, 168; 12, 321; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:encoder; 18, call; 18, 19; 18, 20; 19, identifier:DataTableJSONEncoder; 20, argument_list; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:columns_order; 24, None; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:columns_order; 29, list_comprehension; 29, 30; 29, 33; 30, subscript; 30, 31; 30, 32; 31, identifier:col; 32, string:"id"; 33, for_in_clause; 33, 34; 33, 35; 34, identifier:col; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:__columns; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:col_dict; 41, call; 41, 42; 41, 43; 42, identifier:dict; 43, argument_list; 43, 44; 44, list_comprehension; 44, 45; 44, 50; 45, tuple; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:col; 48, string:"id"; 49, identifier:col; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:col; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:__columns; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:jscode; 59, binary_operator:%; 59, 60; 59, 61; 60, string:"var %s = new google.visualization.DataTable();\n"; 61, identifier:name; 62, if_statement; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:custom_properties; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, augmented_assignment:+=; 68, 69; 68, 70; 69, identifier:jscode; 70, binary_operator:%; 70, 71; 70, 72; 71, string:"%s.setTableProperties(%s);\n"; 72, tuple; 72, 73; 72, 74; 73, identifier:name; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:encoder; 77, identifier:encode; 78, argument_list; 78, 79; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:custom_properties; 82, comment; 83, for_statement; 83, 84; 83, 87; 83, 91; 84, pattern_list; 84, 85; 84, 86; 85, identifier:i; 86, identifier:col; 87, call; 87, 88; 87, 89; 88, identifier:enumerate; 89, argument_list; 89, 90; 90, identifier:columns_order; 91, block; 91, 92; 91, 129; 92, expression_statement; 92, 93; 93, augmented_assignment:+=; 93, 94; 93, 95; 94, identifier:jscode; 95, binary_operator:%; 95, 96; 95, 97; 96, string:"%s.addColumn(%s, %s, %s);\n"; 97, tuple; 97, 98; 97, 99; 97, 109; 97, 119; 98, identifier:name; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:encoder; 102, identifier:encode; 103, argument_list; 103, 104; 104, subscript; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:col_dict; 107, identifier:col; 108, string:"type"; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:encoder; 112, identifier:encode; 113, argument_list; 113, 114; 114, subscript; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:col_dict; 117, identifier:col; 118, string:"label"; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:encoder; 122, identifier:encode; 123, argument_list; 123, 124; 124, subscript; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:col_dict; 127, identifier:col; 128, string:"id"; 129, if_statement; 129, 130; 129, 135; 130, subscript; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:col_dict; 133, identifier:col; 134, string:"custom_properties"; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, augmented_assignment:+=; 137, 138; 137, 139; 138, identifier:jscode; 139, binary_operator:%; 139, 140; 139, 141; 140, string:"%s.setColumnProperties(%d, %s);\n"; 141, tuple; 141, 142; 141, 143; 141, 144; 142, identifier:name; 143, identifier:i; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:encoder; 147, identifier:encode; 148, argument_list; 148, 149; 149, subscript; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:col_dict; 152, identifier:col; 153, string:"custom_properties"; 154, expression_statement; 154, 155; 155, augmented_assignment:+=; 155, 156; 155, 157; 156, identifier:jscode; 157, binary_operator:%; 157, 158; 157, 159; 158, string:"%s.addRows(%d);\n"; 159, tuple; 159, 160; 159, 161; 160, identifier:name; 161, call; 161, 162; 161, 163; 162, identifier:len; 163, argument_list; 163, 164; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:__data; 167, comment; 168, for_statement; 168, 169; 168, 174; 168, 183; 168, 184; 169, tuple_pattern; 169, 170; 169, 171; 170, identifier:i; 171, tuple_pattern; 171, 172; 171, 173; 172, identifier:row; 173, identifier:cp; 174, call; 174, 175; 174, 176; 175, identifier:enumerate; 176, argument_list; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:_PreparedData; 181, argument_list; 181, 182; 182, identifier:order_by; 183, comment; 184, block; 184, 185; 184, 304; 185, for_statement; 185, 186; 185, 189; 185, 193; 186, tuple_pattern; 186, 187; 186, 188; 187, identifier:j; 188, identifier:col; 189, call; 189, 190; 189, 191; 190, identifier:enumerate; 191, argument_list; 191, 192; 192, identifier:columns_order; 193, block; 193, 194; 193, 206; 193, 222; 194, if_statement; 194, 195; 194, 204; 195, boolean_operator:or; 195, 196; 195, 199; 196, comparison_operator:not; 196, 197; 196, 198; 197, identifier:col; 198, identifier:row; 199, comparison_operator:is; 199, 200; 199, 203; 200, subscript; 200, 201; 200, 202; 201, identifier:row; 202, identifier:col; 203, None; 204, block; 204, 205; 205, continue_statement; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:value; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:self; 212, identifier:CoerceValue; 213, argument_list; 213, 214; 213, 217; 214, subscript; 214, 215; 214, 216; 215, identifier:row; 216, identifier:col; 217, subscript; 217, 218; 217, 221; 218, subscript; 218, 219; 218, 220; 219, identifier:col_dict; 220, identifier:col; 221, string:"type"; 222, if_statement; 222, 223; 222, 228; 222, 286; 223, call; 223, 224; 223, 225; 224, identifier:isinstance; 225, argument_list; 225, 226; 225, 227; 226, identifier:value; 227, identifier:tuple; 228, block; 228, 229; 228, 233; 228, 256; 228, 257; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:cell_cp; 232, string:""; 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:value; 239, integer:3; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:cell_cp; 244, binary_operator:%; 244, 245; 244, 246; 245, string:", %s"; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:encoder; 249, identifier:encode; 250, argument_list; 250, 251; 251, subscript; 251, 252; 251, 255; 252, subscript; 252, 253; 252, 254; 253, identifier:row; 254, identifier:col; 255, integer:2; 256, comment; 257, expression_statement; 257, 258; 258, augmented_assignment:+=; 258, 259; 258, 260; 259, identifier:jscode; 260, parenthesized_expression; 260, 261; 261, binary_operator:%; 261, 262; 261, 263; 262, string:"%s.setCell(%d, %d, %s, %s%s);\n"; 263, tuple; 263, 264; 263, 265; 263, 266; 263, 267; 263, 276; 263, 285; 264, identifier:name; 265, identifier:i; 266, identifier:j; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:EscapeForJSCode; 271, argument_list; 271, 272; 271, 273; 272, identifier:encoder; 273, subscript; 273, 274; 273, 275; 274, identifier:value; 275, integer:0; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:self; 279, identifier:EscapeForJSCode; 280, argument_list; 280, 281; 280, 282; 281, identifier:encoder; 282, subscript; 282, 283; 282, 284; 283, identifier:value; 284, integer:1; 285, identifier:cell_cp; 286, else_clause; 286, 287; 287, block; 287, 288; 288, expression_statement; 288, 289; 289, augmented_assignment:+=; 289, 290; 289, 291; 290, identifier:jscode; 291, binary_operator:%; 291, 292; 291, 293; 292, string:"%s.setCell(%d, %d, %s);\n"; 293, tuple; 293, 294; 293, 295; 293, 296; 293, 297; 294, identifier:name; 295, identifier:i; 296, identifier:j; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:self; 300, identifier:EscapeForJSCode; 301, argument_list; 301, 302; 301, 303; 302, identifier:encoder; 303, identifier:value; 304, if_statement; 304, 305; 304, 306; 305, identifier:cp; 306, block; 306, 307; 307, expression_statement; 307, 308; 308, augmented_assignment:+=; 308, 309; 308, 310; 309, identifier:jscode; 310, binary_operator:%; 310, 311; 310, 312; 311, string:"%s.setRowProperties(%d, %s);\n"; 312, tuple; 312, 313; 312, 314; 312, 315; 313, identifier:name; 314, identifier:i; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:encoder; 318, identifier:encode; 319, argument_list; 319, 320; 320, identifier:cp; 321, return_statement; 321, 322; 322, identifier:jscode
def ToJSCode(self, name, columns_order=None, order_by=()): """Writes the data table as a JS code string. This method writes a string of JS code that can be run to generate a DataTable with the specified data. Typically used for debugging only. Args: name: The name of the table. The name would be used as the DataTable's variable name in the created JS code. columns_order: Optional. Specifies the order of columns in the output table. Specify a list of all column IDs in the order in which you want the table created. Note that you must list all column IDs in this parameter, if you use it. order_by: Optional. Specifies the name of the column(s) to sort by. Passed as is to _PreparedData. Returns: A string of JS code that, when run, generates a DataTable with the given name and the data stored in the DataTable object. Example result: "var tab1 = new google.visualization.DataTable(); tab1.addColumn("string", "a", "a"); tab1.addColumn("number", "b", "b"); tab1.addColumn("boolean", "c", "c"); tab1.addRows(10); tab1.setCell(0, 0, "a"); tab1.setCell(0, 1, 1, null, {"foo": "bar"}); tab1.setCell(0, 2, true); ... tab1.setCell(9, 0, "c"); tab1.setCell(9, 1, 3, "3$"); tab1.setCell(9, 2, false);" Raises: DataTableException: The data does not match the type. """ encoder = DataTableJSONEncoder() if columns_order is None: columns_order = [col["id"] for col in self.__columns] col_dict = dict([(col["id"], col) for col in self.__columns]) # We first create the table with the given name jscode = "var %s = new google.visualization.DataTable();\n" % name if self.custom_properties: jscode += "%s.setTableProperties(%s);\n" % ( name, encoder.encode(self.custom_properties)) # We add the columns to the table for i, col in enumerate(columns_order): jscode += "%s.addColumn(%s, %s, %s);\n" % ( name, encoder.encode(col_dict[col]["type"]), encoder.encode(col_dict[col]["label"]), encoder.encode(col_dict[col]["id"])) if col_dict[col]["custom_properties"]: jscode += "%s.setColumnProperties(%d, %s);\n" % ( name, i, encoder.encode(col_dict[col]["custom_properties"])) jscode += "%s.addRows(%d);\n" % (name, len(self.__data)) # We now go over the data and add each row for (i, (row, cp)) in enumerate(self._PreparedData(order_by)): # We add all the elements of this row by their order for (j, col) in enumerate(columns_order): if col not in row or row[col] is None: continue value = self.CoerceValue(row[col], col_dict[col]["type"]) if isinstance(value, tuple): cell_cp = "" if len(value) == 3: cell_cp = ", %s" % encoder.encode(row[col][2]) # We have a formatted value or custom property as well jscode += ("%s.setCell(%d, %d, %s, %s%s);\n" % (name, i, j, self.EscapeForJSCode(encoder, value[0]), self.EscapeForJSCode(encoder, value[1]), cell_cp)) else: jscode += "%s.setCell(%d, %d, %s);\n" % ( name, i, j, self.EscapeForJSCode(encoder, value)) if cp: jscode += "%s.setRowProperties(%d, %s);\n" % ( name, i, encoder.encode(cp)) return jscode
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:ToHtml; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:columns_order; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:order_by; 10, tuple; 11, block; 11, 12; 11, 14; 11, 18; 11, 22; 11, 26; 11, 30; 11, 34; 11, 38; 11, 55; 11, 72; 11, 76; 11, 98; 11, 109; 11, 113; 11, 114; 11, 230; 11, 241; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:table_template; 17, string:"<html><body><table border=\"1\">%s</table></body></html>"; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:columns_template; 21, string:"<thead><tr>%s</tr></thead>"; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:rows_template; 25, string:"<tbody>%s</tbody>"; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:row_template; 29, string:"<tr>%s</tr>"; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:header_cell_template; 33, string:"<th>%s</th>"; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:cell_template; 37, string:"<td>%s</td>"; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:is; 39, 40; 39, 41; 40, identifier:columns_order; 41, None; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:columns_order; 46, list_comprehension; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:col; 49, string:"id"; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:col; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:__columns; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:col_dict; 58, call; 58, 59; 58, 60; 59, identifier:dict; 60, argument_list; 60, 61; 61, list_comprehension; 61, 62; 61, 67; 62, tuple; 62, 63; 62, 66; 63, subscript; 63, 64; 63, 65; 64, identifier:col; 65, string:"id"; 66, identifier:col; 67, for_in_clause; 67, 68; 67, 69; 68, identifier:col; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:__columns; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:columns_list; 75, list:[]; 76, for_statement; 76, 77; 76, 78; 76, 79; 77, identifier:col; 78, identifier:columns_order; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:columns_list; 84, identifier:append; 85, argument_list; 85, 86; 86, binary_operator:%; 86, 87; 86, 88; 87, identifier:header_cell_template; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:html; 91, identifier:escape; 92, argument_list; 92, 93; 93, subscript; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:col_dict; 96, identifier:col; 97, string:"label"; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:columns_html; 101, binary_operator:%; 101, 102; 101, 103; 102, identifier:columns_template; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, string:""; 106, identifier:join; 107, argument_list; 107, 108; 108, identifier:columns_list; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:rows_list; 112, list:[]; 113, comment; 114, for_statement; 114, 115; 114, 118; 114, 124; 115, pattern_list; 115, 116; 115, 117; 116, identifier:row; 117, identifier:unused_cp; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_PreparedData; 122, argument_list; 122, 123; 123, identifier:order_by; 124, block; 124, 125; 124, 129; 124, 130; 124, 216; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:cells_list; 128, list:[]; 129, comment; 130, for_statement; 130, 131; 130, 132; 130, 133; 130, 134; 131, identifier:col; 132, identifier:columns_order; 133, comment; 134, block; 134, 135; 134, 139; 134, 166; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:value; 138, string:""; 139, if_statement; 139, 140; 139, 149; 140, boolean_operator:and; 140, 141; 140, 144; 141, comparison_operator:in; 141, 142; 141, 143; 142, identifier:col; 143, identifier:row; 144, comparison_operator:is; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:row; 147, identifier:col; 148, None; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:value; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:CoerceValue; 157, argument_list; 157, 158; 157, 161; 158, subscript; 158, 159; 158, 160; 159, identifier:row; 160, identifier:col; 161, subscript; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:col_dict; 164, identifier:col; 165, string:"type"; 166, if_statement; 166, 167; 166, 172; 166, 173; 166, 195; 167, call; 167, 168; 167, 169; 168, identifier:isinstance; 169, argument_list; 169, 170; 169, 171; 170, identifier:value; 171, identifier:tuple; 172, comment; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:cells_list; 178, identifier:append; 179, argument_list; 179, 180; 180, binary_operator:%; 180, 181; 180, 182; 181, identifier:cell_template; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:html; 185, identifier:escape; 186, argument_list; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:self; 190, identifier:ToString; 191, argument_list; 191, 192; 192, subscript; 192, 193; 192, 194; 193, identifier:value; 194, integer:1; 195, else_clause; 195, 196; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:cells_list; 201, identifier:append; 202, argument_list; 202, 203; 203, binary_operator:%; 203, 204; 203, 205; 204, identifier:cell_template; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:html; 208, identifier:escape; 209, argument_list; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:self; 213, identifier:ToString; 214, argument_list; 214, 215; 215, identifier:value; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:rows_list; 220, identifier:append; 221, argument_list; 221, 222; 222, binary_operator:%; 222, 223; 222, 224; 223, identifier:row_template; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, string:""; 227, identifier:join; 228, argument_list; 228, 229; 229, identifier:cells_list; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:rows_html; 233, binary_operator:%; 233, 234; 233, 235; 234, identifier:rows_template; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, string:""; 238, identifier:join; 239, argument_list; 239, 240; 240, identifier:rows_list; 241, return_statement; 241, 242; 242, binary_operator:%; 242, 243; 242, 244; 243, identifier:table_template; 244, parenthesized_expression; 244, 245; 245, binary_operator:+; 245, 246; 245, 247; 246, identifier:columns_html; 247, identifier:rows_html
def ToHtml(self, columns_order=None, order_by=()): """Writes the data table as an HTML table code string. Args: columns_order: Optional. Specifies the order of columns in the output table. Specify a list of all column IDs in the order in which you want the table created. Note that you must list all column IDs in this parameter, if you use it. order_by: Optional. Specifies the name of the column(s) to sort by. Passed as is to _PreparedData. Returns: An HTML table code string. Example result (the result is without the newlines): <html><body><table border="1"> <thead><tr><th>a</th><th>b</th><th>c</th></tr></thead> <tbody> <tr><td>1</td><td>"z"</td><td>2</td></tr> <tr><td>"3$"</td><td>"w"</td><td></td></tr> </tbody> </table></body></html> Raises: DataTableException: The data does not match the type. """ table_template = "<html><body><table border=\"1\">%s</table></body></html>" columns_template = "<thead><tr>%s</tr></thead>" rows_template = "<tbody>%s</tbody>" row_template = "<tr>%s</tr>" header_cell_template = "<th>%s</th>" cell_template = "<td>%s</td>" if columns_order is None: columns_order = [col["id"] for col in self.__columns] col_dict = dict([(col["id"], col) for col in self.__columns]) columns_list = [] for col in columns_order: columns_list.append(header_cell_template % html.escape(col_dict[col]["label"])) columns_html = columns_template % "".join(columns_list) rows_list = [] # We now go over the data and add each row for row, unused_cp in self._PreparedData(order_by): cells_list = [] # We add all the elements of this row by their order for col in columns_order: # For empty string we want empty quotes (""). value = "" if col in row and row[col] is not None: value = self.CoerceValue(row[col], col_dict[col]["type"]) if isinstance(value, tuple): # We have a formatted value and we're going to use it cells_list.append(cell_template % html.escape(self.ToString(value[1]))) else: cells_list.append(cell_template % html.escape(self.ToString(value))) rows_list.append(row_template % "".join(cells_list)) rows_html = rows_template % "".join(rows_list) return table_template % (columns_html + rows_html)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:ToCsv; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:columns_order; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:order_by; 10, tuple; 11, default_parameter; 11, 12; 11, 13; 12, identifier:separator; 13, string:","; 14, block; 14, 15; 14, 17; 14, 25; 14, 37; 14, 54; 14, 71; 14, 94; 14, 112; 14, 113; 14, 244; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:csv_buffer; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:six; 23, identifier:StringIO; 24, argument_list; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:writer; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:csv; 31, identifier:writer; 32, argument_list; 32, 33; 32, 34; 33, identifier:csv_buffer; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:delimiter; 36, identifier:separator; 37, if_statement; 37, 38; 37, 41; 38, comparison_operator:is; 38, 39; 38, 40; 39, identifier:columns_order; 40, None; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:columns_order; 45, list_comprehension; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:col; 48, string:"id"; 49, for_in_clause; 49, 50; 49, 51; 50, identifier:col; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:__columns; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:col_dict; 57, call; 57, 58; 57, 59; 58, identifier:dict; 59, argument_list; 59, 60; 60, list_comprehension; 60, 61; 60, 66; 61, tuple; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:col; 64, string:"id"; 65, identifier:col; 66, for_in_clause; 66, 67; 66, 68; 67, identifier:col; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:__columns; 71, function_definition; 71, 72; 71, 73; 71, 75; 72, function_name:ensure_str; 73, parameters; 73, 74; 74, identifier:s; 75, block; 75, 76; 75, 78; 75, 87; 76, expression_statement; 76, 77; 77, string:"Compatibility function. Ensures using of str rather than unicode."; 78, if_statement; 78, 79; 78, 84; 79, call; 79, 80; 79, 81; 80, identifier:isinstance; 81, argument_list; 81, 82; 81, 83; 82, identifier:s; 83, identifier:str; 84, block; 84, 85; 85, return_statement; 85, 86; 86, identifier:s; 87, return_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:s; 91, identifier:encode; 92, argument_list; 92, 93; 93, string:"utf-8"; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:writer; 98, identifier:writerow; 99, argument_list; 99, 100; 100, list_comprehension; 100, 101; 100, 109; 101, call; 101, 102; 101, 103; 102, identifier:ensure_str; 103, argument_list; 103, 104; 104, subscript; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:col_dict; 107, identifier:col; 108, string:"label"; 109, for_in_clause; 109, 110; 109, 111; 110, identifier:col; 111, identifier:columns_order; 112, comment; 113, for_statement; 113, 114; 113, 117; 113, 123; 114, pattern_list; 114, 115; 114, 116; 115, identifier:row; 116, identifier:unused_cp; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:_PreparedData; 121, argument_list; 121, 122; 122, identifier:order_by; 123, block; 123, 124; 123, 128; 123, 129; 123, 237; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:cells_list; 127, list:[]; 128, comment; 129, for_statement; 129, 130; 129, 131; 129, 132; 130, identifier:col; 131, identifier:columns_order; 132, block; 132, 133; 132, 137; 132, 164; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:value; 136, string:""; 137, if_statement; 137, 138; 137, 147; 138, boolean_operator:and; 138, 139; 138, 142; 139, comparison_operator:in; 139, 140; 139, 141; 140, identifier:col; 141, identifier:row; 142, comparison_operator:is; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:row; 145, identifier:col; 146, None; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:value; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:CoerceValue; 155, argument_list; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:row; 158, identifier:col; 159, subscript; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:col_dict; 162, identifier:col; 163, string:"type"; 164, if_statement; 164, 165; 164, 170; 164, 171; 164, 220; 165, call; 165, 166; 165, 167; 166, identifier:isinstance; 167, argument_list; 167, 168; 167, 169; 168, identifier:value; 169, identifier:tuple; 170, comment; 171, block; 171, 172; 172, if_statement; 172, 173; 172, 183; 172, 201; 173, comparison_operator:in; 173, 174; 173, 179; 174, subscript; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:col_dict; 177, identifier:col; 178, string:"type"; 179, list:["date", "datetime", "timeofday"]; 179, 180; 179, 181; 179, 182; 180, string:"date"; 181, string:"datetime"; 182, string:"timeofday"; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:cells_list; 188, identifier:append; 189, argument_list; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:ensure_str; 192, argument_list; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:ToString; 197, argument_list; 197, 198; 198, subscript; 198, 199; 198, 200; 199, identifier:value; 200, integer:1; 201, else_clause; 201, 202; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:cells_list; 207, identifier:append; 208, argument_list; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:ensure_str; 211, argument_list; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:ToString; 216, argument_list; 216, 217; 217, subscript; 217, 218; 217, 219; 218, identifier:value; 219, integer:0; 220, else_clause; 220, 221; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:cells_list; 226, identifier:append; 227, argument_list; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:ensure_str; 230, argument_list; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:self; 234, identifier:ToString; 235, argument_list; 235, 236; 236, identifier:value; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:writer; 241, identifier:writerow; 242, argument_list; 242, 243; 243, identifier:cells_list; 244, return_statement; 244, 245; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:csv_buffer; 248, identifier:getvalue; 249, argument_list
def ToCsv(self, columns_order=None, order_by=(), separator=","): """Writes the data table as a CSV string. Output is encoded in UTF-8 because the Python "csv" module can't handle Unicode properly according to its documentation. Args: columns_order: Optional. Specifies the order of columns in the output table. Specify a list of all column IDs in the order in which you want the table created. Note that you must list all column IDs in this parameter, if you use it. order_by: Optional. Specifies the name of the column(s) to sort by. Passed as is to _PreparedData. separator: Optional. The separator to use between the values. Returns: A CSV string representing the table. Example result: 'a','b','c' 1,'z',2 3,'w','' Raises: DataTableException: The data does not match the type. """ csv_buffer = six.StringIO() writer = csv.writer(csv_buffer, delimiter=separator) if columns_order is None: columns_order = [col["id"] for col in self.__columns] col_dict = dict([(col["id"], col) for col in self.__columns]) def ensure_str(s): "Compatibility function. Ensures using of str rather than unicode." if isinstance(s, str): return s return s.encode("utf-8") writer.writerow([ensure_str(col_dict[col]["label"]) for col in columns_order]) # We now go over the data and add each row for row, unused_cp in self._PreparedData(order_by): cells_list = [] # We add all the elements of this row by their order for col in columns_order: value = "" if col in row and row[col] is not None: value = self.CoerceValue(row[col], col_dict[col]["type"]) if isinstance(value, tuple): # We have a formatted value. Using it only for date/time types. if col_dict[col]["type"] in ["date", "datetime", "timeofday"]: cells_list.append(ensure_str(self.ToString(value[1]))) else: cells_list.append(ensure_str(self.ToString(value[0]))) else: cells_list.append(ensure_str(self.ToString(value))) writer.writerow(cells_list) return csv_buffer.getvalue()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:_ToJSonObj; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:columns_order; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:order_by; 10, tuple; 11, block; 11, 12; 11, 14; 11, 31; 11, 48; 11, 49; 11, 53; 11, 106; 11, 107; 11, 111; 11, 252; 11, 262; 11, 275; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 18; 15, comparison_operator:is; 15, 16; 15, 17; 16, identifier:columns_order; 17, None; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:columns_order; 22, list_comprehension; 22, 23; 22, 26; 23, subscript; 23, 24; 23, 25; 24, identifier:col; 25, string:"id"; 26, for_in_clause; 26, 27; 26, 28; 27, identifier:col; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:__columns; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:col_dict; 34, call; 34, 35; 34, 36; 35, identifier:dict; 36, argument_list; 36, 37; 37, list_comprehension; 37, 38; 37, 43; 38, tuple; 38, 39; 38, 42; 39, subscript; 39, 40; 39, 41; 40, identifier:col; 41, string:"id"; 42, identifier:col; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:col; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:__columns; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:col_objs; 52, list:[]; 53, for_statement; 53, 54; 53, 55; 53, 56; 54, identifier:col_id; 55, identifier:columns_order; 56, block; 56, 57; 56, 82; 56, 99; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:col_obj; 60, dictionary; 60, 61; 60, 68; 60, 75; 61, pair; 61, 62; 61, 63; 62, string:"id"; 63, subscript; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:col_dict; 66, identifier:col_id; 67, string:"id"; 68, pair; 68, 69; 68, 70; 69, string:"label"; 70, subscript; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:col_dict; 73, identifier:col_id; 74, string:"label"; 75, pair; 75, 76; 75, 77; 76, string:"type"; 77, subscript; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:col_dict; 80, identifier:col_id; 81, string:"type"; 82, if_statement; 82, 83; 82, 88; 83, subscript; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:col_dict; 86, identifier:col_id; 87, string:"custom_properties"; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:col_obj; 93, string:"p"; 94, subscript; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:col_dict; 97, identifier:col_id; 98, string:"custom_properties"; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:col_objs; 103, identifier:append; 104, argument_list; 104, 105; 105, identifier:col_obj; 106, comment; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:row_objs; 110, list:[]; 111, for_statement; 111, 112; 111, 115; 111, 121; 112, pattern_list; 112, 113; 112, 114; 113, identifier:row; 114, identifier:cp; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_PreparedData; 119, argument_list; 119, 120; 120, identifier:order_by; 121, block; 121, 122; 121, 126; 121, 229; 121, 236; 121, 245; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:cell_objs; 125, list:[]; 126, for_statement; 126, 127; 126, 128; 126, 129; 127, identifier:col; 128, identifier:columns_order; 129, block; 129, 130; 129, 150; 129, 222; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:value; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:CoerceValue; 137, argument_list; 137, 138; 137, 145; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:row; 141, identifier:get; 142, argument_list; 142, 143; 142, 144; 143, identifier:col; 144, None; 145, subscript; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:col_dict; 148, identifier:col; 149, string:"type"; 150, if_statement; 150, 151; 150, 154; 150, 159; 150, 213; 151, comparison_operator:is; 151, 152; 151, 153; 152, identifier:value; 153, None; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:cell_obj; 158, None; 159, elif_clause; 159, 160; 159, 165; 160, call; 160, 161; 160, 162; 161, identifier:isinstance; 162, argument_list; 162, 163; 162, 164; 163, identifier:value; 164, identifier:tuple; 165, block; 165, 166; 165, 175; 165, 197; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:cell_obj; 169, dictionary; 169, 170; 170, pair; 170, 171; 170, 172; 171, string:"v"; 172, subscript; 172, 173; 172, 174; 173, identifier:value; 174, integer:0; 175, if_statement; 175, 176; 175, 188; 176, boolean_operator:and; 176, 177; 176, 183; 177, comparison_operator:>; 177, 178; 177, 182; 178, call; 178, 179; 178, 180; 179, identifier:len; 180, argument_list; 180, 181; 181, identifier:value; 182, integer:1; 183, comparison_operator:is; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:value; 186, integer:1; 187, None; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:cell_obj; 193, string:"f"; 194, subscript; 194, 195; 194, 196; 195, identifier:value; 196, integer:1; 197, if_statement; 197, 198; 197, 204; 198, comparison_operator:==; 198, 199; 198, 203; 199, call; 199, 200; 199, 201; 200, identifier:len; 201, argument_list; 201, 202; 202, identifier:value; 203, integer:3; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 210; 207, subscript; 207, 208; 207, 209; 208, identifier:cell_obj; 209, string:"p"; 210, subscript; 210, 211; 210, 212; 211, identifier:value; 212, integer:2; 213, else_clause; 213, 214; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:cell_obj; 218, dictionary; 218, 219; 219, pair; 219, 220; 219, 221; 220, string:"v"; 221, identifier:value; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:cell_objs; 226, identifier:append; 227, argument_list; 227, 228; 228, identifier:cell_obj; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:row_obj; 232, dictionary; 232, 233; 233, pair; 233, 234; 233, 235; 234, string:"c"; 235, identifier:cell_objs; 236, if_statement; 236, 237; 236, 238; 237, identifier:cp; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:row_obj; 243, string:"p"; 244, identifier:cp; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:row_objs; 249, identifier:append; 250, argument_list; 250, 251; 251, identifier:row_obj; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:json_obj; 255, dictionary; 255, 256; 255, 259; 256, pair; 256, 257; 256, 258; 257, string:"cols"; 258, identifier:col_objs; 259, pair; 259, 260; 259, 261; 260, string:"rows"; 261, identifier:row_objs; 262, if_statement; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:self; 265, identifier:custom_properties; 266, block; 266, 267; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 272; 269, subscript; 269, 270; 269, 271; 270, identifier:json_obj; 271, string:"p"; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:custom_properties; 275, return_statement; 275, 276; 276, identifier:json_obj
def _ToJSonObj(self, columns_order=None, order_by=()): """Returns an object suitable to be converted to JSON. Args: columns_order: Optional. A list of all column IDs in the order in which you want them created in the output table. If specified, all column IDs must be present. order_by: Optional. Specifies the name of the column(s) to sort by. Passed as is to _PreparedData(). Returns: A dictionary object for use by ToJSon or ToJSonResponse. """ if columns_order is None: columns_order = [col["id"] for col in self.__columns] col_dict = dict([(col["id"], col) for col in self.__columns]) # Creating the column JSON objects col_objs = [] for col_id in columns_order: col_obj = {"id": col_dict[col_id]["id"], "label": col_dict[col_id]["label"], "type": col_dict[col_id]["type"]} if col_dict[col_id]["custom_properties"]: col_obj["p"] = col_dict[col_id]["custom_properties"] col_objs.append(col_obj) # Creating the rows jsons row_objs = [] for row, cp in self._PreparedData(order_by): cell_objs = [] for col in columns_order: value = self.CoerceValue(row.get(col, None), col_dict[col]["type"]) if value is None: cell_obj = None elif isinstance(value, tuple): cell_obj = {"v": value[0]} if len(value) > 1 and value[1] is not None: cell_obj["f"] = value[1] if len(value) == 3: cell_obj["p"] = value[2] else: cell_obj = {"v": value} cell_objs.append(cell_obj) row_obj = {"c": cell_objs} if cp: row_obj["p"] = cp row_objs.append(row_obj) json_obj = {"cols": col_objs, "rows": row_objs} if self.custom_properties: json_obj["p"] = self.custom_properties return json_obj
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:ToJSon; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:columns_order; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:order_by; 10, tuple; 11, block; 11, 12; 11, 14; 11, 31; 11, 46; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:encoded_response_str; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, call; 19, 20; 19, 21; 20, identifier:DataTableJSONEncoder; 21, argument_list; 22, identifier:encode; 23, argument_list; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:_ToJSonObj; 28, argument_list; 28, 29; 28, 30; 29, identifier:columns_order; 30, identifier:order_by; 31, if_statement; 31, 32; 31, 38; 32, not_operator; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:isinstance; 35, argument_list; 35, 36; 35, 37; 36, identifier:encoded_response_str; 37, identifier:str; 38, block; 38, 39; 39, return_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:encoded_response_str; 43, identifier:encode; 44, argument_list; 44, 45; 45, string:"utf-8"; 46, return_statement; 46, 47; 47, identifier:encoded_response_str
def ToJSon(self, columns_order=None, order_by=()): """Returns a string that can be used in a JS DataTable constructor. This method writes a JSON string that can be passed directly into a Google Visualization API DataTable constructor. Use this output if you are hosting the visualization HTML on your site, and want to code the data table in Python. Pass this string into the google.visualization.DataTable constructor, e.g,: ... on my page that hosts my visualization ... google.setOnLoadCallback(drawTable); function drawTable() { var data = new google.visualization.DataTable(_my_JSon_string, 0.6); myTable.draw(data); } Args: columns_order: Optional. Specifies the order of columns in the output table. Specify a list of all column IDs in the order in which you want the table created. Note that you must list all column IDs in this parameter, if you use it. order_by: Optional. Specifies the name of the column(s) to sort by. Passed as is to _PreparedData(). Returns: A JSon constructor string to generate a JS DataTable with the data stored in the DataTable object. Example result (the result is without the newlines): {cols: [{id:"a",label:"a",type:"number"}, {id:"b",label:"b",type:"string"}, {id:"c",label:"c",type:"number"}], rows: [{c:[{v:1},{v:"z"},{v:2}]}, c:{[{v:3,f:"3$"},{v:"w"},null]}], p: {'foo': 'bar'}} Raises: DataTableException: The data does not match the type. """ encoded_response_str = DataTableJSONEncoder().encode(self._ToJSonObj(columns_order, order_by)) if not isinstance(encoded_response_str, str): return encoded_response_str.encode("utf-8") return encoded_response_str
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_radix_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:L; 5, default_parameter; 5, 6; 5, 7; 6, identifier:i; 7, integer:0; 8, block; 8, 9; 8, 11; 8, 21; 8, 25; 8, 36; 8, 71; 8, 85; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 18; 12, comparison_operator:<=; 12, 13; 12, 17; 13, call; 13, 14; 13, 15; 14, identifier:len; 15, argument_list; 15, 16; 16, identifier:L; 17, integer:1; 18, block; 18, 19; 19, return_statement; 19, 20; 20, identifier:L; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:done_bucket; 24, list:[]; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:buckets; 28, list_comprehension; 28, 29; 28, 30; 29, list:[]; 30, for_in_clause; 30, 31; 30, 32; 31, identifier:x; 32, call; 32, 33; 32, 34; 33, identifier:range; 34, argument_list; 34, 35; 35, integer:255; 36, for_statement; 36, 37; 36, 38; 36, 39; 37, identifier:s; 38, identifier:L; 39, block; 39, 40; 40, if_statement; 40, 41; 40, 47; 40, 55; 41, comparison_operator:>=; 41, 42; 41, 43; 42, identifier:i; 43, call; 43, 44; 43, 45; 44, identifier:len; 45, argument_list; 45, 46; 46, identifier:s; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:done_bucket; 52, identifier:append; 53, argument_list; 53, 54; 54, identifier:s; 55, else_clause; 55, 56; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 69; 59, attribute; 59, 60; 59, 68; 60, subscript; 60, 61; 60, 62; 61, identifier:buckets; 62, call; 62, 63; 62, 64; 63, identifier:ord; 64, argument_list; 64, 65; 65, subscript; 65, 66; 65, 67; 66, identifier:s; 67, identifier:i; 68, identifier:append; 69, argument_list; 69, 70; 70, identifier:s; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:buckets; 74, list_comprehension; 74, 75; 74, 82; 75, call; 75, 76; 75, 77; 76, identifier:_radix_sort; 77, argument_list; 77, 78; 77, 79; 78, identifier:b; 79, binary_operator:+; 79, 80; 79, 81; 80, identifier:i; 81, integer:1; 82, for_in_clause; 82, 83; 82, 84; 83, identifier:b; 84, identifier:buckets; 85, return_statement; 85, 86; 86, binary_operator:+; 86, 87; 86, 88; 87, identifier:done_bucket; 88, list_comprehension; 88, 89; 88, 90; 88, 93; 89, identifier:b; 90, for_in_clause; 90, 91; 90, 92; 91, identifier:blist; 92, identifier:buckets; 93, for_in_clause; 93, 94; 93, 95; 94, identifier:b; 95, identifier:blist
def _radix_sort(L, i=0): """ Most significant char radix sort """ if len(L) <= 1: return L done_bucket = [] buckets = [ [] for x in range(255) ] for s in L: if i >= len(s): done_bucket.append(s) else: buckets[ ord(s[i]) ].append(s) buckets = [ _radix_sort(b, i + 1) for b in buckets ] return done_bucket + [ b for blist in buckets for b in blist ]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:fixed_legend_filter_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:fixed_legend_filter_sort; 6, block; 6, 7; 6, 9; 6, 15; 6, 16; 6, 33; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:allowed_values; 12, list:["TOP", "BOTTOM"]; 12, 13; 12, 14; 13, string:"TOP"; 14, string:"BOTTOM"; 15, comment; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:not; 17, 18; 17, 19; 18, identifier:fixed_legend_filter_sort; 19, identifier:allowed_values; 20, block; 20, 21; 21, raise_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:ValueError; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 30; 26, attribute; 26, 27; 26, 28; 26, 29; 27, string:"Invalid value for `fixed_legend_filter_sort` ({0}), must be one of {1}"; 28, comment; 29, identifier:format; 30, argument_list; 30, 31; 30, 32; 31, identifier:fixed_legend_filter_sort; 32, identifier:allowed_values; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_fixed_legend_filter_sort; 38, identifier:fixed_legend_filter_sort
def fixed_legend_filter_sort(self, fixed_legend_filter_sort): """Sets the fixed_legend_filter_sort of this ChartSettings. Whether to display \"Top\"- or \"Bottom\"-ranked series in the fixed legend # noqa: E501 :param fixed_legend_filter_sort: The fixed_legend_filter_sort of this ChartSettings. # noqa: E501 :type: str """ allowed_values = ["TOP", "BOTTOM"] # noqa: E501 if fixed_legend_filter_sort not in allowed_values: raise ValueError( "Invalid value for `fixed_legend_filter_sort` ({0}), must be one of {1}" # noqa: E501 .format(fixed_legend_filter_sort, allowed_values) ) self._fixed_legend_filter_sort = fixed_legend_filter_sort
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:print_table; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:title; 5, identifier:headers; 6, identifier:rows; 7, default_parameter; 7, 8; 7, 9; 8, identifier:sort_columns; 9, None; 10, block; 10, 11; 10, 13; 10, 66; 10, 83; 10, 97; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:sort_columns; 16, None; 17, block; 17, 18; 18, if_statement; 18, 19; 18, 24; 18, 38; 18, 61; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:sort_columns; 23, identifier:int; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:rows; 28, call; 28, 29; 28, 30; 29, identifier:sorted; 30, argument_list; 30, 31; 30, 32; 31, identifier:rows; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:key; 34, call; 34, 35; 34, 36; 35, identifier:itemgetter; 36, argument_list; 36, 37; 37, identifier:sort_columns; 38, elif_clause; 38, 39; 38, 46; 39, call; 39, 40; 39, 41; 40, identifier:isinstance; 41, argument_list; 41, 42; 41, 43; 42, identifier:sort_columns; 43, tuple; 43, 44; 43, 45; 44, identifier:list; 45, identifier:tuple; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:rows; 50, call; 50, 51; 50, 52; 51, identifier:sorted; 52, argument_list; 52, 53; 52, 54; 53, identifier:rows; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:key; 56, call; 56, 57; 56, 58; 57, identifier:itemgetter; 58, argument_list; 58, 59; 59, list_splat; 59, 60; 60, identifier:sort_columns; 61, else_clause; 61, 62; 62, block; 62, 63; 63, assert_statement; 63, 64; 63, 65; 64, False; 65, string:"Sort_columns must be int or list/tuple of int"; 66, if_statement; 66, 67; 66, 68; 66, 76; 67, identifier:title; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:print; 72, argument_list; 72, 73; 73, binary_operator:%; 73, 74; 73, 75; 74, string:'\n%s:'; 75, identifier:title; 76, else_clause; 76, 77; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:print; 81, argument_list; 81, 82; 82, string:''; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:print; 86, argument_list; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:tabulate; 90, identifier:tabulate; 91, argument_list; 91, 92; 91, 93; 91, 94; 92, identifier:rows; 93, identifier:headers; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:tablefmt; 96, string:'simple'; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:print; 100, argument_list; 100, 101; 101, string:''
def print_table(title, headers, rows, sort_columns=None): """ Print a table of rows with headers using tabulate. Parameters: title (:term: string): String that will be output before the Table. headers (list or tuple): List of strings that defines the header for each row. Each string in this list may be multiline by inserting EOL in the string rows (iterable of iterables) The outer iterable is the rows. The inner iterables are the colums for each row args (int or list of int that defines sort) Defines the cols that will be sorted. If int, it defines the column that will be sorted. If list of int, the sort is in sort order of cols in the list (i.e. minor sorts to the left, major sorts to the right) """ if sort_columns is not None: if isinstance(sort_columns, int): rows = sorted(rows, key=itemgetter(sort_columns)) elif isinstance(sort_columns, (list, tuple)): rows = sorted(rows, key=itemgetter(*sort_columns)) else: assert False, "Sort_columns must be int or list/tuple of int" if title: print('\n%s:' % title) else: print('') print(tabulate.tabulate(rows, headers, tablefmt='simple')) print('')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 6; 2, function_name:formatted; 3, parameters; 3, 4; 4, identifier:self; 5, comment; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 15; 6, 106; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:ret; 14, string:"Statistics (times in seconds, lengths in Bytes):\n"; 15, if_statement; 15, 16; 15, 19; 15, 100; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:enabled; 19, block; 19, 20; 19, 44; 19, 45; 19, 49; 19, 66; 19, 67; 19, 84; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:snapshot; 23, call; 23, 24; 23, 25; 24, identifier:sorted; 25, argument_list; 25, 26; 25, 31; 25, 41; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:snapshot; 30, argument_list; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:key; 33, lambda; 33, 34; 33, 36; 34, lambda_parameters; 34, 35; 35, identifier:item; 36, attribute; 36, 37; 36, 40; 37, subscript; 37, 38; 37, 39; 38, identifier:item; 39, integer:1; 40, identifier:avg_time; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:reverse; 43, True; 44, comment; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:include_svr; 48, False; 49, for_statement; 49, 50; 49, 53; 49, 54; 49, 55; 49, 56; 50, pattern_list; 50, 51; 50, 52; 51, identifier:name; 52, identifier:stats; 53, identifier:snapshot; 54, comment; 55, comment; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:stats; 60, identifier:_server_time_stored; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:include_svr; 65, True; 66, comment; 67, if_statement; 67, 68; 67, 69; 67, 76; 68, identifier:include_svr; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, augmented_assignment:+=; 71, 72; 71, 73; 72, identifier:ret; 73, attribute; 73, 74; 73, 75; 74, identifier:OperationStatistic; 75, identifier:_formatted_header_w_svr; 76, else_clause; 76, 77; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, augmented_assignment:+=; 79, 80; 79, 81; 80, identifier:ret; 81, attribute; 81, 82; 81, 83; 82, identifier:OperationStatistic; 83, identifier:_formatted_header; 84, for_statement; 84, 85; 84, 88; 84, 89; 84, 90; 85, pattern_list; 85, 86; 85, 87; 86, identifier:name; 87, identifier:stats; 88, identifier:snapshot; 89, comment; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, augmented_assignment:+=; 92, 93; 92, 94; 93, identifier:ret; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:stats; 97, identifier:formatted; 98, argument_list; 98, 99; 99, identifier:include_svr; 100, else_clause; 100, 101; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, augmented_assignment:+=; 103, 104; 103, 105; 104, identifier:ret; 105, string:"Disabled"; 106, return_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:ret; 110, identifier:strip; 111, argument_list
def formatted(self): # pylint: disable=line-too-long """ Return a human readable string with the statistics for this container. The operations are sorted by decreasing average time. The three columns for `ServerTime` are included only if the WBEM server has returned WBEM server response times. Example if statistics are enabled:: Statistics (times in seconds, lengths in Bytes): Count Excep ClientTime ServerTime RequestLen ReplyLen Operation Cnt Avg Min Max Avg Min Max Avg Min Max Avg Min Max 3 0 0.234 0.100 0.401 0.204 0.080 0.361 1233 1000 1500 26667 20000 35000 EnumerateInstances 1 0 0.100 0.100 0.100 0.080 0.080 0.080 1200 1200 1200 22000 22000 22000 EnumerateInstanceNames . . . Example if statistics are disabled:: Statistics (times in seconds, lengths in Bytes): Disabled """ # noqa: E501 # pylint: enable=line-too-long ret = "Statistics (times in seconds, lengths in Bytes):\n" if self.enabled: snapshot = sorted(self.snapshot(), key=lambda item: item[1].avg_time, reverse=True) # Test to see if any server time is non-zero include_svr = False for name, stats in snapshot: # pylint: disable=unused-variable # pylint: disable=protected-access if stats._server_time_stored: include_svr = True # pylint: disable=protected-access if include_svr: ret += OperationStatistic._formatted_header_w_svr else: ret += OperationStatistic._formatted_header for name, stats in snapshot: # pylint: disable=unused-variable ret += stats.formatted(include_svr) else: ret += "Disabled" return ret.strip()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_clusters; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:platform; 6, identifier:retry_contexts; 7, identifier:all_clusters; 8, block; 8, 9; 8, 11; 8, 15; 8, 27; 8, 120; 8, 141; 8, 156; 9, expression_statement; 9, 10; 10, string:''' return clusters sorted by load. '''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:possible_cluster_info; 14, dictionary; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:candidates; 18, call; 18, 19; 18, 20; 19, identifier:set; 20, argument_list; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:copy; 24, identifier:copy; 25, argument_list; 25, 26; 26, identifier:all_clusters; 27, while_statement; 27, 28; 27, 32; 28, boolean_operator:and; 28, 29; 28, 30; 29, identifier:candidates; 30, not_operator; 30, 31; 31, identifier:possible_cluster_info; 32, block; 32, 33; 32, 38; 32, 101; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:wait_for_any_cluster; 36, argument_list; 36, 37; 37, identifier:retry_contexts; 38, for_statement; 38, 39; 38, 40; 38, 50; 39, identifier:cluster; 40, call; 40, 41; 40, 42; 41, identifier:sorted; 42, argument_list; 42, 43; 42, 44; 43, identifier:candidates; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:key; 46, call; 46, 47; 46, 48; 47, identifier:attrgetter; 48, argument_list; 48, 49; 49, string:'priority'; 50, block; 50, 51; 50, 59; 50, 65; 50, 71; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:ctx; 54, subscript; 54, 55; 54, 56; 55, identifier:retry_contexts; 56, attribute; 56, 57; 56, 58; 57, identifier:cluster; 58, identifier:name; 59, if_statement; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:ctx; 62, identifier:in_retry_wait; 63, block; 63, 64; 64, continue_statement; 65, if_statement; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:ctx; 68, identifier:failed; 69, block; 69, 70; 70, continue_statement; 71, try_statement; 71, 72; 71, 89; 72, block; 72, 73; 72, 83; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:cluster_info; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:get_cluster_info; 80, argument_list; 80, 81; 80, 82; 81, identifier:cluster; 82, identifier:platform; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:possible_cluster_info; 87, identifier:cluster; 88, identifier:cluster_info; 89, except_clause; 89, 90; 89, 91; 90, identifier:OsbsException; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:ctx; 96, identifier:try_again_later; 97, argument_list; 97, 98; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:find_cluster_retry_delay; 101, expression_statement; 101, 102; 102, augmented_assignment:-=; 102, 103; 102, 104; 103, identifier:candidates; 104, call; 104, 105; 104, 106; 105, identifier:set; 106, argument_list; 106, 107; 107, list_comprehension; 107, 108; 107, 109; 107, 112; 108, identifier:c; 109, for_in_clause; 109, 110; 109, 111; 110, identifier:c; 111, identifier:candidates; 112, if_clause; 112, 113; 113, attribute; 113, 114; 113, 119; 114, subscript; 114, 115; 114, 116; 115, identifier:retry_contexts; 116, attribute; 116, 117; 116, 118; 117, identifier:c; 118, identifier:name; 119, identifier:failed; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:ret; 123, call; 123, 124; 123, 125; 124, identifier:sorted; 125, argument_list; 125, 126; 125, 131; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:possible_cluster_info; 129, identifier:values; 130, argument_list; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:key; 133, lambda; 133, 134; 133, 136; 134, lambda_parameters; 134, 135; 135, identifier:c; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:c; 139, identifier:cluster; 140, identifier:priority; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:ret; 144, call; 144, 145; 144, 146; 145, identifier:sorted; 146, argument_list; 146, 147; 146, 148; 147, identifier:ret; 148, keyword_argument; 148, 149; 148, 150; 149, identifier:key; 150, lambda; 150, 151; 150, 153; 151, lambda_parameters; 151, 152; 152, identifier:c; 153, attribute; 153, 154; 153, 155; 154, identifier:c; 155, identifier:load; 156, return_statement; 156, 157; 157, identifier:ret
def get_clusters(self, platform, retry_contexts, all_clusters): ''' return clusters sorted by load. ''' possible_cluster_info = {} candidates = set(copy.copy(all_clusters)) while candidates and not possible_cluster_info: wait_for_any_cluster(retry_contexts) for cluster in sorted(candidates, key=attrgetter('priority')): ctx = retry_contexts[cluster.name] if ctx.in_retry_wait: continue if ctx.failed: continue try: cluster_info = self.get_cluster_info(cluster, platform) possible_cluster_info[cluster] = cluster_info except OsbsException: ctx.try_again_later(self.find_cluster_retry_delay) candidates -= set([c for c in candidates if retry_contexts[c.name].failed]) ret = sorted(possible_cluster_info.values(), key=lambda c: c.cluster.priority) ret = sorted(ret, key=lambda c: c.load) return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 1, 20; 2, function_name:get_profile_names_and_default; 3, parameters; 4, type; 4, 5; 5, parenthesized_expression; 5, 6; 6, subscript; 6, 7; 6, 10; 6, 15; 7, attribute; 7, 8; 7, 9; 8, identifier:typing; 9, identifier:Tuple; 10, subscript; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:typing; 13, identifier:Sequence; 14, identifier:str; 15, subscript; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:typing; 18, identifier:Optional; 19, identifier:Profile; 20, block; 20, 21; 20, 23; 21, expression_statement; 21, 22; 22, comment; 23, with_statement; 23, 24; 23, 34; 24, with_clause; 24, 25; 25, with_item; 25, 26; 26, as_pattern; 26, 27; 26, 32; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:ProfileStore; 30, identifier:open; 31, argument_list; 32, as_pattern_target; 32, 33; 33, identifier:config; 34, block; 34, 35; 35, return_statement; 35, 36; 36, expression_list; 36, 37; 36, 41; 37, call; 37, 38; 37, 39; 38, identifier:sorted; 39, argument_list; 39, 40; 40, identifier:config; 41, attribute; 41, 42; 41, 43; 42, identifier:config; 43, identifier:default
def get_profile_names_and_default() -> ( typing.Tuple[typing.Sequence[str], typing.Optional[Profile]]): """Return the list of profile names and the default profile object. The list of names is sorted. """ with ProfileStore.open() as config: return sorted(config), config.default
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:print_all_commands; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, keyword_separator; 6, default_parameter; 6, 7; 6, 8; 7, identifier:no_pager; 8, False; 9, block; 9, 10; 9, 12; 9, 22; 9, 39; 9, 55; 9, 59; 9, 176; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:formatter; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:parent_parser; 20, identifier:_get_formatter; 21, argument_list; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:command_names; 25, call; 25, 26; 25, 27; 26, identifier:sorted; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 38; 29, attribute; 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:parent_parser; 35, identifier:subparsers; 36, identifier:choices; 37, identifier:keys; 38, argument_list; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:max_name_len; 42, binary_operator:+; 42, 43; 42, 54; 43, call; 43, 44; 43, 45; 44, identifier:max; 45, argument_list; 45, 46; 46, list_comprehension; 46, 47; 46, 51; 47, call; 47, 48; 47, 49; 48, identifier:len; 49, argument_list; 49, 50; 50, identifier:name; 51, for_in_clause; 51, 52; 51, 53; 52, identifier:name; 53, identifier:command_names; 54, integer:1; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:commands; 58, string:""; 59, for_statement; 59, 60; 59, 61; 59, 62; 60, identifier:name; 61, identifier:command_names; 62, block; 62, 63; 62, 75; 62, 84; 62, 97; 62, 167; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:command; 66, subscript; 66, 67; 66, 74; 67, attribute; 67, 68; 67, 73; 68, attribute; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:parent_parser; 72, identifier:subparsers; 73, identifier:choices; 74, identifier:name; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:extra_padding; 78, binary_operator:-; 78, 79; 78, 80; 79, identifier:max_name_len; 80, call; 80, 81; 80, 82; 81, identifier:len; 82, argument_list; 82, 83; 83, identifier:name; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:command_line; 87, binary_operator:%; 87, 88; 87, 89; 88, string:'%s%s%s'; 89, tuple; 89, 90; 89, 91; 89, 94; 90, identifier:name; 91, binary_operator:*; 91, 92; 91, 93; 92, string:' '; 93, identifier:extra_padding; 94, attribute; 94, 95; 94, 96; 95, identifier:command; 96, identifier:description; 97, while_statement; 97, 98; 97, 106; 98, comparison_operator:>; 98, 99; 98, 103; 99, call; 99, 100; 99, 101; 100, identifier:len; 101, argument_list; 101, 102; 102, identifier:command_line; 103, attribute; 103, 104; 103, 105; 104, identifier:formatter; 105, identifier:_width; 106, block; 106, 107; 106, 119; 106, 127; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:lines; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:textwrap; 113, identifier:wrap; 114, argument_list; 114, 115; 114, 116; 115, identifier:command_line; 116, attribute; 116, 117; 116, 118; 117, identifier:formatter; 118, identifier:_width; 119, expression_statement; 119, 120; 120, augmented_assignment:+=; 120, 121; 120, 122; 121, identifier:commands; 122, binary_operator:%; 122, 123; 122, 124; 123, string:"%s\n"; 124, subscript; 124, 125; 124, 126; 125, identifier:lines; 126, integer:0; 127, if_statement; 127, 128; 127, 134; 127, 161; 128, comparison_operator:>; 128, 129; 128, 133; 129, call; 129, 130; 129, 131; 130, identifier:len; 131, argument_list; 131, 132; 132, identifier:lines; 133, integer:1; 134, block; 134, 135; 134, 148; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:lines; 139, integer:1; 140, binary_operator:+; 140, 141; 140, 145; 141, parenthesized_expression; 141, 142; 142, binary_operator:*; 142, 143; 142, 144; 143, string:' '; 144, identifier:max_name_len; 145, subscript; 145, 146; 145, 147; 146, identifier:lines; 147, integer:1; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:command_line; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, string:' '; 154, identifier:join; 155, argument_list; 155, 156; 156, subscript; 156, 157; 156, 158; 157, identifier:lines; 158, slice; 158, 159; 158, 160; 159, integer:1; 160, colon; 161, else_clause; 161, 162; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:command_line; 166, None; 167, if_statement; 167, 168; 167, 169; 168, identifier:command_line; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, augmented_assignment:+=; 171, 172; 171, 173; 172, identifier:commands; 173, binary_operator:%; 173, 174; 173, 175; 174, string:"%s\n"; 175, identifier:command_line; 176, if_statement; 176, 177; 176, 178; 176, 189; 177, identifier:no_pager; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:print; 182, argument_list; 182, 183; 183, subscript; 183, 184; 183, 185; 184, identifier:commands; 185, slice; 185, 186; 185, 187; 186, colon; 187, unary_operator:-; 187, 188; 188, integer:1; 189, else_clause; 189, 190; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:print_with_pager; 194, argument_list; 194, 195; 195, subscript; 195, 196; 195, 197; 196, identifier:commands; 197, slice; 197, 198; 197, 199; 198, colon; 199, unary_operator:-; 199, 200; 200, integer:1
def print_all_commands(self, *, no_pager=False): """Print help for all commands. Commands are sorted in alphabetical order and wrapping is done based on the width of the terminal. """ formatter = self.parent_parser._get_formatter() command_names = sorted(self.parent_parser.subparsers.choices.keys()) max_name_len = max([len(name) for name in command_names]) + 1 commands = "" for name in command_names: command = self.parent_parser.subparsers.choices[name] extra_padding = max_name_len - len(name) command_line = '%s%s%s' % ( name, ' ' * extra_padding, command.description) while len(command_line) > formatter._width: lines = textwrap.wrap(command_line, formatter._width) commands += "%s\n" % lines[0] if len(lines) > 1: lines[1] = (' ' * max_name_len) + lines[1] command_line = ' '.join(lines[1:]) else: command_line = None if command_line: commands += "%s\n" % command_line if no_pager: print(commands[:-1]) else: print_with_pager(commands[:-1])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:rank_dated_files; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:pattern; 5, identifier:dir; 6, default_parameter; 6, 7; 6, 8; 7, identifier:descending; 8, True; 9, block; 9, 10; 9, 12; 9, 27; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:files; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:glob; 18, identifier:glob; 19, argument_list; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:op; 23, identifier:join; 24, argument_list; 24, 25; 24, 26; 25, identifier:dir; 26, identifier:pattern; 27, return_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:sorted; 30, argument_list; 30, 31; 30, 32; 31, identifier:files; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:reverse; 34, identifier:descending
def rank_dated_files(pattern, dir, descending=True): """Search a directory for files that match a pattern. Return an ordered list of these files by filename. Args: pattern: The glob pattern to search for. dir: Path to directory where the files will be searched for. descending: Default True, will sort alphabetically by descending order. Returns: list: Rank-ordered list by filename. """ files = glob.glob(op.join(dir, pattern)) return sorted(files, reverse=descending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:best_structures; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:uniprot_id; 5, default_parameter; 5, 6; 5, 7; 6, identifier:outname; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:outdir; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:seq_ident_cutoff; 13, float:0.0; 14, default_parameter; 14, 15; 14, 16; 15, identifier:force_rerun; 16, False; 17, block; 17, 18; 17, 20; 17, 24; 17, 32; 17, 33; 17, 43; 17, 65; 17, 66; 17, 223; 17, 232; 17, 233; 17, 256; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:outfile; 23, string:''; 24, if_statement; 24, 25; 24, 27; 25, not_operator; 25, 26; 26, identifier:outdir; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:outdir; 31, string:''; 32, comment; 33, if_statement; 33, 34; 33, 38; 34, boolean_operator:and; 34, 35; 34, 37; 35, not_operator; 35, 36; 36, identifier:outname; 37, identifier:outdir; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:outname; 42, identifier:uniprot_id; 43, if_statement; 43, 44; 43, 45; 44, identifier:outname; 45, block; 45, 46; 45, 56; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:outname; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:op; 52, identifier:join; 53, argument_list; 53, 54; 53, 55; 54, identifier:outdir; 55, identifier:outname; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:outfile; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, string:'{}.json'; 62, identifier:format; 63, argument_list; 63, 64; 64, identifier:outname; 65, comment; 66, if_statement; 66, 67; 66, 81; 66, 115; 66, 116; 67, not_operator; 67, 68; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:ssbio; 72, identifier:utils; 73, identifier:force_rerun; 74, argument_list; 74, 75; 74, 78; 75, keyword_argument; 75, 76; 75, 77; 76, identifier:flag; 77, identifier:force_rerun; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:outfile; 80, identifier:outfile; 81, block; 81, 82; 81, 103; 82, with_statement; 82, 83; 82, 93; 83, with_clause; 83, 84; 84, with_item; 84, 85; 85, as_pattern; 85, 86; 85, 91; 86, call; 86, 87; 86, 88; 87, identifier:open; 88, argument_list; 88, 89; 88, 90; 89, identifier:outfile; 90, string:'r'; 91, as_pattern_target; 91, 92; 92, identifier:f; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:raw_data; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:json; 100, identifier:load; 101, argument_list; 101, 102; 102, identifier:f; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:log; 107, identifier:debug; 108, argument_list; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, string:'{}: loaded existing json file'; 112, identifier:format; 113, argument_list; 113, 114; 114, identifier:uniprot_id; 115, comment; 116, else_clause; 116, 117; 116, 118; 117, comment; 118, block; 118, 119; 118, 139; 118, 187; 118, 188; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:response; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:requests; 125, identifier:get; 126, argument_list; 126, 127; 126, 133; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, string:'https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/{}'; 130, identifier:format; 131, argument_list; 131, 132; 132, identifier:uniprot_id; 133, keyword_argument; 133, 134; 133, 135; 134, identifier:data; 135, dictionary; 135, 136; 136, pair; 136, 137; 136, 138; 137, string:'key'; 138, string:'value'; 139, if_statement; 139, 140; 139, 145; 139, 165; 140, comparison_operator:==; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:response; 143, identifier:status_code; 144, integer:404; 145, block; 145, 146; 145, 158; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:log; 150, identifier:debug; 151, argument_list; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, string:'{}: 404 returned, probably no structures available.'; 155, identifier:format; 156, argument_list; 156, 157; 157, identifier:uniprot_id; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:raw_data; 161, dictionary; 161, 162; 162, pair; 162, 163; 162, 164; 163, identifier:uniprot_id; 164, dictionary; 165, else_clause; 165, 166; 166, block; 166, 167; 166, 179; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:log; 171, identifier:debug; 172, argument_list; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, string:'{}: Obtained best structures'; 176, identifier:format; 177, argument_list; 177, 178; 178, identifier:uniprot_id; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:raw_data; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:response; 185, identifier:json; 186, argument_list; 187, comment; 188, if_statement; 188, 189; 188, 190; 189, identifier:outfile; 190, block; 190, 191; 190, 211; 191, with_statement; 191, 192; 191, 202; 192, with_clause; 192, 193; 193, with_item; 193, 194; 194, as_pattern; 194, 195; 194, 200; 195, call; 195, 196; 195, 197; 196, identifier:open; 197, argument_list; 197, 198; 197, 199; 198, identifier:outfile; 199, string:'w'; 200, as_pattern_target; 200, 201; 201, identifier:f; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:json; 207, identifier:dump; 208, argument_list; 208, 209; 208, 210; 209, identifier:raw_data; 210, identifier:f; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:log; 215, identifier:debug; 216, argument_list; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, string:'{}: Saved json file of best structures'; 220, identifier:format; 221, argument_list; 221, 222; 222, identifier:uniprot_id; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:data; 226, subscript; 226, 227; 226, 231; 227, call; 227, 228; 227, 229; 228, identifier:dict; 229, argument_list; 229, 230; 230, identifier:raw_data; 231, identifier:uniprot_id; 232, comment; 233, if_statement; 233, 234; 233, 237; 234, comparison_operator:!=; 234, 235; 234, 236; 235, identifier:seq_ident_cutoff; 236, integer:0; 237, block; 237, 238; 238, for_statement; 238, 239; 238, 240; 238, 241; 239, identifier:result; 240, identifier:data; 241, block; 241, 242; 242, if_statement; 242, 243; 242, 248; 243, comparison_operator:<; 243, 244; 243, 247; 244, subscript; 244, 245; 244, 246; 245, identifier:result; 246, string:'coverage'; 247, identifier:seq_ident_cutoff; 248, block; 248, 249; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:data; 253, identifier:remove; 254, argument_list; 254, 255; 255, identifier:result; 256, return_statement; 256, 257; 257, identifier:data
def best_structures(uniprot_id, outname=None, outdir=None, seq_ident_cutoff=0.0, force_rerun=False): """Use the PDBe REST service to query for the best PDB structures for a UniProt ID. More information found here: https://www.ebi.ac.uk/pdbe/api/doc/sifts.html Link used to retrieve results: https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/:accession The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and, if the same, resolution. Here is the ranking algorithm described by the PDB paper: https://nar.oxfordjournals.org/content/44/D1/D385.full "Finally, a single quality indicator is also calculated for each entry by taking the harmonic average of all the percentile scores representing model and model-data-fit quality measures and then subtracting 10 times the numerical value of the resolution (in Angstrom) of the entry to ensure that resolution plays a role in characterising the quality of a structure. This single empirical 'quality measure' value is used by the PDBe query system to sort results and identify the 'best' structure in a given context. At present, entries determined by methods other than X-ray crystallography do not have similar data quality information available and are not considered as 'best structures'." Args: uniprot_id (str): UniProt Accession ID outname (str): Basename of the output file of JSON results outdir (str): Path to output directory of JSON results seq_ident_cutoff (float): Cutoff results based on percent coverage (in decimal form) force_rerun (bool): Obtain best structures mapping ignoring previously downloaded results Returns: list: Rank-ordered list of dictionaries representing chain-specific PDB entries. Keys are: * pdb_id: the PDB ID which maps to the UniProt ID * chain_id: the specific chain of the PDB which maps to the UniProt ID * coverage: the percent coverage of the entire UniProt sequence * resolution: the resolution of the structure * start: the structure residue number which maps to the start of the mapped sequence * end: the structure residue number which maps to the end of the mapped sequence * unp_start: the sequence residue number which maps to the structure start * unp_end: the sequence residue number which maps to the structure end * experimental_method: type of experiment used to determine structure * tax_id: taxonomic ID of the protein's original organism """ outfile = '' if not outdir: outdir = '' # if output dir is specified but not outname, use the uniprot if not outname and outdir: outname = uniprot_id if outname: outname = op.join(outdir, outname) outfile = '{}.json'.format(outname) # Load a possibly existing json file if not ssbio.utils.force_rerun(flag=force_rerun, outfile=outfile): with open(outfile, 'r') as f: raw_data = json.load(f) log.debug('{}: loaded existing json file'.format(uniprot_id)) # Otherwise run the web request else: # TODO: add a checker for a cached file of uniprot -> PDBs - can be generated within gempro pipeline and stored response = requests.get('https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/{}'.format(uniprot_id), data={'key': 'value'}) if response.status_code == 404: log.debug('{}: 404 returned, probably no structures available.'.format(uniprot_id)) raw_data = {uniprot_id: {}} else: log.debug('{}: Obtained best structures'.format(uniprot_id)) raw_data = response.json() # Write the json file if specified if outfile: with open(outfile, 'w') as f: json.dump(raw_data, f) log.debug('{}: Saved json file of best structures'.format(uniprot_id)) data = dict(raw_data)[uniprot_id] # Filter for sequence identity percentage if seq_ident_cutoff != 0: for result in data: if result['coverage'] < seq_ident_cutoff: data.remove(result) return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:map_uniprot_to_pdb; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:seq_ident_cutoff; 7, float:0.0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:outdir; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:force_rerun; 13, False; 14, block; 14, 15; 14, 17; 14, 39; 14, 40; 14, 48; 14, 68; 14, 98; 14, 117; 14, 150; 14, 154; 14, 314; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 22; 18, not_operator; 18, 19; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:representative_sequence; 22, block; 22, 23; 22, 37; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:log; 27, identifier:error; 28, argument_list; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, string:'{}: no representative sequence set, cannot use best structures API'; 32, identifier:format; 33, argument_list; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:id; 37, return_statement; 37, 38; 38, None; 39, comment; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:uniprot_id; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:representative_sequence; 47, identifier:uniprot; 48, if_statement; 48, 49; 48, 51; 49, not_operator; 49, 50; 50, identifier:uniprot_id; 51, block; 51, 52; 51, 66; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:log; 56, identifier:error; 57, argument_list; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, string:'{}: no representative UniProt ID set, cannot use best structures API'; 61, identifier:format; 62, argument_list; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:id; 66, return_statement; 66, 67; 67, None; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:in; 69, 70; 69, 71; 70, string:'-'; 71, identifier:uniprot_id; 72, block; 72, 73; 72, 87; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:log; 77, identifier:debug; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, string:'{}: "-" detected in UniProt ID, isoform specific sequences are ignored with best structures API'; 82, identifier:format; 83, argument_list; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:id; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:uniprot_id; 90, subscript; 90, 91; 90, 97; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:uniprot_id; 94, identifier:split; 95, argument_list; 95, 96; 96, string:'-'; 97, integer:0; 98, if_statement; 98, 99; 98, 101; 99, not_operator; 99, 100; 100, identifier:outdir; 101, block; 101, 102; 101, 108; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:outdir; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:sequence_dir; 108, if_statement; 108, 109; 108, 111; 109, not_operator; 109, 110; 110, identifier:outdir; 111, block; 111, 112; 112, raise_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:ValueError; 115, argument_list; 115, 116; 116, string:'Output directory must be specified'; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:best_structures; 120, call; 120, 121; 120, 128; 121, attribute; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:ssbio; 125, identifier:databases; 126, identifier:pdb; 127, identifier:best_structures; 128, argument_list; 128, 129; 128, 130; 128, 141; 128, 144; 128, 147; 129, identifier:uniprot_id; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:outname; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, string:'{}_best_structures'; 135, identifier:format; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:custom_slugify; 139, argument_list; 139, 140; 140, identifier:uniprot_id; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:outdir; 143, identifier:outdir; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:seq_ident_cutoff; 146, identifier:seq_ident_cutoff; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:force_rerun; 149, identifier:force_rerun; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:new_pdbs; 153, list:[]; 154, if_statement; 154, 155; 154, 156; 154, 297; 155, identifier:best_structures; 156, block; 156, 157; 156, 161; 156, 278; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:rank; 160, integer:1; 161, for_statement; 161, 162; 161, 163; 161, 164; 162, identifier:best_structure; 163, identifier:best_structures; 164, block; 164, 165; 164, 178; 164, 185; 164, 194; 164, 195; 164, 209; 164, 210; 164, 211; 164, 218; 164, 224; 164, 233; 164, 243; 164, 254; 164, 264; 164, 274; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:currpdb; 168, call; 168, 169; 168, 170; 169, identifier:str; 170, argument_list; 170, 171; 171, call; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:best_structure; 175, string:'pdb_id'; 176, identifier:lower; 177, argument_list; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:new_pdbs; 182, identifier:append; 183, argument_list; 183, 184; 184, identifier:currpdb; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:currchain; 188, call; 188, 189; 188, 190; 189, identifier:str; 190, argument_list; 190, 191; 191, subscript; 191, 192; 191, 193; 192, identifier:best_structure; 193, string:'chain_id'; 194, comment; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:new_pdb; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:load_pdb; 202, argument_list; 202, 203; 202, 206; 203, keyword_argument; 203, 204; 203, 205; 204, identifier:pdb_id; 205, identifier:currpdb; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:mapped_chains; 208, identifier:currchain; 209, comment; 210, comment; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:new_pdb; 215, identifier:add_chain_ids; 216, argument_list; 216, 217; 217, identifier:currchain; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:pdb_specific_keys; 221, list:['experimental_method', 'resolution']; 221, 222; 221, 223; 222, string:'experimental_method'; 223, string:'resolution'; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:chain_specific_keys; 227, list:['coverage', 'start', 'end', 'unp_start', 'unp_end']; 227, 228; 227, 229; 227, 230; 227, 231; 227, 232; 228, string:'coverage'; 229, string:'start'; 230, string:'end'; 231, string:'unp_start'; 232, string:'unp_end'; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:new_pdb; 237, identifier:update; 238, argument_list; 238, 239; 238, 240; 239, identifier:best_structure; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:only_keys; 242, identifier:pdb_specific_keys; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:new_chain; 246, call; 246, 247; 246, 252; 247, attribute; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:new_pdb; 250, identifier:chains; 251, identifier:get_by_id; 252, argument_list; 252, 253; 253, identifier:currchain; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:new_chain; 258, identifier:update; 259, argument_list; 259, 260; 259, 261; 260, identifier:best_structure; 261, keyword_argument; 261, 262; 261, 263; 262, identifier:only_keys; 263, identifier:chain_specific_keys; 264, expression_statement; 264, 265; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:new_chain; 268, identifier:update; 269, argument_list; 269, 270; 270, dictionary; 270, 271; 271, pair; 271, 272; 271, 273; 272, string:'rank'; 273, identifier:rank; 274, expression_statement; 274, 275; 275, augmented_assignment:+=; 275, 276; 275, 277; 276, identifier:rank; 277, integer:1; 278, expression_statement; 278, 279; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:log; 282, identifier:debug; 283, argument_list; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, string:'{}, {}: {} PDB/chain pairs mapped'; 287, identifier:format; 288, argument_list; 288, 289; 288, 292; 288, 293; 289, attribute; 289, 290; 289, 291; 290, identifier:self; 291, identifier:id; 292, identifier:uniprot_id; 293, call; 293, 294; 293, 295; 294, identifier:len; 295, argument_list; 295, 296; 296, identifier:best_structures; 297, else_clause; 297, 298; 298, block; 298, 299; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:log; 303, identifier:debug; 304, argument_list; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, string:'{}, {}: no PDB/chain pairs mapped'; 308, identifier:format; 309, argument_list; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:self; 312, identifier:id; 313, identifier:uniprot_id; 314, return_statement; 314, 315; 315, identifier:new_pdbs
def map_uniprot_to_pdb(self, seq_ident_cutoff=0.0, outdir=None, force_rerun=False): """Map the representative sequence's UniProt ID to PDB IDs using the PDBe "Best Structures" API. Will save a JSON file of the results to the protein sequences folder. The "Best structures" API is available at https://www.ebi.ac.uk/pdbe/api/doc/sifts.html The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and, if the same, resolution. Args: seq_ident_cutoff (float): Sequence identity cutoff in decimal form outdir (str): Output directory to cache JSON results of search force_rerun (bool): Force re-downloading of JSON results if they already exist Returns: list: A rank-ordered list of PDBProp objects that map to the UniProt ID """ if not self.representative_sequence: log.error('{}: no representative sequence set, cannot use best structures API'.format(self.id)) return None # Check if a UniProt ID is attached to the representative sequence uniprot_id = self.representative_sequence.uniprot if not uniprot_id: log.error('{}: no representative UniProt ID set, cannot use best structures API'.format(self.id)) return None if '-' in uniprot_id: log.debug('{}: "-" detected in UniProt ID, isoform specific sequences are ignored with best structures API'.format(self.id)) uniprot_id = uniprot_id.split('-')[0] if not outdir: outdir = self.sequence_dir if not outdir: raise ValueError('Output directory must be specified') best_structures = ssbio.databases.pdb.best_structures(uniprot_id, outname='{}_best_structures'.format(custom_slugify(uniprot_id)), outdir=outdir, seq_ident_cutoff=seq_ident_cutoff, force_rerun=force_rerun) new_pdbs = [] if best_structures: rank = 1 for best_structure in best_structures: currpdb = str(best_structure['pdb_id'].lower()) new_pdbs.append(currpdb) currchain = str(best_structure['chain_id']) # load_pdb will append this protein to the list new_pdb = self.load_pdb(pdb_id=currpdb, mapped_chains=currchain) # Also add this chain to the chains attribute so we can save the # info we get from best_structures new_pdb.add_chain_ids(currchain) pdb_specific_keys = ['experimental_method', 'resolution'] chain_specific_keys = ['coverage', 'start', 'end', 'unp_start', 'unp_end'] new_pdb.update(best_structure, only_keys=pdb_specific_keys) new_chain = new_pdb.chains.get_by_id(currchain) new_chain.update(best_structure, only_keys=chain_specific_keys) new_chain.update({'rank': rank}) rank += 1 log.debug('{}, {}: {} PDB/chain pairs mapped'.format(self.id, uniprot_id, len(best_structures))) else: log.debug('{}, {}: no PDB/chain pairs mapped'.format(self.id, uniprot_id)) return new_pdbs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:map_uniprot_to_pdb; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:seq_ident_cutoff; 7, float:0.0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:outdir; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:force_rerun; 13, False; 14, block; 14, 15; 14, 17; 14, 18; 14, 22; 14, 65; 14, 72; 14, 89; 14, 93; 14, 94; 14, 198; 14, 221; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:all_representative_uniprots; 21, list:[]; 22, for_statement; 22, 23; 22, 24; 22, 27; 23, identifier:g; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:genes_with_a_representative_sequence; 27, block; 27, 28; 27, 38; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:uniprot_id; 31, attribute; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:g; 35, identifier:protein; 36, identifier:representative_sequence; 37, identifier:uniprot; 38, if_statement; 38, 39; 38, 40; 38, 41; 39, identifier:uniprot_id; 40, comment; 41, block; 41, 42; 41, 58; 42, if_statement; 42, 43; 42, 46; 43, comparison_operator:in; 43, 44; 43, 45; 44, string:'-'; 45, identifier:uniprot_id; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:uniprot_id; 50, subscript; 50, 51; 50, 57; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:uniprot_id; 54, identifier:split; 55, argument_list; 55, 56; 56, string:'-'; 57, integer:0; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:all_representative_uniprots; 62, identifier:append; 63, argument_list; 63, 64; 64, identifier:uniprot_id; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:log; 69, identifier:info; 70, argument_list; 70, 71; 71, string:'Mapping UniProt IDs --> PDB IDs...'; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:uniprots_to_pdbs; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:bs_unip; 78, identifier:mapping; 79, argument_list; 79, 80; 79, 83; 79, 86; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:fr; 82, string:'ACC'; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:to; 85, string:'PDB_ID'; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:query; 88, identifier:all_representative_uniprots; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:counter; 92, integer:0; 93, comment; 94, for_statement; 94, 95; 94, 96; 94, 102; 95, identifier:g; 96, call; 96, 97; 96, 98; 97, identifier:tqdm; 98, argument_list; 98, 99; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:genes_with_a_representative_sequence; 102, block; 102, 103; 102, 113; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:uniprot_id; 106, attribute; 106, 107; 106, 112; 107, attribute; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:g; 110, identifier:protein; 111, identifier:representative_sequence; 112, identifier:uniprot; 113, if_statement; 113, 114; 113, 115; 114, identifier:uniprot_id; 115, block; 115, 116; 115, 132; 116, if_statement; 116, 117; 116, 120; 117, comparison_operator:in; 117, 118; 117, 119; 118, string:'-'; 119, identifier:uniprot_id; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:uniprot_id; 124, subscript; 124, 125; 124, 131; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:uniprot_id; 128, identifier:split; 129, argument_list; 129, 130; 130, string:'-'; 131, integer:0; 132, if_statement; 132, 133; 132, 136; 132, 181; 133, comparison_operator:in; 133, 134; 133, 135; 134, identifier:uniprot_id; 135, identifier:uniprots_to_pdbs; 136, block; 136, 137; 136, 156; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:best_structures; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:g; 144, identifier:protein; 145, identifier:map_uniprot_to_pdb; 146, argument_list; 146, 147; 146, 150; 146, 153; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:seq_ident_cutoff; 149, identifier:seq_ident_cutoff; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:outdir; 152, identifier:outdir; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:force_rerun; 155, identifier:force_rerun; 156, if_statement; 156, 157; 156, 158; 157, identifier:best_structures; 158, block; 158, 159; 158, 163; 159, expression_statement; 159, 160; 160, augmented_assignment:+=; 160, 161; 160, 162; 161, identifier:counter; 162, integer:1; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:log; 167, identifier:debug; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, string:'{}: {} PDBs mapped'; 172, identifier:format; 173, argument_list; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:g; 176, identifier:id; 177, call; 177, 178; 177, 179; 178, identifier:len; 179, argument_list; 179, 180; 180, identifier:best_structures; 181, else_clause; 181, 182; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:log; 187, identifier:debug; 188, argument_list; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, string:'{}, {}: no PDBs available'; 192, identifier:format; 193, argument_list; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:g; 196, identifier:id; 197, identifier:uniprot_id; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:log; 202, identifier:info; 203, argument_list; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, string:'{}/{}: number of genes with at least one experimental structure'; 207, identifier:format; 208, argument_list; 208, 209; 208, 215; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:genes_with_experimental_structures; 215, call; 215, 216; 215, 217; 216, identifier:len; 217, argument_list; 217, 218; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:genes; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:log; 225, identifier:info; 226, argument_list; 226, 227; 227, string:'Completed UniProt --> best PDB mapping. See the "df_pdb_ranking" attribute for a summary dataframe.'
def map_uniprot_to_pdb(self, seq_ident_cutoff=0.0, outdir=None, force_rerun=False): """Map all representative sequences' UniProt ID to PDB IDs using the PDBe "Best Structures" API. Will save a JSON file of the results to each protein's ``sequences`` folder. The "Best structures" API is available at https://www.ebi.ac.uk/pdbe/api/doc/sifts.html The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and, if the same, resolution. Args: seq_ident_cutoff (float): Sequence identity cutoff in decimal form outdir (str): Output directory to cache JSON results of search force_rerun (bool): Force re-downloading of JSON results if they already exist Returns: list: A rank-ordered list of PDBProp objects that map to the UniProt ID """ # First get all UniProt IDs and check if they have PDBs all_representative_uniprots = [] for g in self.genes_with_a_representative_sequence: uniprot_id = g.protein.representative_sequence.uniprot if uniprot_id: # TODO: add warning or something for isoform ids? if '-' in uniprot_id: uniprot_id = uniprot_id.split('-')[0] all_representative_uniprots.append(uniprot_id) log.info('Mapping UniProt IDs --> PDB IDs...') uniprots_to_pdbs = bs_unip.mapping(fr='ACC', to='PDB_ID', query=all_representative_uniprots) counter = 0 # Now run the best_structures API for all genes for g in tqdm(self.genes_with_a_representative_sequence): uniprot_id = g.protein.representative_sequence.uniprot if uniprot_id: if '-' in uniprot_id: uniprot_id = uniprot_id.split('-')[0] if uniprot_id in uniprots_to_pdbs: best_structures = g.protein.map_uniprot_to_pdb(seq_ident_cutoff=seq_ident_cutoff, outdir=outdir, force_rerun=force_rerun) if best_structures: counter += 1 log.debug('{}: {} PDBs mapped'.format(g.id, len(best_structures))) else: log.debug('{}, {}: no PDBs available'.format(g.id, uniprot_id)) log.info('{}/{}: number of genes with at least one experimental structure'.format(len(self.genes_with_experimental_structures), len(self.genes))) log.info('Completed UniProt --> best PDB mapping. See the "df_pdb_ranking" attribute for a summary dataframe.')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:get_properties_by_type; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:type; 6, default_parameter; 6, 7; 6, 8; 7, identifier:recursive; 8, True; 9, default_parameter; 9, 10; 9, 11; 10, identifier:parent_path; 11, string:""; 12, block; 12, 13; 12, 15; 12, 22; 12, 60; 12, 64; 12, 175; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 17; 16, identifier:parent_path; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, augmented_assignment:+=; 19, 20; 19, 21; 20, identifier:parent_path; 21, string:"."; 22, if_statement; 22, 23; 22, 28; 23, call; 23, 24; 23, 25; 24, identifier:isinstance; 25, argument_list; 25, 26; 25, 27; 26, identifier:type; 27, identifier:str; 28, block; 28, 29; 29, if_statement; 29, 30; 29, 33; 29, 53; 30, comparison_operator:==; 30, 31; 30, 32; 31, identifier:type; 32, string:"*"; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:type; 37, binary_operator:-; 37, 38; 37, 46; 38, call; 38, 39; 38, 40; 39, identifier:set; 40, argument_list; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:MAPPING_NAME_TYPE; 44, identifier:keys; 45, argument_list; 46, call; 46, 47; 46, 48; 47, identifier:set; 48, argument_list; 48, 49; 49, list:["nested", "multi_field", "multifield"]; 49, 50; 49, 51; 49, 52; 50, string:"nested"; 51, string:"multi_field"; 52, string:"multifield"; 53, else_clause; 53, 54; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:type; 58, list:[type]; 58, 59; 59, identifier:type; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:properties; 63, list:[]; 64, for_statement; 64, 65; 64, 66; 64, 76; 65, identifier:prop; 66, call; 66, 67; 66, 68; 67, identifier:list; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:properties; 74, identifier:values; 75, argument_list; 76, block; 76, 77; 76, 139; 76, 144; 77, if_statement; 77, 78; 77, 83; 77, 98; 78, comparison_operator:in; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:prop; 81, identifier:type; 82, identifier:type; 83, block; 83, 84; 83, 97; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:properties; 88, identifier:append; 89, argument_list; 89, 90; 90, tuple; 90, 91; 90, 96; 91, binary_operator:+; 91, 92; 91, 93; 92, identifier:parent_path; 93, attribute; 93, 94; 93, 95; 94, identifier:prop; 95, identifier:name; 96, identifier:prop; 97, continue_statement; 98, elif_clause; 98, 99; 98, 124; 99, boolean_operator:and; 99, 100; 99, 113; 100, boolean_operator:and; 100, 101; 100, 106; 101, comparison_operator:==; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:prop; 104, identifier:type; 105, string:"multi_field"; 106, comparison_operator:in; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:prop; 109, identifier:name; 110, attribute; 110, 111; 110, 112; 111, identifier:prop; 112, identifier:fields; 113, comparison_operator:in; 113, 114; 113, 123; 114, attribute; 114, 115; 114, 122; 115, subscript; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:prop; 118, identifier:fields; 119, attribute; 119, 120; 119, 121; 120, identifier:prop; 121, identifier:name; 122, identifier:type; 123, identifier:type; 124, block; 124, 125; 124, 138; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:properties; 129, identifier:append; 130, argument_list; 130, 131; 131, tuple; 131, 132; 131, 137; 132, binary_operator:+; 132, 133; 132, 134; 133, identifier:parent_path; 134, attribute; 134, 135; 134, 136; 135, identifier:prop; 136, identifier:name; 137, identifier:prop; 138, continue_statement; 139, if_statement; 139, 140; 139, 142; 140, not_operator; 140, 141; 141, identifier:recursive; 142, block; 142, 143; 143, continue_statement; 144, if_statement; 144, 145; 144, 152; 145, comparison_operator:in; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:prop; 148, identifier:type; 149, list:["nested", "object"]; 149, 150; 149, 151; 150, string:"nested"; 151, string:"object"; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:properties; 157, identifier:extend; 158, argument_list; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:prop; 162, identifier:get_properties_by_type; 163, argument_list; 163, 164; 163, 165; 163, 168; 164, identifier:type; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:recursive; 167, identifier:recursive; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:parent_path; 170, binary_operator:+; 170, 171; 170, 172; 171, identifier:parent_path; 172, attribute; 172, 173; 172, 174; 173, identifier:prop; 174, identifier:name; 175, return_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:sorted; 178, argument_list; 178, 179; 179, identifier:properties
def get_properties_by_type(self, type, recursive=True, parent_path=""): """ Returns a sorted list of fields that match the type. :param type the type of the field "string","integer" or a list of types :param recursive recurse to sub object :returns a sorted list of fields the match the type """ if parent_path: parent_path += "." if isinstance(type, str): if type == "*": type = set(MAPPING_NAME_TYPE.keys()) - set(["nested", "multi_field", "multifield"]) else: type = [type] properties = [] for prop in list(self.properties.values()): if prop.type in type: properties.append((parent_path + prop.name, prop)) continue elif prop.type == "multi_field" and prop.name in prop.fields and prop.fields[prop.name].type in type: properties.append((parent_path + prop.name, prop)) continue if not recursive: continue if prop.type in ["nested", "object"]: properties.extend( prop.get_properties_by_type(type, recursive=recursive, parent_path=parent_path + prop.name)) return sorted(properties)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:insert; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:index; 6, identifier:key; 7, identifier:value; 8, block; 8, 9; 8, 11; 8, 44; 8, 54; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 17; 12, comparison_operator:in; 12, 13; 12, 14; 13, identifier:key; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:keyOrder; 17, block; 17, 18; 17, 29; 17, 35; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:n; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:keyOrder; 26, identifier:index; 27, argument_list; 27, 28; 28, identifier:key; 29, delete_statement; 29, 30; 30, subscript; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:keyOrder; 34, identifier:n; 35, if_statement; 35, 36; 35, 39; 36, comparison_operator:<; 36, 37; 36, 38; 37, identifier:n; 38, identifier:index; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, augmented_assignment:-=; 41, 42; 41, 43; 42, identifier:index; 43, integer:1; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 51; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:keyOrder; 50, identifier:insert; 51, argument_list; 51, 52; 51, 53; 52, identifier:index; 53, identifier:key; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 63; 56, attribute; 56, 57; 56, 62; 57, call; 57, 58; 57, 59; 58, identifier:super; 59, argument_list; 59, 60; 59, 61; 60, identifier:SortedDict; 61, identifier:self; 62, identifier:__setitem__; 63, argument_list; 63, 64; 63, 65; 64, identifier:key; 65, identifier:value
def insert(self, index, key, value): """Inserts the key, value pair before the item with the given index.""" if key in self.keyOrder: n = self.keyOrder.index(key) del self.keyOrder[n] if n < index: index -= 1 self.keyOrder.insert(index, key) super(SortedDict, self).__setitem__(key, value)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 11; 2, function_name:sort_qualified_edges; 3, parameters; 3, 4; 4, identifier:graph; 5, type; 5, 6; 6, generic_type; 6, 7; 6, 8; 7, identifier:Iterable; 8, type_parameter; 8, 9; 9, type; 9, 10; 10, identifier:EdgeTuple; 11, block; 11, 12; 11, 14; 11, 58; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:qualified_edges; 17, generator_expression; 17, 18; 17, 23; 17, 40; 18, tuple; 18, 19; 18, 20; 18, 21; 18, 22; 19, identifier:u; 20, identifier:v; 21, identifier:k; 22, identifier:d; 23, for_in_clause; 23, 24; 23, 29; 24, pattern_list; 24, 25; 24, 26; 24, 27; 24, 28; 25, identifier:u; 26, identifier:v; 27, identifier:k; 28, identifier:d; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:graph; 32, identifier:edges; 33, argument_list; 33, 34; 33, 37; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:keys; 36, True; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:data; 39, True; 40, if_clause; 40, 41; 41, boolean_operator:and; 41, 42; 41, 50; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:graph; 45, identifier:has_edge_citation; 46, argument_list; 46, 47; 46, 48; 46, 49; 47, identifier:u; 48, identifier:v; 49, identifier:k; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:graph; 53, identifier:has_edge_evidence; 54, argument_list; 54, 55; 54, 56; 54, 57; 55, identifier:u; 56, identifier:v; 57, identifier:k; 58, return_statement; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:sorted; 61, argument_list; 61, 62; 61, 63; 62, identifier:qualified_edges; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:key; 65, identifier:_sort_qualified_edges_helper
def sort_qualified_edges(graph) -> Iterable[EdgeTuple]: """Return the qualified edges, sorted first by citation, then by evidence, then by annotations. :param BELGraph graph: A BEL graph """ qualified_edges = ( (u, v, k, d) for u, v, k, d in graph.edges(keys=True, data=True) if graph.has_edge_citation(u, v, k) and graph.has_edge_evidence(u, v, k) ) return sorted(qualified_edges, key=_sort_qualified_edges_helper)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 1, 10; 2, function_name:_citation_sort_key; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:t; 6, type; 6, 7; 7, identifier:EdgeTuple; 8, type; 8, 9; 9, identifier:str; 10, block; 10, 11; 10, 13; 11, expression_statement; 11, 12; 12, comment; 13, return_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, string:'"{}", "{}"'; 17, identifier:format; 18, argument_list; 18, 19; 18, 26; 19, subscript; 19, 20; 19, 25; 20, subscript; 20, 21; 20, 24; 21, subscript; 21, 22; 21, 23; 22, identifier:t; 23, integer:3; 24, identifier:CITATION; 25, identifier:CITATION_TYPE; 26, subscript; 26, 27; 26, 32; 27, subscript; 27, 28; 27, 31; 28, subscript; 28, 29; 28, 30; 29, identifier:t; 30, integer:3; 31, identifier:CITATION; 32, identifier:CITATION_REFERENCE
def _citation_sort_key(t: EdgeTuple) -> str: """Make a confusing 4 tuple sortable by citation.""" return '"{}", "{}"'.format(t[3][CITATION][CITATION_TYPE], t[3][CITATION][CITATION_REFERENCE])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 1, 18; 2, function_name:clean_pubmed_identifiers; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:pmids; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:Iterable; 9, type_parameter; 9, 10; 10, type; 10, 11; 11, identifier:str; 12, type; 12, 13; 13, generic_type; 13, 14; 13, 15; 14, identifier:List; 15, type_parameter; 15, 16; 16, type; 16, 17; 17, identifier:str; 18, block; 18, 19; 18, 21; 19, expression_statement; 19, 20; 20, comment; 21, return_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:sorted; 24, argument_list; 24, 25; 25, set_comprehension; 25, 26; 25, 34; 26, call; 26, 27; 26, 33; 27, attribute; 27, 28; 27, 32; 28, call; 28, 29; 28, 30; 29, identifier:str; 30, argument_list; 30, 31; 31, identifier:pmid; 32, identifier:strip; 33, argument_list; 34, for_in_clause; 34, 35; 34, 36; 35, identifier:pmid; 36, identifier:pmids
def clean_pubmed_identifiers(pmids: Iterable[str]) -> List[str]: """Clean a list of PubMed identifiers with string strips, deduplicates, and sorting.""" return sorted({str(pmid).strip() for pmid in pmids})
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 7; 2, function_name:hash_dump; 3, parameters; 3, 4; 4, identifier:data; 5, type; 5, 6; 6, identifier:str; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, return_statement; 10, 11; 11, call; 11, 12; 11, 33; 12, attribute; 12, 13; 12, 32; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:hashlib; 16, identifier:sha512; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 30; 19, attribute; 19, 20; 19, 29; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:json; 23, identifier:dumps; 24, argument_list; 24, 25; 24, 26; 25, identifier:data; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:sort_keys; 28, True; 29, identifier:encode; 30, argument_list; 30, 31; 31, string:'utf-8'; 32, identifier:hexdigest; 33, argument_list
def hash_dump(data) -> str: """Hash an arbitrary JSON dictionary by dumping it in sorted order, encoding it in UTF-8, then hashing the bytes. :param data: An arbitrary JSON-serializable object :type data: dict or list or tuple """ return hashlib.sha512(json.dumps(data, sort_keys=True).encode('utf-8')).hexdigest()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:yield_sorted_by_type; 3, parameters; 3, 4; 4, list_splat_pattern; 4, 5; 5, identifier:typelist; 6, block; 6, 7; 6, 9; 6, 42; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:decorate; 11, parameters; 11, 12; 12, identifier:fun; 13, block; 13, 14; 13, 40; 14, decorated_definition; 14, 15; 14, 20; 15, decorator; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:wraps; 18, argument_list; 18, 19; 19, identifier:fun; 20, function_definition; 20, 21; 20, 22; 20, 27; 21, function_name:decorated; 22, parameters; 22, 23; 22, 25; 23, list_splat_pattern; 23, 24; 24, identifier:args; 25, dictionary_splat_pattern; 25, 26; 26, identifier:kwds; 27, block; 27, 28; 28, return_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:iterate_by_type; 31, argument_list; 31, 32; 31, 39; 32, call; 32, 33; 32, 34; 33, identifier:fun; 34, argument_list; 34, 35; 34, 37; 35, list_splat; 35, 36; 36, identifier:args; 37, dictionary_splat; 37, 38; 38, identifier:kwds; 39, identifier:typelist; 40, return_statement; 40, 41; 41, identifier:decorated; 42, return_statement; 42, 43; 43, identifier:decorate
def yield_sorted_by_type(*typelist): """ a useful decorator for the collect_impl method of SuperChange subclasses. Caches the yielded changes, and re-emits them collected by their type. The order of the types can be specified by listing the types as arguments to this decorator. Unlisted types will be yielded last in no guaranteed order. Grouping happens by exact type match only. Inheritance is not taken into consideration for grouping. """ def decorate(fun): @wraps(fun) def decorated(*args, **kwds): return iterate_by_type(fun(*args, **kwds), typelist) return decorated return decorate
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 1, 22; 2, function_name:build_route_timetable; 3, parameters; 3, 4; 3, 8; 3, 12; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:feed; 6, type; 6, 7; 7, string:"Feed"; 8, typed_parameter; 8, 9; 8, 10; 9, identifier:route_id; 10, type; 10, 11; 11, identifier:str; 12, typed_parameter; 12, 13; 12, 14; 13, identifier:dates; 14, type; 14, 15; 15, generic_type; 15, 16; 15, 17; 16, identifier:List; 17, type_parameter; 17, 18; 18, type; 18, 19; 19, identifier:str; 20, type; 20, 21; 21, identifier:DataFrame; 22, block; 22, 23; 22, 25; 22, 34; 22, 44; 22, 58; 22, 72; 22, 81; 22, 85; 22, 169; 22, 178; 23, expression_statement; 23, 24; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:dates; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:feed; 31, identifier:restrict_dates; 32, argument_list; 32, 33; 33, identifier:dates; 34, if_statement; 34, 35; 34, 37; 35, not_operator; 35, 36; 36, identifier:dates; 37, block; 37, 38; 38, return_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:pd; 42, identifier:DataFrame; 43, argument_list; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:t; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:pd; 50, identifier:merge; 51, argument_list; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:feed; 54, identifier:trips; 55, attribute; 55, 56; 55, 57; 56, identifier:feed; 57, identifier:stop_times; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:t; 61, call; 61, 62; 61, 71; 62, attribute; 62, 63; 62, 70; 63, subscript; 63, 64; 63, 65; 64, identifier:t; 65, comparison_operator:==; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:t; 68, string:"route_id"; 69, identifier:route_id; 70, identifier:copy; 71, argument_list; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:a; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:feed; 78, identifier:compute_trip_activity; 79, argument_list; 79, 80; 80, identifier:dates; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:frames; 84, list:[]; 85, for_statement; 85, 86; 85, 87; 85, 88; 85, 89; 86, identifier:date; 87, identifier:dates; 88, comment; 89, block; 89, 90; 89, 103; 89, 120; 89, 126; 89, 127; 89, 128; 89, 129; 89, 144; 89, 162; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:ids; 93, subscript; 93, 94; 93, 97; 93, 102; 94, attribute; 94, 95; 94, 96; 95, identifier:a; 96, identifier:loc; 97, comparison_operator:==; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:a; 100, identifier:date; 101, integer:1; 102, string:"trip_id"; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:f; 106, call; 106, 107; 106, 119; 107, attribute; 107, 108; 107, 118; 108, subscript; 108, 109; 108, 110; 109, identifier:t; 110, call; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, subscript; 112, 113; 112, 114; 113, identifier:t; 114, string:"trip_id"; 115, identifier:isin; 116, argument_list; 116, 117; 117, identifier:ids; 118, identifier:copy; 119, argument_list; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:f; 124, string:"date"; 125, identifier:date; 126, comment; 127, comment; 128, comment; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:f; 133, string:"dt"; 134, call; 134, 135; 134, 140; 135, attribute; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:f; 138, string:"departure_time"; 139, identifier:fillna; 140, argument_list; 140, 141; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:method; 143, string:"ffill"; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:f; 148, string:"min_dt"; 149, call; 149, 150; 149, 160; 150, attribute; 150, 151; 150, 159; 151, subscript; 151, 152; 151, 158; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:f; 155, identifier:groupby; 156, argument_list; 156, 157; 157, string:"trip_id"; 158, string:"dt"; 159, identifier:transform; 160, argument_list; 160, 161; 161, identifier:min; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:frames; 166, identifier:append; 167, argument_list; 167, 168; 168, identifier:f; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:f; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:pd; 175, identifier:concat; 176, argument_list; 176, 177; 177, identifier:frames; 178, return_statement; 178, 179; 179, call; 179, 180; 179, 191; 180, attribute; 180, 181; 180, 190; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:f; 184, identifier:sort_values; 185, argument_list; 185, 186; 186, list:["date", "min_dt", "stop_sequence"]; 186, 187; 186, 188; 186, 189; 187, string:"date"; 188, string:"min_dt"; 189, string:"stop_sequence"; 190, identifier:drop; 191, argument_list; 191, 192; 191, 195; 192, list:["min_dt", "dt"]; 192, 193; 192, 194; 193, string:"min_dt"; 194, string:"dt"; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:axis; 197, integer:1
def build_route_timetable( feed: "Feed", route_id: str, dates: List[str] ) -> DataFrame: """ Return a timetable for the given route and dates. Parameters ---------- feed : Feed route_id : string ID of a route in ``feed.routes`` dates : string or list A YYYYMMDD date string or list thereof Returns ------- DataFrame The columns are all those in ``feed.trips`` plus those in ``feed.stop_times`` plus ``'date'``, and the trip IDs are restricted to the given route ID. The result is sorted first by date and then by grouping by trip ID and sorting the groups by their first departure time. Skip dates outside of the Feed's dates. If there is no route activity on the given dates, then return an empty DataFrame. Notes ----- Assume the following feed attributes are not ``None``: - ``feed.stop_times`` - Those used in :func:`.trips.get_trips` """ dates = feed.restrict_dates(dates) if not dates: return pd.DataFrame() t = pd.merge(feed.trips, feed.stop_times) t = t[t["route_id"] == route_id].copy() a = feed.compute_trip_activity(dates) frames = [] for date in dates: # Slice to trips active on date ids = a.loc[a[date] == 1, "trip_id"] f = t[t["trip_id"].isin(ids)].copy() f["date"] = date # Groupby trip ID and sort groups by their minimum departure time. # For some reason NaN departure times mess up the transform below. # So temporarily fill NaN departure times as a workaround. f["dt"] = f["departure_time"].fillna(method="ffill") f["min_dt"] = f.groupby("trip_id")["dt"].transform(min) frames.append(f) f = pd.concat(frames) return f.sort_values(["date", "min_dt", "stop_sequence"]).drop( ["min_dt", "dt"], axis=1 )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 1, 14; 2, function_name:almost_equal; 3, parameters; 3, 4; 3, 8; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:f; 6, type; 6, 7; 7, identifier:DataFrame; 8, typed_parameter; 8, 9; 8, 10; 9, identifier:g; 10, type; 10, 11; 11, identifier:DataFrame; 12, type; 12, 13; 13, identifier:bool; 14, block; 14, 15; 14, 17; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 25; 17, 33; 18, boolean_operator:or; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:f; 21, identifier:empty; 22, attribute; 22, 23; 22, 24; 23, identifier:g; 24, identifier:empty; 25, block; 25, 26; 26, return_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:f; 30, identifier:equals; 31, argument_list; 31, 32; 32, identifier:g; 33, else_clause; 33, 34; 33, 35; 34, comment; 35, block; 35, 36; 35, 65; 35, 94; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:F; 39, parenthesized_expression; 39, 40; 40, call; 40, 41; 40, 61; 41, attribute; 41, 42; 41, 60; 42, call; 42, 43; 42, 53; 43, attribute; 43, 44; 43, 52; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:f; 47, identifier:sort_index; 48, argument_list; 48, 49; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:axis; 51, integer:1; 52, identifier:sort_values; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:list; 56, argument_list; 56, 57; 57, attribute; 57, 58; 57, 59; 58, identifier:f; 59, identifier:columns; 60, identifier:reset_index; 61, argument_list; 61, 62; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:drop; 64, True; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:G; 68, parenthesized_expression; 68, 69; 69, call; 69, 70; 69, 90; 70, attribute; 70, 71; 70, 89; 71, call; 71, 72; 71, 82; 72, attribute; 72, 73; 72, 81; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:g; 76, identifier:sort_index; 77, argument_list; 77, 78; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:axis; 80, integer:1; 81, identifier:sort_values; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:list; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:g; 88, identifier:columns; 89, identifier:reset_index; 90, argument_list; 90, 91; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:drop; 93, True; 94, return_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:F; 98, identifier:equals; 99, argument_list; 99, 100; 100, identifier:G
def almost_equal(f: DataFrame, g: DataFrame) -> bool: """ Return ``True`` if and only if the given DataFrames are equal after sorting their columns names, sorting their values, and reseting their indices. """ if f.empty or g.empty: return f.equals(g) else: # Put in canonical order F = ( f.sort_index(axis=1) .sort_values(list(f.columns)) .reset_index(drop=True) ) G = ( g.sort_index(axis=1) .sort_values(list(g.columns)) .reset_index(drop=True) ) return F.equals(G)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 1, 22; 2, function_name:build_stop_timetable; 3, parameters; 3, 4; 3, 8; 3, 12; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:feed; 6, type; 6, 7; 7, string:"Feed"; 8, typed_parameter; 8, 9; 8, 10; 9, identifier:stop_id; 10, type; 10, 11; 11, identifier:str; 12, typed_parameter; 12, 13; 12, 14; 13, identifier:dates; 14, type; 14, 15; 15, generic_type; 15, 16; 15, 17; 16, identifier:List; 17, type_parameter; 17, 18; 18, type; 18, 19; 19, identifier:str; 20, type; 20, 21; 21, identifier:DataFrame; 22, block; 22, 23; 22, 25; 22, 34; 22, 44; 22, 58; 22, 72; 22, 81; 22, 85; 22, 133; 22, 142; 23, expression_statement; 23, 24; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:dates; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:feed; 31, identifier:restrict_dates; 32, argument_list; 32, 33; 33, identifier:dates; 34, if_statement; 34, 35; 34, 37; 35, not_operator; 35, 36; 36, identifier:dates; 37, block; 37, 38; 38, return_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:pd; 42, identifier:DataFrame; 43, argument_list; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:t; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:pd; 50, identifier:merge; 51, argument_list; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:feed; 54, identifier:trips; 55, attribute; 55, 56; 55, 57; 56, identifier:feed; 57, identifier:stop_times; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:t; 61, call; 61, 62; 61, 71; 62, attribute; 62, 63; 62, 70; 63, subscript; 63, 64; 63, 65; 64, identifier:t; 65, comparison_operator:==; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:t; 68, string:"stop_id"; 69, identifier:stop_id; 70, identifier:copy; 71, argument_list; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:a; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:feed; 78, identifier:compute_trip_activity; 79, argument_list; 79, 80; 80, identifier:dates; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:frames; 84, list:[]; 85, for_statement; 85, 86; 85, 87; 85, 88; 85, 89; 86, identifier:date; 87, identifier:dates; 88, comment; 89, block; 89, 90; 89, 103; 89, 120; 89, 126; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:ids; 93, subscript; 93, 94; 93, 97; 93, 102; 94, attribute; 94, 95; 94, 96; 95, identifier:a; 96, identifier:loc; 97, comparison_operator:==; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:a; 100, identifier:date; 101, integer:1; 102, string:"trip_id"; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:f; 106, call; 106, 107; 106, 119; 107, attribute; 107, 108; 107, 118; 108, subscript; 108, 109; 108, 110; 109, identifier:t; 110, call; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, subscript; 112, 113; 112, 114; 113, identifier:t; 114, string:"trip_id"; 115, identifier:isin; 116, argument_list; 116, 117; 117, identifier:ids; 118, identifier:copy; 119, argument_list; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:f; 124, string:"date"; 125, identifier:date; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:frames; 130, identifier:append; 131, argument_list; 131, 132; 132, identifier:f; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:f; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:pd; 139, identifier:concat; 140, argument_list; 140, 141; 141, identifier:frames; 142, return_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:f; 146, identifier:sort_values; 147, argument_list; 147, 148; 148, list:["date", "departure_time"]; 148, 149; 148, 150; 149, string:"date"; 150, string:"departure_time"
def build_stop_timetable( feed: "Feed", stop_id: str, dates: List[str] ) -> DataFrame: """ Return a DataFrame containing the timetable for the given stop ID and dates. Parameters ---------- feed : Feed stop_id : string ID of the stop for which to build the timetable dates : string or list A YYYYMMDD date string or list thereof Returns ------- DataFrame The columns are all those in ``feed.trips`` plus those in ``feed.stop_times`` plus ``'date'``, and the stop IDs are restricted to the given stop ID. The result is sorted by date then departure time. Notes ----- Assume the following feed attributes are not ``None``: - ``feed.trips`` - Those used in :func:`.stop_times.get_stop_times` """ dates = feed.restrict_dates(dates) if not dates: return pd.DataFrame() t = pd.merge(feed.trips, feed.stop_times) t = t[t["stop_id"] == stop_id].copy() a = feed.compute_trip_activity(dates) frames = [] for date in dates: # Slice to stops active on date ids = a.loc[a[date] == 1, "trip_id"] f = t[t["trip_id"].isin(ids)].copy() f["date"] = date frames.append(f) f = pd.concat(frames) return f.sort_values(["date", "departure_time"])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_unit_property_names; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:unit_id; 7, None; 8, block; 8, 9; 8, 11; 8, 63; 9, expression_statement; 9, 10; 10, string:'''Get a list of property names for a given unit, or for all units if unit_id is None Parameters ---------- unit_id: int The unit id for which the property names will be returned If None (default), will return property names for all units Returns ---------- property_names The list of property names from the specified unit(s) '''; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:is; 12, 13; 12, 14; 13, identifier:unit_id; 14, None; 15, block; 15, 16; 15, 20; 15, 48; 15, 61; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:property_names; 19, list:[]; 20, for_statement; 20, 21; 20, 22; 20, 27; 21, identifier:unit_id; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:get_unit_ids; 26, argument_list; 27, block; 27, 28; 27, 37; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:curr_property_names; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:get_unit_property_names; 35, argument_list; 35, 36; 36, identifier:unit_id; 37, for_statement; 37, 38; 37, 39; 37, 40; 38, identifier:curr_property_name; 39, identifier:curr_property_names; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:property_names; 45, identifier:append; 46, argument_list; 46, 47; 47, identifier:curr_property_name; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:property_names; 51, call; 51, 52; 51, 53; 52, identifier:sorted; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:list; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:set; 59, argument_list; 59, 60; 60, identifier:property_names; 61, return_statement; 61, 62; 62, identifier:property_names; 63, if_statement; 63, 64; 63, 73; 63, 127; 64, call; 64, 65; 64, 66; 65, identifier:isinstance; 66, argument_list; 66, 67; 66, 68; 67, identifier:unit_id; 68, tuple; 68, 69; 68, 70; 69, identifier:int; 70, attribute; 70, 71; 70, 72; 71, identifier:np; 72, identifier:integer; 73, block; 73, 74; 74, if_statement; 74, 75; 74, 82; 74, 115; 75, comparison_operator:in; 75, 76; 75, 77; 76, identifier:unit_id; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:get_unit_ids; 81, argument_list; 82, block; 82, 83; 82, 98; 82, 113; 83, if_statement; 83, 84; 83, 89; 84, comparison_operator:not; 84, 85; 84, 86; 85, identifier:unit_id; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:_unit_properties; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 97; 92, subscript; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:_unit_properties; 96, identifier:unit_id; 97, dictionary; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:property_names; 101, call; 101, 102; 101, 103; 102, identifier:sorted; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 112; 105, attribute; 105, 106; 105, 111; 106, subscript; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:_unit_properties; 110, identifier:unit_id; 111, identifier:keys; 112, argument_list; 113, return_statement; 113, 114; 114, identifier:property_names; 115, else_clause; 115, 116; 116, block; 116, 117; 117, raise_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:ValueError; 120, argument_list; 120, 121; 121, binary_operator:+; 121, 122; 121, 126; 122, call; 122, 123; 122, 124; 123, identifier:str; 124, argument_list; 124, 125; 125, identifier:unit_id; 126, string:" is not a valid unit_id"; 127, else_clause; 127, 128; 128, block; 128, 129; 129, raise_statement; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:ValueError; 132, argument_list; 132, 133; 133, binary_operator:+; 133, 134; 133, 138; 134, call; 134, 135; 134, 136; 135, identifier:str; 136, argument_list; 136, 137; 137, identifier:unit_id; 138, string:" must be an int"
def get_unit_property_names(self, unit_id=None): '''Get a list of property names for a given unit, or for all units if unit_id is None Parameters ---------- unit_id: int The unit id for which the property names will be returned If None (default), will return property names for all units Returns ---------- property_names The list of property names from the specified unit(s) ''' if unit_id is None: property_names = [] for unit_id in self.get_unit_ids(): curr_property_names = self.get_unit_property_names(unit_id) for curr_property_name in curr_property_names: property_names.append(curr_property_name) property_names = sorted(list(set(property_names))) return property_names if isinstance(unit_id, (int, np.integer)): if unit_id in self.get_unit_ids(): if unit_id not in self._unit_properties: self._unit_properties[unit_id] = {} property_names = sorted(self._unit_properties[unit_id].keys()) return property_names else: raise ValueError(str(unit_id) + " is not a valid unit_id") else: raise ValueError(str(unit_id) + " must be an int")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:copy_unit_properties; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:sorting; 6, default_parameter; 6, 7; 6, 8; 7, identifier:unit_ids; 8, None; 9, block; 9, 10; 9, 12; 9, 25; 10, expression_statement; 10, 11; 11, string:'''Copy unit properties from another sorting extractor to the current sorting extractor. Parameters ---------- sorting: SortingExtractor The sorting extractor from which the properties will be copied unit_ids: (array_like, int) The list (or single value) of unit_ids for which the properties will be copied. '''; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:unit_ids; 15, None; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:unit_ids; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:sorting; 23, identifier:get_unit_ids; 24, argument_list; 25, if_statement; 25, 26; 25, 31; 25, 76; 26, call; 26, 27; 26, 28; 27, identifier:isinstance; 28, argument_list; 28, 29; 28, 30; 29, identifier:unit_ids; 30, identifier:int; 31, block; 31, 32; 31, 43; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:curr_property_names; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:sorting; 38, identifier:get_unit_property_names; 39, argument_list; 39, 40; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:unit_id; 42, identifier:unit_ids; 43, for_statement; 43, 44; 43, 45; 43, 46; 44, identifier:curr_property_name; 45, identifier:curr_property_names; 46, block; 46, 47; 46, 61; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:value; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:sorting; 53, identifier:get_unit_property; 54, argument_list; 54, 55; 54, 58; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:unit_id; 57, identifier:unit_ids; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:property_name; 60, identifier:curr_property_name; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:set_unit_property; 66, argument_list; 66, 67; 66, 70; 66, 73; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:unit_id; 69, identifier:unit_ids; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:property_name; 72, identifier:curr_property_name; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:value; 75, identifier:value; 76, else_clause; 76, 77; 77, block; 77, 78; 78, for_statement; 78, 79; 78, 80; 78, 81; 79, identifier:unit_id; 80, identifier:unit_ids; 81, block; 81, 82; 81, 93; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:curr_property_names; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:sorting; 88, identifier:get_unit_property_names; 89, argument_list; 89, 90; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:unit_id; 92, identifier:unit_id; 93, for_statement; 93, 94; 93, 95; 93, 96; 94, identifier:curr_property_name; 95, identifier:curr_property_names; 96, block; 96, 97; 96, 111; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:value; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:sorting; 103, identifier:get_unit_property; 104, argument_list; 104, 105; 104, 108; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:unit_id; 107, identifier:unit_id; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:property_name; 110, identifier:curr_property_name; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:set_unit_property; 116, argument_list; 116, 117; 116, 120; 116, 123; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:unit_id; 119, identifier:unit_id; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:property_name; 122, identifier:curr_property_name; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:value; 125, identifier:value
def copy_unit_properties(self, sorting, unit_ids=None): '''Copy unit properties from another sorting extractor to the current sorting extractor. Parameters ---------- sorting: SortingExtractor The sorting extractor from which the properties will be copied unit_ids: (array_like, int) The list (or single value) of unit_ids for which the properties will be copied. ''' if unit_ids is None: unit_ids = sorting.get_unit_ids() if isinstance(unit_ids, int): curr_property_names = sorting.get_unit_property_names(unit_id=unit_ids) for curr_property_name in curr_property_names: value = sorting.get_unit_property(unit_id=unit_ids, property_name=curr_property_name) self.set_unit_property(unit_id=unit_ids, property_name=curr_property_name, value=value) else: for unit_id in unit_ids: curr_property_names = sorting.get_unit_property_names(unit_id=unit_id) for curr_property_name in curr_property_names: value = sorting.get_unit_property(unit_id=unit_id, property_name=curr_property_name) self.set_unit_property(unit_id=unit_id, property_name=curr_property_name, value=value)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:copy_unit_spike_features; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:sorting; 6, default_parameter; 6, 7; 6, 8; 7, identifier:unit_ids; 8, None; 9, block; 9, 10; 9, 12; 9, 25; 10, expression_statement; 10, 11; 11, string:'''Copy unit spike features from another sorting extractor to the current sorting extractor. Parameters ---------- sorting: SortingExtractor The sorting extractor from which the spike features will be copied unit_ids: (array_like, int) The list (or single value) of unit_ids for which the spike features will be copied. def get_unit_spike_features(self, unit_id, feature_name, start_frame=None, end_frame=None): '''; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:unit_ids; 15, None; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:unit_ids; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:sorting; 23, identifier:get_unit_ids; 24, argument_list; 25, if_statement; 25, 26; 25, 31; 25, 76; 26, call; 26, 27; 26, 28; 27, identifier:isinstance; 28, argument_list; 28, 29; 28, 30; 29, identifier:unit_ids; 30, identifier:int; 31, block; 31, 32; 31, 43; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:curr_feature_names; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:sorting; 38, identifier:get_unit_spike_feature_names; 39, argument_list; 39, 40; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:unit_id; 42, identifier:unit_ids; 43, for_statement; 43, 44; 43, 45; 43, 46; 44, identifier:curr_feature_name; 45, identifier:curr_feature_names; 46, block; 46, 47; 46, 61; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:value; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:sorting; 53, identifier:get_unit_spike_features; 54, argument_list; 54, 55; 54, 58; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:unit_id; 57, identifier:unit_ids; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:feature_name; 60, identifier:curr_feature_name; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:set_unit_spike_features; 66, argument_list; 66, 67; 66, 70; 66, 73; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:unit_id; 69, identifier:unit_ids; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:feature_name; 72, identifier:curr_feature_name; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:value; 75, identifier:value; 76, else_clause; 76, 77; 77, block; 77, 78; 78, for_statement; 78, 79; 78, 80; 78, 81; 79, identifier:unit_id; 80, identifier:unit_ids; 81, block; 81, 82; 81, 93; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:curr_feature_names; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:sorting; 88, identifier:get_unit_spike_feature_names; 89, argument_list; 89, 90; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:unit_id; 92, identifier:unit_id; 93, for_statement; 93, 94; 93, 95; 93, 96; 94, identifier:curr_feature_name; 95, identifier:curr_feature_names; 96, block; 96, 97; 96, 111; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:value; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:sorting; 103, identifier:get_unit_spike_features; 104, argument_list; 104, 105; 104, 108; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:unit_id; 107, identifier:unit_id; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:feature_name; 110, identifier:curr_feature_name; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:set_unit_spike_features; 116, argument_list; 116, 117; 116, 120; 116, 123; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:unit_id; 119, identifier:unit_id; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:feature_name; 122, identifier:curr_feature_name; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:value; 125, identifier:value
def copy_unit_spike_features(self, sorting, unit_ids=None): '''Copy unit spike features from another sorting extractor to the current sorting extractor. Parameters ---------- sorting: SortingExtractor The sorting extractor from which the spike features will be copied unit_ids: (array_like, int) The list (or single value) of unit_ids for which the spike features will be copied. def get_unit_spike_features(self, unit_id, feature_name, start_frame=None, end_frame=None): ''' if unit_ids is None: unit_ids = sorting.get_unit_ids() if isinstance(unit_ids, int): curr_feature_names = sorting.get_unit_spike_feature_names(unit_id=unit_ids) for curr_feature_name in curr_feature_names: value = sorting.get_unit_spike_features(unit_id=unit_ids, feature_name=curr_feature_name) self.set_unit_spike_features(unit_id=unit_ids, feature_name=curr_feature_name, value=value) else: for unit_id in unit_ids: curr_feature_names = sorting.get_unit_spike_feature_names(unit_id=unit_id) for curr_feature_name in curr_feature_names: value = sorting.get_unit_spike_features(unit_id=unit_id, feature_name=curr_feature_name) self.set_unit_spike_features(unit_id=unit_id, feature_name=curr_feature_name, value=value)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:merge_units; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:unit_ids; 6, block; 6, 7; 6, 9; 6, 13; 6, 42; 6, 46; 7, expression_statement; 7, 8; 8, string:'''This function merges two roots from the curation tree according to the given unit_ids. It creates a new unit_id and root that has the merged roots as children. Parameters ---------- unit_ids: list The unit ids to be merged '''; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:root_ids; 12, list:[]; 13, for_statement; 13, 14; 13, 15; 13, 24; 14, identifier:i; 15, call; 15, 16; 15, 17; 16, identifier:range; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:len; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_roots; 24, block; 24, 25; 24, 35; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:root_id; 28, attribute; 28, 29; 28, 34; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_roots; 33, identifier:i; 34, identifier:unit_id; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:root_ids; 39, identifier:append; 40, argument_list; 40, 41; 41, identifier:root_id; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:indices_to_be_deleted; 45, list:[]; 46, if_statement; 46, 47; 46, 67; 46, 68; 46, 366; 47, parenthesized_expression; 47, 48; 48, boolean_operator:and; 48, 49; 48, 61; 49, call; 49, 50; 49, 56; 50, attribute; 50, 51; 50, 55; 51, call; 51, 52; 51, 53; 52, identifier:set; 53, argument_list; 53, 54; 54, identifier:unit_ids; 55, identifier:issubset; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:set; 59, argument_list; 59, 60; 60, identifier:root_ids; 61, comparison_operator:>; 61, 62; 61, 66; 62, call; 62, 63; 62, 64; 63, identifier:len; 64, argument_list; 64, 65; 65, identifier:unit_ids; 66, integer:1; 67, comment; 68, block; 68, 69; 68, 73; 68, 93; 68, 102; 68, 117; 68, 124; 68, 128; 68, 145; 68, 156; 68, 165; 68, 172; 68, 176; 68, 273; 68, 282; 68, 291; 68, 305; 68, 307; 68, 330; 68, 339; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:all_feature_names; 72, list:[]; 73, for_statement; 73, 74; 73, 75; 73, 76; 74, identifier:unit_id; 75, identifier:unit_ids; 76, block; 76, 77; 76, 86; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:feature_names; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:get_unit_spike_feature_names; 84, argument_list; 84, 85; 85, identifier:unit_id; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:all_feature_names; 90, identifier:append; 91, argument_list; 91, 92; 92, identifier:feature_names; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:shared_feature_names; 96, call; 96, 97; 96, 98; 97, identifier:set; 98, argument_list; 98, 99; 99, subscript; 99, 100; 99, 101; 100, identifier:all_feature_names; 101, integer:0; 102, for_statement; 102, 103; 102, 104; 102, 109; 103, identifier:feature_names; 104, subscript; 104, 105; 104, 106; 105, identifier:all_feature_names; 106, slice; 106, 107; 106, 108; 107, integer:1; 108, colon; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:shared_feature_names; 114, identifier:intersection_update; 115, argument_list; 115, 116; 116, identifier:feature_names; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:shared_feature_names; 120, call; 120, 121; 120, 122; 121, identifier:list; 122, argument_list; 122, 123; 123, identifier:shared_feature_names; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:shared_features; 127, list:[]; 128, for_statement; 128, 129; 128, 130; 128, 137; 129, identifier:i; 130, call; 130, 131; 130, 132; 131, identifier:range; 132, argument_list; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:len; 135, argument_list; 135, 136; 136, identifier:shared_feature_names; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:shared_features; 142, identifier:append; 143, argument_list; 143, 144; 144, list:[]; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:new_root_id; 148, binary_operator:+; 148, 149; 148, 155; 149, call; 149, 150; 149, 151; 150, identifier:max; 151, argument_list; 151, 152; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:_all_ids; 155, integer:1; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:_all_ids; 162, identifier:append; 163, argument_list; 163, 164; 164, identifier:new_root_id; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:new_root; 168, call; 168, 169; 168, 170; 169, identifier:Unit; 170, argument_list; 170, 171; 171, identifier:new_root_id; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:all_spike_trains; 175, list:[]; 176, for_statement; 176, 177; 176, 178; 176, 179; 177, identifier:unit_id; 178, identifier:unit_ids; 179, block; 179, 180; 179, 189; 179, 200; 179, 215; 179, 243; 179, 249; 179, 265; 179, 266; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:root_index; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:root_ids; 186, identifier:index; 187, argument_list; 187, 188; 188, identifier:unit_id; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:new_root; 193, identifier:add_child; 194, argument_list; 194, 195; 195, subscript; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:_roots; 199, identifier:root_index; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:all_spike_trains; 204, identifier:append; 205, argument_list; 205, 206; 206, call; 206, 207; 206, 214; 207, attribute; 207, 208; 207, 213; 208, subscript; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:_roots; 212, identifier:root_index; 213, identifier:get_spike_train; 214, argument_list; 215, for_statement; 215, 216; 215, 219; 215, 223; 216, pattern_list; 216, 217; 216, 218; 217, identifier:i; 218, identifier:feature_name; 219, call; 219, 220; 219, 221; 220, identifier:enumerate; 221, argument_list; 221, 222; 222, identifier:shared_feature_names; 223, block; 223, 224; 223, 234; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:features; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:get_unit_spike_features; 231, argument_list; 231, 232; 231, 233; 232, identifier:unit_id; 233, identifier:feature_name; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 241; 236, attribute; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:shared_features; 239, identifier:i; 240, identifier:append; 241, argument_list; 241, 242; 242, identifier:features; 243, delete_statement; 243, 244; 244, subscript; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:self; 247, identifier:_unit_features; 248, identifier:unit_id; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 258; 251, attribute; 251, 252; 251, 257; 252, subscript; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:_roots; 256, identifier:root_index; 257, identifier:set_spike_train; 258, argument_list; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:np; 262, identifier:asarray; 263, argument_list; 263, 264; 264, list:[]; 265, comment; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:indices_to_be_deleted; 270, identifier:append; 271, argument_list; 271, 272; 272, identifier:root_index; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:all_spike_trains; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:np; 279, identifier:concatenate; 280, argument_list; 280, 281; 281, identifier:all_spike_trains; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:sort_indices; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:np; 288, identifier:argsort; 289, argument_list; 289, 290; 290, identifier:all_spike_trains; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:new_root; 295, identifier:set_spike_train; 296, argument_list; 296, 297; 297, subscript; 297, 298; 297, 304; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:np; 301, identifier:asarray; 302, argument_list; 302, 303; 303, identifier:all_spike_trains; 304, identifier:sort_indices; 305, delete_statement; 305, 306; 306, identifier:all_spike_trains; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:self; 311, identifier:_roots; 312, list_comprehension; 312, 313; 312, 318; 312, 326; 313, subscript; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:self; 316, identifier:_roots; 317, identifier:i; 318, for_in_clause; 318, 319; 318, 322; 319, pattern_list; 319, 320; 319, 321; 320, identifier:i; 321, identifier:_; 322, call; 322, 323; 322, 324; 323, identifier:enumerate; 324, argument_list; 324, 325; 325, identifier:root_ids; 326, if_clause; 326, 327; 327, comparison_operator:not; 327, 328; 327, 329; 328, identifier:i; 329, identifier:indices_to_be_deleted; 330, expression_statement; 330, 331; 331, call; 331, 332; 331, 337; 332, attribute; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:self; 335, identifier:_roots; 336, identifier:append; 337, argument_list; 337, 338; 338, identifier:new_root; 339, for_statement; 339, 340; 339, 343; 339, 347; 340, pattern_list; 340, 341; 340, 342; 341, identifier:i; 342, identifier:feature_name; 343, call; 343, 344; 343, 345; 344, identifier:enumerate; 345, argument_list; 345, 346; 346, identifier:shared_feature_names; 347, block; 347, 348; 348, expression_statement; 348, 349; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:self; 352, identifier:set_unit_spike_features; 353, argument_list; 353, 354; 353, 355; 353, 356; 354, identifier:new_root_id; 355, identifier:feature_name; 356, subscript; 356, 357; 356, 365; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:np; 360, identifier:concatenate; 361, argument_list; 361, 362; 362, subscript; 362, 363; 362, 364; 363, identifier:shared_features; 364, identifier:i; 365, identifier:sort_indices; 366, else_clause; 366, 367; 367, block; 367, 368; 368, raise_statement; 368, 369; 369, call; 369, 370; 369, 371; 370, identifier:ValueError; 371, argument_list; 371, 372; 372, binary_operator:+; 372, 373; 372, 377; 373, call; 373, 374; 373, 375; 374, identifier:str; 375, argument_list; 375, 376; 376, identifier:unit_ids; 377, string:" has one or more invalid unit ids"
def merge_units(self, unit_ids): '''This function merges two roots from the curation tree according to the given unit_ids. It creates a new unit_id and root that has the merged roots as children. Parameters ---------- unit_ids: list The unit ids to be merged ''' root_ids = [] for i in range(len(self._roots)): root_id = self._roots[i].unit_id root_ids.append(root_id) indices_to_be_deleted = [] if(set(unit_ids).issubset(set(root_ids)) and len(unit_ids) > 1): #Find all unique feature names and create all feature lists all_feature_names = [] for unit_id in unit_ids: feature_names = self.get_unit_spike_feature_names(unit_id) all_feature_names.append(feature_names) shared_feature_names = set(all_feature_names[0]) for feature_names in all_feature_names[1:]: shared_feature_names.intersection_update(feature_names) shared_feature_names = list(shared_feature_names) shared_features = [] for i in range(len(shared_feature_names)): shared_features.append([]) new_root_id = max(self._all_ids)+1 self._all_ids.append(new_root_id) new_root = Unit(new_root_id) all_spike_trains = [] for unit_id in unit_ids: root_index = root_ids.index(unit_id) new_root.add_child(self._roots[root_index]) all_spike_trains.append(self._roots[root_index].get_spike_train()) for i, feature_name in enumerate(shared_feature_names): features = self.get_unit_spike_features(unit_id, feature_name) shared_features[i].append(features) del self._unit_features[unit_id] self._roots[root_index].set_spike_train(np.asarray([])) #clear spiketrain indices_to_be_deleted.append(root_index) all_spike_trains = np.concatenate(all_spike_trains) sort_indices = np.argsort(all_spike_trains) new_root.set_spike_train(np.asarray(all_spike_trains)[sort_indices]) del all_spike_trains self._roots = [self._roots[i] for i,_ in enumerate(root_ids) if i not in indices_to_be_deleted] self._roots.append(new_root) for i, feature_name in enumerate(shared_feature_names): self.set_unit_spike_features(new_root_id, feature_name, np.concatenate(shared_features[i])[sort_indices]) else: raise ValueError(str(unit_ids) + " has one or more invalid unit ids")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:split_unit; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:unit_id; 6, identifier:indices; 7, block; 7, 8; 7, 10; 7, 14; 7, 43; 8, expression_statement; 8, 9; 9, string:'''This function splits a root from the curation tree according to the given unit_id and indices. It creates two new unit_ids and roots that have the split root as a child. This function splits the spike train of the root by the given indices. Parameters ---------- unit_id: int The unit id to be split indices: list The indices of the unit spike train at which the spike train will be split. '''; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:root_ids; 13, list:[]; 14, for_statement; 14, 15; 14, 16; 14, 25; 15, identifier:i; 16, call; 16, 17; 16, 18; 17, identifier:range; 18, argument_list; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:len; 21, argument_list; 21, 22; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:_roots; 25, block; 25, 26; 25, 36; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:root_id; 29, attribute; 29, 30; 29, 35; 30, subscript; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:_roots; 34, identifier:i; 35, identifier:unit_id; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:root_ids; 40, identifier:append; 41, argument_list; 41, 42; 42, identifier:root_id; 43, if_statement; 43, 44; 43, 48; 43, 306; 44, parenthesized_expression; 44, 45; 45, comparison_operator:in; 45, 46; 45, 47; 46, identifier:unit_id; 47, identifier:root_ids; 48, block; 48, 49; 48, 69; 48, 78; 48, 86; 48, 98; 48, 124; 48, 145; 48, 151; 48, 153; 48, 164; 48, 173; 48, 180; 48, 187; 48, 194; 48, 205; 48, 214; 48, 221; 48, 228; 48, 235; 48, 244; 48, 253; 48, 294; 48, 300; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:indices_1; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:np; 55, identifier:sort; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:np; 60, identifier:asarray; 61, argument_list; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:list; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:set; 67, argument_list; 67, 68; 68, identifier:indices; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:root_index; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:root_ids; 75, identifier:index; 76, argument_list; 76, 77; 77, identifier:unit_id; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:new_child; 81, subscript; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_roots; 85, identifier:root_index; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:original_spike_train; 89, call; 89, 90; 89, 97; 90, attribute; 90, 91; 90, 96; 91, subscript; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:_roots; 95, identifier:root_index; 96, identifier:get_spike_train; 97, argument_list; 98, try_statement; 98, 99; 98, 106; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:spike_train_1; 103, subscript; 103, 104; 103, 105; 104, identifier:original_spike_train; 105, identifier:indices_1; 106, except_clause; 106, 107; 106, 108; 107, identifier:IndexError; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:print; 112, argument_list; 112, 113; 113, binary_operator:+; 113, 114; 113, 120; 114, binary_operator:+; 114, 115; 114, 119; 115, call; 115, 116; 115, 117; 116, identifier:str; 117, argument_list; 117, 118; 118, identifier:indices; 119, string:" out of bounds for the spike train of "; 120, call; 120, 121; 120, 122; 121, identifier:str; 122, argument_list; 122, 123; 123, identifier:unit_id; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:indices_2; 127, call; 127, 128; 127, 129; 128, identifier:list; 129, argument_list; 129, 130; 130, binary_operator:-; 130, 131; 130, 141; 131, call; 131, 132; 131, 133; 132, identifier:set; 133, argument_list; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:range; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:len; 139, argument_list; 139, 140; 140, identifier:original_spike_train; 141, call; 141, 142; 141, 143; 142, identifier:set; 143, argument_list; 143, 144; 144, identifier:indices_1; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:spike_train_2; 148, subscript; 148, 149; 148, 150; 149, identifier:original_spike_train; 150, identifier:indices_2; 151, delete_statement; 151, 152; 152, identifier:original_spike_train; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:new_root_1_id; 156, binary_operator:+; 156, 157; 156, 163; 157, call; 157, 158; 157, 159; 158, identifier:max; 159, argument_list; 159, 160; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:_all_ids; 163, integer:1; 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:self; 169, identifier:_all_ids; 170, identifier:append; 171, argument_list; 171, 172; 172, identifier:new_root_1_id; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:new_root_1; 176, call; 176, 177; 176, 178; 177, identifier:Unit; 178, argument_list; 178, 179; 179, identifier:new_root_1_id; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:new_root_1; 184, identifier:add_child; 185, argument_list; 185, 186; 186, identifier:new_child; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:new_root_1; 191, identifier:set_spike_train; 192, argument_list; 192, 193; 193, identifier:spike_train_1; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:new_root_2_id; 197, binary_operator:+; 197, 198; 197, 204; 198, call; 198, 199; 198, 200; 199, identifier:max; 200, argument_list; 200, 201; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:_all_ids; 204, integer:1; 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:_all_ids; 211, identifier:append; 212, argument_list; 212, 213; 213, identifier:new_root_2_id; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:new_root_2; 217, call; 217, 218; 217, 219; 218, identifier:Unit; 219, argument_list; 219, 220; 220, identifier:new_root_2_id; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:new_root_2; 225, identifier:add_child; 226, argument_list; 226, 227; 227, identifier:new_child; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:new_root_2; 232, identifier:set_spike_train; 233, argument_list; 233, 234; 234, identifier:spike_train_2; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 242; 237, attribute; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:_roots; 241, identifier:append; 242, argument_list; 242, 243; 243, identifier:new_root_1; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:self; 249, identifier:_roots; 250, identifier:append; 251, argument_list; 251, 252; 252, identifier:new_root_2; 253, for_statement; 253, 254; 253, 255; 253, 261; 254, identifier:feature_name; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:get_unit_spike_feature_names; 259, argument_list; 259, 260; 260, identifier:unit_id; 261, block; 261, 262; 261, 272; 261, 283; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:full_features; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:self; 268, identifier:get_unit_spike_features; 269, argument_list; 269, 270; 269, 271; 270, identifier:unit_id; 271, identifier:feature_name; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:self; 276, identifier:set_unit_spike_features; 277, argument_list; 277, 278; 277, 279; 277, 280; 278, identifier:new_root_1_id; 279, identifier:feature_name; 280, subscript; 280, 281; 280, 282; 281, identifier:full_features; 282, identifier:indices_1; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:set_unit_spike_features; 288, argument_list; 288, 289; 288, 290; 288, 291; 289, identifier:new_root_2_id; 290, identifier:feature_name; 291, subscript; 291, 292; 291, 293; 292, identifier:full_features; 293, identifier:indices_2; 294, delete_statement; 294, 295; 295, subscript; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:_unit_features; 299, identifier:unit_id; 300, delete_statement; 300, 301; 301, subscript; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:self; 304, identifier:_roots; 305, identifier:root_index; 306, else_clause; 306, 307; 307, block; 307, 308; 308, raise_statement; 308, 309; 309, call; 309, 310; 309, 311; 310, identifier:ValueError; 311, argument_list; 311, 312; 312, binary_operator:+; 312, 313; 312, 317; 313, call; 313, 314; 313, 315; 314, identifier:str; 315, argument_list; 315, 316; 316, identifier:unit_id; 317, string:" non-valid unit id"
def split_unit(self, unit_id, indices): '''This function splits a root from the curation tree according to the given unit_id and indices. It creates two new unit_ids and roots that have the split root as a child. This function splits the spike train of the root by the given indices. Parameters ---------- unit_id: int The unit id to be split indices: list The indices of the unit spike train at which the spike train will be split. ''' root_ids = [] for i in range(len(self._roots)): root_id = self._roots[i].unit_id root_ids.append(root_id) if(unit_id in root_ids): indices_1 = np.sort(np.asarray(list(set(indices)))) root_index = root_ids.index(unit_id) new_child = self._roots[root_index] original_spike_train = self._roots[root_index].get_spike_train() try: spike_train_1 = original_spike_train[indices_1] except IndexError: print(str(indices) + " out of bounds for the spike train of " + str(unit_id)) indices_2 = list(set(range(len(original_spike_train))) - set(indices_1)) spike_train_2 = original_spike_train[indices_2] del original_spike_train new_root_1_id = max(self._all_ids)+1 self._all_ids.append(new_root_1_id) new_root_1 = Unit(new_root_1_id) new_root_1.add_child(new_child) new_root_1.set_spike_train(spike_train_1) new_root_2_id = max(self._all_ids)+1 self._all_ids.append(new_root_2_id) new_root_2 = Unit(new_root_2_id) new_root_2.add_child(new_child) new_root_2.set_spike_train(spike_train_2) self._roots.append(new_root_1) self._roots.append(new_root_2) for feature_name in self.get_unit_spike_feature_names(unit_id): full_features = self.get_unit_spike_features(unit_id, feature_name) self.set_unit_spike_features(new_root_1_id, feature_name, full_features[indices_1]) self.set_unit_spike_features(new_root_2_id, feature_name, full_features[indices_2]) del self._unit_features[unit_id] del self._roots[root_index] else: raise ValueError(str(unit_id) + " non-valid unit id")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_find_best_fit; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:pbin; 6, block; 6, 7; 6, 9; 6, 37; 6, 51; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:fit; 12, generator_expression; 12, 13; 12, 26; 13, tuple; 13, 14; 13, 25; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:pbin; 17, identifier:fitness; 18, argument_list; 18, 19; 18, 22; 19, subscript; 19, 20; 19, 21; 20, identifier:r; 21, integer:0; 22, subscript; 22, 23; 22, 24; 23, identifier:r; 24, integer:1; 25, identifier:k; 26, for_in_clause; 26, 27; 26, 30; 27, pattern_list; 27, 28; 27, 29; 28, identifier:k; 29, identifier:r; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_sorted_rect; 35, identifier:items; 36, argument_list; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:fit; 40, generator_expression; 40, 41; 40, 42; 40, 45; 41, identifier:f; 42, for_in_clause; 42, 43; 42, 44; 43, identifier:f; 44, identifier:fit; 45, if_clause; 45, 46; 46, comparison_operator:is; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:f; 49, integer:0; 50, None; 51, try_statement; 51, 52; 51, 69; 52, block; 52, 53; 52, 67; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, pattern_list; 55, 56; 55, 57; 56, identifier:_; 57, identifier:rect; 58, call; 58, 59; 58, 60; 59, identifier:min; 60, argument_list; 60, 61; 60, 62; 61, identifier:fit; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:key; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:first_item; 67, return_statement; 67, 68; 68, identifier:rect; 69, except_clause; 69, 70; 69, 71; 70, identifier:ValueError; 71, block; 71, 72; 72, return_statement; 72, 73; 73, None
def _find_best_fit(self, pbin): """ Return best fitness rectangle from rectangles packing _sorted_rect list Arguments: pbin (PackingAlgorithm): Packing bin Returns: key of the rectangle with best fitness """ fit = ((pbin.fitness(r[0], r[1]), k) for k, r in self._sorted_rect.items()) fit = (f for f in fit if f[0] is not None) try: _, rect = min(fit, key=self.first_item) return rect except ValueError: return None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 1, 8; 2, function_name:_item_attributes_match; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:crypto_config; 5, identifier:plaintext_item; 6, identifier:encrypted_item; 7, comment; 8, block; 8, 9; 8, 11; 8, 48; 9, expression_statement; 9, 10; 10, comment; 11, for_statement; 11, 12; 11, 15; 11, 20; 12, pattern_list; 12, 13; 12, 14; 13, identifier:name; 14, identifier:value; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:plaintext_item; 18, identifier:items; 19, argument_list; 20, block; 20, 21; 20, 36; 21, if_statement; 21, 22; 21, 34; 22, comparison_operator:==; 22, 23; 22, 31; 23, call; 23, 24; 23, 29; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:crypto_config; 27, identifier:attribute_actions; 28, identifier:action; 29, argument_list; 29, 30; 30, identifier:name; 31, attribute; 31, 32; 31, 33; 32, identifier:CryptoAction; 33, identifier:ENCRYPT_AND_SIGN; 34, block; 34, 35; 35, continue_statement; 36, if_statement; 36, 37; 36, 45; 37, comparison_operator:!=; 37, 38; 37, 44; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:encrypted_item; 41, identifier:get; 42, argument_list; 42, 43; 43, identifier:name; 44, identifier:value; 45, block; 45, 46; 46, return_statement; 46, 47; 47, False; 48, return_statement; 48, 49; 49, True
def _item_attributes_match(crypto_config, plaintext_item, encrypted_item): # type: (CryptoConfig, Dict, Dict) -> Bool """Determines whether the unencrypted values in the plaintext items attributes are the same as those in the encrypted item. Essentially this uses brute force to cover when we don't know the primary and sort index attribute names, since they can't be encrypted. :param CryptoConfig crypto_config: CryptoConfig used in encrypting the given items :param dict plaintext_item: The plaintext item :param dict encrypted_item: The encrypted item :return: Bool response, True if the unencrypted attributes in the plaintext item match those in the encrypted item :rtype: bool """ for name, value in plaintext_item.items(): if crypto_config.attribute_actions.action(name) == CryptoAction.ENCRYPT_AND_SIGN: continue if encrypted_item.get(name) != value: return False return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:load_css; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 18; 5, 27; 5, 38; 5, 47; 5, 186; 5, 192; 5, 193; 5, 237; 5, 259; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:icons; 11, call; 11, 12; 11, 13; 12, identifier:dict; 13, argument_list; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:common_prefix; 17, None; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:parser; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:tinycss; 24, identifier:make_parser; 25, argument_list; 25, 26; 26, string:'page3'; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:stylesheet; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:parser; 33, identifier:parse_stylesheet_file; 34, argument_list; 34, 35; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:css_file; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:is_icon; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:re; 44, identifier:compile; 45, argument_list; 45, 46; 46, string:"\.(.*):before,?"; 47, for_statement; 47, 48; 47, 49; 47, 52; 48, identifier:rule; 49, attribute; 49, 50; 49, 51; 50, identifier:stylesheet; 51, identifier:rules; 52, block; 52, 53; 52, 63; 52, 64; 52, 74; 52, 75; 52, 107; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:selector; 56, call; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:rule; 60, identifier:selector; 61, identifier:as_css; 62, argument_list; 63, comment; 64, if_statement; 64, 65; 64, 72; 65, not_operator; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:is_icon; 69, identifier:match; 70, argument_list; 70, 71; 71, identifier:selector; 72, block; 72, 73; 73, continue_statement; 74, comment; 75, if_statement; 75, 76; 75, 79; 75, 88; 76, comparison_operator:is; 76, 77; 76, 78; 77, identifier:common_prefix; 78, None; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:common_prefix; 83, subscript; 83, 84; 83, 85; 84, identifier:selector; 85, slice; 85, 86; 85, 87; 86, integer:1; 87, colon; 88, else_clause; 88, 89; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:common_prefix; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:os; 97, identifier:path; 98, identifier:commonprefix; 99, argument_list; 99, 100; 100, tuple; 100, 101; 100, 102; 101, identifier:common_prefix; 102, subscript; 102, 103; 102, 104; 103, identifier:selector; 104, slice; 104, 105; 104, 106; 105, integer:1; 106, colon; 107, for_statement; 107, 108; 107, 109; 107, 115; 108, identifier:match; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:is_icon; 112, identifier:finditer; 113, argument_list; 113, 114; 114, identifier:selector; 115, block; 115, 116; 115, 126; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:name; 119, subscript; 119, 120; 119, 125; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:match; 123, identifier:groups; 124, argument_list; 125, integer:0; 126, for_statement; 126, 127; 126, 128; 126, 131; 127, identifier:declaration; 128, attribute; 128, 129; 128, 130; 129, identifier:rule; 130, identifier:declarations; 131, block; 131, 132; 132, if_statement; 132, 133; 132, 138; 133, comparison_operator:==; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:declaration; 136, identifier:name; 137, string:"content"; 138, block; 138, 139; 138, 149; 138, 150; 138, 169; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:val; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:declaration; 146, identifier:value; 147, identifier:as_css; 148, argument_list; 149, comment; 150, if_statement; 150, 151; 150, 158; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:re; 154, identifier:match; 155, argument_list; 155, 156; 155, 157; 156, string:"^['\"].*['\"]$"; 157, identifier:val; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:val; 162, subscript; 162, 163; 162, 164; 163, identifier:val; 164, slice; 164, 165; 164, 166; 164, 167; 165, integer:1; 166, colon; 167, unary_operator:-; 167, 168; 168, integer:1; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 174; 171, subscript; 171, 172; 171, 173; 172, identifier:icons; 173, identifier:name; 174, call; 174, 175; 174, 176; 175, identifier:unichr; 176, argument_list; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:int; 179, argument_list; 179, 180; 179, 185; 180, subscript; 180, 181; 180, 182; 181, identifier:val; 182, slice; 182, 183; 182, 184; 183, integer:1; 184, colon; 185, integer:16; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:common_prefix; 189, boolean_operator:or; 189, 190; 189, 191; 190, identifier:common_prefix; 191, string:''; 192, comment; 193, if_statement; 193, 194; 193, 205; 194, boolean_operator:and; 194, 195; 194, 199; 195, not_operator; 195, 196; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:keep_prefix; 199, comparison_operator:>; 199, 200; 199, 204; 200, call; 200, 201; 200, 202; 201, identifier:len; 202, argument_list; 202, 203; 203, identifier:common_prefix; 204, integer:0; 205, block; 205, 206; 205, 210; 205, 233; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:non_prefixed_icons; 209, dictionary; 210, for_statement; 210, 211; 210, 212; 210, 217; 211, identifier:name; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:icons; 215, identifier:keys; 216, argument_list; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 230; 220, subscript; 220, 221; 220, 222; 221, identifier:non_prefixed_icons; 222, subscript; 222, 223; 222, 224; 223, identifier:name; 224, slice; 224, 225; 224, 229; 225, call; 225, 226; 225, 227; 226, identifier:len; 227, argument_list; 227, 228; 228, identifier:common_prefix; 229, colon; 230, subscript; 230, 231; 230, 232; 231, identifier:icons; 232, identifier:name; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:icons; 236, identifier:non_prefixed_icons; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:sorted_icons; 240, call; 240, 241; 240, 242; 241, identifier:OrderedDict; 242, argument_list; 242, 243; 243, call; 243, 244; 243, 245; 244, identifier:sorted; 245, argument_list; 245, 246; 245, 251; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:icons; 249, identifier:items; 250, argument_list; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:key; 253, lambda; 253, 254; 253, 256; 254, lambda_parameters; 254, 255; 255, identifier:t; 256, subscript; 256, 257; 256, 258; 257, identifier:t; 258, integer:0; 259, return_statement; 259, 260; 260, expression_list; 260, 261; 260, 262; 261, identifier:sorted_icons; 262, identifier:common_prefix
def load_css(self): """ Creates a dict of all icons available in CSS file, and finds out what's their common prefix. :returns sorted icons dict, common icon prefix """ icons = dict() common_prefix = None parser = tinycss.make_parser('page3') stylesheet = parser.parse_stylesheet_file(self.css_file) is_icon = re.compile("\.(.*):before,?") for rule in stylesheet.rules: selector = rule.selector.as_css() # Skip CSS classes that are not icons if not is_icon.match(selector): continue # Find out what the common prefix is if common_prefix is None: common_prefix = selector[1:] else: common_prefix = os.path.commonprefix((common_prefix, selector[1:])) for match in is_icon.finditer(selector): name = match.groups()[0] for declaration in rule.declarations: if declaration.name == "content": val = declaration.value.as_css() # Strip quotation marks if re.match("^['\"].*['\"]$", val): val = val[1:-1] icons[name] = unichr(int(val[1:], 16)) common_prefix = common_prefix or '' # Remove common prefix if not self.keep_prefix and len(common_prefix) > 0: non_prefixed_icons = {} for name in icons.keys(): non_prefixed_icons[name[len(common_prefix):]] = icons[name] icons = non_prefixed_icons sorted_icons = OrderedDict(sorted(icons.items(), key=lambda t: t[0])) return sorted_icons, common_prefix
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:rules; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 256; 5, 257; 5, 330; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:list_of_rules; 11, list:[]; 12, for_statement; 12, 13; 12, 14; 12, 17; 13, identifier:main_row; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:dict_rules; 17, block; 17, 18; 18, if_statement; 18, 19; 18, 22; 18, 232; 19, comparison_operator:in; 19, 20; 19, 21; 20, string:'rules'; 21, identifier:main_row; 22, block; 22, 23; 23, for_statement; 23, 24; 23, 25; 23, 28; 24, identifier:rule_row; 25, subscript; 25, 26; 25, 27; 26, identifier:main_row; 27, string:'rules'; 28, block; 28, 29; 29, if_statement; 29, 30; 29, 33; 29, 188; 30, comparison_operator:in; 30, 31; 30, 32; 31, string:'grants'; 32, identifier:rule_row; 33, block; 33, 34; 34, for_statement; 34, 35; 34, 36; 34, 39; 35, identifier:grant_row; 36, subscript; 36, 37; 36, 38; 37, identifier:rule_row; 38, string:'grants'; 39, block; 39, 40; 40, if_statement; 40, 41; 40, 44; 40, 45; 40, 123; 40, 180; 41, comparison_operator:in; 41, 42; 41, 43; 42, string:'group_id'; 43, identifier:grant_row; 44, comment; 45, block; 45, 46; 45, 52; 45, 53; 45, 70; 45, 116; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:group_id; 49, subscript; 49, 50; 49, 51; 50, identifier:grant_row; 51, string:'group_id'; 52, comment; 53, if_statement; 53, 54; 53, 57; 53, 64; 54, comparison_operator:in; 54, 55; 54, 56; 55, string:'name'; 56, identifier:grant_row; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:row_name; 61, subscript; 61, 62; 61, 63; 62, identifier:grant_row; 63, string:'name'; 64, else_clause; 64, 65; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:row_name; 69, None; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:fr; 73, call; 73, 74; 73, 75; 74, identifier:FirewallRule; 75, argument_list; 75, 76; 75, 79; 75, 82; 75, 85; 75, 90; 75, 95; 75, 100; 75, 105; 75, 108; 75, 111; 76, subscript; 76, 77; 76, 78; 77, identifier:main_row; 78, string:'id'; 79, subscript; 79, 80; 79, 81; 80, identifier:main_row; 81, string:'name'; 82, subscript; 82, 83; 82, 84; 83, identifier:main_row; 84, string:'description'; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:rules_direction; 87, subscript; 87, 88; 87, 89; 88, identifier:rule_row; 89, string:'direction'; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:rules_ip_protocol; 92, subscript; 92, 93; 92, 94; 93, identifier:rule_row; 94, string:'ip_protocol'; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:rules_from_port; 97, subscript; 97, 98; 97, 99; 98, identifier:rule_row; 99, string:'from_port'; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:rules_to_port; 102, subscript; 102, 103; 102, 104; 103, identifier:rule_row; 104, string:'to_port'; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:rules_grants_group_id; 107, identifier:group_id; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:rules_grants_name; 110, identifier:row_name; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:rules_description; 113, subscript; 113, 114; 113, 115; 114, identifier:grant_row; 115, string:'description'; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:list_of_rules; 120, identifier:append; 121, argument_list; 121, 122; 122, identifier:fr; 123, elif_clause; 123, 124; 123, 127; 124, comparison_operator:in; 124, 125; 124, 126; 125, string:'cidr_ip'; 126, identifier:grant_row; 127, block; 127, 128; 127, 173; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:fr; 131, call; 131, 132; 131, 133; 132, identifier:FirewallRule; 133, argument_list; 133, 134; 133, 137; 133, 140; 133, 143; 133, 148; 133, 153; 133, 158; 133, 163; 133, 168; 134, subscript; 134, 135; 134, 136; 135, identifier:main_row; 136, string:'id'; 137, subscript; 137, 138; 137, 139; 138, identifier:main_row; 139, string:'name'; 140, subscript; 140, 141; 140, 142; 141, identifier:main_row; 142, string:'description'; 143, keyword_argument; 143, 144; 143, 145; 144, identifier:rules_direction; 145, subscript; 145, 146; 145, 147; 146, identifier:rule_row; 147, string:'direction'; 148, keyword_argument; 148, 149; 148, 150; 149, identifier:rules_ip_protocol; 150, subscript; 150, 151; 150, 152; 151, identifier:rule_row; 152, string:'ip_protocol'; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:rules_from_port; 155, subscript; 155, 156; 155, 157; 156, identifier:rule_row; 157, string:'from_port'; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:rules_to_port; 160, subscript; 160, 161; 160, 162; 161, identifier:rule_row; 162, string:'to_port'; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:rules_grants_cidr_ip; 165, subscript; 165, 166; 165, 167; 166, identifier:grant_row; 167, string:'cidr_ip'; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:rules_description; 170, subscript; 170, 171; 170, 172; 171, identifier:grant_row; 172, string:'description'; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:list_of_rules; 177, identifier:append; 178, argument_list; 178, 179; 179, identifier:fr; 180, else_clause; 180, 181; 181, block; 181, 182; 182, raise_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:ValueError; 185, argument_list; 185, 186; 185, 187; 186, string:"Unsupported grant:"; 187, identifier:grant_row; 188, else_clause; 188, 189; 189, block; 189, 190; 189, 225; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:fr; 193, call; 193, 194; 193, 195; 194, identifier:FirewallRule; 195, argument_list; 195, 196; 195, 199; 195, 202; 195, 205; 195, 210; 195, 215; 195, 220; 196, subscript; 196, 197; 196, 198; 197, identifier:main_row; 198, string:'id'; 199, subscript; 199, 200; 199, 201; 200, identifier:main_row; 201, string:'name'; 202, subscript; 202, 203; 202, 204; 203, identifier:main_row; 204, string:'description'; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:rules_direction; 207, subscript; 207, 208; 207, 209; 208, identifier:rule_row; 209, string:'direction'; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:rules_ip_protocol; 212, subscript; 212, 213; 212, 214; 213, identifier:rule_row; 214, string:'ip_protocol'; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:rules_from_port; 217, subscript; 217, 218; 217, 219; 218, identifier:rule_row; 219, string:'from_port'; 220, keyword_argument; 220, 221; 220, 222; 221, identifier:rules_to_port; 222, subscript; 222, 223; 222, 224; 223, identifier:rule_row; 224, string:'to_port'; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:list_of_rules; 229, identifier:append; 230, argument_list; 230, 231; 231, identifier:fr; 232, else_clause; 232, 233; 233, block; 233, 234; 233, 249; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:fr; 237, call; 237, 238; 237, 239; 238, identifier:FirewallRule; 239, argument_list; 239, 240; 239, 243; 239, 246; 240, subscript; 240, 241; 240, 242; 241, identifier:main_row; 242, string:'id'; 243, subscript; 243, 244; 243, 245; 244, identifier:main_row; 245, string:'name'; 246, subscript; 246, 247; 246, 248; 247, identifier:main_row; 248, string:'description'; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:list_of_rules; 253, identifier:append; 254, argument_list; 254, 255; 255, identifier:fr; 256, comment; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:sorted_list; 260, call; 260, 261; 260, 262; 261, identifier:sorted; 262, argument_list; 262, 263; 262, 264; 263, identifier:list_of_rules; 264, keyword_argument; 264, 265; 264, 266; 265, identifier:key; 266, lambda; 266, 267; 266, 269; 267, lambda_parameters; 267, 268; 268, identifier:fr; 269, tuple; 269, 270; 269, 276; 269, 282; 269, 288; 269, 294; 269, 300; 269, 306; 269, 312; 269, 318; 269, 324; 270, call; 270, 271; 270, 272; 271, identifier:str; 272, argument_list; 272, 273; 273, attribute; 273, 274; 273, 275; 274, identifier:fr; 275, identifier:id; 276, call; 276, 277; 276, 278; 277, identifier:str; 278, argument_list; 278, 279; 279, attribute; 279, 280; 279, 281; 280, identifier:fr; 281, identifier:name; 282, call; 282, 283; 282, 284; 283, identifier:str; 284, argument_list; 284, 285; 285, attribute; 285, 286; 285, 287; 286, identifier:fr; 287, identifier:description; 288, call; 288, 289; 288, 290; 289, identifier:str; 290, argument_list; 290, 291; 291, attribute; 291, 292; 291, 293; 292, identifier:fr; 293, identifier:rules_direction; 294, call; 294, 295; 294, 296; 295, identifier:str; 296, argument_list; 296, 297; 297, attribute; 297, 298; 297, 299; 298, identifier:fr; 299, identifier:rules_ip_protocol; 300, call; 300, 301; 300, 302; 301, identifier:str; 302, argument_list; 302, 303; 303, attribute; 303, 304; 303, 305; 304, identifier:fr; 305, identifier:rules_from_port; 306, call; 306, 307; 306, 308; 307, identifier:str; 308, argument_list; 308, 309; 309, attribute; 309, 310; 309, 311; 310, identifier:fr; 311, identifier:rules_to_port; 312, call; 312, 313; 312, 314; 313, identifier:str; 314, argument_list; 314, 315; 315, attribute; 315, 316; 315, 317; 316, identifier:fr; 317, identifier:rules_grants_group_id; 318, call; 318, 319; 318, 320; 319, identifier:str; 320, argument_list; 320, 321; 321, attribute; 321, 322; 321, 323; 322, identifier:fr; 323, identifier:rules_grants_name; 324, call; 324, 325; 324, 326; 325, identifier:str; 326, argument_list; 326, 327; 327, attribute; 327, 328; 327, 329; 328, identifier:fr; 329, identifier:rules_grants_cidr_ip; 330, return_statement; 330, 331; 331, identifier:sorted_list
def rules(self): """ Returns a sorted list of firewall rules. Returns: list """ list_of_rules = [] for main_row in self.dict_rules: if 'rules' in main_row: for rule_row in main_row['rules']: if 'grants' in rule_row: for grant_row in rule_row['grants']: if 'group_id' in grant_row: # Set a var to not go over 80 chars group_id = grant_row['group_id'] # Some VPC grants don't specify a name if 'name' in grant_row: row_name = grant_row['name'] else: row_name = None fr = FirewallRule( main_row['id'], main_row['name'], main_row['description'], rules_direction=rule_row['direction'], rules_ip_protocol=rule_row['ip_protocol'], rules_from_port=rule_row['from_port'], rules_to_port=rule_row['to_port'], rules_grants_group_id=group_id, rules_grants_name=row_name, rules_description=grant_row['description']) list_of_rules.append(fr) elif 'cidr_ip' in grant_row: fr = FirewallRule( main_row['id'], main_row['name'], main_row['description'], rules_direction=rule_row['direction'], rules_ip_protocol=rule_row['ip_protocol'], rules_from_port=rule_row['from_port'], rules_to_port=rule_row['to_port'], rules_grants_cidr_ip=grant_row['cidr_ip'], rules_description=grant_row['description']) list_of_rules.append(fr) else: raise ValueError("Unsupported grant:", grant_row) else: fr = FirewallRule( main_row['id'], main_row['name'], main_row['description'], rules_direction=rule_row['direction'], rules_ip_protocol=rule_row['ip_protocol'], rules_from_port=rule_row['from_port'], rules_to_port=rule_row['to_port']) list_of_rules.append(fr) else: fr = FirewallRule(main_row['id'], main_row['name'], main_row['description']) list_of_rules.append(fr) # Sort the data in order to get a consistent output sorted_list = sorted(list_of_rules, key=lambda fr: (str(fr.id), str(fr.name), str(fr.description), str(fr.rules_direction), str(fr.rules_ip_protocol), str(fr.rules_from_port), str(fr.rules_to_port), str(fr.rules_grants_group_id), str(fr.rules_grants_name), str(fr.rules_grants_cidr_ip))) return sorted_list
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_select_mgmt_networks; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:conf; 6, block; 6, 7; 6, 9; 6, 15; 6, 41; 6, 86; 6, 111; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:nets; 12, subscript; 12, 13; 12, 14; 13, identifier:conf; 14, string:'nets'; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:mgmts; 18, call; 18, 19; 18, 20; 19, identifier:sorted; 20, argument_list; 20, 21; 21, list_comprehension; 21, 22; 21, 23; 21, 32; 22, identifier:name; 23, for_in_clause; 23, 24; 23, 27; 24, pattern_list; 24, 25; 24, 26; 25, identifier:name; 26, identifier:net; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:nets; 30, identifier:iteritems; 31, argument_list; 32, if_clause; 32, 33; 33, comparison_operator:is; 33, 34; 33, 40; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:net; 37, identifier:get; 38, argument_list; 38, 39; 39, string:'management'; 40, True; 41, if_statement; 41, 42; 41, 48; 42, comparison_operator:==; 42, 43; 42, 47; 43, call; 43, 44; 43, 45; 44, identifier:len; 45, argument_list; 45, 46; 46, identifier:mgmts; 47, integer:0; 48, block; 48, 49; 48, 63; 48, 71; 48, 79; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:mgmt_name; 52, subscript; 52, 53; 52, 62; 53, call; 53, 54; 53, 55; 54, identifier:sorted; 55, argument_list; 55, 56; 56, parenthesized_expression; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:nets; 60, identifier:keys; 61, argument_list; 62, integer:0; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:LOGGER; 67, identifier:debug; 68, argument_list; 68, 69; 68, 70; 69, string:'No management network configured, selecting network %s'; 70, identifier:mgmt_name; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 78; 73, subscript; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:nets; 76, identifier:mgmt_name; 77, string:'management'; 78, True; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:mgmts; 83, identifier:append; 84, argument_list; 84, 85; 85, identifier:mgmt_name; 86, for_statement; 86, 87; 86, 88; 86, 89; 87, identifier:mgmt_name; 88, identifier:mgmts; 89, block; 89, 90; 90, if_statement; 90, 91; 90, 102; 91, comparison_operator:is; 91, 92; 91, 101; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:nets; 96, identifier:mgmt_name; 97, identifier:get; 98, argument_list; 98, 99; 98, 100; 99, string:'dns_domain_name'; 100, None; 101, None; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 110; 105, subscript; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:nets; 108, identifier:mgmt_name; 109, string:'dns_domain_name'; 110, string:'lago.local'; 111, return_statement; 111, 112; 112, identifier:mgmts
def _select_mgmt_networks(self, conf): """ Select management networks. If no management network is found, it will mark the first network found by sorted the network lists. Also adding default DNS domain, if none is set. Args: conf(spec): spec """ nets = conf['nets'] mgmts = sorted( [ name for name, net in nets.iteritems() if net.get('management') is True ] ) if len(mgmts) == 0: mgmt_name = sorted((nets.keys()))[0] LOGGER.debug( 'No management network configured, selecting network %s', mgmt_name ) nets[mgmt_name]['management'] = True mgmts.append(mgmt_name) for mgmt_name in mgmts: if nets[mgmt_name].get('dns_domain_name', None) is None: nets[mgmt_name]['dns_domain_name'] = 'lago.local' return mgmts
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:validate_wavetable; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 15; 5, 48; 5, 49; 5, 58; 5, 112; 5, 113; 5, 128; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:wave; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_wavetable; 15, if_statement; 15, 16; 15, 24; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:N; 19, identifier:any; 20, argument_list; 20, 21; 21, comparison_operator:<=; 21, 22; 21, 23; 22, identifier:wave; 23, integer:0; 24, block; 24, 25; 24, 38; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:wrong; 28, subscript; 28, 29; 28, 37; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:N; 32, identifier:where; 33, argument_list; 33, 34; 34, comparison_operator:<=; 34, 35; 34, 36; 35, identifier:wave; 36, integer:0; 37, integer:0; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:exceptions; 42, identifier:ZeroWavelength; 43, argument_list; 43, 44; 43, 45; 44, string:'Negative or Zero wavelength occurs in wavelength array'; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:rows; 47, identifier:wrong; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:sorted; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:N; 55, identifier:sort; 56, argument_list; 56, 57; 57, identifier:wave; 58, if_statement; 58, 59; 58, 68; 59, not_operator; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:N; 63, identifier:alltrue; 64, argument_list; 64, 65; 65, comparison_operator:==; 65, 66; 65, 67; 66, identifier:sorted; 67, identifier:wave; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 84; 69, 85; 69, 87; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:N; 73, identifier:alltrue; 74, argument_list; 74, 75; 75, comparison_operator:==; 75, 76; 75, 83; 76, subscript; 76, 77; 76, 78; 77, identifier:sorted; 78, slice; 78, 79; 78, 80; 78, 81; 79, colon; 80, colon; 81, unary_operator:-; 81, 82; 82, integer:1; 83, identifier:wave; 84, comment; 85, block; 85, 86; 86, pass_statement; 87, else_clause; 87, 88; 88, block; 88, 89; 88, 102; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:wrong; 92, subscript; 92, 93; 92, 101; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:N; 96, identifier:where; 97, argument_list; 97, 98; 98, comparison_operator:!=; 98, 99; 98, 100; 99, identifier:sorted; 100, identifier:wave; 101, integer:0; 102, raise_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:exceptions; 106, identifier:UnsortedWavelength; 107, argument_list; 107, 108; 107, 109; 108, string:'Wavelength array is not monotonic'; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:rows; 111, identifier:wrong; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:dw; 116, binary_operator:-; 116, 117; 116, 122; 117, subscript; 117, 118; 117, 119; 118, identifier:sorted; 119, slice; 119, 120; 119, 121; 120, integer:1; 121, colon; 122, subscript; 122, 123; 122, 124; 123, identifier:sorted; 124, slice; 124, 125; 124, 126; 125, colon; 126, unary_operator:-; 126, 127; 127, integer:1; 128, if_statement; 128, 129; 128, 137; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:N; 132, identifier:any; 133, argument_list; 133, 134; 134, comparison_operator:==; 134, 135; 134, 136; 135, identifier:dw; 136, integer:0; 137, block; 137, 138; 137, 151; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:wrong; 141, subscript; 141, 142; 141, 150; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:N; 145, identifier:where; 146, argument_list; 146, 147; 147, comparison_operator:==; 147, 148; 147, 149; 148, identifier:dw; 149, integer:0; 150, integer:0; 151, raise_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:exceptions; 155, identifier:DuplicateWavelength; 156, argument_list; 156, 157; 156, 158; 157, string:"Wavelength array contains duplicate entries"; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:rows; 160, identifier:wrong
def validate_wavetable(self): """Enforce monotonic, ascending wavelength array with no zero or negative values. Raises ------ pysynphot.exceptions.DuplicateWavelength Wavelength array contains duplicate entries. pysynphot.exceptions.UnsortedWavelength Wavelength array is not monotonic ascending or descending. pysynphot.exceptions.ZeroWavelength Wavelength array has zero or negative value(s). """ # First check for invalid values wave = self._wavetable if N.any(wave <= 0): wrong = N.where(wave <= 0)[0] raise exceptions.ZeroWavelength( 'Negative or Zero wavelength occurs in wavelength array', rows=wrong) # Now check for monotonicity & enforce ascending sorted = N.sort(wave) if not N.alltrue(sorted == wave): if N.alltrue(sorted[::-1] == wave): # monotonic descending is allowed pass else: wrong = N.where(sorted != wave)[0] raise exceptions.UnsortedWavelength( 'Wavelength array is not monotonic', rows=wrong) # Check for duplicate values dw = sorted[1:] - sorted[:-1] if N.any(dw == 0): wrong = N.where(dw == 0)[0] raise exceptions.DuplicateWavelength( "Wavelength array contains duplicate entries", rows=wrong)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:getStateIndex; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:state; 6, block; 6, 7; 6, 9; 6, 18; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:statecodes; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:getStateCode; 16, argument_list; 16, 17; 17, identifier:state; 18, return_statement; 18, 19; 19, call; 19, 20; 19, 31; 20, attribute; 20, 21; 20, 30; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:np; 24, identifier:searchsorted; 25, argument_list; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:codes; 29, identifier:statecodes; 30, identifier:astype; 31, argument_list; 31, 32; 32, identifier:int
def getStateIndex(self,state): """ Returns the index of a state by calculating the state code and searching for this code a sorted list. Can be called on multiple states at once. """ statecodes = self.getStateCode(state) return np.searchsorted(self.codes,statecodes).astype(int)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:guess_chimera_path; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:search_all; 6, False; 7, block; 7, 8; 7, 10; 7, 22; 7, 58; 7, 74; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:paths; 13, call; 13, 14; 13, 15; 14, identifier:_search_chimera; 15, argument_list; 15, 16; 15, 17; 15, 18; 15, 19; 16, identifier:CHIMERA_BINARY; 17, identifier:CHIMERA_LOCATIONS; 18, identifier:CHIMERA_PREFIX; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:search_all; 21, identifier:search_all; 22, if_statement; 22, 23; 22, 27; 22, 28; 23, boolean_operator:and; 23, 24; 23, 26; 24, not_operator; 24, 25; 25, identifier:paths; 26, identifier:search_all; 27, comment; 28, block; 28, 29; 28, 46; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:headless; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, string:'{0[0]}{1}{0[1]}'; 35, identifier:format; 36, argument_list; 36, 37; 36, 45; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:os; 41, identifier:path; 42, identifier:split; 43, argument_list; 43, 44; 44, identifier:CHIMERA_BINARY; 45, string:'-headless'; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:paths; 49, call; 49, 50; 49, 51; 50, identifier:_search_chimera; 51, argument_list; 51, 52; 51, 53; 51, 54; 51, 55; 52, identifier:headless; 53, identifier:CHIMERA_LOCATIONS; 54, identifier:CHIMERA_PREFIX; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:search_all; 57, identifier:search_all; 58, if_statement; 58, 59; 58, 61; 59, not_operator; 59, 60; 60, identifier:paths; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:sys; 66, identifier:exit; 67, argument_list; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, string:"Could not find UCSF Chimera.\n{}"; 71, identifier:format; 72, argument_list; 72, 73; 73, identifier:_INSTRUCTIONS; 74, return_statement; 74, 75; 75, identifier:paths
def guess_chimera_path(search_all=False): """ Try to guess Chimera installation path. Parameters ---------- search_all : bool, optional, default=False If no CHIMERADIR env var is set, collect all posible locations of Chimera installations. Returns ------- paths: list of str Alphabetically sorted list of possible Chimera locations """ paths = _search_chimera(CHIMERA_BINARY, CHIMERA_LOCATIONS, CHIMERA_PREFIX, search_all=search_all) if not paths and search_all: # try headless? headless = '{0[0]}{1}{0[1]}'.format(os.path.split(CHIMERA_BINARY), '-headless') paths = _search_chimera(headless, CHIMERA_LOCATIONS, CHIMERA_PREFIX, search_all=search_all) if not paths: sys.exit("Could not find UCSF Chimera.\n{}".format(_INSTRUCTIONS)) return paths
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_topo_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:forward; 7, True; 8, block; 8, 9; 8, 11; 8, 15; 8, 21; 8, 25; 8, 26; 8, 67; 8, 100; 8, 157; 8, 183; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:topo_list; 14, list:[]; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:queue; 18, call; 18, 19; 18, 20; 19, identifier:deque; 20, argument_list; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:indeg; 24, dictionary; 25, comment; 26, if_statement; 26, 27; 26, 28; 26, 47; 27, identifier:forward; 28, block; 28, 29; 28, 35; 28, 41; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:get_edges; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:out_edges; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:get_degree; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:inc_degree; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:get_next; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:tail; 47, else_clause; 47, 48; 48, block; 48, 49; 48, 55; 48, 61; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:get_edges; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:inc_edges; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:get_degree; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:out_degree; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:get_next; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:head; 67, for_statement; 67, 68; 67, 69; 67, 74; 68, identifier:node; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:node_list; 73, argument_list; 74, block; 74, 75; 74, 82; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:degree; 78, call; 78, 79; 78, 80; 79, identifier:get_degree; 80, argument_list; 80, 81; 81, identifier:node; 82, if_statement; 82, 83; 82, 84; 82, 91; 83, identifier:degree; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:indeg; 89, identifier:node; 90, identifier:degree; 91, else_clause; 91, 92; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:queue; 97, identifier:append; 98, argument_list; 98, 99; 99, identifier:node; 100, while_statement; 100, 101; 100, 102; 101, identifier:queue; 102, block; 102, 103; 102, 111; 102, 118; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:curr_node; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:queue; 109, identifier:popleft; 110, argument_list; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:topo_list; 115, identifier:append; 116, argument_list; 116, 117; 117, identifier:curr_node; 118, for_statement; 118, 119; 118, 120; 118, 124; 119, identifier:edge; 120, call; 120, 121; 120, 122; 121, identifier:get_edges; 122, argument_list; 122, 123; 123, identifier:curr_node; 124, block; 124, 125; 124, 132; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:tail_id; 128, call; 128, 129; 128, 130; 129, identifier:get_next; 130, argument_list; 130, 131; 131, identifier:edge; 132, if_statement; 132, 133; 132, 136; 133, comparison_operator:in; 133, 134; 133, 135; 134, identifier:tail_id; 135, identifier:indeg; 136, block; 136, 137; 136, 143; 137, expression_statement; 137, 138; 138, augmented_assignment:-=; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:indeg; 141, identifier:tail_id; 142, integer:1; 143, if_statement; 143, 144; 143, 149; 144, comparison_operator:==; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:indeg; 147, identifier:tail_id; 148, integer:0; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:queue; 154, identifier:append; 155, argument_list; 155, 156; 156, identifier:tail_id; 157, if_statement; 157, 158; 157, 171; 157, 176; 158, comparison_operator:==; 158, 159; 158, 163; 159, call; 159, 160; 159, 161; 160, identifier:len; 161, argument_list; 161, 162; 162, identifier:topo_list; 163, call; 163, 164; 163, 165; 164, identifier:len; 165, argument_list; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:node_list; 170, argument_list; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:valid; 175, True; 176, else_clause; 176, 177; 176, 178; 177, comment; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:valid; 182, False; 183, return_statement; 183, 184; 184, tuple; 184, 185; 184, 186; 185, identifier:valid; 186, identifier:topo_list
def _topo_sort(self, forward=True): """ Topological sort. Returns a list of nodes where the successors (based on outgoing and incoming edges selected by the forward parameter) of any given node appear in the sequence after that node. """ topo_list = [] queue = deque() indeg = {} # select the operation that will be performed if forward: get_edges = self.out_edges get_degree = self.inc_degree get_next = self.tail else: get_edges = self.inc_edges get_degree = self.out_degree get_next = self.head for node in self.node_list(): degree = get_degree(node) if degree: indeg[node] = degree else: queue.append(node) while queue: curr_node = queue.popleft() topo_list.append(curr_node) for edge in get_edges(curr_node): tail_id = get_next(edge) if tail_id in indeg: indeg[tail_id] -= 1 if indeg[tail_id] == 0: queue.append(tail_id) if len(topo_list) == len(self.node_list()): valid = True else: # the graph has cycles, invalid topological sort valid = False return (valid, topo_list)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 34; 2, function_name:execute_sql; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 3, 28; 3, 31; 4, identifier:server_context; 5, identifier:schema_name; 6, identifier:sql; 7, default_parameter; 7, 8; 7, 9; 8, identifier:container_path; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:max_rows; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:sort; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:offset; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:container_filter; 21, None; 22, default_parameter; 22, 23; 22, 24; 23, identifier:save_in_session; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:parameters; 27, None; 28, default_parameter; 28, 29; 28, 30; 29, identifier:required_version; 30, None; 31, default_parameter; 31, 32; 31, 33; 32, identifier:timeout; 33, identifier:_default_timeout; 34, block; 34, 35; 34, 37; 34, 50; 34, 60; 34, 71; 34, 82; 34, 93; 34, 104; 34, 115; 34, 138; 34, 149; 35, expression_statement; 35, 36; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:url; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:server_context; 43, identifier:build_url; 44, argument_list; 44, 45; 44, 46; 44, 47; 45, string:'query'; 46, string:'executeSql.api'; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:container_path; 49, identifier:container_path; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:payload; 53, dictionary; 53, 54; 53, 57; 54, pair; 54, 55; 54, 56; 55, string:'schemaName'; 56, identifier:schema_name; 57, pair; 57, 58; 57, 59; 58, string:'sql'; 59, identifier:sql; 60, if_statement; 60, 61; 60, 64; 61, comparison_operator:is; 61, 62; 61, 63; 62, identifier:container_filter; 63, None; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:payload; 69, string:'containerFilter'; 70, identifier:container_filter; 71, if_statement; 71, 72; 71, 75; 72, comparison_operator:is; 72, 73; 72, 74; 73, identifier:max_rows; 74, None; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:payload; 80, string:'maxRows'; 81, identifier:max_rows; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:offset; 85, None; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:payload; 91, string:'offset'; 92, identifier:offset; 93, if_statement; 93, 94; 93, 97; 94, comparison_operator:is; 94, 95; 94, 96; 95, identifier:sort; 96, None; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:payload; 102, string:'query.sort'; 103, identifier:sort; 104, if_statement; 104, 105; 104, 108; 105, comparison_operator:is; 105, 106; 105, 107; 106, identifier:save_in_session; 107, None; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:payload; 113, string:'saveInSession'; 114, identifier:save_in_session; 115, if_statement; 115, 116; 115, 119; 116, comparison_operator:is; 116, 117; 116, 118; 117, identifier:parameters; 118, None; 119, block; 119, 120; 120, for_statement; 120, 121; 120, 124; 120, 129; 121, pattern_list; 121, 122; 121, 123; 122, identifier:key; 123, identifier:value; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:parameters; 127, identifier:items; 128, argument_list; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 137; 132, subscript; 132, 133; 132, 134; 133, identifier:payload; 134, binary_operator:+; 134, 135; 134, 136; 135, string:'query.param.'; 136, identifier:key; 137, identifier:value; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:required_version; 141, None; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:payload; 147, string:'apiVersion'; 148, identifier:required_version; 149, return_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:server_context; 153, identifier:make_request; 154, argument_list; 154, 155; 154, 156; 154, 157; 155, identifier:url; 156, identifier:payload; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:timeout; 159, identifier:timeout
def execute_sql(server_context, schema_name, sql, container_path=None, max_rows=None, sort=None, offset=None, container_filter=None, save_in_session=None, parameters=None, required_version=None, timeout=_default_timeout): """ Execute sql query against a LabKey server. :param server_context: A LabKey server context. See utils.create_server_context. :param schema_name: schema of table :param sql: String of labkey sql to execute :param container_path: labkey container path if not already set in context :param max_rows: max number of rows to return :param sort: comma separated list of column names to sort by :param offset: number of rows to offset results by :param container_filter: enumeration of the various container filters available. See: https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.Query.html#.containerFilter :param save_in_session: save query result as a named view to the session :param parameters: parameter values to pass through to a parameterized query :param required_version: Api version of response :param timeout: timeout of request in seconds (defaults to 30s) :return: """ url = server_context.build_url('query', 'executeSql.api', container_path=container_path) payload = { 'schemaName': schema_name, 'sql': sql } if container_filter is not None: payload['containerFilter'] = container_filter if max_rows is not None: payload['maxRows'] = max_rows if offset is not None: payload['offset'] = offset if sort is not None: payload['query.sort'] = sort if save_in_session is not None: payload['saveInSession'] = save_in_session if parameters is not None: for key, value in parameters.items(): payload['query.param.' + key] = value if required_version is not None: payload['apiVersion'] = required_version return server_context.make_request(url, payload, timeout=timeout)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:find_missing_projections; 3, parameters; 3, 4; 3, 5; 4, identifier:label_list; 5, identifier:projections; 6, block; 6, 7; 6, 9; 6, 15; 6, 22; 6, 60; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:unmapped_combinations; 12, call; 12, 13; 12, 14; 13, identifier:set; 14, argument_list; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:in; 16, 17; 16, 18; 17, identifier:WILDCARD_COMBINATION; 18, identifier:projections; 19, block; 19, 20; 20, return_statement; 20, 21; 21, list:[]; 22, for_statement; 22, 23; 22, 24; 22, 29; 23, identifier:labeled_segment; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:label_list; 27, identifier:ranges; 28, argument_list; 29, block; 29, 30; 29, 48; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:combination; 33, call; 33, 34; 33, 35; 34, identifier:tuple; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:sorted; 38, argument_list; 38, 39; 39, list_comprehension; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:label; 42, identifier:value; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:label; 45, subscript; 45, 46; 45, 47; 46, identifier:labeled_segment; 47, integer:2; 48, if_statement; 48, 49; 48, 52; 49, comparison_operator:not; 49, 50; 49, 51; 50, identifier:combination; 51, identifier:projections; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:unmapped_combinations; 57, identifier:add; 58, argument_list; 58, 59; 59, identifier:combination; 60, return_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:sorted; 63, argument_list; 63, 64; 64, identifier:unmapped_combinations
def find_missing_projections(label_list, projections): """ Finds all combinations of labels in `label_list` that are not covered by an entry in the dictionary of `projections`. Returns a list containing tuples of uncovered label combinations or en empty list if there are none. All uncovered label combinations are naturally sorted. Each entry in the dictionary of projections represents a single projection that maps a combination of labels (key) to a single new label (value). The combination of labels to be mapped is a tuple of naturally sorted labels that apply to one or more segments simultaneously. By defining a special wildcard projection using `('**',)` is is not required to specify a projection for every single combination of labels. Args: label_list (audiomate.annotations.LabelList): The label list to relabel projections (dict): A dictionary that maps tuples of label combinations to string labels. Returns: List: List of combinations of labels that are not covered by any projection Example: >>> ll = annotations.LabelList(labels=[ ... annotations.Label('b', 3.2, 4.5), ... annotations.Label('a', 4.0, 4.9), ... annotations.Label('c', 4.2, 5.1) ... ]) >>> find_missing_projections(ll, {('b',): 'new_label'}) [('a', 'b'), ('a', 'b', 'c'), ('a', 'c'), ('c',)] """ unmapped_combinations = set() if WILDCARD_COMBINATION in projections: return [] for labeled_segment in label_list.ranges(): combination = tuple(sorted([label.value for label in labeled_segment[2]])) if combination not in projections: unmapped_combinations.add(combination) return sorted(unmapped_combinations)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:load_projections; 3, parameters; 3, 4; 4, identifier:projections_file; 5, block; 5, 6; 5, 8; 5, 12; 5, 63; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:projections; 11, dictionary; 12, for_statement; 12, 13; 12, 14; 12, 21; 13, identifier:parts; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:textfile; 17, identifier:read_separated_lines_generator; 18, argument_list; 18, 19; 18, 20; 19, identifier:projections_file; 20, string:'|'; 21, block; 21, 22; 21, 47; 21, 57; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:combination; 25, call; 25, 26; 25, 27; 26, identifier:tuple; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:sorted; 30, argument_list; 30, 31; 31, list_comprehension; 31, 32; 31, 37; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:label; 35, identifier:strip; 36, argument_list; 37, for_in_clause; 37, 38; 37, 39; 38, identifier:label; 39, call; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:parts; 43, integer:0; 44, identifier:split; 45, argument_list; 45, 46; 46, string:' '; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:new_label; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:parts; 54, integer:1; 55, identifier:strip; 56, argument_list; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:projections; 61, identifier:combination; 62, identifier:new_label; 63, return_statement; 63, 64; 64, identifier:projections
def load_projections(projections_file): """ Loads projections defined in the given `projections_file`. The `projections_file` is expected to be in the following format:: old_label_1 | new_label_1 old_label_1 old_label_2 | new_label_2 old_label_3 | You can define one projection per line. Each projection starts with a list of one or multiple old labels (separated by a single whitespace) that are separated from the new label by a pipe (`|`). In the code above, the segment labeled with `old_label_1` will be labeled with `new_label_1` after applying the projection. Segments that are labeled with `old_label_1` **and** `old_label_2` concurrently are relabeled to `new_label_2`. All segments labeled with `old_label_3` are dropped. Combinations of multiple labels are automatically sorted in natural order. Args: projections_file (str): Path to the file with projections Returns: dict: Dictionary where the keys are tuples of labels to project to the key's value Example: >>> load_projections('/path/to/projections.txt') {('b',): 'foo', ('a', 'b'): 'a_b', ('a',): 'bar'} """ projections = {} for parts in textfile.read_separated_lines_generator(projections_file, '|'): combination = tuple(sorted([label.strip() for label in parts[0].split(' ')])) new_label = parts[1].strip() projections[combination] = new_label return projections
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:label_values; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 21; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:all_labels; 11, call; 11, 12; 11, 13; 12, identifier:set; 13, argument_list; 13, 14; 14, list_comprehension; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:l; 17, identifier:value; 18, for_in_clause; 18, 19; 18, 20; 19, identifier:l; 20, identifier:self; 21, return_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:sorted; 24, argument_list; 24, 25; 25, identifier:all_labels
def label_values(self): """ Return a list of all occuring label values. Returns: list: Lexicographically sorted list (str) of label values. Example: >>> ll = LabelList(labels=[ >>> Label('a', 3.2, 4.5), >>> Label('b', 5.1, 8.9), >>> Label('c', 7.2, 10.5), >>> Label('d', 10.5, 14), >>> Label('d', 15, 18) >>> ]) >>> ll.label_values() ['a', 'b', 'c', 'd'] """ all_labels = set([l.value for l in self]) return sorted(all_labels)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_utt_regions; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 16; 5, 132; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:regions; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:current_offset; 15, integer:0; 16, for_statement; 16, 17; 16, 18; 16, 24; 17, identifier:utt_idx; 18, call; 18, 19; 18, 20; 19, identifier:sorted; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:utt_ids; 24, block; 24, 25; 24, 29; 24, 33; 24, 37; 24, 74; 24, 95; 24, 113; 24, 120; 24, 127; 24, 128; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:offset; 28, identifier:current_offset; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:num_frames; 32, list:[]; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:refs; 36, list:[]; 37, for_statement; 37, 38; 37, 39; 37, 42; 38, identifier:cnt; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:containers; 42, block; 42, 43; 42, 59; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:num_frames; 47, identifier:append; 48, argument_list; 48, 49; 49, subscript; 49, 50; 49, 58; 50, attribute; 50, 51; 50, 57; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:cnt; 54, identifier:get; 55, argument_list; 55, 56; 56, identifier:utt_idx; 57, identifier:shape; 58, integer:0; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:refs; 63, identifier:append; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:cnt; 68, identifier:get; 69, argument_list; 69, 70; 69, 71; 70, identifier:utt_idx; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:mem_map; 73, True; 74, if_statement; 74, 75; 74, 84; 75, comparison_operator:!=; 75, 76; 75, 83; 76, call; 76, 77; 76, 78; 77, identifier:len; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:set; 81, argument_list; 81, 82; 82, identifier:num_frames; 83, integer:1; 84, block; 84, 85; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:ValueError; 88, argument_list; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, string:'Utterance {} has not the same number of frames in all containers!'; 92, identifier:format; 93, argument_list; 93, 94; 94, identifier:utt_idx; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:num_chunks; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:math; 101, identifier:ceil; 102, argument_list; 102, 103; 103, binary_operator:/; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:num_frames; 106, integer:0; 107, call; 107, 108; 107, 109; 108, identifier:float; 109, argument_list; 109, 110; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:frames_per_chunk; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:region; 116, tuple; 116, 117; 116, 118; 116, 119; 117, identifier:offset; 118, identifier:num_chunks; 119, identifier:refs; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:regions; 124, identifier:append; 125, argument_list; 125, 126; 126, identifier:region; 127, comment; 128, expression_statement; 128, 129; 129, augmented_assignment:+=; 129, 130; 129, 131; 130, identifier:current_offset; 131, identifier:num_chunks; 132, return_statement; 132, 133; 133, identifier:regions
def get_utt_regions(self): """ Return the regions of all utterances, assuming all utterances are concatenated. It is assumed that the utterances are sorted in ascending order for concatenation. A region is defined by offset (in chunks), length (num-chunks) and a list of references to the utterance datasets in the containers. Returns: list: List of with a tuple for every utterances containing the region info. """ regions = [] current_offset = 0 for utt_idx in sorted(self.utt_ids): offset = current_offset num_frames = [] refs = [] for cnt in self.containers: num_frames.append(cnt.get(utt_idx).shape[0]) refs.append(cnt.get(utt_idx, mem_map=True)) if len(set(num_frames)) != 1: raise ValueError('Utterance {} has not the same number of frames in all containers!'.format(utt_idx)) num_chunks = math.ceil(num_frames[0] / float(self.frames_per_chunk)) region = (offset, num_chunks, refs) regions.append(region) # Sets the offset for the next utterances current_offset += num_chunks return regions
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:write_separated_lines; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:path; 5, identifier:values; 6, default_parameter; 6, 7; 6, 8; 7, identifier:separator; 8, string:' '; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort_by_column; 11, integer:0; 12, block; 12, 13; 12, 15; 12, 26; 12, 185; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:f; 18, call; 18, 19; 18, 20; 19, identifier:open; 20, argument_list; 20, 21; 20, 22; 20, 23; 21, identifier:path; 22, string:'w'; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:encoding; 25, string:'utf-8'; 26, if_statement; 26, 27; 26, 33; 26, 116; 27, comparison_operator:is; 27, 28; 27, 32; 28, call; 28, 29; 28, 30; 29, identifier:type; 30, argument_list; 30, 31; 31, identifier:values; 32, identifier:dict; 33, block; 33, 34; 33, 70; 34, if_statement; 34, 35; 34, 40; 34, 60; 35, comparison_operator:in; 35, 36; 35, 37; 36, identifier:sort_by_column; 37, list:[0, 1]; 37, 38; 37, 39; 38, integer:0; 39, integer:1; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:items; 44, call; 44, 45; 44, 46; 45, identifier:sorted; 46, argument_list; 46, 47; 46, 52; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:values; 50, identifier:items; 51, argument_list; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:key; 54, lambda; 54, 55; 54, 57; 55, lambda_parameters; 55, 56; 56, identifier:t; 57, subscript; 57, 58; 57, 59; 58, identifier:t; 59, identifier:sort_by_column; 60, else_clause; 60, 61; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:items; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:values; 68, identifier:items; 69, argument_list; 70, for_statement; 70, 71; 70, 74; 70, 75; 71, pattern_list; 71, 72; 71, 73; 72, identifier:key; 73, identifier:value; 74, identifier:items; 75, block; 75, 76; 75, 102; 76, if_statement; 76, 77; 76, 85; 77, comparison_operator:in; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:type; 80, argument_list; 80, 81; 81, identifier:value; 82, list:[list, set]; 82, 83; 82, 84; 83, identifier:list; 84, identifier:set; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:value; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:separator; 92, identifier:join; 93, argument_list; 93, 94; 94, list_comprehension; 94, 95; 94, 99; 95, call; 95, 96; 95, 97; 96, identifier:str; 97, argument_list; 97, 98; 98, identifier:x; 99, for_in_clause; 99, 100; 99, 101; 100, identifier:x; 101, identifier:value; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:f; 106, identifier:write; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, string:'{}{}{}\n'; 111, identifier:format; 112, argument_list; 112, 113; 112, 114; 112, 115; 113, identifier:key; 114, identifier:separator; 115, identifier:value; 116, elif_clause; 116, 117; 116, 130; 117, boolean_operator:or; 117, 118; 117, 124; 118, comparison_operator:is; 118, 119; 118, 123; 119, call; 119, 120; 119, 121; 120, identifier:type; 121, argument_list; 121, 122; 122, identifier:values; 123, identifier:list; 124, comparison_operator:is; 124, 125; 124, 129; 125, call; 125, 126; 125, 127; 126, identifier:type; 127, argument_list; 127, 128; 128, identifier:values; 129, identifier:set; 130, block; 130, 131; 130, 153; 131, if_statement; 131, 132; 131, 139; 131, 147; 132, comparison_operator:<=; 132, 133; 132, 134; 132, 135; 133, integer:0; 134, identifier:sort_by_column; 135, call; 135, 136; 135, 137; 136, identifier:len; 137, argument_list; 137, 138; 138, identifier:values; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:items; 143, call; 143, 144; 143, 145; 144, identifier:sorted; 145, argument_list; 145, 146; 146, identifier:values; 147, else_clause; 147, 148; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:items; 152, identifier:values; 153, for_statement; 153, 154; 153, 155; 153, 156; 154, identifier:record; 155, identifier:items; 156, block; 156, 157; 156, 168; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:str_values; 160, list_comprehension; 160, 161; 160, 165; 161, call; 161, 162; 161, 163; 162, identifier:str; 163, argument_list; 163, 164; 164, identifier:value; 165, for_in_clause; 165, 166; 165, 167; 166, identifier:value; 167, identifier:record; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:f; 172, identifier:write; 173, argument_list; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, string:'{}\n'; 177, identifier:format; 178, argument_list; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:separator; 182, identifier:join; 183, argument_list; 183, 184; 184, identifier:str_values; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:f; 189, identifier:close; 190, argument_list
def write_separated_lines(path, values, separator=' ', sort_by_column=0): """ Writes list or dict to file line by line. Dict can have list as value then they written separated on the line. Parameters: path (str): Path to write file to. values (dict, list): A dictionary or a list to write to the file. separator (str): Separator to use between columns. sort_by_column (int): if >= 0, sorts the list by the given index, if its 0 or 1 and its a dictionary it sorts it by either the key (0) or value (1). By default 0, meaning sorted by the first column or the key. """ f = open(path, 'w', encoding='utf-8') if type(values) is dict: if sort_by_column in [0, 1]: items = sorted(values.items(), key=lambda t: t[sort_by_column]) else: items = values.items() for key, value in items: if type(value) in [list, set]: value = separator.join([str(x) for x in value]) f.write('{}{}{}\n'.format(key, separator, value)) elif type(values) is list or type(values) is set: if 0 <= sort_by_column < len(values): items = sorted(values) else: items = values for record in items: str_values = [str(value) for value in record] f.write('{}\n'.format(separator.join(str_values))) f.close()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_set; 3, parameters; 3, 4; 4, identifier:s; 5, block; 5, 6; 5, 8; 5, 21; 5, 28; 5, 45; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 15; 9, not_operator; 9, 10; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 14; 13, identifier:s; 14, identifier:Set; 15, block; 15, 16; 16, raise_statement; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:TypeError; 19, argument_list; 19, 20; 20, string:"sets only"; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:s; 24, call; 24, 25; 24, 26; 25, identifier:frozenset; 26, argument_list; 26, 27; 27, identifier:s; 28, if_statement; 28, 29; 28, 32; 29, comparison_operator:not; 29, 30; 29, 31; 30, identifier:s; 31, identifier:_sort_set_memo; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, subscript; 35, 36; 35, 37; 36, identifier:_sort_set_memo; 37, identifier:s; 38, call; 38, 39; 38, 40; 39, identifier:sorted; 40, argument_list; 40, 41; 40, 42; 41, identifier:s; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:key; 44, identifier:_sort_set_key; 45, return_statement; 45, 46; 46, subscript; 46, 47; 46, 48; 47, identifier:_sort_set_memo; 48, identifier:s
def sort_set(s): """Return a sorted list of the contents of a set This is intended to be used to iterate over world state, where you just need keys to be in some deterministic order, but the sort order should be obvious from the key. Non-strings come before strings and then tuples. Tuples compare element-wise as normal. But ultimately all comparisons are between values' ``repr``. This is memoized. """ if not isinstance(s, Set): raise TypeError("sets only") s = frozenset(s) if s not in _sort_set_memo: _sort_set_memo[s] = sorted(s, key=_sort_set_key) return _sort_set_memo[s]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_tags; 3, parameters; 3, 4; 4, identifier:tagstring; 5, block; 5, 6; 5, 8; 5, 14; 5, 21; 5, 25; 5, 29; 5, 30; 5, 31; 5, 35; 5, 42; 5, 169; 5, 191; 5, 201; 5, 207; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 11; 9, not_operator; 9, 10; 10, identifier:tagstring; 11, block; 11, 12; 12, return_statement; 12, 13; 13, list:[]; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:tagstring; 17, call; 17, 18; 17, 19; 18, identifier:force_text; 19, argument_list; 19, 20; 20, identifier:tagstring; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:words; 24, list:[]; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:buffer; 28, list:[]; 29, comment; 30, comment; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:to_be_split; 34, list:[]; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:i; 38, call; 38, 39; 38, 40; 39, identifier:iter; 40, argument_list; 40, 41; 41, identifier:tagstring; 42, try_statement; 42, 43; 42, 149; 43, block; 43, 44; 44, while_statement; 44, 45; 44, 46; 45, True; 46, block; 46, 47; 46, 56; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:c; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:six; 53, identifier:next; 54, argument_list; 54, 55; 55, identifier:i; 56, if_statement; 56, 57; 56, 60; 56, 140; 57, comparison_operator:==; 57, 58; 57, 59; 58, identifier:c; 59, string:'"'; 60, block; 60, 61; 60, 80; 60, 89; 60, 110; 61, if_statement; 61, 62; 61, 63; 62, identifier:buffer; 63, block; 63, 64; 63, 76; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:to_be_split; 68, identifier:append; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, string:''; 73, identifier:join; 74, argument_list; 74, 75; 75, identifier:buffer; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:buffer; 79, list:[]; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:c; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:six; 86, identifier:next; 87, argument_list; 87, 88; 88, identifier:i; 89, while_statement; 89, 90; 89, 93; 90, comparison_operator:!=; 90, 91; 90, 92; 91, identifier:c; 92, string:'"'; 93, block; 93, 94; 93, 101; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:buffer; 98, identifier:append; 99, argument_list; 99, 100; 100, identifier:c; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:c; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:six; 107, identifier:next; 108, argument_list; 108, 109; 109, identifier:i; 110, if_statement; 110, 111; 110, 112; 111, identifier:buffer; 112, block; 112, 113; 112, 126; 112, 136; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:word; 116, call; 116, 117; 116, 125; 117, attribute; 117, 118; 117, 124; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, string:''; 121, identifier:join; 122, argument_list; 122, 123; 123, identifier:buffer; 124, identifier:strip; 125, argument_list; 126, if_statement; 126, 127; 126, 128; 127, identifier:word; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:words; 133, identifier:append; 134, argument_list; 134, 135; 135, identifier:word; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:buffer; 139, list:[]; 140, else_clause; 140, 141; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:buffer; 146, identifier:append; 147, argument_list; 147, 148; 148, identifier:c; 149, except_clause; 149, 150; 149, 151; 149, 152; 149, 153; 150, identifier:StopIteration; 151, comment; 152, comment; 153, block; 153, 154; 154, if_statement; 154, 155; 154, 156; 155, identifier:buffer; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:to_be_split; 161, identifier:append; 162, argument_list; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, string:''; 166, identifier:join; 167, argument_list; 167, 168; 168, identifier:buffer; 169, if_statement; 169, 170; 169, 171; 170, identifier:to_be_split; 171, block; 171, 172; 172, for_statement; 172, 173; 172, 174; 172, 175; 173, identifier:chunk; 174, identifier:to_be_split; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:words; 180, identifier:extend; 181, argument_list; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:split_strip; 184, argument_list; 184, 185; 184, 186; 185, identifier:chunk; 186, subscript; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:settings; 189, identifier:TAGGIT_SELECTIZE; 190, string:'DELIMITER'; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:words; 194, call; 194, 195; 194, 196; 195, identifier:list; 196, argument_list; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:set; 199, argument_list; 199, 200; 200, identifier:words; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:words; 205, identifier:sort; 206, argument_list; 207, return_statement; 207, 208; 208, identifier:words
def parse_tags(tagstring): """ Parses tag input, with multiple word input being activated and delineated by commas and double quotes. Quotes take precedence, so they may contain commas. Returns a sorted list of unique tag names. Adapted from Taggit, modified to not split strings on spaces. Ported from Jonathan Buchanan's `django-tagging <http://django-tagging.googlecode.com/>`_ """ if not tagstring: return [] tagstring = force_text(tagstring) words = [] buffer = [] # Defer splitting of non-quoted sections until we know if there are # any unquoted commas. to_be_split = [] i = iter(tagstring) try: while True: c = six.next(i) if c == '"': if buffer: to_be_split.append(''.join(buffer)) buffer = [] c = six.next(i) while c != '"': buffer.append(c) c = six.next(i) if buffer: word = ''.join(buffer).strip() if word: words.append(word) buffer = [] else: buffer.append(c) except StopIteration: # If we were parsing an open quote which was never closed treat # the buffer as unquoted. if buffer: to_be_split.append(''.join(buffer)) if to_be_split: for chunk in to_be_split: words.extend(split_strip(chunk, settings.TAGGIT_SELECTIZE['DELIMITER'])) words = list(set(words)) words.sort() return words
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_sort_by_unique_fields; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:model; 5, identifier:model_objs; 6, identifier:unique_fields; 7, block; 7, 8; 7, 10; 7, 28; 7, 53; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:unique_fields; 13, list_comprehension; 13, 14; 13, 15; 13, 22; 14, identifier:field; 15, for_in_clause; 15, 16; 15, 17; 16, identifier:field; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:model; 20, identifier:_meta; 21, identifier:fields; 22, if_clause; 22, 23; 23, comparison_operator:in; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:field; 26, identifier:attname; 27, identifier:unique_fields; 28, function_definition; 28, 29; 28, 30; 28, 32; 29, function_name:sort_key; 30, parameters; 30, 31; 31, identifier:model_obj; 32, block; 32, 33; 33, return_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:tuple; 36, generator_expression; 36, 37; 36, 50; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:field; 40, identifier:get_db_prep_save; 41, argument_list; 41, 42; 41, 49; 42, call; 42, 43; 42, 44; 43, identifier:getattr; 44, argument_list; 44, 45; 44, 46; 45, identifier:model_obj; 46, attribute; 46, 47; 46, 48; 47, identifier:field; 48, identifier:attname; 49, identifier:connection; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:field; 52, identifier:unique_fields; 53, return_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:sorted; 56, argument_list; 56, 57; 56, 58; 57, identifier:model_objs; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:key; 60, identifier:sort_key
def _sort_by_unique_fields(model, model_objs, unique_fields): """ Sort a list of models by their unique fields. Sorting models in an upsert greatly reduces the chances of deadlock when doing concurrent upserts """ unique_fields = [ field for field in model._meta.fields if field.attname in unique_fields ] def sort_key(model_obj): return tuple( field.get_db_prep_save(getattr(model_obj, field.attname), connection) for field in unique_fields ) return sorted(model_objs, key=sort_key)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:find; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:datum; 6, block; 6, 7; 6, 9; 6, 24; 6, 80; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 21; 10, boolean_operator:and; 10, 11; 10, 18; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:datum; 16, identifier:value; 17, identifier:dict; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:expressions; 21, block; 21, 22; 22, return_statement; 22, 23; 23, identifier:datum; 24, if_statement; 24, 25; 24, 40; 25, boolean_operator:or; 25, 26; 25, 33; 26, call; 26, 27; 26, 28; 27, identifier:isinstance; 28, argument_list; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:datum; 31, identifier:value; 32, identifier:dict; 33, call; 33, 34; 33, 35; 34, identifier:isinstance; 35, argument_list; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:datum; 38, identifier:value; 39, identifier:list; 40, block; 40, 41; 40, 58; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:key; 44, parenthesized_expression; 44, 45; 45, conditional_expression:if; 45, 46; 45, 54; 45, 57; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:functools; 49, identifier:cmp_to_key; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:_compare; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:expressions; 57, None; 58, return_statement; 58, 59; 59, list:[jsonpath_rw.DatumInContext.wrap( [value for value in sorted(datum.value, key=key)])]; 59, 60; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:jsonpath_rw; 64, identifier:DatumInContext; 65, identifier:wrap; 66, argument_list; 66, 67; 67, list_comprehension; 67, 68; 67, 69; 68, identifier:value; 69, for_in_clause; 69, 70; 69, 71; 70, identifier:value; 71, call; 71, 72; 71, 73; 72, identifier:sorted; 73, argument_list; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:datum; 76, identifier:value; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:key; 79, identifier:key; 80, return_statement; 80, 81; 81, identifier:datum
def find(self, datum): """Return sorted value of This if list or dict.""" if isinstance(datum.value, dict) and self.expressions: return datum if isinstance(datum.value, dict) or isinstance(datum.value, list): key = (functools.cmp_to_key(self._compare) if self.expressions else None) return [jsonpath_rw.DatumInContext.wrap( [value for value in sorted(datum.value, key=key)])] return datum
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_subset_riverid_index_list; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:river_id_list; 6, block; 6, 7; 6, 9; 6, 13; 6, 17; 6, 21; 6, 71; 6, 80; 6, 89; 6, 98; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:netcdf_river_indices_list; 12, list:[]; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:valid_river_ids; 16, list:[]; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:missing_river_ids; 20, list:[]; 21, for_statement; 21, 22; 21, 23; 21, 24; 21, 25; 22, identifier:river_id; 23, identifier:river_id_list; 24, comment; 25, block; 25, 26; 26, try_statement; 26, 27; 26, 48; 27, block; 27, 28; 27, 41; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 34; 30, attribute; 30, 31; 30, 32; 30, 33; 31, identifier:netcdf_river_indices_list; 32, line_continuation:\; 33, identifier:append; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:get_river_index; 39, argument_list; 39, 40; 40, identifier:river_id; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:valid_river_ids; 45, identifier:append; 46, argument_list; 46, 47; 47, identifier:river_id; 48, except_clause; 48, 49; 48, 50; 49, identifier:IndexError; 50, block; 50, 51; 50, 64; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:log; 54, argument_list; 54, 55; 54, 63; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, concatenated_string; 57, 58; 57, 59; 58, string:"ReachID {0} not found in netCDF dataset."; 59, string:" Skipping ..."; 60, identifier:format; 61, argument_list; 61, 62; 62, identifier:river_id; 63, string:"WARNING"; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:missing_river_ids; 68, identifier:append; 69, argument_list; 69, 70; 70, identifier:river_id; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:np_valid_river_indices_list; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:np; 77, identifier:array; 78, argument_list; 78, 79; 79, identifier:netcdf_river_indices_list; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:np_valid_river_ids; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:np; 86, identifier:array; 87, argument_list; 87, 88; 88, identifier:valid_river_ids; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:sorted_indexes; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:np; 95, identifier:argsort; 96, argument_list; 96, 97; 97, identifier:np_valid_river_indices_list; 98, return_statement; 98, 99; 99, tuple; 99, 100; 99, 103; 99, 106; 100, subscript; 100, 101; 100, 102; 101, identifier:np_valid_river_indices_list; 102, identifier:sorted_indexes; 103, subscript; 103, 104; 103, 105; 104, identifier:np_valid_river_ids; 105, identifier:sorted_indexes; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:np; 109, identifier:array; 110, argument_list; 110, 111; 111, identifier:missing_river_ids
def get_subset_riverid_index_list(self, river_id_list): """ Gets the subset riverid_list from the netcdf file Optional returns include the list of valid river ids in the dataset as well as a list of missing rive rids Parameters ---------- river_id_list: list or :obj:`numpy.array` Array of river ID's for the river segments you want the index of. Returns ------- :obj:`numpy.array` A sorted array of the river index in the NetCDF file that were found. :obj:`numpy.array` A sorted array of the river IDs that were found. list An array of the missing river ids. """ netcdf_river_indices_list = [] valid_river_ids = [] missing_river_ids = [] for river_id in river_id_list: # get where streamids are in netcdf file try: netcdf_river_indices_list \ .append(self.get_river_index(river_id)) valid_river_ids.append(river_id) except IndexError: log("ReachID {0} not found in netCDF dataset." " Skipping ...".format(river_id), "WARNING") missing_river_ids.append(river_id) np_valid_river_indices_list = np.array(netcdf_river_indices_list) np_valid_river_ids = np.array(valid_river_ids) sorted_indexes = np.argsort(np_valid_river_indices_list) return(np_valid_river_indices_list[sorted_indexes], np_valid_river_ids[sorted_indexes], np.array(missing_river_ids))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:get_child_type_choices; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:request; 6, identifier:action; 7, block; 7, 8; 7, 10; 7, 11; 7, 26; 7, 27; 7, 37; 7, 41; 7, 45; 7, 122; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:choices; 14, call; 14, 15; 14, 23; 15, attribute; 15, 16; 15, 21; 15, 22; 16, call; 16, 17; 16, 18; 17, identifier:super; 18, argument_list; 18, 19; 18, 20; 19, identifier:ChildModelPluginPolymorphicParentModelAdmin; 20, identifier:self; 21, line_continuation:\; 22, identifier:get_child_type_choices; 23, argument_list; 23, 24; 23, 25; 24, identifier:request; 25, identifier:action; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:plugins; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:child_model_plugin_class; 35, identifier:get_plugins; 36, argument_list; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:labels; 40, dictionary; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:sort_priorities; 44, dictionary; 45, if_statement; 45, 46; 45, 47; 46, identifier:plugins; 47, block; 47, 48; 47, 84; 47, 98; 48, for_statement; 48, 49; 48, 50; 48, 51; 49, identifier:plugin; 50, identifier:plugins; 51, block; 51, 52; 51, 60; 51, 71; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:pk; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:plugin; 58, identifier:content_type; 59, identifier:pk; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:labels; 64, identifier:pk; 65, call; 65, 66; 65, 67; 66, identifier:capfirst; 67, argument_list; 67, 68; 68, attribute; 68, 69; 68, 70; 69, identifier:plugin; 70, identifier:verbose_name; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:sort_priorities; 75, identifier:pk; 76, call; 76, 77; 76, 78; 77, identifier:getattr; 78, argument_list; 78, 79; 78, 80; 78, 81; 79, identifier:plugin; 80, string:'sort_priority'; 81, subscript; 81, 82; 81, 83; 82, identifier:labels; 83, identifier:pk; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:choices; 87, list_comprehension; 87, 88; 87, 93; 88, tuple; 88, 89; 88, 90; 89, identifier:ctype; 90, subscript; 90, 91; 90, 92; 91, identifier:labels; 92, identifier:ctype; 93, for_in_clause; 93, 94; 93, 97; 94, pattern_list; 94, 95; 94, 96; 95, identifier:ctype; 96, identifier:_; 97, identifier:choices; 98, return_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:sorted; 101, argument_list; 101, 102; 101, 103; 102, identifier:choices; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:cmp; 105, lambda; 105, 106; 105, 109; 106, lambda_parameters; 106, 107; 106, 108; 107, identifier:a; 108, identifier:b; 109, call; 109, 110; 109, 111; 110, identifier:cmp; 111, argument_list; 111, 112; 111, 117; 112, subscript; 112, 113; 112, 114; 113, identifier:sort_priorities; 114, subscript; 114, 115; 114, 116; 115, identifier:a; 116, integer:0; 117, subscript; 117, 118; 117, 119; 118, identifier:sort_priorities; 119, subscript; 119, 120; 119, 121; 120, identifier:b; 121, integer:0; 122, return_statement; 122, 123; 123, identifier:choices
def get_child_type_choices(self, request, action): """ Override choice labels with ``verbose_name`` from plugins and sort. """ # Get choices from the super class to check permissions. choices = super(ChildModelPluginPolymorphicParentModelAdmin, self) \ .get_child_type_choices(request, action) # Update label with verbose name from plugins. plugins = self.child_model_plugin_class.get_plugins() labels = {} sort_priorities = {} if plugins: for plugin in plugins: pk = plugin.content_type.pk labels[pk] = capfirst(plugin.verbose_name) sort_priorities[pk] = getattr(plugin, 'sort_priority', labels[pk]) choices = [(ctype, labels[ctype]) for ctype, _ in choices] return sorted(choices, cmp=lambda a, b: cmp( sort_priorities[a[0]], sort_priorities[b[0]] ) ) return choices