sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:gather_categories; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:imap; 5, identifier:header; 6, default_parameter; 6, 7; 6, 8; 7, identifier:categories; 8, None; 9, block; 9, 10; 9, 12; 9, 13; 9, 34; 9, 55; 9, 61; 9, 68; 9, 127; 9, 128; 9, 151; 9, 152; 9, 213; 9, 214; 9, 220; 9, 242; 9, 254; 9, 255; 9, 256; 9, 338; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:categories; 16, None; 17, block; 17, 18; 18, return_statement; 18, 19; 19, dictionary; 19, 20; 20, pair; 20, 21; 20, 22; 21, string:"default"; 22, call; 22, 23; 22, 24; 23, identifier:DataCategory; 24, argument_list; 24, 25; 24, 33; 25, call; 25, 26; 25, 27; 26, identifier:set; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:imap; 31, identifier:keys; 32, argument_list; 33, dictionary; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:cat_ids; 37, list_comprehension; 37, 38; 37, 44; 37, 47; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:header; 41, identifier:index; 42, argument_list; 42, 43; 43, identifier:cat; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:cat; 46, identifier:categories; 47, if_clause; 47, 48; 48, boolean_operator:and; 48, 49; 48, 52; 49, comparison_operator:in; 49, 50; 49, 51; 50, identifier:cat; 51, identifier:header; 52, comparison_operator:not; 52, 53; 52, 54; 53, string:"="; 54, identifier:cat; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:table; 58, call; 58, 59; 58, 60; 59, identifier:OrderedDict; 60, argument_list; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:conditions; 64, call; 64, 65; 64, 66; 65, identifier:defaultdict; 66, argument_list; 66, 67; 67, identifier:set; 68, for_statement; 68, 69; 68, 72; 68, 76; 69, pattern_list; 69, 70; 69, 71; 70, identifier:i; 71, identifier:cat; 72, call; 72, 73; 72, 74; 73, identifier:enumerate; 74, argument_list; 74, 75; 75, identifier:categories; 76, block; 76, 77; 77, if_statement; 77, 78; 77, 92; 78, boolean_operator:and; 78, 79; 78, 82; 79, comparison_operator:in; 79, 80; 79, 81; 80, string:"="; 81, identifier:cat; 82, comparison_operator:in; 82, 83; 82, 91; 83, subscript; 83, 84; 83, 90; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:cat; 87, identifier:split; 88, argument_list; 88, 89; 89, string:"="; 90, integer:0; 91, identifier:header; 92, block; 92, 93; 92, 111; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:cat_name; 96, subscript; 96, 97; 96, 98; 97, identifier:header; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:header; 101, identifier:index; 102, argument_list; 102, 103; 103, subscript; 103, 104; 103, 110; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:cat; 107, identifier:split; 108, argument_list; 108, 109; 109, string:"="; 110, integer:0; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:conditions; 116, identifier:cat_name; 117, identifier:add; 118, argument_list; 118, 119; 119, subscript; 119, 120; 119, 126; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:cat; 123, identifier:split; 124, argument_list; 124, 125; 125, string:"="; 126, integer:1; 127, comment; 128, if_statement; 128, 129; 128, 134; 129, boolean_operator:and; 129, 130; 129, 132; 130, not_operator; 130, 131; 131, identifier:cat_ids; 132, not_operator; 132, 133; 133, identifier:conditions; 134, block; 134, 135; 135, return_statement; 135, 136; 136, dictionary; 136, 137; 137, pair; 137, 138; 137, 139; 138, string:"default"; 139, call; 139, 140; 139, 141; 140, identifier:DataCategory; 141, argument_list; 141, 142; 141, 150; 142, call; 142, 143; 142, 144; 143, identifier:set; 144, argument_list; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:imap; 148, identifier:keys; 149, argument_list; 150, dictionary; 151, comment; 152, if_statement; 152, 153; 152, 157; 153, boolean_operator:and; 153, 154; 153, 155; 154, identifier:cat_ids; 155, not_operator; 155, 156; 156, identifier:conditions; 157, block; 157, 158; 157, 211; 158, for_statement; 158, 159; 158, 162; 158, 167; 159, pattern_list; 159, 160; 159, 161; 160, identifier:sid; 161, identifier:row; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:imap; 165, identifier:items; 166, argument_list; 167, block; 167, 168; 167, 183; 167, 200; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:cat_name; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, string:"_"; 174, identifier:join; 175, argument_list; 175, 176; 176, list_comprehension; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:row; 179, identifier:cid; 180, for_in_clause; 180, 181; 180, 182; 181, identifier:cid; 182, identifier:cat_ids; 183, if_statement; 183, 184; 183, 187; 184, comparison_operator:not; 184, 185; 184, 186; 185, identifier:cat_name; 186, identifier:table; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:table; 192, identifier:cat_name; 193, call; 193, 194; 193, 195; 194, identifier:DataCategory; 195, argument_list; 195, 196; 195, 199; 196, call; 196, 197; 196, 198; 197, identifier:set; 198, argument_list; 199, dictionary; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 209; 202, attribute; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:table; 206, identifier:cat_name; 207, identifier:sids; 208, identifier:add; 209, argument_list; 209, 210; 210, identifier:sid; 211, return_statement; 211, 212; 212, identifier:table; 213, comment; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:cond_ids; 217, call; 217, 218; 217, 219; 218, identifier:set; 219, argument_list; 220, for_statement; 220, 221; 220, 222; 220, 223; 221, identifier:k; 222, identifier:conditions; 223, block; 223, 224; 224, try_statement; 224, 225; 224, 238; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:cond_ids; 230, identifier:add; 231, argument_list; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:header; 235, identifier:index; 236, argument_list; 236, 237; 237, identifier:k; 238, except_clause; 238, 239; 238, 240; 239, identifier:ValueError; 240, block; 240, 241; 241, continue_statement; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:idx_to_test; 245, call; 245, 246; 245, 252; 246, attribute; 246, 247; 246, 251; 247, call; 247, 248; 247, 249; 248, identifier:set; 249, argument_list; 249, 250; 250, identifier:cat_ids; 251, identifier:union; 252, argument_list; 252, 253; 253, identifier:cond_ids; 254, comment; 255, comment; 256, for_statement; 256, 257; 256, 260; 256, 265; 257, pattern_list; 257, 258; 257, 259; 258, identifier:sid; 259, identifier:row; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:imap; 263, identifier:items; 264, argument_list; 265, block; 265, 266; 266, if_statement; 266, 267; 266, 286; 267, call; 267, 268; 267, 269; 268, identifier:all; 269, argument_list; 269, 270; 270, list_comprehension; 270, 271; 270, 283; 271, comparison_operator:in; 271, 272; 271, 280; 272, subscript; 272, 273; 272, 274; 273, identifier:row; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:header; 277, identifier:index; 278, argument_list; 278, 279; 279, identifier:c; 280, subscript; 280, 281; 280, 282; 281, identifier:conditions; 282, identifier:c; 283, for_in_clause; 283, 284; 283, 285; 284, identifier:c; 285, identifier:conditions; 286, block; 286, 287; 286, 302; 286, 327; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:key; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, string:"_"; 293, identifier:join; 294, argument_list; 294, 295; 295, list_comprehension; 295, 296; 295, 299; 296, subscript; 296, 297; 296, 298; 297, identifier:row; 298, identifier:idx; 299, for_in_clause; 299, 300; 299, 301; 300, identifier:idx; 301, identifier:idx_to_test; 302, try_statement; 302, 303; 302, 312; 303, block; 303, 304; 304, assert_statement; 304, 305; 305, comparison_operator:in; 305, 306; 305, 307; 306, identifier:key; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:table; 310, identifier:keys; 311, argument_list; 312, except_clause; 312, 313; 312, 314; 313, identifier:AssertionError; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 320; 317, subscript; 317, 318; 317, 319; 318, identifier:table; 319, identifier:key; 320, call; 320, 321; 320, 322; 321, identifier:DataCategory; 322, argument_list; 322, 323; 322, 326; 323, call; 323, 324; 323, 325; 324, identifier:set; 325, argument_list; 326, dictionary; 327, expression_statement; 327, 328; 328, call; 328, 329; 328, 336; 329, attribute; 329, 330; 329, 335; 330, attribute; 330, 331; 330, 334; 331, subscript; 331, 332; 331, 333; 332, identifier:table; 333, identifier:key; 334, identifier:sids; 335, identifier:add; 336, argument_list; 336, 337; 337, identifier:sid; 338, try_statement; 338, 339; 338, 347; 338, 366; 339, block; 339, 340; 340, assert_statement; 340, 341; 341, comparison_operator:>; 341, 342; 341, 346; 342, call; 342, 343; 342, 344; 343, identifier:len; 344, argument_list; 344, 345; 345, identifier:table; 346, integer:0; 347, except_clause; 347, 348; 347, 349; 348, identifier:AssertionError; 349, block; 349, 350; 350, return_statement; 350, 351; 351, dictionary; 351, 352; 352, pair; 352, 353; 352, 354; 353, string:"default"; 354, call; 354, 355; 354, 356; 355, identifier:DataCategory; 356, argument_list; 356, 357; 356, 365; 357, call; 357, 358; 357, 359; 358, identifier:set; 359, argument_list; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:imap; 363, identifier:keys; 364, argument_list; 365, dictionary; 366, else_clause; 366, 367; 367, block; 367, 368; 368, return_statement; 368, 369; 369, identifier:table
def gather_categories(imap, header, categories=None): """ Find the user specified categories in the map and create a dictionary to contain the relevant data for each type within the categories. Multiple categories will have their types combined such that each possible combination will have its own entry in the dictionary. :type imap: dict :param imap: The input mapping file data keyed by SampleID :type header: list :param header: The header line from the input mapping file. This will be searched for the user-specified categories :type categories: list :param categories: The list of user-specified category column name from mapping file :rtype: dict :return: A sorted dictionary keyed on the combinations of all the types found within the user-specified categories. Each entry will contain an empty DataCategory namedtuple. If no categories are specified, a single entry with the key 'default' will be returned """ # If no categories provided, return all SampleIDs if categories is None: return {"default": DataCategory(set(imap.keys()), {})} cat_ids = [header.index(cat) for cat in categories if cat in header and "=" not in cat] table = OrderedDict() conditions = defaultdict(set) for i, cat in enumerate(categories): if "=" in cat and cat.split("=")[0] in header: cat_name = header[header.index(cat.split("=")[0])] conditions[cat_name].add(cat.split("=")[1]) # If invalid categories or conditions identified, return all SampleIDs if not cat_ids and not conditions: return {"default": DataCategory(set(imap.keys()), {})} #If only category column given, return column-wise SampleIDs if cat_ids and not conditions: for sid, row in imap.items(): cat_name = "_".join([row[cid] for cid in cat_ids]) if cat_name not in table: table[cat_name] = DataCategory(set(), {}) table[cat_name].sids.add(sid) return table # Collect all condition names cond_ids = set() for k in conditions: try: cond_ids.add(header.index(k)) except ValueError: continue idx_to_test = set(cat_ids).union(cond_ids) # If column name and condition given, return overlapping SampleIDs of column and # condition combinations for sid, row in imap.items(): if all([row[header.index(c)] in conditions[c] for c in conditions]): key = "_".join([row[idx] for idx in idx_to_test]) try: assert key in table.keys() except AssertionError: table[key] = DataCategory(set(), {}) table[key].sids.add(sid) try: assert len(table) > 0 except AssertionError: return {"default": DataCategory(set(imap.keys()), {})} else: return table
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:color_mapping; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:sample_map; 5, identifier:header; 6, identifier:group_column; 7, default_parameter; 7, 8; 7, 9; 8, identifier:color_column; 9, None; 10, block; 10, 11; 10, 13; 10, 19; 10, 29; 10, 104; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:group_colors; 16, call; 16, 17; 16, 18; 17, identifier:OrderedDict; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:group_gather; 22, call; 22, 23; 22, 24; 23, identifier:gather_categories; 24, argument_list; 24, 25; 24, 26; 24, 27; 25, identifier:sample_map; 26, identifier:header; 27, list:[group_column]; 27, 28; 28, identifier:group_column; 29, if_statement; 29, 30; 29, 33; 29, 77; 30, comparison_operator:is; 30, 31; 30, 32; 31, identifier:color_column; 32, None; 33, block; 33, 34; 33, 44; 33, 45; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:color_gather; 37, call; 37, 38; 37, 39; 38, identifier:gather_categories; 39, argument_list; 39, 40; 39, 41; 39, 42; 40, identifier:sample_map; 41, identifier:header; 42, list:[color_column]; 42, 43; 43, identifier:color_column; 44, comment; 45, for_statement; 45, 46; 45, 47; 45, 48; 46, identifier:group; 47, identifier:group_gather; 48, block; 48, 49; 49, for_statement; 49, 50; 49, 51; 49, 52; 49, 53; 49, 54; 50, identifier:color; 51, identifier:color_gather; 52, comment; 53, comment; 54, block; 54, 55; 55, if_statement; 55, 56; 55, 70; 56, call; 56, 57; 56, 64; 57, attribute; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:group_gather; 61, identifier:group; 62, identifier:sids; 63, identifier:intersection; 64, argument_list; 64, 65; 65, attribute; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:color_gather; 68, identifier:color; 69, identifier:sids; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:group_colors; 75, identifier:group; 76, identifier:color; 77, else_clause; 77, 78; 78, block; 78, 79; 78, 90; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:bcolors; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:itertools; 85, identifier:cycle; 86, argument_list; 86, 87; 87, attribute; 87, 88; 87, 89; 88, identifier:Set3_12; 89, identifier:hex_colors; 90, for_statement; 90, 91; 90, 92; 90, 93; 91, identifier:group; 92, identifier:group_gather; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, subscript; 96, 97; 96, 98; 97, identifier:group_colors; 98, identifier:group; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:bcolors; 102, identifier:next; 103, argument_list; 104, return_statement; 104, 105; 105, identifier:group_colors
def color_mapping(sample_map, header, group_column, color_column=None): """ Determine color-category mapping. If color_column was specified, then map the category names to color values. Otherwise, use the palettable colors to automatically generate a set of colors for the group values. :type sample_map: dict :param unifracFN: Map associating each line of the mapping file with the appropriate sample ID (each value of the map also contains the sample ID) :type header: tuple :param A tuple of header line for mapping file :type group_column: str :param group_column: String denoting the column name for sample groups. :type color_column: str :param color_column: String denoting the column name for sample colors. :type return: dict :param return: {SampleID: Color} """ group_colors = OrderedDict() group_gather = gather_categories(sample_map, header, [group_column]) if color_column is not None: color_gather = gather_categories(sample_map, header, [color_column]) # match sample IDs between color_gather and group_gather for group in group_gather: for color in color_gather: # allow incomplete assignment of colors, if group sids overlap at # all with the color sids, consider it a match if group_gather[group].sids.intersection(color_gather[color].sids): group_colors[group] = color else: bcolors = itertools.cycle(Set3_12.hex_colors) for group in group_gather: group_colors[group] = bcolors.next() return group_colors
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 29; 2, function_name:shuffle_genome; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 12; 3, 15; 3, 16; 3, 19; 3, 22; 3, 23; 3, 26; 4, identifier:genome; 5, identifier:cat; 6, default_parameter; 6, 7; 6, 8; 7, identifier:fraction; 8, call; 8, 9; 8, 10; 9, identifier:float; 10, argument_list; 10, 11; 11, integer:100; 12, default_parameter; 12, 13; 12, 14; 13, identifier:plot; 14, True; 15, line_continuation:\; 16, default_parameter; 16, 17; 16, 18; 17, identifier:alpha; 18, float:0.1; 19, default_parameter; 19, 20; 19, 21; 20, identifier:beta; 21, integer:100000; 22, line_continuation:\; 23, default_parameter; 23, 24; 23, 25; 24, identifier:min_length; 25, integer:1000; 26, default_parameter; 26, 27; 26, 28; 27, identifier:max_length; 28, integer:200000; 29, block; 29, 30; 29, 32; 29, 41; 29, 62; 29, 69; 29, 73; 29, 74; 29, 160; 29, 161; 29, 168; 29, 169; 29, 252; 29, 253; 30, expression_statement; 30, 31; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:header; 35, binary_operator:%; 35, 36; 35, 37; 36, string:'>randomized_%s'; 37, parenthesized_expression; 37, 38; 38, attribute; 38, 39; 38, 40; 39, identifier:genome; 40, identifier:name; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:sequence; 44, call; 44, 45; 44, 46; 45, identifier:list; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, string:''; 50, identifier:join; 51, argument_list; 51, 52; 52, list_comprehension; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:i; 55, integer:1; 56, for_in_clause; 56, 57; 56, 58; 57, identifier:i; 58, call; 58, 59; 58, 60; 59, identifier:parse_fasta; 60, argument_list; 60, 61; 61, identifier:genome; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:length; 65, call; 65, 66; 65, 67; 66, identifier:len; 67, argument_list; 67, 68; 68, identifier:sequence; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:shuffled; 72, list:[]; 73, comment; 74, while_statement; 74, 75; 74, 78; 75, comparison_operator:is; 75, 76; 75, 77; 76, identifier:sequence; 77, False; 78, block; 78, 79; 78, 92; 78, 102; 78, 129; 78, 137; 78, 138; 78, 139; 78, 140; 78, 141; 78, 142; 78, 154; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:s; 82, call; 82, 83; 82, 84; 83, identifier:int; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:random; 88, identifier:gammavariate; 89, argument_list; 89, 90; 89, 91; 90, identifier:alpha; 91, identifier:beta; 92, if_statement; 92, 93; 92, 100; 93, boolean_operator:or; 93, 94; 93, 97; 94, comparison_operator:<=; 94, 95; 94, 96; 95, identifier:s; 96, identifier:min_length; 97, comparison_operator:>=; 97, 98; 97, 99; 98, identifier:s; 99, identifier:max_length; 100, block; 100, 101; 101, continue_statement; 102, if_statement; 102, 103; 102, 109; 102, 118; 103, comparison_operator:<; 103, 104; 103, 108; 104, call; 104, 105; 104, 106; 105, identifier:len; 106, argument_list; 106, 107; 107, identifier:sequence; 108, identifier:s; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:seq; 113, subscript; 113, 114; 113, 115; 114, identifier:sequence; 115, slice; 115, 116; 115, 117; 116, integer:0; 117, colon; 118, else_clause; 118, 119; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:seq; 123, subscript; 123, 124; 123, 125; 124, identifier:sequence; 125, slice; 125, 126; 125, 127; 125, 128; 126, integer:0; 127, colon; 128, identifier:s; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:sequence; 132, subscript; 132, 133; 132, 134; 133, identifier:sequence; 134, slice; 134, 135; 134, 136; 135, identifier:s; 136, colon; 137, comment; 138, comment; 139, comment; 140, comment; 141, comment; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:shuffled; 146, identifier:append; 147, argument_list; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, string:''; 151, identifier:join; 152, argument_list; 152, 153; 153, identifier:seq; 154, if_statement; 154, 155; 154, 158; 155, comparison_operator:==; 155, 156; 155, 157; 156, identifier:sequence; 157, list:[]; 158, block; 158, 159; 159, break_statement; 160, comment; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:random; 165, identifier:shuffle; 166, argument_list; 166, 167; 167, identifier:shuffled; 168, comment; 169, if_statement; 169, 170; 169, 176; 169, 181; 170, comparison_operator:==; 170, 171; 170, 172; 171, identifier:fraction; 172, call; 172, 173; 172, 174; 173, identifier:float; 174, argument_list; 174, 175; 175, integer:100; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:subset; 180, identifier:shuffled; 181, else_clause; 181, 182; 182, block; 182, 183; 182, 194; 182, 202; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:max_pieces; 186, call; 186, 187; 186, 188; 187, identifier:int; 188, argument_list; 188, 189; 189, binary_operator:/; 189, 190; 189, 193; 190, binary_operator:*; 190, 191; 190, 192; 191, identifier:length; 192, identifier:fraction; 193, integer:100; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 199; 196, pattern_list; 196, 197; 196, 198; 197, identifier:subset; 198, identifier:total; 199, expression_list; 199, 200; 199, 201; 200, list:[]; 201, integer:0; 202, for_statement; 202, 203; 202, 204; 202, 205; 203, identifier:fragment; 204, identifier:shuffled; 205, block; 205, 206; 205, 213; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:length; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:fragment; 213, if_statement; 213, 214; 213, 219; 213, 231; 214, comparison_operator:<=; 214, 215; 214, 218; 215, binary_operator:+; 215, 216; 215, 217; 216, identifier:total; 217, identifier:length; 218, identifier:max_pieces; 219, block; 219, 220; 219, 227; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:subset; 224, identifier:append; 225, argument_list; 225, 226; 226, identifier:fragment; 227, expression_statement; 227, 228; 228, augmented_assignment:+=; 228, 229; 228, 230; 229, identifier:total; 230, identifier:length; 231, else_clause; 231, 232; 232, block; 232, 233; 232, 239; 232, 251; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:diff; 236, binary_operator:-; 236, 237; 236, 238; 237, identifier:max_pieces; 238, identifier:total; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:subset; 243, identifier:append; 244, argument_list; 244, 245; 245, subscript; 245, 246; 245, 247; 246, identifier:fragment; 247, slice; 247, 248; 247, 249; 247, 250; 248, integer:0; 249, colon; 250, identifier:diff; 251, break_statement; 252, comment; 253, if_statement; 253, 254; 253, 257; 253, 268; 254, comparison_operator:is; 254, 255; 254, 256; 255, identifier:cat; 256, True; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, yield; 259, 260; 260, list:[header, ''.join(subset)]; 260, 261; 260, 262; 261, identifier:header; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, string:''; 265, identifier:join; 266, argument_list; 266, 267; 267, identifier:subset; 268, else_clause; 268, 269; 269, block; 269, 270; 270, for_statement; 270, 271; 270, 274; 270, 278; 271, pattern_list; 271, 272; 271, 273; 272, identifier:i; 273, identifier:seq; 274, call; 274, 275; 274, 276; 275, identifier:enumerate; 276, argument_list; 276, 277; 277, identifier:subset; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, yield; 280, 281; 281, list:['%s fragment:%s' % (header, i), seq]; 281, 282; 281, 287; 282, binary_operator:%; 282, 283; 282, 284; 283, string:'%s fragment:%s'; 284, tuple; 284, 285; 284, 286; 285, identifier:header; 286, identifier:i; 287, identifier:seq
def shuffle_genome(genome, cat, fraction = float(100), plot = True, \ alpha = 0.1, beta = 100000, \ min_length = 1000, max_length = 200000): """ randomly shuffle genome """ header = '>randomized_%s' % (genome.name) sequence = list(''.join([i[1] for i in parse_fasta(genome)])) length = len(sequence) shuffled = [] # break genome into pieces while sequence is not False: s = int(random.gammavariate(alpha, beta)) if s <= min_length or s >= max_length: continue if len(sequence) < s: seq = sequence[0:] else: seq = sequence[0:s] sequence = sequence[s:] # if bool(random.getrandbits(1)) is True: # seq = rev_c(seq) # print('fragment length: %s reverse complement: True' % ('{:,}'.format(s)), file=sys.stderr) # else: # print('fragment length: %s reverse complement: False' % ('{:,}'.format(s)), file=sys.stderr) shuffled.append(''.join(seq)) if sequence == []: break # shuffle pieces random.shuffle(shuffled) # subset fragments if fraction == float(100): subset = shuffled else: max_pieces = int(length * fraction/100) subset, total = [], 0 for fragment in shuffled: length = len(fragment) if total + length <= max_pieces: subset.append(fragment) total += length else: diff = max_pieces - total subset.append(fragment[0:diff]) break # combine sequences, if requested if cat is True: yield [header, ''.join(subset)] else: for i, seq in enumerate(subset): yield ['%s fragment:%s' % (header, i), seq]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:sam2fastq; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:sam; 5, default_parameter; 5, 6; 5, 7; 6, identifier:singles; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:force; 10, False; 11, block; 11, 12; 11, 14; 11, 22; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 19; 16, pattern_list; 16, 17; 16, 18; 17, identifier:L; 18, identifier:R; 19, expression_list; 19, 20; 19, 21; 20, None; 21, None; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:line; 24, identifier:sam; 25, block; 25, 26; 25, 37; 25, 49; 25, 84; 25, 99; 25, 111; 25, 112; 25, 129; 25, 130; 25, 174; 25, 175; 25, 263; 26, if_statement; 26, 27; 26, 35; 27, comparison_operator:is; 27, 28; 27, 34; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:line; 31, identifier:startswith; 32, argument_list; 32, 33; 33, string:'@'; 34, True; 35, block; 35, 36; 36, continue_statement; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:line; 40, call; 40, 41; 40, 48; 41, attribute; 41, 42; 41, 47; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:line; 45, identifier:strip; 46, argument_list; 47, identifier:split; 48, argument_list; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:bit; 52, list_comprehension; 52, 53; 52, 59; 52, 60; 53, conditional_expression:if; 53, 54; 53, 55; 53, 58; 54, True; 55, comparison_operator:==; 55, 56; 55, 57; 56, identifier:i; 57, string:'1'; 58, False; 59, line_continuation:\; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:i; 62, subscript; 62, 63; 62, 79; 63, subscript; 63, 64; 63, 78; 64, call; 64, 65; 64, 76; 65, attribute; 65, 66; 65, 75; 66, call; 66, 67; 66, 68; 67, identifier:bin; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:int; 71, argument_list; 71, 72; 72, subscript; 72, 73; 72, 74; 73, identifier:line; 74, integer:1; 75, identifier:split; 76, argument_list; 76, 77; 77, string:'b'; 78, integer:1; 79, slice; 79, 80; 79, 81; 79, 82; 80, colon; 81, colon; 82, unary_operator:-; 82, 83; 83, integer:1; 84, while_statement; 84, 85; 84, 91; 85, comparison_operator:<; 85, 86; 85, 90; 86, call; 86, 87; 86, 88; 87, identifier:len; 88, argument_list; 88, 89; 89, identifier:bit; 90, integer:8; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:bit; 96, identifier:append; 97, argument_list; 97, 98; 98, False; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 110; 101, pattern_list; 101, 102; 101, 103; 101, 104; 101, 105; 101, 106; 101, 107; 101, 108; 101, 109; 102, identifier:pair; 103, identifier:proper; 104, identifier:na; 105, identifier:nap; 106, identifier:rev; 107, identifier:mrev; 108, identifier:left; 109, identifier:right; 110, identifier:bit; 111, comment; 112, if_statement; 112, 113; 112, 116; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:pair; 115, False; 116, block; 116, 117; 116, 128; 117, if_statement; 117, 118; 117, 121; 118, comparison_operator:is; 118, 119; 118, 120; 119, identifier:singles; 120, True; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:print_single; 125, argument_list; 125, 126; 125, 127; 126, identifier:line; 127, identifier:rev; 128, continue_statement; 129, comment; 130, if_statement; 130, 131; 130, 134; 130, 160; 131, comparison_operator:is; 131, 132; 131, 133; 132, identifier:rev; 133, True; 134, block; 134, 135; 134, 148; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:seq; 138, subscript; 138, 139; 138, 147; 139, call; 139, 140; 139, 141; 140, identifier:rc; 141, argument_list; 141, 142; 142, list:['', line[9]]; 142, 143; 142, 144; 143, string:''; 144, subscript; 144, 145; 144, 146; 145, identifier:line; 146, integer:9; 147, integer:1; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:qual; 151, subscript; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:line; 154, integer:10; 155, slice; 155, 156; 155, 157; 155, 158; 156, colon; 157, colon; 158, unary_operator:-; 158, 159; 159, integer:1; 160, else_clause; 160, 161; 161, block; 161, 162; 161, 168; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:seq; 165, subscript; 165, 166; 165, 167; 166, identifier:line; 167, integer:9; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:qual; 171, subscript; 171, 172; 171, 173; 172, identifier:line; 173, integer:10; 174, comment; 175, if_statement; 175, 176; 175, 179; 176, comparison_operator:is; 176, 177; 176, 178; 177, identifier:left; 178, True; 179, block; 179, 180; 179, 218; 179, 228; 179, 244; 180, if_statement; 180, 181; 180, 188; 181, boolean_operator:and; 181, 182; 181, 185; 182, comparison_operator:is; 182, 183; 182, 184; 183, identifier:L; 184, None; 185, comparison_operator:is; 185, 186; 185, 187; 186, identifier:force; 187, False; 188, block; 188, 189; 188, 199; 188, 214; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:print; 192, argument_list; 192, 193; 192, 194; 193, string:'sam file is not sorted'; 194, keyword_argument; 194, 195; 194, 196; 195, identifier:file; 196, attribute; 196, 197; 196, 198; 197, identifier:sys; 198, identifier:stderr; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:print; 202, argument_list; 202, 203; 202, 209; 203, binary_operator:%; 203, 204; 203, 205; 204, string:'\te.g.: %s'; 205, parenthesized_expression; 205, 206; 206, subscript; 206, 207; 206, 208; 207, identifier:line; 208, integer:0; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:file; 211, attribute; 211, 212; 211, 213; 212, identifier:sys; 213, identifier:stderr; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:exit; 217, argument_list; 218, if_statement; 218, 219; 218, 222; 219, comparison_operator:is; 219, 220; 219, 221; 220, identifier:L; 221, None; 222, block; 222, 223; 222, 227; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:L; 226, None; 227, continue_statement; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:L; 231, list:['@%s' % line[0], seq, '+%s' % line[0], qual]; 231, 232; 231, 237; 231, 238; 231, 243; 232, binary_operator:%; 232, 233; 232, 234; 233, string:'@%s'; 234, subscript; 234, 235; 234, 236; 235, identifier:line; 236, integer:0; 237, identifier:seq; 238, binary_operator:%; 238, 239; 238, 240; 239, string:'+%s'; 240, subscript; 240, 241; 240, 242; 241, identifier:line; 242, integer:0; 243, identifier:qual; 244, if_statement; 244, 245; 244, 248; 245, comparison_operator:is; 245, 246; 245, 247; 246, identifier:R; 247, None; 248, block; 248, 249; 248, 252; 248, 255; 249, expression_statement; 249, 250; 250, yield; 250, 251; 251, identifier:L; 252, expression_statement; 252, 253; 253, yield; 253, 254; 254, identifier:R; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 260; 257, pattern_list; 257, 258; 257, 259; 258, identifier:L; 259, identifier:R; 260, expression_list; 260, 261; 260, 262; 261, None; 262, None; 263, if_statement; 263, 264; 263, 267; 264, comparison_operator:is; 264, 265; 264, 266; 265, identifier:right; 266, True; 267, block; 267, 268; 267, 306; 267, 316; 267, 332; 268, if_statement; 268, 269; 268, 276; 269, boolean_operator:and; 269, 270; 269, 273; 270, comparison_operator:is; 270, 271; 270, 272; 271, identifier:R; 272, None; 273, comparison_operator:is; 273, 274; 273, 275; 274, identifier:force; 275, False; 276, block; 276, 277; 276, 287; 276, 302; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 280; 279, identifier:print; 280, argument_list; 280, 281; 280, 282; 281, string:'sam file is not sorted'; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:file; 284, attribute; 284, 285; 284, 286; 285, identifier:sys; 286, identifier:stderr; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 290; 289, identifier:print; 290, argument_list; 290, 291; 290, 297; 291, binary_operator:%; 291, 292; 291, 293; 292, string:'\te.g.: %s'; 293, parenthesized_expression; 293, 294; 294, subscript; 294, 295; 294, 296; 295, identifier:line; 296, integer:0; 297, keyword_argument; 297, 298; 297, 299; 298, identifier:file; 299, attribute; 299, 300; 299, 301; 300, identifier:sys; 301, identifier:stderr; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 305; 304, identifier:exit; 305, argument_list; 306, if_statement; 306, 307; 306, 310; 307, comparison_operator:is; 307, 308; 307, 309; 308, identifier:R; 309, None; 310, block; 310, 311; 310, 315; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:R; 314, None; 315, continue_statement; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:R; 319, list:['@%s' % line[0], seq, '+%s' % line[0], qual]; 319, 320; 319, 325; 319, 326; 319, 331; 320, binary_operator:%; 320, 321; 320, 322; 321, string:'@%s'; 322, subscript; 322, 323; 322, 324; 323, identifier:line; 324, integer:0; 325, identifier:seq; 326, binary_operator:%; 326, 327; 326, 328; 327, string:'+%s'; 328, subscript; 328, 329; 328, 330; 329, identifier:line; 330, integer:0; 331, identifier:qual; 332, if_statement; 332, 333; 332, 336; 333, comparison_operator:is; 333, 334; 333, 335; 334, identifier:L; 335, None; 336, block; 336, 337; 336, 340; 336, 343; 337, expression_statement; 337, 338; 338, yield; 338, 339; 339, identifier:L; 340, expression_statement; 340, 341; 341, yield; 341, 342; 342, identifier:R; 343, expression_statement; 343, 344; 344, assignment; 344, 345; 344, 348; 345, pattern_list; 345, 346; 345, 347; 346, identifier:L; 347, identifier:R; 348, expression_list; 348, 349; 348, 350; 349, None; 350, None
def sam2fastq(sam, singles = False, force = False): """ convert sam to fastq """ L, R = None, None for line in sam: if line.startswith('@') is True: continue line = line.strip().split() bit = [True if i == '1' else False \ for i in bin(int(line[1])).split('b')[1][::-1]] while len(bit) < 8: bit.append(False) pair, proper, na, nap, rev, mrev, left, right = bit # make sure read is paired if pair is False: if singles is True: print_single(line, rev) continue # check if sequence is reverse-complemented if rev is True: seq = rc(['', line[9]])[1] qual = line[10][::-1] else: seq = line[9] qual = line[10] # check if read is forward or reverse, return when both have been found if left is True: if L is not None and force is False: print('sam file is not sorted', file = sys.stderr) print('\te.g.: %s' % (line[0]), file = sys.stderr) exit() if L is not None: L = None continue L = ['@%s' % line[0], seq, '+%s' % line[0], qual] if R is not None: yield L yield R L, R = None, None if right is True: if R is not None and force is False: print('sam file is not sorted', file = sys.stderr) print('\te.g.: %s' % (line[0]), file = sys.stderr) exit() if R is not None: R = None continue R = ['@%s' % line[0], seq, '+%s' % line[0], qual] if L is not None: yield L yield R L, R = None, None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sort_sam; 3, parameters; 3, 4; 3, 5; 4, identifier:sam; 5, identifier:sort; 6, block; 6, 7; 6, 9; 6, 31; 6, 143; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:tempdir; 12, binary_operator:%; 12, 13; 12, 14; 13, string:'%s/'; 14, parenthesized_expression; 14, 15; 15, subscript; 15, 16; 15, 30; 16, call; 16, 17; 16, 27; 17, attribute; 17, 18; 17, 26; 18, call; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:os; 22, identifier:path; 23, identifier:abspath; 24, argument_list; 24, 25; 25, identifier:sam; 26, identifier:rsplit; 27, argument_list; 27, 28; 27, 29; 28, string:'/'; 29, integer:1; 30, integer:0; 31, if_statement; 31, 32; 31, 35; 31, 121; 32, comparison_operator:is; 32, 33; 32, 34; 33, identifier:sort; 34, True; 35, block; 35, 36; 35, 51; 35, 114; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:mapping; 39, binary_operator:%; 39, 40; 39, 41; 40, string:'%s.sorted.sam'; 41, parenthesized_expression; 41, 42; 42, subscript; 42, 43; 42, 50; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:sam; 46, identifier:rsplit; 47, argument_list; 47, 48; 47, 49; 48, string:'.'; 49, integer:1; 50, integer:0; 51, if_statement; 51, 52; 51, 55; 51, 81; 52, comparison_operator:!=; 52, 53; 52, 54; 53, identifier:sam; 54, string:'-'; 55, block; 55, 56; 56, if_statement; 56, 57; 56, 67; 57, comparison_operator:is; 57, 58; 57, 66; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:os; 62, identifier:path; 63, identifier:exists; 64, argument_list; 64, 65; 65, identifier:mapping; 66, False; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:os; 72, identifier:system; 73, argument_list; 73, 74; 74, binary_operator:%; 74, 75; 74, 76; 75, string:"\ sort -k1 --buffer-size=%sG -T %s -o %s %s\ "; 76, tuple; 76, 77; 76, 78; 76, 79; 76, 80; 77, identifier:sbuffer; 78, identifier:tempdir; 79, identifier:mapping; 80, identifier:sam; 81, else_clause; 81, 82; 82, block; 82, 83; 82, 87; 82, 108; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:mapping; 86, string:'stdin-sam.sorted.sam'; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:p; 90, call; 90, 91; 90, 92; 91, identifier:Popen; 92, argument_list; 92, 93; 92, 100; 92, 105; 93, binary_operator:%; 93, 94; 93, 95; 93, 96; 94, string:"sort -k1 --buffer-size=%sG -T %s -o %s"; 95, line_continuation:\; 96, tuple; 96, 97; 96, 98; 96, 99; 97, identifier:sbuffer; 98, identifier:tempdir; 99, identifier:mapping; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:stdin; 102, attribute; 102, 103; 102, 104; 103, identifier:sys; 104, identifier:stdin; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:shell; 107, True; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:p; 112, identifier:communicate; 113, argument_list; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:mapping; 117, call; 117, 118; 117, 119; 118, identifier:open; 119, argument_list; 119, 120; 120, identifier:mapping; 121, else_clause; 121, 122; 122, block; 122, 123; 123, if_statement; 123, 124; 123, 127; 123, 134; 124, comparison_operator:==; 124, 125; 124, 126; 125, identifier:sam; 126, string:'-'; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:mapping; 131, attribute; 131, 132; 131, 133; 132, identifier:sys; 133, identifier:stdin; 134, else_clause; 134, 135; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:mapping; 139, call; 139, 140; 139, 141; 140, identifier:open; 141, argument_list; 141, 142; 142, identifier:sam; 143, return_statement; 143, 144; 144, identifier:mapping
def sort_sam(sam, sort): """ sort sam file """ tempdir = '%s/' % (os.path.abspath(sam).rsplit('/', 1)[0]) if sort is True: mapping = '%s.sorted.sam' % (sam.rsplit('.', 1)[0]) if sam != '-': if os.path.exists(mapping) is False: os.system("\ sort -k1 --buffer-size=%sG -T %s -o %s %s\ " % (sbuffer, tempdir, mapping, sam)) else: mapping = 'stdin-sam.sorted.sam' p = Popen("sort -k1 --buffer-size=%sG -T %s -o %s" \ % (sbuffer, tempdir, mapping), stdin = sys.stdin, shell = True) p.communicate() mapping = open(mapping) else: if sam == '-': mapping = sys.stdin else: mapping = open(sam) return mapping
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:crossmap; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, identifier:fas; 5, identifier:reads; 6, identifier:options; 7, identifier:no_shrink; 8, identifier:keepDB; 9, identifier:threads; 10, identifier:cluster; 11, identifier:nodes; 12, block; 12, 13; 12, 15; 12, 24; 12, 28; 12, 215; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:is; 16, 17; 16, 18; 17, identifier:cluster; 18, True; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:threads; 23, string:'48'; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:btc; 27, list:[]; 28, for_statement; 28, 29; 28, 30; 28, 31; 29, identifier:fa; 30, identifier:fas; 31, block; 31, 32; 31, 40; 31, 47; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:btd; 35, call; 35, 36; 35, 37; 36, identifier:bowtiedb; 37, argument_list; 37, 38; 37, 39; 38, identifier:fa; 39, identifier:keepDB; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 46; 42, pattern_list; 42, 43; 42, 44; 42, 45; 43, identifier:F; 44, identifier:R; 45, identifier:U; 46, identifier:reads; 47, if_statement; 47, 48; 47, 51; 47, 144; 48, comparison_operator:is; 48, 49; 48, 50; 49, identifier:F; 50, False; 51, block; 51, 52; 51, 61; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:U; 55, False; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:u; 60, False; 61, for_statement; 61, 62; 61, 65; 61, 69; 62, pattern_list; 62, 63; 62, 64; 63, identifier:i; 64, identifier:f; 65, call; 65, 66; 65, 67; 66, identifier:enumerate; 67, argument_list; 67, 68; 68, identifier:F; 69, block; 69, 70; 69, 76; 69, 87; 69, 127; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:r; 73, subscript; 73, 74; 73, 75; 74, identifier:R; 75, identifier:i; 76, if_statement; 76, 77; 76, 80; 77, comparison_operator:is; 77, 78; 77, 79; 78, identifier:U; 79, False; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:u; 84, subscript; 84, 85; 84, 86; 85, identifier:U; 86, identifier:i; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:sam; 90, binary_operator:%; 90, 91; 90, 92; 91, string:'%s/%s-vs-%s'; 92, tuple; 92, 93; 92, 98; 92, 99; 92, 109; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:os; 96, identifier:getcwd; 97, argument_list; 98, line_continuation:\; 99, subscript; 99, 100; 99, 107; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:fa; 103, identifier:rsplit; 104, argument_list; 104, 105; 104, 106; 105, string:'/'; 106, integer:1; 107, unary_operator:-; 107, 108; 108, integer:1; 109, subscript; 109, 110; 109, 126; 110, call; 110, 111; 110, 123; 111, attribute; 111, 112; 111, 122; 112, subscript; 112, 113; 112, 120; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:f; 116, identifier:rsplit; 117, argument_list; 117, 118; 117, 119; 118, string:'/'; 119, integer:1; 120, unary_operator:-; 120, 121; 121, integer:1; 122, identifier:rsplit; 123, argument_list; 123, 124; 123, 125; 124, string:'.'; 125, integer:3; 126, integer:0; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:btc; 131, identifier:append; 132, argument_list; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:bowtie; 135, argument_list; 135, 136; 135, 137; 135, 138; 135, 139; 135, 140; 135, 141; 135, 142; 135, 143; 136, identifier:sam; 137, identifier:btd; 138, identifier:f; 139, identifier:r; 140, identifier:u; 141, identifier:options; 142, identifier:no_shrink; 143, identifier:threads; 144, else_clause; 144, 145; 145, block; 145, 146; 145, 150; 145, 154; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:f; 149, False; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:r; 153, False; 154, for_statement; 154, 155; 154, 156; 154, 157; 155, identifier:u; 156, identifier:U; 157, block; 157, 158; 157, 198; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:sam; 161, binary_operator:%; 161, 162; 161, 163; 162, string:'%s/%s-vs-%s'; 163, tuple; 163, 164; 163, 169; 163, 170; 163, 180; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:os; 167, identifier:getcwd; 168, argument_list; 169, line_continuation:\; 170, subscript; 170, 171; 170, 178; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:fa; 174, identifier:rsplit; 175, argument_list; 175, 176; 175, 177; 176, string:'/'; 177, integer:1; 178, unary_operator:-; 178, 179; 179, integer:1; 180, subscript; 180, 181; 180, 197; 181, call; 181, 182; 181, 194; 182, attribute; 182, 183; 182, 193; 183, subscript; 183, 184; 183, 191; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:u; 187, identifier:rsplit; 188, argument_list; 188, 189; 188, 190; 189, string:'/'; 190, integer:1; 191, unary_operator:-; 191, 192; 192, integer:1; 193, identifier:rsplit; 194, argument_list; 194, 195; 194, 196; 195, string:'.'; 196, integer:3; 197, integer:0; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:btc; 202, identifier:append; 203, argument_list; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:bowtie; 206, argument_list; 206, 207; 206, 208; 206, 209; 206, 210; 206, 211; 206, 212; 206, 213; 206, 214; 207, identifier:sam; 208, identifier:btd; 209, identifier:f; 210, identifier:r; 211, identifier:u; 212, identifier:options; 213, identifier:no_shrink; 214, identifier:threads; 215, if_statement; 215, 216; 215, 219; 215, 242; 216, comparison_operator:is; 216, 217; 216, 218; 217, identifier:cluster; 218, False; 219, block; 219, 220; 220, for_statement; 220, 221; 220, 222; 220, 223; 221, identifier:i; 222, identifier:btc; 223, block; 223, 224; 223, 236; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:p; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:subprocess; 230, identifier:Popen; 231, argument_list; 231, 232; 231, 233; 232, identifier:i; 233, keyword_argument; 233, 234; 233, 235; 234, identifier:shell; 235, True; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:p; 240, identifier:communicate; 241, argument_list; 242, else_clause; 242, 243; 243, block; 243, 244; 243, 275; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:ID; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, string:''; 250, identifier:join; 251, generator_expression; 251, 252; 251, 269; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:random; 255, identifier:choice; 256, argument_list; 256, 257; 257, list_comprehension; 257, 258; 257, 262; 258, call; 258, 259; 258, 260; 259, identifier:str; 260, argument_list; 260, 261; 261, identifier:i; 262, for_in_clause; 262, 263; 262, 264; 263, identifier:i; 264, call; 264, 265; 264, 266; 265, identifier:range; 266, argument_list; 266, 267; 266, 268; 267, integer:0; 268, integer:9; 269, for_in_clause; 269, 270; 269, 271; 270, identifier:_; 271, call; 271, 272; 271, 273; 272, identifier:range; 273, argument_list; 273, 274; 274, integer:5; 275, for_statement; 275, 276; 275, 279; 275, 288; 276, pattern_list; 276, 277; 276, 278; 277, identifier:node; 278, identifier:commands; 279, call; 279, 280; 279, 281; 280, identifier:enumerate; 281, argument_list; 281, 282; 281, 287; 282, call; 282, 283; 282, 284; 283, identifier:chunks; 284, argument_list; 284, 285; 284, 286; 285, identifier:btc; 286, identifier:nodes; 287, integer:1; 288, block; 288, 289; 288, 306; 288, 319; 288, 325; 288, 344; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:bs; 292, call; 292, 293; 292, 294; 293, identifier:open; 294, argument_list; 294, 295; 294, 305; 295, binary_operator:%; 295, 296; 295, 297; 296, string:'%s/crossmap-qsub.%s.%s.sh'; 297, tuple; 297, 298; 297, 303; 297, 304; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:os; 301, identifier:getcwd; 302, argument_list; 303, identifier:ID; 304, identifier:node; 305, string:'w'; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 309; 308, identifier:print; 309, argument_list; 309, 310; 309, 316; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, string:'\n'; 313, identifier:join; 314, argument_list; 314, 315; 315, identifier:commands; 316, keyword_argument; 316, 317; 316, 318; 317, identifier:file; 318, identifier:bs; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:bs; 323, identifier:close; 324, argument_list; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:p; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:subprocess; 331, identifier:Popen; 332, argument_list; 332, 333; 332, 340; 332, 341; 333, binary_operator:%; 333, 334; 333, 335; 333, 336; 334, string:'qsub -V -N crossmap %s'; 335, line_continuation:\; 336, parenthesized_expression; 336, 337; 337, attribute; 337, 338; 337, 339; 338, identifier:bs; 339, identifier:name; 340, line_continuation:\; 341, keyword_argument; 341, 342; 341, 343; 342, identifier:shell; 343, True; 344, expression_statement; 344, 345; 345, call; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:p; 348, identifier:communicate; 349, argument_list
def crossmap(fas, reads, options, no_shrink, keepDB, threads, cluster, nodes): """ map all read sets against all fasta files """ if cluster is True: threads = '48' btc = [] for fa in fas: btd = bowtiedb(fa, keepDB) F, R, U = reads if F is not False: if U is False: u = False for i, f in enumerate(F): r = R[i] if U is not False: u = U[i] sam = '%s/%s-vs-%s' % (os.getcwd(), \ fa.rsplit('/', 1)[-1], f.rsplit('/', 1)[-1].rsplit('.', 3)[0]) btc.append(bowtie(sam, btd, f, r, u, options, no_shrink, threads)) else: f = False r = False for u in U: sam = '%s/%s-vs-%s' % (os.getcwd(), \ fa.rsplit('/', 1)[-1], u.rsplit('/', 1)[-1].rsplit('.', 3)[0]) btc.append(bowtie(sam, btd, f, r, u, options, no_shrink, threads)) if cluster is False: for i in btc: p = subprocess.Popen(i, shell = True) p.communicate() else: ID = ''.join(random.choice([str(i) for i in range(0, 9)]) for _ in range(5)) for node, commands in enumerate(chunks(btc, nodes), 1): bs = open('%s/crossmap-qsub.%s.%s.sh' % (os.getcwd(), ID, node), 'w') print('\n'.join(commands), file=bs) bs.close() p = subprocess.Popen(\ 'qsub -V -N crossmap %s' \ % (bs.name), \ shell = True) p.communicate()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:bit_by_bit; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:in_data; 6, block; 6, 7; 6, 9; 6, 10; 6, 28; 6, 34; 6, 100; 6, 138; 6, 155; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, if_statement; 10, 11; 10, 16; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 15; 14, identifier:in_data; 15, identifier:str; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:in_data; 20, list_comprehension; 20, 21; 20, 25; 21, call; 21, 22; 21, 23; 22, identifier:ord; 23, argument_list; 23, 24; 24, identifier:c; 25, for_in_clause; 25, 26; 25, 27; 26, identifier:c; 27, identifier:in_data; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:register; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:NonDirectInit; 34, for_statement; 34, 35; 34, 36; 34, 37; 35, identifier:octet; 36, identifier:in_data; 37, block; 37, 38; 37, 53; 38, if_statement; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:ReflectIn; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:octet; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:reflect; 50, argument_list; 50, 51; 50, 52; 51, identifier:octet; 52, integer:8; 53, for_statement; 53, 54; 53, 55; 53, 59; 54, identifier:i; 55, call; 55, 56; 55, 57; 56, identifier:range; 57, argument_list; 57, 58; 58, integer:8; 59, block; 59, 60; 59, 68; 59, 91; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:topbit; 63, binary_operator:&; 63, 64; 63, 65; 64, identifier:register; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:MSB_Mask; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:register; 71, binary_operator:|; 71, 72; 71, 81; 72, parenthesized_expression; 72, 73; 73, binary_operator:&; 73, 74; 73, 78; 74, parenthesized_expression; 74, 75; 75, binary_operator:<<; 75, 76; 75, 77; 76, identifier:register; 77, integer:1; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:Mask; 81, parenthesized_expression; 81, 82; 82, binary_operator:&; 82, 83; 82, 90; 83, parenthesized_expression; 83, 84; 84, binary_operator:>>; 84, 85; 84, 86; 85, identifier:octet; 86, parenthesized_expression; 86, 87; 87, binary_operator:-; 87, 88; 87, 89; 88, integer:7; 89, identifier:i; 90, integer:0x01; 91, if_statement; 91, 92; 91, 93; 92, identifier:topbit; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, augmented_assignment:^=; 95, 96; 95, 97; 96, identifier:register; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:Poly; 100, for_statement; 100, 101; 100, 102; 100, 108; 101, identifier:i; 102, call; 102, 103; 102, 104; 103, identifier:range; 104, argument_list; 104, 105; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:Width; 108, block; 108, 109; 108, 117; 108, 129; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:topbit; 112, binary_operator:&; 112, 113; 112, 114; 113, identifier:register; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:MSB_Mask; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:register; 120, parenthesized_expression; 120, 121; 121, binary_operator:&; 121, 122; 121, 126; 122, parenthesized_expression; 122, 123; 123, binary_operator:<<; 123, 124; 123, 125; 124, identifier:register; 125, integer:1; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:Mask; 129, if_statement; 129, 130; 129, 131; 130, identifier:topbit; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, augmented_assignment:^=; 133, 134; 133, 135; 134, identifier:register; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:Poly; 138, if_statement; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:ReflectOut; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:register; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:reflect; 150, argument_list; 150, 151; 150, 152; 151, identifier:register; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:Width; 155, return_statement; 155, 156; 156, binary_operator:^; 156, 157; 156, 158; 157, identifier:register; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:XorOut
def bit_by_bit(self, in_data): """ Classic simple and slow CRC implementation. This function iterates bit by bit over the augmented input message and returns the calculated CRC value at the end. """ # If the input data is a string, convert to bytes. if isinstance(in_data, str): in_data = [ord(c) for c in in_data] register = self.NonDirectInit for octet in in_data: if self.ReflectIn: octet = self.reflect(octet, 8) for i in range(8): topbit = register & self.MSB_Mask register = ((register << 1) & self.Mask) | ((octet >> (7 - i)) & 0x01) if topbit: register ^= self.Poly for i in range(self.Width): topbit = register & self.MSB_Mask register = ((register << 1) & self.Mask) if topbit: register ^= self.Poly if self.ReflectOut: register = self.reflect(register, self.Width) return register ^ self.XorOut
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parse_ggKbase_tables; 3, parameters; 3, 4; 3, 5; 4, identifier:tables; 5, identifier:id_type; 6, block; 6, 7; 6, 9; 6, 13; 6, 282; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:g2info; 12, dictionary; 13, for_statement; 13, 14; 13, 15; 13, 16; 14, identifier:table; 15, identifier:tables; 16, block; 16, 17; 17, for_statement; 17, 18; 17, 19; 17, 23; 18, identifier:line; 19, call; 19, 20; 19, 21; 20, identifier:open; 21, argument_list; 21, 22; 22, identifier:table; 23, block; 23, 24; 23, 37; 23, 70; 23, 84; 23, 95; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:line; 27, call; 27, 28; 27, 35; 28, attribute; 28, 29; 28, 34; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:line; 32, identifier:strip; 33, argument_list; 34, identifier:split; 35, argument_list; 35, 36; 36, string:'\t'; 37, if_statement; 37, 38; 37, 46; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:line; 42, integer:0; 43, identifier:startswith; 44, argument_list; 44, 45; 45, string:'name'; 46, block; 46, 47; 46, 51; 46, 57; 46, 63; 46, 69; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:header; 50, identifier:line; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:header; 55, integer:4; 56, string:'genome size (bp)'; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:header; 61, integer:12; 62, string:'#SCGs'; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:header; 67, integer:13; 68, string:'#SCG duplicates'; 69, continue_statement; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 76; 72, pattern_list; 72, 73; 72, 74; 72, 75; 73, identifier:name; 74, identifier:code; 75, identifier:info; 76, expression_list; 76, 77; 76, 80; 76, 83; 77, subscript; 77, 78; 77, 79; 78, identifier:line; 79, integer:0; 80, subscript; 80, 81; 80, 82; 81, identifier:line; 82, integer:1; 83, identifier:line; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:info; 87, list_comprehension; 87, 88; 87, 92; 88, call; 88, 89; 88, 90; 89, identifier:to_int; 90, argument_list; 90, 91; 91, identifier:i; 92, for_in_clause; 92, 93; 92, 94; 93, identifier:i; 94, identifier:info; 95, if_statement; 95, 96; 95, 99; 95, 100; 95, 201; 96, comparison_operator:is; 96, 97; 96, 98; 97, identifier:id_type; 98, False; 99, comment; 100, block; 100, 101; 100, 114; 100, 155; 100, 178; 101, if_statement; 101, 102; 101, 109; 102, boolean_operator:or; 102, 103; 102, 106; 103, comparison_operator:in; 103, 104; 103, 105; 104, string:'UNK'; 105, identifier:code; 106, comparison_operator:in; 106, 107; 106, 108; 107, string:'unknown'; 108, identifier:code; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:code; 113, identifier:name; 114, if_statement; 114, 115; 114, 126; 115, boolean_operator:and; 115, 116; 115, 120; 116, parenthesized_expression; 116, 117; 117, comparison_operator:!=; 117, 118; 117, 119; 118, identifier:name; 119, identifier:code; 120, parenthesized_expression; 120, 121; 121, boolean_operator:and; 121, 122; 121, 123; 122, identifier:name; 123, comparison_operator:in; 123, 124; 123, 125; 124, identifier:code; 125, identifier:g2info; 126, block; 126, 127; 126, 137; 126, 151; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:print; 130, argument_list; 130, 131; 130, 132; 131, string:'# duplicate name or code in table(s)'; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:file; 134, attribute; 134, 135; 134, 136; 135, identifier:sys; 136, identifier:stderr; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:print; 140, argument_list; 140, 141; 140, 146; 141, binary_operator:%; 141, 142; 141, 143; 142, string:'# %s and/or %s'; 143, tuple; 143, 144; 143, 145; 144, identifier:name; 145, identifier:code; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:file; 148, attribute; 148, 149; 148, 150; 149, identifier:sys; 150, identifier:stderr; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:exit; 154, argument_list; 155, if_statement; 155, 156; 155, 159; 156, comparison_operator:not; 156, 157; 156, 158; 157, identifier:name; 158, identifier:g2info; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:g2info; 164, identifier:name; 165, dictionary_comprehension; 165, 166; 165, 169; 166, pair; 166, 167; 166, 168; 167, identifier:item; 168, identifier:stat; 169, for_in_clause; 169, 170; 169, 173; 170, pattern_list; 170, 171; 170, 172; 171, identifier:item; 172, identifier:stat; 173, call; 173, 174; 173, 175; 174, identifier:zip; 175, argument_list; 175, 176; 175, 177; 176, identifier:header; 177, identifier:info; 178, if_statement; 178, 179; 178, 182; 179, comparison_operator:not; 179, 180; 179, 181; 180, identifier:code; 181, identifier:g2info; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:g2info; 187, identifier:code; 188, dictionary_comprehension; 188, 189; 188, 192; 189, pair; 189, 190; 189, 191; 190, identifier:item; 191, identifier:stat; 192, for_in_clause; 192, 193; 192, 196; 193, pattern_list; 193, 194; 193, 195; 194, identifier:item; 195, identifier:stat; 196, call; 196, 197; 196, 198; 197, identifier:zip; 198, argument_list; 198, 199; 198, 200; 199, identifier:header; 200, identifier:info; 201, else_clause; 201, 202; 202, block; 202, 203; 202, 237; 202, 247; 202, 265; 203, if_statement; 203, 204; 203, 207; 203, 212; 203, 221; 204, comparison_operator:==; 204, 205; 204, 206; 205, identifier:id_type; 206, string:'name'; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:ID; 211, identifier:name; 212, elif_clause; 212, 213; 212, 216; 213, comparison_operator:==; 213, 214; 213, 215; 214, identifier:id_type; 215, string:'code'; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:ID; 220, identifier:code; 221, else_clause; 221, 222; 222, block; 222, 223; 222, 233; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:print; 226, argument_list; 226, 227; 226, 228; 227, string:'# specify name or code column using -id'; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:file; 230, attribute; 230, 231; 230, 232; 231, identifier:sys; 232, identifier:stderr; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 236; 235, identifier:exit; 236, argument_list; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:ID; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:ID; 243, identifier:replace; 244, argument_list; 244, 245; 244, 246; 245, string:' '; 246, string:''; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 252; 249, subscript; 249, 250; 249, 251; 250, identifier:g2info; 251, identifier:ID; 252, dictionary_comprehension; 252, 253; 252, 256; 253, pair; 253, 254; 253, 255; 254, identifier:item; 255, identifier:stat; 256, for_in_clause; 256, 257; 256, 260; 257, pattern_list; 257, 258; 257, 259; 258, identifier:item; 259, identifier:stat; 260, call; 260, 261; 260, 262; 261, identifier:zip; 262, argument_list; 262, 263; 262, 264; 263, identifier:header; 264, identifier:info; 265, if_statement; 265, 266; 265, 273; 266, comparison_operator:==; 266, 267; 266, 272; 267, subscript; 267, 268; 267, 271; 268, subscript; 268, 269; 268, 270; 269, identifier:g2info; 270, identifier:ID; 271, string:'genome size (bp)'; 272, string:''; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 281; 276, subscript; 276, 277; 276, 280; 277, subscript; 277, 278; 277, 279; 278, identifier:g2info; 279, identifier:ID; 280, string:'genome size (bp)'; 281, integer:0; 282, return_statement; 282, 283; 283, identifier:g2info
def parse_ggKbase_tables(tables, id_type): """ convert ggKbase genome info tables to dictionary """ g2info = {} for table in tables: for line in open(table): line = line.strip().split('\t') if line[0].startswith('name'): header = line header[4] = 'genome size (bp)' header[12] = '#SCGs' header[13] = '#SCG duplicates' continue name, code, info = line[0], line[1], line info = [to_int(i) for i in info] if id_type is False: # try to use name and code ID if 'UNK' in code or 'unknown' in code: code = name if (name != code) and (name and code in g2info): print('# duplicate name or code in table(s)', file=sys.stderr) print('# %s and/or %s' % (name, code), file=sys.stderr) exit() if name not in g2info: g2info[name] = {item:stat for item, stat in zip(header, info)} if code not in g2info: g2info[code] = {item:stat for item, stat in zip(header, info)} else: if id_type == 'name': ID = name elif id_type == 'code': ID = code else: print('# specify name or code column using -id', file=sys.stderr) exit() ID = ID.replace(' ', '') g2info[ID] = {item:stat for item, stat in zip(header, info)} if g2info[ID]['genome size (bp)'] == '': g2info[ID]['genome size (bp)'] = 0 return g2info
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:top_hits; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:hits; 5, identifier:num; 6, identifier:column; 7, identifier:reverse; 8, block; 8, 9; 8, 11; 8, 26; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:hits; 15, identifier:sort; 16, argument_list; 16, 17; 16, 23; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:key; 19, call; 19, 20; 19, 21; 20, identifier:itemgetter; 21, argument_list; 21, 22; 22, identifier:column; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:reverse; 25, identifier:reverse; 26, for_statement; 26, 27; 26, 28; 26, 34; 27, identifier:hit; 28, subscript; 28, 29; 28, 30; 29, identifier:hits; 30, slice; 30, 31; 30, 32; 30, 33; 31, integer:0; 32, colon; 33, identifier:num; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, yield; 36, 37; 37, identifier:hit
def top_hits(hits, num, column, reverse): """ get top hits after sorting by column number """ hits.sort(key = itemgetter(column), reverse = reverse) for hit in hits[0:num]: yield hit
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:numBlast_sort; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:blast; 5, identifier:numHits; 6, identifier:evalueT; 7, identifier:bitT; 8, block; 8, 9; 8, 11; 8, 27; 8, 30; 8, 40; 8, 140; 8, 149; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:header; 14, list:['#query', 'target', 'pident', 'alen', 'mismatch', 'gapopen', 'qstart', 'qend', 'tstart', 'tend', 'evalue', 'bitscore']; 14, 15; 14, 16; 14, 17; 14, 18; 14, 19; 14, 20; 14, 21; 14, 22; 14, 23; 14, 24; 14, 25; 14, 26; 15, string:'#query'; 16, string:'target'; 17, string:'pident'; 18, string:'alen'; 19, string:'mismatch'; 20, string:'gapopen'; 21, string:'qstart'; 22, string:'qend'; 23, string:'tstart'; 24, string:'tend'; 25, string:'evalue'; 26, string:'bitscore'; 27, expression_statement; 27, 28; 28, yield; 28, 29; 29, identifier:header; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:hmm; 33, dictionary_comprehension; 33, 34; 33, 37; 34, pair; 34, 35; 34, 36; 35, identifier:h; 36, list:[]; 37, for_in_clause; 37, 38; 37, 39; 38, identifier:h; 39, identifier:header; 40, for_statement; 40, 41; 40, 42; 40, 43; 41, identifier:line; 42, identifier:blast; 43, block; 43, 44; 43, 53; 43, 66; 43, 67; 43, 89; 43, 101; 43, 111; 43, 121; 44, if_statement; 44, 45; 44, 51; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:line; 48, identifier:startswith; 49, argument_list; 49, 50; 50, string:'#'; 51, block; 51, 52; 52, continue_statement; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:line; 56, call; 56, 57; 56, 64; 57, attribute; 57, 58; 57, 63; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:line; 61, identifier:strip; 62, argument_list; 63, identifier:split; 64, argument_list; 64, 65; 65, string:'\t'; 66, comment; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 76; 69, pattern_list; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:line; 72, integer:10; 73, subscript; 73, 74; 73, 75; 74, identifier:line; 75, integer:11; 76, expression_list; 76, 77; 76, 83; 77, call; 77, 78; 77, 79; 78, identifier:float; 79, argument_list; 79, 80; 80, subscript; 80, 81; 80, 82; 81, identifier:line; 82, integer:10; 83, call; 83, 84; 83, 85; 84, identifier:float; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:line; 88, integer:11; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, pattern_list; 91, 92; 91, 93; 92, identifier:evalue; 93, identifier:bit; 94, expression_list; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:line; 97, integer:10; 98, subscript; 98, 99; 98, 100; 99, identifier:line; 100, integer:11; 101, if_statement; 101, 102; 101, 109; 102, boolean_operator:and; 102, 103; 102, 106; 103, comparison_operator:is; 103, 104; 103, 105; 104, identifier:evalueT; 105, False; 106, comparison_operator:>; 106, 107; 106, 108; 107, identifier:evalue; 108, identifier:evalueT; 109, block; 109, 110; 110, continue_statement; 111, if_statement; 111, 112; 111, 119; 112, boolean_operator:and; 112, 113; 112, 116; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:bitT; 115, False; 116, comparison_operator:<; 116, 117; 116, 118; 117, identifier:bit; 118, identifier:bitT; 119, block; 119, 120; 120, continue_statement; 121, for_statement; 121, 122; 121, 125; 121, 130; 122, pattern_list; 122, 123; 122, 124; 123, identifier:i; 124, identifier:h; 125, call; 125, 126; 125, 127; 126, identifier:zip; 127, argument_list; 127, 128; 127, 129; 128, identifier:line; 129, identifier:header; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 138; 133, attribute; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:hmm; 136, identifier:h; 137, identifier:append; 138, argument_list; 138, 139; 139, identifier:i; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:hmm; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:pd; 146, identifier:DataFrame; 147, argument_list; 147, 148; 148, identifier:hmm; 149, for_statement; 149, 150; 149, 153; 149, 162; 150, pattern_list; 150, 151; 150, 152; 151, identifier:query; 152, identifier:df; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:hmm; 156, identifier:groupby; 157, argument_list; 157, 158; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:by; 160, list:['#query']; 160, 161; 161, string:'#query'; 162, block; 162, 163; 162, 178; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:df; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:df; 169, identifier:sort_values; 170, argument_list; 170, 171; 170, 175; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:by; 173, list:['bitscore']; 173, 174; 174, string:'bitscore'; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:ascending; 177, False; 178, for_statement; 178, 179; 178, 180; 178, 190; 179, identifier:hit; 180, subscript; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, subscript; 182, 183; 182, 184; 183, identifier:df; 184, identifier:header; 185, identifier:values; 186, slice; 186, 187; 186, 188; 186, 189; 187, integer:0; 188, colon; 189, identifier:numHits; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, yield; 192, 193; 193, identifier:hit
def numBlast_sort(blast, numHits, evalueT, bitT): """ parse b6 output with sorting """ header = ['#query', 'target', 'pident', 'alen', 'mismatch', 'gapopen', 'qstart', 'qend', 'tstart', 'tend', 'evalue', 'bitscore'] yield header hmm = {h:[] for h in header} for line in blast: if line.startswith('#'): continue line = line.strip().split('\t') # Evalue and Bitscore thresholds line[10], line[11] = float(line[10]), float(line[11]) evalue, bit = line[10], line[11] if evalueT is not False and evalue > evalueT: continue if bitT is not False and bit < bitT: continue for i, h in zip(line, header): hmm[h].append(i) hmm = pd.DataFrame(hmm) for query, df in hmm.groupby(by = ['#query']): df = df.sort_values(by = ['bitscore'], ascending = False) for hit in df[header].values[0:numHits]: yield hit
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:numDomtblout; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:domtblout; 5, identifier:numHits; 6, identifier:evalueT; 7, identifier:bitT; 8, identifier:sort; 9, block; 9, 10; 9, 12; 9, 31; 9, 58; 9, 61; 9, 69; 9, 280; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:sort; 15, True; 16, block; 16, 17; 16, 30; 17, for_statement; 17, 18; 17, 19; 17, 26; 18, identifier:hit; 19, call; 19, 20; 19, 21; 20, identifier:numDomtblout_sort; 21, argument_list; 21, 22; 21, 23; 21, 24; 21, 25; 22, identifier:domtblout; 23, identifier:numHits; 24, identifier:evalueT; 25, identifier:bitT; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, yield; 28, 29; 29, identifier:hit; 30, return_statement; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:header; 34, list:['#target name', 'target accession', 'tlen', 'query name', 'query accession', 'qlen', 'full E-value', 'full score', 'full bias', 'domain #', '# domains', 'domain c-Evalue', 'domain i-Evalue', 'domain score', 'domain bias', 'hmm from', 'hmm to', 'seq from', 'seq to', 'env from', 'env to', 'acc', 'target description']; 34, 35; 34, 36; 34, 37; 34, 38; 34, 39; 34, 40; 34, 41; 34, 42; 34, 43; 34, 44; 34, 45; 34, 46; 34, 47; 34, 48; 34, 49; 34, 50; 34, 51; 34, 52; 34, 53; 34, 54; 34, 55; 34, 56; 34, 57; 35, string:'#target name'; 36, string:'target accession'; 37, string:'tlen'; 38, string:'query name'; 39, string:'query accession'; 40, string:'qlen'; 41, string:'full E-value'; 42, string:'full score'; 43, string:'full bias'; 44, string:'domain #'; 45, string:'# domains'; 46, string:'domain c-Evalue'; 47, string:'domain i-Evalue'; 48, string:'domain score'; 49, string:'domain bias'; 50, string:'hmm from'; 51, string:'hmm to'; 52, string:'seq from'; 53, string:'seq to'; 54, string:'env from'; 55, string:'env to'; 56, string:'acc'; 57, string:'target description'; 58, expression_statement; 58, 59; 59, yield; 59, 60; 60, identifier:header; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 66; 63, pattern_list; 63, 64; 63, 65; 64, identifier:prev; 65, identifier:hits; 66, expression_list; 66, 67; 66, 68; 67, None; 68, list:[]; 69, for_statement; 69, 70; 69, 71; 69, 72; 70, identifier:line; 71, identifier:domtblout; 72, block; 72, 73; 72, 82; 72, 83; 72, 95; 72, 108; 72, 117; 72, 124; 72, 125; 72, 135; 72, 136; 72, 158; 72, 170; 72, 182; 72, 212; 72, 276; 73, if_statement; 73, 74; 73, 80; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:line; 77, identifier:startswith; 78, argument_list; 78, 79; 79, string:'#'; 80, block; 80, 81; 81, continue_statement; 82, comment; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:line; 86, call; 86, 87; 86, 94; 87, attribute; 87, 88; 87, 93; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:line; 91, identifier:strip; 92, argument_list; 93, identifier:split; 94, argument_list; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:desc; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, string:' '; 101, identifier:join; 102, argument_list; 102, 103; 103, subscript; 103, 104; 103, 105; 104, identifier:line; 105, slice; 105, 106; 105, 107; 106, integer:18; 107, colon; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:line; 111, subscript; 111, 112; 111, 113; 112, identifier:line; 113, slice; 113, 114; 113, 115; 113, 116; 114, integer:0; 115, colon; 116, integer:18; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:line; 121, identifier:append; 122, argument_list; 122, 123; 123, identifier:desc; 124, comment; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:ID; 128, binary_operator:+; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:line; 131, integer:0; 132, subscript; 132, 133; 132, 134; 133, identifier:line; 134, integer:9; 135, comment; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 145; 138, pattern_list; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:line; 141, integer:11; 142, subscript; 142, 143; 142, 144; 143, identifier:line; 144, integer:13; 145, expression_list; 145, 146; 145, 152; 146, call; 146, 147; 146, 148; 147, identifier:float; 148, argument_list; 148, 149; 149, subscript; 149, 150; 149, 151; 150, identifier:line; 151, integer:11; 152, call; 152, 153; 152, 154; 153, identifier:float; 154, argument_list; 154, 155; 155, subscript; 155, 156; 155, 157; 156, identifier:line; 157, integer:13; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, pattern_list; 160, 161; 160, 162; 161, identifier:evalue; 162, identifier:bitscore; 163, expression_list; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:line; 166, integer:11; 167, subscript; 167, 168; 167, 169; 168, identifier:line; 169, integer:13; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 179; 172, pattern_list; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:line; 175, integer:11; 176, subscript; 176, 177; 176, 178; 177, identifier:line; 178, integer:13; 179, expression_list; 179, 180; 179, 181; 180, identifier:evalue; 181, identifier:bitscore; 182, if_statement; 182, 183; 182, 186; 183, comparison_operator:!=; 183, 184; 183, 185; 184, identifier:ID; 185, identifier:prev; 186, block; 186, 187; 186, 208; 187, if_statement; 187, 188; 187, 194; 188, comparison_operator:>; 188, 189; 188, 193; 189, call; 189, 190; 189, 191; 190, identifier:len; 191, argument_list; 191, 192; 192, identifier:hits; 193, integer:0; 194, block; 194, 195; 195, for_statement; 195, 196; 195, 197; 195, 204; 196, identifier:hit; 197, call; 197, 198; 197, 199; 198, identifier:top_hits; 199, argument_list; 199, 200; 199, 201; 199, 202; 199, 203; 200, identifier:hits; 201, identifier:numHits; 202, integer:13; 203, True; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, yield; 206, 207; 207, identifier:hit; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:hits; 211, list:[]; 212, if_statement; 212, 213; 212, 220; 212, 228; 212, 244; 212, 260; 213, boolean_operator:and; 213, 214; 213, 217; 214, comparison_operator:==; 214, 215; 214, 216; 215, identifier:evalueT; 216, False; 217, comparison_operator:==; 217, 218; 217, 219; 218, identifier:bitT; 219, False; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:hits; 225, identifier:append; 226, argument_list; 226, 227; 227, identifier:line; 228, elif_clause; 228, 229; 228, 236; 229, boolean_operator:and; 229, 230; 229, 233; 230, comparison_operator:<=; 230, 231; 230, 232; 231, identifier:evalue; 232, identifier:evalueT; 233, comparison_operator:==; 233, 234; 233, 235; 234, identifier:bitT; 235, False; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:hits; 241, identifier:append; 242, argument_list; 242, 243; 243, identifier:line; 244, elif_clause; 244, 245; 244, 252; 245, boolean_operator:and; 245, 246; 245, 249; 246, comparison_operator:<=; 246, 247; 246, 248; 247, identifier:evalue; 248, identifier:evalueT; 249, comparison_operator:>=; 249, 250; 249, 251; 250, identifier:bit; 251, identifier:bitT; 252, block; 252, 253; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:hits; 257, identifier:append; 258, argument_list; 258, 259; 259, identifier:line; 260, elif_clause; 260, 261; 260, 268; 261, boolean_operator:and; 261, 262; 261, 265; 262, comparison_operator:==; 262, 263; 262, 264; 263, identifier:evalueT; 264, False; 265, comparison_operator:>=; 265, 266; 265, 267; 266, identifier:bit; 267, identifier:bitT; 268, block; 268, 269; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:hits; 273, identifier:append; 274, argument_list; 274, 275; 275, identifier:line; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:prev; 279, identifier:ID; 280, for_statement; 280, 281; 280, 282; 280, 289; 281, identifier:hit; 282, call; 282, 283; 282, 284; 283, identifier:top_hits; 284, argument_list; 284, 285; 284, 286; 284, 287; 284, 288; 285, identifier:hits; 286, identifier:numHits; 287, integer:13; 288, True; 289, block; 289, 290; 290, expression_statement; 290, 291; 291, yield; 291, 292; 292, identifier:hit
def numDomtblout(domtblout, numHits, evalueT, bitT, sort): """ parse hmm domain table output this version is faster but does not work unless the table is sorted """ if sort is True: for hit in numDomtblout_sort(domtblout, numHits, evalueT, bitT): yield hit return header = ['#target name', 'target accession', 'tlen', 'query name', 'query accession', 'qlen', 'full E-value', 'full score', 'full bias', 'domain #', '# domains', 'domain c-Evalue', 'domain i-Evalue', 'domain score', 'domain bias', 'hmm from', 'hmm to', 'seq from', 'seq to', 'env from', 'env to', 'acc', 'target description'] yield header prev, hits = None, [] for line in domtblout: if line.startswith('#'): continue # parse line and get description line = line.strip().split() desc = ' '.join(line[18:]) line = line[0:18] line.append(desc) # create ID based on query name and domain number ID = line[0] + line[9] # domain c-Evalue and domain score thresholds line[11], line[13] = float(line[11]), float(line[13]) evalue, bitscore = line[11], line[13] line[11], line[13] = evalue, bitscore if ID != prev: if len(hits) > 0: for hit in top_hits(hits, numHits, 13, True): yield hit hits = [] if evalueT == False and bitT == False: hits.append(line) elif evalue <= evalueT and bitT == False: hits.append(line) elif evalue <= evalueT and bit >= bitT: hits.append(line) elif evalueT == False and bit >= bitT: hits.append(line) prev = ID for hit in top_hits(hits, numHits, 13, True): yield hit
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:compare_clades; 3, parameters; 3, 4; 4, identifier:pw; 5, block; 5, 6; 5, 8; 5, 22; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:names; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:set; 16, argument_list; 16, 17; 17, list_comprehension; 17, 18; 17, 19; 18, identifier:i; 19, for_in_clause; 19, 20; 19, 21; 20, identifier:i; 21, identifier:pw; 22, for_statement; 22, 23; 22, 24; 22, 29; 23, identifier:i; 24, call; 24, 25; 24, 26; 25, identifier:range; 26, argument_list; 26, 27; 26, 28; 27, integer:0; 28, integer:4; 29, block; 29, 30; 29, 38; 29, 220; 29, 225; 29, 262; 29, 263; 29, 267; 29, 272; 29, 335; 29, 340; 29, 341; 29, 345; 29, 350; 29, 416; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 35; 32, pattern_list; 32, 33; 32, 34; 33, identifier:wi; 34, identifier:bt; 35, expression_list; 35, 36; 35, 37; 36, dictionary; 37, dictionary; 38, for_statement; 38, 39; 38, 40; 38, 41; 39, identifier:a; 40, identifier:names; 41, block; 41, 42; 42, for_statement; 42, 43; 42, 44; 42, 47; 43, identifier:b; 44, subscript; 44, 45; 44, 46; 45, identifier:pw; 46, identifier:a; 47, block; 47, 48; 47, 58; 47, 66; 47, 88; 47, 156; 48, if_statement; 48, 49; 48, 56; 49, boolean_operator:or; 49, 50; 49, 53; 50, comparison_operator:not; 50, 51; 50, 52; 51, string:';'; 52, identifier:a; 53, comparison_operator:not; 53, 54; 53, 55; 54, string:';'; 55, identifier:b; 56, block; 56, 57; 57, continue_statement; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:pident; 61, subscript; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:pw; 64, identifier:a; 65, identifier:b; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 71; 68, pattern_list; 68, 69; 68, 70; 69, identifier:cA; 70, identifier:cB; 71, expression_list; 71, 72; 71, 80; 72, subscript; 72, 73; 72, 79; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:a; 76, identifier:split; 77, argument_list; 77, 78; 78, string:';'; 79, identifier:i; 80, subscript; 80, 81; 80, 87; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:b; 84, identifier:split; 85, argument_list; 85, 86; 86, string:';'; 87, identifier:i; 88, if_statement; 88, 89; 88, 100; 88, 125; 89, boolean_operator:and; 89, 90; 89, 97; 90, boolean_operator:and; 90, 91; 90, 94; 91, comparison_operator:==; 91, 92; 91, 93; 92, identifier:i; 93, integer:0; 94, comparison_operator:in; 94, 95; 94, 96; 95, string:'_'; 96, identifier:cA; 97, comparison_operator:in; 97, 98; 97, 99; 98, string:'_'; 99, identifier:cB; 100, block; 100, 101; 100, 113; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:cA; 104, subscript; 104, 105; 104, 112; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:cA; 108, identifier:rsplit; 109, argument_list; 109, 110; 109, 111; 110, string:'_'; 111, integer:1; 112, integer:1; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:cB; 116, subscript; 116, 117; 116, 124; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:cB; 120, identifier:rsplit; 121, argument_list; 121, 122; 121, 123; 122, string:'_'; 123, integer:1; 124, integer:1; 125, elif_clause; 125, 126; 125, 133; 126, boolean_operator:or; 126, 127; 126, 130; 127, comparison_operator:in; 127, 128; 127, 129; 128, string:'>'; 129, identifier:cA; 130, comparison_operator:in; 130, 131; 130, 132; 131, string:'>'; 132, identifier:cB; 133, block; 133, 134; 133, 145; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:cA; 137, subscript; 137, 138; 137, 144; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:cA; 141, identifier:split; 142, argument_list; 142, 143; 143, string:'>'; 144, integer:1; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:cB; 148, subscript; 148, 149; 148, 155; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:cB; 152, identifier:split; 153, argument_list; 153, 154; 154, string:'>'; 155, integer:1; 156, if_statement; 156, 157; 156, 160; 156, 181; 157, comparison_operator:==; 157, 158; 157, 159; 158, identifier:cA; 159, identifier:cB; 160, block; 160, 161; 160, 172; 161, if_statement; 161, 162; 161, 165; 162, comparison_operator:not; 162, 163; 162, 164; 163, identifier:cA; 164, identifier:wi; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 171; 168, subscript; 168, 169; 168, 170; 169, identifier:wi; 170, identifier:cA; 171, list:[]; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:wi; 177, identifier:cA; 178, identifier:append; 179, argument_list; 179, 180; 180, identifier:pident; 181, else_clause; 181, 182; 182, block; 182, 183; 182, 194; 182, 209; 183, if_statement; 183, 184; 183, 187; 184, comparison_operator:not; 184, 185; 184, 186; 185, identifier:cA; 186, identifier:bt; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:bt; 192, identifier:cA; 193, dictionary; 194, if_statement; 194, 195; 194, 200; 195, comparison_operator:not; 195, 196; 195, 197; 196, identifier:cB; 197, subscript; 197, 198; 197, 199; 198, identifier:bt; 199, identifier:cA; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 208; 203, subscript; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:bt; 206, identifier:cA; 207, identifier:cB; 208, list:[]; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 218; 211, attribute; 211, 212; 211, 217; 212, subscript; 212, 213; 212, 216; 213, subscript; 213, 214; 213, 215; 214, identifier:bt; 215, identifier:cA; 216, identifier:cB; 217, identifier:append; 218, argument_list; 218, 219; 219, identifier:pident; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 223; 222, identifier:print; 223, argument_list; 223, 224; 224, string:'\n# min. within'; 225, for_statement; 225, 226; 225, 229; 225, 237; 226, pattern_list; 226, 227; 226, 228; 227, identifier:clade; 228, identifier:pidents; 229, call; 229, 230; 229, 231; 230, identifier:list; 231, argument_list; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:wi; 235, identifier:items; 236, argument_list; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 241; 240, identifier:print; 241, argument_list; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, string:'\t'; 245, identifier:join; 246, argument_list; 246, 247; 247, list:['wi:%s' % str(i), clade, str(min(pidents))]; 247, 248; 247, 254; 247, 255; 248, binary_operator:%; 248, 249; 248, 250; 249, string:'wi:%s'; 250, call; 250, 251; 250, 252; 251, identifier:str; 252, argument_list; 252, 253; 253, identifier:i; 254, identifier:clade; 255, call; 255, 256; 255, 257; 256, identifier:str; 257, argument_list; 257, 258; 258, call; 258, 259; 258, 260; 259, identifier:min; 260, argument_list; 260, 261; 261, identifier:pidents; 262, comment; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:comps; 266, list:[]; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 270; 269, identifier:print; 270, argument_list; 270, 271; 271, string:'\n# max. between'; 272, for_statement; 272, 273; 272, 274; 272, 278; 273, identifier:comp; 274, call; 274, 275; 274, 276; 275, identifier:print_pairwise; 276, argument_list; 276, 277; 277, identifier:bt; 278, block; 278, 279; 279, if_statement; 279, 280; 279, 283; 280, comparison_operator:is; 280, 281; 280, 282; 281, identifier:comp; 282, None; 283, block; 283, 284; 283, 309; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 287; 286, identifier:print; 287, argument_list; 287, 288; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, string:'\t'; 291, identifier:join; 292, argument_list; 292, 293; 293, binary_operator:+; 293, 294; 293, 301; 294, list:['bt:%s' % str(i)]; 294, 295; 295, binary_operator:%; 295, 296; 295, 297; 296, string:'bt:%s'; 297, call; 297, 298; 297, 299; 298, identifier:str; 299, argument_list; 299, 300; 300, identifier:i; 301, list_comprehension; 301, 302; 301, 306; 302, call; 302, 303; 302, 304; 303, identifier:str; 304, argument_list; 304, 305; 305, identifier:j; 306, for_in_clause; 306, 307; 306, 308; 307, identifier:j; 308, identifier:comp; 309, if_statement; 309, 310; 309, 315; 310, comparison_operator:!=; 310, 311; 310, 314; 311, subscript; 311, 312; 311, 313; 312, identifier:comp; 313, integer:0; 314, string:'#'; 315, block; 315, 316; 316, expression_statement; 316, 317; 317, call; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:comps; 320, identifier:extend; 321, argument_list; 321, 322; 322, list_comprehension; 322, 323; 322, 324; 322, 331; 323, identifier:j; 324, for_in_clause; 324, 325; 324, 326; 325, identifier:j; 326, subscript; 326, 327; 326, 328; 327, identifier:comp; 328, slice; 328, 329; 328, 330; 329, integer:1; 330, colon; 331, if_clause; 331, 332; 332, comparison_operator:!=; 332, 333; 332, 334; 333, identifier:j; 334, string:'-'; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 338; 337, identifier:print_comps; 338, argument_list; 338, 339; 339, identifier:comps; 340, comment; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 344; 343, identifier:comps; 344, list:[]; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 348; 347, identifier:print; 348, argument_list; 348, 349; 349, string:'\n# median between'; 350, for_statement; 350, 351; 350, 352; 350, 359; 351, identifier:comp; 352, call; 352, 353; 352, 354; 353, identifier:print_pairwise; 354, argument_list; 354, 355; 354, 356; 355, identifier:bt; 356, keyword_argument; 356, 357; 356, 358; 357, identifier:median; 358, True; 359, block; 359, 360; 360, if_statement; 360, 361; 360, 364; 361, comparison_operator:is; 361, 362; 361, 363; 362, identifier:comp; 363, None; 364, block; 364, 365; 364, 390; 365, expression_statement; 365, 366; 366, call; 366, 367; 366, 368; 367, identifier:print; 368, argument_list; 368, 369; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, string:'\t'; 372, identifier:join; 373, argument_list; 373, 374; 374, binary_operator:+; 374, 375; 374, 382; 375, list:['bt:%s' % str(i)]; 375, 376; 376, binary_operator:%; 376, 377; 376, 378; 377, string:'bt:%s'; 378, call; 378, 379; 378, 380; 379, identifier:str; 380, argument_list; 380, 381; 381, identifier:i; 382, list_comprehension; 382, 383; 382, 387; 383, call; 383, 384; 383, 385; 384, identifier:str; 385, argument_list; 385, 386; 386, identifier:j; 387, for_in_clause; 387, 388; 387, 389; 388, identifier:j; 389, identifier:comp; 390, if_statement; 390, 391; 390, 396; 391, comparison_operator:!=; 391, 392; 391, 395; 392, subscript; 392, 393; 392, 394; 393, identifier:comp; 394, integer:0; 395, string:'#'; 396, block; 396, 397; 397, expression_statement; 397, 398; 398, call; 398, 399; 398, 402; 399, attribute; 399, 400; 399, 401; 400, identifier:comps; 401, identifier:extend; 402, argument_list; 402, 403; 403, list_comprehension; 403, 404; 403, 405; 403, 412; 404, identifier:j; 405, for_in_clause; 405, 406; 405, 407; 406, identifier:j; 407, subscript; 407, 408; 407, 409; 408, identifier:comp; 409, slice; 409, 410; 409, 411; 410, integer:1; 411, colon; 412, if_clause; 412, 413; 413, comparison_operator:!=; 413, 414; 413, 415; 414, identifier:j; 415, string:'-'; 416, expression_statement; 416, 417; 417, call; 417, 418; 417, 419; 418, identifier:print_comps; 419, argument_list; 419, 420; 420, identifier:comps
def compare_clades(pw): """ print min. pident within each clade and then matrix of between-clade max. """ names = sorted(set([i for i in pw])) for i in range(0, 4): wi, bt = {}, {} for a in names: for b in pw[a]: if ';' not in a or ';' not in b: continue pident = pw[a][b] cA, cB = a.split(';')[i], b.split(';')[i] if i == 0 and '_' in cA and '_' in cB: cA = cA.rsplit('_', 1)[1] cB = cB.rsplit('_', 1)[1] elif '>' in cA or '>' in cB: cA = cA.split('>')[1] cB = cB.split('>')[1] if cA == cB: if cA not in wi: wi[cA] = [] wi[cA].append(pident) else: if cA not in bt: bt[cA] = {} if cB not in bt[cA]: bt[cA][cB] = [] bt[cA][cB].append(pident) print('\n# min. within') for clade, pidents in list(wi.items()): print('\t'.join(['wi:%s' % str(i), clade, str(min(pidents))])) # print matrix of maximum between groups comps = [] print('\n# max. between') for comp in print_pairwise(bt): if comp is not None: print('\t'.join(['bt:%s' % str(i)] + [str(j) for j in comp])) if comp[0] != '#': comps.extend([j for j in comp[1:] if j != '-']) print_comps(comps) # print matrix of median between groups comps = [] print('\n# median between') for comp in print_pairwise(bt, median = True): if comp is not None: print('\t'.join(['bt:%s' % str(i)] + [str(j) for j in comp])) if comp[0] != '#': comps.extend([j for j in comp[1:] if j != '-']) print_comps(comps)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:searchAccession; 3, parameters; 3, 4; 4, identifier:acc; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 20; 5, 95; 5, 96; 5, 106; 5, 181; 5, 182; 5, 192; 5, 267; 5, 298; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 15; 12, pattern_list; 12, 13; 12, 14; 13, identifier:out; 14, identifier:error; 15, call; 15, 16; 15, 17; 16, identifier:entrez; 17, argument_list; 17, 18; 17, 19; 18, string:'genome'; 19, identifier:acc; 20, for_statement; 20, 21; 20, 22; 20, 27; 21, identifier:line; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:out; 25, identifier:splitlines; 26, argument_list; 27, block; 27, 28; 27, 41; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:line; 31, call; 31, 32; 31, 40; 32, attribute; 32, 33; 32, 39; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:line; 36, identifier:decode; 37, argument_list; 37, 38; 38, string:'ascii'; 39, identifier:strip; 40, argument_list; 41, if_statement; 41, 42; 41, 49; 42, boolean_operator:or; 42, 43; 42, 46; 43, comparison_operator:in; 43, 44; 43, 45; 44, string:'Assembly_Accession'; 45, identifier:line; 46, comparison_operator:in; 46, 47; 46, 48; 47, string:'BioSample'; 48, identifier:line; 49, block; 49, 50; 49, 82; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:newAcc; 53, subscript; 53, 54; 53, 81; 54, call; 54, 55; 54, 79; 55, attribute; 55, 56; 55, 78; 56, subscript; 56, 57; 56, 77; 57, call; 57, 58; 57, 75; 58, attribute; 58, 59; 58, 74; 59, subscript; 59, 60; 59, 73; 60, call; 60, 61; 60, 71; 61, attribute; 61, 62; 61, 70; 62, subscript; 62, 63; 62, 69; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:line; 66, identifier:split; 67, argument_list; 67, 68; 68, string:'>'; 69, integer:1; 70, identifier:split; 71, argument_list; 71, 72; 72, string:'<'; 73, integer:0; 74, identifier:split; 75, argument_list; 75, 76; 76, string:'.'; 77, integer:0; 78, identifier:split; 79, argument_list; 79, 80; 80, string:','; 81, integer:0; 82, if_statement; 82, 83; 82, 89; 83, comparison_operator:>; 83, 84; 83, 88; 84, call; 84, 85; 84, 86; 85, identifier:len; 86, argument_list; 86, 87; 87, identifier:newAcc; 88, integer:0; 89, block; 89, 90; 90, return_statement; 90, 91; 91, tuple; 91, 92; 91, 93; 91, 94; 92, True; 93, identifier:acc; 94, identifier:newAcc; 95, comment; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, pattern_list; 98, 99; 98, 100; 99, identifier:out; 100, identifier:error; 101, call; 101, 102; 101, 103; 102, identifier:entrez; 103, argument_list; 103, 104; 103, 105; 104, string:'nucleotide'; 105, identifier:acc; 106, for_statement; 106, 107; 106, 108; 106, 113; 107, identifier:line; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:out; 111, identifier:splitlines; 112, argument_list; 113, block; 113, 114; 113, 127; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:line; 117, call; 117, 118; 117, 126; 118, attribute; 118, 119; 118, 125; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:line; 122, identifier:decode; 123, argument_list; 123, 124; 124, string:'ascii'; 125, identifier:strip; 126, argument_list; 127, if_statement; 127, 128; 127, 135; 128, boolean_operator:or; 128, 129; 128, 132; 129, comparison_operator:in; 129, 130; 129, 131; 130, string:'Assembly_Accession'; 131, identifier:line; 132, comparison_operator:in; 132, 133; 132, 134; 133, string:'BioSample'; 134, identifier:line; 135, block; 135, 136; 135, 168; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:newAcc; 139, subscript; 139, 140; 139, 167; 140, call; 140, 141; 140, 165; 141, attribute; 141, 142; 141, 164; 142, subscript; 142, 143; 142, 163; 143, call; 143, 144; 143, 161; 144, attribute; 144, 145; 144, 160; 145, subscript; 145, 146; 145, 159; 146, call; 146, 147; 146, 157; 147, attribute; 147, 148; 147, 156; 148, subscript; 148, 149; 148, 155; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:line; 152, identifier:split; 153, argument_list; 153, 154; 154, string:'>'; 155, integer:1; 156, identifier:split; 157, argument_list; 157, 158; 158, string:'<'; 159, integer:0; 160, identifier:split; 161, argument_list; 161, 162; 162, string:'.'; 163, integer:0; 164, identifier:split; 165, argument_list; 165, 166; 166, string:','; 167, integer:0; 168, if_statement; 168, 169; 168, 175; 169, comparison_operator:>; 169, 170; 169, 174; 170, call; 170, 171; 170, 172; 171, identifier:len; 172, argument_list; 172, 173; 173, identifier:newAcc; 174, integer:0; 175, block; 175, 176; 176, return_statement; 176, 177; 177, tuple; 177, 178; 177, 179; 177, 180; 178, True; 179, identifier:acc; 180, identifier:newAcc; 181, comment; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 187; 184, pattern_list; 184, 185; 184, 186; 185, identifier:out; 186, identifier:error; 187, call; 187, 188; 187, 189; 188, identifier:entrez; 189, argument_list; 189, 190; 189, 191; 190, string:'assembly'; 191, identifier:acc; 192, for_statement; 192, 193; 192, 194; 192, 199; 193, identifier:line; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:out; 197, identifier:splitlines; 198, argument_list; 199, block; 199, 200; 199, 213; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:line; 203, call; 203, 204; 203, 212; 204, attribute; 204, 205; 204, 211; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:line; 208, identifier:decode; 209, argument_list; 209, 210; 210, string:'ascii'; 211, identifier:strip; 212, argument_list; 213, if_statement; 213, 214; 213, 221; 214, boolean_operator:or; 214, 215; 214, 218; 215, comparison_operator:in; 215, 216; 215, 217; 216, string:'Assembly_Accession'; 217, identifier:line; 218, comparison_operator:in; 218, 219; 218, 220; 219, string:'BioSample'; 220, identifier:line; 221, block; 221, 222; 221, 254; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:newAcc; 225, subscript; 225, 226; 225, 253; 226, call; 226, 227; 226, 251; 227, attribute; 227, 228; 227, 250; 228, subscript; 228, 229; 228, 249; 229, call; 229, 230; 229, 247; 230, attribute; 230, 231; 230, 246; 231, subscript; 231, 232; 231, 245; 232, call; 232, 233; 232, 243; 233, attribute; 233, 234; 233, 242; 234, subscript; 234, 235; 234, 241; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:line; 238, identifier:split; 239, argument_list; 239, 240; 240, string:'>'; 241, integer:1; 242, identifier:split; 243, argument_list; 243, 244; 244, string:'<'; 245, integer:0; 246, identifier:split; 247, argument_list; 247, 248; 248, string:'.'; 249, integer:0; 250, identifier:split; 251, argument_list; 251, 252; 252, string:','; 253, integer:0; 254, if_statement; 254, 255; 254, 261; 255, comparison_operator:>; 255, 256; 255, 260; 256, call; 256, 257; 256, 258; 257, identifier:len; 258, argument_list; 258, 259; 259, identifier:newAcc; 260, integer:0; 261, block; 261, 262; 262, return_statement; 262, 263; 263, tuple; 263, 264; 263, 265; 263, 266; 264, True; 265, identifier:acc; 266, identifier:newAcc; 267, for_statement; 267, 268; 267, 269; 267, 274; 268, identifier:error; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:error; 272, identifier:splitlines; 273, argument_list; 274, block; 274, 275; 274, 288; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:error; 278, call; 278, 279; 278, 287; 279, attribute; 279, 280; 279, 286; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:error; 283, identifier:decode; 284, argument_list; 284, 285; 285, string:'ascii'; 286, identifier:strip; 287, argument_list; 288, if_statement; 288, 289; 288, 292; 289, comparison_operator:in; 289, 290; 289, 291; 290, string:'500 Can'; 291, identifier:error; 292, block; 292, 293; 293, return_statement; 293, 294; 294, tuple; 294, 295; 294, 296; 294, 297; 295, False; 296, identifier:acc; 297, string:'no network'; 298, return_statement; 298, 299; 299, tuple; 299, 300; 299, 301; 299, 302; 300, False; 301, identifier:acc; 302, string:'efetch failed'
def searchAccession(acc): """ attempt to use NCBI Entrez to get BioSample ID """ # try genbank file # genome database out, error = entrez('genome', acc) for line in out.splitlines(): line = line.decode('ascii').strip() if 'Assembly_Accession' in line or 'BioSample' in line: newAcc = line.split('>')[1].split('<')[0].split('.')[0].split(',')[0] if len(newAcc) > 0: return (True, acc, newAcc) # nucleotide database out, error = entrez('nucleotide', acc) for line in out.splitlines(): line = line.decode('ascii').strip() if 'Assembly_Accession' in line or 'BioSample' in line: newAcc = line.split('>')[1].split('<')[0].split('.')[0].split(',')[0] if len(newAcc) > 0: return (True, acc, newAcc) # assembly database out, error = entrez('assembly', acc) for line in out.splitlines(): line = line.decode('ascii').strip() if 'Assembly_Accession' in line or 'BioSample' in line: newAcc = line.split('>')[1].split('<')[0].split('.')[0].split(',')[0] if len(newAcc) > 0: return (True, acc, newAcc) for error in error.splitlines(): error = error.decode('ascii').strip() if '500 Can' in error: return (False, acc, 'no network') return (False, acc, 'efetch failed')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:_configure_logger; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:fmt; 5, identifier:quiet; 6, identifier:level; 7, identifier:fpath; 8, identifier:pre_hooks; 9, identifier:post_hooks; 10, identifier:metric_grouping_interval; 11, block; 11, 12; 11, 14; 11, 15; 11, 27; 11, 29; 11, 33; 11, 34; 11, 61; 11, 67; 11, 81; 11, 91; 11, 100; 11, 106; 11, 129; 11, 145; 11, 161; 11, 168; 11, 182; 11, 186; 11, 187; 11, 199; 11, 217; 11, 225; 11, 243; 11, 252; 11, 253; 11, 280; 11, 281; 11, 289; 11, 298; 11, 305; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:level; 18, call; 18, 19; 18, 20; 19, identifier:getattr; 20, argument_list; 20, 21; 20, 22; 21, identifier:logging; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:level; 25, identifier:upper; 26, argument_list; 27, global_statement; 27, 28; 28, identifier:_GLOBAL_LOG_CONFIGURED; 29, if_statement; 29, 30; 29, 31; 30, identifier:_GLOBAL_LOG_CONFIGURED; 31, block; 31, 32; 32, return_statement; 33, comment; 34, function_definition; 34, 35; 34, 36; 34, 38; 35, function_name:wrap_hook; 36, parameters; 36, 37; 37, identifier:fn; 38, block; 38, 39; 38, 59; 39, decorated_definition; 39, 40; 39, 45; 40, decorator; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:wraps; 43, argument_list; 43, 44; 44, identifier:fn; 45, function_definition; 45, 46; 45, 47; 45, 51; 46, function_name:processor; 47, parameters; 47, 48; 47, 49; 47, 50; 48, identifier:logger; 49, identifier:method_name; 50, identifier:event_dict; 51, block; 51, 52; 51, 57; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:fn; 55, argument_list; 55, 56; 56, identifier:event_dict; 57, return_statement; 57, 58; 58, identifier:event_dict; 59, return_statement; 59, 60; 60, identifier:processor; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:processors; 64, call; 64, 65; 64, 66; 65, identifier:define_log_processors; 66, argument_list; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:processors; 71, identifier:extend; 72, argument_list; 72, 73; 73, list_comprehension; 73, 74; 73, 78; 74, call; 74, 75; 74, 76; 75, identifier:wrap_hook; 76, argument_list; 76, 77; 77, identifier:h; 78, for_in_clause; 78, 79; 78, 80; 79, identifier:h; 80, identifier:pre_hooks; 81, if_statement; 81, 82; 81, 83; 82, identifier:metric_grouping_interval; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:processors; 88, identifier:append; 89, argument_list; 89, 90; 90, identifier:metrics_grouping_processor; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:log_renderer; 94, call; 94, 95; 94, 96; 95, identifier:define_log_renderer; 96, argument_list; 96, 97; 96, 98; 96, 99; 97, identifier:fmt; 98, identifier:fpath; 99, identifier:quiet; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:stderr_required; 103, parenthesized_expression; 103, 104; 104, not_operator; 104, 105; 105, identifier:quiet; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:pretty_to_stderr; 109, parenthesized_expression; 109, 110; 110, boolean_operator:and; 110, 111; 110, 112; 111, identifier:stderr_required; 112, parenthesized_expression; 112, 113; 113, boolean_operator:or; 113, 114; 113, 117; 114, comparison_operator:==; 114, 115; 114, 116; 115, identifier:fmt; 116, string:"pretty"; 117, parenthesized_expression; 117, 118; 118, boolean_operator:and; 118, 119; 118, 122; 119, comparison_operator:is; 119, 120; 119, 121; 120, identifier:fmt; 121, None; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:sys; 126, identifier:stderr; 127, identifier:isatty; 128, argument_list; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:should_inject_pretty_renderer; 132, parenthesized_expression; 132, 133; 133, boolean_operator:and; 133, 134; 133, 135; 134, identifier:pretty_to_stderr; 135, not_operator; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:isinstance; 138, argument_list; 138, 139; 138, 140; 139, identifier:log_renderer; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:structlog; 143, identifier:dev; 144, identifier:ConsoleRenderer; 145, if_statement; 145, 146; 145, 147; 146, identifier:should_inject_pretty_renderer; 147, block; 147, 148; 147, 152; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:stderr_required; 151, False; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:processors; 156, identifier:append; 157, argument_list; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:StderrConsoleRenderer; 160, argument_list; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:processors; 165, identifier:append; 166, argument_list; 166, 167; 167, identifier:log_renderer; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:processors; 172, identifier:extend; 173, argument_list; 173, 174; 174, list_comprehension; 174, 175; 174, 179; 175, call; 175, 176; 175, 177; 176, identifier:wrap_hook; 177, argument_list; 177, 178; 178, identifier:h; 179, for_in_clause; 179, 180; 179, 181; 180, identifier:h; 181, identifier:post_hooks; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:streams; 185, list:[]; 186, comment; 187, if_statement; 187, 188; 187, 189; 188, identifier:stderr_required; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:streams; 194, identifier:append; 195, argument_list; 195, 196; 196, attribute; 196, 197; 196, 198; 197, identifier:sys; 198, identifier:stderr; 199, if_statement; 199, 200; 199, 203; 199, 204; 199, 205; 200, comparison_operator:is; 200, 201; 200, 202; 201, identifier:fpath; 202, None; 203, comment; 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:streams; 210, identifier:append; 211, argument_list; 211, 212; 212, call; 212, 213; 212, 214; 213, identifier:open; 214, argument_list; 214, 215; 214, 216; 215, identifier:fpath; 216, string:'a'; 217, assert_statement; 217, 218; 217, 224; 218, comparison_operator:!=; 218, 219; 218, 223; 219, call; 219, 220; 219, 221; 220, identifier:len; 221, argument_list; 221, 222; 222, identifier:streams; 223, integer:0; 224, string:"cannot configure logger for 0 streams"; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:stream; 228, conditional_expression:if; 228, 229; 228, 232; 228, 238; 229, subscript; 229, 230; 229, 231; 230, identifier:streams; 231, integer:0; 232, comparison_operator:==; 232, 233; 232, 237; 233, call; 233, 234; 233, 235; 234, identifier:len; 235, argument_list; 235, 236; 236, identifier:streams; 237, integer:1; 238, call; 238, 239; 238, 240; 239, identifier:Stream; 240, argument_list; 240, 241; 241, list_splat; 241, 242; 242, identifier:streams; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:atexit; 247, identifier:register; 248, argument_list; 248, 249; 249, attribute; 249, 250; 249, 251; 250, identifier:stream; 251, identifier:close; 252, comment; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:structlog; 257, identifier:configure; 258, argument_list; 258, 259; 258, 262; 258, 265; 258, 274; 258, 277; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:processors; 261, identifier:processors; 262, keyword_argument; 262, 263; 262, 264; 263, identifier:context_class; 264, identifier:dict; 265, keyword_argument; 265, 266; 265, 267; 266, identifier:logger_factory; 267, call; 267, 268; 267, 269; 268, identifier:LevelLoggerFactory; 269, argument_list; 269, 270; 269, 271; 270, identifier:stream; 271, keyword_argument; 271, 272; 271, 273; 272, identifier:level; 273, identifier:level; 274, keyword_argument; 274, 275; 274, 276; 275, identifier:wrapper_class; 276, identifier:BoundLevelLogger; 277, keyword_argument; 277, 278; 277, 279; 278, identifier:cache_logger_on_first_use; 279, True; 280, comment; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:stdlib_root_log; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:logging; 287, identifier:getLogger; 288, argument_list; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:stdlib_root_log; 293, identifier:addHandler; 294, argument_list; 294, 295; 295, call; 295, 296; 295, 297; 296, identifier:StdlibStructlogHandler; 297, argument_list; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:stdlib_root_log; 302, identifier:setLevel; 303, argument_list; 303, 304; 304, identifier:level; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:_GLOBAL_LOG_CONFIGURED; 308, True
def _configure_logger(fmt, quiet, level, fpath, pre_hooks, post_hooks, metric_grouping_interval): """ configures a logger when required write to stderr or a file """ # NOTE not thread safe. Multiple BaseScripts cannot be instantiated concurrently. level = getattr(logging, level.upper()) global _GLOBAL_LOG_CONFIGURED if _GLOBAL_LOG_CONFIGURED: return # since the hooks need to run through structlog, need to wrap them like processors def wrap_hook(fn): @wraps(fn) def processor(logger, method_name, event_dict): fn(event_dict) return event_dict return processor processors = define_log_processors() processors.extend( [ wrap_hook(h) for h in pre_hooks ] ) if metric_grouping_interval: processors.append(metrics_grouping_processor) log_renderer = define_log_renderer(fmt, fpath, quiet) stderr_required = (not quiet) pretty_to_stderr = ( stderr_required and ( fmt == "pretty" or (fmt is None and sys.stderr.isatty()) ) ) should_inject_pretty_renderer = ( pretty_to_stderr and not isinstance(log_renderer, structlog.dev.ConsoleRenderer) ) if should_inject_pretty_renderer: stderr_required = False processors.append(StderrConsoleRenderer()) processors.append(log_renderer) processors.extend( [ wrap_hook(h) for h in post_hooks ] ) streams = [] # we need to use a stream if we are writing to both file and stderr, and both are json if stderr_required: streams.append(sys.stderr) if fpath is not None: # TODO handle creating a directory for this log file ? # TODO set mode and encoding appropriately streams.append(open(fpath, 'a')) assert len(streams) != 0, "cannot configure logger for 0 streams" stream = streams[0] if len(streams) == 1 else Stream(*streams) atexit.register(stream.close) # a global level struct log config unless otherwise specified. structlog.configure( processors=processors, context_class=dict, logger_factory=LevelLoggerFactory(stream, level=level), wrapper_class=BoundLevelLogger, cache_logger_on_first_use=True, ) # TODO take care of removing other handlers stdlib_root_log = logging.getLogger() stdlib_root_log.addHandler(StdlibStructlogHandler()) stdlib_root_log.setLevel(level) _GLOBAL_LOG_CONFIGURED = True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:combine_modifiers; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:graphemes; 6, block; 6, 7; 6, 9; 6, 13; 6, 17; 6, 24; 6, 210; 6, 211; 6, 221; 6, 225; 6, 229; 6, 287; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:result; 12, list:[]; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:temp; 16, string:""; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:count; 20, call; 20, 21; 20, 22; 21, identifier:len; 22, argument_list; 22, 23; 23, identifier:graphemes; 24, for_statement; 24, 25; 24, 26; 24, 30; 25, identifier:grapheme; 26, call; 26, 27; 26, 28; 27, identifier:reversed; 28, argument_list; 28, 29; 29, identifier:graphemes; 30, block; 30, 31; 30, 35; 30, 90; 30, 91; 30, 125; 30, 126; 30, 197; 30, 206; 31, expression_statement; 31, 32; 32, augmented_assignment:-=; 32, 33; 32, 34; 33, identifier:count; 34, integer:1; 35, if_statement; 35, 36; 35, 62; 36, boolean_operator:and; 36, 37; 36, 52; 36, 53; 37, boolean_operator:and; 37, 38; 37, 44; 38, comparison_operator:==; 38, 39; 38, 43; 39, call; 39, 40; 39, 41; 40, identifier:len; 41, argument_list; 41, 42; 42, identifier:grapheme; 43, integer:1; 44, comparison_operator:==; 44, 45; 44, 51; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:unicodedata; 48, identifier:category; 49, argument_list; 49, 50; 50, identifier:grapheme; 51, string:"Lm"; 52, line_continuation:\; 53, not_operator; 53, 54; 54, comparison_operator:in; 54, 55; 54, 59; 55, call; 55, 56; 55, 57; 56, identifier:ord; 57, argument_list; 57, 58; 58, identifier:grapheme; 59, list:[712, 716]; 59, 60; 59, 61; 60, integer:712; 61, integer:716; 62, block; 62, 63; 62, 69; 62, 70; 62, 71; 62, 88; 62, 89; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:temp; 66, binary_operator:+; 66, 67; 66, 68; 67, identifier:grapheme; 68, identifier:temp; 69, comment; 70, comment; 71, if_statement; 71, 72; 71, 75; 72, comparison_operator:==; 72, 73; 72, 74; 73, identifier:count; 74, integer:0; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 82; 78, subscript; 78, 79; 78, 80; 79, identifier:result; 80, unary_operator:-; 80, 81; 81, integer:1; 82, binary_operator:+; 82, 83; 82, 84; 83, identifier:temp; 84, subscript; 84, 85; 84, 86; 85, identifier:result; 86, unary_operator:-; 86, 87; 87, integer:1; 88, continue_statement; 89, comment; 90, comment; 91, if_statement; 91, 92; 91, 107; 92, boolean_operator:and; 92, 93; 92, 99; 93, comparison_operator:==; 93, 94; 93, 98; 94, call; 94, 95; 94, 96; 95, identifier:len; 96, argument_list; 96, 97; 97, identifier:grapheme; 98, integer:1; 99, comparison_operator:in; 99, 100; 99, 104; 100, call; 100, 101; 100, 102; 101, identifier:ord; 102, argument_list; 102, 103; 103, identifier:grapheme; 104, list:[712, 716]; 104, 105; 104, 106; 105, integer:712; 106, integer:716; 107, block; 107, 108; 107, 120; 107, 124; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 114; 110, subscript; 110, 111; 110, 112; 111, identifier:result; 112, unary_operator:-; 112, 113; 113, integer:1; 114, binary_operator:+; 114, 115; 114, 116; 115, identifier:grapheme; 116, subscript; 116, 117; 116, 118; 117, identifier:result; 118, unary_operator:-; 118, 119; 119, integer:1; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:temp; 123, string:""; 124, continue_statement; 125, comment; 126, if_statement; 126, 127; 126, 142; 127, boolean_operator:and; 127, 128; 127, 134; 128, comparison_operator:==; 128, 129; 128, 133; 129, call; 129, 130; 129, 131; 130, identifier:len; 131, argument_list; 131, 132; 132, identifier:grapheme; 133, integer:1; 134, comparison_operator:==; 134, 135; 134, 141; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:unicodedata; 138, identifier:category; 139, argument_list; 139, 140; 140, identifier:grapheme; 141, string:"Sk"; 142, block; 142, 143; 143, if_statement; 143, 144; 143, 150; 143, 163; 144, comparison_operator:==; 144, 145; 144, 149; 145, call; 145, 146; 145, 147; 146, identifier:len; 147, argument_list; 147, 148; 148, identifier:result; 149, integer:0; 150, block; 150, 151; 150, 158; 150, 162; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:result; 155, identifier:append; 156, argument_list; 156, 157; 157, identifier:grapheme; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:temp; 161, string:""; 162, continue_statement; 163, else_clause; 163, 164; 164, block; 164, 165; 165, if_statement; 165, 166; 165, 179; 166, comparison_operator:==; 166, 167; 166, 178; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:unicodedata; 170, identifier:category; 171, argument_list; 171, 172; 172, subscript; 172, 173; 172, 177; 173, subscript; 173, 174; 173, 175; 174, identifier:result; 175, unary_operator:-; 175, 176; 176, integer:1; 177, integer:0; 178, string:"Sk"; 179, block; 179, 180; 179, 192; 179, 196; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 186; 182, subscript; 182, 183; 182, 184; 183, identifier:result; 184, unary_operator:-; 184, 185; 185, integer:1; 186, binary_operator:+; 186, 187; 186, 188; 187, identifier:grapheme; 188, subscript; 188, 189; 188, 190; 189, identifier:result; 190, unary_operator:-; 190, 191; 191, integer:1; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:temp; 195, string:""; 196, continue_statement; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:result; 201, identifier:append; 202, argument_list; 202, 203; 203, binary_operator:+; 203, 204; 203, 205; 204, identifier:grapheme; 205, identifier:temp; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:temp; 209, string:""; 210, comment; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:segments; 214, subscript; 214, 215; 214, 216; 215, identifier:result; 216, slice; 216, 217; 216, 218; 216, 219; 217, colon; 218, colon; 219, unary_operator:-; 219, 220; 220, integer:1; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:i; 224, integer:0; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:r; 228, list:[]; 229, while_statement; 229, 230; 229, 236; 229, 237; 230, comparison_operator:<; 230, 231; 230, 232; 231, identifier:i; 232, call; 232, 233; 232, 234; 233, identifier:len; 234, argument_list; 234, 235; 235, identifier:segments; 236, comment; 237, block; 237, 238; 238, if_statement; 238, 239; 238, 252; 238, 272; 239, comparison_operator:in; 239, 240; 239, 249; 240, call; 240, 241; 240, 242; 241, identifier:ord; 242, argument_list; 242, 243; 243, subscript; 243, 244; 243, 247; 244, subscript; 244, 245; 244, 246; 245, identifier:segments; 246, identifier:i; 247, unary_operator:-; 247, 248; 248, integer:1; 249, list:[865, 860]; 249, 250; 249, 251; 250, integer:865; 251, integer:860; 252, block; 252, 253; 252, 268; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:r; 257, identifier:append; 258, argument_list; 258, 259; 259, binary_operator:+; 259, 260; 259, 263; 260, subscript; 260, 261; 260, 262; 261, identifier:segments; 262, identifier:i; 263, subscript; 263, 264; 263, 265; 264, identifier:segments; 265, binary_operator:+; 265, 266; 265, 267; 266, identifier:i; 267, integer:1; 268, expression_statement; 268, 269; 269, augmented_assignment:+=; 269, 270; 269, 271; 270, identifier:i; 271, integer:2; 272, else_clause; 272, 273; 273, block; 273, 274; 273, 283; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:r; 278, identifier:append; 279, argument_list; 279, 280; 280, subscript; 280, 281; 280, 282; 281, identifier:segments; 282, identifier:i; 283, expression_statement; 283, 284; 284, augmented_assignment:+=; 284, 285; 284, 286; 285, identifier:i; 286, integer:1; 287, return_statement; 287, 288; 288, identifier:r
def combine_modifiers(self, graphemes): """ Given a string that is space-delimited on Unicode grapheme clusters, group Unicode modifier letters with their preceding base characters, deal with tie bars, etc. Parameters ---------- string : str A Unicode string tokenized into grapheme clusters to be tokenized into simple IPA. """ result = [] temp = "" count = len(graphemes) for grapheme in reversed(graphemes): count -= 1 if len(grapheme) == 1 and unicodedata.category(grapheme) == "Lm" \ and not ord(grapheme) in [712, 716]: temp = grapheme + temp # hack for the cases where a space modifier is the first character in the # string if count == 0: result[-1] = temp + result[-1] continue # pragma: no cover # catch and repair stress marks if len(grapheme) == 1 and ord(grapheme) in [712, 716]: result[-1] = grapheme + result[-1] temp = "" continue # combine contour tone marks (non-accents) if len(grapheme) == 1 and unicodedata.category(grapheme) == "Sk": if len(result) == 0: result.append(grapheme) temp = "" continue else: if unicodedata.category(result[-1][0]) == "Sk": result[-1] = grapheme + result[-1] temp = "" continue result.append(grapheme + temp) temp = "" # last check for tie bars segments = result[::-1] i = 0 r = [] while i < len(segments): # tie bars if ord(segments[i][-1]) in [865, 860]: r.append(segments[i] + segments[i + 1]) i += 2 else: r.append(segments[i]) i += 1 return r
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:check_mismatches; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:read; 5, identifier:pair; 6, identifier:mismatches; 7, identifier:mm_option; 8, identifier:req_map; 9, block; 9, 10; 9, 12; 9, 13; 9, 48; 9, 49; 9, 56; 9, 63; 9, 64; 9, 75; 9, 76; 9, 83; 9, 84; 9, 100; 9, 101; 9, 127; 9, 128; 9, 180; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:pair; 16, False; 17, block; 17, 18; 17, 25; 17, 32; 17, 33; 17, 40; 17, 41; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:mm; 21, call; 21, 22; 21, 23; 22, identifier:count_mismatches; 23, argument_list; 23, 24; 24, identifier:read; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:is; 26, 27; 26, 28; 27, identifier:mm; 28, False; 29, block; 29, 30; 30, return_statement; 30, 31; 31, False; 32, comment; 33, if_statement; 33, 34; 33, 37; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:mismatches; 36, False; 37, block; 37, 38; 38, return_statement; 38, 39; 39, True; 40, comment; 41, if_statement; 41, 42; 41, 45; 42, comparison_operator:<=; 42, 43; 42, 44; 43, identifier:mm; 44, identifier:mismatches; 45, block; 45, 46; 46, return_statement; 46, 47; 47, True; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:r_mm; 52, call; 52, 53; 52, 54; 53, identifier:count_mismatches; 54, argument_list; 54, 55; 55, identifier:read; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:p_mm; 59, call; 59, 60; 59, 61; 60, identifier:count_mismatches; 61, argument_list; 61, 62; 62, identifier:pair; 63, comment; 64, if_statement; 64, 65; 64, 72; 65, boolean_operator:and; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:r_mm; 68, False; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:p_mm; 71, False; 72, block; 72, 73; 73, return_statement; 73, 74; 74, False; 75, comment; 76, if_statement; 76, 77; 76, 80; 77, comparison_operator:is; 77, 78; 77, 79; 78, identifier:mismatches; 79, False; 80, block; 80, 81; 81, return_statement; 81, 82; 82, True; 83, comment; 84, if_statement; 84, 85; 84, 88; 85, comparison_operator:is; 85, 86; 85, 87; 86, identifier:req_map; 87, True; 88, block; 88, 89; 89, if_statement; 89, 90; 89, 97; 90, boolean_operator:or; 90, 91; 90, 94; 91, comparison_operator:is; 91, 92; 91, 93; 92, identifier:r_mm; 93, False; 94, comparison_operator:is; 94, 95; 94, 96; 95, identifier:p_mm; 96, False; 97, block; 97, 98; 98, return_statement; 98, 99; 99, False; 100, comment; 101, if_statement; 101, 102; 101, 105; 102, comparison_operator:==; 102, 103; 102, 104; 103, identifier:mm_option; 104, string:'one'; 105, block; 105, 106; 106, if_statement; 106, 107; 106, 124; 107, boolean_operator:or; 107, 108; 107, 116; 108, parenthesized_expression; 108, 109; 109, boolean_operator:and; 109, 110; 109, 113; 110, comparison_operator:is; 110, 111; 110, 112; 111, identifier:r_mm; 112, False; 113, comparison_operator:<=; 113, 114; 113, 115; 114, identifier:r_mm; 115, identifier:mismatches; 116, parenthesized_expression; 116, 117; 117, boolean_operator:and; 117, 118; 117, 121; 118, comparison_operator:is; 118, 119; 118, 120; 119, identifier:p_mm; 120, False; 121, comparison_operator:<=; 121, 122; 121, 123; 122, identifier:p_mm; 123, identifier:mismatches; 124, block; 124, 125; 125, return_statement; 125, 126; 126, True; 127, comment; 128, if_statement; 128, 129; 128, 132; 128, 133; 128, 134; 129, comparison_operator:==; 129, 130; 129, 131; 130, identifier:mm_option; 131, string:'both'; 132, comment; 133, comment; 134, block; 134, 135; 135, if_statement; 135, 136; 135, 139; 135, 147; 135, 159; 136, comparison_operator:is; 136, 137; 136, 138; 137, identifier:r_mm; 138, False; 139, block; 139, 140; 140, if_statement; 140, 141; 140, 144; 141, comparison_operator:<=; 141, 142; 141, 143; 142, identifier:p_mm; 143, identifier:mismatches; 144, block; 144, 145; 145, return_statement; 145, 146; 146, True; 147, elif_clause; 147, 148; 147, 151; 148, comparison_operator:is; 148, 149; 148, 150; 149, identifier:p_mm; 150, False; 151, block; 151, 152; 152, if_statement; 152, 153; 152, 156; 153, comparison_operator:<=; 153, 154; 153, 155; 154, identifier:r_mm; 155, identifier:mismatches; 156, block; 156, 157; 157, return_statement; 157, 158; 158, True; 159, elif_clause; 159, 160; 159, 177; 160, boolean_operator:and; 160, 161; 160, 169; 161, parenthesized_expression; 161, 162; 162, boolean_operator:and; 162, 163; 162, 166; 163, comparison_operator:is; 163, 164; 163, 165; 164, identifier:r_mm; 165, False; 166, comparison_operator:<=; 166, 167; 166, 168; 167, identifier:r_mm; 168, identifier:mismatches; 169, parenthesized_expression; 169, 170; 170, boolean_operator:and; 170, 171; 170, 174; 171, comparison_operator:is; 171, 172; 171, 173; 172, identifier:p_mm; 173, False; 174, comparison_operator:<=; 174, 175; 174, 176; 175, identifier:p_mm; 176, identifier:mismatches; 177, block; 177, 178; 178, return_statement; 178, 179; 179, True; 180, return_statement; 180, 181; 181, False
def check_mismatches(read, pair, mismatches, mm_option, req_map): """ - check to see if the read maps with <= threshold number of mismatches - mm_option = 'one' or 'both' depending on whether or not one or both reads in a pair need to pass the mismatch threshold - pair can be False if read does not have a pair - make sure alignment score is not 0, which would indicate that the read was not aligned to the reference """ # if read is not paired, make sure it is mapped and that mm <= thresh if pair is False: mm = count_mismatches(read) if mm is False: return False # if no threshold is supplied, return True if mismatches is False: return True # passes threshold? if mm <= mismatches: return True # paired reads r_mm = count_mismatches(read) p_mm = count_mismatches(pair) # if neither read is mapped, return False if r_mm is False and p_mm is False: return False # if no threshold, return True if mismatches is False: return True # if req_map is True, both reads have to map if req_map is True: if r_mm is False or p_mm is False: return False ## if option is 'one,' only one read has to pass threshold if mm_option == 'one': if (r_mm is not False and r_mm <= mismatches) or (p_mm is not False and p_mm <= mismatches): return True ## if option is 'both,' both reads have to pass threshold if mm_option == 'both': ## if one read in pair does not map to the scaffold, ## make sure the other read passes threshold if r_mm is False: if p_mm <= mismatches: return True elif p_mm is False: if r_mm <= mismatches: return True elif (r_mm is not False and r_mm <= mismatches) and (p_mm is not False and p_mm <= mismatches): return True return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:get_steam; 3, parameters; 4, block; 4, 5; 4, 7; 4, 8; 4, 9; 4, 10; 4, 30; 4, 31; 4, 32; 4, 40; 4, 54; 4, 68; 4, 69; 4, 70; 4, 71; 4, 72; 4, 97; 4, 98; 4, 99; 4, 100; 4, 101; 5, expression_statement; 5, 6; 6, comment; 7, comment; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:helper; 13, lambda; 13, 14; 13, 16; 14, lambda_parameters; 14, 15; 15, identifier:udd; 16, conditional_expression:if; 16, 17; 16, 21; 16, 29; 17, call; 17, 18; 17, 19; 18, identifier:Steam; 19, argument_list; 19, 20; 20, identifier:udd; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:os; 25, identifier:path; 26, identifier:exists; 27, argument_list; 27, 28; 28, identifier:udd; 29, None; 30, comment; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:plat; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:platform; 38, identifier:system; 39, argument_list; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:==; 41, 42; 41, 43; 42, identifier:plat; 43, string:'Darwin'; 44, block; 44, 45; 45, return_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:helper; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:paths; 52, identifier:default_osx_userdata_path; 53, argument_list; 54, if_statement; 54, 55; 54, 58; 55, comparison_operator:==; 55, 56; 55, 57; 56, identifier:plat; 57, string:'Linux'; 58, block; 58, 59; 59, return_statement; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:helper; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:paths; 66, identifier:default_linux_userdata_path; 67, argument_list; 68, comment; 69, comment; 70, comment; 71, comment; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:==; 73, 74; 73, 75; 74, identifier:plat; 75, string:'Windows'; 76, block; 76, 77; 76, 85; 76, 86; 76, 87; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:possible_dir; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:winutils; 83, identifier:find_userdata_directory; 84, argument_list; 85, comment; 86, comment; 87, return_statement; 87, 88; 88, conditional_expression:if; 88, 89; 88, 93; 88, 96; 89, call; 89, 90; 89, 91; 90, identifier:helper; 91, argument_list; 91, 92; 92, identifier:possible_dir; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:possible_dir; 95, None; 96, None; 97, comment; 98, comment; 99, comment; 100, comment; 101, return_statement; 101, 102; 102, None
def get_steam(): """ Returns a Steam object representing the current Steam installation on the users computer. If the user doesn't have Steam installed, returns None. """ # Helper function which checks if the potential userdata directory exists # and returns a new Steam instance with that userdata directory if it does. # If the directory doesnt exist it returns None instead helper = lambda udd: Steam(udd) if os.path.exists(udd) else None # For both OS X and Linux, Steam stores it's userdata in a consistent # location. plat = platform.system() if plat == 'Darwin': return helper(paths.default_osx_userdata_path()) if plat == 'Linux': return helper(paths.default_linux_userdata_path()) # Windows is a bit trickier. The userdata directory is stored in the Steam # installation directory, meaning that theoretically it could be anywhere. # Luckily, Valve stores the installation directory in the registry, so its # still possible for us to figure out automatically if plat == 'Windows': possible_dir = winutils.find_userdata_directory() # Unlike the others, `possible_dir` might be None (if something odd # happened with the registry) return helper(possible_dir) if possible_dir is not None else None # This should never be hit. Windows, OS X, and Linux should be the only # supported platforms. # TODO: Add logging here so that the user (developer) knows that something # odd happened. return None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:generate_barcodes; 3, parameters; 3, 4; 3, 5; 4, identifier:nIds; 5, default_parameter; 5, 6; 5, 7; 6, identifier:codeLen; 7, integer:12; 8, block; 8, 9; 8, 11; 8, 41; 8, 56; 8, 77; 8, 78; 8, 90; 8, 115; 8, 119; 8, 195; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 17; 12, function_name:next_code; 13, parameters; 13, 14; 13, 15; 13, 16; 14, identifier:b; 15, identifier:c; 16, identifier:i; 17, block; 17, 18; 18, return_statement; 18, 19; 19, binary_operator:+; 19, 20; 19, 27; 20, binary_operator:+; 20, 21; 20, 26; 21, subscript; 21, 22; 21, 23; 22, identifier:c; 23, slice; 23, 24; 23, 25; 24, colon; 25, identifier:i; 26, identifier:b; 27, parenthesized_expression; 27, 28; 28, conditional_expression:if; 28, 29; 28, 36; 28, 40; 29, subscript; 29, 30; 29, 31; 30, identifier:c; 31, slice; 31, 32; 31, 35; 32, binary_operator:+; 32, 33; 32, 34; 33, identifier:i; 34, integer:1; 35, colon; 36, comparison_operator:<; 36, 37; 36, 38; 37, identifier:i; 38, unary_operator:-; 38, 39; 39, integer:1; 40, string:''; 41, function_definition; 41, 42; 41, 43; 41, 44; 42, function_name:rand_base; 43, parameters; 44, block; 44, 45; 45, return_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:random; 49, identifier:choice; 50, argument_list; 50, 51; 51, list:['A', 'T', 'C', 'G']; 51, 52; 51, 53; 51, 54; 51, 55; 52, string:'A'; 53, string:'T'; 54, string:'C'; 55, string:'G'; 56, function_definition; 56, 57; 56, 58; 56, 60; 57, function_name:rand_seq; 58, parameters; 58, 59; 59, identifier:n; 60, block; 60, 61; 61, return_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, string:''; 65, identifier:join; 66, argument_list; 66, 67; 67, list_comprehension; 67, 68; 67, 71; 68, call; 68, 69; 68, 70; 69, identifier:rand_base; 70, argument_list; 71, for_in_clause; 71, 72; 71, 73; 72, identifier:_; 73, call; 73, 74; 73, 75; 74, identifier:range; 75, argument_list; 75, 76; 76, identifier:n; 77, comment; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:hpf; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:re; 84, identifier:compile; 85, argument_list; 85, 86; 85, 87; 86, string:'aaaa|cccc|gggg|tttt'; 87, attribute; 87, 88; 87, 89; 88, identifier:re; 89, identifier:IGNORECASE; 90, while_statement; 90, 91; 90, 92; 91, True; 92, block; 92, 93; 92, 101; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:codes; 96, list:[rand_seq(codeLen)]; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:rand_seq; 99, argument_list; 99, 100; 100, identifier:codeLen; 101, if_statement; 101, 102; 101, 113; 102, parenthesized_expression; 102, 103; 103, comparison_operator:is; 103, 104; 103, 112; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:hpf; 107, identifier:search; 108, argument_list; 108, 109; 109, subscript; 109, 110; 109, 111; 110, identifier:codes; 111, integer:0; 112, None; 113, block; 113, 114; 114, break_statement; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:idx; 118, integer:0; 119, while_statement; 119, 120; 119, 126; 120, comparison_operator:<; 120, 121; 120, 125; 121, call; 121, 122; 121, 123; 122, identifier:len; 123, argument_list; 123, 124; 124, identifier:codes; 125, identifier:nIds; 126, block; 126, 127; 126, 131; 126, 185; 127, expression_statement; 127, 128; 128, augmented_assignment:-=; 128, 129; 128, 130; 129, identifier:idx; 130, integer:1; 131, if_statement; 131, 132; 131, 136; 131, 152; 132, comparison_operator:<; 132, 133; 132, 134; 133, identifier:idx; 134, unary_operator:-; 134, 135; 135, identifier:codeLen; 136, block; 136, 137; 136, 142; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:idx; 140, unary_operator:-; 140, 141; 141, integer:1; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:codes; 146, identifier:append; 147, argument_list; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:rand_seq; 150, argument_list; 150, 151; 151, identifier:codeLen; 152, else_clause; 152, 153; 153, block; 153, 154; 153, 168; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:nc; 157, call; 157, 158; 157, 159; 158, identifier:next_code; 159, argument_list; 159, 160; 159, 163; 159, 167; 160, call; 160, 161; 160, 162; 161, identifier:rand_base; 162, argument_list; 163, subscript; 163, 164; 163, 165; 164, identifier:codes; 165, unary_operator:-; 165, 166; 166, integer:1; 167, identifier:idx; 168, if_statement; 168, 169; 168, 177; 169, comparison_operator:is; 169, 170; 169, 176; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:hpf; 173, identifier:search; 174, argument_list; 174, 175; 175, identifier:nc; 176, None; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:codes; 182, identifier:append; 183, argument_list; 183, 184; 184, identifier:nc; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:codes; 188, call; 188, 189; 188, 190; 189, identifier:list; 190, argument_list; 190, 191; 191, call; 191, 192; 191, 193; 192, identifier:set; 193, argument_list; 193, 194; 194, identifier:codes; 195, return_statement; 195, 196; 196, identifier:codes
def generate_barcodes(nIds, codeLen=12): """ Given a list of sample IDs generate unique n-base barcodes for each. Note that only 4^n unique barcodes are possible. """ def next_code(b, c, i): return c[:i] + b + (c[i+1:] if i < -1 else '') def rand_base(): return random.choice(['A', 'T', 'C', 'G']) def rand_seq(n): return ''.join([rand_base() for _ in range(n)]) # homopolymer filter regex: match if 4 identical bases in a row hpf = re.compile('aaaa|cccc|gggg|tttt', re.IGNORECASE) while True: codes = [rand_seq(codeLen)] if (hpf.search(codes[0]) is None): break idx = 0 while len(codes) < nIds: idx -= 1 if idx < -codeLen: idx = -1 codes.append(rand_seq(codeLen)) else: nc = next_code(rand_base(), codes[-1], idx) if hpf.search(nc) is None: codes.append(nc) codes = list(set(codes)) return codes
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:parse_fasta_annotations; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:fastas; 5, identifier:annot_tables; 6, identifier:trans_table; 7, block; 7, 8; 7, 10; 7, 58; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 14; 11, comparison_operator:is; 11, 12; 11, 13; 12, identifier:annot_tables; 13, False; 14, block; 14, 15; 14, 19; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:annots; 18, dictionary; 19, for_statement; 19, 20; 19, 21; 19, 22; 20, identifier:table; 21, identifier:annot_tables; 22, block; 22, 23; 23, for_statement; 23, 24; 23, 25; 23, 29; 24, identifier:cds; 25, call; 25, 26; 25, 27; 26, identifier:open; 27, argument_list; 27, 28; 28, identifier:table; 29, block; 29, 30; 29, 46; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 37; 32, pattern_list; 32, 33; 32, 34; 32, 35; 32, 36; 33, identifier:ID; 34, identifier:start; 35, identifier:end; 36, identifier:strand; 37, call; 37, 38; 37, 45; 38, attribute; 38, 39; 38, 44; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:cds; 42, identifier:strip; 43, argument_list; 44, identifier:split; 45, argument_list; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:annots; 50, identifier:ID; 51, list:[start, end, int(strand)]; 51, 52; 51, 53; 51, 54; 52, identifier:start; 53, identifier:end; 54, call; 54, 55; 54, 56; 55, identifier:int; 56, argument_list; 56, 57; 57, identifier:strand; 58, for_statement; 58, 59; 58, 60; 58, 61; 59, identifier:fasta; 60, identifier:fastas; 61, block; 61, 62; 62, for_statement; 62, 63; 62, 64; 62, 68; 63, identifier:seq; 64, call; 64, 65; 64, 66; 65, identifier:parse_fasta; 66, argument_list; 66, 67; 67, identifier:fasta; 68, block; 68, 69; 68, 101; 68, 189; 68, 208; 68, 209; 68, 318; 68, 335; 69, if_statement; 69, 70; 69, 86; 70, boolean_operator:and; 70, 71; 70, 83; 71, parenthesized_expression; 71, 72; 72, boolean_operator:and; 72, 73; 72, 78; 73, comparison_operator:not; 73, 74; 73, 75; 74, string:'# ;gc_cont'; 75, subscript; 75, 76; 75, 77; 76, identifier:seq; 77, integer:0; 78, comparison_operator:not; 78, 79; 78, 80; 79, string:'# ID='; 80, subscript; 80, 81; 80, 82; 81, identifier:seq; 82, integer:0; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:annot_tables; 85, False; 86, block; 86, 87; 86, 97; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:print; 90, argument_list; 90, 91; 90, 92; 91, string:'# specify fasta from Prodigal or annotations table (-t)'; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:file; 94, attribute; 94, 95; 94, 96; 95, identifier:sys; 96, identifier:stderr; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:exit; 100, argument_list; 101, if_statement; 101, 102; 101, 107; 101, 160; 102, comparison_operator:in; 102, 103; 102, 104; 103, string:'ID='; 104, subscript; 104, 105; 104, 106; 105, identifier:seq; 106, integer:0; 107, block; 107, 108; 107, 130; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:ID; 111, subscript; 111, 112; 111, 129; 112, call; 112, 113; 112, 126; 113, attribute; 113, 114; 113, 125; 114, subscript; 114, 115; 114, 124; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:seq; 119, integer:0; 120, identifier:rsplit; 121, argument_list; 121, 122; 121, 123; 122, string:'ID='; 123, integer:1; 124, integer:1; 125, identifier:split; 126, argument_list; 126, 127; 126, 128; 127, string:';'; 128, integer:1; 129, integer:0; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:contig; 133, subscript; 133, 134; 133, 159; 134, call; 134, 135; 134, 153; 135, attribute; 135, 136; 135, 152; 136, subscript; 136, 137; 136, 151; 137, call; 137, 138; 137, 149; 138, attribute; 138, 139; 138, 148; 139, subscript; 139, 140; 139, 147; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:seq; 144, integer:0; 145, identifier:split; 146, argument_list; 147, integer:0; 148, identifier:split; 149, argument_list; 149, 150; 150, string:'>'; 151, integer:1; 152, identifier:rsplit; 153, argument_list; 153, 154; 153, 158; 154, binary_operator:%; 154, 155; 154, 156; 155, string:'_%s'; 156, parenthesized_expression; 156, 157; 157, identifier:ID; 158, integer:1; 159, integer:0; 160, else_clause; 160, 161; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:contig; 165, subscript; 165, 166; 165, 188; 166, call; 166, 167; 166, 185; 167, attribute; 167, 168; 167, 184; 168, subscript; 168, 169; 168, 183; 169, call; 169, 170; 169, 181; 170, attribute; 170, 171; 170, 180; 171, subscript; 171, 172; 171, 179; 172, call; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:seq; 176, integer:0; 177, identifier:split; 178, argument_list; 179, integer:0; 180, identifier:split; 181, argument_list; 181, 182; 182, string:'>'; 183, integer:1; 184, identifier:rsplit; 185, argument_list; 185, 186; 185, 187; 186, string:'_'; 187, integer:1; 188, integer:0; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:locus; 192, subscript; 192, 193; 192, 207; 193, call; 193, 194; 193, 205; 194, attribute; 194, 195; 194, 204; 195, subscript; 195, 196; 195, 203; 196, call; 196, 197; 196, 202; 197, attribute; 197, 198; 197, 201; 198, subscript; 198, 199; 198, 200; 199, identifier:seq; 200, integer:0; 201, identifier:split; 202, argument_list; 203, integer:0; 204, identifier:split; 205, argument_list; 205, 206; 206, string:'>'; 207, integer:1; 208, comment; 209, if_statement; 209, 210; 209, 222; 209, 292; 209, 293; 210, parenthesized_expression; 210, 211; 211, boolean_operator:or; 211, 212; 211, 217; 212, comparison_operator:in; 212, 213; 212, 214; 213, string:'# ;gc_cont'; 214, subscript; 214, 215; 214, 216; 215, identifier:seq; 216, integer:0; 217, comparison_operator:in; 217, 218; 217, 219; 218, string:'# ID='; 219, subscript; 219, 220; 219, 221; 220, identifier:seq; 221, integer:0; 222, block; 222, 223; 222, 234; 222, 256; 222, 272; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:info; 226, call; 226, 227; 226, 232; 227, attribute; 227, 228; 227, 231; 228, subscript; 228, 229; 228, 230; 229, identifier:seq; 230, integer:0; 231, identifier:split; 232, argument_list; 232, 233; 233, string:' # '; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 240; 236, pattern_list; 236, 237; 236, 238; 236, 239; 237, identifier:start; 238, identifier:end; 239, identifier:strand; 240, expression_list; 240, 241; 240, 247; 240, 253; 241, call; 241, 242; 241, 243; 242, identifier:int; 243, argument_list; 243, 244; 244, subscript; 244, 245; 244, 246; 245, identifier:info; 246, integer:1; 247, call; 247, 248; 247, 249; 248, identifier:int; 249, argument_list; 249, 250; 250, subscript; 250, 251; 250, 252; 251, identifier:info; 252, integer:2; 253, subscript; 253, 254; 253, 255; 254, identifier:info; 255, integer:3; 256, if_statement; 256, 257; 256, 260; 256, 265; 257, comparison_operator:==; 257, 258; 257, 259; 258, identifier:strand; 259, string:'1'; 260, block; 260, 261; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:strand; 264, integer:1; 265, else_clause; 265, 266; 266, block; 266, 267; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:strand; 270, unary_operator:-; 270, 271; 271, integer:1; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:product; 275, list:[''.join(info[4].split()[1:])]; 275, 276; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, string:''; 279, identifier:join; 280, argument_list; 280, 281; 281, subscript; 281, 282; 281, 289; 282, call; 282, 283; 282, 288; 283, attribute; 283, 284; 283, 287; 284, subscript; 284, 285; 284, 286; 285, identifier:info; 286, integer:4; 287, identifier:split; 288, argument_list; 289, slice; 289, 290; 289, 291; 290, integer:1; 291, colon; 292, comment; 293, else_clause; 293, 294; 294, block; 294, 295; 294, 304; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 301; 297, pattern_list; 297, 298; 297, 299; 297, 300; 298, identifier:start; 299, identifier:end; 300, identifier:strand; 301, subscript; 301, 302; 301, 303; 302, identifier:annots; 303, identifier:locus; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:product; 307, subscript; 307, 308; 307, 317; 308, call; 308, 309; 308, 314; 309, attribute; 309, 310; 309, 313; 310, subscript; 310, 311; 310, 312; 311, identifier:seq; 312, integer:0; 313, identifier:split; 314, argument_list; 314, 315; 314, 316; 315, string:' '; 316, integer:1; 317, integer:1; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:info; 321, dictionary; 321, 322; 321, 326; 321, 332; 322, pair; 322, 323; 322, 324; 323, string:'transl_table'; 324, list:[trans_table]; 324, 325; 325, identifier:trans_table; 326, pair; 326, 327; 326, 328; 327, string:'translation'; 328, list:[seq[1]]; 328, 329; 329, subscript; 329, 330; 329, 331; 330, identifier:seq; 331, integer:1; 332, pair; 332, 333; 332, 334; 333, string:'product'; 334, identifier:product; 335, expression_statement; 335, 336; 336, yield; 336, 337; 337, expression_list; 337, 338; 337, 339; 338, identifier:contig; 339, list:[locus, [start, end, strand], info]; 339, 340; 339, 341; 339, 345; 340, identifier:locus; 341, list:[start, end, strand]; 341, 342; 341, 343; 341, 344; 342, identifier:start; 343, identifier:end; 344, identifier:strand; 345, identifier:info
def parse_fasta_annotations(fastas, annot_tables, trans_table): """ parse gene call information from Prodigal fasta output """ if annot_tables is not False: annots = {} for table in annot_tables: for cds in open(table): ID, start, end, strand = cds.strip().split() annots[ID] = [start, end, int(strand)] for fasta in fastas: for seq in parse_fasta(fasta): if ('# ;gc_cont' not in seq[0] and '# ID=' not in seq[0]) and annot_tables is False: print('# specify fasta from Prodigal or annotations table (-t)', file=sys.stderr) exit() if 'ID=' in seq[0]: ID = seq[0].rsplit('ID=', 1)[1].split(';', 1)[0] contig = seq[0].split()[0].split('>')[1].rsplit('_%s' % (ID), 1)[0] else: contig = seq[0].split()[0].split('>')[1].rsplit('_', 1)[0] locus = seq[0].split()[0].split('>')[1] # annotation info from Prodigal if ('# ;gc_cont' in seq[0] or '# ID=' in seq[0]): info = seq[0].split(' # ') start, end, strand = int(info[1]), int(info[2]), info[3] if strand == '1': strand = 1 else: strand = -1 product = [''.join(info[4].split()[1:])] # annotation info from table else: start, end, strand = annots[locus] product = seq[0].split(' ', 1)[1] info = {'transl_table':[trans_table], \ 'translation':[seq[1]], \ 'product':product} yield contig, [locus, [start, end, strand], info]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:find_consensus; 3, parameters; 3, 4; 4, identifier:bases; 5, block; 5, 6; 5, 8; 5, 17; 5, 34; 5, 35; 5, 78; 5, 96; 5, 126; 5, 186; 5, 192; 5, 198; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:nucs; 11, list:['A', 'T', 'G', 'C', 'N']; 11, 12; 11, 13; 11, 14; 11, 15; 11, 16; 12, string:'A'; 13, string:'T'; 14, string:'G'; 15, string:'C'; 16, string:'N'; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:total; 20, call; 20, 21; 20, 22; 21, identifier:sum; 22, argument_list; 22, 23; 23, list_comprehension; 23, 24; 23, 27; 23, 30; 24, subscript; 24, 25; 24, 26; 25, identifier:bases; 26, identifier:nuc; 27, for_in_clause; 27, 28; 27, 29; 28, identifier:nuc; 29, identifier:nucs; 30, if_clause; 30, 31; 31, comparison_operator:in; 31, 32; 31, 33; 32, identifier:nuc; 33, identifier:bases; 34, comment; 35, try_statement; 35, 36; 35, 54; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:top; 40, call; 40, 41; 40, 42; 41, identifier:max; 42, argument_list; 42, 43; 43, list_comprehension; 43, 44; 43, 47; 43, 50; 44, subscript; 44, 45; 44, 46; 45, identifier:bases; 46, identifier:nuc; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:nuc; 49, identifier:nucs; 50, if_clause; 50, 51; 51, comparison_operator:in; 51, 52; 51, 53; 52, identifier:nuc; 53, identifier:bases; 54, except_clause; 54, 55; 55, block; 55, 56; 55, 64; 55, 70; 55, 76; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:bases; 60, string:'consensus'; 61, tuple; 61, 62; 61, 63; 62, string:'N'; 63, string:'n/a'; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:bases; 68, string:'consensus frequency'; 69, string:'n/a'; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:bases; 74, string:'reference frequency'; 75, string:'n/a'; 76, return_statement; 76, 77; 77, identifier:bases; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:top; 81, list_comprehension; 81, 82; 81, 87; 81, 90; 82, tuple; 82, 83; 82, 84; 83, identifier:nuc; 84, subscript; 84, 85; 84, 86; 85, identifier:bases; 86, identifier:nuc; 87, for_in_clause; 87, 88; 87, 89; 88, identifier:nuc; 89, identifier:bases; 90, if_clause; 90, 91; 91, comparison_operator:==; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:bases; 94, identifier:nuc; 95, identifier:top; 96, if_statement; 96, 97; 96, 104; 96, 113; 97, comparison_operator:==; 97, 98; 97, 103; 98, subscript; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:top; 101, integer:0; 102, integer:1; 103, integer:0; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:bases; 109, string:'consensus'; 110, tuple; 110, 111; 110, 112; 111, string:'n/a'; 112, integer:0; 113, else_clause; 113, 114; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:bases; 119, string:'consensus'; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:random; 123, identifier:choice; 124, argument_list; 124, 125; 125, identifier:top; 126, if_statement; 126, 127; 126, 130; 126, 139; 127, comparison_operator:==; 127, 128; 127, 129; 128, identifier:total; 129, integer:0; 130, block; 130, 131; 130, 135; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:c_freq; 134, string:'n/a'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:ref_freq; 138, string:'n/a'; 139, else_clause; 139, 140; 140, block; 140, 141; 140, 157; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:c_freq; 144, binary_operator:/; 144, 145; 144, 153; 145, call; 145, 146; 145, 147; 146, identifier:float; 147, argument_list; 147, 148; 148, subscript; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:bases; 151, string:'consensus'; 152, integer:1; 153, call; 153, 154; 153, 155; 154, identifier:float; 155, argument_list; 155, 156; 156, identifier:total; 157, if_statement; 157, 158; 157, 163; 157, 168; 158, comparison_operator:not; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:bases; 161, string:'ref'; 162, identifier:bases; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:ref_freq; 167, integer:0; 168, else_clause; 168, 169; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:ref_freq; 173, binary_operator:/; 173, 174; 173, 182; 174, call; 174, 175; 174, 176; 175, identifier:float; 176, argument_list; 176, 177; 177, subscript; 177, 178; 177, 179; 178, identifier:bases; 179, subscript; 179, 180; 179, 181; 180, identifier:bases; 181, string:'ref'; 182, call; 182, 183; 182, 184; 183, identifier:float; 184, argument_list; 184, 185; 185, identifier:total; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:bases; 190, string:'consensus frequency'; 191, identifier:c_freq; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 197; 194, subscript; 194, 195; 194, 196; 195, identifier:bases; 196, string:'reference frequency'; 197, identifier:ref_freq; 198, return_statement; 198, 199; 199, identifier:bases
def find_consensus(bases): """ find consensus base based on nucleotide frequencies """ nucs = ['A', 'T', 'G', 'C', 'N'] total = sum([bases[nuc] for nuc in nucs if nuc in bases]) # save most common base as consensus (random nuc if there is a tie) try: top = max([bases[nuc] for nuc in nucs if nuc in bases]) except: bases['consensus'] = ('N', 'n/a') bases['consensus frequency'] = 'n/a' bases['reference frequency'] = 'n/a' return bases top = [(nuc, bases[nuc]) for nuc in bases if bases[nuc] == top] if top[0][1] == 0: bases['consensus'] = ('n/a', 0) else: bases['consensus'] = random.choice(top) if total == 0: c_freq = 'n/a' ref_freq = 'n/a' else: c_freq = float(bases['consensus'][1]) / float(total) if bases['ref'] not in bases: ref_freq = 0 else: ref_freq = float(bases[bases['ref']]) / float(total) bases['consensus frequency'] = c_freq bases['reference frequency'] = ref_freq return bases
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:print_consensus; 3, parameters; 3, 4; 4, identifier:genomes; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 14; 5, 132; 5, 133; 5, 209; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:cons; 12, dictionary; 13, comment; 14, for_statement; 14, 15; 14, 18; 14, 26; 15, pattern_list; 15, 16; 15, 17; 16, identifier:genome; 17, identifier:contigs; 18, call; 18, 19; 18, 20; 19, identifier:list; 20, argument_list; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:genomes; 24, identifier:items; 25, argument_list; 26, block; 26, 27; 26, 33; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, subscript; 29, 30; 29, 31; 30, identifier:cons; 31, identifier:genome; 32, dictionary; 33, for_statement; 33, 34; 33, 37; 33, 45; 34, pattern_list; 34, 35; 34, 36; 35, identifier:contig; 36, identifier:samples; 37, call; 37, 38; 37, 39; 38, identifier:list; 39, argument_list; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:contigs; 43, identifier:items; 44, argument_list; 45, block; 45, 46; 46, for_statement; 46, 47; 46, 50; 46, 58; 47, pattern_list; 47, 48; 47, 49; 48, identifier:sample; 49, identifier:stats; 50, call; 50, 51; 50, 52; 51, identifier:list; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:samples; 56, identifier:items; 57, argument_list; 58, block; 58, 59; 58, 74; 58, 86; 59, if_statement; 59, 60; 59, 65; 60, comparison_operator:not; 60, 61; 60, 62; 61, identifier:sample; 62, subscript; 62, 63; 62, 64; 63, identifier:cons; 64, identifier:genome; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 73; 68, subscript; 68, 69; 68, 72; 69, subscript; 69, 70; 69, 71; 70, identifier:cons; 71, identifier:genome; 72, identifier:sample; 73, dictionary; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:seq; 77, assignment; 77, 78; 77, 85; 78, subscript; 78, 79; 78, 84; 79, subscript; 79, 80; 79, 83; 80, subscript; 80, 81; 80, 82; 81, identifier:cons; 82, identifier:genome; 83, identifier:sample; 84, identifier:contig; 85, list:[]; 86, for_statement; 86, 87; 86, 90; 86, 97; 87, pattern_list; 87, 88; 87, 89; 88, identifier:pos; 89, identifier:ps; 90, call; 90, 91; 90, 92; 91, identifier:enumerate; 92, argument_list; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:stats; 95, string:'bp_stats'; 96, integer:1; 97, block; 97, 98; 97, 112; 97, 125; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 103; 100, pattern_list; 100, 101; 100, 102; 101, identifier:ref; 102, identifier:consensus; 103, expression_list; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:ps; 106, string:'ref'; 107, subscript; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:ps; 110, string:'consensus'; 111, integer:0; 112, if_statement; 112, 113; 112, 116; 113, comparison_operator:==; 113, 114; 113, 115; 114, identifier:consensus; 115, string:'n/a'; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:consensus; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:ref; 123, identifier:lower; 124, argument_list; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:seq; 129, identifier:append; 130, argument_list; 130, 131; 131, identifier:consensus; 132, comment; 133, for_statement; 133, 134; 133, 137; 133, 142; 134, pattern_list; 134, 135; 134, 136; 135, identifier:genome; 136, identifier:samples; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:cons; 140, identifier:items; 141, argument_list; 142, block; 142, 143; 143, for_statement; 143, 144; 143, 147; 143, 152; 144, pattern_list; 144, 145; 144, 146; 145, identifier:sample; 146, identifier:contigs; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:samples; 150, identifier:items; 151, argument_list; 152, block; 152, 153; 152, 161; 152, 169; 152, 203; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:fn; 156, binary_operator:%; 156, 157; 156, 158; 157, string:'%s.%s.consensus.fa'; 158, tuple; 158, 159; 158, 160; 159, identifier:genome; 160, identifier:sample; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:f; 164, call; 164, 165; 164, 166; 165, identifier:open; 166, argument_list; 166, 167; 166, 168; 167, identifier:fn; 168, string:'w'; 169, for_statement; 169, 170; 169, 173; 169, 178; 170, pattern_list; 170, 171; 170, 172; 171, identifier:contig; 172, identifier:seq; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:contigs; 176, identifier:items; 177, argument_list; 178, block; 178, 179; 178, 190; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:print; 182, argument_list; 182, 183; 182, 187; 183, binary_operator:%; 183, 184; 183, 185; 184, string:'>%s'; 185, parenthesized_expression; 185, 186; 186, identifier:contig; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:file; 189, identifier:f; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 193; 192, identifier:print; 193, argument_list; 193, 194; 193, 200; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, string:''; 197, identifier:join; 198, argument_list; 198, 199; 199, identifier:seq; 200, keyword_argument; 200, 201; 200, 202; 201, identifier:file; 202, identifier:f; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:f; 207, identifier:close; 208, argument_list; 209, return_statement; 209, 210; 210, identifier:cons
def print_consensus(genomes): """ print consensensus sequences for each genome and sample """ # generate consensus sequences cons = {} # cons[genome][sample][contig] = consensus for genome, contigs in list(genomes.items()): cons[genome] = {} for contig, samples in list(contigs.items()): for sample, stats in list(samples.items()): if sample not in cons[genome]: cons[genome][sample] = {} seq = cons[genome][sample][contig] = [] for pos, ps in enumerate(stats['bp_stats'], 1): ref, consensus = ps['ref'], ps['consensus'][0] if consensus == 'n/a': consensus = ref.lower() seq.append(consensus) # print consensus sequences for genome, samples in cons.items(): for sample, contigs in samples.items(): fn = '%s.%s.consensus.fa' % (genome, sample) f = open(fn, 'w') for contig, seq in contigs.items(): print('>%s' % (contig), file = f) print(''.join(seq), file = f) f.close() return cons
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parse_cov; 3, parameters; 3, 4; 3, 5; 4, identifier:cov_table; 5, identifier:scaffold2genome; 6, block; 6, 7; 6, 9; 6, 13; 6, 14; 6, 18; 6, 19; 6, 20; 6, 190; 6, 191; 6, 207; 6, 267; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:size; 12, dictionary; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:mapped; 17, dictionary; 18, comment; 19, comment; 20, for_statement; 20, 21; 20, 22; 20, 26; 21, identifier:line; 22, call; 22, 23; 22, 24; 23, identifier:open; 24, argument_list; 24, 25; 25, identifier:cov_table; 26, block; 26, 27; 26, 40; 26, 84; 26, 97; 26, 104; 26, 119; 26, 129; 26, 135; 26, 141; 26, 164; 26, 165; 26, 171; 26, 172; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:line; 30, call; 30, 31; 30, 38; 31, attribute; 31, 32; 31, 37; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:line; 35, identifier:strip; 36, argument_list; 37, identifier:split; 38, argument_list; 38, 39; 39, string:'\t'; 40, if_statement; 40, 41; 40, 49; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, subscript; 43, 44; 43, 45; 44, identifier:line; 45, integer:0; 46, identifier:startswith; 47, argument_list; 47, 48; 48, string:'#'; 49, block; 49, 50; 49, 58; 49, 83; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:samples; 53, subscript; 53, 54; 53, 55; 54, identifier:line; 55, slice; 55, 56; 55, 57; 56, integer:1; 57, colon; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:samples; 61, list_comprehension; 61, 62; 61, 80; 62, subscript; 62, 63; 62, 79; 63, call; 63, 64; 63, 76; 64, attribute; 64, 65; 64, 75; 65, subscript; 65, 66; 65, 73; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:i; 69, identifier:rsplit; 70, argument_list; 70, 71; 70, 72; 71, string:'/'; 72, integer:1; 73, unary_operator:-; 73, 74; 74, integer:1; 75, identifier:split; 76, argument_list; 76, 77; 76, 78; 77, string:'.'; 78, integer:1; 79, integer:0; 80, for_in_clause; 80, 81; 80, 82; 81, identifier:i; 82, identifier:samples; 83, continue_statement; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, pattern_list; 86, 87; 86, 88; 87, identifier:scaffold; 88, identifier:length; 89, call; 89, 90; 89, 95; 90, attribute; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:line; 93, integer:0; 94, identifier:split; 95, argument_list; 95, 96; 96, string:': '; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:length; 100, call; 100, 101; 100, 102; 101, identifier:float; 102, argument_list; 102, 103; 103, identifier:length; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:covs; 107, list_comprehension; 107, 108; 107, 112; 108, call; 108, 109; 108, 110; 109, identifier:float; 110, argument_list; 110, 111; 111, identifier:i; 112, for_in_clause; 112, 113; 112, 114; 113, identifier:i; 114, subscript; 114, 115; 114, 116; 115, identifier:line; 116, slice; 116, 117; 116, 118; 117, integer:1; 118, colon; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:bases; 122, list_comprehension; 122, 123; 122, 126; 123, binary_operator:*; 123, 124; 123, 125; 124, identifier:c; 125, identifier:length; 126, for_in_clause; 126, 127; 126, 128; 127, identifier:c; 128, identifier:covs; 129, if_statement; 129, 130; 129, 133; 130, comparison_operator:not; 130, 131; 130, 132; 131, identifier:scaffold; 132, identifier:scaffold2genome; 133, block; 133, 134; 134, continue_statement; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:genome; 138, subscript; 138, 139; 138, 140; 139, identifier:scaffold2genome; 140, identifier:scaffold; 141, if_statement; 141, 142; 141, 145; 142, comparison_operator:not; 142, 143; 142, 144; 143, identifier:genome; 144, identifier:size; 145, block; 145, 146; 145, 152; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, subscript; 148, 149; 148, 150; 149, identifier:size; 150, identifier:genome; 151, integer:0; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 157; 154, subscript; 154, 155; 154, 156; 155, identifier:mapped; 156, identifier:genome; 157, dictionary_comprehension; 157, 158; 157, 161; 158, pair; 158, 159; 158, 160; 159, identifier:sample; 160, integer:0; 161, for_in_clause; 161, 162; 161, 163; 162, identifier:sample; 163, identifier:samples; 164, comment; 165, expression_statement; 165, 166; 166, augmented_assignment:+=; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:size; 169, identifier:genome; 170, identifier:length; 171, comment; 172, for_statement; 172, 173; 172, 176; 172, 181; 173, pattern_list; 173, 174; 173, 175; 174, identifier:sample; 175, identifier:count; 176, call; 176, 177; 176, 178; 177, identifier:zip; 178, argument_list; 178, 179; 178, 180; 179, identifier:samples; 180, identifier:bases; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, augmented_assignment:+=; 183, 184; 183, 189; 184, subscript; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:mapped; 187, identifier:genome; 188, identifier:sample; 189, identifier:count; 190, comment; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:coverage; 194, dictionary; 194, 195; 194, 198; 194, 201; 194, 204; 195, pair; 195, 196; 195, 197; 196, string:'genome'; 197, list:[]; 198, pair; 198, 199; 198, 200; 199, string:'genome size (bp)'; 200, list:[]; 201, pair; 201, 202; 201, 203; 202, string:'sample'; 203, list:[]; 204, pair; 204, 205; 204, 206; 205, string:'coverage'; 206, list:[]; 207, for_statement; 207, 208; 207, 211; 207, 216; 208, pattern_list; 208, 209; 208, 210; 209, identifier:genome; 210, identifier:length; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:size; 214, identifier:items; 215, argument_list; 216, block; 216, 217; 217, for_statement; 217, 218; 217, 219; 217, 220; 218, identifier:sample; 219, identifier:samples; 220, block; 220, 221; 220, 231; 220, 240; 220, 249; 220, 258; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:cov; 224, binary_operator:/; 224, 225; 224, 230; 225, subscript; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:mapped; 228, identifier:genome; 229, identifier:sample; 230, identifier:length; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 238; 233, attribute; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:coverage; 236, string:'genome'; 237, identifier:append; 238, argument_list; 238, 239; 239, identifier:genome; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 247; 242, attribute; 242, 243; 242, 246; 243, subscript; 243, 244; 243, 245; 244, identifier:coverage; 245, string:'genome size (bp)'; 246, identifier:append; 247, argument_list; 247, 248; 248, identifier:length; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 256; 251, attribute; 251, 252; 251, 255; 252, subscript; 252, 253; 252, 254; 253, identifier:coverage; 254, string:'sample'; 255, identifier:append; 256, argument_list; 256, 257; 257, identifier:sample; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 265; 260, attribute; 260, 261; 260, 264; 261, subscript; 261, 262; 261, 263; 262, identifier:coverage; 263, string:'coverage'; 264, identifier:append; 265, argument_list; 265, 266; 266, identifier:cov; 267, return_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:pd; 271, identifier:DataFrame; 272, argument_list; 272, 273; 273, identifier:coverage
def parse_cov(cov_table, scaffold2genome): """ calculate genome coverage from scaffold coverage table """ size = {} # size[genome] = genome size mapped = {} # mapped[genome][sample] = mapped bases # parse coverage files for line in open(cov_table): line = line.strip().split('\t') if line[0].startswith('#'): samples = line[1:] samples = [i.rsplit('/', 1)[-1].split('.', 1)[0] for i in samples] continue scaffold, length = line[0].split(': ') length = float(length) covs = [float(i) for i in line[1:]] bases = [c * length for c in covs] if scaffold not in scaffold2genome: continue genome = scaffold2genome[scaffold] if genome not in size: size[genome] = 0 mapped[genome] = {sample:0 for sample in samples} # keep track of genome size size[genome] += length # keep track of number of mapped bases for sample, count in zip(samples, bases): mapped[genome][sample] += count # calculate coverage from base counts and genome size coverage = {'genome':[], 'genome size (bp)':[], 'sample':[], 'coverage':[]} for genome, length in size.items(): for sample in samples: cov = mapped[genome][sample] / length coverage['genome'].append(genome) coverage['genome size (bp)'].append(length) coverage['sample'].append(sample) coverage['coverage'].append(cov) return pd.DataFrame(coverage)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:print_genome_matrix; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:hits; 5, identifier:fastas; 6, identifier:id2desc; 7, identifier:file_name; 8, block; 8, 9; 8, 11; 8, 19; 8, 26; 8, 34; 8, 50; 8, 122; 8, 130; 8, 138; 8, 154; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:out; 14, call; 14, 15; 14, 16; 15, identifier:open; 16, argument_list; 16, 17; 16, 18; 17, identifier:file_name; 18, string:'w'; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:fastas; 22, call; 22, 23; 22, 24; 23, identifier:sorted; 24, argument_list; 24, 25; 25, identifier:fastas; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:print; 29, argument_list; 29, 30; 29, 31; 30, string:'## percent identity between genomes'; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:file; 33, identifier:out; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:print; 37, argument_list; 37, 38; 37, 47; 38, binary_operator:%; 38, 39; 38, 40; 39, string:'# - \t %s'; 40, parenthesized_expression; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, string:'\t'; 44, identifier:join; 45, argument_list; 45, 46; 46, identifier:fastas; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:file; 49, identifier:out; 50, for_statement; 50, 51; 50, 52; 50, 53; 51, identifier:fasta; 52, identifier:fastas; 53, block; 53, 54; 53, 59; 53, 109; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:line; 57, list:[fasta]; 57, 58; 58, identifier:fasta; 59, for_statement; 59, 60; 59, 61; 59, 62; 60, identifier:other; 61, identifier:fastas; 62, block; 62, 63; 62, 99; 63, if_statement; 63, 64; 63, 67; 63, 72; 64, comparison_operator:==; 64, 65; 64, 66; 65, identifier:other; 66, identifier:fasta; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:average; 71, string:'-'; 72, else_clause; 72, 73; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:average; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:numpy; 80, identifier:average; 81, argument_list; 81, 82; 82, list_comprehension; 82, 83; 82, 92; 83, subscript; 83, 84; 83, 91; 84, subscript; 84, 85; 84, 90; 85, subscript; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:hits; 88, identifier:fasta; 89, identifier:other; 90, identifier:i; 91, integer:3; 92, for_in_clause; 92, 93; 92, 94; 93, identifier:i; 94, subscript; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:hits; 97, identifier:fasta; 98, identifier:other; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:line; 103, identifier:append; 104, argument_list; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:str; 107, argument_list; 107, 108; 108, identifier:average; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:print; 112, argument_list; 112, 113; 112, 119; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, string:'\t'; 116, identifier:join; 117, argument_list; 117, 118; 118, identifier:line; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:file; 121, identifier:out; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:print; 125, argument_list; 125, 126; 125, 127; 126, string:''; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:file; 129, identifier:out; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 133; 132, identifier:print; 133, argument_list; 133, 134; 133, 135; 134, string:'## percent of orfs that are orthologous between genomes'; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:file; 137, identifier:out; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:print; 141, argument_list; 141, 142; 141, 151; 142, binary_operator:%; 142, 143; 142, 144; 143, string:'# - \t %s'; 144, parenthesized_expression; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, string:'\t'; 148, identifier:join; 149, argument_list; 149, 150; 150, identifier:fastas; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:file; 153, identifier:out; 154, for_statement; 154, 155; 154, 156; 154, 157; 155, identifier:fasta; 156, identifier:fastas; 157, block; 157, 158; 157, 163; 157, 235; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:line; 161, list:[fasta]; 161, 162; 162, identifier:fasta; 163, for_statement; 163, 164; 163, 165; 163, 166; 164, identifier:other; 165, identifier:fastas; 166, block; 166, 167; 166, 225; 167, if_statement; 167, 168; 167, 171; 167, 176; 168, comparison_operator:==; 168, 169; 168, 170; 169, identifier:other; 170, identifier:fasta; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:percent; 175, string:'-'; 176, else_clause; 176, 177; 177, block; 177, 178; 177, 192; 177, 214; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:orthologs; 181, call; 181, 182; 181, 183; 182, identifier:float; 183, argument_list; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:len; 186, argument_list; 186, 187; 187, subscript; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:hits; 190, identifier:fasta; 191, identifier:other; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:orfs; 195, call; 195, 196; 195, 197; 196, identifier:float; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 200; 199, identifier:len; 200, argument_list; 200, 201; 201, list_comprehension; 201, 202; 201, 203; 201, 206; 202, identifier:i; 203, for_in_clause; 203, 204; 203, 205; 204, identifier:i; 205, identifier:id2desc; 206, if_clause; 206, 207; 207, comparison_operator:==; 207, 208; 207, 213; 208, subscript; 208, 209; 208, 212; 209, subscript; 209, 210; 209, 211; 210, identifier:id2desc; 211, identifier:i; 212, integer:0; 213, identifier:fasta; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:percent; 217, binary_operator:*; 217, 218; 217, 224; 218, call; 218, 219; 218, 220; 219, identifier:float; 220, argument_list; 220, 221; 221, binary_operator:/; 221, 222; 221, 223; 222, identifier:orthologs; 223, identifier:orfs; 224, integer:100; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:line; 229, identifier:append; 230, argument_list; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:str; 233, argument_list; 233, 234; 234, identifier:percent; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 238; 237, identifier:print; 238, argument_list; 238, 239; 238, 245; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, string:'\t'; 242, identifier:join; 243, argument_list; 243, 244; 244, identifier:line; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:file; 247, identifier:out
def print_genome_matrix(hits, fastas, id2desc, file_name): """ optimize later? slow ... should combine with calculate_threshold module """ out = open(file_name, 'w') fastas = sorted(fastas) print('## percent identity between genomes', file=out) print('# - \t %s' % ('\t'.join(fastas)), file=out) for fasta in fastas: line = [fasta] for other in fastas: if other == fasta: average = '-' else: average = numpy.average([hits[fasta][other][i][3] for i in hits[fasta][other]]) line.append(str(average)) print('\t'.join(line), file=out) print('', file=out) print('## percent of orfs that are orthologous between genomes', file=out) print('# - \t %s' % ('\t'.join(fastas)), file=out) for fasta in fastas: line = [fasta] for other in fastas: if other == fasta: percent = '-' else: orthologs = float(len(hits[fasta][other])) orfs = float(len([i for i in id2desc if id2desc[i][0] == fasta])) percent = float(orthologs / orfs) * 100 line.append(str(percent)) print('\t'.join(line), file=out)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:calc_thresholds; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 13; 4, identifier:rbh; 5, identifier:file_name; 6, default_parameter; 6, 7; 6, 8; 7, identifier:thresholds; 8, list:[False, False, False, False]; 8, 9; 8, 10; 8, 11; 8, 12; 9, False; 10, False; 11, False; 12, False; 13, default_parameter; 13, 14; 13, 15; 14, identifier:stdevs; 15, integer:2; 16, block; 16, 17; 16, 19; 16, 26; 16, 30; 16, 71; 16, 79; 16, 87; 16, 91; 16, 369; 16, 375; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:calc_threshold; 22, subscript; 22, 23; 22, 24; 23, identifier:thresholds; 24, unary_operator:-; 24, 25; 25, integer:1; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:norm_threshold; 29, dictionary; 30, for_statement; 30, 31; 30, 32; 30, 43; 31, identifier:pair; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:itertools; 35, identifier:permutations; 36, argument_list; 36, 37; 36, 42; 37, list_comprehension; 37, 38; 37, 39; 38, identifier:i; 39, for_in_clause; 39, 40; 39, 41; 40, identifier:i; 41, identifier:rbh; 42, integer:2; 43, block; 43, 44; 43, 59; 44, if_statement; 44, 45; 44, 50; 45, comparison_operator:not; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:pair; 48, integer:0; 49, identifier:norm_threshold; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 58; 53, subscript; 53, 54; 53, 55; 54, identifier:norm_threshold; 55, subscript; 55, 56; 55, 57; 56, identifier:pair; 57, integer:0; 58, dictionary; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 70; 61, subscript; 61, 62; 61, 67; 62, subscript; 62, 63; 62, 64; 63, identifier:norm_threshold; 64, subscript; 64, 65; 64, 66; 65, identifier:pair; 66, integer:0; 67, subscript; 67, 68; 67, 69; 68, identifier:pair; 69, integer:1; 70, dictionary; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:out; 74, call; 74, 75; 74, 76; 75, identifier:open; 76, argument_list; 76, 77; 76, 78; 77, identifier:file_name; 78, string:'w'; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:print; 82, argument_list; 82, 83; 82, 84; 83, string:'#### summary of rbh comparisons\n'; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:file; 86, identifier:out; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:comparisons; 90, list:[]; 91, for_statement; 91, 92; 91, 93; 91, 94; 92, identifier:genome; 93, identifier:rbh; 94, block; 94, 95; 95, for_statement; 95, 96; 95, 97; 95, 100; 96, identifier:compare; 97, subscript; 97, 98; 97, 99; 98, identifier:rbh; 99, identifier:genome; 100, block; 100, 101; 100, 115; 100, 121; 100, 128; 100, 147; 100, 159; 100, 231; 100, 316; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:pair; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, string:''; 107, identifier:join; 108, argument_list; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:sorted; 111, argument_list; 111, 112; 112, list:[genome, compare]; 112, 113; 112, 114; 113, identifier:genome; 114, identifier:compare; 115, if_statement; 115, 116; 115, 119; 116, comparison_operator:in; 116, 117; 116, 118; 117, identifier:pair; 118, identifier:comparisons; 119, block; 119, 120; 120, continue_statement; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:comparisons; 125, identifier:append; 126, argument_list; 126, 127; 127, identifier:pair; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:scores; 131, dictionary; 131, 132; 131, 135; 131, 138; 131, 141; 131, 144; 132, pair; 132, 133; 132, 134; 133, string:'percent identity'; 134, list:[]; 135, pair; 135, 136; 135, 137; 136, string:'e-value'; 137, list:[]; 138, pair; 138, 139; 138, 140; 139, string:'bit score'; 140, list:[]; 141, pair; 141, 142; 141, 143; 142, string:'normalized bit score'; 143, list:[]; 144, pair; 144, 145; 144, 146; 145, string:'alignment length fraction'; 146, list:[]; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:print; 150, argument_list; 150, 151; 150, 156; 151, binary_operator:%; 151, 152; 151, 153; 152, string:'### blast between %s and %s\n'; 153, tuple; 153, 154; 153, 155; 154, identifier:genome; 155, identifier:compare; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:file; 158, identifier:out; 159, for_statement; 159, 160; 159, 161; 159, 166; 160, identifier:id; 161, subscript; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:rbh; 164, identifier:genome; 165, identifier:compare; 166, block; 166, 167; 166, 186; 166, 195; 166, 204; 166, 213; 166, 222; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 175; 169, pattern_list; 169, 170; 169, 171; 169, 172; 169, 173; 169, 174; 170, identifier:pident; 171, identifier:length_fraction; 172, identifier:e; 173, identifier:bit; 174, identifier:norm_bit; 175, subscript; 175, 176; 175, 183; 176, subscript; 176, 177; 176, 182; 177, subscript; 177, 178; 177, 181; 178, subscript; 178, 179; 178, 180; 179, identifier:rbh; 180, identifier:genome; 181, identifier:compare; 182, identifier:id; 183, slice; 183, 184; 183, 185; 184, integer:3; 185, colon; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 193; 188, attribute; 188, 189; 188, 192; 189, subscript; 189, 190; 189, 191; 190, identifier:scores; 191, string:'percent identity'; 192, identifier:append; 193, argument_list; 193, 194; 194, identifier:pident; 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:scores; 200, string:'alignment length fraction'; 201, identifier:append; 202, argument_list; 202, 203; 203, identifier:length_fraction; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 211; 206, attribute; 206, 207; 206, 210; 207, subscript; 207, 208; 207, 209; 208, identifier:scores; 209, string:'e-value'; 210, identifier:append; 211, argument_list; 211, 212; 212, identifier:e; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 220; 215, attribute; 215, 216; 215, 219; 216, subscript; 216, 217; 216, 218; 217, identifier:scores; 218, string:'bit score'; 219, identifier:append; 220, argument_list; 220, 221; 221, identifier:bit; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 229; 224, attribute; 224, 225; 224, 228; 225, subscript; 225, 226; 225, 227; 226, identifier:scores; 227, string:'normalized bit score'; 228, identifier:append; 229, argument_list; 229, 230; 230, identifier:norm_bit; 231, if_statement; 231, 232; 231, 235; 232, comparison_operator:is; 232, 233; 232, 234; 233, identifier:calc_threshold; 234, True; 235, block; 235, 236; 235, 242; 235, 251; 235, 260; 235, 269; 235, 279; 235, 289; 235, 300; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:norms; 239, subscript; 239, 240; 239, 241; 240, identifier:scores; 241, string:'normalized bit score'; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:average; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:numpy; 248, identifier:average; 249, argument_list; 249, 250; 250, identifier:norms; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:std; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:numpy; 257, identifier:std; 258, argument_list; 258, 259; 259, identifier:norms; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:normal_thresh; 263, binary_operator:-; 263, 264; 263, 265; 264, identifier:average; 265, parenthesized_expression; 265, 266; 266, binary_operator:*; 266, 267; 266, 268; 267, identifier:std; 268, identifier:stdevs; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:print; 272, argument_list; 272, 273; 272, 276; 273, binary_operator:%; 273, 274; 273, 275; 274, string:'## average normalized bit score: %s'; 275, identifier:average; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:file; 278, identifier:out; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 282; 281, identifier:print; 282, argument_list; 282, 283; 282, 286; 283, binary_operator:%; 283, 284; 283, 285; 284, string:'## standard deviation of normalized bit scores: %s'; 285, identifier:std; 286, keyword_argument; 286, 287; 286, 288; 287, identifier:file; 288, identifier:out; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 292; 291, identifier:print; 292, argument_list; 292, 293; 292, 297; 293, binary_operator:%; 293, 294; 293, 295; 294, string:'## normalized bit score threshold set to: %s\n'; 295, parenthesized_expression; 295, 296; 296, identifier:normal_thresh; 297, keyword_argument; 297, 298; 297, 299; 298, identifier:file; 299, identifier:out; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 313; 302, pattern_list; 302, 303; 302, 308; 303, subscript; 303, 304; 303, 307; 304, subscript; 304, 305; 304, 306; 305, identifier:norm_threshold; 306, identifier:genome; 307, identifier:compare; 308, subscript; 308, 309; 308, 312; 309, subscript; 309, 310; 309, 311; 310, identifier:norm_threshold; 311, identifier:compare; 312, identifier:genome; 313, expression_list; 313, 314; 313, 315; 314, identifier:normal_thresh; 315, identifier:normal_thresh; 316, for_statement; 316, 317; 316, 318; 316, 319; 317, identifier:score; 318, identifier:scores; 319, block; 319, 320; 319, 331; 319, 358; 319, 359; 319, 360; 319, 361; 320, expression_statement; 320, 321; 321, call; 321, 322; 321, 323; 322, identifier:print; 323, argument_list; 323, 324; 323, 328; 324, binary_operator:%; 324, 325; 324, 326; 325, string:'## %s'; 326, parenthesized_expression; 326, 327; 327, identifier:score; 328, keyword_argument; 328, 329; 328, 330; 329, identifier:file; 330, identifier:out; 331, if_statement; 331, 332; 331, 340; 332, comparison_operator:>; 332, 333; 332, 339; 333, call; 333, 334; 333, 335; 334, identifier:len; 335, argument_list; 335, 336; 336, subscript; 336, 337; 336, 338; 337, identifier:scores; 338, identifier:score; 339, integer:0; 340, block; 340, 341; 341, expression_statement; 341, 342; 342, call; 342, 343; 342, 344; 343, identifier:print; 344, argument_list; 344, 345; 344, 355; 345, binary_operator:%; 345, 346; 345, 347; 346, string:'## average: %s'; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:numpy; 350, identifier:average; 351, argument_list; 351, 352; 352, subscript; 352, 353; 352, 354; 353, identifier:scores; 354, identifier:score; 355, keyword_argument; 355, 356; 355, 357; 356, identifier:file; 357, identifier:out; 358, comment; 359, comment; 360, comment; 361, expression_statement; 361, 362; 362, call; 362, 363; 362, 364; 363, identifier:print; 364, argument_list; 364, 365; 364, 366; 365, string:''; 366, keyword_argument; 366, 367; 366, 368; 367, identifier:file; 368, identifier:out; 369, expression_statement; 369, 370; 370, call; 370, 371; 370, 374; 371, attribute; 371, 372; 371, 373; 372, identifier:out; 373, identifier:close; 374, argument_list; 375, if_statement; 375, 376; 375, 379; 375, 391; 376, comparison_operator:is; 376, 377; 376, 378; 377, identifier:calc_threshold; 378, True; 379, block; 379, 380; 380, return_statement; 380, 381; 381, binary_operator:+; 381, 382; 381, 389; 382, subscript; 382, 383; 382, 384; 383, identifier:thresholds; 384, slice; 384, 385; 384, 386; 384, 387; 385, integer:0; 386, colon; 387, unary_operator:-; 387, 388; 388, integer:1; 389, list:[norm_threshold]; 389, 390; 390, identifier:norm_threshold; 391, else_clause; 391, 392; 392, block; 392, 393; 393, return_statement; 393, 394; 394, identifier:thresholds
def calc_thresholds(rbh, file_name, thresholds = [False, False, False, False], stdevs = 2): """ if thresholds are not specififed, calculate based on the distribution of normalized bit scores """ calc_threshold = thresholds[-1] norm_threshold = {} for pair in itertools.permutations([i for i in rbh], 2): if pair[0] not in norm_threshold: norm_threshold[pair[0]] = {} norm_threshold[pair[0]][pair[1]] = {} out = open(file_name, 'w') print('#### summary of rbh comparisons\n', file=out) comparisons = [] for genome in rbh: for compare in rbh[genome]: pair = ''.join(sorted([genome, compare])) if pair in comparisons: continue comparisons.append(pair) scores = {'percent identity': [], 'e-value': [], 'bit score': [], 'normalized bit score': [], 'alignment length fraction': []} print('### blast between %s and %s\n' % (genome, compare), file=out) for id in rbh[genome][compare]: pident, length_fraction, e, bit, norm_bit = rbh[genome][compare][id][3:] scores['percent identity'].append(pident) scores['alignment length fraction'].append(length_fraction) scores['e-value'].append(e) scores['bit score'].append(bit) scores['normalized bit score'].append(norm_bit) if calc_threshold is True: norms = scores['normalized bit score'] average = numpy.average(norms) std = numpy.std(norms) normal_thresh = average - (std * stdevs) print('## average normalized bit score: %s' % average, file=out) print('## standard deviation of normalized bit scores: %s' % std, file=out) print('## normalized bit score threshold set to: %s\n' % (normal_thresh), file=out) norm_threshold[genome][compare], norm_threshold[compare][genome] = normal_thresh, normal_thresh for score in scores: print('## %s' % (score), file=out) if len(scores[score]) > 0: print('## average: %s' % numpy.average(scores[score]), file=out) # hist = histogram(scores[score], []) # for line in hist: # print >> out, line print('', file=out) out.close() if calc_threshold is True: return thresholds[0:-1] + [norm_threshold] else: return thresholds
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_update_property; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:tree_to_update; 5, identifier:xpath_root; 6, identifier:xpaths; 7, identifier:values; 8, block; 8, 9; 8, 11; 8, 12; 8, 240; 8, 241; 8, 248; 8, 255; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, function_definition; 12, 13; 12, 14; 12, 20; 13, function_name:update_element; 14, parameters; 14, 15; 14, 16; 14, 17; 14, 18; 14, 19; 15, identifier:elem; 16, identifier:idx; 17, identifier:root; 18, identifier:path; 19, identifier:vals; 20, block; 20, 21; 20, 23; 20, 47; 20, 56; 20, 57; 20, 124; 20, 130; 20, 134; 20, 238; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:has_root; 26, call; 26, 27; 26, 28; 27, identifier:bool; 28, argument_list; 28, 29; 29, boolean_operator:and; 29, 30; 29, 41; 30, boolean_operator:and; 30, 31; 30, 32; 31, identifier:root; 32, comparison_operator:>; 32, 33; 32, 37; 33, call; 33, 34; 33, 35; 34, identifier:len; 35, argument_list; 35, 36; 36, identifier:path; 37, call; 37, 38; 37, 39; 38, identifier:len; 39, argument_list; 39, 40; 40, identifier:root; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:path; 44, identifier:startswith; 45, argument_list; 45, 46; 46, identifier:root; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, pattern_list; 49, 50; 49, 51; 50, identifier:path; 51, identifier:attr; 52, call; 52, 53; 52, 54; 53, identifier:get_xpath_tuple; 54, argument_list; 54, 55; 55, identifier:path; 56, comment; 57, if_statement; 57, 58; 57, 59; 57, 77; 57, 92; 58, identifier:attr; 59, block; 59, 60; 59, 69; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:removed; 63, list:[get_element(elem, path)]; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:get_element; 66, argument_list; 66, 67; 66, 68; 67, identifier:elem; 68, identifier:path; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:remove_element_attributes; 72, argument_list; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:removed; 75, integer:0; 76, identifier:attr; 77, elif_clause; 77, 78; 77, 80; 78, not_operator; 78, 79; 79, identifier:has_root; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:removed; 84, call; 84, 85; 84, 86; 85, identifier:wrap_value; 86, argument_list; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:remove_element; 89, argument_list; 89, 90; 89, 91; 90, identifier:elem; 91, identifier:path; 92, else_clause; 92, 93; 93, block; 93, 94; 93, 102; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:path; 97, call; 97, 98; 97, 99; 98, identifier:get_xpath_branch; 99, argument_list; 99, 100; 99, 101; 100, identifier:root; 101, identifier:path; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:removed; 105, conditional_expression:if; 105, 106; 105, 107; 105, 110; 106, list:[]; 107, comparison_operator:!=; 107, 108; 107, 109; 108, identifier:idx; 109, integer:0; 110, list_comprehension; 110, 111; 110, 117; 111, call; 111, 112; 111, 113; 112, identifier:remove_element; 113, argument_list; 113, 114; 113, 115; 113, 116; 114, identifier:e; 115, identifier:path; 116, True; 117, for_in_clause; 117, 118; 117, 119; 118, identifier:e; 119, call; 119, 120; 119, 121; 120, identifier:get_elements; 121, argument_list; 121, 122; 121, 123; 122, identifier:elem; 123, identifier:root; 124, if_statement; 124, 125; 124, 127; 125, not_operator; 125, 126; 126, identifier:vals; 127, block; 127, 128; 128, return_statement; 128, 129; 129, identifier:removed; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:items; 133, list:[]; 134, for_statement; 134, 135; 134, 138; 134, 145; 135, pattern_list; 135, 136; 135, 137; 136, identifier:i; 137, identifier:val; 138, call; 138, 139; 138, 140; 139, identifier:enumerate; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:wrap_value; 143, argument_list; 143, 144; 144, identifier:vals; 145, block; 145, 146; 145, 150; 145, 165; 145, 182; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:elem_to_update; 149, identifier:elem; 150, if_statement; 150, 151; 150, 152; 151, identifier:has_root; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:elem_to_update; 156, call; 156, 157; 156, 158; 157, identifier:insert_element; 158, argument_list; 158, 159; 158, 160; 158, 164; 159, identifier:elem; 160, parenthesized_expression; 160, 161; 161, binary_operator:+; 161, 162; 161, 163; 162, identifier:i; 163, identifier:idx; 164, identifier:root; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:val; 168, conditional_expression:if; 168, 169; 168, 175; 168, 181; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:val; 172, identifier:decode; 173, argument_list; 173, 174; 174, string:'utf-8'; 175, not_operator; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:isinstance; 178, argument_list; 178, 179; 178, 180; 179, identifier:val; 180, identifier:string_types; 181, identifier:val; 182, if_statement; 182, 183; 182, 185; 182, 199; 182, 219; 183, not_operator; 183, 184; 184, identifier:attr; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:items; 190, identifier:append; 191, argument_list; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:insert_element; 194, argument_list; 194, 195; 194, 196; 194, 197; 194, 198; 195, identifier:elem_to_update; 196, identifier:i; 197, identifier:path; 198, identifier:val; 199, elif_clause; 199, 200; 199, 201; 200, identifier:path; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:items; 206, identifier:append; 207, argument_list; 207, 208; 208, call; 208, 209; 208, 210; 209, identifier:insert_element; 210, argument_list; 210, 211; 210, 212; 210, 213; 210, 214; 211, identifier:elem_to_update; 212, identifier:i; 213, identifier:path; 214, dictionary_splat; 214, 215; 215, dictionary; 215, 216; 216, pair; 216, 217; 216, 218; 217, identifier:attr; 218, identifier:val; 219, else_clause; 219, 220; 220, block; 220, 221; 220, 231; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 224; 223, identifier:set_element_attributes; 224, argument_list; 224, 225; 224, 226; 225, identifier:elem_to_update; 226, dictionary_splat; 226, 227; 227, dictionary; 227, 228; 228, pair; 228, 229; 228, 230; 229, identifier:attr; 230, identifier:val; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:items; 235, identifier:append; 236, argument_list; 236, 237; 237, identifier:elem_to_update; 238, return_statement; 238, 239; 239, identifier:items; 240, comment; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:xpaths; 244, call; 244, 245; 244, 246; 245, identifier:reduce_value; 246, argument_list; 246, 247; 247, identifier:xpaths; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:values; 251, call; 251, 252; 251, 253; 252, identifier:filter_empty; 253, argument_list; 253, 254; 254, identifier:values; 255, if_statement; 255, 256; 255, 261; 255, 271; 256, call; 256, 257; 256, 258; 257, identifier:isinstance; 258, argument_list; 258, 259; 258, 260; 259, identifier:xpaths; 260, identifier:string_types; 261, block; 261, 262; 262, return_statement; 262, 263; 263, call; 263, 264; 263, 265; 264, identifier:update_element; 265, argument_list; 265, 266; 265, 267; 265, 268; 265, 269; 265, 270; 266, identifier:tree_to_update; 267, integer:0; 268, identifier:xpath_root; 269, identifier:xpaths; 270, identifier:values; 271, else_clause; 271, 272; 272, block; 272, 273; 272, 277; 272, 309; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:each; 276, list:[]; 277, for_statement; 277, 278; 277, 281; 277, 285; 278, pattern_list; 278, 279; 278, 280; 279, identifier:index; 280, identifier:xpath; 281, call; 281, 282; 281, 283; 282, identifier:enumerate; 283, argument_list; 283, 284; 284, identifier:xpaths; 285, block; 285, 286; 285, 295; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:value; 289, conditional_expression:if; 289, 290; 289, 293; 289, 294; 290, subscript; 290, 291; 290, 292; 291, identifier:values; 292, identifier:index; 293, identifier:values; 294, None; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:each; 299, identifier:extend; 300, argument_list; 300, 301; 301, call; 301, 302; 301, 303; 302, identifier:update_element; 303, argument_list; 303, 304; 303, 305; 303, 306; 303, 307; 303, 308; 304, identifier:tree_to_update; 305, identifier:index; 306, identifier:xpath_root; 307, identifier:xpath; 308, identifier:value; 309, return_statement; 309, 310; 310, identifier:each
def _update_property(tree_to_update, xpath_root, xpaths, values): """ Default update operation for a single parser property. If xpaths contains one xpath, then one element per value will be inserted at that location in the tree_to_update; otherwise, the number of values must match the number of xpaths. """ # Inner function to update a specific XPATH with the values provided def update_element(elem, idx, root, path, vals): """ Internal helper function to encapsulate single item update """ has_root = bool(root and len(path) > len(root) and path.startswith(root)) path, attr = get_xpath_tuple(path) # 'path/@attr' to ('path', 'attr') if attr: removed = [get_element(elem, path)] remove_element_attributes(removed[0], attr) elif not has_root: removed = wrap_value(remove_element(elem, path)) else: path = get_xpath_branch(root, path) removed = [] if idx != 0 else [remove_element(e, path, True) for e in get_elements(elem, root)] if not vals: return removed items = [] for i, val in enumerate(wrap_value(vals)): elem_to_update = elem if has_root: elem_to_update = insert_element(elem, (i + idx), root) val = val.decode('utf-8') if not isinstance(val, string_types) else val if not attr: items.append(insert_element(elem_to_update, i, path, val)) elif path: items.append(insert_element(elem_to_update, i, path, **{attr: val})) else: set_element_attributes(elem_to_update, **{attr: val}) items.append(elem_to_update) return items # Code to update each of the XPATHs with each of the values xpaths = reduce_value(xpaths) values = filter_empty(values) if isinstance(xpaths, string_types): return update_element(tree_to_update, 0, xpath_root, xpaths, values) else: each = [] for index, xpath in enumerate(xpaths): value = values[index] if values else None each.extend(update_element(tree_to_update, index, xpath_root, xpath, value)) return each
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:validate_complex_list; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:prop; 5, identifier:value; 6, default_parameter; 6, 7; 6, 8; 7, identifier:xpath_map; 8, None; 9, block; 9, 10; 9, 12; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:value; 15, None; 16, block; 16, 17; 16, 26; 16, 48; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:validate_type; 20, argument_list; 20, 21; 20, 22; 20, 23; 21, identifier:prop; 22, identifier:value; 23, tuple; 23, 24; 23, 25; 24, identifier:dict; 25, identifier:list; 26, if_statement; 26, 27; 26, 30; 26, 37; 27, comparison_operator:in; 27, 28; 27, 29; 28, identifier:prop; 29, identifier:_complex_definitions; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:complex_keys; 34, subscript; 34, 35; 34, 36; 35, identifier:_complex_definitions; 36, identifier:prop; 37, else_clause; 37, 38; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:complex_keys; 42, conditional_expression:if; 42, 43; 42, 44; 42, 47; 43, dictionary; 44, comparison_operator:is; 44, 45; 44, 46; 45, identifier:xpath_map; 46, None; 47, identifier:xpath_map; 48, for_statement; 48, 49; 48, 52; 48, 59; 49, pattern_list; 49, 50; 49, 51; 50, identifier:idx; 51, identifier:complex_struct; 52, call; 52, 53; 52, 54; 53, identifier:enumerate; 54, argument_list; 54, 55; 55, call; 55, 56; 55, 57; 56, identifier:wrap_value; 57, argument_list; 57, 58; 58, identifier:value; 59, block; 59, 60; 59, 73; 59, 80; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:cs_idx; 63, binary_operator:+; 63, 64; 63, 72; 64, binary_operator:+; 64, 65; 64, 68; 65, binary_operator:+; 65, 66; 65, 67; 66, identifier:prop; 67, string:'['; 68, call; 68, 69; 68, 70; 69, identifier:str; 70, argument_list; 70, 71; 71, identifier:idx; 72, string:']'; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:validate_type; 76, argument_list; 76, 77; 76, 78; 76, 79; 77, identifier:cs_idx; 78, identifier:complex_struct; 79, identifier:dict; 80, for_statement; 80, 81; 80, 84; 80, 88; 81, pattern_list; 81, 82; 81, 83; 82, identifier:cs_prop; 83, identifier:cs_val; 84, call; 84, 85; 84, 86; 85, identifier:iteritems; 86, argument_list; 86, 87; 87, identifier:complex_struct; 88, block; 88, 89; 88, 100; 88, 124; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:cs_key; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, string:'.'; 95, identifier:join; 96, argument_list; 96, 97; 97, tuple; 97, 98; 97, 99; 98, identifier:cs_idx; 99, identifier:cs_prop; 100, if_statement; 100, 101; 100, 104; 101, comparison_operator:not; 101, 102; 101, 103; 102, identifier:cs_prop; 103, identifier:complex_keys; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:_validation_error; 108, argument_list; 108, 109; 108, 110; 108, 111; 108, 112; 109, identifier:prop; 110, None; 111, identifier:value; 112, parenthesized_expression; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, string:'keys: {0}'; 116, identifier:format; 117, argument_list; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, string:','; 121, identifier:join; 122, argument_list; 122, 123; 123, identifier:complex_keys; 124, if_statement; 124, 125; 124, 131; 124, 141; 125, not_operator; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:isinstance; 128, argument_list; 128, 129; 128, 130; 129, identifier:cs_val; 130, identifier:list; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:validate_type; 135, argument_list; 135, 136; 135, 137; 135, 138; 136, identifier:cs_key; 137, identifier:cs_val; 138, tuple; 138, 139; 138, 140; 139, identifier:string_types; 140, identifier:list; 141, else_clause; 141, 142; 142, block; 142, 143; 143, for_statement; 143, 144; 143, 147; 143, 151; 144, pattern_list; 144, 145; 144, 146; 145, identifier:list_idx; 146, identifier:list_val; 147, call; 147, 148; 147, 149; 148, identifier:enumerate; 149, argument_list; 149, 150; 150, identifier:cs_val; 151, block; 151, 152; 151, 165; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:list_prop; 155, binary_operator:+; 155, 156; 155, 164; 156, binary_operator:+; 156, 157; 156, 160; 157, binary_operator:+; 157, 158; 157, 159; 158, identifier:cs_key; 159, string:'['; 160, call; 160, 161; 160, 162; 161, identifier:str; 162, argument_list; 162, 163; 163, identifier:list_idx; 164, string:']'; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:validate_type; 168, argument_list; 168, 169; 168, 170; 168, 171; 169, identifier:list_prop; 170, identifier:list_val; 171, identifier:string_types
def validate_complex_list(prop, value, xpath_map=None): """ Default validation for Attribute Details data structure """ if value is not None: validate_type(prop, value, (dict, list)) if prop in _complex_definitions: complex_keys = _complex_definitions[prop] else: complex_keys = {} if xpath_map is None else xpath_map for idx, complex_struct in enumerate(wrap_value(value)): cs_idx = prop + '[' + str(idx) + ']' validate_type(cs_idx, complex_struct, dict) for cs_prop, cs_val in iteritems(complex_struct): cs_key = '.'.join((cs_idx, cs_prop)) if cs_prop not in complex_keys: _validation_error(prop, None, value, ('keys: {0}'.format(','.join(complex_keys)))) if not isinstance(cs_val, list): validate_type(cs_key, cs_val, (string_types, list)) else: for list_idx, list_val in enumerate(cs_val): list_prop = cs_key + '[' + str(list_idx) + ']' validate_type(list_prop, list_val, string_types)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:validate_dates; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:prop; 5, identifier:value; 6, default_parameter; 6, 7; 6, 8; 7, identifier:xpath_map; 8, None; 9, block; 9, 10; 9, 12; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:value; 15, None; 16, block; 16, 17; 16, 24; 16, 31; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:validate_type; 20, argument_list; 20, 21; 20, 22; 20, 23; 21, identifier:prop; 22, identifier:value; 23, identifier:dict; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:date_keys; 27, call; 27, 28; 27, 29; 28, identifier:set; 29, argument_list; 29, 30; 30, identifier:value; 31, if_statement; 31, 32; 31, 33; 32, identifier:date_keys; 33, block; 33, 34; 33, 86; 33, 92; 33, 105; 33, 111; 33, 118; 33, 125; 33, 142; 33, 159; 33, 176; 33, 193; 34, if_statement; 34, 35; 34, 42; 35, boolean_operator:or; 35, 36; 35, 39; 36, comparison_operator:not; 36, 37; 36, 38; 37, identifier:DATE_TYPE; 38, identifier:date_keys; 39, comparison_operator:not; 39, 40; 39, 41; 40, identifier:DATE_VALUES; 41, identifier:date_keys; 42, block; 42, 43; 42, 67; 43, if_statement; 43, 44; 43, 47; 43, 54; 44, comparison_operator:in; 44, 45; 44, 46; 45, identifier:prop; 46, identifier:_complex_definitions; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:complex_keys; 51, subscript; 51, 52; 51, 53; 52, identifier:_complex_definitions; 53, identifier:prop; 54, else_clause; 54, 55; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:complex_keys; 59, conditional_expression:if; 59, 60; 59, 63; 59, 66; 60, subscript; 60, 61; 60, 62; 61, identifier:_complex_definitions; 62, identifier:DATES; 63, comparison_operator:is; 63, 64; 63, 65; 64, identifier:xpath_map; 65, None; 66, identifier:xpath_map; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:_validation_error; 70, argument_list; 70, 71; 70, 72; 70, 73; 70, 74; 71, identifier:prop; 72, None; 73, identifier:value; 74, parenthesized_expression; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, string:'keys: {0}'; 78, identifier:format; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, string:','; 83, identifier:join; 84, argument_list; 84, 85; 85, identifier:complex_keys; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:date_type; 89, subscript; 89, 90; 89, 91; 90, identifier:value; 91, identifier:DATE_TYPE; 92, if_statement; 92, 93; 92, 96; 93, comparison_operator:not; 93, 94; 93, 95; 94, identifier:date_type; 95, identifier:DATE_TYPES; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:_validation_error; 100, argument_list; 100, 101; 100, 102; 100, 103; 100, 104; 101, string:'dates.type'; 102, None; 103, identifier:date_type; 104, identifier:DATE_TYPES; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:date_vals; 108, subscript; 108, 109; 108, 110; 109, identifier:value; 110, identifier:DATE_VALUES; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:validate_type; 114, argument_list; 114, 115; 114, 116; 114, 117; 115, string:'dates.values'; 116, identifier:date_vals; 117, identifier:list; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:dates_len; 121, call; 121, 122; 121, 123; 122, identifier:len; 123, argument_list; 123, 124; 124, identifier:date_vals; 125, if_statement; 125, 126; 125, 133; 126, boolean_operator:and; 126, 127; 126, 130; 127, comparison_operator:==; 127, 128; 127, 129; 128, identifier:date_type; 129, identifier:DATE_TYPE_MISSING; 130, comparison_operator:!=; 130, 131; 130, 132; 131, identifier:dates_len; 132, integer:0; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:_validation_error; 137, argument_list; 137, 138; 137, 139; 137, 140; 137, 141; 138, string:'len(dates.values)'; 139, None; 140, identifier:dates_len; 141, integer:0; 142, if_statement; 142, 143; 142, 150; 143, boolean_operator:and; 143, 144; 143, 147; 144, comparison_operator:==; 144, 145; 144, 146; 145, identifier:date_type; 146, identifier:DATE_TYPE_SINGLE; 147, comparison_operator:!=; 147, 148; 147, 149; 148, identifier:dates_len; 149, integer:1; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:_validation_error; 154, argument_list; 154, 155; 154, 156; 154, 157; 154, 158; 155, string:'len(dates.values)'; 156, None; 157, identifier:dates_len; 158, integer:1; 159, if_statement; 159, 160; 159, 167; 160, boolean_operator:and; 160, 161; 160, 164; 161, comparison_operator:==; 161, 162; 161, 163; 162, identifier:date_type; 163, identifier:DATE_TYPE_RANGE; 164, comparison_operator:!=; 164, 165; 164, 166; 165, identifier:dates_len; 166, integer:2; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:_validation_error; 171, argument_list; 171, 172; 171, 173; 171, 174; 171, 175; 172, string:'len(dates.values)'; 173, None; 174, identifier:dates_len; 175, integer:2; 176, if_statement; 176, 177; 176, 184; 177, boolean_operator:and; 177, 178; 177, 181; 178, comparison_operator:==; 178, 179; 178, 180; 179, identifier:date_type; 180, identifier:DATE_TYPE_MULTIPLE; 181, comparison_operator:<; 181, 182; 181, 183; 182, identifier:dates_len; 183, integer:2; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:_validation_error; 188, argument_list; 188, 189; 188, 190; 188, 191; 188, 192; 189, string:'len(dates.values)'; 190, None; 191, identifier:dates_len; 192, string:'at least two'; 193, for_statement; 193, 194; 193, 197; 193, 201; 194, pattern_list; 194, 195; 194, 196; 195, identifier:idx; 196, identifier:date; 197, call; 197, 198; 197, 199; 198, identifier:enumerate; 199, argument_list; 199, 200; 200, identifier:date_vals; 201, block; 201, 202; 201, 213; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:date_key; 205, binary_operator:+; 205, 206; 205, 212; 206, binary_operator:+; 206, 207; 206, 208; 207, string:'dates.value['; 208, call; 208, 209; 208, 210; 209, identifier:str; 210, argument_list; 210, 211; 211, identifier:idx; 212, string:']'; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 216; 215, identifier:validate_type; 216, argument_list; 216, 217; 216, 218; 216, 219; 217, identifier:date_key; 218, identifier:date; 219, identifier:string_types
def validate_dates(prop, value, xpath_map=None): """ Default validation for Date Types data structure """ if value is not None: validate_type(prop, value, dict) date_keys = set(value) if date_keys: if DATE_TYPE not in date_keys or DATE_VALUES not in date_keys: if prop in _complex_definitions: complex_keys = _complex_definitions[prop] else: complex_keys = _complex_definitions[DATES] if xpath_map is None else xpath_map _validation_error(prop, None, value, ('keys: {0}'.format(','.join(complex_keys)))) date_type = value[DATE_TYPE] if date_type not in DATE_TYPES: _validation_error('dates.type', None, date_type, DATE_TYPES) date_vals = value[DATE_VALUES] validate_type('dates.values', date_vals, list) dates_len = len(date_vals) if date_type == DATE_TYPE_MISSING and dates_len != 0: _validation_error('len(dates.values)', None, dates_len, 0) if date_type == DATE_TYPE_SINGLE and dates_len != 1: _validation_error('len(dates.values)', None, dates_len, 1) if date_type == DATE_TYPE_RANGE and dates_len != 2: _validation_error('len(dates.values)', None, dates_len, 2) if date_type == DATE_TYPE_MULTIPLE and dates_len < 2: _validation_error('len(dates.values)', None, dates_len, 'at least two') for idx, date in enumerate(date_vals): date_key = 'dates.value[' + str(idx) + ']' validate_type(date_key, date, string_types)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:getCharacterSet; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 16; 5, 20; 5, 24; 5, 245; 6, expression_statement; 6, 7; 7, string:'''Get a character set with individual members or ranges. Current index is on '[', the start of the character set. '''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:chars; 11, string:u''; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:c; 15, None; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:cnt; 19, integer:1; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:start; 23, integer:0; 24, while_statement; 24, 25; 24, 26; 25, True; 26, block; 26, 27; 26, 31; 26, 39; 26, 40; 26, 41; 26, 42; 26, 43; 26, 44; 26, 45; 26, 46; 26, 177; 26, 213; 26, 240; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:escaped_slash; 30, False; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:c; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:next; 38, argument_list; 39, comment; 40, comment; 41, comment; 42, comment; 43, comment; 44, comment; 45, comment; 46, if_statement; 46, 47; 46, 59; 46, 108; 46, 164; 47, boolean_operator:and; 47, 48; 47, 55; 48, comparison_operator:==; 48, 49; 48, 54; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:lookahead; 53, argument_list; 54, string:u'-'; 55, not_operator; 55, 56; 56, comparison_operator:==; 56, 57; 56, 58; 57, identifier:c; 58, string:u'\\'; 59, block; 59, 60; 59, 64; 59, 70; 59, 71; 59, 79; 59, 80; 59, 98; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:f; 63, identifier:c; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:next; 69, argument_list; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:c; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:next; 78, argument_list; 79, comment; 80, if_statement; 80, 81; 80, 90; 81, boolean_operator:or; 81, 82; 81, 84; 82, not_operator; 82, 83; 83, identifier:c; 84, parenthesized_expression; 84, 85; 85, comparison_operator:in; 85, 86; 85, 87; 86, identifier:c; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:meta_chars; 90, block; 90, 91; 91, raise_statement; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:StringGenerator; 95, identifier:SyntaxError; 96, argument_list; 96, 97; 97, string:u"unexpected end of class range"; 98, expression_statement; 98, 99; 99, augmented_assignment:+=; 99, 100; 99, 101; 100, identifier:chars; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:getCharacterRange; 105, argument_list; 105, 106; 105, 107; 106, identifier:f; 107, identifier:c; 108, elif_clause; 108, 109; 108, 112; 109, comparison_operator:==; 109, 110; 109, 111; 110, identifier:c; 111, string:u'\\'; 112, block; 112, 113; 113, if_statement; 113, 114; 113, 123; 113, 137; 114, comparison_operator:in; 114, 115; 114, 120; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:lookahead; 119, argument_list; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:meta_chars; 123, block; 123, 124; 123, 132; 123, 136; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:c; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:next; 131, argument_list; 132, expression_statement; 132, 133; 133, augmented_assignment:+=; 133, 134; 133, 135; 134, identifier:chars; 135, identifier:c; 136, continue_statement; 137, elif_clause; 137, 138; 137, 147; 138, comparison_operator:in; 138, 139; 138, 144; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:lookahead; 143, argument_list; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:string_code; 147, block; 147, 148; 147, 156; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:c; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:next; 155, argument_list; 156, expression_statement; 156, 157; 157, augmented_assignment:+=; 157, 158; 157, 159; 158, identifier:chars; 159, subscript; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:string_code; 163, identifier:c; 164, elif_clause; 164, 165; 164, 172; 165, boolean_operator:and; 165, 166; 165, 167; 166, identifier:c; 167, comparison_operator:not; 167, 168; 167, 169; 168, identifier:c; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:meta_chars; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, augmented_assignment:+=; 174, 175; 174, 176; 175, identifier:chars; 176, identifier:c; 177, if_statement; 177, 178; 177, 181; 178, comparison_operator:==; 178, 179; 178, 180; 179, identifier:c; 180, string:u']'; 181, block; 181, 182; 181, 212; 182, if_statement; 182, 183; 182, 190; 182, 201; 183, comparison_operator:==; 183, 184; 183, 189; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:lookahead; 188, argument_list; 189, string:u'{'; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 196; 193, list_pattern; 193, 194; 193, 195; 194, identifier:start; 195, identifier:cnt; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:getQuantifier; 200, argument_list; 201, else_clause; 201, 202; 202, block; 202, 203; 202, 208; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:start; 206, unary_operator:-; 206, 207; 207, integer:1; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:cnt; 211, integer:1; 212, break_statement; 213, if_statement; 213, 214; 213, 230; 214, boolean_operator:and; 214, 215; 214, 222; 215, boolean_operator:and; 215, 216; 215, 217; 216, identifier:c; 217, comparison_operator:in; 217, 218; 217, 219; 218, identifier:c; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:meta_chars; 222, not_operator; 222, 223; 223, comparison_operator:==; 223, 224; 223, 229; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:self; 227, identifier:last; 228, argument_list; 229, string:u"\\"; 230, block; 230, 231; 231, raise_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:StringGenerator; 235, identifier:SyntaxError; 236, argument_list; 236, 237; 237, binary_operator:%; 237, 238; 237, 239; 238, string:u"Un-escaped character in class definition: %s"; 239, identifier:c; 240, if_statement; 240, 241; 240, 243; 241, not_operator; 241, 242; 242, identifier:c; 243, block; 243, 244; 244, break_statement; 245, return_statement; 245, 246; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:StringGenerator; 249, identifier:CharacterSet; 250, argument_list; 250, 251; 250, 252; 250, 253; 251, identifier:chars; 252, identifier:start; 253, identifier:cnt
def getCharacterSet(self): '''Get a character set with individual members or ranges. Current index is on '[', the start of the character set. ''' chars = u'' c = None cnt = 1 start = 0 while True: escaped_slash = False c = self.next() # print "pattern : ", self.pattern # print "C : ", c # print "Slash : ", c == u'\\' # print 'chars : ', chars # print 'index : ', self.index # print 'last : ', self.last() # print 'lookahead : ', self.lookahead() if self.lookahead() == u'-' and not c == u'\\': f = c self.next() # skip hyphen c = self.next() # get far range if not c or (c in self.meta_chars): raise StringGenerator.SyntaxError(u"unexpected end of class range") chars += self.getCharacterRange(f, c) elif c == u'\\': if self.lookahead() in self.meta_chars: c = self.next() chars += c continue elif self.lookahead() in self.string_code: c = self.next() chars += self.string_code[c] elif c and c not in self.meta_chars: chars += c if c == u']': if self.lookahead() == u'{': [start, cnt] = self.getQuantifier() else: start = -1 cnt = 1 break if c and c in self.meta_chars and not self.last() == u"\\": raise StringGenerator.SyntaxError(u"Un-escaped character in class definition: %s" % c) if not c: break return StringGenerator.CharacterSet(chars, start, cnt)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:getSequence; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:level; 7, integer:0; 8, block; 8, 9; 8, 11; 8, 15; 8, 19; 8, 23; 8, 27; 8, 31; 8, 348; 8, 349; 8, 361; 8, 377; 9, expression_statement; 9, 10; 10, string:'''Get a sequence of nodes.'''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:seq; 14, list:[]; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:op; 18, string:''; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:left_operand; 22, None; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:right_operand; 26, None; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:sequence_closed; 30, False; 31, while_statement; 31, 32; 31, 33; 32, True; 33, block; 33, 34; 33, 42; 33, 47; 33, 242; 33, 243; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:c; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:next; 41, argument_list; 42, if_statement; 42, 43; 42, 45; 43, not_operator; 43, 44; 44, identifier:c; 45, block; 45, 46; 46, break_statement; 47, if_statement; 47, 48; 47, 55; 47, 67; 47, 93; 47, 118; 47, 146; 47, 179; 47, 197; 47, 215; 48, boolean_operator:and; 48, 49; 48, 50; 49, identifier:c; 50, comparison_operator:not; 50, 51; 50, 52; 51, identifier:c; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:meta_chars; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:seq; 60, identifier:append; 61, argument_list; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:getLiteral; 66, argument_list; 67, elif_clause; 67, 68; 67, 81; 68, boolean_operator:and; 68, 69; 68, 74; 69, boolean_operator:and; 69, 70; 69, 71; 70, identifier:c; 71, comparison_operator:==; 71, 72; 71, 73; 72, identifier:c; 73, string:u'$'; 74, comparison_operator:==; 74, 75; 74, 80; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:lookahead; 79, argument_list; 80, string:u'{'; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:seq; 86, identifier:append; 87, argument_list; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:getSource; 92, argument_list; 93, elif_clause; 93, 94; 93, 106; 94, boolean_operator:and; 94, 95; 94, 98; 95, comparison_operator:==; 95, 96; 95, 97; 96, identifier:c; 97, string:u'['; 98, not_operator; 98, 99; 99, comparison_operator:==; 99, 100; 99, 105; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:last; 104, argument_list; 105, string:u'\\'; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:seq; 111, identifier:append; 112, argument_list; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:getCharacterSet; 117, argument_list; 118, elif_clause; 118, 119; 118, 131; 119, boolean_operator:and; 119, 120; 119, 123; 120, comparison_operator:==; 120, 121; 120, 122; 121, identifier:c; 122, string:u'('; 123, not_operator; 123, 124; 124, comparison_operator:==; 124, 125; 124, 130; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:last; 129, argument_list; 130, string:u'\\'; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:seq; 136, identifier:append; 137, argument_list; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:getSequence; 142, argument_list; 142, 143; 143, binary_operator:+; 143, 144; 143, 145; 144, identifier:level; 145, integer:1; 146, elif_clause; 146, 147; 146, 159; 146, 160; 147, boolean_operator:and; 147, 148; 147, 151; 148, comparison_operator:==; 148, 149; 148, 150; 149, identifier:c; 150, string:u')'; 151, not_operator; 151, 152; 152, comparison_operator:==; 152, 153; 152, 158; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:last; 157, argument_list; 158, string:u'\\'; 159, comment; 160, block; 160, 161; 160, 174; 160, 178; 161, if_statement; 161, 162; 161, 165; 161, 166; 162, comparison_operator:==; 162, 163; 162, 164; 163, identifier:level; 164, integer:0; 165, comment; 166, block; 166, 167; 167, raise_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:StringGenerator; 171, identifier:SyntaxError; 172, argument_list; 172, 173; 173, string:u"Extra closing parenthesis"; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:sequence_closed; 177, True; 178, break_statement; 179, elif_clause; 179, 180; 179, 192; 180, boolean_operator:and; 180, 181; 180, 184; 181, comparison_operator:==; 181, 182; 181, 183; 182, identifier:c; 183, string:u'|'; 184, not_operator; 184, 185; 185, comparison_operator:==; 185, 186; 185, 191; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:last; 190, argument_list; 191, string:u'\\'; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:op; 196, identifier:c; 197, elif_clause; 197, 198; 197, 210; 198, boolean_operator:and; 198, 199; 198, 202; 199, comparison_operator:==; 199, 200; 199, 201; 200, identifier:c; 201, string:u'&'; 202, not_operator; 202, 203; 203, comparison_operator:==; 203, 204; 203, 209; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:last; 208, argument_list; 209, string:u'\\'; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:op; 214, identifier:c; 215, else_clause; 215, 216; 216, block; 216, 217; 217, if_statement; 217, 218; 217, 232; 218, boolean_operator:and; 218, 219; 218, 224; 219, comparison_operator:in; 219, 220; 219, 221; 220, identifier:c; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:meta_chars; 224, not_operator; 224, 225; 225, comparison_operator:==; 225, 226; 225, 231; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:last; 230, argument_list; 231, string:u"\\"; 232, block; 232, 233; 233, raise_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:StringGenerator; 237, identifier:SyntaxError; 238, argument_list; 238, 239; 239, binary_operator:%; 239, 240; 239, 241; 240, string:u"Un-escaped special character: %s"; 241, identifier:c; 242, comment; 243, if_statement; 243, 244; 243, 248; 243, 277; 244, boolean_operator:and; 244, 245; 244, 246; 245, identifier:op; 246, not_operator; 246, 247; 247, identifier:left_operand; 248, block; 248, 249; 248, 269; 249, if_statement; 249, 250; 249, 259; 250, boolean_operator:or; 250, 251; 250, 253; 251, not_operator; 251, 252; 252, identifier:seq; 253, comparison_operator:<; 253, 254; 253, 258; 254, call; 254, 255; 254, 256; 255, identifier:len; 256, argument_list; 256, 257; 257, identifier:seq; 258, integer:1; 259, block; 259, 260; 260, raise_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:StringGenerator; 264, identifier:SyntaxError; 265, argument_list; 265, 266; 266, binary_operator:%; 266, 267; 266, 268; 267, string:u"Operator: %s with no left operand"; 268, identifier:op; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:left_operand; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:seq; 275, identifier:pop; 276, argument_list; 277, elif_clause; 277, 278; 277, 288; 278, boolean_operator:and; 278, 279; 278, 287; 279, boolean_operator:and; 279, 280; 279, 281; 280, identifier:op; 281, comparison_operator:>=; 281, 282; 281, 286; 282, call; 282, 283; 282, 284; 283, identifier:len; 284, argument_list; 284, 285; 285, identifier:seq; 286, integer:1; 287, identifier:left_operand; 288, block; 288, 289; 288, 297; 288, 298; 288, 336; 288, 340; 288, 344; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:right_operand; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:seq; 295, identifier:pop; 296, argument_list; 297, comment; 298, if_statement; 298, 299; 298, 302; 298, 317; 299, comparison_operator:==; 299, 300; 299, 301; 300, identifier:op; 301, string:u'|'; 302, block; 302, 303; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:seq; 307, identifier:append; 308, argument_list; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:StringGenerator; 312, identifier:SequenceOR; 313, argument_list; 313, 314; 314, list:[left_operand, right_operand]; 314, 315; 314, 316; 315, identifier:left_operand; 316, identifier:right_operand; 317, elif_clause; 317, 318; 317, 321; 318, comparison_operator:==; 318, 319; 318, 320; 319, identifier:op; 320, string:u'&'; 321, block; 321, 322; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:seq; 326, identifier:append; 327, argument_list; 327, 328; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:StringGenerator; 331, identifier:SequenceAND; 332, argument_list; 332, 333; 333, list:[left_operand, right_operand]; 333, 334; 333, 335; 334, identifier:left_operand; 335, identifier:right_operand; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 339; 338, identifier:op; 339, string:u''; 340, expression_statement; 340, 341; 341, assignment; 341, 342; 341, 343; 342, identifier:left_operand; 343, None; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 347; 346, identifier:right_operand; 347, None; 348, comment; 349, if_statement; 349, 350; 349, 351; 350, identifier:op; 351, block; 351, 352; 352, raise_statement; 352, 353; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:StringGenerator; 356, identifier:SyntaxError; 357, argument_list; 357, 358; 358, binary_operator:%; 358, 359; 358, 360; 359, string:u"Operator: %s with no right operand"; 360, identifier:op; 361, if_statement; 361, 362; 361, 368; 361, 369; 362, boolean_operator:and; 362, 363; 362, 366; 363, comparison_operator:>; 363, 364; 363, 365; 364, identifier:level; 365, integer:0; 366, not_operator; 366, 367; 367, identifier:sequence_closed; 368, comment; 369, block; 369, 370; 370, raise_statement; 370, 371; 371, call; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:StringGenerator; 374, identifier:SyntaxError; 375, argument_list; 375, 376; 376, string:u"Missing closing parenthesis"; 377, return_statement; 377, 378; 378, call; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:StringGenerator; 381, identifier:Sequence; 382, argument_list; 382, 383; 383, identifier:seq
def getSequence(self, level=0): '''Get a sequence of nodes.''' seq = [] op = '' left_operand = None right_operand = None sequence_closed = False while True: c = self.next() if not c: break if c and c not in self.meta_chars: seq.append(self.getLiteral()) elif c and c == u'$' and self.lookahead() == u'{': seq.append(self.getSource()) elif c == u'[' and not self.last() == u'\\': seq.append(self.getCharacterSet()) elif c == u'(' and not self.last() == u'\\': seq.append(self.getSequence(level + 1)) elif c == u')' and not self.last() == u'\\': # end of this sequence if level == 0: # there should be no parens here raise StringGenerator.SyntaxError(u"Extra closing parenthesis") sequence_closed = True break elif c == u'|' and not self.last() == u'\\': op = c elif c == u'&' and not self.last() == u'\\': op = c else: if c in self.meta_chars and not self.last() == u"\\": raise StringGenerator.SyntaxError(u"Un-escaped special character: %s" % c) #print( op,len(seq) ) if op and not left_operand: if not seq or len(seq) < 1: raise StringGenerator.SyntaxError(u"Operator: %s with no left operand" % op) left_operand = seq.pop() elif op and len(seq) >= 1 and left_operand: right_operand = seq.pop() #print( "popped: [%s] %s:%s"%( op, left_operand, right_operand) ) if op == u'|': seq.append(StringGenerator.SequenceOR([left_operand, right_operand])) elif op == u'&': seq.append(StringGenerator.SequenceAND([left_operand, right_operand])) op = u'' left_operand = None right_operand = None # check for syntax errors if op: raise StringGenerator.SyntaxError(u"Operator: %s with no right operand" % op) if level > 0 and not sequence_closed: # it means we are finishing a non-first-level sequence without closing parens raise StringGenerator.SyntaxError(u"Missing closing parenthesis") return StringGenerator.Sequence(seq)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:process; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:data; 6, identifier:type; 7, identifier:history; 8, block; 8, 9; 8, 11; 8, 17; 8, 25; 8, 32; 8, 40; 8, 44; 8, 131; 8, 140; 8, 149; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:in; 12, 13; 12, 14; 13, identifier:type; 14, identifier:history; 15, block; 15, 16; 16, return_statement; 17, if_statement; 17, 18; 17, 23; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:type; 21, identifier:enum; 22, argument_list; 23, block; 23, 24; 24, return_statement; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:history; 29, identifier:append; 30, argument_list; 30, 31; 31, identifier:type; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:resolved; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:type; 38, identifier:resolve; 39, argument_list; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:value; 43, None; 44, if_statement; 44, 45; 44, 50; 44, 55; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:type; 48, identifier:multi_occurrence; 49, argument_list; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:value; 54, list:[]; 55, else_clause; 55, 56; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 64; 58, comparison_operator:>; 58, 59; 58, 63; 59, call; 59, 60; 59, 61; 60, identifier:len; 61, argument_list; 61, 62; 62, identifier:resolved; 63, integer:0; 64, block; 64, 65; 65, if_statement; 65, 66; 65, 71; 65, 95; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:resolved; 69, identifier:mixed; 70, argument_list; 71, block; 71, 72; 71, 83; 71, 89; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:value; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:Factory; 78, identifier:property; 79, argument_list; 79, 80; 80, attribute; 80, 81; 80, 82; 81, identifier:resolved; 82, identifier:name; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:md; 86, attribute; 86, 87; 86, 88; 87, identifier:value; 88, identifier:__metadata__; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:md; 93, identifier:sxtype; 94, identifier:resolved; 95, else_clause; 95, 96; 96, block; 96, 97; 96, 108; 96, 114; 96, 120; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:value; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:Factory; 103, identifier:object; 104, argument_list; 104, 105; 105, attribute; 105, 106; 105, 107; 106, identifier:resolved; 107, identifier:name; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:md; 111, attribute; 111, 112; 111, 113; 112, identifier:value; 113, identifier:__metadata__; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:md; 118, identifier:sxtype; 119, identifier:resolved; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:md; 124, identifier:ordering; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:ordering; 129, argument_list; 129, 130; 130, identifier:resolved; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 134; 133, identifier:setattr; 134, argument_list; 134, 135; 134, 136; 134, 139; 135, identifier:data; 136, attribute; 136, 137; 136, 138; 137, identifier:type; 138, identifier:name; 139, identifier:value; 140, if_statement; 140, 141; 140, 144; 141, comparison_operator:is; 141, 142; 141, 143; 142, identifier:value; 143, None; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:data; 148, identifier:value; 149, if_statement; 149, 150; 149, 156; 150, not_operator; 150, 151; 151, call; 151, 152; 151, 153; 152, identifier:isinstance; 153, argument_list; 153, 154; 153, 155; 154, identifier:data; 155, identifier:list; 156, block; 156, 157; 156, 165; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:add_attributes; 162, argument_list; 162, 163; 162, 164; 163, identifier:data; 164, identifier:resolved; 165, for_statement; 165, 166; 165, 169; 165, 174; 166, pattern_list; 166, 167; 166, 168; 167, identifier:child; 168, identifier:ancestry; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:resolved; 172, identifier:children; 173, argument_list; 174, block; 174, 175; 174, 185; 175, if_statement; 175, 176; 175, 183; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:skip_child; 180, argument_list; 180, 181; 180, 182; 181, identifier:child; 182, identifier:ancestry; 183, block; 183, 184; 184, continue_statement; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:process; 190, argument_list; 190, 191; 190, 192; 190, 193; 191, identifier:data; 192, identifier:child; 193, subscript; 193, 194; 193, 195; 194, identifier:history; 195, slice; 195, 196; 196, colon
def process(self, data, type, history): """ process the specified type then process its children """ if type in history: return if type.enum(): return history.append(type) resolved = type.resolve() value = None if type.multi_occurrence(): value = [] else: if len(resolved) > 0: if resolved.mixed(): value = Factory.property(resolved.name) md = value.__metadata__ md.sxtype = resolved else: value = Factory.object(resolved.name) md = value.__metadata__ md.sxtype = resolved md.ordering = self.ordering(resolved) setattr(data, type.name, value) if value is not None: data = value if not isinstance(data, list): self.add_attributes(data, resolved) for child, ancestry in resolved.children(): if self.skip_child(child, ancestry): continue self.process(data, child, history[:])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_process_tz; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:dt; 6, identifier:naive; 7, identifier:tz; 8, block; 8, 9; 8, 11; 8, 101; 8, 108; 8, 115; 8, 152; 8, 158; 8, 207; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:_tz; 13, parameters; 13, 14; 14, identifier:t; 15, block; 15, 16; 15, 25; 15, 45; 15, 76; 15, 99; 16, if_statement; 16, 17; 16, 22; 17, comparison_operator:in; 17, 18; 17, 19; 18, identifier:t; 19, tuple; 19, 20; 19, 21; 20, None; 21, string:'naive'; 22, block; 22, 23; 23, return_statement; 23, 24; 24, identifier:t; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:==; 26, 27; 26, 28; 27, identifier:t; 28, string:'local'; 29, block; 29, 30; 29, 41; 30, if_statement; 30, 31; 30, 35; 31, boolean_operator:and; 31, 32; 31, 33; 32, identifier:__debug__; 33, not_operator; 33, 34; 34, identifier:localtz; 35, block; 35, 36; 36, raise_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:ValueError; 39, argument_list; 39, 40; 40, string:"Requested conversion to local timezone, but `localtz` not installed."; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:t; 44, identifier:localtz; 45, if_statement; 45, 46; 45, 52; 46, not_operator; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:isinstance; 49, argument_list; 49, 50; 49, 51; 50, identifier:t; 51, identifier:tzinfo; 52, block; 52, 53; 52, 69; 53, if_statement; 53, 54; 53, 58; 54, boolean_operator:and; 54, 55; 54, 56; 55, identifier:__debug__; 56, not_operator; 56, 57; 57, identifier:localtz; 58, block; 58, 59; 59, raise_statement; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:ValueError; 62, argument_list; 62, 63; 63, binary_operator:+; 63, 64; 63, 65; 64, string:"The `pytz` package must be installed to look up timezone: "; 65, call; 65, 66; 65, 67; 66, identifier:repr; 67, argument_list; 67, 68; 68, identifier:t; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:t; 72, call; 72, 73; 72, 74; 73, identifier:get_tz; 74, argument_list; 74, 75; 75, identifier:t; 76, if_statement; 76, 77; 76, 85; 76, 86; 77, boolean_operator:and; 77, 78; 77, 84; 78, not_operator; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:hasattr; 81, argument_list; 81, 82; 81, 83; 82, identifier:t; 83, string:'normalize'; 84, identifier:get_tz; 85, comment; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:t; 90, call; 90, 91; 90, 92; 91, identifier:get_tz; 92, argument_list; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:t; 96, identifier:tzname; 97, argument_list; 97, 98; 98, identifier:dt; 99, return_statement; 99, 100; 100, identifier:t; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:naive; 104, call; 104, 105; 104, 106; 105, identifier:_tz; 106, argument_list; 106, 107; 107, identifier:naive; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:tz; 111, call; 111, 112; 111, 113; 112, identifier:_tz; 113, argument_list; 113, 114; 114, identifier:tz; 115, if_statement; 115, 116; 115, 122; 116, boolean_operator:and; 116, 117; 116, 121; 117, not_operator; 117, 118; 118, attribute; 118, 119; 118, 120; 119, identifier:dt; 120, identifier:tzinfo; 121, identifier:naive; 122, block; 122, 123; 123, if_statement; 123, 124; 123, 129; 123, 139; 124, call; 124, 125; 124, 126; 125, identifier:hasattr; 126, argument_list; 126, 127; 126, 128; 127, identifier:naive; 128, string:'localize'; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:dt; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:naive; 136, identifier:localize; 137, argument_list; 137, 138; 138, identifier:dt; 139, else_clause; 139, 140; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:dt; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:dt; 147, identifier:replace; 148, argument_list; 148, 149; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:tzinfo; 151, identifier:naive; 152, if_statement; 152, 153; 152, 155; 153, not_operator; 153, 154; 154, identifier:tz; 155, block; 155, 156; 156, return_statement; 156, 157; 157, identifier:dt; 158, if_statement; 158, 159; 158, 164; 158, 179; 158, 195; 159, call; 159, 160; 159, 161; 160, identifier:hasattr; 161, argument_list; 161, 162; 161, 163; 162, identifier:tz; 163, string:'normalize'; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:dt; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:tz; 171, identifier:normalize; 172, argument_list; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:dt; 176, identifier:astimezone; 177, argument_list; 177, 178; 178, identifier:tz; 179, elif_clause; 179, 180; 179, 183; 180, comparison_operator:==; 180, 181; 180, 182; 181, identifier:tz; 182, string:'naive'; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:dt; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:dt; 190, identifier:replace; 191, argument_list; 191, 192; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:tzinfo; 194, None; 195, else_clause; 195, 196; 196, block; 196, 197; 196, 206; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:dt; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:dt; 203, identifier:astimezone; 204, argument_list; 204, 205; 205, identifier:tz; 206, comment; 207, return_statement; 207, 208; 208, identifier:dt
def _process_tz(self, dt, naive, tz): """Process timezone casting and conversion.""" def _tz(t): if t in (None, 'naive'): return t if t == 'local': if __debug__ and not localtz: raise ValueError("Requested conversion to local timezone, but `localtz` not installed.") t = localtz if not isinstance(t, tzinfo): if __debug__ and not localtz: raise ValueError("The `pytz` package must be installed to look up timezone: " + repr(t)) t = get_tz(t) if not hasattr(t, 'normalize') and get_tz: # Attempt to handle non-pytz tzinfo. t = get_tz(t.tzname(dt)) return t naive = _tz(naive) tz = _tz(tz) if not dt.tzinfo and naive: if hasattr(naive, 'localize'): dt = naive.localize(dt) else: dt = dt.replace(tzinfo=naive) if not tz: return dt if hasattr(tz, 'normalize'): dt = tz.normalize(dt.astimezone(tz)) elif tz == 'naive': dt = dt.replace(tzinfo=None) else: dt = dt.astimezone(tz) # Warning: this might not always be entirely correct! return dt
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:__dfs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:v; 6, identifier:index; 7, identifier:layers; 8, block; 8, 9; 8, 11; 8, 60; 8, 155; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:==; 12, 13; 12, 14; 13, identifier:index; 14, integer:0; 15, block; 15, 16; 15, 21; 15, 49; 15, 58; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:path; 19, list:[v]; 19, 20; 20, identifier:v; 21, while_statement; 21, 22; 21, 29; 22, comparison_operator:!=; 22, 23; 22, 28; 23, subscript; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:_dfs_parent; 27, identifier:v; 28, identifier:v; 29, block; 29, 30; 29, 41; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:path; 34, identifier:append; 35, argument_list; 35, 36; 36, subscript; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_dfs_parent; 40, identifier:v; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:v; 44, subscript; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:_dfs_parent; 48, identifier:v; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:_dfs_paths; 55, identifier:append; 56, argument_list; 56, 57; 57, identifier:path; 58, return_statement; 58, 59; 59, True; 60, for_statement; 60, 61; 60, 62; 60, 67; 60, 68; 61, identifier:neighbour; 62, subscript; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:_graph; 66, identifier:v; 67, comment; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 77; 69, 78; 70, comparison_operator:in; 70, 71; 70, 72; 71, identifier:neighbour; 72, subscript; 72, 73; 72, 74; 73, identifier:layers; 74, binary_operator:-; 74, 75; 74, 76; 75, identifier:index; 76, integer:1; 77, comment; 78, block; 78, 79; 78, 87; 79, if_statement; 79, 80; 79, 85; 80, comparison_operator:in; 80, 81; 80, 82; 81, identifier:neighbour; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_dfs_parent; 85, block; 85, 86; 86, continue_statement; 87, if_statement; 87, 88; 87, 132; 88, boolean_operator:or; 88, 89; 88, 110; 88, 111; 89, parenthesized_expression; 89, 90; 90, boolean_operator:and; 90, 91; 90, 96; 91, comparison_operator:in; 91, 92; 91, 93; 92, identifier:neighbour; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:_left; 96, parenthesized_expression; 96, 97; 97, boolean_operator:or; 97, 98; 97, 103; 98, comparison_operator:not; 98, 99; 98, 100; 99, identifier:v; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:_matching; 103, comparison_operator:!=; 103, 104; 103, 105; 104, identifier:neighbour; 105, subscript; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:_matching; 109, identifier:v; 110, line_continuation:\; 111, parenthesized_expression; 111, 112; 112, boolean_operator:and; 112, 113; 112, 118; 113, comparison_operator:in; 113, 114; 113, 115; 114, identifier:neighbour; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_right; 118, parenthesized_expression; 118, 119; 119, boolean_operator:and; 119, 120; 119, 125; 120, comparison_operator:in; 120, 121; 120, 122; 121, identifier:v; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:_matching; 125, comparison_operator:==; 125, 126; 125, 127; 126, identifier:neighbour; 127, subscript; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:_matching; 131, identifier:v; 132, block; 132, 133; 132, 141; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 140; 135, subscript; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:_dfs_parent; 139, identifier:neighbour; 140, identifier:v; 141, if_statement; 141, 142; 141, 152; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:__dfs; 146, argument_list; 146, 147; 146, 148; 146, 151; 147, identifier:neighbour; 148, binary_operator:-; 148, 149; 148, 150; 149, identifier:index; 150, integer:1; 151, identifier:layers; 152, block; 152, 153; 153, return_statement; 153, 154; 154, True; 155, return_statement; 155, 156; 156, False
def __dfs(self, v, index, layers): """ we recursively run dfs on each vertices in free_vertex, :param v: vertices in free_vertex :return: True if P is not empty (i.e., the maximal set of vertex-disjoint alternating path of length k) and false otherwise. """ if index == 0: path = [v] while self._dfs_parent[v] != v: path.append(self._dfs_parent[v]) v = self._dfs_parent[v] self._dfs_paths.append(path) return True for neighbour in self._graph[v]: # check the neighbours of vertex if neighbour in layers[index - 1]: # if neighbour is in left, we are traversing unmatched edges.. if neighbour in self._dfs_parent: continue if (neighbour in self._left and (v not in self._matching or neighbour != self._matching[v])) or \ (neighbour in self._right and (v in self._matching and neighbour == self._matching[v])): self._dfs_parent[neighbour] = v if self.__dfs(neighbour, index-1, layers): return True return False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:login; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:username; 6, identifier:password; 7, default_parameter; 7, 8; 7, 9; 8, identifier:login_token; 9, None; 10, block; 10, 11; 10, 13; 10, 45; 10, 68; 10, 105; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:login_token; 16, None; 17, block; 17, 18; 17, 35; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:token_doc; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:post; 25, argument_list; 25, 26; 25, 29; 25, 32; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:action; 28, string:'query'; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:meta; 31, string:'tokens'; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:type; 34, string:'login'; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:login_token; 38, subscript; 38, 39; 38, 44; 39, subscript; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:token_doc; 42, string:'query'; 43, string:'tokens'; 44, string:'logintoken'; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:login_doc; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:post; 52, argument_list; 52, 53; 52, 56; 52, 59; 52, 62; 52, 65; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:action; 55, string:"clientlogin"; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:username; 58, identifier:username; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:password; 61, identifier:password; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:logintoken; 64, identifier:login_token; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:loginreturnurl; 67, string:"http://example.org/"; 68, if_statement; 68, 69; 68, 76; 68, 87; 69, comparison_operator:==; 69, 70; 69, 75; 70, subscript; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:login_doc; 73, string:'clientlogin'; 74, string:'status'; 75, string:"UI"; 76, block; 76, 77; 77, raise_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:ClientInteractionRequest; 81, identifier:from_doc; 82, argument_list; 82, 83; 82, 84; 83, identifier:login_token; 84, subscript; 84, 85; 84, 86; 85, identifier:login_doc; 86, string:'clientlogin'; 87, elif_clause; 87, 88; 87, 95; 88, comparison_operator:!=; 88, 89; 88, 94; 89, subscript; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:login_doc; 92, string:'clientlogin'; 93, string:'status'; 94, string:'PASS'; 95, block; 95, 96; 96, raise_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:LoginError; 100, identifier:from_doc; 101, argument_list; 101, 102; 102, subscript; 102, 103; 102, 104; 103, identifier:login_doc; 104, string:'clientlogin'; 105, return_statement; 105, 106; 106, subscript; 106, 107; 106, 108; 107, identifier:login_doc; 108, string:'clientlogin'
def login(self, username, password, login_token=None): """ Authenticate with the given credentials. If authentication is successful, all further requests sent will be signed the authenticated user. Note that passwords are sent as plaintext. This is a limitation of the Mediawiki API. Use a https host if you want your password to be secure :Parameters: username : str The username of the user to be authenticated password : str The password of the user to be authenticated :Raises: :class:`mwapi.errors.LoginError` : if authentication fails :class:`mwapi.errors.ClientInteractionRequest` : if authentication requires a continue_login() call :class:`mwapi.errors.APIError` : if the API responds with an error """ if login_token is None: token_doc = self.post(action='query', meta='tokens', type='login') login_token = token_doc['query']['tokens']['logintoken'] login_doc = self.post( action="clientlogin", username=username, password=password, logintoken=login_token, loginreturnurl="http://example.org/") if login_doc['clientlogin']['status'] == "UI": raise ClientInteractionRequest.from_doc( login_token, login_doc['clientlogin']) elif login_doc['clientlogin']['status'] != 'PASS': raise LoginError.from_doc(login_doc['clientlogin']) return login_doc['clientlogin']
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:cut_levels; 3, parameters; 3, 4; 3, 5; 4, identifier:nodes; 5, identifier:start_level; 6, block; 6, 7; 6, 9; 6, 13; 6, 17; 6, 152; 6, 168; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:final; 12, list:[]; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:removed; 16, list:[]; 17, for_statement; 17, 18; 17, 19; 17, 20; 18, identifier:node; 19, identifier:nodes; 20, block; 20, 21; 20, 37; 20, 56; 20, 118; 21, if_statement; 21, 22; 21, 28; 21, 29; 22, not_operator; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:hasattr; 25, argument_list; 25, 26; 25, 27; 26, identifier:node; 27, string:'level'; 28, comment; 29, block; 29, 30; 29, 36; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:remove; 33, argument_list; 33, 34; 33, 35; 34, identifier:node; 35, identifier:removed; 36, continue_statement; 37, if_statement; 37, 38; 37, 47; 37, 48; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:node; 42, identifier:attr; 43, identifier:get; 44, argument_list; 44, 45; 44, 46; 45, string:'soft_root'; 46, False; 47, comment; 48, block; 48, 49; 48, 55; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:remove; 52, argument_list; 52, 53; 52, 54; 53, identifier:node; 54, identifier:removed; 55, continue_statement; 56, if_statement; 56, 57; 56, 62; 56, 63; 56, 94; 56, 110; 57, comparison_operator:==; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:node; 60, identifier:level; 61, identifier:start_level; 62, comment; 63, block; 63, 64; 63, 71; 63, 77; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:final; 68, identifier:append; 69, argument_list; 69, 70; 70, identifier:node; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:node; 75, identifier:parent; 76, None; 77, if_statement; 77, 78; 77, 87; 78, boolean_operator:and; 78, 79; 78, 83; 79, not_operator; 79, 80; 80, attribute; 80, 81; 80, 82; 81, identifier:node; 82, identifier:visible; 83, not_operator; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:node; 86, identifier:children; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:remove; 91, argument_list; 91, 92; 91, 93; 92, identifier:node; 93, identifier:removed; 94, elif_clause; 94, 95; 94, 102; 94, 103; 95, comparison_operator:==; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:node; 98, identifier:level; 99, binary_operator:+; 99, 100; 99, 101; 100, identifier:start_level; 101, integer:1; 102, comment; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:node; 108, identifier:children; 109, list:[]; 110, else_clause; 110, 111; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:remove; 115, argument_list; 115, 116; 115, 117; 116, identifier:node; 117, identifier:removed; 118, if_statement; 118, 119; 118, 123; 119, not_operator; 119, 120; 120, attribute; 120, 121; 120, 122; 121, identifier:node; 122, identifier:visible; 123, block; 123, 124; 123, 128; 123, 142; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:keep_node; 127, False; 128, for_statement; 128, 129; 128, 130; 128, 133; 129, identifier:child; 130, attribute; 130, 131; 130, 132; 131, identifier:node; 132, identifier:children; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:keep_node; 137, boolean_operator:or; 137, 138; 137, 139; 138, identifier:keep_node; 139, attribute; 139, 140; 139, 141; 140, identifier:child; 141, identifier:visible; 142, if_statement; 142, 143; 142, 145; 143, not_operator; 143, 144; 144, identifier:keep_node; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 149; 148, identifier:remove; 149, argument_list; 149, 150; 149, 151; 150, identifier:node; 151, identifier:removed; 152, for_statement; 152, 153; 152, 154; 152, 155; 153, identifier:node; 154, identifier:removed; 155, block; 155, 156; 156, if_statement; 156, 157; 156, 160; 157, comparison_operator:in; 157, 158; 157, 159; 158, identifier:node; 159, identifier:final; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:final; 165, identifier:remove; 166, argument_list; 166, 167; 167, identifier:node; 168, return_statement; 168, 169; 169, identifier:final
def cut_levels(nodes, start_level): """ cutting nodes away from menus """ final = [] removed = [] for node in nodes: if not hasattr(node, 'level'): # remove and ignore nodes that don't have level information remove(node, removed) continue if node.attr.get('soft_root', False): # remove and ignore nodes that are behind a node marked as 'soft_root' remove(node, removed) continue if node.level == start_level: # turn nodes that are on from_level into root nodes final.append(node) node.parent = None if not node.visible and not node.children: remove(node, removed) elif node.level == start_level + 1: # remove nodes that are deeper than one level node.children = [] else: remove(node, removed) if not node.visible: keep_node = False for child in node.children: keep_node = keep_node or child.visible if not keep_node: remove(node, removed) for node in removed: if node in final: final.remove(node) return final
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:S; 3, parameters; 3, 4; 3, 5; 4, identifier:Document; 5, list_splat_pattern; 5, 6; 6, identifier:fields; 7, block; 7, 8; 7, 10; 7, 14; 7, 118; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:result; 13, list:[]; 14, for_statement; 14, 15; 14, 16; 14, 17; 15, identifier:field; 16, identifier:fields; 17, block; 17, 18; 17, 42; 17, 46; 17, 65; 17, 76; 17, 93; 17, 104; 18, if_statement; 18, 19; 18, 24; 18, 25; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:field; 23, identifier:tuple; 24, comment; 25, block; 25, 26; 25, 32; 25, 41; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 31; 28, pattern_list; 28, 29; 28, 30; 29, identifier:field; 30, identifier:direction; 31, identifier:field; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:result; 36, identifier:append; 37, argument_list; 37, 38; 38, tuple; 38, 39; 38, 40; 39, identifier:field; 40, identifier:direction; 41, continue_statement; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:direction; 45, identifier:ASCENDING; 46, if_statement; 46, 47; 46, 54; 47, not_operator; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:field; 51, identifier:startswith; 52, argument_list; 52, 53; 53, string:'__'; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:field; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:field; 61, identifier:replace; 62, argument_list; 62, 63; 62, 64; 63, string:'__'; 64, string:'.'; 65, if_statement; 65, 66; 65, 71; 66, comparison_operator:==; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:field; 69, integer:0; 70, string:'-'; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:direction; 75, identifier:DESCENDING; 76, if_statement; 76, 77; 76, 84; 77, comparison_operator:in; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:field; 80, integer:0; 81, tuple; 81, 82; 81, 83; 82, string:'+'; 83, string:'-'; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:field; 88, subscript; 88, 89; 88, 90; 89, identifier:field; 90, slice; 90, 91; 90, 92; 91, integer:1; 92, colon; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:_field; 96, call; 96, 97; 96, 98; 97, identifier:traverse; 98, argument_list; 98, 99; 98, 100; 98, 101; 99, identifier:Document; 100, identifier:field; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:default; 103, None; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:result; 108, identifier:append; 109, argument_list; 109, 110; 110, tuple; 110, 111; 110, 117; 111, conditional_expression:if; 111, 112; 111, 115; 111, 116; 112, parenthesized_expression; 112, 113; 113, unary_operator:~; 113, 114; 114, identifier:_field; 115, identifier:_field; 116, identifier:field; 117, identifier:direction; 118, return_statement; 118, 119; 119, identifier:result
def S(Document, *fields): """Generate a MongoDB sort order list using the Django ORM style.""" result = [] for field in fields: if isinstance(field, tuple): # Unpack existing tuple. field, direction = field result.append((field, direction)) continue direction = ASCENDING if not field.startswith('__'): field = field.replace('__', '.') if field[0] == '-': direction = DESCENDING if field[0] in ('+', '-'): field = field[1:] _field = traverse(Document, field, default=None) result.append(((~_field) if _field else field, direction)) return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:valid; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:cnpj; 6, block; 6, 7; 6, 9; 6, 19; 6, 23; 6, 31; 6, 39; 6, 43; 6, 49; 6, 89; 6, 105; 6, 117; 6, 123; 6, 131; 6, 135; 6, 141; 6, 181; 6, 197; 6, 209; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 16; 10, comparison_operator:!=; 10, 11; 10, 15; 11, call; 11, 12; 11, 13; 12, identifier:len; 13, argument_list; 13, 14; 14, identifier:cnpj; 15, integer:14; 16, block; 16, 17; 17, return_statement; 17, 18; 18, False; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:tam; 22, integer:12; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:nums; 26, subscript; 26, 27; 26, 28; 27, identifier:cnpj; 28, slice; 28, 29; 28, 30; 29, colon; 30, identifier:tam; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:digs; 34, subscript; 34, 35; 34, 36; 35, identifier:cnpj; 36, slice; 36, 37; 36, 38; 37, identifier:tam; 38, colon; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:tot; 42, integer:0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:pos; 46, binary_operator:-; 46, 47; 46, 48; 47, identifier:tam; 48, integer:7; 49, for_statement; 49, 50; 49, 51; 49, 58; 50, identifier:i; 51, call; 51, 52; 51, 53; 52, identifier:range; 53, argument_list; 53, 54; 53, 55; 53, 56; 54, identifier:tam; 55, integer:0; 56, unary_operator:-; 56, 57; 57, integer:1; 58, block; 58, 59; 58, 74; 58, 80; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:tot; 62, binary_operator:+; 62, 63; 62, 64; 63, identifier:tot; 64, binary_operator:*; 64, 65; 64, 73; 65, call; 65, 66; 65, 67; 66, identifier:int; 67, argument_list; 67, 68; 68, subscript; 68, 69; 68, 70; 69, identifier:nums; 70, binary_operator:-; 70, 71; 70, 72; 71, identifier:tam; 72, identifier:i; 73, identifier:pos; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:pos; 77, binary_operator:-; 77, 78; 77, 79; 78, identifier:pos; 79, integer:1; 80, if_statement; 80, 81; 80, 84; 81, comparison_operator:<; 81, 82; 81, 83; 82, identifier:pos; 83, integer:2; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:pos; 88, integer:9; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:res; 92, conditional_expression:if; 92, 93; 92, 94; 92, 99; 93, integer:0; 94, comparison_operator:<; 94, 95; 94, 98; 95, binary_operator:%; 95, 96; 95, 97; 96, identifier:tot; 97, integer:11; 98, integer:2; 99, binary_operator:-; 99, 100; 99, 101; 100, integer:11; 101, parenthesized_expression; 101, 102; 102, binary_operator:%; 102, 103; 102, 104; 103, identifier:tot; 104, integer:11; 105, if_statement; 105, 106; 105, 114; 106, comparison_operator:!=; 106, 107; 106, 108; 107, identifier:res; 108, call; 108, 109; 108, 110; 109, identifier:int; 110, argument_list; 110, 111; 111, subscript; 111, 112; 111, 113; 112, identifier:digs; 113, integer:0; 114, block; 114, 115; 115, return_statement; 115, 116; 116, False; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:tam; 120, binary_operator:+; 120, 121; 120, 122; 121, identifier:tam; 122, integer:1; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:nums; 126, subscript; 126, 127; 126, 128; 127, identifier:cnpj; 128, slice; 128, 129; 128, 130; 129, colon; 130, identifier:tam; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:tot; 134, integer:0; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:pos; 138, binary_operator:-; 138, 139; 138, 140; 139, identifier:tam; 140, integer:7; 141, for_statement; 141, 142; 141, 143; 141, 150; 142, identifier:i; 143, call; 143, 144; 143, 145; 144, identifier:range; 145, argument_list; 145, 146; 145, 147; 145, 148; 146, identifier:tam; 147, integer:0; 148, unary_operator:-; 148, 149; 149, integer:1; 150, block; 150, 151; 150, 166; 150, 172; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:tot; 154, binary_operator:+; 154, 155; 154, 156; 155, identifier:tot; 156, binary_operator:*; 156, 157; 156, 165; 157, call; 157, 158; 157, 159; 158, identifier:int; 159, argument_list; 159, 160; 160, subscript; 160, 161; 160, 162; 161, identifier:nums; 162, binary_operator:-; 162, 163; 162, 164; 163, identifier:tam; 164, identifier:i; 165, identifier:pos; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:pos; 169, binary_operator:-; 169, 170; 169, 171; 170, identifier:pos; 171, integer:1; 172, if_statement; 172, 173; 172, 176; 173, comparison_operator:<; 173, 174; 173, 175; 174, identifier:pos; 175, integer:2; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:pos; 180, integer:9; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:res; 184, conditional_expression:if; 184, 185; 184, 186; 184, 191; 185, integer:0; 186, comparison_operator:<; 186, 187; 186, 190; 187, binary_operator:%; 187, 188; 187, 189; 188, identifier:tot; 189, integer:11; 190, integer:2; 191, binary_operator:-; 191, 192; 191, 193; 192, integer:11; 193, parenthesized_expression; 193, 194; 194, binary_operator:%; 194, 195; 194, 196; 195, identifier:tot; 196, integer:11; 197, if_statement; 197, 198; 197, 206; 198, comparison_operator:!=; 198, 199; 198, 200; 199, identifier:res; 200, call; 200, 201; 200, 202; 201, identifier:int; 202, argument_list; 202, 203; 203, subscript; 203, 204; 203, 205; 204, identifier:digs; 205, integer:1; 206, block; 206, 207; 207, return_statement; 207, 208; 208, False; 209, return_statement; 209, 210; 210, True
def valid(self, cnpj): """Check if a CNPJ is valid. We should avoid sending invalid CNPJ to the web service as we know it is going to be a waste of bandwidth. Assumes CNPJ is a string. """ if len(cnpj) != 14: return False tam = 12 nums = cnpj[:tam] digs = cnpj[tam:] tot = 0 pos = tam-7 for i in range(tam, 0, -1): tot = tot + int(nums[tam-i])*pos pos = pos - 1 if pos < 2: pos = 9 res = 0 if tot % 11 < 2 else 11 - (tot % 11) if res != int(digs[0]): return False tam = tam + 1 nums = cnpj[:tam] tot = 0 pos = tam-7 for i in range(tam, 0, -1): tot = tot + int(nums[tam-i])*pos pos = pos - 1 if pos < 2: pos = 9 res = 0 if tot % 11 < 2 else 11 - (tot % 11) if res != int(digs[1]): return False return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:arrayuniqify; 3, parameters; 3, 4; 3, 5; 4, identifier:X; 5, default_parameter; 5, 6; 5, 7; 6, identifier:retainorder; 7, False; 8, block; 8, 9; 8, 11; 8, 19; 8, 25; 8, 47; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:s; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:X; 17, identifier:argsort; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:X; 22, subscript; 22, 23; 22, 24; 23, identifier:X; 24, identifier:s; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:D; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:np; 31, identifier:append; 32, argument_list; 32, 33; 32, 35; 33, list:[True]; 33, 34; 34, True; 35, comparison_operator:!=; 35, 36; 35, 41; 36, subscript; 36, 37; 36, 38; 37, identifier:X; 38, slice; 38, 39; 38, 40; 39, integer:1; 40, colon; 41, subscript; 41, 42; 41, 43; 42, identifier:X; 43, slice; 43, 44; 43, 45; 44, colon; 45, unary_operator:-; 45, 46; 46, integer:1; 47, if_statement; 47, 48; 47, 49; 47, 107; 48, identifier:retainorder; 49, block; 49, 50; 49, 69; 49, 99; 49, 105; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:DD; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:np; 56, identifier:append; 57, argument_list; 57, 58; 57, 65; 58, subscript; 58, 59; 58, 64; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:D; 62, identifier:nonzero; 63, argument_list; 64, integer:0; 65, call; 65, 66; 65, 67; 66, identifier:len; 67, argument_list; 67, 68; 68, identifier:X; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:ind; 72, list_comprehension; 72, 73; 72, 86; 73, call; 73, 74; 73, 75; 74, identifier:min; 75, argument_list; 75, 76; 76, subscript; 76, 77; 76, 78; 77, identifier:s; 78, slice; 78, 79; 78, 80; 78, 81; 79, identifier:x; 80, colon; 81, subscript; 81, 82; 81, 83; 82, identifier:DD; 83, binary_operator:+; 83, 84; 83, 85; 84, identifier:i; 85, integer:1; 86, for_in_clause; 86, 87; 86, 90; 87, tuple_pattern; 87, 88; 87, 89; 88, identifier:i; 89, identifier:x; 90, call; 90, 91; 90, 92; 91, identifier:enumerate; 92, argument_list; 92, 93; 93, subscript; 93, 94; 93, 95; 94, identifier:DD; 95, slice; 95, 96; 95, 97; 96, colon; 97, unary_operator:-; 97, 98; 98, integer:1; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:ind; 103, identifier:sort; 104, argument_list; 105, return_statement; 105, 106; 106, identifier:ind; 107, else_clause; 107, 108; 108, block; 108, 109; 109, return_statement; 109, 110; 110, list:[D,s]; 110, 111; 110, 112; 111, identifier:D; 112, identifier:s
def arrayuniqify(X, retainorder=False): """ Very fast uniqify routine for numpy arrays. **Parameters** **X** : numpy array Determine the unique elements of this numpy array. **retainorder** : Boolean, optional Whether or not to return indices corresponding to unique values of `X` that also sort the values. Default value is `False`, in which case `[D,s]` is returned. This can be used to produce a uniqified version of `X` by simply taking:: X[s][D] or:: X[s[D.nonzero()[0]]] **Returns** **D** : numpy array List of "first differences" in the sorted verion of `X`. Returned when `retainorder` is `False` (default). **s** : numpy array Permutation that will sort `X`. Returned when `retainorder` is `False` (default). **ind** : numpy array List of indices that correspond to unique values of `X`, without sorting those values. Returned when `retainorder` is `True`. **See Also:** :func:`tabular.fast.recarrayuniqify` """ s = X.argsort() X = X[s] D = np.append([True],X[1:] != X[:-1]) if retainorder: DD = np.append(D.nonzero()[0],len(X)) ind = [min(s[x:DD[i+1]]) for (i,x) in enumerate(DD[:-1])] ind.sort() return ind else: return [D,s]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:equalspairs; 3, parameters; 3, 4; 3, 5; 4, identifier:X; 5, identifier:Y; 6, block; 6, 7; 6, 9; 6, 17; 6, 39; 6, 60; 6, 77; 6, 86; 6, 102; 6, 118; 6, 129; 6, 145; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:T; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:Y; 15, identifier:copy; 16, argument_list; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:R; 20, subscript; 20, 21; 20, 38; 21, call; 21, 22; 21, 37; 22, attribute; 22, 23; 22, 36; 23, parenthesized_expression; 23, 24; 24, comparison_operator:!=; 24, 25; 24, 30; 25, subscript; 25, 26; 25, 27; 26, identifier:T; 27, slice; 27, 28; 27, 29; 28, integer:1; 29, colon; 30, subscript; 30, 31; 30, 32; 31, identifier:T; 32, slice; 32, 33; 32, 34; 33, colon; 34, unary_operator:-; 34, 35; 35, integer:1; 36, identifier:nonzero; 37, argument_list; 38, integer:0; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:R; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:np; 45, identifier:append; 46, argument_list; 46, 47; 46, 48; 47, identifier:R; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:np; 51, identifier:array; 52, argument_list; 52, 53; 53, list:[len(T)-1]; 53, 54; 54, binary_operator:-; 54, 55; 54, 59; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, identifier:T; 59, integer:1; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:M; 63, subscript; 63, 64; 63, 65; 64, identifier:R; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:R; 68, identifier:searchsorted; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:range; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:len; 75, argument_list; 75, 76; 76, identifier:T; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:D; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:T; 83, identifier:searchsorted; 84, argument_list; 84, 85; 85, identifier:X; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:T; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:np; 92, identifier:append; 93, argument_list; 93, 94; 93, 95; 94, identifier:T; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:np; 98, identifier:array; 99, argument_list; 99, 100; 100, list:[0]; 100, 101; 101, integer:0; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:M; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:np; 108, identifier:append; 109, argument_list; 109, 110; 109, 111; 110, identifier:M; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:np; 114, identifier:array; 115, argument_list; 115, 116; 116, list:[0]; 116, 117; 117, integer:0; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:A; 121, binary_operator:*; 121, 122; 121, 128; 122, parenthesized_expression; 122, 123; 123, comparison_operator:==; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:T; 126, identifier:D; 127, identifier:X; 128, identifier:D; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:B; 132, binary_operator:*; 132, 133; 132, 139; 133, parenthesized_expression; 133, 134; 134, comparison_operator:==; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:T; 137, identifier:D; 138, identifier:X; 139, parenthesized_expression; 139, 140; 140, binary_operator:+; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:M; 143, identifier:D; 144, integer:1; 145, return_statement; 145, 146; 146, list:[A,B]; 146, 147; 146, 148; 147, identifier:A; 148, identifier:B
def equalspairs(X, Y): """ Indices of elements in a sorted numpy array equal to those in another. Given numpy array `X` and sorted numpy array `Y`, determine the indices in Y equal to indices in X. Returns `[A,B]` where `A` and `B` are numpy arrays of indices in `X` such that:: Y[A[i]:B[i]] = Y[Y == X[i]]` `A[i] = B[i] = 0` if `X[i]` is not in `Y`. **Parameters** **X** : numpy array Numpy array to compare to the sorted numpy array `Y`. **Y** : numpy array Sorted numpy array. Determine the indices of elements of `Y` equal to those in numpy array `X`. **Returns** **A** : numpy array List of indices in `Y`, `len(A) = len(Y)`. **B** : numpy array List of indices in `Y`, `len(B) = len(Y)`. **See Also:** :func:`tabular.fast.recarrayequalspairs` """ T = Y.copy() R = (T[1:] != T[:-1]).nonzero()[0] R = np.append(R,np.array([len(T)-1])) M = R[R.searchsorted(range(len(T)))] D = T.searchsorted(X) T = np.append(T,np.array([0])) M = np.append(M,np.array([0])) A = (T[D] == X) * D B = (T[D] == X) * (M[D] + 1) return [A,B]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:loadSV; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:fname; 5, default_parameter; 5, 6; 5, 7; 6, identifier:shape; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:titles; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:aligned; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:byteorder; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:renamer; 19, None; 20, dictionary_splat_pattern; 20, 21; 21, identifier:kwargs; 22, block; 22, 23; 22, 25; 22, 36; 22, 57; 22, 78; 22, 99; 22, 137; 22, 159; 23, expression_statement; 23, 24; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 30; 27, list_pattern; 27, 28; 27, 29; 28, identifier:columns; 29, identifier:metadata; 30, call; 30, 31; 30, 32; 31, identifier:loadSVcols; 32, argument_list; 32, 33; 32, 34; 33, identifier:fname; 34, dictionary_splat; 34, 35; 35, identifier:kwargs; 36, if_statement; 36, 37; 36, 44; 36, 51; 37, comparison_operator:in; 37, 38; 37, 39; 38, string:'names'; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:metadata; 42, identifier:keys; 43, argument_list; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:names; 48, subscript; 48, 49; 48, 50; 49, identifier:metadata; 50, string:'names'; 51, else_clause; 51, 52; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:names; 56, None; 57, if_statement; 57, 58; 57, 65; 57, 72; 58, comparison_operator:in; 58, 59; 58, 60; 59, string:'formats'; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:metadata; 63, identifier:keys; 64, argument_list; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:formats; 69, subscript; 69, 70; 69, 71; 70, identifier:metadata; 71, string:'formats'; 72, else_clause; 72, 73; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:formats; 77, None; 78, if_statement; 78, 79; 78, 86; 78, 93; 79, comparison_operator:in; 79, 80; 79, 81; 80, string:'dtype'; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:metadata; 84, identifier:keys; 85, argument_list; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:dtype; 90, subscript; 90, 91; 90, 92; 91, identifier:metadata; 92, string:'dtype'; 93, else_clause; 93, 94; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:dtype; 98, None; 99, if_statement; 99, 100; 99, 103; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:renamer; 102, None; 103, block; 103, 104; 103, 106; 103, 113; 104, print_statement; 104, 105; 105, string:'Trying user-given renamer ...'; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:renamed; 109, call; 109, 110; 109, 111; 110, identifier:renamer; 111, argument_list; 111, 112; 112, identifier:names; 113, if_statement; 113, 114; 113, 126; 113, 133; 114, comparison_operator:==; 114, 115; 114, 119; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, identifier:renamed; 119, call; 119, 120; 119, 121; 120, identifier:len; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:uniqify; 124, argument_list; 124, 125; 125, identifier:renamed; 126, block; 126, 127; 126, 131; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:names; 130, identifier:renamed; 131, print_statement; 131, 132; 132, string:'''... using renamed names (original names will be in return metadata)'''; 133, else_clause; 133, 134; 134, block; 134, 135; 135, print_statement; 135, 136; 136, string:'... renamer failed to produce unique names, not using.'; 137, if_statement; 137, 138; 137, 152; 138, boolean_operator:and; 138, 139; 138, 140; 139, identifier:names; 140, comparison_operator:!=; 140, 141; 140, 145; 141, call; 141, 142; 141, 143; 142, identifier:len; 143, argument_list; 143, 144; 144, identifier:names; 145, call; 145, 146; 145, 147; 146, identifier:len; 147, argument_list; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:uniqify; 150, argument_list; 150, 151; 151, identifier:names; 152, block; 152, 153; 152, 155; 153, print_statement; 153, 154; 154, string:'Names are not unique, reverting to default naming scheme.'; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:names; 158, None; 159, return_statement; 159, 160; 160, list:[utils.fromarrays(columns, type=np.ndarray, dtype=dtype, shape=shape, formats=formats, names=names, titles=titles, aligned=aligned, byteorder=byteorder), metadata]; 160, 161; 160, 193; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:utils; 164, identifier:fromarrays; 165, argument_list; 165, 166; 165, 167; 165, 172; 165, 175; 165, 178; 165, 181; 165, 184; 165, 187; 165, 190; 166, identifier:columns; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:type; 169, attribute; 169, 170; 169, 171; 170, identifier:np; 171, identifier:ndarray; 172, keyword_argument; 172, 173; 172, 174; 173, identifier:dtype; 174, identifier:dtype; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:shape; 177, identifier:shape; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:formats; 180, identifier:formats; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:names; 183, identifier:names; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:titles; 186, identifier:titles; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:aligned; 189, identifier:aligned; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:byteorder; 192, identifier:byteorder; 193, identifier:metadata
def loadSV(fname, shape=None, titles=None, aligned=False, byteorder=None, renamer=None, **kwargs): """ Load a delimited text file to a numpy record array. Basically, this function calls loadSVcols and combines columns returned by that function into a numpy ndarray with stuctured dtype. Also uses and returns metadata including column names, formats, coloring, &c. if these items are determined during the loading process. **Parameters** **fname** : string or file object Path (or file object) corresponding to a separated variable (CSV) text file. **names** : list of strings Sets the names of the columns of the resulting tabarray. If not specified, `names` value is determined first by looking for metadata in the header of the file, and if that is not found, are assigned by NumPy's `f0, f1, ... fn` convention. See **namesinheader** parameter below. **formats** : string or list of strings Sets the datatypes of the columns. The value of `formats` can be a list or comma-delimited string of values describing values for each column (e.g. "str,str,int,float" or ["str", "str", "int", "float"]), a single value to apply to all columns, or anything that can be used in numpy.rec.array constructor. If the **formats** (or **dtype**) parameter are not specified, typing is done by inference. See **typer** parameter below. **dtype** : numpy dtype object Sets the numpy dtype of the resulting tabarray, combining column format and column name information. If dtype is set, any **names** and **formats** specifications will be overriden. If the **dtype** (or **formats**) parameter are not specified, typing is done by inference. See **typer** parameter below. The **names**, **formats** and **dtype** parameters duplicate parameters of the NumPy record array creation inferface. Additional paramters of the NumPy inferface that are passed through are **shape**, **titles**, **byteorder** and **aligned** (see NumPy documentation for more information.) **kwargs**: keyword argument dictionary of variable length Contains various parameters to be passed down to loadSVcols. These may include **skiprows**, **comments**, **delimiter**, **lineterminator**, **uselines**, **usecols**, **excludecols**, **metametadata**, **namesinheader**,**headerlines**, **valuefixer**, **linefixer**, **colfixer**, **delimiter_regex**, **inflines**, **typer**, **missingvalues**, **fillingvalues**, **verbosity**, and various CSV module parameters like **escapechar**, **quoting**, **quotechar**, **doublequote**, **skipinitialspace**. **Returns** **R** : numpy record array Record array constructed from data in the SV file **metadata** : dictionary Metadata read and constructed during process of reading file. **See Also:** :func:`tabular.io.loadSVcols`, :func:`tabular.io.saveSV`, :func:`tabular.io.DEFAULT_TYPEINFERER` """ [columns, metadata] = loadSVcols(fname, **kwargs) if 'names' in metadata.keys(): names = metadata['names'] else: names = None if 'formats' in metadata.keys(): formats = metadata['formats'] else: formats = None if 'dtype' in metadata.keys(): dtype = metadata['dtype'] else: dtype = None if renamer is not None: print 'Trying user-given renamer ...' renamed = renamer(names) if len(renamed) == len(uniqify(renamed)): names = renamed print '''... using renamed names (original names will be in return metadata)''' else: print '... renamer failed to produce unique names, not using.' if names and len(names) != len(uniqify(names)): print 'Names are not unique, reverting to default naming scheme.' names = None return [utils.fromarrays(columns, type=np.ndarray, dtype=dtype, shape=shape, formats=formats, names=names, titles=titles, aligned=aligned, byteorder=byteorder), metadata]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:loadSVrecs; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:fname; 5, default_parameter; 5, 6; 5, 7; 6, identifier:uselines; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:skiprows; 10, integer:0; 11, default_parameter; 11, 12; 11, 13; 12, identifier:linefixer; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:delimiter_regex; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:verbosity; 19, identifier:DEFAULT_VERBOSITY; 20, dictionary_splat_pattern; 20, 21; 21, identifier:metadata; 22, block; 22, 23; 22, 25; 22, 48; 22, 72; 22, 83; 22, 115; 22, 136; 22, 226; 22, 240; 22, 285; 22, 291; 22, 307; 23, expression_statement; 23, 24; 24, comment; 25, if_statement; 25, 26; 25, 35; 26, boolean_operator:and; 26, 27; 26, 28; 27, identifier:delimiter_regex; 28, call; 28, 29; 28, 30; 29, identifier:isinstance; 30, argument_list; 30, 31; 30, 32; 31, identifier:delimiter_regex; 32, attribute; 32, 33; 32, 34; 33, identifier:types; 34, identifier:StringType; 35, block; 35, 36; 35, 39; 36, import_statement; 36, 37; 37, dotted_name; 37, 38; 38, identifier:re; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:delimiter_regex; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:re; 45, identifier:compile; 46, argument_list; 46, 47; 47, identifier:delimiter_regex; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 54; 50, list_pattern; 50, 51; 50, 52; 50, 53; 51, identifier:metadata; 52, identifier:inferedlines; 53, identifier:WHOLETHING; 54, call; 54, 55; 54, 56; 55, identifier:getmetadata; 56, argument_list; 56, 57; 56, 58; 56, 61; 56, 64; 56, 67; 56, 70; 57, identifier:fname; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:skiprows; 60, identifier:skiprows; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:linefixer; 63, identifier:linefixer; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:delimiter_regex; 66, identifier:delimiter_regex; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:verbosity; 69, identifier:verbosity; 70, dictionary_splat; 70, 71; 71, identifier:metadata; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:is; 73, 74; 73, 75; 74, identifier:uselines; 75, None; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:uselines; 80, tuple; 80, 81; 80, 82; 81, integer:0; 82, False; 83, if_statement; 83, 84; 83, 88; 83, 97; 83, 108; 84, call; 84, 85; 84, 86; 85, identifier:is_string_like; 86, argument_list; 86, 87; 87, identifier:fname; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:fh; 92, call; 92, 93; 92, 94; 93, identifier:file; 94, argument_list; 94, 95; 94, 96; 95, identifier:fname; 96, string:'rU'; 97, elif_clause; 97, 98; 97, 103; 98, call; 98, 99; 98, 100; 99, identifier:hasattr; 100, argument_list; 100, 101; 100, 102; 101, identifier:fname; 102, string:'readline'; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:fh; 107, identifier:fname; 108, else_clause; 108, 109; 109, block; 109, 110; 110, raise_statement; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:ValueError; 113, argument_list; 113, 114; 114, string:'fname must be a string or file handle'; 115, for_statement; 115, 116; 115, 117; 115, 129; 116, identifier:_ind; 117, call; 117, 118; 117, 119; 118, identifier:range; 119, argument_list; 119, 120; 120, binary_operator:+; 120, 121; 120, 126; 121, binary_operator:+; 121, 122; 121, 123; 122, identifier:skiprows; 123, subscript; 123, 124; 123, 125; 124, identifier:uselines; 125, integer:0; 126, subscript; 126, 127; 126, 128; 127, identifier:metadata; 128, string:'headerlines'; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:fh; 134, identifier:readline; 135, argument_list; 136, if_statement; 136, 137; 136, 140; 137, boolean_operator:or; 137, 138; 137, 139; 138, identifier:linefixer; 139, identifier:delimiter_regex; 140, block; 140, 141; 140, 150; 140, 168; 140, 179; 140, 203; 140, 215; 140, 222; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:fh2; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:tempfile; 147, identifier:TemporaryFile; 148, argument_list; 148, 149; 149, string:'w+b'; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:F; 153, call; 153, 154; 153, 166; 154, attribute; 154, 155; 154, 165; 155, call; 155, 156; 155, 163; 156, attribute; 156, 157; 156, 162; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:fh; 160, identifier:read; 161, argument_list; 162, identifier:strip; 163, argument_list; 163, 164; 164, string:'\n'; 165, identifier:split; 166, argument_list; 166, 167; 167, string:'\n'; 168, if_statement; 168, 169; 168, 170; 169, identifier:linefixer; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:F; 174, call; 174, 175; 174, 176; 175, identifier:map; 176, argument_list; 176, 177; 176, 178; 177, identifier:linefixer; 178, identifier:F; 179, if_statement; 179, 180; 179, 181; 180, identifier:delimiter_regex; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:F; 185, call; 185, 186; 185, 187; 186, identifier:map; 187, argument_list; 187, 188; 187, 202; 188, lambda; 188, 189; 188, 191; 189, lambda_parameters; 189, 190; 190, identifier:line; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:delimiter_regex; 194, identifier:sub; 195, argument_list; 195, 196; 195, 201; 196, attribute; 196, 197; 196, 200; 197, subscript; 197, 198; 197, 199; 198, identifier:metadata; 199, string:'dialect'; 200, identifier:delimiter; 201, identifier:line; 202, identifier:F; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:fh2; 207, identifier:write; 208, argument_list; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, string:'\n'; 212, identifier:join; 213, argument_list; 213, 214; 214, identifier:F; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:fh2; 219, identifier:seek; 220, argument_list; 220, 221; 221, integer:0; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:fh; 225, identifier:fh2; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:reader; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:csv; 232, identifier:reader; 233, argument_list; 233, 234; 233, 235; 234, identifier:fh; 235, keyword_argument; 235, 236; 235, 237; 236, identifier:dialect; 237, subscript; 237, 238; 237, 239; 238, identifier:metadata; 239, string:'dialect'; 240, if_statement; 240, 241; 240, 244; 240, 276; 241, subscript; 241, 242; 241, 243; 242, identifier:uselines; 243, integer:1; 244, block; 244, 245; 244, 249; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:linelist; 248, list:[]; 249, for_statement; 249, 250; 249, 251; 249, 252; 250, identifier:ln; 251, identifier:reader; 252, block; 252, 253; 253, if_statement; 253, 254; 253, 265; 253, 273; 254, comparison_operator:<=; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:reader; 257, identifier:line_num; 258, binary_operator:-; 258, 259; 258, 262; 259, subscript; 259, 260; 259, 261; 260, identifier:uselines; 261, integer:1; 262, subscript; 262, 263; 262, 264; 263, identifier:uselines; 264, integer:0; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:linelist; 270, identifier:append; 271, argument_list; 271, 272; 272, identifier:ln; 273, else_clause; 273, 274; 274, block; 274, 275; 275, break_statement; 276, else_clause; 276, 277; 277, block; 277, 278; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:linelist; 281, call; 281, 282; 281, 283; 282, identifier:list; 283, argument_list; 283, 284; 284, identifier:reader; 285, expression_statement; 285, 286; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:fh; 289, identifier:close; 290, argument_list; 291, if_statement; 291, 292; 291, 298; 292, comparison_operator:==; 292, 293; 292, 297; 293, subscript; 293, 294; 293, 295; 294, identifier:linelist; 295, unary_operator:-; 295, 296; 296, integer:1; 297, list:[]; 298, block; 298, 299; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:linelist; 303, identifier:pop; 304, argument_list; 304, 305; 305, unary_operator:-; 305, 306; 306, integer:1; 307, return_statement; 307, 308; 308, list:[linelist,metadata]; 308, 309; 308, 310; 309, identifier:linelist; 310, identifier:metadata
def loadSVrecs(fname, uselines=None, skiprows=0, linefixer=None, delimiter_regex=None, verbosity=DEFAULT_VERBOSITY, **metadata): """ Load a separated value text file to a list of lists of strings of records. Takes a tabular text file with a specified delimeter and end-of-line character, and return data as a list of lists of strings corresponding to records (rows). Also uses and returns metadata (including column names, formats, coloring, &c.) if these items are determined during the loading process. **Parameters** **fname** : string or file object Path (or file object) corresponding to a separated variable (CSV) text file. **delimiter** : single-character string When reading text file, character to use as delimiter to split fields. If not specified, the delimiter is determined first by looking for special-format metadata specifying the delimiter, and then if no specification is found, attempts are made to infer delimiter from file contents. (See **inflines** parameter below.) **delimiter_regex** : regular expression (compiled or in string format) Regular expression to use to recognize delimiters, in place of a single character. (For instance, to have whitespace delimiting, using delimiter_regex = '[\s*]+') **lineterminator** : single-character string Line terminator to use when reading in using SVfile. **skipinitialspace** : boolean If true, strips whitespace following the delimiter from field. The **delimiter**, **linterminator** and **skipinitialspace** parameters are passed on as parameters to the python CSV module, which is used for reading in delimited text files. Additional parameters from that interface that are replicated in this constructor include **quotechar**, **escapechar**, **quoting**, **doublequote** and **dialect** (see CSV module documentation for more information). **skiprows** : non-negative integer, optional When reading from a text file, the first `skiprows` lines are ignored. Default is 0, e.g no rows are skipped. **uselines** : pair of non-negative integer, optional When reading from a text file, range of lines of data to load. (In contrast to **skiprows**, which specifies file rows to ignore before looking for header information, **uselines** specifies which data (non-header) lines to use, after header has been striped and processed.) See **headerlines** below. **comments** : single-character string, optional When reading from a text file, character used to distinguish header lines. If specified, any lines beginning with this character at the top of the file are assumed to contain header information and not row data. **headerlines** : integer, optional When reading from a text file, the number of lines at the top of the file (after the first `skiprows` lines) corresponding to the header of the file, where metadata can be found. Lines after headerlines are assumed to contain row contents. If not specified, value is determined first by looking for special metametadata in first line of file (see Tabular reference documentation for more information about this), and if no such metadata is found, is inferred by looking at file contents. **namesinheader** : Boolean, optional When reading from a text file, if `namesinheader == True`, then assume the column names are in the last header line (unless overridden by existing metadata or metametadata directive). Default is True. **linefixer** : callable, optional This callable is applied to every line in the file. If specified, the called is applied directly to the strings in the file, after they're split in lines but before they're split into fields. The purpose is to make lines with errors or mistakes amenable to delimiter inference and field-splitting. **inflines** : integer, optional Number of lines of file to use as sample data when inferring delimiter and header. **metametadata** : dictionary of integers or pairs of integers Specifies supplementary metametadata information for use with SVfile loading. See Tabular reference documentation for more information **Returns** **records** : list of lists of strings List of lists corresponding to records (rows) of data. **metadata** : dictionary Metadata read and constructed during process of reading file. **See Also:** :func:`tabular.io.loadSV`, :func:`tabular.io.saveSV`, :func:`tabular.io.DEFAULT_TYPEINFERER` """ if delimiter_regex and isinstance(delimiter_regex, types.StringType): import re delimiter_regex = re.compile(delimiter_regex) [metadata, inferedlines, WHOLETHING] = getmetadata(fname, skiprows=skiprows, linefixer=linefixer, delimiter_regex=delimiter_regex, verbosity=verbosity, **metadata) if uselines is None: uselines = (0,False) if is_string_like(fname): fh = file(fname, 'rU') elif hasattr(fname, 'readline'): fh = fname else: raise ValueError('fname must be a string or file handle') for _ind in range(skiprows+uselines[0] + metadata['headerlines']): fh.readline() if linefixer or delimiter_regex: fh2 = tempfile.TemporaryFile('w+b') F = fh.read().strip('\n').split('\n') if linefixer: F = map(linefixer,F) if delimiter_regex: F = map(lambda line: delimiter_regex.sub(metadata['dialect'].delimiter, line), F) fh2.write('\n'.join(F)) fh2.seek(0) fh = fh2 reader = csv.reader(fh, dialect=metadata['dialect']) if uselines[1]: linelist = [] for ln in reader: if reader.line_num <= uselines[1] - uselines[0]: linelist.append(ln) else: break else: linelist = list(reader) fh.close() if linelist[-1] == []: linelist.pop(-1) return [linelist,metadata]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 1, 13; 2, function_name:dflt_interval; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:cd_id; 7, type; 7, 8; 8, identifier:str; 9, type; 9, 10; 10, tuple; 10, 11; 10, 12; 11, identifier:int; 12, identifier:int; 13, block; 13, 14; 13, 16; 13, 23; 13, 27; 13, 31; 13, 84; 13, 109; 13, 115; 13, 123; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:LOGGER; 20, identifier:debug; 21, argument_list; 21, 22; 22, string:'RevocationCache.dflt_interval >>>'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:fro; 26, None; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:to; 30, None; 31, for_statement; 31, 32; 31, 33; 31, 34; 32, identifier:rr_id; 33, identifier:self; 34, block; 34, 35; 34, 44; 34, 50; 35, if_statement; 35, 36; 35, 42; 36, comparison_operator:!=; 36, 37; 36, 38; 37, identifier:cd_id; 38, call; 38, 39; 38, 40; 39, identifier:rev_reg_id2cred_def_id; 40, argument_list; 40, 41; 41, identifier:rr_id; 42, block; 42, 43; 43, continue_statement; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:entry; 47, subscript; 47, 48; 47, 49; 48, identifier:self; 49, identifier:rr_id; 50, if_statement; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:entry; 53, identifier:rr_delta_frames; 54, block; 54, 55; 54, 74; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:to; 58, attribute; 58, 59; 58, 73; 59, call; 59, 60; 59, 61; 60, identifier:max; 61, argument_list; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:entry; 64, identifier:rr_delta_frames; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:key; 67, lambda; 67, 68; 67, 70; 68, lambda_parameters; 68, 69; 69, identifier:f; 70, attribute; 70, 71; 70, 72; 71, identifier:f; 72, identifier:to; 73, identifier:to; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:fro; 77, call; 77, 78; 77, 79; 78, identifier:min; 79, argument_list; 79, 80; 79, 83; 80, boolean_operator:or; 80, 81; 80, 82; 81, identifier:fro; 82, identifier:to; 83, identifier:to; 84, if_statement; 84, 85; 84, 90; 85, not_operator; 85, 86; 86, parenthesized_expression; 86, 87; 87, boolean_operator:and; 87, 88; 87, 89; 88, identifier:fro; 89, identifier:to; 90, block; 90, 91; 90, 99; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:LOGGER; 95, identifier:debug; 96, argument_list; 96, 97; 96, 98; 97, string:'RevocationCache.dflt_interval <!< No data for default non-revoc interval on cred def id %s'; 98, identifier:cd_id; 99, raise_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:CacheIndex; 102, argument_list; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, string:'No data for default non-revoc interval on cred def id {}'; 106, identifier:format; 107, argument_list; 107, 108; 108, identifier:cd_id; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:rv; 112, tuple; 112, 113; 112, 114; 113, identifier:fro; 114, identifier:to; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:LOGGER; 119, identifier:debug; 120, argument_list; 120, 121; 120, 122; 121, string:'RevocationCache.dflt_interval <<< %s'; 122, identifier:rv; 123, return_statement; 123, 124; 124, identifier:rv
def dflt_interval(self, cd_id: str) -> (int, int): """ Return default non-revocation interval from latest 'to' times on delta frames of revocation cache entries on indices stemming from input cred def id. Compute the 'from'/'to' values as the earliest/latest 'to' values of all cached delta frames on all rev reg ids stemming from the input cred def id. E.g., on frames for rev-reg-0: -[xx]---[xxxx]-[x]---[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]--> time rev-reg-1: ----------------------[xxxx]----[xxx]---[xxxxxxxxxxxxxxxxxxxx]---------> time rev-reg-2: -------------------------------------------[xx]-----[xxxx]-----[xxxxx]-> time rev-reg-3: -----------------------------------------------------------[xxxxxxxx]--> time return the most recent interval covering all matching revocation registries in the cache; i.e.,: interval: -------------------------------------------------------------[*******]-> time Raise CacheIndex if there are no matching entries. :param cd_id: cred def identifier to match :return: default non-revocation interval as 2-tuple (fro, to) """ LOGGER.debug('RevocationCache.dflt_interval >>>') fro = None to = None for rr_id in self: if cd_id != rev_reg_id2cred_def_id(rr_id): continue entry = self[rr_id] if entry.rr_delta_frames: to = max(entry.rr_delta_frames, key=lambda f: f.to).to fro = min(fro or to, to) if not (fro and to): LOGGER.debug( 'RevocationCache.dflt_interval <!< No data for default non-revoc interval on cred def id %s', cd_id) raise CacheIndex('No data for default non-revoc interval on cred def id {}'.format(cd_id)) rv = (fro, to) LOGGER.debug('RevocationCache.dflt_interval <<< %s', rv) return rv
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 1, 15; 2, function_name:parse; 3, parameters; 3, 4; 3, 8; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:base_dir; 6, type; 6, 7; 7, identifier:str; 8, typed_default_parameter; 8, 9; 8, 10; 8, 12; 9, identifier:timestamp; 10, type; 10, 11; 11, identifier:int; 12, None; 13, type; 13, 14; 14, identifier:int; 15, block; 15, 16; 15, 18; 15, 27; 15, 50; 15, 102; 15, 113; 15, 137; 15, 180; 15, 251; 15, 395; 15, 403; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:LOGGER; 22, identifier:debug; 23, argument_list; 23, 24; 23, 25; 23, 26; 24, string:'parse >>> base_dir: %s, timestamp: %s'; 25, identifier:base_dir; 26, identifier:timestamp; 27, if_statement; 27, 28; 27, 33; 28, not_operator; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:isdir; 31, argument_list; 31, 32; 32, identifier:base_dir; 33, block; 33, 34; 33, 41; 33, 48; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:LOGGER; 38, identifier:info; 39, argument_list; 39, 40; 40, string:'No cache archives available: not feeding cache'; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:LOGGER; 45, identifier:debug; 46, argument_list; 46, 47; 47, string:'parse <<< None'; 48, return_statement; 48, 49; 49, None; 50, if_statement; 50, 51; 50, 53; 51, not_operator; 51, 52; 52, identifier:timestamp; 53, block; 53, 54; 53, 74; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:timestamps; 57, list_comprehension; 57, 58; 57, 62; 57, 68; 58, call; 58, 59; 58, 60; 59, identifier:int; 60, argument_list; 60, 61; 61, identifier:t; 62, for_in_clause; 62, 63; 62, 64; 63, identifier:t; 64, call; 64, 65; 64, 66; 65, identifier:listdir; 66, argument_list; 66, 67; 67, identifier:base_dir; 68, if_clause; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:t; 72, identifier:isdigit; 73, argument_list; 74, if_statement; 74, 75; 74, 76; 74, 84; 75, identifier:timestamps; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:timestamp; 80, call; 80, 81; 80, 82; 81, identifier:max; 82, argument_list; 82, 83; 83, identifier:timestamps; 84, else_clause; 84, 85; 85, block; 85, 86; 85, 93; 85, 100; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:LOGGER; 90, identifier:info; 91, argument_list; 91, 92; 92, string:'No cache archives available: not feeding cache'; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:LOGGER; 97, identifier:debug; 98, argument_list; 98, 99; 99, string:'parse <<< None'; 100, return_statement; 100, 101; 101, None; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:timestamp_dir; 105, call; 105, 106; 105, 107; 106, identifier:join; 107, argument_list; 107, 108; 107, 109; 108, identifier:base_dir; 109, call; 109, 110; 109, 111; 110, identifier:str; 111, argument_list; 111, 112; 112, identifier:timestamp; 113, if_statement; 113, 114; 113, 119; 114, not_operator; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:isdir; 117, argument_list; 117, 118; 118, identifier:timestamp_dir; 119, block; 119, 120; 119, 128; 119, 135; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:LOGGER; 124, identifier:error; 125, argument_list; 125, 126; 125, 127; 126, string:'No such archived cache directory: %s'; 127, identifier:timestamp_dir; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:LOGGER; 132, identifier:debug; 133, argument_list; 133, 134; 134, string:'parse <<< None'; 135, return_statement; 135, 136; 136, None; 137, with_statement; 137, 138; 137, 143; 138, with_clause; 138, 139; 139, with_item; 139, 140; 140, attribute; 140, 141; 140, 142; 141, identifier:SCHEMA_CACHE; 142, identifier:lock; 143, block; 143, 144; 144, with_statement; 144, 145; 144, 159; 145, with_clause; 145, 146; 146, with_item; 146, 147; 147, as_pattern; 147, 148; 147, 157; 148, call; 148, 149; 148, 150; 149, identifier:open; 150, argument_list; 150, 151; 150, 156; 151, call; 151, 152; 151, 153; 152, identifier:join; 153, argument_list; 153, 154; 153, 155; 154, identifier:timestamp_dir; 155, string:'schema'; 156, string:'r'; 157, as_pattern_target; 157, 158; 158, identifier:archive; 159, block; 159, 160; 159, 173; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:schemata; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:json; 166, identifier:loads; 167, argument_list; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:archive; 171, identifier:read; 172, argument_list; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:SCHEMA_CACHE; 177, identifier:feed; 178, argument_list; 178, 179; 179, identifier:schemata; 180, with_statement; 180, 181; 180, 186; 181, with_clause; 181, 182; 182, with_item; 182, 183; 183, attribute; 183, 184; 183, 185; 184, identifier:CRED_DEF_CACHE; 185, identifier:lock; 186, block; 186, 187; 187, with_statement; 187, 188; 187, 202; 188, with_clause; 188, 189; 189, with_item; 189, 190; 190, as_pattern; 190, 191; 190, 200; 191, call; 191, 192; 191, 193; 192, identifier:open; 193, argument_list; 193, 194; 193, 199; 194, call; 194, 195; 194, 196; 195, identifier:join; 196, argument_list; 196, 197; 196, 198; 197, identifier:timestamp_dir; 198, string:'cred_def'; 199, string:'r'; 200, as_pattern_target; 200, 201; 201, identifier:archive; 202, block; 202, 203; 202, 216; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:cred_defs; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:json; 209, identifier:loads; 210, argument_list; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:archive; 214, identifier:read; 215, argument_list; 216, for_statement; 216, 217; 216, 218; 216, 219; 217, identifier:cd_id; 218, identifier:cred_defs; 219, block; 219, 220; 220, if_statement; 220, 221; 220, 224; 220, 233; 221, comparison_operator:in; 221, 222; 221, 223; 222, identifier:cd_id; 223, identifier:CRED_DEF_CACHE; 224, block; 224, 225; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:LOGGER; 229, identifier:warning; 230, argument_list; 230, 231; 230, 232; 231, string:'Cred def cache already has cred def on %s: skipping'; 232, identifier:cd_id; 233, else_clause; 233, 234; 234, block; 234, 235; 234, 243; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:CRED_DEF_CACHE; 239, identifier:cd_id; 240, subscript; 240, 241; 240, 242; 241, identifier:cred_defs; 242, identifier:cd_id; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:LOGGER; 247, identifier:info; 248, argument_list; 248, 249; 248, 250; 249, string:'Cred def cache imported cred def for cred def id %s'; 250, identifier:cd_id; 251, with_statement; 251, 252; 251, 257; 252, with_clause; 252, 253; 253, with_item; 253, 254; 254, attribute; 254, 255; 254, 256; 255, identifier:REVO_CACHE; 256, identifier:lock; 257, block; 257, 258; 258, with_statement; 258, 259; 258, 273; 259, with_clause; 259, 260; 260, with_item; 260, 261; 261, as_pattern; 261, 262; 261, 271; 262, call; 262, 263; 262, 264; 263, identifier:open; 264, argument_list; 264, 265; 264, 270; 265, call; 265, 266; 265, 267; 266, identifier:join; 267, argument_list; 267, 268; 267, 269; 268, identifier:timestamp_dir; 269, string:'revocation'; 270, string:'r'; 271, as_pattern_target; 271, 272; 272, identifier:archive; 273, block; 273, 274; 273, 287; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:rr_cache_entries; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:json; 280, identifier:loads; 281, argument_list; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:archive; 285, identifier:read; 286, argument_list; 287, for_statement; 287, 288; 287, 291; 287, 296; 288, tuple_pattern; 288, 289; 288, 290; 289, identifier:rr_id; 290, identifier:entry; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:rr_cache_entries; 294, identifier:items; 295, argument_list; 296, block; 296, 297; 297, if_statement; 297, 298; 297, 301; 297, 310; 298, comparison_operator:in; 298, 299; 298, 300; 299, identifier:rr_id; 300, identifier:REVO_CACHE; 301, block; 301, 302; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:LOGGER; 306, identifier:warning; 307, argument_list; 307, 308; 307, 309; 308, string:'Revocation cache already has entry on %s: skipping'; 309, identifier:rr_id; 310, else_clause; 310, 311; 311, block; 311, 312; 311, 321; 311, 344; 311, 351; 311, 374; 311, 381; 311, 387; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:rr_cache_entry; 315, call; 315, 316; 315, 317; 316, identifier:RevoCacheEntry; 317, argument_list; 317, 318; 318, subscript; 318, 319; 318, 320; 319, identifier:entry; 320, string:'rev_reg_def'; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:rr_cache_entry; 325, identifier:rr_delta_frames; 326, list_comprehension; 326, 327; 326, 339; 327, call; 327, 328; 327, 329; 328, identifier:RevRegUpdateFrame; 329, argument_list; 329, 330; 329, 333; 329, 336; 330, subscript; 330, 331; 330, 332; 331, identifier:f; 332, string:'_to'; 333, subscript; 333, 334; 333, 335; 334, identifier:f; 335, string:'_timestamp'; 336, subscript; 336, 337; 336, 338; 337, identifier:f; 338, string:'_rr_update'; 339, for_in_clause; 339, 340; 339, 341; 340, identifier:f; 341, subscript; 341, 342; 341, 343; 342, identifier:entry; 343, string:'rr_delta_frames'; 344, expression_statement; 344, 345; 345, call; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:rr_cache_entry; 348, identifier:cull; 349, argument_list; 349, 350; 350, True; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:rr_cache_entry; 355, identifier:rr_state_frames; 356, list_comprehension; 356, 357; 356, 369; 357, call; 357, 358; 357, 359; 358, identifier:RevRegUpdateFrame; 359, argument_list; 359, 360; 359, 363; 359, 366; 360, subscript; 360, 361; 360, 362; 361, identifier:f; 362, string:'_to'; 363, subscript; 363, 364; 363, 365; 364, identifier:f; 365, string:'_timestamp'; 366, subscript; 366, 367; 366, 368; 367, identifier:f; 368, string:'_rr_update'; 369, for_in_clause; 369, 370; 369, 371; 370, identifier:f; 371, subscript; 371, 372; 371, 373; 372, identifier:entry; 373, string:'rr_state_frames'; 374, expression_statement; 374, 375; 375, call; 375, 376; 375, 379; 376, attribute; 376, 377; 376, 378; 377, identifier:rr_cache_entry; 378, identifier:cull; 379, argument_list; 379, 380; 380, False; 381, expression_statement; 381, 382; 382, assignment; 382, 383; 382, 386; 383, subscript; 383, 384; 383, 385; 384, identifier:REVO_CACHE; 385, identifier:rr_id; 386, identifier:rr_cache_entry; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:LOGGER; 391, identifier:info; 392, argument_list; 392, 393; 392, 394; 393, string:'Revocation cache imported entry for rev reg id %s'; 394, identifier:rr_id; 395, expression_statement; 395, 396; 396, call; 396, 397; 396, 400; 397, attribute; 397, 398; 397, 399; 398, identifier:LOGGER; 399, identifier:debug; 400, argument_list; 400, 401; 400, 402; 401, string:'parse <<< %s'; 402, identifier:timestamp; 403, return_statement; 403, 404; 404, identifier:timestamp
def parse(base_dir: str, timestamp: int = None) -> int: """ Parse and update from archived cache files. Only accept new content; do not overwrite any existing cache content. :param base_dir: archive base directory :param timestamp: epoch time of cache serving as subdirectory, default most recent :return: epoch time of cache serving as subdirectory, None if there is no such archive. """ LOGGER.debug('parse >>> base_dir: %s, timestamp: %s', base_dir, timestamp) if not isdir(base_dir): LOGGER.info('No cache archives available: not feeding cache') LOGGER.debug('parse <<< None') return None if not timestamp: timestamps = [int(t) for t in listdir(base_dir) if t.isdigit()] if timestamps: timestamp = max(timestamps) else: LOGGER.info('No cache archives available: not feeding cache') LOGGER.debug('parse <<< None') return None timestamp_dir = join(base_dir, str(timestamp)) if not isdir(timestamp_dir): LOGGER.error('No such archived cache directory: %s', timestamp_dir) LOGGER.debug('parse <<< None') return None with SCHEMA_CACHE.lock: with open(join(timestamp_dir, 'schema'), 'r') as archive: schemata = json.loads(archive.read()) SCHEMA_CACHE.feed(schemata) with CRED_DEF_CACHE.lock: with open(join(timestamp_dir, 'cred_def'), 'r') as archive: cred_defs = json.loads(archive.read()) for cd_id in cred_defs: if cd_id in CRED_DEF_CACHE: LOGGER.warning('Cred def cache already has cred def on %s: skipping', cd_id) else: CRED_DEF_CACHE[cd_id] = cred_defs[cd_id] LOGGER.info('Cred def cache imported cred def for cred def id %s', cd_id) with REVO_CACHE.lock: with open(join(timestamp_dir, 'revocation'), 'r') as archive: rr_cache_entries = json.loads(archive.read()) for (rr_id, entry) in rr_cache_entries.items(): if rr_id in REVO_CACHE: LOGGER.warning('Revocation cache already has entry on %s: skipping', rr_id) else: rr_cache_entry = RevoCacheEntry(entry['rev_reg_def']) rr_cache_entry.rr_delta_frames = [ RevRegUpdateFrame( f['_to'], f['_timestamp'], f['_rr_update']) for f in entry['rr_delta_frames'] ] rr_cache_entry.cull(True) rr_cache_entry.rr_state_frames = [ RevRegUpdateFrame( f['_to'], f['_timestamp'], f['_rr_update']) for f in entry['rr_state_frames'] ] rr_cache_entry.cull(False) REVO_CACHE[rr_id] = rr_cache_entry LOGGER.info('Revocation cache imported entry for rev reg id %s', rr_id) LOGGER.debug('parse <<< %s', timestamp) return timestamp
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_convert_hbf_meta_val_for_xml; 3, parameters; 3, 4; 3, 5; 4, identifier:key; 5, identifier:val; 6, block; 6, 7; 6, 9; 6, 26; 6, 30; 6, 34; 6, 91; 6, 155; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 15; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 14; 13, identifier:val; 14, identifier:list; 15, block; 15, 16; 16, return_statement; 16, 17; 17, list_comprehension; 17, 18; 17, 23; 18, call; 18, 19; 18, 20; 19, identifier:_convert_hbf_meta_val_for_xml; 20, argument_list; 20, 21; 20, 22; 21, identifier:key; 22, identifier:i; 23, for_in_clause; 23, 24; 23, 25; 24, identifier:i; 25, identifier:val; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:is_literal; 29, True; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:content; 33, None; 34, if_statement; 34, 35; 34, 40; 34, 81; 35, call; 35, 36; 35, 37; 36, identifier:isinstance; 37, argument_list; 37, 38; 37, 39; 38, identifier:val; 39, identifier:dict; 40, block; 40, 41; 40, 45; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:ret; 44, identifier:val; 45, if_statement; 45, 46; 45, 49; 45, 54; 46, comparison_operator:in; 46, 47; 46, 48; 47, string:'@href'; 48, identifier:val; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:is_literal; 53, False; 54, else_clause; 54, 55; 55, block; 55, 56; 55, 65; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:content; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:val; 62, identifier:get; 63, argument_list; 63, 64; 64, string:'$'; 65, if_statement; 65, 66; 65, 76; 66, boolean_operator:and; 66, 67; 66, 72; 67, call; 67, 68; 67, 69; 68, identifier:isinstance; 69, argument_list; 69, 70; 69, 71; 70, identifier:content; 71, identifier:dict; 72, call; 72, 73; 72, 74; 73, identifier:_contains_hbf_meta_keys; 74, argument_list; 74, 75; 75, identifier:val; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:is_literal; 80, False; 81, else_clause; 81, 82; 82, block; 82, 83; 82, 87; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:ret; 86, dictionary; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:content; 90, identifier:val; 91, if_statement; 91, 92; 91, 93; 91, 137; 92, identifier:is_literal; 93, block; 93, 94; 93, 102; 93, 110; 93, 126; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:ret; 98, identifier:setdefault; 99, argument_list; 99, 100; 99, 101; 100, string:'@xsi:type'; 101, string:'nex:LiteralMeta'; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:ret; 106, identifier:setdefault; 107, argument_list; 107, 108; 107, 109; 108, string:'@property'; 109, identifier:key; 110, if_statement; 110, 111; 110, 114; 111, comparison_operator:is; 111, 112; 111, 113; 112, identifier:content; 113, None; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:ret; 119, identifier:setdefault; 120, argument_list; 120, 121; 120, 122; 121, string:'@datatype'; 122, call; 122, 123; 122, 124; 123, identifier:_python_instance_to_nexml_meta_datatype; 124, argument_list; 124, 125; 125, identifier:content; 126, if_statement; 126, 127; 126, 130; 127, comparison_operator:is; 127, 128; 127, 129; 128, identifier:ret; 129, identifier:val; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:ret; 135, string:'$'; 136, identifier:content; 137, else_clause; 137, 138; 138, block; 138, 139; 138, 147; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:ret; 143, identifier:setdefault; 144, argument_list; 144, 145; 144, 146; 145, string:'@xsi:type'; 146, string:'nex:ResourceMeta'; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:ret; 151, identifier:setdefault; 152, argument_list; 152, 153; 152, 154; 153, string:'@rel'; 154, identifier:key; 155, return_statement; 155, 156; 156, identifier:ret
def _convert_hbf_meta_val_for_xml(key, val): """Convert to a BadgerFish-style dict for addition to a dict suitable for addition to XML tree or for v1.0 to v0.0 conversion.""" if isinstance(val, list): return [_convert_hbf_meta_val_for_xml(key, i) for i in val] is_literal = True content = None if isinstance(val, dict): ret = val if '@href' in val: is_literal = False else: content = val.get('$') if isinstance(content, dict) and _contains_hbf_meta_keys(val): is_literal = False else: ret = {} content = val if is_literal: ret.setdefault('@xsi:type', 'nex:LiteralMeta') ret.setdefault('@property', key) if content is not None: ret.setdefault('@datatype', _python_instance_to_nexml_meta_datatype(content)) if ret is not val: ret['$'] = content else: ret.setdefault('@xsi:type', 'nex:ResourceMeta') ret.setdefault('@rel', key) return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:validate_params_match; 3, parameters; 3, 4; 3, 5; 4, identifier:method; 5, identifier:parameters; 6, block; 6, 7; 6, 9; 6, 18; 6, 19; 6, 35; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:argspec; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:inspect; 15, identifier:getargspec; 16, argument_list; 16, 17; 17, identifier:method; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:default_length; 22, conditional_expression:if; 22, 23; 22, 29; 22, 34; 23, call; 23, 24; 23, 25; 24, identifier:len; 25, argument_list; 25, 26; 26, attribute; 26, 27; 26, 28; 27, identifier:argspec; 28, identifier:defaults; 29, comparison_operator:is; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:argspec; 32, identifier:defaults; 33, None; 34, integer:0; 35, if_statement; 35, 36; 35, 41; 35, 90; 36, call; 36, 37; 36, 38; 37, identifier:isinstance; 38, argument_list; 38, 39; 38, 40; 39, identifier:parameters; 40, identifier:list; 41, block; 41, 42; 41, 66; 41, 80; 42, if_statement; 42, 43; 42, 60; 43, boolean_operator:and; 43, 44; 43, 55; 44, comparison_operator:>; 44, 45; 44, 49; 45, call; 45, 46; 45, 47; 46, identifier:len; 47, argument_list; 47, 48; 48, identifier:parameters; 49, call; 49, 50; 49, 51; 50, identifier:len; 51, argument_list; 51, 52; 52, attribute; 52, 53; 52, 54; 53, identifier:argspec; 54, identifier:args; 55, comparison_operator:is; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:argspec; 58, identifier:varargs; 59, None; 60, block; 60, 61; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:InvalidParamsError; 64, argument_list; 64, 65; 65, string:"Too many parameters"; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:remaining_parameters; 69, binary_operator:-; 69, 70; 69, 76; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, attribute; 73, 74; 73, 75; 74, identifier:argspec; 75, identifier:args; 76, call; 76, 77; 76, 78; 77, identifier:len; 78, argument_list; 78, 79; 79, identifier:parameters; 80, if_statement; 80, 81; 80, 84; 81, comparison_operator:>; 81, 82; 81, 83; 82, identifier:remaining_parameters; 83, identifier:default_length; 84, block; 84, 85; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:InvalidParamsError; 88, argument_list; 88, 89; 89, string:"Not enough parameters"; 90, elif_clause; 90, 91; 90, 96; 91, call; 91, 92; 91, 93; 92, identifier:isinstance; 93, argument_list; 93, 94; 93, 95; 94, identifier:parameters; 95, identifier:dict; 96, block; 96, 97; 96, 111; 96, 131; 96, 150; 96, 164; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:missing_parameters; 100, list_comprehension; 100, 101; 100, 102; 100, 107; 101, identifier:key; 102, for_in_clause; 102, 103; 102, 104; 103, identifier:key; 104, attribute; 104, 105; 104, 106; 105, identifier:argspec; 106, identifier:args; 107, if_clause; 107, 108; 108, comparison_operator:not; 108, 109; 108, 110; 109, identifier:key; 110, identifier:parameters; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:default_parameters; 114, call; 114, 115; 114, 116; 115, identifier:set; 116, argument_list; 116, 117; 117, subscript; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:argspec; 120, identifier:args; 121, slice; 121, 122; 121, 130; 122, binary_operator:-; 122, 123; 122, 129; 123, call; 123, 124; 123, 125; 124, identifier:len; 125, argument_list; 125, 126; 126, attribute; 126, 127; 126, 128; 127, identifier:argspec; 128, identifier:args; 129, identifier:default_length; 130, colon; 131, for_statement; 131, 132; 131, 133; 131, 134; 132, identifier:key; 133, identifier:missing_parameters; 134, block; 134, 135; 135, if_statement; 135, 136; 135, 139; 136, comparison_operator:not; 136, 137; 136, 138; 137, identifier:key; 138, identifier:default_parameters; 139, block; 139, 140; 140, raise_statement; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:InvalidParamsError; 143, argument_list; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, string:"Parameter {} has not been satisfied"; 147, identifier:format; 148, argument_list; 148, 149; 149, identifier:key; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:extra_params; 153, list_comprehension; 153, 154; 153, 155; 153, 158; 154, identifier:key; 155, for_in_clause; 155, 156; 155, 157; 156, identifier:key; 157, identifier:parameters; 158, if_clause; 158, 159; 159, comparison_operator:not; 159, 160; 159, 161; 160, identifier:key; 161, attribute; 161, 162; 161, 163; 162, identifier:argspec; 163, identifier:args; 164, if_statement; 164, 165; 164, 177; 165, boolean_operator:and; 165, 166; 165, 172; 166, comparison_operator:>; 166, 167; 166, 171; 167, call; 167, 168; 167, 169; 168, identifier:len; 169, argument_list; 169, 170; 170, identifier:extra_params; 171, integer:0; 172, comparison_operator:is; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:argspec; 175, identifier:keywords; 176, None; 177, block; 177, 178; 178, raise_statement; 178, 179; 179, call; 179, 180; 179, 181; 180, identifier:InvalidParamsError; 181, argument_list; 181, 182; 182, string:"Too many parameters"
def validate_params_match(method, parameters): """Validates that the given parameters are exactly the method's declared parameters. :param method: The method to be called :type method: function :param parameters: The parameters to use in the call :type parameters: dict[str, object] | list[object] """ argspec = inspect.getargspec(method) # pylint: disable=deprecated-method default_length = len(argspec.defaults) if argspec.defaults is not None else 0 if isinstance(parameters, list): if len(parameters) > len(argspec.args) and argspec.varargs is None: raise InvalidParamsError("Too many parameters") remaining_parameters = len(argspec.args) - len(parameters) if remaining_parameters > default_length: raise InvalidParamsError("Not enough parameters") elif isinstance(parameters, dict): missing_parameters = [key for key in argspec.args if key not in parameters] default_parameters = set(argspec.args[len(argspec.args) - default_length:]) for key in missing_parameters: if key not in default_parameters: raise InvalidParamsError("Parameter {} has not been satisfied".format(key)) extra_params = [key for key in parameters if key not in argspec.args] if len(extra_params) > 0 and argspec.keywords is None: raise InvalidParamsError("Too many parameters")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:addcols; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:X; 5, identifier:cols; 6, default_parameter; 6, 7; 6, 8; 7, identifier:names; 8, None; 9, block; 9, 10; 9, 12; 9, 36; 9, 190; 9, 210; 9, 240; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 18; 13, call; 13, 14; 13, 15; 14, identifier:isinstance; 15, argument_list; 15, 16; 15, 17; 16, identifier:names; 17, identifier:str; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:names; 22, list_comprehension; 22, 23; 22, 28; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:n; 26, identifier:strip; 27, argument_list; 28, for_in_clause; 28, 29; 28, 30; 29, identifier:n; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:names; 33, identifier:split; 34, argument_list; 34, 35; 35, string:','; 36, if_statement; 36, 37; 36, 42; 36, 153; 37, call; 37, 38; 37, 39; 38, identifier:isinstance; 39, argument_list; 39, 40; 39, 41; 40, identifier:cols; 41, identifier:list; 42, block; 42, 43; 43, if_statement; 43, 44; 43, 71; 43, 122; 44, call; 44, 45; 44, 46; 45, identifier:any; 46, argument_list; 46, 47; 47, list_comprehension; 47, 48; 47, 68; 48, boolean_operator:or; 48, 49; 48, 62; 48, 63; 49, boolean_operator:or; 49, 50; 49, 57; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:x; 54, attribute; 54, 55; 54, 56; 55, identifier:np; 56, identifier:ndarray; 57, call; 57, 58; 57, 59; 58, identifier:isinstance; 59, argument_list; 59, 60; 59, 61; 60, identifier:x; 61, identifier:list; 62, line_continuation:\; 63, call; 63, 64; 63, 65; 64, identifier:isinstance; 65, argument_list; 65, 66; 65, 67; 66, identifier:x; 67, identifier:tuple; 68, for_in_clause; 68, 69; 68, 70; 69, identifier:x; 70, identifier:cols; 71, block; 71, 72; 71, 90; 71, 105; 72, assert_statement; 72, 73; 72, 89; 73, call; 73, 74; 73, 75; 74, identifier:all; 75, argument_list; 75, 76; 76, list_comprehension; 76, 77; 76, 86; 77, comparison_operator:==; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:len; 80, argument_list; 80, 81; 81, identifier:x; 82, call; 82, 83; 82, 84; 83, identifier:len; 84, argument_list; 84, 85; 85, identifier:X; 86, for_in_clause; 86, 87; 86, 88; 87, identifier:x; 88, identifier:cols; 89, string:'Trying to add columns of wrong length.'; 90, assert_statement; 90, 91; 90, 104; 91, boolean_operator:and; 91, 92; 91, 95; 92, comparison_operator:!=; 92, 93; 92, 94; 93, identifier:names; 94, None; 95, comparison_operator:==; 95, 96; 95, 100; 96, call; 96, 97; 96, 98; 97, identifier:len; 98, argument_list; 98, 99; 99, identifier:cols; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, identifier:names; 104, string:'Number of columns to add must equal number of new names.'; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:cols; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:utils; 111, identifier:fromarrays; 112, argument_list; 112, 113; 112, 114; 112, 119; 113, identifier:cols; 114, keyword_argument; 114, 115; 114, 116; 115, identifier:type; 116, attribute; 116, 117; 116, 118; 117, identifier:np; 118, identifier:ndarray; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:names; 121, identifier:names; 122, else_clause; 122, 123; 123, block; 123, 124; 123, 135; 124, assert_statement; 124, 125; 124, 134; 125, comparison_operator:==; 125, 126; 125, 130; 126, call; 126, 127; 126, 128; 127, identifier:len; 128, argument_list; 128, 129; 129, identifier:cols; 130, call; 130, 131; 130, 132; 131, identifier:len; 132, argument_list; 132, 133; 133, identifier:X; 134, string:'Trying to add column of wrong length.'; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:cols; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:utils; 141, identifier:fromarrays; 142, argument_list; 142, 143; 142, 145; 142, 150; 143, list:[cols]; 143, 144; 144, identifier:cols; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:type; 147, attribute; 147, 148; 147, 149; 148, identifier:np; 149, identifier:ndarray; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:names; 152, identifier:names; 153, else_clause; 153, 154; 154, block; 154, 155; 154, 163; 155, assert_statement; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:isinstance; 158, argument_list; 158, 159; 158, 160; 159, identifier:cols; 160, attribute; 160, 161; 160, 162; 161, identifier:np; 162, identifier:ndarray; 163, if_statement; 163, 164; 163, 171; 164, comparison_operator:==; 164, 165; 164, 170; 165, attribute; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:cols; 168, identifier:dtype; 169, identifier:names; 170, None; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:cols; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:utils; 178, identifier:fromarrays; 179, argument_list; 179, 180; 179, 182; 179, 187; 180, list:[cols]; 180, 181; 181, identifier:cols; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:type; 184, attribute; 184, 185; 184, 186; 185, identifier:np; 186, identifier:ndarray; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:names; 189, identifier:names; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:Replacements; 193, list_comprehension; 193, 194; 193, 195; 193, 202; 194, identifier:a; 195, for_in_clause; 195, 196; 195, 197; 196, identifier:a; 197, attribute; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:cols; 200, identifier:dtype; 201, identifier:names; 202, if_clause; 202, 203; 203, comparison_operator:in; 203, 204; 203, 205; 204, identifier:a; 205, attribute; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:X; 208, identifier:dtype; 209, identifier:names; 210, if_statement; 210, 211; 210, 217; 211, comparison_operator:>; 211, 212; 211, 216; 212, call; 212, 213; 212, 214; 213, identifier:len; 214, argument_list; 214, 215; 215, identifier:Replacements; 216, integer:0; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:print; 221, argument_list; 221, 222; 221, 223; 222, string:'Replacing columns'; 223, list_comprehension; 223, 224; 223, 225; 223, 232; 224, identifier:a; 225, for_in_clause; 225, 226; 225, 227; 226, identifier:a; 227, attribute; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:cols; 230, identifier:dtype; 231, identifier:names; 232, if_clause; 232, 233; 233, comparison_operator:in; 233, 234; 233, 235; 234, identifier:a; 235, attribute; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:X; 238, identifier:dtype; 239, identifier:names; 240, return_statement; 240, 241; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:utils; 244, identifier:fromarrays; 245, argument_list; 245, 246; 245, 288; 245, 293; 246, binary_operator:+; 246, 247; 246, 269; 247, list_comprehension; 247, 248; 247, 262; 248, conditional_expression:if; 248, 249; 248, 252; 248, 259; 249, subscript; 249, 250; 249, 251; 250, identifier:X; 251, identifier:a; 252, comparison_operator:not; 252, 253; 252, 254; 253, identifier:a; 254, attribute; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:cols; 257, identifier:dtype; 258, identifier:names; 259, subscript; 259, 260; 259, 261; 260, identifier:cols; 261, identifier:a; 262, for_in_clause; 262, 263; 262, 264; 263, identifier:a; 264, attribute; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:X; 267, identifier:dtype; 268, identifier:names; 269, list_comprehension; 269, 270; 269, 273; 269, 280; 270, subscript; 270, 271; 270, 272; 271, identifier:cols; 272, identifier:a; 273, for_in_clause; 273, 274; 273, 275; 274, identifier:a; 275, attribute; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:cols; 278, identifier:dtype; 279, identifier:names; 280, if_clause; 280, 281; 281, comparison_operator:not; 281, 282; 281, 283; 282, identifier:a; 283, attribute; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:X; 286, identifier:dtype; 287, identifier:names; 288, keyword_argument; 288, 289; 288, 290; 289, identifier:type; 290, attribute; 290, 291; 290, 292; 291, identifier:np; 292, identifier:ndarray; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:names; 295, binary_operator:+; 295, 296; 295, 304; 296, call; 296, 297; 296, 298; 297, identifier:list; 298, argument_list; 298, 299; 299, attribute; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:X; 302, identifier:dtype; 303, identifier:names; 304, list_comprehension; 304, 305; 304, 306; 304, 313; 305, identifier:a; 306, for_in_clause; 306, 307; 306, 308; 307, identifier:a; 308, attribute; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:cols; 311, identifier:dtype; 312, identifier:names; 313, if_clause; 313, 314; 314, comparison_operator:not; 314, 315; 314, 316; 315, identifier:a; 316, attribute; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:X; 319, identifier:dtype; 320, identifier:names
def addcols(X, cols, names=None): """ Add one or more columns to a numpy ndarray. Technical dependency of :func:`tabular.spreadsheet.aggregate_in`. Implemented by the tabarray method :func:`tabular.tab.tabarray.addcols`. **Parameters** **X** : numpy ndarray with structured dtype or recarray The recarray to add columns to. **cols** : numpy ndarray, or list of arrays of columns Column(s) to add. **names**: list of strings, optional Names of the new columns. Only applicable when `cols` is a list of arrays. **Returns** **out** : numpy ndarray with structured dtype New numpy array made up of `X` plus the new columns. **See also:** :func:`tabular.spreadsheet.colstack` """ if isinstance(names,str): names = [n.strip() for n in names.split(',')] if isinstance(cols, list): if any([isinstance(x,np.ndarray) or isinstance(x,list) or \ isinstance(x,tuple) for x in cols]): assert all([len(x) == len(X) for x in cols]), \ 'Trying to add columns of wrong length.' assert names != None and len(cols) == len(names), \ 'Number of columns to add must equal number of new names.' cols = utils.fromarrays(cols,type=np.ndarray,names = names) else: assert len(cols) == len(X), 'Trying to add column of wrong length.' cols = utils.fromarrays([cols], type=np.ndarray,names=names) else: assert isinstance(cols, np.ndarray) if cols.dtype.names == None: cols = utils.fromarrays([cols],type=np.ndarray, names=names) Replacements = [a for a in cols.dtype.names if a in X.dtype.names] if len(Replacements) > 0: print('Replacing columns', [a for a in cols.dtype.names if a in X.dtype.names]) return utils.fromarrays( [X[a] if a not in cols.dtype.names else cols[a] for a in X.dtype.names] + [cols[a] for a in cols.dtype.names if a not in X.dtype.names], type=np.ndarray, names=list(X.dtype.names) + [a for a in cols.dtype.names if a not in X.dtype.names])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:replace; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:X; 5, identifier:old; 6, identifier:new; 7, default_parameter; 7, 8; 7, 9; 8, identifier:strict; 9, True; 10, default_parameter; 10, 11; 10, 12; 11, identifier:cols; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:rows; 15, None; 16, block; 16, 17; 16, 19; 16, 48; 16, 67; 17, expression_statement; 17, 18; 18, comment; 19, if_statement; 19, 20; 19, 23; 19, 32; 20, comparison_operator:==; 20, 21; 20, 22; 21, identifier:cols; 22, None; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:cols; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:X; 30, identifier:dtype; 31, identifier:names; 32, elif_clause; 32, 33; 32, 38; 33, call; 33, 34; 33, 35; 34, identifier:isinstance; 35, argument_list; 35, 36; 35, 37; 36, identifier:cols; 37, identifier:str; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:cols; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:cols; 45, identifier:split; 46, argument_list; 46, 47; 47, string:','; 48, if_statement; 48, 49; 48, 52; 49, comparison_operator:==; 49, 50; 49, 51; 50, identifier:rows; 51, None; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:rows; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:np; 59, identifier:ones; 60, argument_list; 60, 61; 60, 66; 61, tuple; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:len; 64, argument_list; 64, 65; 65, identifier:X; 66, identifier:bool; 67, if_statement; 67, 68; 67, 69; 67, 129; 68, identifier:strict; 69, block; 69, 70; 69, 79; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:new; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:np; 76, identifier:array; 77, argument_list; 77, 78; 78, identifier:new; 79, for_statement; 79, 80; 79, 81; 79, 82; 80, identifier:a; 81, identifier:cols; 82, block; 82, 83; 82, 103; 83, if_statement; 83, 84; 83, 93; 84, comparison_operator:<; 84, 85; 84, 90; 85, subscript; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:X; 88, identifier:dtype; 89, identifier:a; 90, attribute; 90, 91; 90, 92; 91, identifier:new; 92, identifier:dtype; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:print; 97, argument_list; 97, 98; 97, 99; 97, 100; 97, 101; 97, 102; 98, string:'WARNING: dtype of column'; 99, identifier:a; 100, string:'is inferior to dtype of '; 101, identifier:new; 102, string:'which may cause problems.'; 103, try_statement; 103, 104; 103, 120; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 119; 107, subscript; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:X; 110, identifier:a; 111, subscript; 111, 112; 111, 118; 112, parenthesized_expression; 112, 113; 113, comparison_operator:==; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:X; 116, identifier:a; 117, identifier:old; 118, identifier:rows; 119, identifier:new; 120, except_clause; 120, 121; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:print; 125, argument_list; 125, 126; 125, 127; 125, 128; 126, string:'Replacement not made on column'; 127, identifier:a; 128, string:'.'; 129, else_clause; 129, 130; 130, block; 130, 131; 131, for_statement; 131, 132; 131, 133; 131, 134; 132, identifier:a; 133, identifier:cols; 134, block; 134, 135; 134, 139; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:QuickRep; 138, True; 139, try_statement; 139, 140; 139, 154; 139, 164; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:colstr; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, string:''; 147, identifier:join; 148, argument_list; 148, 149; 149, subscript; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:X; 152, identifier:a; 153, identifier:rows; 154, except_clause; 154, 155; 154, 156; 155, identifier:TypeError; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:print; 160, argument_list; 160, 161; 160, 162; 160, 163; 161, string:'Not replacing in column'; 162, identifier:a; 163, string:'due to type mismatch.'; 164, else_clause; 164, 165; 165, block; 165, 166; 165, 186; 165, 201; 165, 271; 165, 328; 165, 347; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:avoid; 169, list_comprehension; 169, 170; 169, 174; 170, call; 170, 171; 170, 172; 171, identifier:ord; 172, argument_list; 172, 173; 173, identifier:o; 174, for_in_clause; 174, 175; 174, 176; 175, identifier:o; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:utils; 179, identifier:uniqify; 180, argument_list; 180, 181; 181, binary_operator:+; 181, 182; 181, 185; 182, binary_operator:+; 182, 183; 182, 184; 183, identifier:old; 184, identifier:new; 185, identifier:colstr; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:ok; 189, call; 189, 190; 189, 199; 190, attribute; 190, 191; 190, 198; 191, call; 191, 192; 191, 193; 192, identifier:set; 193, argument_list; 193, 194; 194, call; 194, 195; 194, 196; 195, identifier:range; 196, argument_list; 196, 197; 197, integer:256; 198, identifier:difference; 199, argument_list; 199, 200; 200, identifier:avoid; 201, if_statement; 201, 202; 201, 208; 201, 221; 202, comparison_operator:>; 202, 203; 202, 207; 203, call; 203, 204; 203, 205; 204, identifier:len; 205, argument_list; 205, 206; 206, identifier:ok; 207, integer:0; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:sep; 212, call; 212, 213; 212, 214; 213, identifier:chr; 214, argument_list; 214, 215; 215, subscript; 215, 216; 215, 220; 216, call; 216, 217; 216, 218; 217, identifier:list; 218, argument_list; 218, 219; 219, identifier:ok; 220, integer:0; 221, else_clause; 221, 222; 222, block; 222, 223; 222, 238; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:ok; 226, call; 226, 227; 226, 236; 227, attribute; 227, 228; 227, 235; 228, call; 228, 229; 228, 230; 229, identifier:set; 230, argument_list; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:range; 233, argument_list; 233, 234; 234, integer:65536; 235, identifier:difference; 236, argument_list; 236, 237; 237, identifier:avoid; 238, if_statement; 238, 239; 238, 245; 238, 258; 239, comparison_operator:>; 239, 240; 239, 244; 240, call; 240, 241; 240, 242; 241, identifier:len; 242, argument_list; 242, 243; 243, identifier:ok; 244, integer:0; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:sep; 249, call; 249, 250; 249, 251; 250, identifier:unichr; 251, argument_list; 251, 252; 252, subscript; 252, 253; 252, 257; 253, call; 253, 254; 253, 255; 254, identifier:list; 255, argument_list; 255, 256; 256, identifier:ok; 257, integer:0; 258, else_clause; 258, 259; 259, block; 259, 260; 259, 267; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 263; 262, identifier:print; 263, argument_list; 263, 264; 263, 265; 263, 266; 264, string:'All unicode characters represented in column'; 265, identifier:a; 266, string:', can\t replace quickly.'; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:QuickRep; 270, False; 271, if_statement; 271, 272; 271, 273; 271, 303; 272, identifier:QuickRep; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:newrows; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:np; 280, identifier:array; 281, argument_list; 281, 282; 282, call; 282, 283; 282, 301; 283, attribute; 283, 284; 283, 300; 284, call; 284, 285; 284, 297; 285, attribute; 285, 286; 285, 296; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:sep; 289, identifier:join; 290, argument_list; 290, 291; 291, subscript; 291, 292; 291, 295; 292, subscript; 292, 293; 292, 294; 293, identifier:X; 294, identifier:a; 295, identifier:rows; 296, identifier:replace; 297, argument_list; 297, 298; 297, 299; 298, identifier:old; 299, identifier:new; 300, identifier:split; 301, argument_list; 301, 302; 302, identifier:sep; 303, else_clause; 303, 304; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:newrows; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:np; 311, identifier:array; 312, argument_list; 312, 313; 313, list_comprehension; 313, 314; 313, 321; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:aa; 317, identifier:replace; 318, argument_list; 318, 319; 318, 320; 319, identifier:old; 320, identifier:new; 321, for_in_clause; 321, 322; 321, 323; 322, identifier:aa; 323, subscript; 323, 324; 323, 327; 324, subscript; 324, 325; 324, 326; 325, identifier:X; 326, identifier:a; 327, identifier:rows; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 335; 330, subscript; 330, 331; 330, 334; 331, subscript; 331, 332; 331, 333; 332, identifier:X; 333, identifier:a; 334, identifier:rows; 335, call; 335, 336; 335, 345; 336, subscript; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:np; 339, identifier:cast; 340, subscript; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:X; 343, identifier:dtype; 344, identifier:a; 345, argument_list; 345, 346; 346, identifier:newrows; 347, if_statement; 347, 348; 347, 357; 348, comparison_operator:>; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:newrows; 351, identifier:dtype; 352, subscript; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:X; 355, identifier:dtype; 356, identifier:a; 357, block; 357, 358; 358, expression_statement; 358, 359; 359, call; 359, 360; 359, 361; 360, identifier:print; 361, argument_list; 361, 362; 361, 363; 361, 364; 362, string:'WARNING: dtype of column'; 363, identifier:a; 364, concatenated_string; 364, 365; 364, 366; 364, 367; 365, string:'is inferior to the '; 366, string:'dtype of its replacement which may cause problems '; 367, string:'(ends of strings might get chopped off).'
def replace(X, old, new, strict=True, cols=None, rows=None): """ Replace value `old` with `new` everywhere it appears in-place. Implemented by the tabarray method :func:`tabular.tab.tabarray.replace`. **Parameters** **X** : numpy ndarray with structured dtype Numpy array for which in-place replacement of `old` with `new` is to be done. **old** : string **new** : string **strict** : boolean, optional * If `strict` = `True`, replace only exact occurences of `old`. * If `strict` = `False`, assume `old` and `new` are strings and replace all occurences of substrings (e.g. like :func:`str.replace`) **cols** : list of strings, optional Names of columns to make replacements in; if `None`, make replacements everywhere. **rows** : list of booleans or integers, optional Rows to make replacements in; if `None`, make replacements everywhere. Note: This function does in-place replacements. Thus there are issues handling data types here when replacement dtype is larger than original dtype. This can be resolved later by making a new array when necessary ... """ if cols == None: cols = X.dtype.names elif isinstance(cols, str): cols = cols.split(',') if rows == None: rows = np.ones((len(X),), bool) if strict: new = np.array(new) for a in cols: if X.dtype[a] < new.dtype: print('WARNING: dtype of column', a, 'is inferior to dtype of ', new, 'which may cause problems.') try: X[a][(X[a] == old)[rows]] = new except: print('Replacement not made on column', a, '.') else: for a in cols: QuickRep = True try: colstr = ''.join(X[a][rows]) except TypeError: print('Not replacing in column', a, 'due to type mismatch.') else: avoid = [ord(o) for o in utils.uniqify(old + new + colstr)] ok = set(range(256)).difference(avoid) if len(ok) > 0: sep = chr(list(ok)[0]) else: ok = set(range(65536)).difference(avoid) if len(ok) > 0: sep = unichr(list(ok)[0]) else: print('All unicode characters represented in column', a, ', can\t replace quickly.') QuickRep = False if QuickRep: newrows = np.array(sep.join(X[a][rows]) .replace(old, new).split(sep)) else: newrows = np.array([aa.replace(old,new) for aa in X[a][rows]]) X[a][rows] = np.cast[X.dtype[a]](newrows) if newrows.dtype > X.dtype[a]: print('WARNING: dtype of column', a, 'is inferior to the ' 'dtype of its replacement which may cause problems ' '(ends of strings might get chopped off).')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:rowstack; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:seq; 5, default_parameter; 5, 6; 5, 7; 6, identifier:mode; 7, string:'nulls'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:nullvals; 10, None; 11, block; 11, 12; 11, 14; 11, 25; 11, 26; 12, expression_statement; 12, 13; 13, string:''' Vertically stack a sequence of numpy ndarrays with structured dtype Analog of numpy.vstack Implemented by the tabarray method :func:`tabular.tab.tabarray.rowstack` which uses :func:`tabular.tabarray.tab_rowstack`. **Parameters** **seq** : sequence of numpy recarrays List, tuple, etc. of numpy recarrays to stack vertically. **mode** : string in ['nulls', 'commons', 'abort'] Denotes how to proceed if the recarrays have different dtypes, e.g. different sets of named columns. * if `mode` == ``nulls``, the resulting set of columns is determined by the union of the dtypes of all recarrays to be stacked, and missing data is filled with null values as defined by :func:`tabular.spreadsheet.nullvalue`; this is the default mode. * elif `mode` == ``commons``, the resulting set of columns is determined by the intersection of the dtypes of all recarrays to be stacked, e.g. common columns. * elif `mode` == ``abort``, raise an error when the recarrays to stack have different dtypes. **Returns** **out** : numpy ndarray with structured dtype Result of vertically stacking the arrays in `seq`. **See also:** `numpy.vstack <http://docs.scipy.org/doc/numpy/reference/generated/numpy.vstack.html>`_. '''; 14, if_statement; 14, 15; 14, 18; 15, comparison_operator:==; 15, 16; 15, 17; 16, identifier:nullvals; 17, None; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:nullvals; 22, attribute; 22, 23; 22, 24; 23, identifier:utils; 24, identifier:DEFAULT_NULLVALUEFORMAT; 25, comment; 26, if_statement; 26, 27; 26, 33; 26, 334; 27, comparison_operator:>; 27, 28; 27, 32; 28, call; 28, 29; 28, 30; 29, identifier:len; 30, argument_list; 30, 31; 31, identifier:seq; 32, integer:1; 33, block; 33, 34; 33, 44; 33, 89; 34, assert_statement; 34, 35; 34, 41; 34, 42; 35, comparison_operator:in; 35, 36; 35, 37; 36, identifier:mode; 37, list:['commons','nulls','abort']; 37, 38; 37, 39; 37, 40; 38, string:'commons'; 39, string:'nulls'; 40, string:'abort'; 41, line_continuation:\; 42, parenthesized_expression; 42, 43; 43, string:'"mode" argument must either by "commons", "abort", or "nulls".'; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:==; 45, 46; 45, 47; 46, identifier:mode; 47, string:'abort'; 48, block; 48, 49; 49, if_statement; 49, 50; 49, 77; 49, 83; 50, not_operator; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:all; 53, argument_list; 53, 54; 54, list_comprehension; 54, 55; 54, 74; 55, comparison_operator:==; 55, 56; 55, 64; 56, call; 56, 57; 56, 58; 57, identifier:set; 58, argument_list; 58, 59; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:l; 62, identifier:dtype; 63, identifier:names; 64, call; 64, 65; 64, 66; 65, identifier:set; 66, argument_list; 66, 67; 67, attribute; 67, 68; 67, 73; 68, attribute; 68, 69; 68, 72; 69, subscript; 69, 70; 69, 71; 70, identifier:seq; 71, integer:0; 72, identifier:dtype; 73, identifier:names; 74, for_in_clause; 74, 75; 74, 76; 75, identifier:l; 76, identifier:seq; 77, block; 77, 78; 78, raise_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:ValueError; 81, argument_list; 81, 82; 82, string:'Some column names are different.'; 83, else_clause; 83, 84; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:mode; 88, string:'commons'; 89, if_statement; 89, 90; 89, 93; 89, 244; 90, comparison_operator:==; 90, 91; 90, 92; 91, identifier:mode; 92, string:'nulls'; 93, block; 93, 94; 93, 127; 93, 164; 93, 177; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:names; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:utils; 100, identifier:uniqify; 101, argument_list; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:utils; 105, identifier:listunion; 106, argument_list; 106, 107; 107, list_comprehension; 107, 108; 107, 116; 107, 119; 108, call; 108, 109; 108, 110; 109, identifier:list; 110, argument_list; 110, 111; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:s; 114, identifier:dtype; 115, identifier:names; 116, for_in_clause; 116, 117; 116, 118; 117, identifier:s; 118, identifier:seq; 119, if_clause; 119, 120; 120, comparison_operator:!=; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:s; 124, identifier:dtype; 125, identifier:names; 126, None; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:formats; 130, list_comprehension; 130, 131; 130, 161; 131, attribute; 131, 132; 131, 160; 132, call; 132, 133; 132, 134; 133, identifier:max; 134, argument_list; 134, 135; 135, list_comprehension; 135, 136; 135, 141; 135, 144; 136, subscript; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:s; 139, identifier:dtype; 140, identifier:att; 141, for_in_clause; 141, 142; 141, 143; 142, identifier:s; 143, identifier:seq; 144, if_clause; 144, 145; 145, boolean_operator:and; 145, 146; 145, 153; 146, comparison_operator:!=; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:s; 150, identifier:dtype; 151, identifier:names; 152, None; 153, comparison_operator:in; 153, 154; 153, 155; 154, identifier:att; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:s; 158, identifier:dtype; 159, identifier:names; 160, identifier:str; 161, for_in_clause; 161, 162; 161, 163; 162, identifier:att; 163, identifier:names; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:dtype; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:np; 170, identifier:dtype; 171, argument_list; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:zip; 174, argument_list; 174, 175; 174, 176; 175, identifier:names; 176, identifier:formats; 177, return_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:utils; 181, identifier:fromarrays; 182, argument_list; 182, 183; 182, 236; 182, 241; 183, list_comprehension; 183, 184; 183, 227; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:utils; 187, identifier:listunion; 188, argument_list; 188, 189; 189, list_comprehension; 189, 190; 189, 224; 190, conditional_expression:if; 190, 191; 190, 198; 190, 214; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:s; 195, identifier:att; 196, identifier:tolist; 197, argument_list; 198, parenthesized_expression; 198, 199; 199, boolean_operator:and; 199, 200; 199, 207; 200, comparison_operator:!=; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:s; 204, identifier:dtype; 205, identifier:names; 206, None; 207, comparison_operator:in; 207, 208; 207, 209; 208, identifier:att; 209, attribute; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:s; 212, identifier:dtype; 213, identifier:names; 214, binary_operator:*; 214, 215; 214, 220; 215, list:[nullvals(format)]; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:nullvals; 218, argument_list; 218, 219; 219, identifier:format; 220, call; 220, 221; 220, 222; 221, identifier:len; 222, argument_list; 222, 223; 223, identifier:s; 224, for_in_clause; 224, 225; 224, 226; 225, identifier:s; 226, identifier:seq; 227, for_in_clause; 227, 228; 227, 231; 228, tuple_pattern; 228, 229; 228, 230; 229, identifier:att; 230, identifier:format; 231, call; 231, 232; 231, 233; 232, identifier:zip; 233, argument_list; 233, 234; 233, 235; 234, identifier:names; 235, identifier:formats; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:type; 238, attribute; 238, 239; 238, 240; 239, identifier:np; 240, identifier:ndarray; 241, keyword_argument; 241, 242; 241, 243; 242, identifier:dtype; 243, identifier:dtype; 244, elif_clause; 244, 245; 244, 248; 245, comparison_operator:==; 245, 246; 245, 247; 246, identifier:mode; 247, string:'commons'; 248, block; 248, 249; 248, 282; 248, 303; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:names; 252, list_comprehension; 252, 253; 252, 254; 252, 263; 253, identifier:x; 254, for_in_clause; 254, 255; 254, 256; 255, identifier:x; 256, attribute; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, subscript; 258, 259; 258, 260; 259, identifier:seq; 260, integer:0; 261, identifier:dtype; 262, identifier:names; 263, if_clause; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:all; 266, argument_list; 266, 267; 267, list_comprehension; 267, 268; 267, 275; 268, comparison_operator:in; 268, 269; 268, 270; 269, identifier:x; 270, attribute; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:l; 273, identifier:dtype; 274, identifier:names; 275, for_in_clause; 275, 276; 275, 277; 276, identifier:l; 277, subscript; 277, 278; 277, 279; 278, identifier:seq; 279, slice; 279, 280; 279, 281; 280, integer:1; 281, colon; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:formats; 285, list_comprehension; 285, 286; 285, 300; 286, attribute; 286, 287; 286, 299; 287, call; 287, 288; 287, 289; 288, identifier:max; 289, argument_list; 289, 290; 290, list_comprehension; 290, 291; 290, 296; 291, subscript; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:a; 294, identifier:dtype; 295, identifier:att; 296, for_in_clause; 296, 297; 296, 298; 297, identifier:a; 298, identifier:seq; 299, identifier:str; 300, for_in_clause; 300, 301; 300, 302; 301, identifier:att; 302, identifier:names; 303, return_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:utils; 307, identifier:fromrecords; 308, argument_list; 308, 309; 308, 323; 308, 328; 308, 331; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:utils; 312, identifier:listunion; 313, argument_list; 313, 314; 314, list_comprehension; 314, 315; 314, 320; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:ar; 318, identifier:tolist; 319, argument_list; 320, for_in_clause; 320, 321; 320, 322; 321, identifier:ar; 322, identifier:seq; 323, keyword_argument; 323, 324; 323, 325; 324, identifier:type; 325, attribute; 325, 326; 325, 327; 326, identifier:np; 327, identifier:ndarray; 328, keyword_argument; 328, 329; 328, 330; 329, identifier:names; 330, identifier:names; 331, keyword_argument; 331, 332; 331, 333; 332, identifier:formats; 333, identifier:formats; 334, else_clause; 334, 335; 335, block; 335, 336; 336, return_statement; 336, 337; 337, subscript; 337, 338; 337, 339; 338, identifier:seq; 339, integer:0
def rowstack(seq, mode='nulls', nullvals=None): ''' Vertically stack a sequence of numpy ndarrays with structured dtype Analog of numpy.vstack Implemented by the tabarray method :func:`tabular.tab.tabarray.rowstack` which uses :func:`tabular.tabarray.tab_rowstack`. **Parameters** **seq** : sequence of numpy recarrays List, tuple, etc. of numpy recarrays to stack vertically. **mode** : string in ['nulls', 'commons', 'abort'] Denotes how to proceed if the recarrays have different dtypes, e.g. different sets of named columns. * if `mode` == ``nulls``, the resulting set of columns is determined by the union of the dtypes of all recarrays to be stacked, and missing data is filled with null values as defined by :func:`tabular.spreadsheet.nullvalue`; this is the default mode. * elif `mode` == ``commons``, the resulting set of columns is determined by the intersection of the dtypes of all recarrays to be stacked, e.g. common columns. * elif `mode` == ``abort``, raise an error when the recarrays to stack have different dtypes. **Returns** **out** : numpy ndarray with structured dtype Result of vertically stacking the arrays in `seq`. **See also:** `numpy.vstack <http://docs.scipy.org/doc/numpy/reference/generated/numpy.vstack.html>`_. ''' if nullvals == None: nullvals = utils.DEFAULT_NULLVALUEFORMAT #newseq = [ss for ss in seq if len(ss) > 0] if len(seq) > 1: assert mode in ['commons','nulls','abort'], \ ('"mode" argument must either by "commons", "abort", or "nulls".') if mode == 'abort': if not all([set(l.dtype.names) == set(seq[0].dtype.names) for l in seq]): raise ValueError('Some column names are different.') else: mode = 'commons' if mode == 'nulls': names = utils.uniqify(utils.listunion([list(s.dtype.names) for s in seq if s.dtype.names != None])) formats = [max([s.dtype[att] for s in seq if s.dtype.names != None and att in s.dtype.names]).str for att in names] dtype = np.dtype(zip(names,formats)) return utils.fromarrays([utils.listunion([s[att].tolist() if (s.dtype.names != None and att in s.dtype.names) else [nullvals(format)] * len(s) for s in seq]) for (att, format) in zip(names, formats)], type=np.ndarray, dtype=dtype) elif mode == 'commons': names = [x for x in seq[0].dtype.names if all([x in l.dtype.names for l in seq[1:]])] formats = [max([a.dtype[att] for a in seq]).str for att in names] return utils.fromrecords(utils.listunion( [ar.tolist() for ar in seq]), type=np.ndarray, names=names, formats=formats) else: return seq[0]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:colstack; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:seq; 5, default_parameter; 5, 6; 5, 7; 6, identifier:mode; 7, string:'abort'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:returnnaming; 10, False; 11, block; 11, 12; 11, 14; 11, 23; 11, 48; 11, 78; 11, 97; 11, 225; 12, expression_statement; 12, 13; 13, comment; 14, assert_statement; 14, 15; 14, 22; 15, comparison_operator:in; 15, 16; 15, 17; 16, identifier:mode; 17, list:['first','drop','abort','rename']; 17, 18; 17, 19; 17, 20; 17, 21; 18, string:'first'; 19, string:'drop'; 20, string:'abort'; 21, string:'rename'; 22, string:'mode argument must take on value "first","drop", "rename", or "abort".'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:AllNames; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:utils; 29, identifier:uniqify; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:utils; 34, identifier:listunion; 35, argument_list; 35, 36; 36, list_comprehension; 36, 37; 36, 45; 37, call; 37, 38; 37, 39; 38, identifier:list; 39, argument_list; 39, 40; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:l; 43, identifier:dtype; 44, identifier:names; 45, for_in_clause; 45, 46; 45, 47; 46, identifier:l; 47, identifier:seq; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:NameList; 51, list_comprehension; 51, 52; 51, 75; 52, tuple; 52, 53; 52, 54; 53, identifier:x; 54, list_comprehension; 54, 55; 54, 56; 54, 65; 55, identifier:i; 56, for_in_clause; 56, 57; 56, 58; 57, identifier:i; 58, call; 58, 59; 58, 60; 59, identifier:range; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:len; 63, argument_list; 63, 64; 64, identifier:seq; 65, if_clause; 65, 66; 66, comparison_operator:in; 66, 67; 66, 68; 67, identifier:x; 68, attribute; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:seq; 72, identifier:i; 73, identifier:dtype; 74, identifier:names; 75, for_in_clause; 75, 76; 75, 77; 76, identifier:x; 77, identifier:AllNames; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:Commons; 81, list_comprehension; 81, 82; 81, 85; 81, 88; 82, subscript; 82, 83; 82, 84; 83, identifier:x; 84, integer:0; 85, for_in_clause; 85, 86; 85, 87; 86, identifier:x; 87, identifier:NameList; 88, if_clause; 88, 89; 89, comparison_operator:>; 89, 90; 89, 96; 90, call; 90, 91; 90, 92; 91, identifier:len; 92, argument_list; 92, 93; 93, subscript; 93, 94; 93, 95; 94, identifier:x; 95, integer:1; 96, integer:1; 97, if_statement; 97, 98; 97, 108; 97, 208; 98, boolean_operator:or; 98, 99; 98, 105; 99, comparison_operator:>; 99, 100; 99, 104; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, identifier:Commons; 104, integer:0; 105, comparison_operator:==; 105, 106; 105, 107; 106, identifier:mode; 107, string:'first'; 108, block; 108, 109; 109, if_statement; 109, 110; 109, 113; 109, 121; 109, 145; 110, comparison_operator:==; 110, 111; 110, 112; 111, identifier:mode; 112, string:'abort'; 113, block; 113, 114; 114, raise_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:ValueError; 117, argument_list; 117, 118; 118, binary_operator:+; 118, 119; 118, 120; 119, string:'There are common column names with differing '; 120, string:'values in the columns'; 121, elif_clause; 121, 122; 121, 125; 122, comparison_operator:==; 122, 123; 122, 124; 123, identifier:mode; 124, string:'drop'; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:Names; 129, list_comprehension; 129, 130; 129, 136; 129, 141; 130, tuple; 130, 131; 130, 134; 130, 135; 131, subscript; 131, 132; 131, 133; 132, identifier:L; 133, integer:0; 134, identifier:x; 135, identifier:x; 136, for_in_clause; 136, 137; 136, 140; 137, tuple_pattern; 137, 138; 137, 139; 138, identifier:x; 139, identifier:L; 140, identifier:NameList; 141, if_clause; 141, 142; 142, comparison_operator:not; 142, 143; 142, 144; 143, identifier:x; 144, identifier:Commons; 145, elif_clause; 145, 146; 145, 149; 146, comparison_operator:==; 146, 147; 146, 148; 147, identifier:mode; 148, string:'rename'; 149, block; 149, 150; 149, 157; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:NameDict; 153, call; 153, 154; 153, 155; 154, identifier:dict; 155, argument_list; 155, 156; 156, identifier:NameList; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:Names; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:utils; 163, identifier:listunion; 164, argument_list; 164, 165; 165, list_comprehension; 165, 166; 165, 199; 165, 200; 166, list_comprehension; 166, 167; 166, 192; 167, conditional_expression:if; 167, 168; 167, 172; 167, 180; 167, 181; 168, tuple; 168, 169; 168, 170; 168, 171; 169, identifier:i; 170, identifier:n; 171, identifier:n; 172, comparison_operator:==; 172, 173; 172, 179; 173, call; 173, 174; 173, 175; 174, identifier:len; 175, argument_list; 175, 176; 176, subscript; 176, 177; 176, 178; 177, identifier:NameDict; 178, identifier:n; 179, integer:1; 180, line_continuation:\; 181, tuple; 181, 182; 181, 183; 181, 184; 182, identifier:i; 183, identifier:n; 184, binary_operator:+; 184, 185; 184, 188; 185, binary_operator:+; 185, 186; 185, 187; 186, identifier:n; 187, string:'_'; 188, call; 188, 189; 188, 190; 189, identifier:str; 190, argument_list; 190, 191; 191, identifier:i; 192, for_in_clause; 192, 193; 192, 194; 193, identifier:n; 194, attribute; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:s; 197, identifier:dtype; 198, identifier:names; 199, line_continuation:\; 200, for_in_clause; 200, 201; 200, 204; 201, tuple_pattern; 201, 202; 201, 203; 202, identifier:i; 203, identifier:s; 204, call; 204, 205; 204, 206; 205, identifier:enumerate; 206, argument_list; 206, 207; 207, identifier:seq; 208, else_clause; 208, 209; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:Names; 213, list_comprehension; 213, 214; 213, 220; 214, tuple; 214, 215; 214, 218; 214, 219; 215, subscript; 215, 216; 215, 217; 216, identifier:L; 217, integer:0; 218, identifier:x; 219, identifier:x; 220, for_in_clause; 220, 221; 220, 224; 221, tuple_pattern; 221, 222; 221, 223; 222, identifier:x; 223, identifier:L; 224, identifier:NameList; 225, if_statement; 225, 226; 225, 227; 225, 262; 226, identifier:returnnaming; 227, block; 227, 228; 228, return_statement; 228, 229; 229, expression_list; 229, 230; 229, 261; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:utils; 233, identifier:fromarrays; 234, argument_list; 234, 235; 234, 247; 234, 252; 235, list_comprehension; 235, 236; 235, 241; 236, subscript; 236, 237; 236, 240; 237, subscript; 237, 238; 237, 239; 238, identifier:seq; 239, identifier:i; 240, identifier:x; 241, for_in_clause; 241, 242; 241, 246; 242, tuple_pattern; 242, 243; 242, 244; 242, 245; 243, identifier:i; 244, identifier:x; 245, identifier:y; 246, identifier:Names; 247, keyword_argument; 247, 248; 247, 249; 248, identifier:type; 249, attribute; 249, 250; 249, 251; 250, identifier:np; 251, identifier:ndarray; 252, keyword_argument; 252, 253; 252, 254; 253, identifier:names; 254, subscript; 254, 255; 254, 260; 255, call; 255, 256; 255, 257; 256, identifier:zip; 257, argument_list; 257, 258; 258, list_splat; 258, 259; 259, identifier:Names; 260, integer:2; 261, identifier:Names; 262, else_clause; 262, 263; 263, block; 263, 264; 264, return_statement; 264, 265; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:utils; 268, identifier:fromarrays; 269, argument_list; 269, 270; 269, 282; 269, 287; 270, list_comprehension; 270, 271; 270, 276; 271, subscript; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:seq; 274, identifier:i; 275, identifier:x; 276, for_in_clause; 276, 277; 276, 281; 277, tuple_pattern; 277, 278; 277, 279; 277, 280; 278, identifier:i; 279, identifier:x; 280, identifier:y; 281, identifier:Names; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:type; 284, attribute; 284, 285; 284, 286; 285, identifier:np; 286, identifier:ndarray; 287, keyword_argument; 287, 288; 287, 289; 288, identifier:names; 289, subscript; 289, 290; 289, 295; 290, call; 290, 291; 290, 292; 291, identifier:zip; 292, argument_list; 292, 293; 293, list_splat; 293, 294; 294, identifier:Names; 295, integer:2
def colstack(seq, mode='abort',returnnaming=False): """ Horizontally stack a sequence of numpy ndarrays with structured dtypes Analog of numpy.hstack for recarrays. Implemented by the tabarray method :func:`tabular.tab.tabarray.colstack` which uses :func:`tabular.tabarray.tab_colstack`. **Parameters** **seq** : sequence of numpy ndarray with structured dtype List, tuple, etc. of numpy recarrays to stack vertically. **mode** : string in ['first','drop','abort','rename'] Denotes how to proceed if when multiple recarrays share the same column name: * if `mode` == ``first``, take the column from the first recarray in `seq` containing the shared column name. * elif `mode` == ``abort``, raise an error when the recarrays to stack share column names; this is the default mode. * elif `mode` == ``drop``, drop any column that shares its name with any other column among the sequence of recarrays. * elif `mode` == ``rename``, for any set of all columns sharing the same name, rename all columns by appending an underscore, '_', followed by an integer, starting with '0' and incrementing by 1 for each subsequent column. **Returns** **out** : numpy ndarray with structured dtype Result of horizontally stacking the arrays in `seq`. **See also:** `numpy.hstack <http://docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html>`_. """ assert mode in ['first','drop','abort','rename'], \ 'mode argument must take on value "first","drop", "rename", or "abort".' AllNames = utils.uniqify(utils.listunion( [list(l.dtype.names) for l in seq])) NameList = [(x, [i for i in range(len(seq)) if x in seq[i].dtype.names]) for x in AllNames] Commons = [x[0] for x in NameList if len(x[1]) > 1] if len(Commons) > 0 or mode == 'first': if mode == 'abort': raise ValueError('There are common column names with differing ' + 'values in the columns') elif mode == 'drop': Names = [(L[0], x,x) for (x, L) in NameList if x not in Commons] elif mode == 'rename': NameDict = dict(NameList) Names = utils.listunion([[(i,n,n) if len(NameDict[n]) == 1 else \ (i,n,n + '_' + str(i)) for n in s.dtype.names] \ for (i,s) in enumerate(seq)]) else: Names = [(L[0], x,x) for (x, L) in NameList] if returnnaming: return utils.fromarrays([seq[i][x] for (i, x,y) in Names], type= np.ndarray,names=zip(*Names)[2]),Names else: return utils.fromarrays([seq[i][x] for (i, x,y) in Names], type= np.ndarray,names=zip(*Names)[2])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:getjp2image; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:date; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sourceId; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:observatory; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:instrument; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:detector; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:measurement; 19, None; 20, block; 20, 21; 20, 23; 20, 27; 20, 31; 20, 66; 20, 97; 20, 125; 20, 153; 20, 181; 20, 209; 20, 215; 21, expression_statement; 21, 22; 22, string:''' Helioviewer.org and JHelioviewer operate off of JPEG2000 formatted image data generated from science-quality FITS files. Use the APIs below to interact directly with these intermediary JPEG2000 files. Download a JP2 image for the specified datasource that is the closest match in time to the `date` requested. Either `sourceId` must be specified, or the combination of `observatory`, `instrument`, `detector`, and `measurement`. Request Parameters: Parameter Required Type Example Description date Required string 2014-01-01T23:59:59Z Desired date/time of the JP2 image. ISO 8601 combined UTC date and time UTC format. sourceId Optional number 14 Unique image datasource identifier. observatory Optional string SDO Observatory name. instrument Optional string AIA Instrument name. detector Optional string AIA Detector name. measurement Optional string 335 Measurement name. jpip Optional boolean false Optionally return a JPIP URI instead of the binary data of the image itself. json Optional boolean false Optionally return a JSON object. EXAMPLE: http://helioviewer.org/api/v1/getJP2Image/?date=2014-01-01T23:59:59Z&sourceId=14&jpip=true '''; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:base_url; 26, string:'http://helioviewer.org/api/v1/getJP2Image/?'; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:req_url; 30, string:''; 31, try_statement; 31, 32; 31, 59; 32, block; 32, 33; 32, 38; 32, 53; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:validate_iso8601; 36, argument_list; 36, 37; 37, identifier:date; 38, if_statement; 38, 39; 38, 48; 39, not_operator; 39, 40; 40, comparison_operator:==; 40, 41; 40, 47; 41, subscript; 41, 42; 41, 43; 42, identifier:date; 43, slice; 43, 44; 43, 46; 44, unary_operator:-; 44, 45; 45, integer:1; 46, colon; 47, string:'Z'; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, augmented_assignment:+=; 50, 51; 50, 52; 51, identifier:date; 52, string:'Z'; 53, expression_statement; 53, 54; 54, augmented_assignment:+=; 54, 55; 54, 56; 55, identifier:base_url; 56, binary_operator:+; 56, 57; 56, 58; 57, string:'date='; 58, identifier:date; 59, except_clause; 59, 60; 60, block; 60, 61; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:ValueError; 64, argument_list; 64, 65; 65, string:"Your date input is not in iso8601 format. ex: 2014-01-01T23:59:59"; 66, if_statement; 66, 67; 66, 68; 67, identifier:sourceId; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 76; 69, 84; 70, not_operator; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:isinstance; 73, argument_list; 73, 74; 73, 75; 74, identifier:sourceId; 75, identifier:int; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:logger; 81, identifier:error; 82, argument_list; 82, 83; 83, string:"The sourceId argument should be an int, ignoring it"; 84, else_clause; 84, 85; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, augmented_assignment:+=; 87, 88; 87, 89; 88, identifier:base_url; 89, binary_operator:+; 89, 90; 89, 96; 90, binary_operator:+; 90, 91; 90, 92; 91, string:"sourceId="; 92, call; 92, 93; 92, 94; 93, identifier:str; 94, argument_list; 94, 95; 95, identifier:sourceId; 96, string:"&"; 97, if_statement; 97, 98; 97, 99; 98, identifier:observatory; 99, block; 99, 100; 100, if_statement; 100, 101; 100, 107; 100, 115; 101, not_operator; 101, 102; 102, call; 102, 103; 102, 104; 103, identifier:isinstance; 104, argument_list; 104, 105; 104, 106; 105, identifier:observatory; 106, identifier:str; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:logger; 112, identifier:error; 113, argument_list; 113, 114; 114, string:"The observatory argument should be a str, ignoring it"; 115, else_clause; 115, 116; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, augmented_assignment:+=; 118, 119; 118, 120; 119, identifier:base_url; 120, binary_operator:+; 120, 121; 120, 124; 121, binary_operator:+; 121, 122; 121, 123; 122, string:"observatory="; 123, identifier:observatory; 124, string:"&"; 125, if_statement; 125, 126; 125, 127; 126, identifier:instrument; 127, block; 127, 128; 128, if_statement; 128, 129; 128, 135; 128, 143; 129, not_operator; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:isinstance; 132, argument_list; 132, 133; 132, 134; 133, identifier:instrument; 134, identifier:str; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:logger; 140, identifier:error; 141, argument_list; 141, 142; 142, string:"The instrument argument should be a str, ignoring it"; 143, else_clause; 143, 144; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, augmented_assignment:+=; 146, 147; 146, 148; 147, identifier:base_url; 148, binary_operator:+; 148, 149; 148, 152; 149, binary_operator:+; 149, 150; 149, 151; 150, string:"instrument="; 151, identifier:instrument; 152, string:"&"; 153, if_statement; 153, 154; 153, 155; 154, identifier:detector; 155, block; 155, 156; 156, if_statement; 156, 157; 156, 163; 156, 171; 157, not_operator; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:isinstance; 160, argument_list; 160, 161; 160, 162; 161, identifier:detector; 162, identifier:str; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:logger; 168, identifier:error; 169, argument_list; 169, 170; 170, string:"The detector argument should be a str, ignoring it"; 171, else_clause; 171, 172; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, augmented_assignment:+=; 174, 175; 174, 176; 175, identifier:base_url; 176, binary_operator:+; 176, 177; 176, 180; 177, binary_operator:+; 177, 178; 177, 179; 178, string:"detector="; 179, identifier:detector; 180, string:"&"; 181, if_statement; 181, 182; 181, 183; 182, identifier:measurement; 183, block; 183, 184; 184, if_statement; 184, 185; 184, 191; 184, 199; 185, not_operator; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:isinstance; 188, argument_list; 188, 189; 188, 190; 189, identifier:measurement; 190, identifier:str; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:logger; 196, identifier:error; 197, argument_list; 197, 198; 198, string:"The measurement argument should be a str, ignoring it"; 199, else_clause; 199, 200; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, augmented_assignment:+=; 202, 203; 202, 204; 203, identifier:base_url; 204, binary_operator:+; 204, 205; 204, 208; 205, binary_operator:+; 205, 206; 205, 207; 206, string:"measurement="; 207, identifier:detector; 208, string:"&"; 209, expression_statement; 209, 210; 210, augmented_assignment:+=; 210, 211; 210, 212; 211, identifier:req_url; 212, binary_operator:+; 212, 213; 212, 214; 213, identifier:base_url; 214, string:"jpip=true"; 215, return_statement; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:dispatch_http_get; 218, argument_list; 218, 219; 219, identifier:req_url
def getjp2image(date, sourceId=None, observatory=None, instrument=None, detector=None, measurement=None): ''' Helioviewer.org and JHelioviewer operate off of JPEG2000 formatted image data generated from science-quality FITS files. Use the APIs below to interact directly with these intermediary JPEG2000 files. Download a JP2 image for the specified datasource that is the closest match in time to the `date` requested. Either `sourceId` must be specified, or the combination of `observatory`, `instrument`, `detector`, and `measurement`. Request Parameters: Parameter Required Type Example Description date Required string 2014-01-01T23:59:59Z Desired date/time of the JP2 image. ISO 8601 combined UTC date and time UTC format. sourceId Optional number 14 Unique image datasource identifier. observatory Optional string SDO Observatory name. instrument Optional string AIA Instrument name. detector Optional string AIA Detector name. measurement Optional string 335 Measurement name. jpip Optional boolean false Optionally return a JPIP URI instead of the binary data of the image itself. json Optional boolean false Optionally return a JSON object. EXAMPLE: http://helioviewer.org/api/v1/getJP2Image/?date=2014-01-01T23:59:59Z&sourceId=14&jpip=true ''' base_url = 'http://helioviewer.org/api/v1/getJP2Image/?' req_url = '' try: validate_iso8601(date) if not date[-1:] == 'Z': date += 'Z' base_url += 'date=' + date except: raise ValueError( "Your date input is not in iso8601 format. ex: 2014-01-01T23:59:59") if sourceId: if not isinstance(sourceId, int): logger.error("The sourceId argument should be an int, ignoring it") else: base_url += "sourceId=" + str(sourceId) + "&" if observatory: if not isinstance(observatory, str): logger.error( "The observatory argument should be a str, ignoring it") else: base_url += "observatory=" + observatory + "&" if instrument: if not isinstance(instrument, str): logger.error( "The instrument argument should be a str, ignoring it") else: base_url += "instrument=" + instrument + "&" if detector: if not isinstance(detector, str): logger.error("The detector argument should be a str, ignoring it") else: base_url += "detector=" + detector + "&" if measurement: if not isinstance(measurement, str): logger.error( "The measurement argument should be a str, ignoring it") else: base_url += "measurement=" + detector + "&" req_url += base_url + "jpip=true" return dispatch_http_get(req_url)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 1, 26; 2, function_name:loads_loader; 3, parameters; 3, 4; 3, 10; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:load_module; 6, type; 6, 7; 7, attribute; 7, 8; 7, 9; 8, identifier:types; 9, identifier:ModuleType; 10, typed_parameter; 10, 11; 10, 12; 11, identifier:pairs; 12, type; 12, 13; 13, generic_type; 13, 14; 13, 15; 14, identifier:Dict; 15, type_parameter; 15, 16; 15, 18; 16, type; 16, 17; 17, identifier:str; 18, type; 18, 19; 19, identifier:str; 20, type; 20, 21; 21, generic_type; 21, 22; 21, 23; 22, identifier:Optional; 23, type_parameter; 23, 24; 24, type; 24, 25; 25, identifier:JSGValidateable; 26, block; 26, 27; 26, 29; 26, 35; 26, 36; 26, 51; 26, 67; 26, 76; 26, 77; 26, 147; 26, 148; 26, 149; 26, 173; 27, expression_statement; 27, 28; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:cntxt; 32, attribute; 32, 33; 32, 34; 33, identifier:load_module; 34, identifier:_CONTEXT; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:possible_type; 39, conditional_expression:if; 39, 40; 39, 45; 39, 50; 40, subscript; 40, 41; 40, 42; 41, identifier:pairs; 42, attribute; 42, 43; 42, 44; 43, identifier:cntxt; 44, identifier:TYPE; 45, comparison_operator:in; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:cntxt; 48, identifier:TYPE; 49, identifier:pairs; 50, None; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:target_class; 54, conditional_expression:if; 54, 55; 54, 61; 54, 66; 55, call; 55, 56; 55, 57; 56, identifier:getattr; 57, argument_list; 57, 58; 57, 59; 57, 60; 58, identifier:load_module; 59, identifier:possible_type; 60, None; 61, call; 61, 62; 61, 63; 62, identifier:isinstance; 63, argument_list; 63, 64; 63, 65; 64, identifier:possible_type; 65, identifier:str; 66, None; 67, if_statement; 67, 68; 67, 69; 68, identifier:target_class; 69, block; 69, 70; 70, return_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:target_class; 73, argument_list; 73, 74; 74, dictionary_splat; 74, 75; 75, identifier:pairs; 76, comment; 77, for_statement; 77, 78; 77, 79; 77, 82; 78, identifier:type_exception; 79, attribute; 79, 80; 79, 81; 80, identifier:cntxt; 81, identifier:TYPE_EXCEPTIONS; 82, block; 82, 83; 82, 101; 82, 109; 82, 115; 82, 121; 82, 139; 83, if_statement; 83, 84; 83, 90; 84, not_operator; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:hasattr; 87, argument_list; 87, 88; 87, 89; 88, identifier:load_module; 89, identifier:type_exception; 90, block; 90, 91; 91, raise_statement; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:ValueError; 94, argument_list; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:UNKNOWN_TYPE_EXCEPTION; 98, identifier:format; 99, argument_list; 99, 100; 100, identifier:type_exception; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:target_class; 104, call; 104, 105; 104, 106; 105, identifier:getattr; 106, argument_list; 106, 107; 106, 108; 107, identifier:load_module; 108, identifier:type_exception; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:target_strict; 112, attribute; 112, 113; 112, 114; 113, identifier:target_class; 114, identifier:_strict; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:target_class; 119, identifier:_strict; 120, False; 121, try_statement; 121, 122; 121, 131; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:rval; 126, call; 126, 127; 126, 128; 127, identifier:target_class; 128, argument_list; 128, 129; 129, dictionary_splat; 129, 130; 130, identifier:pairs; 131, finally_clause; 131, 132; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:target_class; 137, identifier:_strict; 138, identifier:target_strict; 139, if_statement; 139, 140; 139, 144; 140, call; 140, 141; 140, 142; 141, identifier:is_valid; 142, argument_list; 142, 143; 143, identifier:rval; 144, block; 144, 145; 145, return_statement; 145, 146; 146, identifier:rval; 147, comment; 148, comment; 149, if_statement; 149, 150; 149, 158; 150, boolean_operator:and; 150, 151; 150, 155; 151, not_operator; 151, 152; 152, attribute; 152, 153; 152, 154; 153, identifier:cntxt; 154, identifier:TYPE; 155, attribute; 155, 156; 155, 157; 156, identifier:cntxt; 157, identifier:TYPE_EXCEPTIONS; 158, block; 158, 159; 159, return_statement; 159, 160; 160, call; 160, 161; 160, 170; 161, call; 161, 162; 161, 163; 162, identifier:getattr; 163, argument_list; 163, 164; 163, 165; 164, identifier:load_module; 165, subscript; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:cntxt; 168, identifier:TYPE_EXCEPTIONS; 169, integer:0; 170, argument_list; 170, 171; 171, dictionary_splat; 171, 172; 172, identifier:pairs; 173, if_statement; 173, 174; 173, 179; 173, 185; 174, comparison_operator:in; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:cntxt; 177, identifier:TYPE; 178, identifier:pairs; 179, block; 179, 180; 180, raise_statement; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:ValueError; 183, argument_list; 183, 184; 184, string:f'Unknown reference type: "{cntxt.TYPE}": "{pairs[cntxt.TYPE]}"'; 185, else_clause; 185, 186; 186, block; 186, 187; 187, raise_statement; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:ValueError; 190, argument_list; 190, 191; 191, string:f'Missing "{cntxt.TYPE}" element'
def loads_loader(load_module: types.ModuleType, pairs: Dict[str, str]) -> Optional[JSGValidateable]: """json loader objecthook :param load_module: Module that contains the various types :param pairs: key/value tuples (In our case, they are str/str) :return: """ cntxt = load_module._CONTEXT # If the type element is a member of the JSON, load it possible_type = pairs[cntxt.TYPE] if cntxt.TYPE in pairs else None target_class = getattr(load_module, possible_type, None) if isinstance(possible_type, str) else None if target_class: return target_class(**pairs) # See whether there are any exception types that are valid for the incoming data for type_exception in cntxt.TYPE_EXCEPTIONS: if not hasattr(load_module, type_exception): raise ValueError(UNKNOWN_TYPE_EXCEPTION.format(type_exception)) target_class = getattr(load_module, type_exception) target_strict = target_class._strict target_class._strict = False try: rval = target_class(**pairs) finally: target_class._strict = target_strict if is_valid(rval): return rval # If there is not a type variable and nothing fits, just load up the first (and perhaps only) exception # It will later fail any is_valid tests if not cntxt.TYPE and cntxt.TYPE_EXCEPTIONS: return getattr(load_module, cntxt.TYPE_EXCEPTIONS[0])(**pairs) if cntxt.TYPE in pairs: raise ValueError(f'Unknown reference type: "{cntxt.TYPE}": "{pairs[cntxt.TYPE]}"') else: raise ValueError(f'Missing "{cntxt.TYPE}" element')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 1, 11; 2, function_name:get_cred_def; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:cd_id; 7, type; 7, 8; 8, identifier:str; 9, type; 9, 10; 10, identifier:str; 11, block; 11, 12; 11, 14; 11, 22; 11, 31; 11, 194; 11, 202; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:LOGGER; 18, identifier:debug; 19, argument_list; 19, 20; 19, 21; 20, string:'_BaseAgent.get_cred_def >>> cd_id: %s'; 21, identifier:cd_id; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:rv_json; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:json; 28, identifier:dumps; 29, argument_list; 29, 30; 30, dictionary; 31, with_statement; 31, 32; 31, 37; 32, with_clause; 32, 33; 33, with_item; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:CRED_DEF_CACHE; 36, identifier:lock; 37, block; 37, 38; 37, 72; 37, 85; 37, 95; 37, 104; 37, 139; 37, 175; 37, 186; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:in; 39, 40; 39, 41; 40, identifier:cd_id; 41, identifier:CRED_DEF_CACHE; 42, block; 42, 43; 42, 51; 42, 62; 42, 70; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:LOGGER; 47, identifier:info; 48, argument_list; 48, 49; 48, 50; 49, string:'_BaseAgent.get_cred_def: got cred def for %s from cache'; 50, identifier:cd_id; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:rv_json; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:json; 57, identifier:dumps; 58, argument_list; 58, 59; 59, subscript; 59, 60; 59, 61; 60, identifier:CRED_DEF_CACHE; 61, identifier:cd_id; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:LOGGER; 66, identifier:debug; 67, argument_list; 67, 68; 67, 69; 68, string:'_BaseAgent.get_cred_def <<< %s'; 69, identifier:rv_json; 70, return_statement; 70, 71; 71, identifier:rv_json; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:req_json; 75, await; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:ledger; 79, identifier:build_get_cred_def_request; 80, argument_list; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:did; 84, identifier:cd_id; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:resp_json; 88, await; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:_submit; 93, argument_list; 93, 94; 94, identifier:req_json; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:resp; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:json; 101, identifier:loads; 102, argument_list; 102, 103; 103, identifier:resp_json; 104, if_statement; 104, 105; 104, 120; 105, not_operator; 105, 106; 106, parenthesized_expression; 106, 107; 107, boolean_operator:and; 107, 108; 107, 111; 108, comparison_operator:in; 108, 109; 108, 110; 109, string:'result'; 110, identifier:resp; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:resp; 115, string:'result'; 116, identifier:get; 117, argument_list; 117, 118; 117, 119; 118, string:'data'; 119, None; 120, block; 120, 121; 120, 129; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:LOGGER; 125, identifier:debug; 126, argument_list; 126, 127; 126, 128; 127, string:'_BaseAgent.get_cred_def: <!< no cred def exists on %s'; 128, identifier:cd_id; 129, raise_statement; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:AbsentCredDef; 132, argument_list; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, string:'No cred def exists on {}'; 136, identifier:format; 137, argument_list; 137, 138; 138, identifier:cd_id; 139, try_statement; 139, 140; 139, 153; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 146; 143, tuple_pattern; 143, 144; 143, 145; 144, identifier:_; 145, identifier:rv_json; 146, await; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:ledger; 150, identifier:parse_get_cred_def_response; 151, argument_list; 151, 152; 152, identifier:resp_json; 153, except_clause; 153, 154; 153, 155; 153, 156; 154, identifier:IndyError; 155, comment; 156, block; 156, 157; 156, 165; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:LOGGER; 161, identifier:debug; 162, argument_list; 162, 163; 162, 164; 163, string:'_BaseAgent.get_cred_def: <!< no cred def exists on %s'; 164, identifier:cd_id; 165, raise_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:AbsentCredDef; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, string:'No cred def exists on {}'; 172, identifier:format; 173, argument_list; 173, 174; 174, identifier:cd_id; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:CRED_DEF_CACHE; 179, identifier:cd_id; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:json; 183, identifier:loads; 184, argument_list; 184, 185; 185, identifier:rv_json; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:LOGGER; 190, identifier:info; 191, argument_list; 191, 192; 191, 193; 192, string:'_BaseAgent.get_cred_def: got cred def %s from ledger'; 193, identifier:cd_id; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:LOGGER; 198, identifier:debug; 199, argument_list; 199, 200; 199, 201; 200, string:'_BaseAgent.get_cred_def <<< %s'; 201, identifier:rv_json; 202, return_statement; 202, 203; 203, identifier:rv_json
async def get_cred_def(self, cd_id: str) -> str: """ Get credential definition from ledger by its identifier. Raise AbsentCredDef for no such credential definition, logging any error condition and raising BadLedgerTxn on bad request. Raise ClosedPool if cred def not in cache and pool is closed. Retrieve the credential definition from the agent's credential definition cache if it has it; cache it en passant if it does not (and if there is a corresponding credential definition on the ledger). :param cd_id: (credential definition) identifier string ('<issuer-did>:3:CL:<schema-seq-no>:<tag>') :return: credential definition json as retrieved from ledger, empty production for no such cred def """ LOGGER.debug('_BaseAgent.get_cred_def >>> cd_id: %s', cd_id) rv_json = json.dumps({}) with CRED_DEF_CACHE.lock: if cd_id in CRED_DEF_CACHE: LOGGER.info('_BaseAgent.get_cred_def: got cred def for %s from cache', cd_id) rv_json = json.dumps(CRED_DEF_CACHE[cd_id]) LOGGER.debug('_BaseAgent.get_cred_def <<< %s', rv_json) return rv_json req_json = await ledger.build_get_cred_def_request(self.did, cd_id) resp_json = await self._submit(req_json) resp = json.loads(resp_json) if not ('result' in resp and resp['result'].get('data', None)): LOGGER.debug('_BaseAgent.get_cred_def: <!< no cred def exists on %s', cd_id) raise AbsentCredDef('No cred def exists on {}'.format(cd_id)) try: (_, rv_json) = await ledger.parse_get_cred_def_response(resp_json) except IndyError: # ledger replied, but there is no such cred def LOGGER.debug('_BaseAgent.get_cred_def: <!< no cred def exists on %s', cd_id) raise AbsentCredDef('No cred def exists on {}'.format(cd_id)) CRED_DEF_CACHE[cd_id] = json.loads(rv_json) LOGGER.info('_BaseAgent.get_cred_def: got cred def %s from ledger', cd_id) LOGGER.debug('_BaseAgent.get_cred_def <<< %s', rv_json) return rv_json
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:convert_nexson_format; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:blob; 5, identifier:out_nexson_format; 6, default_parameter; 6, 7; 6, 8; 7, identifier:current_format; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:remove_old_structs; 11, True; 12, default_parameter; 12, 13; 12, 14; 13, identifier:pristine_if_invalid; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:sort_arbitrary; 17, False; 18, block; 18, 19; 18, 21; 18, 32; 18, 39; 18, 54; 18, 66; 18, 78; 18, 105; 18, 121; 18, 128; 18, 212; 18, 221; 18, 229; 19, expression_statement; 19, 20; 20, comment; 21, if_statement; 21, 22; 21, 24; 22, not_operator; 22, 23; 23, identifier:current_format; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:current_format; 28, call; 28, 29; 28, 30; 29, identifier:detect_nexson_version; 30, argument_list; 30, 31; 31, identifier:blob; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:out_nexson_format; 35, call; 35, 36; 35, 37; 36, identifier:resolve_nexson_format; 37, argument_list; 37, 38; 38, identifier:out_nexson_format; 39, if_statement; 39, 40; 39, 43; 40, comparison_operator:==; 40, 41; 40, 42; 41, identifier:current_format; 42, identifier:out_nexson_format; 43, block; 43, 44; 43, 52; 44, if_statement; 44, 45; 44, 46; 45, identifier:sort_arbitrary; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:sort_arbitrarily_ordered_nexson; 50, argument_list; 50, 51; 51, identifier:blob; 52, return_statement; 52, 53; 53, identifier:blob; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:two2zero; 57, boolean_operator:and; 57, 58; 57, 62; 58, call; 58, 59; 58, 60; 59, identifier:_is_by_id_hbf; 60, argument_list; 60, 61; 61, identifier:out_nexson_format; 62, call; 62, 63; 62, 64; 63, identifier:_is_badgerfish_version; 64, argument_list; 64, 65; 65, identifier:current_format; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:zero2two; 69, boolean_operator:and; 69, 70; 69, 74; 70, call; 70, 71; 70, 72; 71, identifier:_is_by_id_hbf; 72, argument_list; 72, 73; 73, identifier:current_format; 74, call; 74, 75; 74, 76; 75, identifier:_is_badgerfish_version; 76, argument_list; 76, 77; 77, identifier:out_nexson_format; 78, if_statement; 78, 79; 78, 82; 78, 83; 79, boolean_operator:or; 79, 80; 79, 81; 80, identifier:two2zero; 81, identifier:zero2two; 82, comment; 83, block; 83, 84; 83, 101; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:blob; 87, call; 87, 88; 87, 89; 88, identifier:convert_nexson_format; 89, argument_list; 89, 90; 89, 91; 89, 92; 89, 95; 89, 98; 90, identifier:blob; 91, identifier:DIRECT_HONEY_BADGERFISH; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:current_format; 94, identifier:current_format; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:remove_old_structs; 97, identifier:remove_old_structs; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:pristine_if_invalid; 100, identifier:pristine_if_invalid; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:current_format; 104, identifier:DIRECT_HONEY_BADGERFISH; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:ccdict; 108, dictionary; 108, 109; 108, 112; 108, 115; 108, 118; 109, pair; 109, 110; 109, 111; 110, string:'output_format'; 111, identifier:out_nexson_format; 112, pair; 112, 113; 112, 114; 113, string:'input_format'; 114, identifier:current_format; 115, pair; 115, 116; 115, 117; 116, string:'remove_old_structs'; 117, identifier:remove_old_structs; 118, pair; 118, 119; 118, 120; 119, string:'pristine_if_invalid'; 120, identifier:pristine_if_invalid; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:ccfg; 124, call; 124, 125; 124, 126; 125, identifier:ConversionConfig; 126, argument_list; 126, 127; 127, identifier:ccdict; 128, if_statement; 128, 129; 128, 133; 128, 141; 128, 159; 128, 177; 128, 195; 129, call; 129, 130; 129, 131; 130, identifier:_is_badgerfish_version; 131, argument_list; 131, 132; 132, identifier:current_format; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:converter; 137, call; 137, 138; 137, 139; 138, identifier:Badgerfish2DirectNexson; 139, argument_list; 139, 140; 140, identifier:ccfg; 141, elif_clause; 141, 142; 141, 146; 142, call; 142, 143; 142, 144; 143, identifier:_is_badgerfish_version; 144, argument_list; 144, 145; 145, identifier:out_nexson_format; 146, block; 146, 147; 146, 152; 147, assert_statement; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:_is_direct_hbf; 150, argument_list; 150, 151; 151, identifier:current_format; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:converter; 155, call; 155, 156; 155, 157; 156, identifier:Direct2BadgerfishNexson; 157, argument_list; 157, 158; 158, identifier:ccfg; 159, elif_clause; 159, 160; 159, 169; 160, boolean_operator:and; 160, 161; 160, 165; 161, call; 161, 162; 161, 163; 162, identifier:_is_direct_hbf; 163, argument_list; 163, 164; 164, identifier:current_format; 165, parenthesized_expression; 165, 166; 166, comparison_operator:==; 166, 167; 166, 168; 167, identifier:out_nexson_format; 168, identifier:BY_ID_HONEY_BADGERFISH; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:converter; 173, call; 173, 174; 173, 175; 174, identifier:Direct2OptimalNexson; 175, argument_list; 175, 176; 176, identifier:ccfg; 177, elif_clause; 177, 178; 177, 187; 178, boolean_operator:and; 178, 179; 178, 183; 179, call; 179, 180; 179, 181; 180, identifier:_is_direct_hbf; 181, argument_list; 181, 182; 182, identifier:out_nexson_format; 183, parenthesized_expression; 183, 184; 184, comparison_operator:==; 184, 185; 184, 186; 185, identifier:current_format; 186, identifier:BY_ID_HONEY_BADGERFISH; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:converter; 191, call; 191, 192; 191, 193; 192, identifier:Optimal2DirectNexson; 193, argument_list; 193, 194; 194, identifier:ccfg; 195, else_clause; 195, 196; 196, block; 196, 197; 197, raise_statement; 197, 198; 198, call; 198, 199; 198, 200; 199, identifier:NotImplementedError; 200, argument_list; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, string:'Conversion from {i} to {o}'; 204, identifier:format; 205, argument_list; 205, 206; 205, 209; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:i; 208, identifier:current_format; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:o; 211, identifier:out_nexson_format; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:blob; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:converter; 218, identifier:convert; 219, argument_list; 219, 220; 220, identifier:blob; 221, if_statement; 221, 222; 221, 223; 222, identifier:sort_arbitrary; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:sort_arbitrarily_ordered_nexson; 227, argument_list; 227, 228; 228, identifier:blob; 229, return_statement; 229, 230; 230, identifier:blob
def convert_nexson_format(blob, out_nexson_format, current_format=None, remove_old_structs=True, pristine_if_invalid=False, sort_arbitrary=False): """Take a dict form of NexSON and converts its datastructures to those needed to serialize as out_nexson_format. If current_format is not specified, it will be inferred. If `remove_old_structs` is False and different honeybadgerfish varieties are selected, the `blob` will be 'fat" containing both types of lookup structures. If pristine_if_invalid is False, then the object may be corrupted if it is an invalid nexson struct. Setting this to False can result in faster translation, but if an exception is raised the object may be polluted with partially constructed fields for the out_nexson_format. """ if not current_format: current_format = detect_nexson_version(blob) out_nexson_format = resolve_nexson_format(out_nexson_format) if current_format == out_nexson_format: if sort_arbitrary: sort_arbitrarily_ordered_nexson(blob) return blob two2zero = _is_by_id_hbf(out_nexson_format) and _is_badgerfish_version(current_format) zero2two = _is_by_id_hbf(current_format) and _is_badgerfish_version(out_nexson_format) if two2zero or zero2two: # go from 0.0 -> 1.0 then the 1.0->1.2 should succeed without nexml... blob = convert_nexson_format(blob, DIRECT_HONEY_BADGERFISH, current_format=current_format, remove_old_structs=remove_old_structs, pristine_if_invalid=pristine_if_invalid) current_format = DIRECT_HONEY_BADGERFISH ccdict = {'output_format': out_nexson_format, 'input_format': current_format, 'remove_old_structs': remove_old_structs, 'pristine_if_invalid': pristine_if_invalid} ccfg = ConversionConfig(ccdict) if _is_badgerfish_version(current_format): converter = Badgerfish2DirectNexson(ccfg) elif _is_badgerfish_version(out_nexson_format): assert _is_direct_hbf(current_format) converter = Direct2BadgerfishNexson(ccfg) elif _is_direct_hbf(current_format) and (out_nexson_format == BY_ID_HONEY_BADGERFISH): converter = Direct2OptimalNexson(ccfg) elif _is_direct_hbf(out_nexson_format) and (current_format == BY_ID_HONEY_BADGERFISH): converter = Optimal2DirectNexson(ccfg) else: raise NotImplementedError('Conversion from {i} to {o}'.format(i=current_format, o=out_nexson_format)) blob = converter.convert(blob) if sort_arbitrary: sort_arbitrarily_ordered_nexson(blob) return blob
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_inplace_sort_by_id; 3, parameters; 3, 4; 4, identifier:unsorted_list; 5, block; 5, 6; 5, 8; 5, 17; 5, 32; 5, 38; 5, 43; 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:unsorted_list; 14, identifier:list; 15, block; 15, 16; 16, return_statement; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:sorted_list; 20, list_comprehension; 20, 21; 20, 29; 21, tuple; 21, 22; 21, 28; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:i; 25, identifier:get; 26, argument_list; 26, 27; 27, string:'@id'; 28, identifier:i; 29, for_in_clause; 29, 30; 29, 31; 30, identifier:i; 31, identifier:unsorted_list; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:sorted_list; 36, identifier:sort; 37, argument_list; 38, delete_statement; 38, 39; 39, subscript; 39, 40; 39, 41; 40, identifier:unsorted_list; 41, slice; 41, 42; 42, colon; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:unsorted_list; 47, identifier:extend; 48, argument_list; 48, 49; 49, list_comprehension; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:i; 52, integer:1; 53, for_in_clause; 53, 54; 53, 55; 54, identifier:i; 55, identifier:sorted_list
def _inplace_sort_by_id(unsorted_list): """Takes a list of dicts each of which has an '@id' key, sorts the elements in the list by the value of the @id key. Assumes that @id is unique or the dicts have a meaningul < operator """ if not isinstance(unsorted_list, list): return sorted_list = [(i.get('@id'), i) for i in unsorted_list] sorted_list.sort() del unsorted_list[:] unsorted_list.extend([i[1] for i in sorted_list])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:cull_nonmatching_trees; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:nexson; 5, identifier:tree_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:curr_version; 8, None; 9, block; 9, 10; 9, 12; 9, 24; 9, 39; 9, 46; 9, 52; 9, 56; 9, 119; 9, 136; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:curr_version; 15, None; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:curr_version; 20, call; 20, 21; 20, 22; 21, identifier:detect_nexson_version; 22, argument_list; 22, 23; 23, identifier:nexson; 24, if_statement; 24, 25; 24, 30; 25, not_operator; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:_is_by_id_hbf; 28, argument_list; 28, 29; 29, identifier:curr_version; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:nexson; 34, call; 34, 35; 34, 36; 35, identifier:convert_nexson_format; 36, argument_list; 36, 37; 36, 38; 37, identifier:nexson; 38, identifier:BY_ID_HONEY_BADGERFISH; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:nexml_el; 42, call; 42, 43; 42, 44; 43, identifier:get_nexml_el; 44, argument_list; 44, 45; 45, identifier:nexson; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:tree_groups; 49, subscript; 49, 50; 49, 51; 50, identifier:nexml_el; 51, string:'treesById'; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:tree_groups_to_del; 55, list:[]; 56, for_statement; 56, 57; 56, 60; 56, 65; 57, pattern_list; 57, 58; 57, 59; 58, identifier:tgi; 59, identifier:tree_group; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:tree_groups; 63, identifier:items; 64, argument_list; 65, block; 65, 66; 65, 72; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:tbi; 69, subscript; 69, 70; 69, 71; 70, identifier:tree_group; 71, string:'treeById'; 72, if_statement; 72, 73; 72, 76; 72, 110; 73, comparison_operator:in; 73, 74; 73, 75; 74, identifier:tree_id; 75, identifier:tbi; 76, block; 76, 77; 76, 93; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:trees_to_del; 80, list_comprehension; 80, 81; 80, 82; 80, 89; 81, identifier:i; 82, for_in_clause; 82, 83; 82, 84; 83, identifier:i; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:tbi; 87, identifier:keys; 88, argument_list; 89, if_clause; 89, 90; 90, comparison_operator:!=; 90, 91; 90, 92; 91, identifier:i; 92, identifier:tree_id; 93, for_statement; 93, 94; 93, 95; 93, 96; 94, identifier:tid; 95, identifier:trees_to_del; 96, block; 96, 97; 96, 106; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:tree_group; 102, string:'^ot:treeElementOrder'; 103, identifier:remove; 104, argument_list; 104, 105; 105, identifier:tid; 106, delete_statement; 106, 107; 107, subscript; 107, 108; 107, 109; 108, identifier:tbi; 109, identifier:tid; 110, else_clause; 110, 111; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:tree_groups_to_del; 116, identifier:append; 117, argument_list; 117, 118; 118, identifier:tgi; 119, for_statement; 119, 120; 119, 121; 119, 122; 120, identifier:tgid; 121, identifier:tree_groups_to_del; 122, block; 122, 123; 122, 132; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 130; 125, attribute; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:nexml_el; 128, string:'^ot:treesElementOrder'; 129, identifier:remove; 130, argument_list; 130, 131; 131, identifier:tgid; 132, delete_statement; 132, 133; 133, subscript; 133, 134; 133, 135; 134, identifier:tree_groups; 135, identifier:tgid; 136, return_statement; 136, 137; 137, identifier:nexson
def cull_nonmatching_trees(nexson, tree_id, curr_version=None): """Modifies `nexson` and returns it in version 1.2.1 with any tree that does not match the ID removed. Note that this does not search through the NexSON for every node, edge, tree that was deleted. So the resulting NexSON may have broken references ! """ if curr_version is None: curr_version = detect_nexson_version(nexson) if not _is_by_id_hbf(curr_version): nexson = convert_nexson_format(nexson, BY_ID_HONEY_BADGERFISH) nexml_el = get_nexml_el(nexson) tree_groups = nexml_el['treesById'] tree_groups_to_del = [] for tgi, tree_group in tree_groups.items(): tbi = tree_group['treeById'] if tree_id in tbi: trees_to_del = [i for i in tbi.keys() if i != tree_id] for tid in trees_to_del: tree_group['^ot:treeElementOrder'].remove(tid) del tbi[tid] else: tree_groups_to_del.append(tgi) for tgid in tree_groups_to_del: nexml_el['^ot:treesElementOrder'].remove(tgid) del tree_groups[tgid] return nexson
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 1, 30; 2, function_name:_validate; 3, parameters; 3, 4; 3, 5; 3, 9; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:val; 7, type; 7, 8; 8, identifier:list; 9, typed_default_parameter; 9, 10; 9, 11; 9, 17; 10, identifier:log; 11, type; 11, 12; 12, generic_type; 12, 13; 12, 14; 13, identifier:Optional; 14, type_parameter; 14, 15; 15, type; 15, 16; 16, identifier:Logger; 17, None; 18, type; 18, 19; 19, generic_type; 19, 20; 19, 21; 20, identifier:Tuple; 21, type_parameter; 21, 22; 21, 24; 22, type; 22, 23; 23, identifier:bool; 24, type; 24, 25; 25, generic_type; 25, 26; 25, 27; 26, identifier:List; 27, type_parameter; 27, 28; 28, type; 28, 29; 29, identifier:str; 30, block; 30, 31; 30, 33; 30, 37; 30, 135; 30, 149; 31, expression_statement; 31, 32; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:errors; 36, list:[]; 37, if_statement; 37, 38; 37, 44; 37, 52; 38, not_operator; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:isinstance; 41, argument_list; 41, 42; 41, 43; 42, identifier:val; 43, identifier:list; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:errors; 49, identifier:append; 50, argument_list; 50, 51; 51, string:f"{self._variable_name}: {repr(val)} is not an array"; 52, else_clause; 52, 53; 53, block; 53, 54; 53, 93; 53, 112; 54, for_statement; 54, 55; 54, 56; 54, 64; 55, identifier:i; 56, call; 56, 57; 56, 58; 57, identifier:range; 58, argument_list; 58, 59; 58, 60; 59, integer:0; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:val; 64, block; 64, 65; 64, 71; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:v; 68, subscript; 68, 69; 68, 70; 69, identifier:val; 70, identifier:i; 71, if_statement; 71, 72; 71, 85; 72, not_operator; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:conforms; 75, argument_list; 75, 76; 75, 77; 75, 80; 76, identifier:v; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:_type; 80, attribute; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_context; 84, identifier:NAMESPACE; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:errors; 90, identifier:append; 91, argument_list; 91, 92; 92, string:f"{self._variable_name} element {i}: {v} is not a {self._type.__name__}"; 93, if_statement; 93, 94; 93, 102; 94, comparison_operator:<; 94, 95; 94, 99; 95, call; 95, 96; 95, 97; 96, identifier:len; 97, argument_list; 97, 98; 98, identifier:val; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:_min; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:errors; 107, identifier:append; 108, argument_list; 108, 109; 109, concatenated_string; 109, 110; 109, 111; 110, string:f"{self._variable_name}: at least {self._min} value{'s' if self._min > 1 else ''} required - "; 111, string:f"element has {len(val) if len(val) else 'none'}"; 112, if_statement; 112, 113; 112, 127; 113, boolean_operator:and; 113, 114; 113, 119; 114, comparison_operator:is; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_max; 118, None; 119, comparison_operator:>; 119, 120; 119, 124; 120, call; 120, 121; 120, 122; 121, identifier:len; 122, argument_list; 122, 123; 123, identifier:val; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:_max; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:errors; 132, identifier:append; 133, argument_list; 133, 134; 134, string:f"{self._variable_name}: no more than {self._max} values permitted - element has {len(val)}"; 135, if_statement; 135, 136; 135, 137; 136, identifier:log; 137, block; 137, 138; 138, for_statement; 138, 139; 138, 140; 138, 141; 139, identifier:error; 140, identifier:errors; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:log; 146, identifier:log; 147, argument_list; 147, 148; 148, identifier:error; 149, return_statement; 149, 150; 150, expression_list; 150, 151; 150, 156; 151, not_operator; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:bool; 154, argument_list; 154, 155; 155, identifier:errors; 156, identifier:errors
def _validate(self, val: list, log: Optional[Logger] = None) -> Tuple[bool, List[str]]: """ Determine whether val is a valid instance of this array :returns: Success indicator and error list """ errors = [] if not isinstance(val, list): errors.append(f"{self._variable_name}: {repr(val)} is not an array") else: for i in range(0, len(val)): v = val[i] if not conforms(v, self._type, self._context.NAMESPACE): errors.append(f"{self._variable_name} element {i}: {v} is not a {self._type.__name__}") if len(val) < self._min: errors.append( f"{self._variable_name}: at least {self._min} value{'s' if self._min > 1 else ''} required - " f"element has {len(val) if len(val) else 'none'}") if self._max is not None and len(val) > self._max: errors.append( f"{self._variable_name}: no more than {self._max} values permitted - element has {len(val)}") if log: for error in errors: log.log(error) return not bool(errors), errors
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 1, 16; 2, function_name:_sync_revoc; 3, parameters; 3, 4; 3, 5; 3, 9; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:rr_id; 7, type; 7, 8; 8, identifier:str; 9, typed_default_parameter; 9, 10; 9, 11; 9, 13; 10, identifier:rr_size; 11, type; 11, 12; 12, identifier:int; 13, None; 14, type; 14, 15; 15, None; 16, block; 16, 17; 16, 19; 16, 28; 16, 37; 16, 74; 16, 181; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:LOGGER; 23, identifier:debug; 24, argument_list; 24, 25; 24, 26; 24, 27; 25, string:'Issuer._sync_revoc >>> rr_id: %s, rr_size: %s'; 26, identifier:rr_id; 27, identifier:rr_size; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 33; 30, tuple_pattern; 30, 31; 30, 32; 31, identifier:cd_id; 32, identifier:tag; 33, call; 33, 34; 33, 35; 34, identifier:rev_reg_id2cred_def_id__tag; 35, argument_list; 35, 36; 36, identifier:rr_id; 37, try_statement; 37, 38; 37, 47; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, await; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:get_cred_def; 45, argument_list; 45, 46; 46, identifier:cd_id; 47, except_clause; 47, 48; 47, 49; 48, identifier:AbsentCredDef; 49, block; 49, 50; 49, 61; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:LOGGER; 54, identifier:debug; 55, argument_list; 55, 56; 55, 57; 55, 60; 56, string:'Issuer._sync_revoc: <!< tails tree %s may be for another ledger; no cred def found on %s'; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:_dir_tails; 60, identifier:cd_id; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:AbsentCredDef; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, string:'Tails tree {} may be for another ledger; no cred def found on {}'; 68, identifier:format; 69, argument_list; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:_dir_tails; 73, identifier:cd_id; 74, with_statement; 74, 75; 74, 80; 75, with_clause; 75, 76; 76, with_item; 76, 77; 77, attribute; 77, 78; 77, 79; 78, identifier:REVO_CACHE; 79, identifier:lock; 80, block; 80, 81; 80, 91; 80, 102; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:revo_cache_entry; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:REVO_CACHE; 87, identifier:get; 88, argument_list; 88, 89; 88, 90; 89, identifier:rr_id; 90, None; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:tails; 94, conditional_expression:if; 94, 95; 94, 96; 94, 99; 95, None; 96, comparison_operator:is; 96, 97; 96, 98; 97, identifier:revo_cache_entry; 98, None; 99, attribute; 99, 100; 99, 101; 100, identifier:revo_cache_entry; 101, identifier:tails; 102, if_statement; 102, 103; 102, 106; 102, 107; 103, comparison_operator:is; 103, 104; 103, 105; 104, identifier:tails; 105, None; 106, comment; 107, block; 107, 108; 107, 156; 108, try_statement; 108, 109; 108, 126; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:tails; 113, await; 113, 114; 114, call; 114, 115; 114, 125; 115, attribute; 115, 116; 115, 124; 116, call; 116, 117; 116, 118; 117, identifier:Tails; 118, argument_list; 118, 119; 118, 122; 118, 123; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_dir_tails; 122, identifier:cd_id; 123, identifier:tag; 124, identifier:open; 125, argument_list; 126, except_clause; 126, 127; 126, 128; 127, identifier:AbsentTails; 128, block; 128, 129; 128, 138; 128, 139; 128, 155; 129, expression_statement; 129, 130; 130, await; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:_create_rev_reg; 135, argument_list; 135, 136; 135, 137; 136, identifier:rr_id; 137, identifier:rr_size; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:tails; 142, await; 142, 143; 143, call; 143, 144; 143, 154; 144, attribute; 144, 145; 144, 153; 145, call; 145, 146; 145, 147; 146, identifier:Tails; 147, argument_list; 147, 148; 147, 151; 147, 152; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:_dir_tails; 151, identifier:cd_id; 152, identifier:tag; 153, identifier:open; 154, argument_list; 155, comment; 156, if_statement; 156, 157; 156, 160; 156, 171; 157, comparison_operator:is; 157, 158; 157, 159; 158, identifier:revo_cache_entry; 159, None; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 166; 163, subscript; 163, 164; 163, 165; 164, identifier:REVO_CACHE; 165, identifier:rr_id; 166, call; 166, 167; 166, 168; 167, identifier:RevoCacheEntry; 168, argument_list; 168, 169; 168, 170; 169, None; 170, identifier:tails; 171, else_clause; 171, 172; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 180; 175, attribute; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:REVO_CACHE; 178, identifier:rr_id; 179, identifier:tails; 180, identifier:tails; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:LOGGER; 185, identifier:debug; 186, argument_list; 186, 187; 187, string:'Issuer._sync_revoc <<<'
async def _sync_revoc(self, rr_id: str, rr_size: int = None) -> None: """ Create revoc registry if need be for input revocation registry identifier; open and cache tails file reader. :param rr_id: revocation registry identifier :param rr_size: if new revocation registry necessary, its size (default as per _create_rev_reg()) """ LOGGER.debug('Issuer._sync_revoc >>> rr_id: %s, rr_size: %s', rr_id, rr_size) (cd_id, tag) = rev_reg_id2cred_def_id__tag(rr_id) try: await self.get_cred_def(cd_id) except AbsentCredDef: LOGGER.debug( 'Issuer._sync_revoc: <!< tails tree %s may be for another ledger; no cred def found on %s', self._dir_tails, cd_id) raise AbsentCredDef('Tails tree {} may be for another ledger; no cred def found on {}'.format( self._dir_tails, cd_id)) with REVO_CACHE.lock: revo_cache_entry = REVO_CACHE.get(rr_id, None) tails = None if revo_cache_entry is None else revo_cache_entry.tails if tails is None: # it's a new revocation registry, or not yet set in cache try: tails = await Tails(self._dir_tails, cd_id, tag).open() except AbsentTails: await self._create_rev_reg(rr_id, rr_size) # it's a new revocation registry tails = await Tails(self._dir_tails, cd_id, tag).open() # symlink should exist now if revo_cache_entry is None: REVO_CACHE[rr_id] = RevoCacheEntry(None, tails) else: REVO_CACHE[rr_id].tails = tails LOGGER.debug('Issuer._sync_revoc <<<')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:add_namespace_uri; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:ns_uri; 6, default_parameter; 6, 7; 6, 8; 7, identifier:prefix; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:schema_location; 11, None; 12, block; 12, 13; 12, 15; 12, 17; 13, expression_statement; 13, 14; 14, comment; 15, assert_statement; 15, 16; 16, identifier:ns_uri; 17, if_statement; 17, 18; 17, 23; 17, 24; 17, 25; 17, 26; 17, 27; 17, 28; 17, 29; 17, 104; 18, comparison_operator:in; 18, 19; 18, 20; 19, identifier:ns_uri; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:__ns_uri_map; 23, comment; 24, comment; 25, comment; 26, comment; 27, comment; 28, comment; 29, block; 29, 30; 29, 39; 29, 48; 29, 49; 29, 69; 29, 77; 29, 78; 29, 79; 29, 80; 29, 94; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:ni; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:__lookup_uri; 37, argument_list; 37, 38; 38, identifier:ns_uri; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:new_ni; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:copy; 45, identifier:deepcopy; 46, argument_list; 46, 47; 47, identifier:ni; 48, comment; 49, if_statement; 49, 50; 49, 51; 50, identifier:prefix; 51, block; 51, 52; 51, 60; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:__check_prefix_conflict; 57, argument_list; 57, 58; 57, 59; 58, identifier:ni; 59, identifier:prefix; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:new_ni; 65, identifier:prefixes; 66, identifier:add; 67, argument_list; 67, 68; 68, identifier:prefix; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:__merge_schema_locations; 74, argument_list; 74, 75; 74, 76; 75, identifier:new_ni; 76, identifier:schema_location; 77, comment; 78, comment; 79, comment; 80, for_statement; 80, 81; 80, 82; 80, 85; 81, identifier:p; 82, attribute; 82, 83; 82, 84; 83, identifier:new_ni; 84, identifier:prefixes; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 93; 88, subscript; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:__prefix_map; 92, identifier:p; 93, identifier:new_ni; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 103; 96, subscript; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:__ns_uri_map; 100, attribute; 100, 101; 100, 102; 101, identifier:new_ni; 102, identifier:uri; 103, identifier:new_ni; 104, else_clause; 104, 105; 104, 106; 104, 107; 105, comment; 106, comment; 107, block; 107, 108; 107, 119; 107, 128; 108, if_statement; 108, 109; 108, 110; 109, identifier:prefix; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:__check_prefix_conflict; 116, argument_list; 116, 117; 116, 118; 117, identifier:ns_uri; 118, identifier:prefix; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:ni; 122, call; 122, 123; 122, 124; 123, identifier:_NamespaceInfo; 124, argument_list; 124, 125; 124, 126; 124, 127; 125, identifier:ns_uri; 126, identifier:prefix; 127, identifier:schema_location; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:__add_namespaceinfo; 133, argument_list; 133, 134; 134, identifier:ni
def add_namespace_uri(self, ns_uri, prefix=None, schema_location=None): """Adds a new namespace to this set, optionally with a prefix and schema location URI. If the namespace already exists, the given prefix and schema location are merged with the existing entry: * If non-None, ``prefix`` is added to the set. The preferred prefix is not modified. * If a schema location is not already associated with the namespace, it is set to ``schema_location`` (if given). If the namespace doesn't already exist in this set (so a new one is being created) and a prefix is given, that prefix becomes preferred. If not given, a preference as a default namespace is used. Args: ns_uri (str): The URI of the new namespace prefix (str): The desired prefix for the new namespace (optional) schema_location (str): The desired schema location for the new namespace (optional). Raises: DuplicatePrefixError: If a prefix is given which already maps to a different namespace ConflictingSchemaLocationError: If a schema location is given and the namespace already exists in this set with a different schema location. """ assert ns_uri if ns_uri in self.__ns_uri_map: # We have a _NamespaceInfo object for this URI already. So this # is a merge operation. # # We modify a copy of the real _NamespaceInfo so that we are # exception-safe: if something goes wrong, we don't end up with a # half-changed NamespaceSet. ni = self.__lookup_uri(ns_uri) new_ni = copy.deepcopy(ni) # Reconcile prefixes if prefix: self.__check_prefix_conflict(ni, prefix) new_ni.prefixes.add(prefix) self.__merge_schema_locations(new_ni, schema_location) # At this point, we have a legit new_ni object. Now we update # the set, ensuring our invariants. This should replace # all instances of the old ni in this set. for p in new_ni.prefixes: self.__prefix_map[p] = new_ni self.__ns_uri_map[new_ni.uri] = new_ni else: # A brand new namespace. The incoming prefix should not exist at # all in the prefix map. if prefix: self.__check_prefix_conflict(ns_uri, prefix) ni = _NamespaceInfo(ns_uri, prefix, schema_location) self.__add_namespaceinfo(ni)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_schemaloc_string; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ns_uris; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:delim; 13, string:"\n"; 14, block; 14, 15; 14, 17; 14, 32; 14, 42; 14, 46; 14, 76; 14, 82; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 20; 18, not_operator; 18, 19; 19, identifier:ns_uris; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:ns_uris; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:six; 27, identifier:iterkeys; 28, argument_list; 28, 29; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:__ns_uri_map; 32, if_statement; 32, 33; 32, 34; 33, identifier:sort; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:ns_uris; 38, call; 38, 39; 38, 40; 39, identifier:sorted; 40, argument_list; 40, 41; 41, identifier:ns_uris; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:schemalocs; 45, list:[]; 46, for_statement; 46, 47; 46, 48; 46, 49; 47, identifier:ns_uri; 48, identifier:ns_uris; 49, block; 49, 50; 49, 59; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:ni; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:__lookup_uri; 57, argument_list; 57, 58; 58, identifier:ns_uri; 59, if_statement; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:ni; 62, identifier:schema_location; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:schemalocs; 68, identifier:append; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, string:"{0.uri} {0.schema_location}"; 73, identifier:format; 74, argument_list; 74, 75; 75, identifier:ni; 76, if_statement; 76, 77; 76, 79; 77, not_operator; 77, 78; 78, identifier:schemalocs; 79, block; 79, 80; 80, return_statement; 80, 81; 81, string:""; 82, return_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, string:'xsi:schemaLocation="{0}"'; 86, identifier:format; 87, argument_list; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:delim; 91, identifier:join; 92, argument_list; 92, 93; 93, identifier:schemalocs
def get_schemaloc_string(self, ns_uris=None, sort=False, delim="\n"): """Constructs and returns a schemalocation attribute. If no namespaces in this set have any schema locations defined, returns an empty string. Args: ns_uris (iterable): The namespaces to include in the constructed attribute value. If None, all are included. sort (bool): Whether the sort the namespace URIs. delim (str): The delimiter to use between namespace/schemaloc *pairs*. Returns: str: A schemalocation attribute in the format: ``xsi:schemaLocation="nsuri schemaloc<delim>nsuri2 schemaloc2<delim>..."`` """ if not ns_uris: ns_uris = six.iterkeys(self.__ns_uri_map) if sort: ns_uris = sorted(ns_uris) schemalocs = [] for ns_uri in ns_uris: ni = self.__lookup_uri(ns_uri) if ni.schema_location: schemalocs.append("{0.uri} {0.schema_location}".format(ni)) if not schemalocs: return "" return 'xsi:schemaLocation="{0}"'.format(delim.join(schemalocs))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:tab_join; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:ToMerge; 5, default_parameter; 5, 6; 5, 7; 6, identifier:keycols; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:nullvals; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:renamer; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:returnrenaming; 16, False; 17, default_parameter; 17, 18; 17, 19; 18, identifier:Names; 19, None; 20, block; 20, 21; 20, 23; 20, 49; 20, 76; 20, 103; 20, 168; 20, 172; 20, 235; 20, 244; 20, 250; 21, expression_statement; 21, 22; 22, string:''' Database-join for tabular arrays. Wrapper for :func:`tabular.spreadsheet.join` that deals with the coloring and returns the result as a tabarray. Method calls:: data = tabular.spreadsheet.join '''; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 28; 25, list_pattern; 25, 26; 25, 27; 26, identifier:Result; 27, identifier:Renaming; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:spreadsheet; 31, identifier:join; 32, argument_list; 32, 33; 32, 34; 32, 37; 32, 40; 32, 43; 32, 46; 33, identifier:ToMerge; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:keycols; 36, identifier:keycols; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:nullvals; 39, identifier:nullvals; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:renamer; 42, identifier:renamer; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:returnrenaming; 45, True; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:Names; 48, identifier:Names; 49, if_statement; 49, 50; 49, 55; 49, 64; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:ToMerge; 54, identifier:dict; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:Names; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:ToMerge; 62, identifier:keys; 63, argument_list; 64, else_clause; 64, 65; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:Names; 69, call; 69, 70; 69, 71; 70, identifier:range; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:len; 74, argument_list; 74, 75; 75, identifier:ToMerge; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:Colorings; 79, call; 79, 80; 79, 81; 80, identifier:dict; 81, argument_list; 81, 82; 82, list_comprehension; 82, 83; 82, 100; 83, conditional_expression:if; 83, 84; 83, 91; 83, 99; 84, tuple; 84, 85; 84, 86; 85, identifier:k; 86, attribute; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:ToMerge; 89, identifier:k; 90, identifier:coloring; 91, comparison_operator:in; 91, 92; 91, 93; 92, string:'coloring'; 93, call; 93, 94; 93, 95; 94, identifier:dir; 95, argument_list; 95, 96; 96, subscript; 96, 97; 96, 98; 97, identifier:ToMerge; 98, identifier:k; 99, dictionary; 100, for_in_clause; 100, 101; 100, 102; 101, identifier:k; 102, identifier:Names; 103, for_statement; 103, 104; 103, 105; 103, 106; 104, identifier:k; 105, identifier:Names; 106, block; 106, 107; 107, if_statement; 107, 108; 107, 115; 108, comparison_operator:in; 108, 109; 108, 110; 109, identifier:k; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:Renaming; 113, identifier:keys; 114, argument_list; 115, block; 115, 116; 115, 122; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:l; 119, subscript; 119, 120; 119, 121; 120, identifier:ToMerge; 121, identifier:k; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 123, 128; 124, subscript; 124, 125; 124, 126; 125, identifier:Colorings; 126, identifier:k; 127, line_continuation:\; 128, call; 128, 129; 128, 130; 129, identifier:dict; 130, argument_list; 130, 131; 131, list_comprehension; 131, 132; 131, 159; 132, tuple; 132, 133; 132, 134; 133, identifier:g; 134, list_comprehension; 134, 135; 134, 152; 135, conditional_expression:if; 135, 136; 135, 137; 135, 147; 136, identifier:n; 137, not_operator; 137, 138; 138, comparison_operator:in; 138, 139; 138, 140; 139, identifier:n; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:Renaming; 144, identifier:k; 145, identifier:keys; 146, argument_list; 147, subscript; 147, 148; 147, 151; 148, subscript; 148, 149; 148, 150; 149, identifier:Renaming; 150, identifier:k; 151, identifier:n; 152, for_in_clause; 152, 153; 152, 154; 153, identifier:n; 154, subscript; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:l; 157, identifier:coloring; 158, identifier:g; 159, for_in_clause; 159, 160; 159, 161; 160, identifier:g; 161, call; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, subscript; 163, 164; 163, 165; 164, identifier:Colorings; 165, identifier:k; 166, identifier:keys; 167, argument_list; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:Coloring; 171, dictionary; 172, for_statement; 172, 173; 172, 174; 172, 179; 173, identifier:k; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:Colorings; 177, identifier:keys; 178, argument_list; 179, block; 179, 180; 180, for_statement; 180, 181; 180, 182; 180, 189; 181, identifier:j; 182, call; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:Colorings; 186, identifier:k; 187, identifier:keys; 188, argument_list; 189, block; 189, 190; 190, if_statement; 190, 191; 190, 198; 190, 218; 191, comparison_operator:in; 191, 192; 191, 193; 192, identifier:j; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:Coloring; 196, identifier:keys; 197, argument_list; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:Coloring; 203, identifier:j; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:utils; 207, identifier:uniqify; 208, argument_list; 208, 209; 209, binary_operator:+; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:Coloring; 212, identifier:j; 213, subscript; 213, 214; 213, 217; 214, subscript; 214, 215; 214, 216; 215, identifier:Colorings; 216, identifier:k; 217, identifier:j; 218, else_clause; 218, 219; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:Coloring; 224, identifier:j; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:utils; 228, identifier:uniqify; 229, argument_list; 229, 230; 230, subscript; 230, 231; 230, 234; 231, subscript; 231, 232; 231, 233; 232, identifier:Colorings; 233, identifier:k; 234, identifier:j; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:Result; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:Result; 241, identifier:view; 242, argument_list; 242, 243; 243, identifier:tabarray; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:Result; 248, identifier:coloring; 249, identifier:Coloring; 250, if_statement; 250, 251; 250, 252; 250, 257; 251, identifier:returnrenaming; 252, block; 252, 253; 253, return_statement; 253, 254; 254, list:[Result,Renaming]; 254, 255; 254, 256; 255, identifier:Result; 256, identifier:Renaming; 257, else_clause; 257, 258; 258, block; 258, 259; 259, return_statement; 259, 260; 260, identifier:Result
def tab_join(ToMerge, keycols=None, nullvals=None, renamer=None, returnrenaming=False, Names=None): ''' Database-join for tabular arrays. Wrapper for :func:`tabular.spreadsheet.join` that deals with the coloring and returns the result as a tabarray. Method calls:: data = tabular.spreadsheet.join ''' [Result,Renaming] = spreadsheet.join(ToMerge, keycols=keycols, nullvals=nullvals, renamer=renamer, returnrenaming=True, Names=Names) if isinstance(ToMerge,dict): Names = ToMerge.keys() else: Names = range(len(ToMerge)) Colorings = dict([(k,ToMerge[k].coloring) if 'coloring' in dir(ToMerge[k]) else {} for k in Names]) for k in Names: if k in Renaming.keys(): l = ToMerge[k] Colorings[k] = \ dict([(g, [n if not n in Renaming[k].keys() else Renaming[k][n] for n in l.coloring[g]]) for g in Colorings[k].keys()]) Coloring = {} for k in Colorings.keys(): for j in Colorings[k].keys(): if j in Coloring.keys(): Coloring[j] = utils.uniqify(Coloring[j] + Colorings[k][j]) else: Coloring[j] = utils.uniqify(Colorings[k][j]) Result = Result.view(tabarray) Result.coloring = Coloring if returnrenaming: return [Result,Renaming] else: return Result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:aggregate; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:On; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:AggFuncDict; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:AggFunc; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:AggList; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:returnsort; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:KeepOthers; 22, True; 23, default_parameter; 23, 24; 23, 25; 24, identifier:keyfuncdict; 25, None; 26, block; 26, 27; 26, 29; 26, 97; 26, 106; 26, 114; 27, expression_statement; 27, 28; 28, comment; 29, if_statement; 29, 30; 29, 31; 29, 63; 30, identifier:returnsort; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, list_pattern; 34, 35; 34, 36; 35, identifier:data; 36, identifier:s; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:spreadsheet; 40, identifier:aggregate; 41, argument_list; 41, 42; 41, 45; 41, 48; 41, 51; 41, 54; 41, 57; 41, 60; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:X; 44, identifier:self; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:On; 47, identifier:On; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:AggFuncDict; 50, identifier:AggFuncDict; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:AggFunc; 53, identifier:AggFunc; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:AggList; 56, identifier:AggList; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:returnsort; 59, identifier:returnsort; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:keyfuncdict; 62, identifier:keyfuncdict; 63, else_clause; 63, 64; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:data; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:spreadsheet; 71, identifier:aggregate; 72, argument_list; 72, 73; 72, 76; 72, 79; 72, 82; 72, 85; 72, 88; 72, 91; 72, 94; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:X; 75, identifier:self; 76, keyword_argument; 76, 77; 76, 78; 77, identifier:On; 78, identifier:On; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:AggFuncDict; 81, identifier:AggFuncDict; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:AggFunc; 84, identifier:AggFunc; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:AggList; 87, identifier:AggList; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:returnsort; 90, identifier:returnsort; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:KeepOthers; 93, identifier:KeepOthers; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:keyfuncdict; 96, identifier:keyfuncdict; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:data; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:data; 103, identifier:view; 104, argument_list; 104, 105; 105, identifier:tabarray; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:data; 110, identifier:coloring; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:coloring; 114, if_statement; 114, 115; 114, 116; 114, 121; 115, identifier:returnsort; 116, block; 116, 117; 117, return_statement; 117, 118; 118, list:[data, s]; 118, 119; 118, 120; 119, identifier:data; 120, identifier:s; 121, else_clause; 121, 122; 122, block; 122, 123; 123, return_statement; 123, 124; 124, identifier:data
def aggregate(self, On=None, AggFuncDict=None, AggFunc=None, AggList = None, returnsort=False,KeepOthers=True, keyfuncdict=None): """ Aggregate a tabarray on columns for given functions. Method wraps:: tabular.spreadsheet.aggregate(self, On, AggFuncDict, AggFunc, returnsort) """ if returnsort: [data, s] = spreadsheet.aggregate(X=self, On=On, AggFuncDict=AggFuncDict, AggFunc=AggFunc, AggList=AggList, returnsort=returnsort, keyfuncdict=keyfuncdict) else: data = spreadsheet.aggregate(X=self, On=On, AggFuncDict=AggFuncDict, AggFunc=AggFunc, AggList = AggList, returnsort=returnsort, KeepOthers=KeepOthers, keyfuncdict=keyfuncdict) data = data.view(tabarray) data.coloring = self.coloring if returnsort: return [data, s] else: return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:argsort; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 12; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:axis; 7, unary_operator:-; 7, 8; 8, integer:1; 9, default_parameter; 9, 10; 9, 11; 10, identifier:kind; 11, string:'quicksort'; 12, default_parameter; 12, 13; 12, 14; 13, identifier:order; 14, None; 15, block; 15, 16; 15, 18; 15, 35; 15, 46; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:index_array; 21, call; 21, 22; 21, 29; 22, attribute; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:np; 26, identifier:core; 27, identifier:fromnumeric; 28, identifier:_wrapit; 29, argument_list; 29, 30; 29, 31; 29, 32; 29, 33; 29, 34; 30, identifier:self; 31, string:'argsort'; 32, identifier:axis; 33, identifier:kind; 34, identifier:order; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:index_array; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:index_array; 41, identifier:view; 42, argument_list; 42, 43; 43, attribute; 43, 44; 43, 45; 44, identifier:np; 45, identifier:ndarray; 46, return_statement; 46, 47; 47, identifier:index_array
def argsort(self, axis=-1, kind='quicksort', order=None): """ Returns the indices that would sort an array. .. note:: This method wraps `numpy.argsort`. This documentation is modified from that of `numpy.argsort`. Perform an indirect sort along the given axis using the algorithm specified by the `kind` keyword. It returns an array of indices of the same shape as the original array that index data along the given axis in sorted order. **Parameters** **axis** : int or None, optional Axis along which to sort. The default is -1 (the last axis). If `None`, the flattened array is used. **kind** : {'quicksort', 'mergesort', 'heapsort'}, optional Sorting algorithm. **order** : list, optional This argument specifies which fields to compare first, second, etc. Not all fields need be specified. **Returns** **index_array** : ndarray, int Array of indices that sort the tabarray along the specified axis. In other words, ``a[index_array]`` yields a sorted `a`. **See Also** sort : Describes sorting algorithms used. lexsort : Indirect stable sort with multiple keys. ndarray.sort : Inplace sort. **Notes** See `numpy.sort` for notes on the different sorting algorithms. **Examples** Sorting with keys: >>> x = tabarray([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')]) >>> x tabarray([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')]) >>> x.argsort(order=('x','y')) array([1, 0]) >>> x.argsort(order=('y','x')) array([0, 1]) """ index_array = np.core.fromnumeric._wrapit(self, 'argsort', axis, kind, order) index_array = index_array.view(np.ndarray) return index_array
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_finalize_namespaces; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ns_dict; 7, None; 8, block; 8, 9; 8, 11; 8, 36; 8, 37; 8, 59; 8, 60; 8, 61; 8, 67; 8, 68; 8, 91; 8, 92; 8, 103; 8, 104; 8, 105; 8, 106; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 13; 11, 14; 12, identifier:ns_dict; 13, comment; 14, block; 14, 15; 15, for_statement; 15, 16; 15, 19; 15, 25; 16, pattern_list; 16, 17; 16, 18; 17, identifier:ns; 18, identifier:alias; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:six; 22, identifier:iteritems; 23, argument_list; 23, 24; 24, identifier:ns_dict; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_collected_namespaces; 32, identifier:add_namespace_uri; 33, argument_list; 33, 34; 33, 35; 34, identifier:ns; 35, identifier:alias; 36, comment; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_collected_namespaces; 43, identifier:add_namespace_uri; 44, argument_list; 44, 45; 44, 52; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:ns_uri; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:idgen; 50, identifier:get_id_namespace; 51, argument_list; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:prefix; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:idgen; 57, identifier:get_id_namespace_alias; 58, argument_list; 59, comment; 60, comment; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:_fix_example_namespace; 66, argument_list; 67, comment; 68, for_statement; 68, 69; 68, 72; 68, 80; 69, pattern_list; 69, 70; 69, 71; 70, identifier:prefix; 71, identifier:uri; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:six; 75, identifier:iteritems; 76, argument_list; 76, 77; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:_input_namespaces; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:_collected_namespaces; 87, identifier:add_namespace_uri; 88, argument_list; 88, 89; 88, 90; 89, identifier:uri; 90, identifier:prefix; 91, comment; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_collected_namespaces; 98, identifier:import_from; 99, argument_list; 99, 100; 100, attribute; 100, 101; 100, 102; 101, identifier:namespaces; 102, identifier:XML_NAMESPACES; 103, comment; 104, comment; 105, comment; 106, for_statement; 106, 107; 106, 108; 106, 113; 107, identifier:ns_uri; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_collected_namespaces; 112, identifier:namespace_uris; 113, block; 113, 114; 113, 125; 113, 129; 113, 130; 113, 141; 113, 165; 113, 177; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:preferred_prefix; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_collected_namespaces; 122, identifier:preferred_prefix_for_namespace; 123, argument_list; 123, 124; 124, identifier:ns_uri; 125, if_statement; 125, 126; 125, 127; 126, identifier:preferred_prefix; 127, block; 127, 128; 128, continue_statement; 129, comment; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:prefixes; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:_collected_namespaces; 138, identifier:get_prefixes; 139, argument_list; 139, 140; 140, identifier:ns_uri; 141, if_statement; 141, 142; 141, 143; 141, 154; 142, identifier:prefixes; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:prefix; 147, call; 147, 148; 147, 149; 148, identifier:next; 149, argument_list; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:iter; 152, argument_list; 152, 153; 153, identifier:prefixes; 154, else_clause; 154, 155; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:prefix; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:namespaces; 162, identifier:lookup_name; 163, argument_list; 163, 164; 164, identifier:ns_uri; 165, if_statement; 165, 166; 165, 169; 166, comparison_operator:is; 166, 167; 166, 168; 167, identifier:prefix; 168, None; 169, block; 169, 170; 170, raise_statement; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:namespaces; 174, identifier:NoPrefixesError; 175, argument_list; 175, 176; 176, identifier:ns_uri; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 184; 179, attribute; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:self; 182, identifier:_collected_namespaces; 183, identifier:set_preferred_prefix_for_namespace; 184, argument_list; 184, 185; 184, 188; 184, 191; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:ns_uri; 187, identifier:ns_uri; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:prefix; 190, identifier:prefix; 191, keyword_argument; 191, 192; 191, 193; 192, identifier:add_if_not_exist; 193, True
def _finalize_namespaces(self, ns_dict=None): """Returns a dictionary of namespaces to be exported with an XML document. This loops over all the namespaces that were discovered and built during the execution of ``collect()`` and ``_parse_collected_classes()`` and attempts to merge them all. Raises: .namespaces.DuplicatePrefixError: If namespace prefix was mapped to more than one namespace. .namespaces.NoPrefixError: If a namespace was collected that is not mapped to a prefix. """ if ns_dict: # Add the user's entries to our set for ns, alias in six.iteritems(ns_dict): self._collected_namespaces.add_namespace_uri(ns, alias) # Add the ID namespaces self._collected_namespaces.add_namespace_uri( ns_uri=idgen.get_id_namespace(), prefix=idgen.get_id_namespace_alias() ) # Remap the example namespace to the one expected by the APIs if the # sample example namespace is found. self._fix_example_namespace() # Add _input_namespaces for prefix, uri in six.iteritems(self._input_namespaces): self._collected_namespaces.add_namespace_uri(uri, prefix) # Add some default XML namespaces to make sure they're there. self._collected_namespaces.import_from(namespaces.XML_NAMESPACES) # python-stix's generateDS-generated binding classes can't handle # default namespaces. So make sure there are no preferred defaults in # the set. Get prefixes from the global namespace set if we have to. for ns_uri in self._collected_namespaces.namespace_uris: preferred_prefix = self._collected_namespaces.preferred_prefix_for_namespace(ns_uri) if preferred_prefix: continue # No preferred prefix set for namespace. Try to assign one. prefixes = self._collected_namespaces.get_prefixes(ns_uri) if prefixes: prefix = next(iter(prefixes)) else: prefix = namespaces.lookup_name(ns_uri) if prefix is None: raise namespaces.NoPrefixesError(ns_uri) self._collected_namespaces.set_preferred_prefix_for_namespace( ns_uri=ns_uri, prefix=prefix, add_if_not_exist=True )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:update_empty_fields; 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, 28; 7, 46; 7, 64; 7, 95; 7, 113; 7, 129; 7, 146; 7, 164; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 16; 11, comparison_operator:is; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_is_deprecated; 15, None; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_is_deprecated; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:kwargs; 25, identifier:get; 26, argument_list; 26, 27; 27, string:'is_deprecated'; 28, if_statement; 28, 29; 28, 34; 29, comparison_operator:is; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_is_dubious; 33, None; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_is_dubious; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:kwargs; 43, identifier:get; 44, argument_list; 44, 45; 45, string:'is_dubious'; 46, if_statement; 46, 47; 46, 52; 47, comparison_operator:is; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:_is_synonym; 51, None; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:_is_synonym; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:kwargs; 61, identifier:get; 62, argument_list; 62, 63; 63, string:'is_synonym'; 64, if_statement; 64, 65; 64, 70; 65, comparison_operator:is; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:_synonyms; 69, identifier:_EMPTY_TUPLE; 70, block; 70, 71; 70, 82; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:_synonyms; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:kwargs; 79, identifier:get; 80, argument_list; 80, 81; 81, string:'synonyms'; 82, if_statement; 82, 83; 82, 88; 83, comparison_operator:is; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:_synonyms; 87, None; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:_synonyms; 94, identifier:_EMPTY_TUPLE; 95, if_statement; 95, 96; 95, 101; 96, comparison_operator:is; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:rank; 100, None; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:_rank; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:kwargs; 110, identifier:get; 111, argument_list; 111, 112; 112, string:'rank'; 113, if_statement; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:_nomenclature_code; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:_nomenclature_code; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:kwargs; 126, identifier:get; 127, argument_list; 127, 128; 128, string:'nomenclature_code'; 129, if_statement; 129, 130; 129, 134; 130, not_operator; 130, 131; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:_unique_name; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:_unique_name; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:kwargs; 143, identifier:get; 144, argument_list; 144, 145; 145, string:'unique_name'; 146, if_statement; 146, 147; 146, 152; 147, comparison_operator:is; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:_taxonomic_lineage; 151, None; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:_taxonomic_lineage; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:kwargs; 161, identifier:get; 162, argument_list; 162, 163; 163, string:'taxonomic_lineage'; 164, if_statement; 164, 165; 164, 170; 165, comparison_operator:is; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:_parent; 169, None; 170, block; 170, 171; 170, 182; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:_parent; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:kwargs; 179, identifier:get; 180, argument_list; 180, 181; 181, string:'parent'; 182, if_statement; 182, 183; 182, 198; 183, boolean_operator:and; 183, 184; 183, 195; 184, boolean_operator:and; 184, 185; 184, 190; 185, comparison_operator:is; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:_parent; 189, None; 190, comparison_operator:is; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:_taxomachine_wrapper; 194, None; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:_taxonomic_lineage; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:_fill_parent_attr; 204, argument_list
def update_empty_fields(self, **kwargs): """Updates the field of info about an OTU that might not be filled in by a match_names or taxon call.""" if self._is_deprecated is None: self._is_deprecated = kwargs.get('is_deprecated') if self._is_dubious is None: self._is_dubious = kwargs.get('is_dubious') if self._is_synonym is None: self._is_synonym = kwargs.get('is_synonym') if self._synonyms is _EMPTY_TUPLE: self._synonyms = kwargs.get('synonyms') if self._synonyms is None: self._synonyms = _EMPTY_TUPLE if self.rank is None: self._rank = kwargs.get('rank') if self._nomenclature_code: self._nomenclature_code = kwargs.get('nomenclature_code') if not self._unique_name: self._unique_name = kwargs.get('unique_name') if self._taxonomic_lineage is None: self._taxonomic_lineage = kwargs.get('taxonomic_lineage') if self._parent is None: self._parent = kwargs.get('parent') if self._parent is None and self._taxomachine_wrapper is not None and self._taxonomic_lineage: self._fill_parent_attr()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 1, 25; 2, function_name:get_creds; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 14; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:proof_req_json; 7, type; 7, 8; 8, identifier:str; 9, typed_default_parameter; 9, 10; 9, 11; 9, 13; 10, identifier:filt; 11, type; 11, 12; 12, identifier:dict; 13, None; 14, typed_default_parameter; 14, 15; 14, 16; 14, 18; 15, identifier:filt_dflt_incl; 16, type; 16, 17; 17, identifier:bool; 18, False; 19, type; 19, 20; 20, tuple; 20, 21; 20, 24; 21, subscript; 21, 22; 21, 23; 22, identifier:Set; 23, identifier:str; 24, identifier:str; 25, block; 25, 26; 25, 28; 25, 37; 25, 46; 25, 50; 25, 65; 25, 74; 25, 80; 25, 120; 25, 311; 25, 327; 25, 338; 25, 346; 26, expression_statement; 26, 27; 27, comment; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:LOGGER; 32, identifier:debug; 33, argument_list; 33, 34; 33, 35; 33, 36; 34, string:'HolderProver.get_creds >>> proof_req_json: %s, filt: %s'; 35, identifier:proof_req_json; 36, identifier:filt; 37, if_statement; 37, 38; 37, 41; 38, comparison_operator:is; 38, 39; 38, 40; 39, identifier:filt; 40, None; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:filt; 45, dictionary; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:rv; 49, None; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:creds_json; 53, await; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:anoncreds; 57, identifier:prover_get_credentials_for_proof_req; 58, argument_list; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:wallet; 63, identifier:handle; 64, identifier:proof_req_json; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:creds; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:json; 71, identifier:loads; 72, argument_list; 72, 73; 73, identifier:creds_json; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:cred_ids; 77, call; 77, 78; 77, 79; 78, identifier:set; 79, argument_list; 80, if_statement; 80, 81; 80, 82; 81, identifier:filt; 82, block; 82, 83; 83, for_statement; 83, 84; 83, 85; 83, 86; 84, identifier:cd_id; 85, identifier:filt; 86, block; 86, 87; 87, try_statement; 87, 88; 87, 102; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:json; 93, identifier:loads; 94, argument_list; 94, 95; 95, await; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:get_cred_def; 100, argument_list; 100, 101; 101, identifier:cd_id; 102, except_clause; 102, 103; 102, 104; 103, identifier:AbsentCredDef; 104, block; 104, 105; 104, 113; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:LOGGER; 109, identifier:warning; 110, argument_list; 110, 111; 110, 112; 111, string:'HolderProver.get_creds: ignoring filter criterion, no cred def on %s'; 112, identifier:cd_id; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:filt; 117, identifier:pop; 118, argument_list; 118, 119; 119, identifier:cd_id; 120, for_statement; 120, 121; 120, 122; 120, 135; 121, identifier:inner_creds; 122, call; 122, 123; 122, 134; 123, attribute; 123, 124; 123, 133; 124, dictionary; 124, 125; 124, 129; 125, dictionary_splat; 125, 126; 126, subscript; 126, 127; 126, 128; 127, identifier:creds; 128, string:'attrs'; 129, dictionary_splat; 129, 130; 130, subscript; 130, 131; 130, 132; 131, identifier:creds; 132, string:'predicates'; 133, identifier:values; 134, argument_list; 135, block; 135, 136; 136, for_statement; 136, 137; 136, 138; 136, 139; 136, 140; 137, identifier:cred; 138, identifier:inner_creds; 139, comment; 140, block; 140, 141; 140, 147; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:cred_info; 144, subscript; 144, 145; 144, 146; 145, identifier:cred; 146, string:'cred_info'; 147, if_statement; 147, 148; 147, 149; 147, 300; 148, identifier:filt; 149, block; 149, 150; 149, 156; 149, 174; 149, 229; 149, 291; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:cred_cd_id; 153, subscript; 153, 154; 153, 155; 154, identifier:cred_info; 155, string:'cred_def_id'; 156, if_statement; 156, 157; 156, 160; 157, comparison_operator:not; 157, 158; 157, 159; 158, identifier:cred_cd_id; 159, identifier:filt; 160, block; 160, 161; 160, 173; 161, if_statement; 161, 162; 161, 163; 162, identifier:filt_dflt_incl; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:cred_ids; 168, identifier:add; 169, argument_list; 169, 170; 170, subscript; 170, 171; 170, 172; 171, identifier:cred_info; 172, string:'referent'; 173, continue_statement; 174, if_statement; 174, 175; 174, 183; 174, 184; 175, comparison_operator:in; 175, 176; 175, 177; 176, string:'attr-match'; 177, parenthesized_expression; 177, 178; 178, boolean_operator:or; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:filt; 181, identifier:cred_cd_id; 182, dictionary; 183, comment; 184, block; 184, 185; 185, if_statement; 185, 186; 185, 227; 186, not_operator; 186, 187; 187, comparison_operator:<=; 187, 188; 187, 220; 188, call; 188, 189; 188, 219; 189, attribute; 189, 190; 189, 218; 190, dictionary_comprehension; 190, 191; 190, 207; 191, pair; 191, 192; 191, 193; 192, identifier:k; 193, call; 193, 194; 193, 195; 194, identifier:str; 195, argument_list; 195, 196; 196, subscript; 196, 197; 196, 206; 197, call; 197, 198; 197, 203; 198, attribute; 198, 199; 198, 202; 199, subscript; 199, 200; 199, 201; 200, identifier:filt; 201, identifier:cred_cd_id; 202, identifier:get; 203, argument_list; 203, 204; 203, 205; 204, string:'attr-match'; 205, dictionary; 206, identifier:k; 207, for_in_clause; 207, 208; 207, 209; 208, identifier:k; 209, call; 209, 210; 209, 215; 210, attribute; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:filt; 213, identifier:cred_cd_id; 214, identifier:get; 215, argument_list; 215, 216; 215, 217; 216, string:'attr-match'; 217, dictionary; 218, identifier:items; 219, argument_list; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:cred_info; 224, string:'attrs'; 225, identifier:items; 226, argument_list; 227, block; 227, 228; 228, continue_statement; 229, if_statement; 229, 230; 229, 238; 229, 239; 230, comparison_operator:in; 230, 231; 230, 232; 231, string:'minima'; 232, parenthesized_expression; 232, 233; 233, boolean_operator:or; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:filt; 236, identifier:cred_cd_id; 237, dictionary; 238, comment; 239, block; 239, 240; 239, 252; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:minima; 243, call; 243, 244; 243, 249; 244, attribute; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:filt; 247, identifier:cred_cd_id; 248, identifier:get; 249, argument_list; 249, 250; 249, 251; 250, string:'minima'; 251, dictionary; 252, try_statement; 252, 253; 252, 286; 253, block; 253, 254; 254, if_statement; 254, 255; 254, 284; 255, call; 255, 256; 255, 257; 256, identifier:any; 257, generator_expression; 257, 258; 257, 281; 258, boolean_operator:or; 258, 259; 258, 265; 259, parenthesized_expression; 259, 260; 260, comparison_operator:not; 260, 261; 260, 262; 261, identifier:attr; 262, subscript; 262, 263; 262, 264; 263, identifier:cred_info; 264, string:'attrs'; 265, parenthesized_expression; 265, 266; 266, comparison_operator:<; 266, 267; 266, 275; 267, call; 267, 268; 267, 269; 268, identifier:int; 269, argument_list; 269, 270; 270, subscript; 270, 271; 270, 274; 271, subscript; 271, 272; 271, 273; 272, identifier:cred_info; 273, string:'attrs'; 274, identifier:attr; 275, call; 275, 276; 275, 277; 276, identifier:int; 277, argument_list; 277, 278; 278, subscript; 278, 279; 278, 280; 279, identifier:minima; 280, identifier:attr; 281, for_in_clause; 281, 282; 281, 283; 282, identifier:attr; 283, identifier:minima; 284, block; 284, 285; 285, continue_statement; 286, except_clause; 286, 287; 286, 288; 287, identifier:ValueError; 288, block; 288, 289; 288, 290; 289, continue_statement; 290, comment; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:cred_ids; 295, identifier:add; 296, argument_list; 296, 297; 297, subscript; 297, 298; 297, 299; 298, identifier:cred_info; 299, string:'referent'; 300, else_clause; 300, 301; 301, block; 301, 302; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:cred_ids; 306, identifier:add; 307, argument_list; 307, 308; 308, subscript; 308, 309; 308, 310; 309, identifier:cred_info; 310, string:'referent'; 311, if_statement; 311, 312; 311, 313; 312, identifier:filt; 313, block; 313, 314; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:creds; 317, call; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:json; 320, identifier:loads; 321, argument_list; 321, 322; 322, call; 322, 323; 322, 324; 323, identifier:prune_creds_json; 324, argument_list; 324, 325; 324, 326; 325, identifier:creds; 326, identifier:cred_ids; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:rv; 330, tuple; 330, 331; 330, 332; 331, identifier:cred_ids; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:json; 335, identifier:dumps; 336, argument_list; 336, 337; 337, identifier:creds; 338, expression_statement; 338, 339; 339, call; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:LOGGER; 342, identifier:debug; 343, argument_list; 343, 344; 343, 345; 344, string:'HolderProver.get_creds <<< %s'; 345, identifier:rv; 346, return_statement; 346, 347; 347, identifier:rv
async def get_creds(self, proof_req_json: str, filt: dict = None, filt_dflt_incl: bool = False) -> (Set[str], str): """ Get credentials from HolderProver wallet corresponding to proof request and filter criteria; return credential identifiers from wallet and credentials json. Return empty set and empty production for no such credentials. :param proof_req_json: proof request json as Verifier creates; has entries for proof request's nonce, name, and version; plus credential's requested attributes, requested predicates. I.e., :: { 'nonce': string, # indy-sdk makes no semantic specification on this value 'name': string, # indy-sdk makes no semantic specification on this value 'version': numeric-string, # indy-sdk makes no semantic specification on this value 'requested_attributes': { '<attr_uuid>': { # aka attr_referent, a proof-request local identifier 'name': string, # attribute name (matches case- and space-insensitively) 'restrictions' [ # optional { "schema_id": string, # optional "schema_issuer_did": string, # optional "schema_name": string, # optional "schema_version": string, # optional "issuer_did": string, # optional "cred_def_id": string # optional }, { ... # if more than one restriction given, combined disjunctively (i.e., via OR) } ], 'non_revoked': { # optional - indy-sdk ignores when getting creds from wallet 'from': int, # optional, epoch seconds 'to': int # optional, epoch seconds } }, ... }, 'requested_predicates': { '<pred_uuid>': { # aka predicate_referent, a proof-request local predicate identifier 'name': string, # attribute name (matches case- and space-insensitively) 'p_type': '>=', 'p_value': int, # predicate value 'restrictions': [ # optional { "schema_id": string, # optional "schema_issuer_did": string, # optional "schema_name": string, # optional "schema_version": string, # optional "issuer_did": string, # optional "cred_def_id": string # optional }, { ... # if more than one restriction given, combined disjunctively (i.e., via OR) } ], 'non_revoked': { # optional - indy-sdk ignores when getting creds from wallet 'from': int, # optional, epoch seconds 'to': int # optional, epoch seconds } }, ... }, 'non_revoked': { # optional - indy-sdk ignores when getting creds from wallet 'from': Optional<int>, 'to': Optional<int> } } :param filt: filter for matching attribute-value pairs and predicates; dict mapping each cred def id to dict (specify empty dict or none for no filter, matching all) mapping attributes to values to match or compare. E.g., :: { 'Vx4E82R17q...:3:CL:16:0': { 'attr-match': { 'name': 'Alex', 'sex': 'M', 'favouriteDrink': None }, 'minima': { # if both attr-match and minima present, combined conjunctively (i.e., via AND) 'favouriteNumber' : 10, 'score': '100' # nicety: implementation converts to int for caller }, }, 'R17v42T4pk...:3:CL:19:0': { 'attr-match': { 'height': 175, 'birthdate': '1975-11-15' # combined conjunctively (i.e., via AND) } }, 'Z9ccax812j...:3:CL:27:0': { 'attr-match': {} # match all attributes on this cred def } ... } :param filt_dflt_incl: whether to include (True) all credentials from wallet that filter does not identify by cred def, or to exclude (False) all such credentials :return: tuple with (set of referents, creds json for input proof request); empty set and empty production for no such credential """ LOGGER.debug('HolderProver.get_creds >>> proof_req_json: %s, filt: %s', proof_req_json, filt) if filt is None: filt = {} rv = None creds_json = await anoncreds.prover_get_credentials_for_proof_req(self.wallet.handle, proof_req_json) creds = json.loads(creds_json) cred_ids = set() if filt: for cd_id in filt: try: json.loads(await self.get_cred_def(cd_id)) except AbsentCredDef: LOGGER.warning('HolderProver.get_creds: ignoring filter criterion, no cred def on %s', cd_id) filt.pop(cd_id) for inner_creds in {**creds['attrs'], **creds['predicates']}.values(): for cred in inner_creds: # cred is a dict in a list of dicts cred_info = cred['cred_info'] if filt: cred_cd_id = cred_info['cred_def_id'] if cred_cd_id not in filt: if filt_dflt_incl: cred_ids.add(cred_info['referent']) continue if 'attr-match' in (filt[cred_cd_id] or {}): # maybe filt[cred_cd_id]: None if not {k: str(filt[cred_cd_id].get('attr-match', {})[k]) for k in filt[cred_cd_id].get('attr-match', {})}.items() <= cred_info['attrs'].items(): continue if 'minima' in (filt[cred_cd_id] or {}): # maybe filt[cred_cd_id]: None minima = filt[cred_cd_id].get('minima', {}) try: if any((attr not in cred_info['attrs']) or (int(cred_info['attrs'][attr]) < int(minima[attr])) for attr in minima): continue except ValueError: continue # int conversion failed - reject candidate cred_ids.add(cred_info['referent']) else: cred_ids.add(cred_info['referent']) if filt: creds = json.loads(prune_creds_json(creds, cred_ids)) rv = (cred_ids, json.dumps(creds)) LOGGER.debug('HolderProver.get_creds <<< %s', rv) return rv
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:summary; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:processors; 5, identifier:metrics; 6, identifier:context; 7, block; 7, 8; 7, 10; 7, 11; 7, 47; 7, 83; 7, 123; 7, 127; 7, 209; 7, 216; 7, 253; 7, 258; 7, 265; 7, 274; 7, 306; 7, 315; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 21; 12, function_name:display_header; 13, parameters; 13, 14; 13, 15; 13, 18; 14, identifier:processors; 15, default_parameter; 15, 16; 15, 17; 16, identifier:before; 17, string:''; 18, default_parameter; 18, 19; 18, 20; 19, identifier:after; 20, string:''; 21, block; 21, 22; 21, 24; 21, 32; 21, 42; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:print; 27, argument_list; 27, 28; 27, 29; 28, identifier:before; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:end; 31, string:' '; 32, for_statement; 32, 33; 32, 34; 32, 35; 33, identifier:processor; 34, identifier:processors; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:processor; 40, identifier:display_header; 41, argument_list; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:print; 45, argument_list; 45, 46; 46, identifier:after; 47, function_definition; 47, 48; 47, 49; 47, 57; 48, function_name:display_separator; 49, parameters; 49, 50; 49, 51; 49, 54; 50, identifier:processors; 51, default_parameter; 51, 52; 51, 53; 52, identifier:before; 53, string:''; 54, default_parameter; 54, 55; 54, 56; 55, identifier:after; 56, string:''; 57, block; 57, 58; 57, 60; 57, 68; 57, 78; 58, expression_statement; 58, 59; 59, comment; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:print; 63, argument_list; 63, 64; 63, 65; 64, identifier:before; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:end; 67, string:' '; 68, for_statement; 68, 69; 68, 70; 68, 71; 69, identifier:processor; 70, identifier:processors; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:processor; 76, identifier:display_separator; 77, argument_list; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:print; 81, argument_list; 81, 82; 82, identifier:after; 83, function_definition; 83, 84; 83, 85; 83, 96; 84, function_name:display_metrics; 85, parameters; 85, 86; 85, 87; 85, 90; 85, 93; 86, identifier:processors; 87, default_parameter; 87, 88; 87, 89; 88, identifier:before; 89, string:''; 90, default_parameter; 90, 91; 90, 92; 91, identifier:after; 92, string:''; 93, default_parameter; 93, 94; 93, 95; 94, identifier:metrics; 95, list:[]; 96, block; 96, 97; 96, 99; 96, 107; 96, 118; 97, expression_statement; 97, 98; 98, comment; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:print; 102, argument_list; 102, 103; 102, 104; 103, identifier:before; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:end; 106, string:' '; 107, for_statement; 107, 108; 107, 109; 107, 110; 108, identifier:processor; 109, identifier:processors; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:processor; 115, identifier:display_metrics; 116, argument_list; 116, 117; 117, identifier:metrics; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:print; 121, argument_list; 121, 122; 122, identifier:after; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:summary; 126, dictionary; 127, for_statement; 127, 128; 127, 129; 127, 130; 128, identifier:m; 129, identifier:metrics; 130, block; 130, 131; 130, 139; 130, 145; 130, 161; 130, 169; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:lang; 134, subscript; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:metrics; 137, identifier:m; 138, string:'language'; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:has_key; 142, comparison_operator:in; 142, 143; 142, 144; 143, identifier:lang; 144, identifier:summary; 145, if_statement; 145, 146; 145, 148; 146, not_operator; 146, 147; 147, identifier:has_key; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:summary; 153, identifier:lang; 154, dictionary; 154, 155; 154, 158; 155, pair; 155, 156; 155, 157; 156, string:'file_count'; 157, integer:0; 158, pair; 158, 159; 158, 160; 159, string:'language'; 160, identifier:lang; 161, expression_statement; 161, 162; 162, augmented_assignment:+=; 162, 163; 162, 168; 163, subscript; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:summary; 166, identifier:lang; 167, string:'file_count'; 168, integer:1; 169, for_statement; 169, 170; 169, 171; 169, 174; 170, identifier:i; 171, subscript; 171, 172; 171, 173; 172, identifier:metrics; 173, identifier:m; 174, block; 174, 175; 174, 185; 174, 197; 175, if_statement; 175, 176; 175, 182; 175, 183; 176, comparison_operator:not; 176, 177; 176, 178; 177, identifier:i; 178, list:['sloc', 'comments', 'mccabe']; 178, 179; 178, 180; 178, 181; 179, string:'sloc'; 180, string:'comments'; 181, string:'mccabe'; 182, comment; 183, block; 183, 184; 184, continue_statement; 185, if_statement; 185, 186; 185, 188; 186, not_operator; 186, 187; 187, identifier:has_key; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 196; 191, subscript; 191, 192; 191, 195; 192, subscript; 192, 193; 192, 194; 193, identifier:summary; 194, identifier:lang; 195, identifier:i; 196, integer:0; 197, expression_statement; 197, 198; 198, augmented_assignment:+=; 198, 199; 198, 204; 199, subscript; 199, 200; 199, 203; 200, subscript; 200, 201; 200, 202; 201, identifier:summary; 202, identifier:lang; 203, identifier:i; 204, subscript; 204, 205; 204, 208; 205, subscript; 205, 206; 205, 207; 206, identifier:metrics; 207, identifier:m; 208, identifier:i; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:total; 212, dictionary; 212, 213; 213, pair; 213, 214; 213, 215; 214, string:'language'; 215, string:'Total'; 216, for_statement; 216, 217; 216, 218; 216, 219; 217, identifier:m; 218, identifier:summary; 219, block; 219, 220; 220, for_statement; 220, 221; 220, 222; 220, 225; 221, identifier:i; 222, subscript; 222, 223; 222, 224; 223, identifier:summary; 224, identifier:m; 225, block; 225, 226; 225, 232; 225, 243; 226, if_statement; 226, 227; 226, 230; 227, comparison_operator:==; 227, 228; 227, 229; 228, identifier:i; 229, string:'language'; 230, block; 230, 231; 231, continue_statement; 232, if_statement; 232, 233; 232, 236; 233, comparison_operator:not; 233, 234; 233, 235; 234, identifier:i; 235, identifier:total; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 242; 239, subscript; 239, 240; 239, 241; 240, identifier:total; 241, identifier:i; 242, integer:0; 243, expression_statement; 243, 244; 244, augmented_assignment:+=; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:total; 247, identifier:i; 248, subscript; 248, 249; 248, 252; 249, subscript; 249, 250; 249, 251; 250, identifier:summary; 251, identifier:m; 252, identifier:i; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 256; 255, identifier:print; 256, argument_list; 256, 257; 257, string:'Metrics Summary:'; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 261; 260, identifier:display_header; 261, argument_list; 261, 262; 261, 263; 261, 264; 262, identifier:processors; 263, string:'Files'; 264, string:''; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 268; 267, identifier:display_separator; 268, argument_list; 268, 269; 268, 270; 268, 273; 269, identifier:processors; 270, binary_operator:*; 270, 271; 270, 272; 271, string:'-'; 272, integer:5; 273, string:''; 274, for_statement; 274, 275; 274, 276; 274, 289; 275, identifier:k; 276, call; 276, 277; 276, 278; 277, identifier:sorted; 278, argument_list; 278, 279; 278, 284; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:summary; 282, identifier:keys; 283, argument_list; 284, keyword_argument; 284, 285; 284, 286; 285, identifier:key; 286, attribute; 286, 287; 286, 288; 287, identifier:str; 288, identifier:lower; 289, block; 289, 290; 290, expression_statement; 290, 291; 291, call; 291, 292; 291, 293; 292, identifier:display_metrics; 293, argument_list; 293, 294; 293, 295; 293, 302; 293, 303; 294, identifier:processors; 295, binary_operator:%; 295, 296; 295, 297; 296, string:'%5d'; 297, subscript; 297, 298; 297, 301; 298, subscript; 298, 299; 298, 300; 299, identifier:summary; 300, identifier:k; 301, string:'file_count'; 302, string:''; 303, subscript; 303, 304; 303, 305; 304, identifier:summary; 305, identifier:k; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 309; 308, identifier:display_separator; 309, argument_list; 309, 310; 309, 311; 309, 314; 310, identifier:processors; 311, binary_operator:*; 311, 312; 311, 313; 312, string:'-'; 313, integer:5; 314, string:''; 315, expression_statement; 315, 316; 316, call; 316, 317; 316, 318; 317, identifier:display_metrics; 318, argument_list; 318, 319; 318, 320; 318, 325; 318, 326; 319, identifier:processors; 320, binary_operator:%; 320, 321; 320, 322; 321, string:'%5d'; 322, subscript; 322, 323; 322, 324; 323, identifier:total; 324, string:'file_count'; 325, string:''; 326, identifier:total
def summary(processors, metrics, context): """Print the summary""" # display aggregated metric values on language level def display_header(processors, before='', after=''): """Display the header for the summary results.""" print(before, end=' ') for processor in processors: processor.display_header() print(after) def display_separator(processors, before='', after=''): """Display the header for the summary results.""" print(before, end=' ') for processor in processors: processor.display_separator() print(after) def display_metrics(processors, before='', after='', metrics=[]): """Display the header for the summary results.""" print(before, end=' ') for processor in processors: processor.display_metrics(metrics) print(after) summary = {} for m in metrics: lang = metrics[m]['language'] has_key = lang in summary if not has_key: summary[lang] = {'file_count': 0, 'language': lang} summary[lang]['file_count'] += 1 for i in metrics[m]: if i not in ['sloc', 'comments', 'mccabe']: # include metrics to be used continue if not has_key: summary[lang][i] = 0 summary[lang][i] += metrics[m][i] total = {'language': 'Total'} for m in summary: for i in summary[m]: if i == 'language': continue if i not in total: total[i] = 0 total[i] += summary[m][i] print('Metrics Summary:') display_header(processors, 'Files', '') display_separator(processors, '-'*5, '') for k in sorted(summary.keys(), key=str.lower): display_metrics(processors, '%5d' % summary[k]['file_count'], '', summary[k]) display_separator(processors, '-'*5, '') display_metrics(processors, '%5d' % total['file_count'], '', total)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:run_experiment; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:experiment; 5, identifier:roleouts; 6, identifier:episodes; 7, default_parameter; 7, 8; 7, 9; 8, identifier:in_cloud; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:dynProfile; 12, None; 13, block; 13, 14; 13, 16; 13, 407; 13, 452; 14, expression_statement; 14, 15; 15, comment; 16, function_definition; 16, 17; 16, 18; 16, 19; 17, function_name:run; 18, parameters; 19, block; 19, 20; 19, 45; 19, 54; 19, 62; 19, 71; 19, 80; 19, 89; 19, 90; 19, 91; 19, 98; 19, 402; 20, if_statement; 20, 21; 20, 24; 20, 35; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:dynProfile; 23, None; 24, block; 24, 25; 24, 34; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:maxsteps; 28, call; 28, 29; 28, 30; 29, identifier:len; 30, argument_list; 30, 31; 31, attribute; 31, 32; 31, 33; 32, identifier:experiment; 33, identifier:profile; 34, comment; 35, else_clause; 35, 36; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:maxsteps; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:dynProfile; 43, identifier:shape; 44, integer:1; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:na; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:experiment; 53, identifier:agents; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:ni; 57, binary_operator:*; 57, 58; 57, 61; 58, binary_operator:*; 58, 59; 58, 60; 59, identifier:roleouts; 60, identifier:episodes; 61, identifier:maxsteps; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:all_action; 65, call; 65, 66; 65, 67; 66, identifier:zeros; 67, argument_list; 67, 68; 68, tuple; 68, 69; 68, 70; 69, identifier:na; 70, integer:0; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:all_reward; 74, call; 74, 75; 74, 76; 75, identifier:zeros; 76, argument_list; 76, 77; 77, tuple; 77, 78; 77, 79; 78, identifier:na; 79, integer:0; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:epsilon; 83, call; 83, 84; 83, 85; 84, identifier:zeros; 85, argument_list; 85, 86; 86, tuple; 86, 87; 86, 88; 87, identifier:na; 88, identifier:ni; 89, comment; 90, comment; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:vmarkup; 94, call; 94, 95; 94, 96; 95, identifier:vectorize; 96, argument_list; 96, 97; 97, identifier:get_markup; 98, for_statement; 98, 99; 98, 100; 98, 104; 99, identifier:roleout; 100, call; 100, 101; 100, 102; 101, identifier:range; 102, argument_list; 102, 103; 103, identifier:roleouts; 104, block; 104, 105; 104, 133; 104, 134; 104, 141; 104, 142; 104, 148; 104, 149; 104, 158; 104, 167; 104, 388; 104, 395; 105, if_statement; 105, 106; 105, 109; 105, 110; 106, comparison_operator:is; 106, 107; 106, 108; 107, identifier:dynProfile; 108, None; 109, comment; 110, block; 110, 111; 110, 117; 110, 118; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:i; 114, binary_operator:*; 114, 115; 114, 116; 115, identifier:roleout; 116, identifier:episodes; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:experiment; 122, identifier:profile; 123, subscript; 123, 124; 123, 125; 123, 131; 124, identifier:dynProfile; 125, slice; 125, 126; 125, 127; 125, 128; 126, identifier:i; 127, colon; 128, binary_operator:+; 128, 129; 128, 130; 129, identifier:i; 130, identifier:episodes; 131, slice; 131, 132; 132, colon; 133, comment; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:experiment; 138, identifier:doEpisodes; 139, argument_list; 139, 140; 140, identifier:episodes; 141, comment; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:nei; 145, binary_operator:*; 145, 146; 145, 147; 146, identifier:episodes; 147, identifier:maxsteps; 148, comment; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:epi_action; 152, call; 152, 153; 152, 154; 153, identifier:zeros; 154, argument_list; 154, 155; 155, tuple; 155, 156; 155, 157; 156, integer:0; 157, identifier:nei; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:epi_reward; 161, call; 161, 162; 161, 163; 162, identifier:zeros; 163, argument_list; 163, 164; 164, tuple; 164, 165; 164, 166; 165, integer:0; 166, identifier:nei; 167, for_statement; 167, 168; 167, 173; 167, 174; 167, 186; 168, pattern_list; 168, 169; 168, 170; 169, identifier:i; 170, tuple_pattern; 170, 171; 170, 172; 171, identifier:task; 172, identifier:agent; 173, line_continuation:\; 174, call; 174, 175; 174, 176; 175, identifier:enumerate; 176, argument_list; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:zip; 179, argument_list; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:experiment; 182, identifier:tasks; 183, attribute; 183, 184; 183, 185; 184, identifier:experiment; 185, identifier:agents; 186, block; 186, 187; 186, 198; 186, 209; 186, 327; 186, 328; 186, 347; 186, 362; 186, 376; 186, 382; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:action; 190, call; 190, 191; 190, 192; 191, identifier:copy; 192, argument_list; 192, 193; 193, subscript; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:agent; 196, identifier:history; 197, string:"action"; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:reward; 201, call; 201, 202; 201, 203; 202, identifier:copy; 203, argument_list; 203, 204; 204, subscript; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:agent; 207, identifier:history; 208, string:"reward"; 209, for_statement; 209, 210; 209, 211; 209, 215; 210, identifier:j; 211, call; 211, 212; 211, 213; 212, identifier:range; 213, argument_list; 213, 214; 214, identifier:nei; 215, block; 215, 216; 216, if_statement; 216, 217; 216, 224; 216, 268; 216, 317; 217, call; 217, 218; 217, 219; 218, identifier:isinstance; 219, argument_list; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:agent; 222, identifier:learner; 223, identifier:DirectSearchLearner; 224, block; 224, 225; 224, 242; 224, 248; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 232; 227, subscript; 227, 228; 227, 229; 227, 230; 228, identifier:action; 229, identifier:j; 230, slice; 230, 231; 231, colon; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:task; 235, identifier:denormalize; 236, argument_list; 236, 237; 237, subscript; 237, 238; 237, 239; 237, 240; 238, identifier:action; 239, identifier:j; 240, slice; 240, 241; 241, colon; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:k; 245, binary_operator:*; 245, 246; 245, 247; 246, identifier:nei; 247, identifier:roleout; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 259; 250, subscript; 250, 251; 250, 252; 250, 253; 251, identifier:epsilon; 252, identifier:i; 253, slice; 253, 254; 253, 255; 253, 256; 254, identifier:k; 255, colon; 256, binary_operator:+; 256, 257; 256, 258; 257, identifier:k; 258, identifier:nei; 259, subscript; 259, 260; 259, 267; 260, attribute; 260, 261; 260, 266; 261, attribute; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:agent; 264, identifier:learner; 265, identifier:explorer; 266, identifier:sigma; 267, integer:0; 268, elif_clause; 268, 269; 268, 276; 269, call; 269, 270; 269, 271; 270, identifier:isinstance; 271, argument_list; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:agent; 274, identifier:learner; 275, identifier:ValueBasedLearner; 276, block; 276, 277; 276, 293; 276, 299; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 284; 279, subscript; 279, 280; 279, 281; 279, 282; 280, identifier:action; 281, identifier:j; 282, slice; 282, 283; 283, colon; 284, call; 284, 285; 284, 286; 285, identifier:vmarkup; 286, argument_list; 286, 287; 286, 292; 287, subscript; 287, 288; 287, 289; 287, 290; 288, identifier:action; 289, identifier:j; 290, slice; 290, 291; 291, colon; 292, identifier:task; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:k; 296, binary_operator:*; 296, 297; 296, 298; 297, identifier:nei; 298, identifier:roleout; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 310; 301, subscript; 301, 302; 301, 303; 301, 304; 302, identifier:epsilon; 303, identifier:i; 304, slice; 304, 305; 304, 306; 304, 307; 305, identifier:k; 306, colon; 307, binary_operator:+; 307, 308; 307, 309; 308, identifier:k; 309, identifier:nei; 310, attribute; 310, 311; 310, 316; 311, attribute; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:agent; 314, identifier:learner; 315, identifier:explorer; 316, identifier:epsilon; 317, else_clause; 317, 318; 318, block; 318, 319; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:action; 322, call; 322, 323; 322, 324; 323, identifier:vmarkup; 324, argument_list; 324, 325; 324, 326; 325, identifier:action; 326, identifier:task; 327, comment; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 331; 330, identifier:epi_action; 331, attribute; 331, 332; 331, 346; 332, subscript; 332, 333; 332, 334; 332, 337; 333, identifier:c_; 334, attribute; 334, 335; 334, 336; 335, identifier:epi_action; 336, identifier:T; 337, call; 337, 338; 337, 345; 338, attribute; 338, 339; 338, 344; 339, subscript; 339, 340; 339, 341; 339, 343; 340, identifier:action; 341, slice; 341, 342; 342, colon; 343, integer:0; 344, identifier:flatten; 345, argument_list; 346, identifier:T; 347, expression_statement; 347, 348; 348, assignment; 348, 349; 348, 350; 349, identifier:epi_reward; 350, attribute; 350, 351; 350, 361; 351, subscript; 351, 352; 351, 353; 351, 356; 352, identifier:c_; 353, attribute; 353, 354; 353, 355; 354, identifier:epi_reward; 355, identifier:T; 356, call; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:reward; 359, identifier:flatten; 360, argument_list; 361, identifier:T; 362, if_statement; 362, 363; 362, 368; 363, call; 363, 364; 363, 365; 364, identifier:hasattr; 365, argument_list; 365, 366; 365, 367; 366, identifier:agent; 367, string:"module"; 368, block; 368, 369; 369, print_statement; 369, 370; 369, 371; 370, string:"PARAMS:"; 371, attribute; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:agent; 374, identifier:module; 375, identifier:params; 376, expression_statement; 376, 377; 377, call; 377, 378; 377, 381; 378, attribute; 378, 379; 378, 380; 379, identifier:agent; 380, identifier:learn; 381, argument_list; 382, expression_statement; 382, 383; 383, call; 383, 384; 383, 387; 384, attribute; 384, 385; 384, 386; 385, identifier:agent; 386, identifier:reset; 387, argument_list; 388, expression_statement; 388, 389; 389, assignment; 389, 390; 389, 391; 390, identifier:all_action; 391, subscript; 391, 392; 391, 393; 391, 394; 392, identifier:c_; 393, identifier:all_action; 394, identifier:epi_action; 395, expression_statement; 395, 396; 396, assignment; 396, 397; 396, 398; 397, identifier:all_reward; 398, subscript; 398, 399; 398, 400; 398, 401; 399, identifier:c_; 400, identifier:all_reward; 401, identifier:epi_reward; 402, return_statement; 402, 403; 403, expression_list; 403, 404; 403, 405; 403, 406; 404, identifier:all_action; 405, identifier:all_reward; 406, identifier:epsilon; 407, if_statement; 407, 408; 407, 409; 407, 441; 408, identifier:in_cloud; 409, block; 409, 410; 409, 413; 409, 425; 409, 434; 410, import_statement; 410, 411; 411, dotted_name; 411, 412; 412, identifier:cloud; 413, expression_statement; 413, 414; 414, assignment; 414, 415; 414, 416; 415, identifier:job_id; 416, call; 416, 417; 416, 420; 417, attribute; 417, 418; 417, 419; 418, identifier:cloud; 419, identifier:call; 420, argument_list; 420, 421; 420, 422; 421, identifier:run; 422, keyword_argument; 422, 423; 422, 424; 423, identifier:_high_cpu; 424, False; 425, expression_statement; 425, 426; 426, assignment; 426, 427; 426, 428; 427, identifier:result; 428, call; 428, 429; 428, 432; 429, attribute; 429, 430; 429, 431; 430, identifier:cloud; 431, identifier:result; 432, argument_list; 432, 433; 433, identifier:job_id; 434, expression_statement; 434, 435; 435, assignment; 435, 436; 435, 440; 436, pattern_list; 436, 437; 436, 438; 436, 439; 437, identifier:all_action; 438, identifier:all_reward; 439, identifier:epsilon; 440, identifier:result; 441, else_clause; 441, 442; 442, block; 442, 443; 443, expression_statement; 443, 444; 444, assignment; 444, 445; 444, 449; 445, pattern_list; 445, 446; 445, 447; 445, 448; 446, identifier:all_action; 447, identifier:all_reward; 448, identifier:epsilon; 449, call; 449, 450; 449, 451; 450, identifier:run; 451, argument_list; 452, return_statement; 452, 453; 453, expression_list; 453, 454; 453, 455; 453, 456; 454, identifier:all_action; 455, identifier:all_reward; 456, identifier:epsilon
def run_experiment(experiment, roleouts, episodes, in_cloud=False, dynProfile=None): """ Runs the given experiment and returns the results. """ def run(): if dynProfile is None: maxsteps = len(experiment.profile) # episode length else: maxsteps = dynProfile.shape[1] na = len(experiment.agents) ni = roleouts * episodes * maxsteps all_action = zeros((na, 0)) all_reward = zeros((na, 0)) epsilon = zeros((na, ni)) # exploration rate # Converts to action vector in percentage markup values. vmarkup = vectorize(get_markup) for roleout in range(roleouts): if dynProfile is not None: # Apply new load profile before each roleout (week). i = roleout * episodes # index of first profile value experiment.profile = dynProfile[i:i + episodes, :] # print "PROFILE:", experiment.profile, episodes experiment.doEpisodes(episodes) # number of samples per learning step nei = episodes * maxsteps # num interactions per role epi_action = zeros((0, nei)) epi_reward = zeros((0, nei)) for i, (task, agent) in \ enumerate(zip(experiment.tasks, experiment.agents)): action = copy(agent.history["action"]) reward = copy(agent.history["reward"]) for j in range(nei): if isinstance(agent.learner, DirectSearchLearner): action[j, :] = task.denormalize(action[j, :]) k = nei * roleout epsilon[i, k:k + nei] = agent.learner.explorer.sigma[0] elif isinstance(agent.learner, ValueBasedLearner): action[j, :] = vmarkup(action[j, :], task) k = nei * roleout epsilon[i, k:k + nei] = agent.learner.explorer.epsilon else: action = vmarkup(action, task) # FIXME: Only stores action[0] for all interactions. epi_action = c_[epi_action.T, action[:, 0].flatten()].T epi_reward = c_[epi_reward.T, reward.flatten()].T if hasattr(agent, "module"): print "PARAMS:", agent.module.params agent.learn() agent.reset() all_action = c_[all_action, epi_action] all_reward = c_[all_reward, epi_reward] return all_action, all_reward, epsilon if in_cloud: import cloud job_id = cloud.call(run, _high_cpu=False) result = cloud.result(job_id) all_action, all_reward, epsilon = result else: all_action, all_reward, epsilon = run() return all_action, all_reward, epsilon
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:total_cost; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:p; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:p_cost; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:pcost_model; 13, None; 14, block; 14, 15; 14, 17; 14, 28; 14, 39; 14, 50; 14, 60; 14, 184; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:p; 20, conditional_expression:if; 20, 21; 20, 24; 20, 27; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:p; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:p; 26, None; 27, identifier:p; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:p_cost; 31, conditional_expression:if; 31, 32; 31, 35; 31, 38; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:p_cost; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:p_cost; 37, None; 38, identifier:p_cost; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:pcost_model; 42, conditional_expression:if; 42, 43; 42, 46; 42, 49; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:pcost_model; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:pcost_model; 48, None; 49, identifier:pcost_model; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:p; 53, conditional_expression:if; 53, 54; 53, 55; 53, 59; 54, float:0.0; 55, not_operator; 55, 56; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:online; 59, identifier:p; 60, if_statement; 60, 61; 60, 64; 60, 164; 60, 180; 61, comparison_operator:==; 61, 62; 61, 63; 62, identifier:pcost_model; 63, identifier:PW_LINEAR; 64, block; 64, 65; 64, 74; 64, 75; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:n_segments; 68, binary_operator:-; 68, 69; 68, 73; 69, call; 69, 70; 69, 71; 70, identifier:len; 71, argument_list; 71, 72; 72, identifier:p_cost; 73, integer:1; 74, comment; 75, for_statement; 75, 76; 75, 77; 75, 81; 75, 135; 76, identifier:i; 77, call; 77, 78; 77, 79; 78, identifier:range; 79, argument_list; 79, 80; 80, identifier:n_segments; 81, block; 81, 82; 81, 90; 81, 100; 81, 112; 81, 120; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, pattern_list; 84, 85; 84, 86; 85, identifier:x1; 86, identifier:y1; 87, subscript; 87, 88; 87, 89; 88, identifier:p_cost; 89, identifier:i; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, pattern_list; 92, 93; 92, 94; 93, identifier:x2; 94, identifier:y2; 95, subscript; 95, 96; 95, 97; 96, identifier:p_cost; 97, binary_operator:+; 97, 98; 97, 99; 98, identifier:i; 99, integer:1; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:m; 103, binary_operator:/; 103, 104; 103, 108; 104, parenthesized_expression; 104, 105; 105, binary_operator:-; 105, 106; 105, 107; 106, identifier:y2; 107, identifier:y1; 108, parenthesized_expression; 108, 109; 109, binary_operator:-; 109, 110; 109, 111; 110, identifier:x2; 111, identifier:x1; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:c; 115, binary_operator:-; 115, 116; 115, 117; 116, identifier:y1; 117, binary_operator:*; 117, 118; 117, 119; 118, identifier:m; 119, identifier:x1; 120, if_statement; 120, 121; 120, 125; 121, comparison_operator:<=; 121, 122; 121, 123; 121, 124; 122, identifier:x1; 123, identifier:p; 124, identifier:x2; 125, block; 125, 126; 125, 134; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:result; 129, binary_operator:+; 129, 130; 129, 133; 130, binary_operator:*; 130, 131; 130, 132; 131, identifier:m; 132, identifier:p; 133, identifier:c; 134, break_statement; 135, else_clause; 135, 136; 135, 137; 135, 138; 135, 139; 136, comment; 137, comment; 138, comment; 139, block; 139, 140; 139, 156; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:logger; 144, identifier:error; 145, argument_list; 145, 146; 146, binary_operator:%; 146, 147; 146, 148; 147, string:"Value [%f] outside pwl cost curve [%s]."; 148, tuple; 148, 149; 148, 150; 149, identifier:p; 150, subscript; 150, 151; 150, 155; 151, subscript; 151, 152; 151, 153; 152, identifier:p_cost; 153, unary_operator:-; 153, 154; 154, integer:1; 155, integer:0; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:result; 159, binary_operator:+; 159, 160; 159, 163; 160, binary_operator:*; 160, 161; 160, 162; 161, identifier:m; 162, identifier:p; 163, identifier:c; 164, elif_clause; 164, 165; 164, 168; 164, 169; 164, 170; 164, 171; 165, comparison_operator:==; 165, 166; 165, 167; 166, identifier:pcost_model; 167, identifier:POLYNOMIAL; 168, comment; 169, comment; 170, comment; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:result; 175, call; 175, 176; 175, 177; 176, identifier:polyval; 177, argument_list; 177, 178; 177, 179; 178, identifier:p_cost; 179, identifier:p; 180, else_clause; 180, 181; 181, block; 181, 182; 182, raise_statement; 182, 183; 183, identifier:ValueError; 184, if_statement; 184, 185; 184, 188; 184, 192; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:is_load; 188, block; 188, 189; 189, return_statement; 189, 190; 190, unary_operator:-; 190, 191; 191, identifier:result; 192, else_clause; 192, 193; 193, block; 193, 194; 194, return_statement; 194, 195; 195, identifier:result
def total_cost(self, p=None, p_cost=None, pcost_model=None): """ Computes total cost for the generator at the given output level. """ p = self.p if p is None else p p_cost = self.p_cost if p_cost is None else p_cost pcost_model = self.pcost_model if pcost_model is None else pcost_model p = 0.0 if not self.online else p if pcost_model == PW_LINEAR: n_segments = len(p_cost) - 1 # Iterate over the piece-wise linear segments. for i in range(n_segments): x1, y1 = p_cost[i] x2, y2 = p_cost[i + 1] m = (y2 - y1) / (x2 - x1) c = y1 - m * x1 if x1 <= p <= x2: result = m*p + c break else: # print "TOTC:", self.name, p, self.p_max, p_cost # raise ValueError, "Value [%f] outwith pwl cost curve." % p # Use the last segment for values outwith the cost curve. logger.error("Value [%f] outside pwl cost curve [%s]." % (p, p_cost[-1][0])) result = m*p + c elif pcost_model == POLYNOMIAL: # result = p_cost[-1] # for i in range(1, len(p_cost)): # result += p_cost[-(i + 1)] * p**i result = polyval(p_cost, p) else: raise ValueError if self.is_load: return -result else: return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:offers_to_pwl; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:offers; 6, block; 6, 7; 6, 9; 6, 14; 6, 15; 6, 29; 6, 30; 6, 48; 6, 49; 6, 62; 6, 75; 6, 87; 6, 166; 6, 201; 6, 231; 7, expression_statement; 7, 8; 8, comment; 9, assert_statement; 9, 10; 10, not_operator; 10, 11; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:is_load; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:g_offers; 18, list_comprehension; 18, 19; 18, 20; 18, 23; 19, identifier:offer; 20, for_in_clause; 20, 21; 20, 22; 21, identifier:offer; 22, identifier:offers; 23, if_clause; 23, 24; 24, comparison_operator:==; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:offer; 27, identifier:generator; 28, identifier:self; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:gt_zero; 33, list_comprehension; 33, 34; 33, 35; 33, 38; 34, identifier:offr; 35, for_in_clause; 35, 36; 35, 37; 36, identifier:offr; 37, identifier:g_offers; 38, if_clause; 38, 39; 39, comparison_operator:>; 39, 40; 39, 47; 40, call; 40, 41; 40, 42; 41, identifier:round; 42, argument_list; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:offr; 45, identifier:quantity; 46, integer:4; 47, float:0.0; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:valid; 52, list_comprehension; 52, 53; 52, 54; 52, 57; 53, identifier:offer; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:offer; 56, identifier:gt_zero; 57, if_clause; 57, 58; 58, not_operator; 58, 59; 59, attribute; 59, 60; 59, 61; 60, identifier:offer; 61, identifier:withheld; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:p_offers; 65, list_comprehension; 65, 66; 65, 67; 65, 70; 66, identifier:v; 67, for_in_clause; 67, 68; 67, 69; 68, identifier:v; 69, identifier:valid; 70, if_clause; 70, 71; 71, not_operator; 71, 72; 72, attribute; 72, 73; 72, 74; 73, identifier:v; 74, identifier:reactive; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:q_offers; 78, list_comprehension; 78, 79; 78, 80; 78, 83; 79, identifier:v; 80, for_in_clause; 80, 81; 80, 82; 81, identifier:v; 82, identifier:valid; 83, if_clause; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:v; 86, identifier:reactive; 87, if_statement; 87, 88; 87, 89; 87, 113; 88, identifier:p_offers; 89, block; 89, 90; 89, 101; 89, 107; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:p_cost; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:_offbids_to_points; 99, argument_list; 99, 100; 100, identifier:p_offers; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:pcost_model; 106, identifier:PW_LINEAR; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:online; 112, True; 113, else_clause; 113, 114; 114, block; 114, 115; 114, 129; 114, 135; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:p_cost; 120, list:[(0.0, 0.0), (self.p_max, 0.0)]; 120, 121; 120, 124; 121, tuple; 121, 122; 121, 123; 122, float:0.0; 123, float:0.0; 124, tuple; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:p_max; 128, float:0.0; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:pcost_model; 134, identifier:PW_LINEAR; 135, if_statement; 135, 136; 135, 137; 135, 138; 135, 139; 135, 158; 136, identifier:q_offers; 137, comment; 138, comment; 139, block; 139, 140; 139, 146; 139, 152; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:p_min; 145, float:0.0; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:p_max; 151, float:0.0; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:online; 157, True; 158, else_clause; 158, 159; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:online; 165, False; 166, if_statement; 166, 167; 166, 168; 166, 186; 167, identifier:q_offers; 168, block; 168, 169; 168, 180; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:q_cost; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:_offbids_to_points; 178, argument_list; 178, 179; 179, identifier:q_offers; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:qcost_model; 185, identifier:PW_LINEAR; 186, else_clause; 186, 187; 187, block; 187, 188; 187, 194; 187, 195; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:q_cost; 193, None; 194, comment; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:qcost_model; 200, identifier:PW_LINEAR; 201, if_statement; 201, 202; 201, 213; 202, boolean_operator:and; 202, 203; 202, 208; 203, not_operator; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:len; 206, argument_list; 206, 207; 207, identifier:p_offers; 208, not_operator; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:q_offers; 213, block; 213, 214; 213, 225; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:logger; 218, identifier:info; 219, argument_list; 219, 220; 220, binary_operator:%; 220, 221; 220, 222; 221, string:"No valid offers for generator [%s], shutting down."; 222, attribute; 222, 223; 222, 224; 223, identifier:self; 224, identifier:name; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:online; 230, False; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:_adjust_limits; 236, argument_list
def offers_to_pwl(self, offers): """ Updates the piece-wise linear total cost function using the given offer blocks. Based on off2case.m from MATPOWER by Ray Zimmerman, developed at PSERC Cornell. See U{http://www.pserc.cornell.edu/matpower/} for more info. """ assert not self.is_load # Only apply offers associated with this generator. g_offers = [offer for offer in offers if offer.generator == self] # Fliter out zero quantity offers. gt_zero = [offr for offr in g_offers if round(offr.quantity, 4) > 0.0] # Ignore withheld offers. valid = [offer for offer in gt_zero if not offer.withheld] p_offers = [v for v in valid if not v.reactive] q_offers = [v for v in valid if v.reactive] if p_offers: self.p_cost = self._offbids_to_points(p_offers) self.pcost_model = PW_LINEAR self.online = True else: self.p_cost = [(0.0, 0.0), (self.p_max, 0.0)] self.pcost_model = PW_LINEAR if q_offers: # Dispatch at zero real power without shutting down # if capacity offered for reactive power. self.p_min = 0.0 self.p_max = 0.0 self.online = True else: self.online = False if q_offers: self.q_cost = self._offbids_to_points(q_offers) self.qcost_model = PW_LINEAR else: self.q_cost = None#[(0.0, 0.0), (self.q_max, 0.0)] self.qcost_model = PW_LINEAR if not len(p_offers) and not len(q_offers): logger.info("No valid offers for generator [%s], shutting down." % self.name) self.online = False self._adjust_limits()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:bids_to_pwl; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:bids; 6, block; 6, 7; 6, 9; 6, 13; 6, 14; 6, 28; 6, 29; 6, 47; 6, 48; 6, 61; 6, 74; 6, 86; 6, 154; 6, 208; 6, 209; 6, 210; 7, expression_statement; 7, 8; 8, comment; 9, assert_statement; 9, 10; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:is_load; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:vl_bids; 17, list_comprehension; 17, 18; 17, 19; 17, 22; 18, identifier:bid; 19, for_in_clause; 19, 20; 19, 21; 20, identifier:bid; 21, identifier:bids; 22, if_clause; 22, 23; 23, comparison_operator:==; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:bid; 26, identifier:vLoad; 27, identifier:self; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:gt_zero; 32, list_comprehension; 32, 33; 32, 34; 32, 37; 33, identifier:bid; 34, for_in_clause; 34, 35; 34, 36; 35, identifier:bid; 36, identifier:vl_bids; 37, if_clause; 37, 38; 38, comparison_operator:>; 38, 39; 38, 46; 39, call; 39, 40; 39, 41; 40, identifier:round; 41, argument_list; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:bid; 44, identifier:quantity; 45, integer:4; 46, float:0.0; 47, comment; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:valid_bids; 51, list_comprehension; 51, 52; 51, 53; 51, 56; 52, identifier:bid; 53, for_in_clause; 53, 54; 53, 55; 54, identifier:bid; 55, identifier:gt_zero; 56, if_clause; 56, 57; 57, not_operator; 57, 58; 58, attribute; 58, 59; 58, 60; 59, identifier:bid; 60, identifier:withheld; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:p_bids; 64, list_comprehension; 64, 65; 64, 66; 64, 69; 65, identifier:v; 66, for_in_clause; 66, 67; 66, 68; 67, identifier:v; 68, identifier:valid_bids; 69, if_clause; 69, 70; 70, not_operator; 70, 71; 71, attribute; 71, 72; 71, 73; 72, identifier:v; 73, identifier:reactive; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:q_bids; 77, list_comprehension; 77, 78; 77, 79; 77, 82; 78, identifier:v; 79, for_in_clause; 79, 80; 79, 81; 80, identifier:v; 81, identifier:valid_bids; 82, if_clause; 82, 83; 83, attribute; 83, 84; 83, 85; 84, identifier:v; 85, identifier:reactive; 86, if_statement; 86, 87; 86, 88; 86, 113; 87, identifier:p_bids; 88, block; 88, 89; 88, 101; 88, 107; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:p_cost; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_offbids_to_points; 98, argument_list; 98, 99; 98, 100; 99, identifier:p_bids; 100, True; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:pcost_model; 106, identifier:PW_LINEAR; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:online; 112, True; 113, else_clause; 113, 114; 114, block; 114, 115; 114, 129; 114, 135; 114, 148; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:p_cost; 120, list:[(0.0, 0.0), (self.p_max, 0.0)]; 120, 121; 120, 124; 121, tuple; 121, 122; 121, 123; 122, float:0.0; 123, float:0.0; 124, tuple; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:p_max; 128, float:0.0; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:pcost_model; 134, identifier:PW_LINEAR; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:logger; 139, identifier:info; 140, argument_list; 140, 141; 141, binary_operator:%; 141, 142; 141, 145; 142, concatenated_string; 142, 143; 142, 144; 143, string:"No valid active power bids for dispatchable load "; 144, string:"[%s], shutting down."; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:name; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:online; 153, False; 154, if_statement; 154, 155; 154, 156; 154, 181; 155, identifier:q_bids; 156, block; 156, 157; 156, 169; 156, 175; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:q_cost; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:_offbids_to_points; 166, argument_list; 166, 167; 166, 168; 167, identifier:q_bids; 168, True; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:qcost_model; 174, identifier:PW_LINEAR; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:online; 180, True; 181, else_clause; 181, 182; 182, block; 182, 183; 182, 202; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:q_cost; 188, list:[(self.q_min, 0.0), (0.0, 0.0), (self.q_max, 0.0)]; 188, 189; 188, 194; 188, 197; 189, tuple; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:q_min; 193, float:0.0; 194, tuple; 194, 195; 194, 196; 195, float:0.0; 196, float:0.0; 197, tuple; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:q_max; 201, float:0.0; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:qcost_model; 207, identifier:PW_LINEAR; 208, comment; 209, comment; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:_adjust_limits; 215, argument_list
def bids_to_pwl(self, bids): """ Updates the piece-wise linear total cost function using the given bid blocks. Based on off2case.m from MATPOWER by Ray Zimmerman, developed at PSERC Cornell. See U{http://www.pserc.cornell.edu/matpower/} for more info. """ assert self.is_load # Apply only those bids associated with this dispatchable load. vl_bids = [bid for bid in bids if bid.vLoad == self] # Filter out zero quantity bids. gt_zero = [bid for bid in vl_bids if round(bid.quantity, 4) > 0.0] # Ignore withheld offers. valid_bids = [bid for bid in gt_zero if not bid.withheld] p_bids = [v for v in valid_bids if not v.reactive] q_bids = [v for v in valid_bids if v.reactive] if p_bids: self.p_cost = self._offbids_to_points(p_bids, True) self.pcost_model = PW_LINEAR self.online = True else: self.p_cost = [(0.0, 0.0), (self.p_max, 0.0)] self.pcost_model = PW_LINEAR logger.info("No valid active power bids for dispatchable load " "[%s], shutting down." % self.name) self.online = False if q_bids: self.q_cost = self._offbids_to_points(q_bids, True) self.qcost_model = PW_LINEAR self.online = True else: self.q_cost = [(self.q_min, 0.0), (0.0, 0.0), (self.q_max, 0.0)] self.qcost_model = PW_LINEAR # logger.info("No valid bids for dispatchable load, shutting down.") # self.online = False self._adjust_limits()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:DoxyfileParse; 3, parameters; 3, 4; 4, identifier:file_contents; 5, block; 5, 6; 5, 8; 5, 12; 5, 15; 5, 29; 5, 35; 5, 49; 5, 55; 5, 61; 5, 69; 5, 73; 5, 74; 5, 78; 5, 82; 5, 86; 5, 90; 5, 130; 5, 277; 5, 278; 5, 330; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:data; 11, dictionary; 12, import_statement; 12, 13; 13, dotted_name; 13, 14; 14, identifier:shlex; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:lex; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:shlex; 21, identifier:shlex; 22, argument_list; 22, 23; 22, 26; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:instream; 25, identifier:file_contents; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:posix; 28, True; 29, expression_statement; 29, 30; 30, augmented_assignment:+=; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:lex; 33, identifier:wordchars; 34, string:"*+./-:"; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:lex; 39, identifier:whitespace; 40, call; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:lex; 44, identifier:whitespace; 45, identifier:replace; 46, argument_list; 46, 47; 46, 48; 47, string:"\n"; 48, string:""; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:lex; 53, identifier:escape; 54, string:""; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:lineno; 58, attribute; 58, 59; 58, 60; 59, identifier:lex; 60, identifier:lineno; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:token; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:lex; 67, identifier:get_token; 68, argument_list; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:key; 72, identifier:token; 73, comment; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:last_token; 77, string:""; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:key_token; 81, False; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:next_key; 85, False; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:new_data; 89, True; 90, function_definition; 90, 91; 90, 92; 90, 97; 91, function_name:append_data; 92, parameters; 92, 93; 92, 94; 92, 95; 92, 96; 93, identifier:data; 94, identifier:key; 95, identifier:new_data; 96, identifier:token; 97, block; 97, 98; 98, if_statement; 98, 99; 98, 109; 98, 119; 99, boolean_operator:or; 99, 100; 99, 101; 100, identifier:new_data; 101, comparison_operator:==; 101, 102; 101, 108; 102, call; 102, 103; 102, 104; 103, identifier:len; 104, argument_list; 104, 105; 105, subscript; 105, 106; 105, 107; 106, identifier:data; 107, identifier:key; 108, integer:0; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:data; 115, identifier:key; 116, identifier:append; 117, argument_list; 117, 118; 118, identifier:token; 119, else_clause; 119, 120; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, augmented_assignment:+=; 122, 123; 122, 129; 123, subscript; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:data; 126, identifier:key; 127, unary_operator:-; 127, 128; 128, integer:1; 129, identifier:token; 130, while_statement; 130, 131; 130, 132; 131, identifier:token; 132, block; 132, 133; 132, 244; 132, 248; 132, 256; 133, if_statement; 133, 134; 133, 138; 133, 149; 133, 156; 133, 167; 134, comparison_operator:in; 134, 135; 134, 136; 135, identifier:token; 136, list:['\n']; 136, 137; 137, string:'\n'; 138, block; 138, 139; 139, if_statement; 139, 140; 139, 144; 140, comparison_operator:not; 140, 141; 140, 142; 141, identifier:last_token; 142, list:['\\']; 142, 143; 143, string:'\\'; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:key_token; 148, True; 149, elif_clause; 149, 150; 149, 154; 150, comparison_operator:in; 150, 151; 150, 152; 151, identifier:token; 152, list:['\\']; 152, 153; 153, string:'\\'; 154, block; 154, 155; 155, pass_statement; 156, elif_clause; 156, 157; 156, 158; 157, identifier:key_token; 158, block; 158, 159; 158, 163; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:key; 162, identifier:token; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:key_token; 166, False; 167, else_clause; 167, 168; 168, block; 168, 169; 169, if_statement; 169, 170; 169, 173; 169, 191; 169, 230; 170, comparison_operator:==; 170, 171; 170, 172; 171, identifier:token; 172, string:"+="; 173, block; 173, 174; 174, if_statement; 174, 175; 174, 182; 175, not_operator; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:data; 179, identifier:has_key; 180, argument_list; 180, 181; 181, identifier:key; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:data; 187, identifier:key; 188, call; 188, 189; 188, 190; 189, identifier:list; 190, argument_list; 191, elif_clause; 191, 192; 191, 195; 192, comparison_operator:==; 192, 193; 192, 194; 193, identifier:token; 194, string:"="; 195, block; 195, 196; 196, if_statement; 196, 197; 196, 207; 196, 220; 197, boolean_operator:and; 197, 198; 197, 201; 198, comparison_operator:==; 198, 199; 198, 200; 199, identifier:key; 200, string:"TAGFILES"; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:data; 204, identifier:has_key; 205, argument_list; 205, 206; 206, identifier:key; 207, block; 207, 208; 207, 216; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:append_data; 211, argument_list; 211, 212; 211, 213; 211, 214; 211, 215; 212, identifier:data; 213, identifier:key; 214, False; 215, string:"="; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:new_data; 219, False; 220, else_clause; 220, 221; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:data; 226, identifier:key; 227, call; 227, 228; 227, 229; 228, identifier:list; 229, argument_list; 230, else_clause; 230, 231; 231, block; 231, 232; 231, 240; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 235; 234, identifier:append_data; 235, argument_list; 235, 236; 235, 237; 235, 238; 235, 239; 236, identifier:data; 237, identifier:key; 238, identifier:new_data; 239, identifier:token; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:new_data; 243, True; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:last_token; 247, identifier:token; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:token; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:lex; 254, identifier:get_token; 255, argument_list; 256, if_statement; 256, 257; 256, 264; 257, boolean_operator:and; 257, 258; 257, 261; 258, comparison_operator:==; 258, 259; 258, 260; 259, identifier:last_token; 260, string:'\\'; 261, comparison_operator:!=; 261, 262; 261, 263; 262, identifier:token; 263, string:'\n'; 264, block; 264, 265; 264, 269; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:new_data; 268, False; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:append_data; 272, argument_list; 272, 273; 272, 274; 272, 275; 272, 276; 273, identifier:data; 274, identifier:key; 275, identifier:new_data; 276, string:'\\'; 277, comment; 278, for_statement; 278, 279; 278, 282; 278, 287; 279, tuple_pattern; 279, 280; 279, 281; 280, identifier:k; 281, identifier:v; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:data; 285, identifier:items; 286, argument_list; 287, block; 287, 288; 287, 303; 287, 304; 287, 314; 288, if_statement; 288, 289; 288, 295; 289, comparison_operator:==; 289, 290; 289, 294; 290, call; 290, 291; 290, 292; 291, identifier:len; 292, argument_list; 292, 293; 293, identifier:v; 294, integer:0; 295, block; 295, 296; 296, expression_statement; 296, 297; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:data; 300, identifier:pop; 301, argument_list; 301, 302; 302, identifier:k; 303, comment; 304, if_statement; 304, 305; 304, 312; 305, comparison_operator:in; 305, 306; 305, 307; 306, identifier:k; 307, list:["INPUT", "FILE_PATTERNS", "EXCLUDE_PATTERNS", "TAGFILES"]; 307, 308; 307, 309; 307, 310; 307, 311; 308, string:"INPUT"; 309, string:"FILE_PATTERNS"; 310, string:"EXCLUDE_PATTERNS"; 311, string:"TAGFILES"; 312, block; 312, 313; 313, continue_statement; 314, if_statement; 314, 315; 314, 321; 315, comparison_operator:==; 315, 316; 315, 320; 316, call; 316, 317; 316, 318; 317, identifier:len; 318, argument_list; 318, 319; 319, identifier:v; 320, integer:1; 321, block; 321, 322; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 327; 324, subscript; 324, 325; 324, 326; 325, identifier:data; 326, identifier:k; 327, subscript; 327, 328; 327, 329; 328, identifier:v; 329, integer:0; 330, return_statement; 330, 331; 331, identifier:data
def DoxyfileParse(file_contents): """ Parse a Doxygen source file and return a dictionary of all the values. Values will be strings and lists of strings. """ data = {} import shlex lex = shlex.shlex(instream = file_contents, posix = True) lex.wordchars += "*+./-:" lex.whitespace = lex.whitespace.replace("\n", "") lex.escape = "" lineno = lex.lineno token = lex.get_token() key = token # the first token should be a key last_token = "" key_token = False next_key = False new_data = True def append_data(data, key, new_data, token): if new_data or len(data[key]) == 0: data[key].append(token) else: data[key][-1] += token while token: if token in ['\n']: if last_token not in ['\\']: key_token = True elif token in ['\\']: pass elif key_token: key = token key_token = False else: if token == "+=": if not data.has_key(key): data[key] = list() elif token == "=": if key == "TAGFILES" and data.has_key(key): append_data( data, key, False, "=" ) new_data=False else: data[key] = list() else: append_data( data, key, new_data, token ) new_data = True last_token = token token = lex.get_token() if last_token == '\\' and token != '\n': new_data = False append_data( data, key, new_data, '\\' ) # compress lists of len 1 into single strings for (k, v) in data.items(): if len(v) == 0: data.pop(k) # items in the following list will be kept as lists and not converted to strings if k in ["INPUT", "FILE_PATTERNS", "EXCLUDE_PATTERNS", "TAGFILES"]: continue if len(v) == 1: data[k] = v[0] return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:DoxySourceScan; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:node; 5, identifier:env; 6, identifier:path; 7, block; 7, 8; 7, 10; 7, 39; 7, 44; 7, 48; 7, 59; 7, 80; 7, 90; 7, 100; 7, 101; 7, 102; 7, 103; 7, 104; 7, 118; 7, 288; 7, 289; 7, 340; 7, 341; 7, 401; 7, 406; 7, 411; 7, 416; 7, 432; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:default_file_patterns; 13, list:[ '*.c', '*.cc', '*.cxx', '*.cpp', '*.c++', '*.java', '*.ii', '*.ixx', '*.ipp', '*.i++', '*.inl', '*.h', '*.hh ', '*.hxx', '*.hpp', '*.h++', '*.idl', '*.odl', '*.cs', '*.php', '*.php3', '*.inc', '*.m', '*.mm', '*.py', ]; 13, 14; 13, 15; 13, 16; 13, 17; 13, 18; 13, 19; 13, 20; 13, 21; 13, 22; 13, 23; 13, 24; 13, 25; 13, 26; 13, 27; 13, 28; 13, 29; 13, 30; 13, 31; 13, 32; 13, 33; 13, 34; 13, 35; 13, 36; 13, 37; 13, 38; 14, string:'*.c'; 15, string:'*.cc'; 16, string:'*.cxx'; 17, string:'*.cpp'; 18, string:'*.c++'; 19, string:'*.java'; 20, string:'*.ii'; 21, string:'*.ixx'; 22, string:'*.ipp'; 23, string:'*.i++'; 24, string:'*.inl'; 25, string:'*.h'; 26, string:'*.hh '; 27, string:'*.hxx'; 28, string:'*.hpp'; 29, string:'*.h++'; 30, string:'*.idl'; 31, string:'*.odl'; 32, string:'*.cs'; 33, string:'*.php'; 34, string:'*.php3'; 35, string:'*.inc'; 36, string:'*.m'; 37, string:'*.mm'; 38, string:'*.py'; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:default_exclude_patterns; 42, list:[ '*~', ]; 42, 43; 43, string:'*~'; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:sources; 47, list:[]; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:data; 51, call; 51, 52; 51, 53; 52, identifier:DoxyfileParse; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:node; 57, identifier:get_contents; 58, argument_list; 59, if_statement; 59, 60; 59, 69; 59, 74; 60, comparison_operator:==; 60, 61; 60, 68; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:data; 64, identifier:get; 65, argument_list; 65, 66; 65, 67; 66, string:"RECURSIVE"; 67, string:"NO"; 68, string:"YES"; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:recursive; 73, True; 74, else_clause; 74, 75; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:recursive; 79, False; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:file_patterns; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:data; 86, identifier:get; 87, argument_list; 87, 88; 87, 89; 88, string:"FILE_PATTERNS"; 89, identifier:default_file_patterns; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:exclude_patterns; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:data; 96, identifier:get; 97, argument_list; 97, 98; 97, 99; 98, string:"EXCLUDE_PATTERNS"; 99, identifier:default_exclude_patterns; 100, comment; 101, comment; 102, comment; 103, comment; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:conf_dir; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:os; 111, identifier:path; 112, identifier:dirname; 113, argument_list; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:str; 116, argument_list; 116, 117; 117, identifier:node; 118, for_statement; 118, 119; 118, 120; 118, 127; 119, identifier:node; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:data; 123, identifier:get; 124, argument_list; 124, 125; 124, 126; 125, string:"INPUT"; 126, list:[]; 127, block; 127, 128; 127, 151; 128, if_statement; 128, 129; 128, 138; 129, not_operator; 129, 130; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:os; 134, identifier:path; 135, identifier:isabs; 136, argument_list; 136, 137; 137, identifier:node; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:node; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:os; 146, identifier:path; 147, identifier:join; 148, argument_list; 148, 149; 148, 150; 149, identifier:conf_dir; 150, identifier:node; 151, if_statement; 151, 152; 151, 160; 151, 168; 152, call; 152, 153; 152, 158; 153, attribute; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:os; 156, identifier:path; 157, identifier:isfile; 158, argument_list; 158, 159; 159, identifier:node; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:sources; 165, identifier:append; 166, argument_list; 166, 167; 167, identifier:node; 168, elif_clause; 168, 169; 168, 177; 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:isdir; 175, argument_list; 175, 176; 176, identifier:node; 177, block; 177, 178; 178, if_statement; 178, 179; 178, 180; 178, 263; 179, identifier:recursive; 180, block; 180, 181; 181, for_statement; 181, 182; 181, 186; 181, 192; 182, pattern_list; 182, 183; 182, 184; 182, 185; 183, identifier:root; 184, identifier:dirs; 185, identifier:files; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:os; 189, identifier:walk; 190, argument_list; 190, 191; 191, identifier:node; 192, block; 192, 193; 193, for_statement; 193, 194; 193, 195; 193, 196; 194, identifier:f; 195, identifier:files; 196, block; 196, 197; 196, 209; 196, 231; 196, 250; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:filename; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:os; 204, identifier:path; 205, identifier:join; 206, argument_list; 206, 207; 206, 208; 207, identifier:root; 208, identifier:f; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:pattern_check; 212, call; 212, 213; 212, 214; 213, identifier:reduce; 214, argument_list; 214, 215; 214, 229; 214, 230; 215, lambda; 215, 216; 215, 219; 216, lambda_parameters; 216, 217; 216, 218; 217, identifier:x; 218, identifier:y; 219, boolean_operator:or; 219, 220; 219, 221; 220, identifier:x; 221, call; 221, 222; 221, 223; 222, identifier:bool; 223, argument_list; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:fnmatch; 226, argument_list; 226, 227; 226, 228; 227, identifier:filename; 228, identifier:y; 229, identifier:file_patterns; 230, False; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:exclude_check; 234, call; 234, 235; 234, 236; 235, identifier:reduce; 236, argument_list; 236, 237; 236, 248; 236, 249; 237, lambda; 237, 238; 237, 241; 238, lambda_parameters; 238, 239; 238, 240; 239, identifier:x; 240, identifier:y; 241, boolean_operator:and; 241, 242; 241, 243; 242, identifier:x; 243, call; 243, 244; 243, 245; 244, identifier:fnmatch; 245, argument_list; 245, 246; 245, 247; 246, identifier:filename; 247, identifier:y; 248, identifier:exclude_patterns; 249, True; 250, if_statement; 250, 251; 250, 255; 251, boolean_operator:and; 251, 252; 251, 253; 252, identifier:pattern_check; 253, not_operator; 253, 254; 254, identifier:exclude_check; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:sources; 260, identifier:append; 261, argument_list; 261, 262; 262, identifier:filename; 263, else_clause; 263, 264; 264, block; 264, 265; 265, for_statement; 265, 266; 265, 267; 265, 268; 266, identifier:pattern; 267, identifier:file_patterns; 268, block; 268, 269; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:sources; 273, identifier:extend; 274, argument_list; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:glob; 278, identifier:glob; 279, argument_list; 279, 280; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, string:"/"; 283, identifier:join; 284, argument_list; 284, 285; 285, list:[node, pattern]; 285, 286; 285, 287; 286, identifier:node; 287, identifier:pattern; 288, comment; 289, for_statement; 289, 290; 289, 291; 289, 298; 290, identifier:node; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:data; 294, identifier:get; 295, argument_list; 295, 296; 295, 297; 296, string:"TAGFILES"; 297, list:[]; 298, block; 298, 299; 298, 310; 298, 333; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:file; 302, subscript; 302, 303; 302, 309; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:node; 306, identifier:split; 307, argument_list; 307, 308; 308, string:"="; 309, integer:0; 310, if_statement; 310, 311; 310, 320; 311, not_operator; 311, 312; 312, call; 312, 313; 312, 318; 313, attribute; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:os; 316, identifier:path; 317, identifier:isabs; 318, argument_list; 318, 319; 319, identifier:file; 320, block; 320, 321; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:file; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:os; 328, identifier:path; 329, identifier:join; 330, argument_list; 330, 331; 330, 332; 331, identifier:conf_dir; 332, identifier:file; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:sources; 337, identifier:append; 338, argument_list; 338, 339; 339, identifier:file; 340, comment; 341, function_definition; 341, 342; 341, 343; 341, 345; 342, function_name:append_additional_source; 343, parameters; 343, 344; 344, identifier:option; 345, block; 345, 346; 345, 356; 346, expression_statement; 346, 347; 347, assignment; 347, 348; 347, 349; 348, identifier:file; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:data; 352, identifier:get; 353, argument_list; 353, 354; 353, 355; 354, identifier:option; 355, string:""; 356, if_statement; 356, 357; 356, 360; 357, comparison_operator:!=; 357, 358; 357, 359; 358, identifier:file; 359, string:""; 360, block; 360, 361; 360, 384; 361, if_statement; 361, 362; 361, 371; 362, not_operator; 362, 363; 363, call; 363, 364; 363, 369; 364, attribute; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:os; 367, identifier:path; 368, identifier:isabs; 369, argument_list; 369, 370; 370, identifier:file; 371, block; 371, 372; 372, expression_statement; 372, 373; 373, assignment; 373, 374; 373, 375; 374, identifier:file; 375, call; 375, 376; 375, 381; 376, attribute; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:os; 379, identifier:path; 380, identifier:join; 381, argument_list; 381, 382; 381, 383; 382, identifier:conf_dir; 383, identifier:file; 384, if_statement; 384, 385; 384, 393; 385, call; 385, 386; 385, 391; 386, attribute; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:os; 389, identifier:path; 390, identifier:isfile; 391, argument_list; 391, 392; 392, identifier:file; 393, block; 393, 394; 394, expression_statement; 394, 395; 395, call; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:sources; 398, identifier:append; 399, argument_list; 399, 400; 400, identifier:file; 401, expression_statement; 401, 402; 402, call; 402, 403; 402, 404; 403, identifier:append_additional_source; 404, argument_list; 404, 405; 405, string:"HTML_STYLESHEET"; 406, expression_statement; 406, 407; 407, call; 407, 408; 407, 409; 408, identifier:append_additional_source; 409, argument_list; 409, 410; 410, string:"HTML_HEADER"; 411, expression_statement; 411, 412; 412, call; 412, 413; 412, 414; 413, identifier:append_additional_source; 414, argument_list; 414, 415; 415, string:"HTML_FOOTER"; 416, expression_statement; 416, 417; 417, assignment; 417, 418; 417, 419; 418, identifier:sources; 419, call; 419, 420; 419, 421; 420, identifier:map; 421, argument_list; 421, 422; 421, 431; 422, lambda; 422, 423; 422, 425; 423, lambda_parameters; 423, 424; 424, identifier:path; 425, call; 425, 426; 425, 429; 426, attribute; 426, 427; 426, 428; 427, identifier:env; 428, identifier:File; 429, argument_list; 429, 430; 430, identifier:path; 431, identifier:sources; 432, return_statement; 432, 433; 433, identifier:sources
def DoxySourceScan(node, env, path): """ Doxygen Doxyfile source scanner. This should scan the Doxygen file and add any files used to generate docs to the list of source files. """ default_file_patterns = [ '*.c', '*.cc', '*.cxx', '*.cpp', '*.c++', '*.java', '*.ii', '*.ixx', '*.ipp', '*.i++', '*.inl', '*.h', '*.hh ', '*.hxx', '*.hpp', '*.h++', '*.idl', '*.odl', '*.cs', '*.php', '*.php3', '*.inc', '*.m', '*.mm', '*.py', ] default_exclude_patterns = [ '*~', ] sources = [] data = DoxyfileParse(node.get_contents()) if data.get("RECURSIVE", "NO") == "YES": recursive = True else: recursive = False file_patterns = data.get("FILE_PATTERNS", default_file_patterns) exclude_patterns = data.get("EXCLUDE_PATTERNS", default_exclude_patterns) # We're running in the top-level directory, but the doxygen # configuration file is in the same directory as node; this means # that relative pathnames in node must be adjusted before they can # go onto the sources list conf_dir = os.path.dirname(str(node)) for node in data.get("INPUT", []): if not os.path.isabs(node): node = os.path.join(conf_dir, node) if os.path.isfile(node): sources.append(node) elif os.path.isdir(node): if recursive: for root, dirs, files in os.walk(node): for f in files: filename = os.path.join(root, f) pattern_check = reduce(lambda x, y: x or bool(fnmatch(filename, y)), file_patterns, False) exclude_check = reduce(lambda x, y: x and fnmatch(filename, y), exclude_patterns, True) if pattern_check and not exclude_check: sources.append(filename) else: for pattern in file_patterns: sources.extend(glob.glob("/".join([node, pattern]))) # Add tagfiles to the list of source files: for node in data.get("TAGFILES", []): file = node.split("=")[0] if not os.path.isabs(file): file = os.path.join(conf_dir, file) sources.append(file) # Add additional files to the list of source files: def append_additional_source(option): file = data.get(option, "") if file != "": if not os.path.isabs(file): file = os.path.join(conf_dir, file) if os.path.isfile(file): sources.append(file) append_additional_source("HTML_STYLESHEET") append_additional_source("HTML_HEADER") append_additional_source("HTML_FOOTER") sources = map( lambda path: env.File(path), sources ) return sources
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_quadratic_costs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:generators; 6, identifier:ipol; 7, identifier:nxyz; 8, identifier:base_mva; 9, block; 9, 10; 9, 12; 9, 19; 9, 26; 9, 40; 9, 63; 9, 91; 9, 119; 9, 128; 9, 321; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:npol; 15, call; 15, 16; 15, 17; 16, identifier:len; 17, argument_list; 17, 18; 18, identifier:ipol; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:rnpol; 22, call; 22, 23; 22, 24; 23, identifier:range; 24, argument_list; 24, 25; 25, identifier:npol; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:gpol; 29, list_comprehension; 29, 30; 29, 31; 29, 34; 30, identifier:g; 31, for_in_clause; 31, 32; 31, 33; 32, identifier:g; 33, identifier:generators; 34, if_clause; 34, 35; 35, comparison_operator:==; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:g; 38, identifier:pcost_model; 39, identifier:POLYNOMIAL; 40, if_statement; 40, 41; 40, 55; 41, list_comprehension; 41, 42; 41, 43; 41, 46; 42, identifier:g; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:g; 45, identifier:gpol; 46, if_clause; 46, 47; 47, comparison_operator:>; 47, 48; 47, 54; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:g; 53, identifier:p_cost; 54, integer:3; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:logger; 60, identifier:error; 61, argument_list; 61, 62; 62, string:"Order of polynomial cost greater than quadratic."; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:iqdr; 66, list_comprehension; 66, 67; 66, 68; 66, 76; 67, identifier:i; 68, for_in_clause; 68, 69; 68, 72; 69, pattern_list; 69, 70; 69, 71; 70, identifier:i; 71, identifier:g; 72, call; 72, 73; 72, 74; 73, identifier:enumerate; 74, argument_list; 74, 75; 75, identifier:generators; 76, if_clause; 76, 77; 77, boolean_operator:and; 77, 78; 77, 83; 78, comparison_operator:==; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:g; 81, identifier:pcost_model; 82, identifier:POLYNOMIAL; 83, comparison_operator:==; 83, 84; 83, 90; 84, call; 84, 85; 84, 86; 85, identifier:len; 86, argument_list; 86, 87; 87, attribute; 87, 88; 87, 89; 88, identifier:g; 89, identifier:p_cost; 90, integer:3; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:ilin; 94, list_comprehension; 94, 95; 94, 96; 94, 104; 95, identifier:i; 96, for_in_clause; 96, 97; 96, 100; 97, pattern_list; 97, 98; 97, 99; 98, identifier:i; 99, identifier:g; 100, call; 100, 101; 100, 102; 101, identifier:enumerate; 102, argument_list; 102, 103; 103, identifier:generators; 104, if_clause; 104, 105; 105, boolean_operator:and; 105, 106; 105, 111; 106, comparison_operator:==; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:g; 109, identifier:pcost_model; 110, identifier:POLYNOMIAL; 111, comparison_operator:==; 111, 112; 111, 118; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, attribute; 115, 116; 115, 117; 116, identifier:g; 117, identifier:p_cost; 118, integer:2; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:polycf; 122, call; 122, 123; 122, 124; 123, identifier:zeros; 124, argument_list; 124, 125; 125, tuple; 125, 126; 125, 127; 126, identifier:npol; 127, integer:3; 128, if_statement; 128, 129; 128, 132; 128, 297; 129, comparison_operator:>; 129, 130; 129, 131; 130, identifier:npol; 131, integer:0; 132, block; 132, 133; 132, 162; 132, 196; 132, 197; 132, 211; 132, 222; 132, 246; 132, 266; 132, 274; 133, if_statement; 133, 134; 133, 140; 134, comparison_operator:>; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:len; 137, argument_list; 137, 138; 138, identifier:iqdr; 139, integer:0; 140, block; 140, 141; 140, 161; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 148; 143, subscript; 143, 144; 143, 145; 143, 146; 144, identifier:polycf; 145, identifier:iqdr; 146, slice; 146, 147; 147, colon; 148, call; 148, 149; 148, 150; 149, identifier:array; 150, argument_list; 150, 151; 151, list_comprehension; 151, 152; 151, 158; 152, call; 152, 153; 152, 154; 153, identifier:list; 154, argument_list; 154, 155; 155, attribute; 155, 156; 155, 157; 156, identifier:g; 157, identifier:p_cost; 158, for_in_clause; 158, 159; 158, 160; 159, identifier:g; 160, identifier:generators; 161, comment; 162, if_statement; 162, 163; 162, 169; 163, comparison_operator:>; 163, 164; 163, 168; 164, call; 164, 165; 164, 166; 165, identifier:len; 166, argument_list; 166, 167; 167, identifier:ilin; 168, integer:0; 169, block; 169, 170; 169, 195; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 178; 172, subscript; 172, 173; 172, 174; 172, 175; 173, identifier:polycf; 174, identifier:ilin; 175, slice; 175, 176; 175, 177; 176, integer:1; 177, colon; 178, call; 178, 179; 178, 180; 179, identifier:array; 180, argument_list; 180, 181; 181, list_comprehension; 181, 182; 181, 192; 182, call; 182, 183; 182, 184; 183, identifier:list; 184, argument_list; 184, 185; 185, subscript; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:g; 188, identifier:p_cost; 189, slice; 189, 190; 189, 191; 190, colon; 191, integer:2; 192, for_in_clause; 192, 193; 192, 194; 193, identifier:g; 194, identifier:generators; 195, comment; 196, comment; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:polycf; 200, binary_operator:*; 200, 201; 200, 202; 201, identifier:polycf; 202, call; 202, 203; 202, 204; 203, identifier:array; 204, argument_list; 204, 205; 205, list:[base_mva**2, base_mva, 1]; 205, 206; 205, 209; 205, 210; 206, binary_operator:**; 206, 207; 206, 208; 207, identifier:base_mva; 208, integer:2; 209, identifier:base_mva; 210, integer:1; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:Pg; 214, call; 214, 215; 214, 220; 215, attribute; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:om; 219, identifier:get_var; 220, argument_list; 220, 221; 221, string:"Pg"; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:Npol; 225, call; 225, 226; 225, 227; 226, identifier:csr_matrix; 227, argument_list; 227, 228; 227, 243; 228, tuple; 228, 229; 228, 233; 229, call; 229, 230; 229, 231; 230, identifier:ones; 231, argument_list; 231, 232; 232, identifier:npol; 233, tuple; 233, 234; 233, 235; 234, identifier:rnpol; 235, binary_operator:+; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:Pg; 238, identifier:i1; 239, call; 239, 240; 239, 241; 240, identifier:array; 241, argument_list; 241, 242; 242, identifier:ipol; 243, tuple; 243, 244; 243, 245; 244, identifier:npol; 245, identifier:nxyz; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:Hpol; 249, call; 249, 250; 249, 251; 250, identifier:csr_matrix; 251, argument_list; 251, 252; 251, 263; 252, tuple; 252, 253; 252, 260; 253, binary_operator:*; 253, 254; 253, 255; 254, integer:2; 255, subscript; 255, 256; 255, 257; 255, 259; 256, identifier:polycf; 257, slice; 257, 258; 258, colon; 259, integer:0; 260, tuple; 260, 261; 260, 262; 261, identifier:rnpol; 262, identifier:rnpol; 263, tuple; 263, 264; 263, 265; 264, identifier:npol; 265, identifier:npol; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:Cpol; 269, subscript; 269, 270; 269, 271; 269, 273; 270, identifier:polycf; 271, slice; 271, 272; 272, colon; 273, integer:1; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:fparm_pol; 277, attribute; 277, 278; 277, 296; 278, parenthesized_expression; 278, 279; 279, binary_operator:*; 279, 280; 279, 284; 280, call; 280, 281; 280, 282; 281, identifier:ones; 282, argument_list; 282, 283; 283, identifier:npol; 284, call; 284, 285; 284, 286; 285, identifier:array; 286, argument_list; 286, 287; 287, list:[[1], [0], [0], [1]]; 287, 288; 287, 290; 287, 292; 287, 294; 288, list:[1]; 288, 289; 289, integer:1; 290, list:[0]; 290, 291; 291, integer:0; 292, list:[0]; 292, 293; 293, integer:0; 294, list:[1]; 294, 295; 295, integer:1; 296, identifier:T; 297, else_clause; 297, 298; 298, block; 298, 299; 298, 305; 298, 312; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:Npol; 302, assignment; 302, 303; 302, 304; 303, identifier:Hpol; 304, None; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:Cpol; 308, call; 308, 309; 308, 310; 309, identifier:array; 310, argument_list; 310, 311; 311, list:[]; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:fparm_pol; 315, call; 315, 316; 315, 317; 316, identifier:zeros; 317, argument_list; 317, 318; 318, tuple; 318, 319; 318, 320; 319, integer:0; 320, integer:4; 321, return_statement; 321, 322; 322, expression_list; 322, 323; 322, 324; 322, 325; 322, 326; 322, 327; 322, 328; 323, identifier:Npol; 324, identifier:Hpol; 325, identifier:Cpol; 326, identifier:fparm_pol; 327, identifier:polycf; 328, identifier:npol
def _quadratic_costs(self, generators, ipol, nxyz, base_mva): """ Returns the quadratic cost components of the objective function. """ npol = len(ipol) rnpol = range(npol) gpol = [g for g in generators if g.pcost_model == POLYNOMIAL] if [g for g in gpol if len(g.p_cost) > 3]: logger.error("Order of polynomial cost greater than quadratic.") iqdr = [i for i, g in enumerate(generators) if g.pcost_model == POLYNOMIAL and len(g.p_cost) == 3] ilin = [i for i, g in enumerate(generators) if g.pcost_model == POLYNOMIAL and len(g.p_cost) == 2] polycf = zeros((npol, 3)) if npol > 0: if len(iqdr) > 0: polycf[iqdr, :] = array([list(g.p_cost) for g in generators])#[iqdr, :].T if len(ilin) > 0: polycf[ilin, 1:] = array([list(g.p_cost[:2]) for g in generators])#[ilin, :].T # Convert to per-unit. polycf = polycf * array([base_mva**2, base_mva, 1]) Pg = self.om.get_var("Pg") Npol = csr_matrix((ones(npol), (rnpol, Pg.i1 + array(ipol))), (npol, nxyz)) Hpol = csr_matrix((2 * polycf[:, 0], (rnpol, rnpol)), (npol, npol)) Cpol = polycf[:, 1] fparm_pol = (ones(npol) * array([[1], [0], [0], [1]])).T else: Npol = Hpol = None Cpol = array([]) fparm_pol = zeros((0, 4)) return Npol, Hpol, Cpol, fparm_pol, polycf, npol
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_gh; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:x; 6, block; 6, 7; 6, 9; 6, 28; 6, 29; 6, 48; 6, 49; 6, 86; 6, 87; 6, 102; 6, 121; 6, 140; 6, 151; 6, 152; 6, 167; 6, 168; 6, 180; 6, 181; 6, 182; 6, 183; 6, 205; 6, 206; 6, 226; 6, 405; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:Pgen; 12, subscript; 12, 13; 12, 14; 13, identifier:x; 14, slice; 14, 15; 14, 20; 14, 21; 15, attribute; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_Pg; 19, identifier:i1; 20, colon; 21, binary_operator:+; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:_Pg; 26, identifier:iN; 27, integer:1; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:Qgen; 32, subscript; 32, 33; 32, 34; 33, identifier:x; 34, slice; 34, 35; 34, 40; 34, 41; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:_Qg; 39, identifier:i1; 40, colon; 41, binary_operator:+; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:_Qg; 46, identifier:iN; 47, integer:1; 48, comment; 49, for_statement; 49, 50; 49, 53; 49, 59; 50, pattern_list; 50, 51; 50, 52; 51, identifier:i; 52, identifier:gen; 53, call; 53, 54; 53, 55; 54, identifier:enumerate; 55, argument_list; 55, 56; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:_gn; 59, block; 59, 60; 59, 72; 59, 73; 59, 85; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:gen; 64, identifier:p; 65, binary_operator:*; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:Pgen; 68, identifier:i; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:_base_mva; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:gen; 77, identifier:q; 78, binary_operator:*; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:Qgen; 81, identifier:i; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_base_mva; 85, comment; 86, comment; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:Sbus; 90, call; 90, 91; 90, 98; 91, attribute; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:om; 96, identifier:case; 97, identifier:getSbus; 98, argument_list; 98, 99; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:_bs; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:Vang; 105, subscript; 105, 106; 105, 107; 106, identifier:x; 107, slice; 107, 108; 107, 113; 107, 114; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_Va; 112, identifier:i1; 113, colon; 114, binary_operator:+; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_Va; 119, identifier:iN; 120, integer:1; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:Vmag; 124, subscript; 124, 125; 124, 126; 125, identifier:x; 126, slice; 126, 127; 126, 132; 126, 133; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:_Vm; 131, identifier:i1; 132, colon; 133, binary_operator:+; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:_Vm; 138, identifier:iN; 139, integer:1; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:V; 143, binary_operator:*; 143, 144; 143, 145; 144, identifier:Vmag; 145, call; 145, 146; 145, 147; 146, identifier:exp; 147, argument_list; 147, 148; 148, binary_operator:*; 148, 149; 148, 150; 149, integer:1j; 150, identifier:Vang; 151, comment; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:mis; 155, binary_operator:-; 155, 156; 155, 166; 156, binary_operator:*; 156, 157; 156, 158; 157, identifier:V; 158, call; 158, 159; 158, 160; 159, identifier:conj; 160, argument_list; 160, 161; 161, binary_operator:*; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:_Ybus; 165, identifier:V; 166, identifier:Sbus; 167, comment; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:g; 171, subscript; 171, 172; 171, 173; 171, 176; 171, 177; 172, identifier:r_; 173, attribute; 173, 174; 173, 175; 174, identifier:mis; 175, identifier:real; 176, comment; 177, attribute; 177, 178; 177, 179; 178, identifier:mis; 179, identifier:imag; 180, comment; 181, comment; 182, comment; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:flow_max; 186, call; 186, 187; 186, 188; 187, identifier:array; 188, argument_list; 188, 189; 189, list_comprehension; 189, 190; 189, 200; 190, binary_operator:**; 190, 191; 190, 199; 191, parenthesized_expression; 191, 192; 192, binary_operator:/; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:l; 195, identifier:rate_a; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:_base_mva; 199, integer:2; 200, for_in_clause; 200, 201; 200, 202; 201, identifier:l; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:_ln; 205, comment; 206, for_statement; 206, 207; 206, 210; 206, 214; 207, pattern_list; 207, 208; 207, 209; 208, identifier:i; 209, identifier:v; 210, call; 210, 211; 210, 212; 211, identifier:enumerate; 212, argument_list; 212, 213; 213, identifier:flow_max; 214, block; 214, 215; 215, if_statement; 215, 216; 215, 219; 216, comparison_operator:==; 216, 217; 216, 218; 217, identifier:v; 218, float:0.0; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:flow_max; 224, identifier:i; 225, identifier:Inf; 226, if_statement; 226, 227; 226, 232; 226, 273; 227, comparison_operator:==; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:flow_lim; 231, identifier:IFLOW; 232, block; 232, 233; 232, 241; 232, 249; 232, 250; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:If; 236, binary_operator:*; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:self; 239, identifier:_Yf; 240, identifier:V; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:It; 244, binary_operator:*; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:self; 247, identifier:_Yt; 248, identifier:V; 249, comment; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:h; 253, subscript; 253, 254; 253, 255; 253, 264; 254, identifier:r_; 255, binary_operator:-; 255, 256; 255, 263; 256, parenthesized_expression; 256, 257; 257, binary_operator:*; 257, 258; 257, 259; 258, identifier:If; 259, call; 259, 260; 259, 261; 260, identifier:conj; 261, argument_list; 261, 262; 262, identifier:If; 263, identifier:flow_max; 264, binary_operator:-; 264, 265; 264, 272; 265, parenthesized_expression; 265, 266; 266, binary_operator:*; 266, 267; 266, 268; 267, identifier:It; 268, call; 268, 269; 268, 270; 269, identifier:conj; 270, argument_list; 270, 271; 271, identifier:It; 272, identifier:flow_max; 273, else_clause; 273, 274; 274, block; 274, 275; 274, 289; 274, 303; 274, 304; 274, 319; 274, 320; 274, 335; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:i_fbus; 278, list_comprehension; 278, 279; 278, 284; 279, attribute; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:e; 282, identifier:from_bus; 283, identifier:_i; 284, for_in_clause; 284, 285; 284, 286; 285, identifier:e; 286, attribute; 286, 287; 286, 288; 287, identifier:self; 288, identifier:_ln; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:i_tbus; 292, list_comprehension; 292, 293; 292, 298; 293, attribute; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:e; 296, identifier:to_bus; 297, identifier:_i; 298, for_in_clause; 298, 299; 298, 300; 299, identifier:e; 300, attribute; 300, 301; 300, 302; 301, identifier:self; 302, identifier:_ln; 303, comment; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:Sf; 307, binary_operator:*; 307, 308; 307, 311; 308, subscript; 308, 309; 308, 310; 309, identifier:V; 310, identifier:i_fbus; 311, call; 311, 312; 311, 313; 312, identifier:conj; 313, argument_list; 313, 314; 314, binary_operator:*; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:self; 317, identifier:_Yf; 318, identifier:V; 319, comment; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 323; 322, identifier:St; 323, binary_operator:*; 323, 324; 323, 327; 324, subscript; 324, 325; 324, 326; 325, identifier:V; 326, identifier:i_tbus; 327, call; 327, 328; 327, 329; 328, identifier:conj; 329, argument_list; 329, 330; 330, binary_operator:*; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:self; 333, identifier:_Yt; 334, identifier:V; 335, if_statement; 335, 336; 335, 341; 335, 342; 335, 343; 335, 367; 335, 401; 336, comparison_operator:==; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:self; 339, identifier:flow_lim; 340, identifier:PFLOW; 341, comment; 342, comment; 343, block; 343, 344; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 347; 346, identifier:h; 347, subscript; 347, 348; 347, 349; 347, 358; 348, identifier:r_; 349, binary_operator:-; 349, 350; 349, 357; 350, binary_operator:**; 350, 351; 350, 356; 351, call; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:Sf; 354, identifier:real; 355, argument_list; 356, integer:2; 357, identifier:flow_max; 358, binary_operator:-; 358, 359; 358, 366; 359, binary_operator:**; 359, 360; 359, 365; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:St; 363, identifier:real; 364, argument_list; 365, integer:2; 366, identifier:flow_max; 367, elif_clause; 367, 368; 367, 373; 367, 374; 367, 375; 368, comparison_operator:==; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:self; 371, identifier:flow_lim; 372, identifier:SFLOW; 373, comment; 374, comment; 375, block; 375, 376; 376, expression_statement; 376, 377; 377, assignment; 377, 378; 377, 379; 378, identifier:h; 379, attribute; 379, 380; 379, 400; 380, subscript; 380, 381; 380, 382; 380, 391; 381, identifier:r_; 382, binary_operator:-; 382, 383; 382, 390; 383, parenthesized_expression; 383, 384; 384, binary_operator:*; 384, 385; 384, 386; 385, identifier:Sf; 386, call; 386, 387; 386, 388; 387, identifier:conj; 388, argument_list; 388, 389; 389, identifier:Sf; 390, identifier:flow_max; 391, binary_operator:-; 391, 392; 391, 399; 392, parenthesized_expression; 392, 393; 393, binary_operator:*; 393, 394; 393, 395; 394, identifier:St; 395, call; 395, 396; 395, 397; 396, identifier:conj; 397, argument_list; 397, 398; 398, identifier:St; 399, identifier:flow_max; 400, identifier:real; 401, else_clause; 401, 402; 402, block; 402, 403; 403, raise_statement; 403, 404; 404, identifier:ValueError; 405, return_statement; 405, 406; 406, expression_list; 406, 407; 406, 408; 407, identifier:h; 408, identifier:g
def _gh(self, x): """ Evaluates the constraint function values. """ Pgen = x[self._Pg.i1:self._Pg.iN + 1] # Active generation in p.u. Qgen = x[self._Qg.i1:self._Qg.iN + 1] # Reactive generation in p.u. for i, gen in enumerate(self._gn): gen.p = Pgen[i] * self._base_mva # active generation in MW gen.q = Qgen[i] * self._base_mva # reactive generation in MVAr # Rebuild the net complex bus power injection vector in p.u. Sbus = self.om.case.getSbus(self._bs) Vang = x[self._Va.i1:self._Va.iN + 1] Vmag = x[self._Vm.i1:self._Vm.iN + 1] V = Vmag * exp(1j * Vang) # Evaluate the power flow equations. mis = V * conj(self._Ybus * V) - Sbus # Equality constraints (power flow). g = r_[mis.real, # active power mismatch for all buses mis.imag] # reactive power mismatch for all buses # Inequality constraints (branch flow limits). # (line constraint is actually on square of limit) flow_max = array([(l.rate_a / self._base_mva)**2 for l in self._ln]) # FIXME: There must be a more elegant method for this. for i, v in enumerate(flow_max): if v == 0.0: flow_max[i] = Inf if self.flow_lim == IFLOW: If = self._Yf * V It = self._Yt * V # Branch current limits. h = r_[(If * conj(If)) - flow_max, (It * conj(It)) - flow_max] else: i_fbus = [e.from_bus._i for e in self._ln] i_tbus = [e.to_bus._i for e in self._ln] # Complex power injected at "from" bus (p.u.). Sf = V[i_fbus] * conj(self._Yf * V) # Complex power injected at "to" bus (p.u.). St = V[i_tbus] * conj(self._Yt * V) if self.flow_lim == PFLOW: # active power limit, P (Pan Wei) # Branch real power limits. h = r_[Sf.real()**2 - flow_max, St.real()**2 - flow_max] elif self.flow_lim == SFLOW: # apparent power limit, |S| # Branch apparent power limits. h = r_[(Sf * conj(Sf)) - flow_max, (St * conj(St)) - flow_max].real else: raise ValueError return h, g
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:performAction; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:action; 6, block; 6, 7; 6, 9; 6, 29; 6, 39; 6, 51; 6, 52; 6, 69; 6, 70; 6, 84; 6, 85; 6, 86; 6, 108; 6, 109; 6, 170; 6, 176; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:gs; 12, list_comprehension; 12, 13; 12, 14; 12, 21; 13, identifier:g; 14, for_in_clause; 14, 15; 14, 16; 15, identifier:g; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:case; 20, identifier:online_generators; 21, if_clause; 21, 22; 22, comparison_operator:!=; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:g; 26, identifier:bus; 27, identifier:type; 28, identifier:REFERENCE; 29, assert_statement; 29, 30; 30, comparison_operator:==; 30, 31; 30, 35; 31, call; 31, 32; 31, 33; 32, identifier:len; 33, argument_list; 33, 34; 34, identifier:action; 35, call; 35, 36; 35, 37; 36, identifier:len; 37, argument_list; 37, 38; 38, identifier:gs; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:logger; 43, identifier:info; 44, argument_list; 44, 45; 45, binary_operator:%; 45, 46; 45, 47; 46, string:"Action: %s"; 47, call; 47, 48; 47, 49; 48, identifier:list; 49, argument_list; 49, 50; 50, identifier:action; 51, comment; 52, for_statement; 52, 53; 52, 56; 52, 60; 53, pattern_list; 53, 54; 53, 55; 54, identifier:i; 55, identifier:g; 56, call; 56, 57; 56, 58; 57, identifier:enumerate; 58, argument_list; 58, 59; 59, identifier:gs; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:g; 65, identifier:p; 66, subscript; 66, 67; 66, 68; 67, identifier:action; 68, identifier:i; 69, comment; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 83; 72, attribute; 72, 73; 72, 82; 73, call; 73, 74; 73, 75; 74, identifier:NewtonPF; 75, argument_list; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:case; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:verbose; 81, False; 82, identifier:solve; 83, argument_list; 84, comment; 85, comment; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 97; 88, subscript; 88, 89; 88, 92; 88, 94; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:_Pg; 92, slice; 92, 93; 93, colon; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:_step; 97, list_comprehension; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:g; 100, identifier:p; 101, for_in_clause; 101, 102; 101, 103; 102, identifier:g; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:case; 107, identifier:online_generators; 108, comment; 109, if_statement; 109, 110; 109, 122; 110, comparison_operator:!=; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:_step; 114, binary_operator:-; 114, 115; 114, 121; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:profile; 121, integer:1; 122, block; 122, 123; 122, 141; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:pq_buses; 126, list_comprehension; 126, 127; 126, 128; 126, 135; 127, identifier:b; 128, for_in_clause; 128, 129; 128, 130; 129, identifier:b; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:case; 134, identifier:buses; 135, if_clause; 135, 136; 136, comparison_operator:==; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:b; 139, identifier:type; 140, identifier:PQ; 141, for_statement; 141, 142; 141, 145; 141, 149; 142, pattern_list; 142, 143; 142, 144; 143, identifier:i; 144, identifier:b; 145, call; 145, 146; 145, 147; 146, identifier:enumerate; 147, argument_list; 147, 148; 148, identifier:pq_buses; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:b; 154, identifier:p_demand; 155, binary_operator:*; 155, 156; 155, 161; 156, subscript; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:_Pd0; 160, identifier:i; 161, subscript; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:profile; 165, binary_operator:+; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:_step; 169, integer:1; 170, expression_statement; 170, 171; 171, augmented_assignment:+=; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:_step; 175, integer:1; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:logger; 180, identifier:info; 181, argument_list; 181, 182; 182, binary_operator:%; 182, 183; 182, 184; 183, string:"Entering step %d."; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:_step
def performAction(self, action): """ Perform an action on the world that changes it's internal state. """ gs = [g for g in self.case.online_generators if g.bus.type !=REFERENCE] assert len(action) == len(gs) logger.info("Action: %s" % list(action)) # Set the output of each (non-reference) generator. for i, g in enumerate(gs): g.p = action[i] # Compute power flows and slack generator set-point. NewtonPF(self.case, verbose=False).solve() #FastDecoupledPF(self.case, verbose=False).solve() # Store all generator set-points (only used for plotting). self._Pg[:, self._step] = [g.p for g in self.case.online_generators] # Apply the next load profile value to the original demand at each bus. if self._step != len(self.profile) - 1: pq_buses = [b for b in self.case.buses if b.type == PQ] for i, b in enumerate(pq_buses): b.p_demand = self._Pd0[i] * self.profile[self._step + 1] self._step += 1 logger.info("Entering step %d." % self._step)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 1, 28; 2, function_name:blt; 3, parameters; 3, 4; 3, 12; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:f; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:List; 9, type_parameter; 9, 10; 10, type; 10, 11; 11, identifier:SYM; 12, typed_parameter; 12, 13; 12, 14; 13, identifier:x; 14, type; 14, 15; 15, generic_type; 15, 16; 15, 17; 16, identifier:List; 17, type_parameter; 17, 18; 18, type; 18, 19; 19, identifier:SYM; 20, type; 20, 21; 21, generic_type; 21, 22; 21, 23; 22, identifier:Dict; 23, type_parameter; 23, 24; 23, 26; 24, type; 24, 25; 25, identifier:str; 26, type; 26, 27; 27, identifier:Any; 28, block; 28, 29; 28, 31; 28, 41; 28, 60; 29, expression_statement; 29, 30; 30, comment; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:J; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:ca; 37, identifier:jacobian; 38, argument_list; 38, 39; 38, 40; 39, identifier:f; 40, identifier:x; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 51; 43, pattern_list; 43, 44; 43, 45; 43, 46; 43, 47; 43, 48; 43, 49; 43, 50; 44, identifier:nblock; 45, identifier:rowperm; 46, identifier:colperm; 47, identifier:rowblock; 48, identifier:colblock; 49, identifier:coarserow; 50, identifier:coarsecol; 51, call; 51, 52; 51, 59; 52, attribute; 52, 53; 52, 58; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:J; 56, identifier:sparsity; 57, argument_list; 58, identifier:btf; 59, argument_list; 60, return_statement; 60, 61; 61, dictionary; 61, 62; 61, 65; 61, 68; 61, 71; 61, 74; 61, 77; 61, 80; 61, 83; 62, pair; 62, 63; 62, 64; 63, string:'J'; 64, identifier:J; 65, pair; 65, 66; 65, 67; 66, string:'nblock'; 67, identifier:nblock; 68, pair; 68, 69; 68, 70; 69, string:'rowperm'; 70, identifier:rowperm; 71, pair; 71, 72; 71, 73; 72, string:'colperm'; 73, identifier:colperm; 74, pair; 74, 75; 74, 76; 75, string:'rowblock'; 76, identifier:rowblock; 77, pair; 77, 78; 77, 79; 78, string:'colblock'; 79, identifier:colblock; 80, pair; 80, 81; 80, 82; 81, string:'coarserow'; 82, identifier:coarserow; 83, pair; 83, 84; 83, 85; 84, string:'coarsecol'; 85, identifier:coarsecol
def blt(f: List[SYM], x: List[SYM]) -> Dict[str, Any]: """ Sort equations by dependence """ J = ca.jacobian(f, x) nblock, rowperm, colperm, rowblock, colblock, coarserow, coarsecol = J.sparsity().btf() return { 'J': J, 'nblock': nblock, 'rowperm': rowperm, 'colperm': colperm, 'rowblock': rowblock, 'colblock': colblock, 'coarserow': coarserow, 'coarsecol': coarsecol }
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_generators; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 15; 10, attribute; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:generators; 14, identifier:sort; 15, argument_list; 15, 16; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:key; 18, lambda; 18, 19; 18, 21; 19, lambda_parameters; 19, 20; 20, identifier:gn; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:gn; 24, identifier:bus; 25, identifier:_i
def sort_generators(self): """ Reorders the list of generators according to bus index. """ self.generators.sort(key=lambda gn: gn.bus._i)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:create; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:dotdata; 6, default_parameter; 6, 7; 6, 8; 7, identifier:prog; 8, string:"dot"; 9, default_parameter; 9, 10; 9, 11; 10, identifier:format; 11, string:"xdot"; 12, block; 12, 13; 12, 15; 12, 20; 12, 26; 12, 27; 12, 33; 12, 47; 12, 74; 12, 75; 12, 85; 12, 92; 12, 93; 12, 101; 12, 108; 12, 109; 12, 115; 12, 116; 12, 127; 12, 128; 12, 157; 12, 163; 12, 169; 12, 170; 12, 176; 12, 199; 12, 205; 12, 217; 12, 218; 12, 271; 12, 279; 12, 309; 12, 310; 12, 317; 13, expression_statement; 13, 14; 14, comment; 15, import_statement; 15, 16; 15, 18; 16, dotted_name; 16, 17; 17, identifier:os; 18, dotted_name; 18, 19; 19, identifier:tempfile; 20, import_from_statement; 20, 21; 20, 24; 21, dotted_name; 21, 22; 21, 23; 22, identifier:dot2tex; 23, identifier:dotparsing; 24, dotted_name; 24, 25; 25, identifier:find_graphviz; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:progs; 30, call; 30, 31; 30, 32; 31, identifier:find_graphviz; 32, argument_list; 33, if_statement; 33, 34; 33, 37; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:progs; 36, None; 37, block; 37, 38; 37, 45; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:logger; 42, identifier:warning; 43, argument_list; 43, 44; 44, string:"GraphViz executables not found."; 45, return_statement; 45, 46; 46, None; 47, if_statement; 47, 48; 47, 55; 48, not_operator; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:progs; 52, identifier:has_key; 53, argument_list; 53, 54; 54, identifier:prog; 55, block; 55, 56; 55, 72; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:logger; 60, identifier:warning; 61, argument_list; 61, 62; 62, binary_operator:%; 62, 63; 62, 64; 62, 65; 63, string:'Invalid program [%s]. Available programs are: %s'; 64, line_continuation:\; 65, tuple; 65, 66; 65, 67; 66, identifier:prog; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:progs; 70, identifier:keys; 71, argument_list; 72, return_statement; 72, 73; 73, None; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 80; 77, pattern_list; 77, 78; 77, 79; 78, identifier:tmp_fd; 79, identifier:tmp_name; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:tempfile; 83, identifier:mkstemp; 84, argument_list; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:os; 89, identifier:close; 90, argument_list; 90, 91; 91, identifier:tmp_fd; 92, comment; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:dot_fd; 96, call; 96, 97; 96, 98; 97, identifier:file; 98, argument_list; 98, 99; 98, 100; 99, identifier:tmp_name; 100, string:"w+b"; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:dot_fd; 105, identifier:write; 106, argument_list; 106, 107; 107, identifier:dotdata; 108, comment; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:dot_fd; 113, identifier:close; 114, argument_list; 115, comment; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:tmp_dir; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:os; 123, identifier:path; 124, identifier:dirname; 125, argument_list; 125, 126; 126, identifier:tmp_name; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:p; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:subprocess; 134, identifier:Popen; 135, argument_list; 135, 136; 135, 144; 135, 147; 135, 152; 136, tuple; 136, 137; 136, 140; 136, 143; 137, subscript; 137, 138; 137, 139; 138, identifier:progs; 139, identifier:prog; 140, binary_operator:+; 140, 141; 140, 142; 141, string:'-T'; 142, identifier:format; 143, identifier:tmp_name; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:cwd; 146, identifier:tmp_dir; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:stderr; 149, attribute; 149, 150; 149, 151; 150, identifier:subprocess; 151, identifier:PIPE; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:stdout; 154, attribute; 154, 155; 154, 156; 155, identifier:subprocess; 156, identifier:PIPE; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:stderr; 160, attribute; 160, 161; 160, 162; 161, identifier:p; 162, identifier:stderr; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:stdout; 166, attribute; 166, 167; 166, 168; 167, identifier:p; 168, identifier:stdout; 169, comment; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:stdout_output; 173, call; 173, 174; 173, 175; 174, identifier:list; 175, argument_list; 176, while_statement; 176, 177; 176, 178; 177, True; 178, block; 178, 179; 178, 187; 178, 192; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:data; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:stdout; 185, identifier:read; 186, argument_list; 187, if_statement; 187, 188; 187, 190; 188, not_operator; 188, 189; 189, identifier:data; 190, block; 190, 191; 191, break_statement; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:stdout_output; 196, identifier:append; 197, argument_list; 197, 198; 198, identifier:data; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:stdout; 203, identifier:close; 204, argument_list; 205, if_statement; 205, 206; 205, 207; 206, identifier:stdout_output; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:stdout_output; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, string:''; 214, identifier:join; 215, argument_list; 215, 216; 216, identifier:stdout_output; 217, comment; 218, if_statement; 218, 219; 218, 223; 219, not_operator; 219, 220; 220, attribute; 220, 221; 220, 222; 221, identifier:stderr; 222, identifier:closed; 223, block; 223, 224; 223, 230; 223, 253; 223, 259; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:stderr_output; 227, call; 227, 228; 227, 229; 228, identifier:list; 229, argument_list; 230, while_statement; 230, 231; 230, 232; 231, True; 232, block; 232, 233; 232, 241; 232, 246; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:data; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:stderr; 239, identifier:read; 240, argument_list; 241, if_statement; 241, 242; 241, 244; 242, not_operator; 242, 243; 243, identifier:data; 244, block; 244, 245; 245, break_statement; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:stderr_output; 250, identifier:append; 251, argument_list; 251, 252; 252, identifier:data; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:stderr; 257, identifier:close; 258, argument_list; 259, if_statement; 259, 260; 259, 261; 260, identifier:stderr_output; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:stderr_output; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, string:''; 268, identifier:join; 269, argument_list; 269, 270; 270, identifier:stderr_output; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:status; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:p; 277, identifier:wait; 278, argument_list; 279, if_statement; 279, 280; 279, 283; 279, 298; 280, comparison_operator:!=; 280, 281; 280, 282; 281, identifier:status; 282, integer:0; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:logger; 288, identifier:error; 289, argument_list; 289, 290; 290, binary_operator:%; 290, 291; 290, 294; 291, concatenated_string; 291, 292; 291, 293; 292, string:"Program [%s] terminated with status: %d. stderr "; 293, string:"follows: %s"; 294, tuple; 294, 295; 294, 296; 294, 297; 295, identifier:prog; 296, identifier:status; 297, identifier:stderr_output; 298, elif_clause; 298, 299; 298, 300; 299, identifier:stderr_output; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:logger; 305, identifier:error; 306, argument_list; 306, 307; 306, 308; 307, string:"%s"; 308, identifier:stderr_output; 309, comment; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:os; 314, identifier:unlink; 315, argument_list; 315, 316; 316, identifier:tmp_name; 317, return_statement; 317, 318; 318, identifier:stdout_output
def create(self, dotdata, prog="dot", format="xdot"): """ Creates and returns a representation of the graph using the Graphviz layout program given by 'prog', according to the given format. Writes the graph to a temporary dot file and processes it with the program given by 'prog' (which defaults to 'dot'), reading the output and returning it as a string if the operation is successful. On failure None is returned. Based on PyDot by Ero Carrera. """ import os, tempfile from dot2tex.dotparsing import find_graphviz # Map Graphviz executable names to their paths. progs = find_graphviz() if progs is None: logger.warning("GraphViz executables not found.") return None if not progs.has_key(prog): logger.warning('Invalid program [%s]. Available programs are: %s' % \ (prog, progs.keys())) return None # Make a temporary file ... tmp_fd, tmp_name = tempfile.mkstemp() os.close(tmp_fd) # ... and save the graph to it. dot_fd = file(tmp_name, "w+b") dot_fd.write(dotdata) # DOT language. dot_fd.close() # Get the temporary file directory name. tmp_dir = os.path.dirname(tmp_name) # Process the file using the layout program, specifying the format. p = subprocess.Popen((progs[prog], '-T'+format, tmp_name), cwd=tmp_dir, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stderr = p.stderr stdout = p.stdout # Make sense of the standard output form the process. stdout_output = list() while True: data = stdout.read() if not data: break stdout_output.append(data) stdout.close() if stdout_output: stdout_output = ''.join(stdout_output) # Similarly so for any standard error. if not stderr.closed: stderr_output = list() while True: data = stderr.read() if not data: break stderr_output.append(data) stderr.close() if stderr_output: stderr_output = ''.join(stderr_output) status = p.wait() if status != 0 : logger.error("Program [%s] terminated with status: %d. stderr " \ "follows: %s" % ( prog, status, stderr_output ) ) elif stderr_output: logger.error( "%s", stderr_output ) # Remove the temporary file. os.unlink(tmp_name) return stdout_output
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:format; 3, parameters; 3, 4; 3, 5; 4, identifier:file_metrics; 5, identifier:build_metrics; 6, block; 6, 7; 6, 9; 6, 134; 6, 143; 6, 144; 6, 153; 6, 160; 6, 229; 6, 230; 6, 250; 6, 255; 6, 281; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 16; 10, function_name:indent; 11, parameters; 11, 12; 11, 13; 12, identifier:elem; 13, default_parameter; 13, 14; 13, 15; 14, identifier:level; 15, integer:0; 16, block; 16, 17; 16, 25; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:i; 20, binary_operator:+; 20, 21; 20, 22; 21, string:"\n"; 22, binary_operator:*; 22, 23; 22, 24; 23, identifier:level; 24, string:" "; 25, if_statement; 25, 26; 25, 30; 25, 108; 26, call; 26, 27; 26, 28; 27, identifier:len; 28, argument_list; 28, 29; 29, identifier:elem; 30, block; 30, 31; 30, 54; 30, 75; 30, 87; 31, if_statement; 31, 32; 31, 45; 32, boolean_operator:or; 32, 33; 32, 37; 33, not_operator; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:elem; 36, identifier:text; 37, not_operator; 37, 38; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:elem; 42, identifier:text; 43, identifier:strip; 44, argument_list; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:elem; 50, identifier:text; 51, binary_operator:+; 51, 52; 51, 53; 52, identifier:i; 53, string:" "; 54, if_statement; 54, 55; 54, 68; 55, boolean_operator:or; 55, 56; 55, 60; 56, not_operator; 56, 57; 57, attribute; 57, 58; 57, 59; 58, identifier:elem; 59, identifier:tail; 60, not_operator; 60, 61; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:elem; 65, identifier:tail; 66, identifier:strip; 67, argument_list; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:elem; 73, identifier:tail; 74, identifier:i; 75, for_statement; 75, 76; 75, 77; 75, 78; 76, identifier:elem; 77, identifier:elem; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:indent; 82, argument_list; 82, 83; 82, 84; 83, identifier:elem; 84, binary_operator:+; 84, 85; 84, 86; 85, identifier:level; 86, integer:1; 87, if_statement; 87, 88; 87, 101; 88, boolean_operator:or; 88, 89; 88, 93; 89, not_operator; 89, 90; 90, attribute; 90, 91; 90, 92; 91, identifier:elem; 92, identifier:tail; 93, not_operator; 93, 94; 94, call; 94, 95; 94, 100; 95, attribute; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:elem; 98, identifier:tail; 99, identifier:strip; 100, argument_list; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:elem; 106, identifier:tail; 107, identifier:i; 108, else_clause; 108, 109; 109, block; 109, 110; 110, if_statement; 110, 111; 110, 127; 111, boolean_operator:and; 111, 112; 111, 113; 112, identifier:level; 113, parenthesized_expression; 113, 114; 114, boolean_operator:or; 114, 115; 114, 119; 115, not_operator; 115, 116; 116, attribute; 116, 117; 116, 118; 117, identifier:elem; 118, identifier:tail; 119, not_operator; 119, 120; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:elem; 124, identifier:tail; 125, identifier:strip; 126, argument_list; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:elem; 132, identifier:tail; 133, identifier:i; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:root; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:ET; 140, identifier:Element; 141, argument_list; 141, 142; 142, string:'metrics'; 143, comment; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:files; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:ET; 150, identifier:Element; 151, argument_list; 151, 152; 152, string:'files'; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:root; 157, identifier:append; 158, argument_list; 158, 159; 159, identifier:files; 160, for_statement; 160, 161; 160, 162; 160, 167; 161, identifier:key; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:file_metrics; 165, identifier:keys; 166, argument_list; 167, block; 167, 168; 167, 189; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:tmp_file; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:ET; 174, identifier:SubElement; 175, argument_list; 175, 176; 175, 177; 175, 178; 176, identifier:files; 177, string:"file"; 178, dictionary; 178, 179; 178, 182; 179, pair; 179, 180; 179, 181; 180, string:'name'; 181, identifier:key; 182, pair; 182, 183; 182, 184; 183, string:'language'; 184, subscript; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:file_metrics; 187, identifier:key; 188, string:'language'; 189, for_statement; 189, 190; 189, 191; 189, 198; 190, identifier:name; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:file_metrics; 195, identifier:key; 196, identifier:keys; 197, argument_list; 198, block; 198, 199; 198, 205; 199, if_statement; 199, 200; 199, 203; 200, comparison_operator:==; 200, 201; 200, 202; 201, identifier:name; 202, string:'language'; 203, block; 203, 204; 204, continue_statement; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:tmp_metric; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:ET; 211, identifier:SubElement; 212, argument_list; 212, 213; 212, 214; 212, 215; 213, identifier:tmp_file; 214, string:"metric"; 215, dictionary; 215, 216; 215, 219; 216, pair; 216, 217; 216, 218; 217, string:'name'; 218, identifier:name; 219, pair; 219, 220; 219, 221; 220, string:'value'; 221, call; 221, 222; 221, 223; 222, identifier:str; 223, argument_list; 223, 224; 224, subscript; 224, 225; 224, 228; 225, subscript; 225, 226; 225, 227; 226, identifier:file_metrics; 227, identifier:key; 228, identifier:name; 229, comment; 230, if_statement; 230, 231; 230, 232; 231, identifier:build_metrics; 232, block; 232, 233; 232, 242; 232, 249; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:build; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:ET; 239, identifier:Element; 240, argument_list; 240, 241; 241, string:'build'; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:root; 246, identifier:append; 247, argument_list; 247, 248; 248, identifier:build; 249, comment; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:indent; 253, argument_list; 253, 254; 254, identifier:root; 255, if_statement; 255, 256; 255, 257; 255, 270; 256, identifier:PY3; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:body; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:ET; 264, identifier:tostring; 265, argument_list; 265, 266; 265, 267; 266, identifier:root; 267, keyword_argument; 267, 268; 267, 269; 268, identifier:encoding; 269, string:'unicode'; 270, else_clause; 270, 271; 271, block; 271, 272; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:body; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:ET; 278, identifier:tostring; 279, argument_list; 279, 280; 280, identifier:root; 281, return_statement; 281, 282; 282, identifier:body
def format(file_metrics, build_metrics): """compute output in XML format.""" def indent(elem, level=0): i = "\n" + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i root = ET.Element('metrics') # file_metrics files = ET.Element('files') root.append(files) for key in file_metrics.keys(): tmp_file = ET.SubElement(files, "file", {'name': key, 'language': file_metrics[key]['language']}) for name in file_metrics[key].keys(): if name == 'language': continue tmp_metric = ET.SubElement(tmp_file, "metric", {'name': name, 'value': str(file_metrics[key][name])}) # build_metrics if build_metrics: build = ET.Element('build') root.append(build) # TODO indent(root) if PY3: body = ET.tostring(root, encoding='unicode') else: body = ET.tostring(root) return body
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:governor; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:Xgov; 6, identifier:Pgov; 7, identifier:Vgov; 8, block; 8, 9; 8, 11; 8, 17; 8, 27; 8, 36; 8, 54; 8, 72; 8, 73; 8, 80; 8, 81; 8, 88; 8, 95; 8, 102; 8, 109; 8, 116; 8, 123; 8, 130; 8, 137; 8, 144; 8, 151; 8, 158; 8, 165; 8, 172; 8, 179; 8, 203; 8, 220; 8, 233; 8, 237; 8, 273; 8, 309; 8, 313; 8, 317; 8, 353; 8, 389; 8, 402; 8, 403; 8, 404; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:governors; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:governors; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:omegas; 20, binary_operator:*; 20, 21; 20, 24; 21, binary_operator:*; 21, 22; 21, 23; 22, integer:2; 23, identifier:pi; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:freq; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:F; 30, call; 30, 31; 30, 32; 31, identifier:zeros; 32, argument_list; 32, 33; 33, attribute; 33, 34; 33, 35; 34, identifier:Xgov; 35, identifier:shape; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:typ1; 39, list_comprehension; 39, 40; 39, 45; 39, 48; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:g; 43, identifier:generator; 44, identifier:_i; 45, for_in_clause; 45, 46; 45, 47; 46, identifier:g; 47, identifier:governors; 48, if_clause; 48, 49; 49, comparison_operator:==; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:g; 52, identifier:model; 53, identifier:CONST_POWER; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:typ2; 57, list_comprehension; 57, 58; 57, 63; 57, 66; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:g; 61, identifier:generator; 62, identifier:_i; 63, for_in_clause; 63, 64; 63, 65; 64, identifier:g; 65, identifier:governors; 66, if_clause; 66, 67; 67, comparison_operator:==; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:g; 70, identifier:model; 71, identifier:GENERAL_IEEE; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 79; 75, subscript; 75, 76; 75, 77; 75, 78; 76, identifier:F; 77, identifier:typ1; 78, integer:0; 79, integer:0; 80, comment; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:Pm; 84, subscript; 84, 85; 84, 86; 84, 87; 85, identifier:Xgov; 86, identifier:typ2; 87, integer:0; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:P; 91, subscript; 91, 92; 91, 93; 91, 94; 92, identifier:Xgov; 93, identifier:typ2; 94, integer:1; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:x; 98, subscript; 98, 99; 98, 100; 98, 101; 99, identifier:Xgov; 100, identifier:typ2; 101, integer:2; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:z; 105, subscript; 105, 106; 105, 107; 105, 108; 106, identifier:Xgov; 107, identifier:typ2; 108, integer:3; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:K; 112, subscript; 112, 113; 112, 114; 112, 115; 113, identifier:Pgov; 114, identifier:typ2; 115, integer:0; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:T1; 119, subscript; 119, 120; 119, 121; 119, 122; 120, identifier:Pgov; 121, identifier:typ2; 122, integer:1; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:T2; 126, subscript; 126, 127; 126, 128; 126, 129; 127, identifier:Pgov; 128, identifier:typ2; 129, integer:2; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:T3; 133, subscript; 133, 134; 133, 135; 133, 136; 134, identifier:Pgov; 135, identifier:typ2; 136, integer:3; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:Pup; 140, subscript; 140, 141; 140, 142; 140, 143; 141, identifier:Pgov; 142, identifier:typ2; 143, integer:4; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:Pdown; 147, subscript; 147, 148; 147, 149; 147, 150; 148, identifier:Pgov; 149, identifier:typ2; 150, integer:5; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:Pmax; 154, subscript; 154, 155; 154, 156; 154, 157; 155, identifier:Pgov; 156, identifier:typ2; 157, integer:6; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:Pmin; 161, subscript; 161, 162; 161, 163; 161, 164; 162, identifier:Pgov; 163, identifier:typ2; 164, integer:7; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:P0; 168, subscript; 168, 169; 168, 170; 168, 171; 169, identifier:Pgov; 170, identifier:typ2; 171, integer:8; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:omega; 175, subscript; 175, 176; 175, 177; 175, 178; 176, identifier:Vgov; 177, identifier:typ2; 178, integer:0; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:dx; 182, binary_operator:*; 182, 183; 182, 184; 183, identifier:K; 184, parenthesized_expression; 184, 185; 185, binary_operator:+; 185, 186; 185, 192; 186, binary_operator:*; 186, 187; 186, 191; 187, binary_operator:/; 187, 188; 187, 190; 188, unary_operator:-; 188, 189; 189, integer:1; 190, identifier:T1; 191, identifier:x; 192, binary_operator:*; 192, 193; 192, 199; 193, parenthesized_expression; 193, 194; 194, binary_operator:-; 194, 195; 194, 196; 195, integer:1; 196, binary_operator:/; 196, 197; 196, 198; 197, identifier:T2; 198, identifier:T1; 199, parenthesized_expression; 199, 200; 200, binary_operator:-; 200, 201; 200, 202; 201, identifier:omega; 202, identifier:omegas; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:dP; 206, binary_operator:+; 206, 207; 206, 212; 207, binary_operator:*; 207, 208; 207, 211; 208, binary_operator:/; 208, 209; 208, 210; 209, integer:1; 210, identifier:T1; 211, identifier:x; 212, binary_operator:*; 212, 213; 212, 216; 213, binary_operator:/; 213, 214; 213, 215; 214, identifier:T2; 215, identifier:T1; 216, parenthesized_expression; 216, 217; 217, binary_operator:-; 217, 218; 217, 219; 218, identifier:omega; 219, identifier:omegas; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:y; 223, binary_operator:*; 223, 224; 223, 227; 224, binary_operator:/; 224, 225; 224, 226; 225, integer:1; 226, identifier:T3; 227, parenthesized_expression; 227, 228; 228, binary_operator:-; 228, 229; 228, 232; 229, binary_operator:-; 229, 230; 229, 231; 230, identifier:P0; 231, identifier:P; 232, identifier:Pm; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:y2; 236, identifier:y; 237, if_statement; 237, 238; 237, 249; 238, comparison_operator:>=; 238, 239; 238, 248; 239, call; 239, 240; 239, 241; 240, identifier:sum; 241, argument_list; 241, 242; 242, call; 242, 243; 242, 244; 243, identifier:flatnonzero; 244, argument_list; 244, 245; 245, comparison_operator:>; 245, 246; 245, 247; 246, identifier:y; 247, identifier:Pup; 248, integer:1; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:y2; 253, binary_operator:+; 253, 254; 253, 265; 254, binary_operator:*; 254, 255; 254, 264; 255, parenthesized_expression; 255, 256; 256, binary_operator:-; 256, 257; 256, 258; 257, integer:1; 258, call; 258, 259; 258, 260; 259, identifier:flatnonzero; 260, argument_list; 260, 261; 261, comparison_operator:>; 261, 262; 261, 263; 262, identifier:y; 263, identifier:Pup; 264, identifier:y2; 265, binary_operator:*; 265, 266; 265, 272; 266, call; 266, 267; 266, 268; 267, identifier:flatnonzero; 268, argument_list; 268, 269; 269, comparison_operator:>; 269, 270; 269, 271; 270, identifier:y; 271, identifier:Pup; 272, identifier:Pup; 273, if_statement; 273, 274; 273, 285; 274, comparison_operator:>=; 274, 275; 274, 284; 275, call; 275, 276; 275, 277; 276, identifier:sum; 277, argument_list; 277, 278; 278, call; 278, 279; 278, 280; 279, identifier:flatnonzero; 280, argument_list; 280, 281; 281, comparison_operator:<; 281, 282; 281, 283; 282, identifier:y; 283, identifier:Pdown; 284, integer:1; 285, block; 285, 286; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:y2; 289, binary_operator:+; 289, 290; 289, 301; 290, binary_operator:*; 290, 291; 290, 300; 291, parenthesized_expression; 291, 292; 292, binary_operator:-; 292, 293; 292, 294; 293, integer:1; 294, call; 294, 295; 294, 296; 295, identifier:flatnonzero; 296, argument_list; 296, 297; 297, comparison_operator:<; 297, 298; 297, 299; 298, identifier:y; 299, identifier:Pdown; 300, identifier:y2; 301, binary_operator:*; 301, 302; 301, 308; 302, call; 302, 303; 302, 304; 303, identifier:flatnonzero; 304, argument_list; 304, 305; 305, comparison_operator:<; 305, 306; 305, 307; 306, identifier:y; 307, identifier:Pdown; 308, identifier:Pdown; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:dz; 312, identifier:y2; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:dPm; 316, identifier:y2; 317, if_statement; 317, 318; 317, 329; 318, comparison_operator:>=; 318, 319; 318, 328; 319, call; 319, 320; 319, 321; 320, identifier:sum; 321, argument_list; 321, 322; 322, call; 322, 323; 322, 324; 323, identifier:flatnonzero; 324, argument_list; 324, 325; 325, comparison_operator:>; 325, 326; 325, 327; 326, identifier:z; 327, identifier:Pmax; 328, integer:1; 329, block; 329, 330; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:dPm; 333, binary_operator:+; 333, 334; 333, 345; 334, binary_operator:*; 334, 335; 334, 344; 335, parenthesized_expression; 335, 336; 336, binary_operator:-; 336, 337; 336, 338; 337, integer:1; 338, call; 338, 339; 338, 340; 339, identifier:flatnonzero; 340, argument_list; 340, 341; 341, comparison_operator:>; 341, 342; 341, 343; 342, identifier:z; 343, identifier:Pmax; 344, identifier:dPm; 345, binary_operator:*; 345, 346; 345, 352; 346, call; 346, 347; 346, 348; 347, identifier:flatnonzero; 348, argument_list; 348, 349; 349, comparison_operator:>; 349, 350; 349, 351; 350, identifier:z; 351, identifier:Pmax; 352, integer:0; 353, if_statement; 353, 354; 353, 365; 354, comparison_operator:>=; 354, 355; 354, 364; 355, call; 355, 356; 355, 357; 356, identifier:sum; 357, argument_list; 357, 358; 358, call; 358, 359; 358, 360; 359, identifier:flatnonzero; 360, argument_list; 360, 361; 361, comparison_operator:<; 361, 362; 361, 363; 362, identifier:z; 363, identifier:Pmin; 364, integer:1; 365, block; 365, 366; 366, expression_statement; 366, 367; 367, assignment; 367, 368; 367, 369; 368, identifier:dPm; 369, binary_operator:+; 369, 370; 369, 381; 370, binary_operator:*; 370, 371; 370, 380; 371, parenthesized_expression; 371, 372; 372, binary_operator:-; 372, 373; 372, 374; 373, integer:1; 374, call; 374, 375; 374, 376; 375, identifier:flatnonzero; 376, argument_list; 376, 377; 377, comparison_operator:<; 377, 378; 377, 379; 378, identifier:z; 379, identifier:Pmin; 380, identifier:dPm; 381, binary_operator:*; 381, 382; 381, 388; 382, call; 382, 383; 382, 384; 383, identifier:flatnonzero; 384, argument_list; 384, 385; 385, comparison_operator:<; 385, 386; 385, 387; 386, identifier:z; 387, identifier:Pmin; 388, integer:0; 389, expression_statement; 389, 390; 390, assignment; 390, 391; 390, 396; 391, subscript; 391, 392; 391, 393; 391, 394; 392, identifier:F; 393, identifier:typ2; 394, slice; 394, 395; 395, colon; 396, subscript; 396, 397; 396, 398; 396, 399; 396, 400; 396, 401; 397, identifier:c_; 398, identifier:dPm; 399, identifier:dP; 400, identifier:dx; 401, identifier:dz; 402, comment; 403, comment; 404, return_statement; 404, 405; 405, identifier:F
def governor(self, Xgov, Pgov, Vgov): """ Governor model. Based on Governor.m from MatDyn by Stijn Cole, developed at Katholieke Universiteit Leuven. See U{http://www.esat.kuleuven.be/electa/teaching/ matdyn/} for more information. """ governors = self.governors omegas = 2 * pi * self.freq F = zeros(Xgov.shape) typ1 = [g.generator._i for g in governors if g.model == CONST_POWER] typ2 = [g.generator._i for g in governors if g.model == GENERAL_IEEE] # Governor type 1: constant power F[typ1, 0] = 0 # Governor type 2: IEEE general speed-governing system Pm = Xgov[typ2, 0] P = Xgov[typ2, 1] x = Xgov[typ2, 2] z = Xgov[typ2, 3] K = Pgov[typ2, 0] T1 = Pgov[typ2, 1] T2 = Pgov[typ2, 2] T3 = Pgov[typ2, 3] Pup = Pgov[typ2, 4] Pdown = Pgov[typ2, 5] Pmax = Pgov[typ2, 6] Pmin = Pgov[typ2, 7] P0 = Pgov[typ2, 8] omega = Vgov[typ2, 0] dx = K * (-1 / T1 * x + (1 - T2 / T1) * (omega - omegas)) dP = 1 / T1 * x + T2 / T1 * (omega - omegas) y = 1 / T3 * (P0 - P - Pm) y2 = y if sum(flatnonzero(y > Pup)) >= 1: y2 = (1 - flatnonzero(y > Pup)) * y2 + flatnonzero(y > Pup) * Pup if sum(flatnonzero(y < Pdown)) >= 1: y2 = (1 - flatnonzero(y<Pdown)) * y2 + flatnonzero(y<Pdown) * Pdown dz = y2 dPm = y2 if sum(flatnonzero(z > Pmax)) >= 1: dPm = (1 - flatnonzero(z > Pmax)) * dPm + flatnonzero(z > Pmax) * 0 if sum(flatnonzero(z < Pmin)) >= 1: dPm = (1 - flatnonzero(z < Pmin)) * dPm + flatnonzero(z < Pmin) * 0 F[typ2, :] = c_[dPm, dP, dx, dz] # Governor type 3: # Governor type 4: return F
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:generator; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:Xgen; 6, identifier:Xexc; 7, identifier:Xgov; 8, identifier:Vgen; 9, block; 9, 10; 9, 12; 9, 18; 9, 28; 9, 37; 9, 53; 9, 69; 9, 70; 9, 77; 9, 84; 9, 99; 9, 114; 9, 121; 9, 127; 9, 150; 9, 160; 9, 172; 9, 173; 9, 180; 9, 187; 9, 194; 9, 207; 9, 220; 9, 233; 9, 246; 9, 259; 9, 272; 9, 285; 9, 298; 9, 305; 9, 312; 9, 319; 9, 326; 9, 333; 9, 339; 9, 362; 9, 380; 9, 397; 9, 410; 9, 411; 9, 412; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:generators; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:dyn_generators; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:omegas; 21, binary_operator:*; 21, 22; 21, 25; 22, binary_operator:*; 22, 23; 22, 24; 23, integer:2; 24, identifier:pi; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:freq; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:F; 31, call; 31, 32; 31, 33; 32, identifier:zeros; 33, argument_list; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:Xgen; 36, identifier:shape; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:typ1; 40, list_comprehension; 40, 41; 40, 44; 40, 47; 41, attribute; 41, 42; 41, 43; 42, identifier:g; 43, identifier:_i; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:g; 46, identifier:generators; 47, if_clause; 47, 48; 48, comparison_operator:==; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:g; 51, identifier:model; 52, identifier:CLASSICAL; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:typ2; 56, list_comprehension; 56, 57; 56, 60; 56, 63; 57, attribute; 57, 58; 57, 59; 58, identifier:g; 59, identifier:_i; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:g; 62, identifier:generators; 63, if_clause; 63, 64; 64, comparison_operator:==; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:g; 67, identifier:model; 68, identifier:FOURTH_ORDER; 69, comment; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:omega; 73, subscript; 73, 74; 73, 75; 73, 76; 74, identifier:Xgen; 75, identifier:typ1; 76, integer:1; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:Pm0; 80, subscript; 80, 81; 80, 82; 80, 83; 81, identifier:Xgov; 82, identifier:typ1; 83, integer:0; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:H; 87, subscript; 87, 88; 87, 98; 88, call; 88, 89; 88, 90; 89, identifier:array; 90, argument_list; 90, 91; 91, list_comprehension; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:g; 94, identifier:h; 95, for_in_clause; 95, 96; 95, 97; 96, identifier:g; 97, identifier:generators; 98, identifier:typ1; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:D; 102, subscript; 102, 103; 102, 113; 103, call; 103, 104; 103, 105; 104, identifier:array; 105, argument_list; 105, 106; 106, list_comprehension; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:g; 109, identifier:d; 110, for_in_clause; 110, 111; 110, 112; 111, identifier:g; 112, identifier:generators; 113, identifier:typ1; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:Pe; 117, subscript; 117, 118; 117, 119; 117, 120; 118, identifier:Vgen; 119, identifier:typ1; 120, integer:2; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:ddelta; 124, assignment; 124, 125; 124, 126; 125, identifier:omega; 126, identifier:omegas; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:domega; 130, binary_operator:*; 130, 131; 130, 138; 131, binary_operator:/; 131, 132; 131, 137; 132, binary_operator:*; 132, 133; 132, 134; 133, identifier:pi; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:freq; 137, identifier:H; 138, parenthesized_expression; 138, 139; 139, binary_operator:-; 139, 140; 139, 149; 140, binary_operator:+; 140, 141; 140, 148; 141, binary_operator:*; 141, 142; 141, 144; 142, unary_operator:-; 142, 143; 143, identifier:D; 144, parenthesized_expression; 144, 145; 145, binary_operator:-; 145, 146; 145, 147; 146, identifier:omega; 147, identifier:omegas; 148, identifier:Pm0; 149, identifier:Pe; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:dEq; 153, call; 153, 154; 153, 155; 154, identifier:zeros; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:len; 158, argument_list; 158, 159; 159, identifier:typ1; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 167; 162, subscript; 162, 163; 162, 164; 162, 165; 163, identifier:F; 164, identifier:typ1; 165, slice; 165, 166; 166, colon; 167, subscript; 167, 168; 167, 169; 167, 170; 167, 171; 168, identifier:c_; 169, identifier:ddelta; 170, identifier:domega; 171, identifier:dEq; 172, comment; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:omega; 176, subscript; 176, 177; 176, 178; 176, 179; 177, identifier:Xgen; 178, identifier:typ2; 179, integer:1; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:Eq_tr; 183, subscript; 183, 184; 183, 185; 183, 186; 184, identifier:Xgen; 185, identifier:typ2; 186, integer:2; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:Ed_tr; 190, subscript; 190, 191; 190, 192; 190, 193; 191, identifier:Xgen; 192, identifier:typ2; 193, integer:3; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:H; 197, call; 197, 198; 197, 199; 198, identifier:array; 199, argument_list; 199, 200; 200, list_comprehension; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:g; 203, identifier:h; 204, for_in_clause; 204, 205; 204, 206; 205, identifier:g; 206, identifier:generators; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:D; 210, call; 210, 211; 210, 212; 211, identifier:array; 212, argument_list; 212, 213; 213, list_comprehension; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:g; 216, identifier:d; 217, for_in_clause; 217, 218; 217, 219; 218, identifier:g; 219, identifier:generators; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:xd; 223, call; 223, 224; 223, 225; 224, identifier:array; 225, argument_list; 225, 226; 226, list_comprehension; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:g; 229, identifier:xd; 230, for_in_clause; 230, 231; 230, 232; 231, identifier:g; 232, identifier:generators; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:xq; 236, call; 236, 237; 236, 238; 237, identifier:array; 238, argument_list; 238, 239; 239, list_comprehension; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:g; 242, identifier:xq; 243, for_in_clause; 243, 244; 243, 245; 244, identifier:g; 245, identifier:generators; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:xd_tr; 249, call; 249, 250; 249, 251; 250, identifier:array; 251, argument_list; 251, 252; 252, list_comprehension; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:g; 255, identifier:xd_tr; 256, for_in_clause; 256, 257; 256, 258; 257, identifier:g; 258, identifier:generators; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:xq_tr; 262, call; 262, 263; 262, 264; 263, identifier:array; 264, argument_list; 264, 265; 265, list_comprehension; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:g; 268, identifier:xq_tr; 269, for_in_clause; 269, 270; 269, 271; 270, identifier:g; 271, identifier:generators; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:Td0_tr; 275, call; 275, 276; 275, 277; 276, identifier:array; 277, argument_list; 277, 278; 278, list_comprehension; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:g; 281, identifier:td; 282, for_in_clause; 282, 283; 282, 284; 283, identifier:g; 284, identifier:generators; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:Tq0_tr; 288, call; 288, 289; 288, 290; 289, identifier:array; 290, argument_list; 290, 291; 291, list_comprehension; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:g; 294, identifier:tq; 295, for_in_clause; 295, 296; 295, 297; 296, identifier:g; 297, identifier:generators; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:Id; 301, subscript; 301, 302; 301, 303; 301, 304; 302, identifier:Vgen; 303, identifier:typ2; 304, integer:0; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:Iq; 308, subscript; 308, 309; 308, 310; 308, 311; 309, identifier:Vgen; 310, identifier:typ2; 311, integer:1; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:Pe; 315, subscript; 315, 316; 315, 317; 315, 318; 316, identifier:Vgen; 317, identifier:typ2; 318, integer:2; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:Efd; 322, subscript; 322, 323; 322, 324; 322, 325; 323, identifier:Xexc; 324, identifier:typ2; 325, integer:0; 326, expression_statement; 326, 327; 327, assignment; 327, 328; 327, 329; 328, identifier:Pm; 329, subscript; 329, 330; 329, 331; 329, 332; 330, identifier:Xgov; 331, identifier:typ2; 332, integer:0; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:ddelta; 336, binary_operator:-; 336, 337; 336, 338; 337, identifier:omega; 338, identifier:omegas; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:domega; 342, binary_operator:*; 342, 343; 342, 350; 343, binary_operator:/; 343, 344; 343, 349; 344, binary_operator:*; 344, 345; 344, 346; 345, identifier:pi; 346, attribute; 346, 347; 346, 348; 347, identifier:self; 348, identifier:freq; 349, identifier:H; 350, parenthesized_expression; 350, 351; 351, binary_operator:-; 351, 352; 351, 361; 352, binary_operator:+; 352, 353; 352, 360; 353, binary_operator:*; 353, 354; 353, 356; 354, unary_operator:-; 354, 355; 355, identifier:D; 356, parenthesized_expression; 356, 357; 357, binary_operator:-; 357, 358; 357, 359; 358, identifier:omega; 359, identifier:omegas; 360, identifier:Pm; 361, identifier:Pe; 362, expression_statement; 362, 363; 363, assignment; 363, 364; 363, 365; 364, identifier:dEq; 365, binary_operator:*; 365, 366; 365, 369; 366, binary_operator:/; 366, 367; 366, 368; 367, integer:1; 368, identifier:Td0_tr; 369, parenthesized_expression; 369, 370; 370, binary_operator:+; 370, 371; 370, 374; 371, binary_operator:-; 371, 372; 371, 373; 372, identifier:Efd; 373, identifier:Eq_tr; 374, binary_operator:*; 374, 375; 374, 379; 375, parenthesized_expression; 375, 376; 376, binary_operator:-; 376, 377; 376, 378; 377, identifier:xd; 378, identifier:xd_tr; 379, identifier:Id; 380, expression_statement; 380, 381; 381, assignment; 381, 382; 381, 383; 382, identifier:dEd; 383, binary_operator:*; 383, 384; 383, 387; 384, binary_operator:/; 384, 385; 384, 386; 385, integer:1; 386, identifier:Tq0_tr; 387, parenthesized_expression; 387, 388; 388, binary_operator:-; 388, 389; 388, 391; 389, unary_operator:-; 389, 390; 390, identifier:Ed_tr; 391, binary_operator:*; 391, 392; 391, 396; 392, parenthesized_expression; 392, 393; 393, binary_operator:-; 393, 394; 393, 395; 394, identifier:xq; 395, identifier:xq_tr; 396, identifier:Iq; 397, expression_statement; 397, 398; 398, assignment; 398, 399; 398, 404; 399, subscript; 399, 400; 399, 401; 399, 402; 400, identifier:F; 401, identifier:typ2; 402, slice; 402, 403; 403, colon; 404, subscript; 404, 405; 404, 406; 404, 407; 404, 408; 404, 409; 405, identifier:c_; 406, identifier:ddelta; 407, identifier:domega; 408, identifier:dEq; 409, identifier:dEd; 410, comment; 411, comment; 412, return_statement; 412, 413; 413, identifier:F
def generator(self, Xgen, Xexc, Xgov, Vgen): """ Generator model. Based on Generator.m from MatDyn by Stijn Cole, developed at Katholieke Universiteit Leuven. See U{http://www.esat.kuleuven.be/electa/teaching/ matdyn/} for more information. """ generators = self.dyn_generators omegas = 2 * pi * self.freq F = zeros(Xgen.shape) typ1 = [g._i for g in generators if g.model == CLASSICAL] typ2 = [g._i for g in generators if g.model == FOURTH_ORDER] # Generator type 1: classical model omega = Xgen[typ1, 1] Pm0 = Xgov[typ1, 0] H = array([g.h for g in generators])[typ1] D = array([g.d for g in generators])[typ1] Pe = Vgen[typ1, 2] ddelta = omega = omegas domega = pi * self.freq / H * (-D * (omega - omegas) + Pm0 - Pe) dEq = zeros(len(typ1)) F[typ1, :] = c_[ddelta, domega, dEq] # Generator type 2: 4th order model omega = Xgen[typ2, 1] Eq_tr = Xgen[typ2, 2] Ed_tr = Xgen[typ2, 3] H = array([g.h for g in generators]) D = array([g.d for g in generators]) xd = array([g.xd for g in generators]) xq = array([g.xq for g in generators]) xd_tr = array([g.xd_tr for g in generators]) xq_tr = array([g.xq_tr for g in generators]) Td0_tr = array([g.td for g in generators]) Tq0_tr = array([g.tq for g in generators]) Id = Vgen[typ2, 0] Iq = Vgen[typ2, 1] Pe = Vgen[typ2, 2] Efd = Xexc[typ2, 0] Pm = Xgov[typ2, 0] ddelta = omega - omegas domega = pi * self.freq / H * (-D * (omega - omegas) + Pm - Pe) dEq = 1 / Td0_tr * (Efd - Eq_tr + (xd - xd_tr) * Id) dEd = 1 / Tq0_tr * (-Ed_tr - (xq - xq_tr) * Iq) F[typ2, :] = c_[ddelta, domega, dEq, dEd] # Generator type 3: # Generator type 4: return F
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_const_pf_constraints; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:gn; 6, identifier:base_mva; 7, block; 7, 8; 7, 10; 7, 43; 7, 53; 7, 60; 7, 67; 7, 82; 7, 97; 7, 112; 7, 127; 7, 142; 7, 143; 7, 169; 7, 170; 7, 171; 7, 172; 7, 173; 7, 189; 7, 216; 7, 217; 7, 333; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:ivl; 13, call; 13, 14; 13, 15; 14, identifier:array; 15, argument_list; 15, 16; 16, list_comprehension; 16, 17; 16, 18; 16, 26; 17, identifier:i; 18, for_in_clause; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:i; 21, identifier:g; 22, call; 22, 23; 22, 24; 23, identifier:enumerate; 24, argument_list; 24, 25; 25, identifier:gn; 26, if_clause; 26, 27; 27, boolean_operator:and; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:g; 30, identifier:is_load; 31, parenthesized_expression; 31, 32; 32, boolean_operator:or; 32, 33; 32, 38; 33, comparison_operator:!=; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:g; 36, identifier:q_min; 37, float:0.0; 38, comparison_operator:!=; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:g; 41, identifier:q_max; 42, float:0.0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:vl; 46, list_comprehension; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:gn; 49, identifier:i; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:i; 52, identifier:ivl; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:nvl; 56, call; 56, 57; 56, 58; 57, identifier:len; 58, argument_list; 58, 59; 59, identifier:vl; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:ng; 63, call; 63, 64; 63, 65; 64, identifier:len; 65, argument_list; 65, 66; 66, identifier:gn; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:Pg; 70, binary_operator:/; 70, 71; 70, 81; 71, call; 71, 72; 71, 73; 72, identifier:array; 73, argument_list; 73, 74; 74, list_comprehension; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:g; 77, identifier:p; 78, for_in_clause; 78, 79; 78, 80; 79, identifier:g; 80, identifier:vl; 81, identifier:base_mva; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:Qg; 85, binary_operator:/; 85, 86; 85, 96; 86, call; 86, 87; 86, 88; 87, identifier:array; 88, argument_list; 88, 89; 89, list_comprehension; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:g; 92, identifier:q; 93, for_in_clause; 93, 94; 93, 95; 94, identifier:g; 95, identifier:vl; 96, identifier:base_mva; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:Pmin; 100, binary_operator:/; 100, 101; 100, 111; 101, call; 101, 102; 101, 103; 102, identifier:array; 103, argument_list; 103, 104; 104, list_comprehension; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:g; 107, identifier:p_min; 108, for_in_clause; 108, 109; 108, 110; 109, identifier:g; 110, identifier:vl; 111, identifier:base_mva; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:Qmin; 115, binary_operator:/; 115, 116; 115, 126; 116, call; 116, 117; 116, 118; 117, identifier:array; 118, argument_list; 118, 119; 119, list_comprehension; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:g; 122, identifier:q_min; 123, for_in_clause; 123, 124; 123, 125; 124, identifier:g; 125, identifier:vl; 126, identifier:base_mva; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:Qmax; 130, binary_operator:/; 130, 131; 130, 141; 131, call; 131, 132; 131, 133; 132, identifier:array; 133, argument_list; 133, 134; 134, list_comprehension; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:g; 137, identifier:q_max; 138, for_in_clause; 138, 139; 138, 140; 139, identifier:g; 140, identifier:vl; 141, identifier:base_mva; 142, comment; 143, for_statement; 143, 144; 143, 145; 143, 146; 144, identifier:g; 145, identifier:vl; 146, block; 146, 147; 147, if_statement; 147, 148; 147, 159; 148, boolean_operator:and; 148, 149; 148, 154; 149, comparison_operator:!=; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:g; 152, identifier:qmin; 153, float:0.0; 154, comparison_operator:!=; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:g; 157, identifier:q_max; 158, float:0.0; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:logger; 164, identifier:error; 165, argument_list; 165, 166; 166, concatenated_string; 166, 167; 166, 168; 167, string:"Either Qmin or Qmax must be equal to zero for "; 168, string:"each dispatchable load."; 169, comment; 170, comment; 171, comment; 172, comment; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:Qlim; 176, binary_operator:+; 176, 177; 176, 183; 177, binary_operator:*; 177, 178; 177, 182; 178, parenthesized_expression; 178, 179; 179, comparison_operator:==; 179, 180; 179, 181; 180, identifier:Qmin; 181, float:0.0; 182, identifier:Qmax; 183, binary_operator:*; 183, 184; 183, 188; 184, parenthesized_expression; 184, 185; 185, comparison_operator:==; 185, 186; 185, 187; 186, identifier:Qmax; 187, float:0.0; 188, identifier:Qmin; 189, if_statement; 189, 190; 189, 205; 190, call; 190, 191; 190, 192; 191, identifier:any; 192, argument_list; 192, 193; 193, comparison_operator:>; 193, 194; 193, 204; 194, call; 194, 195; 194, 196; 195, identifier:abs; 196, argument_list; 196, 197; 197, binary_operator:-; 197, 198; 197, 199; 198, identifier:Qg; 199, binary_operator:/; 199, 200; 199, 203; 200, binary_operator:*; 200, 201; 200, 202; 201, identifier:Pg; 202, identifier:Qlim; 203, identifier:Pmin; 204, float:1e-6; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:logger; 210, identifier:error; 211, argument_list; 211, 212; 212, concatenated_string; 212, 213; 212, 214; 212, 215; 213, string:"For a dispatchable load, PG and QG must be "; 214, string:"consistent with the power factor defined by "; 215, string:"PMIN and the Q limits."; 216, comment; 217, if_statement; 217, 218; 217, 221; 217, 306; 218, comparison_operator:>; 218, 219; 218, 220; 219, identifier:nvl; 220, integer:0; 221, block; 221, 222; 221, 226; 221, 230; 221, 238; 221, 245; 221, 253; 221, 268; 221, 277; 221, 295; 221, 302; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:xx; 225, identifier:Pmin; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:yy; 229, identifier:Qlim; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:pftheta; 233, call; 233, 234; 233, 235; 234, identifier:arctan2; 235, argument_list; 235, 236; 235, 237; 236, identifier:yy; 237, identifier:xx; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:pc; 241, call; 241, 242; 241, 243; 242, identifier:sin; 243, argument_list; 243, 244; 244, identifier:pftheta; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:qc; 248, unary_operator:-; 248, 249; 249, call; 249, 250; 249, 251; 250, identifier:cos; 251, argument_list; 251, 252; 252, identifier:pftheta; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:ii; 256, call; 256, 257; 256, 258; 257, identifier:array; 258, argument_list; 258, 259; 259, list:[range(nvl), range(nvl)]; 259, 260; 259, 264; 260, call; 260, 261; 260, 262; 261, identifier:range; 262, argument_list; 262, 263; 263, identifier:nvl; 264, call; 264, 265; 264, 266; 265, identifier:range; 266, argument_list; 266, 267; 267, identifier:nvl; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:jj; 271, subscript; 271, 272; 271, 273; 271, 274; 272, identifier:r_; 273, identifier:ivl; 274, binary_operator:+; 274, 275; 274, 276; 275, identifier:ivl; 276, identifier:ng; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:Avl; 280, call; 280, 281; 280, 282; 281, identifier:csr_matrix; 282, argument_list; 282, 283; 282, 287; 282, 290; 283, subscript; 283, 284; 283, 285; 283, 286; 284, identifier:r_; 285, identifier:pc; 286, identifier:qc; 287, tuple; 287, 288; 287, 289; 288, identifier:ii; 289, identifier:jj; 290, tuple; 290, 291; 290, 292; 291, identifier:nvl; 292, binary_operator:*; 292, 293; 292, 294; 293, integer:2; 294, identifier:ng; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:lvl; 298, call; 298, 299; 298, 300; 299, identifier:zeros; 300, argument_list; 300, 301; 301, identifier:nvl; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 305; 304, identifier:uvl; 305, identifier:lvl; 306, else_clause; 306, 307; 307, block; 307, 308; 307, 319; 307, 326; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:Avl; 311, call; 311, 312; 311, 313; 312, identifier:zeros; 313, argument_list; 313, 314; 314, tuple; 314, 315; 314, 316; 315, integer:0; 316, binary_operator:*; 316, 317; 316, 318; 317, integer:2; 318, identifier:ng; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:lvl; 322, call; 322, 323; 322, 324; 323, identifier:array; 324, argument_list; 324, 325; 325, list:[]; 326, expression_statement; 326, 327; 327, assignment; 327, 328; 327, 329; 328, identifier:uvl; 329, call; 329, 330; 329, 331; 330, identifier:array; 331, argument_list; 331, 332; 332, list:[]; 333, return_statement; 333, 334; 334, call; 334, 335; 334, 336; 335, identifier:LinearConstraint; 336, argument_list; 336, 337; 336, 338; 336, 339; 336, 340; 336, 341; 337, string:"vl"; 338, identifier:Avl; 339, identifier:lvl; 340, identifier:uvl; 341, list:["Pg", "Qg"]; 341, 342; 341, 343; 342, string:"Pg"; 343, string:"Qg"
def _const_pf_constraints(self, gn, base_mva): """ Returns a linear constraint enforcing constant power factor for dispatchable loads. The power factor is derived from the original value of Pmin and either Qmin (for inductive loads) or Qmax (for capacitive loads). If both Qmin and Qmax are zero, this implies a unity power factor without the need for an additional constraint. """ ivl = array([i for i, g in enumerate(gn) if g.is_load and (g.q_min != 0.0 or g.q_max != 0.0)]) vl = [gn[i] for i in ivl] nvl = len(vl) ng = len(gn) Pg = array([g.p for g in vl]) / base_mva Qg = array([g.q for g in vl]) / base_mva Pmin = array([g.p_min for g in vl]) / base_mva Qmin = array([g.q_min for g in vl]) / base_mva Qmax = array([g.q_max for g in vl]) / base_mva # At least one of the Q limits must be zero (corresponding to Pmax==0). for g in vl: if g.qmin != 0.0 and g.q_max != 0.0: logger.error("Either Qmin or Qmax must be equal to zero for " "each dispatchable load.") # Initial values of PG and QG must be consistent with specified power # factor. This is to prevent a user from unknowingly using a case file # which would have defined a different power factor constraint under a # previous version which used PG and QG to define the power factor. Qlim = (Qmin == 0.0) * Qmax + (Qmax == 0.0) * Qmin if any( abs(Qg - Pg * Qlim / Pmin) > 1e-6 ): logger.error("For a dispatchable load, PG and QG must be " "consistent with the power factor defined by " "PMIN and the Q limits.") # Make Avl, lvl, uvl, for lvl <= Avl * r_[Pg, Qg] <= uvl if nvl > 0: xx = Pmin yy = Qlim pftheta = arctan2(yy, xx) pc = sin(pftheta) qc = -cos(pftheta) ii = array([range(nvl), range(nvl)]) jj = r_[ivl, ivl + ng] Avl = csr_matrix(r_[pc, qc], (ii, jj), (nvl, 2 * ng)) lvl = zeros(nvl) uvl = lvl else: Avl = zeros((0, 2 * ng)) lvl = array([]) uvl = array([]) return LinearConstraint("vl", Avl, lvl, uvl, ["Pg", "Qg"])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_voltage_angle_diff_limit; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:buses; 6, identifier:branches; 7, block; 7, 8; 7, 10; 7, 17; 7, 309; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:nb; 13, call; 13, 14; 13, 15; 14, identifier:len; 15, argument_list; 15, 16; 16, identifier:buses; 17, if_statement; 17, 18; 17, 22; 17, 280; 18, not_operator; 18, 19; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:ignore_ang_lim; 22, block; 22, 23; 22, 61; 22, 85; 22, 109; 22, 116; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:iang; 26, list_comprehension; 26, 27; 26, 28; 26, 36; 27, identifier:i; 28, for_in_clause; 28, 29; 28, 32; 29, pattern_list; 29, 30; 29, 31; 30, identifier:i; 31, identifier:b; 32, call; 32, 33; 32, 34; 33, identifier:enumerate; 34, argument_list; 34, 35; 35, identifier:branches; 36, if_clause; 36, 37; 37, boolean_operator:or; 37, 38; 37, 50; 38, parenthesized_expression; 38, 39; 39, boolean_operator:and; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:b; 42, identifier:ang_min; 43, parenthesized_expression; 43, 44; 44, comparison_operator:>; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:b; 47, identifier:ang_min; 48, unary_operator:-; 48, 49; 49, float:360.0; 50, parenthesized_expression; 50, 51; 51, boolean_operator:and; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:b; 54, identifier:ang_max; 55, parenthesized_expression; 55, 56; 56, comparison_operator:<; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:b; 59, identifier:ang_max; 60, float:360.0; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:iangl; 64, subscript; 64, 65; 64, 84; 65, call; 65, 66; 65, 67; 66, identifier:array; 67, argument_list; 67, 68; 68, list_comprehension; 68, 69; 68, 70; 68, 78; 69, identifier:i; 70, for_in_clause; 70, 71; 70, 74; 71, pattern_list; 71, 72; 71, 73; 72, identifier:i; 73, identifier:b; 74, call; 74, 75; 74, 76; 75, identifier:enumerate; 76, argument_list; 76, 77; 77, identifier:branches; 78, if_clause; 78, 79; 79, comparison_operator:is; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:b; 82, identifier:ang_min; 83, None; 84, identifier:iang; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:iangh; 88, subscript; 88, 89; 88, 108; 89, call; 89, 90; 89, 91; 90, identifier:array; 91, argument_list; 91, 92; 92, list_comprehension; 92, 93; 92, 94; 92, 102; 93, identifier:i; 94, for_in_clause; 94, 95; 94, 98; 95, pattern_list; 95, 96; 95, 97; 96, identifier:i; 97, identifier:b; 98, call; 98, 99; 98, 100; 99, identifier:enumerate; 100, argument_list; 100, 101; 101, identifier:branches; 102, if_clause; 102, 103; 103, comparison_operator:is; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:b; 106, identifier:ang_max; 107, None; 108, identifier:iang; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:nang; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, identifier:iang; 116, if_statement; 116, 117; 116, 120; 116, 252; 117, comparison_operator:>; 117, 118; 117, 119; 118, identifier:nang; 119, integer:0; 120, block; 120, 121; 120, 133; 120, 150; 120, 167; 120, 174; 120, 194; 120, 203; 120, 208; 120, 230; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:ii; 124, binary_operator:+; 124, 125; 124, 129; 125, call; 125, 126; 125, 127; 126, identifier:range; 127, argument_list; 127, 128; 128, identifier:nang; 129, call; 129, 130; 129, 131; 130, identifier:range; 131, argument_list; 131, 132; 132, identifier:nang; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:jjf; 136, subscript; 136, 137; 136, 149; 137, call; 137, 138; 137, 139; 138, identifier:array; 139, argument_list; 139, 140; 140, list_comprehension; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:b; 144, identifier:from_bus; 145, identifier:_i; 146, for_in_clause; 146, 147; 146, 148; 147, identifier:b; 148, identifier:branches; 149, identifier:iang; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:jjt; 153, subscript; 153, 154; 153, 166; 154, call; 154, 155; 154, 156; 155, identifier:array; 156, argument_list; 156, 157; 157, list_comprehension; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:b; 161, identifier:to_bus; 162, identifier:_i; 163, for_in_clause; 163, 164; 163, 165; 164, identifier:b; 165, identifier:branches; 166, identifier:iang; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:jj; 170, subscript; 170, 171; 170, 172; 170, 173; 171, identifier:r_; 172, identifier:jjf; 173, identifier:jjt; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:Aang; 177, call; 177, 178; 177, 179; 178, identifier:csr_matrix; 179, argument_list; 179, 180; 179, 191; 180, subscript; 180, 181; 180, 182; 180, 186; 181, identifier:r_; 182, call; 182, 183; 182, 184; 183, identifier:ones; 184, argument_list; 184, 185; 185, identifier:nang; 186, unary_operator:-; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:ones; 189, argument_list; 189, 190; 190, identifier:nang; 191, tuple; 191, 192; 191, 193; 192, identifier:ii; 193, identifier:jj; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:uang; 197, binary_operator:*; 197, 198; 197, 199; 198, identifier:Inf; 199, call; 199, 200; 199, 201; 200, identifier:ones; 201, argument_list; 201, 202; 202, identifier:nang; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:lang; 206, unary_operator:-; 206, 207; 207, identifier:uang; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:lang; 212, identifier:iangl; 213, subscript; 213, 214; 213, 229; 214, call; 214, 215; 214, 216; 215, identifier:array; 216, argument_list; 216, 217; 217, list_comprehension; 217, 218; 217, 226; 218, binary_operator:*; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:b; 221, identifier:ang_min; 222, parenthesized_expression; 222, 223; 223, binary_operator:/; 223, 224; 223, 225; 224, identifier:pi; 225, float:180.0; 226, for_in_clause; 226, 227; 226, 228; 227, identifier:b; 228, identifier:branches; 229, identifier:iangl; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:uang; 234, identifier:iangh; 235, subscript; 235, 236; 235, 251; 236, call; 236, 237; 236, 238; 237, identifier:array; 238, argument_list; 238, 239; 239, list_comprehension; 239, 240; 239, 248; 240, binary_operator:*; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:b; 243, identifier:ang_max; 244, parenthesized_expression; 244, 245; 245, binary_operator:/; 245, 246; 245, 247; 246, identifier:pi; 247, float:180.0; 248, for_in_clause; 248, 249; 248, 250; 249, identifier:b; 250, identifier:branches; 251, identifier:iangh; 252, else_clause; 252, 253; 252, 254; 252, 255; 252, 256; 253, comment; 254, comment; 255, comment; 256, block; 256, 257; 256, 266; 256, 273; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:Aang; 260, call; 260, 261; 260, 262; 261, identifier:zeros; 262, argument_list; 262, 263; 263, tuple; 263, 264; 263, 265; 264, integer:0; 265, identifier:nb; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:lang; 269, call; 269, 270; 269, 271; 270, identifier:array; 271, argument_list; 271, 272; 272, list:[]; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:uang; 276, call; 276, 277; 276, 278; 277, identifier:array; 278, argument_list; 278, 279; 279, list:[]; 280, else_clause; 280, 281; 280, 282; 280, 283; 280, 284; 280, 285; 281, comment; 282, comment; 283, comment; 284, comment; 285, block; 285, 286; 285, 295; 285, 302; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:Aang; 289, call; 289, 290; 289, 291; 290, identifier:zeros; 291, argument_list; 291, 292; 292, tuple; 292, 293; 292, 294; 293, integer:0; 294, identifier:nb; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:lang; 298, call; 298, 299; 298, 300; 299, identifier:array; 300, argument_list; 300, 301; 301, list:[]; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 305; 304, identifier:uang; 305, call; 305, 306; 305, 307; 306, identifier:array; 307, argument_list; 307, 308; 308, list:[]; 309, return_statement; 309, 310; 310, call; 310, 311; 310, 312; 311, identifier:LinearConstraint; 312, argument_list; 312, 313; 312, 314; 312, 315; 312, 316; 312, 317; 313, string:"ang"; 314, identifier:Aang; 315, identifier:lang; 316, identifier:uang; 317, list:["Va"]; 317, 318; 318, string:"Va"
def _voltage_angle_diff_limit(self, buses, branches): """ Returns the constraint on the branch voltage angle differences. """ nb = len(buses) if not self.ignore_ang_lim: iang = [i for i, b in enumerate(branches) if (b.ang_min and (b.ang_min > -360.0)) or (b.ang_max and (b.ang_max < 360.0))] iangl = array([i for i, b in enumerate(branches) if b.ang_min is not None])[iang] iangh = array([i for i, b in enumerate(branches) if b.ang_max is not None])[iang] nang = len(iang) if nang > 0: ii = range(nang) + range(nang) jjf = array([b.from_bus._i for b in branches])[iang] jjt = array([b.to_bus._i for b in branches])[iang] jj = r_[jjf, jjt] Aang = csr_matrix(r_[ones(nang), -ones(nang)], (ii, jj)) uang = Inf * ones(nang) lang = -uang lang[iangl] = array([b.ang_min * (pi / 180.0) for b in branches])[iangl] uang[iangh] = array([b.ang_max * (pi / 180.0) for b in branches])[iangh] else: # Aang = csr_matrix((0, nb), dtype=float64) # lang = array([], dtype=float64) # uang = array([], dtype=float64) Aang = zeros((0, nb)) lang = array([]) uang = array([]) else: # Aang = csr_matrix((0, nb), dtype=float64) # lang = array([], dtype=float64) # uang = array([], dtype=float64) # iang = array([], dtype=float64) Aang = zeros((0, nb)) lang = array([]) uang = array([]) return LinearConstraint("ang", Aang, lang, uang, ["Va"])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_clipPrices; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 41; 5, 42; 5, 74; 5, 75; 5, 112; 5, 113; 5, 150; 5, 151; 5, 152; 5, 153; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:guaranteeOfferPrice; 13, block; 13, 14; 14, for_statement; 14, 15; 14, 16; 14, 19; 15, identifier:offer; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:offers; 19, block; 19, 20; 20, if_statement; 20, 21; 20, 32; 21, boolean_operator:and; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:offer; 24, identifier:accepted; 25, comparison_operator:<; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:offer; 28, identifier:clearedPrice; 29, attribute; 29, 30; 29, 31; 30, identifier:offer; 31, identifier:price; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:offer; 37, identifier:clearedPrice; 38, attribute; 38, 39; 38, 40; 39, identifier:offer; 40, identifier:price; 41, comment; 42, if_statement; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:guaranteeBidPrice; 46, block; 46, 47; 47, for_statement; 47, 48; 47, 49; 47, 52; 48, identifier:bid; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:bids; 52, block; 52, 53; 53, if_statement; 53, 54; 53, 65; 54, boolean_operator:and; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:bid; 57, identifier:accepted; 58, comparison_operator:>; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:bid; 61, identifier:clearedPrice; 62, attribute; 62, 63; 62, 64; 63, identifier:bid; 64, identifier:price; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:bid; 70, identifier:clearedPrice; 71, attribute; 71, 72; 71, 73; 72, identifier:bid; 73, identifier:price; 74, comment; 75, if_statement; 75, 76; 75, 84; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:limits; 81, identifier:has_key; 82, argument_list; 82, 83; 83, string:"maxClearedOffer"; 84, block; 84, 85; 84, 93; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:maxClearedOffer; 88, subscript; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:limits; 92, string:"maxClearedOffer"; 93, for_statement; 93, 94; 93, 95; 93, 98; 94, identifier:offer; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:offers; 98, block; 98, 99; 99, if_statement; 99, 100; 99, 105; 100, comparison_operator:>; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:offer; 103, identifier:clearedPrice; 104, identifier:maxClearedOffer; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:offer; 110, identifier:clearedPrice; 111, identifier:maxClearedOffer; 112, comment; 113, if_statement; 113, 114; 113, 122; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:limits; 119, identifier:has_key; 120, argument_list; 120, 121; 121, string:"minClearedBid"; 122, block; 122, 123; 122, 131; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:minClearedBid; 126, subscript; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:limits; 130, string:"minClearedBid"; 131, for_statement; 131, 132; 131, 133; 131, 136; 132, identifier:bid; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:bids; 136, block; 136, 137; 137, if_statement; 137, 138; 137, 143; 138, comparison_operator:<; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:bid; 141, identifier:clearedPrice; 142, identifier:minClearedBid; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:bid; 148, identifier:clearedPrice; 149, identifier:minClearedBid; 150, comment; 151, comment; 152, comment; 153, if_statement; 153, 154; 153, 159; 154, comparison_operator:!=; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:auctionType; 158, identifier:DISCRIMINATIVE; 159, block; 159, 160; 160, for_statement; 160, 161; 160, 162; 160, 167; 161, identifier:g; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:case; 166, identifier:generators; 167, block; 167, 168; 167, 184; 167, 210; 167, 226; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:gOffers; 171, list_comprehension; 171, 172; 171, 173; 171, 178; 172, identifier:of; 173, for_in_clause; 173, 174; 173, 175; 174, identifier:of; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:offers; 178, if_clause; 178, 179; 179, comparison_operator:==; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:of; 182, identifier:generator; 183, identifier:g; 184, if_statement; 184, 185; 184, 186; 185, identifier:gOffers; 186, block; 186, 187; 186, 200; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:uniformPrice; 190, call; 190, 191; 190, 192; 191, identifier:max; 192, argument_list; 192, 193; 193, list_comprehension; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:of; 196, identifier:clearedPrice; 197, for_in_clause; 197, 198; 197, 199; 198, identifier:of; 199, identifier:gOffers; 200, for_statement; 200, 201; 200, 202; 200, 203; 201, identifier:of; 202, identifier:gOffers; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:of; 208, identifier:clearedPrice; 209, identifier:uniformPrice; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:gBids; 213, list_comprehension; 213, 214; 213, 215; 213, 220; 214, identifier:bid; 215, for_in_clause; 215, 216; 215, 217; 216, identifier:bid; 217, attribute; 217, 218; 217, 219; 218, identifier:self; 219, identifier:bids; 220, if_clause; 220, 221; 221, comparison_operator:==; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:bid; 224, identifier:vLoad; 225, identifier:g; 226, if_statement; 226, 227; 226, 228; 227, identifier:gBids; 228, block; 228, 229; 228, 242; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:uniformPrice; 232, call; 232, 233; 232, 234; 233, identifier:min; 234, argument_list; 234, 235; 235, list_comprehension; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:bid; 238, identifier:cleared_price; 239, for_in_clause; 239, 240; 239, 241; 240, identifier:bid; 241, identifier:gBids; 242, for_statement; 242, 243; 242, 244; 242, 245; 243, identifier:bid; 244, identifier:gBids; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:bid; 250, identifier:clearedPrice; 251, identifier:uniformPrice
def _clipPrices(self): """ Clip cleared prices according to guarantees and limits. """ # Guarantee that cleared offer prices are >= offers. if self.guaranteeOfferPrice: for offer in self.offers: if offer.accepted and offer.clearedPrice < offer.price: offer.clearedPrice = offer.price # Guarantee that cleared bid prices are <= bids. if self.guaranteeBidPrice: for bid in self.bids: if bid.accepted and bid.clearedPrice > bid.price: bid.clearedPrice = bid.price # Clip cleared offer prices. if self.limits.has_key("maxClearedOffer"): maxClearedOffer = self.limits["maxClearedOffer"] for offer in self.offers: if offer.clearedPrice > maxClearedOffer: offer.clearedPrice = maxClearedOffer # Clip cleared bid prices. if self.limits.has_key("minClearedBid"): minClearedBid = self.limits["minClearedBid"] for bid in self.bids: if bid.clearedPrice < minClearedBid: bid.clearedPrice = minClearedBid # Make prices uniform across all offers/bids for each generator after # clipping (except for discrim auction) since clipping may only affect # a single block of a multi-block generator. if self.auctionType != DISCRIMINATIVE: for g in self.case.generators: gOffers = [of for of in self.offers if of.generator == g] if gOffers: uniformPrice = max([of.clearedPrice for of in gOffers]) for of in gOffers: of.clearedPrice = uniformPrice gBids = [bid for bid in self.bids if bid.vLoad == g] if gBids: uniformPrice = min([bid.cleared_price for bid in gBids]) for bid in gBids: bid.clearedPrice = uniformPrice
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:gpu_iuwt_decomposition; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:in1; 5, identifier:scale_count; 6, identifier:scale_adjust; 7, identifier:store_smoothed; 8, identifier:store_on_gpu; 9, block; 9, 10; 9, 12; 9, 13; 9, 20; 9, 44; 9, 45; 9, 54; 9, 55; 9, 80; 9, 81; 9, 105; 9, 106; 9, 115; 9, 124; 9, 133; 9, 134; 9, 147; 9, 160; 9, 164; 9, 165; 9, 173; 9, 182; 9, 195; 9, 208; 9, 209; 9, 210; 9, 211; 9, 274; 9, 275; 9, 276; 9, 277; 9, 400; 9, 401; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:ker; 16, call; 16, 17; 16, 18; 17, identifier:SourceModule; 18, argument_list; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:wavelet_filter; 23, binary_operator:*; 23, 24; 23, 28; 24, parenthesized_expression; 24, 25; 25, binary_operator:/; 25, 26; 25, 27; 26, float:1.; 27, integer:16; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:np; 31, identifier:array; 32, argument_list; 32, 33; 32, 39; 33, list:[1,4,6,4,1]; 33, 34; 33, 35; 33, 36; 33, 37; 33, 38; 34, integer:1; 35, integer:4; 36, integer:6; 37, integer:4; 38, integer:1; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:dtype; 41, attribute; 41, 42; 41, 43; 42, identifier:np; 43, identifier:float32; 44, comment; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:wavelet_filter; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:gpuarray; 51, identifier:to_gpu_async; 52, argument_list; 52, 53; 53, identifier:wavelet_filter; 54, comment; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:detail_coeffs; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:gpuarray; 61, identifier:empty; 62, argument_list; 62, 63; 62, 77; 63, list:[scale_count-scale_adjust, in1.shape[0], in1.shape[1]]; 63, 64; 63, 67; 63, 72; 64, binary_operator:-; 64, 65; 64, 66; 65, identifier:scale_count; 66, identifier:scale_adjust; 67, subscript; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:in1; 70, identifier:shape; 71, integer:0; 72, subscript; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:in1; 75, identifier:shape; 76, integer:1; 77, attribute; 77, 78; 77, 79; 78, identifier:np; 79, identifier:float32; 80, comment; 81, try_statement; 81, 82; 81, 99; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:gpu_in1; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:gpuarray; 89, identifier:to_gpu_async; 90, argument_list; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:in1; 94, identifier:astype; 95, argument_list; 95, 96; 96, attribute; 96, 97; 96, 98; 97, identifier:np; 98, identifier:float32; 99, except_clause; 99, 100; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:gpu_in1; 104, identifier:in1; 105, comment; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:gpu_tmp; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:gpuarray; 112, identifier:empty_like; 113, argument_list; 113, 114; 114, identifier:gpu_in1; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:gpu_out1; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:gpuarray; 121, identifier:empty_like; 122, argument_list; 122, 123; 123, identifier:gpu_in1; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:gpu_out2; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:gpuarray; 130, identifier:empty_like; 131, argument_list; 131, 132; 132, identifier:gpu_in1; 133, comment; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:gpu_scale; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:gpuarray; 140, identifier:zeros; 141, argument_list; 141, 142; 141, 144; 142, list:[1]; 142, 143; 143, integer:1; 144, attribute; 144, 145; 144, 146; 145, identifier:np; 146, identifier:int32; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:gpu_adjust; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:gpuarray; 153, identifier:zeros; 154, argument_list; 154, 155; 154, 157; 155, list:[1]; 155, 156; 156, integer:1; 157, attribute; 157, 158; 157, 159; 158, identifier:np; 159, identifier:int32; 160, expression_statement; 160, 161; 161, augmented_assignment:+=; 161, 162; 161, 163; 162, identifier:gpu_adjust; 163, identifier:scale_adjust; 164, comment; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 170; 167, pattern_list; 167, 168; 167, 169; 168, identifier:gpu_a_trous_row_kernel; 169, identifier:gpu_a_trous_col_kernel; 170, call; 170, 171; 170, 172; 171, identifier:gpu_a_trous; 172, argument_list; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:gpu_store_detail_coeffs; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:ker; 179, identifier:get_function; 180, argument_list; 180, 181; 181, string:"gpu_store_detail_coeffs"; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:grid_rows; 185, call; 185, 186; 185, 187; 186, identifier:int; 187, argument_list; 187, 188; 188, binary_operator://; 188, 189; 188, 194; 189, subscript; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:in1; 192, identifier:shape; 193, integer:0; 194, integer:32; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:grid_cols; 198, call; 198, 199; 198, 200; 199, identifier:int; 200, argument_list; 200, 201; 201, binary_operator://; 201, 202; 201, 207; 202, subscript; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:in1; 205, identifier:shape; 206, integer:1; 207, integer:32; 208, comment; 209, comment; 210, comment; 211, if_statement; 211, 212; 211, 215; 212, comparison_operator:>; 212, 213; 212, 214; 213, identifier:scale_adjust; 214, integer:0; 215, block; 215, 216; 216, for_statement; 216, 217; 216, 218; 216, 223; 217, identifier:i; 218, call; 218, 219; 218, 220; 219, identifier:range; 220, argument_list; 220, 221; 220, 222; 221, integer:0; 222, identifier:scale_adjust; 223, block; 223, 224; 223, 243; 223, 262; 223, 270; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:gpu_a_trous_row_kernel; 227, argument_list; 227, 228; 227, 229; 227, 230; 227, 231; 227, 232; 227, 238; 228, identifier:gpu_in1; 229, identifier:gpu_tmp; 230, identifier:wavelet_filter; 231, identifier:gpu_scale; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:block; 234, tuple; 234, 235; 234, 236; 234, 237; 235, integer:32; 236, integer:32; 237, integer:1; 238, keyword_argument; 238, 239; 238, 240; 239, identifier:grid; 240, tuple; 240, 241; 240, 242; 241, identifier:grid_cols; 242, identifier:grid_rows; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 246; 245, identifier:gpu_a_trous_col_kernel; 246, argument_list; 246, 247; 246, 248; 246, 249; 246, 250; 246, 251; 246, 257; 247, identifier:gpu_tmp; 248, identifier:gpu_out1; 249, identifier:wavelet_filter; 250, identifier:gpu_scale; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:block; 253, tuple; 253, 254; 253, 255; 253, 256; 254, integer:32; 255, integer:32; 256, integer:1; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:grid; 259, tuple; 259, 260; 259, 261; 260, identifier:grid_cols; 261, identifier:grid_rows; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 267; 264, pattern_list; 264, 265; 264, 266; 265, identifier:gpu_in1; 266, identifier:gpu_out1; 267, expression_list; 267, 268; 267, 269; 268, identifier:gpu_out1; 269, identifier:gpu_in1; 270, expression_statement; 270, 271; 271, augmented_assignment:+=; 271, 272; 271, 273; 272, identifier:gpu_scale; 273, integer:1; 274, comment; 275, comment; 276, comment; 277, for_statement; 277, 278; 277, 279; 277, 284; 278, identifier:i; 279, call; 279, 280; 279, 281; 280, identifier:range; 281, argument_list; 281, 282; 281, 283; 282, identifier:scale_adjust; 283, identifier:scale_count; 284, block; 284, 285; 284, 304; 284, 323; 284, 324; 284, 343; 284, 362; 284, 363; 284, 387; 284, 388; 284, 396; 285, expression_statement; 285, 286; 286, call; 286, 287; 286, 288; 287, identifier:gpu_a_trous_row_kernel; 288, argument_list; 288, 289; 288, 290; 288, 291; 288, 292; 288, 293; 288, 299; 289, identifier:gpu_in1; 290, identifier:gpu_tmp; 291, identifier:wavelet_filter; 292, identifier:gpu_scale; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:block; 295, tuple; 295, 296; 295, 297; 295, 298; 296, integer:32; 297, integer:32; 298, integer:1; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:grid; 301, tuple; 301, 302; 301, 303; 302, identifier:grid_cols; 303, identifier:grid_rows; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 307; 306, identifier:gpu_a_trous_col_kernel; 307, argument_list; 307, 308; 307, 309; 307, 310; 307, 311; 307, 312; 307, 318; 308, identifier:gpu_tmp; 309, identifier:gpu_out1; 310, identifier:wavelet_filter; 311, identifier:gpu_scale; 312, keyword_argument; 312, 313; 312, 314; 313, identifier:block; 314, tuple; 314, 315; 314, 316; 314, 317; 315, integer:32; 316, integer:32; 317, integer:1; 318, keyword_argument; 318, 319; 318, 320; 319, identifier:grid; 320, tuple; 320, 321; 320, 322; 321, identifier:grid_cols; 322, identifier:grid_rows; 323, comment; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 327; 326, identifier:gpu_a_trous_row_kernel; 327, argument_list; 327, 328; 327, 329; 327, 330; 327, 331; 327, 332; 327, 338; 328, identifier:gpu_out1; 329, identifier:gpu_tmp; 330, identifier:wavelet_filter; 331, identifier:gpu_scale; 332, keyword_argument; 332, 333; 332, 334; 333, identifier:block; 334, tuple; 334, 335; 334, 336; 334, 337; 335, integer:32; 336, integer:32; 337, integer:1; 338, keyword_argument; 338, 339; 338, 340; 339, identifier:grid; 340, tuple; 340, 341; 340, 342; 341, identifier:grid_cols; 342, identifier:grid_rows; 343, expression_statement; 343, 344; 344, call; 344, 345; 344, 346; 345, identifier:gpu_a_trous_col_kernel; 346, argument_list; 346, 347; 346, 348; 346, 349; 346, 350; 346, 351; 346, 357; 347, identifier:gpu_tmp; 348, identifier:gpu_out2; 349, identifier:wavelet_filter; 350, identifier:gpu_scale; 351, keyword_argument; 351, 352; 351, 353; 352, identifier:block; 353, tuple; 353, 354; 353, 355; 353, 356; 354, integer:32; 355, integer:32; 356, integer:1; 357, keyword_argument; 357, 358; 357, 359; 358, identifier:grid; 359, tuple; 359, 360; 359, 361; 360, identifier:grid_cols; 361, identifier:grid_rows; 362, comment; 363, expression_statement; 363, 364; 364, call; 364, 365; 364, 366; 365, identifier:gpu_store_detail_coeffs; 366, argument_list; 366, 367; 366, 368; 366, 369; 366, 370; 366, 371; 366, 372; 366, 378; 367, identifier:gpu_in1; 368, identifier:gpu_out2; 369, identifier:detail_coeffs; 370, identifier:gpu_scale; 371, identifier:gpu_adjust; 372, keyword_argument; 372, 373; 372, 374; 373, identifier:block; 374, tuple; 374, 375; 374, 376; 374, 377; 375, integer:32; 376, integer:32; 377, integer:1; 378, keyword_argument; 378, 379; 378, 380; 379, identifier:grid; 380, tuple; 380, 381; 380, 382; 380, 383; 381, identifier:grid_cols; 382, identifier:grid_rows; 383, call; 383, 384; 383, 385; 384, identifier:int; 385, argument_list; 385, 386; 386, identifier:scale_count; 387, comment; 388, expression_statement; 388, 389; 389, assignment; 389, 390; 389, 393; 390, pattern_list; 390, 391; 390, 392; 391, identifier:gpu_in1; 392, identifier:gpu_out1; 393, expression_list; 393, 394; 393, 395; 394, identifier:gpu_out1; 395, identifier:gpu_in1; 396, expression_statement; 396, 397; 397, augmented_assignment:+=; 397, 398; 397, 399; 398, identifier:gpu_scale; 399, integer:1; 400, comment; 401, if_statement; 401, 402; 401, 403; 401, 406; 401, 421; 402, identifier:store_on_gpu; 403, block; 403, 404; 404, return_statement; 404, 405; 405, identifier:detail_coeffs; 406, elif_clause; 406, 407; 406, 408; 407, identifier:store_smoothed; 408, block; 408, 409; 409, return_statement; 409, 410; 410, expression_list; 410, 411; 410, 416; 411, call; 411, 412; 411, 415; 412, attribute; 412, 413; 412, 414; 413, identifier:detail_coeffs; 414, identifier:get; 415, argument_list; 416, call; 416, 417; 416, 420; 417, attribute; 417, 418; 417, 419; 418, identifier:gpu_in1; 419, identifier:get; 420, argument_list; 421, else_clause; 421, 422; 422, block; 422, 423; 423, return_statement; 423, 424; 424, call; 424, 425; 424, 428; 425, attribute; 425, 426; 425, 427; 426, identifier:detail_coeffs; 427, identifier:get; 428, argument_list
def gpu_iuwt_decomposition(in1, scale_count, scale_adjust, store_smoothed, store_on_gpu): """ This function calls the a trous algorithm code to decompose the input into its wavelet coefficients. This is the isotropic undecimated wavelet transform implemented for a GPU. INPUTS: in1 (no default): Array on which the decomposition is to be performed. scale_count (no default): Maximum scale to be considered. scale_adjust (no default): Adjustment to scale value if first scales are of no interest. store_smoothed (no default): Boolean specifier for whether the smoothed image is stored or not. store_on_gpu (no default): Boolean specifier for whether the decomposition is stored on the gpu or not. OUTPUTS: detail_coeffs Array containing the detail coefficients. C0 (optional): Array containing the smoothest version of the input. """ # The following simple kernel just allows for the construction of a 3D decomposition on the GPU. ker = SourceModule(""" __global__ void gpu_store_detail_coeffs(float *in1, float *in2, float* out1, int *scale, int *adjust) { const int len = gridDim.x*blockDim.x; const int i = (blockDim.x * blockIdx.x + threadIdx.x); const int j = (blockDim.y * blockIdx.y + threadIdx.y)*len; const int k = (blockDim.z * blockIdx.z + threadIdx.z)*(len*len); const int tid2 = i + j; const int tid3 = i + j + k; if ((blockIdx.z + adjust[0])==scale[0]) { out1[tid3] = in1[tid2] - in2[tid2]; } } """) wavelet_filter = (1./16)*np.array([1,4,6,4,1], dtype=np.float32) # Filter-bank for use in the a trous algorithm. wavelet_filter = gpuarray.to_gpu_async(wavelet_filter) # Initialises an empty array to store the detail coefficients. detail_coeffs = gpuarray.empty([scale_count-scale_adjust, in1.shape[0], in1.shape[1]], np.float32) # Determines whether the array is already on the GPU or not. If not, moves it to the GPU. try: gpu_in1 = gpuarray.to_gpu_async(in1.astype(np.float32)) except: gpu_in1 = in1 # Sets up some working arrays on the GPU to prevent memory transfers. gpu_tmp = gpuarray.empty_like(gpu_in1) gpu_out1 = gpuarray.empty_like(gpu_in1) gpu_out2 = gpuarray.empty_like(gpu_in1) # Sets up some parameters required by the algorithm on the GPU. gpu_scale = gpuarray.zeros([1], np.int32) gpu_adjust = gpuarray.zeros([1], np.int32) gpu_adjust += scale_adjust # Fetches the a trous kernels and sets up the unique storing kernel. gpu_a_trous_row_kernel, gpu_a_trous_col_kernel = gpu_a_trous() gpu_store_detail_coeffs = ker.get_function("gpu_store_detail_coeffs") grid_rows = int(in1.shape[0]//32) grid_cols = int(in1.shape[1]//32) # The following loop, which iterates up to scale_adjust, applies the a trous algorithm to the scales which are # considered insignificant. This is important as each set of wavelet coefficients depends on the last smoothed # version of the input. if scale_adjust>0: for i in range(0, scale_adjust): gpu_a_trous_row_kernel(gpu_in1, gpu_tmp, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) gpu_a_trous_col_kernel(gpu_tmp, gpu_out1, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) gpu_in1, gpu_out1 = gpu_out1, gpu_in1 gpu_scale += 1 # The meat of the algorithm - two sequential applications fo the a trous followed by determination and storing of # the detail coefficients. C0 is reassigned the value of C on each loop - C0 is always the smoothest version of the # input image. for i in range(scale_adjust, scale_count): gpu_a_trous_row_kernel(gpu_in1, gpu_tmp, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) gpu_a_trous_col_kernel(gpu_tmp, gpu_out1, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) # Approximation coefficients. gpu_a_trous_row_kernel(gpu_out1, gpu_tmp, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) gpu_a_trous_col_kernel(gpu_tmp, gpu_out2, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) # Approximation coefficients. gpu_store_detail_coeffs(gpu_in1, gpu_out2, detail_coeffs, gpu_scale, gpu_adjust, block=(32,32,1), grid=(grid_cols, grid_rows, int(scale_count))) # Detail coefficients. gpu_in1, gpu_out1 = gpu_out1, gpu_in1 gpu_scale += 1 # Return values depend on mode. NOTE: store_smoothed does not work if the result stays on the gpu. if store_on_gpu: return detail_coeffs elif store_smoothed: return detail_coeffs.get(), gpu_in1.get() else: return detail_coeffs.get()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:gpu_iuwt_recomposition; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:in1; 5, identifier:scale_adjust; 6, identifier:store_on_gpu; 7, identifier:smoothed_array; 8, block; 8, 9; 8, 11; 8, 35; 8, 36; 8, 45; 8, 46; 8, 47; 8, 57; 8, 102; 8, 103; 8, 127; 8, 128; 8, 137; 8, 138; 8, 151; 8, 157; 8, 158; 8, 166; 8, 179; 8, 192; 8, 193; 8, 194; 8, 195; 8, 270; 8, 330; 8, 331; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:wavelet_filter; 14, binary_operator:*; 14, 15; 14, 19; 15, parenthesized_expression; 15, 16; 16, binary_operator:/; 16, 17; 16, 18; 17, float:1.; 18, integer:16; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:np; 22, identifier:array; 23, argument_list; 23, 24; 23, 30; 24, list:[1,4,6,4,1]; 24, 25; 24, 26; 24, 27; 24, 28; 24, 29; 25, integer:1; 26, integer:4; 27, integer:6; 28, integer:4; 29, integer:1; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:dtype; 32, attribute; 32, 33; 32, 34; 33, identifier:np; 34, identifier:float32; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:wavelet_filter; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:gpuarray; 42, identifier:to_gpu_async; 43, argument_list; 43, 44; 44, identifier:wavelet_filter; 45, comment; 46, comment; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:max_scale; 50, binary_operator:+; 50, 51; 50, 56; 51, subscript; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:in1; 54, identifier:shape; 55, integer:0; 56, identifier:scale_adjust; 57, if_statement; 57, 58; 57, 61; 57, 84; 58, comparison_operator:is; 58, 59; 58, 60; 59, identifier:smoothed_array; 60, None; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:recomposition; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:gpuarray; 68, identifier:zeros; 69, argument_list; 69, 70; 69, 81; 70, list:[in1.shape[1], in1.shape[2]]; 70, 71; 70, 76; 71, subscript; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:in1; 74, identifier:shape; 75, integer:1; 76, subscript; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:in1; 79, identifier:shape; 80, integer:2; 81, attribute; 81, 82; 81, 83; 82, identifier:np; 83, identifier:float32; 84, else_clause; 84, 85; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:recomposition; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:gpuarray; 92, identifier:to_gpu; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:smoothed_array; 97, identifier:astype; 98, argument_list; 98, 99; 99, attribute; 99, 100; 99, 101; 100, identifier:np; 101, identifier:float32; 102, comment; 103, try_statement; 103, 104; 103, 121; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:gpu_in1; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:gpuarray; 111, identifier:to_gpu_async; 112, argument_list; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:in1; 116, identifier:astype; 117, argument_list; 117, 118; 118, attribute; 118, 119; 118, 120; 119, identifier:np; 120, identifier:float32; 121, except_clause; 121, 122; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:gpu_in1; 126, identifier:in1; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:gpu_tmp; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:gpuarray; 134, identifier:empty_like; 135, argument_list; 135, 136; 136, identifier:recomposition; 137, comment; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:gpu_scale; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:gpuarray; 144, identifier:zeros; 145, argument_list; 145, 146; 145, 148; 146, list:[1]; 146, 147; 147, integer:1; 148, attribute; 148, 149; 148, 150; 149, identifier:np; 150, identifier:int32; 151, expression_statement; 151, 152; 152, augmented_assignment:+=; 152, 153; 152, 154; 153, identifier:gpu_scale; 154, binary_operator:-; 154, 155; 154, 156; 155, identifier:max_scale; 156, integer:1; 157, comment; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, pattern_list; 160, 161; 160, 162; 161, identifier:gpu_a_trous_row_kernel; 162, identifier:gpu_a_trous_col_kernel; 163, call; 163, 164; 163, 165; 164, identifier:gpu_a_trous; 165, argument_list; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:grid_rows; 169, call; 169, 170; 169, 171; 170, identifier:int; 171, argument_list; 171, 172; 172, binary_operator://; 172, 173; 172, 178; 173, subscript; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:in1; 176, identifier:shape; 177, integer:1; 178, integer:32; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:grid_cols; 182, call; 182, 183; 182, 184; 183, identifier:int; 184, argument_list; 184, 185; 185, binary_operator://; 185, 186; 185, 191; 186, subscript; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:in1; 189, identifier:shape; 190, integer:2; 191, integer:32; 192, comment; 193, comment; 194, comment; 195, for_statement; 195, 196; 195, 197; 195, 208; 196, identifier:i; 197, call; 197, 198; 197, 199; 198, identifier:range; 199, argument_list; 199, 200; 199, 203; 199, 206; 200, binary_operator:-; 200, 201; 200, 202; 201, identifier:max_scale; 202, integer:1; 203, binary_operator:-; 203, 204; 203, 205; 204, identifier:scale_adjust; 205, integer:1; 206, unary_operator:-; 206, 207; 207, integer:1; 208, block; 208, 209; 208, 228; 208, 247; 208, 266; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 212; 211, identifier:gpu_a_trous_row_kernel; 212, argument_list; 212, 213; 212, 214; 212, 215; 212, 216; 212, 217; 212, 223; 213, identifier:recomposition; 214, identifier:gpu_tmp; 215, identifier:wavelet_filter; 216, identifier:gpu_scale; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:block; 219, tuple; 219, 220; 219, 221; 219, 222; 220, integer:32; 221, integer:32; 222, integer:1; 223, keyword_argument; 223, 224; 223, 225; 224, identifier:grid; 225, tuple; 225, 226; 225, 227; 226, identifier:grid_cols; 227, identifier:grid_rows; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 231; 230, identifier:gpu_a_trous_col_kernel; 231, argument_list; 231, 232; 231, 233; 231, 234; 231, 235; 231, 236; 231, 242; 232, identifier:gpu_tmp; 233, identifier:recomposition; 234, identifier:wavelet_filter; 235, identifier:gpu_scale; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:block; 238, tuple; 238, 239; 238, 240; 238, 241; 239, integer:32; 240, integer:32; 241, integer:1; 242, keyword_argument; 242, 243; 242, 244; 243, identifier:grid; 244, tuple; 244, 245; 244, 246; 245, identifier:grid_cols; 246, identifier:grid_rows; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:recomposition; 250, binary_operator:+; 250, 251; 250, 257; 251, subscript; 251, 252; 251, 253; 251, 255; 252, identifier:recomposition; 253, slice; 253, 254; 254, colon; 255, slice; 255, 256; 256, colon; 257, subscript; 257, 258; 257, 259; 257, 262; 257, 264; 258, identifier:gpu_in1; 259, binary_operator:-; 259, 260; 259, 261; 260, identifier:i; 261, identifier:scale_adjust; 262, slice; 262, 263; 263, colon; 264, slice; 264, 265; 265, colon; 266, expression_statement; 266, 267; 267, augmented_assignment:-=; 267, 268; 267, 269; 268, identifier:gpu_scale; 269, integer:1; 270, if_statement; 270, 271; 270, 274; 271, comparison_operator:>; 271, 272; 271, 273; 272, identifier:scale_adjust; 273, integer:0; 274, block; 274, 275; 275, for_statement; 275, 276; 275, 277; 275, 287; 276, identifier:i; 277, call; 277, 278; 277, 279; 278, identifier:range; 279, argument_list; 279, 280; 279, 283; 279, 285; 280, binary_operator:-; 280, 281; 280, 282; 281, identifier:scale_adjust; 282, integer:1; 283, unary_operator:-; 283, 284; 284, integer:1; 285, unary_operator:-; 285, 286; 286, integer:1; 287, block; 287, 288; 287, 307; 287, 326; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 291; 290, identifier:gpu_a_trous_row_kernel; 291, argument_list; 291, 292; 291, 293; 291, 294; 291, 295; 291, 296; 291, 302; 292, identifier:recomposition; 293, identifier:gpu_tmp; 294, identifier:wavelet_filter; 295, identifier:gpu_scale; 296, keyword_argument; 296, 297; 296, 298; 297, identifier:block; 298, tuple; 298, 299; 298, 300; 298, 301; 299, integer:32; 300, integer:32; 301, integer:1; 302, keyword_argument; 302, 303; 302, 304; 303, identifier:grid; 304, tuple; 304, 305; 304, 306; 305, identifier:grid_cols; 306, identifier:grid_rows; 307, expression_statement; 307, 308; 308, call; 308, 309; 308, 310; 309, identifier:gpu_a_trous_col_kernel; 310, argument_list; 310, 311; 310, 312; 310, 313; 310, 314; 310, 315; 310, 321; 311, identifier:gpu_tmp; 312, identifier:recomposition; 313, identifier:wavelet_filter; 314, identifier:gpu_scale; 315, keyword_argument; 315, 316; 315, 317; 316, identifier:block; 317, tuple; 317, 318; 317, 319; 317, 320; 318, integer:32; 319, integer:32; 320, integer:1; 321, keyword_argument; 321, 322; 321, 323; 322, identifier:grid; 323, tuple; 323, 324; 323, 325; 324, identifier:grid_cols; 325, identifier:grid_rows; 326, expression_statement; 326, 327; 327, augmented_assignment:-=; 327, 328; 327, 329; 328, identifier:gpu_scale; 329, integer:1; 330, comment; 331, if_statement; 331, 332; 331, 333; 331, 336; 332, identifier:store_on_gpu; 333, block; 333, 334; 334, return_statement; 334, 335; 335, identifier:recomposition; 336, else_clause; 336, 337; 337, block; 337, 338; 338, return_statement; 338, 339; 339, call; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:recomposition; 342, identifier:get; 343, argument_list
def gpu_iuwt_recomposition(in1, scale_adjust, store_on_gpu, smoothed_array): """ This function calls the a trous algorithm code to recompose the input into a single array. This is the implementation of the isotropic undecimated wavelet transform recomposition for a GPU. INPUTS: in1 (no default): Array containing wavelet coefficients. scale_adjust (no default): Indicates the number of omitted array pages. store_on_gpu (no default): Boolean specifier for whether the decomposition is stored on the gpu or not. OUTPUTS: recomposiiton Array containing the reconstructed array. """ wavelet_filter = (1./16)*np.array([1,4,6,4,1], dtype=np.float32) # Filter-bank for use in the a trous algorithm. wavelet_filter = gpuarray.to_gpu_async(wavelet_filter) # Determines scale with adjustment and creates a zero array on the GPU to store the output,unless smoothed_array # is given. max_scale = in1.shape[0] + scale_adjust if smoothed_array is None: recomposition = gpuarray.zeros([in1.shape[1], in1.shape[2]], np.float32) else: recomposition = gpuarray.to_gpu(smoothed_array.astype(np.float32)) # Determines whether the array is already on the GPU or not. If not, moves it to the GPU. try: gpu_in1 = gpuarray.to_gpu_async(in1.astype(np.float32)) except: gpu_in1 = in1 # Creates a working array on the GPU. gpu_tmp = gpuarray.empty_like(recomposition) # Creates and fills an array with the appropriate scale value. gpu_scale = gpuarray.zeros([1], np.int32) gpu_scale += max_scale-1 # Fetches the a trous kernels. gpu_a_trous_row_kernel, gpu_a_trous_col_kernel = gpu_a_trous() grid_rows = int(in1.shape[1]//32) grid_cols = int(in1.shape[2]//32) # The following loops call the a trous algorithm code to recompose the input. The first loop assumes that there are # non-zero wavelet coefficients at scales above scale_adjust, while the second loop completes the recomposition # on the scales less than scale_adjust. for i in range(max_scale-1, scale_adjust-1, -1): gpu_a_trous_row_kernel(recomposition, gpu_tmp, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) gpu_a_trous_col_kernel(gpu_tmp, recomposition, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) recomposition = recomposition[:,:] + gpu_in1[i-scale_adjust,:,:] gpu_scale -= 1 if scale_adjust>0: for i in range(scale_adjust-1, -1, -1): gpu_a_trous_row_kernel(recomposition, gpu_tmp, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) gpu_a_trous_col_kernel(gpu_tmp, recomposition, wavelet_filter, gpu_scale, block=(32,32,1), grid=(grid_cols, grid_rows)) gpu_scale -= 1 # Return values depend on mode. if store_on_gpu: return recomposition else: return recomposition.get()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:from_config; 3, parameters; 3, 4; 4, identifier:config; 5, block; 5, 6; 5, 8; 5, 12; 5, 20; 5, 174; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:matrix; 11, dictionary; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:variables; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:config; 18, identifier:keys; 19, argument_list; 20, for_statement; 20, 21; 20, 22; 20, 31; 21, identifier:entries; 22, call; 22, 23; 22, 24; 23, identifier:product; 24, argument_list; 24, 25; 25, list_splat; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:config; 29, identifier:values; 30, argument_list; 31, block; 31, 32; 31, 43; 31, 47; 31, 110; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:combination; 35, call; 35, 36; 35, 37; 36, identifier:dict; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:zip; 40, argument_list; 40, 41; 40, 42; 41, identifier:variables; 42, identifier:entries; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:include; 46, True; 47, for_statement; 47, 48; 47, 49; 47, 54; 48, identifier:value; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:combination; 52, identifier:values; 53, argument_list; 54, block; 54, 55; 55, for_statement; 55, 56; 55, 57; 55, 60; 56, identifier:reducer; 57, attribute; 57, 58; 57, 59; 58, identifier:value; 59, identifier:reducers; 60, block; 60, 61; 60, 97; 61, if_statement; 61, 62; 61, 67; 61, 79; 62, comparison_operator:==; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:reducer; 65, identifier:pattern; 66, string:'-'; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:match; 71, not_operator; 71, 72; 72, attribute; 72, 73; 72, 78; 73, subscript; 73, 74; 73, 75; 74, identifier:combination; 75, attribute; 75, 76; 75, 77; 76, identifier:reducer; 77, identifier:variable; 78, identifier:value; 79, else_clause; 79, 80; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:match; 84, call; 84, 85; 84, 86; 85, identifier:fnmatch; 86, argument_list; 86, 87; 86, 94; 87, attribute; 87, 88; 87, 93; 88, subscript; 88, 89; 88, 90; 89, identifier:combination; 90, attribute; 90, 91; 90, 92; 91, identifier:reducer; 92, identifier:variable; 93, identifier:value; 94, attribute; 94, 95; 94, 96; 95, identifier:reducer; 96, identifier:pattern; 97, if_statement; 97, 98; 97, 105; 98, conditional_expression:if; 98, 99; 98, 100; 98, 103; 99, identifier:match; 100, attribute; 100, 101; 100, 102; 101, identifier:reducer; 102, identifier:is_exclude; 103, not_operator; 103, 104; 104, identifier:match; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:include; 109, False; 110, if_statement; 110, 111; 110, 112; 111, identifier:include; 112, block; 112, 113; 112, 131; 112, 148; 112, 168; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:key; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, string:'-'; 119, identifier:join; 120, generator_expression; 120, 121; 120, 124; 120, 127; 121, attribute; 121, 122; 121, 123; 122, identifier:entry; 123, identifier:alias; 124, for_in_clause; 124, 125; 124, 126; 125, identifier:entry; 126, identifier:entries; 127, if_clause; 127, 128; 128, attribute; 128, 129; 128, 130; 129, identifier:entry; 130, identifier:alias; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:data; 134, call; 134, 135; 134, 136; 135, identifier:dict; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:zip; 139, argument_list; 139, 140; 139, 141; 140, identifier:variables; 141, generator_expression; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:entry; 144, identifier:value; 145, for_in_clause; 145, 146; 145, 147; 146, identifier:entry; 147, identifier:entries; 148, if_statement; 148, 149; 148, 158; 149, boolean_operator:and; 149, 150; 149, 153; 150, comparison_operator:in; 150, 151; 150, 152; 151, identifier:key; 152, identifier:matrix; 153, comparison_operator:!=; 153, 154; 153, 155; 154, identifier:data; 155, subscript; 155, 156; 155, 157; 156, identifier:matrix; 157, identifier:key; 158, block; 158, 159; 159, raise_statement; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:DuplicateEnvironment; 162, argument_list; 162, 163; 162, 164; 162, 165; 163, identifier:key; 164, identifier:data; 165, subscript; 165, 166; 165, 167; 166, identifier:matrix; 167, identifier:key; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:matrix; 172, identifier:key; 173, identifier:data; 174, return_statement; 174, 175; 175, identifier:matrix
def from_config(config): """ Generate a matrix from a configuration dictionary. """ matrix = {} variables = config.keys() for entries in product(*config.values()): combination = dict(zip(variables, entries)) include = True for value in combination.values(): for reducer in value.reducers: if reducer.pattern == '-': match = not combination[reducer.variable].value else: match = fnmatch(combination[reducer.variable].value, reducer.pattern) if match if reducer.is_exclude else not match: include = False if include: key = '-'.join(entry.alias for entry in entries if entry.alias) data = dict( zip(variables, (entry.value for entry in entries)) ) if key in matrix and data != matrix[key]: raise DuplicateEnvironment(key, data, matrix[key]) matrix[key] = data return matrix
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 71; 2, function_name:moresane_by_scale; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 3, 32; 3, 35; 3, 38; 3, 41; 3, 44; 3, 47; 3, 50; 3, 53; 3, 56; 3, 59; 3, 62; 3, 65; 3, 68; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:start_scale; 7, integer:1; 8, default_parameter; 8, 9; 8, 10; 9, identifier:stop_scale; 10, integer:20; 11, default_parameter; 11, 12; 11, 13; 12, identifier:subregion; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sigma_level; 16, integer:4; 17, default_parameter; 17, 18; 17, 19; 18, identifier:loop_gain; 19, float:0.1; 20, default_parameter; 20, 21; 20, 22; 21, identifier:tolerance; 22, float:0.75; 23, default_parameter; 23, 24; 23, 25; 24, identifier:accuracy; 25, float:1e-6; 26, default_parameter; 26, 27; 26, 28; 27, identifier:major_loop_miter; 28, integer:100; 29, default_parameter; 29, 30; 29, 31; 30, identifier:minor_loop_miter; 31, integer:30; 32, default_parameter; 32, 33; 32, 34; 33, identifier:all_on_gpu; 34, False; 35, default_parameter; 35, 36; 35, 37; 36, identifier:decom_mode; 37, string:"ser"; 38, default_parameter; 38, 39; 38, 40; 39, identifier:core_count; 40, integer:1; 41, default_parameter; 41, 42; 41, 43; 42, identifier:conv_device; 43, string:'cpu'; 44, default_parameter; 44, 45; 44, 46; 45, identifier:conv_mode; 46, string:'linear'; 47, default_parameter; 47, 48; 47, 49; 48, identifier:extraction_mode; 49, string:'cpu'; 50, default_parameter; 50, 51; 50, 52; 51, identifier:enforce_positivity; 52, False; 53, default_parameter; 53, 54; 53, 55; 54, identifier:edge_suppression; 55, False; 56, default_parameter; 56, 57; 56, 58; 57, identifier:edge_offset; 58, integer:0; 59, default_parameter; 59, 60; 59, 61; 60, identifier:flux_threshold; 61, integer:0; 62, default_parameter; 62, 63; 62, 64; 63, identifier:neg_comp; 64, False; 65, default_parameter; 65, 66; 65, 67; 66, identifier:edge_excl; 67, integer:0; 68, default_parameter; 68, 69; 68, 70; 69, identifier:int_excl; 70, integer:0; 71, block; 71, 72; 71, 74; 71, 75; 71, 81; 71, 85; 71, 227; 71, 228; 71, 234; 72, expression_statement; 72, 73; 73, comment; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:dirty_data; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:dirty_data; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:scale_count; 84, identifier:start_scale; 85, while_statement; 85, 86; 85, 91; 86, not_operator; 86, 87; 87, parenthesized_expression; 87, 88; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:complete; 91, block; 91, 92; 91, 104; 91, 173; 91, 181; 91, 185; 91, 213; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:logger; 96, identifier:info; 97, argument_list; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, string:"MORESANE at scale {}"; 101, identifier:format; 102, argument_list; 102, 103; 103, identifier:scale_count; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:moresane; 109, argument_list; 109, 110; 109, 113; 109, 116; 109, 119; 109, 122; 109, 125; 109, 128; 109, 131; 109, 134; 109, 137; 109, 140; 109, 143; 109, 146; 109, 149; 109, 152; 109, 155; 109, 158; 109, 161; 109, 164; 109, 167; 109, 170; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:subregion; 112, identifier:subregion; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:scale_count; 115, identifier:scale_count; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:sigma_level; 118, identifier:sigma_level; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:loop_gain; 121, identifier:loop_gain; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:tolerance; 124, identifier:tolerance; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:accuracy; 127, identifier:accuracy; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:major_loop_miter; 130, identifier:major_loop_miter; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:minor_loop_miter; 133, identifier:minor_loop_miter; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:all_on_gpu; 136, identifier:all_on_gpu; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:decom_mode; 139, identifier:decom_mode; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:core_count; 142, identifier:core_count; 143, keyword_argument; 143, 144; 143, 145; 144, identifier:conv_device; 145, identifier:conv_device; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:conv_mode; 148, identifier:conv_mode; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:extraction_mode; 151, identifier:extraction_mode; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:enforce_positivity; 154, identifier:enforce_positivity; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:edge_suppression; 157, identifier:edge_suppression; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:edge_offset; 160, identifier:edge_offset; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:flux_threshold; 163, identifier:flux_threshold; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:neg_comp; 166, identifier:neg_comp; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:edge_excl; 169, identifier:edge_excl; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:int_excl; 172, identifier:int_excl; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:dirty_data; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:residual; 181, expression_statement; 181, 182; 182, augmented_assignment:+=; 182, 183; 182, 184; 183, identifier:scale_count; 184, integer:1; 185, if_statement; 185, 186; 185, 204; 186, parenthesized_expression; 186, 187; 187, comparison_operator:>; 187, 188; 187, 189; 188, identifier:scale_count; 189, binary_operator:-; 189, 190; 189, 203; 190, parenthesized_expression; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:np; 194, identifier:log2; 195, argument_list; 195, 196; 196, subscript; 196, 197; 196, 202; 197, attribute; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:dirty_data; 201, identifier:shape; 202, integer:0; 203, integer:1; 204, block; 204, 205; 204, 212; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:logger; 209, identifier:info; 210, argument_list; 210, 211; 211, string:"Maximum scale reached - finished."; 212, break_statement; 213, if_statement; 213, 214; 213, 218; 214, parenthesized_expression; 214, 215; 215, comparison_operator:>; 215, 216; 215, 217; 216, identifier:scale_count; 217, identifier:stop_scale; 218, block; 218, 219; 218, 226; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:logger; 223, identifier:info; 224, argument_list; 224, 225; 225, string:"Maximum scale reached - finished."; 226, break_statement; 227, comment; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:dirty_data; 233, identifier:dirty_data; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:complete; 239, False
def moresane_by_scale(self, start_scale=1, stop_scale=20, subregion=None, sigma_level=4, loop_gain=0.1, tolerance=0.75, accuracy=1e-6, major_loop_miter=100, minor_loop_miter=30, all_on_gpu=False, decom_mode="ser", core_count=1, conv_device='cpu', conv_mode='linear', extraction_mode='cpu', enforce_positivity=False, edge_suppression=False, edge_offset=0, flux_threshold=0, neg_comp=False, edge_excl=0, int_excl=0): """ Extension of the MORESANE algorithm. This takes a scale-by-scale approach, attempting to remove all sources at the lower scales before moving onto the higher ones. At each step the algorithm may return to previous scales to remove the sources uncovered by the deconvolution. INPUTS: start_scale (default=1) The first scale which is to be considered. stop_scale (default=20) The maximum scale which is to be considered. Optional. subregion (default=None): Size, in pixels, of the central region to be analyzed and deconvolved. sigma_level (default=4) Number of sigma at which thresholding is to be performed. loop_gain (default=0.1): Loop gain for the deconvolution. tolerance (default=0.75): Tolerance level for object extraction. Significant objects contain wavelet coefficients greater than the tolerance multiplied by the maximum wavelet coefficient in the scale under consideration. accuracy (default=1e-6): Threshold on the standard deviation of the residual noise. Exit main loop when this threshold is reached. major_loop_miter (default=100): Maximum number of iterations allowed in the major loop. Exit condition. minor_loop_miter (default=30): Maximum number of iterations allowed in the minor loop. Serves as an exit condition when the SNR does not reach a maximum. all_on_gpu (default=False): Boolean specifier to toggle all gpu modes on. decom_mode (default='ser'): Specifier for decomposition mode - serial, multiprocessing, or gpu. core_count (default=1): In the event that multiprocessing, specifies the number of cores. conv_device (default='cpu'): Specifier for device to be used - cpu or gpu. conv_mode (default='linear'): Specifier for convolution mode - linear or circular. extraction_mode (default='cpu'): Specifier for mode to be used - cpu or gpu. enforce_positivity (default=False): Boolean specifier for whether or not a model must be strictly positive. edge_suppression (default=False): Boolean specifier for whether or not the edges are to be suprressed. edge_offset (default=0): Numeric value for an additional user-specified number of edge pixels to be ignored. This is added to the minimum suppression. OUTPUTS: self.model (no default): Model extracted by the algorithm. self.residual (no default): Residual signal after deconvolution. """ # The following preserves the dirty image as it will be changed on every iteration. dirty_data = self.dirty_data scale_count = start_scale while not (self.complete): logger.info("MORESANE at scale {}".format(scale_count)) self.moresane(subregion=subregion, scale_count=scale_count, sigma_level=sigma_level, loop_gain=loop_gain, tolerance=tolerance, accuracy=accuracy, major_loop_miter=major_loop_miter, minor_loop_miter=minor_loop_miter, all_on_gpu=all_on_gpu, decom_mode=decom_mode, core_count=core_count, conv_device=conv_device, conv_mode=conv_mode, extraction_mode=extraction_mode, enforce_positivity=enforce_positivity, edge_suppression=edge_suppression, edge_offset=edge_offset, flux_threshold=flux_threshold, neg_comp=neg_comp, edge_excl=edge_excl, int_excl=int_excl) self.dirty_data = self.residual scale_count += 1 if (scale_count>(np.log2(self.dirty_data.shape[0]))-1): logger.info("Maximum scale reached - finished.") break if (scale_count>stop_scale): logger.info("Maximum scale reached - finished.") break # Restores the original dirty image. self.dirty_data = dirty_data self.complete = False
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:load_or_create; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:cls; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filename; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:no_input; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:create_new; 13, False; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kwargs; 16, block; 16, 17; 16, 19; 16, 27; 16, 37; 16, 47; 16, 55; 16, 69; 16, 87; 16, 121; 16, 160; 16, 195; 16, 240; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:parser; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:argparse; 25, identifier:ArgumentParser; 26, argument_list; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:parser; 31, identifier:add_argument; 32, argument_list; 32, 33; 32, 34; 33, string:'--no_input'; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:action; 36, string:'store_true'; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:parser; 41, identifier:add_argument; 42, argument_list; 42, 43; 42, 44; 43, string:'--create_new'; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:action; 46, string:'store_true'; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:args; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:parser; 53, identifier:parse_args; 54, argument_list; 55, if_statement; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:args; 58, identifier:no_input; 59, block; 59, 60; 59, 65; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:print; 63, argument_list; 63, 64; 64, string:'Parameter --no_input was given'; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:no_input; 68, True; 69, if_statement; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:args; 72, identifier:create_new; 73, block; 73, 74; 73, 79; 73, 83; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:print; 77, argument_list; 77, 78; 78, string:'Parameter --create_new was given'; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:create_new; 82, True; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:no_input; 86, True; 87, function_definition; 87, 88; 87, 89; 87, 90; 88, function_name:savefile_more_recent; 89, parameters; 90, block; 90, 91; 90, 102; 90, 117; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:time_savefile; 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:getmtime; 100, argument_list; 100, 101; 101, identifier:filename; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:time_program; 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:getmtime; 111, argument_list; 111, 112; 112, subscript; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:sys; 115, identifier:argv; 116, integer:0; 117, return_statement; 117, 118; 118, comparison_operator:>; 118, 119; 118, 120; 119, identifier:time_savefile; 120, identifier:time_program; 121, function_definition; 121, 122; 121, 123; 121, 124; 122, function_name:load_pickle; 123, parameters; 124, block; 124, 125; 124, 148; 124, 158; 125, with_statement; 125, 126; 125, 136; 126, with_clause; 126, 127; 127, with_item; 127, 128; 128, as_pattern; 128, 129; 128, 134; 129, call; 129, 130; 129, 131; 130, identifier:open; 131, argument_list; 131, 132; 131, 133; 132, identifier:filename; 133, string:'rb'; 134, as_pattern_target; 134, 135; 135, identifier:of; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, pattern_list; 139, 140; 139, 141; 140, identifier:statefile_version; 141, identifier:data; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:pickle; 145, identifier:load; 146, argument_list; 146, 147; 147, identifier:of; 148, if_statement; 148, 149; 148, 152; 149, comparison_operator:!=; 149, 150; 149, 151; 150, identifier:statefile_version; 151, identifier:STATEFILE_VERSION; 152, block; 152, 153; 153, raise_statement; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:RuntimeError; 156, argument_list; 156, 157; 157, string:f'Wrong statefile version, please remove state file {filename}'; 158, return_statement; 158, 159; 159, identifier:data; 160, function_definition; 160, 161; 160, 162; 160, 163; 161, function_name:load; 162, parameters; 163, block; 163, 164; 163, 171; 163, 179; 163, 193; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:print; 167, argument_list; 167, 168; 168, binary_operator:%; 168, 169; 168, 170; 169, string:'Loading %s'; 170, identifier:filename; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 176; 173, pattern_list; 173, 174; 173, 175; 174, identifier:obj_list; 175, identifier:config; 176, call; 176, 177; 176, 178; 177, identifier:load_pickle; 178, argument_list; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:system; 182, call; 182, 183; 182, 184; 183, identifier:System; 184, argument_list; 184, 185; 184, 188; 184, 191; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:load_state; 187, identifier:obj_list; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:filename; 190, identifier:filename; 191, dictionary_splat; 191, 192; 192, identifier:kwargs; 193, return_statement; 193, 194; 194, identifier:system; 195, function_definition; 195, 196; 195, 197; 195, 198; 196, function_name:create; 197, parameters; 198, block; 198, 199; 198, 204; 198, 208; 198, 228; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:print; 202, argument_list; 202, 203; 203, string:'Creating new system'; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:config; 207, None; 208, if_statement; 208, 209; 208, 210; 209, identifier:filename; 210, block; 210, 211; 211, try_statement; 211, 212; 211, 221; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 218; 215, pattern_list; 215, 216; 215, 217; 216, identifier:obj_list; 217, identifier:config; 218, call; 218, 219; 218, 220; 219, identifier:load_pickle; 220, argument_list; 221, except_clause; 221, 222; 221, 223; 222, identifier:FileNotFoundError; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:config; 227, None; 228, return_statement; 228, 229; 229, call; 229, 230; 229, 231; 230, identifier:cls; 231, argument_list; 231, 232; 231, 235; 231, 238; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:filename; 234, identifier:filename; 235, keyword_argument; 235, 236; 235, 237; 236, identifier:load_config; 237, identifier:config; 238, dictionary_splat; 238, 239; 239, identifier:kwargs; 240, if_statement; 240, 241; 240, 251; 240, 306; 241, boolean_operator:and; 241, 242; 241, 243; 242, identifier:filename; 243, call; 243, 244; 243, 249; 244, attribute; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:os; 247, identifier:path; 248, identifier:isfile; 249, argument_list; 249, 250; 250, identifier:filename; 251, block; 251, 252; 252, if_statement; 252, 253; 252, 259; 252, 264; 253, boolean_operator:and; 253, 254; 253, 257; 254, call; 254, 255; 254, 256; 255, identifier:savefile_more_recent; 256, argument_list; 257, not_operator; 257, 258; 258, identifier:create_new; 259, block; 259, 260; 260, return_statement; 260, 261; 261, call; 261, 262; 261, 263; 262, identifier:load; 263, argument_list; 264, else_clause; 264, 265; 265, block; 265, 266; 265, 278; 266, if_statement; 266, 267; 266, 268; 267, identifier:no_input; 268, block; 268, 269; 268, 274; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:print; 272, argument_list; 272, 273; 273, string:'Program file more recent. Loading that instead.'; 274, return_statement; 274, 275; 275, call; 275, 276; 275, 277; 276, identifier:create; 277, argument_list; 278, while_statement; 278, 279; 278, 280; 279, True; 280, block; 280, 281; 280, 288; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:answer; 284, call; 284, 285; 284, 286; 285, identifier:input; 286, argument_list; 286, 287; 287, string:'Program file more recent. Do you want to load it? (y/n) '; 288, if_statement; 288, 289; 288, 292; 288, 297; 289, comparison_operator:==; 289, 290; 289, 291; 290, identifier:answer; 291, string:'y'; 292, block; 292, 293; 293, return_statement; 293, 294; 294, call; 294, 295; 294, 296; 295, identifier:create; 296, argument_list; 297, elif_clause; 297, 298; 297, 301; 298, comparison_operator:==; 298, 299; 298, 300; 299, identifier:answer; 300, string:'n'; 301, block; 301, 302; 302, return_statement; 302, 303; 303, call; 303, 304; 303, 305; 304, identifier:load; 305, argument_list; 306, else_clause; 306, 307; 307, block; 307, 308; 308, return_statement; 308, 309; 309, call; 309, 310; 309, 311; 310, identifier:create; 311, argument_list
def load_or_create(cls, filename=None, no_input=False, create_new=False, **kwargs): """ Load system from a dump, if dump file exists, or create a new system if it does not exist. """ parser = argparse.ArgumentParser() parser.add_argument('--no_input', action='store_true') parser.add_argument('--create_new', action='store_true') args = parser.parse_args() if args.no_input: print('Parameter --no_input was given') no_input = True if args.create_new: print('Parameter --create_new was given') create_new = True no_input = True def savefile_more_recent(): time_savefile = os.path.getmtime(filename) time_program = os.path.getmtime(sys.argv[0]) return time_savefile > time_program def load_pickle(): with open(filename, 'rb') as of: statefile_version, data = pickle.load(of) if statefile_version != STATEFILE_VERSION: raise RuntimeError(f'Wrong statefile version, please remove state file {filename}') return data def load(): print('Loading %s' % filename) obj_list, config = load_pickle() system = System(load_state=obj_list, filename=filename, **kwargs) return system def create(): print('Creating new system') config = None if filename: try: obj_list, config = load_pickle() except FileNotFoundError: config = None return cls(filename=filename, load_config=config, **kwargs) if filename and os.path.isfile(filename): if savefile_more_recent() and not create_new: return load() else: if no_input: print('Program file more recent. Loading that instead.') return create() while True: answer = input('Program file more recent. Do you want to load it? (y/n) ') if answer == 'y': return create() elif answer == 'n': return load() else: return create()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:cleanup; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 26; 5, 47; 5, 56; 5, 68; 5, 77; 5, 99; 5, 108; 5, 125; 5, 134; 5, 156; 5, 165; 5, 192; 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:pre_exit_trigger; 13, True; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:logger; 20, identifier:info; 21, argument_list; 21, 22; 21, 23; 22, string:"Shutting down %s, please wait a moment."; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:name; 26, for_statement; 26, 27; 26, 28; 26, 33; 27, identifier:t; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:threading; 31, identifier:enumerate; 32, argument_list; 33, block; 33, 34; 34, if_statement; 34, 35; 34, 40; 35, call; 35, 36; 35, 37; 36, identifier:isinstance; 37, argument_list; 37, 38; 37, 39; 38, identifier:t; 39, identifier:TimerClass; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:t; 45, identifier:cancel; 46, argument_list; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:logger; 53, identifier:debug; 54, argument_list; 54, 55; 55, string:'Timers cancelled'; 56, for_statement; 56, 57; 56, 58; 56, 61; 57, identifier:i; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:objects; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:i; 66, identifier:cleanup; 67, argument_list; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:logger; 74, identifier:debug; 75, argument_list; 75, 76; 76, string:'Sensors etc cleanups done'; 77, for_statement; 77, 78; 77, 79; 77, 92; 78, identifier:ser; 79, generator_expression; 79, 80; 79, 81; 79, 86; 80, identifier:i; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:i; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:services; 86, if_clause; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:isinstance; 89, argument_list; 89, 90; 89, 91; 90, identifier:i; 91, identifier:AbstractUserService; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:ser; 97, identifier:cleanup_system; 98, argument_list; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:logger; 105, identifier:debug; 106, argument_list; 106, 107; 107, string:'User services cleaned up'; 108, if_statement; 108, 109; 108, 116; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:worker_thread; 114, identifier:is_alive; 115, argument_list; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 124; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:worker_thread; 123, identifier:stop; 124, argument_list; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:logger; 131, identifier:debug; 132, argument_list; 132, 133; 133, string:'Worker thread really stopped'; 134, for_statement; 134, 135; 134, 136; 134, 149; 135, identifier:ser; 136, generator_expression; 136, 137; 136, 138; 136, 143; 137, identifier:i; 138, for_in_clause; 138, 139; 138, 140; 139, identifier:i; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:services; 143, if_clause; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:isinstance; 146, argument_list; 146, 147; 146, 148; 147, identifier:i; 148, identifier:AbstractSystemService; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:ser; 154, identifier:cleanup_system; 155, argument_list; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:logger; 162, identifier:debug; 163, argument_list; 163, 164; 164, string:'System services cleaned up'; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:threads; 168, call; 168, 169; 168, 170; 169, identifier:list; 170, generator_expression; 170, 171; 170, 174; 170, 181; 171, attribute; 171, 172; 171, 173; 172, identifier:t; 173, identifier:name; 174, for_in_clause; 174, 175; 174, 176; 175, identifier:t; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:threading; 179, identifier:enumerate; 180, argument_list; 181, if_clause; 181, 182; 182, boolean_operator:and; 182, 183; 182, 188; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:t; 186, identifier:is_alive; 187, argument_list; 188, not_operator; 188, 189; 189, attribute; 189, 190; 189, 191; 190, identifier:t; 191, identifier:daemon; 192, if_statement; 192, 193; 192, 194; 193, identifier:threads; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 202; 197, attribute; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:logger; 201, identifier:info; 202, argument_list; 202, 203; 202, 206; 203, concatenated_string; 203, 204; 203, 205; 204, string:'After cleanup, we have still the following threads '; 205, string:'running: %s'; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, string:', '; 209, identifier:join; 210, argument_list; 210, 211; 211, identifier:threads
def cleanup(self): """ Clean up before quitting """ self.pre_exit_trigger = True self.logger.info("Shutting down %s, please wait a moment.", self.name) for t in threading.enumerate(): if isinstance(t, TimerClass): t.cancel() self.logger.debug('Timers cancelled') for i in self.objects: i.cleanup() self.logger.debug('Sensors etc cleanups done') for ser in (i for i in self.services if isinstance(i, AbstractUserService)): ser.cleanup_system() self.logger.debug('User services cleaned up') if self.worker_thread.is_alive(): self.worker_thread.stop() self.logger.debug('Worker thread really stopped') for ser in (i for i in self.services if isinstance(i, AbstractSystemService)): ser.cleanup_system() self.logger.debug('System services cleaned up') threads = list(t.name for t in threading.enumerate() if t.is_alive() and not t.daemon) if threads: self.logger.info('After cleanup, we have still the following threads ' 'running: %s', ', '.join(threads))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:write_puml; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filename; 7, string:''; 8, block; 8, 9; 8, 11; 8, 44; 8, 65; 8, 72; 8, 79; 8, 105; 8, 112; 8, 335; 8, 342; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:get_type; 13, parameters; 13, 14; 14, identifier:o; 15, block; 15, 16; 15, 20; 15, 42; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:type; 19, string:'program'; 20, if_statement; 20, 21; 20, 26; 20, 31; 21, call; 21, 22; 21, 23; 22, identifier:isinstance; 23, argument_list; 23, 24; 23, 25; 24, identifier:o; 25, identifier:AbstractSensor; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:type; 30, string:'sensor'; 31, elif_clause; 31, 32; 31, 37; 32, call; 32, 33; 32, 34; 33, identifier:isinstance; 34, argument_list; 34, 35; 34, 36; 35, identifier:o; 36, identifier:AbstractActuator; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:type; 41, string:'actuator'; 42, return_statement; 42, 43; 43, identifier:type; 44, if_statement; 44, 45; 44, 46; 44, 55; 45, identifier:filename; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:s; 50, call; 50, 51; 50, 52; 51, identifier:open; 52, argument_list; 52, 53; 52, 54; 53, identifier:filename; 54, string:'w'; 55, else_clause; 55, 56; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:s; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:io; 63, identifier:StringIO; 64, argument_list; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:s; 69, identifier:write; 70, argument_list; 70, 71; 71, string:'@startuml\n'; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:s; 76, identifier:write; 77, argument_list; 77, 78; 78, string:'skinparam state {\n'; 79, for_statement; 79, 80; 79, 83; 79, 93; 80, pattern_list; 80, 81; 80, 82; 81, identifier:k; 82, identifier:v; 83, call; 83, 84; 83, 85; 84, identifier:list; 85, argument_list; 85, 86; 86, call; 86, 87; 86, 92; 87, attribute; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:background_colors; 91, identifier:items; 92, argument_list; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:s; 98, identifier:write; 99, argument_list; 99, 100; 100, binary_operator:%; 100, 101; 100, 102; 101, string:'BackGroundColor<<%s>> %s\n'; 102, tuple; 102, 103; 102, 104; 103, identifier:k; 104, identifier:v; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:s; 109, identifier:write; 110, argument_list; 110, 111; 111, string:'}\n'; 112, for_statement; 112, 113; 112, 114; 112, 119; 113, identifier:o; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:system; 118, identifier:objects; 119, block; 119, 120; 119, 132; 120, if_statement; 120, 121; 120, 130; 121, boolean_operator:or; 121, 122; 121, 127; 122, call; 122, 123; 122, 124; 123, identifier:isinstance; 124, argument_list; 124, 125; 124, 126; 125, identifier:o; 126, identifier:DefaultProgram; 127, attribute; 127, 128; 127, 129; 128, identifier:o; 129, identifier:hide_in_uml; 130, block; 130, 131; 131, continue_statement; 132, if_statement; 132, 133; 132, 138; 133, call; 133, 134; 133, 135; 134, identifier:isinstance; 135, argument_list; 135, 136; 135, 137; 136, identifier:o; 137, identifier:ProgrammableSystemObject; 138, block; 138, 139; 138, 154; 138, 167; 138, 223; 138, 244; 138, 278; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:s; 143, identifier:write; 144, argument_list; 144, 145; 145, binary_operator:%; 145, 146; 145, 147; 146, string:'state "%s" as %s <<%s>>\n'; 147, tuple; 147, 148; 147, 149; 147, 150; 148, identifier:o; 149, identifier:o; 150, call; 150, 151; 150, 152; 151, identifier:get_type; 152, argument_list; 152, 153; 153, identifier:o; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:s; 158, identifier:write; 159, argument_list; 159, 160; 160, binary_operator:%; 160, 161; 160, 162; 161, string:'%s: %s\n'; 162, tuple; 162, 163; 162, 164; 163, identifier:o; 164, attribute; 164, 165; 164, 166; 165, identifier:o; 166, identifier:class_name; 167, if_statement; 167, 168; 167, 173; 167, 203; 168, call; 168, 169; 168, 170; 169, identifier:isinstance; 170, argument_list; 170, 171; 170, 172; 171, identifier:o; 172, identifier:AbstractActuator; 173, block; 173, 174; 174, for_statement; 174, 175; 174, 176; 174, 182; 175, identifier:p; 176, call; 176, 177; 176, 178; 177, identifier:reversed; 178, argument_list; 178, 179; 179, attribute; 179, 180; 179, 181; 180, identifier:o; 181, identifier:program_stack; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:s; 187, identifier:write; 188, argument_list; 188, 189; 189, binary_operator:%; 189, 190; 189, 191; 190, string:'%s: %s :: %s\n'; 191, tuple; 191, 192; 191, 193; 191, 194; 192, identifier:o; 193, identifier:p; 194, call; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:o; 198, identifier:program_status; 199, identifier:get; 200, argument_list; 200, 201; 200, 202; 201, identifier:p; 202, string:'-'; 203, elif_clause; 203, 204; 203, 209; 204, call; 204, 205; 204, 206; 205, identifier:hasattr; 206, argument_list; 206, 207; 206, 208; 207, identifier:o; 208, string:'status'; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:s; 214, identifier:write; 215, argument_list; 215, 216; 216, binary_operator:%; 216, 217; 216, 218; 217, string:'%s: Status: %s\n'; 218, tuple; 218, 219; 218, 220; 219, identifier:o; 220, attribute; 220, 221; 220, 222; 221, identifier:o; 222, identifier:status; 223, if_statement; 223, 224; 223, 230; 224, call; 224, 225; 224, 226; 225, identifier:getattr; 226, argument_list; 226, 227; 226, 228; 226, 229; 227, identifier:o; 228, string:'is_program'; 229, False; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:s; 235, identifier:write; 236, argument_list; 236, 237; 237, binary_operator:%; 237, 238; 237, 239; 238, string:'%s: Priority: %s\n'; 239, tuple; 239, 240; 239, 241; 240, identifier:o; 241, attribute; 241, 242; 241, 243; 242, identifier:o; 243, identifier:priority; 244, for_statement; 244, 245; 244, 246; 244, 249; 245, identifier:t; 246, attribute; 246, 247; 246, 248; 247, identifier:o; 248, identifier:actual_triggers; 249, block; 249, 250; 249, 262; 250, if_statement; 250, 251; 250, 260; 251, boolean_operator:or; 251, 252; 251, 257; 252, call; 252, 253; 252, 254; 253, identifier:isinstance; 254, argument_list; 254, 255; 254, 256; 255, identifier:t; 256, identifier:DefaultProgram; 257, attribute; 257, 258; 257, 259; 258, identifier:t; 259, identifier:hide_in_uml; 260, block; 260, 261; 261, continue_statement; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:s; 266, identifier:write; 267, argument_list; 267, 268; 268, binary_operator:%; 268, 269; 268, 270; 269, string:'%s -[%s]-> %s\n'; 270, tuple; 270, 271; 270, 272; 270, 277; 271, identifier:t; 272, subscript; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:arrow_colors; 276, string:'trigger'; 277, identifier:o; 278, for_statement; 278, 279; 278, 280; 278, 283; 279, identifier:t; 280, attribute; 280, 281; 280, 282; 281, identifier:o; 282, identifier:actual_targets; 283, block; 283, 284; 283, 290; 283, 305; 283, 319; 284, if_statement; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:t; 287, identifier:hide_in_uml; 288, block; 288, 289; 289, continue_statement; 290, if_statement; 290, 291; 290, 294; 290, 299; 291, attribute; 291, 292; 291, 293; 292, identifier:o; 293, identifier:active; 294, block; 294, 295; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:color; 298, string:'active_target'; 299, else_clause; 299, 300; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:color; 304, string:'inactive_target'; 305, if_statement; 305, 306; 305, 314; 306, comparison_operator:==; 306, 307; 306, 313; 307, call; 307, 308; 307, 309; 308, identifier:getattr; 309, argument_list; 309, 310; 309, 311; 309, 312; 310, identifier:t; 311, string:'program'; 312, None; 313, identifier:o; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:color; 318, string:'controlled_target'; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:s; 323, identifier:write; 324, argument_list; 324, 325; 325, binary_operator:%; 325, 326; 325, 327; 326, string:'%s -[%s]-> %s\n'; 327, tuple; 327, 328; 327, 329; 327, 334; 328, identifier:o; 329, subscript; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:self; 332, identifier:arrow_colors; 333, identifier:color; 334, identifier:t; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:s; 339, identifier:write; 340, argument_list; 340, 341; 341, string:'@enduml\n'; 342, if_statement; 342, 343; 342, 344; 342, 351; 343, identifier:filename; 344, block; 344, 345; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:s; 349, identifier:close; 350, argument_list; 351, else_clause; 351, 352; 352, block; 352, 353; 353, return_statement; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:s; 357, identifier:getvalue; 358, argument_list
def write_puml(self, filename=''): """ Writes PUML from the system. If filename is given, stores result in the file. Otherwise returns result as a string. """ def get_type(o): type = 'program' if isinstance(o, AbstractSensor): type = 'sensor' elif isinstance(o, AbstractActuator): type = 'actuator' return type if filename: s = open(filename, 'w') else: s = io.StringIO() s.write('@startuml\n') s.write('skinparam state {\n') for k, v in list(self.background_colors.items()): s.write('BackGroundColor<<%s>> %s\n' % (k, v)) s.write('}\n') for o in self.system.objects: if isinstance(o, DefaultProgram) or o.hide_in_uml: continue if isinstance(o, ProgrammableSystemObject): s.write('state "%s" as %s <<%s>>\n' % (o, o, get_type(o))) s.write('%s: %s\n' % (o, o.class_name)) if isinstance(o, AbstractActuator): for p in reversed(o.program_stack): s.write('%s: %s :: %s\n' % (o, p, o.program_status.get(p, '-'))) elif hasattr(o, 'status'): s.write('%s: Status: %s\n' % (o, o.status)) if getattr(o, 'is_program', False): s.write('%s: Priority: %s\n' % (o, o.priority)) for t in o.actual_triggers: if isinstance(t, DefaultProgram) or t.hide_in_uml: continue s.write('%s -[%s]-> %s\n' % (t, self.arrow_colors['trigger'], o)) for t in o.actual_targets: if t.hide_in_uml: continue if o.active: color = 'active_target' else: color = 'inactive_target' if getattr(t, 'program', None) == o: color = 'controlled_target' s.write('%s -[%s]-> %s\n' % (o, self.arrow_colors[color], t)) s.write('@enduml\n') if filename: s.close() else: return s.getvalue()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:fit; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:X; 6, default_parameter; 6, 7; 6, 8; 7, identifier:y; 8, None; 9, block; 9, 10; 9, 12; 9, 20; 9, 34; 9, 46; 9, 54; 9, 65; 9, 66; 9, 92; 9, 109; 9, 121; 9, 122; 9, 174; 9, 175; 9, 192; 9, 196; 9, 208; 9, 230; 9, 268; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:N; 15, subscript; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:X; 18, identifier:shape; 19, integer:0; 20, if_statement; 20, 21; 20, 24; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:y; 23, None; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:y; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:np; 31, identifier:zeros; 32, argument_list; 32, 33; 33, identifier:N; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:classes; 39, call; 39, 40; 39, 41; 40, identifier:list; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:set; 44, argument_list; 44, 45; 45, identifier:y; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:classes; 52, identifier:sort; 53, argument_list; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:n_classes; 59, call; 59, 60; 59, 61; 60, identifier:len; 61, argument_list; 61, 62; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:classes; 65, comment; 66, if_statement; 66, 67; 66, 71; 67, not_operator; 67, 68; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:sigma; 71, block; 71, 72; 71, 81; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:sigma; 77, call; 77, 78; 77, 79; 78, identifier:median_kneighbour_distance; 79, argument_list; 79, 80; 80, identifier:X; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:gamma; 86, binary_operator:**; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:sigma; 90, unary_operator:-; 90, 91; 91, integer:2; 92, if_statement; 92, 93; 92, 97; 93, not_operator; 93, 94; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:gamma; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:gamma; 103, binary_operator:**; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:sigma; 107, unary_operator:-; 107, 108; 108, integer:2; 109, if_statement; 109, 110; 109, 114; 110, not_operator; 110, 111; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:rho; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:rho; 120, float:0.1; 121, comment; 122, if_statement; 122, 123; 122, 128; 122, 162; 123, comparison_operator:is; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:kernel_pos; 127, None; 128, block; 128, 129; 128, 139; 128, 150; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:B; 132, call; 132, 133; 132, 134; 133, identifier:min; 134, argument_list; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:n_kernels_max; 138, identifier:N; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:kernel_idx; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:np; 146, identifier:random; 147, identifier:permutation; 148, argument_list; 148, 149; 149, identifier:N; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:kernel_pos; 155, subscript; 155, 156; 155, 157; 156, identifier:X; 157, subscript; 157, 158; 157, 159; 158, identifier:kernel_idx; 159, slice; 159, 160; 159, 161; 160, colon; 161, identifier:B; 162, else_clause; 162, 163; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:B; 167, subscript; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:kernel_pos; 172, identifier:shape; 173, integer:0; 174, comment; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:Phi; 178, call; 178, 179; 178, 184; 179, attribute; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:metrics; 182, identifier:pairwise; 183, identifier:rbf_kernel; 184, argument_list; 184, 185; 184, 186; 184, 189; 185, identifier:X; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:kernel_pos; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:gamma; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:theta; 195, dictionary; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:Phi_PhiT; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:np; 202, identifier:dot; 203, argument_list; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:Phi; 206, identifier:T; 207, identifier:Phi; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:inverse_term; 211, call; 211, 212; 211, 217; 212, attribute; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:np; 215, identifier:linalg; 216, identifier:inv; 217, argument_list; 217, 218; 218, binary_operator:+; 218, 219; 218, 220; 219, identifier:Phi_PhiT; 220, binary_operator:*; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:rho; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:np; 227, identifier:eye; 228, argument_list; 228, 229; 229, identifier:B; 230, for_statement; 230, 231; 230, 232; 230, 235; 231, identifier:c; 232, attribute; 232, 233; 232, 234; 233, identifier:self; 234, identifier:classes; 235, block; 235, 236; 235, 248; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:m; 239, call; 239, 240; 239, 246; 240, attribute; 240, 241; 240, 245; 241, parenthesized_expression; 241, 242; 242, comparison_operator:==; 242, 243; 242, 244; 243, identifier:y; 244, identifier:c; 245, identifier:astype; 246, argument_list; 246, 247; 247, identifier:int; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 253; 250, subscript; 250, 251; 250, 252; 251, identifier:theta; 252, identifier:c; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:np; 256, identifier:dot; 257, argument_list; 257, 258; 257, 259; 258, identifier:inverse_term; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:np; 262, identifier:dot; 263, argument_list; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:Phi; 266, identifier:T; 267, identifier:m; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:theta; 273, identifier:theta
def fit(self, X, y=None): """ Fit the inlier model given training data. This function attempts to choose reasonable defaults for parameters sigma and rho if none are specified, which could then be adjusted to improve performance. Parameters ---------- X : array Examples of inlier data, of dimension N times d (rows are examples, columns are data dimensions) y : array, optional If the inliers have multiple classes, then y contains the class assignments as a vector of length N. If this is specified then the model will attempt to assign test data to one of the inlier classes or to the outlier class. """ N = X.shape[0] if y is None: y = np.zeros(N) self.classes = list(set(y)) self.classes.sort() self.n_classes = len(self.classes) # If no kernel parameters specified, try to choose some defaults if not self.sigma: self.sigma = median_kneighbour_distance(X) self.gamma = self.sigma**-2 if not self.gamma: self.gamma = self.sigma**-2 if not self.rho: self.rho = 0.1 # choose kernel basis centres if self.kernel_pos is None: B = min(self.n_kernels_max, N) kernel_idx = np.random.permutation(N) self.kernel_pos = X[kernel_idx[:B]] else: B = self.kernel_pos.shape[0] # fit coefficients Phi = metrics.pairwise.rbf_kernel(X, self.kernel_pos, self.gamma) theta = {} Phi_PhiT = np.dot(Phi.T, Phi) inverse_term = np.linalg.inv(Phi_PhiT + self.rho*np.eye(B)) for c in self.classes: m = (y == c).astype(int) theta[c] = np.dot(inverse_term, np.dot(Phi.T, m)) self.theta = theta
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:predict_sequence; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:X; 6, identifier:A; 7, identifier:pi; 8, default_parameter; 8, 9; 8, 10; 9, identifier:inference; 10, string:'smoothing'; 11, block; 11, 12; 11, 14; 11, 23; 11, 31; 11, 42; 11, 50; 11, 116; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:obsll; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:predict_proba; 21, argument_list; 21, 22; 22, identifier:X; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 28; 25, pattern_list; 25, 26; 25, 27; 26, identifier:T; 27, identifier:S; 28, attribute; 28, 29; 28, 30; 29, identifier:obsll; 30, identifier:shape; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:alpha; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:np; 37, identifier:zeros; 38, argument_list; 38, 39; 39, tuple; 39, 40; 39, 41; 40, identifier:T; 41, identifier:S; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 49; 44, subscript; 44, 45; 44, 46; 44, 47; 45, identifier:alpha; 46, integer:0; 47, slice; 47, 48; 48, colon; 49, identifier:pi; 50, for_statement; 50, 51; 50, 52; 50, 57; 51, identifier:t; 52, call; 52, 53; 52, 54; 53, identifier:range; 54, argument_list; 54, 55; 54, 56; 55, integer:1; 56, identifier:T; 57, block; 57, 58; 57, 78; 57, 95; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 65; 60, subscript; 60, 61; 60, 62; 60, 63; 61, identifier:alpha; 62, identifier:t; 63, slice; 63, 64; 64, colon; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:np; 68, identifier:dot; 69, argument_list; 69, 70; 69, 77; 70, subscript; 70, 71; 70, 72; 70, 75; 71, identifier:alpha; 72, binary_operator:-; 72, 73; 72, 74; 73, identifier:t; 74, integer:1; 75, slice; 75, 76; 76, colon; 77, identifier:A; 78, for_statement; 78, 79; 78, 80; 78, 84; 79, identifier:s; 80, call; 80, 81; 80, 82; 81, identifier:range; 82, argument_list; 82, 83; 83, identifier:S; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, augmented_assignment:*=; 86, 87; 86, 91; 87, subscript; 87, 88; 87, 89; 87, 90; 88, identifier:alpha; 89, identifier:t; 90, identifier:s; 91, subscript; 91, 92; 91, 93; 91, 94; 92, identifier:obsll; 93, identifier:t; 94, identifier:s; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 102; 97, subscript; 97, 98; 97, 99; 97, 100; 98, identifier:alpha; 99, identifier:t; 100, slice; 100, 101; 101, colon; 102, binary_operator:/; 102, 103; 102, 108; 103, subscript; 103, 104; 103, 105; 103, 106; 104, identifier:alpha; 105, identifier:t; 106, slice; 106, 107; 107, colon; 108, call; 108, 109; 108, 110; 109, identifier:sum; 110, argument_list; 110, 111; 111, subscript; 111, 112; 111, 113; 111, 114; 112, identifier:alpha; 113, identifier:t; 114, slice; 114, 115; 115, colon; 116, if_statement; 116, 117; 116, 120; 116, 123; 117, comparison_operator:==; 117, 118; 117, 119; 118, identifier:inference; 119, string:'filtering'; 120, block; 120, 121; 121, return_statement; 121, 122; 122, identifier:alpha; 123, else_clause; 123, 124; 124, block; 124, 125; 124, 136; 124, 147; 124, 162; 124, 234; 124, 280; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:beta; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:np; 131, identifier:zeros; 132, argument_list; 132, 133; 133, tuple; 133, 134; 133, 135; 134, identifier:T; 135, identifier:S; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:gamma; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:np; 142, identifier:zeros; 143, argument_list; 143, 144; 144, tuple; 144, 145; 144, 146; 145, identifier:T; 146, identifier:S; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 156; 149, subscript; 149, 150; 149, 151; 149, 154; 150, identifier:beta; 151, binary_operator:-; 151, 152; 151, 153; 152, identifier:T; 153, integer:1; 154, slice; 154, 155; 155, colon; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:np; 159, identifier:ones; 160, argument_list; 160, 161; 161, identifier:S; 162, for_statement; 162, 163; 162, 164; 162, 174; 163, identifier:t; 164, call; 164, 165; 164, 166; 165, identifier:range; 166, argument_list; 166, 167; 166, 170; 166, 172; 167, binary_operator:-; 167, 168; 167, 169; 168, identifier:T; 169, integer:2; 170, unary_operator:-; 170, 171; 171, integer:1; 172, unary_operator:-; 172, 173; 173, integer:1; 174, block; 174, 175; 174, 213; 175, for_statement; 175, 176; 175, 177; 175, 181; 176, identifier:i; 177, call; 177, 178; 177, 179; 178, identifier:range; 179, argument_list; 179, 180; 180, identifier:S; 181, block; 181, 182; 182, for_statement; 182, 183; 182, 184; 182, 188; 183, identifier:j; 184, call; 184, 185; 184, 186; 185, identifier:range; 186, argument_list; 186, 187; 187, identifier:S; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, augmented_assignment:+=; 190, 191; 190, 195; 191, subscript; 191, 192; 191, 193; 191, 194; 192, identifier:beta; 193, identifier:t; 194, identifier:i; 195, binary_operator:*; 195, 196; 195, 207; 196, binary_operator:*; 196, 197; 196, 201; 197, subscript; 197, 198; 197, 199; 197, 200; 198, identifier:A; 199, identifier:i; 200, identifier:j; 201, subscript; 201, 202; 201, 203; 201, 206; 202, identifier:obsll; 203, binary_operator:+; 203, 204; 203, 205; 204, identifier:t; 205, integer:1; 206, identifier:j; 207, subscript; 207, 208; 207, 209; 207, 212; 208, identifier:beta; 209, binary_operator:+; 209, 210; 209, 211; 210, identifier:t; 211, integer:1; 212, identifier:j; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 220; 215, subscript; 215, 216; 215, 217; 215, 218; 216, identifier:beta; 217, identifier:t; 218, slice; 218, 219; 219, colon; 220, binary_operator:/; 220, 221; 220, 226; 221, subscript; 221, 222; 221, 223; 221, 224; 222, identifier:beta; 223, identifier:t; 224, slice; 224, 225; 225, colon; 226, call; 226, 227; 226, 228; 227, identifier:sum; 228, argument_list; 228, 229; 229, subscript; 229, 230; 229, 231; 229, 232; 230, identifier:beta; 231, identifier:t; 232, slice; 232, 233; 233, colon; 234, for_statement; 234, 235; 234, 236; 234, 240; 235, identifier:t; 236, call; 236, 237; 236, 238; 237, identifier:range; 238, argument_list; 238, 239; 239, identifier:T; 240, block; 240, 241; 240, 259; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 248; 243, subscript; 243, 244; 243, 245; 243, 246; 244, identifier:gamma; 245, identifier:t; 246, slice; 246, 247; 247, colon; 248, binary_operator:*; 248, 249; 248, 254; 249, subscript; 249, 250; 249, 251; 249, 252; 250, identifier:alpha; 251, identifier:t; 252, slice; 252, 253; 253, colon; 254, subscript; 254, 255; 254, 256; 254, 257; 255, identifier:beta; 256, identifier:t; 257, slice; 257, 258; 258, colon; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 266; 261, subscript; 261, 262; 261, 263; 261, 264; 262, identifier:gamma; 263, identifier:t; 264, slice; 264, 265; 265, colon; 266, binary_operator:/; 266, 267; 266, 272; 267, subscript; 267, 268; 267, 269; 267, 270; 268, identifier:gamma; 269, identifier:t; 270, slice; 270, 271; 271, colon; 272, call; 272, 273; 272, 274; 273, identifier:sum; 274, argument_list; 274, 275; 275, subscript; 275, 276; 275, 277; 275, 278; 276, identifier:gamma; 277, identifier:t; 278, slice; 278, 279; 279, colon; 280, return_statement; 280, 281; 281, identifier:gamma
def predict_sequence(self, X, A, pi, inference='smoothing'): """ Calculate class probabilities for a sequence of data. Parameters ---------- X : array Test data, of dimension N times d (rows are time frames, columns are data dimensions) A : class transition matrix, where A[i,j] contains p(y_t=j|y_{t-1}=i) pi : vector of initial class probabilities inference : can be 'smoothing' or 'filtering'. Returns: ------- y_prob : array An array of dimension N times n_inlier_classes+1, containing the probabilities of each row of X being one of the inlier classes, or the outlier class (last column). """ obsll = self.predict_proba(X) T, S = obsll.shape alpha = np.zeros((T, S)) alpha[0, :] = pi for t in range(1, T): alpha[t, :] = np.dot(alpha[t-1, :], A) for s in range(S): alpha[t, s] *= obsll[t, s] alpha[t, :] = alpha[t, :]/sum(alpha[t, :]) if inference == 'filtering': return alpha else: beta = np.zeros((T, S)) gamma = np.zeros((T, S)) beta[T-1, :] = np.ones(S) for t in range(T-2, -1, -1): for i in range(S): for j in range(S): beta[t, i] += A[i, j]*obsll[t+1, j]*beta[t+1, j] beta[t, :] = beta[t, :]/sum(beta[t, :]) for t in range(T): gamma[t, :] = alpha[t, :]*beta[t, :] gamma[t, :] = gamma[t, :]/sum(gamma[t, :]) return gamma
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:add_bgcolor; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:colname; 6, default_parameter; 6, 7; 6, 8; 7, identifier:cmap; 8, string:'copper'; 9, default_parameter; 9, 10; 9, 11; 10, identifier:mode; 11, string:'absmax'; 12, default_parameter; 12, 13; 12, 14; 13, identifier:threshold; 14, integer:2; 15, block; 15, 16; 15, 18; 15, 31; 15, 41; 15, 50; 15, 151; 15, 152; 15, 168; 15, 183; 15, 184; 15, 194; 15, 195; 15, 196; 15, 224; 15, 235; 15, 239; 16, expression_statement; 16, 17; 17, comment; 18, try_statement; 18, 19; 18, 20; 18, 28; 19, comment; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:cmap; 24, call; 24, 25; 24, 26; 25, identifier:cmap_builder; 26, argument_list; 26, 27; 27, identifier:cmap; 28, except_clause; 28, 29; 29, block; 29, 30; 30, pass_statement; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:data; 34, attribute; 34, 35; 34, 40; 35, subscript; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:df; 39, identifier:colname; 40, identifier:values; 41, if_statement; 41, 42; 41, 48; 42, comparison_operator:==; 42, 43; 42, 47; 43, call; 43, 44; 43, 45; 44, identifier:len; 45, argument_list; 45, 46; 46, identifier:data; 47, integer:0; 48, block; 48, 49; 49, return_statement; 50, if_statement; 50, 51; 50, 54; 50, 72; 50, 124; 51, comparison_operator:==; 51, 52; 51, 53; 52, identifier:mode; 53, string:'clip'; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:data; 58, list_comprehension; 58, 59; 58, 69; 59, binary_operator:/; 59, 60; 59, 65; 60, call; 60, 61; 60, 62; 61, identifier:min; 62, argument_list; 62, 63; 62, 64; 63, identifier:x; 64, identifier:threshold; 65, call; 65, 66; 65, 67; 66, identifier:float; 67, argument_list; 67, 68; 68, identifier:threshold; 69, for_in_clause; 69, 70; 69, 71; 70, identifier:x; 71, identifier:data; 72, elif_clause; 72, 73; 72, 76; 73, comparison_operator:==; 73, 74; 73, 75; 74, identifier:mode; 75, string:'absmax'; 76, block; 76, 77; 76, 88; 76, 99; 76, 108; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:m; 80, call; 80, 81; 80, 82; 81, identifier:abs; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:data; 86, identifier:min; 87, argument_list; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:M; 91, call; 91, 92; 91, 93; 92, identifier:abs; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:data; 97, identifier:max; 98, argument_list; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:M; 102, call; 102, 103; 102, 104; 103, identifier:max; 104, argument_list; 104, 105; 105, list:[m, M]; 105, 106; 105, 107; 106, identifier:m; 107, identifier:M; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:!=; 109, 110; 109, 111; 110, identifier:M; 111, integer:0; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:data; 116, binary_operator:/; 116, 117; 116, 123; 117, parenthesized_expression; 117, 118; 118, binary_operator:+; 118, 119; 118, 122; 119, binary_operator:/; 119, 120; 119, 121; 120, identifier:data; 121, identifier:M; 122, integer:1; 123, float:2.; 124, elif_clause; 124, 125; 124, 128; 125, comparison_operator:==; 125, 126; 125, 127; 126, identifier:mode; 127, string:'max'; 128, block; 128, 129; 129, if_statement; 129, 130; 129, 137; 130, comparison_operator:!=; 130, 131; 130, 136; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:data; 134, identifier:max; 135, argument_list; 136, integer:0; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:data; 141, binary_operator:/; 141, 142; 141, 143; 142, identifier:data; 143, call; 143, 144; 143, 145; 144, identifier:float; 145, argument_list; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:data; 149, identifier:max; 150, argument_list; 151, comment; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:rgbcolors; 155, list_comprehension; 155, 156; 155, 165; 156, subscript; 156, 157; 156, 161; 157, call; 157, 158; 157, 159; 158, identifier:cmap; 159, argument_list; 159, 160; 160, identifier:x; 161, slice; 161, 162; 161, 163; 161, 164; 162, integer:0; 163, colon; 164, integer:3; 165, for_in_clause; 165, 166; 165, 167; 166, identifier:x; 167, identifier:data; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:hexcolors; 171, list_comprehension; 171, 172; 171, 180; 172, call; 172, 173; 172, 174; 173, identifier:rgb2hex; 174, argument_list; 174, 175; 174, 177; 175, list_splat; 175, 176; 176, identifier:x; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:normalised; 179, True; 180, for_in_clause; 180, 181; 180, 182; 181, identifier:x; 182, identifier:rgbcolors; 183, comment; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:data; 187, attribute; 187, 188; 187, 193; 188, subscript; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:df; 192, identifier:colname; 193, identifier:values; 194, comment; 195, comment; 196, function_definition; 196, 197; 196, 198; 196, 200; 197, function_name:prec; 198, parameters; 198, 199; 199, identifier:x; 200, block; 200, 201; 201, try_statement; 201, 202; 201, 203; 201, 220; 202, comment; 203, block; 203, 204; 203, 218; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:x; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:easydev; 210, identifier:precision; 211, argument_list; 211, 212; 211, 213; 212, identifier:x; 213, subscript; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:pd_options; 217, string:'precision'; 218, return_statement; 218, 219; 219, identifier:x; 220, except_clause; 220, 221; 221, block; 221, 222; 222, return_statement; 222, 223; 223, identifier:x; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:data; 227, list_comprehension; 227, 228; 227, 232; 228, call; 228, 229; 228, 230; 229, identifier:prec; 230, argument_list; 230, 231; 231, identifier:x; 232, for_in_clause; 232, 233; 232, 234; 233, identifier:x; 234, identifier:data; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:html_formatter; 238, string:'<p style="background-color:{0}">{1}</p>'; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 246; 241, subscript; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:df; 245, identifier:colname; 246, list_comprehension; 246, 247; 246, 254; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:html_formatter; 250, identifier:format; 251, argument_list; 251, 252; 251, 253; 252, identifier:x; 253, identifier:y; 254, for_in_clause; 254, 255; 254, 258; 255, pattern_list; 255, 256; 255, 257; 256, identifier:x; 257, identifier:y; 258, call; 258, 259; 258, 260; 259, identifier:zip; 260, argument_list; 260, 261; 260, 262; 261, identifier:hexcolors; 262, identifier:data
def add_bgcolor(self, colname, cmap='copper', mode='absmax', threshold=2): """Change column content into HTML paragraph with background color :param colname: :param cmap: a colormap (matplotlib) or created using colormap package (from pypi). :param mode: type of normalisation in 'absmax', 'max', 'clip' (see details below) :param threshold: used if mode is set to 'clip' Colormap have values between 0 and 1 so we need to normalised the data between 0 and 1. There are 3 mode to normalise the data so far. If mode is set to 'absmax', negatives and positives values are expected to be found in a range from -inf to inf. Values are scaled in between [0,1] X' = (X / M +1) /2. where m is the absolute maximum. Ideally a colormap should be made of 3 colors, the first color used for negative values, the second for zeros and third color for positive values. If mode is set to 'clip', values are clipped to a max value (parameter *threshold* and values are normalised by that same threshold. If mode is set to 'max', values are normalised by the max. """ try: # if a cmap is provided, it may be just a known cmap name cmap = cmap_builder(cmap) except: pass data = self.df[colname].values if len(data) == 0: return if mode == 'clip': data = [min(x, threshold)/float(threshold) for x in data] elif mode == 'absmax': m = abs(data.min()) M = abs(data.max()) M = max([m, M]) if M != 0: data = (data / M + 1)/2. elif mode == 'max': if data.max() != 0: data = data / float(data.max()) # the expected RGB values for a given data point rgbcolors = [cmap(x)[0:3] for x in data] hexcolors = [rgb2hex(*x, normalised=True) for x in rgbcolors] # need to read original data again data = self.df[colname].values # need to set precision since this is going to be a text not a number # so pandas will not use the precision for those cases: def prec(x): try: # this may fail if for instance x is nan or inf x = easydev.precision(x, self.pd_options['precision']) return x except: return x data = [prec(x) for x in data] html_formatter = '<p style="background-color:{0}">{1}</p>' self.df[colname] = [html_formatter.format(x, y) for x, y in zip(hexcolors, data)]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 1, 21; 2, function_name:list; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filter; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:type; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:limit; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:page; 19, None; 20, comment; 21, block; 21, 22; 21, 24; 21, 30; 21, 48; 21, 68; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:schema; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:LIST_SCHEMA; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:resp; 33, call; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:service; 38, identifier:list; 39, argument_list; 39, 40; 39, 43; 39, 44; 39, 45; 39, 46; 39, 47; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:base; 43, identifier:filter; 44, identifier:type; 45, identifier:sort; 46, identifier:limit; 47, identifier:page; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, pattern_list; 50, 51; 50, 52; 51, identifier:cs; 52, identifier:l; 53, call; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:service; 58, identifier:decode; 59, argument_list; 59, 60; 59, 61; 59, 62; 59, 65; 60, identifier:schema; 61, identifier:resp; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:many; 64, True; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:links; 67, True; 68, return_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:Page; 71, argument_list; 71, 72; 71, 73; 72, identifier:cs; 73, identifier:l
def list(self, filter=None, type=None, sort=None, limit=None, page=None): # pylint: disable=redefined-builtin """Get a list of configs. :param filter: (optional) Filters to apply as a string list. :param type: (optional) `union` or `inter` as string. :param sort: (optional) Sort fields to apply as string list. :param limit: (optional) Limit returned list length. :param page: (optional) Page to return. :return: :class:`configs.Page <configs.Page>` object """ schema = self.LIST_SCHEMA resp = self.service.list(self.base, filter, type, sort, limit, page) cs, l = self.service.decode(schema, resp, many=True, links=True) return Page(cs, l)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:signup_handler; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:remote; 5, list_splat_pattern; 5, 6; 6, identifier:args; 7, dictionary_splat_pattern; 7, 8; 8, identifier:kwargs; 9, block; 9, 10; 9, 12; 9, 13; 9, 23; 9, 24; 9, 31; 9, 40; 9, 49; 9, 50; 9, 75; 9, 84; 9, 317; 9, 318; 9, 329; 9, 347; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:current_user; 16, identifier:is_authenticated; 17, block; 17, 18; 18, return_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:redirect; 21, argument_list; 21, 22; 22, string:'/'; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:oauth_token; 27, call; 27, 28; 27, 29; 28, identifier:token_getter; 29, argument_list; 29, 30; 30, identifier:remote; 31, if_statement; 31, 32; 31, 34; 32, not_operator; 32, 33; 33, identifier:oauth_token; 34, block; 34, 35; 35, return_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:redirect; 38, argument_list; 38, 39; 39, string:'/'; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:session_prefix; 43, call; 43, 44; 43, 45; 44, identifier:token_session_key; 45, argument_list; 45, 46; 46, attribute; 46, 47; 46, 48; 47, identifier:remote; 48, identifier:name; 49, comment; 50, if_statement; 50, 51; 50, 61; 51, not_operator; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:session; 55, identifier:get; 56, argument_list; 56, 57; 56, 60; 57, binary_operator:+; 57, 58; 57, 59; 58, identifier:session_prefix; 59, string:'_autoregister'; 60, False; 61, block; 61, 62; 62, return_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:redirect; 65, argument_list; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:url_for; 68, argument_list; 68, 69; 68, 70; 69, string:'.login'; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:remote_app; 72, attribute; 72, 73; 72, 74; 73, identifier:remote; 74, identifier:name; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:form; 78, call; 78, 79; 78, 80; 79, identifier:create_registrationform; 80, argument_list; 80, 81; 81, attribute; 81, 82; 81, 83; 82, identifier:request; 83, identifier:form; 84, if_statement; 84, 85; 84, 90; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:form; 88, identifier:validate_on_submit; 89, argument_list; 90, block; 90, 91; 90, 102; 90, 113; 90, 114; 90, 121; 90, 132; 90, 133; 90, 143; 90, 144; 90, 162; 90, 172; 90, 183; 90, 247; 90, 248; 90, 271; 90, 272; 90, 282; 90, 292; 90, 293; 90, 302; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:account_info; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:session; 97, identifier:get; 98, argument_list; 98, 99; 99, binary_operator:+; 99, 100; 99, 101; 100, identifier:session_prefix; 101, string:'_account_info'; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:response; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:session; 108, identifier:get; 109, argument_list; 109, 110; 110, binary_operator:+; 110, 111; 110, 112; 111, identifier:session_prefix; 112, string:'_response'; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:user; 117, call; 117, 118; 117, 119; 118, identifier:oauth_register; 119, argument_list; 119, 120; 120, identifier:form; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:is; 122, 123; 122, 124; 123, identifier:user; 124, None; 125, block; 125, 126; 126, raise_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:OAuthError; 129, argument_list; 129, 130; 129, 131; 130, string:'Could not create user.'; 131, identifier:remote; 132, comment; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:session; 137, identifier:pop; 138, argument_list; 138, 139; 138, 142; 139, binary_operator:+; 139, 140; 139, 141; 140, identifier:session_prefix; 141, string:'_autoregister'; 142, None; 143, comment; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:token; 147, call; 147, 148; 147, 149; 148, identifier:token_setter; 149, argument_list; 149, 150; 149, 151; 149, 154; 149, 159; 150, identifier:remote; 151, subscript; 151, 152; 151, 153; 152, identifier:oauth_token; 153, integer:0; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:secret; 156, subscript; 156, 157; 156, 158; 157, identifier:oauth_token; 158, integer:1; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:user; 161, identifier:user; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:handlers; 165, subscript; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:current_oauthclient; 168, identifier:signup_handlers; 169, attribute; 169, 170; 169, 171; 170, identifier:remote; 171, identifier:name; 172, if_statement; 172, 173; 172, 176; 173, comparison_operator:is; 173, 174; 173, 175; 174, identifier:token; 175, None; 176, block; 176, 177; 177, raise_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:OAuthError; 180, argument_list; 180, 181; 180, 182; 181, string:'Could not create token for user.'; 182, identifier:remote; 183, if_statement; 183, 184; 183, 190; 183, 236; 184, not_operator; 184, 185; 185, attribute; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:token; 188, identifier:remote_account; 189, identifier:extra_data; 190, block; 190, 191; 190, 201; 190, 217; 190, 218; 190, 226; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:account_setup; 194, call; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:handlers; 197, string:'setup'; 198, argument_list; 198, 199; 198, 200; 199, identifier:token; 200, identifier:response; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:account_setup_received; 205, identifier:send; 206, argument_list; 206, 207; 206, 208; 206, 211; 206, 214; 207, identifier:remote; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:token; 210, identifier:token; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:response; 213, identifier:response; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:account_setup; 216, identifier:account_setup; 217, comment; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 225; 220, attribute; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:db; 223, identifier:session; 224, identifier:commit; 225, argument_list; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:account_setup_committed; 230, identifier:send; 231, argument_list; 231, 232; 231, 233; 232, identifier:remote; 233, keyword_argument; 233, 234; 233, 235; 234, identifier:token; 235, identifier:token; 236, else_clause; 236, 237; 236, 238; 237, comment; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 246; 241, attribute; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:db; 244, identifier:session; 245, identifier:commit; 246, argument_list; 247, comment; 248, if_statement; 248, 249; 248, 260; 248, 261; 248, 262; 249, not_operator; 249, 250; 250, call; 250, 251; 250, 252; 251, identifier:oauth_authenticate; 252, argument_list; 252, 253; 252, 256; 252, 257; 253, attribute; 253, 254; 253, 255; 254, identifier:remote; 255, identifier:consumer_key; 256, identifier:user; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:require_existing_link; 259, False; 260, comment; 261, comment; 262, block; 262, 263; 263, return_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:redirect; 266, argument_list; 266, 267; 267, call; 267, 268; 267, 269; 268, identifier:url_for; 269, argument_list; 269, 270; 270, string:'security.login'; 271, comment; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:session; 276, identifier:pop; 277, argument_list; 277, 278; 277, 281; 278, binary_operator:+; 278, 279; 278, 280; 279, identifier:session_prefix; 280, string:'_account_info'; 281, None; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:session; 286, identifier:pop; 287, argument_list; 287, 288; 287, 291; 288, binary_operator:+; 288, 289; 288, 290; 289, identifier:session_prefix; 290, string:'_response'; 291, None; 292, comment; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:next_url; 296, call; 296, 297; 296, 298; 297, identifier:get_session_next_url; 298, argument_list; 298, 299; 299, attribute; 299, 300; 299, 301; 300, identifier:remote; 301, identifier:name; 302, if_statement; 302, 303; 302, 304; 302, 310; 303, identifier:next_url; 304, block; 304, 305; 305, return_statement; 305, 306; 306, call; 306, 307; 306, 308; 307, identifier:redirect; 308, argument_list; 308, 309; 309, identifier:next_url; 310, else_clause; 310, 311; 311, block; 311, 312; 312, return_statement; 312, 313; 313, call; 313, 314; 313, 315; 314, identifier:redirect; 315, argument_list; 315, 316; 316, string:'/'; 317, comment; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:account_info; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:session; 324, identifier:get; 325, argument_list; 325, 326; 326, binary_operator:+; 326, 327; 326, 328; 327, identifier:session_prefix; 328, string:'_account_info'; 329, if_statement; 329, 330; 329, 336; 330, not_operator; 330, 331; 331, call; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:form; 334, identifier:is_submitted; 335, argument_list; 336, block; 336, 337; 337, expression_statement; 337, 338; 338, assignment; 338, 339; 338, 340; 339, identifier:form; 340, call; 340, 341; 340, 342; 341, identifier:fill_form; 342, argument_list; 342, 343; 342, 344; 343, identifier:form; 344, subscript; 344, 345; 344, 346; 345, identifier:account_info; 346, string:'user'; 347, return_statement; 347, 348; 348, call; 348, 349; 348, 350; 349, identifier:render_template; 350, argument_list; 350, 351; 350, 356; 350, 359; 350, 362; 350, 379; 350, 396; 351, subscript; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:current_app; 354, identifier:config; 355, string:'OAUTHCLIENT_SIGNUP_TEMPLATE'; 356, keyword_argument; 356, 357; 356, 358; 357, identifier:form; 358, identifier:form; 359, keyword_argument; 359, 360; 359, 361; 360, identifier:remote; 361, identifier:remote; 362, keyword_argument; 362, 363; 362, 364; 363, identifier:app_title; 364, call; 364, 365; 364, 376; 365, attribute; 365, 366; 365, 375; 366, subscript; 366, 367; 366, 372; 367, subscript; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:current_app; 370, identifier:config; 371, string:'OAUTHCLIENT_REMOTE_APPS'; 372, attribute; 372, 373; 372, 374; 373, identifier:remote; 374, identifier:name; 375, identifier:get; 376, argument_list; 376, 377; 376, 378; 377, string:'title'; 378, string:''; 379, keyword_argument; 379, 380; 379, 381; 380, identifier:app_description; 381, call; 381, 382; 381, 393; 382, attribute; 382, 383; 382, 392; 383, subscript; 383, 384; 383, 389; 384, subscript; 384, 385; 384, 388; 385, attribute; 385, 386; 385, 387; 386, identifier:current_app; 387, identifier:config; 388, string:'OAUTHCLIENT_REMOTE_APPS'; 389, attribute; 389, 390; 389, 391; 390, identifier:remote; 391, identifier:name; 392, identifier:get; 393, argument_list; 393, 394; 393, 395; 394, string:'description'; 395, string:''; 396, keyword_argument; 396, 397; 396, 398; 397, identifier:app_icon; 398, call; 398, 399; 398, 410; 399, attribute; 399, 400; 399, 409; 400, subscript; 400, 401; 400, 406; 401, subscript; 401, 402; 401, 405; 402, attribute; 402, 403; 402, 404; 403, identifier:current_app; 404, identifier:config; 405, string:'OAUTHCLIENT_REMOTE_APPS'; 406, attribute; 406, 407; 406, 408; 407, identifier:remote; 408, identifier:name; 409, identifier:get; 410, argument_list; 410, 411; 410, 412; 411, string:'icon'; 412, None
def signup_handler(remote, *args, **kwargs): """Handle extra signup information. :param remote: The remote application. :returns: Redirect response or the template rendered. """ # User already authenticated so move on if current_user.is_authenticated: return redirect('/') # Retrieve token from session oauth_token = token_getter(remote) if not oauth_token: return redirect('/') session_prefix = token_session_key(remote.name) # Test to see if this is coming from on authorized request if not session.get(session_prefix + '_autoregister', False): return redirect(url_for('.login', remote_app=remote.name)) form = create_registrationform(request.form) if form.validate_on_submit(): account_info = session.get(session_prefix + '_account_info') response = session.get(session_prefix + '_response') # Register user user = oauth_register(form) if user is None: raise OAuthError('Could not create user.', remote) # Remove session key session.pop(session_prefix + '_autoregister', None) # Link account and set session data token = token_setter(remote, oauth_token[0], secret=oauth_token[1], user=user) handlers = current_oauthclient.signup_handlers[remote.name] if token is None: raise OAuthError('Could not create token for user.', remote) if not token.remote_account.extra_data: account_setup = handlers['setup'](token, response) account_setup_received.send( remote, token=token, response=response, account_setup=account_setup ) # Registration has been finished db.session.commit() account_setup_committed.send(remote, token=token) else: # Registration has been finished db.session.commit() # Authenticate the user if not oauth_authenticate(remote.consumer_key, user, require_existing_link=False): # Redirect the user after registration (which doesn't include the # activation), waiting for user to confirm his email. return redirect(url_for('security.login')) # Remove account info from session session.pop(session_prefix + '_account_info', None) session.pop(session_prefix + '_response', None) # Redirect to next next_url = get_session_next_url(remote.name) if next_url: return redirect(next_url) else: return redirect('/') # Pre-fill form account_info = session.get(session_prefix + '_account_info') if not form.is_submitted(): form = fill_form(form, account_info['user']) return render_template( current_app.config['OAUTHCLIENT_SIGNUP_TEMPLATE'], form=form, remote=remote, app_title=current_app.config['OAUTHCLIENT_REMOTE_APPS'][ remote.name].get('title', ''), app_description=current_app.config['OAUTHCLIENT_REMOTE_APPS'][ remote.name].get('description', ''), app_icon=current_app.config['OAUTHCLIENT_REMOTE_APPS'][ remote.name].get('icon', None), )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 1, 21; 2, function_name:list_csv; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filter; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:type; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:limit; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:page; 19, None; 20, comment; 21, block; 21, 22; 21, 24; 22, expression_statement; 22, 23; 23, comment; 24, return_statement; 24, 25; 25, attribute; 25, 26; 25, 44; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:service; 31, identifier:list; 32, argument_list; 32, 33; 32, 36; 32, 37; 32, 38; 32, 39; 32, 40; 32, 41; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:base; 36, identifier:filter; 37, identifier:type; 38, identifier:sort; 39, identifier:limit; 40, identifier:page; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:format; 43, string:'csv'; 44, identifier:text
def list_csv(self, filter=None, type=None, sort=None, limit=None, page=None): # pylint: disable=redefined-builtin """Get a list of results as CSV. :param filter: (optional) Filters to apply as a string list. :param type: (optional) `union` or `inter` as string. :param sort: (optional) Sort fields to apply as string list. :param limit: (optional) Limit returned list length. :param page: (optional) Page to return. :rtype: string """ return self.service.list(self.base, filter, type, sort, limit, page, format='csv').text
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 1, 13; 2, function_name:list_logdir; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:filter; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort; 11, None; 12, comment; 13, block; 13, 14; 13, 16; 13, 22; 13, 44; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:schema; 19, call; 19, 20; 19, 21; 20, identifier:LogDirFileSchema; 21, argument_list; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:resp; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:service; 30, identifier:list; 31, argument_list; 31, 32; 31, 42; 31, 43; 32, binary_operator:+; 32, 33; 32, 41; 33, binary_operator:+; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:base; 37, call; 37, 38; 37, 39; 38, identifier:str; 39, argument_list; 39, 40; 40, identifier:id; 41, string:'/logdir/'; 42, identifier:filter; 43, identifier:sort; 44, return_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:service; 50, identifier:decode; 51, argument_list; 51, 52; 51, 53; 51, 54; 52, identifier:schema; 53, identifier:resp; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:many; 56, True
def list_logdir(self, id, filter=None, sort=None): # pylint: disable=invalid-name,redefined-builtin """Get a list of logdir files. :param id: Result ID as an int. :param filter: Filter to apply as string. :param sort: Sort field to apply as string. :return: :class:`results.LogDirFile <results.LogDirFile>` list """ schema = LogDirFileSchema() resp = self.service.list(self.base+str(id)+'/logdir/', filter, sort) return self.service.decode(schema, resp, many=True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_init_report; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 20; 5, 21; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:sections; 13, list:[]; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:section_names; 19, list:[]; 20, comment; 21, try_statement; 21, 22; 21, 91; 21, 95; 22, block; 22, 23; 22, 63; 22, 64; 23, if_statement; 23, 24; 23, 36; 24, comparison_operator:is; 24, 25; 24, 35; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:os; 29, identifier:path; 30, identifier:isdir; 31, argument_list; 31, 32; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:directory; 35, False; 36, block; 36, 37; 36, 54; 37, if_statement; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:verbose; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:print; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, string:"Created directory {}"; 49, identifier:format; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:directory; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:os; 58, identifier:mkdir; 59, argument_list; 59, 60; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:directory; 63, comment; 64, for_statement; 64, 65; 64, 66; 64, 69; 65, identifier:this; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:_to_create; 69, block; 69, 70; 70, try_statement; 70, 71; 70, 87; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:os; 76, identifier:mkdir; 77, argument_list; 77, 78; 78, binary_operator:+; 78, 79; 78, 86; 79, binary_operator:+; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:directory; 83, attribute; 83, 84; 83, 85; 84, identifier:os; 85, identifier:sep; 86, identifier:this; 87, except_clause; 87, 88; 88, block; 88, 89; 88, 90; 89, pass_statement; 90, comment; 91, except_clause; 91, 92; 91, 93; 92, identifier:Exception; 93, block; 93, 94; 94, pass_statement; 95, finally_clause; 95, 96; 95, 97; 96, comment; 97, block; 97, 98; 97, 107; 97, 121; 97, 122; 97, 141; 97, 142; 97, 143; 97, 163; 97, 164; 97, 170; 97, 209; 97, 210; 97, 267; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:temp_path; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:easydev; 104, identifier:get_package_location; 105, argument_list; 105, 106; 106, string:"reports"; 107, expression_statement; 107, 108; 108, augmented_assignment:+=; 108, 109; 108, 110; 109, identifier:temp_path; 110, binary_operator:+; 110, 111; 110, 120; 111, binary_operator:+; 111, 112; 111, 117; 112, binary_operator:+; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:os; 115, identifier:sep; 116, string:"reports"; 117, attribute; 117, 118; 117, 119; 118, identifier:os; 119, identifier:sep; 120, string:"resources"; 121, comment; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:filenames; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:glob; 128, identifier:glob; 129, argument_list; 129, 130; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:os; 134, identifier:sep; 135, identifier:join; 136, argument_list; 136, 137; 137, list:[temp_path, "css", "*.css"]; 137, 138; 137, 139; 137, 140; 138, identifier:temp_path; 139, string:"css"; 140, string:"*.css"; 141, comment; 142, comment; 143, expression_statement; 143, 144; 144, augmented_assignment:+=; 144, 145; 144, 146; 145, identifier:filenames; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:glob; 149, identifier:glob; 150, argument_list; 150, 151; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:os; 155, identifier:sep; 156, identifier:join; 157, argument_list; 157, 158; 158, list:[self.searchpath, '*.css']; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:searchpath; 162, string:'*.css'; 163, comment; 164, expression_statement; 164, 165; 165, augmented_assignment:+=; 165, 166; 165, 167; 166, identifier:filenames; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:extra_css_list; 170, for_statement; 170, 171; 170, 172; 170, 173; 171, identifier:filename; 172, identifier:filenames; 173, block; 173, 174; 173, 189; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:target; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:os; 181, identifier:sep; 182, identifier:join; 183, argument_list; 183, 184; 184, list:[self.directory, 'css' ]; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:directory; 188, string:'css'; 189, if_statement; 189, 190; 189, 200; 190, comparison_operator:is; 190, 191; 190, 199; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:os; 195, identifier:path; 196, identifier:isfile; 197, argument_list; 197, 198; 198, identifier:target; 199, False; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:shutil; 205, identifier:copy; 206, argument_list; 206, 207; 206, 208; 207, identifier:filename; 208, identifier:target; 209, comment; 210, for_statement; 210, 211; 210, 212; 210, 216; 211, identifier:filename; 212, list:['sorttable.js', 'highlight.pack.js', "jquery-1.12.3.min.js"]; 212, 213; 212, 214; 212, 215; 213, string:'sorttable.js'; 214, string:'highlight.pack.js'; 215, string:"jquery-1.12.3.min.js"; 216, block; 216, 217; 216, 233; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:target; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:os; 224, identifier:sep; 225, identifier:join; 226, argument_list; 226, 227; 227, list:[self.directory, 'js', filename ]; 227, 228; 227, 231; 227, 232; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:directory; 231, string:'js'; 232, identifier:filename; 233, if_statement; 233, 234; 233, 244; 234, comparison_operator:is; 234, 235; 234, 243; 235, call; 235, 236; 235, 241; 236, attribute; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:os; 239, identifier:path; 240, identifier:isfile; 241, argument_list; 241, 242; 242, identifier:target; 243, False; 244, block; 244, 245; 244, 259; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:filename; 248, call; 248, 249; 248, 254; 249, attribute; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:os; 252, identifier:sep; 253, identifier:join; 254, argument_list; 254, 255; 255, list:[temp_path, "javascript", filename]; 255, 256; 255, 257; 255, 258; 256, identifier:temp_path; 257, string:"javascript"; 258, identifier:filename; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:shutil; 263, identifier:copy; 264, argument_list; 264, 265; 264, 266; 265, identifier:filename; 266, identifier:target; 267, for_statement; 267, 268; 267, 269; 267, 272; 268, identifier:filename; 269, attribute; 269, 270; 269, 271; 270, identifier:self; 271, identifier:extra_js_list; 272, block; 272, 273; 272, 284; 272, 300; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:basename; 276, call; 276, 277; 276, 282; 277, attribute; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:os; 280, identifier:path; 281, identifier:basename; 282, argument_list; 282, 283; 283, identifier:filename; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:target; 287, call; 287, 288; 287, 293; 288, attribute; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:os; 291, identifier:sep; 292, identifier:join; 293, argument_list; 293, 294; 294, list:[self.directory, 'js', basename ]; 294, 295; 294, 298; 294, 299; 295, attribute; 295, 296; 295, 297; 296, identifier:self; 297, identifier:directory; 298, string:'js'; 299, identifier:basename; 300, if_statement; 300, 301; 300, 311; 301, comparison_operator:is; 301, 302; 301, 310; 302, call; 302, 303; 302, 308; 303, attribute; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:os; 306, identifier:path; 307, identifier:isfile; 308, argument_list; 308, 309; 309, identifier:target; 310, False; 311, block; 311, 312; 312, expression_statement; 312, 313; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:shutil; 316, identifier:copy; 317, argument_list; 317, 318; 317, 319; 318, identifier:filename; 319, identifier:target
def _init_report(self): """create the report directory and return the directory name""" self.sections = [] self.section_names = [] # if the directory already exists, print a warning try: if os.path.isdir(self.directory) is False: if self.verbose: print("Created directory {}".format(self.directory)) os.mkdir(self.directory) # list of directories created in the constructor for this in self._to_create: try: os.mkdir(self.directory + os.sep + this) except: pass # already created ? except Exception: pass finally: # Once the main directory is created, copy files required temp_path = easydev.get_package_location("reports") temp_path += os.sep + "reports" + os.sep + "resources" # Copy the CSS from reports/resources/css filenames = glob.glob(os.sep.join([temp_path, "css", "*.css"])) # If there are CSS in the directory with JINJA templates, use them # as well filenames += glob.glob(os.sep.join([self.searchpath, '*.css'])) # In addition, the user may also provide his own CSS as a list filenames += self.extra_css_list for filename in filenames: target = os.sep.join([self.directory, 'css' ]) if os.path.isfile(target) is False: shutil.copy(filename, target) # We copy all javascript from reports resources for filename in ['sorttable.js', 'highlight.pack.js', "jquery-1.12.3.min.js"]: target = os.sep.join([self.directory, 'js', filename ]) if os.path.isfile(target) is False: filename = os.sep.join([temp_path, "javascript", filename]) shutil.copy(filename, target) for filename in self.extra_js_list: basename = os.path.basename(filename) target = os.sep.join([self.directory, 'js', basename ]) if os.path.isfile(target) is False: shutil.copy(filename, target)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:check_instance; 3, parameters; 3, 4; 4, identifier:function; 5, block; 5, 6; 5, 8; 5, 283; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 16; 9, function_name:wrapper; 10, parameters; 10, 11; 10, 12; 10, 14; 11, identifier:self; 12, list_splat_pattern; 12, 13; 13, identifier:args; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kwargs; 16, block; 16, 17; 16, 84; 16, 85; 16, 86; 16, 87; 16, 88; 16, 178; 16, 274; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:func_trans; 20, dictionary; 20, 21; 20, 26; 20, 31; 20, 36; 20, 41; 20, 46; 20, 51; 20, 56; 20, 63; 20, 70; 20, 77; 21, pair; 21, 22; 21, 23; 22, string:"commit"; 23, attribute; 23, 24; 23, 25; 24, identifier:manager; 25, identifier:Manager; 26, pair; 26, 27; 26, 28; 27, string:"compare_config"; 28, attribute; 28, 29; 28, 30; 29, identifier:manager; 30, identifier:Manager; 31, pair; 31, 32; 31, 33; 32, string:"commit_check"; 33, attribute; 33, 34; 33, 35; 34, identifier:manager; 35, identifier:Manager; 36, pair; 36, 37; 36, 38; 37, string:"device_info"; 38, attribute; 38, 39; 38, 40; 39, identifier:manager; 40, identifier:Manager; 41, pair; 41, 42; 41, 43; 42, string:"diff_config"; 43, attribute; 43, 44; 43, 45; 44, identifier:manager; 45, identifier:Manager; 46, pair; 46, 47; 46, 48; 47, string:"health_check"; 48, attribute; 48, 49; 48, 50; 49, identifier:manager; 50, identifier:Manager; 51, pair; 51, 52; 51, 53; 52, string:"interface_errors"; 53, attribute; 53, 54; 53, 55; 54, identifier:manager; 55, identifier:Manager; 56, pair; 56, 57; 56, 58; 57, string:"op_cmd"; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:paramiko; 61, identifier:client; 62, identifier:SSHClient; 63, pair; 63, 64; 63, 65; 64, string:"shell_cmd"; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:paramiko; 68, identifier:client; 69, identifier:SSHClient; 70, pair; 70, 71; 70, 72; 71, string:"scp_pull"; 72, attribute; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:paramiko; 75, identifier:client; 76, identifier:SSHClient; 77, pair; 77, 78; 77, 79; 78, string:"scp_push"; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:paramiko; 82, identifier:client; 83, identifier:SSHClient; 84, comment; 85, comment; 86, comment; 87, comment; 88, if_statement; 88, 89; 88, 100; 88, 144; 88, 145; 88, 146; 89, boolean_operator:and; 89, 90; 89, 95; 90, comparison_operator:==; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:username; 94, string:"root"; 95, comparison_operator:==; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:function; 98, identifier:__name__; 99, string:"op_cmd"; 100, block; 100, 101; 100, 119; 100, 137; 100, 143; 101, if_statement; 101, 102; 101, 106; 102, not_operator; 102, 103; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:_session; 106, block; 106, 107; 106, 113; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:conn_type; 112, string:"paramiko"; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:connect; 118, argument_list; 119, if_statement; 119, 120; 119, 124; 120, not_operator; 120, 121; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:_shell; 124, block; 124, 125; 124, 131; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:conn_type; 130, string:"root"; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:connect; 136, argument_list; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:shell_to_cli; 142, argument_list; 143, comment; 144, comment; 145, comment; 146, elif_clause; 146, 147; 146, 152; 147, comparison_operator:==; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:function; 150, identifier:__name__; 151, string:'shell_cmd'; 152, block; 152, 153; 152, 171; 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:_shell; 158, block; 158, 159; 158, 165; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:conn_type; 164, string:"shell"; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:connect; 170, argument_list; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:cli_to_shell; 176, argument_list; 177, comment; 178, if_statement; 178, 179; 178, 190; 178, 191; 178, 192; 178, 224; 179, call; 179, 180; 179, 181; 180, identifier:isinstance; 181, argument_list; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:_session; 185, subscript; 185, 186; 185, 187; 186, identifier:func_trans; 187, attribute; 187, 188; 187, 189; 188, identifier:function; 189, identifier:__name__; 190, comment; 191, comment; 192, block; 192, 193; 193, if_statement; 193, 194; 193, 201; 194, comparison_operator:in; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:function; 197, identifier:__name__; 198, list:['scp_pull', 'scp_push']; 198, 199; 198, 200; 199, string:'scp_pull'; 200, string:'scp_push'; 201, block; 201, 202; 202, if_statement; 202, 203; 202, 211; 203, not_operator; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:isinstance; 206, argument_list; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:_scp; 210, identifier:SCPClient; 211, block; 211, 212; 211, 218; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:conn_type; 217, string:"scp"; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:self; 222, identifier:connect; 223, argument_list; 224, else_clause; 224, 225; 225, block; 225, 226; 225, 232; 225, 268; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:disconnect; 231, argument_list; 232, if_statement; 232, 233; 232, 238; 232, 245; 232, 260; 233, comparison_operator:==; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:function; 236, identifier:__name__; 237, string:"op_cmd"; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:conn_type; 244, string:"paramiko"; 245, elif_clause; 245, 246; 245, 253; 246, comparison_operator:in; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:function; 249, identifier:__name__; 250, list:["scp_pull", "scp_push"]; 250, 251; 250, 252; 251, string:"scp_pull"; 252, string:"scp_push"; 253, block; 253, 254; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:conn_type; 259, string:"scp"; 260, else_clause; 260, 261; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:self; 266, identifier:conn_type; 267, string:"ncclient"; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:connect; 273, argument_list; 274, return_statement; 274, 275; 275, call; 275, 276; 275, 277; 276, identifier:function; 277, argument_list; 277, 278; 277, 279; 277, 281; 278, identifier:self; 279, list_splat; 279, 280; 280, identifier:args; 281, dictionary_splat; 281, 282; 282, identifier:kwargs; 283, return_statement; 283, 284; 284, identifier:wrapper
def check_instance(function): """ Wrapper that tests the type of _session. Purpose: This decorator function is used by all functions within | the Jaide class that interact with a device to ensure the | proper session type is in use. If it is not, it will | attempt to migrate _session to that type before moving | to the originally requested function. | > **NOTE:** This function is a decorator, and should not be | > used directly. All other methods in this class that touch | > the Junos device are wrapped by this function to ensure the | > proper connection type is used. @param function: the function that is being wrapped around @type function: function @returns: the originally requested function @rtype: function """ def wrapper(self, *args, **kwargs): func_trans = { "commit": manager.Manager, "compare_config": manager.Manager, "commit_check": manager.Manager, "device_info": manager.Manager, "diff_config": manager.Manager, "health_check": manager.Manager, "interface_errors": manager.Manager, "op_cmd": paramiko.client.SSHClient, "shell_cmd": paramiko.client.SSHClient, "scp_pull": paramiko.client.SSHClient, "scp_push": paramiko.client.SSHClient } # when doing an operational command, logging in as root # brings you to shell, so we need to enter the device as a shell # connection, and move to cli to perform the command # this is a one-off because the isinstance() check will be bypassed if self.username == "root" and function.__name__ == "op_cmd": if not self._session: self.conn_type = "paramiko" self.connect() if not self._shell: self.conn_type = "root" self.connect() self.shell_to_cli() # check if we're in the cli # Have to call shell command separately, since we are using _shell # for comparison, not _session. elif function.__name__ == 'shell_cmd': if not self._shell: self.conn_type = "shell" self.connect() self.cli_to_shell() # check if we're in shell. if isinstance(self._session, func_trans[function.__name__]): # If they're doing SCP, we have to check for both _session and # _scp if function.__name__ in ['scp_pull', 'scp_push']: if not isinstance(self._scp, SCPClient): self.conn_type = "scp" self.connect() else: self.disconnect() if function.__name__ == "op_cmd": self.conn_type = "paramiko" elif function.__name__ in ["scp_pull", "scp_push"]: self.conn_type = "scp" else: self.conn_type = "ncclient" self.connect() return function(self, *args, **kwargs) return wrapper
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:commit; 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:commands; 7, string:""; 8, default_parameter; 8, 9; 8, 10; 9, identifier:confirmed; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:comment; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:at_time; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:synchronize; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:req_format; 22, string:'text'; 23, block; 23, 24; 23, 26; 23, 27; 23, 28; 23, 36; 23, 40; 23, 54; 23, 55; 23, 61; 23, 75; 23, 79; 23, 80; 23, 81; 23, 130; 23, 136; 23, 268; 24, expression_statement; 24, 25; 25, comment; 26, comment; 27, comment; 28, if_statement; 28, 29; 28, 31; 29, not_operator; 29, 30; 30, identifier:commands; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:commands; 35, string:'annotate system ""'; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:clean_cmds; 39, list:[]; 40, for_statement; 40, 41; 40, 42; 40, 46; 41, identifier:cmd; 42, call; 42, 43; 42, 44; 43, identifier:clean_lines; 44, argument_list; 44, 45; 45, identifier:commands; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:clean_cmds; 51, identifier:append; 52, argument_list; 52, 53; 53, identifier:cmd; 54, comment; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:lock; 60, argument_list; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 68; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:_session; 67, identifier:load_configuration; 68, argument_list; 68, 69; 68, 72; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:action; 71, string:'set'; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:config; 74, identifier:commands; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:results; 78, string:""; 79, comment; 80, comment; 81, if_statement; 81, 82; 81, 83; 81, 109; 82, identifier:confirmed; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:results; 87, call; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:_session; 92, identifier:commit; 93, argument_list; 93, 94; 93, 97; 93, 103; 93, 106; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:confirmed; 96, True; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:timeout; 99, call; 99, 100; 99, 101; 100, identifier:str; 101, argument_list; 101, 102; 102, identifier:confirmed; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:comment; 105, identifier:comment; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:synchronize; 108, identifier:synchronize; 109, else_clause; 109, 110; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:results; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_session; 119, identifier:commit; 120, argument_list; 120, 121; 120, 124; 120, 127; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:comment; 123, identifier:comment; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:at_time; 126, identifier:at_time; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:synchronize; 129, identifier:synchronize; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:unlock; 135, argument_list; 136, if_statement; 136, 137; 136, 138; 137, identifier:results; 138, block; 138, 139; 138, 146; 138, 147; 138, 148; 138, 159; 138, 163; 138, 266; 139, if_statement; 139, 140; 139, 143; 140, comparison_operator:==; 140, 141; 140, 142; 141, identifier:req_format; 142, string:'xml'; 143, block; 143, 144; 144, return_statement; 144, 145; 145, identifier:results; 146, comment; 147, comment; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:results; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:ET; 154, identifier:fromstring; 155, argument_list; 155, 156; 156, attribute; 156, 157; 156, 158; 157, identifier:results; 158, identifier:tostring; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:out; 162, string:''; 163, for_statement; 163, 164; 163, 165; 163, 170; 163, 171; 163, 172; 164, identifier:i; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:results; 168, identifier:iter; 169, argument_list; 170, comment; 171, comment; 172, block; 172, 173; 173, if_statement; 173, 174; 173, 179; 173, 184; 173, 195; 173, 206; 173, 207; 173, 208; 173, 240; 173, 241; 173, 242; 174, comparison_operator:==; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:i; 177, identifier:tag; 178, string:'commit-check-success'; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, augmented_assignment:+=; 181, 182; 181, 183; 182, identifier:out; 183, string:'configuration check succeeds\n'; 184, elif_clause; 184, 185; 184, 190; 185, comparison_operator:==; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:i; 188, identifier:tag; 189, string:'commit-success'; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, augmented_assignment:+=; 192, 193; 192, 194; 193, identifier:out; 194, string:'commit complete\n'; 195, elif_clause; 195, 196; 195, 201; 196, comparison_operator:==; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:i; 199, identifier:tag; 200, string:'ok'; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, augmented_assignment:+=; 203, 204; 203, 205; 204, identifier:out; 205, string:'commit complete\n'; 206, comment; 207, comment; 208, elif_clause; 208, 209; 208, 214; 209, comparison_operator:is; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:i; 212, identifier:text; 213, None; 214, block; 214, 215; 215, if_statement; 215, 216; 215, 227; 216, comparison_operator:!=; 216, 217; 216, 226; 217, binary_operator:+; 217, 218; 217, 225; 218, call; 218, 219; 218, 224; 219, attribute; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:i; 222, identifier:text; 223, identifier:strip; 224, argument_list; 225, string:'\n'; 226, string:'\n'; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, augmented_assignment:+=; 229, 230; 229, 231; 230, identifier:out; 231, binary_operator:+; 231, 232; 231, 239; 232, call; 232, 233; 232, 238; 233, attribute; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:i; 236, identifier:text; 237, identifier:strip; 238, argument_list; 239, string:'\n'; 240, comment; 241, comment; 242, elif_clause; 242, 243; 242, 248; 243, comparison_operator:is; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:i; 246, identifier:text; 247, None; 248, block; 248, 249; 249, if_statement; 249, 250; 249, 257; 250, comparison_operator:!=; 250, 251; 250, 256; 251, binary_operator:+; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:i; 254, identifier:tag; 255, string:'\n'; 256, string:'\n'; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, augmented_assignment:+=; 259, 260; 259, 261; 260, identifier:out; 261, binary_operator:+; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:i; 264, identifier:tag; 265, string:'\n'; 266, return_statement; 266, 267; 267, identifier:out; 268, return_statement; 268, 269; 269, False
def commit(self, commands="", confirmed=None, comment=None, at_time=None, synchronize=False, req_format='text'): """ Perform a commit operation. Purpose: Executes a commit operation. All parameters are optional. | commit confirm and commit at are mutually exclusive. All | the others can be used with each other and commit confirm/at. @param commands: A string or list of multiple commands | that the device will compare with. | If a string, it can be a single command, | multiple commands separated by commas, or | a filepath location of a file with multiple | commands, each on its own line. @type commands: str or list @param confirmed: integer value of the number of **seconds** to | confirm the commit for, if requested. @type confirmed: int @param comment: string that the user wants to comment the commit | with. Will show up in the 'show system commit' log. @type comment: str @param at_time: string designating the time at which the commit | should happen. Can be in one of two Junos approved | formats. @type comment: str @param synchronize: boolean set to true if desiring a commit | synchronize operation. @type synchronize: bool @param req_format: string to specify the response format. Accepts | either 'text' or 'xml' @type req_format: str @returns: The reply from the device. @rtype: str """ # ncclient doesn't support a truly blank commit, so if nothing is # passed, use 'annotate system' to make a blank commit if not commands: commands = 'annotate system ""' clean_cmds = [] for cmd in clean_lines(commands): clean_cmds.append(cmd) # try to lock the candidate config so we can make changes. self.lock() self._session.load_configuration(action='set', config=commands) results = "" # confirmed and commit at are mutually exclusive. commit confirm # takes precedence. if confirmed: results = self._session.commit(confirmed=True, timeout=str(confirmed), comment=comment, synchronize=synchronize) else: results = self._session.commit(comment=comment, at_time=at_time, synchronize=synchronize) self.unlock() if results: if req_format == 'xml': return results # commit() DOES NOT return a parse-able xml tree, so we # convert it to an ElementTree xml tree. results = ET.fromstring(results.tostring) out = '' for i in results.iter(): # the success message is just a tag, so we need to get it # specifically. if i.tag == 'commit-check-success': out += 'configuration check succeeds\n' elif i.tag == 'commit-success': out += 'commit complete\n' elif i.tag == 'ok': out += 'commit complete\n' # this is for normal output with a tag and inner text, it will # strip the inner text and add it to the output. elif i.text is not None: if i.text.strip() + '\n' != '\n': out += i.text.strip() + '\n' # this is for elements that don't have inner text, # it will add the tag to the output. elif i.text is None: if i.tag + '\n' != '\n': out += i.tag + '\n' return out return False