sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:sort_cyclic_graph_best_effort; 3, parameters; 3, 4; 3, 5; 4, identifier:graph; 5, default_parameter; 5, 6; 5, 7; 6, identifier:pick_first; 7, string:'head'; 8, block; 8, 9; 8, 11; 8, 15; 8, 21; 8, 22; 8, 23; 8, 24; 8, 47; 8, 51; 8, 89; 8, 93; 8, 131; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:ordered; 14, list:[]; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:visited; 18, call; 18, 19; 18, 20; 19, identifier:set; 20, argument_list; 21, comment; 22, comment; 23, comment; 24, if_statement; 24, 25; 24, 28; 24, 37; 25, comparison_operator:==; 25, 26; 25, 27; 26, identifier:pick_first; 27, string:'head'; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, pattern_list; 31, 32; 31, 33; 32, identifier:fst_attr; 33, identifier:snd_attr; 34, tuple; 34, 35; 34, 36; 35, string:'head_node'; 36, string:'update_node'; 37, else_clause; 37, 38; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, pattern_list; 41, 42; 41, 43; 42, identifier:fst_attr; 43, identifier:snd_attr; 44, tuple; 44, 45; 44, 46; 45, string:'update_node'; 46, string:'head_node'; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:current; 50, identifier:FIRST; 51, while_statement; 51, 52; 51, 55; 52, comparison_operator:is; 52, 53; 52, 54; 53, identifier:current; 54, None; 55, block; 55, 56; 55, 63; 55, 73; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:visited; 60, identifier:add; 61, argument_list; 61, 62; 62, identifier:current; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:current; 66, call; 66, 67; 66, 68; 67, identifier:getattr; 68, argument_list; 68, 69; 68, 72; 69, subscript; 69, 70; 69, 71; 70, identifier:graph; 71, identifier:current; 72, identifier:fst_attr; 73, if_statement; 73, 74; 73, 81; 74, boolean_operator:and; 74, 75; 74, 78; 75, comparison_operator:not; 75, 76; 75, 77; 76, identifier:current; 77, identifier:visited; 78, comparison_operator:is; 78, 79; 78, 80; 79, identifier:current; 80, None; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:ordered; 86, identifier:append; 87, argument_list; 87, 88; 88, identifier:current; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:current; 92, identifier:FIRST; 93, while_statement; 93, 94; 93, 97; 94, comparison_operator:is; 94, 95; 94, 96; 95, identifier:current; 96, None; 97, block; 97, 98; 97, 105; 97, 115; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:visited; 102, identifier:add; 103, argument_list; 103, 104; 104, identifier:current; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:current; 108, call; 108, 109; 108, 110; 109, identifier:getattr; 110, argument_list; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:graph; 113, identifier:current; 114, identifier:snd_attr; 115, if_statement; 115, 116; 115, 123; 116, boolean_operator:and; 116, 117; 116, 120; 117, comparison_operator:not; 117, 118; 117, 119; 118, identifier:current; 119, identifier:visited; 120, comparison_operator:is; 120, 121; 120, 122; 121, identifier:current; 122, None; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:ordered; 128, identifier:append; 129, argument_list; 129, 130; 130, identifier:current; 131, return_statement; 131, 132; 132, identifier:ordered
def sort_cyclic_graph_best_effort(graph, pick_first='head'): """Fallback for cases in which the graph has cycles.""" ordered = [] visited = set() # Go first on the pick_first chain then go back again on the others # that were not visited. Given the way the graph is built both chains # will always contain all the elements. if pick_first == 'head': fst_attr, snd_attr = ('head_node', 'update_node') else: fst_attr, snd_attr = ('update_node', 'head_node') current = FIRST while current is not None: visited.add(current) current = getattr(graph[current], fst_attr) if current not in visited and current is not None: ordered.append(current) current = FIRST while current is not None: visited.add(current) current = getattr(graph[current], snd_attr) if current not in visited and current is not None: ordered.append(current) return ordered
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:circlescan; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:x0; 5, identifier:y0; 6, identifier:r1; 7, identifier:r2; 8, block; 8, 9; 8, 11; 8, 12; 8, 22; 8, 32; 8, 33; 8, 37; 8, 38; 8, 48; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:<; 13, 14; 13, 15; 14, identifier:r1; 15, integer:0; 16, block; 16, 17; 17, raise_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:ValueError; 20, argument_list; 20, 21; 21, string:"Initial radius must be non-negative"; 22, if_statement; 22, 23; 22, 26; 23, comparison_operator:<; 23, 24; 23, 25; 24, identifier:r2; 25, integer:0; 26, block; 26, 27; 27, raise_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:ValueError; 30, argument_list; 30, 31; 31, string:"Final radius must be non-negative"; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:previous; 36, list:[]; 37, comment; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:rstep; 41, conditional_expression:if; 41, 42; 41, 43; 41, 46; 42, integer:1; 43, comparison_operator:>=; 43, 44; 43, 45; 44, identifier:r2; 45, identifier:r1; 46, unary_operator:-; 46, 47; 47, integer:1; 48, for_statement; 48, 49; 48, 50; 48, 58; 49, identifier:distance; 50, call; 50, 51; 50, 52; 51, identifier:range; 52, argument_list; 52, 53; 52, 54; 52, 57; 53, identifier:r1; 54, binary_operator:+; 54, 55; 54, 56; 55, identifier:r2; 56, identifier:rstep; 57, identifier:rstep; 58, block; 58, 59; 59, if_statement; 59, 60; 59, 63; 59, 69; 60, comparison_operator:==; 60, 61; 60, 62; 61, identifier:distance; 62, integer:0; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, yield; 65, 66; 66, expression_list; 66, 67; 66, 68; 67, identifier:x0; 68, identifier:y0; 69, else_clause; 69, 70; 69, 71; 69, 72; 70, comment; 71, comment; 72, block; 72, 73; 72, 77; 72, 165; 72, 172; 72, 173; 72, 177; 72, 333; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:a; 76, float:0.707107; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:rotations; 80, dictionary; 80, 81; 80, 90; 80, 100; 80, 110; 80, 122; 80, 133; 80, 145; 80, 155; 81, pair; 81, 82; 81, 83; 82, integer:0; 83, list:[[ 1, 0], [ 0, 1]]; 83, 84; 83, 87; 84, list:[ 1, 0]; 84, 85; 84, 86; 85, integer:1; 86, integer:0; 87, list:[ 0, 1]; 87, 88; 87, 89; 88, integer:0; 89, integer:1; 90, pair; 90, 91; 90, 92; 91, integer:1; 92, list:[[ a, a], [-a, a]]; 92, 93; 92, 96; 93, list:[ a, a]; 93, 94; 93, 95; 94, identifier:a; 95, identifier:a; 96, list:[-a, a]; 96, 97; 96, 99; 97, unary_operator:-; 97, 98; 98, identifier:a; 99, identifier:a; 100, pair; 100, 101; 100, 102; 101, integer:2; 102, list:[[ 0, 1], [-1, 0]]; 102, 103; 102, 106; 103, list:[ 0, 1]; 103, 104; 103, 105; 104, integer:0; 105, integer:1; 106, list:[-1, 0]; 106, 107; 106, 109; 107, unary_operator:-; 107, 108; 108, integer:1; 109, integer:0; 110, pair; 110, 111; 110, 112; 111, integer:3; 112, list:[[-a, a], [-a,-a]]; 112, 113; 112, 117; 113, list:[-a, a]; 113, 114; 113, 116; 114, unary_operator:-; 114, 115; 115, identifier:a; 116, identifier:a; 117, list:[-a,-a]; 117, 118; 117, 120; 118, unary_operator:-; 118, 119; 119, identifier:a; 120, unary_operator:-; 120, 121; 121, identifier:a; 122, pair; 122, 123; 122, 124; 123, integer:4; 124, list:[[-1, 0], [ 0,-1]]; 124, 125; 124, 129; 125, list:[-1, 0]; 125, 126; 125, 128; 126, unary_operator:-; 126, 127; 127, integer:1; 128, integer:0; 129, list:[ 0,-1]; 129, 130; 129, 131; 130, integer:0; 131, unary_operator:-; 131, 132; 132, integer:1; 133, pair; 133, 134; 133, 135; 134, integer:5; 135, list:[[-a,-a], [ a,-a]]; 135, 136; 135, 141; 136, list:[-a,-a]; 136, 137; 136, 139; 137, unary_operator:-; 137, 138; 138, identifier:a; 139, unary_operator:-; 139, 140; 140, identifier:a; 141, list:[ a,-a]; 141, 142; 141, 143; 142, identifier:a; 143, unary_operator:-; 143, 144; 144, identifier:a; 145, pair; 145, 146; 145, 147; 146, integer:6; 147, list:[[ 0,-1], [ 1, 0]]; 147, 148; 147, 152; 148, list:[ 0,-1]; 148, 149; 148, 150; 149, integer:0; 150, unary_operator:-; 150, 151; 151, integer:1; 152, list:[ 1, 0]; 152, 153; 152, 154; 153, integer:1; 154, integer:0; 155, pair; 155, 156; 155, 157; 156, integer:7; 157, list:[[ a,-a], [ a, a]]; 157, 158; 157, 162; 158, list:[ a,-a]; 158, 159; 158, 160; 159, identifier:a; 160, unary_operator:-; 160, 161; 161, identifier:a; 162, list:[ a, a]; 162, 163; 162, 164; 163, identifier:a; 164, identifier:a; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:nangles; 168, call; 168, 169; 168, 170; 169, identifier:len; 170, argument_list; 170, 171; 171, identifier:rotations; 172, comment; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:current; 176, list:[]; 177, for_statement; 177, 178; 177, 179; 177, 183; 178, identifier:angle; 179, call; 179, 180; 179, 181; 180, identifier:range; 181, argument_list; 181, 182; 182, identifier:nangles; 183, block; 183, 184; 183, 188; 183, 192; 183, 198; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:x; 187, integer:0; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:y; 191, identifier:distance; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:d; 195, binary_operator:-; 195, 196; 195, 197; 196, integer:1; 197, identifier:distance; 198, while_statement; 198, 199; 198, 202; 199, comparison_operator:<; 199, 200; 199, 201; 200, identifier:x; 201, identifier:y; 202, block; 202, 203; 202, 225; 202, 247; 202, 253; 202, 259; 202, 260; 202, 261; 202, 262; 202, 280; 202, 297; 202, 298; 202, 329; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:xr; 206, binary_operator:+; 206, 207; 206, 216; 207, binary_operator:*; 207, 208; 207, 215; 208, subscript; 208, 209; 208, 214; 209, subscript; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:rotations; 212, identifier:angle; 213, integer:0; 214, integer:0; 215, identifier:x; 216, binary_operator:*; 216, 217; 216, 224; 217, subscript; 217, 218; 217, 223; 218, subscript; 218, 219; 218, 222; 219, subscript; 219, 220; 219, 221; 220, identifier:rotations; 221, identifier:angle; 222, integer:0; 223, integer:1; 224, identifier:y; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:yr; 228, binary_operator:+; 228, 229; 228, 238; 229, binary_operator:*; 229, 230; 229, 237; 230, subscript; 230, 231; 230, 236; 231, subscript; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:rotations; 234, identifier:angle; 235, integer:1; 236, integer:0; 237, identifier:x; 238, binary_operator:*; 238, 239; 238, 246; 239, subscript; 239, 240; 239, 245; 240, subscript; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:rotations; 243, identifier:angle; 244, integer:1; 245, integer:1; 246, identifier:y; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:xr; 250, binary_operator:+; 250, 251; 250, 252; 251, identifier:x0; 252, identifier:xr; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:yr; 256, binary_operator:+; 256, 257; 256, 258; 257, identifier:y0; 258, identifier:yr; 259, comment; 260, comment; 261, comment; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:point; 265, tuple; 265, 266; 265, 273; 266, call; 266, 267; 266, 268; 267, identifier:int; 268, argument_list; 268, 269; 269, call; 269, 270; 269, 271; 270, identifier:round; 271, argument_list; 271, 272; 272, identifier:xr; 273, call; 273, 274; 273, 275; 274, identifier:int; 275, argument_list; 275, 276; 276, call; 276, 277; 276, 278; 277, identifier:round; 278, argument_list; 278, 279; 279, identifier:yr; 280, if_statement; 280, 281; 280, 284; 281, comparison_operator:not; 281, 282; 281, 283; 282, identifier:point; 283, identifier:previous; 284, block; 284, 285; 284, 290; 285, expression_statement; 285, 286; 286, yield; 286, 287; 287, expression_list; 287, 288; 287, 289; 288, identifier:xr; 289, identifier:yr; 290, expression_statement; 290, 291; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:current; 294, identifier:append; 295, argument_list; 295, 296; 296, identifier:point; 297, comment; 298, if_statement; 298, 299; 298, 303; 298, 312; 299, parenthesized_expression; 299, 300; 300, comparison_operator:<; 300, 301; 300, 302; 301, identifier:d; 302, integer:0; 303, block; 303, 304; 304, expression_statement; 304, 305; 305, augmented_assignment:+=; 305, 306; 305, 307; 306, identifier:d; 307, binary_operator:+; 307, 308; 307, 309; 308, integer:3; 309, binary_operator:*; 309, 310; 309, 311; 310, integer:2; 311, identifier:x; 312, else_clause; 312, 313; 313, block; 313, 314; 313, 325; 314, expression_statement; 314, 315; 315, augmented_assignment:+=; 315, 316; 315, 317; 316, identifier:d; 317, binary_operator:-; 317, 318; 317, 319; 318, integer:5; 319, binary_operator:*; 319, 320; 319, 321; 320, integer:2; 321, parenthesized_expression; 321, 322; 322, binary_operator:-; 322, 323; 322, 324; 323, identifier:y; 324, identifier:x; 325, expression_statement; 325, 326; 326, augmented_assignment:-=; 326, 327; 326, 328; 327, identifier:y; 328, integer:1; 329, expression_statement; 329, 330; 330, augmented_assignment:+=; 330, 331; 330, 332; 331, identifier:x; 332, integer:1; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:previous; 336, identifier:current
def circlescan(x0, y0, r1, r2): """Scan pixels in a circle pattern around a center point :param x0: Center x-coordinate :type x0: float :param y0: Center y-coordinate :type y0: float :param r1: Initial radius :type r1: float :param r2: Final radius :type r2: float :returns: Coordinate generator :rtype: function """ # Validate inputs if r1 < 0: raise ValueError("Initial radius must be non-negative") if r2 < 0: raise ValueError("Final radius must be non-negative") # List of pixels visited in previous diameter previous = [] # Scan distances outward (1) or inward (-1) rstep = 1 if r2 >= r1 else -1 for distance in range(r1, r2 + rstep, rstep): if distance == 0: yield x0, y0 else: # Computes points for first octant and the rotate by multiples of # 45 degrees to compute the other octants a = 0.707107 rotations = {0: [[ 1, 0], [ 0, 1]], 1: [[ a, a], [-a, a]], 2: [[ 0, 1], [-1, 0]], 3: [[-a, a], [-a,-a]], 4: [[-1, 0], [ 0,-1]], 5: [[-a,-a], [ a,-a]], 6: [[ 0,-1], [ 1, 0]], 7: [[ a,-a], [ a, a]]} nangles = len(rotations) # List of pixels visited in current diameter current = [] for angle in range(nangles): x = 0 y = distance d = 1 - distance while x < y: xr = rotations[angle][0][0]*x + rotations[angle][0][1]*y yr = rotations[angle][1][0]*x + rotations[angle][1][1]*y xr = x0 + xr yr = y0 + yr # First check if point was in previous diameter # since our scan pattern can lead to duplicates in # neighboring diameters point = (int(round(xr)), int(round(yr))) if point not in previous: yield xr, yr current.append(point) # Move pixel according to circle constraint if (d < 0): d += 3 + 2 * x else: d += 5 - 2 * (y-x) y -= 1 x += 1 previous = current
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:ringscan; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:x0; 5, identifier:y0; 6, identifier:r1; 7, identifier:r2; 8, default_parameter; 8, 9; 8, 10; 9, identifier:metric; 10, identifier:chebyshev; 11, block; 11, 12; 11, 14; 11, 15; 11, 25; 11, 35; 11, 48; 11, 49; 11, 53; 11, 103; 11, 110; 11, 116; 11, 117; 11, 127; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:<; 16, 17; 16, 18; 17, identifier:r1; 18, integer:0; 19, block; 19, 20; 20, raise_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:ValueError; 23, argument_list; 23, 24; 24, string:"Initial radius must be non-negative"; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:<; 26, 27; 26, 28; 27, identifier:r2; 28, integer:0; 29, block; 29, 30; 30, raise_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:ValueError; 33, argument_list; 33, 34; 34, string:"Final radius must be non-negative"; 35, if_statement; 35, 36; 35, 42; 36, not_operator; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:hasattr; 39, argument_list; 39, 40; 39, 41; 40, identifier:metric; 41, string:"__call__"; 42, block; 42, 43; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:TypeError; 46, argument_list; 46, 47; 47, string:"Metric not callable"; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:direction; 52, integer:0; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:steps; 56, dictionary; 56, 57; 56, 62; 56, 68; 56, 74; 56, 81; 56, 87; 56, 93; 56, 98; 57, pair; 57, 58; 57, 59; 58, integer:0; 59, list:[ 1, 0]; 59, 60; 59, 61; 60, integer:1; 61, integer:0; 62, pair; 62, 63; 62, 64; 63, integer:1; 64, list:[ 1,-1]; 64, 65; 64, 66; 65, integer:1; 66, unary_operator:-; 66, 67; 67, integer:1; 68, pair; 68, 69; 68, 70; 69, integer:2; 70, list:[ 0,-1]; 70, 71; 70, 72; 71, integer:0; 72, unary_operator:-; 72, 73; 73, integer:1; 74, pair; 74, 75; 74, 76; 75, integer:3; 76, list:[-1,-1]; 76, 77; 76, 79; 77, unary_operator:-; 77, 78; 78, integer:1; 79, unary_operator:-; 79, 80; 80, integer:1; 81, pair; 81, 82; 81, 83; 82, integer:4; 83, list:[-1, 0]; 83, 84; 83, 86; 84, unary_operator:-; 84, 85; 85, integer:1; 86, integer:0; 87, pair; 87, 88; 87, 89; 88, integer:5; 89, list:[-1, 1]; 89, 90; 89, 92; 90, unary_operator:-; 90, 91; 91, integer:1; 92, integer:1; 93, pair; 93, 94; 93, 95; 94, integer:6; 95, list:[ 0, 1]; 95, 96; 95, 97; 96, integer:0; 97, integer:1; 98, pair; 98, 99; 98, 100; 99, integer:7; 100, list:[ 1, 1]; 100, 101; 100, 102; 101, integer:1; 102, integer:1; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:nsteps; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, identifier:steps; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:center; 113, list:[x0, y0]; 113, 114; 113, 115; 114, identifier:x0; 115, identifier:y0; 116, comment; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:rstep; 120, conditional_expression:if; 120, 121; 120, 122; 120, 125; 121, integer:1; 122, comparison_operator:>=; 122, 123; 122, 124; 123, identifier:r2; 124, identifier:r1; 125, unary_operator:-; 125, 126; 126, integer:1; 127, for_statement; 127, 128; 127, 129; 127, 137; 128, identifier:distance; 129, call; 129, 130; 129, 131; 130, identifier:range; 131, argument_list; 131, 132; 131, 133; 131, 136; 132, identifier:r1; 133, binary_operator:+; 133, 134; 133, 135; 134, identifier:r2; 135, identifier:rstep; 136, identifier:rstep; 137, block; 137, 138; 137, 146; 137, 150; 137, 151; 137, 155; 137, 249; 137, 250; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:initial; 141, list:[x0, y0 + distance]; 141, 142; 141, 143; 142, identifier:x0; 143, binary_operator:+; 143, 144; 143, 145; 144, identifier:y0; 145, identifier:distance; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:current; 149, identifier:initial; 150, comment; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:ntrys; 154, integer:0; 155, while_statement; 155, 156; 155, 157; 155, 158; 156, True; 157, comment; 158, block; 158, 159; 158, 174; 158, 175; 158, 194; 158, 225; 158, 229; 158, 238; 158, 239; 158, 243; 159, if_statement; 159, 160; 159, 163; 160, comparison_operator:==; 160, 161; 160, 162; 161, identifier:distance; 162, integer:0; 163, block; 163, 164; 163, 173; 164, expression_statement; 164, 165; 165, yield; 165, 166; 166, expression_list; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:current; 169, integer:0; 170, subscript; 170, 171; 170, 172; 171, identifier:current; 172, integer:1; 173, break_statement; 174, comment; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:nextpoint; 178, list_comprehension; 178, 179; 178, 188; 179, binary_operator:+; 179, 180; 179, 183; 180, subscript; 180, 181; 180, 182; 181, identifier:current; 182, identifier:i; 183, subscript; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:steps; 186, identifier:direction; 187, identifier:i; 188, for_in_clause; 188, 189; 188, 190; 189, identifier:i; 190, call; 190, 191; 190, 192; 191, identifier:range; 192, argument_list; 192, 193; 193, integer:2; 194, if_statement; 194, 195; 194, 202; 194, 203; 195, comparison_operator:!=; 195, 196; 195, 201; 196, call; 196, 197; 196, 198; 197, identifier:metric; 198, argument_list; 198, 199; 198, 200; 199, identifier:center; 200, identifier:nextpoint; 201, identifier:distance; 202, comment; 203, block; 203, 204; 203, 208; 203, 214; 203, 215; 203, 224; 204, expression_statement; 204, 205; 205, augmented_assignment:+=; 205, 206; 205, 207; 206, identifier:ntrys; 207, integer:1; 208, if_statement; 208, 209; 208, 212; 209, comparison_operator:==; 209, 210; 209, 211; 210, identifier:ntrys; 211, identifier:nsteps; 212, block; 212, 213; 213, break_statement; 214, comment; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:direction; 218, binary_operator:%; 218, 219; 218, 223; 219, parenthesized_expression; 219, 220; 220, binary_operator:+; 220, 221; 220, 222; 221, identifier:direction; 222, integer:1; 223, identifier:nsteps; 224, continue_statement; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:ntrys; 228, integer:0; 229, expression_statement; 229, 230; 230, yield; 230, 231; 231, expression_list; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:current; 234, integer:0; 235, subscript; 235, 236; 235, 237; 236, identifier:current; 237, integer:1; 238, comment; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:current; 242, identifier:nextpoint; 243, if_statement; 243, 244; 243, 247; 244, comparison_operator:==; 244, 245; 244, 246; 245, identifier:current; 246, identifier:initial; 247, block; 247, 248; 248, break_statement; 249, comment; 250, if_statement; 250, 251; 250, 254; 251, comparison_operator:==; 251, 252; 251, 253; 252, identifier:ntrys; 253, identifier:nsteps; 254, block; 254, 255; 255, break_statement
def ringscan(x0, y0, r1, r2, metric=chebyshev): """Scan pixels in a ring pattern around a center point clockwise :param x0: Center x-coordinate :type x0: int :param y0: Center y-coordinate :type y0: int :param r1: Initial radius :type r1: int :param r2: Final radius :type r2: int :param metric: Distance metric :type metric: function :returns: Coordinate generator :rtype: function """ # Validate inputs if r1 < 0: raise ValueError("Initial radius must be non-negative") if r2 < 0: raise ValueError("Final radius must be non-negative") if not hasattr(metric, "__call__"): raise TypeError("Metric not callable") # Define clockwise step directions direction = 0 steps = {0: [ 1, 0], 1: [ 1,-1], 2: [ 0,-1], 3: [-1,-1], 4: [-1, 0], 5: [-1, 1], 6: [ 0, 1], 7: [ 1, 1]} nsteps = len(steps) center = [x0, y0] # Scan distances outward (1) or inward (-1) rstep = 1 if r2 >= r1 else -1 for distance in range(r1, r2 + rstep, rstep): initial = [x0, y0 + distance] current = initial # Number of tries to find a valid neighrbor ntrys = 0 while True: # Short-circuit special case if distance == 0: yield current[0], current[1] break # Try and take a step and check if still within distance nextpoint = [current[i] + steps[direction][i] for i in range(2)] if metric(center, nextpoint) != distance: # Check if we tried all step directions and failed ntrys += 1 if ntrys == nsteps: break # Try the next direction direction = (direction + 1) % nsteps continue ntrys = 0 yield current[0], current[1] # Check if we have come all the way around current = nextpoint if current == initial: break # Check if we tried all step directions and failed if ntrys == nsteps: break
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:fetch; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:callback; 6, block; 6, 7; 6, 9; 6, 10; 6, 25; 6, 26; 6, 36; 6, 37; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:session; 13, call; 13, 14; 13, 21; 14, attribute; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:client; 19, identifier:sync; 20, identifier:start; 21, argument_list; 21, 22; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:device_uuid; 25, comment; 26, if_statement; 26, 27; 26, 34; 27, boolean_operator:or; 27, 28; 27, 31; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:session; 30, None; 31, comparison_operator:not; 31, 32; 31, 33; 32, string:'id'; 33, identifier:session; 34, block; 34, 35; 35, return_statement; 36, comment; 37, while_statement; 37, 38; 37, 39; 37, 40; 38, True; 39, comment; 40, block; 40, 41; 40, 59; 40, 60; 40, 65; 40, 66; 40, 70; 40, 98; 40, 99; 40, 100; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:queue_items; 44, call; 44, 45; 44, 52; 45, attribute; 45, 46; 45, 51; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:client; 50, identifier:sync; 51, identifier:fetch; 52, argument_list; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:device_uuid; 56, subscript; 56, 57; 56, 58; 57, identifier:session; 58, string:'id'; 59, comment; 60, if_statement; 60, 61; 60, 63; 61, not_operator; 61, 62; 62, identifier:queue_items; 63, block; 63, 64; 64, break_statement; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:ack_keys; 69, list:[]; 70, for_statement; 70, 71; 70, 72; 70, 73; 71, identifier:item; 72, identifier:queue_items; 73, block; 73, 74; 74, if_statement; 74, 75; 74, 84; 75, call; 75, 76; 75, 77; 76, identifier:callback; 77, argument_list; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:item; 80, string:'meta'; 81, subscript; 81, 82; 81, 83; 82, identifier:item; 83, string:'data'; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:ack_keys; 89, identifier:append; 90, argument_list; 90, 91; 91, subscript; 91, 92; 91, 97; 92, subscript; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:item; 95, string:'meta'; 96, string:'sync'; 97, string:'ack_key'; 98, comment; 99, comment; 100, if_statement; 100, 101; 100, 102; 101, identifier:ack_keys; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 112; 105, attribute; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:client; 110, identifier:sync; 111, identifier:ack; 112, argument_list; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:device_uuid; 116, identifier:ack_keys
def fetch(self, callback): """ Perform a full synchronization flow. .. code-block:: python :linenos: >>> client = basecrm.Client(access_token='<YOUR_PERSONAL_ACCESS_TOKEN>') >>> sync = basecrm.Sync(client=client, device_uuid='<YOUR_DEVICES_UUID>') >>> sync.fetch(lambda meta, data: basecrm.Sync.ACK) :param callback: Callback that will be called for every item in a queue. Takes two input arguments: synchronization meta data and assodicated data. It must return either ack or nack. """ # Set up a new synchronization session for a given device's UUID session = self.client.sync.start(self.device_uuid) # Check if there is anything to synchronize if session is None or 'id' not in session: return # Drain the main queue until there is no more data (empty array) while True: # Fetch the main queue queue_items = self.client.sync.fetch(self.device_uuid, session['id']) # nothing more to synchronize ? if not queue_items: break # let client know about both data and meta ack_keys = [] for item in queue_items: if callback(item['meta'], item['data']): ack_keys.append(item['meta']['sync']['ack_key']) # As we fetch new data, we need to send acknowledgement keys # if any .. if ack_keys: self.client.sync.ack(self.device_uuid, ack_keys)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:define_residues_for_plotting_traj; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:analysis_cutoff; 6, block; 6, 7; 6, 9; 6, 15; 6, 16; 6, 58; 6, 89; 6, 131; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:residue_counts_fraction; 14, dictionary; 15, comment; 16, for_statement; 16, 17; 16, 18; 16, 21; 17, identifier:traj; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:residue_counts; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 29; 24, subscript; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:residue_counts_fraction; 28, identifier:traj; 29, dictionary_comprehension; 29, 30; 29, 45; 30, pair; 30, 31; 30, 32; 31, identifier:residue; 32, binary_operator:/; 32, 33; 32, 37; 33, call; 33, 34; 33, 35; 34, identifier:float; 35, argument_list; 35, 36; 36, identifier:values; 37, call; 37, 38; 37, 39; 38, identifier:len; 39, argument_list; 39, 40; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:contacts_per_timeframe; 44, identifier:traj; 45, for_in_clause; 45, 46; 45, 49; 46, pattern_list; 46, 47; 46, 48; 47, identifier:residue; 48, identifier:values; 49, call; 49, 50; 49, 57; 50, attribute; 50, 51; 50, 56; 51, subscript; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:residue_counts; 55, identifier:traj; 56, identifier:items; 57, argument_list; 58, for_statement; 58, 59; 58, 60; 58, 63; 59, identifier:traj; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:residue_counts_fraction; 63, block; 63, 64; 64, for_statement; 64, 65; 64, 66; 64, 71; 65, identifier:residue; 66, subscript; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:residue_counts_fraction; 70, identifier:traj; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 81; 74, attribute; 74, 75; 74, 80; 75, subscript; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:frequency; 79, identifier:residue; 80, identifier:append; 81, argument_list; 81, 82; 82, subscript; 82, 83; 82, 88; 83, subscript; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:residue_counts_fraction; 87, identifier:traj; 88, identifier:residue; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:topology_data; 95, identifier:dict_of_plotted_res; 96, dictionary_comprehension; 96, 97; 96, 104; 96, 109; 97, pair; 97, 98; 97, 99; 98, identifier:i; 99, subscript; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:frequency; 103, identifier:i; 104, for_in_clause; 104, 105; 104, 106; 105, identifier:i; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:frequency; 109, if_clause; 109, 110; 110, comparison_operator:>; 110, 111; 110, 119; 111, call; 111, 112; 111, 113; 112, identifier:sum; 113, argument_list; 113, 114; 114, subscript; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:frequency; 118, identifier:i; 119, parenthesized_expression; 119, 120; 120, binary_operator:*; 120, 121; 120, 130; 121, call; 121, 122; 121, 123; 122, identifier:int; 123, argument_list; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:trajectory; 130, identifier:analysis_cutoff; 131, assert_statement; 131, 132; 131, 142; 132, comparison_operator:!=; 132, 133; 132, 141; 133, call; 133, 134; 133, 135; 134, identifier:len; 135, argument_list; 135, 136; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:topology_data; 140, identifier:dict_of_plotted_res; 141, integer:0; 142, binary_operator:+; 142, 143; 142, 176; 143, binary_operator:+; 143, 144; 143, 162; 144, binary_operator:+; 144, 145; 144, 161; 145, binary_operator:+; 145, 146; 145, 147; 146, string:"Nothing to draw for this ligand:(residue number: "; 147, call; 147, 148; 147, 149; 148, identifier:str; 149, argument_list; 149, 150; 150, subscript; 150, 151; 150, 160; 151, attribute; 151, 152; 151, 159; 152, attribute; 152, 153; 152, 158; 153, attribute; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:topology_data; 157, identifier:universe; 158, identifier:ligand; 159, identifier:resids; 160, integer:0; 161, string:" on the chain "; 162, call; 162, 163; 162, 164; 163, identifier:str; 164, argument_list; 164, 165; 165, subscript; 165, 166; 165, 175; 166, attribute; 166, 167; 166, 174; 167, attribute; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:topology_data; 172, identifier:universe; 173, identifier:ligand; 174, identifier:segids; 175, integer:0; 176, string:") - try reducing the analysis cutoff."
def define_residues_for_plotting_traj(self, analysis_cutoff): """ Since plotting all residues that have made contact with the ligand over a lenghty simulation is not always feasible or desirable. Therefore, only the residues that have been in contact with ligand for a long amount of time will be plotted in the final image. The function first determines the fraction of time each residue spends in the vicinity of the ligand for each trajectory. Once the data is processed, analysis cutoff decides whether or not these residues are plotted based on the total frequency this residue has spent in the vicinity of the ligand. The analysis cutoff is supplied for a single trajectory and is therefore multiplied. Takes: * analysis_cutoff * - a fraction (of time) a residue has to spend in the vicinity of the ligand for a single traj Output: * self.frequency * - frequency per residue per trajectory * topol_data.dict_of_plotted_res * - the residues that should be plotted in the final image with the frequency for each trajectory (used for plotting) """ self.residue_counts_fraction = {} #Calculate the fraction of time a residue spends in each simulation for traj in self.residue_counts: self.residue_counts_fraction[traj] = {residue:float(values)/len(self.contacts_per_timeframe[traj]) for residue,values in self.residue_counts[traj].items()} for traj in self.residue_counts_fraction: for residue in self.residue_counts_fraction[traj]: self.frequency[residue].append(self.residue_counts_fraction[traj][residue]) self.topology_data.dict_of_plotted_res = {i:self.frequency[i] for i in self.frequency if sum(self.frequency[i])>(int(len(self.trajectory))*analysis_cutoff)} assert len(self.topology_data.dict_of_plotted_res)!=0,"Nothing to draw for this ligand:(residue number: "+ str(self.topology_data.universe.ligand.resids[0]) +" on the chain "+ str(self.topology_data.universe.ligand.segids[0]) +") - try reducing the analysis cutoff."
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:find_donors_and_acceptors_in_ligand; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 24; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:atom_names; 11, list_comprehension; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:x; 14, identifier:name; 15, for_in_clause; 15, 16; 15, 17; 16, identifier:x; 17, attribute; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:topology_data; 22, identifier:universe; 23, identifier:ligand; 24, try_statement; 24, 25; 24, 88; 25, block; 25, 26; 25, 57; 26, for_statement; 26, 27; 26, 28; 26, 43; 27, identifier:atom; 28, call; 28, 29; 28, 36; 29, attribute; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:topology_data; 34, identifier:mol; 35, identifier:GetSubstructMatches; 36, argument_list; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:HDonorSmarts; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:uniquify; 42, integer:1; 43, block; 43, 44; 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:donors; 50, identifier:append; 51, argument_list; 51, 52; 52, subscript; 52, 53; 52, 54; 53, identifier:atom_names; 54, subscript; 54, 55; 54, 56; 55, identifier:atom; 56, integer:0; 57, for_statement; 57, 58; 57, 59; 57, 74; 58, identifier:atom; 59, call; 59, 60; 59, 67; 60, attribute; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:topology_data; 65, identifier:mol; 66, identifier:GetSubstructMatches; 67, argument_list; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:HAcceptorSmarts; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:uniquify; 73, integer:1; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:acceptors; 81, identifier:append; 82, argument_list; 82, 83; 83, subscript; 83, 84; 83, 85; 84, identifier:atom_names; 85, subscript; 85, 86; 85, 87; 86, identifier:atom; 87, integer:0; 88, except_clause; 88, 89; 88, 93; 89, as_pattern; 89, 90; 89, 91; 90, identifier:Exception; 91, as_pattern_target; 91, 92; 92, identifier:e; 93, block; 93, 94; 93, 103; 93, 109; 93, 115; 93, 142; 93, 146; 93, 157; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:m; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:Chem; 100, identifier:MolFromPDBFile; 101, argument_list; 101, 102; 102, string:"lig.pdb"; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:donors; 108, list:[]; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:acceptors; 114, list:[]; 115, for_statement; 115, 116; 115, 117; 115, 128; 116, identifier:atom; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:m; 120, identifier:GetSubstructMatches; 121, argument_list; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:HDonorSmarts; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:uniquify; 127, integer:1; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:donors; 135, identifier:append; 136, argument_list; 136, 137; 137, subscript; 137, 138; 137, 139; 138, identifier:atom_names; 139, subscript; 139, 140; 139, 141; 140, identifier:atom; 141, integer:0; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:haccep; 145, string:"[$([O,S;H1;v2]-[!$(*=[O,N,P,S])]),$([O,S;H0;v2]),$([O,S;-]),$([N;v3;!$(N-*=!@[O,N,P,S])]),$([nH0,o,s;+0])]"; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:HAcceptorSmarts; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:Chem; 154, identifier:MolFromSmarts; 155, argument_list; 155, 156; 156, identifier:haccep; 157, for_statement; 157, 158; 157, 159; 157, 170; 158, identifier:atom; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:m; 162, identifier:GetSubstructMatches; 163, argument_list; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:HAcceptorSmarts; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:uniquify; 169, integer:1; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:acceptors; 177, identifier:append; 178, argument_list; 178, 179; 179, subscript; 179, 180; 179, 181; 180, identifier:atom_names; 181, subscript; 181, 182; 181, 183; 182, identifier:atom; 183, integer:0
def find_donors_and_acceptors_in_ligand(self): """ Since MDAnalysis a pre-set list for acceptor and donor atoms for proteins and solvents from specific forcefields, it is necessary to find donor and acceptor atoms for the ligand molecule. This function uses RDKit and searches through ligand atoms to find matches for pre-set list of possible donor and acceptor atoms. The resulting list is then parsed to MDAnalysis through the donors and acceptors arguments. """ atom_names=[x.name for x in self.topology_data.universe.ligand] try: for atom in self.topology_data.mol.GetSubstructMatches(self.HDonorSmarts, uniquify=1): self.donors.append(atom_names[atom[0]]) for atom in self.topology_data.mol.GetSubstructMatches(self.HAcceptorSmarts, uniquify=1): self.acceptors.append(atom_names[atom[0]]) except Exception as e: m = Chem.MolFromPDBFile("lig.pdb") self.donors = [] self.acceptors = [] for atom in m.GetSubstructMatches(self.HDonorSmarts, uniquify=1): self.donors.append(atom_names[atom[0]]) haccep = "[$([O,S;H1;v2]-[!$(*=[O,N,P,S])]),$([O,S;H0;v2]),$([O,S;-]),$([N;v3;!$(N-*=!@[O,N,P,S])]),$([nH0,o,s;+0])]" self.HAcceptorSmarts = Chem.MolFromSmarts(haccep) for atom in m.GetSubstructMatches(self.HAcceptorSmarts, uniquify=1): self.acceptors.append(atom_names[atom[0]])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:determine_hbonds_for_drawing; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:analysis_cutoff; 6, block; 6, 7; 6, 9; 6, 18; 6, 88; 6, 89; 6, 126; 6, 127; 6, 133; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:frequency; 14, call; 14, 15; 14, 16; 15, identifier:defaultdict; 16, argument_list; 16, 17; 17, identifier:int; 18, for_statement; 18, 19; 18, 20; 18, 23; 19, identifier:traj; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:hbonds_by_type; 23, block; 23, 24; 24, for_statement; 24, 25; 24, 26; 24, 31; 24, 32; 24, 33; 25, identifier:bond; 26, subscript; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:hbonds_by_type; 30, identifier:traj; 31, comment; 32, comment; 33, block; 33, 34; 34, if_statement; 34, 35; 34, 40; 34, 63; 34, 64; 35, comparison_operator:!=; 35, 36; 35, 39; 36, subscript; 36, 37; 36, 38; 37, identifier:bond; 38, string:"donor_resnm"; 39, string:"LIG"; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, augmented_assignment:+=; 42, 43; 42, 60; 43, subscript; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:frequency; 47, tuple; 47, 48; 47, 51; 47, 54; 47, 57; 48, subscript; 48, 49; 48, 50; 49, identifier:bond; 50, string:"donor_idx"; 51, subscript; 51, 52; 51, 53; 52, identifier:bond; 53, string:"acceptor_atom"; 54, subscript; 54, 55; 54, 56; 55, identifier:bond; 56, string:"donor_atom"; 57, subscript; 57, 58; 57, 59; 58, identifier:bond; 59, string:"acceptor_idx"; 60, subscript; 60, 61; 60, 62; 61, identifier:bond; 62, string:"frequency"; 63, comment; 64, else_clause; 64, 65; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, augmented_assignment:+=; 67, 68; 67, 85; 68, subscript; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:frequency; 72, tuple; 72, 73; 72, 76; 72, 79; 72, 82; 73, subscript; 73, 74; 73, 75; 74, identifier:bond; 75, string:"acceptor_idx"; 76, subscript; 76, 77; 76, 78; 77, identifier:bond; 78, string:"donor_atom"; 79, subscript; 79, 80; 79, 81; 80, identifier:bond; 81, string:"acceptor_atom"; 82, subscript; 82, 83; 82, 84; 83, identifier:bond; 84, string:"donor_idx"; 85, subscript; 85, 86; 85, 87; 86, identifier:bond; 87, string:"frequency"; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:frequency; 94, dictionary_comprehension; 94, 95; 94, 102; 94, 107; 95, pair; 95, 96; 95, 97; 96, identifier:i; 97, subscript; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:frequency; 101, identifier:i; 102, for_in_clause; 102, 103; 102, 104; 103, identifier:i; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:frequency; 107, if_clause; 107, 108; 108, comparison_operator:>; 108, 109; 108, 114; 109, subscript; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:frequency; 113, identifier:i; 114, parenthesized_expression; 114, 115; 115, binary_operator:*; 115, 116; 115, 125; 116, call; 116, 117; 116, 118; 117, identifier:int; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:len; 121, argument_list; 121, 122; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:trajectory; 125, identifier:analysis_cutoff; 126, comment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:hbonds_for_drawing; 132, dictionary; 133, for_statement; 133, 134; 133, 135; 133, 138; 134, identifier:bond; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:frequency; 138, block; 138, 139; 138, 145; 138, 255; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:atomname; 142, subscript; 142, 143; 142, 144; 143, identifier:bond; 144, integer:1; 145, if_statement; 145, 146; 145, 161; 145, 166; 146, boolean_operator:or; 146, 147; 146, 154; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:atomname; 150, identifier:startswith; 151, argument_list; 151, 152; 151, 153; 152, string:"O"; 153, integer:0; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:atomname; 157, identifier:startswith; 158, argument_list; 158, 159; 158, 160; 159, string:"N"; 160, integer:0; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:lig_atom; 165, identifier:atomname; 166, else_clause; 166, 167; 167, block; 167, 168; 167, 197; 167, 210; 167, 226; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:atomindex; 171, subscript; 171, 172; 171, 196; 172, list_comprehension; 172, 173; 172, 174; 172, 190; 173, identifier:index; 174, for_in_clause; 174, 175; 174, 178; 175, pattern_list; 175, 176; 175, 177; 176, identifier:index; 177, identifier:atom; 178, call; 178, 179; 178, 180; 179, identifier:enumerate; 180, argument_list; 180, 181; 181, attribute; 181, 182; 181, 189; 182, attribute; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:topology_data; 187, identifier:universe; 188, identifier:ligand; 189, identifier:atoms; 190, if_clause; 190, 191; 191, comparison_operator:==; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:atom; 194, identifier:name; 195, identifier:atomname; 196, integer:0; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:rdkit_atom; 200, call; 200, 201; 200, 208; 201, attribute; 201, 202; 201, 207; 202, attribute; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:topology_data; 206, identifier:mol; 207, identifier:GetAtomWithIdx; 208, argument_list; 208, 209; 209, identifier:atomindex; 210, for_statement; 210, 211; 210, 212; 210, 217; 211, identifier:neigh; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:rdkit_atom; 215, identifier:GetNeighbors; 216, argument_list; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:neigh_atom_id; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:neigh; 224, identifier:GetIdx; 225, argument_list; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:lig_atom; 229, subscript; 229, 230; 229, 254; 230, list_comprehension; 230, 231; 230, 234; 230, 250; 231, attribute; 231, 232; 231, 233; 232, identifier:atom; 233, identifier:name; 234, for_in_clause; 234, 235; 234, 238; 235, pattern_list; 235, 236; 235, 237; 236, identifier:index; 237, identifier:atom; 238, call; 238, 239; 238, 240; 239, identifier:enumerate; 240, argument_list; 240, 241; 241, attribute; 241, 242; 241, 249; 242, attribute; 242, 243; 242, 248; 243, attribute; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:self; 246, identifier:topology_data; 247, identifier:universe; 248, identifier:ligand; 249, identifier:atoms; 250, if_clause; 250, 251; 251, comparison_operator:==; 251, 252; 251, 253; 252, identifier:index; 253, identifier:neigh_atom_id; 254, integer:0; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 272; 257, subscript; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:hbonds_for_drawing; 261, tuple; 261, 262; 261, 265; 261, 266; 261, 269; 262, subscript; 262, 263; 262, 264; 263, identifier:bond; 264, integer:0; 265, identifier:lig_atom; 266, subscript; 266, 267; 266, 268; 267, identifier:bond; 268, integer:2; 269, subscript; 269, 270; 269, 271; 270, identifier:bond; 271, integer:3; 272, subscript; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:frequency; 276, identifier:bond
def determine_hbonds_for_drawing(self, analysis_cutoff): """ Since plotting all hydrogen bonds could lead to a messy plot, a cutoff has to be imple- mented. In this function the frequency of each hydrogen bond is summated and the total compared against analysis cutoff - a fraction multiplied by trajectory count. Those hydrogen bonds that are present for longer than analysis cutoff will be plotted in the final plot. Takes: * analysis_cutoff * - (user-defined) fraction of time a hydrogen bond has to be present for to be plotted (default - 0.3). It is multiplied by number of trajectories Output: * frequency * - dictionary of hydrogen bond donor-acceptor indices and frequencies These hydrogen bonds will be plotted in the final image. """ self.frequency = defaultdict(int) for traj in self.hbonds_by_type: for bond in self.hbonds_by_type[traj]: # frequency[(residue_atom_idx,ligand_atom_name,residue_atom_name)]=frequency # residue atom name will be used to determine if hydrogen bond is interacting with a sidechain or bakcbone if bond["donor_resnm"]!="LIG": self.frequency[(bond["donor_idx"],bond["acceptor_atom"],bond["donor_atom"],bond["acceptor_idx"])] += bond["frequency"] #check whether ligand is donor or acceptor else: self.frequency[(bond["acceptor_idx"],bond["donor_atom"],bond["acceptor_atom"],bond["donor_idx"])] += bond["frequency"] #Add the frequency counts self.frequency = {i:self.frequency[i] for i in self.frequency if self.frequency[i]>(int(len(self.trajectory))*analysis_cutoff)} #change the ligand atomname to a heavy atom - required for plot since only heavy atoms shown in final image self.hbonds_for_drawing = {} for bond in self.frequency: atomname = bond[1] if atomname.startswith("O",0) or atomname.startswith("N",0): lig_atom=atomname else: atomindex = [index for index,atom in enumerate(self.topology_data.universe.ligand.atoms) if atom.name==atomname][0] rdkit_atom = self.topology_data.mol.GetAtomWithIdx(atomindex) for neigh in rdkit_atom.GetNeighbors(): neigh_atom_id = neigh.GetIdx() lig_atom = [atom.name for index,atom in enumerate(self.topology_data.universe.ligand.atoms) if index==neigh_atom_id][0] self.hbonds_for_drawing[(bond[0],lig_atom,bond[2],bond[3])]=self.frequency[bond]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:distance_function_match; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:l1; 5, identifier:l2; 6, identifier:thresh; 7, identifier:dist_fn; 8, default_parameter; 8, 9; 8, 10; 9, identifier:norm_funcs; 10, list:[]; 11, block; 11, 12; 11, 14; 11, 18; 11, 19; 11, 20; 11, 30; 11, 40; 11, 41; 11, 42; 11, 43; 11, 44; 11, 101; 11, 102; 11, 103; 11, 123; 11, 124; 11, 125; 11, 126; 11, 132; 11, 170; 11, 245; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:common; 17, list:[]; 18, comment; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:l1; 23, call; 23, 24; 23, 25; 24, identifier:list; 25, argument_list; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:enumerate; 28, argument_list; 28, 29; 29, identifier:l1; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:l2; 33, call; 33, 34; 33, 35; 34, identifier:list; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:enumerate; 38, argument_list; 38, 39; 39, identifier:l2; 40, comment; 41, comment; 42, comment; 43, comment; 44, for_statement; 44, 45; 44, 46; 44, 47; 45, identifier:norm_fn; 46, identifier:norm_funcs; 47, block; 47, 48; 47, 82; 47, 83; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 54; 50, pattern_list; 50, 51; 50, 52; 50, 53; 51, identifier:new_common; 52, identifier:l1; 53, identifier:l2; 54, call; 54, 55; 54, 56; 55, identifier:_match_by_norm_func; 56, argument_list; 56, 57; 56, 58; 56, 59; 56, 68; 56, 81; 57, identifier:l1; 58, identifier:l2; 59, lambda; 59, 60; 59, 62; 60, lambda_parameters; 60, 61; 61, identifier:a; 62, call; 62, 63; 62, 64; 63, identifier:norm_fn; 64, argument_list; 64, 65; 65, subscript; 65, 66; 65, 67; 66, identifier:a; 67, integer:1; 68, lambda; 68, 69; 68, 72; 69, lambda_parameters; 69, 70; 69, 71; 70, identifier:a1; 71, identifier:a2; 72, call; 72, 73; 72, 74; 73, identifier:dist_fn; 74, argument_list; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:a1; 77, integer:1; 78, subscript; 78, 79; 78, 80; 79, identifier:a2; 80, integer:1; 81, identifier:thresh; 82, comment; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:common; 87, identifier:extend; 88, generator_expression; 88, 89; 88, 96; 89, tuple; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:c1; 92, integer:0; 93, subscript; 93, 94; 93, 95; 94, identifier:c2; 95, integer:0; 96, for_in_clause; 96, 97; 96, 100; 97, pattern_list; 97, 98; 97, 99; 98, identifier:c1; 99, identifier:c2; 100, identifier:new_common; 101, comment; 102, comment; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:dist_matrix; 106, list_comprehension; 106, 107; 106, 118; 107, list_comprehension; 107, 108; 107, 113; 108, call; 108, 109; 108, 110; 109, identifier:dist_fn; 110, argument_list; 110, 111; 110, 112; 111, identifier:e1; 112, identifier:e2; 113, for_in_clause; 113, 114; 113, 117; 114, pattern_list; 114, 115; 114, 116; 115, identifier:i2; 116, identifier:e2; 117, identifier:l2; 118, for_in_clause; 118, 119; 118, 122; 119, pattern_list; 119, 120; 119, 121; 120, identifier:i1; 121, identifier:e1; 122, identifier:l1; 123, comment; 124, comment; 125, comment; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:components; 129, call; 129, 130; 129, 131; 130, identifier:BipartiteConnectedComponents; 131, argument_list; 132, for_statement; 132, 133; 132, 134; 132, 141; 133, identifier:l1_i; 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:l1; 141, block; 141, 142; 142, for_statement; 142, 143; 142, 144; 142, 151; 143, identifier:l2_i; 144, call; 144, 145; 144, 146; 145, identifier:range; 146, argument_list; 146, 147; 147, call; 147, 148; 147, 149; 148, identifier:len; 149, argument_list; 149, 150; 150, identifier:l2; 151, block; 151, 152; 151, 162; 152, if_statement; 152, 153; 152, 160; 153, comparison_operator:>; 153, 154; 153, 159; 154, subscript; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:dist_matrix; 157, identifier:l1_i; 158, identifier:l2_i; 159, identifier:thresh; 160, block; 160, 161; 161, continue_statement; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:components; 166, identifier:add_edge; 167, argument_list; 167, 168; 167, 169; 168, identifier:l1_i; 169, identifier:l2_i; 170, for_statement; 170, 171; 170, 174; 170, 179; 170, 180; 171, pattern_list; 171, 172; 171, 173; 172, identifier:l1_indices; 173, identifier:l2_indices; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:components; 177, identifier:get_connected_components; 178, argument_list; 179, comment; 180, block; 180, 181; 180, 191; 180, 201; 180, 217; 180, 227; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:part_l1; 184, list_comprehension; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:l1; 187, identifier:i; 188, for_in_clause; 188, 189; 188, 190; 189, identifier:i; 190, identifier:l1_indices; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:part_l2; 194, list_comprehension; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:l2; 197, identifier:i; 198, for_in_clause; 198, 199; 198, 200; 199, identifier:i; 200, identifier:l2_indices; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:part_dist_matrix; 204, list_comprehension; 204, 205; 204, 214; 205, list_comprehension; 205, 206; 205, 211; 206, subscript; 206, 207; 206, 210; 207, subscript; 207, 208; 207, 209; 208, identifier:dist_matrix; 209, identifier:l1_i; 210, identifier:l2_i; 211, for_in_clause; 211, 212; 211, 213; 212, identifier:l2_i; 213, identifier:l2_indices; 214, for_in_clause; 214, 215; 214, 216; 215, identifier:l1_i; 216, identifier:l1_indices; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:part_cmn; 220, call; 220, 221; 220, 222; 221, identifier:_match_munkres; 222, argument_list; 222, 223; 222, 224; 222, 225; 222, 226; 223, identifier:part_l1; 224, identifier:part_l2; 225, identifier:part_dist_matrix; 226, identifier:thresh; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:common; 231, identifier:extend; 232, generator_expression; 232, 233; 232, 240; 233, tuple; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:c1; 236, integer:0; 237, subscript; 237, 238; 237, 239; 238, identifier:c2; 239, integer:0; 240, for_in_clause; 240, 241; 240, 244; 241, pattern_list; 241, 242; 241, 243; 242, identifier:c1; 243, identifier:c2; 244, identifier:part_cmn; 245, return_statement; 245, 246; 246, identifier:common
def distance_function_match(l1, l2, thresh, dist_fn, norm_funcs=[]): """Returns pairs of matching indices from l1 and l2.""" common = [] # We will keep track of the global index in the source list as we # will successively reduce their sizes. l1 = list(enumerate(l1)) l2 = list(enumerate(l2)) # Use the distance function and threshold on hints given by normalization. # See _match_by_norm_func for implementation details. # Also wrap the list element function function to ignore the global list # index computed above. for norm_fn in norm_funcs: new_common, l1, l2 = _match_by_norm_func( l1, l2, lambda a: norm_fn(a[1]), lambda a1, a2: dist_fn(a1[1], a2[1]), thresh) # Keep only the global list index in the end result. common.extend((c1[0], c2[0]) for c1, c2 in new_common) # Take any remaining umatched entries and try to match them using the # Munkres algorithm. dist_matrix = [[dist_fn(e1, e2) for i2, e2 in l2] for i1, e1 in l1] # Call Munkres on connected components on the remaining bipartite graph. # An edge links an element from l1 with an element from l2 only if # the distance between the elements is less (or equal) than the theshold. components = BipartiteConnectedComponents() for l1_i in range(len(l1)): for l2_i in range(len(l2)): if dist_matrix[l1_i][l2_i] > thresh: continue components.add_edge(l1_i, l2_i) for l1_indices, l2_indices in components.get_connected_components(): # Build a partial distance matrix for each connected component. part_l1 = [l1[i] for i in l1_indices] part_l2 = [l2[i] for i in l2_indices] part_dist_matrix = [[dist_matrix[l1_i][l2_i] for l2_i in l2_indices] for l1_i in l1_indices] part_cmn = _match_munkres(part_l1, part_l2, part_dist_matrix, thresh) common.extend((c1[0], c2[0]) for c1, c2 in part_cmn) return common
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_match_by_norm_func; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:l1; 5, identifier:l2; 6, identifier:norm_fn; 7, identifier:dist_fn; 8, identifier:thresh; 9, block; 9, 10; 9, 12; 9, 16; 9, 29; 9, 42; 9, 61; 9, 80; 9, 222; 9, 232; 9, 242; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:common; 15, list:[]; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:l1_only_idx; 19, call; 19, 20; 19, 21; 20, identifier:set; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:range; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:len; 27, argument_list; 27, 28; 28, identifier:l1; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:l2_only_idx; 32, call; 32, 33; 32, 34; 33, identifier:set; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:range; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:len; 40, argument_list; 40, 41; 41, identifier:l2; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:buckets_l1; 45, call; 45, 46; 45, 47; 46, identifier:_group_by_fn; 47, argument_list; 47, 48; 47, 52; 48, call; 48, 49; 48, 50; 49, identifier:enumerate; 50, argument_list; 50, 51; 51, identifier:l1; 52, lambda; 52, 53; 52, 55; 53, lambda_parameters; 53, 54; 54, identifier:x; 55, call; 55, 56; 55, 57; 56, identifier:norm_fn; 57, argument_list; 57, 58; 58, subscript; 58, 59; 58, 60; 59, identifier:x; 60, integer:1; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:buckets_l2; 64, call; 64, 65; 64, 66; 65, identifier:_group_by_fn; 66, argument_list; 66, 67; 66, 71; 67, call; 67, 68; 67, 69; 68, identifier:enumerate; 69, argument_list; 69, 70; 70, identifier:l2; 71, lambda; 71, 72; 71, 74; 72, lambda_parameters; 72, 73; 73, identifier:x; 74, call; 74, 75; 74, 76; 75, identifier:norm_fn; 76, argument_list; 76, 77; 77, subscript; 77, 78; 77, 79; 78, identifier:x; 79, integer:1; 80, for_statement; 80, 81; 80, 84; 80, 89; 81, pattern_list; 81, 82; 81, 83; 82, identifier:normed; 83, identifier:l1_elements; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:buckets_l1; 87, identifier:items; 88, argument_list; 89, block; 89, 90; 89, 100; 89, 108; 89, 118; 89, 128; 89, 171; 89, 175; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:l2_elements; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:buckets_l2; 96, identifier:get; 97, argument_list; 97, 98; 97, 99; 98, identifier:normed; 99, list:[]; 100, if_statement; 100, 101; 100, 106; 101, boolean_operator:or; 101, 102; 101, 104; 102, not_operator; 102, 103; 103, identifier:l1_elements; 104, not_operator; 104, 105; 105, identifier:l2_elements; 106, block; 106, 107; 107, continue_statement; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 115; 110, pattern_list; 110, 111; 110, 112; 111, identifier:_; 112, tuple_pattern; 112, 113; 112, 114; 113, identifier:_; 114, identifier:e1_first; 115, subscript; 115, 116; 115, 117; 116, identifier:l1_elements; 117, integer:0; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 125; 120, pattern_list; 120, 121; 120, 122; 121, identifier:_; 122, tuple_pattern; 122, 123; 122, 124; 123, identifier:_; 124, identifier:e2_first; 125, subscript; 125, 126; 125, 127; 126, identifier:l2_elements; 127, integer:0; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:match_is_ambiguous; 131, not_operator; 131, 132; 132, parenthesized_expression; 132, 133; 133, boolean_operator:and; 133, 134; 133, 143; 134, comparison_operator:==; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:len; 137, argument_list; 137, 138; 138, identifier:l1_elements; 139, call; 139, 140; 139, 141; 140, identifier:len; 141, argument_list; 141, 142; 142, identifier:l2_elements; 143, parenthesized_expression; 143, 144; 144, boolean_operator:or; 144, 145; 144, 158; 145, call; 145, 146; 145, 147; 146, identifier:all; 147, generator_expression; 147, 148; 147, 151; 148, comparison_operator:==; 148, 149; 148, 150; 149, identifier:e2; 150, identifier:e2_first; 151, for_in_clause; 151, 152; 151, 157; 152, tuple_pattern; 152, 153; 152, 154; 153, identifier:_; 154, tuple_pattern; 154, 155; 154, 156; 155, identifier:_; 156, identifier:e2; 157, identifier:l2_elements; 158, call; 158, 159; 158, 160; 159, identifier:all; 160, generator_expression; 160, 161; 160, 164; 161, comparison_operator:==; 161, 162; 161, 163; 162, identifier:e1; 163, identifier:e1_first; 164, for_in_clause; 164, 165; 164, 170; 165, tuple_pattern; 165, 166; 165, 167; 166, identifier:_; 167, tuple_pattern; 167, 168; 167, 169; 168, identifier:_; 169, identifier:e1; 170, identifier:l1_elements; 171, if_statement; 171, 172; 171, 173; 172, identifier:match_is_ambiguous; 173, block; 173, 174; 174, continue_statement; 175, for_statement; 175, 176; 175, 183; 175, 188; 176, pattern_list; 176, 177; 176, 180; 177, tuple_pattern; 177, 178; 177, 179; 178, identifier:e1_idx; 179, identifier:e1; 180, tuple_pattern; 180, 181; 180, 182; 181, identifier:e2_idx; 182, identifier:e2; 183, call; 183, 184; 183, 185; 184, identifier:zip; 185, argument_list; 185, 186; 185, 187; 186, identifier:l1_elements; 187, identifier:l2_elements; 188, block; 188, 189; 188, 199; 188, 206; 188, 213; 189, if_statement; 189, 190; 189, 197; 190, comparison_operator:>; 190, 191; 190, 196; 191, call; 191, 192; 191, 193; 192, identifier:dist_fn; 193, argument_list; 193, 194; 193, 195; 194, identifier:e1; 195, identifier:e2; 196, identifier:thresh; 197, block; 197, 198; 198, continue_statement; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:l1_only_idx; 203, identifier:remove; 204, argument_list; 204, 205; 205, identifier:e1_idx; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:l2_only_idx; 210, identifier:remove; 211, argument_list; 211, 212; 212, identifier:e2_idx; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:common; 217, identifier:append; 218, argument_list; 218, 219; 219, tuple; 219, 220; 219, 221; 220, identifier:e1; 221, identifier:e2; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:l1_only; 225, list_comprehension; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:l1; 228, identifier:i; 229, for_in_clause; 229, 230; 229, 231; 230, identifier:i; 231, identifier:l1_only_idx; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:l2_only; 235, list_comprehension; 235, 236; 235, 239; 236, subscript; 236, 237; 236, 238; 237, identifier:l2; 238, identifier:i; 239, for_in_clause; 239, 240; 239, 241; 240, identifier:i; 241, identifier:l2_only_idx; 242, return_statement; 242, 243; 243, expression_list; 243, 244; 243, 245; 243, 246; 244, identifier:common; 245, identifier:l1_only; 246, identifier:l2_only
def _match_by_norm_func(l1, l2, norm_fn, dist_fn, thresh): """Matches elements in l1 and l2 using normalization functions. Splits the elements in each list into buckets given by the normalization function. If the same normalization value points to a bucket from the first list and a bucket from the second list, both with a single element we consider the elements in the list as matching if the distance between them is less (or equal) than the threshold. e.g. l1 = ['X1', 'Y1', 'Y2', 'Z5'], l2 = ['X1', 'Y3', 'Z1'] norm_fn = lambda x: x[0] dist_fn = lambda e1, e2: 0 if e1 == e2 else 1 thresh = 0 The buckets will then be: l1_bucket = {'X': ['X1'], 'Y': ['Y1', 'Y2'], 'Z': ['Z5']} l2_bucket = {'X': ['X1'], 'Y': ['Y3'], 'Z': ['Z1']} For each normalized value: 'X' -> consider 'X1' equal with 'X1' since the distance is equal with the thershold 'Y' -> skip the lists since we have multiple possible matches 'Z' -> consider 'Z1' and 'Z5' as different since the distance is greater than the threshold. Return: [('X1', 'X2')] """ common = [] l1_only_idx = set(range(len(l1))) l2_only_idx = set(range(len(l2))) buckets_l1 = _group_by_fn(enumerate(l1), lambda x: norm_fn(x[1])) buckets_l2 = _group_by_fn(enumerate(l2), lambda x: norm_fn(x[1])) for normed, l1_elements in buckets_l1.items(): l2_elements = buckets_l2.get(normed, []) if not l1_elements or not l2_elements: continue _, (_, e1_first) = l1_elements[0] _, (_, e2_first) = l2_elements[0] match_is_ambiguous = not ( len(l1_elements) == len(l2_elements) and ( all(e2 == e2_first for (_, (_, e2)) in l2_elements) or all(e1 == e1_first for (_, (_, e1)) in l1_elements) ) ) if match_is_ambiguous: continue for (e1_idx, e1), (e2_idx, e2) in zip(l1_elements, l2_elements): if dist_fn(e1, e2) > thresh: continue l1_only_idx.remove(e1_idx) l2_only_idx.remove(e2_idx) common.append((e1, e2)) l1_only = [l1[i] for i in l1_only_idx] l2_only = [l2[i] for i in l2_only_idx] return common, l1_only, l2_only
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:dump; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:props; 5, identifier:fp; 6, default_parameter; 6, 7; 6, 8; 7, identifier:separator; 8, string:'='; 9, default_parameter; 9, 10; 9, 11; 10, identifier:comments; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:timestamp; 14, True; 15, default_parameter; 15, 16; 15, 17; 16, identifier:sort_keys; 17, False; 18, block; 18, 19; 18, 21; 18, 37; 18, 60; 19, expression_statement; 19, 20; 20, comment; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:comments; 24, None; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:print; 29, argument_list; 29, 30; 29, 34; 30, call; 30, 31; 30, 32; 31, identifier:to_comment; 32, argument_list; 32, 33; 33, identifier:comments; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:file; 36, identifier:fp; 37, if_statement; 37, 38; 37, 45; 38, boolean_operator:and; 38, 39; 38, 42; 39, comparison_operator:is; 39, 40; 39, 41; 40, identifier:timestamp; 41, None; 42, comparison_operator:is; 42, 43; 42, 44; 43, identifier:timestamp; 44, False; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:print; 49, argument_list; 49, 50; 49, 57; 50, call; 50, 51; 50, 52; 51, identifier:to_comment; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:java_timestamp; 55, argument_list; 55, 56; 56, identifier:timestamp; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:file; 59, identifier:fp; 60, for_statement; 60, 61; 60, 64; 60, 71; 61, pattern_list; 61, 62; 61, 63; 62, identifier:k; 63, identifier:v; 64, call; 64, 65; 64, 66; 65, identifier:itemize; 66, argument_list; 66, 67; 66, 68; 67, identifier:props; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:sort_keys; 70, identifier:sort_keys; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:print; 75, argument_list; 75, 76; 75, 82; 76, call; 76, 77; 76, 78; 77, identifier:join_key_value; 78, argument_list; 78, 79; 78, 80; 78, 81; 79, identifier:k; 80, identifier:v; 81, identifier:separator; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:file; 84, identifier:fp
def dump(props, fp, separator='=', comments=None, timestamp=True, sort_keys=False): """ Write a series of key-value pairs to a file in simple line-oriented ``.properties`` format. :param props: A mapping or iterable of ``(key, value)`` pairs to write to ``fp``. All keys and values in ``props`` must be text strings. If ``sort_keys`` is `False`, the entries are output in iteration order. :param fp: A file-like object to write the values of ``props`` to. It must have been opened as a text file with a Latin-1-compatible encoding. :param separator: The string to use for separating keys & values. Only ``" "``, ``"="``, and ``":"`` (possibly with added whitespace) should ever be used as the separator. :type separator: text string :param comments: if non-`None`, ``comments`` will be written to ``fp`` as a comment before any other content :type comments: text string or `None` :param timestamp: If neither `None` nor `False`, a timestamp in the form of ``Mon Sep 02 14:00:54 EDT 2016`` is written as a comment to ``fp`` after ``comments`` (if any) and before the key-value pairs. If ``timestamp`` is `True`, the current date & time is used. If it is a number, it is converted from seconds since the epoch to local time. If it is a `datetime.datetime` object, its value is used directly, with naïve objects assumed to be in the local timezone. :type timestamp: `None`, `bool`, number, or `datetime.datetime` :param bool sort_keys: if true, the elements of ``props`` are sorted lexicographically by key in the output :return: `None` """ if comments is not None: print(to_comment(comments), file=fp) if timestamp is not None and timestamp is not False: print(to_comment(java_timestamp(timestamp)), file=fp) for k,v in itemize(props, sort_keys=sort_keys): print(join_key_value(k, v, separator), file=fp)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:dumps; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:props; 5, default_parameter; 5, 6; 5, 7; 6, identifier:separator; 7, string:'='; 8, default_parameter; 8, 9; 8, 10; 9, identifier:comments; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:timestamp; 13, True; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sort_keys; 16, False; 17, block; 17, 18; 17, 20; 17, 26; 17, 44; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:s; 23, call; 23, 24; 23, 25; 24, identifier:StringIO; 25, argument_list; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:dump; 29, argument_list; 29, 30; 29, 31; 29, 32; 29, 35; 29, 38; 29, 41; 30, identifier:props; 31, identifier:s; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:separator; 34, identifier:separator; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:comments; 37, identifier:comments; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:timestamp; 40, identifier:timestamp; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:sort_keys; 43, identifier:sort_keys; 44, return_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:s; 48, identifier:getvalue; 49, argument_list
def dumps(props, separator='=', comments=None, timestamp=True, sort_keys=False): """ Convert a series of key-value pairs to a text string in simple line-oriented ``.properties`` format. :param props: A mapping or iterable of ``(key, value)`` pairs to serialize. All keys and values in ``props`` must be text strings. If ``sort_keys`` is `False`, the entries are output in iteration order. :param separator: The string to use for separating keys & values. Only ``" "``, ``"="``, and ``":"`` (possibly with added whitespace) should ever be used as the separator. :type separator: text string :param comments: if non-`None`, ``comments`` will be output as a comment before any other content :type comments: text string or `None` :param timestamp: If neither `None` nor `False`, a timestamp in the form of ``Mon Sep 02 14:00:54 EDT 2016`` is output as a comment after ``comments`` (if any) and before the key-value pairs. If ``timestamp`` is `True`, the current date & time is used. If it is a number, it is converted from seconds since the epoch to local time. If it is a `datetime.datetime` object, its value is used directly, with naïve objects assumed to be in the local timezone. :type timestamp: `None`, `bool`, number, or `datetime.datetime` :param bool sort_keys: if true, the elements of ``props`` are sorted lexicographically by key in the output :rtype: text string """ s = StringIO() dump(props, s, separator=separator, comments=comments, timestamp=timestamp, sort_keys=sort_keys) return s.getvalue()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:variants; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:case_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:skip; 8, integer:0; 9, default_parameter; 9, 10; 9, 11; 10, identifier:count; 11, integer:1000; 12, default_parameter; 12, 13; 12, 14; 13, identifier:filters; 14, None; 15, block; 15, 16; 15, 18; 15, 24; 15, 36; 15, 42; 15, 53; 15, 57; 15, 92; 15, 125; 15, 199; 15, 265; 15, 279; 15, 316; 15, 366; 15, 397; 15, 401; 15, 430; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:filters; 21, boolean_operator:or; 21, 22; 21, 23; 22, identifier:filters; 23, dictionary; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:logger; 28, identifier:debug; 29, argument_list; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, string:"Looking for variants in {0}"; 33, identifier:format; 34, argument_list; 34, 35; 35, identifier:case_id; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:limit; 39, binary_operator:+; 39, 40; 39, 41; 40, identifier:count; 41, identifier:skip; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:gemini_query; 45, boolean_operator:or; 45, 46; 45, 52; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:filters; 49, identifier:get; 50, argument_list; 50, 51; 51, string:'gemini_query'; 52, string:"SELECT * from variants v"; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:any_filter; 56, False; 57, if_statement; 57, 58; 57, 64; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:filters; 61, identifier:get; 62, argument_list; 62, 63; 63, string:'frequency'; 64, block; 64, 65; 64, 71; 64, 82; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:frequency; 68, subscript; 68, 69; 68, 70; 69, identifier:filters; 70, string:'frequency'; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:extra_info; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, concatenated_string; 76, 77; 76, 78; 77, string:"(v.max_aaf_all < {0} or v.max_aaf_all is"; 78, string:" Null)"; 79, identifier:format; 80, argument_list; 80, 81; 81, identifier:frequency; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:gemini_query; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:build_gemini_query; 89, argument_list; 89, 90; 89, 91; 90, identifier:gemini_query; 91, identifier:extra_info; 92, if_statement; 92, 93; 92, 99; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:filters; 96, identifier:get; 97, argument_list; 97, 98; 98, string:'cadd'; 99, block; 99, 100; 99, 106; 99, 115; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:cadd_score; 103, subscript; 103, 104; 103, 105; 104, identifier:filters; 105, string:'cadd'; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:extra_info; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, string:"(v.cadd_scaled > {0})"; 112, identifier:format; 113, argument_list; 113, 114; 114, identifier:cadd_score; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:gemini_query; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:build_gemini_query; 122, argument_list; 122, 123; 122, 124; 123, identifier:gemini_query; 124, identifier:extra_info; 125, if_statement; 125, 126; 125, 132; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:filters; 129, identifier:get; 130, argument_list; 130, 131; 131, string:'gene_ids'; 132, block; 132, 133; 132, 147; 132, 151; 132, 185; 132, 189; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:gene_list; 136, list_comprehension; 136, 137; 136, 142; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:gene_id; 140, identifier:strip; 141, argument_list; 142, for_in_clause; 142, 143; 142, 144; 143, identifier:gene_id; 144, subscript; 144, 145; 144, 146; 145, identifier:filters; 146, string:'gene_ids'; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:gene_string; 150, string:"v.gene in ("; 151, for_statement; 151, 152; 151, 155; 151, 159; 152, pattern_list; 152, 153; 152, 154; 153, identifier:index; 154, identifier:gene_id; 155, call; 155, 156; 155, 157; 156, identifier:enumerate; 157, argument_list; 157, 158; 158, identifier:gene_list; 159, block; 159, 160; 160, if_statement; 160, 161; 160, 164; 160, 174; 161, comparison_operator:==; 161, 162; 161, 163; 162, identifier:index; 163, integer:0; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, augmented_assignment:+=; 166, 167; 166, 168; 167, identifier:gene_string; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, string:"'{0}'"; 171, identifier:format; 172, argument_list; 172, 173; 173, identifier:gene_id; 174, else_clause; 174, 175; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, augmented_assignment:+=; 177, 178; 177, 179; 178, identifier:gene_string; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, string:", '{0}'"; 182, identifier:format; 183, argument_list; 183, 184; 184, identifier:gene_id; 185, expression_statement; 185, 186; 186, augmented_assignment:+=; 186, 187; 186, 188; 187, identifier:gene_string; 188, string:")"; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:gemini_query; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:build_gemini_query; 196, argument_list; 196, 197; 196, 198; 197, identifier:gemini_query; 198, identifier:gene_string; 199, if_statement; 199, 200; 199, 206; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:filters; 203, identifier:get; 204, argument_list; 204, 205; 205, string:'range'; 206, block; 206, 207; 206, 215; 206, 233; 206, 255; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:chrom; 210, subscript; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:filters; 213, string:'range'; 214, string:'chromosome'; 215, if_statement; 215, 216; 215, 223; 216, not_operator; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:chrom; 220, identifier:startswith; 221, argument_list; 221, 222; 222, string:'chr'; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:chrom; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, string:"chr{0}"; 230, identifier:format; 231, argument_list; 231, 232; 232, identifier:chrom; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:range_string; 236, call; 236, 237; 236, 243; 237, attribute; 237, 238; 237, 242; 238, concatenated_string; 238, 239; 238, 240; 238, 241; 239, string:"v.chrom = '{0}' AND "; 240, string:"((v.start BETWEEN {1} AND {2}) OR "; 241, string:"(v.end BETWEEN {1} AND {2}))"; 242, identifier:format; 243, argument_list; 243, 244; 243, 245; 243, 250; 244, identifier:chrom; 245, subscript; 245, 246; 245, 249; 246, subscript; 246, 247; 246, 248; 247, identifier:filters; 248, string:'range'; 249, string:'start'; 250, subscript; 250, 251; 250, 254; 251, subscript; 251, 252; 251, 253; 252, identifier:filters; 253, string:'range'; 254, string:'end'; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:gemini_query; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:build_gemini_query; 262, argument_list; 262, 263; 262, 264; 263, identifier:gemini_query; 264, identifier:range_string; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:filtered_variants; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:self; 271, identifier:_variants; 272, argument_list; 272, 273; 272, 276; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:case_id; 275, identifier:case_id; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:gemini_query; 278, identifier:gemini_query; 279, if_statement; 279, 280; 279, 286; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:filters; 283, identifier:get; 284, argument_list; 284, 285; 285, string:'consequence'; 286, block; 286, 287; 286, 296; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:consequences; 290, call; 290, 291; 290, 292; 291, identifier:set; 292, argument_list; 292, 293; 293, subscript; 293, 294; 293, 295; 294, identifier:filters; 295, string:'consequence'; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 299; 298, identifier:filtered_variants; 299, generator_expression; 299, 300; 299, 301; 299, 304; 300, identifier:variant; 301, for_in_clause; 301, 302; 301, 303; 302, identifier:variant; 303, identifier:filtered_variants; 304, if_clause; 304, 305; 305, call; 305, 306; 305, 314; 306, attribute; 306, 307; 306, 313; 307, call; 307, 308; 307, 309; 308, identifier:set; 309, argument_list; 309, 310; 310, attribute; 310, 311; 310, 312; 311, identifier:variant; 312, identifier:consequences; 313, identifier:intersection; 314, argument_list; 314, 315; 315, identifier:consequences; 316, if_statement; 316, 317; 316, 323; 317, call; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:filters; 320, identifier:get; 321, argument_list; 321, 322; 322, string:'impact_severities'; 323, block; 323, 324; 323, 341; 323, 345; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 327; 326, identifier:severities; 327, call; 327, 328; 327, 329; 328, identifier:set; 329, argument_list; 329, 330; 330, list_comprehension; 330, 331; 330, 336; 331, call; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:severity; 334, identifier:strip; 335, argument_list; 336, for_in_clause; 336, 337; 336, 338; 337, identifier:severity; 338, subscript; 338, 339; 338, 340; 339, identifier:filters; 340, string:'impact_severities'; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 344; 343, identifier:new_filtered_variants; 344, list:[]; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:filtered_variants; 348, generator_expression; 348, 349; 348, 350; 348, 353; 349, identifier:variant; 350, for_in_clause; 350, 351; 350, 352; 351, identifier:variant; 352, identifier:filtered_variants; 353, if_clause; 353, 354; 354, call; 354, 355; 354, 364; 355, attribute; 355, 356; 355, 363; 356, call; 356, 357; 356, 358; 357, identifier:set; 358, argument_list; 358, 359; 359, list:[variant.impact_severity]; 359, 360; 360, attribute; 360, 361; 360, 362; 361, identifier:variant; 362, identifier:impact_severity; 363, identifier:intersection; 364, argument_list; 364, 365; 365, identifier:severities; 366, if_statement; 366, 367; 366, 373; 367, call; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:filters; 370, identifier:get; 371, argument_list; 371, 372; 372, string:'sv_len'; 373, block; 373, 374; 373, 383; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 377; 376, identifier:sv_len; 377, call; 377, 378; 377, 379; 378, identifier:int; 379, argument_list; 379, 380; 380, subscript; 380, 381; 380, 382; 381, identifier:filters; 382, string:'sv_len'; 383, expression_statement; 383, 384; 384, assignment; 384, 385; 384, 386; 385, identifier:filtered_variants; 386, generator_expression; 386, 387; 386, 388; 386, 391; 387, identifier:variant; 388, for_in_clause; 388, 389; 388, 390; 389, identifier:variant; 390, identifier:filtered_variants; 391, if_clause; 391, 392; 392, comparison_operator:>=; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:variant; 395, identifier:sv_len; 396, identifier:sv_len; 397, expression_statement; 397, 398; 398, assignment; 398, 399; 398, 400; 399, identifier:variants; 400, list:[]; 401, for_statement; 401, 402; 401, 405; 401, 409; 402, pattern_list; 402, 403; 402, 404; 403, identifier:index; 404, identifier:variant_obj; 405, call; 405, 406; 405, 407; 406, identifier:enumerate; 407, argument_list; 407, 408; 408, identifier:filtered_variants; 409, block; 409, 410; 410, if_statement; 410, 411; 410, 414; 411, comparison_operator:>=; 411, 412; 411, 413; 412, identifier:index; 413, identifier:skip; 414, block; 414, 415; 415, if_statement; 415, 416; 415, 419; 415, 427; 416, comparison_operator:<; 416, 417; 416, 418; 417, identifier:index; 418, identifier:limit; 419, block; 419, 420; 420, expression_statement; 420, 421; 421, call; 421, 422; 421, 425; 422, attribute; 422, 423; 422, 424; 423, identifier:variants; 424, identifier:append; 425, argument_list; 425, 426; 426, identifier:variant_obj; 427, else_clause; 427, 428; 428, block; 428, 429; 429, break_statement; 430, return_statement; 430, 431; 431, call; 431, 432; 431, 433; 432, identifier:Results; 433, argument_list; 433, 434; 433, 435; 434, identifier:variants; 435, call; 435, 436; 435, 437; 436, identifier:len; 437, argument_list; 437, 438; 438, identifier:variants
def variants(self, case_id, skip=0, count=1000, filters=None): """Return count variants for a case. This function needs to have different behaviours based on what is asked for. It should allways try to give minimal information back to improve on speed. For example, if consequences are not asked for we will not build all transcripts. If not sv variants we will not build sv coordinates. So the minimal case is to just show what is asked for in the variants interface. Args: case_id (str): A gemini db skip (int): Skip first variants count (int): The number of variants to return filters (dict): A dictionary with filters. Currently this will look like: { gene_list: [] (list of hgnc ids), frequency: None (float), cadd: None (float), consequence: [] (list of consequences), impact_severities: [] (list of consequences), genetic_models [] (list of genetic models) } Returns: puzzle.constants.Results : Named tuple with variants and nr_of_variants """ filters = filters or {} logger.debug("Looking for variants in {0}".format(case_id)) limit = count + skip gemini_query = filters.get('gemini_query') or "SELECT * from variants v" any_filter = False if filters.get('frequency'): frequency = filters['frequency'] extra_info = "(v.max_aaf_all < {0} or v.max_aaf_all is"\ " Null)".format(frequency) gemini_query = self.build_gemini_query(gemini_query, extra_info) if filters.get('cadd'): cadd_score = filters['cadd'] extra_info = "(v.cadd_scaled > {0})".format(cadd_score) gemini_query = self.build_gemini_query(gemini_query, extra_info) if filters.get('gene_ids'): gene_list = [gene_id.strip() for gene_id in filters['gene_ids']] gene_string = "v.gene in (" for index, gene_id in enumerate(gene_list): if index == 0: gene_string += "'{0}'".format(gene_id) else: gene_string += ", '{0}'".format(gene_id) gene_string += ")" gemini_query = self.build_gemini_query(gemini_query, gene_string) if filters.get('range'): chrom = filters['range']['chromosome'] if not chrom.startswith('chr'): chrom = "chr{0}".format(chrom) range_string = "v.chrom = '{0}' AND "\ "((v.start BETWEEN {1} AND {2}) OR "\ "(v.end BETWEEN {1} AND {2}))".format( chrom, filters['range']['start'], filters['range']['end'] ) gemini_query = self.build_gemini_query(gemini_query, range_string) filtered_variants = self._variants( case_id=case_id, gemini_query=gemini_query, ) if filters.get('consequence'): consequences = set(filters['consequence']) filtered_variants = (variant for variant in filtered_variants if set(variant.consequences).intersection(consequences)) if filters.get('impact_severities'): severities = set([severity.strip() for severity in filters['impact_severities']]) new_filtered_variants = [] filtered_variants = (variant for variant in filtered_variants if set([variant.impact_severity]).intersection(severities)) if filters.get('sv_len'): sv_len = int(filters['sv_len']) filtered_variants = (variant for variant in filtered_variants if variant.sv_len >= sv_len) variants = [] for index, variant_obj in enumerate(filtered_variants): if index >= skip: if index < limit: variants.append(variant_obj) else: break return Results(variants, len(variants))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:_format_variant; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 4, identifier:self; 5, identifier:case_id; 6, identifier:gemini_variant; 7, identifier:individual_objs; 8, default_parameter; 8, 9; 8, 10; 9, identifier:index; 10, integer:0; 11, default_parameter; 11, 12; 11, 13; 12, identifier:add_all_info; 13, False; 14, block; 14, 15; 14, 17; 14, 23; 14, 46; 14, 86; 14, 94; 14, 95; 14, 104; 14, 118; 14, 124; 14, 125; 14, 133; 14, 134; 14, 142; 14, 143; 14, 154; 14, 165; 14, 166; 14, 285; 14, 286; 14, 293; 14, 307; 14, 314; 14, 315; 14, 316; 14, 343; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:chrom; 20, subscript; 20, 21; 20, 22; 21, identifier:gemini_variant; 22, string:'chrom'; 23, if_statement; 23, 24; 23, 37; 24, boolean_operator:or; 24, 25; 24, 31; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:chrom; 28, identifier:startswith; 29, argument_list; 29, 30; 30, string:'chr'; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:chrom; 34, identifier:startswith; 35, argument_list; 35, 36; 36, string:'CHR'; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:chrom; 41, subscript; 41, 42; 41, 43; 42, identifier:chrom; 43, slice; 43, 44; 43, 45; 44, integer:3; 45, colon; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:variant_dict; 49, dictionary; 49, 50; 49, 53; 49, 61; 49, 66; 49, 71; 49, 76; 49, 81; 50, pair; 50, 51; 50, 52; 51, string:'CHROM'; 52, identifier:chrom; 53, pair; 53, 54; 53, 55; 54, string:'POS'; 55, call; 55, 56; 55, 57; 56, identifier:str; 57, argument_list; 57, 58; 58, subscript; 58, 59; 58, 60; 59, identifier:gemini_variant; 60, string:'start'; 61, pair; 61, 62; 61, 63; 62, string:'ID'; 63, subscript; 63, 64; 63, 65; 64, identifier:gemini_variant; 65, string:'rs_ids'; 66, pair; 66, 67; 66, 68; 67, string:'REF'; 68, subscript; 68, 69; 68, 70; 69, identifier:gemini_variant; 70, string:'ref'; 71, pair; 71, 72; 71, 73; 72, string:'ALT'; 73, subscript; 73, 74; 73, 75; 74, identifier:gemini_variant; 75, string:'alt'; 76, pair; 76, 77; 76, 78; 77, string:'QUAL'; 78, subscript; 78, 79; 78, 80; 79, identifier:gemini_variant; 80, string:'qual'; 81, pair; 81, 82; 81, 83; 82, string:'FILTER'; 83, subscript; 83, 84; 83, 85; 84, identifier:gemini_variant; 85, string:'filter'; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:variant; 89, call; 89, 90; 89, 91; 90, identifier:Variant; 91, argument_list; 91, 92; 92, dictionary_splat; 92, 93; 93, identifier:variant_dict; 94, comment; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:variant; 99, identifier:update_variant_id; 100, argument_list; 100, 101; 101, subscript; 101, 102; 101, 103; 102, identifier:gemini_variant; 103, string:'variant_id'; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:logger; 108, identifier:debug; 109, argument_list; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, string:"Creating a variant object of variant {0}"; 113, identifier:format; 114, argument_list; 114, 115; 115, attribute; 115, 116; 115, 117; 116, identifier:variant; 117, identifier:variant_id; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:variant; 122, string:'index'; 123, identifier:index; 124, comment; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:_add_most_severe_consequence; 130, argument_list; 130, 131; 130, 132; 131, identifier:variant; 132, identifier:gemini_variant; 133, comment; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:_add_impact_severity; 139, argument_list; 139, 140; 139, 141; 140, identifier:variant; 141, identifier:gemini_variant; 142, comment; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:variant; 147, identifier:start; 148, call; 148, 149; 148, 150; 149, identifier:int; 150, argument_list; 150, 151; 151, subscript; 151, 152; 151, 153; 152, identifier:gemini_variant; 153, string:'start'; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:variant; 158, identifier:stop; 159, call; 159, 160; 159, 161; 160, identifier:int; 161, argument_list; 161, 162; 162, subscript; 162, 163; 162, 164; 163, identifier:gemini_variant; 164, string:'end'; 165, comment; 166, if_statement; 166, 167; 166, 172; 166, 199; 167, comparison_operator:==; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:variant_type; 171, string:'sv'; 172, block; 172, 173; 172, 181; 172, 192; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:variant; 177, identifier:sv_type; 178, subscript; 178, 179; 178, 180; 179, identifier:gemini_variant; 180, string:'sub_type'; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:variant; 185, identifier:stop; 186, call; 186, 187; 186, 188; 187, identifier:int; 188, argument_list; 188, 189; 189, subscript; 189, 190; 189, 191; 190, identifier:gemini_variant; 191, string:'end'; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:_add_sv_coordinates; 197, argument_list; 197, 198; 198, identifier:variant; 199, else_clause; 199, 200; 199, 201; 199, 202; 200, comment; 201, comment; 202, block; 202, 203; 202, 211; 202, 219; 202, 227; 202, 235; 202, 236; 202, 249; 202, 250; 202, 256; 202, 267; 202, 268; 202, 274; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:_add_transcripts; 208, argument_list; 208, 209; 208, 210; 209, identifier:variant; 210, identifier:gemini_variant; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:_add_thousand_g; 216, argument_list; 216, 217; 216, 218; 217, identifier:variant; 218, identifier:gemini_variant; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:_add_exac; 224, argument_list; 224, 225; 224, 226; 225, identifier:variant; 226, identifier:gemini_variant; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:_add_gmaf; 232, argument_list; 232, 233; 232, 234; 233, identifier:variant; 234, identifier:gemini_variant; 235, comment; 236, if_statement; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:gemini_variant; 239, string:'cadd_scaled'; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:variant; 245, identifier:cadd_score; 246, subscript; 246, 247; 246, 248; 247, identifier:gemini_variant; 248, string:'cadd_scaled'; 249, comment; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:polyphen; 253, subscript; 253, 254; 253, 255; 254, identifier:gemini_variant; 255, string:'polyphen_pred'; 256, if_statement; 256, 257; 256, 258; 257, identifier:polyphen; 258, block; 258, 259; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:variant; 263, identifier:add_severity; 264, argument_list; 264, 265; 264, 266; 265, string:'Polyphen'; 266, identifier:polyphen; 267, comment; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:sift; 271, subscript; 271, 272; 271, 273; 272, identifier:gemini_variant; 273, string:'sift_pred'; 274, if_statement; 274, 275; 274, 276; 275, identifier:sift; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:variant; 281, identifier:add_severity; 282, argument_list; 282, 283; 282, 284; 283, string:'SIFT'; 284, identifier:sift; 285, comment; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:self; 290, identifier:_add_hgnc_symbols; 291, argument_list; 291, 292; 292, identifier:variant; 293, if_statement; 293, 294; 293, 299; 294, comparison_operator:==; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:self; 297, identifier:variant_type; 298, string:'snv'; 299, block; 299, 300; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:self; 304, identifier:_add_genes; 305, argument_list; 305, 306; 306, identifier:variant; 307, expression_statement; 307, 308; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:self; 311, identifier:_add_consequences; 312, argument_list; 312, 313; 313, identifier:variant; 314, comment; 315, comment; 316, if_statement; 316, 317; 316, 318; 317, identifier:add_all_info; 318, block; 318, 319; 318, 329; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:self; 323, identifier:_add_genotypes; 324, argument_list; 324, 325; 324, 326; 324, 327; 324, 328; 325, identifier:variant; 326, identifier:gemini_variant; 327, identifier:case_id; 328, identifier:individual_objs; 329, if_statement; 329, 330; 329, 335; 330, comparison_operator:==; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:self; 333, identifier:variant_type; 334, string:'sv'; 335, block; 335, 336; 336, expression_statement; 336, 337; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:self; 340, identifier:_add_genes; 341, argument_list; 341, 342; 342, identifier:variant; 343, return_statement; 343, 344; 344, identifier:variant
def _format_variant(self, case_id, gemini_variant, individual_objs, index=0, add_all_info=False): """Make a puzzle variant from a gemini variant Args: case_id (str): related case id gemini_variant (GeminiQueryRow): The gemini variant individual_objs (list(dict)): A list of Individuals index(int): The index of the variant Returns: variant (dict): A Variant object """ chrom = gemini_variant['chrom'] if chrom.startswith('chr') or chrom.startswith('CHR'): chrom = chrom[3:] variant_dict = { 'CHROM':chrom, 'POS':str(gemini_variant['start']), 'ID':gemini_variant['rs_ids'], 'REF':gemini_variant['ref'], 'ALT':gemini_variant['alt'], 'QUAL':gemini_variant['qual'], 'FILTER':gemini_variant['filter'] } variant = Variant(**variant_dict) # Use the gemini id for fast search variant.update_variant_id(gemini_variant['variant_id']) logger.debug("Creating a variant object of variant {0}".format( variant.variant_id)) variant['index'] = index # Add the most severe consequence self._add_most_severe_consequence(variant, gemini_variant) #Add the impact severity self._add_impact_severity(variant, gemini_variant) ### POSITON ANNOATTIONS ### variant.start = int(gemini_variant['start']) variant.stop = int(gemini_variant['end']) #Add the sv specific coordinates if self.variant_type == 'sv': variant.sv_type = gemini_variant['sub_type'] variant.stop = int(gemini_variant['end']) self._add_sv_coordinates(variant) else: ### Consequence and region annotations #Add the transcript information self._add_transcripts(variant, gemini_variant) self._add_thousand_g(variant, gemini_variant) self._add_exac(variant, gemini_variant) self._add_gmaf(variant, gemini_variant) #### Check the impact annotations #### if gemini_variant['cadd_scaled']: variant.cadd_score = gemini_variant['cadd_scaled'] # We use the prediction in text polyphen = gemini_variant['polyphen_pred'] if polyphen: variant.add_severity('Polyphen', polyphen) # We use the prediction in text sift = gemini_variant['sift_pred'] if sift: variant.add_severity('SIFT', sift) #Add the genes based on the hgnc symbols self._add_hgnc_symbols(variant) if self.variant_type == 'snv': self._add_genes(variant) self._add_consequences(variant) ### GENOTYPE ANNOATTIONS ### #Get the genotype info if add_all_info: self._add_genotypes(variant, gemini_variant, case_id, individual_objs) if self.variant_type == 'sv': self._add_genes(variant) return variant
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:arg; 3, parameters; 3, 4; 3, 6; 4, list_splat_pattern; 4, 5; 5, identifier:args; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 201; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:decorate; 13, parameters; 13, 14; 14, identifier:func; 15, block; 15, 16; 15, 18; 15, 19; 15, 20; 15, 39; 15, 40; 15, 50; 15, 71; 15, 153; 15, 199; 16, expression_statement; 16, 17; 17, comment; 18, comment; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:func; 24, identifier:__cmd_name__; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:kwargs; 28, identifier:pop; 29, argument_list; 29, 30; 29, 31; 30, string:'cmd_name'; 31, call; 31, 32; 31, 33; 32, identifier:getattr; 33, argument_list; 33, 34; 33, 35; 33, 36; 34, identifier:func; 35, string:'__cmd_name__'; 36, attribute; 36, 37; 36, 38; 37, identifier:func; 38, identifier:__name__; 39, comment; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:func; 44, identifier:__cls__; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:utils; 48, identifier:check_class; 49, argument_list; 50, if_statement; 50, 51; 50, 57; 50, 58; 50, 59; 51, not_operator; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:hasattr; 54, argument_list; 54, 55; 54, 56; 55, identifier:func; 56, string:'__arguments__'; 57, comment; 58, comment; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:func; 64, identifier:__arguments__; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:utils; 68, identifier:get_functarguments; 69, argument_list; 69, 70; 70, identifier:func; 71, if_statement; 71, 72; 71, 81; 71, 82; 71, 83; 71, 84; 72, boolean_operator:or; 72, 73; 72, 77; 73, call; 73, 74; 73, 75; 74, identifier:len; 75, argument_list; 75, 76; 76, identifier:args; 77, call; 77, 78; 77, 79; 78, identifier:len; 79, argument_list; 79, 80; 80, identifier:kwargs; 81, comment; 82, comment; 83, comment; 84, block; 84, 85; 84, 109; 84, 141; 84, 142; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:arg_name; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:kwargs; 91, identifier:get; 92, argument_list; 92, 93; 92, 94; 93, string:'dest'; 94, call; 94, 95; 94, 106; 95, attribute; 95, 96; 95, 105; 96, call; 96, 97; 96, 103; 97, attribute; 97, 98; 97, 102; 98, subscript; 98, 99; 98, 100; 99, identifier:args; 100, unary_operator:-; 100, 101; 101, integer:1; 102, identifier:lstrip; 103, argument_list; 103, 104; 104, string:'-'; 105, identifier:replace; 106, argument_list; 106, 107; 106, 108; 107, string:'-'; 108, string:'_'; 109, try_statement; 109, 110; 109, 111; 109, 137; 110, comment; 111, block; 111, 112; 111, 123; 111, 124; 111, 130; 111, 131; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:idx; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:func; 119, identifier:__named__; 120, identifier:index; 121, argument_list; 121, 122; 122, identifier:arg_name; 123, comment; 124, delete_statement; 124, 125; 125, subscript; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:func; 128, identifier:__named__; 129, identifier:idx; 130, comment; 131, delete_statement; 131, 132; 132, subscript; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:func; 135, identifier:__arguments__; 136, identifier:idx; 137, except_clause; 137, 138; 137, 139; 138, identifier:ValueError; 139, block; 139, 140; 140, pass_statement; 141, comment; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:func; 147, identifier:__arguments__; 148, identifier:append; 149, argument_list; 149, 150; 150, tuple; 150, 151; 150, 152; 151, identifier:args; 152, identifier:kwargs; 153, if_statement; 153, 154; 153, 167; 153, 168; 153, 169; 154, boolean_operator:and; 154, 155; 154, 160; 155, comparison_operator:is; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:func; 158, identifier:__cls__; 159, None; 160, call; 160, 161; 160, 162; 161, identifier:isinstance; 162, argument_list; 162, 163; 162, 164; 163, identifier:func; 164, attribute; 164, 165; 164, 166; 165, identifier:types; 166, identifier:FunctionType; 167, comment; 168, comment; 169, block; 169, 170; 169, 179; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:ap_; 173, call; 173, 174; 173, 175; 174, identifier:ArgParseInator; 175, argument_list; 175, 176; 176, keyword_argument; 176, 177; 176, 178; 177, identifier:skip_init; 178, True; 179, if_statement; 179, 180; 179, 187; 179, 188; 180, comparison_operator:not; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:func; 183, identifier:__cmd_name__; 184, attribute; 184, 185; 184, 186; 185, identifier:ap_; 186, identifier:commands; 187, comment; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 198; 191, subscript; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:ap_; 194, identifier:commands; 195, attribute; 195, 196; 195, 197; 196, identifier:func; 197, identifier:__cmd_name__; 198, identifier:func; 199, return_statement; 199, 200; 200, identifier:func; 201, return_statement; 201, 202; 202, identifier:decorate
def arg(*args, **kwargs): """ Dcorates a function or a class method to add to the argument parser """ def decorate(func): """ Decorate """ # we'll set the command name with the passed cmd_name argument, if # exist, else the command name will be the function name func.__cmd_name__ = kwargs.pop( 'cmd_name', getattr(func, '__cmd_name__', func.__name__)) # retrieve the class (SillyClass) func.__cls__ = utils.check_class() if not hasattr(func, '__arguments__'): # if the funcion hasn't the __arguments__ yet, we'll setup them # using get_functarguments. func.__arguments__ = utils.get_functarguments(func) if len(args) or len(kwargs): # if we have some argument or keyword argument # we'll try to get the destination name from the kwargs ('dest') # else we'll use the last arg name as destination arg_name = kwargs.get( 'dest', args[-1].lstrip('-').replace('-', '_')) try: # we try to get the command index. idx = func.__named__.index(arg_name) # and delete it from the named list del func.__named__[idx] # and delete it from the arguments list del func.__arguments__[idx] except ValueError: pass # append the args and kwargs to the function arguments list func.__arguments__.append((args, kwargs,)) if func.__cls__ is None and isinstance(func, types.FunctionType): # if the function don't have a class and is a FunctionType # we'll add it directly to he commands list. ap_ = ArgParseInator(skip_init=True) if func.__cmd_name__ not in ap_.commands: # we'll add it if not exists ap_.commands[func.__cmd_name__] = func return func return decorate
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:class_args; 3, parameters; 3, 4; 4, identifier:cls; 5, block; 5, 6; 5, 8; 5, 9; 5, 18; 5, 19; 5, 27; 5, 28; 5, 34; 5, 38; 5, 39; 5, 50; 5, 51; 5, 102; 5, 167; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:ap_; 12, call; 12, 13; 12, 14; 13, identifier:ArgParseInator; 14, argument_list; 14, 15; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:skip_init; 17, True; 18, comment; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:utils; 23, identifier:collect_appendvars; 24, argument_list; 24, 25; 24, 26; 25, identifier:ap_; 26, identifier:cls; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:cls; 32, identifier:__cls__; 33, identifier:cls; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:cmds; 37, dictionary; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:cls; 43, identifier:__arguments__; 44, call; 44, 45; 44, 46; 45, identifier:getattr; 46, argument_list; 46, 47; 46, 48; 46, 49; 47, identifier:cls; 48, string:'__arguments__'; 49, list:[]; 50, comment; 51, for_statement; 51, 52; 51, 53; 51, 78; 51, 79; 52, identifier:func; 53, list_comprehension; 53, 54; 53, 55; 53, 64; 54, identifier:f; 55, for_in_clause; 55, 56; 55, 57; 56, identifier:f; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:cls; 61, identifier:__dict__; 62, identifier:values; 63, argument_list; 64, if_clause; 64, 65; 65, boolean_operator:and; 65, 66; 65, 71; 66, call; 66, 67; 66, 68; 67, identifier:hasattr; 68, argument_list; 68, 69; 68, 70; 69, identifier:f; 70, string:'__cmd_name__'; 71, not_operator; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:inspect; 75, identifier:isclass; 76, argument_list; 76, 77; 77, identifier:f; 78, comment; 79, block; 79, 80; 79, 86; 79, 87; 79, 93; 79, 94; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:func; 84, identifier:__subcommands__; 85, None; 86, comment; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:func; 91, identifier:__cls__; 92, identifier:cls; 93, comment; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 101; 96, subscript; 96, 97; 96, 98; 97, identifier:cmds; 98, attribute; 98, 99; 98, 100; 99, identifier:func; 100, identifier:__cmd_name__; 101, identifier:func; 102, if_statement; 102, 103; 102, 116; 102, 117; 102, 118; 102, 119; 102, 137; 103, boolean_operator:and; 103, 104; 103, 109; 104, call; 104, 105; 104, 106; 105, identifier:hasattr; 106, argument_list; 106, 107; 106, 108; 107, identifier:cls; 108, string:'__cmd_name__'; 109, comparison_operator:not; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:cls; 112, identifier:__cmd_name__; 113, attribute; 113, 114; 113, 115; 114, identifier:ap_; 115, identifier:commands; 116, comment; 117, comment; 118, comment; 119, block; 119, 120; 119, 126; 119, 127; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:cls; 124, identifier:__subcommands__; 125, identifier:cmds; 126, comment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 136; 129, subscript; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:ap_; 132, identifier:commands; 133, attribute; 133, 134; 133, 135; 134, identifier:cls; 135, identifier:__cmd_name__; 136, identifier:cls; 137, else_clause; 137, 138; 137, 139; 137, 140; 137, 141; 138, comment; 139, comment; 140, comment; 141, block; 141, 142; 142, for_statement; 142, 143; 142, 146; 142, 151; 143, pattern_list; 143, 144; 143, 145; 144, identifier:name; 145, identifier:func; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:cmds; 149, identifier:items; 150, argument_list; 151, block; 151, 152; 152, if_statement; 152, 153; 152, 158; 153, comparison_operator:not; 153, 154; 153, 155; 154, identifier:name; 155, attribute; 155, 156; 155, 157; 156, identifier:ap_; 157, identifier:commands; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 166; 161, subscript; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:ap_; 164, identifier:commands; 165, identifier:name; 166, identifier:func; 167, return_statement; 167, 168; 168, identifier:cls
def class_args(cls): """ Decorates a class to handle the arguments parser. """ # get the Singleton ap_ = ArgParseInator(skip_init=True) # collect special vars (really need?) utils.collect_appendvars(ap_, cls) # set class reference cls.__cls__ = cls cmds = {} # get eventual class arguments cls.__arguments__ = getattr(cls, '__arguments__', []) # cycle through class functions for func in [f for f in cls.__dict__.values() if hasattr(f, '__cmd_name__') and not inspect.isclass(f)]: # clear subcommands func.__subcommands__ = None # set the parent class func.__cls__ = cls # assign to commands dict cmds[func.__cmd_name__] = func if hasattr(cls, '__cmd_name__') and cls.__cmd_name__ not in ap_.commands: # if che class has the __cmd_name__ attribute and is not already present # in the ArgParseInator commands # set the class subcommands cls.__subcommands__ = cmds # add the class as ArgParseInator command ap_.commands[cls.__cmd_name__] = cls else: # else if we don't have a __cmd_name__ # we will add all the functions directly to the ArgParseInator commands # if it don't already exists. for name, func in cmds.items(): if name not in ap_.commands: ap_.commands[name] = func return cls
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_args; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 15; 5, 16; 5, 22; 5, 40; 5, 41; 5, 63; 5, 107; 5, 108; 5, 120; 5, 121; 5, 243; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_compile; 14, argument_list; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:args; 21, None; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:_self_event; 27, argument_list; 27, 28; 27, 29; 27, 30; 27, 38; 28, string:'before_parse'; 29, string:'parse'; 30, list_splat; 30, 31; 31, subscript; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:sys; 34, identifier:argv; 35, slice; 35, 36; 35, 37; 36, integer:1; 37, colon; 38, dictionary_splat; 38, 39; 39, dictionary; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:cmds; 44, list_comprehension; 44, 45; 44, 46; 44, 55; 45, identifier:cmd; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:cmd; 48, subscript; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:sys; 51, identifier:argv; 52, slice; 52, 53; 52, 54; 53, integer:1; 54, colon; 55, if_clause; 55, 56; 56, not_operator; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:cmd; 60, identifier:startswith; 61, argument_list; 61, 62; 62, string:"-"; 63, if_statement; 63, 64; 63, 90; 63, 91; 63, 92; 63, 93; 63, 94; 64, parenthesized_expression; 64, 65; 65, boolean_operator:and; 65, 66; 65, 83; 66, boolean_operator:and; 66, 67; 66, 80; 67, boolean_operator:and; 67, 68; 67, 74; 68, comparison_operator:>; 68, 69; 68, 73; 69, call; 69, 70; 69, 71; 70, identifier:len; 71, argument_list; 71, 72; 72, identifier:cmds; 73, integer:0; 74, not_operator; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:utils; 78, identifier:check_help; 79, argument_list; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:default_cmd; 83, comparison_operator:not; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:cmds; 86, integer:0; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:commands; 90, comment; 91, comment; 92, comment; 93, comment; 94, block; 94, 95; 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:sys; 100, identifier:argv; 101, identifier:insert; 102, argument_list; 102, 103; 102, 104; 103, integer:1; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:default_cmd; 107, comment; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:args; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:parser; 118, identifier:parse_args; 119, argument_list; 120, comment; 121, if_statement; 121, 122; 121, 125; 121, 126; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:args; 125, comment; 126, block; 126, 127; 126, 219; 126, 236; 126, 237; 127, if_statement; 127, 128; 127, 139; 127, 140; 127, 141; 128, boolean_operator:and; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:add_output; 132, comparison_operator:is; 132, 133; 132, 138; 133, attribute; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:args; 137, identifier:output; 138, None; 139, comment; 140, comment; 141, block; 141, 142; 141, 152; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:encoding; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:args; 151, identifier:encoding; 152, if_statement; 152, 153; 152, 164; 152, 165; 152, 166; 152, 185; 153, comparison_operator:==; 153, 154; 153, 163; 154, call; 154, 155; 154, 162; 155, attribute; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:args; 160, identifier:encoding; 161, identifier:lower; 162, argument_list; 163, string:'raw'; 164, comment; 165, comment; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:_output; 172, call; 172, 173; 172, 174; 173, identifier:open; 174, argument_list; 174, 175; 174, 180; 175, attribute; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:args; 179, identifier:output; 180, attribute; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:args; 184, identifier:write_mode; 185, else_clause; 185, 186; 185, 187; 185, 188; 186, comment; 187, comment; 188, block; 188, 189; 188, 192; 189, import_statement; 189, 190; 190, dotted_name; 190, 191; 191, identifier:codecs; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:_output; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:codecs; 200, identifier:open; 201, argument_list; 201, 202; 201, 207; 201, 212; 202, attribute; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:args; 206, identifier:output; 207, attribute; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:args; 211, identifier:write_mode; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:encoding; 214, attribute; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:args; 218, identifier:encoding; 219, if_statement; 219, 220; 219, 223; 219, 224; 219, 225; 220, attribute; 220, 221; 220, 222; 221, identifier:self; 222, identifier:_cfg_factory; 223, comment; 224, comment; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:cfg_file; 231, attribute; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:self; 234, identifier:args; 235, identifier:config; 236, comment; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:self; 241, identifier:_is_parsed; 242, True; 243, return_statement; 243, 244; 244, identifier:self
def parse_args(self): """ Parse our arguments. """ # compile the parser self._compile() # clear the args self.args = None self._self_event('before_parse', 'parse', *sys.argv[1:], **{}) # list commands/subcommands in argv cmds = [cmd for cmd in sys.argv[1:] if not cmd.startswith("-")] if (len(cmds) > 0 and not utils.check_help() and self.default_cmd and cmds[0] not in self.commands): # if we have at least one command which is not an help command # and we have a default command and the first command in arguments # is not in commands we insert the default command as second # argument (actually the first command) sys.argv.insert(1, self.default_cmd) # let's parse the arguments self.args = self.parser.parse_args() # set up the output. if self.args: # if we have some arguments if self.add_output and self.args.output is not None: # If add_output is True and we have an output file # setup the encoding self.encoding = self.args.encoding if self.args.encoding.lower() == 'raw': # if we have passed a raw encoding we will write directly # to the output file. self._output = open(self.args.output, self.args.write_mode) else: # else we will use the codecs module to write to the # output file. import codecs self._output = codecs.open( self.args.output, self.args.write_mode, encoding=self.args.encoding) if self._cfg_factory: # if we have a config factory setup the config file with the # right param self.cfg_file = self.args.config # now is parsed. self._is_parsed = True return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:check_command; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, dictionary_splat_pattern; 5, 6; 6, identifier:new_attributes; 7, block; 7, 8; 7, 10; 7, 11; 7, 23; 7, 78; 7, 109; 7, 110; 7, 118; 7, 119; 7, 133; 7, 134; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, if_statement; 11, 12; 11, 16; 12, not_operator; 12, 13; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_is_parsed; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:parse_args; 22, argument_list; 23, if_statement; 23, 24; 23, 28; 23, 29; 23, 34; 23, 46; 24, not_operator; 24, 25; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:commands; 28, comment; 29, block; 29, 30; 30, raise_statement; 30, 31; 31, attribute; 31, 32; 31, 33; 32, identifier:exceptions; 33, identifier:ArgParseInatorNoCommandsFound; 34, elif_clause; 34, 35; 34, 38; 34, 39; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_single; 38, comment; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:func; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:_single; 46, else_clause; 46, 47; 47, block; 47, 48; 47, 65; 47, 66; 48, if_statement; 48, 49; 48, 55; 49, not_operator; 49, 50; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:args; 54, identifier:command; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:parser; 62, identifier:error; 63, argument_list; 63, 64; 64, string:"too few arguments"; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:func; 69, subscript; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:commands; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:args; 77, identifier:command; 78, if_statement; 78, 79; 78, 88; 78, 89; 78, 102; 79, boolean_operator:and; 79, 80; 79, 85; 80, call; 80, 81; 80, 82; 81, identifier:hasattr; 82, argument_list; 82, 83; 82, 84; 83, identifier:func; 84, string:'__subcommands__'; 85, attribute; 85, 86; 85, 87; 86, identifier:func; 87, identifier:__subcommands__; 88, comment; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:command; 93, subscript; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:func; 96, identifier:__subcommands__; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:args; 101, identifier:subcommand; 102, else_clause; 102, 103; 102, 104; 103, comment; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:command; 108, identifier:func; 109, comment; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:cmd_name; 115, attribute; 115, 116; 115, 117; 116, identifier:command; 117, identifier:__cmd_name__; 118, comment; 119, if_statement; 119, 120; 119, 130; 120, not_operator; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:check_auth; 125, argument_list; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:id; 128, argument_list; 128, 129; 129, identifier:command; 130, block; 130, 131; 131, return_statement; 131, 132; 132, integer:0; 133, comment; 134, return_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:_execute; 139, argument_list; 139, 140; 139, 141; 139, 142; 140, identifier:func; 141, identifier:command; 142, dictionary_splat; 142, 143; 143, identifier:new_attributes
def check_command(self, **new_attributes): """ Check if 'was passed a valid action in the command line and if so, executes it by passing parameters and returning the result. :return: (Any) Return the result of the called function, if provided, or None. """ # let's parse arguments if we didn't before. if not self._is_parsed: self.parse_args() if not self.commands: # if we don't have commands raise an Exception raise exceptions.ArgParseInatorNoCommandsFound elif self._single: # if we have a single function we get it directly func = self._single else: if not self.args.command: self.parser.error("too few arguments") # get the right command func = self.commands[self.args.command] if hasattr(func, '__subcommands__') and func.__subcommands__: # if we have subcommands get the command from them command = func.__subcommands__[self.args.subcommand] else: # else the command IS the function command = func # get the command name self.cmd_name = command.__cmd_name__ # check authorization if not self.check_auth(id(command)): return 0 # let's execute the command. return self._execute(func, command, **new_attributes)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:merge; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 27; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:merged_root; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:_recursive_merge; 17, argument_list; 17, 18; 17, 21; 17, 24; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:root; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:head; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:update; 27, if_statement; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:conflicts; 31, block; 31, 32; 32, raise_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:MergeError; 35, argument_list; 35, 36; 35, 37; 36, string:'Conflicts Occurred in Merge Process'; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:conflicts
def merge(self): """Populates result members. Performs the merge algorithm using the specified config and fills in the members that provide stats about the merging procedure. Attributes: merged_root: The result of the merge. aligned_root, aligned_head, aligned_update: Copies of root, head and update in which all matched list entities have the same list index for easier diff viewing. head_stats, update_stats: Stats for each list field present in the head or update objects. Instance of :class:`json_merger.stats.ListMatchStats` conflicts: List of :class:`json_merger.conflict.Conflict` instances that occured during the merge. Raises: :class:`json_merger.errors.MergeError` : If conflicts occur during the call. Example: >>> from json_merger import Merger >>> # We compare people by their name >>> from json_merger.comparator import PrimaryKeyComparator >>> from json_merger.config import DictMergerOps, UnifierOps >>> from json_merger.errors import MergeError >>> # Use this only for doctest :) >>> from pprint import pprint as pp >>> >>> root = {'people': [{'name': 'Jimmy', 'age': 30}]} >>> head = {'people': [{'name': 'Jimmy', 'age': 31}, ... {'name': 'George'}]} >>> update = {'people': [{'name': 'John'}, ... {'name': 'Jimmy', 'age': 32}]} >>> >>> class NameComparator(PrimaryKeyComparator): ... # Two objects are the same entitity if they have the ... # same name. ... primary_key_fields = ['name'] >>> m = Merger(root, head, update, ... DictMergerOps.FALLBACK_KEEP_HEAD, ... UnifierOps.KEEP_UPDATE_AND_HEAD_ENTITIES_HEAD_FIRST, ... comparators = {'people': NameComparator}) >>> # We do a merge >>> try: ... m.merge() ... except MergeError as e: ... # Conflicts are the same thing as the exception content. ... assert e.content == m.conflicts >>> # This is how the lists are aligned: >>> pp(m.aligned_root['people'], width=60) ['#$PLACEHOLDER$#', {'age': 30, 'name': 'Jimmy'}, '#$PLACEHOLDER$#'] >>> pp(m.aligned_head['people'], width=60) ['#$PLACEHOLDER$#', {'age': 31, 'name': 'Jimmy'}, {'name': 'George'}] >>> pp(m.aligned_update['people'], width=60) [{'name': 'John'}, {'age': 32, 'name': 'Jimmy'}, '#$PLACEHOLDER$#'] >>> # This is the end result of the merge: >>> pp(m.merged_root, width=60) {'people': [{'name': 'John'}, {'age': 31, 'name': 'Jimmy'}, {'name': 'George'}]} >>> # With some conflicts: >>> pp(m.conflicts, width=60) [('SET_FIELD', ('people', 1, 'age'), 32)] >>> # And some stats: >>> pp(m.head_stats[('people',)].in_result) [{'age': 31, 'name': 'Jimmy'}, {'name': 'George'}] >>> pp(m.update_stats[('people',)].not_in_result) [] Note: Even if conflicts occur, merged_root, aligned_root, aligned_head and aligned_update are always populated by following the startegies set for the merger instance. """ self.merged_root = self._recursive_merge(self.root, self.head, self.update) if self.conflicts: raise MergeError('Conflicts Occurred in Merge Process', self.conflicts)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_zadd; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:key; 6, identifier:pk; 7, default_parameter; 7, 8; 7, 9; 8, identifier:ts; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:ttl; 12, None; 13, block; 13, 14; 13, 16; 14, expression_statement; 14, 15; 15, comment; 16, return_statement; 16, 17; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:r; 22, identifier:eval; 23, argument_list; 23, 24; 23, 27; 23, 28; 23, 29; 23, 36; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:LUA_ZADD; 27, integer:1; 28, identifier:key; 29, boolean_operator:or; 29, 30; 29, 31; 30, identifier:ts; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_time; 35, argument_list; 36, identifier:pk
def _zadd(self, key, pk, ts=None, ttl=None): """Redis lua func to add an event to the corresponding sorted set. :param key: the key to be stored in redis server :param pk: the primary key of event :param ts: timestamp of the event, default to redis_server's current timestamp :param ttl: the expiration time of event since the last update """ return self.r.eval(self.LUA_ZADD, 1, key, ts or self._time(), pk)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:add; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:event; 6, identifier:pk; 7, default_parameter; 7, 8; 7, 9; 8, identifier:ts; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:ttl; 12, None; 13, block; 13, 14; 13, 16; 13, 26; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:key; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:_keygen; 23, argument_list; 23, 24; 23, 25; 24, identifier:event; 25, identifier:ts; 26, try_statement; 26, 27; 26, 40; 27, block; 27, 28; 27, 38; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_zadd; 33, argument_list; 33, 34; 33, 35; 33, 36; 33, 37; 34, identifier:key; 35, identifier:pk; 36, identifier:ts; 37, identifier:ttl; 38, return_statement; 38, 39; 39, True; 40, except_clause; 40, 41; 40, 47; 40, 48; 40, 49; 40, 50; 41, as_pattern; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:redis; 44, identifier:ConnectionError; 45, as_pattern_target; 45, 46; 46, identifier:e; 47, comment; 48, comment; 49, comment; 50, block; 50, 51; 50, 62; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:logger; 57, identifier:error; 58, argument_list; 58, 59; 59, binary_operator:%; 59, 60; 59, 61; 60, string:"redis event store failed with connection error %r"; 61, identifier:e; 62, return_statement; 62, 63; 63, False
def add(self, event, pk, ts=None, ttl=None): """Add an event to event store. All events were stored in a sorted set in redis with timestamp as rank score. :param event: the event to be added, format should be ``table_action`` :param pk: the primary key of event :param ts: timestamp of the event, default to redis_server's current timestamp :param ttl: the expiration time of event since the last update :return: bool """ key = self._keygen(event, ts) try: self._zadd(key, pk, ts, ttl) return True except redis.ConnectionError as e: # connection error typically happens when redis server can't be # reached or timed out, the error will be silent with an error # log and return None. self.logger.error( "redis event store failed with connection error %r" % e) return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:add_parameter; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kwargs; 7, block; 7, 8; 7, 10; 7, 27; 7, 38; 7, 48; 7, 57; 7, 79; 7, 87; 7, 113; 7, 126; 7, 136; 7, 152; 7, 179; 7, 216; 7, 224; 7, 252; 7, 287; 7, 303; 7, 316; 8, expression_statement; 8, 9; 9, string:'''Add the parameter to ``Parameters``. **Arguments** The arguments are lumped into two groups:``Parameters.add_parameter`` and ``argparse.ArgumentParser.add_argument``. Parameters that are only used by ``Parameters.add_parameter`` are removed before ``kwargs`` is passed directly to argparse.ArgumentParser.add_argument``. .. note:: Once ``parse`` has been called ``Parameters.parsed`` will be True and it is inadvisable to add more parameters to the ``Parameters``. *``Parameters.add_parameter`` Arguments* :``environment_prefix``: Prefix to add when searching the environment for this parameter. Default: os.path.basename(sys.argv[0]). :``group``: Group (namespace or prefix) for parameter (corresponds to section name in configuration files). Default: 'default'. :``options``: REQUIRED. The list of options to match for this parameter in argv. :``only``: Iterable containing the components that this parameter applies to (i.e. 'environment', 'configuration', 'argument'). Default: ('environment', 'configuration', 'argument'). *``argparse.ArgumentParser.add_argument`` Arguments* :``name or flags``: Positional argument filled in by options keyword argument. :``action``: The basic type of action to be taken when this argument is encountered at the command line. :``nargs``: The number of command-line arguments that should be consumed. :``const``: A constant value required by some action and nargs selections. :``default``: The value produced if the argument is absent from the command line. :``type``: The type to which the command-line argument should be converted. :``choices``: A container of the allowable values for the argument. :``required``: Whether or not the command-line option may be omitted (optionals only). :``help``: A brief description of what the argument does. :``metavar``: A name for the argument in usage messages. :``dest``: The name of the attribute to be added to the object returned by parse_args(). '''; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:parameter_name; 13, call; 13, 14; 13, 25; 14, attribute; 14, 15; 14, 24; 15, call; 15, 16; 15, 17; 16, identifier:max; 17, argument_list; 17, 18; 17, 21; 18, subscript; 18, 19; 18, 20; 19, identifier:kwargs; 20, string:'options'; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:key; 23, identifier:len; 24, identifier:lstrip; 25, argument_list; 25, 26; 26, string:'-'; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:in; 28, 29; 28, 30; 29, string:'dest'; 30, identifier:kwargs; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:parameter_name; 35, subscript; 35, 36; 35, 37; 36, identifier:kwargs; 37, string:'dest'; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:group; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:kwargs; 44, identifier:pop; 45, argument_list; 45, 46; 45, 47; 46, string:'group'; 47, string:'default'; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:groups; 54, identifier:add; 55, argument_list; 55, 56; 56, identifier:group; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:parameter_name; 60, call; 60, 61; 60, 76; 61, attribute; 61, 62; 61, 75; 62, call; 62, 63; 62, 73; 63, attribute; 63, 64; 63, 72; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, string:'.'; 67, identifier:join; 68, argument_list; 68, 69; 69, list:[ group, parameter_name ]; 69, 70; 69, 71; 70, identifier:group; 71, identifier:parameter_name; 72, identifier:lstrip; 73, argument_list; 73, 74; 74, string:'.'; 75, identifier:replace; 76, argument_list; 76, 77; 76, 78; 77, string:'-'; 78, string:'_'; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:logger; 83, identifier:info; 84, argument_list; 84, 85; 84, 86; 85, string:'adding parameter %s'; 86, identifier:parameter_name; 87, if_statement; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:parsed; 91, block; 91, 92; 91, 100; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:logger; 96, identifier:warn; 97, argument_list; 97, 98; 97, 99; 98, string:'adding parameter %s after parse'; 99, identifier:parameter_name; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:warnings; 104, identifier:warn; 105, argument_list; 105, 106; 105, 112; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, string:'adding parameter {} after parse'; 109, identifier:format; 110, argument_list; 110, 111; 111, identifier:parameter_name; 112, identifier:RuntimeWarning; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:parameters; 119, identifier:parameter_name; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:copy; 123, identifier:copy; 124, argument_list; 124, 125; 125, identifier:kwargs; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 135; 128, subscript; 128, 129; 128, 134; 129, subscript; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:parameters; 133, identifier:parameter_name; 134, string:'group'; 135, identifier:group; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 145; 138, subscript; 138, 139; 138, 144; 139, subscript; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:parameters; 143, identifier:parameter_name; 144, string:'type'; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:kwargs; 148, identifier:get; 149, argument_list; 149, 150; 149, 151; 150, string:'type'; 151, identifier:str; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 161; 154, subscript; 154, 155; 154, 160; 155, subscript; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:parameters; 159, identifier:parameter_name; 160, string:'environment_prefix'; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:kwargs; 164, identifier:pop; 165, argument_list; 165, 166; 165, 167; 166, string:'environment_prefix'; 167, call; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:os; 171, identifier:path; 172, identifier:basename; 173, argument_list; 173, 174; 174, subscript; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:sys; 177, identifier:argv; 178, integer:0; 179, if_statement; 179, 180; 179, 189; 180, comparison_operator:is; 180, 181; 180, 188; 181, subscript; 181, 182; 181, 187; 182, subscript; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:parameters; 186, identifier:parameter_name; 187, string:'environment_prefix'; 188, None; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 199; 192, subscript; 192, 193; 192, 198; 193, subscript; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:parameters; 197, identifier:parameter_name; 198, string:'environment_prefix'; 199, call; 199, 200; 199, 213; 200, attribute; 200, 201; 200, 212; 201, call; 201, 202; 201, 211; 202, attribute; 202, 203; 202, 210; 203, subscript; 203, 204; 203, 209; 204, subscript; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:parameters; 208, identifier:parameter_name; 209, string:'environment_prefix'; 210, identifier:upper; 211, argument_list; 212, identifier:replace; 213, argument_list; 213, 214; 213, 215; 214, string:'-'; 215, string:'_'; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:logger; 220, identifier:info; 221, argument_list; 221, 222; 221, 223; 222, string:'group: %s'; 223, identifier:group; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 237; 226, attribute; 226, 227; 226, 236; 227, call; 227, 228; 227, 233; 228, attribute; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:grouped_parameters; 232, identifier:setdefault; 233, argument_list; 233, 234; 233, 235; 234, identifier:group; 235, dictionary; 236, identifier:setdefault; 237, argument_list; 237, 238; 237, 247; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:parameter_name; 241, identifier:replace; 242, argument_list; 242, 243; 242, 246; 243, binary_operator:+; 243, 244; 243, 245; 244, identifier:group; 245, string:'.'; 246, string:''; 247, subscript; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:self; 250, identifier:parameters; 251, identifier:parameter_name; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:action_defaults; 255, dictionary; 255, 256; 255, 264; 255, 272; 255, 275; 255, 278; 255, 281; 255, 284; 256, pair; 256, 257; 256, 258; 257, string:'store'; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:kwargs; 261, identifier:get; 262, argument_list; 262, 263; 263, string:'default'; 264, pair; 264, 265; 264, 266; 265, string:'store_const'; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:kwargs; 269, identifier:get; 270, argument_list; 270, 271; 271, string:'const'; 272, pair; 272, 273; 272, 274; 273, string:'store_true'; 274, False; 275, pair; 275, 276; 275, 277; 276, string:'store_false'; 277, True; 278, pair; 278, 279; 278, 280; 279, string:'append'; 280, list:[]; 281, pair; 281, 282; 281, 283; 282, string:'append_const'; 283, list:[]; 284, pair; 284, 285; 284, 286; 285, string:'count'; 286, integer:0; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 294; 289, subscript; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:defaults; 293, identifier:parameter_name; 294, subscript; 294, 295; 294, 296; 295, identifier:action_defaults; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:kwargs; 299, identifier:get; 300, argument_list; 300, 301; 300, 302; 301, string:'action'; 302, string:'store'; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:logger; 307, identifier:info; 308, argument_list; 308, 309; 308, 310; 309, string:'default value: %s'; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:kwargs; 313, identifier:get; 314, argument_list; 314, 315; 315, string:'default'; 316, if_statement; 316, 317; 316, 327; 317, comparison_operator:in; 317, 318; 317, 319; 318, string:'argument'; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:kwargs; 322, identifier:pop; 323, argument_list; 323, 324; 323, 325; 324, string:'only'; 325, list:[ 'argument' ]; 325, 326; 326, string:'argument'; 327, block; 327, 328; 327, 352; 327, 417; 328, if_statement; 328, 329; 328, 334; 329, comparison_operator:not; 329, 330; 329, 331; 330, identifier:group; 331, attribute; 331, 332; 331, 333; 332, identifier:self; 333, identifier:_group_parsers; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 342; 337, subscript; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:self; 340, identifier:_group_parsers; 341, identifier:group; 342, call; 342, 343; 342, 350; 343, attribute; 343, 344; 343, 349; 344, subscript; 344, 345; 344, 348; 345, attribute; 345, 346; 345, 347; 346, identifier:self; 347, identifier:_group_parsers; 348, string:'default'; 349, identifier:add_argument_group; 350, argument_list; 350, 351; 351, identifier:group; 352, if_statement; 352, 353; 352, 360; 353, boolean_operator:and; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:self; 356, identifier:_group_prefix; 357, comparison_operator:!=; 357, 358; 357, 359; 358, identifier:group; 359, string:'default'; 360, block; 360, 361; 360, 373; 360, 382; 360, 407; 361, expression_statement; 361, 362; 362, assignment; 362, 363; 362, 364; 363, identifier:long_option; 364, call; 364, 365; 364, 366; 365, identifier:max; 366, argument_list; 366, 367; 366, 370; 367, subscript; 367, 368; 367, 369; 368, identifier:kwargs; 369, string:'options'; 370, keyword_argument; 370, 371; 370, 372; 371, identifier:key; 372, identifier:len; 373, expression_statement; 373, 374; 374, call; 374, 375; 374, 380; 375, attribute; 375, 376; 375, 379; 376, subscript; 376, 377; 376, 378; 377, identifier:kwargs; 378, string:'options'; 379, identifier:remove; 380, argument_list; 380, 381; 381, identifier:long_option; 382, expression_statement; 382, 383; 383, call; 383, 384; 383, 389; 384, attribute; 384, 385; 384, 388; 385, subscript; 385, 386; 385, 387; 386, identifier:kwargs; 387, string:'options'; 388, identifier:append; 389, argument_list; 389, 390; 390, call; 390, 391; 390, 394; 391, attribute; 391, 392; 391, 393; 392, identifier:long_option; 393, identifier:replace; 394, argument_list; 394, 395; 394, 396; 395, string:'--'; 396, binary_operator:+; 396, 397; 396, 406; 397, binary_operator:+; 397, 398; 397, 399; 398, string:'--'; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:group; 402, identifier:replace; 403, argument_list; 403, 404; 403, 405; 404, string:'_'; 405, string:'-'; 406, string:'-'; 407, expression_statement; 407, 408; 408, call; 408, 409; 408, 412; 409, attribute; 409, 410; 409, 411; 410, identifier:logger; 411, identifier:debug; 412, argument_list; 412, 413; 412, 414; 413, string:'options: %s'; 414, subscript; 414, 415; 414, 416; 415, identifier:kwargs; 416, string:'options'; 417, expression_statement; 417, 418; 418, call; 418, 419; 418, 426; 419, attribute; 419, 420; 419, 425; 420, subscript; 420, 421; 420, 424; 421, attribute; 421, 422; 421, 423; 422, identifier:self; 423, identifier:_group_parsers; 424, identifier:group; 425, identifier:add_argument; 426, argument_list; 426, 427; 426, 434; 427, list_splat; 427, 428; 428, call; 428, 429; 428, 432; 429, attribute; 429, 430; 429, 431; 430, identifier:kwargs; 431, identifier:pop; 432, argument_list; 432, 433; 433, string:'options'; 434, dictionary_splat; 434, 435; 435, identifier:kwargs
def add_parameter(self, **kwargs): '''Add the parameter to ``Parameters``. **Arguments** The arguments are lumped into two groups:``Parameters.add_parameter`` and ``argparse.ArgumentParser.add_argument``. Parameters that are only used by ``Parameters.add_parameter`` are removed before ``kwargs`` is passed directly to argparse.ArgumentParser.add_argument``. .. note:: Once ``parse`` has been called ``Parameters.parsed`` will be True and it is inadvisable to add more parameters to the ``Parameters``. *``Parameters.add_parameter`` Arguments* :``environment_prefix``: Prefix to add when searching the environment for this parameter. Default: os.path.basename(sys.argv[0]). :``group``: Group (namespace or prefix) for parameter (corresponds to section name in configuration files). Default: 'default'. :``options``: REQUIRED. The list of options to match for this parameter in argv. :``only``: Iterable containing the components that this parameter applies to (i.e. 'environment', 'configuration', 'argument'). Default: ('environment', 'configuration', 'argument'). *``argparse.ArgumentParser.add_argument`` Arguments* :``name or flags``: Positional argument filled in by options keyword argument. :``action``: The basic type of action to be taken when this argument is encountered at the command line. :``nargs``: The number of command-line arguments that should be consumed. :``const``: A constant value required by some action and nargs selections. :``default``: The value produced if the argument is absent from the command line. :``type``: The type to which the command-line argument should be converted. :``choices``: A container of the allowable values for the argument. :``required``: Whether or not the command-line option may be omitted (optionals only). :``help``: A brief description of what the argument does. :``metavar``: A name for the argument in usage messages. :``dest``: The name of the attribute to be added to the object returned by parse_args(). ''' parameter_name = max(kwargs['options'], key = len).lstrip('-') if 'dest' in kwargs: parameter_name = kwargs['dest'] group = kwargs.pop('group', 'default') self.groups.add(group) parameter_name = '.'.join([ group, parameter_name ]).lstrip('.').replace('-', '_') logger.info('adding parameter %s', parameter_name) if self.parsed: logger.warn('adding parameter %s after parse', parameter_name) warnings.warn('adding parameter {} after parse'.format(parameter_name), RuntimeWarning) self.parameters[parameter_name] = copy.copy(kwargs) self.parameters[parameter_name]['group'] = group self.parameters[parameter_name]['type'] = kwargs.get('type', str) self.parameters[parameter_name]['environment_prefix'] = kwargs.pop('environment_prefix', os.path.basename(sys.argv[0])) if self.parameters[parameter_name]['environment_prefix'] is not None: self.parameters[parameter_name]['environment_prefix'] = self.parameters[parameter_name]['environment_prefix'].upper().replace('-', '_') logger.info('group: %s', group) self.grouped_parameters.setdefault(group, {}).setdefault(parameter_name.replace(group + '.', ''), self.parameters[parameter_name]) action_defaults = { 'store': kwargs.get('default'), 'store_const': kwargs.get('const'), 'store_true': False, 'store_false': True, 'append': [], 'append_const': [], 'count': 0, } self.defaults[parameter_name] = action_defaults[kwargs.get('action', 'store')] logger.info('default value: %s', kwargs.get('default')) if 'argument' in kwargs.pop('only', [ 'argument' ]): if group not in self._group_parsers: self._group_parsers[group] = self._group_parsers['default'].add_argument_group(group) if self._group_prefix and group != 'default': long_option = max(kwargs['options'], key = len) kwargs['options'].remove(long_option) kwargs['options'].append(long_option.replace('--', '--' + group.replace('_', '-') + '-')) logger.debug('options: %s', kwargs['options']) self._group_parsers[group].add_argument(*kwargs.pop('options'), **kwargs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:mysql_pub; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:mysql_dsn; 5, default_parameter; 5, 6; 5, 7; 6, identifier:tables; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:blocking; 10, False; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kwargs; 13, block; 13, 14; 13, 16; 13, 17; 13, 24; 13, 50; 13, 51; 13, 80; 13, 112; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:parsed; 20, call; 20, 21; 20, 22; 21, identifier:urlparse; 22, argument_list; 22, 23; 23, identifier:mysql_dsn; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:mysql_settings; 27, dictionary; 27, 28; 27, 33; 27, 40; 27, 45; 28, pair; 28, 29; 28, 30; 29, string:"host"; 30, attribute; 30, 31; 30, 32; 31, identifier:parsed; 32, identifier:hostname; 33, pair; 33, 34; 33, 35; 34, string:"port"; 35, boolean_operator:or; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:parsed; 38, identifier:port; 39, integer:3306; 40, pair; 40, 41; 40, 42; 41, string:"user"; 42, attribute; 42, 43; 42, 44; 43, identifier:parsed; 44, identifier:username; 45, pair; 45, 46; 45, 47; 46, string:"passwd"; 47, attribute; 47, 48; 47, 49; 48, identifier:parsed; 49, identifier:password; 50, comment; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:stream; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:pymysqlreplication; 57, identifier:BinLogStreamReader; 58, argument_list; 58, 59; 58, 60; 58, 69; 58, 72; 58, 78; 59, identifier:mysql_settings; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:server_id; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:random; 65, identifier:randint; 66, argument_list; 66, 67; 66, 68; 67, integer:1000000000; 68, integer:4294967295; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:blocking; 71, identifier:blocking; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:only_events; 74, list:[DeleteRowsEvent, UpdateRowsEvent, WriteRowsEvent]; 74, 75; 74, 76; 74, 77; 75, identifier:DeleteRowsEvent; 76, identifier:UpdateRowsEvent; 77, identifier:WriteRowsEvent; 78, dictionary_splat; 78, 79; 79, identifier:kwargs; 80, function_definition; 80, 81; 80, 82; 80, 84; 81, function_name:_pk; 82, parameters; 82, 83; 83, identifier:values; 84, block; 84, 85; 84, 100; 85, if_statement; 85, 86; 85, 93; 86, call; 86, 87; 86, 88; 87, identifier:isinstance; 88, argument_list; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:event; 91, identifier:primary_key; 92, identifier:str; 93, block; 93, 94; 94, return_statement; 94, 95; 95, subscript; 95, 96; 95, 97; 96, identifier:values; 97, attribute; 97, 98; 97, 99; 98, identifier:event; 99, identifier:primary_key; 100, return_statement; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:tuple; 103, generator_expression; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:values; 106, identifier:k; 107, for_in_clause; 107, 108; 107, 109; 108, identifier:k; 109, attribute; 109, 110; 109, 111; 110, identifier:event; 111, identifier:primary_key; 112, for_statement; 112, 113; 112, 114; 112, 115; 113, identifier:event; 114, identifier:stream; 115, block; 115, 116; 115, 123; 115, 133; 115, 157; 115, 170; 115, 380; 116, if_statement; 116, 117; 116, 121; 117, not_operator; 117, 118; 118, attribute; 118, 119; 118, 120; 119, identifier:event; 120, identifier:primary_key; 121, block; 121, 122; 122, continue_statement; 123, if_statement; 123, 124; 123, 131; 124, boolean_operator:and; 124, 125; 124, 126; 125, identifier:tables; 126, comparison_operator:not; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:event; 129, identifier:table; 130, identifier:tables; 131, block; 131, 132; 132, continue_statement; 133, try_statement; 133, 134; 133, 141; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:rows; 138, attribute; 138, 139; 138, 140; 139, identifier:event; 140, identifier:rows; 141, except_clause; 141, 142; 141, 148; 142, as_pattern; 142, 143; 142, 146; 143, tuple; 143, 144; 143, 145; 144, identifier:UnicodeDecodeError; 145, identifier:ValueError; 146, as_pattern_target; 146, 147; 147, identifier:e; 148, block; 148, 149; 148, 156; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:logger; 153, identifier:exception; 154, argument_list; 154, 155; 155, identifier:e; 156, continue_statement; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:timestamp; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:datetime; 164, identifier:datetime; 165, identifier:fromtimestamp; 166, argument_list; 166, 167; 167, attribute; 167, 168; 167, 169; 168, identifier:event; 169, identifier:timestamp; 170, if_statement; 170, 171; 170, 176; 170, 240; 170, 310; 171, call; 171, 172; 171, 173; 172, identifier:isinstance; 173, argument_list; 173, 174; 173, 175; 174, identifier:event; 175, identifier:WriteRowsEvent; 176, block; 176, 177; 176, 185; 176, 192; 176, 201; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:sg_name; 180, binary_operator:%; 180, 181; 180, 182; 181, string:"%s_write"; 182, attribute; 182, 183; 182, 184; 183, identifier:event; 184, identifier:table; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:sg; 188, call; 188, 189; 188, 190; 189, identifier:signal; 190, argument_list; 190, 191; 191, identifier:sg_name; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:sg_raw; 195, call; 195, 196; 195, 197; 196, identifier:signal; 197, argument_list; 197, 198; 198, binary_operator:%; 198, 199; 198, 200; 199, string:"%s_raw"; 200, identifier:sg_name; 201, for_statement; 201, 202; 201, 203; 201, 204; 202, identifier:row; 203, identifier:rows; 204, block; 204, 205; 204, 214; 204, 221; 204, 228; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:pk; 208, call; 208, 209; 208, 210; 209, identifier:_pk; 210, argument_list; 210, 211; 211, subscript; 211, 212; 211, 213; 212, identifier:row; 213, string:"values"; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:sg; 218, identifier:send; 219, argument_list; 219, 220; 220, identifier:pk; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:sg_raw; 225, identifier:send; 226, argument_list; 226, 227; 227, identifier:row; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:logger; 232, identifier:debug; 233, argument_list; 233, 234; 234, binary_operator:%; 234, 235; 234, 236; 235, string:"%s -> %s, %s"; 236, tuple; 236, 237; 236, 238; 236, 239; 237, identifier:sg_name; 238, identifier:pk; 239, identifier:timestamp; 240, elif_clause; 240, 241; 240, 246; 241, call; 241, 242; 241, 243; 242, identifier:isinstance; 243, argument_list; 243, 244; 243, 245; 244, identifier:event; 245, identifier:UpdateRowsEvent; 246, block; 246, 247; 246, 255; 246, 262; 246, 271; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:sg_name; 250, binary_operator:%; 250, 251; 250, 252; 251, string:"%s_update"; 252, attribute; 252, 253; 252, 254; 253, identifier:event; 254, identifier:table; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:sg; 258, call; 258, 259; 258, 260; 259, identifier:signal; 260, argument_list; 260, 261; 261, identifier:sg_name; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:sg_raw; 265, call; 265, 266; 265, 267; 266, identifier:signal; 267, argument_list; 267, 268; 268, binary_operator:%; 268, 269; 268, 270; 269, string:"%s_raw"; 270, identifier:sg_name; 271, for_statement; 271, 272; 271, 273; 271, 274; 272, identifier:row; 273, identifier:rows; 274, block; 274, 275; 274, 284; 274, 291; 274, 298; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:pk; 278, call; 278, 279; 278, 280; 279, identifier:_pk; 280, argument_list; 280, 281; 281, subscript; 281, 282; 281, 283; 282, identifier:row; 283, string:"after_values"; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:sg; 288, identifier:send; 289, argument_list; 289, 290; 290, identifier:pk; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:sg_raw; 295, identifier:send; 296, argument_list; 296, 297; 297, identifier:row; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:logger; 302, identifier:debug; 303, argument_list; 303, 304; 304, binary_operator:%; 304, 305; 304, 306; 305, string:"%s -> %s, %s"; 306, tuple; 306, 307; 306, 308; 306, 309; 307, identifier:sg_name; 308, identifier:pk; 309, identifier:timestamp; 310, elif_clause; 310, 311; 310, 316; 311, call; 311, 312; 311, 313; 312, identifier:isinstance; 313, argument_list; 313, 314; 313, 315; 314, identifier:event; 315, identifier:DeleteRowsEvent; 316, block; 316, 317; 316, 325; 316, 332; 316, 341; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:sg_name; 320, binary_operator:%; 320, 321; 320, 322; 321, string:"%s_delete"; 322, attribute; 322, 323; 322, 324; 323, identifier:event; 324, identifier:table; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:sg; 328, call; 328, 329; 328, 330; 329, identifier:signal; 330, argument_list; 330, 331; 331, identifier:sg_name; 332, expression_statement; 332, 333; 333, assignment; 333, 334; 333, 335; 334, identifier:sg_raw; 335, call; 335, 336; 335, 337; 336, identifier:signal; 337, argument_list; 337, 338; 338, binary_operator:%; 338, 339; 338, 340; 339, string:"%s_raw"; 340, identifier:sg_name; 341, for_statement; 341, 342; 341, 343; 341, 344; 342, identifier:row; 343, identifier:rows; 344, block; 344, 345; 344, 354; 344, 361; 344, 368; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:pk; 348, call; 348, 349; 348, 350; 349, identifier:_pk; 350, argument_list; 350, 351; 351, subscript; 351, 352; 351, 353; 352, identifier:row; 353, string:"values"; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:sg; 358, identifier:send; 359, argument_list; 359, 360; 360, identifier:pk; 361, expression_statement; 361, 362; 362, call; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:sg_raw; 365, identifier:send; 366, argument_list; 366, 367; 367, identifier:row; 368, expression_statement; 368, 369; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:logger; 372, identifier:debug; 373, argument_list; 373, 374; 374, binary_operator:%; 374, 375; 374, 376; 375, string:"%s -> %s, %s"; 376, tuple; 376, 377; 376, 378; 376, 379; 377, identifier:sg_name; 378, identifier:pk; 379, identifier:timestamp; 380, expression_statement; 380, 381; 381, call; 381, 382; 381, 388; 382, attribute; 382, 383; 382, 387; 383, call; 383, 384; 383, 385; 384, identifier:signal; 385, argument_list; 385, 386; 386, string:"mysql_binlog_pos"; 387, identifier:send; 388, argument_list; 388, 389; 389, binary_operator:%; 389, 390; 389, 391; 390, string:"%s:%s"; 391, tuple; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:stream; 394, identifier:log_file; 395, attribute; 395, 396; 395, 397; 396, identifier:stream; 397, identifier:log_pos
def mysql_pub(mysql_dsn, tables=None, blocking=False, **kwargs): """MySQL row-based binlog events pub. **General Usage** Listen and pub all tables events:: mysql_pub(mysql_dsn) Listen and pub only some tables events:: mysql_pub(mysql_dsn, tables=["test"]) By default the ``mysql_pub`` will process and pub all existing row-based binlog (starting from current binlog file with pos 0) and quit, you may set blocking to True to block and wait for new binlog, enable this option if you're running the script as a daemon:: mysql_pub(mysql_dsn, blocking=True) The binlog stream act as a mysql slave and read binlog from master, so the server_id matters, if it's conflict with other slaves or scripts, strange bugs may happen. By default, the server_id is randomized by ``randint(1000000000, 4294967295)``, you may set it to a specific value by server_id arg:: mysql_pub(mysql_dsn, blocking=True, server_id=1024) **Signals Illustrate** Sometimes you want more info than the pk value, the mysql_pub expose a raw signal which will send the original binlog stream events. For example, the following sql:: INSERT INTO test (data) VALUES ('a'); The row-based binlog generated from the sql, reads by binlog stream and generates signals equals to:: signal("test_write").send(1) signal("test_write_raw").send({'values': {'data': 'a', 'id': 1}}) **Binlog Pos Signal** The mysql_pub has a unique signal ``mysql_binlog_pos`` which contains the binlog file and binlog pos, you can record the signal and resume binlog stream from last position with it. :param mysql_dsn: mysql dsn with row-based binlog enabled. :param tables: which tables to enable mysql_pub. :param blocking: whether mysql_pub should wait more binlog when all existing binlog processed. :param kwargs: more kwargs to be passed to binlog stream. """ # parse mysql settings parsed = urlparse(mysql_dsn) mysql_settings = { "host": parsed.hostname, "port": parsed.port or 3306, "user": parsed.username, "passwd": parsed.password } # connect to binlog stream stream = pymysqlreplication.BinLogStreamReader( mysql_settings, server_id=random.randint(1000000000, 4294967295), blocking=blocking, only_events=[DeleteRowsEvent, UpdateRowsEvent, WriteRowsEvent], **kwargs ) def _pk(values): if isinstance(event.primary_key, str): return values[event.primary_key] return tuple(values[k] for k in event.primary_key) for event in stream: if not event.primary_key: continue if tables and event.table not in tables: continue try: rows = event.rows except (UnicodeDecodeError, ValueError) as e: logger.exception(e) continue timestamp = datetime.datetime.fromtimestamp(event.timestamp) if isinstance(event, WriteRowsEvent): sg_name = "%s_write" % event.table sg = signal(sg_name) sg_raw = signal("%s_raw" % sg_name) for row in rows: pk = _pk(row["values"]) sg.send(pk) sg_raw.send(row) logger.debug("%s -> %s, %s" % (sg_name, pk, timestamp)) elif isinstance(event, UpdateRowsEvent): sg_name = "%s_update" % event.table sg = signal(sg_name) sg_raw = signal("%s_raw" % sg_name) for row in rows: pk = _pk(row["after_values"]) sg.send(pk) sg_raw.send(row) logger.debug("%s -> %s, %s" % (sg_name, pk, timestamp)) elif isinstance(event, DeleteRowsEvent): sg_name = "%s_delete" % event.table sg = signal(sg_name) sg_raw = signal("%s_raw" % sg_name) for row in rows: pk = _pk(row["values"]) sg.send(pk) sg_raw.send(row) logger.debug("%s -> %s, %s" % (sg_name, pk, timestamp)) signal("mysql_binlog_pos").send( "%s:%s" % (stream.log_file, stream.log_pos))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:do_step; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:values; 6, identifier:xy_values; 7, identifier:coeff; 8, identifier:width; 9, block; 9, 10; 9, 12; 9, 27; 9, 206; 9, 225; 9, 243; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:forces; 15, dictionary_comprehension; 15, 16; 15, 19; 16, pair; 16, 17; 16, 18; 17, identifier:k; 18, list:[]; 19, for_in_clause; 19, 20; 19, 23; 20, pattern_list; 20, 21; 20, 22; 21, identifier:k; 22, identifier:i; 23, call; 23, 24; 23, 25; 24, identifier:enumerate; 25, argument_list; 25, 26; 26, identifier:xy_values; 27, for_statement; 27, 28; 27, 35; 27, 43; 28, pattern_list; 28, 29; 28, 32; 29, tuple_pattern; 29, 30; 29, 31; 30, identifier:index1; 31, identifier:value1; 32, tuple_pattern; 32, 33; 32, 34; 33, identifier:index2; 34, identifier:value2; 35, call; 35, 36; 35, 37; 36, identifier:combinations; 37, argument_list; 37, 38; 37, 42; 38, call; 38, 39; 38, 40; 39, identifier:enumerate; 40, argument_list; 40, 41; 41, identifier:xy_values; 42, integer:2; 43, block; 43, 44; 43, 65; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:f; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:calc_2d_forces; 51, argument_list; 51, 52; 51, 55; 51, 58; 51, 61; 51, 64; 52, subscript; 52, 53; 52, 54; 53, identifier:value1; 54, integer:0; 55, subscript; 55, 56; 55, 57; 56, identifier:value1; 57, integer:1; 58, subscript; 58, 59; 58, 60; 59, identifier:value2; 60, integer:0; 61, subscript; 61, 62; 61, 63; 62, identifier:value2; 63, integer:1; 64, identifier:width; 65, if_statement; 65, 66; 65, 73; 65, 139; 66, comparison_operator:<; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:coeff; 69, identifier:index1; 70, subscript; 70, 71; 70, 72; 71, identifier:coeff; 72, identifier:index2; 73, block; 73, 74; 74, if_statement; 74, 75; 74, 88; 74, 89; 74, 113; 75, comparison_operator:<; 75, 76; 75, 83; 76, binary_operator:-; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:b_lenght; 80, subscript; 80, 81; 80, 82; 81, identifier:coeff; 82, identifier:index2; 83, binary_operator:/; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:b_lenght; 87, integer:10; 88, comment; 89, block; 89, 90; 89, 101; 89, 102; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:forces; 95, identifier:index1; 96, identifier:append; 97, argument_list; 97, 98; 98, subscript; 98, 99; 98, 100; 99, identifier:f; 100, integer:1; 101, comment; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:forces; 107, identifier:index2; 108, identifier:append; 109, argument_list; 109, 110; 110, subscript; 110, 111; 110, 112; 111, identifier:f; 112, integer:0; 113, else_clause; 113, 114; 113, 115; 114, comment; 115, block; 115, 116; 115, 127; 115, 128; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, subscript; 119, 120; 119, 121; 120, identifier:forces; 121, identifier:index1; 122, identifier:append; 123, argument_list; 123, 124; 124, subscript; 124, 125; 124, 126; 125, identifier:f; 126, integer:0; 127, comment; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:forces; 133, identifier:index2; 134, identifier:append; 135, argument_list; 135, 136; 136, subscript; 136, 137; 136, 138; 137, identifier:f; 138, integer:1; 139, else_clause; 139, 140; 140, block; 140, 141; 141, if_statement; 141, 142; 141, 155; 141, 156; 141, 180; 142, comparison_operator:<; 142, 143; 142, 150; 143, binary_operator:-; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:b_lenght; 147, subscript; 147, 148; 147, 149; 148, identifier:coeff; 149, identifier:index1; 150, binary_operator:/; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:b_lenght; 154, integer:10; 155, comment; 156, block; 156, 157; 156, 168; 156, 169; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 164; 159, attribute; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:forces; 162, identifier:index1; 163, identifier:append; 164, argument_list; 164, 165; 165, subscript; 165, 166; 165, 167; 166, identifier:f; 167, integer:0; 168, comment; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, subscript; 172, 173; 172, 174; 173, identifier:forces; 174, identifier:index2; 175, identifier:append; 176, argument_list; 176, 177; 177, subscript; 177, 178; 177, 179; 178, identifier:f; 179, integer:1; 180, else_clause; 180, 181; 180, 182; 181, comment; 182, block; 182, 183; 182, 194; 182, 195; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 190; 185, attribute; 185, 186; 185, 189; 186, subscript; 186, 187; 186, 188; 187, identifier:forces; 188, identifier:index1; 189, identifier:append; 190, argument_list; 190, 191; 191, subscript; 191, 192; 191, 193; 192, identifier:f; 193, integer:1; 194, comment; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 202; 197, attribute; 197, 198; 197, 201; 198, subscript; 198, 199; 198, 200; 199, identifier:forces; 200, identifier:index2; 201, identifier:append; 202, argument_list; 202, 203; 203, subscript; 203, 204; 203, 205; 204, identifier:f; 205, integer:0; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:forces; 209, dictionary_comprehension; 209, 210; 209, 216; 210, pair; 210, 211; 210, 212; 211, identifier:k; 212, call; 212, 213; 212, 214; 213, identifier:sum; 214, argument_list; 214, 215; 215, identifier:v; 216, for_in_clause; 216, 217; 216, 220; 217, pattern_list; 217, 218; 217, 219; 218, identifier:k; 219, identifier:v; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:forces; 223, identifier:items; 224, argument_list; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:energy; 228, call; 228, 229; 228, 230; 229, identifier:sum; 230, argument_list; 230, 231; 231, list_comprehension; 231, 232; 231, 236; 232, call; 232, 233; 232, 234; 233, identifier:abs; 234, argument_list; 234, 235; 235, identifier:x; 236, for_in_clause; 236, 237; 236, 238; 237, identifier:x; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:forces; 241, identifier:values; 242, argument_list; 243, return_statement; 243, 244; 244, expression_list; 244, 245; 244, 262; 245, list_comprehension; 245, 246; 245, 254; 246, parenthesized_expression; 246, 247; 247, binary_operator:+; 247, 248; 247, 253; 248, binary_operator:/; 248, 249; 248, 252; 249, subscript; 249, 250; 249, 251; 250, identifier:forces; 251, identifier:k; 252, integer:10; 253, identifier:v; 254, for_in_clause; 254, 255; 254, 258; 255, pattern_list; 255, 256; 255, 257; 256, identifier:k; 257, identifier:v; 258, call; 258, 259; 258, 260; 259, identifier:enumerate; 260, argument_list; 260, 261; 261, identifier:values; 262, identifier:energy
def do_step(self, values, xy_values,coeff, width): """Calculates forces between two diagrams and pushes them apart by tenth of width""" forces = {k:[] for k,i in enumerate(xy_values)} for (index1, value1), (index2,value2) in combinations(enumerate(xy_values),2): f = self.calc_2d_forces(value1[0],value1[1],value2[0],value2[1],width) if coeff[index1] < coeff[index2]: if self.b_lenght-coeff[index2]<self.b_lenght/10: #a quick and dirty solution, but works forces[index1].append(f[1]) # push to left (smaller projection value) forces[index2].append(f[0]) else: #all is normal forces[index1].append(f[0]) # push to left (smaller projection value) forces[index2].append(f[1]) else: if self.b_lenght-coeff[index1]<self.b_lenght/10: #a quick and dirty solution, but works forces[index1].append(f[0]) # push to left (smaller projection value) forces[index2].append(f[1]) else: #if all is normal forces[index1].append(f[1]) # push to left (smaller projection value) forces[index2].append(f[0]) forces = {k:sum(v) for k,v in forces.items()} energy = sum([abs(x) for x in forces.values()]) return [(forces[k]/10+v) for k, v in enumerate(values)], energy
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:execute_get; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:resource; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 21; 8, 33; 8, 39; 8, 45; 8, 114; 8, 154; 8, 166; 8, 193; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:url; 14, binary_operator:%; 14, 15; 14, 16; 15, string:'%s/%s'; 16, tuple; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:base_url; 20, identifier:resource; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:headers; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:kwargs; 27, identifier:pop; 28, argument_list; 28, 29; 28, 30; 29, string:'headers'; 30, call; 30, 31; 30, 32; 31, identifier:dict; 32, argument_list; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, subscript; 35, 36; 35, 37; 36, identifier:headers; 37, string:'Accept'; 38, string:'application/json'; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:headers; 43, string:'Content-Type'; 44, string:'application/json'; 45, if_statement; 45, 46; 45, 47; 46, identifier:kwargs; 47, block; 47, 48; 47, 57; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:separator; 51, conditional_expression:if; 51, 52; 51, 53; 51, 56; 52, string:'&'; 53, comparison_operator:in; 53, 54; 53, 55; 54, string:'?'; 55, identifier:url; 56, string:'?'; 57, for_statement; 57, 58; 57, 61; 57, 66; 58, pattern_list; 58, 59; 58, 60; 59, identifier:key; 60, identifier:value; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:kwargs; 64, identifier:items; 65, argument_list; 66, block; 66, 67; 66, 110; 67, if_statement; 67, 68; 67, 82; 67, 98; 68, boolean_operator:and; 68, 69; 68, 74; 69, call; 69, 70; 69, 71; 70, identifier:hasattr; 71, argument_list; 71, 72; 71, 73; 72, identifier:value; 73, string:'__iter__'; 74, comparison_operator:not; 74, 75; 74, 79; 75, call; 75, 76; 75, 77; 76, identifier:type; 77, argument_list; 77, 78; 78, identifier:value; 79, attribute; 79, 80; 79, 81; 80, identifier:six; 81, identifier:string_types; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:url; 86, binary_operator:%; 86, 87; 86, 88; 87, string:'%s%s%s=%s'; 88, tuple; 88, 89; 88, 90; 88, 91; 88, 92; 89, identifier:url; 90, identifier:separator; 91, identifier:key; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, string:','; 95, identifier:join; 96, argument_list; 96, 97; 97, identifier:value; 98, else_clause; 98, 99; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:url; 103, binary_operator:%; 103, 104; 103, 105; 104, string:'%s%s%s=%s'; 105, tuple; 105, 106; 105, 107; 105, 108; 105, 109; 106, identifier:url; 107, identifier:separator; 108, identifier:key; 109, identifier:value; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:separator; 113, string:'&'; 114, if_statement; 114, 115; 114, 118; 114, 129; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_access_token; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:headers; 123, string:'Authorization'; 124, binary_operator:%; 124, 125; 124, 126; 125, string:'Bearer %s'; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:_access_token; 129, else_clause; 129, 130; 130, block; 130, 131; 130, 140; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:separator; 134, conditional_expression:if; 134, 135; 134, 136; 134, 139; 135, string:'&'; 136, comparison_operator:in; 136, 137; 136, 138; 137, string:'?'; 138, identifier:url; 139, string:'?'; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:url; 143, binary_operator:%; 143, 144; 143, 145; 144, string:'%s%sclient_id=%s&client_secret=%s'; 145, tuple; 145, 146; 145, 147; 145, 148; 145, 151; 146, identifier:url; 147, identifier:separator; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:_client_id; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:_client_secret; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:response; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:requests; 160, identifier:get; 161, argument_list; 161, 162; 161, 163; 162, identifier:url; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:headers; 165, identifier:headers; 166, if_statement; 166, 167; 166, 174; 167, comparison_operator:!=; 167, 168; 167, 173; 168, binary_operator://; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:response; 171, identifier:status_code; 172, integer:100; 173, integer:2; 174, block; 174, 175; 175, raise_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:GhostException; 178, argument_list; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:response; 181, identifier:status_code; 182, call; 182, 183; 182, 190; 183, attribute; 183, 184; 183, 189; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:response; 187, identifier:json; 188, argument_list; 189, identifier:get; 190, argument_list; 190, 191; 190, 192; 191, string:'errors'; 192, list:[]; 193, return_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:response; 197, identifier:json; 198, argument_list
def execute_get(self, resource, **kwargs): """ Execute an HTTP GET request against the API endpoints. This method is meant for internal use. :param resource: The last part of the URI :param kwargs: Additional query parameters (and optionally headers) :return: The HTTP response as JSON or `GhostException` if unsuccessful """ url = '%s/%s' % (self.base_url, resource) headers = kwargs.pop('headers', dict()) headers['Accept'] = 'application/json' headers['Content-Type'] = 'application/json' if kwargs: separator = '&' if '?' in url else '?' for key, value in kwargs.items(): if hasattr(value, '__iter__') and type(value) not in six.string_types: url = '%s%s%s=%s' % (url, separator, key, ','.join(value)) else: url = '%s%s%s=%s' % (url, separator, key, value) separator = '&' if self._access_token: headers['Authorization'] = 'Bearer %s' % self._access_token else: separator = '&' if '?' in url else '?' url = '%s%sclient_id=%s&client_secret=%s' % ( url, separator, self._client_id, self._client_secret ) response = requests.get(url, headers=headers) if response.status_code // 100 != 2: raise GhostException(response.status_code, response.json().get('errors', [])) return response.json()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:variants; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:case_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:skip; 8, integer:0; 9, default_parameter; 9, 10; 9, 11; 10, identifier:count; 11, integer:1000; 12, default_parameter; 12, 13; 12, 14; 13, identifier:filters; 14, None; 15, block; 15, 16; 15, 18; 15, 24; 15, 35; 15, 41; 15, 47; 15, 72; 15, 76; 15, 93; 15, 97; 15, 114; 15, 118; 15, 135; 15, 139; 15, 156; 15, 160; 15, 177; 15, 183; 15, 192; 15, 202; 15, 212; 15, 222; 15, 226; 15, 230; 15, 408; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:filters; 21, boolean_operator:or; 21, 22; 21, 23; 22, identifier:filters; 23, dictionary; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:case_obj; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:case; 31, argument_list; 31, 32; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:case_id; 34, identifier:case_id; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:limit; 38, binary_operator:+; 38, 39; 38, 40; 39, identifier:count; 40, identifier:skip; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:genes; 44, call; 44, 45; 44, 46; 45, identifier:set; 46, argument_list; 47, if_statement; 47, 48; 47, 54; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:filters; 51, identifier:get; 52, argument_list; 52, 53; 53, string:'gene_ids'; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:genes; 58, call; 58, 59; 58, 60; 59, identifier:set; 60, argument_list; 60, 61; 61, list_comprehension; 61, 62; 61, 67; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:gene_id; 65, identifier:strip; 66, argument_list; 67, for_in_clause; 67, 68; 67, 69; 68, identifier:gene_id; 69, subscript; 69, 70; 69, 71; 70, identifier:filters; 71, string:'gene_ids'; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:frequency; 75, None; 76, if_statement; 76, 77; 76, 83; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:filters; 80, identifier:get; 81, argument_list; 81, 82; 82, string:'frequency'; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:frequency; 87, call; 87, 88; 87, 89; 88, identifier:float; 89, argument_list; 89, 90; 90, subscript; 90, 91; 90, 92; 91, identifier:filters; 92, string:'frequency'; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:cadd; 96, None; 97, if_statement; 97, 98; 97, 104; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:filters; 101, identifier:get; 102, argument_list; 102, 103; 103, string:'cadd'; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:cadd; 108, call; 108, 109; 108, 110; 109, identifier:float; 110, argument_list; 110, 111; 111, subscript; 111, 112; 111, 113; 112, identifier:filters; 113, string:'cadd'; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:genetic_models; 117, None; 118, if_statement; 118, 119; 118, 125; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:filters; 122, identifier:get; 123, argument_list; 123, 124; 124, string:'genetic_models'; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:genetic_models; 129, call; 129, 130; 129, 131; 130, identifier:set; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 134; 133, identifier:filters; 134, string:'genetic_models'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:sv_len; 138, None; 139, if_statement; 139, 140; 139, 146; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:filters; 143, identifier:get; 144, argument_list; 144, 145; 145, string:'sv_len'; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:sv_len; 150, call; 150, 151; 150, 152; 151, identifier:float; 152, argument_list; 152, 153; 153, subscript; 153, 154; 153, 155; 154, identifier:filters; 155, string:'sv_len'; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:impact_severities; 159, None; 160, if_statement; 160, 161; 160, 167; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:filters; 164, identifier:get; 165, argument_list; 165, 166; 166, string:'impact_severities'; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:impact_severities; 171, call; 171, 172; 171, 173; 172, identifier:set; 173, argument_list; 173, 174; 174, subscript; 174, 175; 174, 176; 175, identifier:filters; 176, string:'impact_severities'; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:vcf_file_path; 180, attribute; 180, 181; 180, 182; 181, identifier:case_obj; 182, identifier:variant_source; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:head; 188, call; 188, 189; 188, 190; 189, identifier:get_header; 190, argument_list; 190, 191; 191, identifier:vcf_file_path; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:vep_header; 197, attribute; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:head; 201, identifier:vep_columns; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:snpeff_header; 207, attribute; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:head; 211, identifier:snpeff_columns; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:variants; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:_get_filtered_variants; 219, argument_list; 219, 220; 219, 221; 220, identifier:vcf_file_path; 221, identifier:filters; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:result; 225, list:[]; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:skip_index; 229, integer:0; 230, for_statement; 230, 231; 230, 234; 230, 238; 231, pattern_list; 231, 232; 231, 233; 232, identifier:index; 233, identifier:variant; 234, call; 234, 235; 234, 236; 235, identifier:enumerate; 236, argument_list; 236, 237; 237, identifier:variants; 238, block; 238, 239; 238, 243; 239, expression_statement; 239, 240; 240, augmented_assignment:+=; 240, 241; 240, 242; 241, identifier:index; 242, integer:1; 243, if_statement; 243, 244; 243, 247; 243, 402; 244, comparison_operator:>=; 244, 245; 244, 246; 245, identifier:skip_index; 246, identifier:skip; 247, block; 247, 248; 247, 265; 247, 288; 247, 305; 247, 321; 247, 337; 247, 364; 247, 380; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:variant_obj; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:_format_variants; 255, argument_list; 255, 256; 255, 259; 255, 262; 256, keyword_argument; 256, 257; 256, 258; 257, identifier:variant; 258, identifier:variant; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:index; 261, identifier:index; 262, keyword_argument; 262, 263; 262, 264; 263, identifier:case_obj; 264, identifier:case_obj; 265, if_statement; 265, 266; 265, 269; 266, boolean_operator:and; 266, 267; 266, 268; 267, identifier:genes; 268, identifier:variant_obj; 269, block; 269, 270; 270, if_statement; 270, 271; 270, 283; 271, not_operator; 271, 272; 272, call; 272, 273; 272, 281; 273, attribute; 273, 274; 273, 280; 274, call; 274, 275; 274, 276; 275, identifier:set; 276, argument_list; 276, 277; 277, subscript; 277, 278; 277, 279; 278, identifier:variant_obj; 279, string:'gene_symbols'; 280, identifier:intersection; 281, argument_list; 281, 282; 282, identifier:genes; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:variant_obj; 287, None; 288, if_statement; 288, 289; 288, 292; 289, boolean_operator:and; 289, 290; 289, 291; 290, identifier:impact_severities; 291, identifier:variant_obj; 292, block; 292, 293; 293, if_statement; 293, 294; 293, 300; 294, not_operator; 294, 295; 295, comparison_operator:in; 295, 296; 295, 299; 296, subscript; 296, 297; 296, 298; 297, identifier:variant_obj; 298, string:'impact_severity'; 299, identifier:impact_severities; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:variant_obj; 304, None; 305, if_statement; 305, 306; 305, 309; 306, boolean_operator:and; 306, 307; 306, 308; 307, identifier:frequency; 308, identifier:variant_obj; 309, block; 309, 310; 310, if_statement; 310, 311; 310, 316; 311, comparison_operator:>; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:variant_obj; 314, identifier:max_freq; 315, identifier:frequency; 316, block; 316, 317; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:variant_obj; 320, None; 321, if_statement; 321, 322; 321, 325; 322, boolean_operator:and; 322, 323; 322, 324; 323, identifier:cadd; 324, identifier:variant_obj; 325, block; 325, 326; 326, if_statement; 326, 327; 326, 332; 327, comparison_operator:<; 327, 328; 327, 331; 328, subscript; 328, 329; 328, 330; 329, identifier:variant_obj; 330, string:'cadd_score'; 331, identifier:cadd; 332, block; 332, 333; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:variant_obj; 336, None; 337, if_statement; 337, 338; 337, 341; 338, boolean_operator:and; 338, 339; 338, 340; 339, identifier:genetic_models; 340, identifier:variant_obj; 341, block; 341, 342; 341, 351; 342, expression_statement; 342, 343; 343, assignment; 343, 344; 343, 345; 344, identifier:models; 345, call; 345, 346; 345, 347; 346, identifier:set; 347, argument_list; 347, 348; 348, attribute; 348, 349; 348, 350; 349, identifier:variant_obj; 350, identifier:genetic_models; 351, if_statement; 351, 352; 351, 359; 352, not_operator; 352, 353; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:models; 356, identifier:intersection; 357, argument_list; 357, 358; 358, identifier:genetic_models; 359, block; 359, 360; 360, expression_statement; 360, 361; 361, assignment; 361, 362; 361, 363; 362, identifier:variant_obj; 363, None; 364, if_statement; 364, 365; 364, 368; 365, boolean_operator:and; 365, 366; 365, 367; 366, identifier:sv_len; 367, identifier:variant_obj; 368, block; 368, 369; 369, if_statement; 369, 370; 369, 375; 370, comparison_operator:<; 370, 371; 370, 374; 371, attribute; 371, 372; 371, 373; 372, identifier:variant_obj; 373, identifier:sv_len; 374, identifier:sv_len; 375, block; 375, 376; 376, expression_statement; 376, 377; 377, assignment; 377, 378; 377, 379; 378, identifier:variant_obj; 379, None; 380, if_statement; 380, 381; 380, 382; 381, identifier:variant_obj; 382, block; 382, 383; 382, 387; 383, expression_statement; 383, 384; 384, augmented_assignment:+=; 384, 385; 384, 386; 385, identifier:skip_index; 386, integer:1; 387, if_statement; 387, 388; 387, 391; 387, 399; 388, comparison_operator:<=; 388, 389; 388, 390; 389, identifier:skip_index; 390, identifier:limit; 391, block; 391, 392; 392, expression_statement; 392, 393; 393, call; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, identifier:result; 396, identifier:append; 397, argument_list; 397, 398; 398, identifier:variant_obj; 399, else_clause; 399, 400; 400, block; 400, 401; 401, break_statement; 402, else_clause; 402, 403; 403, block; 403, 404; 404, expression_statement; 404, 405; 405, augmented_assignment:+=; 405, 406; 405, 407; 406, identifier:skip_index; 407, integer:1; 408, return_statement; 408, 409; 409, call; 409, 410; 409, 411; 410, identifier:Results; 411, argument_list; 411, 412; 411, 413; 412, identifier:result; 413, call; 413, 414; 413, 415; 414, identifier:len; 415, argument_list; 415, 416; 416, identifier:result
def variants(self, case_id, skip=0, count=1000, filters=None): """Return all variants in the VCF. This function will apply the given filter and return the 'count' first variants. If skip the first 'skip' variants will not be regarded. Args: case_id (str): Path to a vcf file (for this adapter) skip (int): Skip first variants count (int): The number of variants to return filters (dict): A dictionary with filters. Currently this will look like: { gene_list: [] (list of hgnc ids), frequency: None (float), cadd: None (float), sv_len: None (float), consequence: [] (list of consequences), is_lof: None (Bool), genetic_models [] (list of genetic models) sv_type: List (list of sv types), } Returns: puzzle.constants.Results : Named tuple with variants and nr_of_variants """ filters = filters or {} case_obj = self.case(case_id=case_id) limit = count + skip genes = set() if filters.get('gene_ids'): genes = set([gene_id.strip() for gene_id in filters['gene_ids']]) frequency = None if filters.get('frequency'): frequency = float(filters['frequency']) cadd = None if filters.get('cadd'): cadd = float(filters['cadd']) genetic_models = None if filters.get('genetic_models'): genetic_models = set(filters['genetic_models']) sv_len = None if filters.get('sv_len'): sv_len = float(filters['sv_len']) impact_severities = None if filters.get('impact_severities'): impact_severities = set(filters['impact_severities']) vcf_file_path = case_obj.variant_source self.head = get_header(vcf_file_path) self.vep_header = self.head.vep_columns self.snpeff_header = self.head.snpeff_columns variants = self._get_filtered_variants(vcf_file_path, filters) result = [] skip_index = 0 for index, variant in enumerate(variants): index += 1 if skip_index >= skip: variant_obj = self._format_variants( variant=variant, index=index, case_obj=case_obj, ) if genes and variant_obj: if not set(variant_obj['gene_symbols']).intersection(genes): variant_obj = None if impact_severities and variant_obj: if not variant_obj['impact_severity'] in impact_severities: variant_obj = None if frequency and variant_obj: if variant_obj.max_freq > frequency: variant_obj = None if cadd and variant_obj: if variant_obj['cadd_score'] < cadd: variant_obj = None if genetic_models and variant_obj: models = set(variant_obj.genetic_models) if not models.intersection(genetic_models): variant_obj = None if sv_len and variant_obj: if variant_obj.sv_len < sv_len: variant_obj = None if variant_obj: skip_index += 1 if skip_index <= limit: result.append(variant_obj) else: break else: skip_index += 1 return Results(result, len(result))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_get_filtered_variants; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:vcf_file_path; 6, default_parameter; 6, 7; 6, 8; 7, identifier:filters; 8, dictionary; 9, block; 9, 10; 9, 12; 9, 18; 9, 24; 9, 30; 9, 55; 9, 72; 9, 89; 9, 101; 9, 155; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:genes; 15, call; 15, 16; 15, 17; 16, identifier:set; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:consequences; 21, call; 21, 22; 21, 23; 22, identifier:set; 23, argument_list; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:sv_types; 27, call; 27, 28; 27, 29; 28, identifier:set; 29, argument_list; 30, if_statement; 30, 31; 30, 37; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:filters; 34, identifier:get; 35, argument_list; 35, 36; 36, string:'gene_ids'; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:genes; 41, call; 41, 42; 41, 43; 42, identifier:set; 43, argument_list; 43, 44; 44, list_comprehension; 44, 45; 44, 50; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:gene_id; 48, identifier:strip; 49, argument_list; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:gene_id; 52, subscript; 52, 53; 52, 54; 53, identifier:filters; 54, string:'gene_ids'; 55, if_statement; 55, 56; 55, 62; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:filters; 59, identifier:get; 60, argument_list; 60, 61; 61, string:'consequence'; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:consequences; 66, call; 66, 67; 66, 68; 67, identifier:set; 68, argument_list; 68, 69; 69, subscript; 69, 70; 69, 71; 70, identifier:filters; 71, string:'consequence'; 72, if_statement; 72, 73; 72, 79; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:filters; 76, identifier:get; 77, argument_list; 77, 78; 78, string:'sv_types'; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:sv_types; 83, call; 83, 84; 83, 85; 84, identifier:set; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:filters; 88, string:'sv_types'; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:logger; 93, identifier:info; 94, argument_list; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, string:"Get variants from {0}"; 98, identifier:format; 99, argument_list; 99, 100; 100, identifier:vcf_file_path; 101, if_statement; 101, 102; 101, 108; 101, 146; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:filters; 105, identifier:get; 106, argument_list; 106, 107; 107, string:'range'; 108, block; 108, 109; 108, 132; 108, 139; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:range_str; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, string:"{0}:{1}-{2}"; 115, identifier:format; 116, argument_list; 116, 117; 116, 122; 116, 127; 117, subscript; 117, 118; 117, 121; 118, subscript; 118, 119; 118, 120; 119, identifier:filters; 120, string:'range'; 121, string:'chromosome'; 122, subscript; 122, 123; 122, 126; 123, subscript; 123, 124; 123, 125; 124, identifier:filters; 125, string:'range'; 126, string:'start'; 127, subscript; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:filters; 130, string:'range'; 131, string:'end'; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:vcf; 135, call; 135, 136; 135, 137; 136, identifier:VCF; 137, argument_list; 137, 138; 138, identifier:vcf_file_path; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:handle; 142, call; 142, 143; 142, 144; 143, identifier:vcf; 144, argument_list; 144, 145; 145, identifier:range_str; 146, else_clause; 146, 147; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:handle; 151, call; 151, 152; 151, 153; 152, identifier:VCF; 153, argument_list; 153, 154; 154, identifier:vcf_file_path; 155, for_statement; 155, 156; 155, 157; 155, 158; 156, identifier:variant; 157, identifier:handle; 158, block; 158, 159; 158, 166; 158, 170; 158, 198; 158, 221; 158, 244; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:variant_line; 162, call; 162, 163; 162, 164; 163, identifier:str; 164, argument_list; 164, 165; 165, identifier:variant; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:keep_variant; 169, True; 170, if_statement; 170, 171; 170, 174; 171, boolean_operator:and; 171, 172; 171, 173; 172, identifier:genes; 173, identifier:keep_variant; 174, block; 174, 175; 174, 179; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:keep_variant; 178, False; 179, for_statement; 179, 180; 179, 181; 179, 182; 180, identifier:gene; 181, identifier:genes; 182, block; 182, 183; 183, if_statement; 183, 184; 183, 192; 184, comparison_operator:in; 184, 185; 184, 191; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, string:"{0}"; 188, identifier:format; 189, argument_list; 189, 190; 190, identifier:gene; 191, identifier:variant_line; 192, block; 192, 193; 192, 197; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:keep_variant; 196, True; 197, break_statement; 198, if_statement; 198, 199; 198, 202; 199, boolean_operator:and; 199, 200; 199, 201; 200, identifier:consequences; 201, identifier:keep_variant; 202, block; 202, 203; 202, 207; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:keep_variant; 206, False; 207, for_statement; 207, 208; 207, 209; 207, 210; 208, identifier:consequence; 209, identifier:consequences; 210, block; 210, 211; 211, if_statement; 211, 212; 211, 215; 212, comparison_operator:in; 212, 213; 212, 214; 213, identifier:consequence; 214, identifier:variant_line; 215, block; 215, 216; 215, 220; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:keep_variant; 219, True; 220, break_statement; 221, if_statement; 221, 222; 221, 225; 222, boolean_operator:and; 222, 223; 222, 224; 223, identifier:sv_types; 224, identifier:keep_variant; 225, block; 225, 226; 225, 230; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:keep_variant; 229, False; 230, for_statement; 230, 231; 230, 232; 230, 233; 231, identifier:sv_type; 232, identifier:sv_types; 233, block; 233, 234; 234, if_statement; 234, 235; 234, 238; 235, comparison_operator:in; 235, 236; 235, 237; 236, identifier:sv_type; 237, identifier:variant_line; 238, block; 238, 239; 238, 243; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:keep_variant; 242, True; 243, break_statement; 244, if_statement; 244, 245; 244, 246; 245, identifier:keep_variant; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, yield; 248, 249; 249, identifier:variant
def _get_filtered_variants(self, vcf_file_path, filters={}): """Check if variants follows the filters This function will try to make filters faster for the vcf adapter Args: vcf_file_path(str): Path to vcf filters (dict): A dictionary with filters Yields: varian_line (str): A vcf variant line """ genes = set() consequences = set() sv_types = set() if filters.get('gene_ids'): genes = set([gene_id.strip() for gene_id in filters['gene_ids']]) if filters.get('consequence'): consequences = set(filters['consequence']) if filters.get('sv_types'): sv_types = set(filters['sv_types']) logger.info("Get variants from {0}".format(vcf_file_path)) if filters.get('range'): range_str = "{0}:{1}-{2}".format( filters['range']['chromosome'], filters['range']['start'], filters['range']['end']) vcf = VCF(vcf_file_path) handle = vcf(range_str) else: handle = VCF(vcf_file_path) for variant in handle: variant_line = str(variant) keep_variant = True if genes and keep_variant: keep_variant = False for gene in genes: if "{0}".format(gene) in variant_line: keep_variant = True break if consequences and keep_variant: keep_variant = False for consequence in consequences: if consequence in variant_line: keep_variant = True break if sv_types and keep_variant: keep_variant = False for sv_type in sv_types: if sv_type in variant_line: keep_variant = True break if keep_variant: yield variant
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:group; 3, parameters; 3, 4; 3, 5; 4, identifier:iterable; 5, identifier:key; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, for_statement; 9, 10; 9, 13; 9, 26; 10, pattern_list; 10, 11; 10, 12; 11, identifier:_; 12, identifier:grouped; 13, call; 13, 14; 13, 15; 14, identifier:groupby; 15, argument_list; 15, 16; 15, 23; 16, call; 16, 17; 16, 18; 17, identifier:sorted; 18, argument_list; 18, 19; 18, 20; 19, identifier:iterable; 20, keyword_argument; 20, 21; 20, 22; 21, identifier:key; 22, identifier:key; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:key; 25, identifier:key; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, yield; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:list; 31, argument_list; 31, 32; 32, identifier:grouped
def group(iterable, key): """ groupby which sorts the input, discards the key and returns the output as a sequence of lists. """ for _, grouped in groupby(sorted(iterable, key=key), key=key): yield list(grouped)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:run; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 15; 5, 33; 5, 44; 5, 50; 5, 56; 5, 65; 5, 81; 5, 101; 5, 124; 5, 244; 5, 304; 6, expression_statement; 6, 7; 7, string:'''Run the LaTeX compilation.'''; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:old_dir; 14, list:[]; 15, if_statement; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:opt; 20, identifier:clean; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:old_dir; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:os; 30, identifier:listdir; 31, argument_list; 31, 32; 32, string:'.'; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 39; 35, pattern_list; 35, 36; 35, 37; 35, 38; 36, identifier:cite_counter; 37, identifier:toc_file; 38, identifier:gloss_files; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_read_latex_files; 43, argument_list; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:latex_run; 49, argument_list; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:read_glossaries; 55, argument_list; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:gloss_changed; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:makeindex_runs; 63, argument_list; 63, 64; 64, identifier:gloss_files; 65, if_statement; 65, 66; 65, 74; 66, boolean_operator:or; 66, 67; 66, 68; 67, identifier:gloss_changed; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:_is_toc_changed; 72, argument_list; 72, 73; 73, identifier:toc_file; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:latex_run; 80, argument_list; 81, if_statement; 81, 82; 81, 88; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:_need_bib_run; 86, argument_list; 86, 87; 87, identifier:cite_counter; 88, block; 88, 89; 88, 95; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:bibtex_run; 94, argument_list; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:latex_run; 100, argument_list; 101, while_statement; 101, 102; 101, 108; 102, parenthesized_expression; 102, 103; 103, comparison_operator:<; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:latex_run_counter; 107, identifier:MAX_RUNS; 108, block; 108, 109; 108, 118; 109, if_statement; 109, 110; 109, 116; 110, not_operator; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:need_latex_rerun; 115, argument_list; 116, block; 116, 117; 117, break_statement; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:latex_run; 123, argument_list; 124, if_statement; 124, 125; 124, 130; 125, attribute; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:opt; 129, identifier:check_cite; 130, block; 130, 131; 130, 137; 130, 160; 130, 186; 130, 209; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:cites; 134, call; 134, 135; 134, 136; 135, identifier:set; 136, argument_list; 137, with_statement; 137, 138; 137, 151; 138, with_clause; 138, 139; 139, with_item; 139, 140; 140, as_pattern; 140, 141; 140, 149; 141, call; 141, 142; 141, 143; 142, identifier:open; 143, argument_list; 143, 144; 144, binary_operator:%; 144, 145; 144, 146; 145, string:'%s.aux'; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:project_name; 149, as_pattern_target; 149, 150; 150, identifier:fobj; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:aux_content; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:fobj; 158, identifier:read; 159, argument_list; 160, for_statement; 160, 161; 160, 162; 160, 168; 161, identifier:match; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:BIBCITE_PATTERN; 165, identifier:finditer; 166, argument_list; 166, 167; 167, identifier:aux_content; 168, block; 168, 169; 168, 179; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:name; 172, subscript; 172, 173; 172, 178; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:match; 176, identifier:groups; 177, argument_list; 178, integer:0; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:cites; 183, identifier:add; 184, argument_list; 184, 185; 185, identifier:name; 186, with_statement; 186, 187; 186, 200; 187, with_clause; 187, 188; 188, with_item; 188, 189; 189, as_pattern; 189, 190; 189, 198; 190, call; 190, 191; 190, 192; 191, identifier:open; 192, argument_list; 192, 193; 193, binary_operator:%; 193, 194; 193, 195; 194, string:'%s.bib'; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:bib_file; 198, as_pattern_target; 198, 199; 199, identifier:fobj; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:bib_content; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:fobj; 207, identifier:read; 208, argument_list; 209, for_statement; 209, 210; 209, 211; 209, 217; 210, identifier:match; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:BIBENTRY_PATTERN; 214, identifier:finditer; 215, argument_list; 215, 216; 216, identifier:bib_content; 217, block; 217, 218; 217, 228; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:name; 221, subscript; 221, 222; 221, 227; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:match; 225, identifier:groups; 226, argument_list; 227, integer:0; 228, if_statement; 228, 229; 228, 232; 229, comparison_operator:not; 229, 230; 229, 231; 230, identifier:name; 231, identifier:cites; 232, block; 232, 233; 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:log; 239, identifier:info; 240, argument_list; 240, 241; 241, binary_operator:%; 241, 242; 241, 243; 242, string:'Bib entry not cited: "%s"'; 243, identifier:name; 244, if_statement; 244, 245; 244, 250; 245, attribute; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:opt; 249, identifier:clean; 250, block; 250, 251; 250, 255; 250, 266; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:ending; 254, string:'.dvi'; 255, if_statement; 255, 256; 255, 261; 256, attribute; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:self; 259, identifier:opt; 260, identifier:pdf; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:ending; 265, string:'.pdf'; 266, for_statement; 266, 267; 266, 268; 266, 274; 267, identifier:fname; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:os; 271, identifier:listdir; 272, argument_list; 272, 273; 273, string:'.'; 274, block; 274, 275; 275, if_statement; 275, 276; 275, 290; 276, not_operator; 276, 277; 277, parenthesized_expression; 277, 278; 278, boolean_operator:or; 278, 279; 278, 284; 279, comparison_operator:in; 279, 280; 279, 281; 280, identifier:fname; 281, attribute; 281, 282; 281, 283; 282, identifier:self; 283, identifier:old_dir; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:fname; 287, identifier:endswith; 288, argument_list; 288, 289; 289, identifier:ending; 290, block; 290, 291; 291, try_statement; 291, 292; 291, 300; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:os; 297, identifier:remove; 298, argument_list; 298, 299; 299, identifier:fname; 300, except_clause; 300, 301; 300, 302; 301, identifier:IOError; 302, block; 302, 303; 303, pass_statement; 304, if_statement; 304, 305; 304, 310; 305, attribute; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:self; 308, identifier:opt; 309, identifier:preview; 310, block; 310, 311; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:self; 315, identifier:open_preview; 316, argument_list
def run(self): '''Run the LaTeX compilation.''' # store files self.old_dir = [] if self.opt.clean: self.old_dir = os.listdir('.') cite_counter, toc_file, gloss_files = self._read_latex_files() self.latex_run() self.read_glossaries() gloss_changed = self.makeindex_runs(gloss_files) if gloss_changed or self._is_toc_changed(toc_file): self.latex_run() if self._need_bib_run(cite_counter): self.bibtex_run() self.latex_run() while (self.latex_run_counter < MAX_RUNS): if not self.need_latex_rerun(): break self.latex_run() if self.opt.check_cite: cites = set() with open('%s.aux' % self.project_name) as fobj: aux_content = fobj.read() for match in BIBCITE_PATTERN.finditer(aux_content): name = match.groups()[0] cites.add(name) with open('%s.bib' % self.bib_file) as fobj: bib_content = fobj.read() for match in BIBENTRY_PATTERN.finditer(bib_content): name = match.groups()[0] if name not in cites: self.log.info('Bib entry not cited: "%s"' % name) if self.opt.clean: ending = '.dvi' if self.opt.pdf: ending = '.pdf' for fname in os.listdir('.'): if not (fname in self.old_dir or fname.endswith(ending)): try: os.remove(fname) except IOError: pass if self.opt.preview: self.open_preview()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:embed_ising; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:source_linear; 5, identifier:source_quadratic; 6, identifier:embedding; 7, identifier:target_adjacency; 8, default_parameter; 8, 9; 8, 10; 9, identifier:chain_strength; 10, float:1.0; 11, block; 11, 12; 11, 14; 11, 15; 11, 37; 11, 38; 11, 39; 11, 49; 11, 138; 11, 139; 11, 140; 11, 144; 11, 312; 11, 313; 11, 317; 11, 336; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:unused; 18, binary_operator:-; 18, 19; 18, 24; 19, set_comprehension; 19, 20; 19, 21; 20, identifier:v; 21, for_in_clause; 21, 22; 21, 23; 22, identifier:v; 23, identifier:target_adjacency; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, call; 26, 27; 26, 28; 27, identifier:set; 28, argument_list; 29, identifier:union; 30, argument_list; 30, 31; 31, list_splat; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:embedding; 35, identifier:values; 36, argument_list; 37, comment; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:target_linear; 42, dictionary_comprehension; 42, 43; 42, 46; 43, pair; 43, 44; 43, 45; 44, identifier:v; 45, float:0.; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:v; 48, identifier:target_adjacency; 49, for_statement; 49, 50; 49, 53; 49, 57; 50, pattern_list; 50, 51; 50, 52; 51, identifier:v; 52, identifier:bias; 53, call; 53, 54; 53, 55; 54, identifier:iteritems; 55, argument_list; 55, 56; 56, identifier:source_linear; 57, block; 57, 58; 57, 104; 57, 113; 58, try_statement; 58, 59; 58, 66; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:chain_variables; 63, subscript; 63, 64; 63, 65; 64, identifier:embedding; 65, identifier:v; 66, except_clause; 66, 67; 66, 68; 66, 69; 66, 70; 66, 71; 67, identifier:KeyError; 68, comment; 69, comment; 70, comment; 71, block; 71, 72; 71, 98; 72, try_statement; 72, 73; 72, 85; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:embedding; 78, identifier:v; 79, set; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:unused; 83, identifier:pop; 84, argument_list; 85, except_clause; 85, 86; 85, 87; 86, identifier:KeyError; 87, block; 87, 88; 88, raise_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:ValueError; 91, argument_list; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, string:'no embedding provided for source variable {}'; 95, identifier:format; 96, argument_list; 96, 97; 97, identifier:v; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:chain_variables; 101, subscript; 101, 102; 101, 103; 102, identifier:embedding; 103, identifier:v; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:b; 107, binary_operator:/; 107, 108; 107, 109; 108, identifier:bias; 109, call; 109, 110; 109, 111; 110, identifier:len; 111, argument_list; 111, 112; 112, identifier:chain_variables; 113, for_statement; 113, 114; 113, 115; 113, 116; 114, identifier:s; 115, identifier:chain_variables; 116, block; 116, 117; 117, try_statement; 117, 118; 117, 125; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, augmented_assignment:+=; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:target_linear; 123, identifier:s; 124, identifier:b; 125, except_clause; 125, 126; 125, 127; 126, identifier:KeyError; 127, block; 127, 128; 128, raise_statement; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:ValueError; 131, argument_list; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, string:'chain variable {} not in target_adjacency'; 135, identifier:format; 136, argument_list; 136, 137; 137, identifier:s; 138, comment; 139, comment; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:target_quadratic; 143, dictionary; 144, for_statement; 144, 145; 144, 150; 144, 154; 145, pattern_list; 145, 146; 145, 149; 146, tuple_pattern; 146, 147; 146, 148; 147, identifier:u; 148, identifier:v; 149, identifier:bias; 150, call; 150, 151; 150, 152; 151, identifier:iteritems; 152, argument_list; 152, 153; 153, identifier:source_quadratic; 154, block; 154, 155; 154, 161; 154, 176; 154, 191; 154, 240; 154, 255; 154, 264; 154, 265; 154, 266; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:edges; 158, call; 158, 159; 158, 160; 159, identifier:set; 160, argument_list; 161, if_statement; 161, 162; 161, 165; 162, comparison_operator:not; 162, 163; 162, 164; 163, identifier:u; 164, identifier:embedding; 165, block; 165, 166; 166, raise_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:ValueError; 169, argument_list; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, string:'no embedding provided for source variable {}'; 173, identifier:format; 174, argument_list; 174, 175; 175, identifier:u; 176, if_statement; 176, 177; 176, 180; 177, comparison_operator:not; 177, 178; 177, 179; 178, identifier:v; 179, identifier:embedding; 180, block; 180, 181; 181, raise_statement; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:ValueError; 184, argument_list; 184, 185; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, string:'no embedding provided for source variable {}'; 188, identifier:format; 189, argument_list; 189, 190; 190, identifier:v; 191, for_statement; 191, 192; 191, 193; 191, 196; 192, identifier:s; 193, subscript; 193, 194; 193, 195; 194, identifier:embedding; 195, identifier:u; 196, block; 196, 197; 197, for_statement; 197, 198; 197, 199; 197, 202; 198, identifier:t; 199, subscript; 199, 200; 199, 201; 200, identifier:embedding; 201, identifier:v; 202, block; 202, 203; 203, try_statement; 203, 204; 203, 227; 204, block; 204, 205; 205, if_statement; 205, 206; 205, 217; 206, boolean_operator:and; 206, 207; 206, 212; 207, comparison_operator:in; 207, 208; 207, 209; 208, identifier:s; 209, subscript; 209, 210; 209, 211; 210, identifier:target_adjacency; 211, identifier:t; 212, comparison_operator:not; 212, 213; 212, 216; 213, tuple; 213, 214; 213, 215; 214, identifier:t; 215, identifier:s; 216, identifier:edges; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:edges; 222, identifier:add; 223, argument_list; 223, 224; 224, tuple; 224, 225; 224, 226; 225, identifier:s; 226, identifier:t; 227, except_clause; 227, 228; 227, 229; 228, identifier:KeyError; 229, block; 229, 230; 230, raise_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:ValueError; 233, argument_list; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, string:'chain variable {} not in target_adjacency'; 237, identifier:format; 238, argument_list; 238, 239; 239, identifier:s; 240, if_statement; 240, 241; 240, 243; 241, not_operator; 241, 242; 242, identifier:edges; 243, block; 243, 244; 244, raise_statement; 244, 245; 245, call; 245, 246; 245, 247; 246, identifier:ValueError; 247, argument_list; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, string:"no edges in target graph between source variables {}, {}"; 251, identifier:format; 252, argument_list; 252, 253; 252, 254; 253, identifier:u; 254, identifier:v; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:b; 258, binary_operator:/; 258, 259; 258, 260; 259, identifier:bias; 260, call; 260, 261; 260, 262; 261, identifier:len; 262, argument_list; 262, 263; 263, identifier:edges; 264, comment; 265, comment; 266, for_statement; 266, 267; 266, 270; 266, 271; 267, pattern_list; 267, 268; 267, 269; 268, identifier:s; 269, identifier:t; 270, identifier:edges; 271, block; 271, 272; 272, if_statement; 272, 273; 272, 278; 272, 287; 272, 302; 273, comparison_operator:in; 273, 274; 273, 277; 274, tuple; 274, 275; 274, 276; 275, identifier:s; 276, identifier:t; 277, identifier:target_quadratic; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, augmented_assignment:+=; 280, 281; 280, 286; 281, subscript; 281, 282; 281, 283; 282, identifier:target_quadratic; 283, tuple; 283, 284; 283, 285; 284, identifier:s; 285, identifier:t; 286, identifier:b; 287, elif_clause; 287, 288; 287, 293; 288, comparison_operator:in; 288, 289; 288, 292; 289, tuple; 289, 290; 289, 291; 290, identifier:t; 291, identifier:s; 292, identifier:target_quadratic; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, augmented_assignment:+=; 295, 296; 295, 301; 296, subscript; 296, 297; 296, 298; 297, identifier:target_quadratic; 298, tuple; 298, 299; 298, 300; 299, identifier:t; 300, identifier:s; 301, identifier:b; 302, else_clause; 302, 303; 303, block; 303, 304; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 311; 306, subscript; 306, 307; 306, 308; 307, identifier:target_quadratic; 308, tuple; 308, 309; 308, 310; 309, identifier:s; 310, identifier:t; 311, identifier:b; 312, comment; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:chain_quadratic; 316, dictionary; 317, for_statement; 317, 318; 317, 319; 317, 323; 318, identifier:chain; 319, call; 319, 320; 319, 321; 320, identifier:itervalues; 321, argument_list; 321, 322; 322, identifier:embedding; 323, block; 323, 324; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:chain_quadratic; 328, identifier:update; 329, argument_list; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:chain_to_quadratic; 332, argument_list; 332, 333; 332, 334; 332, 335; 333, identifier:chain; 334, identifier:target_adjacency; 335, identifier:chain_strength; 336, return_statement; 336, 337; 337, expression_list; 337, 338; 337, 339; 337, 340; 338, identifier:target_linear; 339, identifier:target_quadratic; 340, identifier:chain_quadratic
def embed_ising(source_linear, source_quadratic, embedding, target_adjacency, chain_strength=1.0): """Embeds a logical Ising model onto another graph via an embedding. Args: source_linear (dict): The linear biases to be embedded. Should be a dict of the form {v: bias, ...} where v is a variable in the source model and bias is the linear bias associated with v. source_quadratic (dict): The quadratic biases to be embedded. Should be a dict of the form {(u, v): bias, ...} where u, v are variables in the source model and bias is the quadratic bias associated with (u, v). embedding (dict): The mapping from the source graph to the target graph. Should be of the form {v: {s, ...}, ...} where v is a variable in the source model and s is a variable in the target model. target_adjacency (dict/:class:`networkx.Graph`): The adjacency dict of the target graph. Should be a dict of the form {s: Ns, ...} where s is a variable in the target graph and Ns is the set of neighbours of s. chain_strength (float, optional): The quadratic bias that should be used to create chains. Returns: (dict, dict, dict): A 3-tuple containing: dict: The linear biases of the target problem. In the form {s: bias, ...} where s is a node in the target graph and bias is the associated linear bias. dict: The quadratic biases of the target problem. A dict of the form {(s, t): bias, ...} where (s, t) is an edge in the target graph and bias is the associated quadratic bias. dict: The quadratic biases that induce the variables in the target problem to act as one. A dict of the form {(s, t): -chain_strength, ...} which is the quadratic biases associated with the chains. Examples: >>> source_linear = {'a': 1, 'b': 1} >>> source_quadratic = {('a', 'b'): -1} >>> embedding = {'a': [0, 1], 'b': [2]} >>> target_adjacency = {0: {1, 2}, 1: {0, 2}, 2: {0, 1}} >>> target_linear, target_quadratic, chain_quadratic = embed_ising( ... source_linear, source_quadratic, embedding, target_adjacency) >>> target_linear {0: 0.5, 1: 0.5, 2: 1.0} >>> target_quadratic {(0, 2): -0.5, (1, 2): -0.5} >>> chain_quadratic {(0, 1): -1.0} """ # store variables in the target graph that the embedding hasn't used unused = {v for v in target_adjacency} - set().union(*embedding.values()) # ok, let's begin with the linear biases. # we spread the value of h evenly over the chain target_linear = {v: 0. for v in target_adjacency} for v, bias in iteritems(source_linear): try: chain_variables = embedding[v] except KeyError: # if our embedding doesn't deal with this variable, assume it's an isolated vertex and embed it to one of # the unused variables. if this turns out to not be an isolated vertex, it will be caught below when # handling quadratic biases try: embedding[v] = {unused.pop()} except KeyError: raise ValueError('no embedding provided for source variable {}'.format(v)) chain_variables = embedding[v] b = bias / len(chain_variables) for s in chain_variables: try: target_linear[s] += b except KeyError: raise ValueError('chain variable {} not in target_adjacency'.format(s)) # next up the quadratic biases. # We spread the quadratic biases evenly over the edges target_quadratic = {} for (u, v), bias in iteritems(source_quadratic): edges = set() if u not in embedding: raise ValueError('no embedding provided for source variable {}'.format(u)) if v not in embedding: raise ValueError('no embedding provided for source variable {}'.format(v)) for s in embedding[u]: for t in embedding[v]: try: if s in target_adjacency[t] and (t, s) not in edges: edges.add((s, t)) except KeyError: raise ValueError('chain variable {} not in target_adjacency'.format(s)) if not edges: raise ValueError("no edges in target graph between source variables {}, {}".format(u, v)) b = bias / len(edges) # in some cases the logical J can have (u, v) and (v, u) as inputs, so make # sure we are not doubling them up with our choice of ordering for s, t in edges: if (s, t) in target_quadratic: target_quadratic[(s, t)] += b elif (t, s) in target_quadratic: target_quadratic[(t, s)] += b else: target_quadratic[(s, t)] = b # finally we need to connect the nodes in the chains chain_quadratic = {} for chain in itervalues(embedding): chain_quadratic.update(chain_to_quadratic(chain, target_adjacency, chain_strength)) return target_linear, target_quadratic, chain_quadratic
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:fit_richness; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:atol; 7, float:1.e-3; 8, default_parameter; 8, 9; 8, 10; 9, identifier:maxiter; 10, integer:50; 11, block; 11, 12; 11, 14; 11, 15; 11, 16; 11, 42; 11, 65; 11, 84; 11, 85; 11, 105; 11, 125; 11, 129; 11, 133; 11, 245; 11, 254; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, comment; 16, if_statement; 16, 17; 16, 29; 17, call; 17, 18; 17, 28; 18, attribute; 18, 19; 18, 27; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:np; 22, identifier:isnan; 23, argument_list; 23, 24; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:u; 27, identifier:any; 28, argument_list; 29, block; 29, 30; 29, 37; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:logger; 34, identifier:warning; 35, argument_list; 35, 36; 36, string:"NaN signal probability found"; 37, return_statement; 37, 38; 38, expression_list; 38, 39; 38, 40; 38, 41; 39, float:0.; 40, float:0.; 41, None; 42, if_statement; 42, 43; 42, 52; 43, not_operator; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:np; 47, identifier:any; 48, argument_list; 48, 49; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:u; 52, block; 52, 53; 52, 60; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:logger; 57, identifier:warning; 58, argument_list; 58, 59; 59, string:"Signal probability is zero for all objects"; 60, return_statement; 60, 61; 61, expression_list; 61, 62; 61, 63; 61, 64; 62, float:0.; 63, float:0.; 64, None; 65, if_statement; 65, 66; 65, 71; 66, comparison_operator:==; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:f; 70, integer:0; 71, block; 71, 72; 71, 79; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:logger; 76, identifier:warning; 77, argument_list; 77, 78; 78, string:"Observable fraction is zero"; 79, return_statement; 79, 80; 80, expression_list; 80, 81; 80, 82; 80, 83; 81, float:0.; 82, float:0.; 83, None; 84, comment; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:richness; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:np; 91, identifier:array; 92, argument_list; 92, 93; 93, list:[0., 1./self.f, 10./self.f]; 93, 94; 93, 95; 93, 100; 94, float:0.; 95, binary_operator:/; 95, 96; 95, 97; 96, float:1.; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:f; 100, binary_operator:/; 100, 101; 100, 102; 101, float:10.; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:f; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:loglike; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:np; 111, identifier:array; 112, argument_list; 112, 113; 113, list_comprehension; 113, 114; 113, 122; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:value; 118, argument_list; 118, 119; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:richness; 121, identifier:r; 122, for_in_clause; 122, 123; 122, 124; 123, identifier:r; 124, identifier:richness; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:found_maximum; 128, False; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:iteration; 132, integer:0; 133, while_statement; 133, 134; 133, 136; 134, not_operator; 134, 135; 135, identifier:found_maximum; 136, block; 136, 137; 136, 153; 136, 228; 136, 232; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:parabola; 140, call; 140, 141; 140, 148; 141, attribute; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:ugali; 145, identifier:utils; 146, identifier:parabola; 147, identifier:Parabola; 148, argument_list; 148, 149; 148, 150; 149, identifier:richness; 150, binary_operator:*; 150, 151; 150, 152; 151, float:2.; 152, identifier:loglike; 153, if_statement; 153, 154; 153, 159; 153, 164; 154, comparison_operator:<; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:parabola; 157, identifier:vertex_x; 158, float:0.; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:found_maximum; 163, True; 164, else_clause; 164, 165; 165, block; 165, 166; 165, 178; 165, 198; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:richness; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:np; 172, identifier:append; 173, argument_list; 173, 174; 173, 175; 174, identifier:richness; 175, attribute; 175, 176; 175, 177; 176, identifier:parabola; 177, identifier:vertex_x; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:loglike; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:np; 184, identifier:append; 185, argument_list; 185, 186; 185, 187; 186, identifier:loglike; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:self; 190, identifier:value; 191, argument_list; 191, 192; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:richness; 194, subscript; 194, 195; 194, 196; 195, identifier:richness; 196, unary_operator:-; 196, 197; 197, integer:1; 198, if_statement; 198, 199; 198, 223; 199, comparison_operator:<; 199, 200; 199, 222; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:np; 203, identifier:fabs; 204, argument_list; 204, 205; 205, binary_operator:-; 205, 206; 205, 210; 206, subscript; 206, 207; 206, 208; 207, identifier:loglike; 208, unary_operator:-; 208, 209; 209, integer:1; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:np; 213, identifier:max; 214, argument_list; 214, 215; 215, subscript; 215, 216; 215, 217; 216, identifier:loglike; 217, slice; 217, 218; 217, 219; 217, 220; 218, integer:0; 219, colon; 220, unary_operator:-; 220, 221; 221, integer:1; 222, identifier:atol; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:found_maximum; 227, True; 228, expression_statement; 228, 229; 229, augmented_assignment:+=; 229, 230; 229, 231; 230, identifier:iteration; 231, integer:1; 232, if_statement; 232, 233; 232, 236; 233, comparison_operator:>; 233, 234; 233, 235; 234, identifier:iteration; 235, identifier:maxiter; 236, block; 236, 237; 236, 244; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:logger; 241, identifier:warning; 242, argument_list; 242, 243; 243, string:"Maximum number of iterations reached"; 244, break_statement; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:index; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:np; 251, identifier:argmax; 252, argument_list; 252, 253; 253, identifier:loglike; 254, return_statement; 254, 255; 255, expression_list; 255, 256; 255, 259; 255, 262; 256, subscript; 256, 257; 256, 258; 257, identifier:loglike; 258, identifier:index; 259, subscript; 259, 260; 259, 261; 260, identifier:richness; 261, identifier:index; 262, identifier:parabola
def fit_richness(self, atol=1.e-3, maxiter=50): """ Maximize the log-likelihood as a function of richness. ADW 2018-06-04: Does it make sense to set the richness to the mle? Parameters: ----------- atol : absolute tolerence for conversion maxiter : maximum number of iterations Returns: -------- loglike, richness, parabola : the maximum loglike, the mle, and the parabola """ # Check whether the signal probability for all objects are zero # This can occur for finite kernels on the edge of the survey footprint if np.isnan(self.u).any(): logger.warning("NaN signal probability found") return 0., 0., None if not np.any(self.u): logger.warning("Signal probability is zero for all objects") return 0., 0., None if self.f == 0: logger.warning("Observable fraction is zero") return 0., 0., None # Richness corresponding to 0, 1, and 10 observable stars richness = np.array([0., 1./self.f, 10./self.f]) loglike = np.array([self.value(richness=r) for r in richness]) found_maximum = False iteration = 0 while not found_maximum: parabola = ugali.utils.parabola.Parabola(richness, 2.*loglike) if parabola.vertex_x < 0.: found_maximum = True else: richness = np.append(richness, parabola.vertex_x) loglike = np.append(loglike, self.value(richness=richness[-1])) if np.fabs(loglike[-1] - np.max(loglike[0: -1])) < atol: found_maximum = True iteration+=1 if iteration > maxiter: logger.warning("Maximum number of iterations reached") break index = np.argmax(loglike) return loglike[index], richness[index], parabola
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:make_request; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:url; 5, default_parameter; 5, 6; 5, 7; 6, identifier:method; 7, string:'GET'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:query; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:body; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:auth; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:timeout; 19, integer:10; 20, default_parameter; 20, 21; 20, 22; 21, identifier:client; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:macaroons; 25, None; 26, block; 26, 27; 26, 29; 26, 33; 26, 43; 26, 44; 26, 73; 26, 74; 26, 125; 26, 136; 26, 151; 26, 163; 26, 164; 26, 207; 26, 208; 26, 269; 26, 270; 26, 278; 26, 279; 27, expression_statement; 27, 28; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:headers; 32, dictionary; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:kwargs; 36, dictionary; 36, 37; 36, 40; 37, pair; 37, 38; 37, 39; 38, string:'timeout'; 39, identifier:timeout; 40, pair; 40, 41; 40, 42; 41, string:'headers'; 42, identifier:headers; 43, comment; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:body; 47, None; 48, block; 48, 49; 48, 67; 49, if_statement; 49, 50; 49, 57; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:body; 54, attribute; 54, 55; 54, 56; 55, identifier:collections; 56, identifier:Mapping; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:body; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:json; 64, identifier:dumps; 65, argument_list; 65, 66; 66, identifier:body; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 72; 69, subscript; 69, 70; 69, 71; 70, identifier:kwargs; 71, string:'data'; 72, identifier:body; 73, comment; 74, if_statement; 74, 75; 74, 80; 74, 98; 74, 113; 75, comparison_operator:in; 75, 76; 75, 77; 76, identifier:method; 77, tuple; 77, 78; 77, 79; 78, string:'GET'; 79, string:'HEAD'; 80, block; 80, 81; 81, if_statement; 81, 82; 81, 83; 82, identifier:query; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:url; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, string:'{}?{}'; 90, identifier:format; 91, argument_list; 91, 92; 91, 93; 92, identifier:url; 93, call; 93, 94; 93, 95; 94, identifier:urlencode; 95, argument_list; 95, 96; 95, 97; 96, identifier:query; 97, True; 98, elif_clause; 98, 99; 98, 106; 99, comparison_operator:in; 99, 100; 99, 101; 100, identifier:method; 101, tuple; 101, 102; 101, 103; 101, 104; 101, 105; 102, string:'DELETE'; 103, string:'PATCH'; 104, string:'POST'; 105, string:'PUT'; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:headers; 111, string:'Content-Type'; 112, string:'application/json'; 113, else_clause; 113, 114; 114, block; 114, 115; 115, raise_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:ValueError; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, string:'invalid method {}'; 122, identifier:format; 123, argument_list; 123, 124; 124, identifier:method; 125, if_statement; 125, 126; 125, 129; 126, comparison_operator:is; 126, 127; 126, 128; 127, identifier:macaroons; 128, None; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:headers; 134, string:'Macaroons'; 135, identifier:macaroons; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:kwargs; 140, string:'auth'; 141, conditional_expression:if; 141, 142; 141, 143; 141, 146; 142, identifier:auth; 143, comparison_operator:is; 143, 144; 143, 145; 144, identifier:client; 145, None; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:client; 149, identifier:auth; 150, argument_list; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:api_method; 154, call; 154, 155; 154, 156; 155, identifier:getattr; 156, argument_list; 156, 157; 156, 158; 157, identifier:requests; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:method; 161, identifier:lower; 162, argument_list; 163, comment; 164, try_statement; 164, 165; 164, 175; 164, 188; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:response; 169, call; 169, 170; 169, 171; 170, identifier:api_method; 171, argument_list; 171, 172; 171, 173; 172, identifier:url; 173, dictionary_splat; 173, 174; 174, identifier:kwargs; 175, except_clause; 175, 176; 175, 181; 176, attribute; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:requests; 179, identifier:exceptions; 180, identifier:Timeout; 181, block; 181, 182; 182, raise_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:timeout_error; 185, argument_list; 185, 186; 185, 187; 186, identifier:url; 187, identifier:timeout; 188, except_clause; 188, 189; 188, 193; 189, as_pattern; 189, 190; 189, 191; 190, identifier:Exception; 191, as_pattern_target; 191, 192; 192, identifier:err; 193, block; 193, 194; 193, 202; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:msg; 197, call; 197, 198; 197, 199; 198, identifier:_server_error_message; 199, argument_list; 199, 200; 199, 201; 200, identifier:url; 201, identifier:err; 202, raise_statement; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:ServerError; 205, argument_list; 205, 206; 206, identifier:msg; 207, comment; 208, try_statement; 208, 209; 208, 216; 208, 244; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:response; 214, identifier:raise_for_status; 215, argument_list; 216, except_clause; 216, 217; 216, 221; 217, as_pattern; 217, 218; 217, 219; 218, identifier:HTTPError; 219, as_pattern_target; 219, 220; 220, identifier:err; 221, block; 221, 222; 221, 234; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:msg; 225, call; 225, 226; 225, 227; 226, identifier:_server_error_message; 227, argument_list; 227, 228; 227, 229; 228, identifier:url; 229, attribute; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:err; 232, identifier:response; 233, identifier:text; 234, raise_statement; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:ServerError; 237, argument_list; 237, 238; 237, 243; 238, attribute; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:err; 241, identifier:response; 242, identifier:status_code; 243, identifier:msg; 244, except_clause; 244, 245; 244, 253; 245, as_pattern; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:requests; 249, identifier:exceptions; 250, identifier:RequestException; 251, as_pattern_target; 251, 252; 252, identifier:err; 253, block; 253, 254; 253, 264; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:msg; 257, call; 257, 258; 257, 259; 258, identifier:_server_error_message; 259, argument_list; 259, 260; 259, 261; 260, identifier:url; 261, attribute; 261, 262; 261, 263; 262, identifier:err; 263, identifier:message; 264, raise_statement; 264, 265; 265, call; 265, 266; 265, 267; 266, identifier:ServerError; 267, argument_list; 267, 268; 268, identifier:msg; 269, comment; 270, if_statement; 270, 271; 270, 275; 271, not_operator; 271, 272; 272, attribute; 272, 273; 272, 274; 273, identifier:response; 274, identifier:content; 275, block; 275, 276; 276, return_statement; 276, 277; 277, dictionary; 278, comment; 279, try_statement; 279, 280; 279, 287; 280, block; 280, 281; 281, return_statement; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:response; 285, identifier:json; 286, argument_list; 287, except_clause; 287, 288; 287, 292; 288, as_pattern; 288, 289; 288, 290; 289, identifier:Exception; 290, as_pattern_target; 290, 291; 291, identifier:err; 292, block; 292, 293; 292, 303; 292, 310; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:msg; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, string:'Error decoding JSON response: {} message: {}'; 299, identifier:format; 300, argument_list; 300, 301; 300, 302; 301, identifier:url; 302, identifier:err; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:log; 307, identifier:error; 308, argument_list; 308, 309; 309, identifier:msg; 310, raise_statement; 310, 311; 311, call; 311, 312; 311, 313; 312, identifier:ServerError; 313, argument_list; 313, 314; 314, identifier:msg
def make_request( url, method='GET', query=None, body=None, auth=None, timeout=10, client=None, macaroons=None): """Make a request with the provided data. @param url The url to make the request to. @param method The HTTP request method (defaulting to "GET"). @param query A dict of the query key and values. @param body The optional body as a string or as a JSON decoded dict. @param auth The optional username and password as a tuple, not used if client is not None @param timeout The request timeout in seconds, defaulting to 10 seconds. @param client (httpbakery.Client) holds a context for making http requests with macaroons. @param macaroons Optional JSON serialized, base64 encoded macaroons to be included in the request header. POST/PUT request bodies are assumed to be in JSON format. Return the response content as a JSON decoded object, or an empty dict. Raise a ServerError if a problem occurs in the request/response process. Raise a ValueError if invalid parameters are provided. """ headers = {} kwargs = {'timeout': timeout, 'headers': headers} # Handle the request body. if body is not None: if isinstance(body, collections.Mapping): body = json.dumps(body) kwargs['data'] = body # Handle request methods. if method in ('GET', 'HEAD'): if query: url = '{}?{}'.format(url, urlencode(query, True)) elif method in ('DELETE', 'PATCH', 'POST', 'PUT'): headers['Content-Type'] = 'application/json' else: raise ValueError('invalid method {}'.format(method)) if macaroons is not None: headers['Macaroons'] = macaroons kwargs['auth'] = auth if client is None else client.auth() api_method = getattr(requests, method.lower()) # Perform the request. try: response = api_method(url, **kwargs) except requests.exceptions.Timeout: raise timeout_error(url, timeout) except Exception as err: msg = _server_error_message(url, err) raise ServerError(msg) # Handle error responses. try: response.raise_for_status() except HTTPError as err: msg = _server_error_message(url, err.response.text) raise ServerError(err.response.status_code, msg) except requests.exceptions.RequestException as err: msg = _server_error_message(url, err.message) raise ServerError(msg) # Some requests just result in a status with no response body. if not response.content: return {} # Assume the response body is a JSON encoded string. try: return response.json() except Exception as err: msg = 'Error decoding JSON response: {} message: {}'.format(url, err) log.error(msg) raise ServerError(msg)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:time_stops; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 13; 9, not_operator; 9, 10; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:supports_time; 13, block; 13, 14; 14, return_statement; 14, 15; 15, list:[]; 16, if_statement; 16, 17; 16, 24; 16, 327; 17, comparison_operator:==; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:service; 22, identifier:calendar; 23, string:'standard'; 24, block; 24, 25; 24, 33; 24, 41; 24, 48; 24, 290; 24, 325; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:units; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:service; 32, identifier:time_interval_units; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:interval; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:service; 40, identifier:time_interval; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:steps; 44, list:[self.time_start]; 44, 45; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:time_start; 48, if_statement; 48, 49; 48, 55; 48, 102; 48, 179; 49, comparison_operator:in; 49, 50; 49, 51; 50, identifier:units; 51, tuple; 51, 52; 51, 53; 51, 54; 52, string:'years'; 53, string:'decades'; 54, string:'centuries'; 55, block; 55, 56; 55, 84; 56, if_statement; 56, 57; 56, 60; 56, 65; 56, 76; 57, comparison_operator:==; 57, 58; 57, 59; 58, identifier:units; 59, string:'years'; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:years; 64, identifier:interval; 65, elif_clause; 65, 66; 65, 69; 66, comparison_operator:==; 66, 67; 66, 68; 67, identifier:units; 68, string:'decades'; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:years; 73, binary_operator:*; 73, 74; 73, 75; 74, integer:10; 75, identifier:interval; 76, else_clause; 76, 77; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:years; 81, binary_operator:*; 81, 82; 81, 83; 82, integer:100; 83, identifier:interval; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:next_value; 87, lambda; 87, 88; 87, 90; 88, lambda_parameters; 88, 89; 89, identifier:x; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:x; 93, identifier:replace; 94, argument_list; 94, 95; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:year; 97, binary_operator:+; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:x; 100, identifier:year; 101, identifier:years; 102, elif_clause; 102, 103; 102, 106; 103, comparison_operator:==; 103, 104; 103, 105; 104, identifier:units; 105, string:'months'; 106, block; 106, 107; 106, 175; 107, function_definition; 107, 108; 107, 109; 107, 111; 108, function_name:_fn; 109, parameters; 109, 110; 110, identifier:x; 111, block; 111, 112; 111, 129; 111, 142; 111, 160; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:year; 115, binary_operator:+; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:x; 118, identifier:year; 119, binary_operator://; 119, 120; 119, 128; 120, parenthesized_expression; 120, 121; 121, binary_operator:-; 121, 122; 121, 127; 122, binary_operator:+; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:x; 125, identifier:month; 126, identifier:interval; 127, integer:1; 128, integer:12; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:month; 132, boolean_operator:or; 132, 133; 132, 141; 133, binary_operator:%; 133, 134; 133, 140; 134, parenthesized_expression; 134, 135; 135, binary_operator:+; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:x; 138, identifier:month; 139, identifier:interval; 140, integer:12; 141, integer:12; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:day; 145, call; 145, 146; 145, 147; 146, identifier:min; 147, argument_list; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:x; 150, identifier:day; 151, subscript; 151, 152; 151, 159; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:calendar; 155, identifier:monthrange; 156, argument_list; 156, 157; 156, 158; 157, identifier:year; 158, identifier:month; 159, integer:1; 160, return_statement; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:x; 164, identifier:replace; 165, argument_list; 165, 166; 165, 169; 165, 172; 166, keyword_argument; 166, 167; 166, 168; 167, identifier:year; 168, identifier:year; 169, keyword_argument; 169, 170; 169, 171; 170, identifier:month; 171, identifier:month; 172, keyword_argument; 172, 173; 172, 174; 173, identifier:day; 174, identifier:day; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:next_value; 178, identifier:_fn; 179, else_clause; 179, 180; 180, block; 180, 181; 180, 281; 181, if_statement; 181, 182; 181, 185; 181, 195; 181, 209; 181, 223; 181, 237; 181, 251; 181, 265; 182, comparison_operator:==; 182, 183; 182, 184; 183, identifier:units; 184, string:'milliseconds'; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:delta; 189, call; 189, 190; 189, 191; 190, identifier:timedelta; 191, argument_list; 191, 192; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:milliseconds; 194, identifier:interval; 195, elif_clause; 195, 196; 195, 199; 196, comparison_operator:==; 196, 197; 196, 198; 197, identifier:units; 198, string:'seconds'; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:delta; 203, call; 203, 204; 203, 205; 204, identifier:timedelta; 205, argument_list; 205, 206; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:seconds; 208, identifier:interval; 209, elif_clause; 209, 210; 209, 213; 210, comparison_operator:==; 210, 211; 210, 212; 211, identifier:units; 212, string:'minutes'; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:delta; 217, call; 217, 218; 217, 219; 218, identifier:timedelta; 219, argument_list; 219, 220; 220, keyword_argument; 220, 221; 220, 222; 221, identifier:minutes; 222, identifier:interval; 223, elif_clause; 223, 224; 223, 227; 224, comparison_operator:==; 224, 225; 224, 226; 225, identifier:units; 226, string:'hours'; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:delta; 231, call; 231, 232; 231, 233; 232, identifier:timedelta; 233, argument_list; 233, 234; 234, keyword_argument; 234, 235; 234, 236; 235, identifier:hours; 236, identifier:interval; 237, elif_clause; 237, 238; 237, 241; 238, comparison_operator:==; 238, 239; 238, 240; 239, identifier:units; 240, string:'days'; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:delta; 245, call; 245, 246; 245, 247; 246, identifier:timedelta; 247, argument_list; 247, 248; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:days; 250, identifier:interval; 251, elif_clause; 251, 252; 251, 255; 252, comparison_operator:==; 252, 253; 252, 254; 253, identifier:units; 254, string:'weeks'; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:delta; 259, call; 259, 260; 259, 261; 260, identifier:timedelta; 261, argument_list; 261, 262; 262, keyword_argument; 262, 263; 262, 264; 263, identifier:weeks; 264, identifier:interval; 265, else_clause; 265, 266; 266, block; 266, 267; 267, raise_statement; 267, 268; 268, call; 268, 269; 268, 270; 269, identifier:ValidationError; 270, argument_list; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, string:"Service has an invalid time_interval_units: {}"; 274, identifier:format; 275, argument_list; 275, 276; 276, attribute; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:self; 279, identifier:service; 280, identifier:time_interval_units; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:next_value; 284, lambda; 284, 285; 284, 287; 285, lambda_parameters; 285, 286; 286, identifier:x; 287, binary_operator:+; 287, 288; 287, 289; 288, identifier:x; 289, identifier:delta; 290, while_statement; 290, 291; 290, 299; 291, comparison_operator:<; 291, 292; 291, 296; 292, subscript; 292, 293; 292, 294; 293, identifier:steps; 294, unary_operator:-; 294, 295; 295, integer:1; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:time_end; 299, block; 299, 300; 299, 310; 299, 318; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:value; 303, call; 303, 304; 303, 305; 304, identifier:next_value; 305, argument_list; 305, 306; 306, subscript; 306, 307; 306, 308; 307, identifier:steps; 308, unary_operator:-; 308, 309; 309, integer:1; 310, if_statement; 310, 311; 310, 316; 311, comparison_operator:>; 311, 312; 311, 313; 312, identifier:value; 313, attribute; 313, 314; 313, 315; 314, identifier:self; 315, identifier:time_end; 316, block; 316, 317; 317, break_statement; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:steps; 322, identifier:append; 323, argument_list; 323, 324; 324, identifier:value; 325, return_statement; 325, 326; 326, identifier:steps; 327, else_clause; 327, 328; 327, 329; 328, comment; 329, block; 329, 330; 330, raise_statement; 330, 331; 331, identifier:NotImplementedError
def time_stops(self): """ Valid time steps for this service as a list of datetime objects. """ if not self.supports_time: return [] if self.service.calendar == 'standard': units = self.service.time_interval_units interval = self.service.time_interval steps = [self.time_start] if units in ('years', 'decades', 'centuries'): if units == 'years': years = interval elif units == 'decades': years = 10 * interval else: years = 100 * interval next_value = lambda x: x.replace(year=x.year + years) elif units == 'months': def _fn(x): year = x.year + (x.month+interval-1) // 12 month = (x.month+interval) % 12 or 12 day = min(x.day, calendar.monthrange(year, month)[1]) return x.replace(year=year, month=month, day=day) next_value = _fn else: if units == 'milliseconds': delta = timedelta(milliseconds=interval) elif units == 'seconds': delta = timedelta(seconds=interval) elif units == 'minutes': delta = timedelta(minutes=interval) elif units == 'hours': delta = timedelta(hours=interval) elif units == 'days': delta = timedelta(days=interval) elif units == 'weeks': delta = timedelta(weeks=interval) else: raise ValidationError( "Service has an invalid time_interval_units: {}".format(self.service.time_interval_units) ) next_value = lambda x: x + delta while steps[-1] < self.time_end: value = next_value(steps[-1]) if value > self.time_end: break steps.append(value) return steps else: # TODO raise NotImplementedError
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:sort_strings; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:strings; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort_order; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:reverse; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:case_sensitive; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sort_order_first; 16, True; 17, block; 17, 18; 17, 20; 17, 52; 17, 65; 17, 203; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 23; 21, not_operator; 21, 22; 22, identifier:case_sensitive; 23, block; 23, 24; 23, 38; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:sort_order; 27, call; 27, 28; 27, 29; 28, identifier:tuple; 29, generator_expression; 29, 30; 29, 35; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:s; 33, identifier:lower; 34, argument_list; 35, for_in_clause; 35, 36; 35, 37; 36, identifier:s; 37, identifier:sort_order; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:strings; 41, call; 41, 42; 41, 43; 42, identifier:tuple; 43, generator_expression; 43, 44; 43, 49; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:s; 47, identifier:lower; 48, argument_list; 49, for_in_clause; 49, 50; 49, 51; 50, identifier:s; 51, identifier:strings; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:prefix_len; 55, call; 55, 56; 55, 57; 56, identifier:max; 57, generator_expression; 57, 58; 57, 62; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, identifier:s; 62, for_in_clause; 62, 63; 62, 64; 63, identifier:s; 64, identifier:sort_order; 65, function_definition; 65, 66; 65, 67; 65, 73; 66, function_name:compare; 67, parameters; 67, 68; 67, 69; 67, 70; 68, identifier:a; 69, identifier:b; 70, default_parameter; 70, 71; 70, 72; 71, identifier:prefix_len; 72, identifier:prefix_len; 73, block; 73, 74; 73, 179; 74, if_statement; 74, 75; 74, 76; 75, identifier:prefix_len; 76, block; 76, 77; 77, if_statement; 77, 78; 77, 85; 77, 160; 77, 161; 78, comparison_operator:in; 78, 79; 78, 84; 79, subscript; 79, 80; 79, 81; 80, identifier:a; 81, slice; 81, 82; 81, 83; 82, colon; 83, identifier:prefix_len; 84, identifier:sort_order; 85, block; 85, 86; 86, if_statement; 86, 87; 86, 94; 86, 146; 87, comparison_operator:in; 87, 88; 87, 93; 88, subscript; 88, 89; 88, 90; 89, identifier:b; 90, slice; 90, 91; 90, 92; 91, colon; 92, identifier:prefix_len; 93, identifier:sort_order; 94, block; 94, 95; 94, 119; 94, 133; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:comparison; 98, binary_operator:-; 98, 99; 98, 109; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:sort_order; 102, identifier:index; 103, argument_list; 103, 104; 104, subscript; 104, 105; 104, 106; 105, identifier:a; 106, slice; 106, 107; 106, 108; 107, colon; 108, identifier:prefix_len; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:sort_order; 112, identifier:index; 113, argument_list; 113, 114; 114, subscript; 114, 115; 114, 116; 115, identifier:b; 116, slice; 116, 117; 116, 118; 117, colon; 118, identifier:prefix_len; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:comparison; 122, call; 122, 123; 122, 124; 123, identifier:int; 124, argument_list; 124, 125; 125, binary_operator:/; 125, 126; 125, 127; 126, identifier:comparison; 127, call; 127, 128; 127, 129; 128, identifier:abs; 129, argument_list; 129, 130; 130, boolean_operator:or; 130, 131; 130, 132; 131, identifier:comparison; 132, integer:1; 133, if_statement; 133, 134; 133, 135; 134, identifier:comparison; 135, block; 135, 136; 136, return_statement; 136, 137; 137, binary_operator:*; 137, 138; 137, 139; 138, identifier:comparison; 139, parenthesized_expression; 139, 140; 140, binary_operator:+; 140, 141; 140, 145; 141, binary_operator:*; 141, 142; 141, 144; 142, unary_operator:-; 142, 143; 143, integer:2; 144, identifier:reverse; 145, integer:1; 146, elif_clause; 146, 147; 146, 148; 147, identifier:sort_order_first; 148, block; 148, 149; 149, return_statement; 149, 150; 150, binary_operator:*; 150, 151; 150, 153; 151, unary_operator:-; 151, 152; 152, integer:1; 153, parenthesized_expression; 153, 154; 154, binary_operator:+; 154, 155; 154, 159; 155, binary_operator:*; 155, 156; 155, 158; 156, unary_operator:-; 156, 157; 157, integer:2; 158, identifier:reverse; 159, integer:1; 160, comment; 161, elif_clause; 161, 162; 161, 171; 162, boolean_operator:and; 162, 163; 162, 164; 163, identifier:sort_order_first; 164, comparison_operator:in; 164, 165; 164, 170; 165, subscript; 165, 166; 165, 167; 166, identifier:b; 167, slice; 167, 168; 167, 169; 168, colon; 169, identifier:prefix_len; 170, identifier:sort_order; 171, block; 171, 172; 172, return_statement; 172, 173; 173, binary_operator:+; 173, 174; 173, 178; 174, binary_operator:*; 174, 175; 174, 177; 175, unary_operator:-; 175, 176; 176, integer:2; 177, identifier:reverse; 178, integer:1; 179, return_statement; 179, 180; 180, binary_operator:*; 180, 181; 180, 196; 181, parenthesized_expression; 181, 182; 182, binary_operator:+; 182, 183; 182, 190; 183, binary_operator:*; 183, 184; 183, 186; 184, unary_operator:-; 184, 185; 185, integer:1; 186, parenthesized_expression; 186, 187; 187, comparison_operator:<; 187, 188; 187, 189; 188, identifier:a; 189, identifier:b; 190, binary_operator:*; 190, 191; 190, 192; 191, integer:1; 192, parenthesized_expression; 192, 193; 193, comparison_operator:>; 193, 194; 193, 195; 194, identifier:a; 195, identifier:b; 196, parenthesized_expression; 196, 197; 197, binary_operator:+; 197, 198; 197, 202; 198, binary_operator:*; 198, 199; 198, 201; 199, unary_operator:-; 199, 200; 200, integer:2; 201, identifier:reverse; 202, integer:1; 203, return_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:sorted; 206, argument_list; 206, 207; 206, 208; 207, identifier:strings; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:key; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:functools; 213, identifier:cmp_to_key; 214, argument_list; 214, 215; 215, identifier:compare
def sort_strings(strings, sort_order=None, reverse=False, case_sensitive=False, sort_order_first=True): """Sort a list of strings according to the provided sorted list of string prefixes TODO: - Provide an option to use `.startswith()` rather than a fixed prefix length (will be much slower) Arguments: sort_order_first (bool): Whether strings in sort_order should always preceed "unknown" strings sort_order (sequence of str): Desired ordering as a list of prefixes to the strings If sort_order strings have varying length, the max length will determine the prefix length compared reverse (bool): whether to reverse the sort orded. Passed through to `sorted(strings, reverse=reverse)` case_senstive (bool): Whether to sort in lexographic rather than alphabetic order and whether the prefixes in sort_order are checked in a case-sensitive way Examples: >>> sort_strings(['morn32', 'morning', 'unknown', 'date', 'dow', 'doy', 'moy'], ... ('dat', 'dow', 'moy', 'dom', 'doy', 'mor')) ['date', 'dow', 'moy', 'doy', 'morn32', 'morning', 'unknown'] >>> sort_strings(['morn32', 'morning', 'unknown', 'less unknown', 'lucy', 'date', 'dow', 'doy', 'moy'], ... ('dat', 'dow', 'moy', 'dom', 'doy', 'mor'), reverse=True) ['unknown', 'lucy', 'less unknown', 'morning', 'morn32', 'doy', 'moy', 'dow', 'date'] Strings whose prefixes don't exist in `sort_order` sequence can be interleaved into the sorted list in lexical order by setting `sort_order_first=False` >>> sort_strings(['morn32', 'morning', 'unknown', 'lucy', 'less unknown', 'date', 'dow', 'doy', 'moy'], ... ('dat', 'dow', 'moy', 'dom', 'moy', 'mor'), ... sort_order_first=False) # doctest: +NORMALIZE_WHITESPACE ['date', 'dow', 'doy', 'less unknown', 'lucy', 'moy', 'morn32', 'morning', 'unknown'] """ if not case_sensitive: sort_order = tuple(s.lower() for s in sort_order) strings = tuple(s.lower() for s in strings) prefix_len = max(len(s) for s in sort_order) def compare(a, b, prefix_len=prefix_len): if prefix_len: if a[:prefix_len] in sort_order: if b[:prefix_len] in sort_order: comparison = sort_order.index(a[:prefix_len]) - sort_order.index(b[:prefix_len]) comparison = int(comparison / abs(comparison or 1)) if comparison: return comparison * (-2 * reverse + 1) elif sort_order_first: return -1 * (-2 * reverse + 1) # b may be in sort_order list, so it should be first elif sort_order_first and b[:prefix_len] in sort_order: return -2 * reverse + 1 return (-1 * (a < b) + 1 * (a > b)) * (-2 * reverse + 1) return sorted(strings, key=functools.cmp_to_key(compare))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:clean_field_dict; 3, parameters; 3, 4; 3, 5; 3, 10; 4, identifier:field_dict; 5, default_parameter; 5, 6; 5, 7; 6, identifier:cleaner; 7, attribute; 7, 8; 7, 9; 8, identifier:str; 9, identifier:strip; 10, default_parameter; 10, 11; 10, 12; 11, identifier:time_zone; 12, None; 13, block; 13, 14; 13, 16; 13, 20; 13, 29; 13, 95; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:d; 19, dictionary; 20, if_statement; 20, 21; 20, 24; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:time_zone; 23, None; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:tz; 28, identifier:DEFAULT_TZ; 29, for_statement; 29, 30; 29, 33; 29, 37; 30, pattern_list; 30, 31; 30, 32; 31, identifier:k; 32, identifier:v; 33, call; 33, 34; 33, 35; 34, identifier:viewitems; 35, argument_list; 35, 36; 36, identifier:field_dict; 37, block; 37, 38; 37, 44; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:==; 39, 40; 39, 41; 40, identifier:k; 41, string:'_state'; 42, block; 42, 43; 43, continue_statement; 44, if_statement; 44, 45; 44, 50; 44, 63; 44, 87; 45, call; 45, 46; 45, 47; 46, identifier:isinstance; 47, argument_list; 47, 48; 47, 49; 48, identifier:v; 49, identifier:basestring; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:d; 55, identifier:k; 56, call; 56, 57; 56, 58; 57, identifier:cleaner; 58, argument_list; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:str; 61, argument_list; 61, 62; 62, identifier:v; 63, elif_clause; 63, 64; 63, 75; 64, call; 64, 65; 64, 66; 65, identifier:isinstance; 66, argument_list; 66, 67; 66, 68; 67, identifier:v; 68, tuple; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:datetime; 71, identifier:datetime; 72, attribute; 72, 73; 72, 74; 73, identifier:datetime; 74, identifier:date; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:d; 80, identifier:k; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:tz; 84, identifier:localize; 85, argument_list; 85, 86; 86, identifier:v; 87, else_clause; 87, 88; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:d; 93, identifier:k; 94, identifier:v; 95, return_statement; 95, 96; 96, identifier:d
def clean_field_dict(field_dict, cleaner=str.strip, time_zone=None): r"""Normalize field values by stripping whitespace from strings, localizing datetimes to a timezone, etc >>> (sorted(clean_field_dict({'_state': object(), 'x': 1, 'y': "\t Wash Me! \n" }).items()) == ... [('x', 1), ('y', 'Wash Me!')]) True """ d = {} if time_zone is None: tz = DEFAULT_TZ for k, v in viewitems(field_dict): if k == '_state': continue if isinstance(v, basestring): d[k] = cleaner(str(v)) elif isinstance(v, (datetime.datetime, datetime.date)): d[k] = tz.localize(v) else: d[k] = v return d
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:hist_from_counts; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:counts; 5, default_parameter; 5, 6; 5, 7; 6, identifier:normalize; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:cumulative; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:to_str; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sep; 16, string:','; 17, default_parameter; 17, 18; 17, 19; 18, identifier:min_bin; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:max_bin; 22, None; 23, block; 23, 24; 23, 26; 23, 44; 23, 77; 23, 94; 23, 98; 23, 220; 23, 231; 23, 232; 23, 236; 23, 267; 23, 286; 24, expression_statement; 24, 25; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:counters; 29, list:[dict((i, c)for i, c in enumerate(counts))]; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:dict; 32, generator_expression; 32, 33; 32, 36; 33, tuple; 33, 34; 33, 35; 34, identifier:i; 35, identifier:c; 36, for_in_clause; 36, 37; 36, 40; 37, pattern_list; 37, 38; 37, 39; 38, identifier:i; 39, identifier:c; 40, call; 40, 41; 40, 42; 41, identifier:enumerate; 42, argument_list; 42, 43; 43, identifier:counts; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:intkeys_list; 47, list_comprehension; 47, 48; 47, 74; 48, list_comprehension; 48, 49; 48, 50; 48, 53; 49, identifier:c; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:c; 52, identifier:counts_dict; 53, if_clause; 53, 54; 54, parenthesized_expression; 54, 55; 55, boolean_operator:or; 55, 56; 55, 61; 56, call; 56, 57; 56, 58; 57, identifier:isinstance; 58, argument_list; 58, 59; 58, 60; 59, identifier:c; 60, identifier:int; 61, parenthesized_expression; 61, 62; 62, boolean_operator:and; 62, 63; 62, 68; 63, call; 63, 64; 63, 65; 64, identifier:isinstance; 65, argument_list; 65, 66; 65, 67; 66, identifier:c; 67, identifier:float; 68, comparison_operator:==; 68, 69; 68, 73; 69, call; 69, 70; 69, 71; 70, identifier:int; 71, argument_list; 71, 72; 72, identifier:c; 73, identifier:c; 74, for_in_clause; 74, 75; 74, 76; 75, identifier:counts_dict; 76, identifier:counters; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 82; 79, pattern_list; 79, 80; 79, 81; 80, identifier:min_bin; 81, identifier:max_bin; 82, expression_list; 82, 83; 82, 86; 83, boolean_operator:or; 83, 84; 83, 85; 84, identifier:min_bin; 85, integer:0; 86, boolean_operator:or; 86, 87; 86, 88; 87, identifier:max_bin; 88, binary_operator:-; 88, 89; 88, 93; 89, call; 89, 90; 89, 91; 90, identifier:len; 91, argument_list; 91, 92; 92, identifier:counts; 93, integer:1; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:histograms; 97, list:[]; 98, for_statement; 98, 99; 98, 102; 98, 107; 99, pattern_list; 99, 100; 99, 101; 100, identifier:intkeys; 101, identifier:counts; 102, call; 102, 103; 102, 104; 103, identifier:zip; 104, argument_list; 104, 105; 104, 106; 105, identifier:intkeys_list; 106, identifier:counters; 107, block; 107, 108; 107, 115; 107, 120; 107, 152; 108, expression_statement; 108, 109; 109, augmented_assignment:+=; 109, 110; 109, 111; 110, identifier:histograms; 111, list:[OrderedDict()]; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:OrderedDict; 114, argument_list; 115, if_statement; 115, 116; 115, 118; 116, not_operator; 116, 117; 117, identifier:intkeys; 118, block; 118, 119; 119, continue_statement; 120, if_statement; 120, 121; 120, 122; 121, identifier:normalize; 122, block; 122, 123; 122, 135; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:N; 126, call; 126, 127; 126, 128; 127, identifier:sum; 128, generator_expression; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:counts; 131, identifier:c; 132, for_in_clause; 132, 133; 132, 134; 133, identifier:c; 134, identifier:intkeys; 135, for_statement; 135, 136; 135, 137; 135, 138; 136, identifier:c; 137, identifier:intkeys; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:counts; 143, identifier:c; 144, binary_operator:/; 144, 145; 144, 151; 145, call; 145, 146; 145, 147; 146, identifier:float; 147, argument_list; 147, 148; 148, subscript; 148, 149; 148, 150; 149, identifier:counts; 150, identifier:c; 151, identifier:N; 152, if_statement; 152, 153; 152, 154; 152, 193; 153, identifier:cumulative; 154, block; 154, 155; 155, for_statement; 155, 156; 155, 157; 155, 164; 156, identifier:i; 157, call; 157, 158; 157, 159; 158, identifier:range; 159, argument_list; 159, 160; 159, 161; 160, identifier:min_bin; 161, binary_operator:+; 161, 162; 161, 163; 162, identifier:max_bin; 163, integer:1; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 173; 167, subscript; 167, 168; 167, 172; 168, subscript; 168, 169; 168, 170; 169, identifier:histograms; 170, unary_operator:-; 170, 171; 171, integer:1; 172, identifier:i; 173, binary_operator:+; 173, 174; 173, 181; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:counts; 177, identifier:get; 178, argument_list; 178, 179; 178, 180; 179, identifier:i; 180, integer:0; 181, call; 181, 182; 181, 188; 182, attribute; 182, 183; 182, 187; 183, subscript; 183, 184; 183, 185; 184, identifier:histograms; 185, unary_operator:-; 185, 186; 186, integer:1; 187, identifier:get; 188, argument_list; 188, 189; 188, 192; 189, binary_operator:-; 189, 190; 189, 191; 190, identifier:i; 191, integer:1; 192, integer:0; 193, else_clause; 193, 194; 194, block; 194, 195; 195, for_statement; 195, 196; 195, 197; 195, 204; 196, identifier:i; 197, call; 197, 198; 197, 199; 198, identifier:range; 199, argument_list; 199, 200; 199, 201; 200, identifier:min_bin; 201, binary_operator:+; 201, 202; 201, 203; 202, identifier:max_bin; 203, integer:1; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 213; 207, subscript; 207, 208; 207, 212; 208, subscript; 208, 209; 208, 210; 209, identifier:histograms; 210, unary_operator:-; 210, 211; 211, integer:1; 212, identifier:i; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:counts; 216, identifier:get; 217, argument_list; 217, 218; 217, 219; 218, identifier:i; 219, integer:0; 220, if_statement; 220, 221; 220, 223; 221, not_operator; 221, 222; 222, identifier:histograms; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:histograms; 227, list:[OrderedDict()]; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:OrderedDict; 230, argument_list; 231, comment; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:aligned_histograms; 235, list:[]; 236, for_statement; 236, 237; 236, 238; 236, 245; 237, identifier:i; 238, call; 238, 239; 238, 240; 239, identifier:range; 240, argument_list; 240, 241; 240, 242; 241, identifier:min_bin; 242, binary_operator:+; 242, 243; 242, 244; 243, identifier:max_bin; 244, integer:1; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, augmented_assignment:+=; 247, 248; 247, 249; 248, identifier:aligned_histograms; 249, list:[tuple([i] + [hist.get(i, 0) for hist in histograms])]; 249, 250; 250, call; 250, 251; 250, 252; 251, identifier:tuple; 252, argument_list; 252, 253; 253, binary_operator:+; 253, 254; 253, 256; 254, list:[i]; 254, 255; 255, identifier:i; 256, list_comprehension; 256, 257; 256, 264; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:hist; 260, identifier:get; 261, argument_list; 261, 262; 261, 263; 262, identifier:i; 263, integer:0; 264, for_in_clause; 264, 265; 264, 266; 265, identifier:hist; 266, identifier:histograms; 267, if_statement; 267, 268; 267, 269; 267, 270; 268, identifier:to_str; 269, comment; 270, block; 270, 271; 271, return_statement; 271, 272; 272, call; 272, 273; 272, 274; 273, identifier:str_from_table; 274, argument_list; 274, 275; 274, 276; 274, 279; 275, identifier:aligned_histograms; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:sep; 278, identifier:sep; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:max_rows; 281, binary_operator:+; 281, 282; 281, 285; 282, binary_operator:*; 282, 283; 282, 284; 283, integer:365; 284, integer:2; 285, integer:1; 286, return_statement; 286, 287; 287, identifier:aligned_histograms
def hist_from_counts(counts, normalize=False, cumulative=False, to_str=False, sep=',', min_bin=None, max_bin=None): """Compute an emprical histogram, PMF or CDF in a list of lists TESTME: compare results to hist_from_values_list and hist_from_float_values_list """ counters = [dict((i, c)for i, c in enumerate(counts))] intkeys_list = [[c for c in counts_dict if (isinstance(c, int) or (isinstance(c, float) and int(c) == c))] for counts_dict in counters] min_bin, max_bin = min_bin or 0, max_bin or len(counts) - 1 histograms = [] for intkeys, counts in zip(intkeys_list, counters): histograms += [OrderedDict()] if not intkeys: continue if normalize: N = sum(counts[c] for c in intkeys) for c in intkeys: counts[c] = float(counts[c]) / N if cumulative: for i in range(min_bin, max_bin + 1): histograms[-1][i] = counts.get(i, 0) + histograms[-1].get(i - 1, 0) else: for i in range(min_bin, max_bin + 1): histograms[-1][i] = counts.get(i, 0) if not histograms: histograms = [OrderedDict()] # fill in the zero counts between the integer bins of the histogram aligned_histograms = [] for i in range(min_bin, max_bin + 1): aligned_histograms += [tuple([i] + [hist.get(i, 0) for hist in histograms])] if to_str: # FIXME: add header row return str_from_table(aligned_histograms, sep=sep, max_rows=365 * 2 + 1) return aligned_histograms
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 38; 2, function_name:normalize_serial_number; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 16; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 4, identifier:sn; 5, default_parameter; 5, 6; 5, 7; 6, identifier:max_length; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:left_fill; 10, string:'0'; 11, default_parameter; 11, 12; 11, 13; 12, identifier:right_fill; 13, call; 13, 14; 13, 15; 14, identifier:str; 15, argument_list; 16, default_parameter; 16, 17; 16, 18; 17, identifier:blank; 18, call; 18, 19; 18, 20; 19, identifier:str; 20, argument_list; 21, default_parameter; 21, 22; 21, 23; 22, identifier:valid_chars; 23, string:' -0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 24, default_parameter; 24, 25; 24, 26; 25, identifier:invalid_chars; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:strip_whitespace; 29, True; 30, default_parameter; 30, 31; 30, 32; 31, identifier:join; 32, False; 33, default_parameter; 33, 34; 33, 35; 34, identifier:na; 35, attribute; 35, 36; 35, 37; 36, identifier:rex; 37, identifier:nones; 38, block; 38, 39; 38, 41; 38, 42; 38, 61; 38, 80; 38, 99; 38, 118; 38, 137; 38, 156; 38, 175; 38, 194; 38, 213; 38, 232; 38, 241; 38, 253; 38, 264; 38, 305; 38, 314; 38, 325; 38, 359; 38, 371; 38, 395; 38, 417; 39, expression_statement; 39, 40; 40, comment; 41, comment; 42, if_statement; 42, 43; 42, 46; 42, 53; 43, comparison_operator:is; 43, 44; 43, 45; 44, identifier:max_length; 45, None; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:max_length; 50, attribute; 50, 51; 50, 52; 51, identifier:normalize_serial_number; 52, identifier:max_length; 53, else_clause; 53, 54; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:normalize_serial_number; 59, identifier:max_length; 60, identifier:max_length; 61, if_statement; 61, 62; 61, 65; 61, 72; 62, comparison_operator:is; 62, 63; 62, 64; 63, identifier:left_fill; 64, None; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:left_fill; 69, attribute; 69, 70; 69, 71; 70, identifier:normalize_serial_number; 71, identifier:left_fill; 72, else_clause; 72, 73; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:normalize_serial_number; 78, identifier:left_fill; 79, identifier:left_fill; 80, if_statement; 80, 81; 80, 84; 80, 91; 81, comparison_operator:is; 81, 82; 81, 83; 82, identifier:right_fill; 83, None; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:right_fill; 88, attribute; 88, 89; 88, 90; 89, identifier:normalize_serial_number; 90, identifier:right_fill; 91, else_clause; 91, 92; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:normalize_serial_number; 97, identifier:right_fill; 98, identifier:right_fill; 99, if_statement; 99, 100; 99, 103; 99, 110; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:blank; 102, None; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:blank; 107, attribute; 107, 108; 107, 109; 108, identifier:normalize_serial_number; 109, identifier:blank; 110, else_clause; 110, 111; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:normalize_serial_number; 116, identifier:blank; 117, identifier:blank; 118, if_statement; 118, 119; 118, 122; 118, 129; 119, comparison_operator:is; 119, 120; 119, 121; 120, identifier:valid_chars; 121, None; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:valid_chars; 126, attribute; 126, 127; 126, 128; 127, identifier:normalize_serial_number; 128, identifier:valid_chars; 129, else_clause; 129, 130; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:normalize_serial_number; 135, identifier:valid_chars; 136, identifier:valid_chars; 137, if_statement; 137, 138; 137, 141; 137, 148; 138, comparison_operator:is; 138, 139; 138, 140; 139, identifier:invalid_chars; 140, None; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:invalid_chars; 145, attribute; 145, 146; 145, 147; 146, identifier:normalize_serial_number; 147, identifier:invalid_chars; 148, else_clause; 148, 149; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:normalize_serial_number; 154, identifier:invalid_chars; 155, identifier:invalid_chars; 156, if_statement; 156, 157; 156, 160; 156, 167; 157, comparison_operator:is; 157, 158; 157, 159; 158, identifier:strip_whitespace; 159, None; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:strip_whitespace; 164, attribute; 164, 165; 164, 166; 165, identifier:normalize_serial_number; 166, identifier:strip_whitespace; 167, else_clause; 167, 168; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:normalize_serial_number; 173, identifier:strip_whitespace; 174, identifier:strip_whitespace; 175, if_statement; 175, 176; 175, 179; 175, 186; 176, comparison_operator:is; 176, 177; 176, 178; 177, identifier:join; 178, None; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:join; 183, attribute; 183, 184; 183, 185; 184, identifier:normalize_serial_number; 185, identifier:join; 186, else_clause; 186, 187; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:normalize_serial_number; 192, identifier:join; 193, identifier:join; 194, if_statement; 194, 195; 194, 198; 194, 205; 195, comparison_operator:is; 195, 196; 195, 197; 196, identifier:na; 197, None; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:na; 202, attribute; 202, 203; 202, 204; 203, identifier:normalize_serial_number; 204, identifier:na; 205, else_clause; 205, 206; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:normalize_serial_number; 211, identifier:na; 212, identifier:na; 213, if_statement; 213, 214; 213, 217; 214, comparison_operator:is; 214, 215; 214, 216; 215, identifier:invalid_chars; 216, None; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:invalid_chars; 221, generator_expression; 221, 222; 221, 223; 221, 228; 222, identifier:c; 223, for_in_clause; 223, 224; 223, 225; 224, identifier:c; 225, attribute; 225, 226; 225, 227; 226, identifier:charlist; 227, identifier:ascii_all; 228, if_clause; 228, 229; 229, comparison_operator:not; 229, 230; 229, 231; 230, identifier:c; 231, identifier:valid_chars; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:invalid_chars; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, string:''; 238, identifier:join; 239, argument_list; 239, 240; 240, identifier:invalid_chars; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:sn; 244, call; 244, 245; 244, 251; 245, attribute; 245, 246; 245, 250; 246, call; 246, 247; 246, 248; 247, identifier:str; 248, argument_list; 248, 249; 249, identifier:sn; 250, identifier:strip; 251, argument_list; 251, 252; 252, identifier:invalid_chars; 253, if_statement; 253, 254; 253, 255; 254, identifier:strip_whitespace; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:sn; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:sn; 262, identifier:strip; 263, argument_list; 264, if_statement; 264, 265; 264, 266; 265, identifier:invalid_chars; 266, block; 266, 267; 267, if_statement; 267, 268; 267, 269; 267, 292; 268, identifier:join; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:sn; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:sn; 276, identifier:translate; 277, argument_list; 277, 278; 278, call; 278, 279; 278, 280; 279, identifier:dict; 280, argument_list; 280, 281; 281, call; 281, 282; 281, 283; 282, identifier:zip; 283, argument_list; 283, 284; 283, 285; 284, identifier:invalid_chars; 285, binary_operator:*; 285, 286; 285, 288; 286, list:['']; 286, 287; 287, string:''; 288, call; 288, 289; 288, 290; 289, identifier:len; 290, argument_list; 290, 291; 291, identifier:invalid_chars; 292, else_clause; 292, 293; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:sn; 297, subscript; 297, 298; 297, 303; 298, call; 298, 299; 298, 300; 299, identifier:multisplit; 300, argument_list; 300, 301; 300, 302; 301, identifier:sn; 302, identifier:invalid_chars; 303, unary_operator:-; 303, 304; 304, integer:1; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:sn; 308, subscript; 308, 309; 308, 310; 309, identifier:sn; 310, slice; 310, 311; 310, 313; 311, unary_operator:-; 311, 312; 312, identifier:max_length; 313, colon; 314, if_statement; 314, 315; 314, 316; 315, identifier:strip_whitespace; 316, block; 316, 317; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:sn; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:sn; 323, identifier:strip; 324, argument_list; 325, if_statement; 325, 326; 325, 327; 326, identifier:na; 327, block; 327, 328; 328, if_statement; 328, 329; 328, 342; 328, 347; 329, boolean_operator:and; 329, 330; 329, 339; 330, call; 330, 331; 330, 332; 331, identifier:isinstance; 332, argument_list; 332, 333; 332, 334; 333, identifier:na; 334, tuple; 334, 335; 334, 336; 334, 337; 334, 338; 335, identifier:tuple; 336, identifier:set; 337, identifier:dict; 338, identifier:list; 339, comparison_operator:in; 339, 340; 339, 341; 340, identifier:sn; 341, identifier:na; 342, block; 342, 343; 343, expression_statement; 343, 344; 344, assignment; 344, 345; 344, 346; 345, identifier:sn; 346, string:''; 347, elif_clause; 347, 348; 347, 354; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:na; 351, identifier:match; 352, argument_list; 352, 353; 353, identifier:sn; 354, block; 354, 355; 355, expression_statement; 355, 356; 356, assignment; 356, 357; 356, 358; 357, identifier:sn; 358, string:''; 359, if_statement; 359, 360; 359, 368; 360, boolean_operator:and; 360, 361; 360, 363; 361, not_operator; 361, 362; 362, identifier:sn; 363, not_operator; 363, 364; 364, parenthesized_expression; 364, 365; 365, comparison_operator:is; 365, 366; 365, 367; 366, identifier:blank; 367, False; 368, block; 368, 369; 369, return_statement; 369, 370; 370, identifier:blank; 371, if_statement; 371, 372; 371, 373; 372, identifier:left_fill; 373, block; 373, 374; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 377; 376, identifier:sn; 377, binary_operator:+; 377, 378; 377, 394; 378, binary_operator:*; 378, 379; 378, 380; 379, identifier:left_fill; 380, call; 380, 381; 380, 382; 381, identifier:int; 382, argument_list; 382, 383; 383, binary_operator:-; 383, 384; 383, 385; 384, identifier:max_length; 385, binary_operator:/; 385, 386; 385, 390; 386, call; 386, 387; 386, 388; 387, identifier:len; 388, argument_list; 388, 389; 389, identifier:sn; 390, call; 390, 391; 390, 392; 391, identifier:len; 392, argument_list; 392, 393; 393, identifier:left_fill; 394, identifier:sn; 395, if_statement; 395, 396; 395, 397; 396, identifier:right_fill; 397, block; 397, 398; 398, expression_statement; 398, 399; 399, assignment; 399, 400; 399, 401; 400, identifier:sn; 401, binary_operator:+; 401, 402; 401, 403; 402, identifier:sn; 403, binary_operator:*; 403, 404; 403, 405; 404, identifier:right_fill; 405, parenthesized_expression; 405, 406; 406, binary_operator:-; 406, 407; 406, 408; 407, identifier:max_length; 408, binary_operator:/; 408, 409; 408, 413; 409, call; 409, 410; 409, 411; 410, identifier:len; 411, argument_list; 411, 412; 412, identifier:sn; 413, call; 413, 414; 413, 415; 414, identifier:len; 415, argument_list; 415, 416; 416, identifier:right_fill; 417, return_statement; 417, 418; 418, identifier:sn
def normalize_serial_number(sn, max_length=None, left_fill='0', right_fill=str(), blank=str(), valid_chars=' -0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', invalid_chars=None, strip_whitespace=True, join=False, na=rex.nones): r"""Make a string compatible with typical serial number requirements # Default configuration strips internal and external whitespaces and retains only the last 10 characters >>> normalize_serial_number('1C 234567890 ') '0234567890' >>> normalize_serial_number('1C 234567890 ', max_length=20) '000000001C 234567890' >>> normalize_serial_number('Unknown', blank=None, left_fill=str()) '' >>> normalize_serial_number('N/A', blank='', left_fill=str()) 'A' >>> normalize_serial_number('1C 234567890 ', max_length=20, left_fill='') '1C 234567890' Notice how the max_length setting (20) carries over from the previous test! >>> len(normalize_serial_number('Unknown', blank=False)) 20 >>> normalize_serial_number('Unknown', blank=False) '00000000000000000000' >>> normalize_serial_number(' \t1C\t-\t234567890 \x00\x7f', max_length=14, left_fill='0', ... valid_chars='0123456789ABC', invalid_chars=None, join=True) '1C\t-\t234567890' Notice how the max_length setting carries over from the previous test! >>> len(normalize_serial_number('Unknown', blank=False)) 14 Restore the default max_length setting >>> len(normalize_serial_number('Unknown', blank=False, max_length=10)) 10 >>> normalize_serial_number('NO SERIAL', blank='--=--', left_fill='') # doctest: +NORMALIZE_WHITESPACE 'NO SERIAL' >>> normalize_serial_number('NO SERIAL', blank='', left_fill='') # doctest: +NORMALIZE_WHITESPACE 'NO SERIAL' >>> normalize_serial_number('1C 234567890 ', valid_chars='0123456789') '0234567890' """ # All 9 kwargs have persistent default values stored as attributes of the funcion instance if max_length is None: max_length = normalize_serial_number.max_length else: normalize_serial_number.max_length = max_length if left_fill is None: left_fill = normalize_serial_number.left_fill else: normalize_serial_number.left_fill = left_fill if right_fill is None: right_fill = normalize_serial_number.right_fill else: normalize_serial_number.right_fill = right_fill if blank is None: blank = normalize_serial_number.blank else: normalize_serial_number.blank = blank if valid_chars is None: valid_chars = normalize_serial_number.valid_chars else: normalize_serial_number.valid_chars = valid_chars if invalid_chars is None: invalid_chars = normalize_serial_number.invalid_chars else: normalize_serial_number.invalid_chars = invalid_chars if strip_whitespace is None: strip_whitespace = normalize_serial_number.strip_whitespace else: normalize_serial_number.strip_whitespace = strip_whitespace if join is None: join = normalize_serial_number.join else: normalize_serial_number.join = join if na is None: na = normalize_serial_number.na else: normalize_serial_number.na = na if invalid_chars is None: invalid_chars = (c for c in charlist.ascii_all if c not in valid_chars) invalid_chars = ''.join(invalid_chars) sn = str(sn).strip(invalid_chars) if strip_whitespace: sn = sn.strip() if invalid_chars: if join: sn = sn.translate(dict(zip(invalid_chars, [''] * len(invalid_chars)))) else: sn = multisplit(sn, invalid_chars)[-1] sn = sn[-max_length:] if strip_whitespace: sn = sn.strip() if na: if isinstance(na, (tuple, set, dict, list)) and sn in na: sn = '' elif na.match(sn): sn = '' if not sn and not (blank is False): return blank if left_fill: sn = left_fill * int(max_length - len(sn) / len(left_fill)) + sn if right_fill: sn = sn + right_fill * (max_length - len(sn) / len(right_fill)) return sn
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:listify; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:values; 5, default_parameter; 5, 6; 5, 7; 6, identifier:N; 7, integer:1; 8, default_parameter; 8, 9; 8, 10; 9, identifier:delim; 10, None; 11, block; 11, 12; 11, 14; 11, 23; 11, 24; 11, 91; 11, 92; 11, 140; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:ans; 17, conditional_expression:if; 17, 18; 17, 19; 17, 22; 18, list:[]; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:values; 21, None; 22, identifier:values; 23, comment; 24, if_statement; 24, 25; 24, 37; 24, 45; 25, boolean_operator:and; 25, 26; 25, 31; 26, call; 26, 27; 26, 28; 27, identifier:hasattr; 28, argument_list; 28, 29; 28, 30; 29, identifier:ans; 30, string:'__iter__'; 31, not_operator; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:isinstance; 34, argument_list; 34, 35; 34, 36; 35, identifier:ans; 36, identifier:basestring; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:ans; 41, call; 41, 42; 41, 43; 42, identifier:list; 43, argument_list; 43, 44; 44, identifier:ans; 45, else_clause; 45, 46; 45, 47; 46, comment; 47, block; 47, 48; 48, if_statement; 48, 49; 48, 60; 48, 84; 49, boolean_operator:and; 49, 50; 49, 55; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:delim; 54, identifier:basestring; 55, call; 55, 56; 55, 57; 56, identifier:isinstance; 57, argument_list; 57, 58; 57, 59; 58, identifier:ans; 59, identifier:basestring; 60, block; 60, 61; 61, try_statement; 61, 62; 61, 72; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:ans; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:ans; 69, identifier:split; 70, argument_list; 70, 71; 71, identifier:delim; 72, except_clause; 72, 73; 72, 78; 73, tuple; 73, 74; 73, 75; 73, 76; 73, 77; 74, identifier:IndexError; 75, identifier:ValueError; 76, identifier:AttributeError; 77, identifier:TypeError; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:ans; 82, list:[ans]; 82, 83; 83, identifier:ans; 84, else_clause; 84, 85; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:ans; 89, list:[ans]; 89, 90; 90, identifier:ans; 91, comment; 92, if_statement; 92, 93; 92, 97; 92, 126; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, identifier:ans; 97, block; 97, 98; 98, if_statement; 98, 99; 98, 109; 99, boolean_operator:and; 99, 100; 99, 106; 100, comparison_operator:<; 100, 101; 100, 105; 101, call; 101, 102; 101, 103; 102, identifier:len; 103, argument_list; 103, 104; 104, identifier:ans; 105, identifier:N; 106, comparison_operator:>; 106, 107; 106, 108; 107, identifier:N; 108, integer:1; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, augmented_assignment:+=; 111, 112; 111, 113; 112, identifier:ans; 113, binary_operator:*; 113, 114; 113, 119; 114, list:[ans[-1]]; 114, 115; 115, subscript; 115, 116; 115, 117; 116, identifier:ans; 117, unary_operator:-; 117, 118; 118, integer:1; 119, parenthesized_expression; 119, 120; 120, binary_operator:-; 120, 121; 120, 122; 121, identifier:N; 122, call; 122, 123; 122, 124; 123, identifier:len; 124, argument_list; 124, 125; 125, identifier:ans; 126, else_clause; 126, 127; 127, block; 127, 128; 128, if_statement; 128, 129; 128, 132; 129, comparison_operator:>; 129, 130; 129, 131; 130, identifier:N; 131, integer:1; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:ans; 136, binary_operator:*; 136, 137; 136, 139; 137, list:[[]]; 137, 138; 138, list:[]; 139, identifier:N; 140, return_statement; 140, 141; 141, identifier:ans
def listify(values, N=1, delim=None): """Return an N-length list, with elements values, extrapolating as necessary. >>> listify("don't split into characters") ["don't split into characters"] >>> listify("len = 3", 3) ['len = 3', 'len = 3', 'len = 3'] >>> listify("But split on a delimeter, if requested.", delim=',') ['But split on a delimeter', ' if requested.'] >>> listify(["obj 1", "obj 2", "len = 4"], N=4) ['obj 1', 'obj 2', 'len = 4', 'len = 4'] >>> listify(iter("len=7"), N=7) ['l', 'e', 'n', '=', '7', '7', '7'] >>> listify(iter("len=5")) ['l', 'e', 'n', '=', '5'] >>> listify(None, 3) [[], [], []] >>> listify([None],3) [None, None, None] >>> listify([], 3) [[], [], []] >>> listify('', 2) ['', ''] >>> listify(0) [0] >>> listify(False, 2) [False, False] """ ans = [] if values is None else values # convert non-string non-list iterables into a list if hasattr(ans, '__iter__') and not isinstance(ans, basestring): ans = list(ans) else: # split the string (if possible) if isinstance(delim, basestring) and isinstance(ans, basestring): try: ans = ans.split(delim) except (IndexError, ValueError, AttributeError, TypeError): ans = [ans] else: ans = [ans] # pad the end of the list if a length has been specified if len(ans): if len(ans) < N and N > 1: ans += [ans[-1]] * (N - len(ans)) else: if N > 1: ans = [[]] * N return ans
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:best_fit; 3, parameters; 3, 4; 3, 5; 4, identifier:li; 5, identifier:value; 6, block; 6, 7; 6, 9; 6, 26; 6, 38; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:index; 12, call; 12, 13; 12, 14; 13, identifier:min; 14, argument_list; 14, 15; 14, 20; 15, call; 15, 16; 15, 17; 16, identifier:bisect_left; 17, argument_list; 17, 18; 17, 19; 18, identifier:li; 19, identifier:value; 20, binary_operator:-; 20, 21; 20, 25; 21, call; 21, 22; 21, 23; 22, identifier:len; 23, argument_list; 23, 24; 24, identifier:li; 25, integer:1; 26, if_statement; 26, 27; 26, 35; 27, comparison_operator:in; 27, 28; 27, 29; 28, identifier:index; 29, tuple; 29, 30; 29, 31; 30, integer:0; 31, call; 31, 32; 31, 33; 32, identifier:len; 33, argument_list; 33, 34; 34, identifier:li; 35, block; 35, 36; 36, return_statement; 36, 37; 37, identifier:index; 38, if_statement; 38, 39; 38, 52; 38, 55; 39, comparison_operator:<; 39, 40; 39, 45; 40, binary_operator:-; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:li; 43, identifier:index; 44, identifier:value; 45, binary_operator:-; 45, 46; 45, 47; 46, identifier:value; 47, subscript; 47, 48; 47, 49; 48, identifier:li; 49, binary_operator:-; 49, 50; 49, 51; 50, identifier:index; 51, integer:1; 52, block; 52, 53; 53, return_statement; 53, 54; 54, identifier:index; 55, else_clause; 55, 56; 56, block; 56, 57; 57, return_statement; 57, 58; 58, binary_operator:-; 58, 59; 58, 60; 59, identifier:index; 60, integer:1
def best_fit(li, value): """For a sorted list li, returns the closest item to value""" index = min(bisect_left(li, value), len(li) - 1) if index in (0, len(li)): return index if li[index] - value < value - li[index-1]: return index else: return index-1
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:proj4_to_epsg; 3, parameters; 3, 4; 4, identifier:projection; 5, block; 5, 6; 5, 8; 5, 34; 5, 35; 5, 46; 5, 59; 5, 60; 5, 81; 5, 93; 5, 167; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 12; 9, function_name:make_definition; 10, parameters; 10, 11; 11, identifier:value; 12, block; 12, 13; 13, return_statement; 13, 14; 14, set_comprehension; 14, 15; 14, 24; 14, 32; 15, call; 15, 16; 15, 23; 16, attribute; 16, 17; 16, 22; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:x; 20, identifier:strip; 21, argument_list; 22, identifier:lower; 23, argument_list; 24, for_in_clause; 24, 25; 24, 26; 25, identifier:x; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:value; 29, identifier:split; 30, argument_list; 30, 31; 31, string:'+'; 32, if_clause; 32, 33; 33, identifier:x; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:match; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:EPSG_RE; 41, identifier:search; 42, argument_list; 42, 43; 43, attribute; 43, 44; 43, 45; 44, identifier:projection; 45, identifier:srs; 46, if_statement; 46, 47; 46, 48; 47, identifier:match; 48, block; 48, 49; 49, return_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:int; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:match; 56, identifier:group; 57, argument_list; 57, 58; 58, integer:1; 59, comment; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:pyproj_data_dir; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:os; 67, identifier:path; 68, identifier:join; 69, argument_list; 69, 70; 69, 80; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:os; 74, identifier:path; 75, identifier:dirname; 76, argument_list; 76, 77; 77, attribute; 77, 78; 77, 79; 78, identifier:pyproj; 79, identifier:__file__; 80, string:'data'; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:pyproj_epsg_file; 84, call; 84, 85; 84, 90; 85, attribute; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:os; 88, identifier:path; 89, identifier:join; 90, argument_list; 90, 91; 90, 92; 91, identifier:pyproj_data_dir; 92, string:'epsg'; 93, if_statement; 93, 94; 93, 102; 94, call; 94, 95; 94, 100; 95, attribute; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:os; 98, identifier:path; 99, identifier:exists; 100, argument_list; 100, 101; 101, identifier:pyproj_epsg_file; 102, block; 102, 103; 102, 112; 102, 120; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:definition; 106, call; 106, 107; 106, 108; 107, identifier:make_definition; 108, argument_list; 108, 109; 109, attribute; 109, 110; 109, 111; 110, identifier:projection; 111, identifier:srs; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:f; 115, call; 115, 116; 115, 117; 116, identifier:open; 117, argument_list; 117, 118; 117, 119; 118, identifier:pyproj_epsg_file; 119, string:'r'; 120, for_statement; 120, 121; 120, 122; 120, 127; 121, identifier:line; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:f; 125, identifier:readlines; 126, argument_list; 127, block; 127, 128; 127, 137; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:match; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:PYPROJ_EPSG_FILE_RE; 134, identifier:search; 135, argument_list; 135, 136; 136, identifier:line; 137, if_statement; 137, 138; 137, 139; 138, identifier:match; 139, block; 139, 140; 139, 152; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:file_definition; 143, call; 143, 144; 143, 145; 144, identifier:make_definition; 145, argument_list; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:match; 149, identifier:group; 150, argument_list; 150, 151; 151, integer:2; 152, if_statement; 152, 153; 152, 156; 153, comparison_operator:==; 153, 154; 153, 155; 154, identifier:definition; 155, identifier:file_definition; 156, block; 156, 157; 157, return_statement; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:int; 160, argument_list; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:match; 164, identifier:group; 165, argument_list; 165, 166; 166, integer:1; 167, return_statement; 167, 168; 168, None
def proj4_to_epsg(projection): """Attempts to convert a PROJ4 projection object to an EPSG code and returns None if conversion fails""" def make_definition(value): return {x.strip().lower() for x in value.split('+') if x} # Use the EPSG in the definition if available match = EPSG_RE.search(projection.srs) if match: return int(match.group(1)) # Otherwise, try to look up the EPSG from the pyproj data file pyproj_data_dir = os.path.join(os.path.dirname(pyproj.__file__), 'data') pyproj_epsg_file = os.path.join(pyproj_data_dir, 'epsg') if os.path.exists(pyproj_epsg_file): definition = make_definition(projection.srs) f = open(pyproj_epsg_file, 'r') for line in f.readlines(): match = PYPROJ_EPSG_FILE_RE.search(line) if match: file_definition = make_definition(match.group(2)) if definition == file_definition: return int(match.group(1)) return None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:index_pix_in_pixels; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:pix; 5, identifier:pixels; 6, default_parameter; 6, 7; 6, 8; 7, identifier:sort; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:outside; 11, unary_operator:-; 11, 12; 12, integer:1; 13, block; 13, 14; 13, 16; 13, 17; 13, 18; 13, 19; 13, 31; 13, 32; 13, 42; 13, 84; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, comment; 18, comment; 19, if_statement; 19, 20; 19, 21; 20, identifier:sort; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:pixels; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:np; 28, identifier:sort; 29, argument_list; 29, 30; 30, identifier:pixels; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:index; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:np; 38, identifier:searchsorted; 39, argument_list; 39, 40; 39, 41; 40, identifier:pixels; 41, identifier:pix; 42, if_statement; 42, 43; 42, 49; 42, 68; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:np; 46, identifier:isscalar; 47, argument_list; 47, 48; 48, identifier:index; 49, block; 49, 50; 50, if_statement; 50, 51; 50, 63; 51, not_operator; 51, 52; 52, call; 52, 53; 52, 62; 53, attribute; 53, 54; 53, 61; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:np; 57, identifier:in1d; 58, argument_list; 58, 59; 58, 60; 59, identifier:pix; 60, identifier:pixels; 61, identifier:any; 62, argument_list; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:index; 67, identifier:outside; 68, else_clause; 68, 69; 68, 70; 69, comment; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 83; 73, subscript; 73, 74; 73, 75; 74, identifier:index; 75, unary_operator:~; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:np; 79, identifier:in1d; 80, argument_list; 80, 81; 80, 82; 81, identifier:pix; 82, identifier:pixels; 83, identifier:outside; 84, return_statement; 84, 85; 85, identifier:index
def index_pix_in_pixels(pix,pixels,sort=False,outside=-1): """ Find the indices of a set of pixels into another set of pixels. !!! ASSUMES SORTED PIXELS !!! Parameters: ----------- pix : set of search pixels pixels : set of reference pixels Returns: -------- index : index into the reference pixels """ # ADW: Not really safe to set index = -1 (accesses last entry); # -np.inf would be better, but breaks other code... # ADW: Are the pixels always sorted? Is there a quick way to check? if sort: pixels = np.sort(pixels) # Assumes that 'pixels' is pre-sorted, otherwise...??? index = np.searchsorted(pixels,pix) if np.isscalar(index): if not np.in1d(pix,pixels).any(): index = outside else: # Find objects that are outside the pixels index[~np.in1d(pix,pixels)] = outside return index
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:files; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:entity_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:manifest; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:filename; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:read_file; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:channel; 17, None; 18, block; 18, 19; 18, 21; 18, 66; 18, 70; 18, 102; 19, expression_statement; 19, 20; 20, string:''' Get the files or file contents of a file for an entity. If all files are requested, a dictionary of filenames and urls for the files in the archive are returned. If filename is provided, the url of just that file is returned, if it exists. If filename is provided and read_file is true, the *contents* of the file are returned, if the file exists. @param entity_id The id of the entity to get files for @param manifest The manifest of files for the entity. Providing this reduces queries; if not provided, the manifest is looked up in the charmstore. @param filename The name of the file in the archive to get. @param read_file Whether to get the url for the file or the file contents. @param channel Optional channel name. '''; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:manifest; 24, None; 25, block; 25, 26; 25, 41; 25, 49; 25, 58; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:manifest_url; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, string:'{}/{}/meta/manifest'; 32, identifier:format; 33, argument_list; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:url; 37, call; 37, 38; 37, 39; 38, identifier:_get_path; 39, argument_list; 39, 40; 40, identifier:entity_id; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:manifest_url; 44, call; 44, 45; 44, 46; 45, identifier:_add_channel; 46, argument_list; 46, 47; 46, 48; 47, identifier:manifest_url; 48, identifier:channel; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:manifest; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:_get; 56, argument_list; 56, 57; 57, identifier:manifest_url; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:manifest; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:manifest; 64, identifier:json; 65, argument_list; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:files; 69, dictionary; 70, for_statement; 70, 71; 70, 72; 70, 73; 71, identifier:f; 72, identifier:manifest; 73, block; 73, 74; 73, 80; 73, 96; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:manifest_name; 77, subscript; 77, 78; 77, 79; 78, identifier:f; 79, string:'Name'; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:file_url; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:file_url; 87, argument_list; 87, 88; 87, 92; 87, 93; 88, call; 88, 89; 88, 90; 89, identifier:_get_path; 90, argument_list; 90, 91; 91, identifier:entity_id; 92, identifier:manifest_name; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:channel; 95, identifier:channel; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:files; 100, identifier:manifest_name; 101, identifier:file_url; 102, if_statement; 102, 103; 102, 104; 102, 146; 103, identifier:filename; 104, block; 104, 105; 104, 115; 104, 126; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:file_url; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:files; 111, identifier:get; 112, argument_list; 112, 113; 112, 114; 113, identifier:filename; 114, None; 115, if_statement; 115, 116; 115, 119; 116, comparison_operator:is; 116, 117; 116, 118; 117, identifier:file_url; 118, None; 119, block; 119, 120; 120, raise_statement; 120, 121; 121, call; 121, 122; 121, 123; 122, identifier:EntityNotFound; 123, argument_list; 123, 124; 123, 125; 124, identifier:entity_id; 125, identifier:filename; 126, if_statement; 126, 127; 126, 128; 126, 142; 127, identifier:read_file; 128, block; 128, 129; 128, 138; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:data; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:_get; 136, argument_list; 136, 137; 137, identifier:file_url; 138, return_statement; 138, 139; 139, attribute; 139, 140; 139, 141; 140, identifier:data; 141, identifier:text; 142, else_clause; 142, 143; 143, block; 143, 144; 144, return_statement; 144, 145; 145, identifier:file_url; 146, else_clause; 146, 147; 147, block; 147, 148; 148, return_statement; 148, 149; 149, identifier:files
def files(self, entity_id, manifest=None, filename=None, read_file=False, channel=None): ''' Get the files or file contents of a file for an entity. If all files are requested, a dictionary of filenames and urls for the files in the archive are returned. If filename is provided, the url of just that file is returned, if it exists. If filename is provided and read_file is true, the *contents* of the file are returned, if the file exists. @param entity_id The id of the entity to get files for @param manifest The manifest of files for the entity. Providing this reduces queries; if not provided, the manifest is looked up in the charmstore. @param filename The name of the file in the archive to get. @param read_file Whether to get the url for the file or the file contents. @param channel Optional channel name. ''' if manifest is None: manifest_url = '{}/{}/meta/manifest'.format(self.url, _get_path(entity_id)) manifest_url = _add_channel(manifest_url, channel) manifest = self._get(manifest_url) manifest = manifest.json() files = {} for f in manifest: manifest_name = f['Name'] file_url = self.file_url(_get_path(entity_id), manifest_name, channel=channel) files[manifest_name] = file_url if filename: file_url = files.get(filename, None) if file_url is None: raise EntityNotFound(entity_id, filename) if read_file: data = self._get(file_url) return data.text else: return file_url else: return files
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 33; 2, function_name:search; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 4, identifier:self; 5, identifier:text; 6, default_parameter; 6, 7; 6, 8; 7, identifier:includes; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:doc_type; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:limit; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:autocomplete; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:promulgated_only; 20, False; 21, default_parameter; 21, 22; 21, 23; 22, identifier:tags; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:sort; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:owner; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:series; 32, None; 33, block; 33, 34; 33, 36; 33, 50; 33, 65; 33, 79; 33, 91; 33, 122; 33, 156; 33, 165; 34, expression_statement; 34, 35; 35, string:''' Search for entities in the charmstore. @param text The text to search for. @param includes What metadata to return in results (e.g. charm-config). @param doc_type Filter to this type: bundle or charm. @param limit Maximum number of results to return. @param autocomplete Whether to prefix/suffix match search terms. @param promulgated_only Whether to filter to only promulgated charms. @param tags The tags to filter; can be a list of tags or a single tag. @param sort Sorting the result based on the sort string provided which can be name, author, series and - in front for descending. @param owner Optional owner. If provided, search results will only include entities that owner can view. @param series The series to filter; can be a list of series or a single series. '''; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:queries; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_common_query_parameters; 43, argument_list; 43, 44; 43, 45; 43, 46; 43, 47; 43, 48; 43, 49; 44, identifier:doc_type; 45, identifier:includes; 46, identifier:owner; 47, identifier:promulgated_only; 48, identifier:series; 49, identifier:sort; 50, if_statement; 50, 51; 50, 55; 51, call; 51, 52; 51, 53; 52, identifier:len; 53, argument_list; 53, 54; 54, identifier:text; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:queries; 60, identifier:append; 61, argument_list; 61, 62; 62, tuple; 62, 63; 62, 64; 63, string:'text'; 64, identifier:text; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:limit; 68, None; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:queries; 74, identifier:append; 75, argument_list; 75, 76; 76, tuple; 76, 77; 76, 78; 77, string:'limit'; 78, identifier:limit; 79, if_statement; 79, 80; 79, 81; 80, identifier:autocomplete; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:queries; 86, identifier:append; 87, argument_list; 87, 88; 88, tuple; 88, 89; 88, 90; 89, string:'autocomplete'; 90, integer:1; 91, if_statement; 91, 92; 91, 95; 92, comparison_operator:is; 92, 93; 92, 94; 93, identifier:tags; 94, None; 95, block; 95, 96; 95, 113; 96, if_statement; 96, 97; 96, 103; 97, comparison_operator:is; 97, 98; 97, 102; 98, call; 98, 99; 98, 100; 99, identifier:type; 100, argument_list; 100, 101; 101, identifier:tags; 102, identifier:list; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:tags; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, string:','; 110, identifier:join; 111, argument_list; 111, 112; 112, identifier:tags; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:queries; 117, identifier:append; 118, argument_list; 118, 119; 119, tuple; 119, 120; 119, 121; 120, string:'tags'; 121, identifier:tags; 122, if_statement; 122, 123; 122, 127; 122, 143; 123, call; 123, 124; 123, 125; 124, identifier:len; 125, argument_list; 125, 126; 126, identifier:queries; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:url; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, string:'{}/search?{}'; 134, identifier:format; 135, argument_list; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:url; 139, call; 139, 140; 139, 141; 140, identifier:urlencode; 141, argument_list; 141, 142; 142, identifier:queries; 143, else_clause; 143, 144; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:url; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, string:'{}/search'; 151, identifier:format; 152, argument_list; 152, 153; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:url; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:data; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:_get; 163, argument_list; 163, 164; 164, identifier:url; 165, return_statement; 165, 166; 166, subscript; 166, 167; 166, 172; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:data; 170, identifier:json; 171, argument_list; 172, string:'Results'
def search(self, text, includes=None, doc_type=None, limit=None, autocomplete=False, promulgated_only=False, tags=None, sort=None, owner=None, series=None): ''' Search for entities in the charmstore. @param text The text to search for. @param includes What metadata to return in results (e.g. charm-config). @param doc_type Filter to this type: bundle or charm. @param limit Maximum number of results to return. @param autocomplete Whether to prefix/suffix match search terms. @param promulgated_only Whether to filter to only promulgated charms. @param tags The tags to filter; can be a list of tags or a single tag. @param sort Sorting the result based on the sort string provided which can be name, author, series and - in front for descending. @param owner Optional owner. If provided, search results will only include entities that owner can view. @param series The series to filter; can be a list of series or a single series. ''' queries = self._common_query_parameters(doc_type, includes, owner, promulgated_only, series, sort) if len(text): queries.append(('text', text)) if limit is not None: queries.append(('limit', limit)) if autocomplete: queries.append(('autocomplete', 1)) if tags is not None: if type(tags) is list: tags = ','.join(tags) queries.append(('tags', tags)) if len(queries): url = '{}/search?{}'.format(self.url, urlencode(queries)) else: url = '{}/search'.format(self.url) data = self._get(url) return data.json()['Results']
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:list; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:includes; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:doc_type; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:promulgated_only; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sort; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:owner; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:series; 22, None; 23, block; 23, 24; 23, 26; 23, 40; 23, 74; 23, 83; 24, expression_statement; 24, 25; 25, string:''' List entities in the charmstore. @param includes What metadata to return in results (e.g. charm-config). @param doc_type Filter to this type: bundle or charm. @param promulgated_only Whether to filter to only promulgated charms. @param sort Sorting the result based on the sort string provided which can be name, author, series and - in front for descending. @param owner Optional owner. If provided, search results will only include entities that owner can view. @param series The series to filter; can be a list of series or a single series. '''; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:queries; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_common_query_parameters; 33, argument_list; 33, 34; 33, 35; 33, 36; 33, 37; 33, 38; 33, 39; 34, identifier:doc_type; 35, identifier:includes; 36, identifier:owner; 37, identifier:promulgated_only; 38, identifier:series; 39, identifier:sort; 40, if_statement; 40, 41; 40, 45; 40, 61; 41, call; 41, 42; 41, 43; 42, identifier:len; 43, argument_list; 43, 44; 44, identifier:queries; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:url; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, string:'{}/list?{}'; 52, identifier:format; 53, argument_list; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:url; 57, call; 57, 58; 57, 59; 58, identifier:urlencode; 59, argument_list; 59, 60; 60, identifier:queries; 61, else_clause; 61, 62; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:url; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, string:'{}/list'; 69, identifier:format; 70, argument_list; 70, 71; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:url; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:data; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:_get; 81, argument_list; 81, 82; 82, identifier:url; 83, return_statement; 83, 84; 84, subscript; 84, 85; 84, 90; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:data; 88, identifier:json; 89, argument_list; 90, string:'Results'
def list(self, includes=None, doc_type=None, promulgated_only=False, sort=None, owner=None, series=None): ''' List entities in the charmstore. @param includes What metadata to return in results (e.g. charm-config). @param doc_type Filter to this type: bundle or charm. @param promulgated_only Whether to filter to only promulgated charms. @param sort Sorting the result based on the sort string provided which can be name, author, series and - in front for descending. @param owner Optional owner. If provided, search results will only include entities that owner can view. @param series The series to filter; can be a list of series or a single series. ''' queries = self._common_query_parameters(doc_type, includes, owner, promulgated_only, series, sort) if len(queries): url = '{}/list?{}'.format(self.url, urlencode(queries)) else: url = '{}/list'.format(self.url) data = self._get(url) return data.json()['Results']
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:_common_query_parameters; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:self; 5, identifier:doc_type; 6, identifier:includes; 7, identifier:owner; 8, identifier:promulgated_only; 9, identifier:series; 10, identifier:sort; 11, block; 11, 12; 11, 14; 11, 18; 11, 36; 11, 50; 11, 62; 11, 76; 11, 107; 11, 121; 12, expression_statement; 12, 13; 13, string:''' Extract common query parameters between search and list into slice. @param includes What metadata to return in results (e.g. charm-config). @param doc_type Filter to this type: bundle or charm. @param promulgated_only Whether to filter to only promulgated charms. @param sort Sorting the result based on the sort string provided which can be name, author, series and - in front for descending. @param owner Optional owner. If provided, search results will only include entities that owner can view. @param series The series to filter; can be a list of series or a single series. '''; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:queries; 17, list:[]; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:includes; 21, None; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:queries; 27, identifier:extend; 28, argument_list; 28, 29; 29, list_comprehension; 29, 30; 29, 33; 30, tuple; 30, 31; 30, 32; 31, string:'include'; 32, identifier:include; 33, for_in_clause; 33, 34; 33, 35; 34, identifier:include; 35, identifier:includes; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:doc_type; 39, None; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:queries; 45, identifier:append; 46, argument_list; 46, 47; 47, tuple; 47, 48; 47, 49; 48, string:'type'; 49, identifier:doc_type; 50, if_statement; 50, 51; 50, 52; 51, identifier:promulgated_only; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:queries; 57, identifier:append; 58, argument_list; 58, 59; 59, tuple; 59, 60; 59, 61; 60, string:'promulgated'; 61, integer:1; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:is; 63, 64; 63, 65; 64, identifier:owner; 65, None; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:queries; 71, identifier:append; 72, argument_list; 72, 73; 73, tuple; 73, 74; 73, 75; 74, string:'owner'; 75, identifier:owner; 76, if_statement; 76, 77; 76, 80; 77, comparison_operator:is; 77, 78; 77, 79; 78, identifier:series; 79, None; 80, block; 80, 81; 80, 98; 81, if_statement; 81, 82; 81, 88; 82, comparison_operator:is; 82, 83; 82, 87; 83, call; 83, 84; 83, 85; 84, identifier:type; 85, argument_list; 85, 86; 86, identifier:series; 87, identifier:list; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:series; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, string:','; 95, identifier:join; 96, argument_list; 96, 97; 97, identifier:series; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:queries; 102, identifier:append; 103, argument_list; 103, 104; 104, tuple; 104, 105; 104, 106; 105, string:'series'; 106, identifier:series; 107, if_statement; 107, 108; 107, 111; 108, comparison_operator:is; 108, 109; 108, 110; 109, identifier:sort; 110, None; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:queries; 116, identifier:append; 117, argument_list; 117, 118; 118, tuple; 118, 119; 118, 120; 119, string:'sort'; 120, identifier:sort; 121, return_statement; 121, 122; 122, identifier:queries
def _common_query_parameters(self, doc_type, includes, owner, promulgated_only, series, sort): ''' Extract common query parameters between search and list into slice. @param includes What metadata to return in results (e.g. charm-config). @param doc_type Filter to this type: bundle or charm. @param promulgated_only Whether to filter to only promulgated charms. @param sort Sorting the result based on the sort string provided which can be name, author, series and - in front for descending. @param owner Optional owner. If provided, search results will only include entities that owner can view. @param series The series to filter; can be a list of series or a single series. ''' queries = [] if includes is not None: queries.extend([('include', include) for include in includes]) if doc_type is not None: queries.append(('type', doc_type)) if promulgated_only: queries.append(('promulgated', 1)) if owner is not None: queries.append(('owner', owner)) if series is not None: if type(series) is list: series = ','.join(series) queries.append(('series', series)) if sort is not None: queries.append(('sort', sort)) return queries
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:extract_one; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:L; 6, identifier:P; 7, identifier:R; 8, block; 8, 9; 8, 11; 8, 14; 8, 15; 8, 108; 8, 117; 8, 120; 8, 121; 8, 129; 8, 130; 8, 185; 8, 194; 8, 197; 8, 198; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, yield; 12, 13; 13, string:"*bias*"; 14, comment; 15, if_statement; 15, 16; 15, 21; 15, 26; 15, 36; 16, call; 16, 17; 16, 18; 17, identifier:match; 18, argument_list; 18, 19; 18, 20; 19, identifier:QUOTE; 20, identifier:L; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:L; 25, identifier:QUOTE_TOKEN; 26, elif_clause; 26, 27; 26, 31; 27, call; 27, 28; 27, 29; 28, identifier:isnumberlike; 29, argument_list; 29, 30; 30, identifier:L; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:L; 35, identifier:NUMBER_TOKEN; 36, else_clause; 36, 37; 37, block; 37, 38; 37, 53; 37, 61; 37, 85; 37, 93; 38, expression_statement; 38, 39; 39, yield; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, string:"len(L)={}"; 43, identifier:format; 44, argument_list; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:min; 47, argument_list; 47, 48; 47, 52; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, identifier:L; 52, identifier:CLIP; 53, if_statement; 53, 54; 53, 57; 54, comparison_operator:in; 54, 55; 54, 56; 55, string:"."; 56, identifier:L; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, yield; 59, 60; 60, string:"L:*period*"; 61, if_statement; 61, 62; 61, 66; 62, not_operator; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:nocase; 66, block; 66, 67; 66, 74; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:cf; 70, call; 70, 71; 70, 72; 71, identifier:case_feature; 72, argument_list; 72, 73; 73, identifier:R; 74, if_statement; 74, 75; 74, 76; 75, identifier:cf; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, yield; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, string:"L:{}'"; 82, identifier:format; 83, argument_list; 83, 84; 84, identifier:cf; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:L; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:L; 91, identifier:upper; 92, argument_list; 93, if_statement; 93, 94; 93, 104; 94, not_operator; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:any; 97, generator_expression; 97, 98; 97, 101; 98, comparison_operator:in; 98, 99; 98, 100; 99, identifier:char; 100, identifier:VOWELS; 101, for_in_clause; 101, 102; 101, 103; 102, identifier:char; 103, identifier:L; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, yield; 106, 107; 107, string:"L:*no-vowel*"; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:L_feat; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, string:"L='{}'"; 114, identifier:format; 115, argument_list; 115, 116; 116, identifier:L; 117, expression_statement; 117, 118; 118, yield; 118, 119; 119, identifier:L_feat; 120, comment; 121, expression_statement; 121, 122; 122, yield; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, string:"P='{}'"; 126, identifier:format; 127, argument_list; 127, 128; 128, identifier:P; 129, comment; 130, if_statement; 130, 131; 130, 136; 130, 141; 130, 151; 131, call; 131, 132; 131, 133; 132, identifier:match; 133, argument_list; 133, 134; 133, 135; 134, identifier:QUOTE; 135, identifier:R; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:R; 140, identifier:QUOTE_TOKEN; 141, elif_clause; 141, 142; 141, 146; 142, call; 142, 143; 142, 144; 143, identifier:isnumberlike; 144, argument_list; 144, 145; 145, identifier:R; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:R; 150, identifier:NUMBER_TOKEN; 151, else_clause; 151, 152; 152, block; 152, 153; 152, 177; 153, if_statement; 153, 154; 153, 158; 154, not_operator; 154, 155; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:nocase; 158, block; 158, 159; 158, 166; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:cf; 162, call; 162, 163; 162, 164; 163, identifier:case_feature; 164, argument_list; 164, 165; 165, identifier:R; 166, if_statement; 166, 167; 166, 168; 167, identifier:cf; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, yield; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, string:"R:{}'"; 174, identifier:format; 175, argument_list; 175, 176; 176, identifier:cf; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:R; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:R; 183, identifier:upper; 184, argument_list; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:R_feat; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, string:"R='{}'"; 191, identifier:format; 192, argument_list; 192, 193; 193, identifier:R; 194, expression_statement; 194, 195; 195, yield; 195, 196; 196, identifier:R_feat; 197, comment; 198, expression_statement; 198, 199; 199, yield; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, string:"{},{}"; 203, identifier:format; 204, argument_list; 204, 205; 204, 206; 205, identifier:L_feat; 206, identifier:R_feat
def extract_one(self, L, P, R): """ Given left context `L`, punctuation mark `P`, and right context R`, extract features. Probability distributions for any quantile-based features will not be modified. """ yield "*bias*" # L feature(s) if match(QUOTE, L): L = QUOTE_TOKEN elif isnumberlike(L): L = NUMBER_TOKEN else: yield "len(L)={}".format(min(len(L), CLIP)) if "." in L: yield "L:*period*" if not self.nocase: cf = case_feature(R) if cf: yield "L:{}'".format(cf) L = L.upper() if not any(char in VOWELS for char in L): yield "L:*no-vowel*" L_feat = "L='{}'".format(L) yield L_feat # P feature(s) yield "P='{}'".format(P) # R feature(s) if match(QUOTE, R): R = QUOTE_TOKEN elif isnumberlike(R): R = NUMBER_TOKEN else: if not self.nocase: cf = case_feature(R) if cf: yield "R:{}'".format(cf) R = R.upper() R_feat = "R='{}'".format(R) yield R_feat # the combined L,R feature yield "{},{}".format(L_feat, R_feat)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:call; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:command; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 26; 8, 36; 8, 40; 8, 107; 8, 123; 8, 181; 8, 231; 9, expression_statement; 9, 10; 10, comment; 11, for_statement; 11, 12; 11, 13; 11, 18; 12, identifier:hook; 13, subscript; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:_hooks; 17, string:'precall'; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:hook; 22, argument_list; 22, 23; 22, 24; 22, 25; 23, identifier:self; 24, identifier:command; 25, identifier:kwargs; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:op; 29, call; 29, 30; 29, 31; 30, identifier:getattr; 31, argument_list; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:client; 35, identifier:command; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:attempt; 39, integer:0; 40, while_statement; 40, 41; 40, 42; 41, True; 42, block; 42, 43; 43, try_statement; 43, 44; 43, 54; 44, block; 44, 45; 44, 53; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:data; 48, call; 48, 49; 48, 50; 49, identifier:op; 50, argument_list; 50, 51; 51, dictionary_splat; 51, 52; 52, identifier:kwargs; 53, break_statement; 54, except_clause; 54, 55; 54, 59; 55, as_pattern; 55, 56; 55, 57; 56, identifier:ClientError; 57, as_pattern_target; 57, 58; 58, identifier:e; 59, block; 59, 60; 59, 68; 59, 72; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:exc; 63, call; 63, 64; 63, 65; 64, identifier:translate_exception; 65, argument_list; 65, 66; 65, 67; 66, identifier:e; 67, identifier:kwargs; 68, expression_statement; 68, 69; 69, augmented_assignment:+=; 69, 70; 69, 71; 70, identifier:attempt; 71, integer:1; 72, if_statement; 72, 73; 72, 78; 72, 99; 73, call; 73, 74; 73, 75; 74, identifier:isinstance; 75, argument_list; 75, 76; 75, 77; 76, identifier:exc; 77, identifier:ThroughputException; 78, block; 78, 79; 78, 92; 79, if_statement; 79, 80; 79, 85; 80, comparison_operator:>; 80, 81; 80, 82; 81, identifier:attempt; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:request_retries; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:exc; 90, identifier:re_raise; 91, argument_list; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:exponential_sleep; 97, argument_list; 97, 98; 98, identifier:attempt; 99, else_clause; 99, 100; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:exc; 105, identifier:re_raise; 106, argument_list; 107, for_statement; 107, 108; 107, 109; 107, 114; 108, identifier:hook; 109, subscript; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:_hooks; 113, string:'postcall'; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:hook; 118, argument_list; 118, 119; 118, 120; 118, 121; 118, 122; 119, identifier:self; 120, identifier:command; 121, identifier:kwargs; 122, identifier:data; 123, if_statement; 123, 124; 123, 127; 124, comparison_operator:in; 124, 125; 124, 126; 125, string:'ConsumedCapacity'; 126, identifier:data; 127, block; 127, 128; 127, 134; 127, 140; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:is_read; 131, comparison_operator:in; 131, 132; 131, 133; 132, identifier:command; 133, identifier:READ_COMMANDS; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:consumed; 137, subscript; 137, 138; 137, 139; 138, identifier:data; 139, string:'ConsumedCapacity'; 140, if_statement; 140, 141; 140, 146; 140, 163; 141, call; 141, 142; 141, 143; 142, identifier:isinstance; 143, argument_list; 143, 144; 143, 145; 144, identifier:consumed; 145, identifier:list; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:data; 151, string:'consumed_capacity'; 152, list_comprehension; 152, 153; 152, 160; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:ConsumedCapacity; 156, identifier:from_response; 157, argument_list; 157, 158; 157, 159; 158, identifier:cap; 159, identifier:is_read; 160, for_in_clause; 160, 161; 160, 162; 161, identifier:cap; 162, identifier:consumed; 163, else_clause; 163, 164; 164, block; 164, 165; 164, 175; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:capacity; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:ConsumedCapacity; 171, identifier:from_response; 172, argument_list; 172, 173; 172, 174; 173, identifier:consumed; 174, identifier:is_read; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:data; 179, string:'consumed_capacity'; 180, identifier:capacity; 181, if_statement; 181, 182; 181, 185; 182, comparison_operator:in; 182, 183; 182, 184; 183, string:'consumed_capacity'; 184, identifier:data; 185, block; 185, 186; 185, 210; 186, if_statement; 186, 187; 186, 194; 186, 201; 187, call; 187, 188; 187, 189; 188, identifier:isinstance; 189, argument_list; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:data; 192, string:'consumed_capacity'; 193, identifier:list; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:all_caps; 198, subscript; 198, 199; 198, 200; 199, identifier:data; 200, string:'consumed_capacity'; 201, else_clause; 201, 202; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:all_caps; 206, list:[data['consumed_capacity']]; 206, 207; 207, subscript; 207, 208; 207, 209; 208, identifier:data; 209, string:'consumed_capacity'; 210, for_statement; 210, 211; 210, 212; 210, 217; 211, identifier:hook; 212, subscript; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:_hooks; 216, string:'capacity'; 217, block; 217, 218; 218, for_statement; 218, 219; 218, 220; 218, 221; 219, identifier:cap; 220, identifier:all_caps; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 225; 224, identifier:hook; 225, argument_list; 225, 226; 225, 227; 225, 228; 225, 229; 225, 230; 226, identifier:self; 227, identifier:command; 228, identifier:kwargs; 229, identifier:data; 230, identifier:cap; 231, return_statement; 231, 232; 232, identifier:data
def call(self, command, **kwargs): """ Make a request to DynamoDB using the raw botocore API Parameters ---------- command : str The name of the Dynamo command to execute **kwargs : dict The parameters to pass up in the request Raises ------ exc : :class:`~.DynamoDBError` Returns ------- data : dict """ for hook in self._hooks['precall']: hook(self, command, kwargs) op = getattr(self.client, command) attempt = 0 while True: try: data = op(**kwargs) break except ClientError as e: exc = translate_exception(e, kwargs) attempt += 1 if isinstance(exc, ThroughputException): if attempt > self.request_retries: exc.re_raise() self.exponential_sleep(attempt) else: exc.re_raise() for hook in self._hooks['postcall']: hook(self, command, kwargs, data) if 'ConsumedCapacity' in data: is_read = command in READ_COMMANDS consumed = data['ConsumedCapacity'] if isinstance(consumed, list): data['consumed_capacity'] = [ ConsumedCapacity.from_response(cap, is_read) for cap in consumed ] else: capacity = ConsumedCapacity.from_response(consumed, is_read) data['consumed_capacity'] = capacity if 'consumed_capacity' in data: if isinstance(data['consumed_capacity'], list): all_caps = data['consumed_capacity'] else: all_caps = [data['consumed_capacity']] for hook in self._hooks['capacity']: for cap in all_caps: hook(self, command, kwargs, data, cap) return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 27; 2, function_name:delete_item2; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 4, identifier:self; 5, identifier:tablename; 6, identifier:key; 7, default_parameter; 7, 8; 7, 9; 8, identifier:expr_values; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:alias; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:condition; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:returns; 18, identifier:NONE; 19, default_parameter; 19, 20; 19, 21; 20, identifier:return_capacity; 21, None; 22, default_parameter; 22, 23; 22, 24; 23, identifier:return_item_collection_metrics; 24, identifier:NONE; 25, dictionary_splat_pattern; 25, 26; 26, identifier:kwargs; 27, block; 27, 28; 27, 30; 27, 61; 27, 72; 27, 81; 27, 90; 27, 99; 27, 110; 28, expression_statement; 28, 29; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:keywords; 33, dictionary; 33, 34; 33, 37; 33, 47; 33, 50; 33, 58; 34, pair; 34, 35; 34, 36; 35, string:'TableName'; 36, identifier:tablename; 37, pair; 37, 38; 37, 39; 38, string:'Key'; 39, call; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:dynamizer; 44, identifier:encode_keys; 45, argument_list; 45, 46; 46, identifier:key; 47, pair; 47, 48; 47, 49; 48, string:'ReturnValues'; 49, identifier:returns; 50, pair; 50, 51; 50, 52; 51, string:'ReturnConsumedCapacity'; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:_default_capacity; 56, argument_list; 56, 57; 57, identifier:return_capacity; 58, pair; 58, 59; 58, 60; 59, string:'ReturnItemCollectionMetrics'; 60, identifier:return_item_collection_metrics; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:values; 64, call; 64, 65; 64, 66; 65, identifier:build_expression_values; 66, argument_list; 66, 67; 66, 70; 66, 71; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:dynamizer; 70, identifier:expr_values; 71, identifier:kwargs; 72, if_statement; 72, 73; 72, 74; 73, identifier:values; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:keywords; 79, string:'ExpressionAttributeValues'; 80, identifier:values; 81, if_statement; 81, 82; 81, 83; 82, identifier:alias; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:keywords; 88, string:'ExpressionAttributeNames'; 89, identifier:alias; 90, if_statement; 90, 91; 90, 92; 91, identifier:condition; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:keywords; 97, string:'ConditionExpression'; 98, identifier:condition; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:result; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:call; 106, argument_list; 106, 107; 106, 108; 107, string:'delete_item'; 108, dictionary_splat; 108, 109; 109, identifier:keywords; 110, if_statement; 110, 111; 110, 112; 111, identifier:result; 112, block; 112, 113; 113, return_statement; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:Result; 116, argument_list; 116, 117; 116, 120; 116, 121; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:dynamizer; 120, identifier:result; 121, string:'Attributes'
def delete_item2(self, tablename, key, expr_values=None, alias=None, condition=None, returns=NONE, return_capacity=None, return_item_collection_metrics=NONE, **kwargs): """ Delete an item from a table For many parameters you will want to reference the DynamoDB API: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html Parameters ---------- tablename : str Name of the table to update key : dict Primary key dict specifying the hash key and, if applicable, the range key of the item. expr_values : dict, optional See docs for ExpressionAttributeValues. See also: kwargs alias : dict, optional See docs for ExpressionAttributeNames condition : str, optional See docs for ConditionExpression returns : {NONE, ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATED_NEW}, optional Return either the old or new values, either all attributes or just the ones that changed. (default NONE) return_capacity : {NONE, INDEXES, TOTAL}, optional INDEXES will return the consumed capacity for indexes, TOTAL will return the consumed capacity for the table and the indexes. (default NONE) return_item_collection_metrics : (NONE, SIZE), optional SIZE will return statistics about item collections that were modified. **kwargs : dict, optional If expr_values is not provided, the kwargs dict will be used as the ExpressionAttributeValues (a ':' will be automatically prepended to all keys). """ keywords = { 'TableName': tablename, 'Key': self.dynamizer.encode_keys(key), 'ReturnValues': returns, 'ReturnConsumedCapacity': self._default_capacity(return_capacity), 'ReturnItemCollectionMetrics': return_item_collection_metrics, } values = build_expression_values(self.dynamizer, expr_values, kwargs) if values: keywords['ExpressionAttributeValues'] = values if alias: keywords['ExpressionAttributeNames'] = alias if condition: keywords['ConditionExpression'] = condition result = self.call('delete_item', **keywords) if result: return Result(self.dynamizer, result, 'Attributes')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:update_item; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, identifier:tablename; 6, identifier:key; 7, identifier:updates; 8, default_parameter; 8, 9; 8, 10; 9, identifier:returns; 10, identifier:NONE; 11, default_parameter; 11, 12; 11, 13; 12, identifier:return_capacity; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:expect_or; 16, False; 17, dictionary_splat_pattern; 17, 18; 18, identifier:kwargs; 19, block; 19, 20; 19, 22; 19, 33; 19, 37; 19, 41; 19, 53; 19, 85; 19, 86; 19, 119; 19, 145; 19, 168; 20, expression_statement; 20, 21; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:key; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:dynamizer; 30, identifier:encode_keys; 31, argument_list; 31, 32; 32, identifier:key; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:attr_updates; 36, dictionary; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:expected; 40, dictionary; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:keywords; 44, dictionary; 44, 45; 45, pair; 45, 46; 45, 47; 46, string:'ReturnConsumedCapacity'; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:_default_capacity; 51, argument_list; 51, 52; 52, identifier:return_capacity; 53, for_statement; 53, 54; 53, 55; 53, 56; 54, identifier:update; 55, identifier:updates; 56, block; 56, 57; 56, 71; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:attr_updates; 61, identifier:update; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:update; 66, identifier:attrs; 67, argument_list; 67, 68; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:dynamizer; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:expected; 75, identifier:update; 76, argument_list; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:update; 80, identifier:expected; 81, argument_list; 81, 82; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:dynamizer; 85, comment; 86, for_statement; 86, 87; 86, 90; 86, 102; 87, pattern_list; 87, 88; 87, 89; 88, identifier:k; 89, identifier:v; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:six; 93, identifier:iteritems; 94, argument_list; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:encode_query_kwargs; 97, argument_list; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:dynamizer; 101, identifier:kwargs; 102, block; 102, 103; 102, 113; 103, if_statement; 103, 104; 103, 107; 104, comparison_operator:in; 104, 105; 104, 106; 105, identifier:k; 106, identifier:expected; 107, block; 107, 108; 108, raise_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:ValueError; 111, argument_list; 111, 112; 112, string:"Cannot have more than one condition on a single field"; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:expected; 117, identifier:k; 118, identifier:v; 119, if_statement; 119, 120; 119, 121; 120, identifier:expected; 121, block; 121, 122; 121, 128; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:keywords; 126, string:'Expected'; 127, identifier:expected; 128, if_statement; 128, 129; 128, 135; 129, comparison_operator:>; 129, 130; 129, 134; 130, call; 130, 131; 130, 132; 131, identifier:len; 132, argument_list; 132, 133; 133, identifier:expected; 134, integer:1; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:keywords; 140, string:'ConditionalOperator'; 141, conditional_expression:if; 141, 142; 141, 143; 141, 144; 142, string:'OR'; 143, identifier:expect_or; 144, string:'AND'; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:result; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:call; 152, argument_list; 152, 153; 152, 154; 152, 157; 152, 160; 152, 163; 152, 166; 153, string:'update_item'; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:TableName; 156, identifier:tablename; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:Key; 159, identifier:key; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:AttributeUpdates; 162, identifier:attr_updates; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:ReturnValues; 165, identifier:returns; 166, dictionary_splat; 166, 167; 167, identifier:keywords; 168, if_statement; 168, 169; 168, 170; 169, identifier:result; 170, block; 170, 171; 171, return_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:Result; 174, argument_list; 174, 175; 174, 178; 174, 179; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:dynamizer; 178, identifier:result; 179, string:'Attributes'
def update_item(self, tablename, key, updates, returns=NONE, return_capacity=None, expect_or=False, **kwargs): """ Update a single item in a table This uses the older version of the DynamoDB API. See also: :meth:`~.update_item2`. Parameters ---------- tablename : str Name of the table to update key : dict Primary key dict specifying the hash key and, if applicable, the range key of the item. updates : list List of :class:`~dynamo3.batch.ItemUpdate` returns : {NONE, ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATED_NEW}, optional Return either the old or new values, either all attributes or just the ones that changed. (default NONE) return_capacity : {NONE, INDEXES, TOTAL}, optional INDEXES will return the consumed capacity for indexes, TOTAL will return the consumed capacity for the table and the indexes. (default NONE) expect_or : bool, optional If True, the updates conditionals will be OR'd together. If False, they will be AND'd. (default False). **kwargs : dict, optional Conditional filter on the PUT. Same format as the kwargs for :meth:`~.scan`. Notes ----- There are two ways to specify the expected values of fields. The simplest is via the list of updates. Each updated field may specify a constraint on the current value of that field. You may pass additional constraints in via the **kwargs the same way you would for put_item. This is necessary if you have constraints on fields that are not being updated. """ key = self.dynamizer.encode_keys(key) attr_updates = {} expected = {} keywords = { 'ReturnConsumedCapacity': self._default_capacity(return_capacity), } for update in updates: attr_updates.update(update.attrs(self.dynamizer)) expected.update(update.expected(self.dynamizer)) # Pull the 'expected' constraints from the kwargs for k, v in six.iteritems(encode_query_kwargs(self.dynamizer, kwargs)): if k in expected: raise ValueError("Cannot have more than one condition on a single field") expected[k] = v if expected: keywords['Expected'] = expected if len(expected) > 1: keywords['ConditionalOperator'] = 'OR' if expect_or else 'AND' result = self.call('update_item', TableName=tablename, Key=key, AttributeUpdates=attr_updates, ReturnValues=returns, **keywords) if result: return Result(self.dynamizer, result, 'Attributes')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 38; 2, function_name:query; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 3, 36; 4, identifier:self; 5, identifier:tablename; 6, default_parameter; 6, 7; 6, 8; 7, identifier:attributes; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:consistent; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:count; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:index; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:limit; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:desc; 23, False; 24, default_parameter; 24, 25; 24, 26; 25, identifier:return_capacity; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:filter; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:filter_or; 32, False; 33, default_parameter; 33, 34; 33, 35; 34, identifier:exclusive_start_key; 35, None; 36, dictionary_splat_pattern; 36, 37; 37, identifier:kwargs; 38, block; 38, 39; 38, 41; 38, 72; 38, 83; 38, 94; 38, 128; 38, 147; 38, 162; 39, expression_statement; 39, 40; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:keywords; 44, dictionary; 44, 45; 44, 48; 44, 56; 44, 59; 44, 63; 45, pair; 45, 46; 45, 47; 46, string:'TableName'; 47, identifier:tablename; 48, pair; 48, 49; 48, 50; 49, string:'ReturnConsumedCapacity'; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:_default_capacity; 54, argument_list; 54, 55; 55, identifier:return_capacity; 56, pair; 56, 57; 56, 58; 57, string:'ConsistentRead'; 58, identifier:consistent; 59, pair; 59, 60; 59, 61; 60, string:'ScanIndexForward'; 61, not_operator; 61, 62; 62, identifier:desc; 63, pair; 63, 64; 63, 65; 64, string:'KeyConditions'; 65, call; 65, 66; 65, 67; 66, identifier:encode_query_kwargs; 67, argument_list; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:dynamizer; 71, identifier:kwargs; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:is; 73, 74; 73, 75; 74, identifier:attributes; 75, None; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:keywords; 81, string:'AttributesToGet'; 82, identifier:attributes; 83, if_statement; 83, 84; 83, 87; 84, comparison_operator:is; 84, 85; 84, 86; 85, identifier:index; 86, None; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:keywords; 92, string:'IndexName'; 93, identifier:index; 94, if_statement; 94, 95; 94, 98; 95, comparison_operator:is; 95, 96; 95, 97; 96, identifier:filter; 97, None; 98, block; 98, 99; 98, 116; 99, if_statement; 99, 100; 99, 106; 100, comparison_operator:>; 100, 101; 100, 105; 101, call; 101, 102; 101, 103; 102, identifier:len; 103, argument_list; 103, 104; 104, identifier:filter; 105, integer:1; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:keywords; 111, string:'ConditionalOperator'; 112, conditional_expression:if; 112, 113; 112, 114; 112, 115; 113, string:'OR'; 114, identifier:filter_or; 115, string:'AND'; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 121; 118, subscript; 118, 119; 118, 120; 119, identifier:keywords; 120, string:'QueryFilter'; 121, call; 121, 122; 121, 123; 122, identifier:encode_query_kwargs; 123, argument_list; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:dynamizer; 127, identifier:filter; 128, if_statement; 128, 129; 128, 132; 129, comparison_operator:is; 129, 130; 129, 131; 130, identifier:exclusive_start_key; 131, None; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 134, 139; 135, subscript; 135, 136; 135, 137; 136, identifier:keywords; 137, string:'ExclusiveStartKey'; 138, line_continuation:\; 139, call; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:dynamizer; 144, identifier:maybe_encode_keys; 145, argument_list; 145, 146; 146, identifier:exclusive_start_key; 147, if_statement; 147, 148; 147, 154; 148, not_operator; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:isinstance; 151, argument_list; 151, 152; 151, 153; 152, identifier:limit; 153, identifier:Limit; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:limit; 158, call; 158, 159; 158, 160; 159, identifier:Limit; 160, argument_list; 160, 161; 161, identifier:limit; 162, if_statement; 162, 163; 162, 164; 162, 180; 163, identifier:count; 164, block; 164, 165; 164, 171; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:keywords; 169, string:'Select'; 170, identifier:COUNT; 171, return_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:_count; 176, argument_list; 176, 177; 176, 178; 176, 179; 177, string:'query'; 178, identifier:limit; 179, identifier:keywords; 180, else_clause; 180, 181; 181, block; 181, 182; 182, return_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:ResultSet; 185, argument_list; 185, 186; 185, 187; 185, 188; 185, 189; 186, identifier:self; 187, identifier:limit; 188, string:'query'; 189, dictionary_splat; 189, 190; 190, identifier:keywords
def query(self, tablename, attributes=None, consistent=False, count=False, index=None, limit=None, desc=False, return_capacity=None, filter=None, filter_or=False, exclusive_start_key=None, **kwargs): """ Perform an index query on a table This uses the older version of the DynamoDB API. See also: :meth:`~.query2`. Parameters ---------- tablename : str Name of the table to query attributes : list If present, only fetch these attributes from the item consistent : bool, optional Perform a strongly consistent read of the data (default False) count : bool, optional If True, return a count of matched items instead of the items themselves (default False) index : str, optional The name of the index to query limit : int, optional Maximum number of items to return desc : bool, optional If True, return items in descending order (default False) return_capacity : {NONE, INDEXES, TOTAL}, optional INDEXES will return the consumed capacity for indexes, TOTAL will return the consumed capacity for the table and the indexes. (default NONE) filter : dict, optional Query arguments. Same format as **kwargs, but these arguments filter the results on the server before they are returned. They will NOT use an index, as that is what the **kwargs are for. filter_or : bool, optional If True, multiple filter args will be OR'd together. If False, they will be AND'd together. (default False) exclusive_start_key : dict, optional The ExclusiveStartKey to resume a previous query **kwargs : dict, optional Query arguments (examples below) Examples -------- You may pass in constraints using the Django-style '__' syntax. For example: .. code-block:: python connection.query('mytable', foo__eq=5) connection.query('mytable', foo__eq=5, bar__lt=22) connection.query('mytable', foo__eq=5, bar__between=(1, 10)) """ keywords = { 'TableName': tablename, 'ReturnConsumedCapacity': self._default_capacity(return_capacity), 'ConsistentRead': consistent, 'ScanIndexForward': not desc, 'KeyConditions': encode_query_kwargs(self.dynamizer, kwargs), } if attributes is not None: keywords['AttributesToGet'] = attributes if index is not None: keywords['IndexName'] = index if filter is not None: if len(filter) > 1: keywords['ConditionalOperator'] = 'OR' if filter_or else 'AND' keywords['QueryFilter'] = encode_query_kwargs(self.dynamizer, filter) if exclusive_start_key is not None: keywords['ExclusiveStartKey'] = \ self.dynamizer.maybe_encode_keys(exclusive_start_key) if not isinstance(limit, Limit): limit = Limit(limit) if count: keywords['Select'] = COUNT return self._count('query', limit, keywords) else: return ResultSet(self, limit, 'query', **keywords)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:letter_scales; 3, parameters; 3, 4; 4, identifier:counts; 5, block; 5, 6; 5, 8; 5, 29; 5, 49; 5, 63; 6, expression_statement; 6, 7; 7, comment; 8, try_statement; 8, 9; 8, 23; 9, block; 9, 10; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:scale; 13, binary_operator:/; 13, 14; 13, 15; 14, float:1.0; 15, call; 15, 16; 15, 17; 16, identifier:sum; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:counts; 21, identifier:values; 22, argument_list; 23, except_clause; 23, 24; 23, 25; 23, 26; 24, identifier:ZeroDivisionError; 25, comment; 26, block; 26, 27; 27, return_statement; 27, 28; 28, list:[]; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:freqs; 32, list_comprehension; 32, 33; 32, 38; 32, 47; 33, tuple; 33, 34; 33, 35; 34, identifier:aa; 35, binary_operator:*; 35, 36; 35, 37; 36, identifier:cnt; 37, identifier:scale; 38, for_in_clause; 38, 39; 38, 42; 39, pattern_list; 39, 40; 39, 41; 40, identifier:aa; 41, identifier:cnt; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:counts; 45, identifier:iteritems; 46, argument_list; 47, if_clause; 47, 48; 48, identifier:cnt; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:freqs; 53, identifier:sort; 54, argument_list; 54, 55; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:key; 57, lambda; 57, 58; 57, 60; 58, lambda_parameters; 58, 59; 59, identifier:pair; 60, subscript; 60, 61; 60, 62; 61, identifier:pair; 62, integer:1; 63, return_statement; 63, 64; 64, identifier:freqs
def letter_scales(counts): """Convert letter counts to frequencies, sorted increasing.""" try: scale = 1.0 / sum(counts.values()) except ZeroDivisionError: # This logo is all gaps, nothing can be done return [] freqs = [(aa, cnt*scale) for aa, cnt in counts.iteritems() if cnt] freqs.sort(key=lambda pair: pair[1]) return freqs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:count_diffs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 4, identifier:align; 5, identifier:feats; 6, identifier:inseq; 7, identifier:locus; 8, identifier:cutoff; 9, default_parameter; 9, 10; 9, 11; 10, identifier:verbose; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:verbosity; 14, integer:0; 15, block; 15, 16; 15, 18; 15, 29; 15, 33; 15, 37; 15, 41; 15, 45; 15, 49; 15, 53; 15, 82; 15, 83; 15, 189; 15, 195; 15, 201; 15, 207; 15, 213; 15, 219; 15, 228; 15, 239; 15, 364; 15, 370; 15, 371; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:nfeats; 21, call; 21, 22; 21, 23; 22, identifier:len; 23, argument_list; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:feats; 27, identifier:keys; 28, argument_list; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:mm; 32, integer:0; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:insr; 36, integer:0; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:dels; 40, integer:0; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:gaps; 44, integer:0; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:match; 48, integer:0; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:lastb; 52, string:''; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:l; 56, conditional_expression:if; 56, 57; 56, 63; 56, 76; 57, call; 57, 58; 57, 59; 58, identifier:len; 59, argument_list; 59, 60; 60, subscript; 60, 61; 60, 62; 61, identifier:align; 62, integer:0; 63, comparison_operator:>; 63, 64; 63, 70; 64, call; 64, 65; 64, 66; 65, identifier:len; 66, argument_list; 66, 67; 67, subscript; 67, 68; 67, 69; 68, identifier:align; 69, integer:0; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, subscript; 73, 74; 73, 75; 74, identifier:align; 75, integer:1; 76, call; 76, 77; 76, 78; 77, identifier:len; 78, argument_list; 78, 79; 79, subscript; 79, 80; 79, 81; 80, identifier:align; 81, integer:1; 82, comment; 83, for_statement; 83, 84; 83, 85; 83, 90; 84, identifier:i; 85, call; 85, 86; 85, 87; 86, identifier:range; 87, argument_list; 87, 88; 87, 89; 88, integer:0; 89, identifier:l; 90, block; 90, 91; 91, if_statement; 91, 92; 91, 107; 91, 160; 92, boolean_operator:or; 92, 93; 92, 100; 93, comparison_operator:==; 93, 94; 93, 99; 94, subscript; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:align; 97, integer:0; 98, identifier:i; 99, string:"-"; 100, comparison_operator:==; 100, 101; 100, 106; 101, subscript; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:align; 104, integer:1; 105, identifier:i; 106, string:"-"; 107, block; 107, 108; 107, 134; 108, if_statement; 108, 109; 108, 116; 109, comparison_operator:==; 109, 110; 109, 115; 110, subscript; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:align; 113, integer:0; 114, identifier:i; 115, string:"-"; 116, block; 116, 117; 116, 121; 116, 130; 117, expression_statement; 117, 118; 118, augmented_assignment:+=; 118, 119; 118, 120; 119, identifier:insr; 120, integer:1; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:!=; 122, 123; 122, 124; 123, identifier:lastb; 124, string:'-'; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, augmented_assignment:+=; 127, 128; 127, 129; 128, identifier:gaps; 129, integer:1; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:lastb; 133, string:"-"; 134, if_statement; 134, 135; 134, 142; 135, comparison_operator:==; 135, 136; 135, 141; 136, subscript; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:align; 139, integer:1; 140, identifier:i; 141, string:"-"; 142, block; 142, 143; 142, 147; 142, 156; 143, expression_statement; 143, 144; 144, augmented_assignment:+=; 144, 145; 144, 146; 145, identifier:dels; 146, integer:1; 147, if_statement; 147, 148; 147, 151; 148, comparison_operator:!=; 148, 149; 148, 150; 149, identifier:lastb; 150, string:'-'; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, augmented_assignment:+=; 153, 154; 153, 155; 154, identifier:gaps; 155, integer:1; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:lastb; 159, string:"-"; 160, else_clause; 160, 161; 161, block; 161, 162; 161, 166; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:lastb; 165, string:''; 166, if_statement; 166, 167; 166, 178; 166, 183; 167, comparison_operator:!=; 167, 168; 167, 173; 168, subscript; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:align; 171, integer:0; 172, identifier:i; 173, subscript; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:align; 176, integer:1; 177, identifier:i; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, augmented_assignment:+=; 180, 181; 180, 182; 181, identifier:mm; 182, integer:1; 183, else_clause; 183, 184; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, augmented_assignment:+=; 186, 187; 186, 188; 187, identifier:match; 188, integer:1; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:gper; 192, binary_operator:/; 192, 193; 192, 194; 193, identifier:gaps; 194, identifier:nfeats; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:delper; 198, binary_operator:/; 198, 199; 198, 200; 199, identifier:dels; 200, identifier:l; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:iper; 204, binary_operator:/; 204, 205; 204, 206; 205, identifier:insr; 206, identifier:l; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:mmper; 210, binary_operator:/; 210, 211; 210, 212; 211, identifier:mm; 212, identifier:l; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:mper; 216, binary_operator:/; 216, 217; 216, 218; 217, identifier:match; 218, identifier:l; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:mper2; 222, binary_operator:/; 222, 223; 222, 224; 223, identifier:match; 224, call; 224, 225; 224, 226; 225, identifier:len; 226, argument_list; 226, 227; 227, identifier:inseq; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:logger; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:logging; 234, identifier:getLogger; 235, argument_list; 235, 236; 236, binary_operator:+; 236, 237; 236, 238; 237, string:"Logger."; 238, identifier:__name__; 239, if_statement; 239, 240; 239, 245; 240, boolean_operator:and; 240, 241; 240, 242; 241, identifier:verbose; 242, comparison_operator:>; 242, 243; 242, 244; 243, identifier:verbosity; 244, integer:0; 245, block; 245, 246; 245, 267; 245, 280; 245, 294; 245, 308; 245, 322; 245, 336; 245, 350; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:logger; 250, identifier:info; 251, argument_list; 251, 252; 252, binary_operator:+; 252, 253; 252, 254; 253, string:"Features algined = "; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, string:","; 257, identifier:join; 258, argument_list; 258, 259; 259, call; 259, 260; 259, 261; 260, identifier:list; 261, argument_list; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:feats; 265, identifier:keys; 266, argument_list; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:logger; 271, identifier:info; 272, argument_list; 272, 273; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, string:'{:<22}{:<6d}'; 276, identifier:format; 277, argument_list; 277, 278; 277, 279; 278, string:"Number of feats: "; 279, identifier:nfeats; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:logger; 284, identifier:info; 285, argument_list; 285, 286; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, string:'{:<22}{:<6d}{:<1.2f}'; 289, identifier:format; 290, argument_list; 290, 291; 290, 292; 290, 293; 291, string:"Number of gaps: "; 292, identifier:gaps; 293, identifier:gper; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:logger; 298, identifier:info; 299, argument_list; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, string:'{:<22}{:<6d}{:<1.2f}'; 303, identifier:format; 304, argument_list; 304, 305; 304, 306; 304, 307; 305, string:"Number of deletions: "; 306, identifier:dels; 307, identifier:delper; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:logger; 312, identifier:info; 313, argument_list; 313, 314; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, string:'{:<22}{:<6d}{:<1.2f}'; 317, identifier:format; 318, argument_list; 318, 319; 318, 320; 318, 321; 319, string:"Number of insertions: "; 320, identifier:insr; 321, identifier:iper; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:logger; 326, identifier:info; 327, argument_list; 327, 328; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, string:'{:<22}{:<6d}{:<1.2f}'; 331, identifier:format; 332, argument_list; 332, 333; 332, 334; 332, 335; 333, string:"Number of mismatches: "; 334, identifier:mm; 335, identifier:mmper; 336, expression_statement; 336, 337; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:logger; 340, identifier:info; 341, argument_list; 341, 342; 342, call; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, string:'{:<22}{:<6d}{:<1.2f}'; 345, identifier:format; 346, argument_list; 346, 347; 346, 348; 346, 349; 347, string:"Number of matches: "; 348, identifier:match; 349, identifier:mper; 350, expression_statement; 350, 351; 351, call; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:logger; 354, identifier:info; 355, argument_list; 355, 356; 356, call; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, string:'{:<22}{:<6d}{:<1.2f}'; 359, identifier:format; 360, argument_list; 360, 361; 360, 362; 360, 363; 361, string:"Number of matches: "; 362, identifier:match; 363, identifier:mper2; 364, expression_statement; 364, 365; 365, assignment; 365, 366; 365, 367; 366, identifier:indel; 367, binary_operator:+; 367, 368; 367, 369; 368, identifier:iper; 369, identifier:delper; 370, comment; 371, if_statement; 371, 372; 371, 386; 371, 401; 372, boolean_operator:and; 372, 373; 372, 383; 373, boolean_operator:and; 373, 374; 373, 380; 374, comparison_operator:>; 374, 375; 374, 379; 375, call; 375, 376; 375, 377; 376, identifier:len; 377, argument_list; 377, 378; 378, identifier:inseq; 379, integer:6000; 380, comparison_operator:<; 380, 381; 380, 382; 381, identifier:mmper; 382, float:.10; 383, comparison_operator:>; 383, 384; 383, 385; 384, identifier:mper2; 385, float:.80; 386, block; 386, 387; 386, 397; 387, if_statement; 387, 388; 387, 389; 388, identifier:verbose; 389, block; 389, 390; 390, expression_statement; 390, 391; 391, call; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:logger; 394, identifier:info; 395, argument_list; 395, 396; 396, string:"Alignment coverage high enough to complete annotation 11"; 397, return_statement; 397, 398; 398, expression_list; 398, 399; 398, 400; 399, identifier:insr; 400, identifier:dels; 401, else_clause; 401, 402; 401, 403; 402, comment; 403, block; 403, 404; 403, 410; 404, expression_statement; 404, 405; 405, assignment; 405, 406; 405, 407; 406, identifier:indel_mm; 407, binary_operator:+; 407, 408; 407, 409; 408, identifier:indel; 409, identifier:mper2; 410, if_statement; 410, 411; 410, 427; 410, 445; 411, boolean_operator:and; 411, 412; 411, 424; 412, boolean_operator:and; 412, 413; 412, 421; 413, parenthesized_expression; 413, 414; 414, boolean_operator:or; 414, 415; 414, 418; 415, comparison_operator:>; 415, 416; 415, 417; 416, identifier:indel; 417, float:0.5; 418, comparison_operator:>; 418, 419; 418, 420; 419, identifier:mmper; 420, float:0.05; 421, comparison_operator:<; 421, 422; 421, 423; 422, identifier:mper2; 423, identifier:cutoff; 424, comparison_operator:!=; 424, 425; 424, 426; 425, identifier:indel_mm; 426, integer:1; 427, block; 427, 428; 427, 438; 428, if_statement; 428, 429; 428, 430; 429, identifier:verbose; 430, block; 430, 431; 431, expression_statement; 431, 432; 432, call; 432, 433; 432, 436; 433, attribute; 433, 434; 433, 435; 434, identifier:logger; 435, identifier:info; 436, argument_list; 436, 437; 437, string:"Alignment coverage NOT high enough to return annotation"; 438, return_statement; 438, 439; 439, call; 439, 440; 439, 441; 440, identifier:Annotation; 441, argument_list; 441, 442; 442, keyword_argument; 442, 443; 442, 444; 443, identifier:complete_annotation; 444, False; 445, else_clause; 445, 446; 446, block; 446, 447; 446, 457; 447, if_statement; 447, 448; 447, 449; 448, identifier:verbose; 449, block; 449, 450; 450, expression_statement; 450, 451; 451, call; 451, 452; 451, 455; 452, attribute; 452, 453; 452, 454; 453, identifier:logger; 454, identifier:info; 455, argument_list; 455, 456; 456, string:"Alignment coverage high enough to complete annotation"; 457, return_statement; 457, 458; 458, expression_list; 458, 459; 458, 460; 459, identifier:insr; 460, identifier:dels
def count_diffs(align, feats, inseq, locus, cutoff, verbose=False, verbosity=0): """ count_diffs - Counts the number of mismatches, gaps, and insertions and then determines if those are within an acceptable range. :param align: The alignment :type align: ``List`` :param feats: Dictonary of the features :type feats: ``dict`` :param locus: The gene locus associated with the sequence. :type locus: ``str`` :param inseq: The input sequence :type inseq: ``str`` :param cutoff: The alignment cutoff :type cutoff: ``float`` :param verbose: Flag for running in verbose mode. :type verbose: ``bool`` :param verbosity: Numerical value to indicate how verbose the output will be in verbose mode. :type verbosity: ``int`` :rtype: ``List`` """ nfeats = len(feats.keys()) mm = 0 insr = 0 dels = 0 gaps = 0 match = 0 lastb = '' l = len(align[0]) if len(align[0]) > len(align[1]) else len(align[1]) # Counting gaps, mismatches and insertions for i in range(0, l): if align[0][i] == "-" or align[1][i] == "-": if align[0][i] == "-": insr += 1 if lastb != '-': gaps += 1 lastb = "-" if align[1][i] == "-": dels += 1 if lastb != '-': gaps += 1 lastb = "-" else: lastb = '' if align[0][i] != align[1][i]: mm += 1 else: match += 1 gper = gaps / nfeats delper = dels / l iper = insr / l mmper = mm / l mper = match / l mper2 = match / len(inseq) logger = logging.getLogger("Logger." + __name__) if verbose and verbosity > 0: logger.info("Features algined = " + ",".join(list(feats.keys()))) logger.info('{:<22}{:<6d}'.format("Number of feats: ", nfeats)) logger.info('{:<22}{:<6d}{:<1.2f}'.format("Number of gaps: ", gaps, gper)) logger.info('{:<22}{:<6d}{:<1.2f}'.format("Number of deletions: ", dels, delper)) logger.info('{:<22}{:<6d}{:<1.2f}'.format("Number of insertions: ", insr, iper)) logger.info('{:<22}{:<6d}{:<1.2f}'.format("Number of mismatches: ", mm, mmper)) logger.info('{:<22}{:<6d}{:<1.2f}'.format("Number of matches: ", match, mper)) logger.info('{:<22}{:<6d}{:<1.2f}'.format("Number of matches: ", match, mper2)) indel = iper + delper # ** HARD CODED LOGIC ** # if len(inseq) > 6000 and mmper < .10 and mper2 > .80: if verbose: logger.info("Alignment coverage high enough to complete annotation 11") return insr, dels else: # TODO: These numbers need to be fine tuned indel_mm = indel + mper2 if (indel > 0.5 or mmper > 0.05) and mper2 < cutoff and indel_mm != 1: if verbose: logger.info("Alignment coverage NOT high enough to return annotation") return Annotation(complete_annotation=False) else: if verbose: logger.info("Alignment coverage high enough to complete annotation") return insr, dels
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:cmd_debug; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:argv; 6, identifier:help; 7, block; 7, 8; 7, 10; 7, 28; 7, 34; 7, 56; 7, 73; 7, 90; 7, 107; 7, 124; 7, 147; 7, 156; 7, 165; 7, 173; 7, 183; 7, 317; 7, 347; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:parser; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:argparse; 16, identifier:ArgumentParser; 17, argument_list; 17, 18; 17, 25; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:prog; 20, binary_operator:%; 20, 21; 20, 22; 21, string:"%s debug"; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:progname; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:description; 27, identifier:help; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:instances; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:instances; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:parser; 38, identifier:add_argument; 39, argument_list; 39, 40; 39, 41; 39, 44; 39, 47; 39, 50; 40, string:"instance"; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:nargs; 43, integer:1; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:metavar; 46, string:"instance"; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:help; 49, string:"Name of the instance from the config."; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:choices; 52, call; 52, 53; 52, 54; 53, identifier:sorted; 54, argument_list; 54, 55; 55, identifier:instances; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:parser; 60, identifier:add_argument; 61, argument_list; 61, 62; 61, 63; 61, 64; 61, 67; 61, 70; 62, string:"-v"; 63, string:"--verbose"; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:dest; 66, string:"verbose"; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:action; 69, string:"store_true"; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:help; 72, string:"Print more info and output the startup script"; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:parser; 77, identifier:add_argument; 78, argument_list; 78, 79; 78, 80; 78, 81; 78, 84; 78, 87; 79, string:"-c"; 80, string:"--console-output"; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:dest; 83, string:"console_output"; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:action; 86, string:"store_true"; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:help; 89, string:"Prints the console output of the instance if available"; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:parser; 94, identifier:add_argument; 95, argument_list; 95, 96; 95, 97; 95, 98; 95, 101; 95, 104; 96, string:"-i"; 97, string:"--interactive"; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:dest; 100, string:"interactive"; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:action; 103, string:"store_true"; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:help; 106, string:"Creates a connection and drops you into an interactive Python session"; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:parser; 111, identifier:add_argument; 112, argument_list; 112, 113; 112, 114; 112, 115; 112, 118; 112, 121; 113, string:"-r"; 114, string:"--raw"; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:dest; 117, string:"raw"; 118, keyword_argument; 118, 119; 118, 120; 119, identifier:action; 120, string:"store_true"; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:help; 123, string:"Outputs the raw possibly compressed startup script"; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:parser; 128, identifier:add_argument; 129, argument_list; 129, 130; 129, 131; 129, 132; 129, 135; 129, 138; 129, 141; 129, 144; 130, string:"-o"; 131, string:"--override"; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:nargs; 134, string:"*"; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:type; 137, identifier:str; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:dest; 140, string:"overrides"; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:metavar; 143, string:"OVERRIDE"; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:help; 146, string:"Option to override instance config for startup script (name=value)."; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:args; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:parser; 153, identifier:parse_args; 154, argument_list; 154, 155; 155, identifier:argv; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:overrides; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:_parse_overrides; 163, argument_list; 163, 164; 164, identifier:args; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:overrides; 169, string:'instances'; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:instances; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:instance; 176, subscript; 176, 177; 176, 178; 177, identifier:instances; 178, subscript; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:args; 181, identifier:instance; 182, integer:0; 183, if_statement; 183, 184; 183, 189; 184, call; 184, 185; 184, 186; 185, identifier:hasattr; 186, argument_list; 186, 187; 186, 188; 187, identifier:instance; 188, string:'startup_script'; 189, block; 189, 190; 189, 204; 189, 215; 189, 229; 189, 287; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:startup_script; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:instance; 196, identifier:startup_script; 197, argument_list; 197, 198; 197, 201; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:overrides; 200, identifier:overrides; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:debug; 203, True; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:max_size; 207, call; 207, 208; 207, 209; 208, identifier:getattr; 209, argument_list; 209, 210; 209, 211; 209, 212; 210, identifier:instance; 211, string:'max_startup_script_size'; 212, binary_operator:*; 212, 213; 212, 214; 213, integer:16; 214, integer:1024; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:log; 219, identifier:info; 220, argument_list; 220, 221; 220, 222; 220, 228; 221, string:"Length of startup script: %s/%s"; 222, call; 222, 223; 222, 224; 223, identifier:len; 224, argument_list; 224, 225; 225, subscript; 225, 226; 225, 227; 226, identifier:startup_script; 227, string:'raw'; 228, identifier:max_size; 229, if_statement; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:args; 232, identifier:verbose; 233, block; 233, 234; 234, if_statement; 234, 235; 234, 240; 234, 278; 235, comparison_operator:in; 235, 236; 235, 237; 236, string:'startup_script'; 237, attribute; 237, 238; 237, 239; 238, identifier:instance; 239, identifier:config; 240, block; 240, 241; 241, if_statement; 241, 242; 241, 249; 241, 257; 241, 269; 242, comparison_operator:==; 242, 243; 242, 246; 243, subscript; 243, 244; 243, 245; 244, identifier:startup_script; 245, string:'original'; 246, subscript; 246, 247; 246, 248; 247, identifier:startup_script; 248, string:'raw'; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:log; 254, identifier:info; 255, argument_list; 255, 256; 256, string:"Startup script:"; 257, elif_clause; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:args; 260, identifier:raw; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:log; 266, identifier:info; 267, argument_list; 267, 268; 268, string:"Compressed startup script:"; 269, else_clause; 269, 270; 270, block; 270, 271; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:log; 275, identifier:info; 276, argument_list; 276, 277; 277, string:"Uncompressed startup script:"; 278, else_clause; 278, 279; 279, block; 279, 280; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:log; 284, identifier:info; 285, argument_list; 285, 286; 286, string:"No startup script specified"; 287, if_statement; 287, 288; 287, 291; 287, 302; 288, attribute; 288, 289; 288, 290; 289, identifier:args; 290, identifier:raw; 291, block; 291, 292; 292, expression_statement; 292, 293; 293, call; 293, 294; 293, 295; 294, identifier:print; 295, argument_list; 295, 296; 295, 299; 296, subscript; 296, 297; 296, 298; 297, identifier:startup_script; 298, string:'raw'; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:end; 301, string:''; 302, elif_clause; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:args; 305, identifier:verbose; 306, block; 306, 307; 307, expression_statement; 307, 308; 308, call; 308, 309; 308, 310; 309, identifier:print; 310, argument_list; 310, 311; 310, 314; 311, subscript; 311, 312; 311, 313; 312, identifier:startup_script; 313, string:'original'; 314, keyword_argument; 314, 315; 314, 316; 315, identifier:end; 316, string:''; 317, if_statement; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:args; 320, identifier:console_output; 321, block; 321, 322; 322, if_statement; 322, 323; 322, 328; 322, 338; 323, call; 323, 324; 323, 325; 324, identifier:hasattr; 325, argument_list; 325, 326; 325, 327; 326, identifier:instance; 327, string:'get_console_output'; 328, block; 328, 329; 329, expression_statement; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:print; 332, argument_list; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:instance; 336, identifier:get_console_output; 337, argument_list; 338, else_clause; 338, 339; 339, block; 339, 340; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:log; 344, identifier:error; 345, argument_list; 345, 346; 346, string:"The instance doesn't support console output."; 347, if_statement; 347, 348; 347, 351; 347, 352; 348, attribute; 348, 349; 348, 350; 349, identifier:args; 350, identifier:interactive; 351, comment; 352, block; 352, 353; 352, 356; 352, 361; 352, 381; 352, 388; 352, 411; 353, import_statement; 353, 354; 354, dotted_name; 354, 355; 355, identifier:readline; 356, import_from_statement; 356, 357; 356, 359; 357, dotted_name; 357, 358; 358, identifier:pprint; 359, dotted_name; 359, 360; 360, identifier:pprint; 361, expression_statement; 361, 362; 362, assignment; 362, 363; 362, 364; 363, identifier:local; 364, call; 364, 365; 364, 366; 365, identifier:dict; 366, argument_list; 366, 367; 366, 370; 366, 375; 366, 378; 367, keyword_argument; 367, 368; 367, 369; 368, identifier:ctrl; 369, identifier:self; 370, keyword_argument; 370, 371; 370, 372; 371, identifier:instances; 372, attribute; 372, 373; 372, 374; 373, identifier:self; 374, identifier:instances; 375, keyword_argument; 375, 376; 375, 377; 376, identifier:instance; 377, identifier:instance; 378, keyword_argument; 378, 379; 378, 380; 379, identifier:pprint; 380, identifier:pprint; 381, expression_statement; 381, 382; 382, call; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, identifier:readline; 385, identifier:parse_and_bind; 386, argument_list; 386, 387; 387, string:'tab: complete'; 388, try_statement; 388, 389; 388, 407; 389, block; 389, 390; 389, 393; 390, import_statement; 390, 391; 391, dotted_name; 391, 392; 392, identifier:rlcompleter; 393, expression_statement; 393, 394; 394, call; 394, 395; 394, 398; 395, attribute; 395, 396; 395, 397; 396, identifier:readline; 397, identifier:set_completer; 398, argument_list; 398, 399; 399, attribute; 399, 400; 399, 406; 400, call; 400, 401; 400, 404; 401, attribute; 401, 402; 401, 403; 402, identifier:rlcompleter; 403, identifier:Completer; 404, argument_list; 404, 405; 405, identifier:local; 406, identifier:complete; 407, except_clause; 407, 408; 407, 409; 408, identifier:ImportError; 409, block; 409, 410; 410, pass_statement; 411, expression_statement; 411, 412; 412, call; 412, 413; 412, 419; 413, attribute; 413, 414; 413, 418; 414, call; 414, 415; 414, 416; 415, identifier:__import__; 416, argument_list; 416, 417; 417, string:"code"; 418, identifier:interact; 419, argument_list; 419, 420; 420, keyword_argument; 420, 421; 420, 422; 421, identifier:local; 422, identifier:local
def cmd_debug(self, argv, help): """Prints some debug info for this script""" parser = argparse.ArgumentParser( prog="%s debug" % self.progname, description=help, ) instances = self.instances parser.add_argument("instance", nargs=1, metavar="instance", help="Name of the instance from the config.", choices=sorted(instances)) parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", help="Print more info and output the startup script") parser.add_argument("-c", "--console-output", dest="console_output", action="store_true", help="Prints the console output of the instance if available") parser.add_argument("-i", "--interactive", dest="interactive", action="store_true", help="Creates a connection and drops you into an interactive Python session") parser.add_argument("-r", "--raw", dest="raw", action="store_true", help="Outputs the raw possibly compressed startup script") parser.add_argument("-o", "--override", nargs="*", type=str, dest="overrides", metavar="OVERRIDE", help="Option to override instance config for startup script (name=value).") args = parser.parse_args(argv) overrides = self._parse_overrides(args) overrides['instances'] = self.instances instance = instances[args.instance[0]] if hasattr(instance, 'startup_script'): startup_script = instance.startup_script(overrides=overrides, debug=True) max_size = getattr(instance, 'max_startup_script_size', 16 * 1024) log.info("Length of startup script: %s/%s", len(startup_script['raw']), max_size) if args.verbose: if 'startup_script' in instance.config: if startup_script['original'] == startup_script['raw']: log.info("Startup script:") elif args.raw: log.info("Compressed startup script:") else: log.info("Uncompressed startup script:") else: log.info("No startup script specified") if args.raw: print(startup_script['raw'], end='') elif args.verbose: print(startup_script['original'], end='') if args.console_output: if hasattr(instance, 'get_console_output'): print(instance.get_console_output()) else: log.error("The instance doesn't support console output.") if args.interactive: # pragma: no cover import readline from pprint import pprint local = dict( ctrl=self, instances=self.instances, instance=instance, pprint=pprint) readline.parse_and_bind('tab: complete') try: import rlcompleter readline.set_completer(rlcompleter.Completer(local).complete) except ImportError: pass __import__("code").interact(local=local)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:cmd_ssh; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:argv; 6, identifier:help; 7, block; 7, 8; 7, 10; 7, 28; 7, 39; 7, 61; 7, 76; 7, 83; 7, 87; 7, 91; 7, 148; 7, 149; 7, 194; 7, 200; 7, 216; 7, 274; 7, 280; 7, 292; 7, 298; 7, 314; 7, 324; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:parser; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:argparse; 16, identifier:ArgumentParser; 17, argument_list; 17, 18; 17, 25; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:prog; 20, binary_operator:%; 20, 21; 20, 22; 21, string:"%s ssh"; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:progname; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:description; 27, identifier:help; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:instances; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:get_instances; 35, argument_list; 35, 36; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:command; 38, string:'init_ssh_key'; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:parser; 43, identifier:add_argument; 44, argument_list; 44, 45; 44, 46; 44, 49; 44, 52; 44, 55; 45, string:"instance"; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:nargs; 48, integer:1; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:metavar; 51, string:"instance"; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:help; 54, string:"Name of the instance from the config."; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:choices; 57, call; 57, 58; 57, 59; 58, identifier:sorted; 59, argument_list; 59, 60; 60, identifier:instances; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:parser; 65, identifier:add_argument; 66, argument_list; 66, 67; 66, 68; 66, 73; 67, string:"..."; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:nargs; 70, attribute; 70, 71; 70, 72; 71, identifier:argparse; 72, identifier:REMAINDER; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:help; 75, string:"ssh options"; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:iargs; 79, call; 79, 80; 79, 81; 80, identifier:enumerate; 81, argument_list; 81, 82; 82, identifier:argv; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:sid_index; 86, None; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:user; 90, None; 91, for_statement; 91, 92; 91, 95; 91, 96; 92, pattern_list; 92, 93; 92, 94; 93, identifier:i; 94, identifier:arg; 95, identifier:iargs; 96, block; 96, 97; 96, 111; 97, if_statement; 97, 98; 97, 105; 98, not_operator; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:arg; 102, identifier:startswith; 103, argument_list; 103, 104; 104, string:'-'; 105, block; 105, 106; 105, 110; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:sid_index; 109, identifier:i; 110, break_statement; 111, if_statement; 111, 112; 111, 117; 111, 119; 112, comparison_operator:in; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:arg; 115, integer:1; 116, string:'1246AaCfgKkMNnqsTtVvXxYy'; 117, block; 117, 118; 118, continue_statement; 119, elif_clause; 119, 120; 119, 125; 120, comparison_operator:in; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:arg; 123, integer:1; 124, string:'bcDeFiLlmOopRSw'; 125, block; 125, 126; 125, 134; 125, 147; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:value; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:iargs; 132, identifier:next; 133, argument_list; 134, if_statement; 134, 135; 134, 140; 135, comparison_operator:==; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:arg; 138, integer:1; 139, string:'l'; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:user; 144, subscript; 144, 145; 144, 146; 145, identifier:value; 146, integer:1; 147, continue_statement; 148, comment; 149, if_statement; 149, 150; 149, 153; 149, 161; 150, comparison_operator:is; 150, 151; 150, 152; 151, identifier:sid_index; 152, None; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:parser; 158, identifier:parse_args; 159, argument_list; 159, 160; 160, list:[]; 161, else_clause; 161, 162; 162, block; 162, 163; 162, 169; 162, 186; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:sid; 166, subscript; 166, 167; 166, 168; 167, identifier:argv; 168, identifier:sid_index; 169, if_statement; 169, 170; 169, 173; 170, comparison_operator:in; 170, 171; 170, 172; 171, string:'@'; 172, identifier:sid; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 179; 176, pattern_list; 176, 177; 176, 178; 177, identifier:user; 178, identifier:sid; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:sid; 182, identifier:split; 183, argument_list; 183, 184; 183, 185; 184, string:'@'; 185, integer:1; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:parser; 190, identifier:parse_args; 191, argument_list; 191, 192; 192, list:[sid]; 192, 193; 193, identifier:sid; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:instance; 197, subscript; 197, 198; 197, 199; 198, identifier:instances; 199, identifier:sid; 200, if_statement; 200, 201; 200, 204; 201, comparison_operator:is; 201, 202; 201, 203; 202, identifier:user; 203, None; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:user; 208, call; 208, 209; 208, 214; 209, attribute; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:instance; 212, identifier:config; 213, identifier:get; 214, argument_list; 214, 215; 215, string:'user'; 216, try_statement; 216, 217; 216, 229; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:ssh_info; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:instance; 224, identifier:init_ssh_key; 225, argument_list; 225, 226; 226, keyword_argument; 226, 227; 226, 228; 227, identifier:user; 228, identifier:user; 229, except_clause; 229, 230; 229, 242; 230, as_pattern; 230, 231; 230, 240; 231, tuple; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:instance; 235, identifier:paramiko; 236, identifier:SSHException; 237, attribute; 237, 238; 237, 239; 238, identifier:socket; 239, identifier:error; 240, as_pattern_target; 240, 241; 241, identifier:e; 242, block; 242, 243; 242, 250; 242, 260; 242, 267; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:log; 247, identifier:error; 248, argument_list; 248, 249; 249, string:"Couldn't validate fingerprint for ssh connection."; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:log; 254, identifier:error; 255, argument_list; 255, 256; 256, call; 256, 257; 256, 258; 257, identifier:unicode; 258, argument_list; 258, 259; 259, identifier:e; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:log; 264, identifier:error; 265, argument_list; 265, 266; 266, string:"Is the instance finished starting up?"; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:sys; 271, identifier:exit; 272, argument_list; 272, 273; 273, integer:1; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:client; 277, subscript; 277, 278; 277, 279; 278, identifier:ssh_info; 279, string:'client'; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 291; 282, attribute; 282, 283; 282, 290; 283, attribute; 283, 284; 283, 289; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:client; 287, identifier:get_transport; 288, argument_list; 289, identifier:sock; 290, identifier:close; 291, argument_list; 292, expression_statement; 292, 293; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:client; 296, identifier:close; 297, argument_list; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 308; 300, subscript; 300, 301; 300, 302; 301, identifier:argv; 302, slice; 302, 303; 302, 304; 302, 305; 303, identifier:sid_index; 304, colon; 305, binary_operator:+; 305, 306; 305, 307; 306, identifier:sid_index; 307, integer:1; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:instance; 311, identifier:ssh_args_from_info; 312, argument_list; 312, 313; 313, identifier:ssh_info; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 322; 316, subscript; 316, 317; 316, 318; 317, identifier:argv; 318, slice; 318, 319; 318, 320; 318, 321; 319, integer:0; 320, colon; 321, integer:0; 322, list:['ssh']; 322, 323; 323, string:'ssh'; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:os; 328, identifier:execvp; 329, argument_list; 329, 330; 329, 331; 330, string:'ssh'; 331, identifier:argv
def cmd_ssh(self, argv, help): """Log into the instance with ssh using the automatically generated known hosts""" parser = argparse.ArgumentParser( prog="%s ssh" % self.progname, description=help, ) instances = self.get_instances(command='init_ssh_key') parser.add_argument("instance", nargs=1, metavar="instance", help="Name of the instance from the config.", choices=sorted(instances)) parser.add_argument("...", nargs=argparse.REMAINDER, help="ssh options") iargs = enumerate(argv) sid_index = None user = None for i, arg in iargs: if not arg.startswith('-'): sid_index = i break if arg[1] in '1246AaCfgKkMNnqsTtVvXxYy': continue elif arg[1] in 'bcDeFiLlmOopRSw': value = iargs.next() if arg[1] == 'l': user = value[1] continue # fake parsing for nice error messages if sid_index is None: parser.parse_args([]) else: sid = argv[sid_index] if '@' in sid: user, sid = sid.split('@', 1) parser.parse_args([sid]) instance = instances[sid] if user is None: user = instance.config.get('user') try: ssh_info = instance.init_ssh_key(user=user) except (instance.paramiko.SSHException, socket.error) as e: log.error("Couldn't validate fingerprint for ssh connection.") log.error(unicode(e)) log.error("Is the instance finished starting up?") sys.exit(1) client = ssh_info['client'] client.get_transport().sock.close() client.close() argv[sid_index:sid_index + 1] = instance.ssh_args_from_info(ssh_info) argv[0:0] = ['ssh'] os.execvp('ssh', argv)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 1, 9; 2, function_name:add_alignment; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:ref_seq; 6, identifier:annotation; 7, type; 7, 8; 8, identifier:Annotation; 9, block; 9, 10; 9, 12; 9, 19; 9, 23; 9, 36; 9, 54; 9, 427; 9, 433; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:seq_features; 15, call; 15, 16; 15, 17; 16, identifier:get_seqs; 17, argument_list; 17, 18; 18, identifier:ref_seq; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:annoated_align; 22, dictionary; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:allele; 26, subscript; 26, 27; 26, 35; 27, call; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:ref_seq; 31, identifier:description; 32, identifier:split; 33, argument_list; 33, 34; 34, string:","; 35, integer:0; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:locus; 39, subscript; 39, 40; 39, 53; 40, call; 40, 41; 40, 51; 41, attribute; 41, 42; 41, 50; 42, subscript; 42, 43; 42, 49; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:allele; 46, identifier:split; 47, argument_list; 47, 48; 48, string:"*"; 49, integer:0; 50, identifier:split; 51, argument_list; 51, 52; 52, string:"-"; 53, integer:1; 54, for_statement; 54, 55; 54, 56; 54, 57; 55, identifier:feat; 56, identifier:seq_features; 57, block; 57, 58; 58, if_statement; 58, 59; 58, 64; 58, 394; 59, comparison_operator:in; 59, 60; 59, 61; 60, identifier:feat; 61, attribute; 61, 62; 61, 63; 62, identifier:annotation; 63, identifier:annotation; 64, block; 64, 65; 64, 126; 65, if_statement; 65, 66; 65, 75; 65, 99; 66, call; 66, 67; 66, 68; 67, identifier:isinstance; 68, argument_list; 68, 69; 68, 74; 69, subscript; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:annotation; 72, identifier:annotation; 73, identifier:feat; 74, identifier:DBSeq; 75, block; 75, 76; 75, 90; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:seq_len; 79, call; 79, 80; 79, 81; 80, identifier:len; 81, argument_list; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:str; 84, argument_list; 84, 85; 85, subscript; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:annotation; 88, identifier:annotation; 89, identifier:feat; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:ref_len; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, subscript; 96, 97; 96, 98; 97, identifier:seq_features; 98, identifier:feat; 99, else_clause; 99, 100; 100, block; 100, 101; 100, 117; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:seq_len; 104, call; 104, 105; 104, 106; 105, identifier:len; 106, argument_list; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:str; 109, argument_list; 109, 110; 110, attribute; 110, 111; 110, 116; 111, subscript; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:annotation; 114, identifier:annotation; 115, identifier:feat; 116, identifier:seq; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:ref_len; 120, call; 120, 121; 120, 122; 121, identifier:len; 122, argument_list; 122, 123; 123, subscript; 123, 124; 123, 125; 124, identifier:seq_features; 125, identifier:feat; 126, if_statement; 126, 127; 126, 130; 126, 283; 127, comparison_operator:==; 127, 128; 127, 129; 128, identifier:seq_len; 129, identifier:ref_len; 130, block; 130, 131; 130, 144; 130, 160; 130, 235; 130, 264; 130, 273; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:seq; 134, call; 134, 135; 134, 136; 135, identifier:list; 136, argument_list; 136, 137; 137, attribute; 137, 138; 137, 143; 138, subscript; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:annotation; 141, identifier:annotation; 142, identifier:feat; 143, identifier:seq; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:gaps; 147, subscript; 147, 148; 147, 159; 148, subscript; 148, 149; 148, 158; 149, subscript; 149, 150; 149, 157; 150, subscript; 150, 151; 150, 156; 151, attribute; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:refdata; 155, identifier:annoated_alignments; 156, identifier:locus; 157, identifier:allele; 158, identifier:feat; 159, string:'Gaps'; 160, if_statement; 160, 161; 160, 170; 161, boolean_operator:and; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:verbose; 165, comparison_operator:>; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:verbosity; 169, integer:0; 170, block; 170, 171; 170, 186; 170, 201; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:logger; 177, identifier:info; 178, argument_list; 178, 179; 179, binary_operator:+; 179, 180; 179, 185; 180, binary_operator:+; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:logname; 184, string:" Lengths match for "; 185, identifier:feat; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 193; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:logger; 192, identifier:info; 193, argument_list; 193, 194; 194, binary_operator:+; 194, 195; 194, 200; 195, binary_operator:+; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:logname; 199, string:" Gaps at "; 200, identifier:feat; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:logger; 207, identifier:info; 208, argument_list; 208, 209; 209, binary_operator:+; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:self; 212, identifier:logname; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, string:"-"; 216, identifier:join; 217, argument_list; 217, 218; 218, list_comprehension; 218, 219; 218, 232; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, string:","; 222, identifier:join; 223, argument_list; 223, 224; 224, list_comprehension; 224, 225; 224, 229; 225, call; 225, 226; 225, 227; 226, identifier:str; 227, argument_list; 227, 228; 228, identifier:s; 229, for_in_clause; 229, 230; 229, 231; 230, identifier:s; 231, identifier:g; 232, for_in_clause; 232, 233; 232, 234; 233, identifier:g; 234, identifier:gaps; 235, for_statement; 235, 236; 235, 237; 235, 245; 236, identifier:i; 237, call; 237, 238; 237, 239; 238, identifier:range; 239, argument_list; 239, 240; 239, 241; 240, integer:0; 241, call; 241, 242; 241, 243; 242, identifier:len; 243, argument_list; 243, 244; 244, identifier:gaps; 245, block; 245, 246; 246, for_statement; 246, 247; 246, 248; 246, 251; 247, identifier:j; 248, subscript; 248, 249; 248, 250; 249, identifier:gaps; 250, identifier:i; 251, block; 251, 252; 251, 256; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:loc; 255, identifier:j; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:seq; 260, identifier:insert; 261, argument_list; 261, 262; 261, 263; 262, identifier:loc; 263, string:'-'; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:nseq; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, string:''; 270, identifier:join; 271, argument_list; 271, 272; 272, identifier:seq; 273, expression_statement; 273, 274; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:annoated_align; 277, identifier:update; 278, argument_list; 278, 279; 279, dictionary; 279, 280; 280, pair; 280, 281; 280, 282; 281, identifier:feat; 282, identifier:nseq; 283, else_clause; 283, 284; 284, block; 284, 285; 284, 298; 284, 314; 284, 326; 284, 380; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:in_seq; 288, call; 288, 289; 288, 290; 289, identifier:str; 290, argument_list; 290, 291; 291, attribute; 291, 292; 291, 297; 292, subscript; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:annotation; 295, identifier:annotation; 296, identifier:feat; 297, identifier:seq; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:ref_seq; 301, subscript; 301, 302; 301, 313; 302, subscript; 302, 303; 302, 312; 303, subscript; 303, 304; 303, 311; 304, subscript; 304, 305; 304, 310; 305, attribute; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:self; 308, identifier:refdata; 309, identifier:annoated_alignments; 310, identifier:locus; 311, identifier:allele; 312, identifier:feat; 313, string:'Seq'; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:alignment; 317, call; 317, 318; 317, 323; 318, attribute; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:pairwise2; 321, identifier:align; 322, identifier:globalxx; 323, argument_list; 323, 324; 323, 325; 324, identifier:in_seq; 325, identifier:ref_seq; 326, if_statement; 326, 327; 326, 336; 327, boolean_operator:and; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:self; 330, identifier:verbose; 331, comparison_operator:>; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:self; 334, identifier:verbosity; 335, integer:0; 336, block; 336, 337; 336, 352; 337, expression_statement; 337, 338; 338, call; 338, 339; 338, 344; 339, attribute; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:self; 342, identifier:logger; 343, identifier:info; 344, argument_list; 344, 345; 345, binary_operator:+; 345, 346; 345, 351; 346, binary_operator:+; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:self; 349, identifier:logname; 350, string:" Align2 -> in_seq != ref_len "; 351, identifier:feat; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 359; 354, attribute; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:self; 357, identifier:logger; 358, identifier:info; 359, argument_list; 359, 360; 360, binary_operator:+; 360, 361; 360, 376; 361, binary_operator:+; 361, 362; 361, 375; 362, binary_operator:+; 362, 363; 362, 368; 363, binary_operator:+; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:self; 366, identifier:logname; 367, string:" "; 368, call; 368, 369; 368, 370; 369, identifier:str; 370, argument_list; 370, 371; 371, call; 371, 372; 371, 373; 372, identifier:len; 373, argument_list; 373, 374; 374, identifier:in_seq; 375, string:" == "; 376, call; 376, 377; 376, 378; 377, identifier:str; 378, argument_list; 378, 379; 379, identifier:ref_len; 380, expression_statement; 380, 381; 381, call; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, identifier:annoated_align; 384, identifier:update; 385, argument_list; 385, 386; 386, dictionary; 386, 387; 387, pair; 387, 388; 387, 389; 388, identifier:feat; 389, subscript; 389, 390; 389, 393; 390, subscript; 390, 391; 390, 392; 391, identifier:alignment; 392, integer:0; 393, integer:0; 394, else_clause; 394, 395; 395, block; 395, 396; 395, 417; 396, expression_statement; 396, 397; 397, assignment; 397, 398; 397, 399; 398, identifier:nseq; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, string:''; 402, identifier:join; 403, argument_list; 403, 404; 404, call; 404, 405; 404, 406; 405, identifier:list; 406, argument_list; 406, 407; 407, call; 407, 408; 407, 409; 408, identifier:repeat; 409, argument_list; 409, 410; 409, 411; 410, string:'-'; 411, call; 411, 412; 411, 413; 412, identifier:len; 413, argument_list; 413, 414; 414, subscript; 414, 415; 414, 416; 415, identifier:seq_features; 416, identifier:feat; 417, expression_statement; 417, 418; 418, call; 418, 419; 418, 422; 419, attribute; 419, 420; 419, 421; 420, identifier:annoated_align; 421, identifier:update; 422, argument_list; 422, 423; 423, dictionary; 423, 424; 424, pair; 424, 425; 424, 426; 425, identifier:feat; 426, identifier:nseq; 427, expression_statement; 427, 428; 428, assignment; 428, 429; 428, 432; 429, attribute; 429, 430; 429, 431; 430, identifier:annotation; 431, identifier:aligned; 432, identifier:annoated_align; 433, return_statement; 433, 434; 434, identifier:annotation
def add_alignment(self, ref_seq, annotation) -> Annotation: """ add_alignment - method for adding the alignment to an annotation :param ref_seq: List of reference sequences :type ref_seq: List :param annotation: The complete annotation :type annotation: Annotation :rtype: Annotation """ seq_features = get_seqs(ref_seq) annoated_align = {} allele = ref_seq.description.split(",")[0] locus = allele.split("*")[0].split("-")[1] for feat in seq_features: if feat in annotation.annotation: if isinstance(annotation.annotation[feat], DBSeq): seq_len = len(str(annotation.annotation[feat])) ref_len = len(seq_features[feat]) else: seq_len = len(str(annotation.annotation[feat].seq)) ref_len = len(seq_features[feat]) if seq_len == ref_len: seq = list(annotation.annotation[feat].seq) gaps = self.refdata.annoated_alignments[locus][allele][feat]['Gaps'] if self.verbose and self.verbosity > 0: self.logger.info(self.logname + " Lengths match for " + feat) self.logger.info(self.logname + " Gaps at " + feat) self.logger.info(self.logname + "-".join([",".join([str(s) for s in g]) for g in gaps])) for i in range(0, len(gaps)): for j in gaps[i]: loc = j seq.insert(loc, '-') nseq = ''.join(seq) annoated_align.update({feat: nseq}) else: in_seq = str(annotation.annotation[feat].seq) ref_seq = self.refdata.annoated_alignments[locus][allele][feat]['Seq'] alignment = pairwise2.align.globalxx(in_seq, ref_seq) if self.verbose and self.verbosity > 0: self.logger.info(self.logname + " Align2 -> in_seq != ref_len " + feat) self.logger.info(self.logname + " " + str(len(in_seq)) + " == " + str(ref_len)) annoated_align.update({feat: alignment[0][0]}) else: nseq = ''.join(list(repeat('-', len(seq_features[feat])))) annoated_align.update({feat: nseq}) annotation.aligned = annoated_align return annotation
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:build_tree; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:data; 6, identifier:tagname; 7, default_parameter; 7, 8; 7, 9; 8, identifier:attrs; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:depth; 12, integer:0; 13, block; 13, 14; 13, 16; 13, 25; 13, 46; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:data; 19, None; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:data; 24, string:''; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:indent; 28, conditional_expression:if; 28, 29; 28, 40; 28, 45; 29, parenthesized_expression; 29, 30; 30, binary_operator:%; 30, 31; 30, 32; 31, string:'\n%s'; 32, parenthesized_expression; 32, 33; 33, binary_operator:*; 33, 34; 33, 39; 34, subscript; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:__options; 38, string:'indent'; 39, identifier:depth; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:__options; 44, string:'indent'; 45, string:''; 46, if_statement; 46, 47; 46, 54; 46, 229; 46, 251; 47, call; 47, 48; 47, 49; 48, identifier:isinstance; 49, argument_list; 49, 50; 49, 51; 50, identifier:data; 51, attribute; 51, 52; 51, 53; 52, identifier:utils; 53, identifier:DictTypes; 54, block; 54, 55; 55, if_statement; 55, 56; 55, 72; 55, 94; 56, boolean_operator:and; 56, 57; 56, 62; 57, subscript; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:__options; 61, string:'hasattr'; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:check_structure; 66, argument_list; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:data; 70, identifier:keys; 71, argument_list; 72, block; 72, 73; 72, 84; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, pattern_list; 75, 76; 75, 77; 76, identifier:attrs; 77, identifier:values; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:pickdata; 82, argument_list; 82, 83; 83, identifier:data; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:build_tree; 89, argument_list; 89, 90; 89, 91; 89, 92; 89, 93; 90, identifier:values; 91, identifier:tagname; 92, identifier:attrs; 93, identifier:depth; 94, else_clause; 94, 95; 95, block; 95, 96; 95, 115; 95, 123; 95, 152; 95, 211; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:__tree; 102, identifier:append; 103, argument_list; 103, 104; 104, binary_operator:%; 104, 105; 104, 106; 105, string:'%s%s'; 106, tuple; 106, 107; 106, 108; 107, identifier:indent; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:tag_start; 112, argument_list; 112, 113; 112, 114; 113, identifier:tagname; 114, identifier:attrs; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:iter; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:data; 121, identifier:iteritems; 122, argument_list; 123, if_statement; 123, 124; 123, 129; 124, subscript; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:__options; 128, string:'ksort'; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:iter; 133, call; 133, 134; 133, 135; 134, identifier:sorted; 135, argument_list; 135, 136; 135, 137; 135, 145; 136, identifier:iter; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:key; 139, lambda; 139, 140; 139, 142; 140, lambda_parameters; 140, 141; 141, identifier:x; 142, subscript; 142, 143; 142, 144; 143, identifier:x; 144, integer:0; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:reverse; 147, subscript; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:__options; 151, string:'reverse'; 152, for_statement; 152, 153; 152, 156; 152, 157; 153, pattern_list; 153, 154; 153, 155; 154, identifier:k; 155, identifier:v; 156, identifier:iter; 157, block; 157, 158; 157, 162; 157, 199; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:attrs; 161, dictionary; 162, if_statement; 162, 163; 162, 187; 163, boolean_operator:and; 163, 164; 163, 177; 164, boolean_operator:and; 164, 165; 164, 170; 165, subscript; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:__options; 169, string:'hasattr'; 170, call; 170, 171; 170, 172; 171, identifier:isinstance; 172, argument_list; 172, 173; 172, 174; 173, identifier:v; 174, attribute; 174, 175; 174, 176; 175, identifier:utils; 176, identifier:DictTypes; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:check_structure; 181, argument_list; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:v; 185, identifier:keys; 186, argument_list; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, pattern_list; 190, 191; 190, 192; 191, identifier:attrs; 192, identifier:v; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:pickdata; 197, argument_list; 197, 198; 198, identifier:v; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:build_tree; 204, argument_list; 204, 205; 204, 206; 204, 207; 204, 208; 205, identifier:v; 206, identifier:k; 207, identifier:attrs; 208, binary_operator:+; 208, 209; 208, 210; 209, identifier:depth; 210, integer:1; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 218; 213, attribute; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:__tree; 217, identifier:append; 218, argument_list; 218, 219; 219, binary_operator:%; 219, 220; 219, 221; 220, string:'%s%s'; 221, tuple; 221, 222; 221, 223; 222, identifier:indent; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:self; 226, identifier:tag_end; 227, argument_list; 227, 228; 228, identifier:tagname; 229, elif_clause; 229, 230; 229, 236; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:utils; 233, identifier:is_iterable; 234, argument_list; 234, 235; 235, identifier:data; 236, block; 236, 237; 237, for_statement; 237, 238; 237, 239; 237, 240; 238, identifier:v; 239, identifier:data; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:build_tree; 246, argument_list; 246, 247; 246, 248; 246, 249; 246, 250; 247, identifier:v; 248, identifier:tagname; 249, identifier:attrs; 250, identifier:depth; 251, else_clause; 251, 252; 252, block; 252, 253; 252, 262; 252, 276; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 260; 255, attribute; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:__tree; 259, identifier:append; 260, argument_list; 260, 261; 261, identifier:indent; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:data; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:self; 268, identifier:safedata; 269, argument_list; 269, 270; 269, 271; 270, identifier:data; 271, subscript; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:__options; 275, string:'cdata'; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 283; 278, attribute; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:self; 281, identifier:__tree; 282, identifier:append; 283, argument_list; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:build_tag; 288, argument_list; 288, 289; 288, 290; 288, 291; 289, identifier:tagname; 290, identifier:data; 291, identifier:attrs
def build_tree(self, data, tagname, attrs=None, depth=0): r"""Build xml tree. :param data: data for build xml. :param tagname: element tag name. :param attrs: element attributes. Default:``None``. :type attrs: dict or None :param depth: element depth of the hierarchy. Default:``0``. :type depth: int """ if data is None: data = '' indent = ('\n%s' % (self.__options['indent'] * depth)) if self.__options['indent'] else '' if isinstance(data, utils.DictTypes): if self.__options['hasattr'] and self.check_structure(data.keys()): attrs, values = self.pickdata(data) self.build_tree(values, tagname, attrs, depth) else: self.__tree.append('%s%s' % (indent, self.tag_start(tagname, attrs))) iter = data.iteritems() if self.__options['ksort']: iter = sorted(iter, key=lambda x:x[0], reverse=self.__options['reverse']) for k, v in iter: attrs = {} if self.__options['hasattr'] and isinstance(v, utils.DictTypes) and self.check_structure(v.keys()): attrs, v = self.pickdata(v) self.build_tree(v, k, attrs, depth+1) self.__tree.append('%s%s' % (indent, self.tag_end(tagname))) elif utils.is_iterable(data): for v in data: self.build_tree(v, tagname, attrs, depth) else: self.__tree.append(indent) data = self.safedata(data, self.__options['cdata']) self.__tree.append(self.build_tag(tagname, data, attrs))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_parse_remind; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:filename; 6, default_parameter; 6, 7; 6, 8; 7, identifier:lines; 8, string:''; 9, block; 9, 10; 9, 12; 9, 16; 9, 20; 9, 39; 9, 60; 9, 109; 9, 117; 9, 283; 9, 284; 9, 350; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:files; 15, dictionary; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:reminders; 19, dictionary; 20, if_statement; 20, 21; 20, 22; 21, identifier:lines; 22, block; 22, 23; 22, 27; 22, 33; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:filename; 26, string:'-'; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, subscript; 29, 30; 29, 31; 30, identifier:files; 31, identifier:filename; 32, identifier:lines; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, subscript; 35, 36; 35, 37; 36, identifier:reminders; 37, identifier:filename; 38, dictionary; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:cmd; 42, list:['remind', '-l', '-s%d' % self._month, '-b1', '-y', '-r', filename, str(self._startdate)]; 42, 43; 42, 44; 42, 45; 42, 50; 42, 51; 42, 52; 42, 53; 42, 54; 43, string:'remind'; 44, string:'-l'; 45, binary_operator:%; 45, 46; 45, 47; 46, string:'-s%d'; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:_month; 50, string:'-b1'; 51, string:'-y'; 52, string:'-r'; 53, identifier:filename; 54, call; 54, 55; 54, 56; 55, identifier:str; 56, argument_list; 56, 57; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:_startdate; 60, try_statement; 60, 61; 60, 94; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:rem; 65, call; 65, 66; 65, 92; 66, attribute; 66, 67; 66, 91; 67, subscript; 67, 68; 67, 90; 68, call; 68, 69; 68, 81; 69, attribute; 69, 70; 69, 80; 70, call; 70, 71; 70, 72; 71, identifier:Popen; 72, argument_list; 72, 73; 72, 74; 72, 77; 73, identifier:cmd; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:stdin; 76, identifier:PIPE; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:stdout; 79, identifier:PIPE; 80, identifier:communicate; 81, argument_list; 81, 82; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:input; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:lines; 87, identifier:encode; 88, argument_list; 88, 89; 89, string:'utf-8'; 90, integer:0; 91, identifier:decode; 92, argument_list; 92, 93; 93, string:'utf-8'; 94, except_clause; 94, 95; 94, 96; 95, identifier:OSError; 96, block; 96, 97; 97, raise_statement; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:OSError; 100, argument_list; 100, 101; 101, binary_operator:%; 101, 102; 101, 103; 102, string:'Error running: %s'; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, string:' '; 106, identifier:join; 107, argument_list; 107, 108; 108, identifier:cmd; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:rem; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:rem; 115, identifier:splitlines; 116, argument_list; 117, for_statement; 117, 118; 117, 121; 117, 137; 118, tuple_pattern; 118, 119; 118, 120; 119, identifier:fileinfo; 120, identifier:line; 121, call; 121, 122; 121, 123; 122, identifier:zip; 123, argument_list; 123, 124; 123, 130; 124, subscript; 124, 125; 124, 126; 125, identifier:rem; 126, slice; 126, 127; 126, 128; 126, 129; 127, colon; 128, colon; 129, integer:2; 130, subscript; 130, 131; 130, 132; 131, identifier:rem; 132, slice; 132, 133; 132, 134; 132, 135; 132, 136; 133, integer:1; 134, colon; 135, colon; 136, integer:2; 137, block; 137, 138; 137, 146; 137, 152; 137, 199; 137, 214; 137, 224; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:fileinfo; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:fileinfo; 144, identifier:split; 145, argument_list; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:src_filename; 149, subscript; 149, 150; 149, 151; 150, identifier:fileinfo; 151, integer:3; 152, if_statement; 152, 153; 152, 156; 152, 157; 152, 158; 152, 159; 153, comparison_operator:not; 153, 154; 153, 155; 154, identifier:src_filename; 155, identifier:files; 156, comment; 157, comment; 158, comment; 159, block; 159, 160; 159, 173; 159, 179; 159, 186; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:files; 164, identifier:src_filename; 165, call; 165, 166; 165, 172; 166, attribute; 166, 167; 166, 171; 167, call; 167, 168; 167, 169; 168, identifier:open; 169, argument_list; 169, 170; 170, identifier:src_filename; 171, identifier:readlines; 172, argument_list; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:reminders; 177, identifier:src_filename; 178, dictionary; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:mtime; 182, call; 182, 183; 182, 184; 183, identifier:getmtime; 184, argument_list; 184, 185; 185, identifier:src_filename; 186, if_statement; 186, 187; 186, 192; 187, comparison_operator:>; 187, 188; 187, 189; 188, identifier:mtime; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:_mtime; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:_mtime; 198, identifier:mtime; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:text; 202, subscript; 202, 203; 202, 206; 203, subscript; 203, 204; 203, 205; 204, identifier:files; 205, identifier:src_filename; 206, binary_operator:-; 206, 207; 206, 213; 207, call; 207, 208; 207, 209; 208, identifier:int; 209, argument_list; 209, 210; 210, subscript; 210, 211; 210, 212; 211, identifier:fileinfo; 212, integer:2; 213, integer:1; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:event; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:_parse_remind_line; 221, argument_list; 221, 222; 221, 223; 222, identifier:line; 223, identifier:text; 224, if_statement; 224, 225; 224, 232; 224, 259; 225, comparison_operator:in; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:event; 228, string:'uid'; 229, subscript; 229, 230; 229, 231; 230, identifier:reminders; 231, identifier:src_filename; 232, block; 232, 233; 232, 247; 233, expression_statement; 233, 234; 234, augmented_assignment:+=; 234, 235; 234, 244; 235, subscript; 235, 236; 235, 243; 236, subscript; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:reminders; 239, identifier:src_filename; 240, subscript; 240, 241; 240, 242; 241, identifier:event; 242, string:'uid'; 243, string:'dtstart'; 244, subscript; 244, 245; 244, 246; 245, identifier:event; 246, string:'dtstart'; 247, expression_statement; 247, 248; 248, augmented_assignment:+=; 248, 249; 248, 258; 249, subscript; 249, 250; 249, 257; 250, subscript; 250, 251; 250, 254; 251, subscript; 251, 252; 251, 253; 252, identifier:reminders; 253, identifier:src_filename; 254, subscript; 254, 255; 254, 256; 255, identifier:event; 256, string:'uid'; 257, string:'line'; 258, identifier:line; 259, else_clause; 259, 260; 260, block; 260, 261; 260, 271; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 270; 263, subscript; 263, 264; 263, 267; 264, subscript; 264, 265; 264, 266; 265, identifier:reminders; 266, identifier:src_filename; 267, subscript; 267, 268; 267, 269; 268, identifier:event; 269, string:'uid'; 270, identifier:event; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 282; 273, subscript; 273, 274; 273, 281; 274, subscript; 274, 275; 274, 278; 275, subscript; 275, 276; 275, 277; 276, identifier:reminders; 277, identifier:src_filename; 278, subscript; 278, 279; 278, 280; 279, identifier:event; 280, string:'uid'; 281, string:'line'; 282, identifier:line; 283, comment; 284, for_statement; 284, 285; 284, 286; 284, 291; 285, identifier:source; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:files; 289, identifier:values; 290, argument_list; 291, block; 291, 292; 292, for_statement; 292, 293; 292, 294; 292, 295; 293, identifier:line; 294, identifier:source; 295, block; 295, 296; 296, if_statement; 296, 297; 296, 303; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:line; 300, identifier:startswith; 301, argument_list; 301, 302; 302, string:'include'; 303, block; 303, 304; 303, 319; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:new_file; 307, call; 307, 308; 307, 318; 308, attribute; 308, 309; 308, 317; 309, subscript; 309, 310; 309, 316; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:line; 313, identifier:split; 314, argument_list; 314, 315; 315, string:' '; 316, integer:1; 317, identifier:strip; 318, argument_list; 319, if_statement; 319, 320; 319, 323; 320, comparison_operator:not; 320, 321; 320, 322; 321, identifier:new_file; 322, identifier:reminders; 323, block; 323, 324; 323, 330; 323, 337; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 329; 326, subscript; 326, 327; 326, 328; 327, identifier:reminders; 328, identifier:new_file; 329, dictionary; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:mtime; 333, call; 333, 334; 333, 335; 334, identifier:getmtime; 335, argument_list; 335, 336; 336, identifier:new_file; 337, if_statement; 337, 338; 337, 343; 338, comparison_operator:>; 338, 339; 338, 340; 339, identifier:mtime; 340, attribute; 340, 341; 340, 342; 341, identifier:self; 342, identifier:_mtime; 343, block; 343, 344; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:self; 348, identifier:_mtime; 349, identifier:mtime; 350, return_statement; 350, 351; 351, identifier:reminders
def _parse_remind(self, filename, lines=''): """Calls remind and parses the output into a dict filename -- the remind file (included files will be used as well) lines -- used as stdin to remind (filename will be set to -) """ files = {} reminders = {} if lines: filename = '-' files[filename] = lines reminders[filename] = {} cmd = ['remind', '-l', '-s%d' % self._month, '-b1', '-y', '-r', filename, str(self._startdate)] try: rem = Popen(cmd, stdin=PIPE, stdout=PIPE).communicate(input=lines.encode('utf-8'))[0].decode('utf-8') except OSError: raise OSError('Error running: %s' % ' '.join(cmd)) rem = rem.splitlines() for (fileinfo, line) in zip(rem[::2], rem[1::2]): fileinfo = fileinfo.split() src_filename = fileinfo[3] if src_filename not in files: # There is a race condition with the remind call above here. # This could be solved by parsing the remind -de output, # but I don't see an easy way to do that. files[src_filename] = open(src_filename).readlines() reminders[src_filename] = {} mtime = getmtime(src_filename) if mtime > self._mtime: self._mtime = mtime text = files[src_filename][int(fileinfo[2]) - 1] event = self._parse_remind_line(line, text) if event['uid'] in reminders[src_filename]: reminders[src_filename][event['uid']]['dtstart'] += event['dtstart'] reminders[src_filename][event['uid']]['line'] += line else: reminders[src_filename][event['uid']] = event reminders[src_filename][event['uid']]['line'] = line # Find included files without reminders and add them to the file list for source in files.values(): for line in source: if line.startswith('include'): new_file = line.split(' ')[1].strip() if new_file not in reminders: reminders[new_file] = {} mtime = getmtime(new_file) if mtime > self._mtime: self._mtime = mtime return reminders
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_parse_remind_line; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:line; 6, identifier:text; 7, block; 7, 8; 7, 10; 7, 14; 7, 24; 7, 42; 7, 134; 7, 156; 7, 176; 7, 205; 7, 221; 7, 232; 7, 239; 7, 259; 7, 278; 7, 297; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:event; 13, dictionary; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:line; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:line; 20, identifier:split; 21, argument_list; 21, 22; 21, 23; 22, None; 23, integer:6; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:dat; 27, list_comprehension; 27, 28; 27, 32; 28, call; 28, 29; 28, 30; 29, identifier:int; 30, argument_list; 30, 31; 31, identifier:f; 32, for_in_clause; 32, 33; 32, 34; 33, identifier:f; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, subscript; 36, 37; 36, 38; 37, identifier:line; 38, integer:0; 39, identifier:split; 40, argument_list; 40, 41; 41, string:'/'; 42, if_statement; 42, 43; 42, 48; 42, 114; 43, comparison_operator:!=; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:line; 46, integer:4; 47, string:'*'; 48, block; 48, 49; 48, 62; 48, 91; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:start; 52, call; 52, 53; 52, 54; 53, identifier:divmod; 54, argument_list; 54, 55; 54, 61; 55, call; 55, 56; 55, 57; 56, identifier:int; 57, argument_list; 57, 58; 58, subscript; 58, 59; 58, 60; 59, identifier:line; 60, integer:4; 61, integer:60; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:event; 66, string:'dtstart'; 67, list:[datetime(dat[0], dat[1], dat[2], start[0], start[1], tzinfo=self._localtz)]; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:datetime; 70, argument_list; 70, 71; 70, 74; 70, 77; 70, 80; 70, 83; 70, 86; 71, subscript; 71, 72; 71, 73; 72, identifier:dat; 73, integer:0; 74, subscript; 74, 75; 74, 76; 75, identifier:dat; 76, integer:1; 77, subscript; 77, 78; 77, 79; 78, identifier:dat; 79, integer:2; 80, subscript; 80, 81; 80, 82; 81, identifier:start; 82, integer:0; 83, subscript; 83, 84; 83, 85; 84, identifier:start; 85, integer:1; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:tzinfo; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:_localtz; 91, if_statement; 91, 92; 91, 97; 92, comparison_operator:!=; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:line; 95, integer:3; 96, string:'*'; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:event; 102, string:'duration'; 103, call; 103, 104; 103, 105; 104, identifier:timedelta; 105, argument_list; 105, 106; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:minutes; 108, call; 108, 109; 108, 110; 109, identifier:int; 110, argument_list; 110, 111; 111, subscript; 111, 112; 111, 113; 112, identifier:line; 113, integer:3; 114, else_clause; 114, 115; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 121; 118, subscript; 118, 119; 118, 120; 119, identifier:event; 120, string:'dtstart'; 121, list:[date(dat[0], dat[1], dat[2])]; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:date; 124, argument_list; 124, 125; 124, 128; 124, 131; 125, subscript; 125, 126; 125, 127; 126, identifier:dat; 127, integer:0; 128, subscript; 128, 129; 128, 130; 129, identifier:dat; 130, integer:1; 131, subscript; 131, 132; 131, 133; 132, identifier:dat; 133, integer:2; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:msg; 137, conditional_expression:if; 137, 138; 137, 148; 137, 153; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, string:' '; 141, identifier:join; 142, argument_list; 142, 143; 143, subscript; 143, 144; 143, 145; 144, identifier:line; 145, slice; 145, 146; 145, 147; 146, integer:5; 147, colon; 148, comparison_operator:==; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:line; 151, integer:4; 152, string:'*'; 153, subscript; 153, 154; 153, 155; 154, identifier:line; 155, integer:6; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:msg; 159, call; 159, 160; 159, 173; 160, attribute; 160, 161; 160, 172; 161, call; 161, 162; 161, 169; 162, attribute; 162, 163; 162, 168; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:msg; 166, identifier:strip; 167, argument_list; 168, identifier:replace; 169, argument_list; 169, 170; 169, 171; 170, string:'%_'; 171, string:'\n'; 172, identifier:replace; 173, argument_list; 173, 174; 173, 175; 174, string:'["["]'; 175, string:'['; 176, if_statement; 176, 177; 176, 180; 176, 197; 177, comparison_operator:in; 177, 178; 177, 179; 178, string:' at '; 179, identifier:msg; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 190; 183, tuple_pattern; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:event; 186, string:'msg'; 187, subscript; 187, 188; 187, 189; 188, identifier:event; 189, string:'location'; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:msg; 193, identifier:rsplit; 194, argument_list; 194, 195; 194, 196; 195, string:' at '; 196, integer:1; 197, else_clause; 197, 198; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:event; 203, string:'msg'; 204, identifier:msg; 205, if_statement; 205, 206; 205, 209; 206, comparison_operator:in; 206, 207; 206, 208; 207, string:'%"'; 208, identifier:text; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, subscript; 212, 213; 212, 214; 213, identifier:event; 214, string:'description'; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:Remind; 218, identifier:_gen_description; 219, argument_list; 219, 220; 220, identifier:text; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:tags; 224, call; 224, 225; 224, 230; 225, attribute; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:line; 228, integer:2; 229, identifier:split; 230, argument_list; 230, 231; 231, string:','; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:classes; 235, list:['PUBLIC', 'PRIVATE', 'CONFIDENTIAL']; 235, 236; 235, 237; 235, 238; 236, string:'PUBLIC'; 237, string:'PRIVATE'; 238, string:'CONFIDENTIAL'; 239, for_statement; 239, 240; 239, 241; 239, 247; 240, identifier:tag; 241, subscript; 241, 242; 241, 243; 242, identifier:tags; 243, slice; 243, 244; 243, 245; 244, colon; 245, unary_operator:-; 245, 246; 246, integer:1; 247, block; 247, 248; 248, if_statement; 248, 249; 248, 252; 249, comparison_operator:in; 249, 250; 249, 251; 250, identifier:tag; 251, identifier:classes; 252, block; 252, 253; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 258; 255, subscript; 255, 256; 255, 257; 256, identifier:event; 257, string:'class'; 258, identifier:tag; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 264; 261, subscript; 261, 262; 261, 263; 262, identifier:event; 263, string:'categories'; 264, list_comprehension; 264, 265; 264, 266; 264, 274; 265, identifier:tag; 266, for_in_clause; 266, 267; 266, 268; 267, identifier:tag; 268, subscript; 268, 269; 268, 270; 269, identifier:tags; 270, slice; 270, 271; 270, 272; 271, colon; 272, unary_operator:-; 272, 273; 273, integer:1; 274, if_clause; 274, 275; 275, comparison_operator:not; 275, 276; 275, 277; 276, identifier:tag; 277, identifier:classes; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 283; 280, subscript; 280, 281; 280, 282; 281, identifier:event; 282, string:'uid'; 283, binary_operator:%; 283, 284; 283, 285; 284, string:'%s@%s'; 285, tuple; 285, 286; 285, 294; 286, subscript; 286, 287; 286, 291; 287, subscript; 287, 288; 287, 289; 288, identifier:tags; 289, unary_operator:-; 289, 290; 290, integer:1; 291, slice; 291, 292; 291, 293; 292, integer:7; 293, colon; 294, call; 294, 295; 294, 296; 295, identifier:getfqdn; 296, argument_list; 297, return_statement; 297, 298; 298, identifier:event
def _parse_remind_line(self, line, text): """Parse a line of remind output into a dict line -- the remind output text -- the original remind input """ event = {} line = line.split(None, 6) dat = [int(f) for f in line[0].split('/')] if line[4] != '*': start = divmod(int(line[4]), 60) event['dtstart'] = [datetime(dat[0], dat[1], dat[2], start[0], start[1], tzinfo=self._localtz)] if line[3] != '*': event['duration'] = timedelta(minutes=int(line[3])) else: event['dtstart'] = [date(dat[0], dat[1], dat[2])] msg = ' '.join(line[5:]) if line[4] == '*' else line[6] msg = msg.strip().replace('%_', '\n').replace('["["]', '[') if ' at ' in msg: (event['msg'], event['location']) = msg.rsplit(' at ', 1) else: event['msg'] = msg if '%"' in text: event['description'] = Remind._gen_description(text) tags = line[2].split(',') classes = ['PUBLIC', 'PRIVATE', 'CONFIDENTIAL'] for tag in tags[:-1]: if tag in classes: event['class'] = tag event['categories'] = [tag for tag in tags[:-1] if tag not in classes] event['uid'] = '%s@%s' % (tags[-1][7:], getfqdn()) return event
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_gen_vevent; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:event; 6, identifier:vevent; 7, block; 7, 8; 7, 10; 7, 25; 7, 43; 7, 56; 7, 69; 7, 87; 7, 114; 7, 132; 7, 150; 7, 283; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 20; 12, attribute; 12, 13; 12, 19; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:vevent; 16, identifier:add; 17, argument_list; 17, 18; 18, string:'dtstart'; 19, identifier:value; 20, subscript; 20, 21; 20, 24; 21, subscript; 21, 22; 21, 23; 22, identifier:event; 23, string:'dtstart'; 24, integer:0; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 35; 27, attribute; 27, 28; 27, 34; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:vevent; 31, identifier:add; 32, argument_list; 32, 33; 33, string:'dtstamp'; 34, identifier:value; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:datetime; 38, identifier:fromtimestamp; 39, argument_list; 39, 40; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_mtime; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 53; 45, attribute; 45, 46; 45, 52; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:vevent; 49, identifier:add; 50, argument_list; 50, 51; 51, string:'summary'; 52, identifier:value; 53, subscript; 53, 54; 53, 55; 54, identifier:event; 55, string:'msg'; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 66; 58, attribute; 58, 59; 58, 65; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:vevent; 62, identifier:add; 63, argument_list; 63, 64; 64, string:'uid'; 65, identifier:value; 66, subscript; 66, 67; 66, 68; 67, identifier:event; 68, string:'uid'; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:in; 70, 71; 70, 72; 71, string:'class'; 72, identifier:event; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 84; 76, attribute; 76, 77; 76, 83; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:vevent; 80, identifier:add; 81, argument_list; 81, 82; 82, string:'class'; 83, identifier:value; 84, subscript; 84, 85; 84, 86; 85, identifier:event; 86, string:'class'; 87, if_statement; 87, 88; 87, 100; 88, boolean_operator:and; 88, 89; 88, 92; 89, comparison_operator:in; 89, 90; 89, 91; 90, string:'categories'; 91, identifier:event; 92, comparison_operator:>; 92, 93; 92, 99; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, subscript; 96, 97; 96, 98; 97, identifier:event; 98, string:'categories'; 99, integer:0; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 111; 103, attribute; 103, 104; 103, 110; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:vevent; 107, identifier:add; 108, argument_list; 108, 109; 109, string:'categories'; 110, identifier:value; 111, subscript; 111, 112; 111, 113; 112, identifier:event; 113, string:'categories'; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:in; 115, 116; 115, 117; 116, string:'location'; 117, identifier:event; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 129; 121, attribute; 121, 122; 121, 128; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:vevent; 125, identifier:add; 126, argument_list; 126, 127; 127, string:'location'; 128, identifier:value; 129, subscript; 129, 130; 129, 131; 130, identifier:event; 131, string:'location'; 132, if_statement; 132, 133; 132, 136; 133, comparison_operator:in; 133, 134; 133, 135; 134, string:'description'; 135, identifier:event; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 147; 139, attribute; 139, 140; 139, 146; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:vevent; 143, identifier:add; 144, argument_list; 144, 145; 145, string:'description'; 146, identifier:value; 147, subscript; 147, 148; 147, 149; 148, identifier:event; 149, string:'description'; 150, if_statement; 150, 151; 150, 160; 150, 251; 151, call; 151, 152; 151, 153; 152, identifier:isinstance; 153, argument_list; 153, 154; 153, 159; 154, subscript; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:event; 157, string:'dtstart'; 158, integer:0; 159, identifier:datetime; 160, block; 160, 161; 160, 216; 161, if_statement; 161, 162; 161, 169; 162, comparison_operator:!=; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:_alarm; 166, call; 166, 167; 166, 168; 167, identifier:timedelta; 168, argument_list; 169, block; 169, 170; 169, 179; 169, 192; 169, 203; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:valarm; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:vevent; 176, identifier:add; 177, argument_list; 177, 178; 178, string:'valarm'; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 189; 181, attribute; 181, 182; 181, 188; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:valarm; 185, identifier:add; 186, argument_list; 186, 187; 187, string:'trigger'; 188, identifier:value; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:_alarm; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 202; 194, attribute; 194, 195; 194, 201; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:valarm; 198, identifier:add; 199, argument_list; 199, 200; 200, string:'action'; 201, identifier:value; 202, string:'DISPLAY'; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 213; 205, attribute; 205, 206; 205, 212; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:valarm; 209, identifier:add; 210, argument_list; 210, 211; 211, string:'description'; 212, identifier:value; 213, subscript; 213, 214; 213, 215; 214, identifier:event; 215, string:'msg'; 216, if_statement; 216, 217; 216, 220; 216, 234; 217, comparison_operator:in; 217, 218; 217, 219; 218, string:'duration'; 219, identifier:event; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 231; 223, attribute; 223, 224; 223, 230; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:vevent; 227, identifier:add; 228, argument_list; 228, 229; 229, string:'duration'; 230, identifier:value; 231, subscript; 231, 232; 231, 233; 232, identifier:event; 233, string:'duration'; 234, else_clause; 234, 235; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 246; 238, attribute; 238, 239; 238, 245; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:vevent; 242, identifier:add; 243, argument_list; 243, 244; 244, string:'dtend'; 245, identifier:value; 246, subscript; 246, 247; 246, 250; 247, subscript; 247, 248; 247, 249; 248, identifier:event; 249, string:'dtstart'; 250, integer:0; 251, elif_clause; 251, 252; 251, 260; 252, comparison_operator:==; 252, 253; 252, 259; 253, call; 253, 254; 253, 255; 254, identifier:len; 255, argument_list; 255, 256; 256, subscript; 256, 257; 256, 258; 257, identifier:event; 258, string:'dtstart'; 259, integer:1; 260, block; 260, 261; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 271; 263, attribute; 263, 264; 263, 270; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:vevent; 267, identifier:add; 268, argument_list; 268, 269; 269, string:'dtend'; 270, identifier:value; 271, binary_operator:+; 271, 272; 271, 277; 272, subscript; 272, 273; 272, 276; 273, subscript; 273, 274; 273, 275; 274, identifier:event; 275, string:'dtstart'; 276, integer:0; 277, call; 277, 278; 277, 279; 278, identifier:timedelta; 279, argument_list; 279, 280; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:days; 282, integer:1; 283, if_statement; 283, 284; 283, 292; 284, comparison_operator:>; 284, 285; 284, 291; 285, call; 285, 286; 285, 287; 286, identifier:len; 287, argument_list; 287, 288; 288, subscript; 288, 289; 288, 290; 289, identifier:event; 290, string:'dtstart'; 291, integer:1; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:Remind; 297, identifier:_gen_dtend_rrule; 298, argument_list; 298, 299; 298, 302; 299, subscript; 299, 300; 299, 301; 300, identifier:event; 301, string:'dtstart'; 302, identifier:vevent
def _gen_vevent(self, event, vevent): """Generate vevent from given event""" vevent.add('dtstart').value = event['dtstart'][0] vevent.add('dtstamp').value = datetime.fromtimestamp(self._mtime) vevent.add('summary').value = event['msg'] vevent.add('uid').value = event['uid'] if 'class' in event: vevent.add('class').value = event['class'] if 'categories' in event and len(event['categories']) > 0: vevent.add('categories').value = event['categories'] if 'location' in event: vevent.add('location').value = event['location'] if 'description' in event: vevent.add('description').value = event['description'] if isinstance(event['dtstart'][0], datetime): if self._alarm != timedelta(): valarm = vevent.add('valarm') valarm.add('trigger').value = self._alarm valarm.add('action').value = 'DISPLAY' valarm.add('description').value = event['msg'] if 'duration' in event: vevent.add('duration').value = event['duration'] else: vevent.add('dtend').value = event['dtstart'][0] elif len(event['dtstart']) == 1: vevent.add('dtend').value = event['dtstart'][0] + timedelta(days=1) if len(event['dtstart']) > 1: Remind._gen_dtend_rrule(event['dtstart'], vevent)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 27; 2, function_name:to_remind; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 4, identifier:self; 5, identifier:vevent; 6, default_parameter; 6, 7; 6, 8; 7, identifier:label; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:priority; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:tags; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:tail; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:sep; 20, string:" "; 21, default_parameter; 21, 22; 21, 23; 22, identifier:postdate; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:posttime; 26, None; 27, block; 27, 28; 27, 30; 27, 35; 27, 39; 27, 57; 27, 65; 27, 66; 27, 67; 27, 89; 27, 93; 27, 108; 27, 130; 27, 163; 27, 173; 27, 185; 27, 199; 27, 208; 27, 261; 27, 324; 27, 361; 27, 387; 27, 408; 27, 441; 27, 456; 28, expression_statement; 28, 29; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:remind; 33, list:['REM']; 33, 34; 34, string:'REM'; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:trigdates; 38, None; 39, if_statement; 39, 40; 39, 45; 40, call; 40, 41; 40, 42; 41, identifier:hasattr; 42, argument_list; 42, 43; 42, 44; 43, identifier:vevent; 44, string:'rrule'; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:trigdates; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:Remind; 52, identifier:_parse_rruleset; 53, argument_list; 53, 54; 54, attribute; 54, 55; 54, 56; 55, identifier:vevent; 56, identifier:rruleset; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:dtstart; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:vevent; 63, identifier:dtstart; 64, identifier:value; 65, comment; 66, comment; 67, if_statement; 67, 68; 67, 77; 68, boolean_operator:and; 68, 69; 68, 74; 69, call; 69, 70; 69, 71; 70, identifier:isinstance; 71, argument_list; 71, 72; 71, 73; 72, identifier:dtstart; 73, identifier:datetime; 74, attribute; 74, 75; 74, 76; 75, identifier:dtstart; 76, identifier:tzinfo; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:dtstart; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:dtstart; 84, identifier:astimezone; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:_localtz; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:dtend; 92, None; 93, if_statement; 93, 94; 93, 99; 94, call; 94, 95; 94, 96; 95, identifier:hasattr; 96, argument_list; 96, 97; 96, 98; 97, identifier:vevent; 98, string:'dtend'; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:dtend; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:vevent; 106, identifier:dtend; 107, identifier:value; 108, if_statement; 108, 109; 108, 118; 109, boolean_operator:and; 109, 110; 109, 115; 110, call; 110, 111; 110, 112; 111, identifier:isinstance; 112, argument_list; 112, 113; 112, 114; 113, identifier:dtend; 114, identifier:datetime; 115, attribute; 115, 116; 115, 117; 116, identifier:dtend; 117, identifier:tzinfo; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:dtend; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:dtend; 125, identifier:astimezone; 126, argument_list; 126, 127; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:_localtz; 130, if_statement; 130, 131; 130, 144; 131, boolean_operator:and; 131, 132; 131, 138; 132, not_operator; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:hasattr; 135, argument_list; 135, 136; 135, 137; 136, identifier:vevent; 137, string:'rdate'; 138, not_operator; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:isinstance; 141, argument_list; 141, 142; 141, 143; 142, identifier:trigdates; 143, identifier:str; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:remind; 149, identifier:append; 150, argument_list; 150, 151; 151, call; 151, 152; 151, 160; 152, attribute; 152, 153; 152, 159; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:dtstart; 156, identifier:strftime; 157, argument_list; 157, 158; 158, string:'%b %d %Y'; 159, identifier:replace; 160, argument_list; 160, 161; 160, 162; 161, string:' 0'; 162, string:' '; 163, if_statement; 163, 164; 163, 165; 164, identifier:postdate; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:remind; 170, identifier:append; 171, argument_list; 171, 172; 172, identifier:postdate; 173, if_statement; 173, 174; 173, 175; 174, identifier:priority; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:remind; 180, identifier:append; 181, argument_list; 181, 182; 182, binary_operator:%; 182, 183; 182, 184; 183, string:'PRIORITY %s'; 184, identifier:priority; 185, if_statement; 185, 186; 185, 191; 186, call; 186, 187; 186, 188; 187, identifier:isinstance; 188, argument_list; 188, 189; 188, 190; 189, identifier:trigdates; 190, identifier:list; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:remind; 196, identifier:extend; 197, argument_list; 197, 198; 198, identifier:trigdates; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:duration; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:Remind; 205, identifier:_event_duration; 206, argument_list; 206, 207; 207, identifier:vevent; 208, if_statement; 208, 209; 208, 221; 209, boolean_operator:and; 209, 210; 209, 216; 210, comparison_operator:is; 210, 211; 210, 215; 211, call; 211, 212; 211, 213; 212, identifier:type; 213, argument_list; 213, 214; 214, identifier:dtstart; 215, identifier:date; 216, comparison_operator:>; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:duration; 219, identifier:days; 220, integer:1; 221, block; 221, 222; 221, 229; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:remind; 226, identifier:append; 227, argument_list; 227, 228; 228, string:'*1'; 229, if_statement; 229, 230; 229, 233; 230, comparison_operator:is; 230, 231; 230, 232; 231, identifier:dtend; 232, None; 233, block; 233, 234; 233, 243; 234, expression_statement; 234, 235; 235, augmented_assignment:-=; 235, 236; 235, 237; 236, identifier:dtend; 237, call; 237, 238; 237, 239; 238, identifier:timedelta; 239, argument_list; 239, 240; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:days; 242, integer:1; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:remind; 247, identifier:append; 248, argument_list; 248, 249; 249, call; 249, 250; 249, 258; 250, attribute; 250, 251; 250, 257; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:dtend; 254, identifier:strftime; 255, argument_list; 255, 256; 256, string:'UNTIL %b %d %Y'; 257, identifier:replace; 258, argument_list; 258, 259; 258, 260; 259, string:' 0'; 260, string:' '; 261, if_statement; 261, 262; 261, 267; 262, call; 262, 263; 262, 264; 263, identifier:isinstance; 264, argument_list; 264, 265; 264, 266; 265, identifier:dtstart; 266, identifier:datetime; 267, block; 267, 268; 267, 286; 267, 296; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:remind; 272, identifier:append; 273, argument_list; 273, 274; 274, call; 274, 275; 274, 283; 275, attribute; 275, 276; 275, 282; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:dtstart; 279, identifier:strftime; 280, argument_list; 280, 281; 281, string:'AT %H:%M'; 282, identifier:replace; 283, argument_list; 283, 284; 283, 285; 284, string:' 0'; 285, string:' '; 286, if_statement; 286, 287; 286, 288; 287, identifier:posttime; 288, block; 288, 289; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:remind; 293, identifier:append; 294, argument_list; 294, 295; 295, identifier:posttime; 296, if_statement; 296, 297; 296, 304; 297, comparison_operator:>; 297, 298; 297, 303; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:duration; 301, identifier:total_seconds; 302, argument_list; 303, integer:0; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:remind; 309, identifier:append; 310, argument_list; 310, 311; 311, binary_operator:%; 311, 312; 311, 313; 312, string:'DURATION %d:%02d'; 313, call; 313, 314; 313, 315; 314, identifier:divmod; 315, argument_list; 315, 316; 315, 323; 316, binary_operator:/; 316, 317; 316, 322; 317, call; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:duration; 320, identifier:total_seconds; 321, argument_list; 322, integer:60; 323, integer:60; 324, if_statement; 324, 325; 324, 330; 324, 347; 325, call; 325, 326; 325, 327; 326, identifier:hasattr; 327, argument_list; 327, 328; 327, 329; 328, identifier:vevent; 329, string:'rdate'; 330, block; 330, 331; 331, expression_statement; 331, 332; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:remind; 335, identifier:append; 336, argument_list; 336, 337; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:Remind; 340, identifier:_parse_rdate; 341, argument_list; 341, 342; 342, attribute; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:vevent; 345, identifier:rdate; 346, identifier:value; 347, elif_clause; 347, 348; 347, 353; 348, call; 348, 349; 348, 350; 349, identifier:isinstance; 350, argument_list; 350, 351; 350, 352; 351, identifier:trigdates; 352, identifier:str; 353, block; 353, 354; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:remind; 358, identifier:append; 359, argument_list; 359, 360; 360, identifier:trigdates; 361, if_statement; 361, 362; 361, 367; 362, call; 362, 363; 362, 364; 363, identifier:hasattr; 364, argument_list; 364, 365; 364, 366; 365, identifier:vevent; 366, string:'class'; 367, block; 367, 368; 368, expression_statement; 368, 369; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:remind; 372, identifier:append; 373, argument_list; 373, 374; 374, binary_operator:%; 374, 375; 374, 376; 375, string:'TAG %s'; 376, call; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:Remind; 379, identifier:_abbr_tag; 380, argument_list; 380, 381; 381, call; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, identifier:vevent; 384, identifier:getChildValue; 385, argument_list; 385, 386; 386, string:'class'; 387, if_statement; 387, 388; 387, 389; 388, identifier:tags; 389, block; 389, 390; 390, expression_statement; 390, 391; 391, call; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:remind; 394, identifier:extend; 395, argument_list; 395, 396; 396, list_comprehension; 396, 397; 396, 405; 397, binary_operator:%; 397, 398; 397, 399; 398, string:'TAG %s'; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:Remind; 402, identifier:_abbr_tag; 403, argument_list; 403, 404; 404, identifier:tag; 405, for_in_clause; 405, 406; 405, 407; 406, identifier:tag; 407, identifier:tags; 408, if_statement; 408, 409; 408, 414; 409, call; 409, 410; 409, 411; 410, identifier:hasattr; 411, argument_list; 411, 412; 411, 413; 412, identifier:vevent; 413, string:'categories_list'; 414, block; 414, 415; 415, for_statement; 415, 416; 415, 417; 415, 420; 416, identifier:categories; 417, attribute; 417, 418; 417, 419; 418, identifier:vevent; 419, identifier:categories_list; 420, block; 420, 421; 421, for_statement; 421, 422; 421, 423; 421, 426; 422, identifier:category; 423, attribute; 423, 424; 423, 425; 424, identifier:categories; 425, identifier:value; 426, block; 426, 427; 427, expression_statement; 427, 428; 428, call; 428, 429; 428, 432; 429, attribute; 429, 430; 429, 431; 430, identifier:remind; 431, identifier:append; 432, argument_list; 432, 433; 433, binary_operator:%; 433, 434; 433, 435; 434, string:'TAG %s'; 435, call; 435, 436; 435, 439; 436, attribute; 436, 437; 436, 438; 437, identifier:Remind; 438, identifier:_abbr_tag; 439, argument_list; 439, 440; 440, identifier:category; 441, expression_statement; 441, 442; 442, call; 442, 443; 442, 446; 443, attribute; 443, 444; 443, 445; 444, identifier:remind; 445, identifier:append; 446, argument_list; 446, 447; 447, call; 447, 448; 447, 451; 448, attribute; 448, 449; 448, 450; 449, identifier:Remind; 450, identifier:_gen_msg; 451, argument_list; 451, 452; 451, 453; 451, 454; 451, 455; 452, identifier:vevent; 453, identifier:label; 454, identifier:tail; 455, identifier:sep; 456, return_statement; 456, 457; 457, binary_operator:+; 457, 458; 457, 464; 458, call; 458, 459; 458, 462; 459, attribute; 459, 460; 459, 461; 460, string:' '; 461, identifier:join; 462, argument_list; 462, 463; 463, identifier:remind; 464, string:'\n'
def to_remind(self, vevent, label=None, priority=None, tags=None, tail=None, sep=" ", postdate=None, posttime=None): """Generate a Remind command from the given vevent""" remind = ['REM'] trigdates = None if hasattr(vevent, 'rrule'): trigdates = Remind._parse_rruleset(vevent.rruleset) dtstart = vevent.dtstart.value # If we don't get timezone information, handle it as a naive datetime. # See https://github.com/jspricke/python-remind/issues/2 for reference. if isinstance(dtstart, datetime) and dtstart.tzinfo: dtstart = dtstart.astimezone(self._localtz) dtend = None if hasattr(vevent, 'dtend'): dtend = vevent.dtend.value if isinstance(dtend, datetime) and dtend.tzinfo: dtend = dtend.astimezone(self._localtz) if not hasattr(vevent, 'rdate') and not isinstance(trigdates, str): remind.append(dtstart.strftime('%b %d %Y').replace(' 0', ' ')) if postdate: remind.append(postdate) if priority: remind.append('PRIORITY %s' % priority) if isinstance(trigdates, list): remind.extend(trigdates) duration = Remind._event_duration(vevent) if type(dtstart) is date and duration.days > 1: remind.append('*1') if dtend is not None: dtend -= timedelta(days=1) remind.append(dtend.strftime('UNTIL %b %d %Y').replace(' 0', ' ')) if isinstance(dtstart, datetime): remind.append(dtstart.strftime('AT %H:%M').replace(' 0', ' ')) if posttime: remind.append(posttime) if duration.total_seconds() > 0: remind.append('DURATION %d:%02d' % divmod(duration.total_seconds() / 60, 60)) if hasattr(vevent, 'rdate'): remind.append(Remind._parse_rdate(vevent.rdate.value)) elif isinstance(trigdates, str): remind.append(trigdates) if hasattr(vevent, 'class'): remind.append('TAG %s' % Remind._abbr_tag(vevent.getChildValue('class'))) if tags: remind.extend(['TAG %s' % Remind._abbr_tag(tag) for tag in tags]) if hasattr(vevent, 'categories_list'): for categories in vevent.categories_list: for category in categories.value: remind.append('TAG %s' % Remind._abbr_tag(category)) remind.append(Remind._gen_msg(vevent, label, tail, sep)) return ' '.join(remind) + '\n'
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:module_name_from_path; 3, parameters; 3, 4; 3, 5; 4, identifier:folder_name; 5, default_parameter; 5, 6; 5, 7; 6, identifier:verbose; 7, False; 8, block; 8, 9; 8, 11; 8, 12; 8, 23; 8, 34; 8, 45; 8, 51; 8, 58; 8, 59; 8, 63; 8, 73; 8, 74; 8, 75; 8, 76; 8, 77; 8, 78; 8, 79; 8, 80; 8, 81; 8, 82; 8, 83; 8, 84; 8, 85; 8, 193; 8, 194; 8, 195; 8, 196; 8, 197; 8, 198; 8, 199; 8, 200; 8, 201; 8, 202; 8, 203; 8, 204; 8, 205; 8, 206; 8, 207; 8, 208; 8, 209; 8, 210; 8, 220; 8, 221; 8, 222; 8, 223; 8, 229; 8, 238; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:folder_name; 15, subscript; 15, 16; 15, 22; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:folder_name; 19, identifier:split; 20, argument_list; 20, 21; 21, string:'.pyc'; 22, integer:0; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:folder_name; 26, subscript; 26, 27; 26, 33; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:folder_name; 30, identifier:split; 31, argument_list; 31, 32; 32, string:'.py'; 33, integer:0; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:folder_name; 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:normpath; 43, argument_list; 43, 44; 44, identifier:folder_name; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:path; 48, binary_operator:+; 48, 49; 48, 50; 49, identifier:folder_name; 50, string:'/'; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:package; 54, call; 54, 55; 54, 56; 55, identifier:get_python_package; 56, argument_list; 56, 57; 57, identifier:path; 58, comment; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:module; 62, list:[]; 63, if_statement; 63, 64; 63, 65; 64, identifier:verbose; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:print; 69, argument_list; 69, 70; 70, tuple; 70, 71; 70, 72; 71, string:'folder_name'; 72, identifier:folder_name; 73, comment; 74, comment; 75, comment; 76, comment; 77, comment; 78, comment; 79, comment; 80, comment; 81, comment; 82, comment; 83, comment; 84, comment; 85, while_statement; 85, 86; 85, 87; 86, True; 87, block; 87, 88; 87, 99; 87, 113; 87, 137; 87, 138; 87, 159; 87, 177; 87, 178; 87, 179; 87, 180; 87, 181; 87, 182; 87, 183; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:path; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:os; 95, identifier:path; 96, identifier:dirname; 97, argument_list; 97, 98; 98, identifier:path; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:module; 103, identifier:append; 104, argument_list; 104, 105; 105, call; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:os; 109, identifier:path; 110, identifier:basename; 111, argument_list; 111, 112; 112, identifier:path; 113, if_statement; 113, 114; 113, 124; 114, comparison_operator:==; 114, 115; 114, 123; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:os; 119, identifier:path; 120, identifier:basename; 121, argument_list; 121, 122; 122, identifier:path; 123, identifier:package; 124, block; 124, 125; 124, 136; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:path; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:os; 132, identifier:path; 133, identifier:dirname; 134, argument_list; 134, 135; 135, identifier:path; 136, break_statement; 137, comment; 138, if_statement; 138, 139; 138, 149; 139, comparison_operator:==; 139, 140; 139, 148; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:os; 144, identifier:path; 145, identifier:dirname; 146, argument_list; 146, 147; 147, identifier:path; 148, identifier:path; 149, block; 149, 150; 149, 158; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, pattern_list; 152, 153; 152, 154; 153, identifier:path; 154, identifier:module; 155, expression_list; 155, 156; 155, 157; 156, None; 157, None; 158, break_statement; 159, if_statement; 159, 160; 159, 161; 160, identifier:verbose; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:print; 165, argument_list; 165, 166; 166, tuple; 166, 167; 166, 168; 166, 169; 167, string:'path'; 168, identifier:path; 169, call; 169, 170; 169, 175; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:os; 173, identifier:path; 174, identifier:dirname; 175, argument_list; 175, 176; 176, identifier:path; 177, comment; 178, comment; 179, comment; 180, comment; 181, comment; 182, comment; 183, if_statement; 183, 184; 183, 185; 184, identifier:verbose; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:print; 189, argument_list; 189, 190; 190, tuple; 190, 191; 190, 192; 191, string:'module'; 192, identifier:module; 193, comment; 194, comment; 195, comment; 196, comment; 197, comment; 198, comment; 199, comment; 200, comment; 201, comment; 202, comment; 203, comment; 204, comment; 205, comment; 206, comment; 207, comment; 208, comment; 209, comment; 210, if_statement; 210, 211; 210, 212; 211, identifier:verbose; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 216; 215, identifier:print; 216, argument_list; 216, 217; 217, tuple; 217, 218; 217, 219; 218, string:'module'; 219, identifier:module; 220, comment; 221, comment; 222, comment; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:module; 227, identifier:reverse; 228, argument_list; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:module; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, string:'.'; 235, identifier:join; 236, argument_list; 236, 237; 237, identifier:module; 238, return_statement; 238, 239; 239, expression_list; 239, 240; 239, 241; 240, identifier:module; 241, identifier:path
def module_name_from_path(folder_name, verbose=False): """ takes in a path to a folder or file and return the module path and the path to the module the module is idenitified by the path being in os.path, e.g. if /Users/Projects/Python/ is in os.path, then folder_name = '/Users/PycharmProjects/pylabcontrol/pylabcontrol/scripts/script_dummy.pyc' returns '/Users/PycharmProjects/' as the path and pylabcontrol.scripts.script_dummy as the module Args: folder_name: path to a file of the form '/Users/PycharmProjects/pylabcontrol/pylabcontrol/scripts/script_dummy.pyc' Returns: module: a string of the form, e.g. pylabcontrol.scripts.script_dummy ... path: a string with the path to the module, e.g. /Users/PycharmProjects/ """ # strip off endings folder_name = folder_name.split('.pyc')[0] folder_name = folder_name.split('.py')[0] folder_name = os.path.normpath(folder_name) path = folder_name + '/' package = get_python_package(path) # path = folder_name module = [] if verbose: print(('folder_name', folder_name)) # os_sys_path = os.sys.path # # if os.path.normpath(path) in os_sys_path: # if verbose: # print('warning: path in sys.path!') # os_sys_path.remove(os.path.normpath(path)) # # # if verbose: # for elem in os_sys_path: # # print('os.sys.path', elem) while True: path = os.path.dirname(path) module.append(os.path.basename(path)) if os.path.basename(path) == package: path = os.path.dirname(path) break # failed to identify the module if os.path.dirname(path) == path: path, module = None, None break if verbose: print(('path', path, os.path.dirname(path))) # if path == os.path.dirname(path): # if verbose: # print('break -- os.path.dirname(path)', os.path.dirname(path)) # # path, module = None, None # break # if verbose: print(('module', module)) # OLD START # while path not in os_sys_path: # path = os.path.dirname(path) # # if verbose: # print('path', path, os.path.dirname(path)) # # if path == os.path.dirname(path): # if verbose: # print('break -- os.path.dirname(path)', os.path.dirname(path)) # # path, module = None, None # break # module.append(os.path.basename(path)) # # if verbose: # print('module', module) # OLD END if verbose: print(('module', module)) # module = module[:-1] # print('mod', module) # from the list construct the path like b26_toolkit.pylabcontrol.scripts and load it module.reverse() module = '.'.join(module) return module, path
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:main; 3, parameters; 4, block; 4, 5; 4, 7; 4, 15; 4, 32; 4, 49; 4, 63; 4, 77; 4, 91; 4, 99; 4, 105; 4, 111; 4, 115; 4, 124; 4, 128; 4, 137; 4, 141; 4, 150; 4, 154; 4, 163; 4, 183; 4, 187; 4, 213; 4, 225; 4, 356; 5, expression_statement; 5, 6; 6, comment; 7, expression_statement; 7, 8; 8, assignment; 8, 9; 8, 10; 9, identifier:parser; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:argparse; 13, identifier:ArgumentParser; 14, argument_list; 15, expression_statement; 15, 16; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:parser; 19, identifier:add_argument; 20, argument_list; 20, 21; 20, 22; 20, 23; 20, 26; 20, 29; 21, string:"-f"; 22, string:"--file"; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:required; 25, True; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:help; 28, string:"input file"; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:type; 31, identifier:str; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:parser; 36, identifier:add_argument; 37, argument_list; 37, 38; 37, 39; 37, 40; 37, 43; 37, 46; 38, string:"-l"; 39, string:"--locus"; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:required; 42, True; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:help; 45, string:"Locus"; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:type; 48, identifier:str; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:parser; 53, identifier:add_argument; 54, argument_list; 54, 55; 54, 56; 54, 57; 54, 60; 55, string:"-k"; 56, string:"--kir"; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:help; 59, string:"Option for running with KIR"; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:action; 62, string:'store_true'; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:parser; 67, identifier:add_argument; 68, argument_list; 68, 69; 68, 70; 68, 71; 68, 74; 69, string:"-s"; 70, string:"--server"; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:help; 73, string:"Option for running with a server"; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:action; 76, string:'store_true'; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:parser; 81, identifier:add_argument; 82, argument_list; 82, 83; 82, 84; 82, 85; 82, 88; 83, string:"-v"; 84, string:"--verbose"; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:help; 87, string:"Option for running in verbose"; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:action; 90, string:'store_true'; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:args; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:parser; 97, identifier:parse_args; 98, argument_list; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:fastafile; 102, attribute; 102, 103; 102, 104; 103, identifier:args; 104, identifier:file; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:locus; 108, attribute; 108, 109; 108, 110; 109, identifier:args; 110, identifier:locus; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:verbose; 114, False; 115, if_statement; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:args; 118, identifier:verbose; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:verbose; 123, True; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:verbose; 127, False; 128, if_statement; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:args; 131, identifier:verbose; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:verbose; 136, True; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:kir; 140, False; 141, if_statement; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:args; 144, identifier:kir; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:kir; 149, True; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:serv; 153, False; 154, if_statement; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:args; 157, identifier:server; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:serv; 162, True; 163, if_statement; 163, 164; 163, 165; 164, identifier:verbose; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:logging; 170, identifier:basicConfig; 171, argument_list; 171, 172; 171, 175; 171, 178; 172, keyword_argument; 172, 173; 172, 174; 173, identifier:format; 174, string:'%(asctime)s - %(name)-35s - %(levelname)-5s - %(message)s'; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:datefmt; 177, string:'%m/%d/%Y %I:%M:%S %p'; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:level; 180, attribute; 180, 181; 180, 182; 181, identifier:logging; 182, identifier:INFO; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:server; 186, None; 187, if_statement; 187, 188; 187, 189; 188, identifier:serv; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:server; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:BioSeqDatabase; 196, identifier:open_database; 197, argument_list; 197, 198; 197, 201; 197, 204; 197, 207; 197, 210; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:driver; 200, string:"pymysql"; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:user; 203, string:"root"; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:passwd; 206, string:""; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:host; 209, string:"localhost"; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:db; 212, string:"bioseqdb"; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:seqann; 216, call; 216, 217; 216, 218; 217, identifier:BioSeqAnn; 218, argument_list; 218, 219; 218, 222; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:verbose; 221, True; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:kir; 224, identifier:kir; 225, for_statement; 225, 226; 225, 227; 225, 234; 226, identifier:seq; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:SeqIO; 230, identifier:parse; 231, argument_list; 231, 232; 231, 233; 232, identifier:fastafile; 233, string:"fasta"; 234, block; 234, 235; 234, 247; 234, 264; 234, 268; 234, 351; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:ann; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:seqann; 241, identifier:annotate; 242, argument_list; 242, 243; 242, 244; 243, identifier:seq; 244, keyword_argument; 244, 245; 244, 246; 245, identifier:locus; 246, identifier:locus; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 250; 249, identifier:print; 250, argument_list; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, string:'{:*^20} {:^20} {:*^20}'; 254, identifier:format; 255, argument_list; 255, 256; 255, 257; 255, 263; 256, string:""; 257, call; 257, 258; 257, 259; 258, identifier:str; 259, argument_list; 259, 260; 260, attribute; 260, 261; 260, 262; 261, identifier:seq; 262, identifier:description; 263, string:""; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:l; 267, integer:0; 268, for_statement; 268, 269; 268, 270; 268, 273; 269, identifier:f; 270, attribute; 270, 271; 270, 272; 271, identifier:ann; 272, identifier:annotation; 273, block; 273, 274; 274, if_statement; 274, 275; 274, 284; 274, 315; 275, call; 275, 276; 275, 277; 276, identifier:isinstance; 277, argument_list; 277, 278; 277, 283; 278, subscript; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:ann; 281, identifier:annotation; 282, identifier:f; 283, identifier:DBSeq; 284, block; 284, 285; 284, 304; 285, expression_statement; 285, 286; 286, call; 286, 287; 286, 288; 287, identifier:print; 288, argument_list; 288, 289; 288, 290; 288, 293; 288, 301; 289, identifier:f; 290, attribute; 290, 291; 290, 292; 291, identifier:ann; 292, identifier:method; 293, call; 293, 294; 293, 295; 294, identifier:str; 295, argument_list; 295, 296; 296, subscript; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:ann; 299, identifier:annotation; 300, identifier:f; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:sep; 303, string:"\t"; 304, expression_statement; 304, 305; 305, augmented_assignment:+=; 305, 306; 305, 307; 306, identifier:l; 307, call; 307, 308; 307, 309; 308, identifier:len; 309, argument_list; 309, 310; 310, subscript; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:ann; 313, identifier:annotation; 314, identifier:f; 315, else_clause; 315, 316; 316, block; 316, 317; 316, 338; 317, expression_statement; 317, 318; 318, call; 318, 319; 318, 320; 319, identifier:print; 320, argument_list; 320, 321; 320, 322; 320, 325; 320, 335; 321, identifier:f; 322, attribute; 322, 323; 322, 324; 323, identifier:ann; 324, identifier:method; 325, call; 325, 326; 325, 327; 326, identifier:str; 327, argument_list; 327, 328; 328, attribute; 328, 329; 328, 334; 329, subscript; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:ann; 332, identifier:annotation; 333, identifier:f; 334, identifier:seq; 335, keyword_argument; 335, 336; 335, 337; 336, identifier:sep; 337, string:"\t"; 338, expression_statement; 338, 339; 339, augmented_assignment:+=; 339, 340; 339, 341; 340, identifier:l; 341, call; 341, 342; 341, 343; 342, identifier:len; 343, argument_list; 343, 344; 344, attribute; 344, 345; 344, 350; 345, subscript; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:ann; 348, identifier:annotation; 349, identifier:f; 350, identifier:seq; 351, expression_statement; 351, 352; 352, call; 352, 353; 352, 354; 353, identifier:print; 354, argument_list; 354, 355; 355, string:""; 356, if_statement; 356, 357; 356, 358; 357, identifier:serv; 358, block; 358, 359; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:server; 363, identifier:close; 364, argument_list
def main(): """This is run if file is directly executed, but not if imported as module. Having this in a separate function allows importing the file into interactive python, and still able to execute the function for testing""" parser = argparse.ArgumentParser() parser.add_argument("-f", "--file", required=True, help="input file", type=str) parser.add_argument("-l", "--locus", required=True, help="Locus", type=str) parser.add_argument("-k", "--kir", help="Option for running with KIR", action='store_true') parser.add_argument("-s", "--server", help="Option for running with a server", action='store_true') parser.add_argument("-v", "--verbose", help="Option for running in verbose", action='store_true') args = parser.parse_args() fastafile = args.file locus = args.locus verbose = False if args.verbose: verbose = True verbose = False if args.verbose: verbose = True kir = False if args.kir: kir = True serv = False if args.server: serv = True if verbose: logging.basicConfig(format='%(asctime)s - %(name)-35s - %(levelname)-5s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) server = None if serv: server = BioSeqDatabase.open_database(driver="pymysql", user="root", passwd="", host="localhost", db="bioseqdb") seqann = BioSeqAnn(verbose=True, kir=kir) for seq in SeqIO.parse(fastafile, "fasta"): ann = seqann.annotate(seq, locus=locus) print('{:*^20} {:^20} {:*^20}'.format("", str(seq.description), "")) l = 0 for f in ann.annotation: if isinstance(ann.annotation[f], DBSeq): print(f, ann.method, str(ann.annotation[f]), sep="\t") l += len(ann.annotation[f]) else: print(f, ann.method, str(ann.annotation[f].seq), sep="\t") l += len(ann.annotation[f].seq) print("") if serv: server.close()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_push_subtree; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:leaves; 7, type; 7, 8; 8, generic_type; 8, 9; 8, 10; 9, identifier:List; 10, type_parameter; 10, 11; 11, type; 11, 12; 12, identifier:bytes; 13, block; 13, 14; 13, 16; 13, 23; 13, 38; 13, 39; 13, 40; 13, 53; 13, 71; 13, 86; 13, 91; 13, 109; 13, 119; 13, 134; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:size; 19, call; 19, 20; 19, 21; 20, identifier:len; 21, argument_list; 21, 22; 22, identifier:leaves; 23, if_statement; 23, 24; 23, 30; 24, comparison_operator:!=; 24, 25; 24, 29; 25, call; 25, 26; 25, 27; 26, identifier:count_bits_set; 27, argument_list; 27, 28; 28, identifier:size; 29, integer:1; 30, block; 30, 31; 31, raise_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:ValueError; 34, argument_list; 34, 35; 35, binary_operator:%; 35, 36; 35, 37; 36, string:"invalid subtree with size != 2^k: %s"; 37, identifier:size; 38, comment; 39, comment; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 45; 42, pattern_list; 42, 43; 42, 44; 43, identifier:subtree_h; 44, identifier:mintree_h; 45, expression_list; 45, 46; 45, 50; 46, call; 46, 47; 46, 48; 47, identifier:lowest_bit_set; 48, argument_list; 48, 49; 49, identifier:size; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:__mintree_height; 53, if_statement; 53, 54; 53, 61; 54, boolean_operator:and; 54, 55; 54, 58; 55, comparison_operator:>; 55, 56; 55, 57; 56, identifier:mintree_h; 57, integer:0; 58, comparison_operator:>; 58, 59; 58, 60; 59, identifier:subtree_h; 60, identifier:mintree_h; 61, block; 61, 62; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:ValueError; 65, argument_list; 65, 66; 66, binary_operator:%; 66, 67; 66, 68; 67, string:"subtree %s > current smallest subtree %s"; 68, tuple; 68, 69; 68, 70; 69, identifier:subtree_h; 70, identifier:mintree_h; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, pattern_list; 73, 74; 73, 75; 74, identifier:root_hash; 75, identifier:hashes; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:__hasher; 81, identifier:_hash_full; 82, argument_list; 82, 83; 82, 84; 82, 85; 83, identifier:leaves; 84, integer:0; 85, identifier:size; 86, assert_statement; 86, 87; 87, comparison_operator:==; 87, 88; 87, 89; 88, identifier:hashes; 89, tuple; 89, 90; 90, identifier:root_hash; 91, if_statement; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:hashStore; 95, block; 95, 96; 96, for_statement; 96, 97; 96, 98; 96, 99; 97, identifier:h; 98, identifier:hashes; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:hashStore; 106, identifier:writeLeaf; 107, argument_list; 107, 108; 108, identifier:h; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:new_node_hashes; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:__push_subtree_hash; 116, argument_list; 116, 117; 116, 118; 117, identifier:subtree_h; 118, identifier:root_hash; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:nodes; 122, list_comprehension; 122, 123; 122, 129; 123, tuple; 123, 124; 123, 127; 123, 128; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:tree_size; 127, identifier:height; 128, identifier:h; 129, for_in_clause; 129, 130; 129, 133; 130, pattern_list; 130, 131; 130, 132; 131, identifier:h; 132, identifier:height; 133, identifier:new_node_hashes; 134, if_statement; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:hashStore; 138, block; 138, 139; 139, for_statement; 139, 140; 139, 141; 139, 142; 140, identifier:node; 141, identifier:nodes; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:hashStore; 149, identifier:writeNode; 150, argument_list; 150, 151; 151, identifier:node
def _push_subtree(self, leaves: List[bytes]): """Extend with a full subtree <= the current minimum subtree. The leaves must form a full subtree, i.e. of size 2^k for some k. If there is a minimum subtree (i.e. __mintree_height > 0), then the input subtree must be smaller or of equal size to the minimum subtree. If the subtree is smaller (or no such minimum exists, in an empty tree), we can simply append its hash to self.hashes, since this maintains the invariant property of being sorted in descending size order. If the subtree is of equal size, we are in a similar situation to an addition carry. We handle it by combining the two subtrees into a larger subtree (of size 2^(k+1)), then recursively trying to add this new subtree back into the tree. Any collection of leaves larger than the minimum subtree must undergo additional partition to conform with the structure of a merkle tree, which is a more complex operation, performed by extend(). """ size = len(leaves) if count_bits_set(size) != 1: raise ValueError("invalid subtree with size != 2^k: %s" % size) # in general we want the highest bit, but here it's also the lowest bit # so just reuse that code instead of writing a new highest_bit_set() subtree_h, mintree_h = lowest_bit_set(size), self.__mintree_height if mintree_h > 0 and subtree_h > mintree_h: raise ValueError("subtree %s > current smallest subtree %s" % ( subtree_h, mintree_h)) root_hash, hashes = self.__hasher._hash_full(leaves, 0, size) assert hashes == (root_hash,) if self.hashStore: for h in hashes: self.hashStore.writeLeaf(h) new_node_hashes = self.__push_subtree_hash(subtree_h, root_hash) nodes = [(self.tree_size, height, h) for h, height in new_node_hashes] if self.hashStore: for node in nodes: self.hashStore.writeNode(node)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:blocks; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:aln; 5, default_parameter; 5, 6; 5, 7; 6, identifier:threshold; 7, float:0.5; 8, default_parameter; 8, 9; 8, 10; 9, identifier:weights; 10, None; 11, block; 11, 12; 11, 14; 11, 19; 11, 110; 11, 123; 11, 142; 11, 152; 11, 173; 11, 177; 11, 207; 12, expression_statement; 12, 13; 13, comment; 14, assert_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:len; 17, argument_list; 17, 18; 18, identifier:aln; 19, if_statement; 19, 20; 19, 23; 19, 47; 20, comparison_operator:==; 20, 21; 20, 22; 21, identifier:weights; 22, False; 23, block; 23, 24; 24, function_definition; 24, 25; 24, 26; 24, 28; 25, function_name:pct_nongaps; 26, parameters; 26, 27; 27, identifier:col; 28, block; 28, 29; 29, return_statement; 29, 30; 30, binary_operator:-; 30, 31; 30, 32; 31, integer:1; 32, parenthesized_expression; 32, 33; 33, binary_operator:/; 33, 34; 33, 43; 34, call; 34, 35; 34, 36; 35, identifier:float; 36, argument_list; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:col; 40, identifier:count; 41, argument_list; 41, 42; 42, string:'-'; 43, call; 43, 44; 43, 45; 44, identifier:len; 45, argument_list; 45, 46; 46, identifier:col; 47, else_clause; 47, 48; 48, block; 48, 49; 48, 64; 49, if_statement; 49, 50; 49, 55; 50, comparison_operator:in; 50, 51; 50, 52; 51, identifier:weights; 52, tuple; 52, 53; 52, 54; 53, None; 54, True; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:weights; 59, call; 59, 60; 59, 61; 60, identifier:sequence_weights; 61, argument_list; 61, 62; 61, 63; 62, identifier:aln; 63, string:'avg1'; 64, function_definition; 64, 65; 64, 66; 64, 68; 65, function_name:pct_nongaps; 66, parameters; 66, 67; 67, identifier:col; 68, block; 68, 69; 68, 79; 68, 100; 69, assert_statement; 69, 70; 70, comparison_operator:==; 70, 71; 70, 75; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, identifier:col; 75, call; 75, 76; 75, 77; 76, identifier:len; 77, argument_list; 77, 78; 78, identifier:weights; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:ngaps; 82, call; 82, 83; 82, 84; 83, identifier:sum; 84, generator_expression; 84, 85; 84, 91; 85, binary_operator:*; 85, 86; 85, 87; 86, identifier:wt; 87, parenthesized_expression; 87, 88; 88, comparison_operator:==; 88, 89; 88, 90; 89, identifier:c; 90, string:'-'; 91, for_in_clause; 91, 92; 91, 95; 92, pattern_list; 92, 93; 92, 94; 93, identifier:wt; 94, identifier:c; 95, call; 95, 96; 95, 97; 96, identifier:zip; 97, argument_list; 97, 98; 97, 99; 98, identifier:weights; 99, identifier:col; 100, return_statement; 100, 101; 101, binary_operator:-; 101, 102; 101, 103; 102, integer:1; 103, parenthesized_expression; 103, 104; 104, binary_operator:/; 104, 105; 104, 106; 105, identifier:ngaps; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, identifier:col; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:seqstrs; 113, list_comprehension; 113, 114; 113, 120; 114, call; 114, 115; 114, 116; 115, identifier:str; 116, argument_list; 116, 117; 117, attribute; 117, 118; 117, 119; 118, identifier:rec; 119, identifier:seq; 120, for_in_clause; 120, 121; 120, 122; 121, identifier:rec; 122, identifier:aln; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:clean_cols; 126, list_comprehension; 126, 127; 126, 128; 126, 135; 127, identifier:col; 128, for_in_clause; 128, 129; 128, 130; 129, identifier:col; 130, call; 130, 131; 130, 132; 131, identifier:zip; 132, argument_list; 132, 133; 133, list_splat; 133, 134; 134, identifier:seqstrs; 135, if_clause; 135, 136; 136, comparison_operator:>=; 136, 137; 136, 141; 137, call; 137, 138; 137, 139; 138, identifier:pct_nongaps; 139, argument_list; 139, 140; 140, identifier:col; 141, identifier:threshold; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:alphabet; 145, attribute; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:aln; 149, integer:0; 150, identifier:seq; 151, identifier:alphabet; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:clean_seqs; 155, list_comprehension; 155, 156; 155, 166; 156, call; 156, 157; 156, 158; 157, identifier:Seq; 158, argument_list; 158, 159; 158, 165; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, string:''; 162, identifier:join; 163, argument_list; 163, 164; 164, identifier:row; 165, identifier:alphabet; 166, for_in_clause; 166, 167; 166, 168; 167, identifier:row; 168, call; 168, 169; 168, 170; 169, identifier:zip; 170, argument_list; 170, 171; 171, list_splat; 171, 172; 172, identifier:clean_cols; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:clean_recs; 176, list:[]; 177, for_statement; 177, 178; 177, 181; 177, 186; 178, pattern_list; 178, 179; 178, 180; 179, identifier:rec; 180, identifier:seq; 181, call; 181, 182; 181, 183; 182, identifier:zip; 183, argument_list; 183, 184; 183, 185; 184, identifier:aln; 185, identifier:clean_seqs; 186, block; 186, 187; 186, 194; 186, 200; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:newrec; 190, call; 190, 191; 190, 192; 191, identifier:deepcopy; 192, argument_list; 192, 193; 193, identifier:rec; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:newrec; 198, identifier:seq; 199, identifier:seq; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:clean_recs; 204, identifier:append; 205, argument_list; 205, 206; 206, identifier:newrec; 207, return_statement; 207, 208; 208, call; 208, 209; 208, 210; 209, identifier:MultipleSeqAlignment; 210, argument_list; 210, 211; 210, 212; 211, identifier:clean_recs; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:alphabet; 214, identifier:alphabet
def blocks(aln, threshold=0.5, weights=None): """Remove gappy columns from an alignment.""" assert len(aln) if weights == False: def pct_nongaps(col): return 1 - (float(col.count('-')) / len(col)) else: if weights in (None, True): weights = sequence_weights(aln, 'avg1') def pct_nongaps(col): assert len(col) == len(weights) ngaps = sum(wt * (c == '-') for wt, c in zip(weights, col)) return 1 - (ngaps / len(col)) seqstrs = [str(rec.seq) for rec in aln] clean_cols = [col for col in zip(*seqstrs) if pct_nongaps(col) >= threshold] alphabet = aln[0].seq.alphabet clean_seqs = [Seq(''.join(row), alphabet) for row in zip(*clean_cols)] clean_recs = [] for rec, seq in zip(aln, clean_seqs): newrec = deepcopy(rec) newrec.seq = seq clean_recs.append(newrec) return MultipleSeqAlignment(clean_recs, alphabet=alphabet)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:sequence_weights; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:aln; 5, default_parameter; 5, 6; 5, 7; 6, identifier:scaling; 7, string:'none'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:gap_chars; 10, string:'-.'; 11, block; 11, 12; 11, 14; 11, 15; 11, 16; 11, 17; 11, 122; 11, 225; 11, 235; 11, 239; 11, 240; 11, 241; 11, 242; 11, 295; 11, 296; 11, 297; 11, 298; 11, 320; 11, 404; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:expectk; 20, list:[0.0, 1.0, 1.953, 2.861, 3.705, 4.524, 5.304, 6.026, 6.724, 7.397, 8.04, 8.622, 9.191, 9.739, 10.264, 10.758, 11.194, 11.635, 12.049, 12.468, 12.806, 13.185, 13.539, 13.863, 14.177, 14.466, 14.737, 15.005, 15.245, 15.491, 15.681, 15.916, 16.12, 16.301, 16.485, 16.671, 16.831, 16.979, 17.151, 17.315, 17.427, 17.559, 17.68, 17.791, 17.914, 18.009, 18.113, 18.203, 18.298, 18.391, 18.46, 18.547, 18.617, 18.669, 18.77, 18.806, 18.858, 18.934, 18.978, 19.027, 19.085, 19.119, 19.169, 19.202, 19.256, 19.291, 19.311, 19.357, 19.399, 19.416, 19.456, 19.469, 19.5, 19.53, 19.553, 19.562, 19.602, 19.608, 19.629, 19.655, 19.67, 19.681, 19.7, 19.716, 19.724, 19.748, 19.758, 19.765, 19.782, 19.791, 19.799, 19.812, 19.82, 19.828, 19.844, 19.846, 19.858, 19.863, 19.862, 19.871, 19.882]; 20, 21; 20, 22; 20, 23; 20, 24; 20, 25; 20, 26; 20, 27; 20, 28; 20, 29; 20, 30; 20, 31; 20, 32; 20, 33; 20, 34; 20, 35; 20, 36; 20, 37; 20, 38; 20, 39; 20, 40; 20, 41; 20, 42; 20, 43; 20, 44; 20, 45; 20, 46; 20, 47; 20, 48; 20, 49; 20, 50; 20, 51; 20, 52; 20, 53; 20, 54; 20, 55; 20, 56; 20, 57; 20, 58; 20, 59; 20, 60; 20, 61; 20, 62; 20, 63; 20, 64; 20, 65; 20, 66; 20, 67; 20, 68; 20, 69; 20, 70; 20, 71; 20, 72; 20, 73; 20, 74; 20, 75; 20, 76; 20, 77; 20, 78; 20, 79; 20, 80; 20, 81; 20, 82; 20, 83; 20, 84; 20, 85; 20, 86; 20, 87; 20, 88; 20, 89; 20, 90; 20, 91; 20, 92; 20, 93; 20, 94; 20, 95; 20, 96; 20, 97; 20, 98; 20, 99; 20, 100; 20, 101; 20, 102; 20, 103; 20, 104; 20, 105; 20, 106; 20, 107; 20, 108; 20, 109; 20, 110; 20, 111; 20, 112; 20, 113; 20, 114; 20, 115; 20, 116; 20, 117; 20, 118; 20, 119; 20, 120; 20, 121; 21, float:0.0; 22, float:1.0; 23, float:1.953; 24, float:2.861; 25, float:3.705; 26, float:4.524; 27, float:5.304; 28, float:6.026; 29, float:6.724; 30, float:7.397; 31, float:8.04; 32, float:8.622; 33, float:9.191; 34, float:9.739; 35, float:10.264; 36, float:10.758; 37, float:11.194; 38, float:11.635; 39, float:12.049; 40, float:12.468; 41, float:12.806; 42, float:13.185; 43, float:13.539; 44, float:13.863; 45, float:14.177; 46, float:14.466; 47, float:14.737; 48, float:15.005; 49, float:15.245; 50, float:15.491; 51, float:15.681; 52, float:15.916; 53, float:16.12; 54, float:16.301; 55, float:16.485; 56, float:16.671; 57, float:16.831; 58, float:16.979; 59, float:17.151; 60, float:17.315; 61, float:17.427; 62, float:17.559; 63, float:17.68; 64, float:17.791; 65, float:17.914; 66, float:18.009; 67, float:18.113; 68, float:18.203; 69, float:18.298; 70, float:18.391; 71, float:18.46; 72, float:18.547; 73, float:18.617; 74, float:18.669; 75, float:18.77; 76, float:18.806; 77, float:18.858; 78, float:18.934; 79, float:18.978; 80, float:19.027; 81, float:19.085; 82, float:19.119; 83, float:19.169; 84, float:19.202; 85, float:19.256; 86, float:19.291; 87, float:19.311; 88, float:19.357; 89, float:19.399; 90, float:19.416; 91, float:19.456; 92, float:19.469; 93, float:19.5; 94, float:19.53; 95, float:19.553; 96, float:19.562; 97, float:19.602; 98, float:19.608; 99, float:19.629; 100, float:19.655; 101, float:19.67; 102, float:19.681; 103, float:19.7; 104, float:19.716; 105, float:19.724; 106, float:19.748; 107, float:19.758; 108, float:19.765; 109, float:19.782; 110, float:19.791; 111, float:19.799; 112, float:19.812; 113, float:19.82; 114, float:19.828; 115, float:19.844; 116, float:19.846; 117, float:19.858; 118, float:19.863; 119, float:19.862; 120, float:19.871; 121, float:19.882; 122, function_definition; 122, 123; 122, 124; 122, 126; 123, function_name:col_weight; 124, parameters; 124, 125; 125, identifier:column; 126, block; 126, 127; 126, 129; 126, 130; 126, 143; 126, 169; 126, 170; 126, 171; 126, 178; 126, 179; 126, 180; 126, 187; 126, 188; 126, 211; 126, 221; 127, expression_statement; 127, 128; 128, comment; 129, comment; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:min_nongap; 133, call; 133, 134; 133, 135; 134, identifier:max; 135, argument_list; 135, 136; 135, 137; 136, integer:2; 137, binary_operator:*; 137, 138; 137, 139; 138, float:.2; 139, call; 139, 140; 139, 141; 140, identifier:len; 141, argument_list; 141, 142; 142, identifier:column; 143, if_statement; 143, 144; 143, 158; 144, comparison_operator:<; 144, 145; 144, 157; 145, call; 145, 146; 145, 147; 146, identifier:len; 147, argument_list; 147, 148; 148, list_comprehension; 148, 149; 148, 150; 148, 153; 149, identifier:c; 150, for_in_clause; 150, 151; 150, 152; 151, identifier:c; 152, identifier:column; 153, if_clause; 153, 154; 154, comparison_operator:not; 154, 155; 154, 156; 155, identifier:c; 156, identifier:gap_chars; 157, identifier:min_nongap; 158, block; 158, 159; 159, return_statement; 159, 160; 160, tuple; 160, 161; 160, 168; 161, binary_operator:*; 161, 162; 161, 164; 162, list:[0]; 162, 163; 163, integer:0; 164, call; 164, 165; 164, 166; 165, identifier:len; 166, argument_list; 166, 167; 167, identifier:column; 168, integer:0; 169, comment; 170, comment; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:counts; 174, call; 174, 175; 174, 176; 175, identifier:Counter; 176, argument_list; 176, 177; 177, identifier:column; 178, comment; 179, comment; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:n_residues; 183, call; 183, 184; 183, 185; 184, identifier:len; 185, argument_list; 185, 186; 186, identifier:counts; 187, comment; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:freqs; 191, call; 191, 192; 191, 193; 192, identifier:dict; 193, generator_expression; 193, 194; 193, 202; 194, tuple; 194, 195; 194, 196; 195, identifier:aa; 196, binary_operator:/; 196, 197; 196, 198; 197, float:1.0; 198, parenthesized_expression; 198, 199; 199, binary_operator:*; 199, 200; 199, 201; 200, identifier:n_residues; 201, identifier:count; 202, for_in_clause; 202, 203; 202, 206; 203, pattern_list; 203, 204; 203, 205; 204, identifier:aa; 205, identifier:count; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:counts; 209, identifier:iteritems; 210, argument_list; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:weights; 214, list_comprehension; 214, 215; 214, 218; 215, subscript; 215, 216; 215, 217; 216, identifier:freqs; 217, identifier:aa; 218, for_in_clause; 218, 219; 218, 220; 219, identifier:aa; 220, identifier:column; 221, return_statement; 221, 222; 222, tuple; 222, 223; 222, 224; 223, identifier:weights; 224, identifier:n_residues; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:seq_weights; 228, binary_operator:*; 228, 229; 228, 231; 229, list:[0]; 229, 230; 230, integer:0; 231, call; 231, 232; 231, 233; 232, identifier:len; 233, argument_list; 233, 234; 234, identifier:aln; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:tot_nres; 238, float:0.0; 239, comment; 240, comment; 241, comment; 242, for_statement; 242, 243; 242, 244; 242, 249; 243, identifier:col; 244, call; 244, 245; 244, 246; 245, identifier:zip; 246, argument_list; 246, 247; 247, list_splat; 247, 248; 248, identifier:aln; 249, block; 249, 250; 249, 259; 249, 266; 249, 280; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 255; 252, pattern_list; 252, 253; 252, 254; 253, identifier:wts; 254, identifier:nres; 255, call; 255, 256; 255, 257; 256, identifier:col_weight; 257, argument_list; 257, 258; 258, identifier:col; 259, assert_statement; 259, 260; 260, comparison_operator:<=; 260, 261; 260, 265; 261, call; 261, 262; 261, 263; 262, identifier:sum; 263, argument_list; 263, 264; 264, identifier:wts; 265, integer:20; 266, expression_statement; 266, 267; 267, augmented_assignment:+=; 267, 268; 267, 269; 268, identifier:tot_nres; 269, conditional_expression:if; 269, 270; 269, 273; 269, 279; 270, subscript; 270, 271; 270, 272; 271, identifier:expectk; 272, identifier:nres; 273, comparison_operator:<; 273, 274; 273, 275; 274, identifier:nres; 275, call; 275, 276; 275, 277; 276, identifier:len; 277, argument_list; 277, 278; 278, identifier:expectk; 279, integer:20; 280, for_statement; 280, 281; 280, 284; 280, 288; 281, pattern_list; 281, 282; 281, 283; 282, identifier:idx; 283, identifier:wt; 284, call; 284, 285; 284, 286; 285, identifier:enumerate; 286, argument_list; 286, 287; 287, identifier:wts; 288, block; 288, 289; 289, expression_statement; 289, 290; 290, augmented_assignment:+=; 290, 291; 290, 294; 291, subscript; 291, 292; 291, 293; 292, identifier:seq_weights; 293, identifier:idx; 294, identifier:wt; 295, comment; 296, comment; 297, comment; 298, if_statement; 298, 299; 298, 302; 299, comparison_operator:==; 299, 300; 299, 301; 300, identifier:scaling; 301, string:'none'; 302, block; 302, 303; 302, 312; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:avg_seq_len; 306, binary_operator:/; 306, 307; 306, 308; 307, identifier:tot_nres; 308, call; 308, 309; 308, 310; 309, identifier:len; 310, argument_list; 310, 311; 311, identifier:aln; 312, return_statement; 312, 313; 313, list_comprehension; 313, 314; 313, 317; 314, binary_operator:/; 314, 315; 314, 316; 315, identifier:wt; 316, identifier:avg_seq_len; 317, for_in_clause; 317, 318; 317, 319; 318, identifier:wt; 319, identifier:seq_weights; 320, if_statement; 320, 321; 320, 324; 320, 334; 320, 348; 320, 365; 320, 395; 321, comparison_operator:==; 321, 322; 321, 323; 322, identifier:scaling; 323, string:'max1'; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:scale; 328, binary_operator:/; 328, 329; 328, 330; 329, float:1.0; 330, call; 330, 331; 330, 332; 331, identifier:max; 332, argument_list; 332, 333; 333, identifier:seq_weights; 334, elif_clause; 334, 335; 334, 338; 335, comparison_operator:==; 335, 336; 335, 337; 336, identifier:scaling; 337, string:'sum1'; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:scale; 342, binary_operator:/; 342, 343; 342, 344; 343, float:1.0; 344, call; 344, 345; 344, 346; 345, identifier:sum; 346, argument_list; 346, 347; 347, identifier:seq_weights; 348, elif_clause; 348, 349; 348, 352; 349, comparison_operator:==; 349, 350; 349, 351; 350, identifier:scaling; 351, string:'avg1'; 352, block; 352, 353; 353, expression_statement; 353, 354; 354, assignment; 354, 355; 354, 356; 355, identifier:scale; 356, binary_operator:/; 356, 357; 356, 361; 357, call; 357, 358; 357, 359; 358, identifier:len; 359, argument_list; 359, 360; 360, identifier:aln; 361, call; 361, 362; 361, 363; 362, identifier:sum; 363, argument_list; 363, 364; 364, identifier:seq_weights; 365, elif_clause; 365, 366; 365, 369; 365, 370; 366, comparison_operator:==; 366, 367; 366, 368; 367, identifier:scaling; 368, string:'andy'; 369, comment; 370, block; 370, 371; 370, 383; 371, expression_statement; 371, 372; 372, assignment; 372, 373; 372, 374; 373, identifier:scale; 374, binary_operator:/; 374, 375; 374, 379; 375, call; 375, 376; 375, 377; 376, identifier:len; 377, argument_list; 377, 378; 378, identifier:aln; 379, call; 379, 380; 379, 381; 380, identifier:sum; 381, argument_list; 381, 382; 382, identifier:seq_weights; 383, return_statement; 383, 384; 384, list_comprehension; 384, 385; 384, 392; 385, call; 385, 386; 385, 387; 386, identifier:min; 387, argument_list; 387, 388; 387, 391; 388, binary_operator:*; 388, 389; 388, 390; 389, identifier:scale; 390, identifier:wt; 391, float:1.0; 392, for_in_clause; 392, 393; 392, 394; 393, identifier:wt; 394, identifier:seq_weights; 395, else_clause; 395, 396; 396, block; 396, 397; 397, raise_statement; 397, 398; 398, call; 398, 399; 398, 400; 399, identifier:ValueError; 400, argument_list; 400, 401; 401, binary_operator:%; 401, 402; 401, 403; 402, string:"Unknown scaling scheme '%s'"; 403, identifier:scaling; 404, return_statement; 404, 405; 405, list_comprehension; 405, 406; 405, 409; 406, binary_operator:*; 406, 407; 406, 408; 407, identifier:scale; 408, identifier:wt; 409, for_in_clause; 409, 410; 409, 411; 410, identifier:wt; 411, identifier:seq_weights
def sequence_weights(aln, scaling='none', gap_chars='-.'): """Weight aligned sequences to emphasize more divergent members. Returns a list of floating-point numbers between 0 and 1, corresponding to the proportional weight of each sequence in the alignment. The first list is the weight of the first sequence in the alignment, and so on. Scaling schemes: - 'sum1': Weights sum to 1.0. - 'max1': Weights are all scaled so the max is 1.0. - 'avg1': Average (mean) weight is 1.0. - 'andy': Average (mean) weight is 0.5, ceiling is 1.0. - 'none': Weights are scaled to sum to the effective number of independent sequences. Method: At each column position, award each different residue an equal share of the weight, and then divide that weight equally among the sequences sharing the same residue. For each sequence, sum the contributions from each position to give a sequence weight. See Henikoff & Henikoff (1994): Position-based sequence weights. """ # Probability is hard, let's estimate by sampling! # Sample k from a population of 20 with replacement; how many unique k were # chosen? Average of 10000 runs for k = 0..100 expectk = [0.0, 1.0, 1.953, 2.861, 3.705, 4.524, 5.304, 6.026, 6.724, 7.397, 8.04, 8.622, 9.191, 9.739, 10.264, 10.758, 11.194, 11.635, 12.049, 12.468, 12.806, 13.185, 13.539, 13.863, 14.177, 14.466, 14.737, 15.005, 15.245, 15.491, 15.681, 15.916, 16.12, 16.301, 16.485, 16.671, 16.831, 16.979, 17.151, 17.315, 17.427, 17.559, 17.68, 17.791, 17.914, 18.009, 18.113, 18.203, 18.298, 18.391, 18.46, 18.547, 18.617, 18.669, 18.77, 18.806, 18.858, 18.934, 18.978, 19.027, 19.085, 19.119, 19.169, 19.202, 19.256, 19.291, 19.311, 19.357, 19.399, 19.416, 19.456, 19.469, 19.5, 19.53, 19.553, 19.562, 19.602, 19.608, 19.629, 19.655, 19.67, 19.681, 19.7, 19.716, 19.724, 19.748, 19.758, 19.765, 19.782, 19.791, 19.799, 19.812, 19.82, 19.828, 19.844, 19.846, 19.858, 19.863, 19.862, 19.871, 19.882] def col_weight(column): """Represent the diversity at a position. Award each different residue an equal share of the weight, and then divide that weight equally among the sequences sharing the same residue. So, if in a position of a multiple alignment, r different residues are represented, a residue represented in only one sequence contributes a score of 1/r to that sequence, whereas a residue represented in s sequences contributes a score of 1/rs to each of the s sequences. """ # Skip columns of all or mostly gaps (i.e. rare inserts) min_nongap = max(2, .2*len(column)) if len([c for c in column if c not in gap_chars]) < min_nongap: return ([0] * len(column), 0) # Count the number of occurrences of each residue type # (Treat gaps as a separate, 21st character) counts = Counter(column) # Get residue weights: 1/rs, where # r = nb. residue types, s = count of a particular residue type n_residues = len(counts) # r freqs = dict((aa, 1.0 / (n_residues * count)) for aa, count in counts.iteritems()) weights = [freqs[aa] for aa in column] return (weights, n_residues) seq_weights = [0] * len(aln) tot_nres = 0.0 # Expected no. different types in independent seqs # Sum the contributions from each position along each sequence # -> total weight for col in zip(*aln): wts, nres = col_weight(col) assert sum(wts) <= 20 tot_nres += expectk[nres] if nres < len(expectk) else 20 for idx, wt in enumerate(wts): seq_weights[idx] += wt # if tot_nres == 0: # raise ValueError("Alignment has no meaningful columns to weight") # Normalize w/ the given scaling criterion if scaling == 'none': avg_seq_len = tot_nres / len(aln) return [wt/avg_seq_len for wt in seq_weights] if scaling == 'max1': scale = 1.0 / max(seq_weights) elif scaling == 'sum1': scale = 1.0 / sum(seq_weights) elif scaling == 'avg1': scale = len(aln) / sum(seq_weights) elif scaling == 'andy': # "Robust" strategy used in CHAIN (Neuwald 2003) scale = len(aln) / sum(seq_weights) return [min(scale * wt, 1.0) for wt in seq_weights] else: raise ValueError("Unknown scaling scheme '%s'" % scaling) return [scale * wt for wt in seq_weights]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:load_and_append; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:instrument_dict; 5, default_parameter; 5, 6; 5, 7; 6, identifier:instruments; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:raise_errors; 10, False; 11, block; 11, 12; 11, 14; 11, 23; 11, 27; 11, 34; 11, 38; 11, 291; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 18; 15, comparison_operator:is; 15, 16; 15, 17; 16, identifier:instruments; 17, None; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:instruments; 22, dictionary; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:updated_instruments; 26, dictionary; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:updated_instruments; 31, identifier:update; 32, argument_list; 32, 33; 33, identifier:instruments; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:loaded_failed; 37, dictionary; 38, for_statement; 38, 39; 38, 42; 38, 47; 39, pattern_list; 39, 40; 39, 41; 40, identifier:instrument_name; 41, identifier:instrument_class_name; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:instrument_dict; 45, identifier:items; 46, argument_list; 47, block; 47, 48; 47, 52; 47, 56; 47, 57; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:instrument_settings; 51, None; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:module; 55, None; 56, comment; 57, if_statement; 57, 58; 57, 77; 57, 95; 58, boolean_operator:and; 58, 59; 58, 69; 58, 70; 59, comparison_operator:in; 59, 60; 59, 61; 60, identifier:instrument_name; 61, call; 61, 62; 61, 63; 62, identifier:list; 63, argument_list; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:instruments; 67, identifier:keys; 68, argument_list; 69, line_continuation:\; 70, comparison_operator:==; 70, 71; 70, 72; 71, identifier:instrument_class_name; 72, attribute; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:instruments; 75, identifier:instrument_name; 76, identifier:__name__; 77, block; 77, 78; 77, 89; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:print; 81, argument_list; 81, 82; 82, parenthesized_expression; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, string:'WARNING: instrument {:s} already exists. Did not load!'; 86, identifier:format; 87, argument_list; 87, 88; 88, identifier:instrument_name; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:loaded_failed; 93, identifier:instrument_name; 94, identifier:instrument_name; 95, else_clause; 95, 96; 96, block; 96, 97; 96, 101; 96, 285; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:instrument_instance; 100, None; 101, if_statement; 101, 102; 101, 107; 101, 211; 101, 244; 102, call; 102, 103; 102, 104; 103, identifier:isinstance; 104, argument_list; 104, 105; 104, 106; 105, identifier:instrument_class_name; 106, identifier:dict; 107, block; 107, 108; 107, 119; 107, 128; 107, 137; 107, 146; 107, 153; 107, 161; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:in; 109, 110; 109, 111; 110, string:'settings'; 111, identifier:instrument_class_name; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:instrument_settings; 116, subscript; 116, 117; 116, 118; 117, identifier:instrument_class_name; 118, string:'settings'; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:instrument_filepath; 122, call; 122, 123; 122, 124; 123, identifier:str; 124, argument_list; 124, 125; 125, subscript; 125, 126; 125, 127; 126, identifier:instrument_class_name; 127, string:'filepath'; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:instrument_class_name; 131, call; 131, 132; 131, 133; 132, identifier:str; 133, argument_list; 133, 134; 134, subscript; 134, 135; 134, 136; 135, identifier:instrument_class_name; 136, string:'class'; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, pattern_list; 139, 140; 139, 141; 140, identifier:path_to_module; 141, identifier:_; 142, call; 142, 143; 142, 144; 143, identifier:module_name_from_path; 144, argument_list; 144, 145; 145, identifier:instrument_filepath; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:module; 149, call; 149, 150; 149, 151; 150, identifier:import_module; 151, argument_list; 151, 152; 152, identifier:path_to_module; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:class_of_instrument; 156, call; 156, 157; 156, 158; 157, identifier:getattr; 158, argument_list; 158, 159; 158, 160; 159, identifier:module; 160, identifier:instrument_class_name; 161, try_statement; 161, 162; 161, 193; 162, block; 162, 163; 163, if_statement; 163, 164; 163, 167; 163, 168; 163, 178; 164, comparison_operator:is; 164, 165; 164, 166; 165, identifier:instrument_settings; 166, None; 167, comment; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:instrument_instance; 172, call; 172, 173; 172, 174; 173, identifier:class_of_instrument; 174, argument_list; 174, 175; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:name; 177, identifier:instrument_name; 178, else_clause; 178, 179; 178, 180; 179, comment; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:instrument_instance; 184, call; 184, 185; 184, 186; 185, identifier:class_of_instrument; 186, argument_list; 186, 187; 186, 190; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:name; 189, identifier:instrument_name; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:settings; 192, identifier:instrument_settings; 193, except_clause; 193, 194; 193, 198; 194, as_pattern; 194, 195; 194, 196; 195, identifier:Exception; 196, as_pattern_target; 196, 197; 197, identifier:e; 198, block; 198, 199; 198, 205; 198, 210; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:loaded_failed; 203, identifier:instrument_name; 204, identifier:e; 205, if_statement; 205, 206; 205, 207; 206, identifier:raise_errors; 207, block; 207, 208; 208, raise_statement; 208, 209; 209, identifier:e; 210, continue_statement; 211, elif_clause; 211, 212; 211, 217; 212, call; 212, 213; 212, 214; 213, identifier:isinstance; 214, argument_list; 214, 215; 214, 216; 215, identifier:instrument_class_name; 216, identifier:Instrument; 217, block; 217, 218; 217, 224; 217, 240; 217, 241; 217, 242; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:instrument_class_name; 221, attribute; 221, 222; 221, 223; 222, identifier:instrument_class_name; 223, identifier:__class__; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:instrument_filepath; 227, call; 227, 228; 227, 233; 228, attribute; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:os; 231, identifier:path; 232, identifier:dirname; 233, argument_list; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:inspect; 237, identifier:getfile; 238, argument_list; 238, 239; 239, identifier:instrument_class_name; 240, comment; 241, comment; 242, raise_statement; 242, 243; 243, identifier:NotImplementedError; 244, elif_clause; 244, 245; 244, 250; 245, call; 245, 246; 245, 247; 246, identifier:issubclass; 247, argument_list; 247, 248; 247, 249; 248, identifier:instrument_class_name; 249, identifier:Instrument; 250, block; 250, 251; 250, 255; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:class_of_instrument; 254, identifier:instrument_class_name; 255, if_statement; 255, 256; 255, 259; 255, 260; 255, 270; 256, comparison_operator:is; 256, 257; 256, 258; 257, identifier:instrument_settings; 258, None; 259, comment; 260, block; 260, 261; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:instrument_instance; 264, call; 264, 265; 264, 266; 265, identifier:class_of_instrument; 266, argument_list; 266, 267; 267, keyword_argument; 267, 268; 267, 269; 268, identifier:name; 269, identifier:instrument_name; 270, else_clause; 270, 271; 270, 272; 271, comment; 272, block; 272, 273; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:instrument_instance; 276, call; 276, 277; 276, 278; 277, identifier:class_of_instrument; 278, argument_list; 278, 279; 278, 282; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:name; 281, identifier:instrument_name; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:settings; 284, identifier:instrument_settings; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 290; 287, subscript; 287, 288; 287, 289; 288, identifier:updated_instruments; 289, identifier:instrument_name; 290, identifier:instrument_instance; 291, return_statement; 291, 292; 292, expression_list; 292, 293; 292, 294; 293, identifier:updated_instruments; 294, identifier:loaded_failed
def load_and_append(instrument_dict, instruments=None, raise_errors=False): """ load instrument from instrument_dict and append to instruments Args: instrument_dict: dictionary of form instrument_dict = { name_of_instrument_1 : {"settings" : settings_dictionary, "class" : name_of_class} name_of_instrument_2 : {"settings" : settings_dictionary, "class" : name_of_class} ... } or instrument_dict = { name_of_instrument_1 : name_of_class, name_of_instrument_2 : name_of_class ... } where name_of_class is either a class or a dictionary of the form {class: name_of__class, filepath: path_to_instr_file} instruments: dictionary of form instruments = { name_of_instrument_1 : instance_of_instrument_1, name_of_instrument_2 : instance_of_instrument_2, ... } raise_errors: if true errors are raised, if False they are caught but not raised Returns: dictionary updated_instruments that contains the old and the new instruments and list loaded_failed = [name_of_instrument_1, name_of_instrument_2, ....] that contains the instruments that were requested but could not be loaded """ if instruments is None: instruments = {} updated_instruments = {} updated_instruments.update(instruments) loaded_failed = {} for instrument_name, instrument_class_name in instrument_dict.items(): instrument_settings = None module = None # check if instrument already exists if instrument_name in list(instruments.keys()) \ and instrument_class_name == instruments[instrument_name].__name__: print(('WARNING: instrument {:s} already exists. Did not load!'.format(instrument_name))) loaded_failed[instrument_name] = instrument_name else: instrument_instance = None if isinstance(instrument_class_name, dict): if 'settings' in instrument_class_name: instrument_settings = instrument_class_name['settings'] instrument_filepath = str(instrument_class_name['filepath']) instrument_class_name = str(instrument_class_name['class']) path_to_module, _ = module_name_from_path(instrument_filepath) module = import_module(path_to_module) class_of_instrument = getattr(module, instrument_class_name) try: if instrument_settings is None: # this creates an instance of the class with default settings instrument_instance = class_of_instrument(name=instrument_name) else: # this creates an instance of the class with custom settings instrument_instance = class_of_instrument(name=instrument_name, settings=instrument_settings) except Exception as e: loaded_failed[instrument_name] = e if raise_errors: raise e continue elif isinstance(instrument_class_name, Instrument): instrument_class_name = instrument_class_name.__class__ instrument_filepath = os.path.dirname(inspect.getfile(instrument_class_name)) # here we should also create an instrument instance at some point as in the other cases... # instrument_instance = raise NotImplementedError elif issubclass(instrument_class_name, Instrument): class_of_instrument = instrument_class_name if instrument_settings is None: # this creates an instance of the class with default settings instrument_instance = class_of_instrument(name=instrument_name) else: # this creates an instance of the class with custom settings instrument_instance = class_of_instrument(name=instrument_name, settings=instrument_settings) updated_instruments[instrument_name] = instrument_instance return updated_instruments, loaded_failed
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_scan_nodes; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:nodelist; 5, identifier:context; 6, identifier:instance_types; 7, default_parameter; 7, 8; 7, 9; 8, identifier:current_block; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:ignore_blocks; 12, None; 13, block; 13, 14; 13, 16; 13, 20; 13, 318; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:results; 19, list:[]; 20, for_statement; 20, 21; 20, 22; 20, 23; 20, 24; 21, identifier:node; 22, identifier:nodelist; 23, comment; 24, block; 24, 25; 25, if_statement; 25, 26; 25, 31; 25, 40; 25, 41; 25, 120; 25, 121; 25, 137; 25, 138; 25, 197; 25, 198; 25, 214; 25, 215; 25, 216; 25, 272; 25, 273; 26, call; 26, 27; 26, 28; 27, identifier:isinstance; 28, argument_list; 28, 29; 28, 30; 29, identifier:node; 30, identifier:instance_types; 31, block; 31, 32; 31, 39; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:results; 36, identifier:append; 37, argument_list; 37, 38; 38, identifier:node; 39, comment; 40, comment; 41, elif_clause; 41, 42; 41, 47; 41, 48; 42, call; 42, 43; 42, 44; 43, identifier:isinstance; 44, argument_list; 44, 45; 44, 46; 45, identifier:node; 46, identifier:IncludeNode; 47, comment; 48, block; 48, 49; 49, if_statement; 49, 50; 49, 53; 49, 54; 49, 55; 49, 56; 50, attribute; 50, 51; 50, 52; 51, identifier:node; 52, identifier:template; 53, comment; 54, comment; 55, comment; 56, block; 56, 57; 56, 90; 56, 108; 57, if_statement; 57, 58; 57, 70; 57, 82; 58, not_operator; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:callable; 61, argument_list; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:getattr; 64, argument_list; 64, 65; 64, 68; 64, 69; 65, attribute; 65, 66; 65, 67; 66, identifier:node; 67, identifier:template; 68, string:'render'; 69, None; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:template; 74, call; 74, 75; 74, 76; 75, identifier:get_template; 76, argument_list; 76, 77; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:node; 80, identifier:template; 81, identifier:var; 82, else_clause; 82, 83; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:template; 87, attribute; 87, 88; 87, 89; 88, identifier:node; 89, identifier:template; 90, if_statement; 90, 91; 90, 100; 90, 101; 91, boolean_operator:and; 91, 92; 91, 95; 92, comparison_operator:is; 92, 93; 92, 94; 93, identifier:TemplateAdapter; 94, None; 95, call; 95, 96; 95, 97; 96, identifier:isinstance; 97, argument_list; 97, 98; 97, 99; 98, identifier:template; 99, identifier:TemplateAdapter; 100, comment; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:template; 105, attribute; 105, 106; 105, 107; 106, identifier:template; 107, identifier:template; 108, expression_statement; 108, 109; 109, augmented_assignment:+=; 109, 110; 109, 111; 110, identifier:results; 111, call; 111, 112; 111, 113; 112, identifier:_scan_nodes; 113, argument_list; 113, 114; 113, 117; 113, 118; 113, 119; 114, attribute; 114, 115; 114, 116; 115, identifier:template; 116, identifier:nodelist; 117, identifier:context; 118, identifier:instance_types; 119, identifier:current_block; 120, comment; 121, elif_clause; 121, 122; 121, 127; 122, call; 122, 123; 122, 124; 123, identifier:isinstance; 124, argument_list; 124, 125; 124, 126; 125, identifier:node; 126, identifier:ExtendsNode; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, augmented_assignment:+=; 129, 130; 129, 131; 130, identifier:results; 131, call; 131, 132; 131, 133; 132, identifier:_extend_nodelist; 133, argument_list; 133, 134; 133, 135; 133, 136; 134, identifier:node; 135, identifier:context; 136, identifier:instance_types; 137, comment; 138, elif_clause; 138, 139; 138, 146; 139, boolean_operator:and; 139, 140; 139, 145; 140, call; 140, 141; 140, 142; 141, identifier:isinstance; 142, argument_list; 142, 143; 142, 144; 143, identifier:node; 144, identifier:VariableNode; 145, identifier:current_block; 146, block; 146, 147; 147, if_statement; 147, 148; 147, 155; 147, 156; 148, comparison_operator:==; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:node; 152, identifier:filter_expression; 153, identifier:token; 154, string:'block.super'; 155, comment; 156, block; 156, 157; 156, 181; 157, if_statement; 157, 158; 157, 166; 158, not_operator; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:hasattr; 161, argument_list; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:current_block; 164, identifier:parent; 165, string:'nodelist'; 166, block; 166, 167; 167, raise_statement; 167, 168; 168, call; 168, 169; 168, 170; 169, identifier:TemplateSyntaxError; 170, argument_list; 170, 171; 171, call; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, concatenated_string; 173, 174; 173, 175; 174, string:"Cannot read {{{{ block.super }}}} for {{% block {0} %}}, "; 175, string:"the parent template doesn't have this block."; 176, identifier:format; 177, argument_list; 177, 178; 178, attribute; 178, 179; 178, 180; 179, identifier:current_block; 180, identifier:name; 181, expression_statement; 181, 182; 182, augmented_assignment:+=; 182, 183; 182, 184; 183, identifier:results; 184, call; 184, 185; 184, 186; 185, identifier:_scan_nodes; 186, argument_list; 186, 187; 186, 192; 186, 193; 186, 194; 187, attribute; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:current_block; 190, identifier:parent; 191, identifier:nodelist; 192, identifier:context; 193, identifier:instance_types; 194, attribute; 194, 195; 194, 196; 195, identifier:current_block; 196, identifier:parent; 197, comment; 198, elif_clause; 198, 199; 198, 212; 199, boolean_operator:and; 199, 200; 199, 207; 200, boolean_operator:and; 200, 201; 200, 206; 201, call; 201, 202; 201, 203; 202, identifier:isinstance; 203, argument_list; 203, 204; 203, 205; 204, identifier:node; 205, identifier:BlockNode; 206, identifier:ignore_blocks; 207, comparison_operator:in; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:node; 210, identifier:name; 211, identifier:ignore_blocks; 212, block; 212, 213; 213, continue_statement; 214, comment; 215, comment; 216, elif_clause; 216, 217; 216, 222; 217, call; 217, 218; 217, 219; 218, identifier:hasattr; 219, argument_list; 219, 220; 219, 221; 220, identifier:node; 221, string:'child_nodelists'; 222, block; 222, 223; 223, for_statement; 223, 224; 223, 225; 223, 228; 224, identifier:nodelist_name; 225, attribute; 225, 226; 225, 227; 226, identifier:node; 227, identifier:child_nodelists; 228, block; 228, 229; 229, if_statement; 229, 230; 229, 235; 230, call; 230, 231; 230, 232; 231, identifier:hasattr; 232, argument_list; 232, 233; 232, 234; 233, identifier:node; 234, identifier:nodelist_name; 235, block; 235, 236; 235, 244; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:subnodelist; 239, call; 239, 240; 239, 241; 240, identifier:getattr; 241, argument_list; 241, 242; 241, 243; 242, identifier:node; 243, identifier:nodelist_name; 244, if_statement; 244, 245; 244, 250; 245, call; 245, 246; 245, 247; 246, identifier:isinstance; 247, argument_list; 247, 248; 247, 249; 248, identifier:subnodelist; 249, identifier:NodeList; 250, block; 250, 251; 250, 262; 251, if_statement; 251, 252; 251, 257; 252, call; 252, 253; 252, 254; 253, identifier:isinstance; 254, argument_list; 254, 255; 254, 256; 255, identifier:node; 256, identifier:BlockNode; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:current_block; 261, identifier:node; 262, expression_statement; 262, 263; 263, augmented_assignment:+=; 263, 264; 263, 265; 264, identifier:results; 265, call; 265, 266; 265, 267; 266, identifier:_scan_nodes; 267, argument_list; 267, 268; 267, 269; 267, 270; 267, 271; 268, identifier:subnodelist; 269, identifier:context; 270, identifier:instance_types; 271, identifier:current_block; 272, comment; 273, else_clause; 273, 274; 274, block; 274, 275; 275, for_statement; 275, 276; 275, 277; 275, 281; 276, identifier:attr; 277, call; 277, 278; 277, 279; 278, identifier:dir; 279, argument_list; 279, 280; 280, identifier:node; 281, block; 281, 282; 281, 290; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:obj; 285, call; 285, 286; 285, 287; 286, identifier:getattr; 287, argument_list; 287, 288; 287, 289; 288, identifier:node; 289, identifier:attr; 290, if_statement; 290, 291; 290, 296; 291, call; 291, 292; 291, 293; 292, identifier:isinstance; 293, argument_list; 293, 294; 293, 295; 294, identifier:obj; 295, identifier:NodeList; 296, block; 296, 297; 296, 308; 297, if_statement; 297, 298; 297, 303; 298, call; 298, 299; 298, 300; 299, identifier:isinstance; 300, argument_list; 300, 301; 300, 302; 301, identifier:node; 302, identifier:BlockNode; 303, block; 303, 304; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:current_block; 307, identifier:node; 308, expression_statement; 308, 309; 309, augmented_assignment:+=; 309, 310; 309, 311; 310, identifier:results; 311, call; 311, 312; 311, 313; 312, identifier:_scan_nodes; 313, argument_list; 313, 314; 313, 315; 313, 316; 313, 317; 314, identifier:obj; 315, identifier:context; 316, identifier:instance_types; 317, identifier:current_block; 318, return_statement; 318, 319; 319, identifier:results
def _scan_nodes(nodelist, context, instance_types, current_block=None, ignore_blocks=None): """ Loop through all nodes of a single scope level. :type nodelist: django.template.base.NodeList :type current_block: BlockNode :param instance_types: The instance to look for """ results = [] for node in nodelist: # first check if this is the object instance to look for. if isinstance(node, instance_types): results.append(node) # if it's a Constant Include Node ({% include "template_name.html" %}) # scan the child template elif isinstance(node, IncludeNode): # if there's an error in the to-be-included template, node.template becomes None if node.template: # This is required for Django 1.7 but works on older version too # Check if it quacks like a template object, if not # presume is a template path and get the object out of it if not callable(getattr(node.template, 'render', None)): template = get_template(node.template.var) else: template = node.template if TemplateAdapter is not None and isinstance(template, TemplateAdapter): # Django 1.8: received a new object, take original template template = template.template results += _scan_nodes(template.nodelist, context, instance_types, current_block) # handle {% extends ... %} tags elif isinstance(node, ExtendsNode): results += _extend_nodelist(node, context, instance_types) # in block nodes we have to scan for super blocks elif isinstance(node, VariableNode) and current_block: if node.filter_expression.token == 'block.super': # Found a {{ block.super }} line if not hasattr(current_block.parent, 'nodelist'): raise TemplateSyntaxError( "Cannot read {{{{ block.super }}}} for {{% block {0} %}}, " "the parent template doesn't have this block.".format( current_block.name )) results += _scan_nodes(current_block.parent.nodelist, context, instance_types, current_block.parent) # ignore nested blocks which are already handled elif isinstance(node, BlockNode) and ignore_blocks and node.name in ignore_blocks: continue # if the node has the newly introduced 'child_nodelists' attribute, scan # those attributes for nodelists and recurse them elif hasattr(node, 'child_nodelists'): for nodelist_name in node.child_nodelists: if hasattr(node, nodelist_name): subnodelist = getattr(node, nodelist_name) if isinstance(subnodelist, NodeList): if isinstance(node, BlockNode): current_block = node results += _scan_nodes(subnodelist, context, instance_types, current_block) # else just scan the node for nodelist instance attributes else: for attr in dir(node): obj = getattr(node, attr) if isinstance(obj, NodeList): if isinstance(node, BlockNode): current_block = node results += _scan_nodes(obj, context, instance_types, current_block) return results
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 6; 2, function_name:run_command; 3, parameters; 3, 4; 4, identifier:commands; 5, comment; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 15; 6, 19; 6, 23; 6, 191; 6, 196; 6, 235; 6, 245; 6, 251; 6, 259; 6, 278; 6, 290; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:use_shell; 14, False; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:subprocess_flags; 18, integer:0; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:startupinfo; 22, None; 23, if_statement; 23, 24; 23, 27; 23, 92; 24, comparison_operator:==; 24, 25; 24, 26; 25, identifier:sysstr; 26, string:'Windows'; 27, block; 27, 28; 27, 50; 27, 53; 27, 57; 27, 58; 27, 69; 27, 73; 27, 74; 27, 75; 27, 83; 27, 91; 28, if_statement; 28, 29; 28, 34; 29, call; 29, 30; 29, 31; 30, identifier:isinstance; 31, argument_list; 31, 32; 31, 33; 32, identifier:commands; 33, identifier:list; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:commands; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, string:' '; 41, identifier:join; 42, generator_expression; 42, 43; 42, 47; 43, call; 43, 44; 43, 45; 44, identifier:str; 45, argument_list; 45, 46; 46, identifier:c; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:c; 49, identifier:commands; 50, import_statement; 50, 51; 51, dotted_name; 51, 52; 52, identifier:ctypes; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:SEM_NOGPFAULTERRORBOX; 56, integer:0x0002; 57, comment; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 67; 60, attribute; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:ctypes; 64, identifier:windll; 65, identifier:kernel32; 66, identifier:SetErrorMode; 67, argument_list; 67, 68; 68, identifier:SEM_NOGPFAULTERRORBOX; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:subprocess_flags; 72, integer:0x8000000; 73, comment; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:startupinfo; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:subprocess; 81, identifier:STARTUPINFO; 82, argument_list; 83, expression_statement; 83, 84; 84, augmented_assignment:|=; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:startupinfo; 87, identifier:dwFlags; 88, attribute; 88, 89; 88, 90; 89, identifier:subprocess; 90, identifier:STARTF_USESHOWWINDOW; 91, comment; 92, else_clause; 92, 93; 92, 94; 93, comment; 94, block; 94, 95; 95, if_statement; 95, 96; 95, 100; 95, 107; 96, call; 96, 97; 96, 98; 97, identifier:is_string; 98, argument_list; 98, 99; 99, identifier:commands; 100, block; 100, 101; 100, 105; 100, 106; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:use_shell; 104, True; 105, comment; 106, comment; 107, elif_clause; 107, 108; 107, 113; 107, 114; 108, call; 108, 109; 108, 110; 109, identifier:isinstance; 110, argument_list; 110, 111; 110, 112; 111, identifier:commands; 112, identifier:list; 113, comment; 114, block; 114, 115; 114, 159; 115, if_statement; 115, 116; 115, 144; 116, boolean_operator:or; 116, 117; 116, 130; 116, 131; 117, comparison_operator:==; 117, 118; 117, 123; 117, 129; 118, subscript; 118, 119; 118, 122; 119, subscript; 119, 120; 119, 121; 120, identifier:commands; 121, integer:0; 122, integer:0; 123, subscript; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:commands; 126, integer:0; 127, unary_operator:-; 127, 128; 128, integer:1; 129, string:'"'; 130, line_continuation:\; 131, comparison_operator:==; 131, 132; 131, 137; 131, 143; 132, subscript; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:commands; 135, integer:0; 136, integer:0; 137, subscript; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:commands; 140, integer:0; 141, unary_operator:-; 141, 142; 142, integer:1; 143, string:"'"; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:commands; 149, integer:0; 150, subscript; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:commands; 153, integer:0; 154, slice; 154, 155; 154, 156; 154, 157; 155, integer:1; 156, colon; 157, unary_operator:-; 157, 158; 158, integer:1; 159, for_statement; 159, 160; 159, 163; 159, 167; 160, pattern_list; 160, 161; 160, 162; 161, identifier:idx; 162, identifier:v; 163, call; 163, 164; 163, 165; 164, identifier:enumerate; 165, argument_list; 165, 166; 166, identifier:commands; 167, block; 167, 168; 168, if_statement; 168, 169; 168, 180; 168, 181; 169, boolean_operator:or; 169, 170; 169, 175; 170, call; 170, 171; 170, 172; 171, identifier:isinstance; 172, argument_list; 172, 173; 172, 174; 173, identifier:v; 174, identifier:int; 175, call; 175, 176; 175, 177; 176, identifier:isinstance; 177, argument_list; 177, 178; 177, 179; 178, identifier:v; 179, identifier:float; 180, comment; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:commands; 186, identifier:idx; 187, call; 187, 188; 187, 189; 188, identifier:repr; 189, argument_list; 189, 190; 190, identifier:v; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:print; 194, argument_list; 194, 195; 195, identifier:commands; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:process; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:subprocess; 202, identifier:Popen; 203, argument_list; 203, 204; 203, 205; 203, 208; 203, 213; 203, 221; 203, 226; 203, 229; 203, 232; 204, identifier:commands; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:shell; 207, identifier:use_shell; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:stdout; 210, attribute; 210, 211; 210, 212; 211, identifier:subprocess; 212, identifier:PIPE; 213, keyword_argument; 213, 214; 213, 215; 214, identifier:stdin; 215, call; 215, 216; 215, 217; 216, identifier:open; 217, argument_list; 217, 218; 218, attribute; 218, 219; 218, 220; 219, identifier:os; 220, identifier:devnull; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:stderr; 223, attribute; 223, 224; 223, 225; 224, identifier:subprocess; 225, identifier:STDOUT; 226, keyword_argument; 226, 227; 226, 228; 227, identifier:universal_newlines; 228, True; 229, keyword_argument; 229, 230; 229, 231; 230, identifier:startupinfo; 231, identifier:startupinfo; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:creationflags; 234, identifier:subprocess_flags; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 240; 237, pattern_list; 237, 238; 237, 239; 238, identifier:out; 239, identifier:err; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:process; 243, identifier:communicate; 244, argument_list; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:recode; 248, attribute; 248, 249; 248, 250; 249, identifier:process; 250, identifier:returncode; 251, if_statement; 251, 252; 251, 255; 252, comparison_operator:is; 252, 253; 252, 254; 253, identifier:out; 254, None; 255, block; 255, 256; 256, return_statement; 256, 257; 257, list:['']; 257, 258; 258, string:''; 259, if_statement; 259, 260; 259, 267; 260, boolean_operator:and; 260, 261; 260, 264; 261, comparison_operator:is; 261, 262; 261, 263; 262, identifier:recode; 263, None; 264, comparison_operator:!=; 264, 265; 264, 266; 265, identifier:recode; 266, integer:0; 267, block; 267, 268; 268, raise_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:subprocess; 272, identifier:CalledProcessError; 273, argument_list; 273, 274; 273, 276; 273, 277; 274, unary_operator:-; 274, 275; 275, integer:1; 276, identifier:commands; 277, string:"ERROR occurred when running subprocess!"; 278, if_statement; 278, 279; 278, 282; 279, comparison_operator:in; 279, 280; 279, 281; 280, string:'\n'; 281, identifier:out; 282, block; 282, 283; 283, return_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:out; 287, identifier:split; 288, argument_list; 288, 289; 289, string:'\n'; 290, return_statement; 290, 291; 291, list:[out]; 291, 292; 292, identifier:out
def run_command(commands): # type: (Union[AnyStr, List[AnyStr]]) -> List[AnyStr] """Execute external command, and return the output lines list. In windows, refers to `handling-subprocess-crash-in-windows`_. Args: commands: string or list Returns: output lines .. _handling-subprocess-crash-in-windows: https://stackoverflow.com/questions/5069224/handling-subprocess-crash-in-windows """ # commands = StringClass.convert_unicode2str(commands) # print(commands) use_shell = False subprocess_flags = 0 startupinfo = None if sysstr == 'Windows': if isinstance(commands, list): commands = ' '.join(str(c) for c in commands) import ctypes SEM_NOGPFAULTERRORBOX = 0x0002 # From MSDN ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX) subprocess_flags = 0x8000000 # win32con.CREATE_NO_WINDOW? # this startupinfo structure prevents a console window from popping up on Windows startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # not sure if node outputs on stderr or stdout so capture both else: # for Linux/Unix OS, commands is better to be a list. if is_string(commands): use_shell = True # https://docs.python.org/2/library/subprocess.html # Using shell=True can be a security hazard. elif isinstance(commands, list): # the executable path may be enclosed with quotes, if not windows, delete the quotes if commands[0][0] == commands[0][-1] == '"' or \ commands[0][0] == commands[0][-1] == "'": commands[0] = commands[0][1:-1] for idx, v in enumerate(commands): if isinstance(v, int) or isinstance(v, float): # Fix :TypeError: execv() arg 2 must contain only strings commands[idx] = repr(v) print(commands) process = subprocess.Popen(commands, shell=use_shell, stdout=subprocess.PIPE, stdin=open(os.devnull), stderr=subprocess.STDOUT, universal_newlines=True, startupinfo=startupinfo, creationflags=subprocess_flags) out, err = process.communicate() recode = process.returncode if out is None: return [''] if recode is not None and recode != 0: raise subprocess.CalledProcessError(-1, commands, "ERROR occurred when running subprocess!") if '\n' in out: return out.split('\n') return [out]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:intersect_keys; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:keys; 5, identifier:reffile; 6, default_parameter; 6, 7; 6, 8; 7, identifier:cache; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:clean_accs; 11, False; 12, block; 12, 13; 12, 15; 12, 16; 12, 20; 12, 110; 12, 143; 12, 144; 12, 158; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:index; 19, None; 20, if_statement; 20, 21; 20, 22; 20, 104; 21, identifier:cache; 22, block; 22, 23; 22, 29; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:refcache; 26, binary_operator:+; 26, 27; 26, 28; 27, identifier:reffile; 28, string:'.sqlite'; 29, if_statement; 29, 30; 29, 38; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:os; 34, identifier:path; 35, identifier:exists; 36, argument_list; 36, 37; 37, identifier:refcache; 38, block; 38, 39; 39, if_statement; 39, 40; 39, 57; 39, 65; 40, comparison_operator:<; 40, 41; 40, 49; 41, attribute; 41, 42; 41, 48; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:os; 45, identifier:stat; 46, argument_list; 46, 47; 47, identifier:refcache; 48, identifier:st_mtime; 49, attribute; 49, 50; 49, 56; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:os; 53, identifier:stat; 54, argument_list; 54, 55; 55, identifier:reffile; 56, identifier:st_mtime; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:logging; 62, identifier:warn; 63, argument_list; 63, 64; 64, string:"Outdated cache; rebuilding index"; 65, else_clause; 65, 66; 66, block; 66, 67; 67, try_statement; 67, 68; 67, 90; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:index; 72, parenthesized_expression; 72, 73; 73, conditional_expression:if; 73, 74; 73, 83; 73, 84; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:SeqIO; 77, identifier:index_db; 78, argument_list; 78, 79; 78, 80; 79, identifier:refcache; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:key_function; 82, identifier:clean_accession; 83, identifier:clean_accs; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:SeqIO; 87, identifier:index_db; 88, argument_list; 88, 89; 89, identifier:refcache; 90, except_clause; 90, 91; 90, 92; 91, identifier:Exception; 92, block; 92, 93; 92, 100; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:logging; 97, identifier:warn; 98, argument_list; 98, 99; 99, string:"Skipping corrupted cache; rebuilding index"; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:index; 103, None; 104, else_clause; 104, 105; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:refcache; 109, string:':memory:'; 110, if_statement; 110, 111; 110, 114; 110, 115; 111, comparison_operator:is; 111, 112; 111, 113; 112, identifier:index; 113, None; 114, comment; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:index; 119, parenthesized_expression; 119, 120; 120, conditional_expression:if; 120, 121; 120, 133; 120, 134; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:SeqIO; 124, identifier:index_db; 125, argument_list; 125, 126; 125, 127; 125, 129; 125, 130; 126, identifier:refcache; 127, list:[reffile]; 127, 128; 128, identifier:reffile; 129, string:'fasta'; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:key_function; 132, identifier:clean_accession; 133, identifier:clean_accs; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:SeqIO; 137, identifier:index_db; 138, argument_list; 138, 139; 138, 140; 138, 142; 139, identifier:refcache; 140, list:[reffile]; 140, 141; 141, identifier:reffile; 142, string:'fasta'; 143, comment; 144, if_statement; 144, 145; 144, 146; 145, identifier:clean_accs; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:keys; 150, generator_expression; 150, 151; 150, 155; 151, call; 151, 152; 151, 153; 152, identifier:clean_accession; 153, argument_list; 153, 154; 154, identifier:k; 155, for_in_clause; 155, 156; 155, 157; 156, identifier:k; 157, identifier:keys; 158, for_statement; 158, 159; 158, 160; 158, 161; 159, identifier:key; 160, identifier:keys; 161, block; 161, 162; 161, 186; 162, try_statement; 162, 163; 162, 170; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:record; 167, subscript; 167, 168; 167, 169; 168, identifier:index; 169, identifier:key; 170, except_clause; 170, 171; 170, 172; 170, 173; 171, identifier:LookupError; 172, comment; 173, block; 173, 174; 173, 185; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:logging; 178, identifier:info; 179, argument_list; 179, 180; 179, 181; 180, string:"No match: %s"; 181, call; 181, 182; 181, 183; 182, identifier:repr; 183, argument_list; 183, 184; 184, identifier:key; 185, continue_statement; 186, expression_statement; 186, 187; 187, yield; 187, 188; 188, identifier:record
def intersect_keys(keys, reffile, cache=False, clean_accs=False): """Extract SeqRecords from the index by matching keys. keys - an iterable of sequence identifiers/accessions to select reffile - name of a FASTA file to extract the specified sequences from cache - save an index of the reference FASTA sequence offsets to disk? clean_accs - strip HMMer extensions from sequence accessions? """ # Build/load the index of reference sequences index = None if cache: refcache = reffile + '.sqlite' if os.path.exists(refcache): if os.stat(refcache).st_mtime < os.stat(reffile).st_mtime: logging.warn("Outdated cache; rebuilding index") else: try: index = (SeqIO.index_db(refcache, key_function=clean_accession) if clean_accs else SeqIO.index_db(refcache)) except Exception: logging.warn("Skipping corrupted cache; rebuilding index") index = None else: refcache = ':memory:' if index is None: # Rebuild the index, for whatever reason index = (SeqIO.index_db(refcache, [reffile], 'fasta', key_function=clean_accession) if clean_accs else SeqIO.index_db(refcache, [reffile], 'fasta')) # Extract records by key if clean_accs: keys = (clean_accession(k) for k in keys) for key in keys: try: record = index[key] except LookupError: # Missing keys are rare, so it's faster not to check every time logging.info("No match: %s", repr(key)) continue yield record
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:family_check; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 25; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 16; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:logger; 15, identifier:info; 16, argument_list; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, string:"Checking family relations for {0}"; 20, identifier:format; 21, argument_list; 21, 22; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:family_id; 25, for_statement; 25, 26; 25, 27; 25, 30; 26, identifier:individual_id; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:individuals; 30, block; 30, 31; 30, 45; 30, 53; 30, 67; 30, 95; 30, 101; 30, 107; 30, 230; 30, 231; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:logger; 37, identifier:debug; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, string:"Checking individual {0}"; 42, identifier:format; 43, argument_list; 43, 44; 44, identifier:individual_id; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:individual; 48, subscript; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:individuals; 52, identifier:individual_id; 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, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, string:"Checking if individual {0} is affected"; 64, identifier:format; 65, argument_list; 65, 66; 66, identifier:individual_id; 67, if_statement; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:individual; 70, identifier:affected; 71, block; 71, 72; 71, 86; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:logger; 78, identifier:debug; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, string:"Found affected individual {0}"; 83, identifier:format; 84, argument_list; 84, 85; 85, identifier:individual_id; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:affected_individuals; 92, identifier:add; 93, argument_list; 93, 94; 94, identifier:individual_id; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:father; 98, attribute; 98, 99; 98, 100; 99, identifier:individual; 100, identifier:father; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:mother; 104, attribute; 104, 105; 104, 106; 105, identifier:individual; 106, identifier:mother; 107, if_statement; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:individual; 110, identifier:has_parents; 111, block; 111, 112; 111, 126; 111, 132; 111, 173; 111, 174; 111, 229; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:logger; 118, identifier:debug; 119, argument_list; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, string:"Individual {0} has parents"; 123, identifier:format; 124, argument_list; 124, 125; 125, identifier:individual_id; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:no_relations; 131, False; 132, try_statement; 132, 133; 132, 154; 133, block; 133, 134; 133, 144; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:check_parent; 139, argument_list; 139, 140; 139, 141; 140, identifier:father; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:father; 143, True; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:check_parent; 149, argument_list; 149, 150; 149, 151; 150, identifier:mother; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:father; 153, False; 154, except_clause; 154, 155; 154, 159; 155, as_pattern; 155, 156; 155, 157; 156, identifier:PedigreeError; 157, as_pattern_target; 157, 158; 158, identifier:e; 159, block; 159, 160; 159, 171; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:logger; 166, identifier:error; 167, argument_list; 167, 168; 168, attribute; 168, 169; 168, 170; 169, identifier:e; 170, identifier:message; 171, raise_statement; 171, 172; 172, identifier:e; 173, comment; 174, if_statement; 174, 175; 174, 178; 174, 194; 174, 213; 175, attribute; 175, 176; 175, 177; 176, identifier:individual; 177, identifier:has_both_parents; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:trios; 185, identifier:append; 186, argument_list; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:set; 189, argument_list; 189, 190; 190, list:[individual_id, father, mother]; 190, 191; 190, 192; 190, 193; 191, identifier:individual_id; 192, identifier:father; 193, identifier:mother; 194, elif_clause; 194, 195; 194, 198; 195, comparison_operator:!=; 195, 196; 195, 197; 196, identifier:father; 197, string:'0'; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:duos; 205, identifier:append; 206, argument_list; 206, 207; 207, call; 207, 208; 207, 209; 208, identifier:set; 209, argument_list; 209, 210; 210, list:[individual_id, father]; 210, 211; 210, 212; 211, identifier:individual_id; 212, identifier:father; 213, else_clause; 213, 214; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 222; 217, attribute; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:duos; 221, identifier:append; 222, argument_list; 222, 223; 223, call; 223, 224; 223, 225; 224, identifier:set; 225, argument_list; 225, 226; 226, list:[individual_id, mother]; 226, 227; 226, 228; 227, identifier:individual_id; 228, identifier:mother; 229, comment; 230, comment; 231, for_statement; 231, 232; 231, 233; 231, 236; 232, identifier:individual_2_id; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:individuals; 236, block; 236, 237; 237, if_statement; 237, 238; 237, 241; 238, comparison_operator:!=; 238, 239; 238, 240; 239, identifier:individual_id; 240, identifier:individual_2_id; 241, block; 241, 242; 242, if_statement; 242, 243; 242, 250; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:self; 246, identifier:check_siblings; 247, argument_list; 247, 248; 247, 249; 248, identifier:individual_id; 249, identifier:individual_2_id; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 258; 253, attribute; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:individual; 256, identifier:siblings; 257, identifier:add; 258, argument_list; 258, 259; 259, identifier:individual_2_id
def family_check(self): """ Check if the family members break the structure of the family. eg. nonexistent parent, wrong sex on parent etc. Also extracts all trios found, this is of help for many at the moment since GATK can only do phasing of trios and duos. """ #TODO Make some tests for these self.logger.info("Checking family relations for {0}".format( self.family_id) ) for individual_id in self.individuals: self.logger.debug("Checking individual {0}".format(individual_id)) individual = self.individuals[individual_id] self.logger.debug("Checking if individual {0} is affected".format( individual_id)) if individual.affected: self.logger.debug("Found affected individual {0}".format( individual_id) ) self.affected_individuals.add(individual_id) father = individual.father mother = individual.mother if individual.has_parents: self.logger.debug("Individual {0} has parents".format( individual_id)) self.no_relations = False try: self.check_parent(father, father=True) self.check_parent(mother, father=False) except PedigreeError as e: self.logger.error(e.message) raise e # Check if there is a trio if individual.has_both_parents: self.trios.append(set([individual_id, father, mother])) elif father != '0': self.duos.append(set([individual_id, father])) else: self.duos.append(set([individual_id, mother])) ##TODO self.check_grandparents(individual) # Annotate siblings: for individual_2_id in self.individuals: if individual_id != individual_2_id: if self.check_siblings(individual_id, individual_2_id): individual.siblings.add(individual_2_id)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:to_ped; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:outfile; 7, None; 8, block; 8, 9; 8, 11; 8, 21; 8, 29; 8, 66; 8, 85; 8, 114; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:ped_header; 14, list:[ '#FamilyID', 'IndividualID', 'PaternalID', 'MaternalID', 'Sex', 'Phenotype', ]; 14, 15; 14, 16; 14, 17; 14, 18; 14, 19; 14, 20; 15, string:'#FamilyID'; 16, string:'IndividualID'; 17, string:'PaternalID'; 18, string:'MaternalID'; 19, string:'Sex'; 20, string:'Phenotype'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:extra_headers; 24, list:[ 'InheritanceModel', 'Proband', 'Consultand', 'Alive' ]; 24, 25; 24, 26; 24, 27; 24, 28; 25, string:'InheritanceModel'; 26, string:'Proband'; 27, string:'Consultand'; 28, string:'Alive'; 29, for_statement; 29, 30; 29, 31; 29, 34; 30, identifier:individual_id; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:individuals; 34, block; 34, 35; 34, 43; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:individual; 38, subscript; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:individuals; 42, identifier:individual_id; 43, for_statement; 43, 44; 43, 45; 43, 48; 44, identifier:info; 45, attribute; 45, 46; 45, 47; 46, identifier:individual; 47, identifier:extra_info; 48, block; 48, 49; 49, if_statement; 49, 50; 49, 53; 50, comparison_operator:in; 50, 51; 50, 52; 51, identifier:info; 52, identifier:extra_headers; 53, block; 53, 54; 54, if_statement; 54, 55; 54, 58; 55, comparison_operator:not; 55, 56; 55, 57; 56, identifier:info; 57, identifier:ped_header; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:ped_header; 63, identifier:append; 64, argument_list; 64, 65; 65, identifier:info; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 73; 68, attribute; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:logger; 72, identifier:debug; 73, argument_list; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, string:"Ped headers found: {0}"; 77, identifier:format; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, string:', '; 82, identifier:join; 83, argument_list; 83, 84; 84, identifier:ped_header; 85, if_statement; 85, 86; 85, 87; 85, 102; 86, identifier:outfile; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:outfile; 92, identifier:write; 93, argument_list; 93, 94; 94, binary_operator:+; 94, 95; 94, 101; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, string:'\t'; 98, identifier:join; 99, argument_list; 99, 100; 100, identifier:ped_header; 101, string:'\n'; 102, else_clause; 102, 103; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:print; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, string:'\t'; 111, identifier:join; 112, argument_list; 112, 113; 113, identifier:ped_header; 114, for_statement; 114, 115; 114, 116; 114, 121; 115, identifier:individual; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:to_json; 120, argument_list; 121, block; 121, 122; 121, 126; 121, 135; 121, 144; 121, 153; 121, 162; 121, 171; 121, 180; 121, 211; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:ped_info; 125, list:[]; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:ped_info; 130, identifier:append; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 134; 133, identifier:individual; 134, string:'family_id'; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:ped_info; 139, identifier:append; 140, argument_list; 140, 141; 141, subscript; 141, 142; 141, 143; 142, identifier:individual; 143, string:'id'; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:ped_info; 148, identifier:append; 149, argument_list; 149, 150; 150, subscript; 150, 151; 150, 152; 151, identifier:individual; 152, string:'father'; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:ped_info; 157, identifier:append; 158, argument_list; 158, 159; 159, subscript; 159, 160; 159, 161; 160, identifier:individual; 161, string:'mother'; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:ped_info; 166, identifier:append; 167, argument_list; 167, 168; 168, subscript; 168, 169; 168, 170; 169, identifier:individual; 170, string:'sex'; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:ped_info; 175, identifier:append; 176, argument_list; 176, 177; 177, subscript; 177, 178; 177, 179; 178, identifier:individual; 179, string:'phenotype'; 180, if_statement; 180, 181; 180, 187; 181, comparison_operator:>; 181, 182; 181, 186; 182, call; 182, 183; 182, 184; 183, identifier:len; 184, argument_list; 184, 185; 185, identifier:ped_header; 186, integer:6; 187, block; 187, 188; 188, for_statement; 188, 189; 188, 190; 188, 195; 189, identifier:header; 190, subscript; 190, 191; 190, 192; 191, identifier:ped_header; 192, slice; 192, 193; 192, 194; 193, integer:6; 194, colon; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:ped_info; 200, identifier:append; 201, argument_list; 201, 202; 202, call; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:individual; 206, string:'extra_info'; 207, identifier:get; 208, argument_list; 208, 209; 208, 210; 209, identifier:header; 210, string:'.'; 211, if_statement; 211, 212; 211, 213; 211, 228; 212, identifier:outfile; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:outfile; 218, identifier:write; 219, argument_list; 219, 220; 220, binary_operator:+; 220, 221; 220, 227; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, string:'\t'; 224, identifier:join; 225, argument_list; 225, 226; 226, identifier:ped_info; 227, string:'\n'; 228, else_clause; 228, 229; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:print; 233, argument_list; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, string:'\t'; 237, identifier:join; 238, argument_list; 238, 239; 239, identifier:ped_info
def to_ped(self, outfile=None): """ Print the individuals of the family in ped format The header will be the original ped header plus all headers found in extra info of the individuals """ ped_header = [ '#FamilyID', 'IndividualID', 'PaternalID', 'MaternalID', 'Sex', 'Phenotype', ] extra_headers = [ 'InheritanceModel', 'Proband', 'Consultand', 'Alive' ] for individual_id in self.individuals: individual = self.individuals[individual_id] for info in individual.extra_info: if info in extra_headers: if info not in ped_header: ped_header.append(info) self.logger.debug("Ped headers found: {0}".format( ', '.join(ped_header) )) if outfile: outfile.write('\t'.join(ped_header)+'\n') else: print('\t'.join(ped_header)) for individual in self.to_json(): ped_info = [] ped_info.append(individual['family_id']) ped_info.append(individual['id']) ped_info.append(individual['father']) ped_info.append(individual['mother']) ped_info.append(individual['sex']) ped_info.append(individual['phenotype']) if len(ped_header) > 6: for header in ped_header[6:]: ped_info.append(individual['extra_info'].get(header, '.')) if outfile: outfile.write('\t'.join(ped_info)+'\n') else: print('\t'.join(ped_info))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:refresh_instruments; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 25; 5, 124; 5, 125; 5, 126; 5, 135; 5, 164; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 13; 9, function_name:list_access_nested_dict; 10, parameters; 10, 11; 10, 12; 11, identifier:dict; 12, identifier:somelist; 13, block; 13, 14; 13, 16; 14, expression_statement; 14, 15; 15, comment; 16, return_statement; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:reduce; 19, argument_list; 19, 20; 19, 23; 19, 24; 20, attribute; 20, 21; 20, 22; 21, identifier:operator; 22, identifier:getitem; 23, identifier:somelist; 24, identifier:dict; 25, function_definition; 25, 26; 25, 27; 25, 29; 26, function_name:update; 27, parameters; 27, 28; 28, identifier:item; 29, block; 29, 30; 30, if_statement; 30, 31; 30, 36; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:item; 34, identifier:isExpanded; 35, argument_list; 36, block; 36, 37; 37, for_statement; 37, 38; 37, 39; 37, 47; 38, identifier:index; 39, call; 39, 40; 39, 41; 40, identifier:range; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:item; 45, identifier:childCount; 46, argument_list; 47, block; 47, 48; 47, 57; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:child; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:item; 54, identifier:child; 55, argument_list; 55, 56; 56, identifier:index; 57, if_statement; 57, 58; 57, 65; 57, 117; 58, comparison_operator:==; 58, 59; 58, 64; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:child; 62, identifier:childCount; 63, argument_list; 64, integer:0; 65, block; 65, 66; 65, 76; 65, 82; 65, 111; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 71; 68, pattern_list; 68, 69; 68, 70; 69, identifier:instrument; 70, identifier:path_to_instrument; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:child; 74, identifier:get_instrument; 75, argument_list; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:path_to_instrument; 80, identifier:reverse; 81, argument_list; 82, try_statement; 82, 83; 82, 84; 82, 97; 83, comment; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:value; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:instrument; 91, identifier:read_probes; 92, argument_list; 92, 93; 93, subscript; 93, 94; 93, 95; 94, identifier:path_to_instrument; 95, unary_operator:-; 95, 96; 96, integer:1; 97, except_clause; 97, 98; 97, 99; 97, 100; 98, identifier:AssertionError; 99, comment; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:value; 104, call; 104, 105; 104, 106; 105, identifier:list_access_nested_dict; 106, argument_list; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:instrument; 109, identifier:settings; 110, identifier:path_to_instrument; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:child; 115, identifier:value; 116, identifier:value; 117, else_clause; 117, 118; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:update; 122, argument_list; 122, 123; 123, identifier:child; 124, comment; 125, comment; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:tree_settings; 132, identifier:blockSignals; 133, argument_list; 133, 134; 134, True; 135, for_statement; 135, 136; 135, 137; 135, 147; 136, identifier:index; 137, call; 137, 138; 137, 139; 138, identifier:range; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:tree_settings; 145, identifier:topLevelItemCount; 146, argument_list; 147, block; 147, 148; 147, 159; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:instrument; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:tree_settings; 156, identifier:topLevelItem; 157, argument_list; 157, 158; 158, identifier:index; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:update; 162, argument_list; 162, 163; 163, identifier:instrument; 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:tree_settings; 170, identifier:blockSignals; 171, argument_list; 171, 172; 172, False
def refresh_instruments(self): """ if self.tree_settings has been expanded, ask instruments for their actual values """ def list_access_nested_dict(dict, somelist): """ Allows one to use a list to access a nested dictionary, for example: listAccessNestedDict({'a': {'b': 1}}, ['a', 'b']) returns 1 Args: dict: somelist: Returns: """ return reduce(operator.getitem, somelist, dict) def update(item): if item.isExpanded(): for index in range(item.childCount()): child = item.child(index) if child.childCount() == 0: instrument, path_to_instrument = child.get_instrument() path_to_instrument.reverse() try: #check if item is in probes value = instrument.read_probes(path_to_instrument[-1]) except AssertionError: #if item not in probes, get value from settings instead value = list_access_nested_dict(instrument.settings, path_to_instrument) child.value = value else: update(child) #need to block signals during update so that tree.itemChanged doesn't fire and the gui doesn't try to #reupdate the instruments to their current value self.tree_settings.blockSignals(True) for index in range(self.tree_settings.topLevelItemCount()): instrument = self.tree_settings.topLevelItem(index) update(instrument) self.tree_settings.blockSignals(False)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:post; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:endpoint; 6, identifier:data; 7, default_parameter; 7, 8; 7, 9; 8, identifier:parallelism; 9, integer:5; 10, block; 10, 11; 10, 13; 10, 47; 10, 59; 10, 73; 10, 81; 10, 117; 10, 129; 10, 133; 10, 140; 10, 210; 10, 211; 10, 248; 10, 249; 10, 250; 10, 251; 10, 252; 10, 253; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:headers; 16, dictionary; 16, 17; 16, 20; 16, 23; 16, 31; 16, 39; 17, pair; 17, 18; 17, 19; 18, string:"Content-Type"; 19, string:"application/json"; 20, pair; 20, 21; 20, 22; 21, string:"Accept"; 22, string:"application/json"; 23, pair; 23, 24; 23, 25; 24, string:"x-standardize-only"; 25, conditional_expression:if; 25, 26; 25, 27; 25, 30; 26, string:"true"; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:standardize; 30, string:"false"; 31, pair; 31, 32; 31, 33; 32, string:"x-include-invalid"; 33, conditional_expression:if; 33, 34; 33, 35; 33, 38; 34, string:"true"; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:invalid; 38, string:"false"; 39, pair; 39, 40; 39, 41; 40, string:"x-accept-keypair"; 41, conditional_expression:if; 41, 42; 41, 43; 41, 46; 42, string:"true"; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:accept_keypair; 46, string:"false"; 47, if_statement; 47, 48; 47, 52; 48, not_operator; 48, 49; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:logging; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:headers; 57, string:"x-suppress-logging"; 58, string:"false"; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:params; 62, dictionary; 62, 63; 62, 68; 63, pair; 63, 64; 63, 65; 64, string:"auth-id"; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:auth_id; 68, pair; 68, 69; 68, 70; 69, string:"auth-token"; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:auth_token; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:url; 76, binary_operator:+; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:BASE_URL; 80, identifier:endpoint; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:rs; 84, generator_expression; 84, 85; 84, 110; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:grequests; 88, identifier:post; 89, argument_list; 89, 90; 89, 93; 89, 104; 89, 107; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:url; 92, identifier:url; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:data; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:json; 98, identifier:dumps; 99, argument_list; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:stringify; 102, argument_list; 102, 103; 103, identifier:data_chunk; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:params; 106, identifier:params; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:headers; 109, identifier:headers; 110, for_in_clause; 110, 111; 110, 112; 111, identifier:data_chunk; 112, call; 112, 113; 112, 114; 113, identifier:chunker; 114, argument_list; 114, 115; 114, 116; 115, identifier:data; 116, integer:100; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:responses; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:grequests; 123, identifier:imap; 124, argument_list; 124, 125; 124, 126; 125, identifier:rs; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:size; 128, identifier:parallelism; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:status_codes; 132, dictionary; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:addresses; 136, call; 136, 137; 136, 138; 137, identifier:AddressCollection; 138, argument_list; 138, 139; 139, list:[]; 140, for_statement; 140, 141; 140, 142; 140, 143; 141, identifier:response; 142, identifier:responses; 143, block; 143, 144; 143, 173; 144, if_statement; 144, 145; 144, 154; 144, 163; 145, comparison_operator:not; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:response; 148, identifier:status_code; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:status_codes; 152, identifier:keys; 153, argument_list; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 162; 157, subscript; 157, 158; 157, 159; 158, identifier:status_codes; 159, attribute; 159, 160; 159, 161; 160, identifier:response; 161, identifier:status_code; 162, integer:1; 163, else_clause; 163, 164; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, augmented_assignment:+=; 166, 167; 166, 172; 167, subscript; 167, 168; 167, 169; 168, identifier:status_codes; 169, attribute; 169, 170; 169, 171; 170, identifier:response; 171, identifier:status_code; 172, integer:1; 173, if_statement; 173, 174; 173, 179; 173, 197; 173, 198; 173, 199; 174, comparison_operator:==; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:response; 177, identifier:status_code; 178, integer:200; 179, block; 179, 180; 179, 196; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 188; 182, subscript; 182, 183; 182, 184; 183, identifier:addresses; 184, slice; 184, 185; 184, 186; 184, 187; 185, integer:0; 186, colon; 187, integer:0; 188, call; 188, 189; 188, 190; 189, identifier:AddressCollection; 190, argument_list; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:response; 194, identifier:json; 195, argument_list; 196, comment; 197, comment; 198, comment; 199, elif_clause; 199, 200; 199, 205; 200, comparison_operator:==; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:response; 203, identifier:status_code; 204, integer:401; 205, block; 205, 206; 206, raise_statement; 206, 207; 207, subscript; 207, 208; 207, 209; 208, identifier:ERROR_CODES; 209, integer:401; 210, comment; 211, if_statement; 211, 212; 211, 222; 212, comparison_operator:==; 212, 213; 212, 221; 213, call; 213, 214; 213, 215; 214, identifier:len; 215, argument_list; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:status_codes; 219, identifier:keys; 220, argument_list; 221, integer:1; 222, block; 222, 223; 223, if_statement; 223, 224; 223, 227; 223, 232; 224, comparison_operator:in; 224, 225; 224, 226; 225, integer:200; 226, identifier:status_codes; 227, block; 227, 228; 228, return_statement; 228, 229; 229, expression_list; 229, 230; 229, 231; 230, identifier:addresses; 231, identifier:status_codes; 232, else_clause; 232, 233; 233, block; 233, 234; 234, raise_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:ERROR_CODES; 238, identifier:get; 239, argument_list; 239, 240; 239, 247; 240, subscript; 240, 241; 240, 246; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:status_codes; 244, identifier:keys; 245, argument_list; 246, integer:0; 247, identifier:SmartyStreetsError; 248, comment; 249, comment; 250, comment; 251, comment; 252, comment; 253, return_statement; 253, 254; 254, expression_list; 254, 255; 254, 256; 255, identifier:addresses; 256, identifier:status_codes
def post(self, endpoint, data, parallelism=5): """ Executes most of the request. The parallelism parameter is useful to avoid swamping the API service with calls. Thus the entire set of requests won't be all made at once, but in chunked groups. :param endpoint: string indicating the URL component to call :param data: the JSON ready data to submit (list of dictionaries of addresses) :param parallelism: number of simultaneous requests to make. :return: a tuple of an AddressCollection and a dictionary of the response codes and the count for each. """ headers = { "Content-Type": "application/json", "Accept": "application/json", "x-standardize-only": "true" if self.standardize else "false", "x-include-invalid": "true" if self.invalid else "false", "x-accept-keypair": "true" if self.accept_keypair else "false", } if not self.logging: headers["x-suppress-logging"] = "false" params = {"auth-id": self.auth_id, "auth-token": self.auth_token} url = self.BASE_URL + endpoint rs = ( grequests.post( url=url, data=json.dumps(stringify(data_chunk)), params=params, headers=headers, ) for data_chunk in chunker(data, 100) ) responses = grequests.imap(rs, size=parallelism) status_codes = {} addresses = AddressCollection([]) for response in responses: if response.status_code not in status_codes.keys(): status_codes[response.status_code] = 1 else: status_codes[response.status_code] += 1 if response.status_code == 200: addresses[0:0] = AddressCollection( response.json() ) # Fast list insertion # If an auth error is raised, it's safe to say that this is # going to affect every request, so raise the exception immediately.. elif response.status_code == 401: raise ERROR_CODES[401] # The return value or exception is simple if it is consistent. if len(status_codes.keys()) == 1: if 200 in status_codes: return addresses, status_codes else: raise ERROR_CODES.get(status_codes.keys()[0], SmartyStreetsError) # For any other mix not really sure of the best way to handle it. If it's a mix of 200 # and error codes, then returning the resultant addresses and status code dictionary # seems pretty sensible. But if it's a mix of all error codes (could be a mix of payment # error, input error, potentially server error) this will probably require careful # checking in the code using this interface. return addresses, status_codes
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:get_contents_static; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:block_alias; 6, identifier:context; 7, block; 7, 8; 7, 10; 7, 18; 7, 26; 7, 27; 7, 71; 7, 77; 7, 84; 7, 93; 7, 276; 7, 282; 7, 293; 7, 302; 7, 312; 7, 333; 7, 337; 7, 393; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 14; 10, 15; 11, comparison_operator:not; 11, 12; 11, 13; 12, string:'request'; 13, identifier:context; 14, comment; 15, block; 15, 16; 16, return_statement; 16, 17; 17, string:''; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:current_url; 21, attribute; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:context; 24, string:'request'; 25, identifier:path; 26, comment; 27, try_statement; 27, 28; 27, 64; 28, block; 28, 29; 28, 36; 28, 40; 28, 54; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:resolver_match; 32, call; 32, 33; 32, 34; 33, identifier:resolve; 34, argument_list; 34, 35; 35, identifier:current_url; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:namespace; 39, string:''; 40, if_statement; 40, 41; 40, 44; 40, 45; 41, attribute; 41, 42; 41, 43; 42, identifier:resolver_match; 43, identifier:namespaces; 44, comment; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:namespace; 49, subscript; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:resolver_match; 52, identifier:namespaces; 53, integer:0; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:resolved_view_name; 57, binary_operator:%; 57, 58; 57, 59; 58, string:':%s:%s'; 59, tuple; 59, 60; 59, 61; 60, identifier:namespace; 61, attribute; 61, 62; 61, 63; 62, identifier:resolver_match; 63, identifier:url_name; 64, except_clause; 64, 65; 64, 66; 65, identifier:Resolver404; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:resolved_view_name; 70, None; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:_cache_init; 76, argument_list; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:cache_entry_name; 80, call; 80, 81; 80, 82; 81, identifier:cache_get_key; 82, argument_list; 82, 83; 83, identifier:block_alias; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:siteblocks_static; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:_cache_get; 91, argument_list; 91, 92; 92, identifier:cache_entry_name; 93, if_statement; 93, 94; 93, 96; 94, not_operator; 94, 95; 95, identifier:siteblocks_static; 96, block; 96, 97; 96, 119; 96, 131; 96, 268; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:blocks; 100, call; 100, 101; 100, 116; 101, attribute; 101, 102; 101, 115; 102, call; 102, 103; 102, 108; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:Block; 106, identifier:objects; 107, identifier:filter; 108, argument_list; 108, 109; 108, 112; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:alias; 111, identifier:block_alias; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:hidden; 114, False; 115, identifier:only; 116, argument_list; 116, 117; 116, 118; 117, string:'url'; 118, string:'contents'; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:siteblocks_static; 122, list:[defaultdict(list), defaultdict(list)]; 122, 123; 122, 127; 123, call; 123, 124; 123, 125; 124, identifier:defaultdict; 125, argument_list; 125, 126; 126, identifier:list; 127, call; 127, 128; 127, 129; 128, identifier:defaultdict; 129, argument_list; 129, 130; 130, identifier:list; 131, for_statement; 131, 132; 131, 133; 131, 134; 132, identifier:block; 133, identifier:blocks; 134, block; 134, 135; 134, 196; 135, if_statement; 135, 136; 135, 141; 135, 148; 135, 181; 136, comparison_operator:==; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:block; 139, identifier:url; 140, string:'*'; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:url_re; 145, attribute; 145, 146; 145, 147; 146, identifier:block; 147, identifier:url; 148, elif_clause; 148, 149; 148, 157; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:block; 153, identifier:url; 154, identifier:startswith; 155, argument_list; 155, 156; 156, string:':'; 157, block; 157, 158; 157, 164; 157, 165; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:url_re; 161, attribute; 161, 162; 161, 163; 162, identifier:block; 163, identifier:url; 164, comment; 165, if_statement; 165, 166; 165, 174; 166, comparison_operator:==; 166, 167; 166, 173; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:url_re; 170, identifier:count; 171, argument_list; 171, 172; 172, string:':'; 173, integer:1; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:url_re; 178, binary_operator:%; 178, 179; 178, 180; 179, string:':%s'; 180, identifier:url_re; 181, else_clause; 181, 182; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:url_re; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:re; 189, identifier:compile; 190, argument_list; 190, 191; 191, binary_operator:%; 191, 192; 191, 193; 192, string:r'%s'; 193, attribute; 193, 194; 193, 195; 194, identifier:block; 195, identifier:url; 196, if_statement; 196, 197; 196, 200; 196, 216; 196, 236; 197, attribute; 197, 198; 197, 199; 198, identifier:block; 199, identifier:access_guest; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 212; 203, attribute; 203, 204; 203, 211; 204, subscript; 204, 205; 204, 210; 205, subscript; 205, 206; 205, 207; 206, identifier:siteblocks_static; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:IDX_GUEST; 210, identifier:url_re; 211, identifier:append; 212, argument_list; 212, 213; 213, attribute; 213, 214; 213, 215; 214, identifier:block; 215, identifier:contents; 216, elif_clause; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:block; 219, identifier:access_loggedin; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 232; 223, attribute; 223, 224; 223, 231; 224, subscript; 224, 225; 224, 230; 225, subscript; 225, 226; 225, 227; 226, identifier:siteblocks_static; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:IDX_AUTH; 230, identifier:url_re; 231, identifier:append; 232, argument_list; 232, 233; 233, attribute; 233, 234; 233, 235; 234, identifier:block; 235, identifier:contents; 236, else_clause; 236, 237; 237, block; 237, 238; 237, 253; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 249; 240, attribute; 240, 241; 240, 248; 241, subscript; 241, 242; 241, 247; 242, subscript; 242, 243; 242, 244; 243, identifier:siteblocks_static; 244, attribute; 244, 245; 244, 246; 245, identifier:self; 246, identifier:IDX_GUEST; 247, identifier:url_re; 248, identifier:append; 249, argument_list; 249, 250; 250, attribute; 250, 251; 250, 252; 251, identifier:block; 252, identifier:contents; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 264; 255, attribute; 255, 256; 255, 263; 256, subscript; 256, 257; 256, 262; 257, subscript; 257, 258; 257, 259; 258, identifier:siteblocks_static; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:IDX_AUTH; 262, identifier:url_re; 263, identifier:append; 264, argument_list; 264, 265; 265, attribute; 265, 266; 265, 267; 266, identifier:block; 267, identifier:contents; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:_cache_set; 273, argument_list; 273, 274; 273, 275; 274, identifier:cache_entry_name; 275, identifier:siteblocks_static; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:self; 280, identifier:_cache_save; 281, argument_list; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:user; 285, call; 285, 286; 285, 287; 286, identifier:getattr; 287, argument_list; 287, 288; 287, 291; 287, 292; 288, subscript; 288, 289; 288, 290; 289, identifier:context; 290, string:'request'; 291, string:'user'; 292, None; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:is_authenticated; 296, call; 296, 297; 296, 298; 297, identifier:getattr; 298, argument_list; 298, 299; 298, 300; 298, 301; 299, identifier:user; 300, string:'is_authenticated'; 301, False; 302, if_statement; 302, 303; 302, 305; 303, not_operator; 303, 304; 304, identifier:DJANGO_2; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 309; 308, identifier:is_authenticated; 309, call; 309, 310; 309, 311; 310, identifier:is_authenticated; 311, argument_list; 312, if_statement; 312, 313; 312, 314; 312, 323; 313, identifier:is_authenticated; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:lookup_area; 318, subscript; 318, 319; 318, 320; 319, identifier:siteblocks_static; 320, attribute; 320, 321; 320, 322; 321, identifier:self; 322, identifier:IDX_AUTH; 323, else_clause; 323, 324; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:lookup_area; 328, subscript; 328, 329; 328, 330; 329, identifier:siteblocks_static; 330, attribute; 330, 331; 330, 332; 331, identifier:self; 332, identifier:IDX_GUEST; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:static_block_contents; 336, string:''; 337, if_statement; 337, 338; 337, 341; 337, 351; 337, 365; 338, comparison_operator:in; 338, 339; 338, 340; 339, string:'*'; 340, identifier:lookup_area; 341, block; 341, 342; 342, expression_statement; 342, 343; 343, assignment; 343, 344; 343, 345; 344, identifier:static_block_contents; 345, call; 345, 346; 345, 347; 346, identifier:choice; 347, argument_list; 347, 348; 348, subscript; 348, 349; 348, 350; 349, identifier:lookup_area; 350, string:'*'; 351, elif_clause; 351, 352; 351, 355; 352, comparison_operator:in; 352, 353; 352, 354; 353, identifier:resolved_view_name; 354, identifier:lookup_area; 355, block; 355, 356; 356, expression_statement; 356, 357; 357, assignment; 357, 358; 357, 359; 358, identifier:static_block_contents; 359, call; 359, 360; 359, 361; 360, identifier:choice; 361, argument_list; 361, 362; 362, subscript; 362, 363; 362, 364; 363, identifier:lookup_area; 364, identifier:resolved_view_name; 365, else_clause; 365, 366; 366, block; 366, 367; 367, for_statement; 367, 368; 367, 371; 367, 376; 368, pattern_list; 368, 369; 368, 370; 369, identifier:url; 370, identifier:contents; 371, call; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:lookup_area; 374, identifier:items; 375, argument_list; 376, block; 376, 377; 377, if_statement; 377, 378; 377, 384; 378, call; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:url; 381, identifier:match; 382, argument_list; 382, 383; 383, identifier:current_url; 384, block; 384, 385; 384, 392; 385, expression_statement; 385, 386; 386, assignment; 386, 387; 386, 388; 387, identifier:static_block_contents; 388, call; 388, 389; 388, 390; 389, identifier:choice; 390, argument_list; 390, 391; 391, identifier:contents; 392, break_statement; 393, return_statement; 393, 394; 394, identifier:static_block_contents
def get_contents_static(self, block_alias, context): """Returns contents of a static block.""" if 'request' not in context: # No use in further actions as we won't ever know current URL. return '' current_url = context['request'].path # Resolve current view name to support view names as block URLs. try: resolver_match = resolve(current_url) namespace = '' if resolver_match.namespaces: # More than one namespace, really? Hmm. namespace = resolver_match.namespaces[0] resolved_view_name = ':%s:%s' % (namespace, resolver_match.url_name) except Resolver404: resolved_view_name = None self._cache_init() cache_entry_name = cache_get_key(block_alias) siteblocks_static = self._cache_get(cache_entry_name) if not siteblocks_static: blocks = Block.objects.filter(alias=block_alias, hidden=False).only('url', 'contents') siteblocks_static = [defaultdict(list), defaultdict(list)] for block in blocks: if block.url == '*': url_re = block.url elif block.url.startswith(':'): url_re = block.url # Normalize URL name to include namespace. if url_re.count(':') == 1: url_re = ':%s' % url_re else: url_re = re.compile(r'%s' % block.url) if block.access_guest: siteblocks_static[self.IDX_GUEST][url_re].append(block.contents) elif block.access_loggedin: siteblocks_static[self.IDX_AUTH][url_re].append(block.contents) else: siteblocks_static[self.IDX_GUEST][url_re].append(block.contents) siteblocks_static[self.IDX_AUTH][url_re].append(block.contents) self._cache_set(cache_entry_name, siteblocks_static) self._cache_save() user = getattr(context['request'], 'user', None) is_authenticated = getattr(user, 'is_authenticated', False) if not DJANGO_2: is_authenticated = is_authenticated() if is_authenticated: lookup_area = siteblocks_static[self.IDX_AUTH] else: lookup_area = siteblocks_static[self.IDX_GUEST] static_block_contents = '' if '*' in lookup_area: static_block_contents = choice(lookup_area['*']) elif resolved_view_name in lookup_area: static_block_contents = choice(lookup_area[resolved_view_name]) else: for url, contents in lookup_area.items(): if url.match(current_url): static_block_contents = choice(contents) break return static_block_contents
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:files_to_pif; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:files; 5, default_parameter; 5, 6; 5, 7; 6, identifier:verbose; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:quality_report; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:inline; 13, True; 14, block; 14, 15; 14, 17; 14, 18; 14, 22; 14, 47; 14, 56; 14, 75; 14, 76; 14, 82; 14, 92; 14, 93; 14, 113; 14, 114; 14, 127; 14, 128; 14, 132; 14, 189; 14, 190; 14, 196; 14, 327; 14, 328; 14, 343; 15, expression_statement; 15, 16; 16, string:'''Given a directory that contains output from a DFT calculation, parse the data and return a pif object Input: files - [str] list of files from which the parser is allowed to read. verbose - int, How much status messages to print Output: pif - ChemicalSystem, Results and settings of the DFT calculation in pif format '''; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:found_parser; 21, False; 22, for_statement; 22, 23; 22, 24; 22, 27; 23, identifier:possible_parser; 24, list:[PwscfParser, VaspParser]; 24, 25; 24, 26; 25, identifier:PwscfParser; 26, identifier:VaspParser; 27, block; 27, 28; 28, try_statement; 28, 29; 28, 42; 29, block; 29, 30; 29, 37; 29, 41; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:parser; 33, call; 33, 34; 33, 35; 34, identifier:possible_parser; 35, argument_list; 35, 36; 36, identifier:files; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:found_parser; 40, True; 41, break_statement; 42, except_clause; 42, 43; 42, 44; 42, 45; 43, identifier:InvalidIngesterException; 44, comment; 45, block; 45, 46; 46, pass_statement; 47, if_statement; 47, 48; 47, 50; 48, not_operator; 48, 49; 49, identifier:found_parser; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:Exception; 54, argument_list; 54, 55; 55, string:'Directory is not in correct format for an existing parser'; 56, if_statement; 56, 57; 56, 60; 57, comparison_operator:>; 57, 58; 57, 59; 58, identifier:verbose; 59, integer:0; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:print; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, string:"Found a {} directory"; 68, identifier:format; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:parser; 73, identifier:get_name; 74, argument_list; 75, comment; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:chem; 79, call; 79, 80; 79, 81; 80, identifier:ChemicalSystem; 81, argument_list; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:chem; 86, identifier:chemical_formula; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:parser; 90, identifier:get_composition; 91, argument_list; 92, comment; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:software; 96, call; 96, 97; 96, 98; 97, identifier:Software; 98, argument_list; 98, 99; 98, 106; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:name; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:parser; 104, identifier:get_name; 105, argument_list; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:version; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:parser; 111, identifier:get_version_number; 112, argument_list; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:method; 117, call; 117, 118; 117, 119; 118, identifier:Method; 119, argument_list; 119, 120; 119, 123; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:name; 122, string:'Density Functional Theory'; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:software; 125, list:[software]; 125, 126; 126, identifier:software; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:conditions; 131, list:[]; 132, for_statement; 132, 133; 132, 136; 132, 145; 132, 146; 133, pattern_list; 133, 134; 133, 135; 134, identifier:name; 135, identifier:func; 136, call; 136, 137; 136, 144; 137, attribute; 137, 138; 137, 143; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:parser; 141, identifier:get_setting_functions; 142, argument_list; 143, identifier:items; 144, argument_list; 145, comment; 146, block; 146, 147; 146, 157; 146, 158; 146, 164; 146, 174; 146, 175; 146, 181; 146, 182; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:cond; 150, call; 150, 151; 150, 156; 151, call; 151, 152; 151, 153; 152, identifier:getattr; 153, argument_list; 153, 154; 153, 155; 154, identifier:parser; 155, identifier:func; 156, argument_list; 157, comment; 158, if_statement; 158, 159; 158, 162; 159, comparison_operator:is; 159, 160; 159, 161; 160, identifier:cond; 161, None; 162, block; 162, 163; 163, continue_statement; 164, if_statement; 164, 165; 164, 172; 165, boolean_operator:and; 165, 166; 165, 167; 166, identifier:inline; 167, comparison_operator:is; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:cond; 170, identifier:files; 171, None; 172, block; 172, 173; 173, continue_statement; 174, comment; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:cond; 179, identifier:name; 180, identifier:name; 181, comment; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:conditions; 186, identifier:append; 187, argument_list; 187, 188; 188, identifier:cond; 189, comment; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:chem; 194, identifier:properties; 195, list:[]; 196, for_statement; 196, 197; 196, 200; 196, 209; 196, 210; 197, pattern_list; 197, 198; 197, 199; 198, identifier:name; 199, identifier:func; 200, call; 200, 201; 200, 208; 201, attribute; 201, 202; 201, 207; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:parser; 205, identifier:get_result_functions; 206, argument_list; 207, identifier:items; 208, argument_list; 209, comment; 210, block; 210, 211; 210, 221; 210, 222; 210, 228; 210, 238; 210, 239; 210, 245; 210, 252; 210, 258; 210, 274; 210, 317; 210, 318; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:prop; 214, call; 214, 215; 214, 220; 215, call; 215, 216; 215, 217; 216, identifier:getattr; 217, argument_list; 217, 218; 217, 219; 218, identifier:parser; 219, identifier:func; 220, argument_list; 221, comment; 222, if_statement; 222, 223; 222, 226; 223, comparison_operator:is; 223, 224; 223, 225; 224, identifier:prop; 225, None; 226, block; 226, 227; 227, continue_statement; 228, if_statement; 228, 229; 228, 236; 229, boolean_operator:and; 229, 230; 229, 231; 230, identifier:inline; 231, comparison_operator:is; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:prop; 234, identifier:files; 235, None; 236, block; 236, 237; 237, continue_statement; 238, comment; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:prop; 243, identifier:name; 244, identifier:name; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:prop; 249, identifier:methods; 250, list:[method,]; 250, 251; 251, identifier:method; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:prop; 256, identifier:data_type; 257, string:'COMPUTATIONAL'; 258, if_statement; 258, 259; 258, 268; 259, boolean_operator:and; 259, 260; 259, 263; 260, comparison_operator:>; 260, 261; 260, 262; 261, identifier:verbose; 262, integer:0; 263, call; 263, 264; 263, 265; 264, identifier:isinstance; 265, argument_list; 265, 266; 265, 267; 266, identifier:prop; 267, identifier:Value; 268, block; 268, 269; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:print; 272, argument_list; 272, 273; 273, identifier:name; 274, if_statement; 274, 275; 274, 280; 274, 287; 275, comparison_operator:is; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:prop; 278, identifier:conditions; 279, None; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:prop; 285, identifier:conditions; 286, identifier:conditions; 287, else_clause; 287, 288; 288, block; 288, 289; 288, 308; 289, if_statement; 289, 290; 289, 298; 290, not_operator; 290, 291; 291, call; 291, 292; 291, 293; 292, identifier:isinstance; 293, argument_list; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:prop; 296, identifier:conditions; 297, identifier:list; 298, block; 298, 299; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:prop; 303, identifier:conditions; 304, list:[prop.conditions]; 304, 305; 305, attribute; 305, 306; 305, 307; 306, identifier:prop; 307, identifier:conditions; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 315; 310, attribute; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:prop; 313, identifier:conditions; 314, identifier:extend; 315, argument_list; 315, 316; 316, identifier:conditions; 317, comment; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 325; 320, attribute; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:chem; 323, identifier:properties; 324, identifier:append; 325, argument_list; 325, 326; 326, identifier:prop; 327, comment; 328, if_statement; 328, 329; 328, 336; 329, boolean_operator:and; 329, 330; 329, 331; 330, identifier:quality_report; 331, call; 331, 332; 331, 333; 332, identifier:isinstance; 333, argument_list; 333, 334; 333, 335; 334, identifier:parser; 335, identifier:VaspParser; 336, block; 336, 337; 337, expression_statement; 337, 338; 338, call; 338, 339; 338, 340; 339, identifier:_add_quality_report; 340, argument_list; 340, 341; 340, 342; 341, identifier:parser; 342, identifier:chem; 343, return_statement; 343, 344; 344, identifier:chem
def files_to_pif(files, verbose=0, quality_report=True, inline=True): '''Given a directory that contains output from a DFT calculation, parse the data and return a pif object Input: files - [str] list of files from which the parser is allowed to read. verbose - int, How much status messages to print Output: pif - ChemicalSystem, Results and settings of the DFT calculation in pif format ''' # Look for the first parser compatible with the directory found_parser = False for possible_parser in [PwscfParser, VaspParser]: try: parser = possible_parser(files) found_parser = True break except InvalidIngesterException: # Constructors fail when they cannot find appropriate files pass if not found_parser: raise Exception('Directory is not in correct format for an existing parser') if verbose > 0: print("Found a {} directory".format(parser.get_name())) # Get information about the chemical system chem = ChemicalSystem() chem.chemical_formula = parser.get_composition() # Get software information, to list as method software = Software(name=parser.get_name(), version=parser.get_version_number()) # Define the DFT method object method = Method(name='Density Functional Theory', software=[software]) # Get the settings (aka. "conditions") of the DFT calculations conditions = [] for name, func in parser.get_setting_functions().items(): # Get the condition cond = getattr(parser, func)() # If the condition is None or False, skip it if cond is None: continue if inline and cond.files is not None: continue # Set the name cond.name = name # Set the types conditions.append(cond) # Get the properties of the system chem.properties = [] for name, func in parser.get_result_functions().items(): # Get the property prop = getattr(parser, func)() # If the property is None, skip it if prop is None: continue if inline and prop.files is not None: continue # Add name and other data prop.name = name prop.methods = [method,] prop.data_type='COMPUTATIONAL' if verbose > 0 and isinstance(prop, Value): print(name) if prop.conditions is None: prop.conditions = conditions else: if not isinstance(prop.conditions, list): prop.conditions = [prop.conditions] prop.conditions.extend(conditions) # Add it to the output chem.properties.append(prop) # Check to see if we should add the quality report if quality_report and isinstance(parser, VaspParser): _add_quality_report(parser, chem) return chem
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 1, 11; 2, function_name:_sort_cards; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:cards; 7, type; 7, 8; 8, identifier:Generator; 9, type; 9, 10; 10, identifier:list; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, string:'''sort cards by blocknum and blockseq'''; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:sorted; 17, argument_list; 17, 18; 17, 25; 18, list_comprehension; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:card; 21, identifier:__dict__; 22, for_in_clause; 22, 23; 22, 24; 23, identifier:card; 24, identifier:cards; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:key; 27, call; 27, 28; 27, 29; 28, identifier:itemgetter; 29, argument_list; 29, 30; 29, 31; 29, 32; 30, string:'blocknum'; 31, string:'blockseq'; 32, string:'cardseq'
def _sort_cards(self, cards: Generator) -> list: '''sort cards by blocknum and blockseq''' return sorted([card.__dict__ for card in cards], key=itemgetter('blocknum', 'blockseq', 'cardseq'))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_get_label; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, string:'''Find the label for the output files for this calculation '''; 8, if_statement; 8, 9; 8, 14; 8, 219; 8, 220; 8, 221; 8, 222; 8, 223; 8, 224; 9, comparison_operator:is; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:_label; 13, None; 14, block; 14, 15; 14, 19; 14, 205; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:foundfiles; 18, False; 19, for_statement; 19, 20; 19, 21; 19, 24; 20, identifier:f; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_files; 24, block; 24, 25; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:in; 26, 27; 26, 28; 27, string:".files"; 28, identifier:f; 29, block; 29, 30; 29, 34; 29, 47; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:foundfiles; 33, True; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:_label; 39, subscript; 39, 40; 39, 46; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:f; 43, identifier:split; 44, argument_list; 44, 45; 45, string:"."; 46, integer:0; 47, with_statement; 47, 48; 47, 62; 48, with_clause; 48, 49; 49, with_item; 49, 50; 50, as_pattern; 50, 51; 50, 60; 51, call; 51, 52; 51, 53; 52, identifier:open; 53, argument_list; 53, 54; 53, 59; 54, binary_operator:+; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:_label; 58, string:'.files'; 59, string:'r'; 60, as_pattern_target; 60, 61; 61, identifier:fp; 62, block; 62, 63; 62, 77; 62, 97; 62, 111; 62, 131; 62, 145; 62, 165; 62, 179; 62, 199; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:line; 66, subscript; 66, 67; 66, 76; 67, call; 67, 68; 67, 75; 68, attribute; 68, 69; 68, 74; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:fp; 72, identifier:readline; 73, argument_list; 74, identifier:split; 75, argument_list; 76, integer:0; 77, if_statement; 77, 78; 77, 85; 78, comparison_operator:!=; 78, 79; 78, 80; 79, identifier:line; 80, binary_operator:+; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_label; 84, string:".in"; 85, block; 85, 86; 85, 92; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:fp; 90, identifier:close; 91, argument_list; 92, raise_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:Exception; 95, argument_list; 95, 96; 96, string:'first line must be label.in'; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:line; 100, subscript; 100, 101; 100, 110; 101, call; 101, 102; 101, 109; 102, attribute; 102, 103; 102, 108; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:fp; 106, identifier:readline; 107, argument_list; 108, identifier:split; 109, argument_list; 110, integer:0; 111, if_statement; 111, 112; 111, 119; 112, comparison_operator:!=; 112, 113; 112, 114; 113, identifier:line; 114, binary_operator:+; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_label; 118, string:".txt"; 119, block; 119, 120; 119, 126; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:fp; 124, identifier:close; 125, argument_list; 126, raise_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:Exception; 129, argument_list; 129, 130; 130, string:'second line must be label.txt'; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:line; 134, subscript; 134, 135; 134, 144; 135, call; 135, 136; 135, 143; 136, attribute; 136, 137; 136, 142; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:fp; 140, identifier:readline; 141, argument_list; 142, identifier:split; 143, argument_list; 144, integer:0; 145, if_statement; 145, 146; 145, 153; 146, comparison_operator:!=; 146, 147; 146, 148; 147, identifier:line; 148, binary_operator:+; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:_label; 152, string:"i"; 153, block; 153, 154; 153, 160; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:fp; 158, identifier:close; 159, argument_list; 160, raise_statement; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:Exception; 163, argument_list; 163, 164; 164, string:'third line must be labeli'; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:line; 168, subscript; 168, 169; 168, 178; 169, call; 169, 170; 169, 177; 170, attribute; 170, 171; 170, 176; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:fp; 174, identifier:readline; 175, argument_list; 176, identifier:split; 177, argument_list; 178, integer:0; 179, if_statement; 179, 180; 179, 187; 180, comparison_operator:!=; 180, 181; 180, 182; 181, identifier:line; 182, binary_operator:+; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:_label; 186, string:"o"; 187, block; 187, 188; 187, 194; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:fp; 192, identifier:close; 193, argument_list; 194, raise_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:Exception; 197, argument_list; 197, 198; 198, string:'fourth line must be labelo'; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:fp; 203, identifier:close; 204, argument_list; 205, if_statement; 205, 206; 205, 207; 205, 212; 206, identifier:foundfiles; 207, block; 207, 208; 208, return_statement; 208, 209; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:_label; 212, else_clause; 212, 213; 213, block; 213, 214; 214, raise_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:Exception; 217, argument_list; 217, 218; 218, string:'label.files not found'; 219, comment; 220, comment; 221, comment; 222, comment; 223, comment; 224, else_clause; 224, 225; 225, block; 225, 226; 226, return_statement; 226, 227; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:_label
def _get_label(self): '''Find the label for the output files for this calculation ''' if self._label is None: foundfiles = False for f in self._files: if ".files" in f: foundfiles = True self._label = f.split(".")[0] with open(self._label + '.files', 'r') as fp: line = fp.readline().split()[0] if line != self._label + ".in": fp.close() raise Exception('first line must be label.in') line = fp.readline().split()[0] if line != self._label + ".txt": fp.close() raise Exception('second line must be label.txt') line = fp.readline().split()[0] if line != self._label + "i": fp.close() raise Exception('third line must be labeli') line = fp.readline().split()[0] if line != self._label + "o": fp.close() raise Exception('fourth line must be labelo') fp.close() if foundfiles: return self._label else: raise Exception('label.files not found') #ASE format # (self.prefix + '.in') # input # (self.prefix + '.txt')# output # (self.prefix + 'i') # input # (self.prefix + 'o') # output else: return self._label
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_estimate_progress; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 13; 5, 21; 5, 22; 5, 31; 5, 32; 5, 133; 5, 389; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:estimate; 11, True; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:current_subscript; 16, subscript; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_current_subscript_stage; 20, string:'current_subscript'; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:num_subscripts; 25, call; 25, 26; 25, 27; 26, identifier:len; 27, argument_list; 27, 28; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:scripts; 31, comment; 32, if_statement; 32, 33; 32, 38; 32, 47; 32, 122; 33, comparison_operator:==; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:iterator_type; 37, string:'loop'; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:num_iterations; 42, subscript; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:settings; 46, string:'num_loops'; 47, elif_clause; 47, 48; 47, 53; 48, comparison_operator:==; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:iterator_type; 52, string:'sweep'; 53, block; 53, 54; 53, 62; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:sweep_range; 57, subscript; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:settings; 61, string:'sweep_range'; 62, if_statement; 62, 63; 62, 70; 62, 94; 62, 109; 63, comparison_operator:==; 63, 64; 63, 69; 64, subscript; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:settings; 68, string:'stepping_mode'; 69, string:'value_step'; 70, block; 70, 71; 70, 91; 70, 92; 70, 93; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:num_iterations; 74, binary_operator:+; 74, 75; 74, 90; 75, call; 75, 76; 75, 77; 76, identifier:int; 77, argument_list; 77, 78; 78, binary_operator:/; 78, 79; 78, 87; 79, parenthesized_expression; 79, 80; 80, binary_operator:-; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:sweep_range; 83, string:'max_value'; 84, subscript; 84, 85; 84, 86; 85, identifier:sweep_range; 86, string:'min_value'; 87, subscript; 87, 88; 87, 89; 88, identifier:sweep_range; 89, string:'N/value_step'; 90, integer:1; 91, comment; 92, comment; 93, comment; 94, elif_clause; 94, 95; 94, 102; 95, comparison_operator:==; 95, 96; 95, 101; 96, subscript; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:settings; 100, string:'stepping_mode'; 101, string:'N'; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:num_iterations; 106, subscript; 106, 107; 106, 108; 107, identifier:sweep_range; 108, string:'N/value_step'; 109, else_clause; 109, 110; 110, block; 110, 111; 111, raise_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:KeyError; 114, argument_list; 114, 115; 115, binary_operator:+; 115, 116; 115, 117; 116, string:'unknown key'; 117, subscript; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:settings; 121, string:'stepping_mode'; 122, else_clause; 122, 123; 123, block; 123, 124; 123, 129; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:print; 127, argument_list; 127, 128; 128, string:'unknown iterator type in Iterator receive signal - can\'t estimate ramining time'; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:estimate; 132, False; 133, if_statement; 133, 134; 133, 135; 133, 136; 133, 382; 134, identifier:estimate; 135, comment; 136, block; 136, 137; 136, 143; 136, 366; 136, 367; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:loop_index; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:loop_index; 143, if_statement; 143, 144; 143, 147; 143, 148; 144, comparison_operator:>; 144, 145; 144, 146; 145, identifier:num_subscripts; 146, integer:1; 147, comment; 148, block; 148, 149; 148, 153; 148, 154; 148, 158; 148, 159; 148, 160; 148, 187; 148, 206; 148, 207; 148, 228; 148, 229; 148, 233; 148, 234; 148, 298; 148, 299; 148, 303; 148, 304; 148, 305; 148, 343; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:loop_execution_time; 152, float:0.; 153, comment; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:sub_progress_time; 157, float:0.; 158, comment; 159, comment; 160, if_statement; 160, 161; 160, 164; 160, 181; 161, comparison_operator:is; 161, 162; 161, 163; 162, identifier:current_subscript; 163, None; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:current_subscript_exec_duration; 168, call; 168, 169; 168, 180; 169, attribute; 169, 170; 169, 179; 170, subscript; 170, 171; 170, 176; 171, subscript; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:_current_subscript_stage; 175, string:'subscript_exec_duration'; 176, attribute; 176, 177; 176, 178; 177, identifier:current_subscript; 178, identifier:name; 179, identifier:total_seconds; 180, argument_list; 181, else_clause; 181, 182; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:current_subscript_exec_duration; 186, float:0.0; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:current_subscript_elapsed_time; 190, call; 190, 191; 190, 205; 191, attribute; 191, 192; 191, 204; 192, parenthesized_expression; 192, 193; 193, binary_operator:-; 193, 194; 193, 201; 194, call; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:datetime; 198, identifier:datetime; 199, identifier:now; 200, argument_list; 201, attribute; 201, 202; 201, 203; 202, identifier:current_subscript; 203, identifier:start_time; 204, identifier:total_seconds; 205, argument_list; 206, comment; 207, if_statement; 207, 208; 207, 211; 208, comparison_operator:==; 208, 209; 208, 210; 209, identifier:current_subscript_exec_duration; 210, float:0.0; 211, block; 211, 212; 211, 222; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:remaining_time; 215, call; 215, 216; 215, 221; 216, attribute; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:current_subscript; 219, identifier:remaining_time; 220, identifier:total_seconds; 221, argument_list; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:current_subscript_exec_duration; 225, binary_operator:+; 225, 226; 225, 227; 226, identifier:remaining_time; 227, identifier:current_subscript_elapsed_time; 228, comment; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:remaining_scripts; 232, integer:0; 233, comment; 234, for_statement; 234, 235; 234, 238; 234, 247; 235, pattern_list; 235, 236; 235, 237; 236, identifier:subscript_name; 237, identifier:duration; 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:self; 243, identifier:_current_subscript_stage; 244, string:'subscript_exec_duration'; 245, identifier:items; 246, argument_list; 247, block; 247, 248; 247, 261; 247, 269; 247, 270; 247, 271; 248, if_statement; 248, 249; 248, 256; 249, comparison_operator:==; 249, 250; 249, 255; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:duration; 253, identifier:total_seconds; 254, argument_list; 255, float:0.0; 256, block; 256, 257; 257, expression_statement; 257, 258; 258, augmented_assignment:+=; 258, 259; 258, 260; 259, identifier:remaining_scripts; 260, integer:1; 261, expression_statement; 261, 262; 262, augmented_assignment:+=; 262, 263; 262, 264; 263, identifier:loop_execution_time; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:duration; 267, identifier:total_seconds; 268, argument_list; 269, comment; 270, comment; 271, if_statement; 271, 272; 271, 288; 271, 289; 272, boolean_operator:and; 272, 273; 272, 282; 272, 283; 273, comparison_operator:==; 273, 274; 273, 281; 274, subscript; 274, 275; 274, 280; 275, subscript; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:self; 278, identifier:_current_subscript_stage; 279, string:'subscript_exec_count'; 280, identifier:subscript_name; 281, identifier:loop_index; 282, line_continuation:\; 283, comparison_operator:is; 283, 284; 283, 285; 284, identifier:subscript_name; 285, attribute; 285, 286; 285, 287; 286, identifier:current_subscript; 287, identifier:name; 288, comment; 289, block; 289, 290; 290, expression_statement; 290, 291; 291, augmented_assignment:+=; 291, 292; 291, 293; 292, identifier:sub_progress_time; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:duration; 296, identifier:total_seconds; 297, argument_list; 298, comment; 299, expression_statement; 299, 300; 300, augmented_assignment:+=; 300, 301; 300, 302; 301, identifier:sub_progress_time; 302, identifier:current_subscript_elapsed_time; 303, comment; 304, comment; 305, if_statement; 305, 306; 305, 309; 305, 310; 305, 317; 305, 333; 306, comparison_operator:==; 306, 307; 306, 308; 307, identifier:remaining_scripts; 308, identifier:num_subscripts; 309, comment; 310, block; 310, 311; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:loop_execution_time; 314, binary_operator:*; 314, 315; 314, 316; 315, identifier:num_subscripts; 316, identifier:current_subscript_exec_duration; 317, elif_clause; 317, 318; 317, 321; 318, comparison_operator:>; 318, 319; 318, 320; 319, identifier:remaining_scripts; 320, integer:1; 321, block; 321, 322; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 325; 324, identifier:loop_execution_time; 325, binary_operator:/; 325, 326; 325, 329; 326, binary_operator:*; 326, 327; 326, 328; 327, float:1.; 328, identifier:num_subscripts; 329, parenthesized_expression; 329, 330; 330, binary_operator:-; 330, 331; 330, 332; 331, identifier:num_subscripts; 332, identifier:remaining_scripts; 333, elif_clause; 333, 334; 333, 337; 333, 338; 334, comparison_operator:==; 334, 335; 334, 336; 335, identifier:remaining_scripts; 336, integer:1; 337, comment; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, augmented_assignment:+=; 340, 341; 340, 342; 341, identifier:loop_execution_time; 342, identifier:current_subscript_exec_duration; 343, if_statement; 343, 344; 343, 347; 343, 356; 344, comparison_operator:>; 344, 345; 344, 346; 345, identifier:loop_execution_time; 346, integer:0; 347, block; 347, 348; 348, expression_statement; 348, 349; 349, assignment; 349, 350; 349, 351; 350, identifier:progress_subscript; 351, binary_operator:/; 351, 352; 351, 355; 352, binary_operator:*; 352, 353; 352, 354; 353, float:100.; 354, identifier:sub_progress_time; 355, identifier:loop_execution_time; 356, else_clause; 356, 357; 357, block; 357, 358; 358, expression_statement; 358, 359; 359, assignment; 359, 360; 359, 361; 360, identifier:progress_subscript; 361, binary_operator:/; 361, 362; 361, 365; 362, binary_operator:*; 362, 363; 362, 364; 363, float:1.; 364, identifier:progress_subscript; 365, identifier:num_subscripts; 366, comment; 367, expression_statement; 367, 368; 368, assignment; 368, 369; 368, 370; 369, identifier:progress; 370, binary_operator:/; 370, 371; 370, 381; 371, binary_operator:*; 371, 372; 371, 373; 372, float:100.; 373, parenthesized_expression; 373, 374; 374, binary_operator:+; 374, 375; 374, 378; 375, binary_operator:-; 375, 376; 375, 377; 376, identifier:loop_index; 377, float:1.; 378, binary_operator:*; 378, 379; 378, 380; 379, float:0.01; 380, identifier:progress_subscript; 381, identifier:num_iterations; 382, else_clause; 382, 383; 382, 384; 383, comment; 384, block; 384, 385; 385, expression_statement; 385, 386; 386, assignment; 386, 387; 386, 388; 387, identifier:progress; 388, integer:50; 389, return_statement; 389, 390; 390, identifier:progress
def _estimate_progress(self): """ estimates the current progress that is then used in _receive_signal :return: current progress in percent """ estimate = True # ==== get the current subscript and the time it takes to execute it ===== current_subscript = self._current_subscript_stage['current_subscript'] # ==== get the number of subscripts ===== num_subscripts = len(self.scripts) # ==== get number of iterations and loop index ====================== if self.iterator_type == 'loop': num_iterations = self.settings['num_loops'] elif self.iterator_type == 'sweep': sweep_range = self.settings['sweep_range'] if self.settings['stepping_mode'] == 'value_step': num_iterations = int((sweep_range['max_value'] - sweep_range['min_value']) / sweep_range['N/value_step']) + 1 # len(np.linspace(sweep_range['min_value'], sweep_range['max_value'], # (sweep_range['max_value'] - sweep_range['min_value']) / # sweep_range['N/value_step'] + 1, endpoint=True).tolist()) elif self.settings['stepping_mode'] == 'N': num_iterations = sweep_range['N/value_step'] else: raise KeyError('unknown key' + self.settings['stepping_mode']) else: print('unknown iterator type in Iterator receive signal - can\'t estimate ramining time') estimate = False if estimate: # get number of loops (completed + 1) loop_index = self.loop_index if num_subscripts > 1: # estimate the progress based on the duration the individual subscripts loop_execution_time = 0. # time for a single loop execution in s sub_progress_time = 0. # progress of current loop iteration in s # ==== get typical duration of current subscript ====================== if current_subscript is not None: current_subscript_exec_duration = self._current_subscript_stage['subscript_exec_duration'][ current_subscript.name].total_seconds() else: current_subscript_exec_duration = 0.0 current_subscript_elapsed_time = (datetime.datetime.now() - current_subscript.start_time).total_seconds() # estimate the duration of the current subscript if the script hasn't been executed once fully and subscript_exec_duration is 0 if current_subscript_exec_duration == 0.0: remaining_time = current_subscript.remaining_time.total_seconds() current_subscript_exec_duration = remaining_time + current_subscript_elapsed_time # ==== get typical duration of one loop iteration ====================== remaining_scripts = 0 # script that remain to be executed for the first time for subscript_name, duration in self._current_subscript_stage['subscript_exec_duration'].items(): if duration.total_seconds() == 0.0: remaining_scripts += 1 loop_execution_time += duration.total_seconds() # add the times of the subscripts that have been executed in the current loop # ignore the current subscript, because that will be taken care of later if self._current_subscript_stage['subscript_exec_count'][subscript_name] == loop_index \ and subscript_name is not current_subscript.name: # this subscript has already been executed in this iteration sub_progress_time += duration.total_seconds() # add the proportional duration of the current subscript given by the subscript progress sub_progress_time += current_subscript_elapsed_time # if there are scripts that have not been executed yet # assume that all the scripts that have not been executed yet take as long as the average of the other scripts if remaining_scripts == num_subscripts: # none of the subscript has been finished. assume that all the scripts take as long as the first loop_execution_time = num_subscripts * current_subscript_exec_duration elif remaining_scripts > 1: loop_execution_time = 1. * num_subscripts / (num_subscripts - remaining_scripts) elif remaining_scripts == 1: # there is only one script left which is the current script loop_execution_time += current_subscript_exec_duration if loop_execution_time > 0: progress_subscript = 100. * sub_progress_time / loop_execution_time else: progress_subscript = 1. * progress_subscript / num_subscripts # print(' === script iterator progress estimation loop_index = {:d}/{:d}, progress_subscript = {:f}'.format(loop_index, number_of_iterations, progress_subscript)) progress = 100. * (loop_index - 1. + 0.01 * progress_subscript) / num_iterations else: # if can't estimate the remaining time set to half progress = 50 return progress
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_default_settings; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:sub_scripts; 5, identifier:script_order; 6, identifier:script_execution_freq; 7, identifier:iterator_type; 8, block; 8, 9; 8, 11; 8, 249; 8, 377; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 19; 12, function_name:populate_sweep_param; 13, parameters; 13, 14; 13, 15; 13, 16; 14, identifier:scripts; 15, identifier:parameter_list; 16, default_parameter; 16, 17; 16, 18; 17, identifier:trace; 18, string:''; 19, block; 19, 20; 19, 22; 19, 141; 19, 247; 20, expression_statement; 20, 21; 21, string:''' Args: scripts: a dict of {'class name': <class object>} pairs Returns: A list of all parameters of the input scripts '''; 22, function_definition; 22, 23; 22, 24; 22, 31; 23, function_name:get_parameter_from_dict; 24, parameters; 24, 25; 24, 26; 24, 27; 24, 28; 25, identifier:trace; 26, identifier:dic; 27, identifier:parameter_list; 28, default_parameter; 28, 29; 28, 30; 29, identifier:valid_values; 30, None; 31, block; 31, 32; 31, 34; 31, 51; 31, 139; 32, expression_statement; 32, 33; 33, comment; 34, if_statement; 34, 35; 34, 44; 35, boolean_operator:and; 35, 36; 35, 39; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:valid_values; 38, None; 39, call; 39, 40; 39, 41; 40, identifier:isinstance; 41, argument_list; 41, 42; 41, 43; 42, identifier:dic; 43, identifier:Parameter; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:valid_values; 48, attribute; 48, 49; 48, 50; 49, identifier:dic; 50, identifier:valid_values; 51, for_statement; 51, 52; 51, 55; 51, 60; 52, pattern_list; 52, 53; 52, 54; 53, identifier:key; 54, identifier:value; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:dic; 58, identifier:items; 59, argument_list; 60, block; 60, 61; 61, if_statement; 61, 62; 61, 67; 61, 68; 61, 87; 61, 128; 62, call; 62, 63; 62, 64; 63, identifier:isinstance; 64, argument_list; 64, 65; 64, 66; 65, identifier:value; 66, identifier:dict; 67, comment; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:parameter_list; 72, call; 72, 73; 72, 74; 73, identifier:get_parameter_from_dict; 74, argument_list; 74, 75; 74, 80; 74, 81; 74, 82; 75, binary_operator:+; 75, 76; 75, 79; 76, binary_operator:+; 76, 77; 76, 78; 77, identifier:trace; 78, string:'.'; 79, identifier:key; 80, identifier:value; 81, identifier:parameter_list; 82, subscript; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:dic; 85, identifier:valid_values; 86, identifier:key; 87, elif_clause; 87, 88; 87, 116; 88, boolean_operator:or; 88, 89; 88, 97; 88, 98; 89, parenthesized_expression; 89, 90; 90, comparison_operator:in; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:valid_values; 93, identifier:key; 94, tuple; 94, 95; 94, 96; 95, identifier:float; 96, identifier:int; 97, line_continuation:\; 98, parenthesized_expression; 98, 99; 99, boolean_operator:and; 99, 100; 99, 107; 100, call; 100, 101; 100, 102; 101, identifier:isinstance; 102, argument_list; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:valid_values; 105, identifier:key; 106, identifier:list; 107, comparison_operator:in; 107, 108; 107, 113; 108, subscript; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:valid_values; 111, identifier:key; 112, integer:0; 113, tuple; 113, 114; 113, 115; 114, identifier:float; 115, identifier:int; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:parameter_list; 121, identifier:append; 122, argument_list; 122, 123; 123, binary_operator:+; 123, 124; 123, 127; 124, binary_operator:+; 124, 125; 124, 126; 125, identifier:trace; 126, string:'.'; 127, identifier:key; 128, else_clause; 128, 129; 128, 130; 128, 131; 129, comment; 130, comment; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:print; 135, argument_list; 135, 136; 136, tuple; 136, 137; 136, 138; 137, string:'ignoring sweep parameter'; 138, identifier:key; 139, return_statement; 139, 140; 140, identifier:parameter_list; 141, for_statement; 141, 142; 141, 143; 141, 151; 142, identifier:script_name; 143, call; 143, 144; 143, 145; 144, identifier:list; 145, argument_list; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:scripts; 149, identifier:keys; 150, argument_list; 151, block; 151, 152; 151, 158; 151, 162; 151, 181; 152, import_from_statement; 152, 153; 152, 156; 153, dotted_name; 153, 154; 153, 155; 154, identifier:pylabcontrol; 155, identifier:core; 156, dotted_name; 156, 157; 157, identifier:ScriptIterator; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:script_trace; 161, identifier:trace; 162, if_statement; 162, 163; 162, 166; 162, 171; 163, comparison_operator:==; 163, 164; 163, 165; 164, identifier:script_trace; 165, string:''; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:script_trace; 170, identifier:script_name; 171, else_clause; 171, 172; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:script_trace; 176, binary_operator:+; 176, 177; 176, 180; 177, binary_operator:+; 177, 178; 177, 179; 178, identifier:script_trace; 179, string:'->'; 180, identifier:script_name; 181, if_statement; 181, 182; 181, 189; 181, 190; 181, 209; 182, call; 182, 183; 182, 184; 183, identifier:issubclass; 184, argument_list; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:scripts; 187, identifier:script_name; 188, identifier:ScriptIterator; 189, comment; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:populate_sweep_param; 194, argument_list; 194, 195; 194, 203; 194, 206; 195, subscript; 195, 196; 195, 202; 196, call; 196, 197; 196, 198; 197, identifier:vars; 198, argument_list; 198, 199; 199, subscript; 199, 200; 199, 201; 200, identifier:scripts; 201, identifier:script_name; 202, string:'_SCRIPTS'; 203, keyword_argument; 203, 204; 203, 205; 204, identifier:parameter_list; 205, identifier:parameter_list; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:trace; 208, identifier:script_trace; 209, else_clause; 209, 210; 209, 211; 210, comment; 211, block; 211, 212; 212, for_statement; 212, 213; 212, 214; 212, 215; 212, 237; 213, identifier:setting; 214, line_continuation:\; 215, subscript; 215, 216; 215, 236; 216, list_comprehension; 216, 217; 216, 220; 216, 230; 217, subscript; 217, 218; 217, 219; 218, identifier:elem; 219, integer:1; 220, for_in_clause; 220, 221; 220, 222; 221, identifier:elem; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:inspect; 225, identifier:getmembers; 226, argument_list; 226, 227; 227, subscript; 227, 228; 227, 229; 228, identifier:scripts; 229, identifier:script_name; 230, if_clause; 230, 231; 231, comparison_operator:==; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:elem; 234, integer:0; 235, string:'_DEFAULT_SETTINGS'; 236, integer:0; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:parameter_list; 241, call; 241, 242; 241, 243; 242, identifier:get_parameter_from_dict; 243, argument_list; 243, 244; 243, 245; 243, 246; 244, identifier:script_trace; 245, identifier:setting; 246, identifier:parameter_list; 247, return_statement; 247, 248; 248, identifier:parameter_list; 249, if_statement; 249, 250; 249, 253; 249, 282; 249, 360; 250, comparison_operator:==; 250, 251; 250, 252; 251, identifier:iterator_type; 252, string:'loop'; 253, block; 253, 254; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:script_default_settings; 257, list:[ Parameter('script_order', script_order), Parameter('script_execution_freq', script_execution_freq), Parameter('num_loops', 0, int, 'times the subscripts will be executed'), Parameter('run_all_first', True, bool, 'Run all scripts with nonzero frequency in first pass') ]; 257, 258; 257, 263; 257, 268; 257, 275; 258, call; 258, 259; 258, 260; 259, identifier:Parameter; 260, argument_list; 260, 261; 260, 262; 261, string:'script_order'; 262, identifier:script_order; 263, call; 263, 264; 263, 265; 264, identifier:Parameter; 265, argument_list; 265, 266; 265, 267; 266, string:'script_execution_freq'; 267, identifier:script_execution_freq; 268, call; 268, 269; 268, 270; 269, identifier:Parameter; 270, argument_list; 270, 271; 270, 272; 270, 273; 270, 274; 271, string:'num_loops'; 272, integer:0; 273, identifier:int; 274, string:'times the subscripts will be executed'; 275, call; 275, 276; 275, 277; 276, identifier:Parameter; 277, argument_list; 277, 278; 277, 279; 277, 280; 277, 281; 278, string:'run_all_first'; 279, True; 280, identifier:bool; 281, string:'Run all scripts with nonzero frequency in first pass'; 282, elif_clause; 282, 283; 282, 286; 283, comparison_operator:==; 283, 284; 283, 285; 284, identifier:iterator_type; 285, string:'sweep'; 286, block; 286, 287; 286, 295; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:sweep_params; 290, call; 290, 291; 290, 292; 291, identifier:populate_sweep_param; 292, argument_list; 292, 293; 292, 294; 293, identifier:sub_scripts; 294, list:[]; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:script_default_settings; 298, list:[ Parameter('script_order', script_order), Parameter('script_execution_freq', script_execution_freq), Parameter('sweep_param', sweep_params[0], sweep_params, 'variable over which to sweep'), Parameter('sweep_range', [Parameter('min_value', 0, float, 'min parameter value'), Parameter('max_value', 0, float, 'max parameter value'), Parameter('N/value_step', 0, float, 'either number of steps or parameter value step, depending on mode')]), Parameter('stepping_mode', 'N', ['N', 'value_step'], 'Switch between number of steps and step amount'), Parameter('run_all_first', True, bool, 'Run all scripts with nonzero frequency in first pass') ]; 298, 299; 298, 304; 298, 309; 298, 318; 298, 344; 298, 353; 299, call; 299, 300; 299, 301; 300, identifier:Parameter; 301, argument_list; 301, 302; 301, 303; 302, string:'script_order'; 303, identifier:script_order; 304, call; 304, 305; 304, 306; 305, identifier:Parameter; 306, argument_list; 306, 307; 306, 308; 307, string:'script_execution_freq'; 308, identifier:script_execution_freq; 309, call; 309, 310; 309, 311; 310, identifier:Parameter; 311, argument_list; 311, 312; 311, 313; 311, 316; 311, 317; 312, string:'sweep_param'; 313, subscript; 313, 314; 313, 315; 314, identifier:sweep_params; 315, integer:0; 316, identifier:sweep_params; 317, string:'variable over which to sweep'; 318, call; 318, 319; 318, 320; 319, identifier:Parameter; 320, argument_list; 320, 321; 320, 322; 321, string:'sweep_range'; 322, list:[Parameter('min_value', 0, float, 'min parameter value'), Parameter('max_value', 0, float, 'max parameter value'), Parameter('N/value_step', 0, float, 'either number of steps or parameter value step, depending on mode')]; 322, 323; 322, 330; 322, 337; 323, call; 323, 324; 323, 325; 324, identifier:Parameter; 325, argument_list; 325, 326; 325, 327; 325, 328; 325, 329; 326, string:'min_value'; 327, integer:0; 328, identifier:float; 329, string:'min parameter value'; 330, call; 330, 331; 330, 332; 331, identifier:Parameter; 332, argument_list; 332, 333; 332, 334; 332, 335; 332, 336; 333, string:'max_value'; 334, integer:0; 335, identifier:float; 336, string:'max parameter value'; 337, call; 337, 338; 337, 339; 338, identifier:Parameter; 339, argument_list; 339, 340; 339, 341; 339, 342; 339, 343; 340, string:'N/value_step'; 341, integer:0; 342, identifier:float; 343, string:'either number of steps or parameter value step, depending on mode'; 344, call; 344, 345; 344, 346; 345, identifier:Parameter; 346, argument_list; 346, 347; 346, 348; 346, 349; 346, 352; 347, string:'stepping_mode'; 348, string:'N'; 349, list:['N', 'value_step']; 349, 350; 349, 351; 350, string:'N'; 351, string:'value_step'; 352, string:'Switch between number of steps and step amount'; 353, call; 353, 354; 353, 355; 354, identifier:Parameter; 355, argument_list; 355, 356; 355, 357; 355, 358; 355, 359; 356, string:'run_all_first'; 357, True; 358, identifier:bool; 359, string:'Run all scripts with nonzero frequency in first pass'; 360, else_clause; 360, 361; 361, block; 361, 362; 361, 370; 362, expression_statement; 362, 363; 363, call; 363, 364; 363, 365; 364, identifier:print; 365, argument_list; 365, 366; 366, parenthesized_expression; 366, 367; 367, binary_operator:+; 367, 368; 367, 369; 368, string:'unknown iterator type '; 369, identifier:iterator_type; 370, raise_statement; 370, 371; 371, call; 371, 372; 371, 373; 372, identifier:TypeError; 373, argument_list; 373, 374; 374, binary_operator:+; 374, 375; 374, 376; 375, string:'unknown iterator type '; 376, identifier:iterator_type; 377, return_statement; 377, 378; 378, identifier:script_default_settings
def get_default_settings(sub_scripts, script_order, script_execution_freq, iterator_type): """ assigning the actual script settings depending on the iterator type this might be overwritten by classes that inherit form ScriptIterator Args: sub_scripts: dictionary with the subscripts script_order: execution order of subscripts script_execution_freq: execution frequency of subscripts Returns: the default setting for the iterator """ def populate_sweep_param(scripts, parameter_list, trace=''): ''' Args: scripts: a dict of {'class name': <class object>} pairs Returns: A list of all parameters of the input scripts ''' def get_parameter_from_dict(trace, dic, parameter_list, valid_values=None): """ appends keys in the dict to a list in the form trace.key.subkey.subsubkey... Args: trace: initial prefix (path through scripts and parameters to current location) dic: dictionary parameter_list: list to which append the parameters valid_values: valid values of dictionary values if None dic should be a dictionary Returns: """ if valid_values is None and isinstance(dic, Parameter): valid_values = dic.valid_values for key, value in dic.items(): if isinstance(value, dict): # for nested parameters ex {point: {'x': int, 'y': int}} parameter_list = get_parameter_from_dict(trace + '.' + key, value, parameter_list, dic.valid_values[key]) elif (valid_values[key] in (float, int)) or \ (isinstance(valid_values[key], list) and valid_values[key][0] in (float, int)): parameter_list.append(trace + '.' + key) else: # once down to the form {key: value} # in all other cases ignore parameter print(('ignoring sweep parameter', key)) return parameter_list for script_name in list(scripts.keys()): from pylabcontrol.core import ScriptIterator script_trace = trace if script_trace == '': script_trace = script_name else: script_trace = script_trace + '->' + script_name if issubclass(scripts[script_name], ScriptIterator): # gets subscripts of ScriptIterator objects populate_sweep_param(vars(scripts[script_name])['_SCRIPTS'], parameter_list=parameter_list, trace=script_trace) else: # use inspect instead of vars to get _DEFAULT_SETTINGS also for classes that inherit _DEFAULT_SETTINGS from a superclass for setting in \ [elem[1] for elem in inspect.getmembers(scripts[script_name]) if elem[0] == '_DEFAULT_SETTINGS'][0]: parameter_list = get_parameter_from_dict(script_trace, setting, parameter_list) return parameter_list if iterator_type == 'loop': script_default_settings = [ Parameter('script_order', script_order), Parameter('script_execution_freq', script_execution_freq), Parameter('num_loops', 0, int, 'times the subscripts will be executed'), Parameter('run_all_first', True, bool, 'Run all scripts with nonzero frequency in first pass') ] elif iterator_type == 'sweep': sweep_params = populate_sweep_param(sub_scripts, []) script_default_settings = [ Parameter('script_order', script_order), Parameter('script_execution_freq', script_execution_freq), Parameter('sweep_param', sweep_params[0], sweep_params, 'variable over which to sweep'), Parameter('sweep_range', [Parameter('min_value', 0, float, 'min parameter value'), Parameter('max_value', 0, float, 'max parameter value'), Parameter('N/value_step', 0, float, 'either number of steps or parameter value step, depending on mode')]), Parameter('stepping_mode', 'N', ['N', 'value_step'], 'Switch between number of steps and step amount'), Parameter('run_all_first', True, bool, 'Run all scripts with nonzero frequency in first pass') ] else: print(('unknown iterator type ' + iterator_type)) raise TypeError('unknown iterator type ' + iterator_type) return script_default_settings
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:consensus; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:aln; 5, default_parameter; 5, 6; 5, 7; 6, identifier:weights; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:gap_threshold; 10, float:0.5; 11, default_parameter; 11, 12; 11, 13; 12, identifier:simple; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:trim_ends; 16, True; 17, block; 17, 18; 17, 20; 17, 21; 17, 143; 17, 144; 17, 336; 18, expression_statement; 18, 19; 19, comment; 20, comment; 21, if_statement; 21, 22; 21, 23; 21, 24; 21, 62; 22, identifier:simple; 23, comment; 24, block; 24, 25; 24, 37; 24, 60; 24, 61; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:col_consensus; 28, call; 28, 29; 28, 30; 29, identifier:make_simple_col_consensus; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:alnutils; 34, identifier:aa_frequencies; 35, argument_list; 35, 36; 36, identifier:aln; 37, function_definition; 37, 38; 37, 39; 37, 41; 38, function_name:is_majority_gap; 39, parameters; 39, 40; 40, identifier:col; 41, block; 41, 42; 42, return_statement; 42, 43; 43, parenthesized_expression; 43, 44; 44, comparison_operator:>=; 44, 45; 44, 59; 45, binary_operator:/; 45, 46; 45, 55; 46, call; 46, 47; 46, 48; 47, identifier:float; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:col; 52, identifier:count; 53, argument_list; 53, 54; 54, string:'-'; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, identifier:col; 59, identifier:gap_threshold; 60, comment; 61, comment; 62, else_clause; 62, 63; 62, 64; 63, comment; 64, block; 64, 65; 64, 86; 64, 98; 64, 105; 65, if_statement; 65, 66; 65, 69; 65, 80; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:weights; 68, None; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:seq_weights; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:alnutils; 76, identifier:sequence_weights; 77, argument_list; 77, 78; 77, 79; 78, identifier:aln; 79, string:'avg1'; 80, else_clause; 80, 81; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:seq_weights; 85, identifier:weights; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:aa_frequencies; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:alnutils; 92, identifier:aa_frequencies; 93, argument_list; 93, 94; 93, 95; 94, identifier:aln; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:weights; 97, identifier:seq_weights; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:col_consensus; 101, call; 101, 102; 101, 103; 102, identifier:make_entropy_col_consensus; 103, argument_list; 103, 104; 104, identifier:aa_frequencies; 105, function_definition; 105, 106; 105, 107; 105, 109; 106, function_name:is_majority_gap; 107, parameters; 107, 108; 108, identifier:col; 109, block; 109, 110; 109, 114; 109, 133; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:gap_count; 113, float:0.0; 114, for_statement; 114, 115; 114, 118; 114, 123; 115, pattern_list; 115, 116; 115, 117; 116, identifier:wt; 117, identifier:char; 118, call; 118, 119; 118, 120; 119, identifier:zip; 120, argument_list; 120, 121; 120, 122; 121, identifier:seq_weights; 122, identifier:col; 123, block; 123, 124; 124, if_statement; 124, 125; 124, 128; 125, comparison_operator:==; 125, 126; 125, 127; 126, identifier:char; 127, string:'-'; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, augmented_assignment:+=; 130, 131; 130, 132; 131, identifier:gap_count; 132, identifier:wt; 133, return_statement; 133, 134; 134, parenthesized_expression; 134, 135; 135, comparison_operator:>=; 135, 136; 135, 142; 136, binary_operator:/; 136, 137; 136, 138; 137, identifier:gap_count; 138, call; 138, 139; 138, 140; 139, identifier:sum; 140, argument_list; 140, 141; 141, identifier:seq_weights; 142, identifier:gap_threshold; 143, comment; 144, function_definition; 144, 145; 144, 146; 144, 148; 145, function_name:col_wise_consensus; 146, parameters; 146, 147; 147, identifier:columns; 148, block; 148, 149; 148, 151; 148, 164; 148, 165; 148, 166; 148, 324; 148, 325; 149, expression_statement; 149, 150; 150, comment; 151, if_statement; 151, 152; 151, 154; 151, 155; 152, not_operator; 152, 153; 153, identifier:trim_ends; 154, comment; 155, block; 155, 156; 155, 160; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:in_left_end; 159, True; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:maybe_right_tail; 163, list:[]; 164, comment; 165, comment; 166, for_statement; 166, 167; 166, 168; 166, 169; 166, 170; 167, identifier:col; 168, identifier:columns; 169, comment; 170, block; 170, 171; 170, 192; 170, 231; 170, 232; 170, 239; 170, 260; 170, 261; 170, 262; 170, 263; 170, 264; 170, 265; 170, 266; 170, 267; 170, 268; 170, 269; 170, 278; 170, 279; 170, 286; 170, 322; 170, 323; 171, if_statement; 171, 172; 171, 187; 172, call; 172, 173; 172, 174; 173, identifier:all; 174, generator_expression; 174, 175; 174, 180; 174, 183; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:c; 178, identifier:islower; 179, argument_list; 180, for_in_clause; 180, 181; 180, 182; 181, identifier:c; 182, identifier:col; 183, if_clause; 183, 184; 184, comparison_operator:not; 184, 185; 184, 186; 185, identifier:c; 186, string:'.-'; 187, block; 187, 188; 187, 191; 188, expression_statement; 188, 189; 189, yield; 189, 190; 190, string:'-'; 191, continue_statement; 192, if_statement; 192, 193; 192, 204; 193, call; 193, 194; 193, 195; 194, identifier:any; 195, generator_expression; 195, 196; 195, 201; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:c; 199, identifier:islower; 200, argument_list; 201, for_in_clause; 201, 202; 201, 203; 202, identifier:c; 203, identifier:col; 204, block; 204, 205; 204, 221; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:logging; 209, identifier:warn; 210, argument_list; 210, 211; 211, binary_operator:+; 211, 212; 211, 215; 212, concatenated_string; 212, 213; 212, 214; 213, string:'Mixed lowercase and uppercase letters in a '; 214, string:'column: '; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, string:''; 218, identifier:join; 219, argument_list; 219, 220; 220, identifier:col; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:col; 224, call; 224, 225; 224, 226; 225, identifier:map; 226, argument_list; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:str; 229, identifier:upper; 230, identifier:col; 231, comment; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:is_gap; 235, call; 235, 236; 235, 237; 236, identifier:is_majority_gap; 237, argument_list; 237, 238; 238, identifier:col; 239, if_statement; 239, 240; 239, 242; 239, 243; 240, not_operator; 240, 241; 241, identifier:trim_ends; 242, comment; 243, block; 243, 244; 244, if_statement; 244, 245; 244, 246; 245, identifier:in_left_end; 246, block; 246, 247; 246, 256; 247, if_statement; 247, 248; 247, 250; 247, 251; 248, not_operator; 248, 249; 249, identifier:is_gap; 250, comment; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:in_left_end; 255, False; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:is_gap; 259, False; 260, comment; 261, comment; 262, comment; 263, comment; 264, comment; 265, comment; 266, comment; 267, comment; 268, comment; 269, if_statement; 269, 270; 269, 273; 270, boolean_operator:and; 270, 271; 270, 272; 271, identifier:is_gap; 272, identifier:trim_ends; 273, block; 273, 274; 273, 277; 274, expression_statement; 274, 275; 275, yield; 275, 276; 276, string:'-'; 277, continue_statement; 278, comment; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:cons_char; 282, call; 282, 283; 282, 284; 283, identifier:col_consensus; 284, argument_list; 284, 285; 285, identifier:col; 286, if_statement; 286, 287; 286, 288; 286, 292; 287, identifier:trim_ends; 288, block; 288, 289; 289, expression_statement; 289, 290; 290, yield; 290, 291; 291, identifier:cons_char; 292, else_clause; 292, 293; 292, 294; 293, comment; 294, block; 294, 295; 295, if_statement; 295, 296; 295, 297; 295, 305; 296, identifier:is_gap; 297, block; 297, 298; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:maybe_right_tail; 302, identifier:append; 303, argument_list; 303, 304; 304, identifier:cons_char; 305, else_clause; 305, 306; 305, 307; 306, comment; 307, block; 307, 308; 307, 315; 307, 319; 308, for_statement; 308, 309; 308, 310; 308, 311; 309, identifier:char; 310, identifier:maybe_right_tail; 311, block; 311, 312; 312, expression_statement; 312, 313; 313, yield; 313, 314; 314, string:'-'; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:maybe_right_tail; 318, list:[]; 319, expression_statement; 319, 320; 320, yield; 320, 321; 321, identifier:cons_char; 322, comment; 323, comment; 324, comment; 325, if_statement; 325, 326; 325, 328; 326, not_operator; 326, 327; 327, identifier:trim_ends; 328, block; 328, 329; 329, for_statement; 329, 330; 329, 331; 329, 332; 330, identifier:char; 331, identifier:maybe_right_tail; 332, block; 332, 333; 333, expression_statement; 333, 334; 334, yield; 334, 335; 335, identifier:char; 336, return_statement; 336, 337; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, string:''; 340, identifier:join; 341, argument_list; 341, 342; 342, call; 342, 343; 342, 344; 343, identifier:col_wise_consensus; 344, argument_list; 344, 345; 345, call; 345, 346; 345, 347; 346, identifier:zip; 347, argument_list; 347, 348; 348, list_splat; 348, 349; 349, identifier:aln
def consensus(aln, weights=None, gap_threshold=0.5, simple=False, trim_ends=True): """Get the consensus of an alignment, as a string. Emit gap characters for majority-gap columns; apply various strategies to choose the consensus amino acid type for the remaining columns. Parameters ---------- simple : bool If True, use simple plurality to determine the consensus amino acid type, without weighting sequences for similarity. Otherwise, weight sequences for similarity and use relative entropy to choose the consensus amino acid type. weights : dict or None Sequence weights. If given, used to calculate amino acid frequencies; otherwise calculated within this function (i.e. this is a way to speed up the function if sequence weights have already been calculated). Ignored in 'simple' mode. trim_ends : bool If False, stretch the consensus sequence to include the N- and C-tails of the alignment, even if those flanking columns are mostly gap characters. This avoids terminal gaps in the consensus (needed for MAPGAPS). gap_threshold : float If the proportion of gap characters in a column is greater than or equal to this value (after sequence weighting, if applicable), then the consensus character emitted will be a gap instead of an amino acid type. """ # Choose your algorithms! if simple: # Use the simple, unweighted algorithm col_consensus = make_simple_col_consensus(alnutils.aa_frequencies(aln)) def is_majority_gap(col): return (float(col.count('-')) / len(col) >= gap_threshold) # ENH (alternatively/additionally): does any aa occur more than once? # ENH: choose gap-decisionmaking separately from col_consensus else: # Use the entropy-based, weighted algorithm if weights is None: seq_weights = alnutils.sequence_weights(aln, 'avg1') else: seq_weights = weights aa_frequencies = alnutils.aa_frequencies(aln, weights=seq_weights) col_consensus = make_entropy_col_consensus(aa_frequencies) def is_majority_gap(col): gap_count = 0.0 for wt, char in zip(seq_weights, col): if char == '-': gap_count += wt return (gap_count / sum(seq_weights) >= gap_threshold) # Traverse the alignment, handling gaps etc. def col_wise_consensus(columns): """Calculate the consensus chars for an iterable of columns.""" if not trim_ends: # Track if we're in the N-term or C-term end of the sequence in_left_end = True maybe_right_tail = [] # prev_col = None # prev_char = None for col in columns: # Lowercase cols mean explicitly, "don't include in consensus" if all(c.islower() for c in col if c not in '.-'): yield '-' continue if any(c.islower() for c in col): logging.warn('Mixed lowercase and uppercase letters in a ' 'column: ' + ''.join(col)) col = map(str.upper, col) # Gap chars is_gap = is_majority_gap(col) if not trim_ends: # Avoid N-terminal gaps in the consensus sequence if in_left_end: if not is_gap: # Match -- we're no longer in the left end in_left_end = False is_gap = False # When to yield a gap here: # ----------- --------- ------ ---------- # in_left_end trim_ends is_gap yield gap? # ----------- --------- ------ ---------- # True True (True) yes # True False (False) (no -- def. char) # False True T/F yes, if is_gap # False False (T/F) NO! use maybe_right_tail # ----------- --------- ------ ---------- if is_gap and trim_ends: yield '-' continue # Get the consensus character, using the chosen algorithm cons_char = col_consensus(col) if trim_ends: yield cons_char else: # Avoid C-terminal gaps in the consensus sequence if is_gap: maybe_right_tail.append(cons_char) else: # Match -> gaps weren't the right tail; emit all gaps for char in maybe_right_tail: yield '-' maybe_right_tail = [] yield cons_char # prev_col = col # prev_char = cons_char # Finally, if we were keeping a right (C-term) tail, emit it if not trim_ends: for char in maybe_right_tail: yield char return ''.join(col_wise_consensus(zip(*aln)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:make_simple_col_consensus; 3, parameters; 3, 4; 4, identifier:bg_freqs; 5, block; 5, 6; 5, 8; 5, 9; 5, 203; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 19; 9, 20; 10, function_name:col_consensus; 11, parameters; 11, 12; 11, 13; 11, 16; 12, identifier:col; 13, default_parameter; 13, 14; 13, 15; 14, identifier:prev_col; 15, list:[]; 16, default_parameter; 16, 17; 16, 18; 17, identifier:prev_char; 18, list:[]; 19, comment; 20, block; 20, 21; 20, 30; 20, 33; 20, 34; 20, 55; 20, 56; 20, 70; 20, 186; 20, 187; 20, 194; 20, 201; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:aa_counts; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:sequtils; 27, identifier:aa_frequencies; 28, argument_list; 28, 29; 29, identifier:col; 30, assert_statement; 30, 31; 30, 32; 31, identifier:aa_counts; 32, string:"Column is all gaps! That's not allowed."; 33, comment; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 39; 36, pattern_list; 36, 37; 36, 38; 37, identifier:best_char; 38, identifier:best_score; 39, call; 39, 40; 39, 41; 40, identifier:max; 41, argument_list; 41, 42; 41, 47; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:aa_counts; 45, identifier:iteritems; 46, argument_list; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:key; 49, lambda; 49, 50; 49, 52; 50, lambda_parameters; 50, 51; 51, identifier:kv; 52, subscript; 52, 53; 52, 54; 53, identifier:kv; 54, integer:1; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:ties; 59, list_comprehension; 59, 60; 59, 61; 59, 64; 60, identifier:aa; 61, for_in_clause; 61, 62; 61, 63; 62, identifier:aa; 63, identifier:aa_counts; 64, if_clause; 64, 65; 65, comparison_operator:==; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:aa_counts; 68, identifier:aa; 69, identifier:best_score; 70, if_statement; 70, 71; 70, 77; 70, 78; 70, 79; 70, 80; 70, 173; 71, comparison_operator:>; 71, 72; 71, 76; 72, call; 72, 73; 72, 74; 73, identifier:len; 74, argument_list; 74, 75; 75, identifier:ties; 76, integer:1; 77, comment; 78, comment; 79, comment; 80, block; 80, 81; 80, 144; 80, 167; 81, if_statement; 81, 82; 81, 85; 82, boolean_operator:and; 82, 83; 82, 84; 83, identifier:prev_char; 84, identifier:prev_col; 85, block; 85, 86; 85, 117; 85, 137; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:mc_next; 89, call; 89, 90; 89, 116; 90, attribute; 90, 91; 90, 115; 91, call; 91, 92; 91, 93; 92, identifier:Counter; 93, argument_list; 93, 94; 94, list_comprehension; 94, 95; 94, 96; 94, 105; 95, identifier:b; 96, for_in_clause; 96, 97; 96, 100; 97, pattern_list; 97, 98; 97, 99; 98, identifier:a; 99, identifier:b; 100, call; 100, 101; 100, 102; 101, identifier:zip; 102, argument_list; 102, 103; 102, 104; 103, identifier:prev_col; 104, identifier:col; 105, if_clause; 105, 106; 106, boolean_operator:and; 106, 107; 106, 112; 107, comparison_operator:==; 107, 108; 107, 109; 108, identifier:a; 109, subscript; 109, 110; 109, 111; 110, identifier:prev_char; 111, integer:0; 112, comparison_operator:in; 112, 113; 112, 114; 113, identifier:b; 114, identifier:ties; 115, identifier:most_common; 116, argument_list; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:ties_next; 120, list_comprehension; 120, 121; 120, 124; 120, 127; 121, subscript; 121, 122; 121, 123; 122, identifier:x; 123, integer:0; 124, for_in_clause; 124, 125; 124, 126; 125, identifier:x; 126, identifier:mc_next; 127, if_clause; 127, 128; 128, comparison_operator:==; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:x; 131, integer:1; 132, subscript; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:mc_next; 135, integer:0; 136, integer:1; 137, if_statement; 137, 138; 137, 139; 138, identifier:ties_next; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:ties; 143, identifier:ties_next; 144, if_statement; 144, 145; 144, 151; 144, 152; 145, comparison_operator:>; 145, 146; 145, 150; 146, call; 146, 147; 146, 148; 147, identifier:len; 148, argument_list; 148, 149; 149, identifier:ties; 150, integer:1; 151, comment; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:ties; 157, identifier:sort; 158, argument_list; 158, 159; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:key; 161, lambda; 161, 162; 161, 164; 162, lambda_parameters; 162, 163; 163, identifier:aa; 164, subscript; 164, 165; 164, 166; 165, identifier:bg_freqs; 166, identifier:aa; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:best_char; 170, subscript; 170, 171; 170, 172; 171, identifier:ties; 172, integer:0; 173, else_clause; 173, 174; 174, block; 174, 175; 175, assert_statement; 175, 176; 175, 181; 176, comparison_operator:==; 176, 177; 176, 178; 177, identifier:best_char; 178, subscript; 178, 179; 178, 180; 179, identifier:ties; 180, integer:0; 181, binary_operator:%; 181, 182; 181, 183; 182, string:'WTF %s != %s[0]'; 183, tuple; 183, 184; 183, 185; 184, identifier:best_char; 185, identifier:ties; 186, comment; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 193; 189, subscript; 189, 190; 189, 191; 190, identifier:prev_col; 191, slice; 191, 192; 192, colon; 193, identifier:col; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 200; 196, subscript; 196, 197; 196, 198; 197, identifier:prev_char; 198, slice; 198, 199; 199, colon; 200, identifier:best_char; 201, return_statement; 201, 202; 202, identifier:best_char; 203, return_statement; 203, 204; 204, identifier:col_consensus
def make_simple_col_consensus(bg_freqs): """Consensus by simple plurality, unweighted. Resolves ties by two heuristics: 1. Prefer the aa that follows the preceding consensus aa type most often in the original sequences. 2. Finally, prefer the less-common aa type. """ # Hack: use default kwargs to persist across iterations def col_consensus(col, prev_col=[], prev_char=[]): # Count the amino acid types in this column aa_counts = sequtils.aa_frequencies(col) assert aa_counts, "Column is all gaps! That's not allowed." # Take the most common residue(s) best_char, best_score = max(aa_counts.iteritems(), key=lambda kv: kv[1]) # Resolve ties ties = [aa for aa in aa_counts if aa_counts[aa] == best_score] if len(ties) > 1: # Breaker #1: most common after the prev. consensus char # Resolve a tied col by restricting to rows where the preceding # char is the consensus type for that (preceding) col if prev_char and prev_col: mc_next = Counter( [b for a, b in zip(prev_col, col) if a == prev_char[0] and b in ties] ).most_common() ties_next = [x[0] for x in mc_next if x[1] == mc_next[0][1]] if ties_next: ties = ties_next if len(ties) > 1: # Breaker #2: lowest overall residue frequency ties.sort(key=lambda aa: bg_freqs[aa]) best_char = ties[0] else: assert best_char == ties[0], \ 'WTF %s != %s[0]' % (best_char, ties) # Save values for tie-breaker #1 prev_col[:] = col prev_char[:] = best_char return best_char return col_consensus
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:supported; 3, parameters; 3, 4; 4, identifier:aln; 5, block; 5, 6; 5, 8; 5, 202; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 12; 9, function_name:col_consensus; 10, parameters; 10, 11; 11, identifier:columns; 12, block; 12, 13; 12, 15; 13, expression_statement; 13, 14; 14, comment; 15, for_statement; 15, 16; 15, 17; 15, 18; 16, identifier:col; 17, identifier:columns; 18, block; 18, 19; 18, 58; 18, 59; 18, 98; 18, 99; 18, 118; 18, 130; 19, if_statement; 19, 20; 19, 53; 20, parenthesized_expression; 20, 21; 20, 22; 21, comment; 22, boolean_operator:or; 22, 23; 22, 37; 22, 38; 23, parenthesized_expression; 23, 24; 24, comparison_operator:>=; 24, 25; 24, 31; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:col; 28, identifier:count; 29, argument_list; 29, 30; 30, string:'-'; 31, binary_operator:/; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:len; 34, argument_list; 34, 35; 35, identifier:col; 36, integer:2; 37, comment; 38, call; 38, 39; 38, 40; 39, identifier:all; 40, generator_expression; 40, 41; 40, 46; 40, 49; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:c; 44, identifier:islower; 45, argument_list; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:c; 48, identifier:col; 49, if_clause; 49, 50; 50, comparison_operator:not; 50, 51; 50, 52; 51, identifier:c; 52, string:'.-'; 53, block; 53, 54; 53, 57; 54, expression_statement; 54, 55; 55, yield; 55, 56; 56, string:'-'; 57, continue_statement; 58, comment; 59, if_statement; 59, 60; 59, 71; 60, call; 60, 61; 60, 62; 61, identifier:any; 62, generator_expression; 62, 63; 62, 68; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:c; 66, identifier:islower; 67, argument_list; 68, for_in_clause; 68, 69; 68, 70; 69, identifier:c; 70, identifier:col; 71, block; 71, 72; 71, 88; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:logging; 76, identifier:warn; 77, argument_list; 77, 78; 78, binary_operator:+; 78, 79; 78, 82; 79, concatenated_string; 79, 80; 79, 81; 80, string:'Mixed lowercase and uppercase letters in a '; 81, string:'column: '; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, string:''; 85, identifier:join; 86, argument_list; 86, 87; 87, identifier:col; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:col; 91, call; 91, 92; 91, 93; 92, identifier:map; 93, argument_list; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:str; 96, identifier:upper; 97, identifier:col; 98, comment; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:most_common; 102, call; 102, 103; 102, 117; 103, attribute; 103, 104; 103, 116; 104, call; 104, 105; 104, 106; 105, identifier:Counter; 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:col; 112, if_clause; 112, 113; 113, comparison_operator:not; 113, 114; 113, 115; 114, identifier:c; 115, string:'-'; 116, identifier:most_common; 117, argument_list; 118, if_statement; 118, 119; 118, 121; 118, 122; 119, not_operator; 119, 120; 120, identifier:most_common; 121, comment; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:logging; 127, identifier:warn; 128, argument_list; 128, 129; 129, string:"Column is all gaps! How did that happen?"; 130, if_statement; 130, 131; 130, 138; 130, 139; 130, 143; 130, 193; 131, comparison_operator:==; 131, 132; 131, 137; 132, subscript; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:most_common; 135, integer:0; 136, integer:1; 137, integer:1; 138, comment; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, yield; 141, 142; 142, string:'-'; 143, elif_clause; 143, 144; 143, 163; 143, 164; 144, parenthesized_expression; 144, 145; 145, boolean_operator:and; 145, 146; 145, 152; 146, comparison_operator:>; 146, 147; 146, 151; 147, call; 147, 148; 147, 149; 148, identifier:len; 149, argument_list; 149, 150; 150, identifier:most_common; 151, integer:1; 152, comparison_operator:==; 152, 153; 152, 158; 153, subscript; 153, 154; 153, 157; 154, subscript; 154, 155; 154, 156; 155, identifier:most_common; 156, integer:0; 157, integer:1; 158, subscript; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:most_common; 161, integer:1; 162, integer:1; 163, comment; 164, block; 164, 165; 164, 185; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:ties; 168, list_comprehension; 168, 169; 168, 172; 168, 175; 169, subscript; 169, 170; 169, 171; 170, identifier:x; 171, integer:0; 172, for_in_clause; 172, 173; 172, 174; 173, identifier:x; 174, identifier:most_common; 175, if_clause; 175, 176; 176, comparison_operator:==; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:x; 179, integer:1; 180, subscript; 180, 181; 180, 184; 181, subscript; 181, 182; 181, 183; 182, identifier:most_common; 183, integer:0; 184, integer:1; 185, expression_statement; 185, 186; 186, yield; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, string:''; 190, identifier:join; 191, argument_list; 191, 192; 192, identifier:ties; 193, else_clause; 193, 194; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, yield; 196, 197; 197, subscript; 197, 198; 197, 201; 198, subscript; 198, 199; 198, 200; 199, identifier:most_common; 200, integer:0; 201, integer:0; 202, return_statement; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:list; 205, argument_list; 205, 206; 206, call; 206, 207; 206, 208; 207, identifier:col_consensus; 208, argument_list; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:zip; 211, argument_list; 211, 212; 212, list_splat; 212, 213; 213, identifier:aln
def supported(aln): """Get only the supported consensus residues in each column. Meaning: - Omit majority-gap columns - Omit columns where no residue type appears more than once - In case of a tie, return all the top-scoring residue types (no prioritization) Returns a *list* -- not a string! -- where elements are strings of the consensus character(s), potentially a gap ('-') or multiple chars ('KR'). """ def col_consensus(columns): """Calculate the consensus chars for an iterable of columns.""" for col in columns: if (# Majority gap chars (col.count('-') >= len(col)/2) or # Lowercase cols mean "don't include in consensus" all(c.islower() for c in col if c not in '.-') ): yield '-' continue # Validation - copied from consensus() above if any(c.islower() for c in col): logging.warn('Mixed lowercase and uppercase letters in a ' 'column: ' + ''.join(col)) col = map(str.upper, col) # Calculate the consensus character most_common = Counter( [c for c in col if c not in '-'] ).most_common() if not most_common: # XXX ever reached? logging.warn("Column is all gaps! How did that happen?") if most_common[0][1] == 1: # No char has frequency > 1; no consensus char yield '-' elif (len(most_common) > 1 and most_common[0][1] == most_common[1][1]): # Tie for most-common residue type ties = [x[0] for x in most_common if x[1] == most_common[0][1]] yield ''.join(ties) else: yield most_common[0][0] return list(col_consensus(zip(*aln)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sort_aliases; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:aliases; 6, block; 6, 7; 6, 9; 6, 15; 6, 21; 6, 36; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_cache_init; 14, argument_list; 15, if_statement; 15, 16; 15, 18; 16, not_operator; 16, 17; 17, identifier:aliases; 18, block; 18, 19; 19, return_statement; 19, 20; 20, identifier:aliases; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:parent_aliases; 24, call; 24, 25; 24, 35; 25, attribute; 25, 26; 25, 34; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_cache_get_entry; 30, argument_list; 30, 31; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:CACHE_NAME_PARENTS; 34, identifier:keys; 35, argument_list; 36, return_statement; 36, 37; 37, list_comprehension; 37, 38; 37, 39; 37, 42; 38, identifier:parent_alias; 39, for_in_clause; 39, 40; 39, 41; 40, identifier:parent_alias; 41, identifier:parent_aliases; 42, if_clause; 42, 43; 43, comparison_operator:in; 43, 44; 43, 45; 44, identifier:parent_alias; 45, identifier:aliases
def sort_aliases(self, aliases): """Sorts the given aliases list, returns a sorted list. :param list aliases: :return: sorted aliases list """ self._cache_init() if not aliases: return aliases parent_aliases = self._cache_get_entry(self.CACHE_NAME_PARENTS).keys() return [parent_alias for parent_alias in parent_aliases if parent_alias in aliases]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:load_and_append; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:probe_dict; 5, identifier:probes; 6, default_parameter; 6, 7; 6, 8; 7, identifier:instruments; 8, dictionary; 9, block; 9, 10; 9, 12; 9, 16; 9, 20; 9, 27; 9, 31; 9, 38; 9, 39; 9, 62; 9, 132; 9, 133; 9, 213; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:loaded_failed; 15, dictionary; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:updated_probes; 19, dictionary; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:updated_probes; 24, identifier:update; 25, argument_list; 25, 26; 26, identifier:probes; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:updated_instruments; 30, dictionary; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:updated_instruments; 35, identifier:update; 36, argument_list; 36, 37; 37, identifier:instruments; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:new_instruments; 42, call; 42, 43; 42, 44; 43, identifier:list; 44, argument_list; 44, 45; 45, binary_operator:-; 45, 46; 45, 54; 46, call; 46, 47; 46, 48; 47, identifier:set; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:probe_dict; 52, identifier:keys; 53, argument_list; 54, call; 54, 55; 54, 56; 55, identifier:set; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:probes; 60, identifier:keys; 61, argument_list; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:!=; 63, 64; 63, 65; 64, identifier:new_instruments; 65, list:[]; 66, block; 66, 67; 66, 85; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 72; 69, pattern_list; 69, 70; 69, 71; 70, identifier:updated_instruments; 71, identifier:failed; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:Instrument; 75, identifier:load_and_append; 76, argument_list; 76, 77; 76, 84; 77, dictionary_comprehension; 77, 78; 77, 81; 78, pair; 78, 79; 78, 80; 79, identifier:instrument_name; 80, identifier:instrument_name; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:instrument_name; 83, identifier:new_instruments; 84, identifier:instruments; 85, if_statement; 85, 86; 85, 89; 85, 90; 85, 91; 86, comparison_operator:!=; 86, 87; 86, 88; 87, identifier:failed; 88, list:[]; 89, comment; 90, comment; 91, block; 91, 92; 92, for_statement; 92, 93; 92, 94; 92, 107; 93, identifier:failed_instrument; 94, binary_operator:-; 94, 95; 94, 99; 95, call; 95, 96; 95, 97; 96, identifier:set; 97, argument_list; 97, 98; 98, identifier:failed; 99, call; 99, 100; 99, 101; 100, identifier:set; 101, argument_list; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:instruments; 105, identifier:keys; 106, argument_list; 107, block; 107, 108; 107, 128; 108, for_statement; 108, 109; 108, 110; 108, 113; 109, identifier:probe_name; 110, subscript; 110, 111; 110, 112; 111, identifier:probe_dict; 112, identifier:failed_instrument; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:loaded_failed; 118, identifier:probe_name; 119, call; 119, 120; 119, 121; 120, identifier:ValueError; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, string:'failed to load instrument {:s} already exists. Did not load!'; 125, identifier:format; 126, argument_list; 126, 127; 127, identifier:failed_instrument; 128, delete_statement; 128, 129; 129, subscript; 129, 130; 129, 131; 130, identifier:probe_dict; 131, identifier:failed_instrument; 132, comment; 133, for_statement; 133, 134; 133, 137; 133, 142; 134, pattern_list; 134, 135; 134, 136; 135, identifier:instrument_name; 136, identifier:probe_names; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:probe_dict; 140, identifier:items; 141, argument_list; 142, block; 142, 143; 142, 159; 143, if_statement; 143, 144; 143, 148; 144, not_operator; 144, 145; 145, comparison_operator:in; 145, 146; 145, 147; 146, identifier:instrument_name; 147, identifier:updated_probes; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:updated_probes; 153, identifier:update; 154, argument_list; 154, 155; 155, dictionary; 155, 156; 156, pair; 156, 157; 156, 158; 157, identifier:instrument_name; 158, dictionary; 159, for_statement; 159, 160; 159, 161; 159, 167; 160, identifier:probe_name; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:probe_names; 164, identifier:split; 165, argument_list; 165, 166; 166, string:','; 167, block; 167, 168; 168, if_statement; 168, 169; 168, 174; 168, 189; 169, comparison_operator:in; 169, 170; 169, 171; 170, identifier:probe_name; 171, subscript; 171, 172; 171, 173; 172, identifier:updated_probes; 173, identifier:instrument_name; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:loaded_failed; 179, identifier:probe_name; 180, call; 180, 181; 180, 182; 181, identifier:ValueError; 182, argument_list; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, string:'failed to load probe {:s} already exists. Did not load!'; 186, identifier:format; 187, argument_list; 187, 188; 188, identifier:probe_name; 189, else_clause; 189, 190; 190, block; 190, 191; 190, 201; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:probe_instance; 194, call; 194, 195; 194, 196; 195, identifier:Probe; 196, argument_list; 196, 197; 196, 200; 197, subscript; 197, 198; 197, 199; 198, identifier:updated_instruments; 199, identifier:instrument_name; 200, identifier:probe_name; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:updated_probes; 206, identifier:instrument_name; 207, identifier:update; 208, argument_list; 208, 209; 209, dictionary; 209, 210; 210, pair; 210, 211; 210, 212; 211, identifier:probe_name; 212, identifier:probe_instance; 213, return_statement; 213, 214; 214, expression_list; 214, 215; 214, 216; 214, 217; 215, identifier:updated_probes; 216, identifier:loaded_failed; 217, identifier:updated_instruments
def load_and_append(probe_dict, probes, instruments={}): """ load probes from probe_dict and append to probes, if additional instruments are required create them and add them to instruments Args: probe_dict: dictionary of form probe_dict = { instrument1_name : probe1_of_instrument1, probe2_of_instrument1, ... instrument2_name : probe1_of_instrument2, probe2_of_instrument2, ... } where probe1_of_instrument1 is a valid name of a probe in instrument of class instrument1_name # optional arguments (as key value pairs): # probe_name # instrument_name # probe_info # buffer_length # # # or # probe_dict = { # name_of_probe_1 : instrument_class_1 # name_of_probe_2 : instrument_class_2 # ... # } probes: dictionary of form probe_dict = { instrument1_name: {name_of_probe_1_of_instrument1 : probe_1_instance, name_of_probe_2_instrument1 : probe_2_instance } , ...} instruments: dictionary of form instruments = { name_of_instrument_1 : instance_of_instrument_1, name_of_instrument_2 : instance_of_instrument_2, ... } Returns: updated_probes = { name_of_probe_1 : probe_1_instance, name_of_probe_2 : probe_2_instance, ...} loaded_failed = {name_of_probe_1: exception_1, name_of_probe_2: exception_2, ....} updated_instruments """ loaded_failed = {} updated_probes = {} updated_probes.update(probes) updated_instruments = {} updated_instruments.update(instruments) # ===== load new instruments ======= new_instruments = list(set(probe_dict.keys())-set(probes.keys())) if new_instruments != []: updated_instruments, failed = Instrument.load_and_append({instrument_name: instrument_name for instrument_name in new_instruments}, instruments) if failed != []: # if loading an instrument fails all the probes that depend on that instrument also fail # ignore the failed instrument that did exist already because they failed because they did exist for failed_instrument in set(failed) - set(instruments.keys()): for probe_name in probe_dict[failed_instrument]: loaded_failed[probe_name] = ValueError('failed to load instrument {:s} already exists. Did not load!'.format(failed_instrument)) del probe_dict[failed_instrument] # ===== now we are sure that all the instruments that we need for the probes already exist for instrument_name, probe_names in probe_dict.items(): if not instrument_name in updated_probes: updated_probes.update({instrument_name:{}}) for probe_name in probe_names.split(','): if probe_name in updated_probes[instrument_name]: loaded_failed[probe_name] = ValueError('failed to load probe {:s} already exists. Did not load!'.format(probe_name)) else: probe_instance = Probe(updated_instruments[instrument_name], probe_name) updated_probes[instrument_name].update({probe_name: probe_instance}) return updated_probes, loaded_failed, updated_instruments
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_get_line; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:search_string; 6, identifier:search_file; 7, default_parameter; 7, 8; 7, 9; 8, identifier:return_string; 9, True; 10, default_parameter; 10, 11; 10, 12; 11, identifier:case_sens; 12, True; 13, block; 13, 14; 13, 16; 14, expression_statement; 14, 15; 15, string:'''Return the first line containing a set of strings in a file. If return_string is False, we just return whether such a line was found. If case_sens is False, the search is case insensitive. '''; 16, if_statement; 16, 17; 16, 25; 16, 26; 16, 125; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:os; 21, identifier:path; 22, identifier:isfile; 23, argument_list; 23, 24; 24, identifier:search_file; 25, comment; 26, block; 26, 27; 26, 43; 26, 44; 26, 60; 27, if_statement; 27, 28; 27, 37; 28, comparison_operator:==; 28, 29; 28, 33; 29, call; 29, 30; 29, 31; 30, identifier:type; 31, argument_list; 31, 32; 32, identifier:search_string; 33, call; 33, 34; 33, 35; 34, identifier:type; 35, argument_list; 35, 36; 36, string:''; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:search_string; 41, list:[search_string]; 41, 42; 42, identifier:search_string; 43, comment; 44, if_statement; 44, 45; 44, 47; 45, not_operator; 45, 46; 46, identifier:case_sens; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:search_string; 51, list_comprehension; 51, 52; 51, 57; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:i; 55, identifier:lower; 56, argument_list; 57, for_in_clause; 57, 58; 57, 59; 58, identifier:i; 59, identifier:search_string; 60, with_statement; 60, 61; 60, 70; 60, 71; 61, with_clause; 61, 62; 62, with_item; 62, 63; 63, as_pattern; 63, 64; 63, 68; 64, call; 64, 65; 64, 66; 65, identifier:open; 66, argument_list; 66, 67; 67, identifier:search_file; 68, as_pattern_target; 68, 69; 69, identifier:fp; 70, comment; 71, block; 71, 72; 71, 104; 72, for_statement; 72, 73; 72, 74; 72, 75; 73, identifier:line; 74, identifier:fp; 75, block; 75, 76; 75, 87; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:query_line; 79, conditional_expression:if; 79, 80; 79, 81; 79, 82; 80, identifier:line; 81, identifier:case_sens; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:line; 85, identifier:lower; 86, argument_list; 87, if_statement; 87, 88; 87, 98; 88, call; 88, 89; 88, 90; 89, identifier:all; 90, argument_list; 90, 91; 91, list_comprehension; 91, 92; 91, 95; 92, comparison_operator:in; 92, 93; 92, 94; 93, identifier:i; 94, identifier:query_line; 95, for_in_clause; 95, 96; 95, 97; 96, identifier:i; 97, identifier:search_string; 98, block; 98, 99; 99, return_statement; 99, 100; 100, conditional_expression:if; 100, 101; 100, 102; 100, 103; 101, identifier:line; 102, identifier:return_string; 103, True; 104, if_statement; 104, 105; 104, 106; 104, 121; 105, identifier:return_string; 106, block; 106, 107; 107, raise_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:Exception; 110, argument_list; 110, 111; 111, binary_operator:%; 111, 112; 111, 113; 112, string:'%s not found in %s'; 113, tuple; 113, 114; 113, 120; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, string:' & '; 117, identifier:join; 118, argument_list; 118, 119; 119, identifier:search_string; 120, identifier:search_file; 121, else_clause; 121, 122; 122, block; 122, 123; 123, return_statement; 123, 124; 124, False; 125, else_clause; 125, 126; 126, block; 126, 127; 127, raise_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:Exception; 130, argument_list; 130, 131; 131, binary_operator:%; 131, 132; 131, 133; 132, string:'%s file does not exist'; 133, identifier:search_file
def _get_line(self, search_string, search_file, return_string=True, case_sens=True): '''Return the first line containing a set of strings in a file. If return_string is False, we just return whether such a line was found. If case_sens is False, the search is case insensitive. ''' if os.path.isfile(search_file): # if single search string if type(search_string) == type(''): search_string = [search_string] # if case insensitive, convert everything to lowercase if not case_sens: search_string = [i.lower() for i in search_string] with open(search_file) as fp: # search for the strings line by line for line in fp: query_line = line if case_sens else line.lower() if all([i in query_line for i in search_string]): return line if return_string else True if return_string: raise Exception('%s not found in %s'%(' & '.join(search_string), search_file)) else: return False else: raise Exception('%s file does not exist'%search_file)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:action_remove; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:request; 6, identifier:category_list; 7, block; 7, 8; 7, 10; 7, 27; 7, 42; 7, 53; 7, 64; 7, 75; 7, 85; 7, 97; 7, 121; 7, 129; 7, 188; 7, 201; 7, 209; 7, 252; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 17; 11, not_operator; 11, 12; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:category_list; 15, identifier:editor; 16, identifier:allow_remove; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:SitecatsSecurityException; 21, argument_list; 21, 22; 22, binary_operator:%; 22, 23; 22, 24; 23, string:'`action_remove()` is not supported by parent `%s`category.'; 24, attribute; 24, 25; 24, 26; 25, identifier:category_list; 26, identifier:alias; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:category_id; 30, call; 30, 31; 30, 32; 31, identifier:int; 32, argument_list; 32, 33; 33, call; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:request; 37, identifier:POST; 38, identifier:get; 39, argument_list; 39, 40; 39, 41; 40, string:'category_id'; 41, integer:0; 42, if_statement; 42, 43; 42, 45; 43, not_operator; 43, 44; 44, identifier:category_id; 45, block; 45, 46; 46, raise_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:SitecatsSecurityException; 49, argument_list; 49, 50; 50, binary_operator:%; 50, 51; 50, 52; 51, string:'Unsupported `category_id` value - `%s` - is passed to `action_remove()`.'; 52, identifier:category_id; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:category; 56, call; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, call; 58, 59; 58, 60; 59, identifier:get_cache; 60, argument_list; 61, identifier:get_category_by_id; 62, argument_list; 62, 63; 63, identifier:category_id; 64, if_statement; 64, 65; 64, 67; 65, not_operator; 65, 66; 66, identifier:category; 67, block; 67, 68; 68, raise_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:SitecatsSecurityException; 71, argument_list; 71, 72; 72, binary_operator:%; 72, 73; 72, 74; 73, string:'Unable to get `%s` category in `action_remove()`.'; 74, identifier:category_id; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:cat_ident; 78, boolean_operator:or; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:category; 81, identifier:alias; 82, attribute; 82, 83; 82, 84; 83, identifier:category; 84, identifier:id; 85, if_statement; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:category; 88, identifier:is_locked; 89, block; 89, 90; 90, raise_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:SitecatsSecurityException; 93, argument_list; 93, 94; 94, binary_operator:%; 94, 95; 94, 96; 95, string:'`action_remove()` is not supported by `%s` category.'; 96, identifier:cat_ident; 97, if_statement; 97, 98; 97, 107; 98, comparison_operator:!=; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:category; 101, identifier:parent_id; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:category_list; 105, identifier:get_id; 106, argument_list; 107, block; 107, 108; 108, raise_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:SitecatsSecurityException; 111, argument_list; 111, 112; 112, binary_operator:%; 112, 113; 112, 116; 113, concatenated_string; 113, 114; 113, 115; 114, string:'`action_remove()` is unable to remove `%s`: '; 115, string:'not a child of parent `%s` category.'; 116, tuple; 116, 117; 116, 118; 117, identifier:cat_ident; 118, attribute; 118, 119; 118, 120; 119, identifier:category_list; 120, identifier:alias; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:min_num; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:category_list; 127, identifier:editor; 128, identifier:min_num; 129, function_definition; 129, 130; 129, 131; 129, 133; 130, function_name:check_min_num; 131, parameters; 131, 132; 132, identifier:num; 133, block; 133, 134; 134, if_statement; 134, 135; 134, 144; 135, boolean_operator:and; 135, 136; 135, 139; 136, comparison_operator:is; 136, 137; 136, 138; 137, identifier:min_num; 138, None; 139, comparison_operator:<; 139, 140; 139, 143; 140, binary_operator:-; 140, 141; 140, 142; 141, identifier:num; 142, integer:1; 143, identifier:min_num; 144, block; 144, 145; 144, 154; 144, 183; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:subcats_str; 148, call; 148, 149; 148, 150; 149, identifier:ungettext_lazy; 150, argument_list; 150, 151; 150, 152; 150, 153; 151, string:'subcategory'; 152, string:'subcategories'; 153, identifier:min_num; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:error_msg; 157, binary_operator:%; 157, 158; 157, 164; 158, call; 158, 159; 158, 160; 159, identifier:_; 160, argument_list; 160, 161; 161, concatenated_string; 161, 162; 161, 163; 162, string:'Unable to remove "%(target_category)s" category from "%(parent_category)s": '; 163, string:'parent category requires at least %(num)s %(subcats_str)s.'; 164, dictionary; 164, 165; 164, 170; 164, 177; 164, 180; 165, pair; 165, 166; 165, 167; 166, string:'target_category'; 167, attribute; 167, 168; 167, 169; 168, identifier:category; 169, identifier:title; 170, pair; 170, 171; 170, 172; 171, string:'parent_category'; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:category_list; 175, identifier:get_title; 176, argument_list; 177, pair; 177, 178; 177, 179; 178, string:'num'; 179, identifier:min_num; 180, pair; 180, 181; 180, 182; 181, string:'subcats_str'; 182, identifier:subcats_str; 183, raise_statement; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:SitecatsValidationError; 186, argument_list; 186, 187; 187, identifier:error_msg; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:child_ids; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, call; 193, 194; 193, 195; 194, identifier:get_cache; 195, argument_list; 196, identifier:get_child_ids; 197, argument_list; 197, 198; 198, attribute; 198, 199; 198, 200; 199, identifier:category_list; 200, identifier:alias; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:check_min_num; 204, argument_list; 204, 205; 205, call; 205, 206; 205, 207; 206, identifier:len; 207, argument_list; 207, 208; 208, identifier:child_ids; 209, if_statement; 209, 210; 209, 215; 209, 216; 209, 223; 210, comparison_operator:is; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:category_list; 213, identifier:obj; 214, None; 215, comment; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:category; 221, identifier:delete; 222, argument_list; 223, else_clause; 223, 224; 223, 225; 223, 226; 224, comment; 225, comment; 226, block; 226, 227; 226, 243; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:check_min_num; 230, argument_list; 230, 231; 231, call; 231, 232; 231, 242; 232, attribute; 232, 233; 232, 241; 233, call; 233, 234; 233, 239; 234, attribute; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:category_list; 237, identifier:obj; 238, identifier:get_ties_for_categories_qs; 239, argument_list; 239, 240; 240, identifier:child_ids; 241, identifier:count; 242, argument_list; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 250; 245, attribute; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:category_list; 248, identifier:obj; 249, identifier:remove_from_category; 250, argument_list; 250, 251; 251, identifier:category; 252, return_statement; 252, 253; 253, True
def action_remove(cls, request, category_list): """Handles `remove` action from CategoryList editor. Removes an actual category if a target object is not set for the list. Removes a tie-to-category object if a target object is set for the list. :param Request request: Django request object :param CategoryList category_list: CategoryList object to operate upon. :return: True on success otherwise and exception from SitecatsException family is raised. """ if not category_list.editor.allow_remove: raise SitecatsSecurityException( '`action_remove()` is not supported by parent `%s`category.' % category_list.alias) category_id = int(request.POST.get('category_id', 0)) if not category_id: raise SitecatsSecurityException( 'Unsupported `category_id` value - `%s` - is passed to `action_remove()`.' % category_id) category = get_cache().get_category_by_id(category_id) if not category: raise SitecatsSecurityException('Unable to get `%s` category in `action_remove()`.' % category_id) cat_ident = category.alias or category.id if category.is_locked: raise SitecatsSecurityException('`action_remove()` is not supported by `%s` category.' % cat_ident) if category.parent_id != category_list.get_id(): raise SitecatsSecurityException( '`action_remove()` is unable to remove `%s`: ' 'not a child of parent `%s` category.' % (cat_ident, category_list.alias) ) min_num = category_list.editor.min_num def check_min_num(num): if min_num is not None and num-1 < min_num: subcats_str = ungettext_lazy('subcategory', 'subcategories', min_num) error_msg = _( 'Unable to remove "%(target_category)s" category from "%(parent_category)s": ' 'parent category requires at least %(num)s %(subcats_str)s.' ) % { 'target_category': category.title, 'parent_category': category_list.get_title(), 'num': min_num, 'subcats_str': subcats_str } raise SitecatsValidationError(error_msg) child_ids = get_cache().get_child_ids(category_list.alias) check_min_num(len(child_ids)) if category_list.obj is None: # Remove category itself and children. category.delete() else: # Remove just a category-to-object tie. # TODO filter user/status check_min_num(category_list.obj.get_ties_for_categories_qs(child_ids).count()) category_list.obj.remove_from_category(category) return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:action_add; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:request; 6, identifier:category_list; 7, block; 7, 8; 7, 10; 7, 27; 7, 43; 7, 54; 7, 97; 7, 156; 7, 160; 7, 337; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 17; 11, not_operator; 11, 12; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:category_list; 15, identifier:editor; 16, identifier:allow_add; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:SitecatsSecurityException; 21, argument_list; 21, 22; 22, binary_operator:%; 22, 23; 22, 24; 23, string:'`action_add()` is not supported by `%s` category.'; 24, attribute; 24, 25; 24, 26; 25, identifier:category_list; 26, identifier:alias; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:titles; 30, call; 30, 31; 30, 42; 31, attribute; 31, 32; 31, 41; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:request; 36, identifier:POST; 37, identifier:get; 38, argument_list; 38, 39; 38, 40; 39, string:'category_title'; 40, string:''; 41, identifier:strip; 42, argument_list; 43, if_statement; 43, 44; 43, 46; 44, not_operator; 44, 45; 45, identifier:titles; 46, block; 46, 47; 47, raise_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:SitecatsSecurityException; 50, argument_list; 50, 51; 51, binary_operator:%; 51, 52; 51, 53; 52, string:'Unsupported `category_title` value - `%s` - is passed to `action_add()`.'; 53, identifier:titles; 54, if_statement; 54, 55; 54, 62; 54, 68; 55, comparison_operator:is; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:category_list; 59, identifier:editor; 60, identifier:category_separator; 61, None; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:titles; 66, list:[titles]; 66, 67; 67, identifier:titles; 68, else_clause; 68, 69; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:titles; 73, list_comprehension; 73, 74; 73, 79; 73, 91; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:title; 77, identifier:strip; 78, argument_list; 79, for_in_clause; 79, 80; 79, 81; 80, identifier:title; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:titles; 84, identifier:split; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:category_list; 89, identifier:editor; 90, identifier:category_separator; 91, if_clause; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:title; 95, identifier:strip; 96, argument_list; 97, function_definition; 97, 98; 97, 99; 97, 103; 98, function_name:check_max_num; 99, parameters; 99, 100; 99, 101; 99, 102; 100, identifier:num; 101, identifier:max_num; 102, identifier:category_title; 103, block; 103, 104; 104, if_statement; 104, 105; 104, 114; 105, boolean_operator:and; 105, 106; 105, 109; 106, comparison_operator:is; 106, 107; 106, 108; 107, identifier:max_num; 108, None; 109, comparison_operator:>; 109, 110; 109, 113; 110, binary_operator:+; 110, 111; 110, 112; 111, identifier:num; 112, integer:1; 113, identifier:max_num; 114, block; 114, 115; 114, 124; 114, 151; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:subcats_str; 118, call; 118, 119; 118, 120; 119, identifier:ungettext_lazy; 120, argument_list; 120, 121; 120, 122; 120, 123; 121, string:'subcategory'; 122, string:'subcategories'; 123, identifier:max_num; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:error_msg; 127, binary_operator:%; 127, 128; 127, 134; 128, call; 128, 129; 128, 130; 129, identifier:_; 130, argument_list; 130, 131; 131, concatenated_string; 131, 132; 131, 133; 132, string:'Unable to add "%(target_category)s" category into "%(parent_category)s": '; 133, string:'parent category can have at most %(num)s %(subcats_str)s.'; 134, dictionary; 134, 135; 134, 138; 134, 145; 134, 148; 135, pair; 135, 136; 135, 137; 136, string:'target_category'; 137, identifier:category_title; 138, pair; 138, 139; 138, 140; 139, string:'parent_category'; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:category_list; 143, identifier:get_title; 144, argument_list; 145, pair; 145, 146; 145, 147; 146, string:'num'; 147, identifier:max_num; 148, pair; 148, 149; 148, 150; 149, string:'subcats_str'; 150, identifier:subcats_str; 151, raise_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:SitecatsValidationError; 154, argument_list; 154, 155; 155, identifier:error_msg; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:target_category; 159, None; 160, for_statement; 160, 161; 160, 162; 160, 163; 161, identifier:category_title; 162, identifier:titles; 163, block; 163, 164; 163, 178; 163, 190; 163, 227; 163, 235; 163, 248; 163, 299; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:exists; 167, call; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, call; 169, 170; 169, 171; 170, identifier:get_cache; 171, argument_list; 172, identifier:find_category; 173, argument_list; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:category_list; 176, identifier:alias; 177, identifier:category_title; 178, if_statement; 178, 179; 178, 186; 178, 187; 179, boolean_operator:and; 179, 180; 179, 181; 180, identifier:exists; 181, comparison_operator:is; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:category_list; 184, identifier:obj; 185, None; 186, comment; 187, block; 187, 188; 188, return_statement; 188, 189; 189, identifier:exists; 190, if_statement; 190, 191; 190, 200; 191, boolean_operator:and; 191, 192; 191, 194; 192, not_operator; 192, 193; 193, identifier:exists; 194, not_operator; 194, 195; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:category_list; 198, identifier:editor; 199, identifier:allow_new; 200, block; 200, 201; 200, 222; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:error_msg; 204, binary_operator:%; 204, 205; 204, 211; 205, call; 205, 206; 205, 207; 206, identifier:_; 207, argument_list; 207, 208; 208, concatenated_string; 208, 209; 208, 210; 209, string:'Unable to create a new "%(new_category)s" category inside of "%(parent_category)s": '; 210, string:'parent category does not support this action.'; 211, dictionary; 211, 212; 211, 215; 212, pair; 212, 213; 212, 214; 213, string:'new_category'; 214, identifier:category_title; 215, pair; 215, 216; 215, 217; 216, string:'parent_category'; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:category_list; 220, identifier:get_title; 221, argument_list; 222, raise_statement; 222, 223; 223, call; 223, 224; 223, 225; 224, identifier:SitecatsNewCategoryException; 225, argument_list; 225, 226; 226, identifier:error_msg; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:max_num; 230, attribute; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:category_list; 233, identifier:editor; 234, identifier:max_num; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:child_ids; 238, call; 238, 239; 238, 244; 239, attribute; 239, 240; 239, 243; 240, call; 240, 241; 240, 242; 241, identifier:get_cache; 242, argument_list; 243, identifier:get_child_ids; 244, argument_list; 244, 245; 245, attribute; 245, 246; 245, 247; 246, identifier:category_list; 247, identifier:alias; 248, if_statement; 248, 249; 248, 251; 248, 252; 248, 292; 249, not_operator; 249, 250; 250, identifier:exists; 251, comment; 252, block; 252, 253; 252, 270; 252, 271; 253, if_statement; 253, 254; 253, 259; 254, comparison_operator:is; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:category_list; 257, identifier:obj; 258, None; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 263; 262, identifier:check_max_num; 263, argument_list; 263, 264; 263, 268; 263, 269; 264, call; 264, 265; 264, 266; 265, identifier:len; 266, argument_list; 266, 267; 267, identifier:child_ids; 268, identifier:max_num; 269, identifier:category_title; 270, comment; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:target_category; 274, call; 274, 275; 274, 280; 275, attribute; 275, 276; 275, 279; 276, call; 276, 277; 276, 278; 277, identifier:get_category_model; 278, argument_list; 279, identifier:add; 280, argument_list; 280, 281; 280, 282; 280, 285; 281, identifier:category_title; 282, attribute; 282, 283; 282, 284; 283, identifier:request; 284, identifier:user; 285, keyword_argument; 285, 286; 285, 287; 286, identifier:parent; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:category_list; 290, identifier:get_category_model; 291, argument_list; 292, else_clause; 292, 293; 293, block; 293, 294; 293, 298; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:target_category; 297, identifier:exists; 298, comment; 299, if_statement; 299, 300; 299, 305; 299, 306; 300, comparison_operator:is; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:category_list; 303, identifier:obj; 304, None; 305, comment; 306, block; 306, 307; 306, 325; 307, expression_statement; 307, 308; 308, call; 308, 309; 308, 310; 309, identifier:check_max_num; 310, argument_list; 310, 311; 310, 323; 310, 324; 311, call; 311, 312; 311, 322; 312, attribute; 312, 313; 312, 321; 313, call; 313, 314; 313, 319; 314, attribute; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:category_list; 317, identifier:obj; 318, identifier:get_ties_for_categories_qs; 319, argument_list; 319, 320; 320, identifier:child_ids; 321, identifier:count; 322, argument_list; 323, identifier:max_num; 324, identifier:category_title; 325, expression_statement; 325, 326; 326, call; 326, 327; 326, 332; 327, attribute; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:category_list; 330, identifier:obj; 331, identifier:add_to_category; 332, argument_list; 332, 333; 332, 334; 333, identifier:target_category; 334, attribute; 334, 335; 334, 336; 335, identifier:request; 336, identifier:user; 337, return_statement; 337, 338; 338, identifier:target_category
def action_add(cls, request, category_list): """Handles `add` action from CategoryList editor. Adds an actual category if a target object is not set for the list. Adds a tie-to-category object if a target object is set for the list. :param Request request: Django request object :param CategoryList category_list: CategoryList object to operate upon. :return: CategoryModel object on success otherwise and exception from SitecatsException family is raised. """ if not category_list.editor.allow_add: raise SitecatsSecurityException('`action_add()` is not supported by `%s` category.' % category_list.alias) titles = request.POST.get('category_title', '').strip() if not titles: raise SitecatsSecurityException( 'Unsupported `category_title` value - `%s` - is passed to `action_add()`.' % titles) if category_list.editor.category_separator is None: titles = [titles] else: titles = [ title.strip() for title in titles.split(category_list.editor.category_separator) if title.strip() ] def check_max_num(num, max_num, category_title): if max_num is not None and num+1 > max_num: subcats_str = ungettext_lazy('subcategory', 'subcategories', max_num) error_msg = _( 'Unable to add "%(target_category)s" category into "%(parent_category)s": ' 'parent category can have at most %(num)s %(subcats_str)s.' ) % { 'target_category': category_title, 'parent_category': category_list.get_title(), 'num': max_num, 'subcats_str': subcats_str } raise SitecatsValidationError(error_msg) target_category = None for category_title in titles: exists = get_cache().find_category(category_list.alias, category_title) if exists and category_list.obj is None: # Already exists. return exists if not exists and not category_list.editor.allow_new: error_msg = _( 'Unable to create a new "%(new_category)s" category inside of "%(parent_category)s": ' 'parent category does not support this action.' ) % { 'new_category': category_title, 'parent_category': category_list.get_title() } raise SitecatsNewCategoryException(error_msg) max_num = category_list.editor.max_num child_ids = get_cache().get_child_ids(category_list.alias) if not exists: # Add new category. if category_list.obj is None: check_max_num(len(child_ids), max_num, category_title) # TODO status target_category = get_category_model().add( category_title, request.user, parent=category_list.get_category_model() ) else: target_category = exists # Use existing one for a tie. if category_list.obj is not None: # TODO status check_max_num(category_list.obj.get_ties_for_categories_qs(child_ids).count(), max_num, category_title) category_list.obj.add_to_category(target_category, request.user) return target_category
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:verify; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 12; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:headers; 7, type; 7, 8; 8, identifier:Mapping; 9, default_parameter; 9, 10; 9, 11; 10, identifier:method; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:path; 14, None; 15, block; 15, 16; 15, 18; 15, 26; 15, 37; 15, 48; 15, 70; 15, 94; 15, 108; 15, 130; 15, 142; 15, 163; 15, 176; 15, 190; 15, 200; 15, 212; 15, 222; 15, 228; 15, 236; 15, 265; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 23; 19, not_operator; 19, 20; 20, comparison_operator:in; 20, 21; 20, 22; 21, string:'authorization'; 22, identifier:headers; 23, block; 23, 24; 24, return_statement; 24, 25; 25, False; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 31; 28, pattern_list; 28, 29; 28, 30; 29, identifier:auth_type; 30, identifier:auth_params; 31, call; 31, 32; 31, 33; 32, identifier:parse_authorization_header; 33, argument_list; 33, 34; 34, subscript; 34, 35; 34, 36; 35, identifier:headers; 36, string:'authorization'; 37, if_statement; 37, 38; 37, 45; 38, comparison_operator:!=; 38, 39; 38, 44; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:auth_type; 42, identifier:lower; 43, argument_list; 44, string:'signature'; 45, block; 45, 46; 46, return_statement; 46, 47; 47, False; 48, for_statement; 48, 49; 48, 50; 48, 54; 49, identifier:param; 50, tuple; 50, 51; 50, 52; 50, 53; 51, string:'algorithm'; 52, string:'keyId'; 53, string:'signature'; 54, block; 54, 55; 55, if_statement; 55, 56; 55, 59; 56, comparison_operator:not; 56, 57; 56, 58; 57, identifier:param; 58, identifier:auth_params; 59, block; 59, 60; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:VerifierException; 63, argument_list; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, string:"Unsupported HTTP signature, missing '{}'"; 67, identifier:format; 68, argument_list; 68, 69; 69, identifier:param; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:auth_headers; 73, call; 73, 74; 73, 93; 74, attribute; 74, 75; 74, 92; 75, call; 75, 76; 75, 91; 76, attribute; 76, 77; 76, 90; 77, call; 77, 78; 77, 89; 78, attribute; 78, 79; 78, 88; 79, parenthesized_expression; 79, 80; 80, boolean_operator:or; 80, 81; 80, 87; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:auth_params; 84, identifier:get; 85, argument_list; 85, 86; 86, string:'headers'; 87, string:'date'; 88, identifier:lower; 89, argument_list; 90, identifier:strip; 91, argument_list; 92, identifier:split; 93, argument_list; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:missing_reqd; 97, binary_operator:-; 97, 98; 97, 104; 98, call; 98, 99; 98, 100; 99, identifier:set; 100, argument_list; 100, 101; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:_required_headers; 104, call; 104, 105; 104, 106; 105, identifier:set; 106, argument_list; 106, 107; 107, identifier:auth_headers; 108, if_statement; 108, 109; 108, 110; 109, identifier:missing_reqd; 110, block; 110, 111; 110, 120; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:error_headers; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, string:', '; 117, identifier:join; 118, argument_list; 118, 119; 119, identifier:missing_reqd; 120, raise_statement; 120, 121; 121, call; 121, 122; 121, 123; 122, identifier:VerifierException; 123, argument_list; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, string:'One or more required headers not provided: {}'; 127, identifier:format; 128, argument_list; 128, 129; 129, identifier:error_headers; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 135; 132, pattern_list; 132, 133; 132, 134; 133, identifier:key_id; 134, identifier:algo; 135, expression_list; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:auth_params; 138, string:'keyId'; 139, subscript; 139, 140; 139, 141; 140, identifier:auth_params; 141, string:'algorithm'; 142, if_statement; 142, 143; 142, 152; 143, not_operator; 143, 144; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:_handlers; 149, identifier:supports; 150, argument_list; 150, 151; 151, identifier:algo; 152, block; 152, 153; 153, raise_statement; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:VerifierException; 156, argument_list; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, string:"Unsupported HTTP signature algorithm '{}'"; 160, identifier:format; 161, argument_list; 161, 162; 162, identifier:algo; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:pubkey; 166, await; 166, 167; 167, call; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:_key_finder; 172, identifier:find_key; 173, argument_list; 173, 174; 173, 175; 174, identifier:key_id; 175, identifier:algo; 176, if_statement; 176, 177; 176, 179; 177, not_operator; 177, 178; 178, identifier:pubkey; 179, block; 179, 180; 180, raise_statement; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:VerifierException; 183, argument_list; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, string:"Cannot locate public key for '{}'"; 187, identifier:format; 188, argument_list; 188, 189; 189, identifier:key_id; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:LOGGER; 194, identifier:debug; 195, argument_list; 195, 196; 195, 197; 195, 198; 195, 199; 196, string:"Got %s public key for '%s': %s"; 197, identifier:algo; 198, identifier:key_id; 199, identifier:pubkey; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:handler; 203, call; 203, 204; 203, 209; 204, attribute; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:_handlers; 208, identifier:create_verifier; 209, argument_list; 209, 210; 209, 211; 210, identifier:algo; 211, identifier:pubkey; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:message; 215, call; 215, 216; 215, 217; 216, identifier:generate_message; 217, argument_list; 217, 218; 217, 219; 217, 220; 217, 221; 218, identifier:auth_headers; 219, identifier:headers; 220, identifier:method; 221, identifier:path; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:signature; 225, subscript; 225, 226; 225, 227; 226, identifier:auth_params; 227, string:'signature'; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:raw_signature; 231, call; 231, 232; 231, 233; 232, identifier:decode_string; 233, argument_list; 233, 234; 233, 235; 234, identifier:signature; 235, string:'base64'; 236, if_statement; 236, 237; 236, 244; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:handler; 240, identifier:verify; 241, argument_list; 241, 242; 241, 243; 242, identifier:message; 243, identifier:raw_signature; 244, block; 244, 245; 245, return_statement; 245, 246; 246, dictionary; 246, 247; 246, 250; 246, 253; 246, 256; 246, 259; 246, 262; 247, pair; 247, 248; 247, 249; 248, string:'verified'; 249, True; 250, pair; 250, 251; 250, 252; 251, string:'algorithm'; 252, identifier:algo; 253, pair; 253, 254; 253, 255; 254, string:'headers'; 255, identifier:auth_headers; 256, pair; 256, 257; 256, 258; 257, string:'keyId'; 258, identifier:key_id; 259, pair; 259, 260; 259, 261; 260, string:'key'; 261, identifier:pubkey; 262, pair; 262, 263; 262, 264; 263, string:'signature'; 264, identifier:signature; 265, raise_statement; 265, 266; 266, call; 266, 267; 266, 268; 267, identifier:VerifierException; 268, argument_list; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, string:"Signature could not be verified for keyId '{}'"; 272, identifier:format; 273, argument_list; 273, 274; 274, identifier:key_id
async def verify(self, headers: Mapping, method=None, path=None): """ Parse Signature Authorization header and verify signature `headers` is a dict or multidict of headers `host` is a override for the 'host' header (defaults to value in headers). `method` is the HTTP method (required when using '(request-target)'). `path` is the HTTP path (required when using '(request-target)'). """ if not 'authorization' in headers: return False auth_type, auth_params = parse_authorization_header(headers['authorization']) if auth_type.lower() != 'signature': return False for param in ('algorithm', 'keyId', 'signature'): if param not in auth_params: raise VerifierException("Unsupported HTTP signature, missing '{}'".format(param)) auth_headers = (auth_params.get('headers') or 'date').lower().strip().split() missing_reqd = set(self._required_headers) - set(auth_headers) if missing_reqd: error_headers = ', '.join(missing_reqd) raise VerifierException( 'One or more required headers not provided: {}'.format(error_headers)) key_id, algo = auth_params['keyId'], auth_params['algorithm'] if not self._handlers.supports(algo): raise VerifierException("Unsupported HTTP signature algorithm '{}'".format(algo)) pubkey = await self._key_finder.find_key(key_id, algo) if not pubkey: raise VerifierException("Cannot locate public key for '{}'".format(key_id)) LOGGER.debug("Got %s public key for '%s': %s", algo, key_id, pubkey) handler = self._handlers.create_verifier(algo, pubkey) message = generate_message(auth_headers, headers, method, path) signature = auth_params['signature'] raw_signature = decode_string(signature, 'base64') if handler.verify(message, raw_signature): return { 'verified': True, 'algorithm': algo, 'headers': auth_headers, 'keyId': key_id, 'key': pubkey, 'signature': signature } raise VerifierException("Signature could not be verified for keyId '{}'".format(key_id))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:set_config; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, dictionary_splat_pattern; 5, 6; 6, identifier:config; 7, block; 7, 8; 7, 10; 7, 14; 7, 43; 7, 74; 7, 105; 7, 121; 7, 152; 7, 183; 7, 216; 7, 247; 7, 263; 7, 279; 7, 310; 7, 334; 7, 369; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:reinit; 13, False; 14, if_statement; 14, 15; 14, 18; 15, comparison_operator:in; 15, 16; 15, 17; 16, string:'stdopt'; 17, identifier:config; 18, block; 18, 19; 18, 28; 18, 37; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:stdopt; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:config; 25, identifier:pop; 26, argument_list; 26, 27; 27, string:'stdopt'; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:reinit; 31, parenthesized_expression; 31, 32; 32, comparison_operator:!=; 32, 33; 32, 34; 33, identifier:stdopt; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:stdopt; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:stdopt; 42, identifier:stdopt; 43, if_statement; 43, 44; 43, 47; 44, comparison_operator:in; 44, 45; 44, 46; 45, string:'attachopt'; 46, identifier:config; 47, block; 47, 48; 47, 57; 47, 68; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:attachopt; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:config; 54, identifier:pop; 55, argument_list; 55, 56; 56, string:'attachopt'; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:reinit; 60, boolean_operator:or; 60, 61; 60, 62; 61, identifier:reinit; 62, parenthesized_expression; 62, 63; 63, comparison_operator:!=; 63, 64; 63, 65; 64, identifier:attachopt; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:attachopt; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:attachopt; 73, identifier:attachopt; 74, if_statement; 74, 75; 74, 78; 75, comparison_operator:in; 75, 76; 75, 77; 76, string:'attachvalue'; 77, identifier:config; 78, block; 78, 79; 78, 88; 78, 99; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:attachvalue; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:config; 85, identifier:pop; 86, argument_list; 86, 87; 87, string:'attachvalue'; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:reinit; 91, boolean_operator:or; 91, 92; 91, 93; 92, identifier:reinit; 93, parenthesized_expression; 93, 94; 94, comparison_operator:!=; 94, 95; 94, 96; 95, identifier:attachvalue; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:attachvalue; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:attachvalue; 104, identifier:attachvalue; 105, if_statement; 105, 106; 105, 109; 106, comparison_operator:in; 106, 107; 106, 108; 107, string:'auto2dashes'; 108, identifier:config; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:auto2dashes; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:config; 118, identifier:pop; 119, argument_list; 119, 120; 120, string:'auto2dashes'; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:in; 122, 123; 122, 124; 123, string:'name'; 124, identifier:config; 125, block; 125, 126; 125, 135; 125, 146; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:name; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:config; 132, identifier:pop; 133, argument_list; 133, 134; 134, string:'name'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:reinit; 138, boolean_operator:or; 138, 139; 138, 140; 139, identifier:reinit; 140, parenthesized_expression; 140, 141; 141, comparison_operator:!=; 141, 142; 141, 143; 142, identifier:name; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:name; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:name; 151, identifier:name; 152, if_statement; 152, 153; 152, 156; 153, comparison_operator:in; 153, 154; 153, 155; 154, string:'help'; 155, identifier:config; 156, block; 156, 157; 156, 168; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:help; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:config; 165, identifier:pop; 166, argument_list; 166, 167; 167, string:'help'; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:_set_or_remove_extra_handler; 173, argument_list; 173, 174; 173, 177; 173, 180; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:help; 177, tuple; 177, 178; 177, 179; 178, string:'--help'; 179, string:'-h'; 180, attribute; 180, 181; 180, 182; 181, identifier:self; 182, identifier:help_handler; 183, if_statement; 183, 184; 183, 187; 184, comparison_operator:in; 184, 185; 184, 186; 185, string:'version'; 186, identifier:config; 187, block; 187, 188; 187, 199; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:version; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:config; 196, identifier:pop; 197, argument_list; 197, 198; 198, string:'version'; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:_set_or_remove_extra_handler; 204, argument_list; 204, 205; 204, 210; 204, 213; 205, comparison_operator:is; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:version; 209, None; 210, tuple; 210, 211; 210, 212; 211, string:'--version'; 212, string:'-v'; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:version_handler; 216, if_statement; 216, 217; 216, 220; 217, comparison_operator:in; 217, 218; 217, 219; 218, string:'case_sensitive'; 219, identifier:config; 220, block; 220, 221; 220, 230; 220, 241; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:case_sensitive; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:config; 227, identifier:pop; 228, argument_list; 228, 229; 229, string:'case_sensitive'; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:reinit; 233, boolean_operator:or; 233, 234; 233, 235; 234, identifier:reinit; 235, parenthesized_expression; 235, 236; 236, comparison_operator:!=; 236, 237; 236, 238; 237, identifier:case_sensitive; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:case_sensitive; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:case_sensitive; 246, identifier:case_sensitive; 247, if_statement; 247, 248; 247, 251; 248, comparison_operator:in; 248, 249; 248, 250; 249, string:'optionsfirst'; 250, identifier:config; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:self; 256, identifier:options_first; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:config; 260, identifier:pop; 261, argument_list; 261, 262; 262, string:'optionsfirst'; 263, if_statement; 263, 264; 263, 267; 264, comparison_operator:in; 264, 265; 264, 266; 265, string:'appearedonly'; 266, identifier:config; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:appeared_only; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:config; 276, identifier:pop; 277, argument_list; 277, 278; 278, string:'appearedonly'; 279, if_statement; 279, 280; 279, 283; 280, comparison_operator:in; 280, 281; 280, 282; 281, string:'namedoptions'; 282, identifier:config; 283, block; 283, 284; 283, 293; 283, 304; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:namedoptions; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:config; 290, identifier:pop; 291, argument_list; 291, 292; 292, string:'namedoptions'; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:reinit; 296, boolean_operator:or; 296, 297; 296, 298; 297, identifier:reinit; 298, parenthesized_expression; 298, 299; 299, comparison_operator:!=; 299, 300; 299, 301; 300, identifier:namedoptions; 301, attribute; 301, 302; 301, 303; 302, identifier:self; 303, identifier:namedoptions; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:self; 308, identifier:namedoptions; 309, identifier:namedoptions; 310, if_statement; 310, 311; 310, 314; 311, comparison_operator:in; 311, 312; 311, 313; 312, string:'extra'; 313, identifier:config; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, call; 316, 317; 316, 322; 317, attribute; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:self; 320, identifier:extra; 321, identifier:update; 322, argument_list; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:self; 326, identifier:_formal_extra; 327, argument_list; 327, 328; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:config; 331, identifier:pop; 332, argument_list; 332, 333; 333, string:'extra'; 334, if_statement; 334, 335; 334, 336; 334, 337; 335, identifier:config; 336, comment; 337, block; 337, 338; 338, raise_statement; 338, 339; 339, call; 339, 340; 339, 341; 340, identifier:ValueError; 341, argument_list; 341, 342; 342, binary_operator:%; 342, 343; 342, 344; 343, string:'`%s` %s not accepted key argument%s'; 344, tuple; 344, 345; 344, 351; 344, 360; 345, call; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, string:'`, `'; 348, identifier:join; 349, argument_list; 349, 350; 350, identifier:config; 351, conditional_expression:if; 351, 352; 351, 353; 351, 359; 352, string:'is'; 353, comparison_operator:==; 353, 354; 353, 358; 354, call; 354, 355; 354, 356; 355, identifier:len; 356, argument_list; 356, 357; 357, identifier:config; 358, integer:1; 359, string:'are'; 360, conditional_expression:if; 360, 361; 360, 362; 360, 368; 361, string:''; 362, comparison_operator:==; 362, 363; 362, 367; 363, call; 363, 364; 363, 365; 364, identifier:len; 365, argument_list; 365, 366; 366, identifier:config; 367, integer:1; 368, string:'s'; 369, if_statement; 369, 370; 369, 377; 370, boolean_operator:and; 370, 371; 370, 376; 371, comparison_operator:is; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:self; 374, identifier:doc; 375, None; 376, identifier:reinit; 377, block; 377, 378; 377, 387; 378, expression_statement; 378, 379; 379, call; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:logger; 382, identifier:warning; 383, argument_list; 383, 384; 384, concatenated_string; 384, 385; 384, 386; 385, string:'You changed the config that requires re-initialized'; 386, string:' `Docpie` object. Create a new one instead'; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:self; 391, identifier:_init; 392, argument_list
def set_config(self, **config): """Shadow all the current config.""" reinit = False if 'stdopt' in config: stdopt = config.pop('stdopt') reinit = (stdopt != self.stdopt) self.stdopt = stdopt if 'attachopt' in config: attachopt = config.pop('attachopt') reinit = reinit or (attachopt != self.attachopt) self.attachopt = attachopt if 'attachvalue' in config: attachvalue = config.pop('attachvalue') reinit = reinit or (attachvalue != self.attachvalue) self.attachvalue = attachvalue if 'auto2dashes' in config: self.auto2dashes = config.pop('auto2dashes') if 'name' in config: name = config.pop('name') reinit = reinit or (name != self.name) self.name = name if 'help' in config: self.help = config.pop('help') self._set_or_remove_extra_handler( self.help, ('--help', '-h'), self.help_handler) if 'version' in config: self.version = config.pop('version') self._set_or_remove_extra_handler( self.version is not None, ('--version', '-v'), self.version_handler) if 'case_sensitive' in config: case_sensitive = config.pop('case_sensitive') reinit = reinit or (case_sensitive != self.case_sensitive) self.case_sensitive = case_sensitive if 'optionsfirst' in config: self.options_first = config.pop('optionsfirst') if 'appearedonly' in config: self.appeared_only = config.pop('appearedonly') if 'namedoptions' in config: namedoptions = config.pop('namedoptions') reinit = reinit or (namedoptions != self.namedoptions) self.namedoptions = namedoptions if 'extra' in config: self.extra.update(self._formal_extra(config.pop('extra'))) if config: # should be empty raise ValueError( '`%s` %s not accepted key argument%s' % ( '`, `'.join(config), 'is' if len(config) == 1 else 'are', '' if len(config) == 1 else 's' )) if self.doc is not None and reinit: logger.warning( 'You changed the config that requires re-initialized' ' `Docpie` object. Create a new one instead' ) self._init()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:preview; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:stream; 7, attribute; 7, 8; 7, 9; 8, identifier:sys; 9, identifier:stdout; 10, block; 10, 11; 10, 13; 10, 19; 10, 35; 10, 40; 10, 51; 10, 56; 10, 63; 10, 68; 10, 74; 10, 96; 10, 101; 10, 112; 10, 121; 10, 134; 10, 139; 10, 198; 10, 209; 10, 218; 10, 231; 10, 236; 10, 295; 10, 306; 10, 311; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:write; 16, attribute; 16, 17; 16, 18; 17, identifier:stream; 18, identifier:write; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:write; 22, argument_list; 22, 23; 23, call; 23, 24; 23, 32; 24, attribute; 24, 25; 24, 31; 25, parenthesized_expression; 25, 26; 26, binary_operator:%; 26, 27; 26, 28; 27, string:'[Quick preview of Docpie %s]'; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:_version; 31, identifier:center; 32, argument_list; 32, 33; 32, 34; 33, integer:80; 34, string:'='; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:write; 38, argument_list; 38, 39; 39, string:'\n'; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:write; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, string:' sections '; 47, identifier:center; 48, argument_list; 48, 49; 48, 50; 49, integer:80; 50, string:'-'; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:write; 54, argument_list; 54, 55; 55, string:'\n'; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:write; 59, argument_list; 59, 60; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:usage_text; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:write; 66, argument_list; 66, 67; 67, string:'\n'; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:option_sections; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:option_sections; 74, if_statement; 74, 75; 74, 76; 75, identifier:option_sections; 76, block; 76, 77; 76, 82; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:write; 80, argument_list; 80, 81; 81, string:'\n'; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:write; 85, argument_list; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, string:'\n'; 89, identifier:join; 90, argument_list; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:option_sections; 94, identifier:values; 95, argument_list; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:write; 99, argument_list; 99, 100; 100, string:'\n'; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 104; 103, identifier:write; 104, argument_list; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, string:' str '; 108, identifier:center; 109, argument_list; 109, 110; 109, 111; 110, integer:80; 111, string:'-'; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:write; 115, argument_list; 115, 116; 116, binary_operator:%; 116, 117; 116, 118; 117, string:'\n[%s]\n'; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:usage_name; 121, for_statement; 121, 122; 121, 123; 121, 126; 122, identifier:each; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:usages; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:write; 130, argument_list; 130, 131; 131, binary_operator:%; 131, 132; 131, 133; 132, string:' %s\n'; 133, identifier:each; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:write; 137, argument_list; 137, 138; 138, string:'\n[Options:]\n\n'; 139, for_statement; 139, 140; 139, 143; 139, 150; 140, pattern_list; 140, 141; 140, 142; 141, identifier:title; 142, identifier:sections; 143, call; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:options; 148, identifier:items; 149, argument_list; 150, block; 150, 151; 150, 172; 150, 177; 150, 182; 150, 193; 151, if_statement; 151, 152; 151, 153; 151, 164; 152, identifier:title; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:full_title; 157, binary_operator:%; 157, 158; 157, 159; 158, string:'%s %s'; 159, tuple; 159, 160; 159, 161; 160, identifier:title; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:option_name; 164, else_clause; 164, 165; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:full_title; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:option_name; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:write; 175, argument_list; 175, 176; 176, identifier:full_title; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:write; 180, argument_list; 180, 181; 181, string:'\n'; 182, for_statement; 182, 183; 182, 184; 182, 185; 183, identifier:each; 184, identifier:sections; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:write; 189, argument_list; 189, 190; 190, binary_operator:%; 190, 191; 190, 192; 191, string:' %s\n'; 192, identifier:each; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 196; 195, identifier:write; 196, argument_list; 196, 197; 197, string:'\n'; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 201; 200, identifier:write; 201, argument_list; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, string:' repr '; 205, identifier:center; 206, argument_list; 206, 207; 206, 208; 207, integer:80; 208, string:'-'; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 212; 211, identifier:write; 212, argument_list; 212, 213; 213, binary_operator:%; 213, 214; 213, 215; 214, string:'\n[%s]\n'; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:usage_name; 218, for_statement; 218, 219; 218, 220; 218, 223; 219, identifier:each; 220, attribute; 220, 221; 220, 222; 221, identifier:self; 222, identifier:usages; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:write; 227, argument_list; 227, 228; 228, binary_operator:%; 228, 229; 228, 230; 229, string:' %r\n'; 230, identifier:each; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 234; 233, identifier:write; 234, argument_list; 234, 235; 235, string:'\n[Options:]\n\n'; 236, for_statement; 236, 237; 236, 240; 236, 247; 237, pattern_list; 237, 238; 237, 239; 238, identifier:title; 239, identifier:sections; 240, call; 240, 241; 240, 246; 241, attribute; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:options; 245, identifier:items; 246, argument_list; 247, block; 247, 248; 247, 269; 247, 274; 247, 279; 247, 290; 248, if_statement; 248, 249; 248, 250; 248, 261; 249, identifier:title; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:full_title; 254, binary_operator:%; 254, 255; 254, 256; 255, string:'%s %s'; 256, tuple; 256, 257; 256, 258; 257, identifier:title; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:option_name; 261, else_clause; 261, 262; 262, block; 262, 263; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:full_title; 266, attribute; 266, 267; 266, 268; 267, identifier:self; 268, identifier:option_name; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:write; 272, argument_list; 272, 273; 273, identifier:full_title; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 277; 276, identifier:write; 277, argument_list; 277, 278; 278, string:'\n'; 279, for_statement; 279, 280; 279, 281; 279, 282; 280, identifier:each; 281, identifier:sections; 282, block; 282, 283; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 286; 285, identifier:write; 286, argument_list; 286, 287; 287, binary_operator:%; 287, 288; 287, 289; 288, string:' %r\n'; 289, identifier:each; 290, expression_statement; 290, 291; 291, call; 291, 292; 291, 293; 292, identifier:write; 293, argument_list; 293, 294; 294, string:'\n'; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 298; 297, identifier:write; 298, argument_list; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, string:' auto handlers '; 302, identifier:center; 303, argument_list; 303, 304; 303, 305; 304, integer:80; 305, string:'-'; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 309; 308, identifier:write; 309, argument_list; 309, 310; 310, string:'\n'; 311, for_statement; 311, 312; 311, 315; 311, 322; 312, pattern_list; 312, 313; 312, 314; 313, identifier:key; 314, identifier:value; 315, call; 315, 316; 315, 321; 316, attribute; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:self; 319, identifier:extra; 320, identifier:items; 321, argument_list; 322, block; 322, 323; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 326; 325, identifier:write; 326, argument_list; 326, 327; 327, binary_operator:%; 327, 328; 327, 329; 328, string:'%s %s\n'; 329, tuple; 329, 330; 329, 331; 330, identifier:key; 331, identifier:value
def preview(self, stream=sys.stdout): """A quick preview of docpie. Print all the parsed object""" write = stream.write write(('[Quick preview of Docpie %s]' % self._version).center(80, '=')) write('\n') write(' sections '.center(80, '-')) write('\n') write(self.usage_text) write('\n') option_sections = self.option_sections if option_sections: write('\n') write('\n'.join(option_sections.values())) write('\n') write(' str '.center(80, '-')) write('\n[%s]\n' % self.usage_name) for each in self.usages: write(' %s\n' % each) write('\n[Options:]\n\n') for title, sections in self.options.items(): if title: full_title = '%s %s' % (title, self.option_name) else: full_title = self.option_name write(full_title) write('\n') for each in sections: write(' %s\n' % each) write('\n') write(' repr '.center(80, '-')) write('\n[%s]\n' % self.usage_name) for each in self.usages: write(' %r\n' % each) write('\n[Options:]\n\n') for title, sections in self.options.items(): if title: full_title = '%s %s' % (title, self.option_name) else: full_title = self.option_name write(full_title) write('\n') for each in sections: write(' %r\n' % each) write('\n') write(' auto handlers '.center(80, '-')) write('\n') for key, value in self.extra.items(): write('%s %s\n' % (key, value))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parts; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 18; 5, 22; 5, 23; 5, 24; 5, 28; 5, 29; 5, 30; 5, 34; 5, 45; 5, 49; 5, 120; 5, 192; 5, 220; 5, 255; 5, 256; 5, 265; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:parts; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:upserts; 15, call; 15, 16; 15, 17; 16, identifier:dict; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:deletes; 21, list:[]; 22, comment; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:max_upload_size; 27, integer:700000; 28, comment; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:base_part_size; 33, integer:118; 34, if_statement; 34, 35; 34, 39; 35, not_operator; 35, 36; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:replace_all; 39, block; 39, 40; 39, 44; 40, expression_statement; 40, 41; 41, augmented_assignment:+=; 41, 42; 41, 43; 42, identifier:base_part_size; 43, integer:1; 44, comment; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:part_size; 48, identifier:base_part_size; 49, for_statement; 49, 50; 49, 51; 49, 54; 50, identifier:value; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:upserts; 54, block; 54, 55; 54, 96; 54, 97; 54, 111; 54, 119; 55, if_statement; 55, 56; 55, 66; 55, 67; 56, comparison_operator:>=; 56, 57; 56, 65; 57, parenthesized_expression; 57, 58; 58, binary_operator:+; 58, 59; 58, 60; 59, identifier:part_size; 60, subscript; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:upserts_size; 64, identifier:value; 65, identifier:max_upload_size; 66, comment; 67, block; 67, 68; 67, 82; 67, 88; 67, 92; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:parts; 72, identifier:append; 73, argument_list; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:BatchPart; 76, argument_list; 76, 77; 76, 80; 76, 81; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:replace_all; 80, identifier:upserts; 81, identifier:deletes; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:upserts; 85, call; 85, 86; 85, 87; 86, identifier:dict; 87, argument_list; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:deletes; 91, list:[]; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:part_size; 95, identifier:base_part_size; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 106; 99, subscript; 99, 100; 99, 101; 100, identifier:upserts; 101, subscript; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:lower_val_to_val; 105, identifier:value; 106, subscript; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:upserts; 110, identifier:value; 111, expression_statement; 111, 112; 112, augmented_assignment:+=; 112, 113; 112, 114; 113, identifier:part_size; 114, subscript; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:upserts_size; 118, identifier:value; 119, comment; 120, for_statement; 120, 121; 120, 122; 120, 125; 120, 126; 121, identifier:value; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:deletes; 125, comment; 126, block; 126, 127; 126, 168; 126, 169; 126, 183; 127, if_statement; 127, 128; 127, 139; 128, comparison_operator:>=; 128, 129; 128, 138; 129, parenthesized_expression; 129, 130; 130, binary_operator:+; 130, 131; 130, 137; 131, binary_operator:+; 131, 132; 131, 133; 132, identifier:part_size; 133, call; 133, 134; 133, 135; 134, identifier:len; 135, argument_list; 135, 136; 136, identifier:value; 137, integer:4; 138, identifier:max_upload_size; 139, block; 139, 140; 139, 154; 139, 160; 139, 164; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:parts; 144, identifier:append; 145, argument_list; 145, 146; 146, call; 146, 147; 146, 148; 147, identifier:BatchPart; 148, argument_list; 148, 149; 148, 152; 148, 153; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:replace_all; 152, identifier:upserts; 153, identifier:deletes; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:upserts; 157, call; 157, 158; 157, 159; 158, identifier:dict; 159, argument_list; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:deletes; 163, list:[]; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:part_size; 167, identifier:base_part_size; 168, comment; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:deletes; 173, identifier:append; 174, argument_list; 174, 175; 175, dictionary; 175, 176; 176, pair; 176, 177; 176, 178; 177, string:'value'; 178, subscript; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:lower_val_to_val; 182, identifier:value; 183, expression_statement; 183, 184; 184, augmented_assignment:+=; 184, 185; 184, 186; 185, identifier:part_size; 186, binary_operator:+; 186, 187; 186, 191; 187, call; 187, 188; 187, 189; 188, identifier:len; 189, argument_list; 189, 190; 190, identifier:value; 191, integer:4; 192, if_statement; 192, 193; 192, 204; 192, 205; 193, comparison_operator:>; 193, 194; 193, 203; 194, binary_operator:+; 194, 195; 194, 199; 195, call; 195, 196; 195, 197; 196, identifier:len; 197, argument_list; 197, 198; 198, identifier:upserts; 199, call; 199, 200; 199, 201; 200, identifier:len; 201, argument_list; 201, 202; 202, identifier:deletes; 203, integer:0; 204, comment; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:parts; 210, identifier:append; 211, argument_list; 211, 212; 212, call; 212, 213; 212, 214; 213, identifier:BatchPart; 214, argument_list; 214, 215; 214, 218; 214, 219; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:replace_all; 218, identifier:upserts; 219, identifier:deletes; 220, if_statement; 220, 221; 220, 227; 221, comparison_operator:==; 221, 222; 221, 226; 222, call; 222, 223; 222, 224; 223, identifier:len; 224, argument_list; 224, 225; 225, identifier:parts; 226, integer:0; 227, block; 227, 228; 227, 239; 228, if_statement; 228, 229; 228, 233; 229, not_operator; 229, 230; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:replace_all; 233, block; 233, 234; 234, raise_statement; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:ValueError; 237, argument_list; 237, 238; 238, string:"Batch has no data, and 'replace_all' is False"; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:parts; 243, identifier:append; 244, argument_list; 244, 245; 245, call; 245, 246; 245, 247; 246, identifier:BatchPart; 247, argument_list; 247, 248; 247, 251; 247, 254; 248, attribute; 248, 249; 248, 250; 249, identifier:self; 250, identifier:replace_all; 251, call; 251, 252; 251, 253; 252, identifier:dict; 253, argument_list; 254, list:[]; 255, comment; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 264; 258, attribute; 258, 259; 258, 263; 259, subscript; 259, 260; 259, 261; 260, identifier:parts; 261, unary_operator:-; 261, 262; 262, integer:1; 263, identifier:set_last_part; 264, argument_list; 265, return_statement; 265, 266; 266, identifier:parts
def parts(self): """Return an array of batch parts to submit""" parts = [] upserts = dict() deletes = [] # we keep track of the batch size as we go (pretty close approximation!) so we can chunk it small enough # to limit the HTTP posts to under 700KB - server limits to 750KB, so play it safe max_upload_size = 700000 # loop upserts first - fit the deletes in afterward # '{"replace_all": true, "complete": false, "guid": "6659fbfc-3f08-42ee-998c-9109f650f4b7", "upserts": [], "deletes": []}' base_part_size = 118 if not self.replace_all: base_part_size += 1 # yeah, this is totally overkill :) part_size = base_part_size for value in self.upserts: if (part_size + self.upserts_size[value]) >= max_upload_size: # this record would put us over the limit - close out the batch part and start a new one parts.append(BatchPart(self.replace_all, upserts, deletes)) upserts = dict() deletes = [] part_size = base_part_size # for the new upserts dict, drop the lower-casing of value upserts[self.lower_val_to_val[value]] = self.upserts[value] part_size += self.upserts_size[value] # updating the approximate size of the batch for value in self.deletes: # delete adds length of string plus quotes, comma and space if (part_size + len(value) + 4) >= max_upload_size: parts.append(BatchPart(self.replace_all, upserts, deletes)) upserts = dict() deletes = [] part_size = base_part_size # for the new deletes set, drop the lower-casing of value deletes.append({'value': self.lower_val_to_val[value]}) part_size += len(value) + 4 if len(upserts) + len(deletes) > 0: # finish the batch parts.append(BatchPart(self.replace_all, upserts, deletes)) if len(parts) == 0: if not self.replace_all: raise ValueError("Batch has no data, and 'replace_all' is False") parts.append(BatchPart(self.replace_all, dict(), [])) # last part finishes the batch parts[-1].set_last_part() return parts
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:compare_versions; 3, parameters; 3, 4; 3, 5; 4, identifier:version_a; 5, identifier:version_b; 6, block; 6, 7; 6, 9; 6, 18; 6, 25; 6, 57; 6, 167; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:logger; 13, identifier:debug; 14, argument_list; 14, 15; 14, 16; 14, 17; 15, string:'compare_versions(%s, %s)'; 16, identifier:version_a; 17, identifier:version_b; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:==; 19, 20; 19, 21; 20, identifier:version_a; 21, identifier:version_b; 22, block; 22, 23; 23, return_statement; 23, 24; 24, identifier:a_eq_b; 25, try_statement; 25, 26; 25, 41; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, pattern_list; 29, 30; 29, 31; 30, identifier:chars_a; 31, identifier:chars_b; 32, expression_list; 32, 33; 32, 37; 33, call; 33, 34; 33, 35; 34, identifier:list; 35, argument_list; 35, 36; 36, identifier:version_a; 37, call; 37, 38; 37, 39; 38, identifier:list; 39, argument_list; 39, 40; 40, identifier:version_b; 41, except_clause; 41, 42; 41, 43; 42, identifier:TypeError; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:RpmError; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, concatenated_string; 50, 51; 50, 52; 51, string:'Could not compare {0} to '; 52, string:'{1}'; 53, identifier:format; 54, argument_list; 54, 55; 54, 56; 55, identifier:version_a; 56, identifier:version_b; 57, while_statement; 57, 58; 57, 71; 58, boolean_operator:and; 58, 59; 58, 65; 59, comparison_operator:!=; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:chars_a; 64, integer:0; 65, comparison_operator:!=; 65, 66; 65, 70; 66, call; 66, 67; 66, 68; 67, identifier:len; 68, argument_list; 68, 69; 69, identifier:chars_b; 70, integer:0; 71, block; 71, 72; 71, 83; 71, 89; 71, 136; 71, 152; 71, 160; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:logger; 76, identifier:debug; 77, argument_list; 77, 78; 77, 81; 77, 82; 78, concatenated_string; 78, 79; 78, 80; 79, string:'starting loop comparing %s '; 80, string:'to %s'; 81, identifier:chars_a; 82, identifier:chars_b; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:_check_leading; 86, argument_list; 86, 87; 86, 88; 87, identifier:chars_a; 88, identifier:chars_b; 89, if_statement; 89, 90; 89, 101; 89, 118; 89, 127; 90, boolean_operator:and; 90, 91; 90, 96; 91, comparison_operator:==; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:chars_a; 94, integer:0; 95, string:'~'; 96, comparison_operator:==; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:chars_b; 99, integer:0; 100, string:'~'; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:map; 105, argument_list; 105, 106; 105, 115; 106, lambda; 106, 107; 106, 109; 107, lambda_parameters; 107, 108; 108, identifier:x; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:x; 112, identifier:pop; 113, argument_list; 113, 114; 114, integer:0; 115, tuple; 115, 116; 115, 117; 116, identifier:chars_a; 117, identifier:chars_b; 118, elif_clause; 118, 119; 118, 124; 119, comparison_operator:==; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:chars_a; 122, integer:0; 123, string:'~'; 124, block; 124, 125; 125, return_statement; 125, 126; 126, identifier:b_newer; 127, elif_clause; 127, 128; 127, 133; 128, comparison_operator:==; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:chars_b; 131, integer:0; 132, string:'~'; 133, block; 133, 134; 134, return_statement; 134, 135; 135, identifier:a_newer; 136, if_statement; 136, 137; 136, 150; 137, boolean_operator:or; 137, 138; 137, 144; 138, comparison_operator:==; 138, 139; 138, 143; 139, call; 139, 140; 139, 141; 140, identifier:len; 141, argument_list; 141, 142; 142, identifier:chars_a; 143, integer:0; 144, comparison_operator:==; 144, 145; 144, 149; 145, call; 145, 146; 145, 147; 146, identifier:len; 147, argument_list; 147, 148; 148, identifier:chars_b; 149, integer:0; 150, block; 150, 151; 151, break_statement; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:block_res; 155, call; 155, 156; 155, 157; 156, identifier:_get_block_result; 157, argument_list; 157, 158; 157, 159; 158, identifier:chars_a; 159, identifier:chars_b; 160, if_statement; 160, 161; 160, 164; 161, comparison_operator:!=; 161, 162; 161, 163; 162, identifier:block_res; 163, identifier:a_eq_b; 164, block; 164, 165; 165, return_statement; 165, 166; 166, identifier:block_res; 167, if_statement; 167, 168; 167, 177; 167, 187; 168, comparison_operator:==; 168, 169; 168, 173; 169, call; 169, 170; 169, 171; 170, identifier:len; 171, argument_list; 171, 172; 172, identifier:chars_a; 173, call; 173, 174; 173, 175; 174, identifier:len; 175, argument_list; 175, 176; 176, identifier:chars_b; 177, block; 177, 178; 177, 185; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:logger; 182, identifier:debug; 183, argument_list; 183, 184; 184, string:'versions are equal'; 185, return_statement; 185, 186; 186, identifier:a_eq_b; 187, else_clause; 187, 188; 188, block; 188, 189; 188, 196; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:logger; 193, identifier:debug; 194, argument_list; 194, 195; 195, string:'versions not equal'; 196, return_statement; 196, 197; 197, conditional_expression:if; 197, 198; 197, 199; 197, 208; 198, identifier:a_newer; 199, comparison_operator:>; 199, 200; 199, 204; 200, call; 200, 201; 200, 202; 201, identifier:len; 202, argument_list; 202, 203; 203, identifier:chars_a; 204, call; 204, 205; 204, 206; 205, identifier:len; 206, argument_list; 206, 207; 207, identifier:chars_b; 208, identifier:b_newer
def compare_versions(version_a, version_b): """Compare two RPM version strings Compares two RPM version strings and returns an integer indicating the result of the comparison. The method of comparison mirrors that used by RPM, so results should be the same for any standard RPM package. To perform the comparison, the strings are first checked for equality. If they are equal, the versions are equal. Otherwise, each string is converted to a character list, and a comparison loop is started using these lists. In the comparison loop, first any non-alphanumeric, non-~ characters are trimmed from the front of the list. Then if the first character from both ``a`` and ``b`` is a ~ (tilde), it is trimmed. The ~ (tilde) character indicates that a given package or version should be considered older (even if it is numerically larger), so if ``a`` begins with a tilde, ``b`` is newer, and vice-versa. At this point, if the length of either list has been reduced to 0, the loop is exited. If characters remain in the list, the :any:`_get_block_result` function is used to pop consecutive digits or letters from the front of hte list and compare them. The result of the block comparison is returned if the blocks are not equal. The loop then begins again. If the loop exits without returning a value, the lengths of the remaining character lists are compared. If they have the same length (usually 0, since all characters have been popped), they are considered to be equal. Otherwise, whichever is longer is considered to be newer. Generally, unequal length will be due to one character list having been completely consumed while some characters remain on the other, for example when comparing 1.05b to 1.05. :param unicode version_a: An RPM version or release string :param unicode version_b: An RPM version or release string :return: 1 (if ``a`` is newer), 0 (if versions are equal), or -1 (if ``b`` is newer) :rtype: int :raises RpmError: if an a type is passed that cannot be converted to a list """ logger.debug('compare_versions(%s, %s)', version_a, version_b) if version_a == version_b: return a_eq_b try: chars_a, chars_b = list(version_a), list(version_b) except TypeError: raise RpmError('Could not compare {0} to ' '{1}'.format(version_a, version_b)) while len(chars_a) != 0 and len(chars_b) != 0: logger.debug('starting loop comparing %s ' 'to %s', chars_a, chars_b) _check_leading(chars_a, chars_b) if chars_a[0] == '~' and chars_b[0] == '~': map(lambda x: x.pop(0), (chars_a, chars_b)) elif chars_a[0] == '~': return b_newer elif chars_b[0] == '~': return a_newer if len(chars_a) == 0 or len(chars_b) == 0: break block_res = _get_block_result(chars_a, chars_b) if block_res != a_eq_b: return block_res if len(chars_a) == len(chars_b): logger.debug('versions are equal') return a_eq_b else: logger.debug('versions not equal') return a_newer if len(chars_a) > len(chars_b) else b_newer
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_compare_blocks; 3, parameters; 3, 4; 3, 5; 4, identifier:block_a; 5, identifier:block_b; 6, block; 6, 7; 6, 9; 6, 18; 6, 64; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:logger; 13, identifier:debug; 14, argument_list; 14, 15; 14, 16; 14, 17; 15, string:'_compare_blocks(%s, %s)'; 16, identifier:block_a; 17, identifier:block_b; 18, if_statement; 18, 19; 18, 26; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, subscript; 21, 22; 21, 23; 22, identifier:block_a; 23, integer:0; 24, identifier:isdigit; 25, argument_list; 26, block; 26, 27; 26, 33; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:_trim_zeros; 30, argument_list; 30, 31; 30, 32; 31, identifier:block_a; 32, identifier:block_b; 33, if_statement; 33, 34; 33, 43; 34, comparison_operator:!=; 34, 35; 34, 39; 35, call; 35, 36; 35, 37; 36, identifier:len; 37, argument_list; 37, 38; 38, identifier:block_a; 39, call; 39, 40; 39, 41; 40, identifier:len; 41, argument_list; 41, 42; 42, identifier:block_b; 43, block; 43, 44; 43, 51; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:logger; 48, identifier:debug; 49, argument_list; 49, 50; 50, string:'block lengths are not equal'; 51, return_statement; 51, 52; 52, conditional_expression:if; 52, 53; 52, 54; 52, 63; 53, identifier:a_newer; 54, comparison_operator:>; 54, 55; 54, 59; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, identifier:block_a; 59, call; 59, 60; 59, 61; 60, identifier:len; 61, argument_list; 61, 62; 62, identifier:block_b; 63, identifier:b_newer; 64, if_statement; 64, 65; 64, 68; 64, 78; 65, comparison_operator:==; 65, 66; 65, 67; 66, identifier:block_a; 67, identifier:block_b; 68, block; 68, 69; 68, 76; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:logger; 73, identifier:debug; 74, argument_list; 74, 75; 75, string:'blocks are equal'; 76, return_statement; 76, 77; 77, identifier:a_eq_b; 78, else_clause; 78, 79; 79, block; 79, 80; 79, 87; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:logger; 84, identifier:debug; 85, argument_list; 85, 86; 86, string:'blocks are not equal'; 87, return_statement; 87, 88; 88, conditional_expression:if; 88, 89; 88, 90; 88, 93; 89, identifier:a_newer; 90, comparison_operator:>; 90, 91; 90, 92; 91, identifier:block_a; 92, identifier:block_b; 93, identifier:b_newer
def _compare_blocks(block_a, block_b): """Compare two blocks of characters Compares two blocks of characters of the form returned by either the :any:`_pop_digits` or :any:`_pop_letters` function. Blocks should be character lists containing only digits or only letters. Both blocks should contain the same character type (digits or letters). The method of comparison mirrors the method used by RPM. If the blocks are digit blocks, any leading zeros are trimmed, and whichever block is longer is assumed to be larger. If the resultant blocks are the same length, or if the blocks are non-numeric, they are checked for string equality and considered equal if the string equality comparison returns True. If not, whichever evaluates as greater than the other (again in string comparison) is assumed to be larger. :param list block_a: an all numeric or all alphabetic character list :param list block_b: an all numeric or all alphabetic character list. Alphabetic or numeric character should match ``block_a`` :return: 1 (if ``a`` is newer), 0 (if versions are equal) or -1 (if ``b`` is newer) :rtype: int """ logger.debug('_compare_blocks(%s, %s)', block_a, block_b) if block_a[0].isdigit(): _trim_zeros(block_a, block_b) if len(block_a) != len(block_b): logger.debug('block lengths are not equal') return a_newer if len(block_a) > len(block_b) else b_newer if block_a == block_b: logger.debug('blocks are equal') return a_eq_b else: logger.debug('blocks are not equal') return a_newer if block_a > block_b else b_newer
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:Find; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:node_type; 6, identifier:item_type; 7, block; 7, 8; 7, 10; 7, 75; 8, expression_statement; 8, 9; 9, string:''' method for finding specific types of notation from nodes. will currently return the first one it encounters because this method's only really intended for some types of notation for which the exact value doesn't really matter. :param node_type: the type of node to look under :param item_type: the type of item (notation) being searched for :return: first item_type object encountered '''; 10, if_statement; 10, 11; 10, 16; 11, comparison_operator:==; 11, 12; 11, 13; 12, identifier:node_type; 13, attribute; 13, 14; 13, 15; 14, identifier:OtherNodes; 15, identifier:DirectionNode; 16, block; 16, 17; 16, 33; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:child; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:GetChild; 24, argument_list; 24, 25; 25, binary_operator:-; 25, 26; 25, 32; 26, call; 26, 27; 26, 28; 27, identifier:len; 28, argument_list; 28, 29; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:children; 32, integer:1; 33, while_statement; 33, 34; 33, 48; 34, boolean_operator:and; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:child; 37, None; 38, not_operator; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:isinstance; 41, argument_list; 41, 42; 41, 47; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:child; 45, identifier:GetItem; 46, argument_list; 47, identifier:item_type; 48, block; 48, 49; 48, 66; 49, if_statement; 49, 50; 49, 63; 50, comparison_operator:==; 50, 51; 50, 60; 51, attribute; 51, 52; 51, 59; 52, attribute; 52, 53; 52, 58; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:child; 56, identifier:GetItem; 57, argument_list; 58, identifier:__class__; 59, identifier:__name__; 60, attribute; 60, 61; 60, 62; 61, identifier:item_type; 62, identifier:__name__; 63, block; 63, 64; 64, return_statement; 64, 65; 65, True; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:child; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:child; 72, identifier:GetChild; 73, argument_list; 73, 74; 74, integer:0; 75, if_statement; 75, 76; 75, 81; 76, comparison_operator:==; 76, 77; 76, 78; 77, identifier:node_type; 78, attribute; 78, 79; 78, 80; 79, identifier:OtherNodes; 80, identifier:ExpressionNode; 81, block; 81, 82; 81, 98; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:child; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:GetChild; 89, argument_list; 89, 90; 90, binary_operator:-; 90, 91; 90, 97; 91, call; 91, 92; 91, 93; 92, identifier:len; 93, argument_list; 93, 94; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:children; 97, integer:2; 98, while_statement; 98, 99; 98, 113; 99, boolean_operator:and; 99, 100; 99, 103; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:child; 102, None; 103, not_operator; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:isinstance; 106, argument_list; 106, 107; 106, 112; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:child; 110, identifier:GetItem; 111, argument_list; 112, identifier:item_type; 113, block; 113, 114; 113, 131; 114, if_statement; 114, 115; 114, 128; 115, comparison_operator:==; 115, 116; 115, 125; 116, attribute; 116, 117; 116, 124; 117, attribute; 117, 118; 117, 123; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:child; 121, identifier:GetItem; 122, argument_list; 123, identifier:__class__; 124, identifier:__name__; 125, attribute; 125, 126; 125, 127; 126, identifier:item_type; 127, identifier:__name__; 128, block; 128, 129; 129, return_statement; 129, 130; 130, True; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:child; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:child; 137, identifier:GetChild; 138, argument_list; 138, 139; 139, integer:0
def Find(self, node_type, item_type): ''' method for finding specific types of notation from nodes. will currently return the first one it encounters because this method's only really intended for some types of notation for which the exact value doesn't really matter. :param node_type: the type of node to look under :param item_type: the type of item (notation) being searched for :return: first item_type object encountered ''' if node_type == OtherNodes.DirectionNode: child = self.GetChild(len(self.children) - 1) while child is not None and not isinstance( child.GetItem(), item_type): if child.GetItem().__class__.__name__ == item_type.__name__: return True child = child.GetChild(0) if node_type == OtherNodes.ExpressionNode: child = self.GetChild(len(self.children) - 2) while child is not None and not isinstance( child.GetItem(), item_type): if child.GetItem().__class__.__name__ == item_type.__name__: return True child = child.GetChild(0)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:SplitString; 3, parameters; 3, 4; 4, identifier:value; 5, block; 5, 6; 5, 8; 5, 15; 5, 24; 5, 31; 5, 35; 5, 85; 5, 248; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:string_length; 11, call; 11, 12; 11, 13; 12, identifier:len; 13, argument_list; 13, 14; 14, identifier:value; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:chunks; 18, call; 18, 19; 18, 20; 19, identifier:int; 20, argument_list; 20, 21; 21, binary_operator:/; 21, 22; 21, 23; 22, identifier:string_length; 23, integer:10; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:string_list; 27, call; 27, 28; 27, 29; 28, identifier:list; 29, argument_list; 29, 30; 30, identifier:value; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:lstring; 34, string:""; 35, if_statement; 35, 36; 35, 39; 36, comparison_operator:>; 36, 37; 36, 38; 37, identifier:chunks; 38, integer:1; 39, block; 39, 40; 39, 44; 39, 81; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:lstring; 43, string:"\\markup { \n\r \column { "; 44, for_statement; 44, 45; 44, 46; 44, 53; 45, identifier:i; 46, call; 46, 47; 46, 48; 47, identifier:range; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:int; 51, argument_list; 51, 52; 52, identifier:chunks; 53, block; 53, 54; 53, 58; 53, 64; 53, 77; 54, expression_statement; 54, 55; 55, augmented_assignment:+=; 55, 56; 55, 57; 56, identifier:lstring; 57, string:"\n\r\r \\line { \""; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:index; 61, binary_operator:*; 61, 62; 61, 63; 62, identifier:i; 63, integer:10; 64, for_statement; 64, 65; 64, 66; 64, 70; 65, identifier:i; 66, call; 66, 67; 66, 68; 67, identifier:range; 68, argument_list; 68, 69; 69, identifier:index; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, augmented_assignment:+=; 72, 73; 72, 74; 73, identifier:lstring; 74, subscript; 74, 75; 74, 76; 75, identifier:string_list; 76, identifier:i; 77, expression_statement; 77, 78; 78, augmented_assignment:+=; 78, 79; 78, 80; 79, identifier:lstring; 80, string:"\" \r\r}"; 81, expression_statement; 81, 82; 82, augmented_assignment:+=; 82, 83; 82, 84; 83, identifier:lstring; 84, string:"\n\r } \n }"; 85, if_statement; 85, 86; 85, 89; 86, comparison_operator:==; 86, 87; 86, 88; 87, identifier:lstring; 88, string:""; 89, block; 89, 90; 89, 116; 89, 120; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:indexes; 93, list_comprehension; 93, 94; 93, 95; 93, 104; 94, identifier:i; 95, for_in_clause; 95, 96; 95, 97; 96, identifier:i; 97, call; 97, 98; 97, 99; 98, identifier:range; 99, argument_list; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, identifier:string_list; 104, if_clause; 104, 105; 105, boolean_operator:or; 105, 106; 105, 111; 106, comparison_operator:==; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:string_list; 109, identifier:i; 110, string:"\r"; 111, comparison_operator:==; 111, 112; 111, 115; 112, subscript; 112, 113; 112, 114; 113, identifier:string_list; 114, identifier:i; 115, string:"\n"; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:lstring; 119, string:"\\markup { \n\r \column { "; 120, if_statement; 120, 121; 120, 127; 120, 141; 121, comparison_operator:==; 121, 122; 121, 126; 122, call; 122, 123; 122, 124; 123, identifier:len; 124, argument_list; 124, 125; 125, identifier:indexes; 126, integer:0; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, augmented_assignment:+=; 129, 130; 129, 131; 130, identifier:lstring; 131, binary_operator:+; 131, 132; 131, 140; 132, binary_operator:+; 132, 133; 132, 134; 133, string:"\n\r\r \\line { \""; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, string:""; 137, identifier:join; 138, argument_list; 138, 139; 139, identifier:string_list; 140, string:"\" \n\r\r } \n\r } \n }"; 141, else_clause; 141, 142; 142, block; 142, 143; 142, 147; 142, 157; 142, 164; 142, 223; 142, 244; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:rows; 146, list:[]; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:row_1; 150, subscript; 150, 151; 150, 152; 151, identifier:string_list; 152, slice; 152, 153; 152, 154; 153, colon; 154, subscript; 154, 155; 154, 156; 155, identifier:indexes; 156, integer:0; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:rows; 161, identifier:append; 162, argument_list; 162, 163; 163, identifier:row_1; 164, for_statement; 164, 165; 164, 166; 164, 173; 165, identifier:i; 166, call; 166, 167; 166, 168; 167, identifier:range; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:len; 171, argument_list; 171, 172; 172, identifier:indexes; 173, block; 173, 174; 173, 180; 173, 207; 173, 216; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:start; 177, subscript; 177, 178; 177, 179; 178, identifier:indexes; 179, identifier:i; 180, if_statement; 180, 181; 180, 189; 180, 198; 181, comparison_operator:!=; 181, 182; 181, 183; 182, identifier:i; 183, binary_operator:-; 183, 184; 183, 188; 184, call; 184, 185; 184, 186; 185, identifier:len; 186, argument_list; 186, 187; 187, identifier:indexes; 188, integer:1; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:end; 193, subscript; 193, 194; 193, 195; 194, identifier:indexes; 195, binary_operator:+; 195, 196; 195, 197; 196, identifier:i; 197, integer:1; 198, else_clause; 198, 199; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:end; 203, call; 203, 204; 203, 205; 204, identifier:len; 205, argument_list; 205, 206; 206, identifier:string_list; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:row; 210, subscript; 210, 211; 210, 212; 211, identifier:string_list; 212, slice; 212, 213; 212, 214; 212, 215; 213, identifier:start; 214, colon; 215, identifier:end; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:rows; 220, identifier:append; 221, argument_list; 221, 222; 222, identifier:row; 223, for_statement; 223, 224; 223, 225; 223, 226; 224, identifier:row; 225, identifier:rows; 226, block; 226, 227; 226, 231; 226, 240; 227, expression_statement; 227, 228; 228, augmented_assignment:+=; 228, 229; 228, 230; 229, identifier:lstring; 230, string:"\n\r\r \\line { \""; 231, expression_statement; 231, 232; 232, augmented_assignment:+=; 232, 233; 232, 234; 233, identifier:lstring; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, string:""; 237, identifier:join; 238, argument_list; 238, 239; 239, identifier:row; 240, expression_statement; 240, 241; 241, augmented_assignment:+=; 241, 242; 241, 243; 242, identifier:lstring; 243, string:"\" \r\r}"; 244, expression_statement; 244, 245; 245, augmented_assignment:+=; 245, 246; 245, 247; 246, identifier:lstring; 247, string:"\n\r } \n }"; 248, return_statement; 248, 249; 249, identifier:lstring
def SplitString(value): """simple method that puts in spaces every 10 characters""" string_length = len(value) chunks = int(string_length / 10) string_list = list(value) lstring = "" if chunks > 1: lstring = "\\markup { \n\r \column { " for i in range(int(chunks)): lstring += "\n\r\r \\line { \"" index = i * 10 for i in range(index): lstring += string_list[i] lstring += "\" \r\r}" lstring += "\n\r } \n }" if lstring == "": indexes = [ i for i in range( len(string_list)) if string_list[i] == "\r" or string_list[i] == "\n"] lstring = "\\markup { \n\r \column { " if len(indexes) == 0: lstring += "\n\r\r \\line { \"" + \ "".join(string_list) + "\" \n\r\r } \n\r } \n }" else: rows = [] row_1 = string_list[:indexes[0]] rows.append(row_1) for i in range(len(indexes)): start = indexes[i] if i != len(indexes) - 1: end = indexes[i + 1] else: end = len(string_list) row = string_list[start:end] rows.append(row) for row in rows: lstring += "\n\r\r \\line { \"" lstring += "".join(row) lstring += "\" \r\r}" lstring += "\n\r } \n }" return lstring
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:NumbersToWords; 3, parameters; 3, 4; 4, identifier:number; 5, block; 5, 6; 5, 8; 5, 21; 5, 34; 5, 38; 5, 188; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:units; 11, list:[ 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; 11, 12; 11, 13; 11, 14; 11, 15; 11, 16; 11, 17; 11, 18; 11, 19; 11, 20; 12, string:'one'; 13, string:'two'; 14, string:'three'; 15, string:'four'; 16, string:'five'; 17, string:'six'; 18, string:'seven'; 19, string:'eight'; 20, string:'nine'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:tens; 24, list:[ 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']; 24, 25; 24, 26; 24, 27; 24, 28; 24, 29; 24, 30; 24, 31; 24, 32; 24, 33; 25, string:'ten'; 26, string:'twenty'; 27, string:'thirty'; 28, string:'forty'; 29, string:'fifty'; 30, string:'sixty'; 31, string:'seventy'; 32, string:'eighty'; 33, string:'ninety'; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:output; 37, string:""; 38, if_statement; 38, 39; 38, 42; 38, 182; 39, comparison_operator:!=; 39, 40; 39, 41; 40, identifier:number; 41, integer:0; 42, block; 42, 43; 42, 50; 42, 118; 42, 160; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:str_val; 46, call; 46, 47; 46, 48; 47, identifier:str; 48, argument_list; 48, 49; 49, identifier:number; 50, if_statement; 50, 51; 50, 58; 51, comparison_operator:>; 51, 52; 51, 53; 51, 57; 52, integer:4; 53, call; 53, 54; 53, 55; 54, identifier:len; 55, argument_list; 55, 56; 56, identifier:str_val; 57, integer:2; 58, block; 58, 59; 58, 72; 58, 76; 59, expression_statement; 59, 60; 60, augmented_assignment:+=; 60, 61; 60, 62; 61, identifier:output; 62, subscript; 62, 63; 62, 64; 63, identifier:units; 64, binary_operator:-; 64, 65; 64, 71; 65, call; 65, 66; 65, 67; 66, identifier:int; 67, argument_list; 67, 68; 68, subscript; 68, 69; 68, 70; 69, identifier:str_val; 70, integer:0; 71, integer:1; 72, expression_statement; 72, 73; 73, augmented_assignment:+=; 73, 74; 73, 75; 74, identifier:output; 75, string:"hundred"; 76, if_statement; 76, 77; 76, 82; 77, comparison_operator:!=; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:str_val; 80, integer:1; 81, integer:0; 82, block; 82, 83; 82, 98; 83, expression_statement; 83, 84; 84, augmented_assignment:+=; 84, 85; 84, 86; 85, identifier:output; 86, binary_operator:+; 86, 87; 86, 88; 87, string:"and"; 88, subscript; 88, 89; 88, 90; 89, identifier:tens; 90, binary_operator:-; 90, 91; 90, 97; 91, call; 91, 92; 91, 93; 92, identifier:int; 93, argument_list; 93, 94; 94, subscript; 94, 95; 94, 96; 95, identifier:str_val; 96, integer:1; 97, integer:1; 98, if_statement; 98, 99; 98, 104; 99, comparison_operator:!=; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:str_val; 102, integer:2; 103, integer:0; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, augmented_assignment:+=; 106, 107; 106, 108; 107, identifier:output; 108, subscript; 108, 109; 108, 110; 109, identifier:units; 110, binary_operator:-; 110, 111; 110, 117; 111, call; 111, 112; 111, 113; 112, identifier:int; 113, argument_list; 113, 114; 114, subscript; 114, 115; 114, 116; 115, identifier:str_val; 116, integer:2; 117, integer:1; 118, if_statement; 118, 119; 118, 126; 119, comparison_operator:>; 119, 120; 119, 121; 119, 125; 120, integer:3; 121, call; 121, 122; 121, 123; 122, identifier:len; 123, argument_list; 123, 124; 124, identifier:str_val; 125, integer:1; 126, block; 126, 127; 126, 140; 127, expression_statement; 127, 128; 128, augmented_assignment:+=; 128, 129; 128, 130; 129, identifier:output; 130, subscript; 130, 131; 130, 132; 131, identifier:tens; 132, binary_operator:-; 132, 133; 132, 139; 133, call; 133, 134; 133, 135; 134, identifier:int; 135, argument_list; 135, 136; 136, subscript; 136, 137; 136, 138; 137, identifier:str_val; 138, integer:0; 139, integer:1; 140, if_statement; 140, 141; 140, 146; 141, comparison_operator:!=; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:str_val; 144, integer:1; 145, integer:0; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, augmented_assignment:+=; 148, 149; 148, 150; 149, identifier:output; 150, subscript; 150, 151; 150, 152; 151, identifier:units; 152, binary_operator:-; 152, 153; 152, 159; 153, call; 153, 154; 153, 155; 154, identifier:int; 155, argument_list; 155, 156; 156, subscript; 156, 157; 156, 158; 157, identifier:str_val; 158, integer:1; 159, integer:1; 160, if_statement; 160, 161; 160, 168; 161, comparison_operator:>; 161, 162; 161, 163; 161, 167; 162, integer:2; 163, call; 163, 164; 163, 165; 164, identifier:len; 165, argument_list; 165, 166; 166, identifier:str_val; 167, integer:1; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, augmented_assignment:+=; 170, 171; 170, 172; 171, identifier:output; 172, subscript; 172, 173; 172, 174; 173, identifier:units; 174, binary_operator:-; 174, 175; 174, 181; 175, call; 175, 176; 175, 177; 176, identifier:int; 177, argument_list; 177, 178; 178, subscript; 178, 179; 178, 180; 179, identifier:str_val; 180, integer:0; 181, integer:1; 182, else_clause; 182, 183; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:output; 187, string:"zero"; 188, return_statement; 188, 189; 189, identifier:output
def NumbersToWords(number): """ little function that converts numbers to words. This could be more efficient, and won't work if the number is bigger than 999 but it's for stave names, and I doubt any part would have more than 10 staves let alone 999. """ units = [ 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] tens = [ 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] output = "" if number != 0: str_val = str(number) if 4 > len(str_val) > 2: output += units[int(str_val[0]) - 1] output += "hundred" if str_val[1] != 0: output += "and" + tens[int(str_val[1]) - 1] if str_val[2] != 0: output += units[int(str_val[2]) - 1] if 3 > len(str_val) > 1: output += tens[int(str_val[0]) - 1] if str_val[1] != 0: output += units[int(str_val[1]) - 1] if 2 > len(str_val) == 1: output += units[int(str_val[0]) - 1] else: output = "zero" return output
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:_generate_image; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, identifier:self; 5, identifier:matrix; 6, identifier:width; 7, identifier:height; 8, identifier:padding; 9, identifier:foreground; 10, identifier:background; 11, identifier:image_format; 12, block; 12, 13; 12, 15; 12, 16; 12, 45; 12, 46; 12, 55; 12, 56; 12, 64; 12, 72; 12, 73; 12, 160; 12, 161; 12, 167; 12, 187; 12, 188; 12, 213; 12, 221; 12, 227; 12, 228; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:image; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:Image; 22, identifier:new; 23, argument_list; 23, 24; 23, 25; 23, 44; 24, string:"RGBA"; 25, tuple; 25, 26; 25, 35; 26, binary_operator:+; 26, 27; 26, 32; 27, binary_operator:+; 27, 28; 27, 29; 28, identifier:width; 29, subscript; 29, 30; 29, 31; 30, identifier:padding; 31, integer:2; 32, subscript; 32, 33; 32, 34; 33, identifier:padding; 34, integer:3; 35, binary_operator:+; 35, 36; 35, 41; 36, binary_operator:+; 36, 37; 36, 38; 37, identifier:height; 38, subscript; 38, 39; 38, 40; 39, identifier:padding; 40, integer:0; 41, subscript; 41, 42; 41, 43; 42, identifier:padding; 43, integer:1; 44, identifier:background; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:draw; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:ImageDraw; 52, identifier:Draw; 53, argument_list; 53, 54; 54, identifier:image; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:block_width; 59, binary_operator://; 59, 60; 59, 61; 60, identifier:width; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:columns; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:block_height; 67, binary_operator://; 67, 68; 67, 69; 68, identifier:height; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:rows; 72, comment; 73, for_statement; 73, 74; 73, 77; 73, 81; 74, pattern_list; 74, 75; 74, 76; 75, identifier:row; 76, identifier:row_columns; 77, call; 77, 78; 77, 79; 78, identifier:enumerate; 79, argument_list; 79, 80; 80, identifier:matrix; 81, block; 81, 82; 82, for_statement; 82, 83; 82, 86; 82, 90; 83, pattern_list; 83, 84; 83, 85; 84, identifier:column; 85, identifier:cell; 86, call; 86, 87; 86, 88; 87, identifier:enumerate; 88, argument_list; 88, 89; 89, identifier:row_columns; 90, block; 90, 91; 91, if_statement; 91, 92; 91, 93; 91, 94; 92, identifier:cell; 93, comment; 94, block; 94, 95; 94, 105; 94, 115; 94, 130; 94, 145; 94, 146; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:x1; 98, binary_operator:+; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:padding; 101, integer:2; 102, binary_operator:*; 102, 103; 102, 104; 103, identifier:column; 104, identifier:block_width; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:y1; 108, binary_operator:+; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:padding; 111, integer:0; 112, binary_operator:*; 112, 113; 112, 114; 113, identifier:row; 114, identifier:block_height; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:x2; 118, binary_operator:-; 118, 119; 118, 129; 119, binary_operator:+; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:padding; 122, integer:2; 123, binary_operator:*; 123, 124; 123, 128; 124, parenthesized_expression; 124, 125; 125, binary_operator:+; 125, 126; 125, 127; 126, identifier:column; 127, integer:1; 128, identifier:block_width; 129, integer:1; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:y2; 133, binary_operator:-; 133, 134; 133, 144; 134, binary_operator:+; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:padding; 137, integer:0; 138, binary_operator:*; 138, 139; 138, 143; 139, parenthesized_expression; 139, 140; 140, binary_operator:+; 140, 141; 140, 142; 141, identifier:row; 142, integer:1; 143, identifier:block_height; 144, integer:1; 145, comment; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:draw; 150, identifier:rectangle; 151, argument_list; 151, 152; 151, 157; 152, tuple; 152, 153; 152, 154; 152, 155; 152, 156; 153, identifier:x1; 154, identifier:y1; 155, identifier:x2; 156, identifier:y2; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:fill; 159, identifier:foreground; 160, comment; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:stream; 164, call; 164, 165; 164, 166; 165, identifier:BytesIO; 166, argument_list; 167, if_statement; 167, 168; 167, 175; 168, comparison_operator:==; 168, 169; 168, 174; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:image_format; 172, identifier:upper; 173, argument_list; 174, string:"JPEG"; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:image; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:image; 182, identifier:convert; 183, argument_list; 183, 184; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:mode; 186, string:"RGB"; 187, comment; 188, try_statement; 188, 189; 188, 203; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:image; 194, identifier:save; 195, argument_list; 195, 196; 195, 197; 195, 200; 196, identifier:stream; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:format; 199, identifier:image_format; 200, keyword_argument; 200, 201; 200, 202; 201, identifier:optimize; 202, True; 203, except_clause; 203, 204; 203, 205; 204, identifier:KeyError; 205, block; 205, 206; 206, raise_statement; 206, 207; 207, call; 207, 208; 207, 209; 208, identifier:ValueError; 209, argument_list; 209, 210; 210, binary_operator:%; 210, 211; 210, 212; 211, string:"Pillow does not support requested image format: %s"; 212, identifier:image_format; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:image_raw; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:stream; 219, identifier:getvalue; 220, argument_list; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:stream; 225, identifier:close; 226, argument_list; 227, comment; 228, return_statement; 228, 229; 229, identifier:image_raw
def _generate_image(self, matrix, width, height, padding, foreground, background, image_format): """ Generates an identicon image in requested image format out of the passed block matrix, with the requested width, height, padding, foreground colour, background colour, and image format. Arguments: matrix - Matrix describing which blocks in the identicon should be painted with foreground (background if inverted) colour. width - Width of resulting identicon image in pixels. height - Height of resulting identicon image in pixels. padding - Tuple describing padding around the generated identicon. The tuple should consist out of four values, where each value is the number of pixels to use for padding. The order in tuple is: top, bottom, left, right. foreground - Colour which should be used for foreground (filled blocks), represented as a string of format supported by the PIL.ImageColor module. background - Colour which should be used for background and padding, represented as a string of format supported by the PIL.ImageColor module. image_format - Format to use for the image. Format needs to be supported by the Pillow library. Returns: Identicon image in requested format, returned as a byte list. """ # Set-up a new image object, setting the background to provided value. image = Image.new("RGBA", (width + padding[2] + padding[3], height + padding[0] + padding[1]), background) # Set-up a draw image (for drawing the blocks). draw = ImageDraw.Draw(image) # Calculate the block widht and height. block_width = width // self.columns block_height = height // self.rows # Go through all the elements of a matrix, and draw the rectangles. for row, row_columns in enumerate(matrix): for column, cell in enumerate(row_columns): if cell: # Set-up the coordinates for a block. x1 = padding[2] + column * block_width y1 = padding[0] + row * block_height x2 = padding[2] + (column + 1) * block_width - 1 y2 = padding[0] + (row + 1) * block_height - 1 # Draw the rectangle. draw.rectangle((x1, y1, x2, y2), fill=foreground) # Set-up a stream where image will be saved. stream = BytesIO() if image_format.upper() == "JPEG": image = image.convert(mode="RGB") # Save the image to stream. try: image.save(stream, format=image_format, optimize=True) except KeyError: raise ValueError("Pillow does not support requested image format: %s" % image_format) image_raw = stream.getvalue() stream.close() # Return the resulting image. return image_raw
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:alignment_to_reads; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:sam_merged; 5, identifier:output_dir; 6, default_parameter; 6, 7; 6, 8; 7, identifier:parameters; 8, identifier:DEFAULT_PARAMETERS; 9, default_parameter; 9, 10; 9, 11; 10, identifier:save_memory; 11, True; 12, list_splat_pattern; 12, 13; 13, identifier:bin_fasta; 14, block; 14, 15; 14, 17; 14, 18; 14, 43; 14, 44; 14, 45; 14, 46; 14, 52; 14, 80; 14, 89; 14, 98; 14, 113; 14, 114; 14, 115; 14, 116; 14, 117; 14, 118; 14, 119; 14, 169; 14, 186; 14, 341; 14, 355; 14, 356; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, function_definition; 18, 19; 18, 20; 18, 22; 19, function_name:get_file_string; 20, parameters; 20, 21; 21, identifier:file_thing; 22, block; 22, 23; 22, 41; 23, try_statement; 23, 24; 23, 31; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:file_string; 28, attribute; 28, 29; 28, 30; 29, identifier:file_thing; 30, identifier:name; 31, except_clause; 31, 32; 31, 33; 32, identifier:AttributeError; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:file_string; 37, call; 37, 38; 37, 39; 38, identifier:str; 39, argument_list; 39, 40; 40, identifier:file_thing; 41, return_statement; 41, 42; 42, identifier:file_string; 43, comment; 44, comment; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:bin_chunks; 49, call; 49, 50; 49, 51; 50, identifier:set; 51, argument_list; 52, for_statement; 52, 53; 52, 54; 52, 55; 53, identifier:bin_file; 54, identifier:bin_fasta; 55, block; 55, 56; 56, for_statement; 56, 57; 56, 58; 56, 65; 57, identifier:record; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:SeqIO; 61, identifier:parse; 62, argument_list; 62, 63; 62, 64; 63, identifier:bin_file; 64, string:"fasta"; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:bin_chunks; 70, identifier:add; 71, argument_list; 71, 72; 72, tuple; 72, 73; 72, 77; 73, call; 73, 74; 73, 75; 74, identifier:get_file_string; 75, argument_list; 75, 76; 76, identifier:bin_file; 77, attribute; 77, 78; 77, 79; 78, identifier:record; 79, identifier:id; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:chunk_size; 83, call; 83, 84; 83, 85; 84, identifier:int; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:parameters; 88, string:"chunk_size"; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:mapq_threshold; 92, call; 92, 93; 92, 94; 93, identifier:int; 94, argument_list; 94, 95; 95, subscript; 95, 96; 95, 97; 96, identifier:parameters; 97, string:"mapq_threshold"; 98, function_definition; 98, 99; 98, 100; 98, 102; 99, function_name:read_name; 100, parameters; 100, 101; 101, identifier:read; 102, block; 102, 103; 103, return_statement; 103, 104; 104, subscript; 104, 105; 104, 112; 105, call; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:read; 109, identifier:query_name; 110, identifier:split; 111, argument_list; 112, integer:0; 113, comment; 114, comment; 115, comment; 116, comment; 117, comment; 118, comment; 119, function_definition; 119, 120; 119, 121; 119, 123; 120, function_name:get_base_name; 121, parameters; 121, 122; 122, identifier:bin_file; 123, block; 123, 124; 123, 150; 123, 167; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:base_name; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, string:"."; 130, identifier:join; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 146; 133, call; 133, 134; 133, 144; 134, attribute; 134, 135; 134, 143; 135, call; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:os; 139, identifier:path; 140, identifier:basename; 141, argument_list; 141, 142; 142, identifier:bin_file; 143, identifier:split; 144, argument_list; 144, 145; 145, string:"."; 146, slice; 146, 147; 146, 148; 147, colon; 148, unary_operator:-; 148, 149; 149, integer:1; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:output_path; 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:output_dir; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, string:"{}.readnames"; 164, identifier:format; 165, argument_list; 165, 166; 166, identifier:base_name; 167, return_statement; 167, 168; 168, identifier:output_path; 169, if_statement; 169, 170; 169, 171; 169, 178; 170, identifier:save_memory; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:opened_files; 175, call; 175, 176; 175, 177; 176, identifier:dict; 177, argument_list; 178, else_clause; 178, 179; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:read_names; 183, call; 183, 184; 183, 185; 184, identifier:dict; 185, argument_list; 186, with_statement; 186, 187; 186, 199; 187, with_clause; 187, 188; 188, with_item; 188, 189; 189, as_pattern; 189, 190; 189, 197; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:pysam; 193, identifier:AlignmentFile; 194, argument_list; 194, 195; 194, 196; 195, identifier:sam_merged; 196, string:"rb"; 197, as_pattern_target; 197, 198; 198, identifier:alignment_merged_handle; 199, block; 199, 200; 200, for_statement; 200, 201; 200, 204; 200, 211; 201, tuple_pattern; 201, 202; 201, 203; 202, identifier:my_read_name; 203, identifier:alignment_pool; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:itertools; 207, identifier:groupby; 208, argument_list; 208, 209; 208, 210; 209, identifier:alignment_merged_handle; 210, identifier:read_name; 211, block; 211, 212; 212, for_statement; 212, 213; 212, 214; 212, 215; 213, identifier:my_alignment; 214, identifier:alignment_pool; 215, block; 215, 216; 215, 222; 215, 228; 215, 234; 215, 235; 215, 245; 215, 246; 215, 254; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:relative_position; 219, attribute; 219, 220; 219, 221; 220, identifier:my_alignment; 221, identifier:reference_start; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:contig_name; 225, attribute; 225, 226; 225, 227; 226, identifier:my_alignment; 227, identifier:reference_name; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:chunk_position; 231, binary_operator://; 231, 232; 231, 233; 232, identifier:relative_position; 233, identifier:chunk_size; 234, comment; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:chunk_name; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, string:"{}_{}"; 241, identifier:format; 242, argument_list; 242, 243; 242, 244; 243, identifier:contig_name; 244, identifier:chunk_position; 245, comment; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:quality_test; 249, comparison_operator:>; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:my_alignment; 252, identifier:mapping_quality; 253, identifier:mapq_threshold; 254, for_statement; 254, 255; 254, 256; 254, 257; 255, identifier:bin_file; 256, identifier:bin_fasta; 257, block; 257, 258; 257, 264; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:chunk_tuple; 261, tuple; 261, 262; 261, 263; 262, identifier:bin_file; 263, identifier:chunk_name; 264, if_statement; 264, 265; 264, 270; 265, boolean_operator:and; 265, 266; 265, 269; 266, comparison_operator:in; 266, 267; 266, 268; 267, identifier:chunk_tuple; 268, identifier:bin_chunks; 269, identifier:quality_test; 270, block; 270, 271; 271, if_statement; 271, 272; 271, 273; 271, 318; 272, identifier:save_memory; 273, block; 273, 274; 273, 281; 273, 306; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:output_path; 277, call; 277, 278; 277, 279; 278, identifier:get_base_name; 279, argument_list; 279, 280; 280, identifier:bin_file; 281, try_statement; 281, 282; 281, 289; 282, block; 282, 283; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:output_handle; 286, subscript; 286, 287; 286, 288; 287, identifier:opened_files; 288, identifier:bin_file; 289, except_clause; 289, 290; 289, 291; 290, identifier:KeyError; 291, block; 291, 292; 291, 300; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:output_handle; 295, call; 295, 296; 295, 297; 296, identifier:open; 297, argument_list; 297, 298; 297, 299; 298, identifier:output_path; 299, string:"w"; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 305; 302, subscript; 302, 303; 302, 304; 303, identifier:opened_files; 304, identifier:bin_file; 305, identifier:output_handle; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:output_handle; 310, identifier:write; 311, argument_list; 311, 312; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, string:"@{}\n"; 315, identifier:format; 316, argument_list; 316, 317; 317, identifier:my_read_name; 318, else_clause; 318, 319; 319, block; 319, 320; 320, try_statement; 320, 321; 320, 331; 321, block; 321, 322; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 329; 324, attribute; 324, 325; 324, 328; 325, subscript; 325, 326; 325, 327; 326, identifier:read_names; 327, identifier:my_read_name; 328, identifier:append; 329, argument_list; 329, 330; 330, identifier:bin_file; 331, except_clause; 331, 332; 331, 333; 332, identifier:KeyError; 333, block; 333, 334; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 339; 336, subscript; 336, 337; 336, 338; 337, identifier:read_names; 338, identifier:my_read_name; 339, list:[bin_file]; 339, 340; 340, identifier:bin_file; 341, for_statement; 341, 342; 341, 343; 341, 348; 342, identifier:file_handle; 343, call; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:opened_files; 346, identifier:values; 347, argument_list; 348, block; 348, 349; 349, expression_statement; 349, 350; 350, call; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, identifier:file_handle; 353, identifier:close; 354, argument_list; 355, comment; 356, if_statement; 356, 357; 356, 358; 356, 365; 357, identifier:save_memory; 358, block; 358, 359; 359, return_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:opened_files; 363, identifier:keys; 364, argument_list; 365, else_clause; 365, 366; 366, block; 366, 367; 367, return_statement; 367, 368; 368, identifier:read_names
def alignment_to_reads( sam_merged, output_dir, parameters=DEFAULT_PARAMETERS, save_memory=True, *bin_fasta ): """Generate reads from ambiguous alignment file Extract reads found to be mapping an input FASTA bin. If one read maps, the whole pair is extracted and written to the output paired-end FASTQ files. Reads that mapped and weren't part of a pair are kept in a third 'single' file for people who need it (e.g. to get extra paired reads by fetching the opposite one from the original FASTQ library). Parameters ---------- sam_merged : file, str or pathlib.Path The input alignment file in SAM/BAM format to be processed. output_dir : str or pathlib.Path The output directory to write the network and chunk data into. parameters : dict, optional Parameters for the network to read conversion, similar to alignment_to_network. save_memory : bool, optional Whether to keep the read names into memory or write them in different files, which takes longer but may prevent out-of-memory crashes. Default is True. `*bin_fasta` : file, str or pathlib.Path The bin FASTA files with appropriately named records. Returns ------- A dictionary of files with read names for each bin if save_memory is True, and a dictionary of the read names lists themselves otherwise. Note ---- This will throw an IOError ('close failed in file object destructor') on exit with older versions of pysam for some reason. It's harmless but you may consider upgrading to a later version of pysam if it comes up in a pipeline. """ # Just in case file objects are sent as input def get_file_string(file_thing): try: file_string = file_thing.name except AttributeError: file_string = str(file_thing) return file_string # Global set of chunks against which reads are required to # map - we store them in a tuple that keeps track of the # original bin each chunk came from so we can reattribute the reads later bin_chunks = set() for bin_file in bin_fasta: for record in SeqIO.parse(bin_file, "fasta"): bin_chunks.add((get_file_string(bin_file), record.id)) chunk_size = int(parameters["chunk_size"]) mapq_threshold = int(parameters["mapq_threshold"]) def read_name(read): return read.query_name.split()[0] # Since reading a huge BAM file can take up a # lot of time and resources, we only do it once # but that requires opening fastq files for writing # as matching reads get detected along the # bam and keeping track of which ones are # currently open. def get_base_name(bin_file): base_name = ".".join(os.path.basename(bin_file).split(".")[:-1]) output_path = os.path.join( output_dir, "{}.readnames".format(base_name) ) return output_path if save_memory: opened_files = dict() else: read_names = dict() with pysam.AlignmentFile(sam_merged, "rb") as alignment_merged_handle: for (my_read_name, alignment_pool) in itertools.groupby( alignment_merged_handle, read_name ): for my_alignment in alignment_pool: relative_position = my_alignment.reference_start contig_name = my_alignment.reference_name chunk_position = relative_position // chunk_size # The 'chunk name' is used to detect macthing positions chunk_name = "{}_{}".format(contig_name, chunk_position) # But such matching positions have to map acceptably quality_test = my_alignment.mapping_quality > mapq_threshold for bin_file in bin_fasta: chunk_tuple = (bin_file, chunk_name) if chunk_tuple in bin_chunks and quality_test: if save_memory: output_path = get_base_name(bin_file) try: output_handle = opened_files[bin_file] except KeyError: output_handle = open(output_path, "w") opened_files[bin_file] = output_handle output_handle.write("@{}\n".format(my_read_name)) else: try: read_names[my_read_name].append(bin_file) except KeyError: read_names[my_read_name] = [bin_file] for file_handle in opened_files.values(): file_handle.close() # Return unpaired file names for pair_unpaired_reads() to process if save_memory: return opened_files.keys() else: return read_names
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_validate_config; 3, parameters; 3, 4; 4, identifier:config; 5, block; 5, 6; 5, 8; 5, 21; 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:config; 14, identifier:list; 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:'Config must be a list'; 21, for_statement; 21, 22; 21, 23; 21, 24; 22, identifier:config_dict; 23, identifier:config; 24, block; 24, 25; 24, 38; 24, 48; 24, 54; 24, 67; 24, 77; 24, 101; 24, 111; 24, 126; 24, 136; 24, 184; 24, 204; 25, if_statement; 25, 26; 25, 32; 26, not_operator; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 31; 30, identifier:config_dict; 31, identifier:dict; 32, block; 32, 33; 33, raise_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:TypeError; 36, argument_list; 36, 37; 37, string:'Config must be a list of dictionaries'; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:label; 41, subscript; 41, 42; 41, 47; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:config_dict; 45, identifier:keys; 46, argument_list; 47, integer:0; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:cfg; 51, subscript; 51, 52; 51, 53; 52, identifier:config_dict; 53, identifier:label; 54, if_statement; 54, 55; 54, 61; 55, not_operator; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:isinstance; 58, argument_list; 58, 59; 58, 60; 59, identifier:cfg; 60, identifier:dict; 61, block; 61, 62; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:TypeError; 65, argument_list; 65, 66; 66, string:'Config structure is broken'; 67, if_statement; 67, 68; 67, 71; 68, comparison_operator:not; 68, 69; 68, 70; 69, string:'host'; 70, identifier:cfg; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:TypeError; 75, argument_list; 75, 76; 76, string:'Config entries must have a value for host'; 77, if_statement; 77, 78; 77, 95; 78, boolean_operator:and; 78, 79; 78, 87; 79, not_operator; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:isinstance; 82, argument_list; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:cfg; 85, string:'host'; 86, identifier:str; 87, not_operator; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:isinstance; 90, argument_list; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:cfg; 93, string:'host'; 94, identifier:list; 95, block; 95, 96; 96, raise_statement; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:TypeError; 99, argument_list; 99, 100; 100, string:'Host must be a string or a list.'; 101, if_statement; 101, 102; 101, 105; 102, comparison_operator:not; 102, 103; 102, 104; 103, string:'port'; 104, identifier:cfg; 105, block; 105, 106; 106, raise_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:TypeError; 109, argument_list; 109, 110; 110, string:'Config entries must have a value for port'; 111, if_statement; 111, 112; 111, 120; 112, not_operator; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:isinstance; 115, argument_list; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:cfg; 118, string:'port'; 119, identifier:int; 120, block; 120, 121; 121, raise_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:TypeError; 124, argument_list; 124, 125; 125, string:'Port must be an int'; 126, if_statement; 126, 127; 126, 130; 127, comparison_operator:not; 127, 128; 127, 129; 128, string:'dbpath'; 129, identifier:cfg; 130, block; 130, 131; 131, raise_statement; 131, 132; 132, call; 132, 133; 132, 134; 133, identifier:TypeError; 134, argument_list; 134, 135; 135, string:'Config entries must have a value for dbpath'; 136, if_statement; 136, 137; 136, 145; 137, not_operator; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:isinstance; 140, argument_list; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:cfg; 143, string:'dbpath'; 144, identifier:str; 145, block; 145, 146; 145, 163; 146, if_statement; 146, 147; 146, 155; 147, not_operator; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:isinstance; 150, argument_list; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:cfg; 153, string:'dbpath'; 154, identifier:list; 155, block; 155, 156; 156, raise_statement; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:TypeError; 159, argument_list; 159, 160; 160, concatenated_string; 160, 161; 160, 162; 161, string:'Dbpath must either a string or a list of '; 162, string:'strings'; 163, for_statement; 163, 164; 163, 165; 163, 168; 164, identifier:dbpath; 165, subscript; 165, 166; 165, 167; 166, identifier:cfg; 167, string:'dbpath'; 168, block; 168, 169; 169, if_statement; 169, 170; 169, 176; 170, not_operator; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:isinstance; 173, argument_list; 173, 174; 173, 175; 174, identifier:dbpath; 175, identifier:str; 176, block; 176, 177; 177, raise_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:TypeError; 180, argument_list; 180, 181; 181, concatenated_string; 181, 182; 181, 183; 182, string:'Dbpath must either a string or a list '; 183, string:'of strings'; 184, if_statement; 184, 185; 184, 198; 185, parenthesized_expression; 185, 186; 186, boolean_operator:and; 186, 187; 186, 190; 187, comparison_operator:in; 187, 188; 187, 189; 188, string:'read_preference'; 189, identifier:cfg; 190, not_operator; 190, 191; 191, call; 191, 192; 191, 193; 192, identifier:isinstance; 193, argument_list; 193, 194; 193, 197; 194, subscript; 194, 195; 194, 196; 195, identifier:cfg; 196, string:'read_preference'; 197, identifier:str; 198, block; 198, 199; 199, raise_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:TypeError; 202, argument_list; 202, 203; 203, string:'Read_preference must be a string'; 204, if_statement; 204, 205; 204, 218; 205, parenthesized_expression; 205, 206; 206, boolean_operator:and; 206, 207; 206, 210; 207, comparison_operator:in; 207, 208; 207, 209; 208, string:'replicaSet'; 209, identifier:cfg; 210, not_operator; 210, 211; 211, call; 211, 212; 211, 213; 212, identifier:isinstance; 213, argument_list; 213, 214; 213, 217; 214, subscript; 214, 215; 214, 216; 215, identifier:cfg; 216, string:'replicaSet'; 217, identifier:str; 218, block; 218, 219; 219, raise_statement; 219, 220; 220, call; 220, 221; 220, 222; 221, identifier:TypeError; 222, argument_list; 222, 223; 223, string:'replicaSet must be a string'
def _validate_config(config): """Validate that the provided configurtion is valid. Each dictionary in the configuration list must have the following mandatory entries : {label: {host(string), port(int), dbpath(string|list of strings)}} It can also contain 1 optional key: {read_preference(string)} Args: config: the list of configurations provided at instantiation Raises: TypeError: a fault in the configurations is found """ if not isinstance(config, list): raise TypeError('Config must be a list') for config_dict in config: if not isinstance(config_dict, dict): raise TypeError('Config must be a list of dictionaries') label = config_dict.keys()[0] cfg = config_dict[label] if not isinstance(cfg, dict): raise TypeError('Config structure is broken') if 'host' not in cfg: raise TypeError('Config entries must have a value for host') if not isinstance(cfg['host'], str) and not isinstance(cfg['host'], list): raise TypeError('Host must be a string or a list.') if 'port' not in cfg: raise TypeError('Config entries must have a value for port') if not isinstance(cfg['port'], int): raise TypeError('Port must be an int') if 'dbpath' not in cfg: raise TypeError('Config entries must have a value for dbpath') if not isinstance(cfg['dbpath'], str): if not isinstance(cfg['dbpath'], list): raise TypeError('Dbpath must either a string or a list of ' 'strings') for dbpath in cfg['dbpath']: if not isinstance(dbpath, str): raise TypeError('Dbpath must either a string or a list ' 'of strings') if ('read_preference' in cfg and not isinstance(cfg['read_preference'], str)): raise TypeError('Read_preference must be a string') if ('replicaSet' in cfg and not isinstance(cfg['replicaSet'], str)): raise TypeError('replicaSet must be a string')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:taskinfo; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 23; 5, 40; 5, 48; 5, 62; 5, 76; 5, 90; 5, 106; 5, 122; 5, 123; 5, 124; 5, 141; 5, 148; 5, 154; 5, 484; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:task_input; 11, dictionary; 11, 12; 11, 15; 12, pair; 12, 13; 12, 14; 13, string:'taskName'; 14, string:'QueryTask'; 15, pair; 15, 16; 15, 17; 16, string:'inputParameters'; 17, dictionary; 17, 18; 18, pair; 18, 19; 18, 20; 19, string:"Task_Name"; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:_name; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:info; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:taskengine; 29, identifier:execute; 30, argument_list; 30, 31; 30, 32; 30, 35; 31, identifier:task_input; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_engine; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:cwd; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_cwd; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:task_def; 43, subscript; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:info; 46, string:'outputParameters'; 47, string:'DEFINITION'; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:task_def; 52, string:'name'; 53, call; 53, 54; 53, 55; 54, identifier:str; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:task_def; 59, identifier:pop; 60, argument_list; 60, 61; 61, string:'NAME'; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:task_def; 66, string:'description'; 67, call; 67, 68; 67, 69; 68, identifier:str; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:task_def; 73, identifier:pop; 74, argument_list; 74, 75; 75, string:'DESCRIPTION'; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:task_def; 80, string:'displayName'; 81, call; 81, 82; 81, 83; 82, identifier:str; 83, argument_list; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:task_def; 87, identifier:pop; 88, argument_list; 88, 89; 89, string:'DISPLAY_NAME'; 90, if_statement; 90, 91; 90, 94; 91, comparison_operator:in; 91, 92; 91, 93; 92, string:'COMMUTE_ON_SUBSET'; 93, identifier:task_def; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:task_def; 99, string:'commute_on_subset'; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:task_def; 103, identifier:pop; 104, argument_list; 104, 105; 105, string:'COMMUTE_ON_SUBSET'; 106, if_statement; 106, 107; 106, 110; 107, comparison_operator:in; 107, 108; 107, 109; 108, string:'COMMUTE_ON_DOWNSAMPLE'; 109, identifier:task_def; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:task_def; 115, string:'commute_on_downsample'; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:task_def; 119, identifier:pop; 120, argument_list; 120, 121; 121, string:'COMMUTE_ON_DOWNSAMPLE'; 122, comment; 123, comment; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 125, 130; 126, subscript; 126, 127; 126, 128; 127, identifier:task_def; 128, string:'parameters'; 129, line_continuation:\; 130, list_comprehension; 130, 131; 130, 132; 131, identifier:v; 132, for_in_clause; 132, 133; 132, 134; 133, identifier:v; 134, call; 134, 135; 134, 140; 135, attribute; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:task_def; 138, string:'PARAMETERS'; 139, identifier:values; 140, argument_list; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:task_def; 145, identifier:pop; 146, argument_list; 146, 147; 147, string:'PARAMETERS'; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:parameters; 151, subscript; 151, 152; 151, 153; 152, identifier:task_def; 153, string:'parameters'; 154, for_statement; 154, 155; 154, 156; 154, 157; 155, identifier:parameter; 156, identifier:parameters; 157, block; 157, 158; 157, 172; 157, 186; 157, 200; 157, 214; 157, 230; 157, 246; 157, 320; 157, 336; 157, 356; 157, 388; 157, 420; 157, 436; 157, 452; 157, 468; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:parameter; 162, string:'name'; 163, call; 163, 164; 163, 165; 164, identifier:str; 165, argument_list; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:parameter; 169, identifier:pop; 170, argument_list; 170, 171; 171, string:'NAME'; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:parameter; 176, string:'description'; 177, call; 177, 178; 177, 179; 178, identifier:str; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:parameter; 183, identifier:pop; 184, argument_list; 184, 185; 185, string:'DESCRIPTION'; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:parameter; 190, string:'display_name'; 191, call; 191, 192; 191, 193; 192, identifier:str; 193, argument_list; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:parameter; 197, identifier:pop; 198, argument_list; 198, 199; 199, string:'DISPLAY_NAME'; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:parameter; 204, string:'required'; 205, call; 205, 206; 205, 207; 206, identifier:bool; 207, argument_list; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:parameter; 211, identifier:pop; 212, argument_list; 212, 213; 213, string:'REQUIRED'; 214, if_statement; 214, 215; 214, 218; 215, comparison_operator:in; 215, 216; 215, 217; 216, string:'MIN'; 217, identifier:parameter; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 224; 221, subscript; 221, 222; 221, 223; 222, identifier:parameter; 223, string:'min'; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:parameter; 227, identifier:pop; 228, argument_list; 228, 229; 229, string:'MIN'; 230, if_statement; 230, 231; 230, 234; 231, comparison_operator:in; 231, 232; 231, 233; 232, string:'MAX'; 233, identifier:parameter; 234, block; 234, 235; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:parameter; 239, string:'max'; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:parameter; 243, identifier:pop; 244, argument_list; 244, 245; 245, string:'MAX'; 246, if_statement; 246, 247; 246, 255; 246, 297; 247, call; 247, 248; 247, 253; 248, attribute; 248, 249; 248, 252; 249, subscript; 249, 250; 249, 251; 250, identifier:parameter; 251, string:'TYPE'; 252, identifier:count; 253, argument_list; 253, 254; 254, string:'['; 255, block; 255, 256; 255, 276; 255, 286; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 265; 258, pattern_list; 258, 259; 258, 262; 259, subscript; 259, 260; 259, 261; 260, identifier:parameter; 261, string:'type'; 262, subscript; 262, 263; 262, 264; 263, identifier:parameter; 264, string:'dimensions'; 265, call; 265, 266; 265, 274; 266, attribute; 266, 267; 266, 273; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:parameter; 270, identifier:pop; 271, argument_list; 271, 272; 272, string:'TYPE'; 273, identifier:split; 274, argument_list; 274, 275; 275, string:'['; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 281; 278, subscript; 278, 279; 278, 280; 279, identifier:parameter; 280, string:'dimensions'; 281, binary_operator:+; 281, 282; 281, 283; 282, string:'['; 283, subscript; 283, 284; 283, 285; 284, identifier:parameter; 285, string:'dimensions'; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 291; 288, subscript; 288, 289; 288, 290; 289, identifier:parameter; 290, string:'type'; 291, call; 291, 292; 291, 293; 292, identifier:str; 293, argument_list; 293, 294; 294, subscript; 294, 295; 294, 296; 295, identifier:parameter; 296, string:'type'; 297, else_clause; 297, 298; 298, block; 298, 299; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 304; 301, subscript; 301, 302; 301, 303; 302, identifier:parameter; 303, string:'type'; 304, call; 304, 305; 304, 306; 305, identifier:str; 306, argument_list; 306, 307; 307, subscript; 307, 308; 307, 319; 308, call; 308, 309; 308, 317; 309, attribute; 309, 310; 309, 316; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:parameter; 313, identifier:pop; 314, argument_list; 314, 315; 315, string:'TYPE'; 316, identifier:split; 317, argument_list; 317, 318; 318, string:'ARRAY'; 319, integer:0; 320, if_statement; 320, 321; 320, 324; 321, comparison_operator:in; 321, 322; 321, 323; 322, string:'DIMENSIONS'; 323, identifier:parameter; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 330; 327, subscript; 327, 328; 327, 329; 328, identifier:parameter; 329, string:'dimensions'; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:parameter; 333, identifier:pop; 334, argument_list; 334, 335; 335, string:'DIMENSIONS'; 336, if_statement; 336, 337; 336, 340; 337, comparison_operator:in; 337, 338; 337, 339; 338, string:'DIRECTION'; 339, identifier:parameter; 340, block; 340, 341; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 346; 343, subscript; 343, 344; 343, 345; 344, identifier:parameter; 345, string:'direction'; 346, call; 346, 347; 346, 355; 347, attribute; 347, 348; 347, 354; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:parameter; 351, identifier:pop; 352, argument_list; 352, 353; 353, string:'DIRECTION'; 354, identifier:lower; 355, argument_list; 356, if_statement; 356, 357; 356, 360; 357, comparison_operator:in; 357, 358; 357, 359; 358, string:'DEFAULT'; 359, identifier:parameter; 360, block; 360, 361; 361, if_statement; 361, 362; 361, 367; 361, 379; 362, comparison_operator:is; 362, 363; 362, 366; 363, subscript; 363, 364; 363, 365; 364, identifier:parameter; 365, string:'DEFAULT'; 366, None; 367, block; 367, 368; 368, expression_statement; 368, 369; 369, assignment; 369, 370; 369, 373; 370, subscript; 370, 371; 370, 372; 371, identifier:parameter; 372, string:'default_value'; 373, call; 373, 374; 373, 377; 374, attribute; 374, 375; 374, 376; 375, identifier:parameter; 376, identifier:pop; 377, argument_list; 377, 378; 378, string:'DEFAULT'; 379, else_clause; 379, 380; 380, block; 380, 381; 381, expression_statement; 381, 382; 382, call; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, identifier:parameter; 385, identifier:pop; 386, argument_list; 386, 387; 387, string:'DEFAULT'; 388, if_statement; 388, 389; 388, 392; 389, comparison_operator:in; 389, 390; 389, 391; 390, string:'CHOICE_LIST'; 391, identifier:parameter; 392, block; 392, 393; 393, if_statement; 393, 394; 393, 399; 393, 411; 394, comparison_operator:is; 394, 395; 394, 398; 395, subscript; 395, 396; 395, 397; 396, identifier:parameter; 397, string:'CHOICE_LIST'; 398, None; 399, block; 399, 400; 400, expression_statement; 400, 401; 401, assignment; 401, 402; 401, 405; 402, subscript; 402, 403; 402, 404; 403, identifier:parameter; 404, string:'choice_list'; 405, call; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:parameter; 408, identifier:pop; 409, argument_list; 409, 410; 410, string:'CHOICE_LIST'; 411, else_clause; 411, 412; 412, block; 412, 413; 413, expression_statement; 413, 414; 414, call; 414, 415; 414, 418; 415, attribute; 415, 416; 415, 417; 416, identifier:parameter; 417, identifier:pop; 418, argument_list; 418, 419; 419, string:'CHOICE_LIST'; 420, if_statement; 420, 421; 420, 424; 421, comparison_operator:in; 421, 422; 421, 423; 422, string:'FOLD_CASE'; 423, identifier:parameter; 424, block; 424, 425; 425, expression_statement; 425, 426; 426, assignment; 426, 427; 426, 430; 427, subscript; 427, 428; 427, 429; 428, identifier:parameter; 429, string:'fold_case'; 430, call; 430, 431; 430, 434; 431, attribute; 431, 432; 431, 433; 432, identifier:parameter; 433, identifier:pop; 434, argument_list; 434, 435; 435, string:'FOLD_CASE'; 436, if_statement; 436, 437; 436, 440; 437, comparison_operator:in; 437, 438; 437, 439; 438, string:'AUTO_EXTENSION'; 439, identifier:parameter; 440, block; 440, 441; 441, expression_statement; 441, 442; 442, assignment; 442, 443; 442, 446; 443, subscript; 443, 444; 443, 445; 444, identifier:parameter; 445, string:'auto_extension'; 446, call; 446, 447; 446, 450; 447, attribute; 447, 448; 447, 449; 448, identifier:parameter; 449, identifier:pop; 450, argument_list; 450, 451; 451, string:'AUTO_EXTENSION'; 452, if_statement; 452, 453; 452, 456; 453, comparison_operator:in; 453, 454; 453, 455; 454, string:'IS_TEMPORARY'; 455, identifier:parameter; 456, block; 456, 457; 457, expression_statement; 457, 458; 458, assignment; 458, 459; 458, 462; 459, subscript; 459, 460; 459, 461; 460, identifier:parameter; 461, string:'is_temporary'; 462, call; 462, 463; 462, 466; 463, attribute; 463, 464; 463, 465; 464, identifier:parameter; 465, identifier:pop; 466, argument_list; 466, 467; 467, string:'IS_TEMPORARY'; 468, if_statement; 468, 469; 468, 472; 469, comparison_operator:in; 469, 470; 469, 471; 470, string:'IS_DIRECTORY'; 471, identifier:parameter; 472, block; 472, 473; 473, expression_statement; 473, 474; 474, assignment; 474, 475; 474, 478; 475, subscript; 475, 476; 475, 477; 476, identifier:parameter; 477, string:'is_directory'; 478, call; 478, 479; 478, 482; 479, attribute; 479, 480; 479, 481; 480, identifier:parameter; 481, identifier:pop; 482, argument_list; 482, 483; 483, string:'IS_DIRECTORY'; 484, return_statement; 484, 485; 485, identifier:task_def
def taskinfo(self): """ Retrieve the Task Information """ task_input = {'taskName': 'QueryTask', 'inputParameters': {"Task_Name": self._name}} info = taskengine.execute(task_input, self._engine, cwd=self._cwd) task_def = info['outputParameters']['DEFINITION'] task_def['name'] = str(task_def.pop('NAME')) task_def['description'] = str(task_def.pop('DESCRIPTION')) task_def['displayName'] = str(task_def.pop('DISPLAY_NAME')) if 'COMMUTE_ON_SUBSET' in task_def: task_def['commute_on_subset'] = task_def.pop('COMMUTE_ON_SUBSET') if 'COMMUTE_ON_DOWNSAMPLE' in task_def: task_def['commute_on_downsample'] = task_def.pop('COMMUTE_ON_DOWNSAMPLE') # Convert PARAMETERS into a list instead of a dictionary # which matches the gsf side things task_def['parameters'] = \ [v for v in task_def['PARAMETERS'].values()] task_def.pop('PARAMETERS') parameters = task_def['parameters'] for parameter in parameters: parameter['name'] = str(parameter.pop('NAME')) parameter['description'] = str(parameter.pop('DESCRIPTION')) parameter['display_name'] = str(parameter.pop('DISPLAY_NAME')) parameter['required'] = bool(parameter.pop('REQUIRED')) if 'MIN' in parameter: parameter['min'] = parameter.pop('MIN') if 'MAX' in parameter: parameter['max'] = parameter.pop('MAX') if parameter['TYPE'].count('['): parameter['type'], parameter['dimensions'] = parameter.pop('TYPE').split('[') parameter['dimensions'] = '[' + parameter['dimensions'] parameter['type'] = str(parameter['type']) else: parameter['type'] = str(parameter.pop('TYPE').split('ARRAY')[0]) if 'DIMENSIONS' in parameter: parameter['dimensions'] = parameter.pop('DIMENSIONS') if 'DIRECTION' in parameter: parameter['direction'] = parameter.pop('DIRECTION').lower() if 'DEFAULT' in parameter: if parameter['DEFAULT'] is not None: parameter['default_value'] = parameter.pop('DEFAULT') else: parameter.pop('DEFAULT') if 'CHOICE_LIST' in parameter: if parameter['CHOICE_LIST'] is not None: parameter['choice_list'] = parameter.pop('CHOICE_LIST') else: parameter.pop('CHOICE_LIST') if 'FOLD_CASE' in parameter: parameter['fold_case'] = parameter.pop('FOLD_CASE') if 'AUTO_EXTENSION' in parameter: parameter['auto_extension'] = parameter.pop('AUTO_EXTENSION') if 'IS_TEMPORARY' in parameter: parameter['is_temporary'] = parameter.pop('IS_TEMPORARY') if 'IS_DIRECTORY' in parameter: parameter['is_directory'] = parameter.pop('IS_DIRECTORY') return task_def
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:distance_diagonal_law; 3, parameters; 3, 4; 3, 5; 4, identifier:matrix; 5, default_parameter; 5, 6; 5, 7; 6, identifier:positions; 7, None; 8, block; 8, 9; 8, 11; 8, 20; 8, 59; 8, 73; 8, 92; 8, 96; 8, 119; 8, 186; 8, 193; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:n; 14, call; 14, 15; 14, 16; 15, identifier:min; 16, argument_list; 16, 17; 17, attribute; 17, 18; 17, 19; 18, identifier:matrix; 19, identifier:shape; 20, if_statement; 20, 21; 20, 24; 20, 50; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:positions; 23, None; 24, block; 24, 25; 25, return_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:np; 29, identifier:array; 30, argument_list; 30, 31; 31, list_comprehension; 31, 32; 31, 44; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:np; 35, identifier:average; 36, argument_list; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:np; 40, identifier:diagonal; 41, argument_list; 41, 42; 41, 43; 42, identifier:matrix; 43, identifier:j; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:j; 46, call; 46, 47; 46, 48; 47, identifier:range; 48, argument_list; 48, 49; 49, identifier:n; 50, else_clause; 50, 51; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:contigs; 55, call; 55, 56; 55, 57; 56, identifier:positions_to_contigs; 57, argument_list; 57, 58; 58, identifier:positions; 59, function_definition; 59, 60; 59, 61; 59, 64; 60, function_name:is_intra; 61, parameters; 61, 62; 61, 63; 62, identifier:i; 63, identifier:j; 64, block; 64, 65; 65, return_statement; 65, 66; 66, comparison_operator:==; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:contigs; 69, identifier:i; 70, subscript; 70, 71; 70, 72; 71, identifier:contigs; 72, identifier:j; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:max_intra_distance; 76, call; 76, 77; 76, 78; 77, identifier:max; 78, argument_list; 78, 79; 79, generator_expression; 79, 80; 79, 86; 80, call; 80, 81; 80, 82; 81, identifier:len; 82, argument_list; 82, 83; 83, comparison_operator:==; 83, 84; 83, 85; 84, identifier:contigs; 85, identifier:u; 86, for_in_clause; 86, 87; 86, 88; 87, identifier:u; 88, call; 88, 89; 88, 90; 89, identifier:set; 90, argument_list; 90, 91; 91, identifier:contigs; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:intra_contacts; 95, list:[]; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:inter_contacts; 99, list_comprehension; 99, 100; 99, 112; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:np; 103, identifier:average; 104, argument_list; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:np; 108, identifier:diagonal; 109, argument_list; 109, 110; 109, 111; 110, identifier:matrix; 111, identifier:j; 112, for_in_clause; 112, 113; 112, 114; 113, identifier:j; 114, call; 114, 115; 114, 116; 115, identifier:range; 116, argument_list; 116, 117; 116, 118; 117, identifier:max_intra_distance; 118, identifier:n; 119, for_statement; 119, 120; 119, 121; 119, 125; 120, identifier:j; 121, call; 121, 122; 121, 123; 122, identifier:range; 123, argument_list; 123, 124; 124, identifier:max_intra_distance; 125, block; 125, 126; 125, 136; 125, 166; 125, 167; 125, 168; 125, 169; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:D; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:np; 132, identifier:diagonal; 133, argument_list; 133, 134; 133, 135; 134, identifier:matrix; 135, identifier:j; 136, for_statement; 136, 137; 136, 138; 136, 145; 137, identifier:i; 138, call; 138, 139; 138, 140; 139, identifier:range; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:len; 143, argument_list; 143, 144; 144, identifier:D; 145, block; 145, 146; 145, 150; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:diagonal_intra; 149, list:[]; 150, if_statement; 150, 151; 150, 156; 151, call; 151, 152; 151, 153; 152, identifier:is_intra; 153, argument_list; 153, 154; 153, 155; 154, identifier:i; 155, identifier:j; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:diagonal_intra; 161, identifier:append; 162, argument_list; 162, 163; 163, subscript; 163, 164; 163, 165; 164, identifier:D; 165, identifier:i; 166, comment; 167, comment; 168, comment; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:intra_contacts; 173, identifier:append; 174, argument_list; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:np; 178, identifier:average; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:np; 183, identifier:array; 184, argument_list; 184, 185; 185, identifier:diagonal_intra; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:intra_contacts; 190, identifier:extend; 191, argument_list; 191, 192; 192, identifier:inter_contacts; 193, return_statement; 193, 194; 194, list:[positions, np.array(intra_contacts)]; 194, 195; 194, 196; 195, identifier:positions; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:np; 199, identifier:array; 200, argument_list; 200, 201; 201, identifier:intra_contacts
def distance_diagonal_law(matrix, positions=None): """Compute a distance law trend using the contact averages of equal distances. Specific positions can be supplied if needed. """ n = min(matrix.shape) if positions is None: return np.array([np.average(np.diagonal(matrix, j)) for j in range(n)]) else: contigs = positions_to_contigs(positions) def is_intra(i, j): return contigs[i] == contigs[j] max_intra_distance = max((len(contigs == u) for u in set(contigs))) intra_contacts = [] inter_contacts = [np.average(np.diagonal(matrix, j)) for j in range(max_intra_distance, n)] for j in range(max_intra_distance): D = np.diagonal(matrix, j) for i in range(len(D)): diagonal_intra = [] if is_intra(i, j): diagonal_intra.append(D[i]) # else: # diagonal_inter.append(D[i]) # inter_contacts.append(np.average(np.array(diagonal_inter))) intra_contacts.append(np.average(np.array(diagonal_intra))) intra_contacts.extend(inter_contacts) return [positions, np.array(intra_contacts)]