sequence
stringlengths 546
16.2k
| code
stringlengths 108
19.3k
|
---|---|
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_raw_counts; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 17; 5, 23; 5, 24; 5, 25; 5, 361; 5, 388; 5, 415; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:words; 12, list:[]; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:labels; 16, list:[]; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:words_said; 20, call; 20, 21; 20, 22; 21, identifier:set; 22, argument_list; 23, comment; 24, comment; 25, for_statement; 25, 26; 25, 27; 25, 30; 26, identifier:unit; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:parsed_response; 30, block; 30, 31; 30, 37; 30, 41; 30, 115; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:word; 34, attribute; 34, 35; 34, 36; 35, identifier:unit; 36, identifier:text; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:test; 40, False; 41, if_statement; 41, 42; 41, 47; 41, 98; 42, comparison_operator:==; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:type; 46, string:"PHONETIC"; 47, block; 47, 48; 47, 97; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:test; 51, parenthesized_expression; 51, 52; 52, boolean_operator:and; 52, 53; 52, 87; 52, 88; 53, boolean_operator:and; 53, 54; 53, 79; 53, 80; 54, boolean_operator:and; 54, 55; 54, 75; 54, 76; 55, boolean_operator:and; 55, 56; 55, 72; 56, boolean_operator:and; 56, 57; 56, 69; 57, boolean_operator:and; 57, 58; 57, 66; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:word; 61, identifier:startswith; 62, argument_list; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:letter; 66, comparison_operator:not; 66, 67; 66, 68; 67, string:"T_"; 68, identifier:word; 69, comparison_operator:not; 69, 70; 69, 71; 70, string:"E_"; 71, identifier:word; 72, comparison_operator:not; 72, 73; 72, 74; 73, string:"!"; 74, identifier:word; 75, comment; 76, comparison_operator:not; 76, 77; 76, 78; 77, string:"FILLEDPAUSE_"; 78, identifier:word; 79, comment; 80, not_operator; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:word; 84, identifier:endswith; 85, argument_list; 85, 86; 86, string:'-'; 87, comment; 88, comparison_operator:in; 88, 89; 88, 94; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:word; 92, identifier:lower; 93, argument_list; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:english_words; 97, comment; 98, elif_clause; 98, 99; 98, 104; 98, 105; 99, comparison_operator:==; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:type; 103, string:"SEMANTIC"; 104, comment; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:test; 109, parenthesized_expression; 109, 110; 110, comparison_operator:in; 110, 111; 110, 112; 111, identifier:word; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:permissible_words; 115, if_statement; 115, 116; 115, 117; 115, 219; 115, 253; 115, 283; 115, 317; 116, identifier:test; 117, block; 117, 118; 117, 126; 117, 134; 117, 205; 117, 212; 118, expression_statement; 118, 119; 119, augmented_assignment:+=; 119, 120; 119, 125; 120, subscript; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:measures; 124, string:'COUNT_total_words'; 125, integer:1; 126, expression_statement; 126, 127; 127, augmented_assignment:+=; 127, 128; 127, 133; 128, subscript; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:measures; 132, string:'COUNT_permissible_words'; 133, integer:1; 134, if_statement; 134, 135; 134, 144; 134, 160; 134, 196; 135, call; 135, 136; 135, 137; 136, identifier:any; 137, generator_expression; 137, 138; 137, 141; 138, comparison_operator:==; 138, 139; 138, 140; 139, identifier:word; 140, identifier:w; 141, for_in_clause; 141, 142; 141, 143; 142, identifier:w; 143, identifier:words_said; 144, block; 144, 145; 144, 153; 145, expression_statement; 145, 146; 146, augmented_assignment:+=; 146, 147; 146, 152; 147, subscript; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:measures; 151, string:'COUNT_exact_repetitions'; 152, integer:1; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:labels; 157, identifier:append; 158, argument_list; 158, 159; 159, string:'EXACT REPETITION'; 160, elif_clause; 160, 161; 160, 180; 161, call; 161, 162; 161, 163; 162, identifier:any; 163, generator_expression; 163, 164; 163, 177; 164, comparison_operator:==; 164, 165; 164, 171; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:stemmer; 168, identifier:stem; 169, argument_list; 169, 170; 170, identifier:word; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:stemmer; 174, identifier:stem; 175, argument_list; 175, 176; 176, identifier:w; 177, for_in_clause; 177, 178; 177, 179; 178, identifier:w; 179, identifier:words_said; 180, block; 180, 181; 180, 189; 181, expression_statement; 181, 182; 182, augmented_assignment:+=; 182, 183; 182, 188; 183, subscript; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:measures; 187, string:'COUNT_stem_repetitions'; 188, integer:1; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:labels; 193, identifier:append; 194, argument_list; 194, 195; 195, string:'STEM REPETITION'; 196, else_clause; 196, 197; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:labels; 202, identifier:append; 203, argument_list; 203, 204; 204, string:'PERMISSIBLE WORD'; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:words_said; 209, identifier:add; 210, argument_list; 210, 211; 211, identifier:word; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:words; 216, identifier:append; 217, argument_list; 217, 218; 218, identifier:word; 219, elif_clause; 219, 220; 219, 230; 220, call; 220, 221; 220, 228; 221, attribute; 221, 222; 221, 227; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:word; 225, identifier:lower; 226, argument_list; 227, identifier:startswith; 228, argument_list; 228, 229; 229, string:'e_'; 230, block; 230, 231; 230, 239; 230, 246; 231, expression_statement; 231, 232; 232, augmented_assignment:+=; 232, 233; 232, 238; 233, subscript; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:self; 236, identifier:measures; 237, string:'COUNT_examiner_words'; 238, integer:1; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:words; 243, identifier:append; 244, argument_list; 244, 245; 245, identifier:word; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:labels; 250, identifier:append; 251, argument_list; 251, 252; 252, string:'EXAMINER WORD'; 253, elif_clause; 253, 254; 253, 260; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:word; 257, identifier:endswith; 258, argument_list; 258, 259; 259, string:'-'; 260, block; 260, 261; 260, 269; 260, 276; 261, expression_statement; 261, 262; 262, augmented_assignment:+=; 262, 263; 262, 268; 263, subscript; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:self; 266, identifier:measures; 267, string:'COUNT_word_fragments'; 268, integer:1; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:words; 273, identifier:append; 274, argument_list; 274, 275; 275, identifier:word; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:labels; 280, identifier:append; 281, argument_list; 281, 282; 282, string:'WORD FRAGMENT'; 283, elif_clause; 283, 284; 283, 294; 284, call; 284, 285; 284, 292; 285, attribute; 285, 286; 285, 291; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:word; 289, identifier:lower; 290, argument_list; 291, identifier:startswith; 292, argument_list; 292, 293; 293, string:'filledpause'; 294, block; 294, 295; 294, 303; 294, 310; 295, expression_statement; 295, 296; 296, augmented_assignment:+=; 296, 297; 296, 302; 297, subscript; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:self; 300, identifier:measures; 301, string:'COUNT_filled_pauses'; 302, integer:1; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:words; 307, identifier:append; 308, argument_list; 308, 309; 309, identifier:word; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:labels; 314, identifier:append; 315, argument_list; 315, 316; 316, string:'FILLED PAUSE'; 317, elif_clause; 317, 318; 317, 330; 318, comparison_operator:not; 318, 319; 318, 324; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:word; 322, identifier:lower; 323, argument_list; 324, list:['!sil', 't_noise', 't_cough', 't_lipsmack', 't_breath']; 324, 325; 324, 326; 324, 327; 324, 328; 324, 329; 325, string:'!sil'; 326, string:'t_noise'; 327, string:'t_cough'; 328, string:'t_lipsmack'; 329, string:'t_breath'; 330, block; 330, 331; 330, 339; 330, 347; 330, 354; 331, expression_statement; 331, 332; 332, augmented_assignment:+=; 332, 333; 332, 338; 333, subscript; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:self; 336, identifier:measures; 337, string:'COUNT_total_words'; 338, integer:1; 339, expression_statement; 339, 340; 340, augmented_assignment:+=; 340, 341; 340, 346; 341, subscript; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:self; 344, identifier:measures; 345, string:'COUNT_asides'; 346, integer:1; 347, expression_statement; 347, 348; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:words; 351, identifier:append; 352, argument_list; 352, 353; 353, identifier:word; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:labels; 358, identifier:append; 359, argument_list; 359, 360; 360, string:'ASIDE'; 361, if_statement; 361, 362; 361, 366; 362, not_operator; 362, 363; 363, attribute; 363, 364; 363, 365; 364, identifier:self; 365, identifier:quiet; 366, block; 366, 367; 366, 369; 366, 371; 367, expression_statement; 367, 368; 368, identifier:print; 369, print_statement; 369, 370; 370, string:"Labels:"; 371, expression_statement; 371, 372; 372, call; 372, 373; 372, 374; 373, identifier:print_table; 374, argument_list; 374, 375; 375, list_comprehension; 375, 376; 375, 379; 376, tuple; 376, 377; 376, 378; 377, identifier:word; 378, identifier:label; 379, for_in_clause; 379, 380; 379, 383; 380, pattern_list; 380, 381; 380, 382; 381, identifier:word; 382, identifier:label; 383, call; 383, 384; 383, 385; 384, identifier:zip; 385, argument_list; 385, 386; 385, 387; 386, identifier:words; 387, identifier:labels; 388, expression_statement; 388, 389; 389, assignment; 389, 390; 389, 395; 389, 396; 390, subscript; 390, 391; 390, 394; 391, attribute; 391, 392; 391, 393; 392, identifier:self; 393, identifier:measures; 394, string:'COUNT_unique_permissible_words'; 395, line_continuation:\; 396, binary_operator:-; 396, 397; 396, 409; 396, 410; 397, binary_operator:-; 397, 398; 397, 403; 397, 404; 398, subscript; 398, 399; 398, 402; 399, attribute; 399, 400; 399, 401; 400, identifier:self; 401, identifier:measures; 402, string:'COUNT_permissible_words'; 403, line_continuation:\; 404, subscript; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, identifier:self; 407, identifier:measures; 408, string:'COUNT_exact_repetitions'; 409, line_continuation:\; 410, subscript; 410, 411; 410, 414; 411, attribute; 411, 412; 411, 413; 412, identifier:self; 413, identifier:measures; 414, string:'COUNT_stem_repetitions'; 415, if_statement; 415, 416; 415, 420; 416, not_operator; 416, 417; 417, attribute; 417, 418; 417, 419; 418, identifier:self; 419, identifier:quiet; 420, block; 420, 421; 420, 423; 420, 425; 420, 442; 420, 448; 421, expression_statement; 421, 422; 422, identifier:print; 423, print_statement; 423, 424; 424, string:"Counts:"; 425, expression_statement; 425, 426; 426, assignment; 426, 427; 426, 428; 427, identifier:collection_measures; 428, list_comprehension; 428, 429; 428, 430; 428, 435; 429, identifier:x; 430, for_in_clause; 430, 431; 430, 432; 431, identifier:x; 432, attribute; 432, 433; 432, 434; 433, identifier:self; 434, identifier:measures; 435, if_clause; 435, 436; 436, call; 436, 437; 436, 440; 437, attribute; 437, 438; 437, 439; 438, identifier:x; 439, identifier:startswith; 440, argument_list; 440, 441; 441, string:"COUNT_"; 442, expression_statement; 442, 443; 443, call; 443, 444; 443, 447; 444, attribute; 444, 445; 444, 446; 445, identifier:collection_measures; 446, identifier:sort; 447, argument_list; 448, if_statement; 448, 449; 448, 453; 449, not_operator; 449, 450; 450, attribute; 450, 451; 450, 452; 451, identifier:self; 452, identifier:quiet; 453, block; 453, 454; 454, expression_statement; 454, 455; 455, call; 455, 456; 455, 457; 456, identifier:print_table; 457, argument_list; 457, 458; 458, list_comprehension; 458, 459; 458, 469; 459, tuple; 459, 460; 459, 461; 460, identifier:k; 461, call; 461, 462; 461, 463; 462, identifier:str; 463, argument_list; 463, 464; 464, subscript; 464, 465; 464, 468; 465, attribute; 465, 466; 465, 467; 466, identifier:self; 467, identifier:measures; 468, identifier:k; 469, for_in_clause; 469, 470; 469, 471; 470, identifier:k; 471, identifier:collection_measures | def get_raw_counts(self):
"""Determines counts for unique words, repetitions, etc using the raw text response.
Adds the following measures to the self.measures dictionary:
- COUNT_total_words: count of words (i.e. utterances with semantic content) spoken
by the subject. Filled pauses, silences, coughs, breaths, words by the interviewer,
etc. are all excluded from this count.
- COUNT_permissible_words: Number of words spoken by the subject that qualify as a
valid response according to the clustering criteria. Compound words are counted
as a single word in SEMANTIC clustering, but as two words in PHONETIC clustering.
This is implemented by tokenizing SEMANTIC clustering responses in the __init__
method before calling the current method.
- COUNT_exact_repetitions: Number of words which repeat words spoken earlier in the
response. Responses in SEMANTIC clustering are lemmatized before this function is
called, so slight variations (dog, dogs) may be counted as exact responses.
- COUNT_stem_repetitions: Number of words stems identical to words uttered earlier in
the response, according to the Porter Stemmer. For example, 'sled' and 'sledding'
have the same stem ('sled'), and 'sledding' would be counted as a stem repetition.
- COUNT_examiner_words: Number of words uttered by the examiner. These start
with "E_" in .TextGrid files.
- COUNT_filled_pauses: Number of filled pauses uttered by the subject. These begin
with "FILLEDPAUSE_" in the .TextGrid file.
- COUNT_word_fragments: Number of word fragments uttered by the subject. These
end with "-" in the .TextGrid file.
- COUNT_asides: Words spoken by the subject that do not adhere to the test criteria are
counted as asides, i.e. words that do not start with the appropriate letter or that
do not represent an animal.
- COUNT_unique_permissible_words: Number of works spoken by the subject, less asides,
stem repetitions and exact repetitions.
"""
#for making the table at the end
words = []
labels = []
words_said = set()
# Words like "polar_bear" as one semantically but two phonetically
# Uncategorizable words are counted as asides
for unit in self.parsed_response:
word = unit.text
test = False
if self.type == "PHONETIC":
test = (word.startswith(self.letter) and
"T_" not in word and "E_" not in word and "!" not in word and # Weed out tags
"FILLEDPAUSE_" not in word and # Weed out filled pauses
not word.endswith('-') and # Weed out false starts
word.lower() in self.english_words) #weed out non-words
elif self.type == "SEMANTIC":
#automatically weed out all non-semantically-appropriate responses
test = (word in self.permissible_words)
if test:
self.measures['COUNT_total_words'] += 1
self.measures['COUNT_permissible_words'] += 1
if any(word == w for w in words_said):
self.measures['COUNT_exact_repetitions'] += 1
labels.append('EXACT REPETITION')
elif any(stemmer.stem(word) == stemmer.stem(w) for w in words_said):
self.measures['COUNT_stem_repetitions'] += 1
labels.append('STEM REPETITION')
else:
labels.append('PERMISSIBLE WORD')
words_said.add(word)
words.append(word)
elif word.lower().startswith('e_'):
self.measures['COUNT_examiner_words'] += 1
words.append(word)
labels.append('EXAMINER WORD')
elif word.endswith('-'):
self.measures['COUNT_word_fragments'] += 1
words.append(word)
labels.append('WORD FRAGMENT')
elif word.lower().startswith('filledpause'):
self.measures['COUNT_filled_pauses'] += 1
words.append(word)
labels.append('FILLED PAUSE')
elif word.lower() not in ['!sil', 't_noise', 't_cough', 't_lipsmack', 't_breath']:
self.measures['COUNT_total_words'] += 1
self.measures['COUNT_asides'] += 1
words.append(word)
labels.append('ASIDE')
if not self.quiet:
print
print "Labels:"
print_table([(word,label) for word,label in zip(words,labels)])
self.measures['COUNT_unique_permissible_words'] = \
self.measures['COUNT_permissible_words'] - \
self.measures['COUNT_exact_repetitions'] - \
self.measures['COUNT_stem_repetitions']
if not self.quiet:
print
print "Counts:"
collection_measures = [x for x in self.measures if x.startswith("COUNT_")]
collection_measures.sort()
if not self.quiet:
print_table([(k, str(self.measures[k])) for k in collection_measures]) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:compute_similarity_score; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:unit1; 6, identifier:unit2; 7, block; 7, 8; 7, 10; 7, 390; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 16; 10, 227; 11, comparison_operator:==; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:type; 15, string:"PHONETIC"; 16, block; 16, 17; 16, 23; 16, 29; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:word1; 20, attribute; 20, 21; 20, 22; 21, identifier:unit1; 22, identifier:phonetic_representation; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:word2; 26, attribute; 26, 27; 26, 28; 27, identifier:unit2; 28, identifier:phonetic_representation; 29, if_statement; 29, 30; 29, 35; 29, 181; 30, comparison_operator:==; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:current_similarity_measure; 34, string:"phone"; 35, block; 35, 36; 35, 50; 35, 72; 35, 81; 35, 169; 35, 179; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 41; 38, pattern_list; 38, 39; 38, 40; 39, identifier:word1_length; 40, identifier:word2_length; 41, expression_list; 41, 42; 41, 46; 42, call; 42, 43; 42, 44; 43, identifier:len; 44, argument_list; 44, 45; 45, identifier:word1; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, identifier:word2; 50, if_statement; 50, 51; 50, 54; 50, 55; 51, comparison_operator:>; 51, 52; 51, 53; 52, identifier:word1_length; 53, identifier:word2_length; 54, comment; 55, block; 55, 56; 55, 64; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, pattern_list; 58, 59; 58, 60; 59, identifier:word1; 60, identifier:word2; 61, expression_list; 61, 62; 61, 63; 62, identifier:word2; 63, identifier:word1; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, pattern_list; 66, 67; 66, 68; 67, identifier:word1_length; 68, identifier:word2_length; 69, expression_list; 69, 70; 69, 71; 70, identifier:word2_length; 71, identifier:word1_length; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:current; 75, call; 75, 76; 75, 77; 76, identifier:range; 77, argument_list; 77, 78; 78, binary_operator:+; 78, 79; 78, 80; 79, identifier:word1_length; 80, integer:1; 81, for_statement; 81, 82; 81, 83; 81, 90; 82, identifier:i; 83, call; 83, 84; 83, 85; 84, identifier:range; 85, argument_list; 85, 86; 85, 87; 86, integer:1; 87, binary_operator:+; 87, 88; 87, 89; 88, identifier:word2_length; 89, integer:1; 90, block; 90, 91; 90, 105; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 96; 93, pattern_list; 93, 94; 93, 95; 94, identifier:previous; 95, identifier:current; 96, expression_list; 96, 97; 96, 98; 97, identifier:current; 98, binary_operator:+; 98, 99; 98, 101; 99, list:[i]; 99, 100; 100, identifier:i; 101, binary_operator:*; 101, 102; 101, 104; 102, list:[0]; 102, 103; 103, integer:0; 104, identifier:word1_length; 105, for_statement; 105, 106; 105, 107; 105, 114; 106, identifier:j; 107, call; 107, 108; 107, 109; 108, identifier:range; 109, argument_list; 109, 110; 109, 111; 110, integer:1; 111, binary_operator:+; 111, 112; 111, 113; 112, identifier:word1_length; 113, integer:1; 114, block; 114, 115; 114, 133; 114, 141; 114, 158; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, pattern_list; 117, 118; 117, 119; 118, identifier:add; 119, identifier:delete; 120, expression_list; 120, 121; 120, 126; 121, binary_operator:+; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:previous; 124, identifier:j; 125, integer:1; 126, binary_operator:+; 126, 127; 126, 132; 127, subscript; 127, 128; 127, 129; 128, identifier:current; 129, binary_operator:-; 129, 130; 129, 131; 130, identifier:j; 131, integer:1; 132, integer:1; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:change; 136, subscript; 136, 137; 136, 138; 137, identifier:previous; 138, binary_operator:-; 138, 139; 138, 140; 139, identifier:j; 140, integer:1; 141, if_statement; 141, 142; 141, 153; 142, comparison_operator:!=; 142, 143; 142, 148; 143, subscript; 143, 144; 143, 145; 144, identifier:word1; 145, binary_operator:-; 145, 146; 145, 147; 146, identifier:j; 147, integer:1; 148, subscript; 148, 149; 148, 150; 149, identifier:word2; 150, binary_operator:-; 150, 151; 150, 152; 151, identifier:i; 152, integer:1; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, augmented_assignment:+=; 155, 156; 155, 157; 156, identifier:change; 157, integer:1; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:current; 162, identifier:j; 163, call; 163, 164; 163, 165; 164, identifier:min; 165, argument_list; 165, 166; 165, 167; 165, 168; 166, identifier:add; 167, identifier:delete; 168, identifier:change; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:phonetic_similarity_score; 172, binary_operator:-; 172, 173; 172, 174; 173, integer:1; 174, binary_operator:/; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:current; 177, identifier:word1_length; 178, identifier:word2_length; 179, return_statement; 179, 180; 180, identifier:phonetic_similarity_score; 181, elif_clause; 181, 182; 181, 187; 182, comparison_operator:==; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:current_similarity_measure; 186, string:"biphone"; 187, block; 187, 188; 187, 225; 188, if_statement; 188, 189; 188, 214; 188, 219; 189, boolean_operator:or; 189, 190; 189, 201; 190, comparison_operator:==; 190, 191; 190, 196; 191, subscript; 191, 192; 191, 193; 192, identifier:word1; 193, slice; 193, 194; 193, 195; 194, colon; 195, integer:2; 196, subscript; 196, 197; 196, 198; 197, identifier:word2; 198, slice; 198, 199; 198, 200; 199, colon; 200, integer:2; 201, comparison_operator:==; 201, 202; 201, 208; 202, subscript; 202, 203; 202, 204; 203, identifier:word1; 204, slice; 204, 205; 204, 207; 205, unary_operator:-; 205, 206; 206, integer:2; 207, colon; 208, subscript; 208, 209; 208, 210; 209, identifier:word2; 210, slice; 210, 211; 210, 213; 211, unary_operator:-; 211, 212; 212, integer:2; 213, colon; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:common_biphone_score; 218, integer:1; 219, else_clause; 219, 220; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:common_biphone_score; 224, integer:0; 225, return_statement; 225, 226; 226, identifier:common_biphone_score; 227, elif_clause; 227, 228; 227, 233; 228, comparison_operator:==; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:type; 232, string:"SEMANTIC"; 233, block; 233, 234; 233, 240; 233, 246; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:word1; 237, attribute; 237, 238; 237, 239; 238, identifier:unit1; 239, identifier:text; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:word2; 243, attribute; 243, 244; 243, 245; 244, identifier:unit2; 245, identifier:text; 246, if_statement; 246, 247; 246, 252; 246, 334; 247, comparison_operator:==; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:self; 250, identifier:current_similarity_measure; 251, string:"lsa"; 252, block; 252, 253; 252, 261; 252, 269; 252, 270; 252, 271; 252, 272; 252, 291; 252, 307; 252, 323; 252, 332; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:w1_vec; 256, subscript; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:self; 259, identifier:term_vectors; 260, identifier:word1; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:w2_vec; 264, subscript; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:self; 267, identifier:term_vectors; 268, identifier:word2; 269, comment; 270, comment; 271, comment; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:dot; 275, call; 275, 276; 275, 277; 276, identifier:sum; 277, argument_list; 277, 278; 278, list_comprehension; 278, 279; 278, 282; 279, binary_operator:*; 279, 280; 279, 281; 280, identifier:w1; 281, identifier:w2; 282, for_in_clause; 282, 283; 282, 286; 283, pattern_list; 283, 284; 283, 285; 284, identifier:w1; 285, identifier:w2; 286, call; 286, 287; 286, 288; 287, identifier:zip; 288, argument_list; 288, 289; 288, 290; 289, identifier:w1_vec; 290, identifier:w2_vec; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:norm1; 294, call; 294, 295; 294, 296; 295, identifier:sqrt; 296, argument_list; 296, 297; 297, call; 297, 298; 297, 299; 298, identifier:sum; 299, argument_list; 299, 300; 300, list_comprehension; 300, 301; 300, 304; 301, binary_operator:*; 301, 302; 301, 303; 302, identifier:w; 303, identifier:w; 304, for_in_clause; 304, 305; 304, 306; 305, identifier:w; 306, identifier:w1_vec; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:norm2; 310, call; 310, 311; 310, 312; 311, identifier:sqrt; 312, argument_list; 312, 313; 313, call; 313, 314; 313, 315; 314, identifier:sum; 315, argument_list; 315, 316; 316, list_comprehension; 316, 317; 316, 320; 317, binary_operator:*; 317, 318; 317, 319; 318, identifier:w; 319, identifier:w; 320, for_in_clause; 320, 321; 320, 322; 321, identifier:w; 322, identifier:w2_vec; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:semantic_relatedness_score; 326, binary_operator:/; 326, 327; 326, 328; 327, identifier:dot; 328, parenthesized_expression; 328, 329; 329, binary_operator:*; 329, 330; 329, 331; 330, identifier:norm1; 331, identifier:norm2; 332, return_statement; 332, 333; 333, identifier:semantic_relatedness_score; 334, elif_clause; 334, 335; 334, 340; 334, 341; 335, comparison_operator:==; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:self; 338, identifier:current_similarity_measure; 339, string:"custom"; 340, comment; 341, block; 341, 342; 341, 388; 342, try_statement; 342, 343; 342, 354; 343, block; 343, 344; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 347; 346, identifier:similarity; 347, subscript; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:self; 350, identifier:custom_similarity_scores; 351, tuple; 351, 352; 351, 353; 352, identifier:word1; 353, identifier:word2; 354, except_clause; 354, 355; 354, 356; 355, identifier:KeyError; 356, block; 356, 357; 357, try_statement; 357, 358; 357, 369; 358, block; 358, 359; 359, expression_statement; 359, 360; 360, assignment; 360, 361; 360, 362; 361, identifier:similarity; 362, subscript; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:self; 365, identifier:custom_similarity_scores; 366, tuple; 366, 367; 366, 368; 367, identifier:word2; 368, identifier:word1; 369, except_clause; 369, 370; 369, 371; 370, identifier:KeyError; 371, block; 371, 372; 372, if_statement; 372, 373; 372, 376; 372, 383; 373, comparison_operator:==; 373, 374; 373, 375; 374, identifier:word1; 375, identifier:word2; 376, block; 376, 377; 376, 381; 376, 382; 377, return_statement; 377, 378; 378, attribute; 378, 379; 378, 380; 379, identifier:self; 380, identifier:same_word_similarity; 381, comment; 382, comment; 383, else_clause; 383, 384; 384, block; 384, 385; 384, 387; 385, return_statement; 385, 386; 386, integer:0; 387, comment; 388, return_statement; 388, 389; 389, identifier:similarity; 390, return_statement; 390, 391; 391, None | def compute_similarity_score(self, unit1, unit2):
""" Returns the similarity score between two words.
The type of similarity scoring method used depends on the currently active
method and clustering type.
:param unit1: Unit object corresponding to the first word.
:type unit1: Unit
:param unit2: Unit object corresponding to the second word.
:type unit2: Unit
:return: Number indicating degree of similarity of the two input words.
The maximum value is 1, and a higher value indicates that the words
are more similar.
:rtype : Float
The similarity method used depends both on the type of test being performed
(SEMANTIC or PHONETIC) and the similarity method currently assigned to the
self.current_similarity_measure property of the VFClustEngine object. The
similarity measures used are the following:
- PHONETIC/"phone": the phonetic similarity score (PSS) is calculated
between the phonetic representations of the input units. It is equal
to 1 minus the Levenshtein distance between two strings, normalized
to the length of the longer string. The strings should be compact
phonetic representations of the two words.
(This method is a modification of a Levenshtein distance function
available at http://hetland.org/coding/python/levenshtein.py.)
- PHONETIC/"biphone": the binary common-biphone score (CBS) depends
on whether two words share their initial and/or final biphone
(i.e., set of two phonemes). A score of 1 indicates that two words
have the same intial and/or final biphone; a score of 0 indicates
that two words have neither the same initial nor final biphone.
This is also calculated using the phonetic representation of the
two words.
- SEMANTIC/"lsa": a semantic relatedness score (SRS) is calculated
as the COSINE of the respective term vectors for the first and
second word in an LSA space of the specified clustering_parameter.
Unlike the PHONETIC methods, this method uses the .text property
of the input Unit objects.
"""
if self.type == "PHONETIC":
word1 = unit1.phonetic_representation
word2 = unit2.phonetic_representation
if self.current_similarity_measure == "phone":
word1_length, word2_length = len(word1), len(word2)
if word1_length > word2_length:
# Make sure n <= m, to use O(min(n,m)) space
word1, word2 = word2, word1
word1_length, word2_length = word2_length, word1_length
current = range(word1_length + 1)
for i in range(1, word2_length + 1):
previous, current = current, [i] + [0] * word1_length
for j in range(1, word1_length + 1):
add, delete = previous[j] + 1, current[j - 1] + 1
change = previous[j - 1]
if word1[j - 1] != word2[i - 1]:
change += 1
current[j] = min(add, delete, change)
phonetic_similarity_score = 1 - current[word1_length] / word2_length
return phonetic_similarity_score
elif self.current_similarity_measure == "biphone":
if word1[:2] == word2[:2] or word1[-2:] == word2[-2:]:
common_biphone_score = 1
else:
common_biphone_score = 0
return common_biphone_score
elif self.type == "SEMANTIC":
word1 = unit1.text
word2 = unit2.text
if self.current_similarity_measure == "lsa":
w1_vec = self.term_vectors[word1]
w2_vec = self.term_vectors[word2]
# semantic_relatedness_score = (numpy.dot(w1_vec, w2_vec) /
# numpy.linalg.norm(w1_vec) /
# numpy.linalg.norm(w2_vec))
dot = sum([w1*w2 for w1,w2 in zip(w1_vec, w2_vec)])
norm1 = sqrt(sum([w*w for w in w1_vec]))
norm2 = sqrt(sum([w*w for w in w2_vec]))
semantic_relatedness_score = dot/(norm1 * norm2)
return semantic_relatedness_score
elif self.current_similarity_measure == "custom":
#look it up in dict
try:
similarity = self.custom_similarity_scores[(word1,word2)]
except KeyError:
try:
similarity = self.custom_similarity_scores[(word2,word1)]
except KeyError:
if word1 == word2:
return self.same_word_similarity
#if they're the same word, they pass. This should only happen when checking with
# non-adjacent words in the same cluster
else:
return 0 #if words aren't found, they are defined as dissimilar
return similarity
return None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:compute_pairwise_similarity_score; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 16; 5, 95; 5, 96; 5, 110; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:pairs; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:all_scores; 15, list:[]; 16, for_statement; 16, 17; 16, 20; 16, 26; 17, pattern_list; 17, 18; 17, 19; 18, identifier:i; 19, identifier:unit; 20, call; 20, 21; 20, 22; 21, identifier:enumerate; 22, argument_list; 22, 23; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:parsed_response; 26, block; 26, 27; 27, for_statement; 27, 28; 27, 31; 27, 37; 28, pattern_list; 28, 29; 28, 30; 29, identifier:j; 30, identifier:other_unit; 31, call; 31, 32; 31, 33; 32, identifier:enumerate; 33, argument_list; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:parsed_response; 37, block; 37, 38; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:!=; 39, 40; 39, 41; 40, identifier:i; 41, identifier:j; 42, block; 42, 43; 42, 49; 42, 55; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:pair; 46, tuple; 46, 47; 46, 48; 47, identifier:i; 48, identifier:j; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:rev_pair; 52, tuple; 52, 53; 52, 54; 53, identifier:j; 54, identifier:i; 55, if_statement; 55, 56; 55, 63; 56, boolean_operator:and; 56, 57; 56, 60; 57, comparison_operator:not; 57, 58; 57, 59; 58, identifier:pair; 59, identifier:pairs; 60, comparison_operator:not; 60, 61; 60, 62; 61, identifier:rev_pair; 62, identifier:pairs; 63, block; 63, 64; 63, 74; 63, 81; 63, 88; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:score; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:compute_similarity_score; 71, argument_list; 71, 72; 71, 73; 72, identifier:unit; 73, identifier:other_unit; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:pairs; 78, identifier:append; 79, argument_list; 79, 80; 80, identifier:pair; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:pairs; 85, identifier:append; 86, argument_list; 86, 87; 87, identifier:rev_pair; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:all_scores; 92, identifier:append; 93, argument_list; 93, 94; 94, identifier:score; 95, comment; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:all_scores; 99, list_comprehension; 99, 100; 99, 101; 99, 104; 100, identifier:i; 101, for_in_clause; 101, 102; 101, 103; 102, identifier:i; 103, identifier:all_scores; 104, if_clause; 104, 105; 105, comparison_operator:!=; 105, 106; 105, 107; 106, identifier:i; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:same_word_similarity; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 123; 112, subscript; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:measures; 116, binary_operator:+; 116, 117; 116, 122; 117, binary_operator:+; 117, 118; 117, 119; 118, string:"COLLECTION_"; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:current_similarity_measure; 122, string:"_pairwise_similarity_score_mean"; 123, conditional_expression:if; 123, 124; 123, 128; 123, 129; 123, 135; 124, call; 124, 125; 124, 126; 125, identifier:get_mean; 126, argument_list; 126, 127; 127, identifier:all_scores; 128, line_continuation:\; 129, comparison_operator:>; 129, 130; 129, 134; 130, call; 130, 131; 130, 132; 131, identifier:len; 132, argument_list; 132, 133; 133, identifier:pairs; 134, integer:0; 135, string:'NA' | def compute_pairwise_similarity_score(self):
"""Computes the average pairwise similarity score between all pairs of Units.
The pairwise similarity is calculated as the sum of similarity scores for all pairwise
word pairs in a response -- except any pair composed of a word and
itself -- divided by the total number of words in an attempt. I.e.,
the mean similarity for all pairwise word pairs.
Adds the following measures to the self.measures dictionary:
- COLLECTION_collection_pairwise_similarity_score_mean: mean of pairwise similarity scores
.. todo: divide by (count-1)?
"""
pairs = []
all_scores = []
for i, unit in enumerate(self.parsed_response):
for j, other_unit in enumerate(self.parsed_response):
if i != j:
pair = (i, j)
rev_pair = (j, i)
if pair not in pairs and rev_pair not in pairs:
score = self.compute_similarity_score(unit, other_unit)
pairs.append(pair)
pairs.append(rev_pair)
all_scores.append(score)
#remove any "same word" from the mean
all_scores = [i for i in all_scores if i != self.same_word_similarity]
self.measures["COLLECTION_" + self.current_similarity_measure + "_pairwise_similarity_score_mean"] = get_mean(
all_scores) \
if len(pairs) > 0 else 'NA' |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:compute_between_collection_interval_duration; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:prefix; 6, block; 6, 7; 6, 9; 6, 13; 6, 14; 6, 48; 6, 49; 6, 79; 6, 80; 6, 81; 6, 103; 6, 125; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:durations; 12, list:[]; 13, comment; 14, for_statement; 14, 15; 14, 16; 14, 19; 14, 20; 15, identifier:collection; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:collection_list; 19, comment; 20, block; 20, 21; 20, 29; 20, 30; 20, 39; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:start; 24, attribute; 24, 25; 24, 28; 25, subscript; 25, 26; 25, 27; 26, identifier:collection; 27, integer:0; 28, identifier:start_time; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:end; 33, attribute; 33, 34; 33, 38; 34, subscript; 34, 35; 34, 36; 35, identifier:collection; 36, unary_operator:-; 36, 37; 37, integer:1; 38, identifier:end_time; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:durations; 43, identifier:append; 44, argument_list; 44, 45; 45, tuple; 45, 46; 45, 47; 46, identifier:start; 47, identifier:end; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:interstices; 52, list_comprehension; 52, 53; 52, 66; 53, binary_operator:-; 53, 54; 53, 61; 54, subscript; 54, 55; 54, 60; 55, subscript; 55, 56; 55, 57; 56, identifier:durations; 57, binary_operator:+; 57, 58; 57, 59; 58, identifier:i; 59, integer:1; 60, integer:0; 61, subscript; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:durations; 64, identifier:i; 65, integer:1; 66, for_in_clause; 66, 67; 66, 70; 67, pattern_list; 67, 68; 67, 69; 68, identifier:i; 69, identifier:d; 70, call; 70, 71; 70, 72; 71, identifier:enumerate; 72, argument_list; 72, 73; 73, subscript; 73, 74; 73, 75; 74, identifier:durations; 75, slice; 75, 76; 75, 77; 76, colon; 77, unary_operator:-; 77, 78; 78, integer:1; 79, comment; 80, comment; 81, for_statement; 81, 82; 81, 85; 81, 89; 82, pattern_list; 82, 83; 82, 84; 83, identifier:i; 84, identifier:entry; 85, call; 85, 86; 85, 87; 86, identifier:enumerate; 87, argument_list; 87, 88; 88, identifier:interstices; 89, block; 89, 90; 90, if_statement; 90, 91; 90, 96; 91, comparison_operator:<; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:interstices; 94, identifier:i; 95, integer:0; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:interstices; 101, identifier:i; 102, integer:0; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 112; 105, subscript; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:measures; 109, binary_operator:+; 109, 110; 109, 111; 110, identifier:prefix; 111, string:'between_collection_interval_duration_mean'; 112, conditional_expression:if; 112, 113; 112, 117; 112, 118; 112, 124; 113, call; 113, 114; 113, 115; 114, identifier:get_mean; 115, argument_list; 115, 116; 116, identifier:interstices; 117, line_continuation:\; 118, comparison_operator:>; 118, 119; 118, 123; 119, call; 119, 120; 119, 121; 120, identifier:len; 121, argument_list; 121, 122; 122, identifier:interstices; 123, integer:0; 124, string:'NA'; 125, if_statement; 125, 126; 125, 130; 126, not_operator; 126, 127; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:quiet; 130, block; 130, 131; 130, 133; 130, 145; 130, 197; 130, 202; 130, 204; 131, expression_statement; 131, 132; 132, identifier:print; 133, print_statement; 133, 134; 134, binary_operator:+; 134, 135; 134, 144; 135, binary_operator:+; 135, 136; 135, 141; 136, binary_operator:+; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:current_similarity_measure; 140, string:" between-"; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:current_collection_type; 144, string:" durations"; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:table; 148, binary_operator:+; 148, 149; 148, 162; 148, 163; 149, list:[(self.current_collection_type + " 1 (start,end)", "Interval",
self.current_collection_type + " 2 (start,end)")]; 149, 150; 150, tuple; 150, 151; 150, 156; 150, 157; 151, binary_operator:+; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:current_collection_type; 155, string:" 1 (start,end)"; 156, string:"Interval"; 157, binary_operator:+; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:current_collection_type; 161, string:" 2 (start,end)"; 162, line_continuation:\; 163, list_comprehension; 163, 164; 163, 177; 164, tuple; 164, 165; 164, 169; 164, 173; 165, call; 165, 166; 165, 167; 166, identifier:str; 167, argument_list; 167, 168; 168, identifier:d1; 169, call; 169, 170; 169, 171; 170, identifier:str; 171, argument_list; 171, 172; 172, identifier:i1; 173, call; 173, 174; 173, 175; 174, identifier:str; 175, argument_list; 175, 176; 176, identifier:d2; 177, for_in_clause; 177, 178; 177, 182; 178, pattern_list; 178, 179; 178, 180; 178, 181; 179, identifier:d1; 180, identifier:i1; 181, identifier:d2; 182, call; 182, 183; 182, 184; 183, identifier:zip; 184, argument_list; 184, 185; 184, 191; 184, 192; 185, subscript; 185, 186; 185, 187; 186, identifier:durations; 187, slice; 187, 188; 187, 189; 188, colon; 189, unary_operator:-; 189, 190; 190, integer:1; 191, identifier:interstices; 192, subscript; 192, 193; 192, 194; 193, identifier:durations; 194, slice; 194, 195; 194, 196; 195, integer:1; 196, colon; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 200; 199, identifier:print_table; 200, argument_list; 200, 201; 201, identifier:table; 202, expression_statement; 202, 203; 203, identifier:print; 204, print_statement; 204, 205; 204, 218; 204, 219; 205, binary_operator:+; 205, 206; 205, 217; 206, binary_operator:+; 206, 207; 206, 214; 207, binary_operator:+; 207, 208; 207, 213; 208, binary_operator:+; 208, 209; 208, 210; 209, string:"Mean "; 210, attribute; 210, 211; 210, 212; 211, identifier:self; 212, identifier:current_similarity_measure; 213, string:" between-"; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:current_collection_type; 217, string:" duration"; 218, line_continuation:\; 219, subscript; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:self; 222, identifier:measures; 223, binary_operator:+; 223, 224; 223, 225; 224, identifier:prefix; 225, string:'between_collection_interval_duration_mean' | def compute_between_collection_interval_duration(self, prefix):
"""Calculates BETWEEN-collection intervals for the current collection and measure type
and takes their mean.
:param str prefix: Prefix for the key entry in self.measures.
Negative intervals (for overlapping clusters) are counted as 0 seconds. Intervals are
calculated as being the difference between the ending time of the last word in a collection
and the start time of the first word in the subsequent collection.
Note that these intervals are not necessarily silences, and may include asides, filled
pauses, words from the examiner, etc.
Adds the following measures to the self.measures dictionary:
- TIMING_(similarity_measure)_(collection_type)_between_collection_interval_duration_mean:
average interval duration separating clusters
"""
durations = [] # duration of each collection
for collection in self.collection_list:
# Entry, with timing, in timed_response for first word in collection
start = collection[0].start_time
# Entry, with timing, in timed_response for last word in collection
end = collection[-1].end_time
durations.append((start, end))
# calculation between-duration intervals
interstices = [durations[i + 1][0] - durations[i][1] for i, d in enumerate(durations[:-1])]
# Replace negative interstices (for overlapping clusters) with
# interstices of duration 0
for i, entry in enumerate(interstices):
if interstices[i] < 0:
interstices[i] = 0
self.measures[prefix + 'between_collection_interval_duration_mean'] = get_mean(interstices) \
if len(interstices) > 0 else 'NA'
if not self.quiet:
print
print self.current_similarity_measure + " between-" + self.current_collection_type + " durations"
table = [(self.current_collection_type + " 1 (start,end)", "Interval",
self.current_collection_type + " 2 (start,end)")] + \
[(str(d1), str(i1), str(d2)) for d1, i1, d2 in zip(durations[:-1], interstices, durations[1:])]
print_table(table)
print
print "Mean " + self.current_similarity_measure + " between-" + self.current_collection_type + " duration", \
self.measures[prefix + 'between_collection_interval_duration_mean'] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:compute_within_collection_vowel_duration; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:prefix; 6, default_parameter; 6, 7; 6, 8; 7, identifier:no_singletons; 8, False; 9, block; 9, 10; 9, 12; 9, 29; 9, 33; 9, 89; 9, 111; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 14; 12, 19; 13, identifier:no_singletons; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:min_size; 18, integer:2; 19, else_clause; 19, 20; 20, block; 20, 21; 20, 25; 21, expression_statement; 21, 22; 22, augmented_assignment:+=; 22, 23; 22, 24; 23, identifier:prefix; 24, string:"no_singletons_"; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:min_size; 28, integer:1; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:durations; 32, list:[]; 33, for_statement; 33, 34; 33, 35; 33, 38; 34, identifier:cluster; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:collection_list; 38, block; 38, 39; 39, if_statement; 39, 40; 39, 46; 40, comparison_operator:>=; 40, 41; 40, 45; 41, call; 41, 42; 41, 43; 42, identifier:len; 43, argument_list; 43, 44; 44, identifier:cluster; 45, identifier:min_size; 46, block; 46, 47; 47, for_statement; 47, 48; 47, 49; 47, 50; 48, identifier:word; 49, identifier:cluster; 50, block; 50, 51; 50, 61; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:word; 54, subscript; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:full_timed_response; 58, attribute; 58, 59; 58, 60; 59, identifier:word; 60, identifier:index_in_timed_response; 61, for_statement; 61, 62; 61, 63; 61, 66; 62, identifier:phone; 63, attribute; 63, 64; 63, 65; 64, identifier:word; 65, identifier:phones; 66, block; 66, 67; 67, if_statement; 67, 68; 67, 75; 68, comparison_operator:in; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:phone; 71, identifier:string; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:vowels; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:durations; 80, identifier:append; 81, argument_list; 81, 82; 82, binary_operator:-; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:phone; 85, identifier:end; 86, attribute; 86, 87; 86, 88; 87, identifier:phone; 88, identifier:start; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 98; 91, subscript; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:measures; 95, binary_operator:+; 95, 96; 95, 97; 96, identifier:prefix; 97, string:'within_collection_vowel_duration_mean'; 98, conditional_expression:if; 98, 99; 98, 103; 98, 104; 98, 110; 99, call; 99, 100; 99, 101; 100, identifier:get_mean; 101, argument_list; 101, 102; 102, identifier:durations; 103, line_continuation:\; 104, comparison_operator:>; 104, 105; 104, 109; 105, call; 105, 106; 105, 107; 106, identifier:len; 107, argument_list; 107, 108; 108, identifier:durations; 109, integer:0; 110, string:'NA'; 111, if_statement; 111, 112; 111, 116; 112, not_operator; 112, 113; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:quiet; 116, block; 116, 117; 117, if_statement; 117, 118; 117, 119; 117, 142; 118, identifier:no_singletons; 119, block; 119, 120; 120, print_statement; 120, 121; 120, 134; 120, 135; 121, binary_operator:+; 121, 122; 121, 133; 122, binary_operator:+; 122, 123; 122, 130; 123, binary_operator:+; 123, 124; 123, 129; 124, binary_operator:+; 124, 125; 124, 126; 125, string:"Mean within-"; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:current_similarity_measure; 129, string:"-"; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:current_collection_type; 133, string:" vowel duration, excluding singletons:"; 134, line_continuation:\; 135, subscript; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:measures; 139, binary_operator:+; 139, 140; 139, 141; 140, identifier:prefix; 141, string:'within_collection_vowel_duration_mean'; 142, else_clause; 142, 143; 143, block; 143, 144; 144, print_statement; 144, 145; 144, 158; 144, 159; 145, binary_operator:+; 145, 146; 145, 157; 146, binary_operator:+; 146, 147; 146, 154; 147, binary_operator:+; 147, 148; 147, 153; 148, binary_operator:+; 148, 149; 148, 150; 149, string:"Mean within-"; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:current_similarity_measure; 153, string:"-"; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:current_collection_type; 157, string:" vowel duration, including singletons:"; 158, line_continuation:\; 159, subscript; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:measures; 163, binary_operator:+; 163, 164; 163, 165; 164, identifier:prefix; 165, string:'within_collection_vowel_duration_mean' | def compute_within_collection_vowel_duration(self, prefix, no_singletons=False):
""" Computes the mean duration of vowels from Units within clusters.
:param str prefix: Prefix for the key entry in self.measures
:param bool no_singletons: If False, excludes collections of length 1 from calculations
and adds "no_singletons" to the prefix
Adds the following measures to the self.measures dictionary:
- TIMING_(similarity_measure)_(collection_type)_within_collection_vowel_duration_mean
"""
if no_singletons:
min_size = 2
else:
prefix += "no_singletons_"
min_size = 1
durations = []
for cluster in self.collection_list:
if len(cluster) >= min_size:
for word in cluster:
word = self.full_timed_response[word.index_in_timed_response]
for phone in word.phones:
if phone.string in self.vowels:
durations.append(phone.end - phone.start)
self.measures[prefix + 'within_collection_vowel_duration_mean'] = get_mean(durations) \
if len(durations) > 0 else 'NA'
if not self.quiet:
if no_singletons:
print "Mean within-" + self.current_similarity_measure + "-" + self.current_collection_type + \
" vowel duration, excluding singletons:", \
self.measures[prefix + 'within_collection_vowel_duration_mean']
else:
print "Mean within-" + self.current_similarity_measure + "-" + self.current_collection_type + \
" vowel duration, including singletons:", \
self.measures[prefix + 'within_collection_vowel_duration_mean'] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:print_output; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 34; 5, 183; 5, 184; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, comparison_operator:==; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:response_format; 13, string:"csv"; 14, block; 14, 15; 15, for_statement; 15, 16; 15, 17; 15, 20; 16, identifier:key; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:measures; 20, block; 20, 21; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:in; 22, 23; 22, 24; 23, string:"TIMING_"; 24, identifier:key; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 33; 28, subscript; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:measures; 32, identifier:key; 33, string:"NA"; 34, if_statement; 34, 35; 34, 39; 35, not_operator; 35, 36; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:quiet; 39, block; 39, 40; 39, 42; 39, 52; 39, 66; 39, 72; 39, 74; 39, 92; 39, 106; 39, 112; 39, 114; 39, 116; 39, 134; 40, expression_statement; 40, 41; 41, identifier:print; 42, print_statement; 42, 43; 43, binary_operator:+; 43, 44; 43, 51; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:type; 49, identifier:upper; 50, argument_list; 51, string:" RESULTS:"; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:keys; 55, list_comprehension; 55, 56; 55, 57; 55, 62; 56, identifier:e; 57, for_in_clause; 57, 58; 57, 59; 58, identifier:e; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:measures; 62, if_clause; 62, 63; 63, comparison_operator:in; 63, 64; 63, 65; 64, string:'COUNT_'; 65, identifier:e; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:keys; 70, identifier:sort; 71, argument_list; 72, print_statement; 72, 73; 73, string:"Counts:"; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:print_table; 77, argument_list; 77, 78; 78, list_comprehension; 78, 79; 78, 89; 79, tuple; 79, 80; 79, 81; 80, identifier:entry; 81, call; 81, 82; 81, 83; 82, identifier:str; 83, argument_list; 83, 84; 84, subscript; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:measures; 88, identifier:entry; 89, for_in_clause; 89, 90; 89, 91; 90, identifier:entry; 91, identifier:keys; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:keys; 95, list_comprehension; 95, 96; 95, 97; 95, 102; 96, identifier:e; 97, for_in_clause; 97, 98; 97, 99; 98, identifier:e; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:measures; 102, if_clause; 102, 103; 103, comparison_operator:in; 103, 104; 103, 105; 104, string:'COLLECTION_'; 105, identifier:e; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:keys; 110, identifier:sort; 111, argument_list; 112, expression_statement; 112, 113; 113, identifier:print; 114, print_statement; 114, 115; 115, string:"Collection measures:"; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:print_table; 119, argument_list; 119, 120; 120, list_comprehension; 120, 121; 120, 131; 121, tuple; 121, 122; 121, 123; 122, identifier:entry; 123, call; 123, 124; 123, 125; 124, identifier:str; 125, argument_list; 125, 126; 126, subscript; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:measures; 130, identifier:entry; 131, for_in_clause; 131, 132; 131, 133; 132, identifier:entry; 133, identifier:keys; 134, if_statement; 134, 135; 134, 140; 135, comparison_operator:==; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:response_format; 139, string:"TextGrid"; 140, block; 140, 141; 140, 155; 140, 161; 140, 163; 140, 165; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:keys; 144, list_comprehension; 144, 145; 144, 146; 144, 151; 145, identifier:e; 146, for_in_clause; 146, 147; 146, 148; 147, identifier:e; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:measures; 151, if_clause; 151, 152; 152, comparison_operator:in; 152, 153; 152, 154; 153, string:'TIMING_'; 154, identifier:e; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:keys; 159, identifier:sort; 160, argument_list; 161, expression_statement; 161, 162; 162, identifier:print; 163, print_statement; 163, 164; 164, string:"Time-based measures:"; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:print_table; 168, argument_list; 168, 169; 169, list_comprehension; 169, 170; 169, 180; 170, tuple; 170, 171; 170, 172; 171, identifier:entry; 172, call; 172, 173; 172, 174; 173, identifier:str; 174, argument_list; 174, 175; 175, subscript; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:measures; 179, identifier:entry; 180, for_in_clause; 180, 181; 180, 182; 181, identifier:entry; 182, identifier:keys; 183, comment; 184, if_statement; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:target_file; 188, block; 188, 189; 189, with_statement; 189, 190; 189, 202; 190, with_clause; 190, 191; 191, with_item; 191, 192; 192, as_pattern; 192, 193; 192, 200; 193, call; 193, 194; 193, 195; 194, identifier:open; 195, argument_list; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:target_file; 199, string:'w'; 200, as_pattern_target; 200, 201; 201, identifier:outfile; 202, block; 202, 203; 202, 265; 202, 279; 202, 286; 202, 287; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:header; 206, binary_operator:+; 206, 207; 206, 247; 206, 248; 207, binary_operator:+; 207, 208; 207, 229; 207, 230; 208, binary_operator:+; 208, 209; 208, 211; 208, 212; 209, list:['file_id']; 209, 210; 210, string:'file_id'; 211, line_continuation:\; 212, list_comprehension; 212, 213; 212, 220; 212, 225; 213, binary_operator:+; 213, 214; 213, 219; 214, binary_operator:+; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:self; 217, identifier:type; 218, string:"_"; 219, identifier:e; 220, for_in_clause; 220, 221; 220, 222; 221, identifier:e; 222, attribute; 222, 223; 222, 224; 223, identifier:self; 224, identifier:measures; 225, if_clause; 225, 226; 226, comparison_operator:in; 226, 227; 226, 228; 227, string:'COUNT_'; 228, identifier:e; 229, line_continuation:\; 230, list_comprehension; 230, 231; 230, 238; 230, 243; 231, binary_operator:+; 231, 232; 231, 237; 232, binary_operator:+; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:type; 236, string:"_"; 237, identifier:e; 238, for_in_clause; 238, 239; 238, 240; 239, identifier:e; 240, attribute; 240, 241; 240, 242; 241, identifier:self; 242, identifier:measures; 243, if_clause; 243, 244; 244, comparison_operator:in; 244, 245; 244, 246; 245, string:'COLLECTION_'; 246, identifier:e; 247, line_continuation:\; 248, list_comprehension; 248, 249; 248, 256; 248, 261; 249, binary_operator:+; 249, 250; 249, 255; 250, binary_operator:+; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:self; 253, identifier:type; 254, string:"_"; 255, identifier:e; 256, for_in_clause; 256, 257; 256, 258; 257, identifier:e; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:measures; 261, if_clause; 261, 262; 262, comparison_operator:in; 262, 263; 262, 264; 263, string:'TIMING_'; 264, identifier:e; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:writer; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:csv; 271, identifier:writer; 272, argument_list; 272, 273; 272, 274; 273, identifier:outfile; 274, keyword_argument; 274, 275; 274, 276; 275, identifier:quoting; 276, attribute; 276, 277; 276, 278; 277, identifier:csv; 278, identifier:QUOTE_MINIMAL; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:writer; 283, identifier:writerow; 284, argument_list; 284, 285; 285, identifier:header; 286, comment; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:writer; 291, identifier:writerow; 292, argument_list; 292, 293; 293, binary_operator:+; 293, 294; 293, 300; 294, list:[self.measures["file_id"]]; 294, 295; 295, subscript; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:measures; 299, string:"file_id"; 300, list_comprehension; 300, 301; 300, 320; 301, subscript; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:self; 304, identifier:measures; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, string:"_"; 308, identifier:join; 309, argument_list; 309, 310; 310, subscript; 310, 311; 310, 317; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:e; 314, identifier:split; 315, argument_list; 315, 316; 316, string:'_'; 317, slice; 317, 318; 317, 319; 318, integer:1; 319, colon; 320, for_in_clause; 320, 321; 320, 322; 321, identifier:e; 322, subscript; 322, 323; 322, 324; 323, identifier:header; 324, slice; 324, 325; 324, 326; 325, integer:1; 326, colon | def print_output(self):
""" Outputs final list of measures to screen a csv file.
The .csv file created has the same name as the input file, with
"vfclust_TYPE_CATEGORY" appended to the filename, where TYPE indicates
the type of task performed done (SEMANTIC or PHONETIC) and CATEGORY
indicates the category requirement of the stimulus (i.e. 'f' or 'animals'
for phonetic and semantic fluency test, respectively.
"""
if self.response_format == "csv":
for key in self.measures:
if "TIMING_" in key:
self.measures[key] = "NA"
if not self.quiet:
print
print self.type.upper() + " RESULTS:"
keys = [e for e in self.measures if 'COUNT_' in e]
keys.sort()
print "Counts:"
print_table([(entry, str(self.measures[entry])) for entry in keys])
keys = [e for e in self.measures if 'COLLECTION_' in e]
keys.sort()
print
print "Collection measures:"
print_table([(entry, str(self.measures[entry])) for entry in keys])
if self.response_format == "TextGrid":
keys = [e for e in self.measures if 'TIMING_' in e]
keys.sort()
print
print "Time-based measures:"
print_table([(entry, str(self.measures[entry])) for entry in keys])
#write to CSV file
if self.target_file:
with open(self.target_file, 'w') as outfile:
header = ['file_id'] + \
[self.type + "_" + e for e in self.measures if 'COUNT_' in e] + \
[self.type + "_" + e for e in self.measures if 'COLLECTION_' in e] + \
[self.type + "_" + e for e in self.measures if 'TIMING_' in e]
writer = csv.writer(outfile, quoting=csv.QUOTE_MINIMAL)
writer.writerow(header)
#the split/join gets rid of the type appended just above
writer.writerow([self.measures["file_id"]] +
[self.measures["_".join(e.split('_')[1:])] for e in header[1:]]) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort_by; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:fieldName; 6, default_parameter; 6, 7; 6, 8; 7, identifier:reverse; 8, False; 9, block; 9, 10; 9, 12; 10, expression_statement; 10, 11; 11, string:'''
sort_by - Return a copy of this collection, sorted by the given fieldName.
The fieldName is accessed the same way as other filtering, so it supports custom properties, etc.
@param fieldName <str> - The name of the field on which to sort by
@param reverse <bool> Default False - If True, list will be in reverse order.
@return <QueryableList> - A QueryableList of the same type with the elements sorted based on arguments.
'''; 12, return_statement; 12, 13; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:__class__; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:sorted; 20, argument_list; 20, 21; 20, 22; 20, 34; 21, identifier:self; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:key; 24, lambda; 24, 25; 24, 27; 25, lambda_parameters; 25, 26; 26, identifier:item; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:_get_item_value; 31, argument_list; 31, 32; 31, 33; 32, identifier:item; 33, identifier:fieldName; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:reverse; 36, identifier:reverse | def sort_by(self, fieldName, reverse=False):
'''
sort_by - Return a copy of this collection, sorted by the given fieldName.
The fieldName is accessed the same way as other filtering, so it supports custom properties, etc.
@param fieldName <str> - The name of the field on which to sort by
@param reverse <bool> Default False - If True, list will be in reverse order.
@return <QueryableList> - A QueryableList of the same type with the elements sorted based on arguments.
'''
return self.__class__(
sorted(self, key = lambda item : self._get_item_value(item, fieldName), reverse=reverse)
) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_qtls_from_rqtl_data; 3, parameters; 3, 4; 3, 5; 4, identifier:matrix; 5, identifier:lod_threshold; 6, block; 6, 7; 6, 9; 6, 20; 6, 30; 6, 31; 6, 32; 6, 33; 6, 199; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:t_matrix; 12, call; 12, 13; 12, 14; 13, identifier:list; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:zip; 17, argument_list; 17, 18; 18, list_splat; 18, 19; 19, identifier:matrix; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:qtls; 23, list:[['Trait', 'Linkage Group', 'Position', 'Exact marker', 'LOD']]; 23, 24; 24, list:['Trait', 'Linkage Group', 'Position', 'Exact marker', 'LOD']; 24, 25; 24, 26; 24, 27; 24, 28; 24, 29; 25, string:'Trait'; 26, string:'Linkage Group'; 27, string:'Position'; 28, string:'Exact marker'; 29, string:'LOD'; 30, comment; 31, comment; 32, comment; 33, for_statement; 33, 34; 33, 35; 33, 40; 34, identifier:row; 35, subscript; 35, 36; 35, 37; 36, identifier:t_matrix; 37, slice; 37, 38; 37, 39; 38, integer:3; 39, colon; 40, block; 40, 41; 40, 45; 40, 49; 40, 53; 40, 57; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:lgroup; 44, None; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:max_lod; 48, None; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:peak; 52, None; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:cnt; 56, integer:1; 57, while_statement; 57, 58; 57, 64; 58, comparison_operator:<; 58, 59; 58, 60; 59, identifier:cnt; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:row; 64, block; 64, 65; 64, 78; 64, 193; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:lgroup; 68, None; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:lgroup; 73, subscript; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:t_matrix; 76, integer:1; 77, identifier:cnt; 78, if_statement; 78, 79; 78, 86; 78, 127; 79, comparison_operator:==; 79, 80; 79, 81; 80, identifier:lgroup; 81, subscript; 81, 82; 81, 85; 82, subscript; 82, 83; 82, 84; 83, identifier:t_matrix; 84, integer:1; 85, identifier:cnt; 86, block; 86, 87; 86, 101; 87, if_statement; 87, 88; 87, 91; 88, comparison_operator:is; 88, 89; 88, 90; 89, identifier:max_lod; 90, None; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:max_lod; 95, call; 95, 96; 95, 97; 96, identifier:float; 97, argument_list; 97, 98; 98, subscript; 98, 99; 98, 100; 99, identifier:row; 100, identifier:cnt; 101, if_statement; 101, 102; 101, 113; 102, comparison_operator:>; 102, 103; 102, 109; 103, call; 103, 104; 103, 105; 104, identifier:float; 105, argument_list; 105, 106; 106, subscript; 106, 107; 106, 108; 107, identifier:row; 108, identifier:cnt; 109, call; 109, 110; 109, 111; 110, identifier:float; 111, argument_list; 111, 112; 112, identifier:max_lod; 113, block; 113, 114; 113, 123; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:max_lod; 117, call; 117, 118; 117, 119; 118, identifier:float; 119, argument_list; 119, 120; 120, subscript; 120, 121; 120, 122; 121, identifier:row; 122, identifier:cnt; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:peak; 126, identifier:cnt; 127, else_clause; 127, 128; 128, block; 128, 129; 128, 181; 128, 185; 128, 189; 129, if_statement; 129, 130; 129, 145; 130, boolean_operator:and; 130, 131; 130, 143; 130, 144; 131, boolean_operator:and; 131, 132; 131, 133; 131, 134; 132, identifier:max_lod; 133, line_continuation:\; 134, comparison_operator:>; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:float; 137, argument_list; 137, 138; 138, identifier:max_lod; 139, call; 139, 140; 139, 141; 140, identifier:float; 141, argument_list; 141, 142; 142, identifier:lod_threshold; 143, line_continuation:\; 144, identifier:peak; 145, block; 145, 146; 145, 174; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:qtl; 149, list:[row[0], # trait
t_matrix[1][peak], # LG
t_matrix[2][peak], # pos
t_matrix[0][peak], # marker
max_lod, # LOD value
]; 149, 150; 149, 153; 149, 154; 149, 159; 149, 160; 149, 165; 149, 166; 149, 171; 149, 172; 149, 173; 150, subscript; 150, 151; 150, 152; 151, identifier:row; 152, integer:0; 153, comment; 154, subscript; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:t_matrix; 157, integer:1; 158, identifier:peak; 159, comment; 160, subscript; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:t_matrix; 163, integer:2; 164, identifier:peak; 165, comment; 166, subscript; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:t_matrix; 169, integer:0; 170, identifier:peak; 171, comment; 172, identifier:max_lod; 173, comment; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:qtls; 178, identifier:append; 179, argument_list; 179, 180; 180, identifier:qtl; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:lgroup; 184, None; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:max_lod; 188, None; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:peak; 192, identifier:cnt; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:cnt; 196, binary_operator:+; 196, 197; 196, 198; 197, identifier:cnt; 198, integer:1; 199, return_statement; 199, 200; 200, identifier:qtls | def get_qtls_from_rqtl_data(matrix, lod_threshold):
""" Retrieve the list of significants QTLs for the given input
matrix and using the specified LOD threshold.
This assumes one QTL per linkage group.
:arg matrix, the MapQTL file read in memory
:arg threshold, threshold used to determine if a given LOD value is
reflective the presence of a QTL.
"""
t_matrix = list(zip(*matrix))
qtls = [['Trait', 'Linkage Group', 'Position', 'Exact marker', 'LOD']]
# row 0: markers
# row 1: chr
# row 2: pos
for row in t_matrix[3:]:
lgroup = None
max_lod = None
peak = None
cnt = 1
while cnt < len(row):
if lgroup is None:
lgroup = t_matrix[1][cnt]
if lgroup == t_matrix[1][cnt]:
if max_lod is None:
max_lod = float(row[cnt])
if float(row[cnt]) > float(max_lod):
max_lod = float(row[cnt])
peak = cnt
else:
if max_lod \
and float(max_lod) > float(lod_threshold) \
and peak:
qtl = [row[0], # trait
t_matrix[1][peak], # LG
t_matrix[2][peak], # pos
t_matrix[0][peak], # marker
max_lod, # LOD value
]
qtls.append(qtl)
lgroup = None
max_lod = None
peak = cnt
cnt = cnt + 1
return qtls |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:response; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:code; 5, default_parameter; 5, 6; 5, 7; 6, identifier:body; 7, string:''; 8, default_parameter; 8, 9; 8, 10; 9, identifier:etag; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:last_modified; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:expires; 16, None; 17, dictionary_splat_pattern; 17, 18; 18, identifier:kw; 19, block; 19, 20; 19, 22; 19, 55; 19, 69; 19, 101; 19, 130; 20, expression_statement; 20, 21; 21, comment; 22, if_statement; 22, 23; 22, 26; 23, comparison_operator:is; 23, 24; 23, 25; 24, identifier:etag; 25, None; 26, block; 26, 27; 26, 49; 27, if_statement; 27, 28; 27, 42; 28, not_operator; 28, 29; 29, parenthesized_expression; 29, 30; 30, boolean_operator:and; 30, 31; 30, 36; 31, comparison_operator:==; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:etag; 34, integer:0; 35, string:'"'; 36, comparison_operator:==; 36, 37; 36, 41; 37, subscript; 37, 38; 37, 39; 38, identifier:etag; 39, unary_operator:-; 39, 40; 40, integer:1; 41, string:'"'; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:etag; 46, binary_operator:%; 46, 47; 46, 48; 47, string:'"%s"'; 48, identifier:etag; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 54; 51, subscript; 51, 52; 51, 53; 52, identifier:kw; 53, string:'etag'; 54, identifier:etag; 55, if_statement; 55, 56; 55, 59; 56, comparison_operator:is; 56, 57; 56, 58; 57, identifier:last_modified; 58, None; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:kw; 64, string:'last_modified'; 65, call; 65, 66; 65, 67; 66, identifier:datetime_to_httpdate; 67, argument_list; 67, 68; 68, identifier:last_modified; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:is; 70, 71; 70, 72; 71, identifier:expires; 72, None; 73, block; 73, 74; 74, if_statement; 74, 75; 74, 80; 74, 90; 75, call; 75, 76; 75, 77; 76, identifier:isinstance; 77, argument_list; 77, 78; 77, 79; 78, identifier:expires; 79, identifier:datetime; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:kw; 85, string:'expires'; 86, call; 86, 87; 86, 88; 87, identifier:datetime_to_httpdate; 88, argument_list; 88, 89; 89, identifier:expires; 90, else_clause; 90, 91; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:kw; 96, string:'expires'; 97, call; 97, 98; 97, 99; 98, identifier:timedelta_to_httpdate; 99, argument_list; 99, 100; 100, identifier:expires; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:headers; 104, list_comprehension; 104, 105; 104, 118; 105, tuple; 105, 106; 105, 117; 106, call; 106, 107; 106, 116; 107, attribute; 107, 108; 107, 115; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:k; 111, identifier:replace; 112, argument_list; 112, 113; 112, 114; 113, string:'_'; 114, string:'-'; 115, identifier:title; 116, argument_list; 117, identifier:v; 118, for_in_clause; 118, 119; 118, 122; 119, pattern_list; 119, 120; 119, 121; 120, identifier:k; 121, identifier:v; 122, call; 122, 123; 122, 124; 123, identifier:sorted; 124, argument_list; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:kw; 128, identifier:items; 129, argument_list; 130, return_statement; 130, 131; 131, call; 131, 132; 131, 133; 132, identifier:Response; 133, argument_list; 133, 134; 133, 135; 133, 136; 134, identifier:code; 135, identifier:headers; 136, identifier:body | def response(code, body='', etag=None, last_modified=None, expires=None, **kw):
"""Helper to build an HTTP response.
Parameters:
code
: An integer status code.
body
: The response body. See `Response.__init__` for details.
etag
: A value for the ETag header. Double quotes will be added unless the
string starts and ends with a double quote.
last_modified
: A value for the Last-Modified header as a datetime.datetime object
or Unix timestamp.
expires
: A value for the Expires header as number of seconds, datetime.timedelta
or datetime.datetime object.
Note: a value of type int or float is interpreted as a number of
seconds in the future, *not* as Unix timestamp.
**kw
: All other keyword arguments are interpreted as response headers.
The names will be converted to header names by replacing
underscores with hyphens and converting to title case
(e.g. `x_powered_by` => `X-Powered-By`).
"""
if etag is not None:
if not (etag[0] == '"' and etag[-1] == '"'):
etag = '"%s"' % etag
kw['etag'] = etag
if last_modified is not None:
kw['last_modified'] = datetime_to_httpdate(last_modified)
if expires is not None:
if isinstance(expires, datetime):
kw['expires'] = datetime_to_httpdate(expires)
else:
kw['expires'] = timedelta_to_httpdate(expires)
headers = [(k.replace('_', '-').title(), v) for k, v in sorted(kw.items())]
return Response(code, headers, body) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:plot_optimum_prediction_fraction_correct_cutoffs_over_range; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, identifier:analysis_set; 6, identifier:min_stability_classication_x_cutoff; 7, identifier:max_stability_classication_x_cutoff; 8, default_parameter; 8, 9; 8, 10; 9, identifier:suppress_plot; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:analysis_file_prefix; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:verbose; 16, True; 17, block; 17, 18; 17, 20; 17, 21; 17, 25; 17, 26; 17, 27; 17, 31; 17, 57; 17, 58; 17, 63; 17, 67; 17, 71; 17, 75; 17, 79; 17, 89; 17, 157; 17, 171; 17, 172; 17, 181; 17, 190; 17, 199; 17, 205; 17, 214; 17, 220; 17, 231; 17, 232; 17, 305; 18, expression_statement; 18, 19; 19, string:'''Plots the optimum cutoff for the predictions to maximize the fraction correct metric over a range of experimental cutoffs.
Returns the average scalar corresponding to the best value of fraction correct over a range of cutoff values for the experimental cutoffs.'''; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:analysis_set_prefix; 24, string:''; 25, comment; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:plot_filename; 30, None; 31, if_statement; 31, 32; 31, 34; 32, not_operator; 32, 33; 33, identifier:suppress_plot; 34, block; 34, 35; 34, 45; 34, 51; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:output_filename_prefix; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, string:'{0}{1}optimum_fraction_correct_at_varying_kcal_mol'; 41, identifier:format; 42, argument_list; 42, 43; 42, 44; 43, identifier:analysis_file_prefix; 44, identifier:analysis_set_prefix; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:plot_filename; 48, binary_operator:+; 48, 49; 48, 50; 49, identifier:output_filename_prefix; 50, string:'.png'; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:csv_filename; 54, binary_operator:+; 54, 55; 54, 56; 55, identifier:output_filename_prefix; 56, string:'.txt'; 57, comment; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:lines; 61, list:['ExperimentalCutoff,BestPredictionCutoff']; 61, 62; 62, string:'ExperimentalCutoff,BestPredictionCutoff'; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:x_cutoff; 66, identifier:min_stability_classication_x_cutoff; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:x_values; 70, list:[]; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:y_values; 74, list:[]; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:avg_scale; 78, integer:0; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:plot_graph; 82, boolean_operator:and; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:generate_plots; 86, not_operator; 86, 87; 87, parenthesized_expression; 87, 88; 88, identifier:suppress_plot; 89, while_statement; 89, 90; 89, 95; 90, comparison_operator:<; 90, 91; 90, 92; 91, identifier:x_cutoff; 92, binary_operator:+; 92, 93; 92, 94; 93, identifier:max_stability_classication_x_cutoff; 94, float:0.1; 95, block; 95, 96; 95, 112; 95, 133; 95, 140; 95, 147; 95, 153; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 102; 98, pattern_list; 98, 99; 98, 100; 98, 101; 99, identifier:max_value_cutoff; 100, identifier:max_value; 101, identifier:fraction_correct_range; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:determine_optimum_fraction_correct_cutoffs; 106, argument_list; 106, 107; 106, 108; 106, 111; 107, identifier:analysis_set; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:dataframe; 111, identifier:x_cutoff; 112, if_statement; 112, 113; 112, 114; 113, identifier:plot_graph; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:lines; 119, identifier:append; 120, argument_list; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, string:','; 124, identifier:join; 125, argument_list; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:map; 128, argument_list; 128, 129; 128, 130; 129, identifier:str; 130, tuple; 130, 131; 130, 132; 131, identifier:x_cutoff; 132, identifier:max_value_cutoff; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:x_values; 137, identifier:append; 138, argument_list; 138, 139; 139, identifier:x_cutoff; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:y_values; 144, identifier:append; 145, argument_list; 145, 146; 146, identifier:max_value_cutoff; 147, expression_statement; 147, 148; 148, augmented_assignment:+=; 148, 149; 148, 150; 149, identifier:avg_scale; 150, binary_operator:/; 150, 151; 150, 152; 151, identifier:max_value_cutoff; 152, identifier:x_cutoff; 153, expression_statement; 153, 154; 154, augmented_assignment:+=; 154, 155; 154, 156; 155, identifier:x_cutoff; 156, float:0.1; 157, if_statement; 157, 158; 157, 159; 158, identifier:plot_graph; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:write_file; 163, argument_list; 163, 164; 163, 165; 164, identifier:csv_filename; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, string:'\n'; 168, identifier:join; 169, argument_list; 169, 170; 170, identifier:lines; 171, comment; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:avg_scale; 175, binary_operator:/; 175, 176; 175, 177; 176, identifier:avg_scale; 177, call; 177, 178; 177, 179; 178, identifier:len; 179, argument_list; 179, 180; 180, identifier:x_values; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:x_values; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:numpy; 187, identifier:array; 188, argument_list; 188, 189; 189, identifier:x_values; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:y_values; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:numpy; 196, identifier:array; 197, argument_list; 197, 198; 198, identifier:y_values; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:scalars; 202, binary_operator:/; 202, 203; 202, 204; 203, identifier:y_values; 204, identifier:x_values; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:average_scalar; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:numpy; 211, identifier:mean; 212, argument_list; 212, 213; 213, identifier:scalars; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:plot_label_1; 217, binary_operator:%; 217, 218; 217, 219; 218, string:'Scalar == %0.2f'; 219, identifier:average_scalar; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:plot_label_2; 223, binary_operator:%; 223, 224; 223, 225; 224, string:'sigma == %0.2f'; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:numpy; 228, identifier:std; 229, argument_list; 229, 230; 230, identifier:scalars; 231, comment; 232, if_statement; 232, 233; 232, 234; 233, identifier:plot_graph; 234, block; 234, 235; 235, if_statement; 235, 236; 235, 252; 236, not_operator; 236, 237; 237, parenthesized_expression; 237, 238; 238, boolean_operator:and; 238, 239; 238, 247; 239, call; 239, 240; 239, 245; 240, attribute; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:os; 243, identifier:path; 244, identifier:exists; 245, argument_list; 245, 246; 246, identifier:plot_filename; 247, not_operator; 247, 248; 248, parenthesized_expression; 248, 249; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:recreate_graphs; 252, block; 252, 253; 252, 274; 252, 278; 252, 290; 252, 294; 253, if_statement; 253, 254; 253, 255; 254, identifier:verbose; 255, block; 255, 256; 255, 265; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:log; 261, argument_list; 261, 262; 262, binary_operator:%; 262, 263; 262, 264; 263, string:'Saving scatterplot to %s.'; 264, identifier:plot_filename; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:log; 270, argument_list; 270, 271; 271, binary_operator:%; 271, 272; 271, 273; 272, string:'Saving plot of approximate optimal fraction correct cutoffs over varying experimental cutoffs to %s.'; 273, identifier:plot_filename; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:title; 277, string:'Optimum cutoff for fraction correct metric at varying experimental cutoffs'; 278, if_statement; 278, 279; 278, 280; 279, identifier:analysis_set; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, augmented_assignment:+=; 282, 283; 282, 284; 283, identifier:title; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, string:' for {0}'; 287, identifier:format; 288, argument_list; 288, 289; 289, identifier:analysis_set; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:r_script; 293, string:'''library(ggplot2)
library(gridExtra)
library(scales)
library(qualV)
png('%(plot_filename)s', height=4096, width=4096, bg="white", res=600)
plot_data <- read.csv('%(csv_filename)s', header=T)
max_y = max(plot_data$BestPredictionCutoff)
p <- ggplot(data = plot_data, aes(x = ExperimentalCutoff, y = BestPredictionCutoff)) +
xlab("Experimental cutoff (kcal/mol)") +
ylab("Optimal prediction cutoff (energy units)") +
ggtitle("%(title)s") +
geom_point() +
geom_line() +
geom_smooth() +
geom_text(hjust=0, size=4, color="black", aes(0.5, max_y, fontface="plain", family = "sans", label="%(plot_label_1)s"), parse = T) +
geom_text(hjust=0, size=4, color="black", aes(0.5, max_y - 0.5, fontface="plain", family = "sans", label="%(plot_label_2)s"), parse = T)
p
dev.off()'''; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:RInterface; 298, identifier:_runRScript; 299, argument_list; 299, 300; 300, binary_operator:%; 300, 301; 300, 302; 301, identifier:r_script; 302, call; 302, 303; 302, 304; 303, identifier:locals; 304, argument_list; 305, return_statement; 305, 306; 306, expression_list; 306, 307; 306, 308; 307, identifier:average_scalar; 308, identifier:plot_filename | def plot_optimum_prediction_fraction_correct_cutoffs_over_range(self, analysis_set, min_stability_classication_x_cutoff, max_stability_classication_x_cutoff, suppress_plot = False, analysis_file_prefix = None, verbose = True):
'''Plots the optimum cutoff for the predictions to maximize the fraction correct metric over a range of experimental cutoffs.
Returns the average scalar corresponding to the best value of fraction correct over a range of cutoff values for the experimental cutoffs.'''
# Filenames
analysis_set_prefix = ''
#if analysis_set:
# analysis_set_prefix = '_{0}'.format(analysis_set)
plot_filename = None
if not suppress_plot:
output_filename_prefix = '{0}{1}optimum_fraction_correct_at_varying_kcal_mol'.format(analysis_file_prefix, analysis_set_prefix)
plot_filename = output_filename_prefix + '.png'
csv_filename = output_filename_prefix + '.txt'
# Create CSV input
lines = ['ExperimentalCutoff,BestPredictionCutoff']
x_cutoff = min_stability_classication_x_cutoff
x_values = []
y_values = []
avg_scale = 0
plot_graph = self.generate_plots and not(suppress_plot)
while x_cutoff < max_stability_classication_x_cutoff + 0.1:
max_value_cutoff, max_value, fraction_correct_range = self.determine_optimum_fraction_correct_cutoffs(analysis_set, self.dataframe, x_cutoff)
if plot_graph:
lines.append(','.join(map(str, (x_cutoff, max_value_cutoff))))
x_values.append(x_cutoff)
y_values.append(max_value_cutoff)
avg_scale += max_value_cutoff / x_cutoff
x_cutoff += 0.1
if plot_graph:
write_file(csv_filename, '\n'.join(lines))
# Determine the average scalar needed to fit the plot
avg_scale = avg_scale / len(x_values)
x_values = numpy.array(x_values)
y_values = numpy.array(y_values)
scalars = y_values / x_values
average_scalar = numpy.mean(scalars)
plot_label_1 = 'Scalar == %0.2f' % average_scalar
plot_label_2 = 'sigma == %0.2f' % numpy.std(scalars)
# Create plot
if plot_graph:
if not(os.path.exists(plot_filename) and not(self.recreate_graphs)):
if verbose:
self.log('Saving scatterplot to %s.' % plot_filename)
self.log('Saving plot of approximate optimal fraction correct cutoffs over varying experimental cutoffs to %s.' % plot_filename)
title = 'Optimum cutoff for fraction correct metric at varying experimental cutoffs'
if analysis_set:
title += ' for {0}'.format(analysis_set)
r_script = '''library(ggplot2)
library(gridExtra)
library(scales)
library(qualV)
png('%(plot_filename)s', height=4096, width=4096, bg="white", res=600)
plot_data <- read.csv('%(csv_filename)s', header=T)
max_y = max(plot_data$BestPredictionCutoff)
p <- ggplot(data = plot_data, aes(x = ExperimentalCutoff, y = BestPredictionCutoff)) +
xlab("Experimental cutoff (kcal/mol)") +
ylab("Optimal prediction cutoff (energy units)") +
ggtitle("%(title)s") +
geom_point() +
geom_line() +
geom_smooth() +
geom_text(hjust=0, size=4, color="black", aes(0.5, max_y, fontface="plain", family = "sans", label="%(plot_label_1)s"), parse = T) +
geom_text(hjust=0, size=4, color="black", aes(0.5, max_y - 0.5, fontface="plain", family = "sans", label="%(plot_label_2)s"), parse = T)
p
dev.off()'''
RInterface._runRScript(r_script % locals())
return average_scalar, plot_filename |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_chain_details_by_related_pdb_chains; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:pdb_id; 6, identifier:chain_id; 7, identifier:pfam_accs; 8, block; 8, 9; 8, 11; 8, 17; 8, 23; 8, 31; 8, 49; 8, 53; 8, 54; 8, 58; 8, 127; 8, 128; 8, 148; 8, 171; 8, 175; 8, 180; 8, 185; 8, 195; 8, 229; 8, 239; 8, 245; 8, 255; 8, 267; 8, 285; 8, 286; 8, 307; 8, 322; 9, expression_statement; 9, 10; 10, string:''' Returns a dict of SCOPe details using info
This returns Pfam-level information for a PDB chain i.e. no details on the protein, species, or domain will be returned.
If there are SCOPe entries for the associated Pfam accession numbers which agree then this function returns
pretty complete information.
'''; 11, if_statement; 11, 12; 11, 14; 12, not_operator; 12, 13; 13, identifier:pfam_accs; 14, block; 14, 15; 15, return_statement; 15, 16; 16, None; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:associated_pdb_chains; 20, call; 20, 21; 20, 22; 21, identifier:set; 22, argument_list; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:pfam_api; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:get_pfam_api; 30, argument_list; 31, for_statement; 31, 32; 31, 33; 31, 34; 32, identifier:pfam_acc; 33, identifier:pfam_accs; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:associated_pdb_chains; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:associated_pdb_chains; 41, identifier:union; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:pfam_api; 46, identifier:get_pdb_chains_from_pfam_accession_number; 47, argument_list; 47, 48; 48, identifier:pfam_acc; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:hits; 52, list:[]; 53, comment; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:pfam_scop_mapping; 57, dictionary; 58, for_statement; 58, 59; 58, 60; 58, 61; 59, identifier:pdb_chain_pair; 60, identifier:associated_pdb_chains; 61, block; 61, 62; 61, 74; 61, 92; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 67; 64, pattern_list; 64, 65; 64, 66; 65, identifier:ass_pdb_id; 66, identifier:ass_chain_id; 67, expression_list; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:pdb_chain_pair; 70, integer:0; 71, subscript; 71, 72; 71, 73; 72, identifier:pdb_chain_pair; 73, integer:1; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:hit; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:get_chain_details; 81, argument_list; 81, 82; 81, 83; 81, 86; 81, 89; 82, identifier:ass_pdb_id; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:chain; 85, identifier:ass_chain_id; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:internal_function_call; 88, True; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:pfam_scop_mapping; 91, identifier:pfam_scop_mapping; 92, if_statement; 92, 93; 92, 101; 93, boolean_operator:and; 93, 94; 93, 95; 94, identifier:hit; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:hit; 98, identifier:get; 99, argument_list; 99, 100; 100, string:'chains'; 101, block; 101, 102; 101, 112; 101, 123; 101, 124; 101, 125; 101, 126; 102, assert_statement; 102, 103; 103, parenthesized_expression; 103, 104; 104, comparison_operator:==; 104, 105; 104, 111; 105, call; 105, 106; 105, 107; 106, identifier:len; 107, argument_list; 107, 108; 108, subscript; 108, 109; 108, 110; 109, identifier:hit; 110, string:'chains'; 111, integer:1; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:hits; 116, identifier:append; 117, argument_list; 117, 118; 118, subscript; 118, 119; 118, 122; 119, subscript; 119, 120; 119, 121; 120, identifier:hit; 121, string:'chains'; 122, identifier:ass_chain_id; 123, comment; 124, comment; 125, comment; 126, comment; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:allowed_scop_domains; 131, call; 131, 132; 131, 133; 132, identifier:map; 133, argument_list; 133, 134; 133, 135; 134, identifier:int; 135, subscript; 135, 136; 135, 147; 136, call; 136, 137; 136, 138; 137, identifier:map; 138, argument_list; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:set; 141, identifier:intersection; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:pfam_scop_mapping; 145, identifier:values; 146, argument_list; 147, integer:0; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:allowed_scop_domains; 151, call; 151, 152; 151, 153; 152, identifier:list; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:set; 156, argument_list; 156, 157; 157, binary_operator:+; 157, 158; 157, 162; 158, parenthesized_expression; 158, 159; 159, boolean_operator:or; 159, 160; 159, 161; 160, identifier:allowed_scop_domains; 161, list:[]; 162, parenthesized_expression; 162, 163; 163, boolean_operator:or; 163, 164; 163, 170; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:get_sunid_for_pfam_accs; 168, argument_list; 168, 169; 169, identifier:pfam_accs; 170, list:[]; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:filtered_hits; 174, list:[]; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:print; 178, argument_list; 178, 179; 179, identifier:pfam_accs; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:print; 183, argument_list; 183, 184; 184, identifier:allowed_scop_domains; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:print; 188, argument_list; 188, 189; 189, binary_operator:%; 189, 190; 189, 191; 190, string:'%d hits'; 191, call; 191, 192; 191, 193; 192, identifier:len; 193, argument_list; 193, 194; 194, identifier:hits; 195, for_statement; 195, 196; 195, 197; 195, 198; 196, identifier:hit; 197, identifier:hits; 198, block; 198, 199; 198, 203; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:domains_to_ignore; 202, list:[]; 203, for_statement; 203, 204; 203, 207; 203, 214; 204, pattern_list; 204, 205; 204, 206; 205, identifier:k; 206, identifier:v; 207, call; 207, 208; 207, 213; 208, attribute; 208, 209; 208, 212; 209, subscript; 209, 210; 209, 211; 210, identifier:hit; 211, string:'domains'; 212, identifier:iteritems; 213, argument_list; 214, block; 214, 215; 215, if_statement; 215, 216; 215, 221; 216, comparison_operator:in; 216, 217; 216, 220; 217, subscript; 217, 218; 217, 219; 218, identifier:v; 219, string:'sunid'; 220, identifier:allowed_scop_domains; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:filtered_hits; 226, identifier:append; 227, argument_list; 227, 228; 228, identifier:v; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 232; 231, identifier:print; 232, argument_list; 232, 233; 233, binary_operator:%; 233, 234; 233, 235; 234, string:'%d filtered_hits'; 235, call; 235, 236; 235, 237; 236, identifier:len; 237, argument_list; 237, 238; 238, identifier:filtered_hits; 239, if_statement; 239, 240; 239, 242; 240, not_operator; 240, 241; 241, identifier:filtered_hits; 242, block; 242, 243; 243, return_statement; 243, 244; 244, None; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:d; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:get_basic_pdb_chain_information; 252, argument_list; 252, 253; 252, 254; 253, identifier:pdb_id; 254, identifier:chain_id; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:d; 259, identifier:update; 260, argument_list; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:self; 264, identifier:get_common_fields; 265, argument_list; 265, 266; 266, identifier:filtered_hits; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:d; 271, identifier:update; 272, argument_list; 272, 273; 273, call; 273, 274; 273, 275; 274, identifier:dict; 275, argument_list; 275, 276; 275, 279; 275, 282; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:SCOPe_sources; 278, string:'Pfam + SCOPe'; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:SCOPe_search_fields; 281, string:'Pfam + link_pdb.pdb_chain_id'; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:SCOPe_trust_level; 284, integer:3; 285, comment; 286, for_statement; 286, 287; 286, 290; 286, 300; 287, pattern_list; 287, 288; 287, 289; 288, identifier:k; 289, identifier:v; 290, call; 290, 291; 290, 292; 291, identifier:sorted; 292, argument_list; 292, 293; 293, call; 293, 294; 293, 299; 294, attribute; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:self; 297, identifier:levels; 298, identifier:iteritems; 299, argument_list; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 306; 303, subscript; 303, 304; 303, 305; 304, identifier:d; 305, identifier:v; 306, None; 307, expression_statement; 307, 308; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:d; 311, identifier:update; 312, argument_list; 312, 313; 313, call; 313, 314; 313, 315; 314, identifier:dict; 315, argument_list; 315, 316; 316, call; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:self; 319, identifier:get_common_hierarchy; 320, argument_list; 320, 321; 321, identifier:filtered_hits; 322, return_statement; 322, 323; 323, identifier:d | def get_chain_details_by_related_pdb_chains(self, pdb_id, chain_id, pfam_accs):
''' Returns a dict of SCOPe details using info
This returns Pfam-level information for a PDB chain i.e. no details on the protein, species, or domain will be returned.
If there are SCOPe entries for the associated Pfam accession numbers which agree then this function returns
pretty complete information.
'''
if not pfam_accs:
return None
associated_pdb_chains = set()
pfam_api = self.get_pfam_api()
for pfam_acc in pfam_accs:
associated_pdb_chains = associated_pdb_chains.union(pfam_api.get_pdb_chains_from_pfam_accession_number(pfam_acc))
hits = []
#class_count = {}
pfam_scop_mapping = {}
for pdb_chain_pair in associated_pdb_chains:
ass_pdb_id, ass_chain_id = pdb_chain_pair[0], pdb_chain_pair[1]
hit = self.get_chain_details(ass_pdb_id, chain = ass_chain_id, internal_function_call = True, pfam_scop_mapping = pfam_scop_mapping)
if hit and hit.get('chains'):
assert(len(hit['chains']) == 1)
hits.append(hit['chains'][ass_chain_id])
#for k, v in hit.iteritems():
#class_count[v['sccs']] = class_count.get(v['sccs'], 0)
#class_count[v['sccs']] += 1
#print(' %s, %s: %s' % (v['pdb_id'], k, v['sccs']))
#pprint.pprint(class_count)
allowed_scop_domains = map(int, map(set.intersection, pfam_scop_mapping.values())[0])
allowed_scop_domains = list(set((allowed_scop_domains or []) + (self.get_sunid_for_pfam_accs(pfam_accs) or [])))
filtered_hits = []
print(pfam_accs)
print(allowed_scop_domains)
print('%d hits' % len(hits))
for hit in hits:
domains_to_ignore = []
for k, v in hit['domains'].iteritems():
if v['sunid'] in allowed_scop_domains:
filtered_hits.append(v)
print('%d filtered_hits' % len(filtered_hits))
if not filtered_hits:
return None
d = self.get_basic_pdb_chain_information(pdb_id, chain_id)
d.update(self.get_common_fields(filtered_hits))
d.update(dict(
SCOPe_sources = 'Pfam + SCOPe',
SCOPe_search_fields = 'Pfam + link_pdb.pdb_chain_id',
SCOPe_trust_level = 3
))
# Add the lowest common classification over all related Pfam families
for k, v in sorted(self.levels.iteritems()):
d[v] = None
d.update(dict(self.get_common_hierarchy(filtered_hits)))
return d |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:recall_service; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:service; 6, block; 6, 7; 6, 9; 6, 22; 6, 43; 6, 214; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 16; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 15; 14, identifier:service; 15, identifier:Service; 16, block; 16, 17; 17, raise_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:TypeError; 20, argument_list; 20, 21; 21, string:"service must be of type Service."; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:logger; 26, identifier:warning; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, string:"The deployment for {0} on {1} failed starting the rollback."; 31, identifier:format; 32, argument_list; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:service; 35, identifier:alias; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:url; 41, identifier:geturl; 42, argument_list; 43, function_definition; 43, 44; 43, 45; 43, 47; 44, function_name:anonymous; 45, parameters; 45, 46; 46, identifier:anonymous_service; 47, block; 47, 48; 47, 61; 47, 70; 48, if_statement; 48, 49; 48, 55; 49, not_operator; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:anonymous_service; 54, identifier:Service; 55, block; 55, 56; 56, raise_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:TypeError; 59, argument_list; 59, 60; 60, string:"service must be an instance of Service."; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:containers; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:find_previous_service_containers; 68, argument_list; 68, 69; 69, identifier:anonymous_service; 70, if_statement; 70, 71; 70, 72; 71, identifier:containers; 72, block; 72, 73; 72, 92; 72, 100; 73, for_statement; 73, 74; 73, 75; 73, 85; 74, identifier:name; 75, call; 75, 76; 75, 77; 76, identifier:list; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:anonymous_service; 82, identifier:containers; 83, identifier:keys; 84, argument_list; 85, block; 85, 86; 86, delete_statement; 86, 87; 87, subscript; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:anonymous_service; 90, identifier:containers; 91, identifier:name; 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:anonymous_service; 97, identifier:cargo; 98, identifier:delete; 99, argument_list; 100, for_statement; 100, 101; 100, 104; 100, 110; 100, 111; 101, pattern_list; 101, 102; 101, 103; 102, identifier:name; 103, identifier:container; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:six; 107, identifier:iteritems; 108, argument_list; 108, 109; 109, identifier:containers; 110, comment; 111, block; 111, 112; 112, if_statement; 112, 113; 112, 123; 112, 142; 113, call; 113, 114; 113, 121; 114, attribute; 114, 115; 114, 120; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:container; 118, identifier:state; 119, argument_list; 120, identifier:get; 121, argument_list; 121, 122; 122, string:'running'; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:logger; 128, identifier:info; 129, argument_list; 129, 130; 129, 131; 130, string:"is already running... Might want to investigate."; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:extra; 133, dictionary; 133, 134; 133, 137; 134, pair; 134, 135; 134, 136; 135, string:'formatter'; 136, string:'container'; 137, pair; 137, 138; 137, 139; 138, string:'container'; 139, attribute; 139, 140; 139, 141; 140, identifier:container; 141, identifier:name; 142, else_clause; 142, 143; 143, block; 143, 144; 144, if_statement; 144, 145; 144, 150; 144, 169; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:container; 148, identifier:start; 149, argument_list; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:logger; 155, identifier:info; 156, argument_list; 156, 157; 156, 158; 157, string:"is restarted and healthy."; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:extra; 160, dictionary; 160, 161; 160, 164; 161, pair; 161, 162; 161, 163; 162, string:'formatter'; 163, string:'container'; 164, pair; 164, 165; 164, 166; 165, string:'container'; 166, attribute; 166, 167; 166, 168; 167, identifier:container; 168, identifier:name; 169, else_clause; 169, 170; 170, block; 170, 171; 170, 189; 170, 195; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:logger; 175, identifier:error; 176, argument_list; 176, 177; 176, 178; 177, string:"failed to start."; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:extra; 180, dictionary; 180, 181; 180, 184; 181, pair; 181, 182; 181, 183; 182, string:'formatter'; 183, string:'container'; 184, pair; 184, 185; 184, 186; 185, string:'container'; 186, attribute; 186, 187; 186, 188; 187, identifier:container; 188, identifier:name; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:container; 193, identifier:dump_logs; 194, argument_list; 195, raise_statement; 195, 196; 196, call; 196, 197; 196, 198; 197, identifier:Exception; 198, argument_list; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, string:"The deployment for {0} on {1} went horribly wrong"; 202, identifier:format; 203, argument_list; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:container; 206, identifier:name; 207, call; 207, 208; 207, 213; 208, attribute; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:url; 212, identifier:geturl; 213, argument_list; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:_service_map; 219, argument_list; 219, 220; 219, 221; 219, 222; 220, identifier:service; 221, identifier:anonymous; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:descending; 224, False | def recall_service(self, service):
"""
This method assumes that its a roll back during a deployment. If not used during a deployment session
This method should be extended later to be more useful.
"""
if not isinstance(service, Service):
raise TypeError("service must be of type Service.")
logger.warning("The deployment for {0} on {1} failed starting the rollback.".format(service.alias, self.url.geturl()))
def anonymous(anonymous_service):
if not isinstance(anonymous_service, Service):
raise TypeError("service must be an instance of Service.")
containers = self.find_previous_service_containers(anonymous_service)
if containers:
for name in list(anonymous_service.containers.keys()):
del anonymous_service.containers[name]
anonymous_service.cargo.delete()
for name, container in six.iteritems(containers):
# TODO: add function to container obj to see if its running.
if container.state().get('running'):
logger.info(
"is already running... Might want to investigate.",
extra={'formatter': 'container', 'container': container.name}
)
else:
if container.start():
logger.info(
"is restarted and healthy.",
extra={'formatter': 'container', 'container': container.name}
)
else:
logger.error(
"failed to start.",
extra={'formatter': 'container', 'container': container.name}
)
container.dump_logs()
raise Exception(
"The deployment for {0} on {1} went horribly wrong".format(container.name, self.url.geturl())
)
self._service_map(service, anonymous, descending=False) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:sorted_items; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:d; 5, default_parameter; 5, 6; 5, 7; 6, identifier:key; 7, identifier:__identity; 8, default_parameter; 8, 9; 8, 10; 9, identifier:reverse; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 27; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, function_definition; 15, 16; 15, 17; 15, 19; 16, function_name:pairkey_key; 17, parameters; 17, 18; 18, identifier:item; 19, block; 19, 20; 20, return_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:key; 23, argument_list; 23, 24; 24, subscript; 24, 25; 24, 26; 25, identifier:item; 26, integer:0; 27, return_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:sorted; 30, argument_list; 30, 31; 30, 36; 30, 39; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:d; 34, identifier:items; 35, argument_list; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:key; 38, identifier:pairkey_key; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:reverse; 41, identifier:reverse | def sorted_items(d, key=__identity, reverse=False):
"""
Return the items of the dictionary sorted by the keys
>>> sample = dict(foo=20, bar=42, baz=10)
>>> tuple(sorted_items(sample))
(('bar', 42), ('baz', 10), ('foo', 20))
>>> reverse_string = lambda s: ''.join(reversed(s))
>>> tuple(sorted_items(sample, key=reverse_string))
(('foo', 20), ('bar', 42), ('baz', 10))
>>> tuple(sorted_items(sample, reverse=True))
(('foo', 20), ('baz', 10), ('bar', 42))
"""
# wrap the key func so it operates on the first element of each item
def pairkey_key(item):
return key(item[0])
return sorted(d.items(), key=pairkey_key, reverse=reverse) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:get_or_create_in_transaction; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 4, identifier:tsession; 5, identifier:model; 6, identifier:values; 7, default_parameter; 7, 8; 7, 9; 8, identifier:missing_columns; 9, list:[]; 10, default_parameter; 10, 11; 10, 12; 11, identifier:variable_columns; 12, list:[]; 13, default_parameter; 13, 14; 13, 15; 14, identifier:updatable_columns; 15, list:[]; 16, default_parameter; 16, 17; 16, 18; 17, identifier:only_use_supplied_columns; 18, False; 19, default_parameter; 19, 20; 19, 21; 20, identifier:read_only; 21, False; 22, block; 22, 23; 22, 25; 22, 34; 22, 35; 22, 53; 22, 64; 22, 75; 22, 91; 22, 179; 22, 183; 22, 214; 22, 229; 22, 243; 22, 251; 23, expression_statement; 23, 24; 24, string:'''
Uses the SQLAlchemy model to retrieve an existing record based on the supplied field values or, if there is no
existing record, to create a new database record.
:param tsession: An SQLAlchemy transactioned session
:param model: The name of the SQLAlchemy class representing the table
:param values: A dict of values which will be used to populate the fields of the model
:param missing_columns: Elements of missing_columns are expected to be fields in the model but are left blank regardless of whether they exist in values. This is useful for auto_increment fields.
:param updatable_columns: If these are specified, they are treated as missing columns in the record matching and if a record is found, these fields will be updated
:param variable_columns: If these are specified, they are treated as missing columns in the record matching but are not updated. A good use of these are for datetime fields which default to the current datetime
:param read_only: If this is set then we query the database and return an instance if one exists but we do not create a new record.
:return:
Note: This function is a convenience function and is NOT efficient. The "tsession.query(model).filter_by(**pruned_values)"
call is only (sometimes) efficient if an index exists on the keys of pruned_values. If any of the fields of pruned_values are
large (even if otherwise deferred/loaded lazily) then you will incur a performance hit on lookup. You may need
to reconsider any calls to this function in inner loops of your code.'''; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:values; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:copy; 31, identifier:deepcopy; 32, argument_list; 32, 33; 33, identifier:values; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:fieldnames; 38, list_comprehension; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:c; 41, identifier:name; 42, for_in_clause; 42, 43; 42, 44; 43, identifier:c; 44, call; 44, 45; 44, 46; 45, identifier:list; 46, argument_list; 46, 47; 47, attribute; 47, 48; 47, 52; 48, call; 48, 49; 48, 50; 49, identifier:sqlalchemy_inspect; 50, argument_list; 50, 51; 51, identifier:model; 52, identifier:columns; 53, for_statement; 53, 54; 53, 55; 53, 56; 54, identifier:c; 55, identifier:missing_columns; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:fieldnames; 61, identifier:remove; 62, argument_list; 62, 63; 63, identifier:c; 64, for_statement; 64, 65; 64, 66; 64, 67; 65, identifier:c; 66, identifier:updatable_columns; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:fieldnames; 72, identifier:remove; 73, argument_list; 73, 74; 74, identifier:c; 75, for_statement; 75, 76; 75, 77; 75, 78; 76, identifier:c; 77, identifier:variable_columns; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 83; 80, comparison_operator:in; 80, 81; 80, 82; 81, identifier:c; 82, identifier:fieldnames; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:fieldnames; 88, identifier:remove; 89, argument_list; 89, 90; 90, identifier:c; 91, if_statement; 91, 92; 91, 93; 91, 116; 92, identifier:only_use_supplied_columns; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:fieldnames; 97, call; 97, 98; 97, 99; 98, identifier:sorted; 99, argument_list; 99, 100; 100, call; 100, 101; 100, 107; 101, attribute; 101, 102; 101, 106; 102, call; 102, 103; 102, 104; 103, identifier:set; 104, argument_list; 104, 105; 105, identifier:fieldnames; 106, identifier:intersection; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:set; 110, argument_list; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:values; 114, identifier:keys; 115, argument_list; 116, else_clause; 116, 117; 117, block; 117, 118; 117, 153; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:unexpected_fields; 121, call; 121, 122; 121, 148; 122, attribute; 122, 123; 122, 147; 123, call; 123, 124; 123, 142; 124, attribute; 124, 125; 124, 141; 125, call; 125, 126; 125, 136; 126, attribute; 126, 127; 126, 135; 127, call; 127, 128; 127, 129; 128, identifier:set; 129, argument_list; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:values; 133, identifier:keys; 134, argument_list; 135, identifier:difference; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:set; 139, argument_list; 139, 140; 140, identifier:fieldnames; 141, identifier:difference; 142, argument_list; 142, 143; 143, call; 143, 144; 143, 145; 144, identifier:set; 145, argument_list; 145, 146; 146, identifier:variable_columns; 147, identifier:difference; 148, argument_list; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:set; 151, argument_list; 151, 152; 152, identifier:updatable_columns; 153, if_statement; 153, 154; 153, 155; 154, identifier:unexpected_fields; 155, block; 155, 156; 156, raise_statement; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:Exception; 159, argument_list; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, string:"The fields '{0}' were passed but not found in the schema for table {1}."; 163, identifier:format; 164, argument_list; 164, 165; 164, 174; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, string:"', '"; 168, identifier:join; 169, argument_list; 169, 170; 170, call; 170, 171; 170, 172; 171, identifier:sorted; 172, argument_list; 172, 173; 173, identifier:unexpected_fields; 174, subscript; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:model; 177, identifier:__dict__; 178, string:'__tablename__'; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:pruned_values; 182, dictionary; 183, for_statement; 183, 184; 183, 185; 183, 201; 184, identifier:k; 185, call; 185, 186; 185, 196; 186, attribute; 186, 187; 186, 195; 187, call; 187, 188; 187, 189; 188, identifier:set; 189, argument_list; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:values; 193, identifier:keys; 194, argument_list; 195, identifier:intersection; 196, argument_list; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:set; 199, argument_list; 199, 200; 200, identifier:fieldnames; 201, block; 201, 202; 201, 208; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:v; 205, subscript; 205, 206; 205, 207; 206, identifier:values; 207, identifier:k; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:pruned_values; 212, identifier:k; 213, identifier:v; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:instance; 217, call; 217, 218; 217, 226; 218, attribute; 218, 219; 218, 225; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:tsession; 222, identifier:query; 223, argument_list; 223, 224; 224, identifier:model; 225, identifier:filter_by; 226, argument_list; 226, 227; 227, dictionary_splat; 227, 228; 228, identifier:pruned_values; 229, if_statement; 229, 230; 229, 237; 230, comparison_operator:>; 230, 231; 230, 236; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:instance; 234, identifier:count; 235, argument_list; 236, integer:1; 237, block; 237, 238; 238, raise_statement; 238, 239; 239, call; 239, 240; 239, 241; 240, identifier:Exception; 241, argument_list; 241, 242; 242, string:'Multiple records were found with the search criteria.'; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:instance; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:instance; 249, identifier:first; 250, argument_list; 251, if_statement; 251, 252; 251, 253; 251, 280; 252, identifier:instance; 253, block; 253, 254; 253, 278; 254, if_statement; 254, 255; 254, 258; 255, comparison_operator:==; 255, 256; 255, 257; 256, identifier:read_only; 257, False; 258, block; 258, 259; 258, 272; 259, for_statement; 259, 260; 259, 261; 259, 262; 260, identifier:c; 261, identifier:updatable_columns; 262, block; 262, 263; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:setattr; 266, argument_list; 266, 267; 266, 268; 266, 269; 267, identifier:instance; 268, identifier:c; 269, subscript; 269, 270; 269, 271; 270, identifier:values; 271, identifier:c; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:tsession; 276, identifier:flush; 277, argument_list; 278, return_statement; 278, 279; 279, identifier:instance; 280, else_clause; 280, 281; 281, block; 281, 282; 281, 348; 282, if_statement; 282, 283; 282, 286; 283, comparison_operator:==; 283, 284; 283, 285; 284, identifier:read_only; 285, False; 286, block; 286, 287; 286, 325; 286, 333; 286, 340; 286, 346; 287, if_statement; 287, 288; 287, 301; 287, 302; 288, comparison_operator:!=; 288, 289; 288, 297; 289, call; 289, 290; 289, 291; 290, identifier:sorted; 291, argument_list; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:pruned_values; 295, identifier:keys; 296, argument_list; 297, call; 297, 298; 297, 299; 298, identifier:sorted; 299, argument_list; 299, 300; 300, identifier:fieldnames; 301, comment; 302, block; 302, 303; 303, raise_statement; 303, 304; 304, call; 304, 305; 304, 306; 305, identifier:Exception; 306, argument_list; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, string:'Some required fields are missing: {0}. Either supply these fields or add them to the missing_columns list.'; 310, identifier:format; 311, argument_list; 311, 312; 312, call; 312, 313; 312, 319; 313, attribute; 313, 314; 313, 318; 314, call; 314, 315; 314, 316; 315, identifier:set; 316, argument_list; 316, 317; 317, identifier:fieldnames; 318, identifier:difference; 319, argument_list; 319, 320; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:pruned_values; 323, identifier:keys; 324, argument_list; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:instance; 328, call; 328, 329; 328, 330; 329, identifier:model; 330, argument_list; 330, 331; 331, dictionary_splat; 331, 332; 332, identifier:pruned_values; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:tsession; 337, identifier:add; 338, argument_list; 338, 339; 339, identifier:instance; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:tsession; 344, identifier:flush; 345, argument_list; 346, return_statement; 346, 347; 347, identifier:instance; 348, return_statement; 348, 349; 349, None | def get_or_create_in_transaction(tsession, model, values, missing_columns = [], variable_columns = [], updatable_columns = [], only_use_supplied_columns = False, read_only = False):
'''
Uses the SQLAlchemy model to retrieve an existing record based on the supplied field values or, if there is no
existing record, to create a new database record.
:param tsession: An SQLAlchemy transactioned session
:param model: The name of the SQLAlchemy class representing the table
:param values: A dict of values which will be used to populate the fields of the model
:param missing_columns: Elements of missing_columns are expected to be fields in the model but are left blank regardless of whether they exist in values. This is useful for auto_increment fields.
:param updatable_columns: If these are specified, they are treated as missing columns in the record matching and if a record is found, these fields will be updated
:param variable_columns: If these are specified, they are treated as missing columns in the record matching but are not updated. A good use of these are for datetime fields which default to the current datetime
:param read_only: If this is set then we query the database and return an instance if one exists but we do not create a new record.
:return:
Note: This function is a convenience function and is NOT efficient. The "tsession.query(model).filter_by(**pruned_values)"
call is only (sometimes) efficient if an index exists on the keys of pruned_values. If any of the fields of pruned_values are
large (even if otherwise deferred/loaded lazily) then you will incur a performance hit on lookup. You may need
to reconsider any calls to this function in inner loops of your code.'''
values = copy.deepcopy(values) # todo: this does not seem to be necessary since we do not seem to be writing
fieldnames = [c.name for c in list(sqlalchemy_inspect(model).columns)]
for c in missing_columns:
fieldnames.remove(c)
for c in updatable_columns:
fieldnames.remove(c)
for c in variable_columns:
if c in fieldnames:
fieldnames.remove(c)
if only_use_supplied_columns:
fieldnames = sorted(set(fieldnames).intersection(set(values.keys())))
else:
unexpected_fields = set(values.keys()).difference(set(fieldnames)).difference(set(variable_columns)).difference(set(updatable_columns))
if unexpected_fields:
raise Exception("The fields '{0}' were passed but not found in the schema for table {1}.".format("', '".join(sorted(unexpected_fields)), model.__dict__['__tablename__']))
pruned_values = {}
for k in set(values.keys()).intersection(set(fieldnames)):
v = values[k]
pruned_values[k] = v
instance = tsession.query(model).filter_by(**pruned_values)
if instance.count() > 1:
raise Exception('Multiple records were found with the search criteria.')
instance = instance.first()
if instance:
if read_only == False:
for c in updatable_columns:
setattr(instance, c, values[c])
tsession.flush()
return instance
else:
if read_only == False:
if sorted(pruned_values.keys()) != sorted(fieldnames):
# When adding new records, we require that all necessary fields are present
raise Exception('Some required fields are missing: {0}. Either supply these fields or add them to the missing_columns list.'.format(set(fieldnames).difference(pruned_values.keys())))
instance = model(**pruned_values)
tsession.add(instance)
tsession.flush()
return instance
return None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:errors; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:instance; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 15; 9, 23; 9, 84; 9, 183; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 14; 13, identifier:instance; 14, identifier:dict; 15, block; 15, 16; 16, return_statement; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:_validate; 21, argument_list; 21, 22; 22, identifier:instance; 23, elif_clause; 23, 24; 23, 31; 24, call; 24, 25; 24, 26; 25, identifier:isinstance; 26, argument_list; 26, 27; 26, 28; 27, identifier:instance; 28, attribute; 28, 29; 28, 30; 29, identifier:forms; 30, identifier:BaseForm; 31, block; 31, 32; 31, 52; 32, if_statement; 32, 33; 32, 42; 33, boolean_operator:and; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:instance; 36, identifier:is_bound; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:instance; 40, identifier:is_valid; 41, argument_list; 42, block; 42, 43; 43, return_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:_validate; 48, argument_list; 48, 49; 49, attribute; 49, 50; 49, 51; 50, identifier:instance; 51, identifier:cleaned_data; 52, return_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_validate; 57, argument_list; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:dict; 60, argument_list; 60, 61; 61, list_comprehension; 61, 62; 61, 79; 62, tuple; 62, 63; 62, 64; 63, identifier:f; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:instance; 68, identifier:initial; 69, identifier:get; 70, argument_list; 70, 71; 70, 72; 71, identifier:f; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:instance; 76, identifier:f; 77, identifier:value; 78, argument_list; 79, for_in_clause; 79, 80; 79, 81; 80, identifier:f; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:validators; 84, elif_clause; 84, 85; 84, 92; 85, call; 85, 86; 85, 87; 86, identifier:isinstance; 87, argument_list; 87, 88; 87, 89; 88, identifier:instance; 89, attribute; 89, 90; 89, 91; 90, identifier:formsets; 91, identifier:BaseFormSet; 92, block; 92, 93; 93, if_statement; 93, 94; 93, 97; 93, 150; 94, attribute; 94, 95; 94, 96; 95, identifier:instance; 96, identifier:can_delete; 97, block; 97, 98; 97, 139; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:validate_forms; 101, binary_operator:+; 101, 102; 101, 117; 102, list_comprehension; 102, 103; 102, 104; 102, 109; 103, identifier:form; 104, for_in_clause; 104, 105; 104, 106; 105, identifier:form; 106, attribute; 106, 107; 106, 108; 107, identifier:instance; 108, identifier:initial_forms; 109, if_clause; 109, 110; 110, not_operator; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:instance; 114, identifier:_should_delete_form; 115, argument_list; 115, 116; 116, identifier:form; 117, list_comprehension; 117, 118; 117, 119; 117, 124; 118, identifier:form; 119, for_in_clause; 119, 120; 119, 121; 120, identifier:form; 121, attribute; 121, 122; 121, 123; 122, identifier:instance; 123, identifier:extra_forms; 124, if_clause; 124, 125; 125, parenthesized_expression; 125, 126; 126, boolean_operator:and; 126, 127; 126, 132; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:form; 130, identifier:has_changed; 131, argument_list; 132, not_operator; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:instance; 136, identifier:_should_delete_form; 137, argument_list; 137, 138; 138, identifier:form; 139, return_statement; 139, 140; 140, list_comprehension; 140, 141; 140, 147; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:errors; 145, argument_list; 145, 146; 146, identifier:f; 147, for_in_clause; 147, 148; 147, 149; 148, identifier:f; 149, identifier:validate_forms; 150, else_clause; 150, 151; 151, block; 151, 152; 151, 172; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:validate_forms; 155, binary_operator:+; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:instance; 158, identifier:initial_forms; 159, list_comprehension; 159, 160; 159, 161; 159, 166; 160, identifier:form; 161, for_in_clause; 161, 162; 161, 163; 162, identifier:form; 163, attribute; 163, 164; 163, 165; 164, identifier:instance; 165, identifier:extra_forms; 166, if_clause; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:form; 170, identifier:has_changed; 171, argument_list; 172, return_statement; 172, 173; 173, list_comprehension; 173, 174; 173, 180; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:errors; 178, argument_list; 178, 179; 179, identifier:f; 180, for_in_clause; 180, 181; 180, 182; 181, identifier:f; 182, identifier:validate_forms; 183, elif_clause; 183, 184; 183, 191; 184, call; 184, 185; 184, 186; 185, identifier:isinstance; 186, argument_list; 186, 187; 186, 188; 187, identifier:instance; 188, attribute; 188, 189; 188, 190; 189, identifier:models; 190, identifier:Model; 191, block; 191, 192; 192, return_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:_validate; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 200; 199, identifier:dict; 200, argument_list; 200, 201; 201, list_comprehension; 201, 202; 201, 209; 202, tuple; 202, 203; 202, 204; 203, identifier:f; 204, call; 204, 205; 204, 206; 205, identifier:getattr; 206, argument_list; 206, 207; 206, 208; 207, identifier:instance; 208, identifier:f; 209, for_in_clause; 209, 210; 209, 211; 210, identifier:f; 211, attribute; 211, 212; 211, 213; 212, identifier:self; 213, identifier:validators | def errors(self, instance):
"""Run all field validators and return a dict of errors.
The keys of the resulting dict coorespond to field
names. instance can be a dict (ie, form.cleaned_data), a form,
a formset, or a model instance.
If instance is a form, full_clean() will be called if the form
is bound.
If instance is a formset, full_clean() will be called on each
member form, if bound.
"""
if isinstance(instance, dict):
return self._validate(instance)
elif isinstance(instance, forms.BaseForm):
if instance.is_bound and instance.is_valid():
return self._validate(instance.cleaned_data)
return self._validate(dict(
[
(f, instance.initial.get(f, instance[f].value()))
for f in self.validators
]
))
elif isinstance(instance, formsets.BaseFormSet):
if instance.can_delete:
validate_forms = [
form for form in instance.initial_forms
if not instance._should_delete_form(form)
] + [
form for form in instance.extra_forms
if (form.has_changed() and
not instance._should_delete_form(form))
]
return [
self.errors(f)
for f in validate_forms
]
else:
validate_forms = instance.initial_forms + [
form for form in instance.extra_forms
if form.has_changed()
]
return [self.errors(f) for f in validate_forms]
elif isinstance(instance, models.Model):
return self._validate(dict(
[(f, getattr(instance, f)) for f in self.validators]
)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:multimatch; 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:origin; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:rel; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:target; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:attrs; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:include_ids; 19, False; 20, block; 20, 21; 20, 23; 20, 25; 20, 44; 20, 63; 20, 82; 20, 219; 21, expression_statement; 21, 22; 22, string:'''
Iterator over relationship IDs that match a pattern of components, with multiple options provided for each component
origin - (optional) origin of the relationship (similar to an RDF subject), or set of values. If omitted any origin will be matched.
rel - (optional) type IRI of the relationship (similar to an RDF predicate), or set of values. If omitted any relationship will be matched.
target - (optional) target of the relationship (similar to an RDF object), a boolean, floating point or unicode object, or set of values. If omitted any target will be matched.
attrs - (optional) attribute mapping of relationship metadata, i.e. {attrname1: attrval1, attrname2: attrval2}. If any attribute is specified, an exact match is made (i.e. the attribute name and value must match).
include_ids - If true include statement IDs with yield values
'''; 23, raise_statement; 23, 24; 24, identifier:NotImplementedError; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:origin; 28, conditional_expression:if; 28, 29; 28, 30; 28, 39; 29, identifier:origin; 30, boolean_operator:or; 30, 31; 30, 34; 31, comparison_operator:is; 31, 32; 31, 33; 32, identifier:origin; 33, None; 34, call; 34, 35; 34, 36; 35, identifier:isinstance; 36, argument_list; 36, 37; 36, 38; 37, identifier:origin; 38, identifier:set; 39, call; 39, 40; 39, 41; 40, identifier:set; 41, argument_list; 41, 42; 42, list:[origin]; 42, 43; 43, identifier:origin; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:rel; 47, conditional_expression:if; 47, 48; 47, 49; 47, 58; 48, identifier:rel; 49, boolean_operator:or; 49, 50; 49, 53; 50, comparison_operator:is; 50, 51; 50, 52; 51, identifier:rel; 52, None; 53, call; 53, 54; 53, 55; 54, identifier:isinstance; 55, argument_list; 55, 56; 55, 57; 56, identifier:rel; 57, identifier:set; 58, call; 58, 59; 58, 60; 59, identifier:set; 60, argument_list; 60, 61; 61, list:[rel]; 61, 62; 62, identifier:rel; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:target; 66, conditional_expression:if; 66, 67; 66, 68; 66, 77; 67, identifier:target; 68, boolean_operator:or; 68, 69; 68, 72; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:target; 71, None; 72, call; 72, 73; 72, 74; 73, identifier:isinstance; 74, argument_list; 74, 75; 74, 76; 75, identifier:target; 76, identifier:set; 77, call; 77, 78; 77, 79; 78, identifier:set; 79, argument_list; 79, 80; 80, list:[target]; 80, 81; 81, identifier:target; 82, for_statement; 82, 83; 82, 86; 82, 92; 83, pattern_list; 83, 84; 83, 85; 84, identifier:index; 85, identifier:curr_rel; 86, call; 86, 87; 86, 88; 87, identifier:enumerate; 88, argument_list; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:_relationships; 92, block; 92, 93; 92, 97; 92, 110; 92, 123; 92, 136; 92, 171; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:matches; 96, True; 97, if_statement; 97, 98; 97, 105; 98, boolean_operator:and; 98, 99; 98, 100; 99, identifier:origin; 100, comparison_operator:not; 100, 101; 100, 104; 101, subscript; 101, 102; 101, 103; 102, identifier:curr_rel; 103, identifier:ORIGIN; 104, identifier:origin; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:matches; 109, False; 110, if_statement; 110, 111; 110, 118; 111, boolean_operator:and; 111, 112; 111, 113; 112, identifier:rel; 113, comparison_operator:not; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:curr_rel; 116, identifier:RELATIONSHIP; 117, identifier:rel; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:matches; 122, False; 123, if_statement; 123, 124; 123, 131; 124, boolean_operator:and; 124, 125; 124, 126; 125, identifier:target; 126, comparison_operator:not; 126, 127; 126, 130; 127, subscript; 127, 128; 127, 129; 128, identifier:curr_rel; 129, identifier:TARGET; 130, identifier:target; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:matches; 135, False; 136, if_statement; 136, 137; 136, 138; 137, identifier:attrs; 138, block; 138, 139; 139, for_statement; 139, 140; 139, 143; 139, 148; 140, pattern_list; 140, 141; 140, 142; 141, identifier:k; 142, identifier:v; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:attrs; 146, identifier:items; 147, argument_list; 148, block; 148, 149; 149, if_statement; 149, 150; 149, 166; 150, boolean_operator:or; 150, 151; 150, 156; 151, comparison_operator:not; 151, 152; 151, 153; 152, identifier:k; 153, subscript; 153, 154; 153, 155; 154, identifier:curr_rel; 155, identifier:ATTRIBUTES; 156, comparison_operator:!=; 156, 157; 156, 165; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:curr_rel; 161, identifier:ATTRIBUTES; 162, identifier:get; 163, argument_list; 163, 164; 164, identifier:k; 165, identifier:v; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:matches; 170, False; 171, if_statement; 171, 172; 171, 173; 172, identifier:matches; 173, block; 173, 174; 174, if_statement; 174, 175; 174, 176; 174, 198; 175, identifier:include_ids; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, yield; 178, 179; 179, expression_list; 179, 180; 179, 181; 180, identifier:index; 181, tuple; 181, 182; 181, 185; 181, 188; 181, 191; 182, subscript; 182, 183; 182, 184; 183, identifier:curr_rel; 184, integer:0; 185, subscript; 185, 186; 185, 187; 186, identifier:curr_rel; 187, integer:1; 188, subscript; 188, 189; 188, 190; 189, identifier:curr_rel; 190, integer:2; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:curr_rel; 195, integer:3; 196, identifier:copy; 197, argument_list; 198, else_clause; 198, 199; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, yield; 201, 202; 202, tuple; 202, 203; 202, 206; 202, 209; 202, 212; 203, subscript; 203, 204; 203, 205; 204, identifier:curr_rel; 205, integer:0; 206, subscript; 206, 207; 206, 208; 207, identifier:curr_rel; 208, integer:1; 209, subscript; 209, 210; 209, 211; 210, identifier:curr_rel; 211, integer:2; 212, call; 212, 213; 212, 218; 213, attribute; 213, 214; 213, 217; 214, subscript; 214, 215; 214, 216; 215, identifier:curr_rel; 216, integer:3; 217, identifier:copy; 218, argument_list; 219, return_statement | def multimatch(self, origin=None, rel=None, target=None, attrs=None, include_ids=False):
'''
Iterator over relationship IDs that match a pattern of components, with multiple options provided for each component
origin - (optional) origin of the relationship (similar to an RDF subject), or set of values. If omitted any origin will be matched.
rel - (optional) type IRI of the relationship (similar to an RDF predicate), or set of values. If omitted any relationship will be matched.
target - (optional) target of the relationship (similar to an RDF object), a boolean, floating point or unicode object, or set of values. If omitted any target will be matched.
attrs - (optional) attribute mapping of relationship metadata, i.e. {attrname1: attrval1, attrname2: attrval2}. If any attribute is specified, an exact match is made (i.e. the attribute name and value must match).
include_ids - If true include statement IDs with yield values
'''
raise NotImplementedError
origin = origin if origin is None or isinstance(origin, set) else set([origin])
rel = rel if rel is None or isinstance(rel, set) else set([rel])
target = target if target is None or isinstance(target, set) else set([target])
for index, curr_rel in enumerate(self._relationships):
matches = True
if origin and curr_rel[ORIGIN] not in origin:
matches = False
if rel and curr_rel[RELATIONSHIP] not in rel:
matches = False
if target and curr_rel[TARGET] not in target:
matches = False
if attrs:
for k, v in attrs.items():
if k not in curr_rel[ATTRIBUTES] or curr_rel[ATTRIBUTES].get(k) != v:
matches = False
if matches:
if include_ids:
yield index, (curr_rel[0], curr_rel[1], curr_rel[2], curr_rel[3].copy())
else:
yield (curr_rel[0], curr_rel[1], curr_rel[2], curr_rel[3].copy())
return |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:get_qtls_from_mapqtl_data; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:matrix; 5, identifier:threshold; 6, identifier:inputfile; 7, block; 7, 8; 7, 10; 7, 29; 7, 33; 7, 37; 7, 140; 7, 171; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:trait_name; 13, subscript; 13, 14; 13, 28; 14, call; 14, 15; 14, 26; 15, attribute; 15, 16; 15, 25; 16, subscript; 16, 17; 16, 24; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:inputfile; 20, identifier:split; 21, argument_list; 21, 22; 21, 23; 22, string:')_'; 23, integer:1; 24, integer:1; 25, identifier:split; 26, argument_list; 26, 27; 27, string:'.mqo'; 28, integer:0; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:qtls; 32, list:[]; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:qtl; 36, None; 37, for_statement; 37, 38; 37, 39; 37, 44; 38, identifier:entry; 39, subscript; 39, 40; 39, 41; 40, identifier:matrix; 41, slice; 41, 42; 41, 43; 42, integer:1; 43, colon; 44, block; 44, 45; 44, 54; 44, 93; 44, 107; 44, 121; 45, if_statement; 45, 46; 45, 49; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:qtl; 48, None; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:qtl; 53, identifier:entry; 54, if_statement; 54, 55; 54, 62; 55, comparison_operator:!=; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:qtl; 58, integer:1; 59, subscript; 59, 60; 59, 61; 60, identifier:entry; 61, integer:1; 62, block; 62, 63; 62, 89; 63, if_statement; 63, 64; 63, 75; 64, comparison_operator:>; 64, 65; 64, 71; 65, call; 65, 66; 65, 67; 66, identifier:float; 67, argument_list; 67, 68; 68, subscript; 68, 69; 68, 70; 69, identifier:qtl; 70, integer:4; 71, call; 71, 72; 71, 73; 72, identifier:float; 73, argument_list; 73, 74; 74, identifier:threshold; 75, block; 75, 76; 75, 82; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:qtl; 80, integer:0; 81, identifier:trait_name; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:qtls; 86, identifier:append; 87, argument_list; 87, 88; 88, identifier:qtl; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:qtl; 92, identifier:entry; 93, if_statement; 93, 94; 93, 99; 93, 100; 94, comparison_operator:==; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:entry; 97, integer:4; 98, string:''; 99, comment; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:entry; 105, integer:4; 106, integer:0; 107, if_statement; 107, 108; 107, 113; 107, 114; 108, comparison_operator:==; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:qtl; 111, integer:4; 112, string:''; 113, comment; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:qtl; 119, integer:4; 120, integer:0; 121, if_statement; 121, 122; 121, 135; 122, comparison_operator:>; 122, 123; 122, 129; 123, call; 123, 124; 123, 125; 124, identifier:float; 125, argument_list; 125, 126; 126, subscript; 126, 127; 126, 128; 127, identifier:entry; 128, integer:4; 129, call; 129, 130; 129, 131; 130, identifier:float; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 134; 133, identifier:qtl; 134, integer:4; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:qtl; 139, identifier:entry; 140, if_statement; 140, 141; 140, 152; 141, comparison_operator:>; 141, 142; 141, 148; 142, call; 142, 143; 142, 144; 143, identifier:float; 144, argument_list; 144, 145; 145, subscript; 145, 146; 145, 147; 146, identifier:qtl; 147, integer:4; 148, call; 148, 149; 148, 150; 149, identifier:float; 150, argument_list; 150, 151; 151, identifier:threshold; 152, block; 152, 153; 152, 159; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:qtl; 157, integer:0; 158, identifier:trait_name; 159, if_statement; 159, 160; 159, 163; 160, comparison_operator:not; 160, 161; 160, 162; 161, identifier:qtl; 162, identifier:qtls; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:qtls; 168, identifier:append; 169, argument_list; 169, 170; 170, identifier:qtl; 171, return_statement; 171, 172; 172, identifier:qtls | def get_qtls_from_mapqtl_data(matrix, threshold, inputfile):
"""Extract the QTLs found by MapQTL reading its file.
This assume that there is only one QTL per linkage group.
:arg matrix, the MapQTL file read in memory
:arg threshold, threshold used to determine if a given LOD value is
reflective the presence of a QTL.
:arg inputfile, name of the inputfile in which the QTLs have been
found
"""
trait_name = inputfile.split(')_', 1)[1].split('.mqo')[0]
qtls = []
qtl = None
for entry in matrix[1:]:
if qtl is None:
qtl = entry
if qtl[1] != entry[1]:
if float(qtl[4]) > float(threshold):
qtl[0] = trait_name
qtls.append(qtl)
qtl = entry
if entry[4] == '': # pragma: no cover
entry[4] = 0
if qtl[4] == '': # pragma: no cover
qtl[4] = 0
if float(entry[4]) > float(qtl[4]):
qtl = entry
if float(qtl[4]) > float(threshold):
qtl[0] = trait_name
if qtl not in qtls:
qtls.append(qtl)
return qtls |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:merge_range_pairs; 3, parameters; 3, 4; 4, identifier:prs; 5, block; 5, 6; 5, 8; 5, 12; 5, 23; 5, 30; 5, 34; 5, 38; 5, 144; 6, expression_statement; 6, 7; 7, string:'''Takes in a list of pairs specifying ranges and returns a sorted list of merged, sorted ranges.'''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:new_prs; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:sprs; 15, list_comprehension; 15, 16; 15, 20; 16, call; 16, 17; 16, 18; 17, identifier:sorted; 18, argument_list; 18, 19; 19, identifier:p; 20, for_in_clause; 20, 21; 20, 22; 21, identifier:p; 22, identifier:prs; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:sprs; 26, call; 26, 27; 26, 28; 27, identifier:sorted; 28, argument_list; 28, 29; 29, identifier:sprs; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:merged; 33, False; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:x; 37, integer:0; 38, while_statement; 38, 39; 38, 45; 39, comparison_operator:<; 39, 40; 39, 41; 40, identifier:x; 41, call; 41, 42; 41, 43; 42, identifier:len; 43, argument_list; 43, 44; 44, identifier:sprs; 45, block; 45, 46; 45, 52; 45, 61; 45, 128; 45, 140; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:newx; 49, binary_operator:+; 49, 50; 49, 51; 50, identifier:x; 51, integer:1; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:new_pair; 55, call; 55, 56; 55, 57; 56, identifier:list; 57, argument_list; 57, 58; 58, subscript; 58, 59; 58, 60; 59, identifier:sprs; 60, identifier:x; 61, for_statement; 61, 62; 61, 63; 61, 73; 62, identifier:y; 63, call; 63, 64; 63, 65; 64, identifier:range; 65, argument_list; 65, 66; 65, 69; 66, binary_operator:+; 66, 67; 66, 68; 67, identifier:x; 68, integer:1; 69, call; 69, 70; 69, 71; 70, identifier:len; 71, argument_list; 71, 72; 72, identifier:sprs; 73, block; 73, 74; 74, if_statement; 74, 75; 74, 89; 75, comparison_operator:<=; 75, 76; 75, 79; 75, 86; 76, subscript; 76, 77; 76, 78; 77, identifier:new_pair; 78, integer:0; 79, binary_operator:-; 79, 80; 79, 85; 80, subscript; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:sprs; 83, identifier:y; 84, integer:0; 85, integer:1; 86, subscript; 86, 87; 86, 88; 87, identifier:new_pair; 88, integer:1; 89, block; 89, 90; 89, 106; 89, 122; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:new_pair; 94, integer:0; 95, call; 95, 96; 95, 97; 96, identifier:min; 97, argument_list; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:new_pair; 100, integer:0; 101, subscript; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:sprs; 104, identifier:y; 105, integer:0; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:new_pair; 110, integer:1; 111, call; 111, 112; 111, 113; 112, identifier:max; 113, argument_list; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:new_pair; 116, integer:1; 117, subscript; 117, 118; 117, 121; 118, subscript; 118, 119; 118, 120; 119, identifier:sprs; 120, identifier:y; 121, integer:1; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:newx; 125, binary_operator:+; 125, 126; 125, 127; 126, identifier:y; 127, integer:1; 128, if_statement; 128, 129; 128, 132; 129, comparison_operator:not; 129, 130; 129, 131; 130, identifier:new_pair; 131, identifier:new_prs; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:new_prs; 137, identifier:append; 138, argument_list; 138, 139; 139, identifier:new_pair; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:x; 143, identifier:newx; 144, return_statement; 144, 145; 145, identifier:new_prs | def merge_range_pairs(prs):
'''Takes in a list of pairs specifying ranges and returns a sorted list of merged, sorted ranges.'''
new_prs = []
sprs = [sorted(p) for p in prs]
sprs = sorted(sprs)
merged = False
x = 0
while x < len(sprs):
newx = x + 1
new_pair = list(sprs[x])
for y in range(x + 1, len(sprs)):
if new_pair[0] <= sprs[y][0] - 1 <= new_pair[1]:
new_pair[0] = min(new_pair[0], sprs[y][0])
new_pair[1] = max(new_pair[1], sprs[y][1])
newx = y + 1
if new_pair not in new_prs:
new_prs.append(new_pair)
x = newx
return new_prs |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 29; 2, function_name:create; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:agent_cls; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:n_agents; 10, integer:10; 11, default_parameter; 11, 12; 11, 13; 12, identifier:agent_kwargs; 13, dictionary; 14, default_parameter; 14, 15; 14, 16; 15, identifier:env_cls; 16, identifier:Environment; 17, default_parameter; 17, 18; 17, 19; 18, identifier:env_kwargs; 19, dictionary; 20, default_parameter; 20, 21; 20, 22; 21, identifier:callback; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:conns; 25, integer:0; 26, default_parameter; 26, 27; 26, 28; 27, identifier:log_folder; 28, None; 29, block; 29, 30; 29, 32; 29, 54; 29, 71; 29, 128; 29, 138; 29, 142; 29, 227; 29, 241; 30, expression_statement; 30, 31; 31, comment; 32, if_statement; 32, 33; 32, 39; 33, not_operator; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:issubclass; 36, argument_list; 36, 37; 36, 38; 37, identifier:env_cls; 38, identifier:Environment; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:TypeError; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, string:"Environment class must be derived from ({}"; 47, identifier:format; 48, argument_list; 48, 49; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:Environment; 52, identifier:__class__; 53, identifier:__name__; 54, if_statement; 54, 55; 54, 65; 55, boolean_operator:and; 55, 56; 55, 59; 56, comparison_operator:is; 56, 57; 56, 58; 57, identifier:callback; 58, None; 59, not_operator; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:hasattr; 62, argument_list; 62, 63; 62, 64; 63, identifier:callback; 64, string:'__call__'; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:TypeError; 69, argument_list; 69, 70; 70, string:"Callback must be callable."; 71, if_statement; 71, 72; 71, 77; 71, 104; 72, call; 72, 73; 72, 74; 73, identifier:hasattr; 74, argument_list; 74, 75; 74, 76; 75, identifier:agent_cls; 76, string:'__iter__'; 77, block; 77, 78; 78, for_statement; 78, 79; 78, 80; 78, 81; 79, identifier:e; 80, identifier:agent_cls; 81, block; 81, 82; 82, if_statement; 82, 83; 82, 89; 83, not_operator; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:issubclass; 86, argument_list; 86, 87; 86, 88; 87, identifier:e; 88, identifier:CreativeAgent; 89, block; 89, 90; 90, raise_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:TypeError; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, string:"All agent classes must be derived from {}"; 97, identifier:format; 98, argument_list; 98, 99; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:CreativeAgent; 102, identifier:__class__; 103, identifier:__name__; 104, else_clause; 104, 105; 105, block; 105, 106; 106, if_statement; 106, 107; 106, 113; 107, not_operator; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:issubclass; 110, argument_list; 110, 111; 110, 112; 111, identifier:agent_cls; 112, identifier:CreativeAgent; 113, block; 113, 114; 114, raise_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:TypeError; 117, argument_list; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, string:"Agent class must be derived from {}"; 121, identifier:format; 122, argument_list; 122, 123; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:CreativeAgent; 126, identifier:__class__; 127, identifier:__name__; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:env; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:env_cls; 134, identifier:create; 135, argument_list; 135, 136; 136, dictionary_splat; 136, 137; 137, identifier:env_kwargs; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:agents; 141, list:[]; 142, if_statement; 142, 143; 142, 148; 142, 198; 143, call; 143, 144; 143, 145; 144, identifier:hasattr; 145, argument_list; 145, 146; 145, 147; 146, identifier:agent_cls; 147, string:'__iter__'; 148, block; 148, 149; 149, for_statement; 149, 150; 149, 151; 149, 158; 150, identifier:i; 151, call; 151, 152; 151, 153; 152, identifier:range; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:len; 156, argument_list; 156, 157; 157, identifier:n_agents; 158, block; 158, 159; 158, 167; 158, 175; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 166; 161, subscript; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:agent_kwargs; 164, identifier:i; 165, string:'environment'; 166, identifier:env; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 174; 169, subscript; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:agent_kwargs; 172, identifier:i; 173, string:'log_folder'; 174, identifier:log_folder; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:agents; 178, binary_operator:+; 178, 179; 178, 180; 179, identifier:agents; 180, list_comprehension; 180, 181; 180, 190; 181, call; 181, 182; 181, 185; 182, subscript; 182, 183; 182, 184; 183, identifier:agent_cls; 184, identifier:i; 185, argument_list; 185, 186; 186, dictionary_splat; 186, 187; 187, subscript; 187, 188; 187, 189; 188, identifier:agent_kwargs; 189, identifier:i; 190, for_in_clause; 190, 191; 190, 192; 191, identifier:e; 192, call; 192, 193; 192, 194; 193, identifier:range; 194, argument_list; 194, 195; 195, subscript; 195, 196; 195, 197; 196, identifier:n_agents; 197, identifier:i; 198, else_clause; 198, 199; 199, block; 199, 200; 199, 206; 199, 212; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:agent_kwargs; 204, string:'environment'; 205, identifier:env; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 211; 208, subscript; 208, 209; 208, 210; 209, identifier:agent_kwargs; 210, string:'log_folder'; 211, identifier:log_folder; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:agents; 215, list_comprehension; 215, 216; 215, 221; 216, call; 216, 217; 216, 218; 217, identifier:agent_cls; 218, argument_list; 218, 219; 219, dictionary_splat; 219, 220; 220, identifier:agent_kwargs; 221, for_in_clause; 221, 222; 221, 223; 222, identifier:e; 223, call; 223, 224; 223, 225; 224, identifier:range; 225, argument_list; 225, 226; 226, identifier:n_agents; 227, if_statement; 227, 228; 227, 231; 228, comparison_operator:>; 228, 229; 228, 230; 229, identifier:conns; 230, integer:0; 231, block; 231, 232; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:env; 236, identifier:create_random_connections; 237, argument_list; 237, 238; 238, keyword_argument; 238, 239; 238, 240; 239, identifier:n; 240, identifier:conns; 241, return_statement; 241, 242; 242, call; 242, 243; 242, 244; 243, identifier:Simulation; 244, argument_list; 244, 245; 244, 246; 244, 247; 245, identifier:env; 246, identifier:callback; 247, identifier:log_folder | def create(self, agent_cls=None, n_agents=10, agent_kwargs={},
env_cls=Environment, env_kwargs={}, callback=None, conns=0,
log_folder=None):
"""A convenience function to create simple simulations.
Method first creates environment, then instantiates agents into it
with give arguments, and finally creates simulation for the
environment.
:param agent_cls:
class for agents, or list of classes. If list, then **n_agents**
and **agent_kwargs** are expected to be lists also.
:param n_agents:
amount of agents for simulation, or list of amounts
:param agent_kwargs:
keyword arguments passed to agents at creation time, or list of
keyword arguments.
:param env_cls:
environment class for simulation
:type env_cls:
:py:class:`~creamas.core.environment.Environment`
:param dict env_kwargs:
keyword arguments passed to environment at creation time
:param callable callback:
optional callable to call after each simulation step
:param conns:
Create **conns** amount of initial (random) connections for agents
in the simulation environment.
:param str log_folder:
folder for possible logging. This overwrites *log_folder* keyword
argument from **agent_kwargs** and **env_kwargs**.
"""
if not issubclass(env_cls, Environment):
raise TypeError("Environment class must be derived from ({}"
.format(Environment.__class__.__name__))
if callback is not None and not hasattr(callback, '__call__'):
raise TypeError("Callback must be callable.")
if hasattr(agent_cls, '__iter__'):
for e in agent_cls:
if not issubclass(e, CreativeAgent):
raise TypeError("All agent classes must be derived from {}"
.format(CreativeAgent.__class__.__name__))
else:
if not issubclass(agent_cls, CreativeAgent):
raise TypeError("Agent class must be derived from {}"
.format(CreativeAgent.__class__.__name__))
env = env_cls.create(**env_kwargs)
agents = []
if hasattr(agent_cls, '__iter__'):
for i in range(len(n_agents)):
agent_kwargs[i]['environment'] = env
agent_kwargs[i]['log_folder'] = log_folder
agents = agents + [agent_cls[i](**agent_kwargs[i]) for e in
range(n_agents[i])]
else:
agent_kwargs['environment'] = env
agent_kwargs['log_folder'] = log_folder
agents = [agent_cls(**agent_kwargs) for e in range(n_agents)]
if conns > 0:
env.create_random_connections(n=conns)
return Simulation(env, callback, log_folder) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:callproc; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:procname; 6, default_parameter; 6, 7; 6, 8; 7, identifier:parameters; 8, tuple; 9, default_parameter; 9, 10; 9, 11; 10, identifier:quiet; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:expect_return_value; 14, False; 15, block; 15, 16; 15, 18; 15, 24; 15, 28; 15, 32; 15, 36; 15, 40; 15, 91; 15, 107; 15, 124; 15, 275; 15, 325; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, augmented_assignment:+=; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:procedures_run; 23, integer:1; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:i; 27, integer:0; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:errcode; 31, integer:0; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:caughte; 35, None; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:out_param_indices; 39, list:[]; 40, for_statement; 40, 41; 40, 42; 40, 49; 41, identifier:j; 42, call; 42, 43; 42, 44; 43, identifier:range; 44, argument_list; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:len; 47, argument_list; 47, 48; 48, identifier:parameters; 49, block; 49, 50; 49, 56; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:p; 53, subscript; 53, 54; 53, 55; 54, identifier:parameters; 55, identifier:j; 56, if_statement; 56, 57; 56, 72; 57, boolean_operator:and; 57, 58; 57, 67; 58, comparison_operator:==; 58, 59; 58, 63; 59, call; 59, 60; 59, 61; 60, identifier:type; 61, argument_list; 61, 62; 62, identifier:p; 63, call; 63, 64; 63, 65; 64, identifier:type; 65, argument_list; 65, 66; 66, string:''; 67, comparison_operator:==; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:p; 70, integer:0; 71, string:'@'; 72, block; 72, 73; 72, 84; 73, assert_statement; 73, 74; 74, parenthesized_expression; 74, 75; 75, comparison_operator:==; 75, 76; 75, 82; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:p; 79, identifier:find; 80, argument_list; 80, 81; 81, string:' '; 82, unary_operator:-; 82, 83; 83, integer:1; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:out_param_indices; 88, identifier:append; 89, argument_list; 89, 90; 90, identifier:j; 91, if_statement; 91, 92; 91, 99; 92, comparison_operator:not; 92, 93; 92, 94; 93, identifier:procname; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:list_stored_procedures; 98, argument_list; 99, block; 99, 100; 100, raise_statement; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:Exception; 103, argument_list; 103, 104; 104, binary_operator:%; 104, 105; 104, 106; 105, string:"The stored procedure '%s' does not exist."; 106, identifier:procname; 107, if_statement; 107, 108; 107, 116; 108, not_operator; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:re; 112, identifier:match; 113, argument_list; 113, 114; 113, 115; 114, string:"^\s*\w+\s*$"; 115, identifier:procname; 116, block; 116, 117; 117, raise_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:Exception; 120, argument_list; 120, 121; 121, binary_operator:%; 121, 122; 121, 123; 122, string:"Expected a stored procedure name in callproc but received '%s'."; 123, identifier:procname; 124, while_statement; 124, 125; 124, 130; 125, comparison_operator:<; 125, 126; 125, 127; 126, identifier:i; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:numTries; 130, block; 130, 131; 130, 135; 131, expression_statement; 131, 132; 132, augmented_assignment:+=; 132, 133; 132, 134; 133, identifier:i; 134, integer:1; 135, try_statement; 135, 136; 135, 237; 135, 260; 136, block; 136, 137; 136, 143; 136, 153; 136, 169; 136, 179; 136, 190; 136, 196; 136, 197; 136, 201; 136, 235; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:_get_connection; 142, argument_list; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:cursor; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:connection; 151, identifier:cursor; 152, argument_list; 153, if_statement; 153, 154; 153, 163; 154, comparison_operator:!=; 154, 155; 154, 159; 155, call; 155, 156; 155, 157; 156, identifier:type; 157, argument_list; 157, 158; 158, identifier:parameters; 159, call; 159, 160; 159, 161; 160, identifier:type; 161, argument_list; 161, 162; 162, tuple; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:parameters; 167, tuple; 167, 168; 168, identifier:parameters; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:errcode; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:cursor; 175, identifier:callproc; 176, argument_list; 176, 177; 176, 178; 177, identifier:procname; 178, identifier:parameters; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:lastrowid; 184, call; 184, 185; 184, 186; 185, identifier:int; 186, argument_list; 186, 187; 187, attribute; 187, 188; 187, 189; 188, identifier:cursor; 189, identifier:lastrowid; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:cursor; 194, identifier:close; 195, argument_list; 196, comment; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:out_param_results; 200, list:[]; 201, if_statement; 201, 202; 201, 203; 202, identifier:out_param_indices; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:out_param_results; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:execute; 211, argument_list; 211, 212; 212, binary_operator:%; 212, 213; 212, 214; 213, string:'SELECT %s'; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, string:", "; 217, identifier:join; 218, argument_list; 218, 219; 219, list_comprehension; 219, 220; 219, 232; 220, binary_operator:%; 220, 221; 220, 222; 221, string:'@_%s_%d AS %s'; 222, tuple; 222, 223; 222, 224; 222, 225; 223, identifier:procname; 224, identifier:pindex; 225, subscript; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:parameters; 228, identifier:pindex; 229, slice; 229, 230; 229, 231; 230, integer:1; 231, colon; 232, for_in_clause; 232, 233; 232, 234; 233, identifier:pindex; 234, identifier:out_param_indices; 235, return_statement; 235, 236; 236, identifier:out_param_results; 237, except_clause; 237, 238; 237, 241; 237, 242; 238, attribute; 238, 239; 238, 240; 239, identifier:MySQLdb; 240, identifier:OperationalError; 241, identifier:e; 242, block; 242, 243; 242, 249; 242, 255; 242, 259; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:self; 247, identifier:_close_connection; 248, argument_list; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:errcode; 252, subscript; 252, 253; 252, 254; 253, identifier:e; 254, integer:0; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:caughte; 258, identifier:e; 259, continue_statement; 260, except_clause; 260, 261; 261, block; 261, 262; 261, 268; 261, 274; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:self; 266, identifier:_close_connection; 267, argument_list; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:traceback; 272, identifier:print_exc; 273, argument_list; 274, break_statement; 275, if_statement; 275, 276; 275, 278; 276, not_operator; 276, 277; 277, identifier:quiet; 278, block; 278, 279; 278, 301; 278, 317; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 286; 281, attribute; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:sys; 284, identifier:stderr; 285, identifier:write; 286, argument_list; 286, 287; 287, binary_operator:%; 287, 288; 287, 289; 288, string:"\nSQL execution error call stored procedure %s at %s:"; 289, tuple; 289, 290; 289, 291; 290, identifier:procname; 291, call; 291, 292; 291, 299; 292, attribute; 292, 293; 292, 298; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:datetime; 296, identifier:now; 297, argument_list; 298, identifier:strftime; 299, argument_list; 299, 300; 300, string:"%Y-%m-%d %H:%M:%S"; 301, expression_statement; 301, 302; 302, call; 302, 303; 302, 308; 303, attribute; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:sys; 306, identifier:stderr; 307, identifier:write; 308, argument_list; 308, 309; 309, binary_operator:%; 309, 310; 309, 311; 310, string:"\nErrorcode/Error: %d - '%s'.\n"; 311, tuple; 311, 312; 311, 313; 312, identifier:errcode; 313, call; 313, 314; 313, 315; 314, identifier:str; 315, argument_list; 315, 316; 316, identifier:caughte; 317, expression_statement; 317, 318; 318, call; 318, 319; 318, 324; 319, attribute; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:sys; 322, identifier:stderr; 323, identifier:flush; 324, argument_list; 325, raise_statement; 325, 326; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:MySQLdb; 329, identifier:OperationalError; 330, argument_list; 330, 331; 331, identifier:caughte | def callproc(self, procname, parameters=(), quiet=False, expect_return_value=False):
"""Calls a MySQL stored procedure procname and returns the return values. This uses DictCursor.
To get return values back out of a stored procedure, prefix the parameter with a @ character.
"""
self.procedures_run += 1
i = 0
errcode = 0
caughte = None
out_param_indices = []
for j in range(len(parameters)):
p = parameters[j]
if type(p) == type('') and p[0] == '@':
assert(p.find(' ') == -1)
out_param_indices.append(j)
if procname not in self.list_stored_procedures():
raise Exception("The stored procedure '%s' does not exist." % procname)
if not re.match("^\s*\w+\s*$", procname):
raise Exception("Expected a stored procedure name in callproc but received '%s'." % procname)
while i < self.numTries:
i += 1
try:
self._get_connection()
cursor = self.connection.cursor()
if type(parameters) != type(()):
parameters = (parameters,)
errcode = cursor.callproc(procname, parameters)
self.lastrowid = int(cursor.lastrowid)
cursor.close()
# Get the out parameters
out_param_results = []
if out_param_indices:
out_param_results = self.execute('SELECT %s' % ", ".join(['@_%s_%d AS %s' % (procname, pindex, parameters[pindex][1:]) for pindex in out_param_indices]))
return out_param_results
except MySQLdb.OperationalError, e:
self._close_connection()
errcode = e[0]
caughte = e
continue
except:
self._close_connection()
traceback.print_exc()
break
if not quiet:
sys.stderr.write("\nSQL execution error call stored procedure %s at %s:" % (
procname, datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
sys.stderr.write("\nErrorcode/Error: %d - '%s'.\n" % (errcode, str(caughte)))
sys.stderr.flush()
raise MySQLdb.OperationalError(caughte) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:create_insert_dict_string; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:self; 5, identifier:tblname; 6, identifier:d; 7, default_parameter; 7, 8; 7, 9; 8, identifier:PKfields; 9, list:[]; 10, default_parameter; 10, 11; 10, 12; 11, identifier:fields; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:check_existing; 15, False; 16, block; 16, 17; 16, 19; 16, 35; 16, 51; 16, 55; 16, 59; 17, expression_statement; 17, 18; 18, string:'''The main function of the insert_dict functions.
This creates and returns the SQL query and parameters used by the other functions but does not insert any data into the database.
Simple function for inserting a dictionary whose keys match the fieldnames of tblname. The function returns two values, the
second of which is a dict containing the primary keys of the record. If a record already exists then no insertion is performed and
(False, the dictionary of existing primary keys) is returned. Otherwise, the record is inserted into the database and (True, d)
is returned.'''; 19, if_statement; 19, 20; 19, 29; 20, comparison_operator:==; 20, 21; 20, 25; 21, call; 21, 22; 21, 23; 22, identifier:type; 23, argument_list; 23, 24; 24, identifier:PKfields; 25, call; 25, 26; 25, 27; 26, identifier:type; 27, argument_list; 27, 28; 28, string:""; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:PKfields; 33, list:[PKfields]; 33, 34; 34, identifier:PKfields; 35, if_statement; 35, 36; 35, 39; 36, comparison_operator:==; 36, 37; 36, 38; 37, identifier:fields; 38, None; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:fields; 43, call; 43, 44; 43, 45; 44, identifier:sorted; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:d; 49, identifier:keys; 50, argument_list; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:values; 54, None; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:SQL; 58, None; 59, try_statement; 59, 60; 59, 61; 59, 208; 60, comment; 61, block; 61, 62; 61, 66; 61, 70; 61, 110; 61, 118; 61, 126; 61, 130; 61, 163; 61, 190; 61, 203; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:wherestr; 65, list:[]; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:PKvalues; 69, list:[]; 70, for_statement; 70, 71; 70, 72; 70, 73; 71, identifier:PKfield; 72, identifier:PKfields; 73, block; 73, 74; 74, if_statement; 74, 75; 74, 80; 74, 90; 75, comparison_operator:==; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:d; 78, identifier:PKfield; 79, None; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:wherestr; 85, identifier:append; 86, argument_list; 86, 87; 87, binary_operator:%; 87, 88; 87, 89; 88, string:"%s IS NULL"; 89, identifier:PKfield; 90, else_clause; 90, 91; 91, block; 91, 92; 91, 101; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:wherestr; 96, identifier:append; 97, argument_list; 97, 98; 98, binary_operator:%; 98, 99; 98, 100; 99, string:"%s=%%s"; 100, identifier:PKfield; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:PKvalues; 105, identifier:append; 106, argument_list; 106, 107; 107, subscript; 107, 108; 107, 109; 108, identifier:d; 109, identifier:PKfield; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:PKfields; 113, call; 113, 114; 113, 115; 114, identifier:join; 115, argument_list; 115, 116; 115, 117; 116, identifier:PKfields; 117, string:","; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:wherestr; 121, call; 121, 122; 121, 123; 122, identifier:join; 123, argument_list; 123, 124; 123, 125; 124, identifier:wherestr; 125, string:" AND "; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:record_exists; 129, None; 130, if_statement; 130, 131; 130, 132; 131, identifier:check_existing; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:record_exists; 136, not_operator; 136, 137; 137, parenthesized_expression; 137, 138; 138, not_operator; 138, 139; 139, parenthesized_expression; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:execute_select; 144, argument_list; 144, 145; 144, 154; 144, 160; 145, binary_operator:+; 145, 146; 145, 151; 146, binary_operator:%; 146, 147; 146, 148; 147, string:"SELECT %s FROM %s"; 148, tuple; 148, 149; 148, 150; 149, identifier:PKfields; 150, identifier:tblname; 151, binary_operator:%; 151, 152; 151, 153; 152, string:" WHERE %s"; 153, identifier:wherestr; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:parameters; 156, call; 156, 157; 156, 158; 157, identifier:tuple; 158, argument_list; 158, 159; 159, identifier:PKvalues; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:locked; 162, False; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:SQL; 166, binary_operator:%; 166, 167; 166, 168; 167, string:'INSERT INTO %s (%s) VALUES (%s)'; 168, tuple; 168, 169; 168, 170; 168, 175; 169, identifier:tblname; 170, call; 170, 171; 170, 172; 171, identifier:join; 172, argument_list; 172, 173; 172, 174; 173, identifier:fields; 174, string:", "; 175, call; 175, 176; 175, 177; 176, identifier:join; 177, argument_list; 177, 178; 177, 189; 178, list_comprehension; 178, 179; 178, 180; 179, string:'%s'; 180, for_in_clause; 180, 181; 180, 182; 181, identifier:x; 182, call; 182, 183; 182, 184; 183, identifier:range; 184, argument_list; 184, 185; 185, call; 185, 186; 185, 187; 186, identifier:len; 187, argument_list; 187, 188; 188, identifier:fields; 189, string:','; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:values; 193, call; 193, 194; 193, 195; 194, identifier:tuple; 195, argument_list; 195, 196; 196, list_comprehension; 196, 197; 196, 200; 197, subscript; 197, 198; 197, 199; 198, identifier:d; 199, identifier:k; 200, for_in_clause; 200, 201; 200, 202; 201, identifier:k; 202, identifier:fields; 203, return_statement; 203, 204; 204, expression_list; 204, 205; 204, 206; 204, 207; 205, identifier:SQL; 206, identifier:values; 207, identifier:record_exists; 208, except_clause; 208, 209; 208, 210; 208, 211; 209, identifier:Exception; 210, identifier:e; 211, block; 211, 212; 212, raise_statement; 212, 213; 213, call; 213, 214; 213, 215; 214, identifier:Exception; 215, argument_list; 215, 216; 216, binary_operator:%; 216, 217; 216, 218; 217, string:"Error occurred during database insertion: '%s'. %s"; 218, tuple; 218, 219; 218, 223; 219, call; 219, 220; 219, 221; 220, identifier:str; 221, argument_list; 221, 222; 222, identifier:e; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:traceback; 226, identifier:format_exc; 227, argument_list | def create_insert_dict_string(self, tblname, d, PKfields=[], fields=None, check_existing = False):
'''The main function of the insert_dict functions.
This creates and returns the SQL query and parameters used by the other functions but does not insert any data into the database.
Simple function for inserting a dictionary whose keys match the fieldnames of tblname. The function returns two values, the
second of which is a dict containing the primary keys of the record. If a record already exists then no insertion is performed and
(False, the dictionary of existing primary keys) is returned. Otherwise, the record is inserted into the database and (True, d)
is returned.'''
if type(PKfields) == type(""):
PKfields = [PKfields]
if fields == None:
fields = sorted(d.keys())
values = None
SQL = None
try:
# Search for existing records
wherestr = []
PKvalues = []
for PKfield in PKfields:
if d[PKfield] == None:
wherestr.append("%s IS NULL" % PKfield)
else:
wherestr.append("%s=%%s" % PKfield)
PKvalues.append(d[PKfield])
PKfields = join(PKfields, ",")
wherestr = join(wherestr, " AND ")
record_exists = None
if check_existing:
record_exists = not(not(self.execute_select("SELECT %s FROM %s" % (PKfields, tblname) + " WHERE %s" % wherestr, parameters=tuple(PKvalues), locked = False)))
SQL = 'INSERT INTO %s (%s) VALUES (%s)' % (
tblname, join(fields, ", "), join(['%s' for x in range(len(fields))], ','))
values = tuple([d[k] for k in fields])
return SQL, values, record_exists
except Exception, e:
raise Exception("Error occurred during database insertion: '%s'. %s" % (str(e), traceback.format_exc())) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:fixed_point; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:is_zero; 5, identifier:plus; 6, identifier:minus; 7, identifier:f; 8, identifier:x; 9, block; 9, 10; 9, 12; 9, 57; 10, expression_statement; 10, 11; 11, comment; 12, decorated_definition; 12, 13; 12, 15; 13, decorator; 13, 14; 14, identifier:memo_Y; 15, function_definition; 15, 16; 15, 17; 15, 19; 16, function_name:_fixed_point; 17, parameters; 17, 18; 18, identifier:fixed_point_fun; 19, block; 19, 20; 19, 55; 20, function_definition; 20, 21; 20, 22; 20, 25; 21, function_name:__fixed_point; 22, parameters; 22, 23; 22, 24; 23, identifier:collected; 24, identifier:new; 25, block; 25, 26; 25, 34; 25, 42; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:diff; 29, call; 29, 30; 29, 31; 30, identifier:minus; 31, argument_list; 31, 32; 31, 33; 32, identifier:new; 33, identifier:collected; 34, if_statement; 34, 35; 34, 39; 35, call; 35, 36; 35, 37; 36, identifier:is_zero; 37, argument_list; 37, 38; 38, identifier:diff; 39, block; 39, 40; 40, return_statement; 40, 41; 41, identifier:collected; 42, return_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:fixed_point_fun; 45, argument_list; 45, 46; 45, 51; 46, call; 46, 47; 46, 48; 47, identifier:plus; 48, argument_list; 48, 49; 48, 50; 49, identifier:collected; 50, identifier:diff; 51, call; 51, 52; 51, 53; 52, identifier:f; 53, argument_list; 53, 54; 54, identifier:diff; 55, return_statement; 55, 56; 56, identifier:__fixed_point; 57, return_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:_fixed_point; 60, argument_list; 60, 61; 60, 62; 61, identifier:x; 62, call; 62, 63; 62, 64; 63, identifier:f; 64, argument_list; 64, 65; 65, identifier:x | def fixed_point(is_zero, plus, minus, f, x):
"""
Get the least fixed point when it can be computed piecewise.
.. testsetup::
from proso.func import fixed_point
.. doctest::
>>> sorted(fixed_point(
... is_zero=lambda xs: len(xs) == 0,
... plus=lambda xs, ys: xs + ys,
... minus=lambda xs, ys: [x for x in xs if x not in ys],
... f=lambda xs: [x + 1 for x in xs if x < 10],
... x=[0, 5, 8]
... ))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Args:
is_zero: function returning True if the given value is zero
plus: function taking two values and returning their addition
minus: function taking two values and returning ther difference
f: function computing the expected value
x: initial value
Returns:
The least fixed point.
"""
@memo_Y
def _fixed_point(fixed_point_fun):
def __fixed_point(collected, new):
diff = minus(new, collected)
if is_zero(diff):
return collected
return fixed_point_fun(plus(collected, diff), f(diff))
return __fixed_point
return _fixed_point(x, f(x)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:connections_from_graph; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:env; 5, identifier:G; 6, default_parameter; 6, 7; 6, 8; 7, identifier:edge_data; 8, False; 9, block; 9, 10; 9, 12; 9, 31; 9, 44; 9, 55; 9, 86; 9, 87; 9, 94; 9, 100; 9, 108; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 23; 13, not_operator; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:issubclass; 16, argument_list; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:G; 19, identifier:__class__; 20, tuple; 20, 21; 20, 22; 21, identifier:Graph; 22, identifier:DiGraph; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:TypeError; 27, argument_list; 27, 28; 28, concatenated_string; 28, 29; 28, 30; 29, string:"Graph structure must be derived from Networkx's "; 30, string:"Graph or DiGraph."; 31, if_statement; 31, 32; 31, 38; 32, not_operator; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:hasattr; 35, argument_list; 35, 36; 35, 37; 36, identifier:env; 37, string:'get_agents'; 38, block; 38, 39; 39, raise_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:TypeError; 42, argument_list; 42, 43; 43, string:"Parameter 'env' must have get_agents."; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:addrs; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:env; 50, identifier:get_agents; 51, argument_list; 51, 52; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:addr; 54, True; 55, if_statement; 55, 56; 55, 65; 56, comparison_operator:!=; 56, 57; 56, 61; 57, call; 57, 58; 57, 59; 58, identifier:len; 59, argument_list; 59, 60; 60, identifier:addrs; 61, call; 61, 62; 61, 63; 62, identifier:len; 63, argument_list; 63, 64; 64, identifier:G; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:ValueError; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 77; 71, attribute; 71, 72; 71, 76; 72, concatenated_string; 72, 73; 72, 74; 72, 75; 73, string:"The number of graph nodes and agents in the "; 74, string:"environment (excluding the manager agent) must "; 75, string:"match. Now got {} nodes and {} agents."; 76, identifier:format; 77, argument_list; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:len; 80, argument_list; 80, 81; 81, identifier:G; 82, call; 82, 83; 82, 84; 83, identifier:len; 84, argument_list; 84, 85; 85, identifier:addrs; 86, comment; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:addrs; 90, call; 90, 91; 90, 92; 91, identifier:sort_addrs; 92, argument_list; 92, 93; 93, identifier:addrs; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:_addrs2nodes; 97, argument_list; 97, 98; 97, 99; 98, identifier:addrs; 99, identifier:G; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:conn_map; 103, call; 103, 104; 103, 105; 104, identifier:_edges2conns; 105, argument_list; 105, 106; 105, 107; 106, identifier:G; 107, identifier:edge_data; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:env; 112, identifier:create_connections; 113, argument_list; 113, 114; 114, identifier:conn_map | def connections_from_graph(env, G, edge_data=False):
"""Create connections for agents in the given environment from the given
NetworkX graph structure.
:param env:
Environment where the agents live. The environment should be derived
from :class:`~creamas.core.environment.Environment`,
:class:`~creamas.mp.MultiEnvironment` or
:class:`~creamas.ds.DistributedEnvironment`.
:param G:
NetworkX graph structure, either :class:`networkx.graph.Graph` or
:class:`networkx.digraph.DiGraph`. The graph needs to have the same
number of nodes as the environment has agents (excluding the managers).
:param bool edge_data:
If ``True``, edge data from the given graph is copied to the agents'
:attr:`connections`.
.. note::
By design, manager agents are excluded from the connections and should
not be counted towards environment's agent count.
The created connections are stored in each agent's
:attr:`~creamas.core.agent.CreativeAgent.connections` and the possible
edge data is stored as key-value pairs in the connection dictionary.
The agents are sorted by their environments' hosts and ports before each
agent is mapped to a node in **G**. This should cause some network
generation methods in NetworkX, e.g.
:func:`~networkx.generators.random_graphs.connected_watts_strogatz_graph`,
to create more connections between agents in the same environment and/or
node when using :class:`~creamas.mp.MultiEnvironment` or
:class:`~creamas.ds.DistributedEnvironment`.
"""
if not issubclass(G.__class__, (Graph, DiGraph)):
raise TypeError("Graph structure must be derived from Networkx's "
"Graph or DiGraph.")
if not hasattr(env, 'get_agents'):
raise TypeError("Parameter 'env' must have get_agents.")
addrs = env.get_agents(addr=True)
if len(addrs) != len(G):
raise ValueError("The number of graph nodes and agents in the "
"environment (excluding the manager agent) must "
"match. Now got {} nodes and {} agents."
.format(len(G), len(addrs)))
# Sort agent addresses to the order they were added to the environment.
addrs = sort_addrs(addrs)
_addrs2nodes(addrs, G)
conn_map = _edges2conns(G, edge_data)
env.create_connections(conn_map) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:graph_from_connections; 3, parameters; 3, 4; 3, 5; 4, identifier:env; 5, default_parameter; 5, 6; 5, 7; 6, identifier:directed; 7, False; 8, block; 8, 9; 8, 11; 8, 22; 8, 33; 8, 85; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:G; 14, conditional_expression:if; 14, 15; 14, 18; 14, 19; 15, call; 15, 16; 15, 17; 16, identifier:DiGraph; 17, argument_list; 18, identifier:directed; 19, call; 19, 20; 19, 21; 20, identifier:Graph; 21, argument_list; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:conn_list; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:env; 28, identifier:get_connections; 29, argument_list; 29, 30; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:data; 32, True; 33, for_statement; 33, 34; 33, 37; 33, 38; 34, pattern_list; 34, 35; 34, 36; 35, identifier:agent; 36, identifier:conns; 37, identifier:conn_list; 38, block; 38, 39; 38, 46; 38, 50; 38, 70; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:G; 43, identifier:add_node; 44, argument_list; 44, 45; 45, identifier:agent; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:ebunch; 49, list:[]; 50, for_statement; 50, 51; 50, 54; 50, 59; 51, pattern_list; 51, 52; 51, 53; 52, identifier:nb; 53, identifier:data; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:conns; 57, identifier:items; 58, argument_list; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:ebunch; 64, identifier:append; 65, argument_list; 65, 66; 66, tuple; 66, 67; 66, 68; 66, 69; 67, identifier:agent; 68, identifier:nb; 69, identifier:data; 70, if_statement; 70, 71; 70, 77; 71, comparison_operator:>; 71, 72; 71, 76; 72, call; 72, 73; 72, 74; 73, identifier:len; 74, argument_list; 74, 75; 75, identifier:ebunch; 76, integer:0; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:G; 82, identifier:add_edges_from; 83, argument_list; 83, 84; 84, identifier:ebunch; 85, return_statement; 85, 86; 86, identifier:G | def graph_from_connections(env, directed=False):
"""Create NetworkX graph from agent connections in a given environment.
:param env:
Environment where the agents live. The environment must be derived from
:class:`~creamas.core.environment.Environment`,
:class:`~creamas.mp.MultiEnvironment` or
:class:`~creamas.ds.DistributedEnvironment`.
:param bool directed:
If ``True``, creates an instance of :class:`~networkx.digraph.DiGraph`,
otherwise creates an instance of :class:`~networkx.graph.Graph`.
:returns: The created NetworkX graph.
:rtype:
:class:`~networkx.digraph.DiGraph` or :class:`~networkx.graph.Graph`
.. note::
If the created graph is undirected and two connected agents have
different data stored for each other, then the data for the given edge
is chosen randomly between the two agents.
"""
G = DiGraph() if directed else Graph()
conn_list = env.get_connections(data=True)
for agent, conns in conn_list:
G.add_node(agent)
ebunch = []
for nb, data in conns.items():
ebunch.append((agent, nb, data))
if len(ebunch) > 0:
G.add_edges_from(ebunch)
return G |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:profile; 3, parameters; 3, 4; 3, 5; 4, identifier:request; 5, default_parameter; 5, 6; 5, 7; 6, identifier:status; 7, integer:200; 8, block; 8, 9; 8, 11; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 17; 11, 151; 11, 303; 12, comparison_operator:==; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:request; 15, identifier:method; 16, string:'GET'; 17, block; 17, 18; 17, 134; 18, if_statement; 18, 19; 18, 28; 18, 64; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:request; 23, identifier:GET; 24, identifier:get; 25, argument_list; 25, 26; 25, 27; 26, string:"username"; 27, False; 28, block; 28, 29; 29, try_statement; 29, 30; 29, 56; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:user_profile; 34, attribute; 34, 35; 34, 55; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:User; 39, identifier:objects; 40, identifier:get; 41, argument_list; 41, 42; 41, 52; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:username; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:request; 48, identifier:GET; 49, identifier:get; 50, argument_list; 50, 51; 51, string:"username"; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:userprofile__public; 54, True; 55, identifier:userprofile; 56, except_clause; 56, 57; 56, 58; 57, identifier:ObjectDoesNotExist; 58, block; 58, 59; 59, raise_statement; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:Http404; 62, argument_list; 62, 63; 63, string:"user not found or have not public profile"; 64, else_clause; 64, 65; 65, block; 65, 66; 65, 73; 65, 124; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:user_id; 69, call; 69, 70; 69, 71; 70, identifier:get_user_id; 71, argument_list; 71, 72; 72, identifier:request; 73, if_statement; 73, 74; 73, 88; 74, boolean_operator:and; 74, 75; 74, 83; 75, call; 75, 76; 75, 77; 76, identifier:get_config; 77, argument_list; 77, 78; 77, 79; 77, 80; 78, string:'proso_user'; 79, string:'google.openid.migration'; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:default; 82, True; 83, not_operator; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:is_user_id_overridden; 86, argument_list; 86, 87; 87, identifier:request; 88, block; 88, 89; 88, 98; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:migrated_user; 92, call; 92, 93; 92, 94; 93, identifier:migrate_google_openid_user; 94, argument_list; 94, 95; 95, attribute; 95, 96; 95, 97; 96, identifier:request; 97, identifier:user; 98, if_statement; 98, 99; 98, 102; 99, comparison_operator:is; 99, 100; 99, 101; 100, identifier:migrated_user; 101, None; 102, block; 102, 103; 102, 110; 102, 116; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:auth; 107, identifier:logout; 108, argument_list; 108, 109; 109, identifier:request; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:migrated_user; 114, identifier:backend; 115, string:'social.backends.google.GoogleOAuth2'; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:auth; 120, identifier:login; 121, argument_list; 121, 122; 121, 123; 122, identifier:request; 123, identifier:migrated_user; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:user_profile; 127, call; 127, 128; 127, 129; 128, identifier:get_object_or_404; 129, argument_list; 129, 130; 129, 131; 130, identifier:UserProfile; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:user_id; 133, identifier:user_id; 134, return_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:render_json; 137, argument_list; 137, 138; 137, 139; 137, 140; 137, 143; 137, 146; 138, identifier:request; 139, identifier:user_profile; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:status; 142, identifier:status; 143, keyword_argument; 143, 144; 143, 145; 144, identifier:template; 145, string:'user_profile.html'; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:help_text; 148, attribute; 148, 149; 148, 150; 149, identifier:profile; 150, identifier:__doc__; 151, elif_clause; 151, 152; 151, 157; 152, comparison_operator:==; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:request; 155, identifier:method; 156, string:'POST'; 157, block; 157, 158; 157, 289; 157, 295; 158, with_statement; 158, 159; 158, 166; 159, with_clause; 159, 160; 160, with_item; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:transaction; 164, identifier:atomic; 165, argument_list; 166, block; 166, 167; 166, 181; 166, 188; 166, 198; 166, 208; 166, 224; 166, 240; 166, 269; 166, 283; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:to_save; 170, call; 170, 171; 170, 172; 171, identifier:json_body; 172, argument_list; 172, 173; 173, call; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:request; 177, identifier:body; 178, identifier:decode; 179, argument_list; 179, 180; 180, string:"utf-8"; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:user_id; 184, call; 184, 185; 184, 186; 185, identifier:get_user_id; 186, argument_list; 186, 187; 187, identifier:request; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:user_profile; 191, call; 191, 192; 191, 193; 192, identifier:get_object_or_404; 193, argument_list; 193, 194; 193, 195; 194, identifier:UserProfile; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:user_id; 197, identifier:user_id; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:user; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:to_save; 204, identifier:get; 205, argument_list; 205, 206; 205, 207; 206, string:'user'; 207, None; 208, if_statement; 208, 209; 208, 212; 209, comparison_operator:in; 209, 210; 209, 211; 210, string:'send_emails'; 211, identifier:to_save; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:user_profile; 217, identifier:send_emails; 218, call; 218, 219; 218, 220; 219, identifier:bool; 220, argument_list; 220, 221; 221, subscript; 221, 222; 221, 223; 222, identifier:to_save; 223, string:'send_emails'; 224, if_statement; 224, 225; 224, 228; 225, comparison_operator:in; 225, 226; 225, 227; 226, string:'public'; 227, identifier:to_save; 228, block; 228, 229; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:user_profile; 233, identifier:public; 234, call; 234, 235; 234, 236; 235, identifier:bool; 236, argument_list; 236, 237; 237, subscript; 237, 238; 237, 239; 238, identifier:to_save; 239, string:'public'; 240, if_statement; 240, 241; 240, 242; 241, identifier:user; 242, block; 242, 243; 242, 254; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:error; 246, call; 246, 247; 246, 248; 247, identifier:_save_user; 248, argument_list; 248, 249; 248, 250; 248, 251; 249, identifier:request; 250, identifier:user; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:new; 253, False; 254, if_statement; 254, 255; 254, 256; 255, identifier:error; 256, block; 256, 257; 257, return_statement; 257, 258; 258, call; 258, 259; 258, 260; 259, identifier:render_json; 260, argument_list; 260, 261; 260, 262; 260, 263; 260, 266; 261, identifier:request; 262, identifier:error; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:template; 265, string:'user_json.html'; 266, keyword_argument; 266, 267; 266, 268; 267, identifier:status; 268, integer:400; 269, if_statement; 269, 270; 269, 273; 270, comparison_operator:in; 270, 271; 270, 272; 271, string:'properties'; 272, identifier:to_save; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:user_profile; 278, identifier:save_properties; 279, argument_list; 279, 280; 280, subscript; 280, 281; 280, 282; 281, identifier:to_save; 282, string:'properties'; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:user_profile; 287, identifier:save; 288, argument_list; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:request; 293, identifier:method; 294, string:"GET"; 295, return_statement; 295, 296; 296, call; 296, 297; 296, 298; 297, identifier:profile; 298, argument_list; 298, 299; 298, 300; 299, identifier:request; 300, keyword_argument; 300, 301; 300, 302; 301, identifier:status; 302, integer:202; 303, else_clause; 303, 304; 304, block; 304, 305; 305, return_statement; 305, 306; 306, call; 306, 307; 306, 308; 307, identifier:HttpResponseBadRequest; 308, argument_list; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, string:"method %s is not allowed"; 312, identifier:format; 313, argument_list; 313, 314; 314, attribute; 314, 315; 314, 316; 315, identifier:request; 316, identifier:method | def profile(request, status=200):
"""
Get the user's profile. If the user has no assigned profile, the HTTP 404
is returned. Make a POST request to modify the user's profile.
GET parameters:
html
turn on the HTML version of the API
username:
username of user (only for users with public profile)
stats:
attache addition user statistics
POST parameters (JSON):
send_emails:
switcher turning on sending e-mails to user
public:
swicher making the user's profile publicly available
user:
password:
user's password
password_check:
user's password again to check it
first_name (optional):
user's first name
last_name (optional):
user's last name
"""
if request.method == 'GET':
if request.GET.get("username", False):
try:
user_profile = User.objects.get(username=request.GET.get("username"),
userprofile__public=True).userprofile
except ObjectDoesNotExist:
raise Http404("user not found or have not public profile")
else:
user_id = get_user_id(request)
if get_config('proso_user', 'google.openid.migration', default=True) and not is_user_id_overridden(request):
migrated_user = migrate_google_openid_user(request.user)
if migrated_user is not None:
auth.logout(request)
migrated_user.backend = 'social.backends.google.GoogleOAuth2'
auth.login(request, migrated_user)
user_profile = get_object_or_404(UserProfile, user_id=user_id)
return render_json(
request, user_profile, status=status,
template='user_profile.html', help_text=profile.__doc__)
elif request.method == 'POST':
with transaction.atomic():
to_save = json_body(request.body.decode("utf-8"))
user_id = get_user_id(request)
user_profile = get_object_or_404(UserProfile, user_id=user_id)
user = to_save.get('user', None)
if 'send_emails' in to_save:
user_profile.send_emails = bool(to_save['send_emails'])
if 'public' in to_save:
user_profile.public = bool(to_save['public'])
if user:
error = _save_user(request, user, new=False)
if error:
return render_json(request, error, template='user_json.html', status=400)
if 'properties' in to_save:
user_profile.save_properties(to_save['properties'])
user_profile.save()
request.method = "GET"
return profile(request, status=202)
else:
return HttpResponseBadRequest("method %s is not allowed".format(request.method)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:get_session_identifiers; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:cls; 5, default_parameter; 5, 6; 5, 7; 6, identifier:folder; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:inputfile; 10, None; 11, block; 11, 12; 11, 14; 11, 18; 11, 28; 11, 177; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:sessions; 17, list:[]; 18, if_statement; 18, 19; 18, 22; 19, boolean_operator:and; 19, 20; 19, 21; 20, identifier:inputfile; 21, identifier:folder; 22, block; 22, 23; 23, raise_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:MQ2Exception; 26, argument_list; 26, 27; 27, string:'You should specify either a folder or a file'; 28, if_statement; 28, 29; 28, 30; 28, 117; 29, identifier:folder; 30, block; 30, 31; 30, 44; 31, if_statement; 31, 32; 31, 41; 32, not_operator; 32, 33; 33, call; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:os; 37, identifier:path; 38, identifier:isdir; 39, argument_list; 39, 40; 40, identifier:folder; 41, block; 41, 42; 42, return_statement; 42, 43; 43, identifier:sessions; 44, for_statement; 44, 45; 44, 49; 44, 55; 45, pattern_list; 45, 46; 45, 47; 45, 48; 46, identifier:root; 47, identifier:dirs; 48, identifier:files; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:os; 52, identifier:walk; 53, argument_list; 53, 54; 54, identifier:folder; 55, block; 55, 56; 56, for_statement; 56, 57; 56, 58; 56, 59; 57, identifier:filename; 58, identifier:files; 59, block; 59, 60; 59, 72; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:filename; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:os; 67, identifier:path; 68, identifier:join; 69, argument_list; 69, 70; 69, 71; 70, identifier:root; 71, identifier:filename; 72, for_statement; 72, 73; 72, 74; 72, 75; 73, identifier:ext; 74, identifier:SUPPORTED_FILES; 75, block; 75, 76; 76, if_statement; 76, 77; 76, 83; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:filename; 80, identifier:endswith; 81, argument_list; 81, 82; 82, identifier:ext; 83, block; 83, 84; 83, 93; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:wbook; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:xlrd; 90, identifier:open_workbook; 91, argument_list; 91, 92; 92, identifier:filename; 93, for_statement; 93, 94; 93, 95; 93, 100; 94, identifier:sheet; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:wbook; 98, identifier:sheets; 99, argument_list; 100, block; 100, 101; 101, if_statement; 101, 102; 101, 107; 102, comparison_operator:not; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:sheet; 105, identifier:name; 106, identifier:sessions; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:sessions; 112, identifier:append; 113, argument_list; 113, 114; 114, attribute; 114, 115; 114, 116; 115, identifier:sheet; 116, identifier:name; 117, elif_clause; 117, 118; 117, 119; 118, identifier:inputfile; 119, block; 119, 120; 119, 132; 120, if_statement; 120, 121; 120, 129; 121, call; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:os; 125, identifier:path; 126, identifier:isdir; 127, argument_list; 127, 128; 128, identifier:inputfile; 129, block; 129, 130; 130, return_statement; 130, 131; 131, identifier:sessions; 132, for_statement; 132, 133; 132, 134; 132, 135; 133, identifier:ext; 134, identifier:SUPPORTED_FILES; 135, block; 135, 136; 136, if_statement; 136, 137; 136, 143; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:inputfile; 140, identifier:endswith; 141, argument_list; 141, 142; 142, identifier:ext; 143, block; 143, 144; 143, 153; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:wbook; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:xlrd; 150, identifier:open_workbook; 151, argument_list; 151, 152; 152, identifier:inputfile; 153, for_statement; 153, 154; 153, 155; 153, 160; 154, identifier:sheet; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:wbook; 158, identifier:sheets; 159, argument_list; 160, block; 160, 161; 161, if_statement; 161, 162; 161, 167; 162, comparison_operator:not; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:sheet; 165, identifier:name; 166, identifier:sessions; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:sessions; 172, identifier:append; 173, argument_list; 173, 174; 174, attribute; 174, 175; 174, 176; 175, identifier:sheet; 176, identifier:name; 177, return_statement; 177, 178; 178, identifier:sessions | def get_session_identifiers(cls, folder=None, inputfile=None):
""" Retrieve the list of session identifiers contained in the
data on the folder or the inputfile.
For this plugin, it returns the list of excel sheet available.
:kwarg folder: the path to the folder containing the files to
check. This folder may contain sub-folders.
:kwarg inputfile: the path to the input file to use
"""
sessions = []
if inputfile and folder:
raise MQ2Exception(
'You should specify either a folder or a file')
if folder:
if not os.path.isdir(folder):
return sessions
for root, dirs, files in os.walk(folder):
for filename in files:
filename = os.path.join(root, filename)
for ext in SUPPORTED_FILES:
if filename.endswith(ext):
wbook = xlrd.open_workbook(filename)
for sheet in wbook.sheets():
if sheet.name not in sessions:
sessions.append(sheet.name)
elif inputfile:
if os.path.isdir(inputfile):
return sessions
for ext in SUPPORTED_FILES:
if inputfile.endswith(ext):
wbook = xlrd.open_workbook(inputfile)
for sheet in wbook.sheets():
if sheet.name not in sessions:
sessions.append(sheet.name)
return sessions |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:template2regex; 3, parameters; 3, 4; 3, 5; 4, identifier:template; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ranges; 7, None; 8, block; 8, 9; 8, 11; 8, 47; 8, 56; 8, 60; 8, 64; 8, 81; 8, 85; 8, 89; 8, 94; 8, 98; 8, 102; 8, 106; 8, 312; 8, 324; 8, 336; 8, 346; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 32; 12, boolean_operator:and; 12, 13; 12, 17; 13, call; 13, 14; 13, 15; 14, identifier:len; 15, argument_list; 15, 16; 16, identifier:template; 17, comparison_operator:<; 17, 18; 17, 20; 17, 26; 18, unary_operator:-; 18, 19; 19, integer:1; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:template; 23, identifier:find; 24, argument_list; 24, 25; 25, string:'|'; 26, binary_operator:-; 26, 27; 26, 31; 27, call; 27, 28; 27, 29; 28, identifier:len; 29, argument_list; 29, 30; 30, identifier:template; 31, integer:1; 32, block; 32, 33; 33, raise_statement; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:InvalidTemplateError; 36, argument_list; 36, 37; 37, binary_operator:%; 37, 38; 37, 39; 38, string:"'|' may only appear at the end, found at position %d in %s"; 39, tuple; 39, 40; 39, 46; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:template; 43, identifier:find; 44, argument_list; 44, 45; 45, string:'|'; 46, identifier:template; 47, if_statement; 47, 48; 47, 51; 48, comparison_operator:is; 48, 49; 48, 50; 49, identifier:ranges; 50, None; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:ranges; 55, identifier:DEFAULT_RANGES; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:anchor; 59, True; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:state; 63, identifier:S_PATH; 64, if_statement; 64, 65; 64, 76; 65, boolean_operator:and; 65, 66; 65, 70; 66, call; 66, 67; 66, 68; 67, identifier:len; 68, argument_list; 68, 69; 69, identifier:template; 70, comparison_operator:==; 70, 71; 70, 75; 71, subscript; 71, 72; 71, 73; 72, identifier:template; 73, unary_operator:-; 73, 74; 74, integer:1; 75, string:'|'; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:anchor; 80, False; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:params; 84, list:[]; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:bracketdepth; 88, integer:0; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:result; 92, list:['^']; 92, 93; 93, string:'^'; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:name; 97, string:""; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:pattern; 101, string:"[^/]+"; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:rangename; 105, None; 106, for_statement; 106, 107; 106, 108; 106, 114; 107, identifier:c; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:template_splitter; 111, identifier:split; 112, argument_list; 112, 113; 113, identifier:template; 114, block; 114, 115; 115, if_statement; 115, 116; 115, 119; 115, 229; 116, comparison_operator:==; 116, 117; 116, 118; 117, identifier:state; 118, identifier:S_PATH; 119, block; 119, 120; 120, if_statement; 120, 121; 120, 127; 120, 140; 120, 156; 120, 184; 120, 197; 120, 209; 120, 215; 121, comparison_operator:>; 121, 122; 121, 126; 122, call; 122, 123; 122, 124; 123, identifier:len; 124, argument_list; 124, 125; 125, identifier:c; 126, integer:1; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:result; 132, identifier:append; 133, argument_list; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:re; 137, identifier:escape; 138, argument_list; 138, 139; 139, identifier:c; 140, elif_clause; 140, 141; 140, 144; 141, comparison_operator:==; 141, 142; 141, 143; 142, identifier:c; 143, string:'['; 144, block; 144, 145; 144, 152; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:result; 149, identifier:append; 150, argument_list; 150, 151; 151, string:"("; 152, expression_statement; 152, 153; 153, augmented_assignment:+=; 153, 154; 153, 155; 154, identifier:bracketdepth; 155, integer:1; 156, elif_clause; 156, 157; 156, 160; 157, comparison_operator:==; 157, 158; 157, 159; 158, identifier:c; 159, string:']'; 160, block; 160, 161; 160, 165; 160, 177; 161, expression_statement; 161, 162; 162, augmented_assignment:-=; 162, 163; 162, 164; 163, identifier:bracketdepth; 164, integer:1; 165, if_statement; 165, 166; 165, 169; 166, comparison_operator:<; 166, 167; 166, 168; 167, identifier:bracketdepth; 168, integer:0; 169, block; 169, 170; 170, raise_statement; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:InvalidTemplateError; 173, argument_list; 173, 174; 174, binary_operator:%; 174, 175; 174, 176; 175, string:"Mismatched brackets in %s"; 176, identifier:template; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:result; 181, identifier:append; 182, argument_list; 182, 183; 183, string:")?"; 184, elif_clause; 184, 185; 184, 188; 185, comparison_operator:==; 185, 186; 185, 187; 186, identifier:c; 187, string:'{'; 188, block; 188, 189; 188, 193; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:name; 192, string:""; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:state; 196, identifier:S_TEMPLATE; 197, elif_clause; 197, 198; 197, 201; 198, comparison_operator:==; 198, 199; 198, 200; 199, identifier:c; 200, string:'}'; 201, block; 201, 202; 202, raise_statement; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:InvalidTemplateError; 205, argument_list; 205, 206; 206, binary_operator:%; 206, 207; 206, 208; 207, string:"Mismatched braces in %s"; 208, identifier:template; 209, elif_clause; 209, 210; 209, 213; 210, comparison_operator:==; 210, 211; 210, 212; 211, identifier:c; 212, string:'|'; 213, block; 213, 214; 214, pass_statement; 215, else_clause; 215, 216; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:result; 221, identifier:append; 222, argument_list; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:re; 226, identifier:escape; 227, argument_list; 227, 228; 228, identifier:c; 229, else_clause; 229, 230; 230, block; 230, 231; 231, if_statement; 231, 232; 231, 235; 231, 284; 232, comparison_operator:==; 232, 233; 232, 234; 233, identifier:c; 234, string:'}'; 235, block; 235, 236; 235, 269; 235, 276; 235, 280; 236, if_statement; 236, 237; 236, 242; 236, 256; 237, boolean_operator:and; 237, 238; 237, 239; 238, identifier:rangename; 239, comparison_operator:in; 239, 240; 239, 241; 240, identifier:rangename; 241, identifier:ranges; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:result; 247, identifier:append; 248, argument_list; 248, 249; 249, binary_operator:%; 249, 250; 249, 251; 250, string:"(?P<%s>%s)"; 251, tuple; 251, 252; 251, 253; 252, identifier:name; 253, subscript; 253, 254; 253, 255; 254, identifier:ranges; 255, identifier:rangename; 256, else_clause; 256, 257; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:result; 262, identifier:append; 263, argument_list; 263, 264; 264, binary_operator:%; 264, 265; 264, 266; 265, string:"(?P<%s>%s)"; 266, tuple; 266, 267; 266, 268; 267, identifier:name; 268, identifier:pattern; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:params; 273, identifier:append; 274, argument_list; 274, 275; 275, identifier:name; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:state; 279, identifier:S_PATH; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 283; 282, identifier:rangename; 283, None; 284, else_clause; 284, 285; 285, block; 285, 286; 285, 290; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:name; 289, identifier:c; 290, if_statement; 290, 291; 290, 300; 291, comparison_operator:>; 291, 292; 291, 298; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:name; 295, identifier:find; 296, argument_list; 296, 297; 297, string:":"; 298, unary_operator:-; 298, 299; 299, integer:1; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 306; 303, pattern_list; 303, 304; 303, 305; 304, identifier:name; 305, identifier:rangename; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:name; 309, identifier:split; 310, argument_list; 310, 311; 311, string:":"; 312, if_statement; 312, 313; 312, 316; 313, comparison_operator:!=; 313, 314; 313, 315; 314, identifier:bracketdepth; 315, integer:0; 316, block; 316, 317; 317, raise_statement; 317, 318; 318, call; 318, 319; 318, 320; 319, identifier:InvalidTemplateError; 320, argument_list; 320, 321; 321, binary_operator:%; 321, 322; 321, 323; 322, string:"Mismatched brackets in %s"; 323, identifier:template; 324, if_statement; 324, 325; 324, 328; 325, comparison_operator:==; 325, 326; 325, 327; 326, identifier:state; 327, identifier:S_TEMPLATE; 328, block; 328, 329; 329, raise_statement; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:InvalidTemplateError; 332, argument_list; 332, 333; 333, binary_operator:%; 333, 334; 333, 335; 334, string:"Mismatched braces in %s"; 335, identifier:template; 336, if_statement; 336, 337; 336, 338; 337, identifier:anchor; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:result; 343, identifier:append; 344, argument_list; 344, 345; 345, string:'$'; 346, return_statement; 346, 347; 347, expression_list; 347, 348; 347, 354; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, string:""; 351, identifier:join; 352, argument_list; 352, 353; 353, identifier:result; 354, identifier:params | def template2regex(template, ranges=None):
"""Convert a URL template to a regular expression.
Converts a template, such as /{name}/ to a regular expression, e.g.
/(?P<name>[^/]+)/ and a list of the named parameters found in the template
(e.g. ['name']). Ranges are given after a colon in a template name to
indicate a restriction on the characters that can appear there. For
example, in the template:
"/user/{id:alpha}"
The `id` must contain only characters from a-zA-Z. Other characters there
will cause the pattern not to match.
The ranges parameter is an optional dictionary that maps range names to
regular expressions. New range names can be added, or old range names can
be redefined using this parameter.
Example:
>>> import rhino.mapper
>>> rhino.mapper.template2regex("{fred}")
('^(?P<fred>[^/]+)$', ['fred'])
"""
if len(template) and -1 < template.find('|') < len(template) - 1:
raise InvalidTemplateError("'|' may only appear at the end, found at position %d in %s" % (template.find('|'), template))
if ranges is None:
ranges = DEFAULT_RANGES
anchor = True
state = S_PATH
if len(template) and template[-1] == '|':
anchor = False
params = []
bracketdepth = 0
result = ['^']
name = ""
pattern = "[^/]+"
rangename = None
for c in template_splitter.split(template):
if state == S_PATH:
if len(c) > 1:
result.append(re.escape(c))
elif c == '[':
result.append("(")
bracketdepth += 1
elif c == ']':
bracketdepth -= 1
if bracketdepth < 0:
raise InvalidTemplateError("Mismatched brackets in %s" % template)
result.append(")?")
elif c == '{':
name = ""
state = S_TEMPLATE
elif c == '}':
raise InvalidTemplateError("Mismatched braces in %s" % template)
elif c == '|':
pass
else:
result.append(re.escape(c))
else:
if c == '}':
if rangename and rangename in ranges:
result.append("(?P<%s>%s)" % (name, ranges[rangename]))
else:
result.append("(?P<%s>%s)" % (name, pattern))
params.append(name)
state = S_PATH
rangename = None
else:
name = c
if name.find(":") > -1:
name, rangename = name.split(":")
if bracketdepth != 0:
raise InvalidTemplateError("Mismatched brackets in %s" % template)
if state == S_TEMPLATE:
raise InvalidTemplateError("Mismatched braces in %s" % template)
if anchor:
result.append('$')
return "".join(result), params |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:path; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:target; 6, identifier:args; 7, identifier:kw; 8, block; 8, 9; 8, 11; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 18; 11, 97; 11, 137; 12, comparison_operator:in; 12, 13; 12, 17; 13, call; 13, 14; 13, 15; 14, identifier:type; 15, argument_list; 15, 16; 16, identifier:target; 17, identifier:string_types; 18, block; 18, 19; 19, if_statement; 19, 20; 19, 23; 19, 24; 19, 82; 20, comparison_operator:in; 20, 21; 20, 22; 21, string:':'; 22, identifier:target; 23, comment; 24, block; 24, 25; 24, 37; 24, 45; 24, 55; 24, 65; 24, 71; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 30; 27, pattern_list; 27, 28; 27, 29; 28, identifier:prefix; 29, identifier:rest; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:target; 33, identifier:split; 34, argument_list; 34, 35; 34, 36; 35, string:':'; 36, integer:1; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:route; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:named_routes; 44, identifier:prefix; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:prefix_params; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:route; 51, identifier:_pop_params; 52, argument_list; 52, 53; 52, 54; 53, identifier:args; 54, identifier:kw; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:prefix_path; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:route; 61, identifier:path; 62, argument_list; 62, 63; 62, 64; 63, list:[]; 64, identifier:prefix_params; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:next_mapper; 68, attribute; 68, 69; 68, 70; 69, identifier:route; 70, identifier:resource; 71, return_statement; 71, 72; 72, binary_operator:+; 72, 73; 72, 74; 73, identifier:prefix_path; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:next_mapper; 77, identifier:path; 78, argument_list; 78, 79; 78, 80; 78, 81; 79, identifier:rest; 80, identifier:args; 81, identifier:kw; 82, else_clause; 82, 83; 82, 84; 83, comment; 84, block; 84, 85; 85, return_statement; 85, 86; 86, call; 86, 87; 86, 94; 87, attribute; 87, 88; 87, 93; 88, subscript; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:named_routes; 92, identifier:target; 93, identifier:path; 94, argument_list; 94, 95; 94, 96; 95, identifier:args; 96, identifier:kw; 97, elif_clause; 97, 98; 97, 103; 97, 104; 98, call; 98, 99; 98, 100; 99, identifier:isinstance; 100, argument_list; 100, 101; 100, 102; 101, identifier:target; 102, identifier:Route; 103, comment; 104, block; 104, 105; 104, 124; 105, for_statement; 105, 106; 105, 107; 105, 110; 106, identifier:route; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:routes; 110, block; 110, 111; 111, if_statement; 111, 112; 111, 115; 112, comparison_operator:is; 112, 113; 112, 114; 113, identifier:route; 114, identifier:target; 115, block; 115, 116; 116, return_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:route; 120, identifier:path; 121, argument_list; 121, 122; 121, 123; 122, identifier:args; 123, identifier:kw; 124, raise_statement; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:InvalidArgumentError; 127, argument_list; 127, 128; 128, binary_operator:%; 128, 129; 128, 130; 129, string:"Route '%s' not found in this %s object."; 130, tuple; 130, 131; 130, 132; 131, identifier:target; 132, attribute; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:__class__; 136, identifier:__name__; 137, else_clause; 137, 138; 137, 139; 138, comment; 139, block; 139, 140; 139, 147; 139, 166; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:target_id; 143, call; 143, 144; 143, 145; 144, identifier:id; 145, argument_list; 145, 146; 146, identifier:target; 147, if_statement; 147, 148; 147, 153; 148, comparison_operator:in; 148, 149; 148, 150; 149, identifier:target_id; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:_lookup; 153, block; 153, 154; 154, return_statement; 154, 155; 155, call; 155, 156; 155, 163; 156, attribute; 156, 157; 156, 162; 157, subscript; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:_lookup; 161, identifier:target_id; 162, identifier:path; 163, argument_list; 163, 164; 163, 165; 164, identifier:args; 165, identifier:kw; 166, raise_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:InvalidArgumentError; 169, argument_list; 169, 170; 170, binary_operator:%; 170, 171; 170, 172; 171, string:"No Route found for target '%s' in this %s object."; 172, tuple; 172, 173; 172, 174; 173, identifier:target; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:__class__; 178, identifier:__name__ | def path(self, target, args, kw):
"""Build a URL path fragment for a resource or route.
Possible values for `target`:
A string that does not start with a '.' and does not contain ':'.
: Looks up the route of the same name on this mapper and returns it's
path.
A string of the form 'a:b', 'a:b:c', etc.
: Follows the route to nested mappers by splitting off consecutive
segments. Returns the path of the route found by looking up the
final segment on the last mapper.
A `Route` object
: Returns the path for the route.
A resource that was added previously
: Looks up the first route that points to this resource and
returns its path.
"""
if type(target) in string_types:
if ':' in target:
# Build path a nested route name
prefix, rest = target.split(':', 1)
route = self.named_routes[prefix]
prefix_params = route._pop_params(args, kw)
prefix_path = route.path([], prefix_params)
next_mapper = route.resource
return prefix_path + next_mapper.path(rest, args, kw)
else:
# Build path for a named route
return self.named_routes[target].path(args, kw)
elif isinstance(target, Route):
# Build path for a route instance, used by build_url('.')
for route in self.routes:
if route is target:
return route.path(args, kw)
raise InvalidArgumentError("Route '%s' not found in this %s object." % (target, self.__class__.__name__))
else:
# Build path for resource by object id
target_id = id(target)
if target_id in self._lookup:
return self._lookup[target_id].path(args, kw)
raise InvalidArgumentError("No Route found for target '%s' in this %s object." % (target, self.__class__.__name__)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_children_graph; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:item_ids; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:language; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:forbidden_item_ids; 13, None; 14, block; 14, 15; 14, 17; 14, 28; 14, 135; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:forbidden_item_ids; 20, None; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:forbidden_item_ids; 25, call; 25, 26; 25, 27; 26, identifier:set; 27, argument_list; 28, function_definition; 28, 29; 28, 30; 28, 32; 29, function_name:_children; 30, parameters; 30, 31; 31, identifier:item_ids; 32, block; 32, 33; 32, 94; 33, if_statement; 33, 34; 33, 37; 33, 56; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:item_ids; 36, None; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:items; 41, call; 41, 42; 41, 54; 42, attribute; 42, 43; 42, 53; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:Item; 47, identifier:objects; 48, identifier:filter; 49, argument_list; 49, 50; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:active; 52, True; 53, identifier:prefetch_related; 54, argument_list; 54, 55; 55, string:'children'; 56, else_clause; 56, 57; 57, block; 57, 58; 57, 73; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:item_ids; 61, list_comprehension; 61, 62; 61, 63; 61, 70; 62, identifier:ii; 63, for_in_clause; 63, 64; 63, 65; 64, identifier:iis; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:item_ids; 68, identifier:values; 69, argument_list; 70, for_in_clause; 70, 71; 70, 72; 71, identifier:ii; 72, identifier:iis; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:items; 76, call; 76, 77; 76, 92; 77, attribute; 77, 78; 77, 91; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:Item; 82, identifier:objects; 83, identifier:filter; 84, argument_list; 84, 85; 84, 88; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:id__in; 87, identifier:item_ids; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:active; 90, True; 91, identifier:prefetch_related; 92, argument_list; 92, 93; 93, string:'children'; 94, return_statement; 94, 95; 95, dictionary_comprehension; 95, 96; 95, 126; 95, 129; 96, pair; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:item; 99, identifier:id; 100, call; 100, 101; 100, 102; 101, identifier:sorted; 102, argument_list; 102, 103; 103, list_comprehension; 103, 104; 103, 107; 103, 116; 104, attribute; 104, 105; 104, 106; 105, identifier:_item; 106, identifier:id; 107, for_in_clause; 107, 108; 107, 109; 108, identifier:_item; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:item; 113, identifier:children; 114, identifier:all; 115, argument_list; 116, if_clause; 116, 117; 117, boolean_operator:and; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:_item; 120, identifier:active; 121, comparison_operator:not; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:_item; 124, identifier:id; 125, identifier:forbidden_item_ids; 126, for_in_clause; 126, 127; 126, 128; 127, identifier:item; 128, identifier:items; 129, if_clause; 129, 130; 130, comparison_operator:not; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:item; 133, identifier:id; 134, identifier:forbidden_item_ids; 135, if_statement; 135, 136; 135, 139; 135, 151; 136, comparison_operator:is; 136, 137; 136, 138; 137, identifier:item_ids; 138, None; 139, block; 139, 140; 140, return_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:_reachable_graph; 145, argument_list; 145, 146; 145, 147; 145, 148; 146, None; 147, identifier:_children; 148, keyword_argument; 148, 149; 148, 150; 149, identifier:language; 150, identifier:language; 151, else_clause; 151, 152; 152, block; 152, 153; 152, 166; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:graph; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:get_children_graph; 160, argument_list; 160, 161; 160, 162; 160, 163; 161, None; 162, identifier:language; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:forbidden_item_ids; 165, identifier:forbidden_item_ids; 166, return_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:_subset_graph; 171, argument_list; 171, 172; 171, 173; 172, identifier:graph; 173, binary_operator:-; 173, 174; 173, 178; 174, call; 174, 175; 174, 176; 175, identifier:set; 176, argument_list; 176, 177; 177, identifier:item_ids; 178, call; 178, 179; 178, 180; 179, identifier:set; 180, argument_list; 180, 181; 181, identifier:forbidden_item_ids | def get_children_graph(self, item_ids=None, language=None, forbidden_item_ids=None):
"""
Get a subgraph of items reachable from the given set of items through
the 'child' relation.
Args:
item_ids (list): items which are taken as roots for the reachability
language (str): if specified, filter out items which are not
available in the given language
Returns:
dict: item id -> list of items (child items), root items are
referenced by None key
"""
if forbidden_item_ids is None:
forbidden_item_ids = set()
def _children(item_ids):
if item_ids is None:
items = Item.objects.filter(active=True).prefetch_related('children')
else:
item_ids = [ii for iis in item_ids.values() for ii in iis]
items = Item.objects.filter(id__in=item_ids, active=True).prefetch_related('children')
return {
item.id: sorted([
_item.id for _item in item.children.all()
if _item.active and _item.id not in forbidden_item_ids
])
for item in items if item.id not in forbidden_item_ids
}
if item_ids is None:
return self._reachable_graph(None, _children, language=language)
else:
graph = self.get_children_graph(None, language, forbidden_item_ids=forbidden_item_ids)
return self._subset_graph(graph, set(item_ids) - set(forbidden_item_ids)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_parents_graph; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:item_ids; 6, default_parameter; 6, 7; 6, 8; 7, identifier:language; 8, None; 9, block; 9, 10; 9, 12; 9, 103; 9, 114; 10, expression_statement; 10, 11; 11, comment; 12, function_definition; 12, 13; 12, 14; 12, 16; 13, function_name:_parents; 14, parameters; 14, 15; 15, identifier:item_ids; 16, block; 16, 17; 16, 78; 17, if_statement; 17, 18; 17, 21; 17, 40; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:item_ids; 20, None; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:items; 25, call; 25, 26; 25, 38; 26, attribute; 26, 27; 26, 37; 27, call; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:Item; 31, identifier:objects; 32, identifier:filter; 33, argument_list; 33, 34; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:active; 36, True; 37, identifier:prefetch_related; 38, argument_list; 38, 39; 39, string:'parents'; 40, else_clause; 40, 41; 41, block; 41, 42; 41, 57; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:item_ids; 45, list_comprehension; 45, 46; 45, 47; 45, 54; 46, identifier:ii; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:iis; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:item_ids; 52, identifier:values; 53, argument_list; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:ii; 56, identifier:iis; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:items; 60, call; 60, 61; 60, 76; 61, attribute; 61, 62; 61, 75; 62, call; 62, 63; 62, 68; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:Item; 66, identifier:objects; 67, identifier:filter; 68, argument_list; 68, 69; 68, 72; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:id__in; 71, identifier:item_ids; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:active; 74, True; 75, identifier:prefetch_related; 76, argument_list; 76, 77; 77, string:'parents'; 78, return_statement; 78, 79; 79, dictionary_comprehension; 79, 80; 79, 100; 80, pair; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:item; 83, identifier:id; 84, call; 84, 85; 84, 86; 85, identifier:sorted; 86, argument_list; 86, 87; 87, list_comprehension; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:_item; 90, identifier:id; 91, for_in_clause; 91, 92; 91, 93; 92, identifier:_item; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:item; 97, identifier:parents; 98, identifier:all; 99, argument_list; 100, for_in_clause; 100, 101; 100, 102; 101, identifier:item; 102, identifier:items; 103, return_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:_reachable_graph; 108, argument_list; 108, 109; 108, 110; 108, 111; 109, identifier:item_ids; 110, identifier:_parents; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:language; 113, identifier:language; 114, if_statement; 114, 115; 114, 118; 114, 130; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:item_ids; 117, None; 118, block; 118, 119; 119, return_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:_reachable_graph; 124, argument_list; 124, 125; 124, 126; 124, 127; 125, None; 126, identifier:_parents; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:language; 129, identifier:language; 130, else_clause; 130, 131; 131, block; 131, 132; 131, 142; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:graph; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:get_parents_graph; 139, argument_list; 139, 140; 139, 141; 140, None; 141, identifier:language; 142, return_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:_subset_graph; 147, argument_list; 147, 148; 147, 149; 148, identifier:graph; 149, identifier:item_ids | def get_parents_graph(self, item_ids, language=None):
"""
Get a subgraph of items reachable from the given set of items through
the 'parent' relation.
Args:
item_ids (list): items which are taken as roots for the reachability
language (str): if specified, filter out items which are not
available in the given language
Returns:
dict: item id -> list of items (parent items), root items are
referenced by None key
"""
def _parents(item_ids):
if item_ids is None:
items = Item.objects.filter(active=True).prefetch_related('parents')
else:
item_ids = [ii for iis in item_ids.values() for ii in iis]
items = Item.objects.filter(id__in=item_ids, active=True).prefetch_related('parents')
return {item.id: sorted([_item.id for _item in item.parents.all()]) for item in items}
return self._reachable_graph(item_ids, _parents, language=language)
if item_ids is None:
return self._reachable_graph(None, _parents, language=language)
else:
graph = self.get_parents_graph(None, language)
return self._subset_graph(graph, item_ids) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_graph; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:item_ids; 6, default_parameter; 6, 7; 6, 8; 7, identifier:language; 8, None; 9, block; 9, 10; 9, 12; 9, 116; 10, expression_statement; 10, 11; 11, comment; 12, function_definition; 12, 13; 12, 14; 12, 16; 13, function_name:_related; 14, parameters; 14, 15; 15, identifier:item_ids; 16, block; 16, 17; 16, 80; 17, if_statement; 17, 18; 17, 21; 17, 41; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:item_ids; 20, None; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:items; 25, call; 25, 26; 25, 38; 26, attribute; 26, 27; 26, 37; 27, call; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:Item; 31, identifier:objects; 32, identifier:filter; 33, argument_list; 33, 34; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:active; 36, True; 37, identifier:prefetch_related; 38, argument_list; 38, 39; 38, 40; 39, string:'parents'; 40, string:'children'; 41, else_clause; 41, 42; 42, block; 42, 43; 42, 58; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:item_ids; 46, list_comprehension; 46, 47; 46, 48; 46, 55; 47, identifier:ii; 48, for_in_clause; 48, 49; 48, 50; 49, identifier:iis; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:item_ids; 53, identifier:values; 54, argument_list; 55, for_in_clause; 55, 56; 55, 57; 56, identifier:ii; 57, identifier:iis; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:items; 61, call; 61, 62; 61, 77; 62, attribute; 62, 63; 62, 76; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:Item; 67, identifier:objects; 68, identifier:filter; 69, argument_list; 69, 70; 69, 73; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:id__in; 72, identifier:item_ids; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:active; 75, True; 76, identifier:prefetch_related; 77, argument_list; 77, 78; 77, 79; 78, string:'parents'; 79, string:'children'; 80, return_statement; 80, 81; 81, dictionary_comprehension; 81, 82; 81, 113; 82, pair; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:item; 85, identifier:id; 86, call; 86, 87; 86, 88; 87, identifier:sorted; 88, argument_list; 88, 89; 89, list_comprehension; 89, 90; 89, 93; 89, 110; 90, attribute; 90, 91; 90, 92; 91, identifier:_item; 92, identifier:id; 93, for_in_clause; 93, 94; 93, 95; 94, identifier:rel; 95, list:[item.parents.all(), item.children.all()]; 95, 96; 95, 103; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:item; 100, identifier:parents; 101, identifier:all; 102, argument_list; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:item; 107, identifier:children; 108, identifier:all; 109, argument_list; 110, for_in_clause; 110, 111; 110, 112; 111, identifier:_item; 112, identifier:rel; 113, for_in_clause; 113, 114; 113, 115; 114, identifier:item; 115, identifier:items; 116, if_statement; 116, 117; 116, 120; 116, 132; 117, comparison_operator:is; 117, 118; 117, 119; 118, identifier:item_ids; 119, None; 120, block; 120, 121; 121, return_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:_reachable_graph; 126, argument_list; 126, 127; 126, 128; 126, 129; 127, None; 128, identifier:_related; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:language; 131, identifier:language; 132, else_clause; 132, 133; 133, block; 133, 134; 133, 144; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:graph; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:get_graph; 141, argument_list; 141, 142; 141, 143; 142, None; 143, identifier:language; 144, return_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:_subset_graph; 149, argument_list; 149, 150; 149, 151; 150, identifier:graph; 151, identifier:item_ids | def get_graph(self, item_ids, language=None):
"""
Get a subgraph of items reachable from the given set of items through
any relation.
Args:
item_ids (list): items which are taken as roots for the reachability
language (str): if specified, filter out items which are not
available in the given language
Returns:
dict: item id -> list of items (parent items), root items are
referenced by None key
"""
def _related(item_ids):
if item_ids is None:
items = Item.objects.filter(active=True).prefetch_related('parents', 'children')
else:
item_ids = [ii for iis in item_ids.values() for ii in iis]
items = Item.objects.filter(id__in=item_ids, active=True).prefetch_related('parents', 'children')
return {item.id: sorted([_item.id for rel in [item.parents.all(), item.children.all()] for _item in rel]) for item in items}
if item_ids is None:
return self._reachable_graph(None, _related, language=language)
else:
graph = self.get_graph(None, language)
return self._subset_graph(graph, item_ids) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:translate_item_ids; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:item_ids; 6, identifier:language; 7, default_parameter; 7, 8; 7, 9; 8, identifier:is_nested; 9, None; 10, block; 10, 11; 10, 13; 10, 45; 10, 55; 10, 74; 10, 78; 10, 246; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 17; 13, 25; 13, 39; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:is_nested; 16, None; 17, block; 17, 18; 18, function_definition; 18, 19; 18, 20; 18, 22; 19, function_name:is_nested_fun; 20, parameters; 20, 21; 21, identifier:x; 22, block; 22, 23; 23, return_statement; 23, 24; 24, True; 25, elif_clause; 25, 26; 25, 31; 26, call; 26, 27; 26, 28; 27, identifier:isinstance; 28, argument_list; 28, 29; 28, 30; 29, identifier:is_nested; 30, identifier:bool; 31, block; 31, 32; 32, function_definition; 32, 33; 32, 34; 32, 36; 33, function_name:is_nested_fun; 34, parameters; 34, 35; 35, identifier:x; 36, block; 36, 37; 37, return_statement; 37, 38; 38, identifier:is_nested; 39, else_clause; 39, 40; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:is_nested_fun; 44, identifier:is_nested; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:all_item_type_ids; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:ItemType; 52, identifier:objects; 53, identifier:get_all_item_type_ids; 54, argument_list; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:groupped; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:proso; 62, identifier:list; 63, identifier:group_by; 64, argument_list; 64, 65; 64, 66; 65, identifier:item_ids; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:by; 68, lambda; 68, 69; 68, 71; 69, lambda_parameters; 69, 70; 70, identifier:item_id; 71, subscript; 71, 72; 71, 73; 72, identifier:all_item_type_ids; 73, identifier:item_id; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:result; 77, dictionary; 78, for_statement; 78, 79; 78, 82; 78, 87; 79, pattern_list; 79, 80; 79, 81; 80, identifier:item_type_id; 81, identifier:items; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:groupped; 85, identifier:items; 86, argument_list; 87, block; 87, 88; 88, with_statement; 88, 89; 88, 100; 89, with_clause; 89, 90; 90, with_item; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:timeit; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, string:'translating item type {}'; 97, identifier:format; 98, argument_list; 98, 99; 99, identifier:item_type_id; 100, block; 100, 101; 100, 113; 100, 124; 100, 138; 100, 151; 100, 210; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:item_type; 104, subscript; 104, 105; 104, 112; 105, call; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:ItemType; 109, identifier:objects; 110, identifier:get_all_types; 111, argument_list; 112, identifier:item_type_id; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:model; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:ItemType; 120, identifier:objects; 121, identifier:get_model; 122, argument_list; 122, 123; 123, identifier:item_type_id; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:kwargs; 127, dictionary; 127, 128; 128, pair; 128, 129; 128, 137; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, string:'{}__in'; 132, identifier:format; 133, argument_list; 133, 134; 134, subscript; 134, 135; 134, 136; 135, identifier:item_type; 136, string:'foreign_key'; 137, identifier:items; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:in; 139, 140; 139, 141; 140, string:'language'; 141, identifier:item_type; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 150; 145, subscript; 145, 146; 145, 147; 146, identifier:kwargs; 147, subscript; 147, 148; 147, 149; 148, identifier:item_type; 149, string:'language'; 150, identifier:language; 151, if_statement; 151, 152; 151, 172; 151, 183; 151, 202; 152, boolean_operator:and; 152, 153; 152, 165; 153, call; 153, 154; 153, 155; 154, identifier:any; 155, argument_list; 155, 156; 156, list_comprehension; 156, 157; 156, 162; 157, not_operator; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:is_nested_fun; 160, argument_list; 160, 161; 161, identifier:item_id; 162, for_in_clause; 162, 163; 162, 164; 163, identifier:item_id; 164, identifier:items; 165, call; 165, 166; 165, 167; 166, identifier:hasattr; 167, argument_list; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:model; 170, identifier:objects; 171, string:'prepare_related'; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:objs; 176, call; 176, 177; 176, 182; 177, attribute; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:model; 180, identifier:objects; 181, identifier:prepare_related; 182, argument_list; 183, elif_clause; 183, 184; 183, 191; 184, call; 184, 185; 184, 186; 185, identifier:hasattr; 186, argument_list; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:model; 189, identifier:objects; 190, string:'prepare'; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:objs; 195, call; 195, 196; 195, 201; 196, attribute; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:model; 199, identifier:objects; 200, identifier:prepare; 201, argument_list; 202, else_clause; 202, 203; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:objs; 207, attribute; 207, 208; 207, 209; 208, identifier:model; 209, identifier:objects; 210, for_statement; 210, 211; 210, 212; 210, 219; 211, identifier:obj; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:objs; 215, identifier:filter; 216, argument_list; 216, 217; 217, dictionary_splat; 217, 218; 218, identifier:kwargs; 219, block; 219, 220; 219, 230; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:item_id; 223, call; 223, 224; 223, 225; 224, identifier:getattr; 225, argument_list; 225, 226; 225, 227; 226, identifier:obj; 227, subscript; 227, 228; 227, 229; 228, identifier:item_type; 229, string:'foreign_key'; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:result; 234, identifier:item_id; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:obj; 238, identifier:to_json; 239, argument_list; 239, 240; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:nested; 242, call; 242, 243; 242, 244; 243, identifier:is_nested_fun; 244, argument_list; 244, 245; 245, identifier:item_id; 246, return_statement; 246, 247; 247, identifier:result | def translate_item_ids(self, item_ids, language, is_nested=None):
"""
Translate a list of item ids to JSON objects which reference them.
Args:
item_ids (list[int]): item ids
language (str): language used for further filtering (some objects
for different languages share the same item)
is_nested (function): mapping from item ids to booleans, where the
boolean value indicates whether the item is nested
Returns:
dict: item id -> JSON object
"""
if is_nested is None:
def is_nested_fun(x):
return True
elif isinstance(is_nested, bool):
def is_nested_fun(x):
return is_nested
else:
is_nested_fun = is_nested
all_item_type_ids = ItemType.objects.get_all_item_type_ids()
groupped = proso.list.group_by(item_ids, by=lambda item_id: all_item_type_ids[item_id])
result = {}
for item_type_id, items in groupped.items():
with timeit('translating item type {}'.format(item_type_id)):
item_type = ItemType.objects.get_all_types()[item_type_id]
model = ItemType.objects.get_model(item_type_id)
kwargs = {'{}__in'.format(item_type['foreign_key']): items}
if 'language' in item_type:
kwargs[item_type['language']] = language
if any([not is_nested_fun(item_id) for item_id in items]) and hasattr(model.objects, 'prepare_related'):
objs = model.objects.prepare_related()
elif hasattr(model.objects, 'prepare'):
objs = model.objects.prepare()
else:
objs = model.objects
for obj in objs.filter(**kwargs):
item_id = getattr(obj, item_type['foreign_key'])
result[item_id] = obj.to_json(nested=is_nested_fun(item_id))
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_leaves; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:item_ids; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:language; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:forbidden_item_ids; 13, None; 14, block; 14, 15; 14, 17; 14, 31; 14, 46; 14, 57; 14, 74; 14, 219; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:forbidden_item_ids; 20, conditional_expression:if; 20, 21; 20, 24; 20, 27; 21, call; 21, 22; 21, 23; 22, identifier:set; 23, argument_list; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:forbidden_item_ids; 26, None; 27, call; 27, 28; 27, 29; 28, identifier:set; 29, argument_list; 29, 30; 30, identifier:forbidden_item_ids; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:children; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:get_children_graph; 38, argument_list; 38, 39; 38, 40; 38, 43; 39, identifier:item_ids; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:language; 42, identifier:language; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:forbidden_item_ids; 45, identifier:forbidden_item_ids; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:counts; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:get_children_counts; 53, argument_list; 53, 54; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:active; 56, None; 57, if_statement; 57, 58; 57, 61; 57, 62; 58, comparison_operator:is; 58, 59; 58, 60; 59, identifier:item_ids; 60, None; 61, comment; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:item_ids; 66, call; 66, 67; 66, 68; 67, identifier:set; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:children; 72, identifier:keys; 73, argument_list; 74, function_definition; 74, 75; 74, 76; 74, 78; 75, function_name:_get_leaves; 76, parameters; 76, 77; 77, identifier:item_id; 78, block; 78, 79; 78, 85; 78, 137; 78, 177; 78, 191; 78, 201; 78, 215; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:leaves; 82, call; 82, 83; 82, 84; 83, identifier:set; 84, argument_list; 85, function_definition; 85, 86; 85, 87; 85, 89; 86, function_name:__search; 87, parameters; 87, 88; 88, identifier:item_ids; 89, block; 89, 90; 89, 110; 89, 126; 89, 133; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:result; 93, call; 93, 94; 93, 95; 94, identifier:set; 95, argument_list; 95, 96; 96, call; 96, 97; 96, 98; 97, identifier:flatten; 98, argument_list; 98, 99; 99, list_comprehension; 99, 100; 99, 107; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:children; 103, identifier:get; 104, argument_list; 104, 105; 104, 106; 105, identifier:item_id; 106, list:[]; 107, for_in_clause; 107, 108; 107, 109; 108, identifier:item_id; 109, identifier:item_ids; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:new_leaves; 113, set_comprehension; 113, 114; 113, 115; 113, 118; 114, identifier:item_id; 115, for_in_clause; 115, 116; 115, 117; 116, identifier:item_id; 117, identifier:result; 118, if_clause; 118, 119; 119, comparison_operator:not; 119, 120; 119, 121; 120, identifier:item_id; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:children; 124, identifier:keys; 125, argument_list; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:leaves; 130, identifier:update; 131, argument_list; 131, 132; 132, identifier:new_leaves; 133, return_statement; 133, 134; 134, binary_operator:-; 134, 135; 134, 136; 135, identifier:result; 136, identifier:new_leaves; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:fixed_point; 140, argument_list; 140, 141; 140, 152; 140, 161; 140, 170; 140, 173; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:is_zero; 143, lambda; 143, 144; 143, 146; 144, lambda_parameters; 144, 145; 145, identifier:to_visit; 146, comparison_operator:==; 146, 147; 146, 151; 147, call; 147, 148; 147, 149; 148, identifier:len; 149, argument_list; 149, 150; 150, identifier:to_visit; 151, integer:0; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:minus; 154, lambda; 154, 155; 154, 158; 155, lambda_parameters; 155, 156; 155, 157; 156, identifier:to_visit; 157, identifier:visited; 158, binary_operator:-; 158, 159; 158, 160; 159, identifier:to_visit; 160, identifier:visited; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:plus; 163, lambda; 163, 164; 163, 167; 164, lambda_parameters; 164, 165; 164, 166; 165, identifier:visited_x; 166, identifier:visited_y; 167, binary_operator:|; 167, 168; 167, 169; 168, identifier:visited_x; 169, identifier:visited_y; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:f; 172, identifier:__search; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:x; 175, set; 175, 176; 176, identifier:item_id; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:leaves; 180, set_comprehension; 180, 181; 180, 182; 180, 185; 181, identifier:leaf; 182, for_in_clause; 182, 183; 182, 184; 183, identifier:leaf; 184, identifier:leaves; 185, if_clause; 185, 186; 186, comparison_operator:==; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:counts; 189, identifier:leaf; 190, integer:0; 191, if_statement; 191, 192; 191, 198; 192, comparison_operator:>; 192, 193; 192, 197; 193, call; 193, 194; 193, 195; 194, identifier:len; 195, argument_list; 195, 196; 196, identifier:leaves; 197, integer:0; 198, block; 198, 199; 199, return_statement; 199, 200; 200, identifier:leaves; 201, if_statement; 201, 202; 201, 211; 202, boolean_operator:and; 202, 203; 202, 208; 203, comparison_operator:==; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:counts; 206, identifier:item_id; 207, integer:0; 208, comparison_operator:not; 208, 209; 208, 210; 209, identifier:item_id; 210, identifier:forbidden_item_ids; 211, block; 211, 212; 212, return_statement; 212, 213; 213, set; 213, 214; 214, identifier:item_id; 215, return_statement; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:set; 218, argument_list; 219, return_statement; 219, 220; 220, dictionary_comprehension; 220, 221; 220, 227; 221, pair; 221, 222; 221, 223; 222, identifier:item_id; 223, call; 223, 224; 223, 225; 224, identifier:_get_leaves; 225, argument_list; 225, 226; 226, identifier:item_id; 227, for_in_clause; 227, 228; 227, 229; 228, identifier:item_id; 229, identifier:item_ids | def get_leaves(self, item_ids=None, language=None, forbidden_item_ids=None):
"""
Get mapping of items to their reachable leaves. Leaves having
inactive relations to other items are omitted.
Args:
item_ids (list): items which are taken as roots for the reachability
language (str): if specified, filter out items which are not
available in the given language
Returns:
dict: item id -> list of items (reachable leaves)
"""
forbidden_item_ids = set() if forbidden_item_ids is None else set(forbidden_item_ids)
children = self.get_children_graph(item_ids, language=language, forbidden_item_ids=forbidden_item_ids)
counts = self.get_children_counts(active=None)
if item_ids is None:
# not leaves
item_ids = set(children.keys())
def _get_leaves(item_id):
leaves = set()
def __search(item_ids):
result = set(flatten([children.get(item_id, []) for item_id in item_ids]))
new_leaves = {item_id for item_id in result if item_id not in children.keys()}
leaves.update(new_leaves)
return result - new_leaves
fixed_point(
is_zero=lambda to_visit: len(to_visit) == 0,
minus=lambda to_visit, visited: to_visit - visited,
plus=lambda visited_x, visited_y: visited_x | visited_y,
f=__search,
x={item_id}
)
leaves = {leaf for leaf in leaves if counts[leaf] == 0}
if len(leaves) > 0:
return leaves
if counts[item_id] == 0 and item_id not in forbidden_item_ids:
return {item_id}
return set()
return {item_id: _get_leaves(item_id) for item_id in item_ids} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:get_image; 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:imgtype; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:tags; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:nsfw; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:hidden; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:filetype; 19, None; 20, block; 20, 21; 20, 23; 20, 35; 20, 50; 20, 65; 20, 80; 20, 106; 20, 121; 20, 155; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 29; 24, boolean_operator:and; 24, 25; 24, 27; 25, not_operator; 25, 26; 26, identifier:imgtype; 27, not_operator; 27, 28; 28, identifier:tags; 29, block; 29, 30; 30, raise_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:MissingTypeOrTags; 33, argument_list; 33, 34; 34, string:"'get_image' requires at least one of either type or tags."; 35, if_statement; 35, 36; 35, 44; 36, boolean_operator:and; 36, 37; 36, 38; 37, identifier:imgtype; 38, not_operator; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:isinstance; 41, argument_list; 41, 42; 41, 43; 42, identifier:imgtype; 43, identifier:str; 44, block; 44, 45; 45, raise_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:TypeError; 48, argument_list; 48, 49; 49, string:"type of 'imgtype' must be str."; 50, if_statement; 50, 51; 50, 59; 51, boolean_operator:and; 51, 52; 51, 53; 52, identifier:tags; 53, not_operator; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 58; 57, identifier:tags; 58, identifier:list; 59, block; 59, 60; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:TypeError; 63, argument_list; 63, 64; 64, string:"type of 'tags' must be list or None."; 65, if_statement; 65, 66; 65, 74; 66, boolean_operator:and; 66, 67; 66, 68; 67, identifier:hidden; 68, not_operator; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:isinstance; 71, argument_list; 71, 72; 71, 73; 72, identifier:hidden; 73, identifier:bool; 74, block; 74, 75; 75, raise_statement; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:TypeError; 78, argument_list; 78, 79; 79, string:"type of 'hidden' must be bool or None."; 80, if_statement; 80, 81; 80, 100; 81, boolean_operator:and; 81, 82; 81, 90; 82, boolean_operator:and; 82, 83; 82, 84; 83, identifier:nsfw; 84, not_operator; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:isinstance; 87, argument_list; 87, 88; 87, 89; 88, identifier:nsfw; 89, identifier:bool; 90, parenthesized_expression; 90, 91; 91, boolean_operator:and; 91, 92; 91, 97; 92, call; 92, 93; 92, 94; 93, identifier:isinstance; 94, argument_list; 94, 95; 94, 96; 95, identifier:nsfw; 96, identifier:str; 97, comparison_operator:==; 97, 98; 97, 99; 98, identifier:nsfw; 99, string:'only'; 100, block; 100, 101; 101, raise_statement; 101, 102; 102, call; 102, 103; 102, 104; 103, identifier:TypeError; 104, argument_list; 104, 105; 105, string:"type of 'nsfw' must be str, bool or None."; 106, if_statement; 106, 107; 106, 115; 107, boolean_operator:and; 107, 108; 107, 109; 108, identifier:filetype; 109, not_operator; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:isinstance; 112, argument_list; 112, 113; 112, 114; 113, identifier:filetype; 114, identifier:str; 115, block; 115, 116; 116, raise_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:TypeError; 119, argument_list; 119, 120; 120, string:"type of 'filetype' must be str."; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:url; 124, binary_operator:+; 124, 125; 124, 150; 125, binary_operator:+; 125, 126; 125, 145; 126, binary_operator:+; 126, 127; 126, 140; 127, binary_operator:+; 127, 128; 127, 135; 128, binary_operator:+; 128, 129; 128, 130; 129, string:'https://api.weeb.sh/images/random'; 130, parenthesized_expression; 130, 131; 131, conditional_expression:if; 131, 132; 131, 133; 131, 134; 132, string:f'?type={imgtype}'; 133, identifier:imgtype; 134, string:''; 135, parenthesized_expression; 135, 136; 136, conditional_expression:if; 136, 137; 136, 138; 136, 139; 137, string:f'{"?" if not imgtype else "&"}tags={",".join(tags)}'; 138, identifier:tags; 139, string:''; 140, parenthesized_expression; 140, 141; 141, conditional_expression:if; 141, 142; 141, 143; 141, 144; 142, string:f'&nsfw={nsfw.lower()}'; 143, identifier:nsfw; 144, string:''; 145, parenthesized_expression; 145, 146; 146, conditional_expression:if; 146, 147; 146, 148; 146, 149; 147, string:f'&hidden={hidden}'; 148, identifier:hidden; 149, string:''; 150, parenthesized_expression; 150, 151; 151, conditional_expression:if; 151, 152; 151, 153; 151, 154; 152, string:f'&filetype={filetype}'; 153, identifier:filetype; 154, string:''; 155, with_statement; 155, 156; 155, 166; 156, with_clause; 156, 157; 157, with_item; 157, 158; 158, as_pattern; 158, 159; 158, 164; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:aiohttp; 162, identifier:ClientSession; 163, argument_list; 164, as_pattern_target; 164, 165; 165, identifier:session; 166, block; 166, 167; 167, with_statement; 167, 168; 167, 184; 168, with_clause; 168, 169; 169, with_item; 169, 170; 170, as_pattern; 170, 171; 170, 182; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:session; 174, identifier:get; 175, argument_list; 175, 176; 175, 177; 176, identifier:url; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:headers; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:__headers; 182, as_pattern_target; 182, 183; 183, identifier:resp; 184, block; 184, 185; 185, if_statement; 185, 186; 185, 191; 185, 212; 186, comparison_operator:==; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:resp; 189, identifier:status; 190, integer:200; 191, block; 191, 192; 191, 201; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:js; 195, await; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:resp; 199, identifier:json; 200, argument_list; 201, return_statement; 201, 202; 202, list:[js['url'], js['id'], js['fileType']]; 202, 203; 202, 206; 202, 209; 203, subscript; 203, 204; 203, 205; 204, identifier:js; 205, string:'url'; 206, subscript; 206, 207; 206, 208; 207, identifier:js; 208, string:'id'; 209, subscript; 209, 210; 209, 211; 210, identifier:js; 211, string:'fileType'; 212, else_clause; 212, 213; 213, block; 213, 214; 214, raise_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:Exception; 217, argument_list; 217, 218; 218, subscript; 218, 219; 218, 226; 219, parenthesized_expression; 219, 220; 220, await; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:resp; 224, identifier:json; 225, argument_list; 226, string:'message' | async def get_image(self, imgtype=None, tags=None, nsfw=None, hidden=None, filetype=None):
"""Request an image from weeb.sh.
This function is a coroutine.
Parameters:
imgtype: str - the type of image to get. (If not specified, needs at least one tag)
tags: list - the tags to search by. (If not specified, needs type)
nsfw: str - whether or not the images recieved are nsfw. (Optional)
hidden: bool - whether you only get public images or hidden images uploaded by yourself. (If not specified, both are supplied)
filetype: str - the file type to get. Supported are jpg,jpeg,png,gif. (If not specified, all filetypes are grabbed)
Return Type: `list` (returns as [url, id, filetype])"""
if not imgtype and not tags:
raise MissingTypeOrTags("'get_image' requires at least one of either type or tags.")
if imgtype and not isinstance(imgtype, str):
raise TypeError("type of 'imgtype' must be str.")
if tags and not isinstance(tags, list):
raise TypeError("type of 'tags' must be list or None.")
if hidden and not isinstance(hidden, bool):
raise TypeError("type of 'hidden' must be bool or None.")
if nsfw and not isinstance(nsfw, bool) and (isinstance(nsfw, str) and nsfw == 'only'):
raise TypeError("type of 'nsfw' must be str, bool or None.")
if filetype and not isinstance(filetype, str):
raise TypeError("type of 'filetype' must be str.")
url = 'https://api.weeb.sh/images/random' + (f'?type={imgtype}' if imgtype else '') + (
f'{"?" if not imgtype else "&"}tags={",".join(tags)}' if tags else '') + (
f'&nsfw={nsfw.lower()}' if nsfw else '') + (f'&hidden={hidden}' if hidden else '') + (
f'&filetype={filetype}' if filetype else '')
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=self.__headers) as resp:
if resp.status == 200:
js = await resp.json()
return [js['url'], js['id'], js['fileType']]
else:
raise Exception((await resp.json())['message']) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:query; 3, parameters; 3, 4; 3, 5; 4, identifier:logfile; 5, default_parameter; 5, 6; 5, 7; 6, identifier:jobID; 7, None; 8, block; 8, 9; 8, 11; 8, 19; 8, 49; 8, 72; 8, 78; 8, 84; 8, 85; 8, 89; 8, 117; 8, 132; 8, 145; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:joblist; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:logfile; 17, identifier:readFromLogfile; 18, argument_list; 19, if_statement; 19, 20; 19, 31; 19, 42; 20, boolean_operator:and; 20, 21; 20, 22; 21, identifier:jobID; 22, comparison_operator:==; 22, 23; 22, 27; 23, call; 23, 24; 23, 25; 24, identifier:type; 25, argument_list; 25, 26; 26, identifier:jobID; 27, call; 27, 28; 27, 29; 28, identifier:type; 29, argument_list; 29, 30; 30, integer:1; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:command; 35, list:['qstat', '-j', str(jobID)]; 35, 36; 35, 37; 35, 38; 36, string:'qstat'; 37, string:'-j'; 38, call; 38, 39; 38, 40; 39, identifier:str; 40, argument_list; 40, 41; 41, identifier:jobID; 42, else_clause; 42, 43; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:command; 47, list:['qstat']; 47, 48; 48, string:'qstat'; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:processoutput; 52, call; 52, 53; 52, 71; 53, attribute; 53, 54; 53, 70; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:subprocess; 57, identifier:Popen; 58, argument_list; 58, 59; 58, 60; 58, 65; 59, identifier:command; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:stdout; 62, attribute; 62, 63; 62, 64; 63, identifier:subprocess; 64, identifier:PIPE; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:stderr; 67, attribute; 67, 68; 67, 69; 68, identifier:subprocess; 69, identifier:PIPE; 70, identifier:communicate; 71, argument_list; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:output; 75, subscript; 75, 76; 75, 77; 76, identifier:processoutput; 77, integer:0; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:serror; 81, subscript; 81, 82; 81, 83; 82, identifier:processoutput; 83, integer:1; 84, comment; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:jobs; 88, dictionary; 89, if_statement; 89, 90; 89, 99; 90, comparison_operator:==; 90, 91; 90, 95; 91, call; 91, 92; 91, 93; 92, identifier:type; 93, argument_list; 93, 94; 94, identifier:jobID; 95, call; 95, 96; 95, 97; 96, identifier:type; 97, argument_list; 97, 98; 98, integer:1; 99, block; 99, 100; 100, if_statement; 100, 101; 100, 110; 100, 113; 101, comparison_operator:!=; 101, 102; 101, 108; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:serror; 105, identifier:find; 106, argument_list; 106, 107; 107, string:"Following jobs do not exist"; 108, unary_operator:-; 108, 109; 109, integer:1; 110, block; 110, 111; 111, return_statement; 111, 112; 112, False; 113, else_clause; 113, 114; 114, block; 114, 115; 115, return_statement; 115, 116; 116, True; 117, if_statement; 117, 118; 117, 124; 118, not_operator; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:output; 122, identifier:strip; 123, argument_list; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:colorprinter; 129, identifier:message; 130, argument_list; 130, 131; 131, string:"No jobs running at present."; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:output; 135, call; 135, 136; 135, 143; 136, attribute; 136, 137; 136, 142; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:output; 140, identifier:strip; 141, argument_list; 142, identifier:split; 143, argument_list; 143, 144; 144, string:"\n"; 145, if_statement; 145, 146; 145, 152; 146, comparison_operator:>; 146, 147; 146, 151; 147, call; 147, 148; 147, 149; 148, identifier:len; 149, argument_list; 149, 150; 150, identifier:output; 151, integer:2; 152, block; 152, 153; 152, 378; 153, for_statement; 153, 154; 153, 155; 153, 160; 153, 161; 154, identifier:line; 155, subscript; 155, 156; 155, 157; 156, identifier:output; 157, slice; 157, 158; 157, 159; 158, integer:2; 159, colon; 160, comment; 161, block; 161, 162; 161, 170; 161, 179; 161, 185; 161, 221; 161, 225; 161, 279; 161, 299; 161, 312; 161, 320; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:tokens; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:line; 168, identifier:split; 169, argument_list; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:jid; 173, call; 173, 174; 173, 175; 174, identifier:int; 175, argument_list; 175, 176; 176, subscript; 176, 177; 176, 178; 177, identifier:tokens; 178, integer:0; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:jobstate; 182, subscript; 182, 183; 182, 184; 183, identifier:tokens; 184, integer:4; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:details; 188, dictionary; 188, 189; 188, 192; 188, 197; 188, 202; 188, 207; 188, 210; 189, pair; 189, 190; 189, 191; 190, string:"jobid"; 191, identifier:jid; 192, pair; 192, 193; 192, 194; 193, string:"prior"; 194, subscript; 194, 195; 194, 196; 195, identifier:tokens; 196, integer:1; 197, pair; 197, 198; 197, 199; 198, string:"name"; 199, subscript; 199, 200; 199, 201; 200, identifier:tokens; 201, integer:2; 202, pair; 202, 203; 202, 204; 203, string:"user"; 204, subscript; 204, 205; 204, 206; 205, identifier:tokens; 206, integer:3; 207, pair; 207, 208; 207, 209; 208, string:"state"; 209, identifier:jobstate; 210, pair; 210, 211; 210, 212; 211, string:"submit/start at"; 212, binary_operator:%; 212, 213; 212, 214; 213, string:"%s %s"; 214, tuple; 214, 215; 214, 218; 215, subscript; 215, 216; 215, 217; 216, identifier:tokens; 217, integer:5; 218, subscript; 218, 219; 218, 220; 219, identifier:tokens; 220, integer:6; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:jataskID; 224, integer:0; 225, if_statement; 225, 226; 225, 229; 225, 246; 226, comparison_operator:==; 226, 227; 226, 228; 227, identifier:jobstate; 228, string:"r"; 229, block; 229, 230; 229, 238; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:details; 234, string:"queue"; 235, subscript; 235, 236; 235, 237; 236, identifier:tokens; 237, integer:7; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 243; 240, subscript; 240, 241; 240, 242; 241, identifier:details; 242, string:"slots"; 243, subscript; 243, 244; 243, 245; 244, identifier:tokens; 245, integer:8; 246, elif_clause; 246, 247; 246, 250; 247, comparison_operator:==; 247, 248; 247, 249; 248, identifier:jobstate; 249, string:"qw"; 250, block; 250, 251; 250, 259; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 256; 253, subscript; 253, 254; 253, 255; 254, identifier:details; 255, string:"slots"; 256, subscript; 256, 257; 256, 258; 257, identifier:tokens; 258, integer:7; 259, if_statement; 259, 260; 259, 266; 260, comparison_operator:>=; 260, 261; 260, 265; 261, call; 261, 262; 261, 263; 262, identifier:len; 263, argument_list; 263, 264; 264, identifier:tokens; 265, integer:9; 266, block; 266, 267; 266, 273; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:jataskID; 270, subscript; 270, 271; 270, 272; 271, identifier:tokens; 272, integer:8; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 278; 275, subscript; 275, 276; 275, 277; 276, identifier:details; 277, string:"ja-task-ID"; 278, identifier:jataskID; 279, if_statement; 279, 280; 279, 286; 280, comparison_operator:>; 280, 281; 280, 285; 281, call; 281, 282; 281, 283; 282, identifier:len; 283, argument_list; 283, 284; 284, identifier:tokens; 285, integer:9; 286, block; 286, 287; 286, 293; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:jataskID; 290, subscript; 290, 291; 290, 292; 291, identifier:tokens; 292, integer:9; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 298; 295, subscript; 295, 296; 295, 297; 296, identifier:details; 297, string:"ja-task-ID"; 298, identifier:jataskID; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 304; 301, subscript; 301, 302; 301, 303; 302, identifier:jobs; 303, identifier:jid; 304, boolean_operator:or; 304, 305; 304, 311; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:jobs; 308, identifier:get; 309, argument_list; 309, 310; 310, identifier:jid; 311, dictionary; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 319; 314, subscript; 314, 315; 314, 318; 315, subscript; 315, 316; 315, 317; 316, identifier:jobs; 317, identifier:jid; 318, identifier:jataskID; 319, identifier:details; 320, if_statement; 320, 321; 320, 327; 320, 359; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:joblist; 324, identifier:get; 325, argument_list; 325, 326; 326, identifier:jid; 327, block; 327, 328; 327, 336; 327, 344; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 331; 330, identifier:jobdir; 331, subscript; 331, 332; 331, 335; 332, subscript; 332, 333; 332, 334; 333, identifier:joblist; 334, identifier:jid; 335, string:"Directory"; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 339; 338, identifier:jobtime; 339, subscript; 339, 340; 339, 343; 340, subscript; 340, 341; 340, 342; 341, identifier:joblist; 342, identifier:jid; 343, string:"TimeInSeconds"; 344, expression_statement; 344, 345; 345, call; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:colorprinter; 348, identifier:message; 349, argument_list; 349, 350; 350, binary_operator:%; 350, 351; 350, 352; 351, string:"Job %d submitted %d minutes ago. Status: '%s'. Destination directory: %s."; 352, tuple; 352, 353; 352, 354; 352, 357; 352, 358; 353, identifier:jid; 354, binary_operator:/; 354, 355; 354, 356; 355, identifier:jobtime; 356, integer:60; 357, identifier:jobstate; 358, identifier:jobdir; 359, else_clause; 359, 360; 360, block; 360, 361; 361, expression_statement; 361, 362; 362, call; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:colorprinter; 365, identifier:message; 366, argument_list; 366, 367; 367, binary_operator:%; 367, 368; 367, 369; 368, string:"Job %d submitted at %s %s. Status: '%s'. Destination directory unknown."; 369, tuple; 369, 370; 369, 371; 369, 374; 369, 377; 370, identifier:jid; 371, subscript; 371, 372; 371, 373; 372, identifier:tokens; 373, integer:5; 374, subscript; 374, 375; 374, 376; 375, identifier:tokens; 376, integer:6; 377, identifier:jobstate; 378, return_statement; 378, 379; 379, True | def query(logfile, jobID = None):
"""If jobID is an integer then return False if the job has finished and True if it is still running.
Otherwise, returns a table of jobs run by the user."""
joblist = logfile.readFromLogfile()
if jobID and type(jobID) == type(1):
command = ['qstat', '-j', str(jobID)]
else:
command = ['qstat']
processoutput = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
output = processoutput[0]
serror = processoutput[1]
# Form command
jobs = {}
if type(jobID) == type(1):
if serror.find("Following jobs do not exist") != -1:
return False
else:
return True
if not output.strip():
colorprinter.message("No jobs running at present.")
output = output.strip().split("\n")
if len(output) > 2:
for line in output[2:]:
# We assume that our script names contain no spaces for the parsing below to work
tokens = line.split()
jid = int(tokens[0])
jobstate = tokens[4]
details = { "jobid" : jid,
"prior" : tokens[1],
"name" : tokens[2],
"user" : tokens[3],
"state" : jobstate,
"submit/start at" : "%s %s" % (tokens[5], tokens[6])
}
jataskID = 0
if jobstate == "r":
details["queue"] = tokens[7]
details["slots"] = tokens[8]
elif jobstate == "qw":
details["slots"] = tokens[7]
if len(tokens) >= 9:
jataskID = tokens[8]
details["ja-task-ID"] = jataskID
if len(tokens) > 9:
jataskID = tokens[9]
details["ja-task-ID"] = jataskID
jobs[jid] = jobs.get(jid) or {}
jobs[jid][jataskID] = details
if joblist.get(jid):
jobdir = joblist[jid]["Directory"]
jobtime = joblist[jid]["TimeInSeconds"]
colorprinter.message("Job %d submitted %d minutes ago. Status: '%s'. Destination directory: %s." % (jid, jobtime / 60, jobstate, jobdir))
else:
colorprinter.message("Job %d submitted at %s %s. Status: '%s'. Destination directory unknown." % (jid, tokens[5], tokens[6], jobstate))
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:build; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:client; 5, identifier:repository_tag; 6, identifier:docker_file; 7, default_parameter; 7, 8; 7, 9; 8, identifier:tag; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:use_cache; 12, False; 13, block; 13, 14; 13, 16; 13, 31; 13, 62; 13, 77; 13, 85; 13, 103; 13, 108; 13, 123; 13, 127; 13, 239; 13, 244; 13, 250; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 25; 17, not_operator; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:isinstance; 20, argument_list; 20, 21; 20, 22; 21, identifier:client; 22, attribute; 22, 23; 22, 24; 23, identifier:docker; 24, identifier:Client; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:TypeError; 29, argument_list; 29, 30; 30, string:"client needs to be of type docker.Client."; 31, if_statement; 31, 32; 31, 50; 31, 51; 32, boolean_operator:or; 32, 33; 32, 41; 33, not_operator; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:isinstance; 36, argument_list; 36, 37; 36, 38; 37, identifier:docker_file; 38, attribute; 38, 39; 38, 40; 39, identifier:six; 40, identifier:string_types; 41, not_operator; 41, 42; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:os; 46, identifier:path; 47, identifier:exists; 48, argument_list; 48, 49; 49, identifier:docker_file; 50, comment; 51, block; 51, 52; 52, raise_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:Exception; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, string:"docker file path doesn't exist: {0}"; 59, identifier:format; 60, argument_list; 60, 61; 61, identifier:docker_file; 62, if_statement; 62, 63; 62, 71; 63, not_operator; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:isinstance; 66, argument_list; 66, 67; 66, 68; 67, identifier:repository_tag; 68, attribute; 68, 69; 68, 70; 69, identifier:six; 70, identifier:string_types; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:TypeError; 75, argument_list; 75, 76; 76, string:'repository must be a string'; 77, if_statement; 77, 78; 77, 80; 78, not_operator; 78, 79; 79, identifier:tag; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:tag; 84, string:'latest'; 85, if_statement; 85, 86; 85, 92; 86, not_operator; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:isinstance; 89, argument_list; 89, 90; 89, 91; 90, identifier:use_cache; 91, identifier:bool; 92, block; 92, 93; 93, raise_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:TypeError; 96, argument_list; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, string:"use_cache must be a bool. {0} was passed."; 100, identifier:format; 101, argument_list; 101, 102; 102, identifier:use_cache; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:no_cache; 106, not_operator; 106, 107; 107, identifier:use_cache; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:not; 109, 110; 109, 111; 110, string:':'; 111, identifier:repository_tag; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:repository_tag; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, string:"{0}:{1}"; 119, identifier:format; 120, argument_list; 120, 121; 120, 122; 121, identifier:repository_tag; 122, identifier:tag; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:file_obj; 126, None; 127, try_statement; 127, 128; 127, 220; 127, 228; 128, block; 128, 129; 129, if_statement; 129, 130; 129, 138; 129, 195; 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:isfile; 136, argument_list; 136, 137; 137, identifier:docker_file; 138, block; 138, 139; 138, 147; 138, 163; 138, 164; 138, 165; 138, 166; 138, 167; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:path; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:os; 145, identifier:getcwd; 146, argument_list; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:docker_file; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, string:"./{0}"; 153, identifier:format; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:os; 159, identifier:path; 160, identifier:relpath; 161, argument_list; 161, 162; 162, identifier:docker_file; 163, comment; 164, comment; 165, comment; 166, comment; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:response; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:client; 173, identifier:build; 174, argument_list; 174, 175; 174, 178; 174, 181; 174, 182; 174, 185; 174, 186; 174, 189; 174, 192; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:path; 177, identifier:path; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:nocache; 180, identifier:no_cache; 181, comment; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:dockerfile; 184, identifier:docker_file; 185, comment; 186, keyword_argument; 186, 187; 186, 188; 187, identifier:tag; 188, identifier:repository_tag; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:rm; 191, True; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:stream; 194, True; 195, else_clause; 195, 196; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:response; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:client; 203, identifier:build; 204, argument_list; 204, 205; 204, 208; 204, 211; 204, 214; 204, 217; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:path; 207, identifier:docker_file; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:tag; 210, identifier:repository_tag; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:rm; 213, True; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:nocache; 216, identifier:no_cache; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:stream; 219, True; 220, except_clause; 220, 221; 220, 225; 221, as_pattern; 221, 222; 221, 223; 222, identifier:Exception; 223, as_pattern_target; 223, 224; 224, identifier:e; 225, block; 225, 226; 226, raise_statement; 226, 227; 227, identifier:e; 228, finally_clause; 228, 229; 229, block; 229, 230; 230, if_statement; 230, 231; 230, 232; 231, identifier:file_obj; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:file_obj; 237, identifier:close; 238, argument_list; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 242; 241, identifier:parse_stream; 242, argument_list; 242, 243; 243, identifier:response; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:client; 248, identifier:close; 249, argument_list; 250, return_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:Image; 253, argument_list; 253, 254; 253, 255; 254, identifier:client; 255, identifier:repository_tag | def build(client, repository_tag, docker_file, tag=None, use_cache=False):
"""
Build a docker image
"""
if not isinstance(client, docker.Client):
raise TypeError("client needs to be of type docker.Client.")
if not isinstance(docker_file, six.string_types) or not os.path.exists(docker_file):
# TODO: need to add path stuff for git and http etc.
raise Exception("docker file path doesn't exist: {0}".format(docker_file))
if not isinstance(repository_tag, six.string_types):
raise TypeError('repository must be a string')
if not tag:
tag = 'latest'
if not isinstance(use_cache, bool):
raise TypeError("use_cache must be a bool. {0} was passed.".format(use_cache))
no_cache = not use_cache
if ':' not in repository_tag:
repository_tag = "{0}:{1}".format(repository_tag, tag)
file_obj = None
try:
if os.path.isfile(docker_file):
path = os.getcwd()
docker_file = "./{0}".format(os.path.relpath(docker_file))
# TODO: support using file_obj in the future. Needed for post pre hooks and the injector.
# with open(docker_file) as Dockerfile:
# testing = Dockerfile.read()
# file_obj = BytesIO(testing.encode('utf-8'))
response = client.build(
path=path,
nocache=no_cache,
# custom_context=True,
dockerfile=docker_file,
# fileobj=file_obj,
tag=repository_tag,
rm=True,
stream=True
)
else:
response = client.build(path=docker_file, tag=repository_tag, rm=True, nocache=no_cache, stream=True)
except Exception as e:
raise e
finally:
if file_obj:
file_obj.close()
parse_stream(response)
client.close()
return Image(client, repository_tag) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:create_class; 3, parameters; 3, 4; 4, identifier:request; 5, block; 5, 6; 5, 8; 5, 27; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, comparison_operator:==; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:request; 12, identifier:method; 13, string:'GET'; 14, block; 14, 15; 15, return_statement; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:render; 18, argument_list; 18, 19; 18, 20; 18, 21; 18, 22; 19, identifier:request; 20, string:'classes_create.html'; 21, dictionary; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:help_text; 24, attribute; 24, 25; 24, 26; 25, identifier:create_class; 26, identifier:__doc__; 27, if_statement; 27, 28; 27, 33; 27, 215; 28, comparison_operator:==; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:request; 31, identifier:method; 32, string:'POST'; 33, block; 33, 34; 33, 74; 33, 88; 33, 131; 33, 162; 33, 180; 33, 193; 33, 199; 34, if_statement; 34, 35; 34, 52; 35, boolean_operator:or; 35, 36; 35, 44; 36, not_operator; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:request; 41, identifier:user; 42, identifier:is_authenticated; 43, argument_list; 44, not_operator; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:hasattr; 47, argument_list; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:request; 50, identifier:user; 51, string:"userprofile"; 52, block; 52, 53; 53, return_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:render_json; 56, argument_list; 56, 57; 56, 58; 56, 68; 56, 71; 57, identifier:request; 58, dictionary; 58, 59; 58, 65; 59, pair; 59, 60; 59, 61; 60, string:'error'; 61, call; 61, 62; 61, 63; 62, identifier:_; 63, argument_list; 63, 64; 64, string:'User is not logged in.'; 65, pair; 65, 66; 65, 67; 66, string:'error_type'; 67, string:'user_unauthorized'; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:template; 70, string:'classes_create.html'; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:status; 73, integer:401; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:data; 77, call; 77, 78; 77, 79; 78, identifier:json_body; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:request; 84, identifier:body; 85, identifier:decode; 86, argument_list; 86, 87; 87, string:"utf-8"; 88, if_statement; 88, 89; 88, 109; 89, boolean_operator:and; 89, 90; 89, 93; 90, comparison_operator:in; 90, 91; 90, 92; 91, string:'code'; 92, identifier:data; 93, call; 93, 94; 93, 108; 94, attribute; 94, 95; 94, 107; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:Class; 99, identifier:objects; 100, identifier:filter; 101, argument_list; 101, 102; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:code; 104, subscript; 104, 105; 104, 106; 105, identifier:data; 106, string:'code'; 107, identifier:exists; 108, argument_list; 109, block; 109, 110; 110, return_statement; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:render_json; 113, argument_list; 113, 114; 113, 115; 113, 125; 113, 128; 114, identifier:request; 115, dictionary; 115, 116; 115, 122; 116, pair; 116, 117; 116, 118; 117, string:'error'; 118, call; 118, 119; 118, 120; 119, identifier:_; 120, argument_list; 120, 121; 121, string:'A class with this code already exists.'; 122, pair; 122, 123; 122, 124; 123, string:'error_type'; 124, string:'class_with_code_exists'; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:template; 127, string:'classes_create.html'; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:status; 130, integer:400; 131, if_statement; 131, 132; 131, 140; 132, boolean_operator:or; 132, 133; 132, 136; 133, comparison_operator:not; 133, 134; 133, 135; 134, string:'name'; 135, identifier:data; 136, not_operator; 136, 137; 137, subscript; 137, 138; 137, 139; 138, identifier:data; 139, string:'name'; 140, block; 140, 141; 141, return_statement; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:render_json; 144, argument_list; 144, 145; 144, 146; 144, 156; 144, 159; 145, identifier:request; 146, dictionary; 146, 147; 146, 153; 147, pair; 147, 148; 147, 149; 148, string:'error'; 149, call; 149, 150; 149, 151; 150, identifier:_; 151, argument_list; 151, 152; 152, string:'Class name is missing.'; 153, pair; 153, 154; 153, 155; 154, string:'error_type'; 155, string:'missing_class_name'; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:template; 158, string:'classes_create.html'; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:status; 161, integer:400; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:cls; 165, call; 165, 166; 165, 167; 166, identifier:Class; 167, argument_list; 167, 168; 167, 173; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:name; 170, subscript; 170, 171; 170, 172; 171, identifier:data; 172, string:'name'; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:owner; 175, attribute; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:request; 178, identifier:user; 179, identifier:userprofile; 180, if_statement; 180, 181; 180, 184; 181, comparison_operator:in; 181, 182; 181, 183; 182, string:'code'; 183, identifier:data; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:cls; 189, identifier:code; 190, subscript; 190, 191; 190, 192; 191, identifier:data; 192, string:'code'; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:cls; 197, identifier:save; 198, argument_list; 199, return_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:render_json; 202, argument_list; 202, 203; 202, 204; 202, 209; 202, 212; 203, identifier:request; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:cls; 207, identifier:to_json; 208, argument_list; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:template; 211, string:'classes_create.html'; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:status; 214, integer:201; 215, else_clause; 215, 216; 216, block; 216, 217; 217, return_statement; 217, 218; 218, call; 218, 219; 218, 220; 219, identifier:HttpResponseBadRequest; 220, argument_list; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, string:"method %s is not allowed"; 224, identifier:format; 225, argument_list; 225, 226; 226, attribute; 226, 227; 226, 228; 227, identifier:request; 228, identifier:method | def create_class(request):
"""Create new class
POST parameters (JSON):
name:
Human readable name of class
code (optional):
unique code of class used for joining to class
"""
if request.method == 'GET':
return render(request, 'classes_create.html', {}, help_text=create_class.__doc__)
if request.method == 'POST':
if not request.user.is_authenticated() or not hasattr(request.user, "userprofile"):
return render_json(request, {
'error': _('User is not logged in.'),
'error_type': 'user_unauthorized'
}, template='classes_create.html', status=401)
data = json_body(request.body.decode("utf-8"))
if 'code' in data and Class.objects.filter(code=data['code']).exists():
return render_json(request, {
'error': _('A class with this code already exists.'),
'error_type': 'class_with_code_exists'
}, template='classes_create.html', status=400)
if 'name' not in data or not data['name']:
return render_json(request, {'error': _('Class name is missing.'), 'error_type': 'missing_class_name'},
template='classes_create.html', status=400)
cls = Class(name=data['name'], owner=request.user.userprofile)
if 'code' in data:
cls.code = data['code']
cls.save()
return render_json(request, cls.to_json(), template='classes_create.html', status=201)
else:
return HttpResponseBadRequest("method %s is not allowed".format(request.method)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:create_student; 3, parameters; 3, 4; 4, identifier:request; 5, block; 5, 6; 5, 8; 5, 45; 5, 64; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 18; 9, not_operator; 9, 10; 10, call; 10, 11; 10, 12; 11, identifier:get_config; 12, argument_list; 12, 13; 12, 14; 12, 15; 13, string:'proso_user'; 14, string:'allow_create_students'; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:default; 17, False; 18, block; 18, 19; 19, return_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:render_json; 22, argument_list; 22, 23; 22, 24; 22, 34; 22, 37; 22, 42; 23, identifier:request; 24, dictionary; 24, 25; 24, 31; 25, pair; 25, 26; 25, 27; 26, string:'error'; 27, call; 27, 28; 27, 29; 28, identifier:_; 29, argument_list; 29, 30; 30, string:'Creation of new users is not allowed.'; 31, pair; 31, 32; 31, 33; 32, string:'error_type'; 33, string:'student_creation_not_allowed'; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:template; 36, string:'class_create_student.html'; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:help_text; 39, attribute; 39, 40; 39, 41; 40, identifier:create_student; 41, identifier:__doc__; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:status; 44, integer:403; 45, if_statement; 45, 46; 45, 51; 46, comparison_operator:==; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:request; 49, identifier:method; 50, string:'GET'; 51, block; 51, 52; 52, return_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:render; 55, argument_list; 55, 56; 55, 57; 55, 58; 55, 59; 56, identifier:request; 57, string:'class_create_student.html'; 58, dictionary; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:help_text; 61, attribute; 61, 62; 61, 63; 62, identifier:create_student; 63, identifier:__doc__; 64, if_statement; 64, 65; 64, 70; 64, 411; 65, comparison_operator:==; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:request; 68, identifier:method; 69, string:'POST'; 70, block; 70, 71; 70, 111; 70, 125; 70, 177; 70, 208; 70, 219; 70, 235; 70, 290; 70, 356; 70, 373; 70, 379; 70, 390; 71, if_statement; 71, 72; 71, 89; 72, boolean_operator:or; 72, 73; 72, 81; 73, not_operator; 73, 74; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:request; 78, identifier:user; 79, identifier:is_authenticated; 80, argument_list; 81, not_operator; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:hasattr; 84, argument_list; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:request; 87, identifier:user; 88, string:"userprofile"; 89, block; 89, 90; 90, return_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:render_json; 93, argument_list; 93, 94; 93, 95; 93, 105; 93, 108; 94, identifier:request; 95, dictionary; 95, 96; 95, 102; 96, pair; 96, 97; 96, 98; 97, string:'error'; 98, call; 98, 99; 98, 100; 99, identifier:_; 100, argument_list; 100, 101; 101, string:'User is not logged in.'; 102, pair; 102, 103; 102, 104; 103, string:'error_type'; 104, string:'user_unauthorized'; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:template; 107, string:'class_create_student.html'; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:status; 110, integer:401; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:data; 114, call; 114, 115; 114, 116; 115, identifier:json_body; 116, argument_list; 116, 117; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:request; 121, identifier:body; 122, identifier:decode; 123, argument_list; 123, 124; 124, string:"utf-8"; 125, try_statement; 125, 126; 125, 149; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:cls; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:Class; 134, identifier:objects; 135, identifier:get; 136, argument_list; 136, 137; 136, 142; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:pk; 139, subscript; 139, 140; 139, 141; 140, identifier:data; 141, string:'class'; 142, keyword_argument; 142, 143; 142, 144; 143, identifier:owner; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:request; 147, identifier:user; 148, identifier:userprofile; 149, except_clause; 149, 150; 149, 155; 150, tuple; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:Class; 153, identifier:DoesNotExist; 154, identifier:KeyError; 155, block; 155, 156; 156, return_statement; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:render_json; 159, argument_list; 159, 160; 159, 161; 159, 171; 159, 174; 160, identifier:request; 161, dictionary; 161, 162; 161, 168; 162, pair; 162, 163; 162, 164; 163, string:'error'; 164, call; 164, 165; 164, 166; 165, identifier:_; 166, argument_list; 166, 167; 167, string:'Class with given id not found.'; 168, pair; 168, 169; 168, 170; 169, string:'error_type'; 170, string:'class_not_found'; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:template; 173, string:'class_create_student.html'; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:status; 176, integer:404; 177, if_statement; 177, 178; 177, 186; 178, boolean_operator:or; 178, 179; 178, 182; 179, comparison_operator:not; 179, 180; 179, 181; 180, string:'first_name'; 181, identifier:data; 182, not_operator; 182, 183; 183, subscript; 183, 184; 183, 185; 184, identifier:data; 185, string:'first_name'; 186, block; 186, 187; 187, return_statement; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:render_json; 190, argument_list; 190, 191; 190, 192; 190, 202; 190, 205; 191, identifier:request; 192, dictionary; 192, 193; 192, 199; 193, pair; 193, 194; 193, 195; 194, string:'error'; 195, call; 195, 196; 195, 197; 196, identifier:_; 197, argument_list; 197, 198; 198, string:'First name code is missing.'; 199, pair; 199, 200; 199, 201; 200, string:'error_type'; 201, string:'missing_first_name'; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:template; 204, string:'class_create_student.html'; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:status; 207, integer:400; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:user; 211, call; 211, 212; 211, 213; 212, identifier:User; 213, argument_list; 213, 214; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:first_name; 216, subscript; 216, 217; 216, 218; 217, identifier:data; 218, string:'first_name'; 219, if_statement; 219, 220; 219, 226; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:data; 223, identifier:get; 224, argument_list; 224, 225; 225, string:'last_name'; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:user; 231, identifier:last_name; 232, subscript; 232, 233; 232, 234; 233, identifier:data; 234, string:'last_name'; 235, if_statement; 235, 236; 235, 242; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:data; 239, identifier:get; 240, argument_list; 240, 241; 241, string:'email'; 242, block; 242, 243; 242, 282; 243, if_statement; 243, 244; 243, 260; 244, call; 244, 245; 244, 259; 245, attribute; 245, 246; 245, 258; 246, call; 246, 247; 246, 252; 247, attribute; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:User; 250, identifier:objects; 251, identifier:filter; 252, argument_list; 252, 253; 253, keyword_argument; 253, 254; 253, 255; 254, identifier:email; 255, subscript; 255, 256; 255, 257; 256, identifier:data; 257, string:'email'; 258, identifier:exists; 259, argument_list; 260, block; 260, 261; 261, return_statement; 261, 262; 262, call; 262, 263; 262, 264; 263, identifier:render_json; 264, argument_list; 264, 265; 264, 266; 264, 276; 264, 279; 265, identifier:request; 266, dictionary; 266, 267; 266, 273; 267, pair; 267, 268; 267, 269; 268, string:'error'; 269, call; 269, 270; 269, 271; 270, identifier:_; 271, argument_list; 271, 272; 272, string:'There is already a user with the given e-mail.'; 273, pair; 273, 274; 273, 275; 274, string:'error_type'; 275, string:'email_exists'; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:template; 278, string:'class_create_student.html'; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:status; 281, integer:400; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:user; 286, identifier:email; 287, subscript; 287, 288; 287, 289; 288, identifier:data; 289, string:'email'; 290, if_statement; 290, 291; 290, 297; 290, 345; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:data; 294, identifier:get; 295, argument_list; 295, 296; 296, string:'username'; 297, block; 297, 298; 297, 337; 298, if_statement; 298, 299; 298, 315; 299, call; 299, 300; 299, 314; 300, attribute; 300, 301; 300, 313; 301, call; 301, 302; 301, 307; 302, attribute; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:User; 305, identifier:objects; 306, identifier:filter; 307, argument_list; 307, 308; 308, keyword_argument; 308, 309; 308, 310; 309, identifier:username; 310, subscript; 310, 311; 310, 312; 311, identifier:data; 312, string:'username'; 313, identifier:exists; 314, argument_list; 315, block; 315, 316; 316, return_statement; 316, 317; 317, call; 317, 318; 317, 319; 318, identifier:render_json; 319, argument_list; 319, 320; 319, 321; 319, 331; 319, 334; 320, identifier:request; 321, dictionary; 321, 322; 321, 328; 322, pair; 322, 323; 322, 324; 323, string:'error'; 324, call; 324, 325; 324, 326; 325, identifier:_; 326, argument_list; 326, 327; 327, string:'There is already a user with the given username.'; 328, pair; 328, 329; 328, 330; 329, string:'error_type'; 330, string:'username_exists'; 331, keyword_argument; 331, 332; 331, 333; 332, identifier:template; 333, string:'class_create_student.html'; 334, keyword_argument; 334, 335; 334, 336; 335, identifier:status; 336, integer:400; 337, expression_statement; 337, 338; 338, assignment; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:user; 341, identifier:username; 342, subscript; 342, 343; 342, 344; 343, identifier:data; 344, string:'username'; 345, else_clause; 345, 346; 346, block; 346, 347; 347, expression_statement; 347, 348; 348, assignment; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:user; 351, identifier:username; 352, call; 352, 353; 352, 354; 353, identifier:get_unused_username; 354, argument_list; 354, 355; 355, identifier:user; 356, if_statement; 356, 357; 356, 363; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:data; 360, identifier:get; 361, argument_list; 361, 362; 362, string:'password'; 363, block; 363, 364; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:user; 368, identifier:set_password; 369, argument_list; 369, 370; 370, subscript; 370, 371; 370, 372; 371, identifier:data; 372, string:'password'; 373, expression_statement; 373, 374; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:user; 377, identifier:save; 378, argument_list; 379, expression_statement; 379, 380; 380, call; 380, 381; 380, 386; 381, attribute; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, identifier:cls; 384, identifier:members; 385, identifier:add; 386, argument_list; 386, 387; 387, attribute; 387, 388; 387, 389; 388, identifier:user; 389, identifier:userprofile; 390, return_statement; 390, 391; 391, call; 391, 392; 391, 393; 392, identifier:render_json; 393, argument_list; 393, 394; 393, 395; 393, 405; 393, 408; 394, identifier:request; 395, call; 395, 396; 395, 401; 396, attribute; 396, 397; 396, 400; 397, attribute; 397, 398; 397, 399; 398, identifier:user; 399, identifier:userprofile; 400, identifier:to_json; 401, argument_list; 401, 402; 402, keyword_argument; 402, 403; 402, 404; 403, identifier:nested; 404, True; 405, keyword_argument; 405, 406; 405, 407; 406, identifier:template; 407, string:'class_create_student.html'; 408, keyword_argument; 408, 409; 408, 410; 409, identifier:status; 410, integer:201; 411, else_clause; 411, 412; 412, block; 412, 413; 413, return_statement; 413, 414; 414, call; 414, 415; 414, 416; 415, identifier:HttpResponseBadRequest; 416, argument_list; 416, 417; 417, call; 417, 418; 417, 421; 418, attribute; 418, 419; 418, 420; 419, string:"method %s is not allowed"; 420, identifier:format; 421, argument_list; 421, 422; 422, attribute; 422, 423; 422, 424; 423, identifier:request; 424, identifier:method | def create_student(request):
""" Create new user in class
POST parameters (JSON):
class:
id of the class
username (optional):
username of student, if not provided username is create based on name
password (optional):
password of student
first_name:
first_name of student
last_name (optional):
last_name of student
email (optional):
e-mail of student
"""
if not get_config('proso_user', 'allow_create_students', default=False):
return render_json(request, {
'error': _('Creation of new users is not allowed.'),
'error_type': 'student_creation_not_allowed'
}, template='class_create_student.html', help_text=create_student.__doc__, status=403)
if request.method == 'GET':
return render(request, 'class_create_student.html', {}, help_text=create_student.__doc__)
if request.method == 'POST':
if not request.user.is_authenticated() or not hasattr(request.user, "userprofile"):
return render_json(request, {
'error': _('User is not logged in.'),
'error_type': 'user_unauthorized'
}, template='class_create_student.html', status=401)
data = json_body(request.body.decode("utf-8"))
try:
cls = Class.objects.get(pk=data['class'], owner=request.user.userprofile)
except (Class.DoesNotExist, KeyError):
return render_json(request, {
'error': _('Class with given id not found.'),
'error_type': 'class_not_found',
}, template='class_create_student.html', status=404)
if 'first_name' not in data or not data['first_name']:
return render_json(request, {
'error': _('First name code is missing.'),
'error_type': 'missing_first_name'
}, template='class_create_student.html', status=400)
user = User(first_name=data['first_name'])
if data.get('last_name'):
user.last_name = data['last_name']
if data.get('email'):
if User.objects.filter(email=data['email']).exists():
return render_json(request, {
'error': _('There is already a user with the given e-mail.'),
'error_type': 'email_exists'
}, template='class_create_student.html', status=400)
user.email = data['email']
if data.get('username'):
if User.objects.filter(username=data['username']).exists():
return render_json(request, {
'error': _('There is already a user with the given username.'),
'error_type': 'username_exists'
}, template='class_create_student.html', status=400)
user.username = data['username']
else:
user.username = get_unused_username(user)
if data.get('password'):
user.set_password(data['password'])
user.save()
cls.members.add(user.userprofile)
return render_json(request, user.userprofile.to_json(nested=True), template='class_create_student.html', status=201)
else:
return HttpResponseBadRequest("method %s is not allowed".format(request.method)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 80; 2, function_name:lograptor; 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; 3, 71; 3, 74; 3, 77; 4, identifier:files; 5, default_parameter; 5, 6; 5, 7; 6, identifier:patterns; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:matcher; 10, string:'ruled'; 11, default_parameter; 11, 12; 11, 13; 12, identifier:cfgfiles; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:apps; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:hosts; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:filters; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:time_period; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:time_range; 28, None; 29, default_parameter; 29, 30; 29, 31; 30, identifier:case; 31, False; 32, default_parameter; 32, 33; 32, 34; 33, identifier:invert; 34, False; 35, default_parameter; 35, 36; 35, 37; 36, identifier:word; 37, False; 38, default_parameter; 38, 39; 38, 40; 39, identifier:files_with_match; 40, None; 41, default_parameter; 41, 42; 41, 43; 42, identifier:count; 43, False; 44, default_parameter; 44, 45; 44, 46; 45, identifier:quiet; 46, False; 47, default_parameter; 47, 48; 47, 49; 48, identifier:max_count; 49, integer:0; 50, default_parameter; 50, 51; 50, 52; 51, identifier:only_matching; 52, False; 53, default_parameter; 53, 54; 53, 55; 54, identifier:line_number; 55, False; 56, default_parameter; 56, 57; 56, 58; 57, identifier:with_filename; 58, None; 59, default_parameter; 59, 60; 59, 61; 60, identifier:ip_lookup; 61, False; 62, default_parameter; 62, 63; 62, 64; 63, identifier:uid_lookup; 64, False; 65, default_parameter; 65, 66; 65, 67; 66, identifier:anonymize; 67, False; 68, default_parameter; 68, 69; 68, 70; 69, identifier:thread; 70, False; 71, default_parameter; 71, 72; 71, 73; 72, identifier:before_context; 73, integer:0; 74, default_parameter; 74, 75; 74, 76; 75, identifier:after_context; 76, integer:0; 77, default_parameter; 77, 78; 77, 79; 78, identifier:context; 79, integer:0; 80, block; 80, 81; 80, 83; 80, 89; 80, 97; 80, 103; 80, 109; 80, 115; 80, 121; 80, 127; 80, 133; 80, 139; 80, 145; 80, 151; 80, 157; 80, 163; 80, 169; 80, 175; 80, 181; 80, 187; 80, 193; 80, 199; 80, 205; 80, 211; 80, 217; 80, 223; 80, 229; 80, 241; 80, 252; 80, 263; 80, 274; 80, 281; 81, expression_statement; 81, 82; 82, comment; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:cli_parser; 86, call; 86, 87; 86, 88; 87, identifier:create_argument_parser; 88, argument_list; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:args; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:cli_parser; 95, identifier:parse_args; 96, argument_list; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:args; 101, identifier:files; 102, identifier:files; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:args; 107, identifier:matcher; 108, identifier:matcher; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:args; 113, identifier:cfgfiles; 114, identifier:cfgfiles; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:args; 119, identifier:time_period; 120, identifier:time_period; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:args; 125, identifier:time_range; 126, identifier:time_range; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:args; 131, identifier:case; 132, identifier:case; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:args; 137, identifier:invert; 138, identifier:invert; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:args; 143, identifier:word; 144, identifier:word; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:args; 149, identifier:files_with_match; 150, identifier:files_with_match; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:args; 155, identifier:count; 156, identifier:count; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:args; 161, identifier:quiet; 162, identifier:quiet; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:args; 167, identifier:max_count; 168, identifier:max_count; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:args; 173, identifier:only_matching; 174, identifier:only_matching; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:args; 179, identifier:line_number; 180, identifier:line_number; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:args; 185, identifier:with_filename; 186, identifier:with_filename; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:args; 191, identifier:anonymize; 192, identifier:anonymize; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:args; 197, identifier:ip_lookup; 198, identifier:ip_lookup; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:args; 203, identifier:uid_lookup; 204, identifier:uid_lookup; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:args; 209, identifier:thread; 210, identifier:thread; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:args; 215, identifier:context; 216, identifier:context; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:args; 221, identifier:after_context; 222, identifier:after_context; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:args; 227, identifier:before_context; 228, identifier:before_context; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:args; 233, identifier:patterns; 234, conditional_expression:if; 234, 235; 234, 237; 234, 240; 235, list:['']; 235, 236; 236, string:''; 237, comparison_operator:is; 237, 238; 237, 239; 238, identifier:patterns; 239, None; 240, identifier:patterns; 241, if_statement; 241, 242; 241, 245; 242, comparison_operator:is; 242, 243; 242, 244; 243, identifier:apps; 244, None; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:args; 250, identifier:apps; 251, identifier:apps; 252, if_statement; 252, 253; 252, 256; 253, comparison_operator:is; 253, 254; 253, 255; 254, identifier:hosts; 255, None; 256, block; 256, 257; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:args; 261, identifier:hosts; 262, identifier:hosts; 263, if_statement; 263, 264; 263, 267; 264, comparison_operator:is; 264, 265; 264, 266; 265, identifier:filters; 266, None; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:args; 272, identifier:filters; 273, identifier:filters; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:_lograptor; 277, call; 277, 278; 277, 279; 278, identifier:LogRaptor; 279, argument_list; 279, 280; 280, identifier:args; 281, return_statement; 281, 282; 282, call; 282, 283; 282, 284; 283, identifier:_lograptor; 284, argument_list | def lograptor(files, patterns=None, matcher='ruled', cfgfiles=None, apps=None, hosts=None,
filters=None, time_period=None, time_range=None, case=False, invert=False,
word=False, files_with_match=None, count=False, quiet=False, max_count=0,
only_matching=False, line_number=False, with_filename=None,
ip_lookup=False, uid_lookup=False, anonymize=False, thread=False,
before_context=0, after_context=0, context=0):
"""
Run lograptor with arguments. Experimental feature to use the log processor into
generic Python scripts. This part is still under development, do not use.
:param files: Input files. Each argument can be a file path or a glob pathname.
:param patterns: Regex patterns, select the log line if at least one pattern matches.
:param matcher: Matcher engine, can be 'ruled' (default), 'unruled' or 'unparsed'.
:param cfgfiles: use a specific configuration file.
:param apps: process the log lines related to a list of applications.
:param hosts: process the log lines related to a list of hosts.
:param filters: process the log lines that match all the conditions for rule's field values.
:param time_range: process the log lines related to a time range.
:param time_period: restrict the search scope to a date or a date interval.
:param case: ignore case distinctions, defaults to `False`.
:param invert: invert the sense of patterns regexp matching.
:param word: force PATTERN to match only whole words.
:param files_with_match: get only names of FILEs containing matches, defaults is `False`.
:param count: get only a count of matching lines per FILE.
:param quiet: suppress all normal output.
:param max_count: stop after NUM matches.
:param only_matching: get only the part of a line matching PATTERN.
:param line_number: get line number with output lines.
:param with_filename: get or suppress the file name for each match.
:param ip_lookup: translate IP addresses to DNS names.
:param uid_lookup: translate numeric UIDs to usernames.
:param anonymize: anonymize defined rule's fields value.
:param thread: get the lines of logs related to each log line selected.
:param before_context: get NUM lines of leading context for each log line selected.
:param after_context: get NUM lines of trailing context for each log line selected.
:param context: get NUM lines of output context for each log line selected.
:return:
"""
cli_parser = create_argument_parser()
args = cli_parser.parse_args()
args.files = files
args.matcher = matcher
args.cfgfiles = cfgfiles
args.time_period = time_period
args.time_range = time_range
args.case = case
args.invert = invert
args.word = word
args.files_with_match = files_with_match
args.count = count
args.quiet = quiet
args.max_count = max_count
args.only_matching = only_matching
args.line_number = line_number
args.with_filename = with_filename
args.anonymize = anonymize
args.ip_lookup = ip_lookup
args.uid_lookup = uid_lookup
args.thread = thread
args.context = context
args.after_context = after_context
args.before_context = before_context
args.patterns = [''] if patterns is None else patterns
if apps is not None:
args.apps = apps
if hosts is not None:
args.hosts = hosts
if filters is not None:
args.filters = filters
_lograptor = LogRaptor(args)
return _lograptor() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:sanitize_codon_list; 3, parameters; 3, 4; 3, 5; 4, identifier:codon_list; 5, default_parameter; 5, 6; 5, 7; 6, identifier:forbidden_seqs; 7, tuple; 8, block; 8, 9; 8, 11; 8, 12; 8, 13; 8, 35; 8, 36; 8, 37; 8, 38; 8, 44; 8, 60; 8, 75; 8, 96; 8, 109; 8, 110; 8, 111; 8, 115; 8, 131; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, comment; 13, for_statement; 13, 14; 13, 15; 13, 16; 14, identifier:codon; 15, identifier:codon_list; 16, block; 16, 17; 17, if_statement; 17, 18; 17, 24; 18, comparison_operator:!=; 18, 19; 18, 23; 19, call; 19, 20; 19, 21; 20, identifier:len; 21, argument_list; 21, 22; 22, identifier:codon; 23, integer:3; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:ValueError; 28, argument_list; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, string:"Codons must have exactly 3 bases: '{}'"; 32, identifier:format; 33, argument_list; 33, 34; 34, identifier:codon; 35, comment; 36, comment; 37, comment; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:bad_seqs; 41, call; 41, 42; 41, 43; 42, identifier:set; 43, argument_list; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:bad_seqs; 48, identifier:union; 49, generator_expression; 49, 50; 49, 57; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:restriction_sites; 53, identifier:get; 54, argument_list; 54, 55; 54, 56; 55, identifier:seq; 56, identifier:seq; 57, for_in_clause; 57, 58; 57, 59; 58, identifier:seq; 59, identifier:forbidden_seqs; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:bad_seqs; 64, identifier:union; 65, generator_expression; 65, 66; 65, 72; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:dna; 69, identifier:reverse_complement; 70, argument_list; 70, 71; 71, identifier:seq; 72, for_in_clause; 72, 73; 72, 74; 73, identifier:seq; 74, identifier:bad_seqs; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:bad_seqs; 79, identifier:union; 80, generator_expression; 80, 81; 80, 91; 81, binary_operator:*; 81, 82; 81, 83; 82, identifier:base; 83, parenthesized_expression; 83, 84; 84, binary_operator:+; 84, 85; 84, 90; 85, subscript; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:gen9; 88, identifier:homopolymer_max_lengths; 89, identifier:base; 90, integer:1; 91, for_in_clause; 91, 92; 91, 93; 92, identifier:base; 93, attribute; 93, 94; 93, 95; 94, identifier:dna; 95, identifier:dna_bases; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:bad_seqs; 99, list_comprehension; 99, 100; 99, 106; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:dna; 103, identifier:dna_to_re; 104, argument_list; 104, 105; 105, identifier:bs; 106, for_in_clause; 106, 107; 106, 108; 107, identifier:bs; 108, identifier:bad_seqs; 109, comment; 110, comment; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:num_corrections; 114, integer:0; 115, for_statement; 115, 116; 115, 117; 115, 118; 116, identifier:bad_seq; 117, identifier:bad_seqs; 118, block; 118, 119; 119, while_statement; 119, 120; 119, 126; 120, call; 120, 121; 120, 122; 121, identifier:remove_bad_sequence; 122, argument_list; 122, 123; 122, 124; 122, 125; 123, identifier:codon_list; 124, identifier:bad_seq; 125, identifier:bad_seqs; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, augmented_assignment:+=; 128, 129; 128, 130; 129, identifier:num_corrections; 130, integer:1; 131, return_statement; 131, 132; 132, identifier:num_corrections | def sanitize_codon_list(codon_list, forbidden_seqs=()):
"""
Make silent mutations to the given codon lists to remove any undesirable
sequences that are present within it. Undesirable sequences include
restriction sites, which may be optionally specified as a second argument,
and homopolymers above a pre-defined length. The return value is the
number of corrections made to the codon list.
"""
# Unit test missing for:
# Homopolymer fixing
for codon in codon_list:
if len(codon) != 3:
raise ValueError("Codons must have exactly 3 bases: '{}'".format(codon))
# Compile a collection of all the sequences we don't want to appear in the
# gene. This includes the given restriction sites and their reverse
# complements, plus any homopolymers above a pre-defined length.
bad_seqs = set()
bad_seqs.union(
restriction_sites.get(seq, seq)
for seq in forbidden_seqs)
bad_seqs.union(
dna.reverse_complement(seq)
for seq in bad_seqs)
bad_seqs.union(
base * (gen9.homopolymer_max_lengths[base] + 1)
for base in dna.dna_bases)
bad_seqs = [
dna.dna_to_re(bs)
for bs in bad_seqs]
# Remove every bad sequence from the gene by making silent mutations to the
# codon list.
num_corrections = 0
for bad_seq in bad_seqs:
while remove_bad_sequence(codon_list, bad_seq, bad_seqs):
num_corrections += 1
return num_corrections |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:add_node; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:node; 6, block; 6, 7; 6, 9; 6, 17; 6, 18; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:nodes; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:nodes; 16, argument_list; 17, comment; 18, if_statement; 18, 19; 18, 27; 18, 60; 18, 61; 19, comparison_operator:<=; 19, 20; 19, 24; 20, call; 20, 21; 20, 22; 21, identifier:len; 22, argument_list; 22, 23; 23, identifier:nodes; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:m0; 27, block; 27, 28; 27, 44; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:other_nodes; 31, list_comprehension; 31, 32; 31, 33; 31, 36; 32, identifier:n; 33, for_in_clause; 33, 34; 33, 35; 34, identifier:n; 35, identifier:nodes; 36, if_clause; 36, 37; 37, comparison_operator:!=; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:n; 40, identifier:id; 41, attribute; 41, 42; 41, 43; 42, identifier:node; 43, identifier:id; 44, for_statement; 44, 45; 44, 46; 44, 47; 45, identifier:n; 46, identifier:other_nodes; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:node; 52, identifier:connect; 53, argument_list; 53, 54; 53, 57; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:direction; 56, string:"both"; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:whom; 59, identifier:n; 60, comment; 61, else_clause; 61, 62; 62, block; 62, 63; 63, for_statement; 63, 64; 63, 65; 63, 71; 64, identifier:idx_newvector; 65, call; 65, 66; 65, 67; 66, identifier:xrange; 67, argument_list; 67, 68; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:m; 71, block; 71, 72; 71, 102; 71, 120; 71, 121; 71, 138; 71, 151; 71, 155; 71, 179; 71, 180; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:these_nodes; 75, list_comprehension; 75, 76; 75, 77; 75, 80; 76, identifier:n; 77, for_in_clause; 77, 78; 77, 79; 78, identifier:n; 79, identifier:nodes; 80, if_clause; 80, 81; 81, parenthesized_expression; 81, 82; 82, boolean_operator:and; 82, 83; 82, 90; 83, comparison_operator:!=; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:n; 86, identifier:id; 87, attribute; 87, 88; 87, 89; 88, identifier:node; 89, identifier:id; 90, not_operator; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:n; 94, identifier:is_connected; 95, argument_list; 95, 96; 95, 99; 96, keyword_argument; 96, 97; 96, 98; 97, identifier:direction; 98, string:"either"; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:whom; 101, identifier:node; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:outdegrees; 105, list_comprehension; 105, 106; 105, 117; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:n; 112, identifier:vectors; 113, argument_list; 113, 114; 114, keyword_argument; 114, 115; 114, 116; 115, identifier:direction; 116, string:"outgoing"; 117, for_in_clause; 117, 118; 117, 119; 118, identifier:n; 119, identifier:these_nodes; 120, comment; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:ps; 124, list_comprehension; 124, 125; 124, 135; 125, parenthesized_expression; 125, 126; 126, binary_operator:/; 126, 127; 126, 128; 127, identifier:d; 128, parenthesized_expression; 128, 129; 129, binary_operator:*; 129, 130; 129, 131; 130, float:1.0; 131, call; 131, 132; 131, 133; 132, identifier:sum; 133, argument_list; 133, 134; 134, identifier:outdegrees; 135, for_in_clause; 135, 136; 135, 137; 136, identifier:d; 137, identifier:outdegrees; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:rnd; 141, binary_operator:*; 141, 142; 141, 147; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:random; 145, identifier:random; 146, argument_list; 147, call; 147, 148; 147, 149; 148, identifier:sum; 149, argument_list; 149, 150; 150, identifier:ps; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:cur; 154, float:0.0; 155, for_statement; 155, 156; 155, 159; 155, 163; 156, pattern_list; 156, 157; 156, 158; 157, identifier:i; 158, identifier:p; 159, call; 159, 160; 159, 161; 160, identifier:enumerate; 161, argument_list; 161, 162; 162, identifier:ps; 163, block; 163, 164; 163, 168; 164, expression_statement; 164, 165; 165, augmented_assignment:+=; 165, 166; 165, 167; 166, identifier:cur; 167, identifier:p; 168, if_statement; 168, 169; 168, 172; 169, comparison_operator:<; 169, 170; 169, 171; 170, identifier:rnd; 171, identifier:cur; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:vector_to; 176, subscript; 176, 177; 176, 178; 177, identifier:these_nodes; 178, identifier:i; 179, comment; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:node; 184, identifier:connect; 185, argument_list; 185, 186; 185, 189; 186, keyword_argument; 186, 187; 186, 188; 187, identifier:direction; 188, string:"both"; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:whom; 191, identifier:vector_to | def add_node(self, node):
"""Add newcomers one by one, using linear preferential attachment."""
nodes = self.nodes()
# Start with a core of m0 fully-connected agents...
if len(nodes) <= self.m0:
other_nodes = [n for n in nodes if n.id != node.id]
for n in other_nodes:
node.connect(direction="both", whom=n)
# ...then add newcomers one by one with preferential attachment.
else:
for idx_newvector in xrange(self.m):
these_nodes = [
n for n in nodes if (
n.id != node.id and
not n.is_connected(direction="either", whom=node))]
outdegrees = [
len(n.vectors(direction="outgoing")) for n in these_nodes]
# Select a member using preferential attachment
ps = [(d / (1.0 * sum(outdegrees))) for d in outdegrees]
rnd = random.random() * sum(ps)
cur = 0.0
for i, p in enumerate(ps):
cur += p
if rnd < cur:
vector_to = these_nodes[i]
# Create vector from newcomer to selected member and back
node.connect(direction="both", whom=vector_to) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:get_concepts_to_recalculate; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:users; 6, identifier:lang; 7, default_parameter; 7, 8; 7, 9; 8, identifier:concepts; 9, None; 10, block; 10, 11; 10, 13; 10, 17; 10, 34; 10, 43; 10, 51; 10, 67; 10, 68; 10, 84; 10, 100; 10, 110; 10, 254; 10, 263; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:only_one_user; 16, False; 17, if_statement; 17, 18; 17, 24; 18, not_operator; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:users; 23, identifier:list; 24, block; 24, 25; 24, 29; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:only_one_user; 28, True; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:users; 32, list:[users]; 32, 33; 33, identifier:users; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:mapping; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:get_item_concept_mapping; 41, argument_list; 41, 42; 42, identifier:lang; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:current_user_stats; 46, call; 46, 47; 46, 48; 47, identifier:defaultdict; 48, argument_list; 48, 49; 49, lambda; 49, 50; 50, dictionary; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:user_stats_qs; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:UserStat; 58, identifier:objects; 59, identifier:filter; 60, argument_list; 60, 61; 60, 64; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:user__in; 63, identifier:users; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:stat; 66, string:"answer_count"; 67, comment; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:concepts; 71, None; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:user_stats_qs; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:user_stats_qs; 79, identifier:filter; 80, argument_list; 80, 81; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:concept__in; 83, identifier:concepts; 84, for_statement; 84, 85; 84, 86; 84, 87; 85, identifier:user_stat; 86, identifier:user_stats_qs; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 99; 90, subscript; 90, 91; 90, 96; 91, subscript; 91, 92; 91, 93; 92, identifier:current_user_stats; 93, attribute; 93, 94; 93, 95; 94, identifier:user_stat; 95, identifier:user_id; 96, attribute; 96, 97; 96, 98; 97, identifier:user_stat; 98, identifier:concept_id; 99, identifier:user_stat; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:concepts_to_recalculate; 103, call; 103, 104; 103, 105; 104, identifier:defaultdict; 105, argument_list; 105, 106; 106, lambda; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:set; 109, argument_list; 110, for_statement; 110, 111; 110, 115; 110, 140; 111, pattern_list; 111, 112; 111, 113; 111, 114; 112, identifier:user; 113, identifier:item; 114, identifier:time; 115, call; 115, 116; 115, 135; 116, attribute; 116, 117; 116, 134; 117, call; 117, 118; 117, 131; 118, attribute; 118, 119; 118, 129; 118, 130; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:Answer; 123, identifier:objects; 124, identifier:filter; 125, argument_list; 125, 126; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:user__in; 128, identifier:users; 129, line_continuation:\; 130, identifier:values_list; 131, argument_list; 131, 132; 131, 133; 132, string:"user_id"; 133, string:"item"; 134, identifier:annotate; 135, argument_list; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:Max; 138, argument_list; 138, 139; 139, string:"time"; 140, block; 140, 141; 140, 149; 140, 160; 140, 171; 141, if_statement; 141, 142; 141, 145; 141, 146; 142, comparison_operator:not; 142, 143; 142, 144; 143, identifier:item; 144, identifier:mapping; 145, comment; 146, block; 146, 147; 146, 148; 147, continue_statement; 148, comment; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:time_expiration_lower_bound; 152, call; 152, 153; 152, 154; 153, identifier:get_config; 154, argument_list; 154, 155; 154, 156; 154, 157; 155, string:'proso_models'; 156, string:'knowledge_overview.time_shift_hours'; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:default; 159, integer:4; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:time_expiration_factor; 163, call; 163, 164; 163, 165; 164, identifier:get_config; 165, argument_list; 165, 166; 165, 167; 165, 168; 166, string:'proso_models'; 167, string:'knowledge_overview.time_expiration_factor'; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:default; 170, integer:2; 171, for_statement; 171, 172; 171, 173; 171, 176; 172, identifier:concept; 173, subscript; 173, 174; 173, 175; 174, identifier:mapping; 175, identifier:item; 176, block; 176, 177; 176, 219; 177, if_statement; 177, 178; 177, 198; 178, boolean_operator:and; 178, 179; 178, 188; 178, 189; 179, boolean_operator:and; 179, 180; 179, 183; 180, comparison_operator:in; 180, 181; 180, 182; 181, identifier:user; 182, identifier:current_user_stats; 183, comparison_operator:in; 183, 184; 183, 185; 184, identifier:concept; 185, subscript; 185, 186; 185, 187; 186, identifier:current_user_stats; 187, identifier:user; 188, line_continuation:\; 189, comparison_operator:>; 189, 190; 189, 197; 190, attribute; 190, 191; 190, 196; 191, subscript; 191, 192; 191, 195; 192, subscript; 192, 193; 192, 194; 193, identifier:current_user_stats; 194, identifier:user; 195, identifier:concept; 196, identifier:time; 197, identifier:time; 198, block; 198, 199; 199, if_statement; 199, 200; 199, 216; 200, not_operator; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:has_time_expired; 205, argument_list; 205, 206; 205, 213; 205, 214; 205, 215; 206, attribute; 206, 207; 206, 212; 207, subscript; 207, 208; 207, 211; 208, subscript; 208, 209; 208, 210; 209, identifier:current_user_stats; 210, identifier:user; 211, identifier:concept; 212, identifier:time; 213, identifier:time; 214, identifier:time_expiration_lower_bound; 215, identifier:time_expiration_factor; 216, block; 216, 217; 216, 218; 217, continue_statement; 218, comment; 219, if_statement; 219, 220; 219, 244; 220, boolean_operator:or; 220, 221; 220, 224; 221, comparison_operator:is; 221, 222; 221, 223; 222, identifier:concepts; 223, None; 224, comparison_operator:in; 224, 225; 224, 226; 225, identifier:concept; 226, parenthesized_expression; 226, 227; 227, conditional_expression:if; 227, 228; 227, 235; 227, 243; 228, list_comprehension; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:c; 231, identifier:pk; 232, for_in_clause; 232, 233; 232, 234; 233, identifier:c; 234, identifier:concepts; 235, comparison_operator:==; 235, 236; 235, 242; 236, call; 236, 237; 236, 238; 237, identifier:type; 238, argument_list; 238, 239; 239, subscript; 239, 240; 239, 241; 240, identifier:concepts; 241, integer:0; 242, identifier:Concept; 243, identifier:Concept; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 252; 247, attribute; 247, 248; 247, 251; 248, subscript; 248, 249; 248, 250; 249, identifier:concepts_to_recalculate; 250, identifier:user; 251, identifier:add; 252, argument_list; 252, 253; 253, identifier:concept; 254, if_statement; 254, 255; 254, 256; 255, identifier:only_one_user; 256, block; 256, 257; 257, return_statement; 257, 258; 258, subscript; 258, 259; 258, 260; 259, identifier:concepts_to_recalculate; 260, subscript; 260, 261; 260, 262; 261, identifier:users; 262, integer:0; 263, return_statement; 263, 264; 264, identifier:concepts_to_recalculate | def get_concepts_to_recalculate(self, users, lang, concepts=None):
"""
Get concept which have same changes and have to be recalculated
Args:
users (list of users or user): users whose user stats we are interesting in
lang (str): language of used concepts
concepts (Optional[list of concepts]): list of primary keys of concepts or concepts
Defaults to None meaning all concepts.
Returns:
dict: user -> set of concepts (int) - in case of list of users
list of stats (str) - in case of one user
"""
only_one_user = False
if not isinstance(users, list):
only_one_user = True
users = [users]
mapping = self.get_item_concept_mapping(lang)
current_user_stats = defaultdict(lambda: {})
user_stats_qs = UserStat.objects.filter(user__in=users, stat="answer_count") # we need only one type
if concepts is not None:
user_stats_qs = user_stats_qs.filter(concept__in=concepts)
for user_stat in user_stats_qs:
current_user_stats[user_stat.user_id][user_stat.concept_id] = user_stat
concepts_to_recalculate = defaultdict(lambda: set())
for user, item, time in Answer.objects.filter(user__in=users)\
.values_list("user_id", "item").annotate(Max("time")):
if item not in mapping:
# in reality this should by corner case, so it is efficient to not filter Answers
continue # item is not in concept
time_expiration_lower_bound = get_config('proso_models', 'knowledge_overview.time_shift_hours', default=4)
time_expiration_factor = get_config('proso_models', 'knowledge_overview.time_expiration_factor', default=2)
for concept in mapping[item]:
if user in current_user_stats and concept in current_user_stats[user] \
and current_user_stats[user][concept].time > time:
if not self.has_time_expired(current_user_stats[user][concept].time, time, time_expiration_lower_bound, time_expiration_factor):
continue # cache is up to date
if concepts is None or concept in ([c.pk for c in concepts] if type(concepts[0]) == Concept else Concept):
concepts_to_recalculate[user].add(concept)
if only_one_user:
return concepts_to_recalculate[users[0]]
return concepts_to_recalculate |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:recalculate_concepts; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:concepts; 6, default_parameter; 6, 7; 6, 8; 7, identifier:lang; 8, None; 9, block; 9, 10; 9, 12; 9, 21; 9, 73; 9, 79; 9, 85; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 19; 13, comparison_operator:==; 13, 14; 13, 18; 14, call; 14, 15; 14, 16; 15, identifier:len; 16, argument_list; 16, 17; 17, identifier:concepts; 18, integer:0; 19, block; 19, 20; 20, return_statement; 21, if_statement; 21, 22; 21, 25; 21, 58; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:lang; 24, None; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:items; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:Concept; 33, identifier:objects; 34, identifier:get_concept_item_mapping; 35, argument_list; 35, 36; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:concepts; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:Concept; 42, identifier:objects; 43, identifier:filter; 44, argument_list; 44, 45; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:pk__in; 47, call; 47, 48; 47, 49; 48, identifier:set; 49, argument_list; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:flatten; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:concepts; 56, identifier:values; 57, argument_list; 58, else_clause; 58, 59; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:items; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:Concept; 67, identifier:objects; 68, identifier:get_concept_item_mapping; 69, argument_list; 69, 70; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:lang; 72, identifier:lang; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:environment; 76, call; 76, 77; 76, 78; 77, identifier:get_environment; 78, argument_list; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:mastery_threshold; 82, call; 82, 83; 82, 84; 83, identifier:get_mastery_trashold; 84, argument_list; 85, for_statement; 85, 86; 85, 89; 85, 94; 86, pattern_list; 86, 87; 86, 88; 87, identifier:user; 88, identifier:concepts; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:concepts; 92, identifier:items; 93, argument_list; 94, block; 94, 95; 94, 114; 94, 124; 94, 134; 94, 162; 94, 166; 94, 172; 94, 387; 94, 398; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:all_items; 98, call; 98, 99; 98, 100; 99, identifier:list; 100, argument_list; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:set; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:flatten; 106, argument_list; 106, 107; 107, list_comprehension; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:items; 110, identifier:c; 111, for_in_clause; 111, 112; 111, 113; 112, identifier:c; 113, identifier:concepts; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:answer_counts; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:environment; 120, identifier:number_of_answers_more_items; 121, argument_list; 121, 122; 121, 123; 122, identifier:all_items; 123, identifier:user; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:correct_answer_counts; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:environment; 130, identifier:number_of_correct_answers_more_items; 131, argument_list; 131, 132; 131, 133; 132, identifier:all_items; 133, identifier:user; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:predictions; 137, call; 137, 138; 137, 139; 138, identifier:dict; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:list; 142, argument_list; 142, 143; 143, call; 143, 144; 143, 145; 144, identifier:zip; 145, argument_list; 145, 146; 145, 147; 146, identifier:all_items; 147, call; 147, 148; 147, 153; 148, attribute; 148, 149; 148, 152; 149, call; 149, 150; 149, 151; 150, identifier:get_predictive_model; 151, argument_list; 152, identifier:predict_more_items; 153, argument_list; 153, 154; 153, 155; 153, 156; 153, 157; 154, identifier:environment; 155, identifier:user; 156, identifier:all_items; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:time; 159, call; 159, 160; 159, 161; 160, identifier:get_time_for_knowledge_overview; 161, argument_list; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:new_user_stats; 165, list:[]; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:stats_to_delete_condition; 169, call; 169, 170; 169, 171; 170, identifier:Q; 171, argument_list; 172, for_statement; 172, 173; 172, 174; 172, 175; 173, identifier:concept; 174, identifier:concepts; 175, block; 175, 176; 175, 223; 175, 344; 175, 356; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:answer_aggregates; 179, call; 179, 180; 179, 197; 180, attribute; 180, 181; 180, 196; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:Answer; 185, identifier:objects; 186, identifier:filter; 187, argument_list; 187, 188; 187, 191; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:user; 190, identifier:user; 191, keyword_argument; 191, 192; 191, 193; 192, identifier:item__in; 193, subscript; 193, 194; 193, 195; 194, identifier:items; 195, identifier:concept; 196, identifier:aggregate; 197, argument_list; 197, 198; 197, 204; 197, 211; 197, 217; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:time_spent; 200, call; 200, 201; 200, 202; 201, identifier:Sum; 202, argument_list; 202, 203; 203, string:"response_time"; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:sessions; 206, call; 206, 207; 206, 208; 207, identifier:Count; 208, argument_list; 208, 209; 208, 210; 209, string:"session"; 210, True; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:time_first; 213, call; 213, 214; 213, 215; 214, identifier:Min; 215, argument_list; 215, 216; 216, string:"time"; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:time_last; 219, call; 219, 220; 219, 221; 220, identifier:Max; 221, argument_list; 221, 222; 222, string:"time"; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:stats; 226, dictionary; 226, 227; 226, 240; 226, 253; 226, 261; 226, 277; 226, 293; 226, 314; 226, 321; 226, 326; 226, 335; 227, pair; 227, 228; 227, 229; 228, string:"answer_count"; 229, call; 229, 230; 229, 231; 230, identifier:sum; 231, generator_expression; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:answer_counts; 234, identifier:i; 235, for_in_clause; 235, 236; 235, 237; 236, identifier:i; 237, subscript; 237, 238; 237, 239; 238, identifier:items; 239, identifier:concept; 240, pair; 240, 241; 240, 242; 241, string:"correct_answer_count"; 242, call; 242, 243; 242, 244; 243, identifier:sum; 244, generator_expression; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:correct_answer_counts; 247, identifier:i; 248, for_in_clause; 248, 249; 248, 250; 249, identifier:i; 250, subscript; 250, 251; 250, 252; 251, identifier:items; 252, identifier:concept; 253, pair; 253, 254; 253, 255; 254, string:"item_count"; 255, call; 255, 256; 255, 257; 256, identifier:len; 257, argument_list; 257, 258; 258, subscript; 258, 259; 258, 260; 259, identifier:items; 260, identifier:concept; 261, pair; 261, 262; 261, 263; 262, string:"practiced_items_count"; 263, call; 263, 264; 263, 265; 264, identifier:sum; 265, argument_list; 265, 266; 266, list_comprehension; 266, 267; 266, 272; 267, comparison_operator:>; 267, 268; 267, 271; 268, subscript; 268, 269; 268, 270; 269, identifier:answer_counts; 270, identifier:i; 271, integer:0; 272, for_in_clause; 272, 273; 272, 274; 273, identifier:i; 274, subscript; 274, 275; 274, 276; 275, identifier:items; 276, identifier:concept; 277, pair; 277, 278; 277, 279; 278, string:"mastered_items_count"; 279, call; 279, 280; 279, 281; 280, identifier:sum; 281, argument_list; 281, 282; 282, list_comprehension; 282, 283; 282, 288; 283, comparison_operator:>=; 283, 284; 283, 287; 284, subscript; 284, 285; 284, 286; 285, identifier:predictions; 286, identifier:i; 287, identifier:mastery_threshold; 288, for_in_clause; 288, 289; 288, 290; 289, identifier:i; 290, subscript; 290, 291; 290, 292; 291, identifier:items; 292, identifier:concept; 293, pair; 293, 294; 293, 295; 294, string:"prediction"; 295, binary_operator:/; 295, 296; 295, 308; 296, call; 296, 297; 296, 298; 297, identifier:sum; 298, argument_list; 298, 299; 299, list_comprehension; 299, 300; 299, 303; 300, subscript; 300, 301; 300, 302; 301, identifier:predictions; 302, identifier:i; 303, for_in_clause; 303, 304; 303, 305; 304, identifier:i; 305, subscript; 305, 306; 305, 307; 306, identifier:items; 307, identifier:concept; 308, call; 308, 309; 308, 310; 309, identifier:len; 310, argument_list; 310, 311; 311, subscript; 311, 312; 311, 313; 312, identifier:items; 313, identifier:concept; 314, pair; 314, 315; 314, 316; 315, string:"time_spent"; 316, binary_operator:/; 316, 317; 316, 320; 317, subscript; 317, 318; 317, 319; 318, identifier:answer_aggregates; 319, string:"time_spent"; 320, integer:1000; 321, pair; 321, 322; 321, 323; 322, string:"session_count"; 323, subscript; 323, 324; 323, 325; 324, identifier:answer_aggregates; 325, string:"sessions"; 326, pair; 326, 327; 326, 328; 327, string:"time_first"; 328, call; 328, 329; 328, 334; 329, attribute; 329, 330; 329, 333; 330, subscript; 330, 331; 330, 332; 331, identifier:answer_aggregates; 332, string:"time_first"; 333, identifier:timestamp; 334, argument_list; 335, pair; 335, 336; 335, 337; 336, string:"time_last"; 337, call; 337, 338; 337, 343; 338, attribute; 338, 339; 338, 342; 339, subscript; 339, 340; 339, 341; 340, identifier:answer_aggregates; 341, string:"time_last"; 342, identifier:timestamp; 343, argument_list; 344, expression_statement; 344, 345; 345, augmented_assignment:|=; 345, 346; 345, 347; 346, identifier:stats_to_delete_condition; 347, call; 347, 348; 347, 349; 348, identifier:Q; 349, argument_list; 349, 350; 349, 353; 350, keyword_argument; 350, 351; 350, 352; 351, identifier:user; 352, identifier:user; 353, keyword_argument; 353, 354; 353, 355; 354, identifier:concept; 355, identifier:concept; 356, for_statement; 356, 357; 356, 360; 356, 365; 357, pattern_list; 357, 358; 357, 359; 358, identifier:stat_name; 359, identifier:value; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:stats; 363, identifier:items; 364, argument_list; 365, block; 365, 366; 366, expression_statement; 366, 367; 367, call; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:new_user_stats; 370, identifier:append; 371, argument_list; 371, 372; 372, call; 372, 373; 372, 374; 373, identifier:UserStat; 374, argument_list; 374, 375; 374, 378; 374, 381; 374, 384; 375, keyword_argument; 375, 376; 375, 377; 376, identifier:user_id; 377, identifier:user; 378, keyword_argument; 378, 379; 378, 380; 379, identifier:concept_id; 380, identifier:concept; 381, keyword_argument; 381, 382; 381, 383; 382, identifier:stat; 383, identifier:stat_name; 384, keyword_argument; 384, 385; 384, 386; 385, identifier:value; 386, identifier:value; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 397; 389, attribute; 389, 390; 389, 396; 390, call; 390, 391; 390, 394; 391, attribute; 391, 392; 391, 393; 392, identifier:self; 393, identifier:filter; 394, argument_list; 394, 395; 395, identifier:stats_to_delete_condition; 396, identifier:delete; 397, argument_list; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:self; 402, identifier:bulk_create; 403, argument_list; 403, 404; 404, identifier:new_user_stats | def recalculate_concepts(self, concepts, lang=None):
"""
Recalculated given concepts for given users
Args:
concepts (dict): user id (int -> set of concepts to recalculate)
lang(Optional[str]): language used to get items in all concepts (cached).
Defaults to None, in that case are get items only in used concepts
"""
if len(concepts) == 0:
return
if lang is None:
items = Concept.objects.get_concept_item_mapping(concepts=Concept.objects.filter(pk__in=set(flatten(concepts.values()))))
else:
items = Concept.objects.get_concept_item_mapping(lang=lang)
environment = get_environment()
mastery_threshold = get_mastery_trashold()
for user, concepts in concepts.items():
all_items = list(set(flatten([items[c] for c in concepts])))
answer_counts = environment.number_of_answers_more_items(all_items, user)
correct_answer_counts = environment.number_of_correct_answers_more_items(all_items, user)
predictions = dict(list(zip(all_items, get_predictive_model().
predict_more_items(environment, user, all_items, time=get_time_for_knowledge_overview()))))
new_user_stats = []
stats_to_delete_condition = Q()
for concept in concepts:
answer_aggregates = Answer.objects.filter(user=user, item__in=items[concept]).aggregate(
time_spent=Sum("response_time"),
sessions=Count("session", True),
time_first=Min("time"),
time_last=Max("time"),
)
stats = {
"answer_count": sum(answer_counts[i] for i in items[concept]),
"correct_answer_count": sum(correct_answer_counts[i] for i in items[concept]),
"item_count": len(items[concept]),
"practiced_items_count": sum([answer_counts[i] > 0 for i in items[concept]]),
"mastered_items_count": sum([predictions[i] >= mastery_threshold for i in items[concept]]),
"prediction": sum([predictions[i] for i in items[concept]]) / len(items[concept]),
"time_spent": answer_aggregates["time_spent"] / 1000,
"session_count": answer_aggregates["sessions"],
"time_first": answer_aggregates["time_first"].timestamp(),
"time_last": answer_aggregates["time_last"].timestamp(),
}
stats_to_delete_condition |= Q(user=user, concept=concept)
for stat_name, value in stats.items():
new_user_stats.append(UserStat(user_id=user, concept_id=concept, stat=stat_name, value=value))
self.filter(stats_to_delete_condition).delete()
self.bulk_create(new_user_stats) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:get_user_stats; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:users; 6, default_parameter; 6, 7; 6, 8; 7, identifier:lang; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:concepts; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:since; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:recalculate; 17, True; 18, block; 18, 19; 18, 21; 18, 25; 18, 42; 18, 114; 18, 132; 18, 148; 18, 164; 18, 180; 18, 192; 18, 216; 18, 239; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:only_one_user; 24, False; 25, if_statement; 25, 26; 25, 32; 26, not_operator; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 31; 30, identifier:users; 31, identifier:list; 32, block; 32, 33; 32, 38; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:users; 36, list:[users]; 36, 37; 37, identifier:users; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:only_one_user; 41, True; 42, if_statement; 42, 43; 42, 44; 43, identifier:recalculate; 44, block; 44, 45; 44, 55; 44, 61; 44, 74; 44, 87; 44, 93; 44, 101; 45, if_statement; 45, 46; 45, 49; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:lang; 48, None; 49, block; 49, 50; 50, raise_statement; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:ValueError; 53, argument_list; 53, 54; 54, string:'Recalculation without lang is not supported.'; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:time_start; 58, call; 58, 59; 58, 60; 59, identifier:time_lib; 60, argument_list; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:concepts_to_recalculate; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:Concept; 68, identifier:objects; 69, identifier:get_concepts_to_recalculate; 70, argument_list; 70, 71; 70, 72; 70, 73; 71, identifier:users; 72, identifier:lang; 73, identifier:concepts; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:LOGGER; 78, identifier:debug; 79, argument_list; 79, 80; 79, 81; 80, string:"user_stats - getting identifying concepts to recalculate: %ss"; 81, parenthesized_expression; 81, 82; 82, binary_operator:-; 82, 83; 82, 86; 83, call; 83, 84; 83, 85; 84, identifier:time_lib; 85, argument_list; 86, identifier:time_start; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:time_start; 90, call; 90, 91; 90, 92; 91, identifier:time_lib; 92, argument_list; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:recalculate_concepts; 98, argument_list; 98, 99; 98, 100; 99, identifier:concepts_to_recalculate; 100, identifier:lang; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:LOGGER; 105, identifier:debug; 106, argument_list; 106, 107; 106, 108; 107, string:"user_stats - recalculating concepts: %ss"; 108, parenthesized_expression; 108, 109; 109, binary_operator:-; 109, 110; 109, 113; 110, call; 110, 111; 110, 112; 111, identifier:time_lib; 112, argument_list; 113, identifier:time_start; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:qs; 117, call; 117, 118; 117, 125; 118, attribute; 118, 119; 118, 124; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:prepare_related; 123, argument_list; 124, identifier:filter; 125, argument_list; 125, 126; 125, 129; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:user__in; 128, identifier:users; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:concept__active; 131, True; 132, if_statement; 132, 133; 132, 136; 133, comparison_operator:is; 133, 134; 133, 135; 134, identifier:concepts; 135, None; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:qs; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:qs; 143, identifier:filter; 144, argument_list; 144, 145; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:concept__in; 147, identifier:concepts; 148, if_statement; 148, 149; 148, 152; 149, comparison_operator:is; 149, 150; 149, 151; 150, identifier:lang; 151, None; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:qs; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:qs; 159, identifier:filter; 160, argument_list; 160, 161; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:concept__lang; 163, identifier:lang; 164, if_statement; 164, 165; 164, 168; 165, comparison_operator:is; 165, 166; 165, 167; 166, identifier:since; 167, None; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:qs; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:qs; 175, identifier:filter; 176, argument_list; 176, 177; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:time__gte; 179, identifier:since; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:data; 183, call; 183, 184; 183, 185; 184, identifier:defaultdict; 185, argument_list; 185, 186; 186, lambda; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:defaultdict; 189, argument_list; 189, 190; 190, lambda; 190, 191; 191, dictionary; 192, for_statement; 192, 193; 192, 194; 192, 195; 193, identifier:user_stat; 194, identifier:qs; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 213; 198, subscript; 198, 199; 198, 210; 199, subscript; 199, 200; 199, 205; 200, subscript; 200, 201; 200, 202; 201, identifier:data; 202, attribute; 202, 203; 202, 204; 203, identifier:user_stat; 204, identifier:user_id; 205, attribute; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:user_stat; 208, identifier:concept; 209, identifier:identifier; 210, attribute; 210, 211; 210, 212; 211, identifier:user_stat; 212, identifier:stat; 213, attribute; 213, 214; 213, 215; 214, identifier:user_stat; 215, identifier:value; 216, if_statement; 216, 217; 216, 218; 217, identifier:only_one_user; 218, block; 218, 219; 219, return_statement; 219, 220; 220, subscript; 220, 221; 220, 222; 221, identifier:data; 222, conditional_expression:if; 222, 223; 222, 228; 222, 236; 223, attribute; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:users; 226, integer:0; 227, identifier:pk; 228, comparison_operator:==; 228, 229; 228, 235; 229, call; 229, 230; 229, 231; 230, identifier:type; 231, argument_list; 231, 232; 232, subscript; 232, 233; 232, 234; 233, identifier:users; 234, integer:0; 235, identifier:User; 236, subscript; 236, 237; 236, 238; 237, identifier:users; 238, integer:0; 239, return_statement; 239, 240; 240, identifier:data | def get_user_stats(self, users, lang=None, concepts=None, since=None, recalculate=True):
"""
Finds all UserStats of given concepts and users.
Recompute UserStats if necessary
Args:
users (Optional[list of users] or [user]): list of primary keys of user or users
Defaults to None meaning all users.
lang (string): use only concepts witch the lang. Defaults to None meaning all languages.
concepts (Optional[list of concepts]): list of primary keys of concepts or concepts
Defaults to None meaning all concepts.
Returns:
dict: user_id -> dict (concept_identifier - > (stat_name -> value)) -- for more users
dict: concept_identifier - > (stat_name -> value) -- for one user
"""
only_one_user = False
if not isinstance(users, list):
users = [users]
only_one_user = True
if recalculate:
if lang is None:
raise ValueError('Recalculation without lang is not supported.')
time_start = time_lib()
concepts_to_recalculate = Concept.objects.get_concepts_to_recalculate(users, lang, concepts)
LOGGER.debug("user_stats - getting identifying concepts to recalculate: %ss", (time_lib() - time_start))
time_start = time_lib()
self.recalculate_concepts(concepts_to_recalculate, lang)
LOGGER.debug("user_stats - recalculating concepts: %ss", (time_lib() - time_start))
qs = self.prepare_related().filter(user__in=users, concept__active=True)
if concepts is not None:
qs = qs.filter(concept__in=concepts)
if lang is not None:
qs = qs.filter(concept__lang=lang)
if since is not None:
qs = qs.filter(time__gte=since)
data = defaultdict(lambda: defaultdict(lambda: {}))
for user_stat in qs:
data[user_stat.user_id][user_stat.concept.identifier][user_stat.stat] = user_stat.value
if only_one_user:
return data[users[0].pk if type(users[0]) == User else users[0]]
return data |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:execute; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:self; 5, identifier:sql; 6, default_parameter; 6, 7; 6, 8; 7, identifier:parameters; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:cursorClass; 11, identifier:DictCursor; 12, default_parameter; 12, 13; 12, 14; 13, identifier:quiet; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:locked; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:do_commit; 20, True; 21, block; 21, 22; 21, 24; 21, 28; 21, 32; 21, 36; 21, 40; 21, 69; 21, 335; 21, 385; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:i; 27, integer:0; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:errcode; 31, integer:0; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:caughte; 35, None; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:cursor; 39, None; 40, if_statement; 40, 41; 40, 60; 40, 61; 41, boolean_operator:or; 41, 42; 41, 51; 42, comparison_operator:!=; 42, 43; 42, 49; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:sql; 46, identifier:find; 47, argument_list; 47, 48; 48, string:";"; 49, unary_operator:-; 49, 50; 50, integer:1; 51, comparison_operator:!=; 51, 52; 51, 58; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:sql; 55, identifier:find; 56, argument_list; 56, 57; 57, string:"\\G"; 58, unary_operator:-; 58, 59; 59, integer:1; 60, comment; 61, block; 61, 62; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:Exception; 65, argument_list; 65, 66; 66, binary_operator:%; 66, 67; 66, 68; 67, string:"The SQL command '%s' contains a semi-colon or \\G. This is a potential SQL injection."; 68, identifier:sql; 69, while_statement; 69, 70; 69, 75; 70, comparison_operator:<; 70, 71; 70, 72; 71, identifier:i; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:numTries; 75, block; 75, 76; 75, 80; 75, 330; 76, expression_statement; 76, 77; 77, augmented_assignment:+=; 77, 78; 77, 79; 78, identifier:i; 79, integer:1; 80, try_statement; 80, 81; 80, 222; 80, 277; 81, block; 81, 82; 81, 97; 81, 104; 81, 114; 81, 132; 81, 156; 81, 167; 81, 182; 81, 190; 81, 208; 81, 214; 81, 220; 82, assert_statement; 82, 83; 83, parenthesized_expression; 83, 84; 84, boolean_operator:or; 84, 85; 84, 90; 85, not_operator; 85, 86; 86, parenthesized_expression; 86, 87; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:connection; 90, not_operator; 90, 91; 91, parenthesized_expression; 91, 92; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:connection; 96, identifier:open; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:_get_connection; 102, argument_list; 102, 103; 103, identifier:cursorClass; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:cursor; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:connection; 112, identifier:cursor; 113, argument_list; 114, if_statement; 114, 115; 114, 116; 115, identifier:locked; 116, block; 116, 117; 116, 126; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:cursor; 121, identifier:execute; 122, argument_list; 122, 123; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:lockstring; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:locked; 131, True; 132, if_statement; 132, 133; 132, 134; 132, 145; 133, identifier:parameters; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:errcode; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:cursor; 141, identifier:execute; 142, argument_list; 142, 143; 142, 144; 143, identifier:sql; 144, identifier:parameters; 145, else_clause; 145, 146; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:errcode; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:cursor; 153, identifier:execute; 154, argument_list; 154, 155; 155, identifier:sql; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:lastrowid; 161, call; 161, 162; 161, 163; 162, identifier:int; 163, argument_list; 163, 164; 164, attribute; 164, 165; 164, 166; 165, identifier:cursor; 166, identifier:lastrowid; 167, if_statement; 167, 168; 167, 173; 168, boolean_operator:and; 168, 169; 168, 170; 169, identifier:do_commit; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:isInnoDB; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 181; 176, attribute; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:connection; 180, identifier:commit; 181, argument_list; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:results; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:cursor; 188, identifier:fetchall; 189, argument_list; 190, if_statement; 190, 191; 190, 192; 191, identifier:locked; 192, block; 192, 193; 192, 202; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:cursor; 197, identifier:execute; 198, argument_list; 198, 199; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:unlockstring; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:locked; 207, False; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:cursor; 212, identifier:close; 213, argument_list; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:_close_connection; 219, argument_list; 220, return_statement; 220, 221; 221, identifier:results; 222, except_clause; 222, 223; 222, 226; 222, 227; 223, attribute; 223, 224; 223, 225; 224, identifier:MySQLdb; 225, identifier:OperationalError; 226, identifier:e; 227, block; 227, 228; 227, 257; 227, 263; 227, 270; 227, 276; 228, if_statement; 228, 229; 228, 230; 229, identifier:cursor; 230, block; 230, 231; 230, 251; 231, if_statement; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:self; 234, identifier:locked; 235, block; 235, 236; 235, 245; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:cursor; 240, identifier:execute; 241, argument_list; 241, 242; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:unlockstring; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:self; 249, identifier:locked; 250, False; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:cursor; 255, identifier:close; 256, argument_list; 257, expression_statement; 257, 258; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:_close_connection; 262, argument_list; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:caughte; 266, call; 266, 267; 266, 268; 267, identifier:str; 268, argument_list; 268, 269; 269, identifier:e; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:errcode; 273, subscript; 273, 274; 273, 275; 274, identifier:e; 275, integer:0; 276, continue_statement; 277, except_clause; 277, 278; 277, 279; 277, 280; 278, identifier:Exception; 279, identifier:e; 280, block; 280, 281; 280, 310; 280, 316; 280, 323; 280, 329; 281, if_statement; 281, 282; 281, 283; 282, identifier:cursor; 283, block; 283, 284; 283, 304; 284, if_statement; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:locked; 288, block; 288, 289; 288, 298; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:cursor; 293, identifier:execute; 294, argument_list; 294, 295; 295, attribute; 295, 296; 295, 297; 296, identifier:self; 297, identifier:unlockstring; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:self; 302, identifier:locked; 303, False; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:cursor; 308, identifier:close; 309, argument_list; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:self; 314, identifier:_close_connection; 315, argument_list; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:caughte; 319, call; 319, 320; 319, 321; 320, identifier:str; 321, argument_list; 321, 322; 322, identifier:e; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:traceback; 327, identifier:print_exc; 328, argument_list; 329, break_statement; 330, expression_statement; 330, 331; 331, call; 331, 332; 331, 333; 332, identifier:sleep; 333, argument_list; 333, 334; 334, float:0.2; 335, if_statement; 335, 336; 335, 338; 336, not_operator; 336, 337; 337, identifier:quiet; 338, block; 338, 339; 338, 361; 338, 377; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 346; 341, attribute; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:sys; 344, identifier:stderr; 345, identifier:write; 346, argument_list; 346, 347; 347, binary_operator:%; 347, 348; 347, 349; 348, string:"\nSQL execution error in query %s at %s:"; 349, tuple; 349, 350; 349, 351; 350, identifier:sql; 351, call; 351, 352; 351, 359; 352, attribute; 352, 353; 352, 358; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:datetime; 356, identifier:now; 357, argument_list; 358, identifier:strftime; 359, argument_list; 359, 360; 360, string:"%Y-%m-%d %H:%M:%S"; 361, expression_statement; 361, 362; 362, call; 362, 363; 362, 368; 363, attribute; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:sys; 366, identifier:stderr; 367, identifier:write; 368, argument_list; 368, 369; 369, binary_operator:%; 369, 370; 369, 371; 370, string:"\nErrorcode/Error: %d - '%s'.\n"; 371, tuple; 371, 372; 371, 373; 372, identifier:errcode; 373, call; 373, 374; 373, 375; 374, identifier:str; 375, argument_list; 375, 376; 376, identifier:caughte; 377, expression_statement; 377, 378; 378, call; 378, 379; 378, 384; 379, attribute; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:sys; 382, identifier:stderr; 383, identifier:flush; 384, argument_list; 385, raise_statement; 385, 386; 386, call; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:MySQLdb; 389, identifier:OperationalError; 390, argument_list; 390, 391; 391, identifier:caughte | def execute(self, sql, parameters = None, cursorClass = DictCursor, quiet = False, locked = False, do_commit = True):
"""Execute SQL query. This uses DictCursor by default."""
i = 0
errcode = 0
caughte = None
cursor = None
if sql.find(";") != -1 or sql.find("\\G") != -1:
# Catches some injections
raise Exception("The SQL command '%s' contains a semi-colon or \\G. This is a potential SQL injection." % sql)
while i < self.numTries:
i += 1
try:
assert(not(self.connection) or not(self.connection.open))
self._get_connection(cursorClass)
cursor = self.connection.cursor()
if locked:
cursor.execute(self.lockstring)
self.locked = True
if parameters:
errcode = cursor.execute(sql, parameters)
else:
errcode = cursor.execute(sql)
self.lastrowid = int(cursor.lastrowid)
if do_commit and self.isInnoDB:
self.connection.commit()
results = cursor.fetchall()
if locked:
cursor.execute(self.unlockstring)
self.locked = False
cursor.close()
self._close_connection()
return results
except MySQLdb.OperationalError, e:
if cursor:
if self.locked:
cursor.execute(self.unlockstring)
self.locked = False
cursor.close()
self._close_connection()
caughte = str(e)
errcode = e[0]
continue
except Exception, e:
if cursor:
if self.locked:
cursor.execute(self.unlockstring)
self.locked = False
cursor.close()
self._close_connection()
caughte = str(e)
traceback.print_exc()
break
sleep(0.2)
if not quiet:
sys.stderr.write("\nSQL execution error in query %s at %s:" % (sql, datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
sys.stderr.write("\nErrorcode/Error: %d - '%s'.\n" % (errcode, str(caughte)))
sys.stderr.flush()
raise MySQLdb.OperationalError(caughte) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:create_tasks; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 8; 3, 11; 4, identifier:task_coro; 5, identifier:addrs; 6, list_splat_pattern; 6, 7; 7, identifier:args; 8, default_parameter; 8, 9; 8, 10; 9, identifier:flatten; 10, True; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kwargs; 13, block; 13, 14; 13, 16; 13, 20; 13, 47; 14, expression_statement; 14, 15; 15, string:'''Create and schedule a set of asynchronous tasks.
The function creates the tasks using a given list of agent addresses and
wraps each of them in :func:`asyncio.ensure_future`. The ``*args`` and
``**kwargs`` are passed down to :func:`task_coro` when creating tasks for
each address in :attr:`addrs`.
Usage example for a method in a class derived from
:class:`~creamas.mp.MultiEnvironment`::
async def my_method(self, *args, **kwargs):
async def task(addr, *args, **kwargs):
r_manager = await self.env.connect(addr)
return await r_manager.my_method(*args, **kwargs)
return await util.create_tasks(task, self.addrs, *args, **kwargs)
:param task_coro:
Coroutine which is used for each address in :attr:`addrs`. The
coroutine should accept an agent address as the first parameter.
:param list addrs:
A list of agent addresses used as the first parameters of
:func:`task_coro`.
:param bool flatten:
If ``True`` the returned results are flattened into one list if the
tasks return iterable objects. The parameter does nothing if all the
results are not iterable.
:returns:
An awaitable coroutine which returns the results of tasks as a list or
as a flattened list
'''; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:tasks; 19, list:[]; 20, for_statement; 20, 21; 20, 22; 20, 23; 21, identifier:agent_addr; 22, identifier:addrs; 23, block; 23, 24; 23, 40; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:task; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:asyncio; 30, identifier:ensure_future; 31, argument_list; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:task_coro; 34, argument_list; 34, 35; 34, 36; 34, 38; 35, identifier:agent_addr; 36, list_splat; 36, 37; 37, identifier:args; 38, dictionary_splat; 38, 39; 39, identifier:kwargs; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:tasks; 44, identifier:append; 45, argument_list; 45, 46; 46, identifier:task; 47, return_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:wait_tasks; 50, argument_list; 50, 51; 50, 52; 51, identifier:tasks; 52, identifier:flatten | def create_tasks(task_coro, addrs, *args, flatten=True, **kwargs):
'''Create and schedule a set of asynchronous tasks.
The function creates the tasks using a given list of agent addresses and
wraps each of them in :func:`asyncio.ensure_future`. The ``*args`` and
``**kwargs`` are passed down to :func:`task_coro` when creating tasks for
each address in :attr:`addrs`.
Usage example for a method in a class derived from
:class:`~creamas.mp.MultiEnvironment`::
async def my_method(self, *args, **kwargs):
async def task(addr, *args, **kwargs):
r_manager = await self.env.connect(addr)
return await r_manager.my_method(*args, **kwargs)
return await util.create_tasks(task, self.addrs, *args, **kwargs)
:param task_coro:
Coroutine which is used for each address in :attr:`addrs`. The
coroutine should accept an agent address as the first parameter.
:param list addrs:
A list of agent addresses used as the first parameters of
:func:`task_coro`.
:param bool flatten:
If ``True`` the returned results are flattened into one list if the
tasks return iterable objects. The parameter does nothing if all the
results are not iterable.
:returns:
An awaitable coroutine which returns the results of tasks as a list or
as a flattened list
'''
tasks = []
for agent_addr in addrs:
task = asyncio.ensure_future(task_coro(agent_addr, *args, **kwargs))
tasks.append(task)
return wait_tasks(tasks, flatten) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:write; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:models; 5, default_parameter; 5, 6; 5, 7; 6, identifier:out; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:base; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:propertybase; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:shorteners; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:logger; 19, identifier:logging; 20, block; 20, 21; 20, 23; 20, 27; 20, 28; 20, 41; 20, 47; 20, 55; 20, 62; 20, 78; 20, 93; 20, 94; 20, 95; 20, 102; 20, 108; 20, 109; 20, 123; 20, 222; 21, expression_statement; 21, 22; 22, string:'''
models - input Versa models from which output is generated. Must be a sequence
object, not an iterator
'''; 23, assert_statement; 23, 24; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:out; 26, None; 27, comment; 28, if_statement; 28, 29; 28, 35; 29, not_operator; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:isinstance; 32, argument_list; 32, 33; 32, 34; 33, identifier:models; 34, identifier:list; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:models; 39, list:[models]; 39, 40; 40, identifier:models; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:shorteners; 44, boolean_operator:or; 44, 45; 44, 46; 45, identifier:shorteners; 46, dictionary; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:all_propertybase; 50, conditional_expression:if; 50, 51; 50, 53; 50, 54; 51, list:[propertybase]; 51, 52; 52, identifier:propertybase; 53, identifier:propertybase; 54, list:[]; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:all_propertybase; 59, identifier:append; 60, argument_list; 60, 61; 61, identifier:VERSA_BASEIRI; 62, if_statement; 62, 63; 62, 70; 63, call; 63, 64; 63, 65; 64, identifier:any; 65, argument_list; 65, 66; 66, tuple; 66, 67; 66, 68; 66, 69; 67, identifier:base; 68, identifier:propertybase; 69, identifier:shorteners; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:out; 75, identifier:write; 76, argument_list; 76, 77; 77, string:'# @docheader\n\n* @iri:\n'; 78, if_statement; 78, 79; 78, 80; 79, identifier:base; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:out; 85, identifier:write; 86, argument_list; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, string:' * @base: {0}'; 90, identifier:format; 91, argument_list; 91, 92; 92, identifier:base; 93, comment; 94, comment; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:out; 99, identifier:write; 100, argument_list; 100, 101; 101, string:'\n\n'; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:origin_space; 105, call; 105, 106; 105, 107; 106, identifier:set; 107, argument_list; 108, comment; 109, for_statement; 109, 110; 109, 111; 109, 112; 110, identifier:m; 111, identifier:models; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:origin_space; 117, identifier:update; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:all_origins; 121, argument_list; 121, 122; 122, identifier:m; 123, for_statement; 123, 124; 123, 125; 123, 126; 124, identifier:o; 125, identifier:origin_space; 126, block; 126, 127; 126, 139; 126, 215; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:out; 131, identifier:write; 132, argument_list; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, string:'# {0}\n\n'; 136, identifier:format; 137, argument_list; 137, 138; 138, identifier:o; 139, for_statement; 139, 140; 139, 145; 139, 151; 140, pattern_list; 140, 141; 140, 142; 140, 143; 140, 144; 141, identifier:o_; 142, identifier:r; 143, identifier:t; 144, identifier:a; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:m; 148, identifier:match; 149, argument_list; 149, 150; 150, identifier:o; 151, block; 151, 152; 151, 160; 151, 165; 151, 181; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:abbr_r; 155, call; 155, 156; 155, 157; 156, identifier:abbreviate; 157, argument_list; 157, 158; 157, 159; 158, identifier:r; 159, identifier:all_propertybase; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:value_format; 163, argument_list; 163, 164; 164, identifier:t; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:out; 169, identifier:write; 170, argument_list; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, string:'* {0}: {1}\n'; 174, identifier:format; 175, argument_list; 175, 176; 175, 177; 176, identifier:abbr_r; 177, call; 177, 178; 177, 179; 178, identifier:value_format; 179, argument_list; 179, 180; 180, identifier:t; 181, for_statement; 181, 182; 181, 185; 181, 190; 182, pattern_list; 182, 183; 182, 184; 183, identifier:k; 184, identifier:v; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:a; 188, identifier:items; 189, argument_list; 190, block; 190, 191; 190, 199; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:abbr_k; 194, call; 194, 195; 194, 196; 195, identifier:abbreviate; 196, argument_list; 196, 197; 196, 198; 197, identifier:k; 198, identifier:all_propertybase; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:out; 203, identifier:write; 204, argument_list; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, string:' * {0}: {1}\n'; 208, identifier:format; 209, argument_list; 209, 210; 209, 211; 210, identifier:k; 211, call; 211, 212; 211, 213; 212, identifier:value_format; 213, argument_list; 213, 214; 214, identifier:v; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:out; 219, identifier:write; 220, argument_list; 220, 221; 221, string:'\n'; 222, return_statement | def write(models, out=None, base=None, propertybase=None, shorteners=None, logger=logging):
'''
models - input Versa models from which output is generated. Must be a sequence
object, not an iterator
'''
assert out is not None #Output stream required
if not isinstance(models, list): models = [models]
shorteners = shorteners or {}
all_propertybase = [propertybase] if propertybase else []
all_propertybase.append(VERSA_BASEIRI)
if any((base, propertybase, shorteners)):
out.write('# @docheader\n\n* @iri:\n')
if base:
out.write(' * @base: {0}'.format(base))
#for k, v in shorteners:
# out.write(' * @base: {0}'.format(base))
out.write('\n\n')
origin_space = set()
#base_out = models[0].base
for m in models:
origin_space.update(all_origins(m))
for o in origin_space:
out.write('# {0}\n\n'.format(o))
for o_, r, t, a in m.match(o):
abbr_r = abbreviate(r, all_propertybase)
value_format(t)
out.write('* {0}: {1}\n'.format(abbr_r, value_format(t)))
for k, v in a.items():
abbr_k = abbreviate(k, all_propertybase)
out.write(' * {0}: {1}\n'.format(k, value_format(v)))
out.write('\n')
return |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:register_extensions; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, try_statement; 8, 9; 8, 164; 9, block; 9, 10; 10, for_statement; 10, 11; 10, 14; 10, 23; 11, pattern_list; 11, 12; 11, 13; 12, identifier:extension; 13, identifier:config; 14, call; 14, 15; 14, 22; 15, attribute; 15, 16; 15, 21; 16, subscript; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:config; 20, string:'extensions'; 21, identifier:items; 22, argument_list; 23, block; 23, 24; 23, 28; 23, 29; 23, 38; 23, 39; 23, 67; 23, 74; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:extension_bstr; 27, string:''; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:extension_pieces; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:extension; 35, identifier:split; 36, argument_list; 36, 37; 37, string:'.'; 38, comment; 39, if_statement; 39, 40; 39, 46; 39, 56; 40, comparison_operator:>; 40, 41; 40, 45; 41, call; 41, 42; 41, 43; 42, identifier:len; 43, argument_list; 43, 44; 44, identifier:extension_pieces; 45, integer:1; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:extension_bstr; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, string:'.'; 53, identifier:join; 54, argument_list; 54, 55; 55, identifier:extension_pieces; 56, else_clause; 56, 57; 56, 58; 57, comment; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:extension_bstr; 62, binary_operator:%; 62, 63; 62, 64; 63, string:'glim_extensions.%s'; 64, subscript; 64, 65; 64, 66; 65, identifier:extension_pieces; 66, integer:0; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:extension_module; 70, call; 70, 71; 70, 72; 71, identifier:import_module; 72, argument_list; 72, 73; 73, identifier:extension_bstr; 74, if_statement; 74, 75; 74, 76; 74, 153; 75, identifier:extension_module; 76, block; 76, 77; 76, 85; 76, 95; 76, 103; 76, 113; 76, 129; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:extension_startstr; 80, binary_operator:%; 80, 81; 80, 82; 81, string:'%s.%s'; 82, tuple; 82, 83; 82, 84; 83, identifier:extension_bstr; 84, string:'start'; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:extension_start; 88, call; 88, 89; 88, 90; 89, identifier:import_module; 90, argument_list; 90, 91; 90, 92; 91, identifier:extension_startstr; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:pass_errors; 94, True; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:extension_cmdsstr; 98, binary_operator:%; 98, 99; 98, 100; 99, string:'%s.%s'; 100, tuple; 100, 101; 100, 102; 101, identifier:extension_bstr; 102, string:'commands'; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:extension_cmds; 106, call; 106, 107; 106, 108; 107, identifier:import_module; 108, argument_list; 108, 109; 108, 110; 109, identifier:extension_cmdsstr; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:pass_errors; 112, True; 113, if_statement; 113, 114; 113, 117; 114, comparison_operator:is; 114, 115; 114, 116; 115, identifier:extension_start; 116, None; 117, block; 117, 118; 117, 124; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:before; 121, attribute; 121, 122; 121, 123; 122, identifier:extension_start; 123, identifier:before; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:before; 127, argument_list; 127, 128; 128, identifier:config; 129, if_statement; 129, 130; 129, 133; 130, comparison_operator:is; 130, 131; 130, 132; 131, identifier:extension_cmds; 132, None; 133, block; 133, 134; 134, if_statement; 134, 135; 134, 140; 135, comparison_operator:is; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:commandadapter; 139, None; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:commandadapter; 147, identifier:register_extension; 148, argument_list; 148, 149; 148, 150; 149, identifier:extension_cmds; 150, subscript; 150, 151; 150, 152; 151, identifier:extension_pieces; 152, integer:0; 153, else_clause; 153, 154; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:GlimLog; 159, identifier:error; 160, argument_list; 160, 161; 161, binary_operator:%; 161, 162; 161, 163; 162, string:'Extension %s could not be loaded'; 163, identifier:extension; 164, except_clause; 164, 165; 164, 169; 165, as_pattern; 165, 166; 165, 167; 166, identifier:Exception; 167, as_pattern_target; 167, 168; 168, identifier:e; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:GlimLog; 174, identifier:error; 175, argument_list; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:traceback; 179, identifier:format_exc; 180, argument_list | def register_extensions(self):
"""
Function registers extensions given extensions list
Args
----
extensions (list) : the extensions dict on app.config.<env>
Raises
------
Exception: Raises exception when extension can't be loaded
properly.
"""
try:
for extension, config in self.config['extensions'].items():
extension_bstr = ''
# gather package name if exists
extension_pieces = extension.split('.')
# if the extensions is not in glim_extensions package
if len(extension_pieces) > 1:
extension_bstr = '.'.join(extension_pieces)
else: # if the extension is in glim_extensions package
extension_bstr = 'glim_extensions.%s' % extension_pieces[0]
extension_module = import_module(extension_bstr)
if extension_module:
extension_startstr = '%s.%s' % (extension_bstr, 'start')
extension_start = import_module(extension_startstr, pass_errors=True)
extension_cmdsstr = '%s.%s' % (extension_bstr, 'commands')
extension_cmds = import_module(extension_cmdsstr, pass_errors=True)
if extension_start is not None:
before = extension_start.before
before(config)
if extension_cmds is not None:
if self.commandadapter is not None:
self.commandadapter.register_extension(extension_cmds, extension_pieces[0])
else:
GlimLog.error('Extension %s could not be loaded' % extension)
except Exception as e:
GlimLog.error(traceback.format_exc()) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:request; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 4, identifier:self; 5, identifier:method; 6, identifier:path; 7, default_parameter; 7, 8; 7, 9; 8, identifier:query; 9, dictionary; 10, default_parameter; 10, 11; 10, 12; 11, identifier:headers; 12, dictionary; 13, default_parameter; 13, 14; 13, 15; 14, identifier:body; 15, dictionary; 16, default_parameter; 16, 17; 16, 18; 17, identifier:base_url; 18, None; 19, block; 19, 20; 19, 22; 19, 34; 19, 45; 19, 58; 19, 86; 19, 87; 19, 104; 19, 126; 19, 267; 20, expression_statement; 20, 21; 21, string:'''
Issues a request to Onshape
Args:
- method (str): HTTP method
- path (str): Path e.g. /api/documents/:id
- query (dict, default={}): Query params in key-value pairs
- headers (dict, default={}): Key-value pairs of headers
- body (dict, default={}): Body for POST request
- base_url (str, default=None): Host, including scheme and port (if different from creds file)
Returns:
- requests.Response: Object containing the response from Onshape
'''; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:req_headers; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:_make_headers; 29, argument_list; 29, 30; 29, 31; 29, 32; 29, 33; 30, identifier:method; 31, identifier:path; 32, identifier:query; 33, identifier:headers; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:base_url; 37, None; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:base_url; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:_url; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:url; 48, binary_operator:+; 48, 49; 48, 54; 49, binary_operator:+; 49, 50; 49, 53; 50, binary_operator:+; 50, 51; 50, 52; 51, identifier:base_url; 52, identifier:path; 53, string:'?'; 54, call; 54, 55; 54, 56; 55, identifier:urlencode; 56, argument_list; 56, 57; 57, identifier:query; 58, if_statement; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_logging; 62, block; 62, 63; 62, 70; 62, 77; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:utils; 67, identifier:log; 68, argument_list; 68, 69; 69, identifier:body; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:utils; 74, identifier:log; 75, argument_list; 75, 76; 76, identifier:req_headers; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:utils; 81, identifier:log; 82, argument_list; 82, 83; 83, binary_operator:+; 83, 84; 83, 85; 84, string:'request url: '; 85, identifier:url; 86, comment; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:body; 90, conditional_expression:if; 90, 91; 90, 97; 90, 103; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:json; 94, identifier:dumps; 95, argument_list; 95, 96; 96, identifier:body; 97, comparison_operator:==; 97, 98; 97, 102; 98, call; 98, 99; 98, 100; 99, identifier:type; 100, argument_list; 100, 101; 101, identifier:body; 102, identifier:dict; 103, identifier:body; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:res; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:requests; 110, identifier:request; 111, argument_list; 111, 112; 111, 113; 111, 114; 111, 117; 111, 120; 111, 123; 112, identifier:method; 113, identifier:url; 114, keyword_argument; 114, 115; 114, 116; 115, identifier:headers; 116, identifier:req_headers; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:data; 119, identifier:body; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:allow_redirects; 122, False; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:stream; 125, True; 126, if_statement; 126, 127; 126, 132; 126, 221; 126, 249; 127, comparison_operator:==; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:res; 130, identifier:status_code; 131, integer:307; 132, block; 132, 133; 132, 144; 132, 153; 132, 171; 132, 175; 132, 187; 132, 202; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:location; 136, call; 136, 137; 136, 138; 137, identifier:urlparse; 138, argument_list; 138, 139; 139, subscript; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:res; 142, identifier:headers; 143, string:"Location"; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:querystring; 147, call; 147, 148; 147, 149; 148, identifier:parse_qs; 149, argument_list; 149, 150; 150, attribute; 150, 151; 150, 152; 151, identifier:location; 152, identifier:query; 153, if_statement; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:_logging; 157, block; 157, 158; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:utils; 162, identifier:log; 163, argument_list; 163, 164; 164, binary_operator:+; 164, 165; 164, 166; 165, string:'request redirected to: '; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:location; 169, identifier:geturl; 170, argument_list; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:new_query; 174, dictionary; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:new_base_url; 178, binary_operator:+; 178, 179; 178, 184; 179, binary_operator:+; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:location; 182, identifier:scheme; 183, string:'://'; 184, attribute; 184, 185; 184, 186; 185, identifier:location; 186, identifier:netloc; 187, for_statement; 187, 188; 187, 189; 187, 190; 188, identifier:key; 189, identifier:querystring; 190, block; 190, 191; 190, 201; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:new_query; 195, identifier:key; 196, subscript; 196, 197; 196, 200; 197, subscript; 197, 198; 197, 199; 198, identifier:querystring; 199, identifier:key; 200, integer:0; 201, comment; 202, return_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:request; 207, argument_list; 207, 208; 207, 209; 207, 212; 207, 215; 207, 218; 208, identifier:method; 209, attribute; 209, 210; 209, 211; 210, identifier:location; 211, identifier:path; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:query; 214, identifier:new_query; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:headers; 217, identifier:headers; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:base_url; 220, identifier:new_base_url; 221, elif_clause; 221, 222; 221, 229; 222, not_operator; 222, 223; 223, comparison_operator:<=; 223, 224; 223, 225; 223, 228; 224, integer:200; 225, attribute; 225, 226; 225, 227; 226, identifier:res; 227, identifier:status_code; 228, integer:206; 229, block; 229, 230; 230, if_statement; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:_logging; 234, block; 234, 235; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:utils; 239, identifier:log; 240, argument_list; 240, 241; 240, 246; 241, binary_operator:+; 241, 242; 241, 243; 242, string:'request failed, details: '; 243, attribute; 243, 244; 243, 245; 244, identifier:res; 245, identifier:text; 246, keyword_argument; 246, 247; 246, 248; 247, identifier:level; 248, integer:1; 249, else_clause; 249, 250; 250, block; 250, 251; 251, if_statement; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:_logging; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:utils; 260, identifier:log; 261, argument_list; 261, 262; 262, binary_operator:+; 262, 263; 262, 264; 263, string:'request succeeded, details: '; 264, attribute; 264, 265; 264, 266; 265, identifier:res; 266, identifier:text; 267, return_statement; 267, 268; 268, identifier:res | def request(self, method, path, query={}, headers={}, body={}, base_url=None):
'''
Issues a request to Onshape
Args:
- method (str): HTTP method
- path (str): Path e.g. /api/documents/:id
- query (dict, default={}): Query params in key-value pairs
- headers (dict, default={}): Key-value pairs of headers
- body (dict, default={}): Body for POST request
- base_url (str, default=None): Host, including scheme and port (if different from creds file)
Returns:
- requests.Response: Object containing the response from Onshape
'''
req_headers = self._make_headers(method, path, query, headers)
if base_url is None:
base_url = self._url
url = base_url + path + '?' + urlencode(query)
if self._logging:
utils.log(body)
utils.log(req_headers)
utils.log('request url: ' + url)
# only parse as json string if we have to
body = json.dumps(body) if type(body) == dict else body
res = requests.request(method, url, headers=req_headers, data=body, allow_redirects=False, stream=True)
if res.status_code == 307:
location = urlparse(res.headers["Location"])
querystring = parse_qs(location.query)
if self._logging:
utils.log('request redirected to: ' + location.geturl())
new_query = {}
new_base_url = location.scheme + '://' + location.netloc
for key in querystring:
new_query[key] = querystring[key][0] # won't work for repeated query params
return self.request(method, location.path, query=new_query, headers=headers, base_url=new_base_url)
elif not 200 <= res.status_code <= 206:
if self._logging:
utils.log('request failed, details: ' + res.text, level=1)
else:
if self._logging:
utils.log('request succeeded, details: ' + res.text)
return res |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:fix_pdb; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 15; 5, 21; 5, 51; 5, 52; 6, expression_statement; 6, 7; 7, string:'''A function to fix fatal errors in PDB files when they can be automatically fixed. At present, this only runs if
self.strict is False. We may want a separate property for this since we may want to keep strict mode but still
allow PDBs to be fixed.
The only fixes at the moment are for missing chain IDs which get filled in with a valid PDB ID, if possible.'''; 8, if_statement; 8, 9; 8, 12; 9, attribute; 9, 10; 9, 11; 10, identifier:self; 11, identifier:strict; 12, block; 12, 13; 13, return_statement; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:chains; 18, call; 18, 19; 18, 20; 19, identifier:set; 20, argument_list; 21, for_statement; 21, 22; 21, 23; 21, 26; 22, identifier:l; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:lines; 26, block; 26, 27; 27, if_statement; 27, 28; 27, 41; 28, boolean_operator:or; 28, 29; 28, 35; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:l; 32, identifier:startswith; 33, argument_list; 33, 34; 34, string:'ATOM '; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:l; 38, identifier:startswith; 39, argument_list; 39, 40; 40, string:'HETATM'; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:chains; 46, identifier:add; 47, argument_list; 47, 48; 48, subscript; 48, 49; 48, 50; 49, identifier:l; 50, integer:21; 51, comment; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:in; 53, 54; 53, 55; 54, string:' '; 55, identifier:chains; 56, block; 56, 57; 56, 61; 56, 86; 56, 102; 56, 111; 56, 112; 56, 116; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:fresh_id; 60, None; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:allowed_chain_ids; 64, binary_operator:+; 64, 65; 64, 78; 65, binary_operator:+; 65, 66; 65, 72; 66, call; 66, 67; 66, 68; 67, identifier:list; 68, argument_list; 68, 69; 69, attribute; 69, 70; 69, 71; 70, identifier:string; 71, identifier:uppercase; 72, call; 72, 73; 72, 74; 73, identifier:list; 74, argument_list; 74, 75; 75, attribute; 75, 76; 75, 77; 76, identifier:string; 77, identifier:lowercase; 78, call; 78, 79; 78, 80; 79, identifier:map; 80, argument_list; 80, 81; 80, 82; 81, identifier:str; 82, call; 82, 83; 82, 84; 83, identifier:range; 84, argument_list; 84, 85; 85, integer:10; 86, for_statement; 86, 87; 86, 88; 86, 89; 87, identifier:c; 88, identifier:chains; 89, block; 89, 90; 90, try_statement; 90, 91; 90, 99; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:allowed_chain_ids; 96, identifier:remove; 97, argument_list; 97, 98; 98, identifier:c; 99, except_clause; 99, 100; 100, block; 100, 101; 101, pass_statement; 102, if_statement; 102, 103; 102, 104; 103, identifier:allowed_chain_ids; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:fresh_id; 108, subscript; 108, 109; 108, 110; 109, identifier:allowed_chain_ids; 110, integer:0; 111, comment; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:new_lines; 115, list:[]; 116, if_statement; 116, 117; 116, 118; 117, identifier:fresh_id; 118, block; 118, 119; 118, 176; 119, for_statement; 119, 120; 119, 121; 119, 124; 120, identifier:l; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:lines; 124, block; 124, 125; 125, if_statement; 125, 126; 125, 146; 125, 167; 126, boolean_operator:and; 126, 127; 126, 141; 127, parenthesized_expression; 127, 128; 128, boolean_operator:or; 128, 129; 128, 135; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:l; 132, identifier:startswith; 133, argument_list; 133, 134; 134, string:'ATOM '; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:l; 138, identifier:startswith; 139, argument_list; 139, 140; 140, string:'HETATM'; 141, comparison_operator:==; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:l; 144, integer:21; 145, string:' '; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:new_lines; 151, identifier:append; 152, argument_list; 152, 153; 153, binary_operator:%; 153, 154; 153, 155; 154, string:'%s%s%s'; 155, tuple; 155, 156; 155, 161; 155, 162; 156, subscript; 156, 157; 156, 158; 157, identifier:l; 158, slice; 158, 159; 158, 160; 159, colon; 160, integer:21; 161, identifier:fresh_id; 162, subscript; 162, 163; 162, 164; 163, identifier:l; 164, slice; 164, 165; 164, 166; 165, integer:22; 166, colon; 167, else_clause; 167, 168; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:new_lines; 173, identifier:append; 174, argument_list; 174, 175; 175, identifier:l; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:lines; 181, identifier:new_lines | def fix_pdb(self):
'''A function to fix fatal errors in PDB files when they can be automatically fixed. At present, this only runs if
self.strict is False. We may want a separate property for this since we may want to keep strict mode but still
allow PDBs to be fixed.
The only fixes at the moment are for missing chain IDs which get filled in with a valid PDB ID, if possible.'''
if self.strict:
return
# Get the list of chains
chains = set()
for l in self.lines:
if l.startswith('ATOM ') or l.startswith('HETATM'):
chains.add(l[21])
# If there is a chain with a blank ID, change that ID to a valid unused ID
if ' ' in chains:
fresh_id = None
allowed_chain_ids = list(string.uppercase) + list(string.lowercase) + map(str, range(10))
for c in chains:
try: allowed_chain_ids.remove(c)
except: pass
if allowed_chain_ids:
fresh_id = allowed_chain_ids[0]
# Rewrite the lines
new_lines = []
if fresh_id:
for l in self.lines:
if (l.startswith('ATOM ') or l.startswith('HETATM')) and l[21] == ' ':
new_lines.append('%s%s%s' % (l[:21], fresh_id, l[22:]))
else:
new_lines.append(l)
self.lines = new_lines |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:stripForDDG; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:chains; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:keepHETATM; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:numberOfModels; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:raise_exception; 16, True; 17, block; 17, 18; 17, 20; 17, 28; 17, 34; 17, 38; 17, 42; 17, 46; 17, 50; 17, 54; 17, 58; 17, 265; 17, 271; 17, 277; 17, 283; 17, 284; 17, 288; 17, 299; 17, 300; 17, 308; 17, 323; 17, 329; 17, 335; 17, 345; 17, 352; 17, 356; 17, 368; 17, 373; 18, expression_statement; 18, 19; 19, string:'''Strips a PDB to ATOM lines. If keepHETATM is True then also retain HETATM lines.
By default all PDB chains are kept. The chains parameter should be True or a list.
In the latter case, only those chains in the list are kept.
Unoccupied ATOM lines are discarded.
This function also builds maps from PDB numbering to Rosetta numbering and vice versa.
'''; 20, if_statement; 20, 21; 20, 22; 21, identifier:raise_exception; 22, block; 22, 23; 23, raise_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:Exception; 26, argument_list; 26, 27; 27, string:'This code is deprecated.'; 28, import_from_statement; 28, 29; 28, 32; 29, dotted_name; 29, 30; 29, 31; 30, identifier:Bio; 31, identifier:PDB; 32, dotted_name; 32, 33; 33, identifier:PDBParser; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:resmap; 37, dictionary; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:iresmap; 41, dictionary; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:newlines; 45, list:[]; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:residx; 49, integer:0; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:oldres; 53, None; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:model_number; 57, integer:1; 58, for_statement; 58, 59; 58, 60; 58, 63; 59, identifier:line; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:lines; 63, block; 63, 64; 63, 77; 63, 104; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:fieldtype; 67, call; 67, 68; 67, 76; 68, attribute; 68, 69; 68, 75; 69, subscript; 69, 70; 69, 71; 70, identifier:line; 71, slice; 71, 72; 71, 73; 71, 74; 72, integer:0; 73, colon; 74, integer:6; 75, identifier:strip; 76, argument_list; 77, if_statement; 77, 78; 77, 81; 78, comparison_operator:==; 78, 79; 78, 80; 79, identifier:fieldtype; 80, string:"ENDMDL"; 81, block; 81, 82; 81, 86; 81, 95; 82, expression_statement; 82, 83; 83, augmented_assignment:+=; 83, 84; 83, 85; 84, identifier:model_number; 85, integer:1; 86, if_statement; 86, 87; 86, 93; 87, boolean_operator:and; 87, 88; 87, 89; 88, identifier:numberOfModels; 89, parenthesized_expression; 89, 90; 90, comparison_operator:>; 90, 91; 90, 92; 91, identifier:model_number; 92, identifier:numberOfModels; 93, block; 93, 94; 94, break_statement; 95, if_statement; 95, 96; 95, 98; 96, not_operator; 96, 97; 97, identifier:numberOfModels; 98, block; 98, 99; 99, raise_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:Exception; 102, argument_list; 102, 103; 103, string:"The logic here does not handle multiple models yet."; 104, if_statement; 104, 105; 104, 129; 105, boolean_operator:and; 105, 106; 105, 117; 106, parenthesized_expression; 106, 107; 107, boolean_operator:or; 107, 108; 107, 111; 108, comparison_operator:==; 108, 109; 108, 110; 109, identifier:fieldtype; 110, string:"ATOM"; 111, parenthesized_expression; 111, 112; 112, boolean_operator:and; 112, 113; 112, 116; 113, comparison_operator:==; 113, 114; 113, 115; 114, identifier:fieldtype; 115, string:"HETATM"; 116, identifier:keepHETATM; 117, parenthesized_expression; 117, 118; 118, comparison_operator:!=; 118, 119; 118, 128; 119, call; 119, 120; 119, 121; 120, identifier:float; 121, argument_list; 121, 122; 122, subscript; 122, 123; 122, 124; 123, identifier:line; 124, slice; 124, 125; 124, 126; 124, 127; 125, integer:54; 126, colon; 127, integer:60; 128, integer:0; 129, block; 129, 130; 129, 139; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:chain; 133, subscript; 133, 134; 133, 135; 134, identifier:line; 135, slice; 135, 136; 135, 137; 135, 138; 136, integer:21; 137, colon; 138, integer:22; 139, if_statement; 139, 140; 139, 149; 140, boolean_operator:or; 140, 141; 140, 145; 141, parenthesized_expression; 141, 142; 142, comparison_operator:==; 142, 143; 142, 144; 143, identifier:chains; 144, True; 145, parenthesized_expression; 145, 146; 146, comparison_operator:in; 146, 147; 146, 148; 147, identifier:chain; 148, identifier:chains; 149, block; 149, 150; 149, 159; 149, 160; 149, 169; 149, 218; 149, 225; 149, 226; 149, 250; 149, 258; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:resid; 153, subscript; 153, 154; 153, 155; 154, identifier:line; 155, slice; 155, 156; 155, 157; 155, 158; 156, integer:21; 157, colon; 158, integer:27; 159, comment; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:iCode; 163, subscript; 163, 164; 163, 165; 164, identifier:line; 165, slice; 165, 166; 165, 167; 165, 168; 166, integer:26; 167, colon; 168, integer:27; 169, if_statement; 169, 170; 169, 173; 170, comparison_operator:!=; 170, 171; 170, 172; 171, identifier:resid; 172, identifier:oldres; 173, block; 173, 174; 173, 178; 173, 186; 173, 194; 173, 202; 173, 208; 173, 214; 174, expression_statement; 174, 175; 175, augmented_assignment:+=; 175, 176; 175, 177; 176, identifier:residx; 177, integer:1; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:newnumbering; 181, binary_operator:%; 181, 182; 181, 183; 182, string:"%s%4.i "; 183, tuple; 183, 184; 183, 185; 184, identifier:chain; 185, identifier:residx; 186, assert_statement; 186, 187; 187, parenthesized_expression; 187, 188; 188, comparison_operator:==; 188, 189; 188, 193; 189, call; 189, 190; 189, 191; 190, identifier:len; 191, argument_list; 191, 192; 192, identifier:newnumbering; 193, integer:6; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:id; 197, binary_operator:+; 197, 198; 197, 201; 198, binary_operator:+; 198, 199; 198, 200; 199, identifier:fieldtype; 200, string:"-"; 201, identifier:resid; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:resmap; 206, identifier:id; 207, identifier:residx; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:iresmap; 212, identifier:residx; 213, identifier:id; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:oldres; 217, identifier:resid; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:oldlength; 221, call; 221, 222; 221, 223; 222, identifier:len; 223, argument_list; 223, 224; 224, identifier:line; 225, comment; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:line; 229, binary_operator:%; 229, 230; 229, 231; 230, string:"%s%4.i %s"; 231, tuple; 231, 232; 231, 238; 231, 245; 232, subscript; 232, 233; 232, 234; 233, identifier:line; 234, slice; 234, 235; 234, 236; 234, 237; 235, integer:0; 236, colon; 237, integer:22; 238, subscript; 238, 239; 238, 240; 239, identifier:resmap; 240, binary_operator:+; 240, 241; 240, 244; 241, binary_operator:+; 241, 242; 241, 243; 242, identifier:fieldtype; 243, string:"-"; 244, identifier:resid; 245, subscript; 245, 246; 245, 247; 246, identifier:line; 247, slice; 247, 248; 247, 249; 248, integer:27; 249, colon; 250, assert_statement; 250, 251; 251, parenthesized_expression; 251, 252; 252, comparison_operator:==; 252, 253; 252, 257; 253, call; 253, 254; 253, 255; 254, identifier:len; 255, argument_list; 255, 256; 256, identifier:line; 257, identifier:oldlength; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:newlines; 262, identifier:append; 263, argument_list; 263, 264; 264, identifier:line; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:lines; 270, identifier:newlines; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:ddGresmap; 276, identifier:resmap; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:self; 281, identifier:ddGiresmap; 282, identifier:iresmap; 283, comment; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:tmpfile; 287, string:"/tmp/ddgtemp.pdb"; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:lines; 293, boolean_operator:or; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:self; 296, identifier:lines; 297, list:["\n"]; 297, 298; 298, string:"\n"; 299, comment; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:F; 303, call; 303, 304; 303, 305; 304, identifier:open; 305, argument_list; 305, 306; 305, 307; 306, identifier:tmpfile; 307, string:'w'; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:F; 312, identifier:write; 313, argument_list; 313, 314; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:string; 317, identifier:join; 318, argument_list; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:self; 321, identifier:lines; 322, string:"\n"; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:F; 327, identifier:close; 328, argument_list; 329, expression_statement; 329, 330; 330, assignment; 330, 331; 330, 332; 331, identifier:parser; 332, call; 332, 333; 332, 334; 333, identifier:PDBParser; 334, argument_list; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 338; 337, identifier:structure; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:parser; 341, identifier:get_structure; 342, argument_list; 342, 343; 342, 344; 343, string:'tmp'; 344, identifier:tmpfile; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:os; 349, identifier:remove; 350, argument_list; 350, 351; 351, identifier:tmpfile; 352, expression_statement; 352, 353; 353, assignment; 353, 354; 353, 355; 354, identifier:count; 355, integer:0; 356, for_statement; 356, 357; 356, 358; 356, 363; 357, identifier:residue; 358, call; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:structure; 361, identifier:get_residues; 362, argument_list; 363, block; 363, 364; 364, expression_statement; 364, 365; 365, augmented_assignment:+=; 365, 366; 365, 367; 366, identifier:count; 367, integer:1; 368, assert_statement; 368, 369; 369, parenthesized_expression; 369, 370; 370, comparison_operator:==; 370, 371; 370, 372; 371, identifier:count; 372, identifier:residx; 373, assert_statement; 373, 374; 374, parenthesized_expression; 374, 375; 375, comparison_operator:==; 375, 376; 375, 380; 376, call; 376, 377; 376, 378; 377, identifier:len; 378, argument_list; 378, 379; 379, identifier:resmap; 380, call; 380, 381; 380, 382; 381, identifier:len; 382, argument_list; 382, 383; 383, identifier:iresmap | def stripForDDG(self, chains = True, keepHETATM = False, numberOfModels = None, raise_exception = True):
'''Strips a PDB to ATOM lines. If keepHETATM is True then also retain HETATM lines.
By default all PDB chains are kept. The chains parameter should be True or a list.
In the latter case, only those chains in the list are kept.
Unoccupied ATOM lines are discarded.
This function also builds maps from PDB numbering to Rosetta numbering and vice versa.
'''
if raise_exception:
raise Exception('This code is deprecated.')
from Bio.PDB import PDBParser
resmap = {}
iresmap = {}
newlines = []
residx = 0
oldres = None
model_number = 1
for line in self.lines:
fieldtype = line[0:6].strip()
if fieldtype == "ENDMDL":
model_number += 1
if numberOfModels and (model_number > numberOfModels):
break
if not numberOfModels:
raise Exception("The logic here does not handle multiple models yet.")
if (fieldtype == "ATOM" or (fieldtype == "HETATM" and keepHETATM)) and (float(line[54:60]) != 0):
chain = line[21:22]
if (chains == True) or (chain in chains):
resid = line[21:27] # Chain, residue sequence number, insertion code
iCode = line[26:27]
if resid != oldres:
residx += 1
newnumbering = "%s%4.i " % (chain, residx)
assert(len(newnumbering) == 6)
id = fieldtype + "-" + resid
resmap[id] = residx
iresmap[residx] = id
oldres = resid
oldlength = len(line)
# Add the original line back including the chain [21] and inserting a blank for the insertion code
line = "%s%4.i %s" % (line[0:22], resmap[fieldtype + "-" + resid], line[27:])
assert(len(line) == oldlength)
newlines.append(line)
self.lines = newlines
self.ddGresmap = resmap
self.ddGiresmap = iresmap
# Sanity check against a known library
tmpfile = "/tmp/ddgtemp.pdb"
self.lines = self.lines or ["\n"] # necessary to avoid a crash in the Bio Python module
F = open(tmpfile,'w')
F.write(string.join(self.lines, "\n"))
F.close()
parser=PDBParser()
structure=parser.get_structure('tmp', tmpfile)
os.remove(tmpfile)
count = 0
for residue in structure.get_residues():
count += 1
assert(count == residx)
assert(len(resmap) == len(iresmap)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 1, 14; 2, function_name:neighbors2; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:distance; 6, identifier:chain_residue; 7, default_parameter; 7, 8; 7, 9; 8, identifier:atom; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:resid_list; 12, None; 13, comment; 14, block; 14, 15; 14, 17; 14, 79; 14, 88; 14, 136; 14, 140; 14, 221; 15, expression_statement; 15, 16; 16, string:'''this one is more precise since it uses the chain identifier also'''; 17, if_statement; 17, 18; 17, 21; 17, 22; 17, 45; 18, comparison_operator:==; 18, 19; 18, 20; 19, identifier:atom; 20, None; 21, comment; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:lines; 26, list_comprehension; 26, 27; 26, 28; 26, 36; 27, identifier:line; 28, for_in_clause; 28, 29; 28, 30; 29, identifier:line; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:atomlines; 34, argument_list; 34, 35; 35, identifier:resid_list; 36, if_clause; 36, 37; 37, comparison_operator:in; 37, 38; 37, 44; 38, subscript; 38, 39; 38, 40; 39, identifier:line; 40, slice; 40, 41; 40, 42; 40, 43; 41, integer:17; 42, colon; 43, integer:20; 44, identifier:allowed_PDB_residues_types; 45, else_clause; 45, 46; 45, 47; 46, comment; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:lines; 51, list_comprehension; 51, 52; 51, 53; 51, 61; 52, identifier:line; 53, for_in_clause; 53, 54; 53, 55; 54, identifier:line; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:atomlines; 59, argument_list; 59, 60; 60, identifier:resid_list; 61, if_clause; 61, 62; 62, boolean_operator:and; 62, 63; 62, 71; 63, comparison_operator:in; 63, 64; 63, 70; 64, subscript; 64, 65; 64, 66; 65, identifier:line; 66, slice; 66, 67; 66, 68; 66, 69; 67, integer:17; 68, colon; 69, integer:20; 70, identifier:allowed_PDB_residues_types; 71, comparison_operator:==; 71, 72; 71, 78; 72, subscript; 72, 73; 72, 74; 73, identifier:line; 74, slice; 74, 75; 74, 76; 74, 77; 75, integer:12; 76, colon; 77, integer:16; 78, identifier:atom; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:shash; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:spatialhash; 85, identifier:SpatialHash; 86, argument_list; 86, 87; 87, identifier:distance; 88, for_statement; 88, 89; 88, 90; 88, 91; 89, identifier:line; 90, identifier:lines; 91, block; 91, 92; 91, 123; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:pos; 95, tuple; 95, 96; 95, 105; 95, 114; 96, call; 96, 97; 96, 98; 97, identifier:float; 98, argument_list; 98, 99; 99, subscript; 99, 100; 99, 101; 100, identifier:line; 101, slice; 101, 102; 101, 103; 101, 104; 102, integer:30; 103, colon; 104, integer:38; 105, call; 105, 106; 105, 107; 106, identifier:float; 107, argument_list; 107, 108; 108, subscript; 108, 109; 108, 110; 109, identifier:line; 110, slice; 110, 111; 110, 112; 110, 113; 111, integer:38; 112, colon; 113, integer:46; 114, call; 114, 115; 114, 116; 115, identifier:float; 116, argument_list; 116, 117; 117, subscript; 117, 118; 117, 119; 118, identifier:line; 119, slice; 119, 120; 119, 121; 119, 122; 120, integer:46; 121, colon; 122, integer:54; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:shash; 127, identifier:insert; 128, argument_list; 128, 129; 128, 130; 129, identifier:pos; 130, subscript; 130, 131; 130, 132; 131, identifier:line; 132, slice; 132, 133; 132, 134; 132, 135; 133, integer:21; 134, colon; 135, integer:26; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:neighbor_list; 139, list:[]; 140, for_statement; 140, 141; 140, 142; 140, 143; 141, identifier:line; 142, identifier:lines; 143, block; 143, 144; 143, 153; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:resid; 147, subscript; 147, 148; 147, 149; 148, identifier:line; 149, slice; 149, 150; 149, 151; 149, 152; 150, integer:21; 151, colon; 152, integer:26; 153, if_statement; 153, 154; 153, 157; 154, comparison_operator:==; 154, 155; 154, 156; 155, identifier:resid; 156, identifier:chain_residue; 157, block; 157, 158; 157, 189; 157, 215; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:pos; 161, tuple; 161, 162; 161, 171; 161, 180; 162, call; 162, 163; 162, 164; 163, identifier:float; 164, argument_list; 164, 165; 165, subscript; 165, 166; 165, 167; 166, identifier:line; 167, slice; 167, 168; 167, 169; 167, 170; 168, integer:30; 169, colon; 170, integer:38; 171, call; 171, 172; 171, 173; 172, identifier:float; 173, argument_list; 173, 174; 174, subscript; 174, 175; 174, 176; 175, identifier:line; 176, slice; 176, 177; 176, 178; 176, 179; 177, integer:38; 178, colon; 179, integer:46; 180, call; 180, 181; 180, 182; 181, identifier:float; 182, argument_list; 182, 183; 183, subscript; 183, 184; 183, 185; 184, identifier:line; 185, slice; 185, 186; 185, 187; 185, 188; 186, integer:46; 187, colon; 188, integer:54; 189, for_statement; 189, 190; 189, 191; 189, 198; 190, identifier:data; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:shash; 194, identifier:nearby; 195, argument_list; 195, 196; 195, 197; 196, identifier:pos; 197, identifier:distance; 198, block; 198, 199; 199, if_statement; 199, 200; 199, 205; 200, comparison_operator:not; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:data; 203, integer:1; 204, identifier:neighbor_list; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:neighbor_list; 210, identifier:append; 211, argument_list; 211, 212; 212, subscript; 212, 213; 212, 214; 213, identifier:data; 214, integer:1; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:neighbor_list; 219, identifier:sort; 220, argument_list; 221, return_statement; 221, 222; 222, identifier:neighbor_list | def neighbors2(self, distance, chain_residue, atom = None, resid_list = None):
#atom = " CA "
'''this one is more precise since it uses the chain identifier also'''
if atom == None: # consider all atoms
lines = [line for line in self.atomlines(resid_list) if line[17:20] in allowed_PDB_residues_types]
else: # consider only given atoms
lines = [line for line in self.atomlines(resid_list) if line[17:20] in allowed_PDB_residues_types and line[12:16] == atom]
shash = spatialhash.SpatialHash(distance)
for line in lines:
pos = (float(line[30:38]), float(line[38:46]), float(line[46:54]))
shash.insert(pos, line[21:26])
neighbor_list = []
for line in lines:
resid = line[21:26]
if resid == chain_residue:
pos = (float(line[30:38]), float(line[38:46]), float(line[46:54]))
for data in shash.nearby(pos, distance):
if data[1] not in neighbor_list:
neighbor_list.append(data[1])
neighbor_list.sort()
return neighbor_list |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:add_document; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:self; 5, identifier:item_uri; 6, identifier:name; 7, identifier:metadata; 8, default_parameter; 8, 9; 8, 10; 9, identifier:content; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:docurl; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:file; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:displaydoc; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:preferName; 22, False; 23, default_parameter; 23, 24; 23, 25; 24, identifier:contrib_id; 25, None; 26, block; 26, 27; 26, 29; 26, 54; 26, 72; 26, 73; 26, 82; 26, 91; 26, 137; 26, 190; 26, 197; 26, 215; 26, 223; 27, expression_statement; 27, 28; 28, comment; 29, if_statement; 29, 30; 29, 36; 29, 48; 30, boolean_operator:and; 30, 31; 30, 33; 31, not_operator; 31, 32; 32, identifier:preferName; 33, comparison_operator:is; 33, 34; 33, 35; 34, identifier:file; 35, None; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:docid; 40, call; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:os; 44, identifier:path; 45, identifier:basename; 46, argument_list; 46, 47; 47, identifier:file; 48, else_clause; 48, 49; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:docid; 53, identifier:name; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:docmeta; 57, dictionary; 57, 58; 58, pair; 58, 59; 58, 60; 59, string:"metadata"; 60, dictionary; 60, 61; 60, 66; 60, 69; 61, pair; 61, 62; 61, 63; 62, string:"@context"; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:context; 66, pair; 66, 67; 66, 68; 67, string:"@type"; 68, string:"foaf:Document"; 69, pair; 69, 70; 69, 71; 70, string:"dcterms:identifier"; 71, identifier:docid; 72, comment; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:docmeta; 78, string:"metadata"; 79, identifier:update; 80, argument_list; 80, 81; 81, identifier:metadata; 82, if_statement; 82, 83; 82, 84; 83, identifier:contrib_id; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:docmeta; 89, string:'contribution_id'; 90, identifier:contrib_id; 91, if_statement; 91, 92; 91, 95; 91, 102; 91, 118; 91, 130; 92, comparison_operator:is; 92, 93; 92, 94; 93, identifier:content; 94, None; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:docmeta; 100, string:'document_content'; 101, identifier:content; 102, elif_clause; 102, 103; 102, 106; 103, comparison_operator:is; 103, 104; 103, 105; 104, identifier:docurl; 105, None; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 114; 109, subscript; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:docmeta; 112, string:"metadata"; 113, string:"dcterms:source"; 114, dictionary; 114, 115; 115, pair; 115, 116; 115, 117; 116, string:"@id"; 117, identifier:docurl; 118, elif_clause; 118, 119; 118, 122; 118, 123; 119, comparison_operator:is; 119, 120; 119, 121; 120, identifier:file; 121, None; 122, comment; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:docmeta; 127, subscript; 127, 128; 127, 129; 128, identifier:docmeta; 129, string:'metadata'; 130, else_clause; 130, 131; 131, block; 131, 132; 132, raise_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:Exception; 135, argument_list; 135, 136; 136, string:"One of content, docurl or file must be specified in add_document"; 137, if_statement; 137, 138; 137, 141; 137, 168; 138, comparison_operator:is; 138, 139; 138, 140; 139, identifier:file; 140, None; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:result; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:api_request; 149, argument_list; 149, 150; 149, 151; 149, 154; 149, 165; 150, identifier:item_uri; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:method; 153, string:'POST'; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:data; 156, dictionary; 156, 157; 157, pair; 157, 158; 157, 159; 158, string:'metadata'; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:json; 162, identifier:dumps; 163, argument_list; 163, 164; 164, identifier:docmeta; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:file; 167, identifier:file; 168, else_clause; 168, 169; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:result; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:api_request; 177, argument_list; 177, 178; 177, 179; 177, 182; 178, identifier:item_uri; 179, keyword_argument; 179, 180; 179, 181; 180, identifier:method; 181, string:'POST'; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:data; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:json; 187, identifier:dumps; 188, argument_list; 188, 189; 189, identifier:docmeta; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:__check_success; 195, argument_list; 195, 196; 196, identifier:result; 197, if_statement; 197, 198; 197, 199; 198, identifier:displaydoc; 199, block; 199, 200; 199, 207; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:itemmeta; 203, dictionary; 203, 204; 204, pair; 204, 205; 204, 206; 205, string:"http://alveo.edu.org/vocabulary/display_document"; 206, identifier:docid; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:modify_item; 212, argument_list; 212, 213; 212, 214; 213, identifier:item_uri; 214, identifier:itemmeta; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:doc_uri; 218, binary_operator:+; 218, 219; 218, 222; 219, binary_operator:+; 219, 220; 219, 221; 220, identifier:item_uri; 221, string:"/document/"; 222, identifier:name; 223, return_statement; 223, 224; 224, identifier:doc_uri | def add_document(self, item_uri, name, metadata,
content=None, docurl=None, file=None,
displaydoc=False, preferName=False,
contrib_id=None):
"""Add a document to an existing item
:param item_uri: the URI that references the item
:type item_uri: String
:param name: The document name
:type name: String
:param metadata: a dictionary of metadata values describing the document
:type metadata: Dict
:param content: optional content of the document
:type content: byte array
:param docurl: optional url referencing the document
:type docurl: String
:param file: optional full path to file to be uploaded
:type file: String
:param displaydoc: if True, make this the display document for the item
:type displaydoc: Boolean
:param preferName: if True, given document name will be the document id rather than
filename. Useful if you want to upload under a different filename.
:type preferName: Boolean
:param contrib_id: if present, add this document to this contribution as well as
associating it with the item
:type contrib_id: Integer
:rtype: String
:returns: The URL of the newly created document
"""
if not preferName and file is not None:
docid = os.path.basename(file)
else:
docid = name
docmeta = {"metadata": {"@context": self.context,
"@type": "foaf:Document",
"dcterms:identifier": docid,
}
}
# add in metadata we are passed
docmeta["metadata"].update(metadata)
if contrib_id:
docmeta['contribution_id'] = contrib_id
if content is not None:
docmeta['document_content'] = content
elif docurl is not None:
docmeta["metadata"]["dcterms:source"] = { "@id": docurl }
elif file is not None:
# we only pass the metadata part of the dictionary
docmeta = docmeta['metadata']
else:
raise Exception("One of content, docurl or file must be specified in add_document")
if file is not None:
result = self.api_request(item_uri, method='POST', data={'metadata': json.dumps(docmeta)}, file=file)
else:
result = self.api_request(item_uri, method='POST', data=json.dumps(docmeta))
self.__check_success(result)
if displaydoc:
itemmeta = {"http://alveo.edu.org/vocabulary/display_document": docid}
self.modify_item(item_uri, itemmeta)
doc_uri = item_uri + "/document/" + name
return doc_uri |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_agents; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:addr; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:agent_cls; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:include_manager; 13, False; 14, block; 14, 15; 14, 17; 14, 32; 14, 71; 14, 91; 14, 104; 15, expression_statement; 15, 16; 16, string:'''Get agents in the environment.
:param bool addr: If ``True``, returns only addresses of the agents.
:param agent_cls:
Optional, if specified returns only agents belonging to that
particular class.
:param bool include_manager:
If `True``` includes the environment's manager, i.e. the agent in
the address ``tcp://environment-host:port/0``, to the returned
list if the environment has attribute :attr:`manager`. If
environment does not have :attr:`manager`, then the parameter does
nothing.
:returns: A list of agents in the environment.
:rtype: list
.. note::
By design, manager agents are excluded from the returned lists of
agents by default.
'''; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:agents; 20, call; 20, 21; 20, 22; 21, identifier:list; 22, argument_list; 22, 23; 23, call; 23, 24; 23, 31; 24, attribute; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:agents; 29, identifier:dict; 30, identifier:values; 31, argument_list; 32, if_statement; 32, 33; 32, 44; 33, boolean_operator:and; 33, 34; 33, 39; 34, call; 34, 35; 34, 36; 35, identifier:hasattr; 36, argument_list; 36, 37; 36, 38; 37, identifier:self; 38, string:'manager'; 39, comparison_operator:is; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:manager; 43, None; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 48; 46, not_operator; 46, 47; 47, identifier:include_manager; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:agents; 52, list_comprehension; 52, 53; 52, 54; 52, 57; 53, identifier:a; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:a; 56, identifier:agents; 57, if_clause; 57, 58; 58, comparison_operator:!=; 58, 59; 58, 70; 59, subscript; 59, 60; 59, 69; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:a; 64, identifier:addr; 65, identifier:rsplit; 66, argument_list; 66, 67; 66, 68; 67, string:'/'; 68, integer:1; 69, integer:1; 70, string:'0'; 71, if_statement; 71, 72; 71, 75; 72, comparison_operator:is; 72, 73; 72, 74; 73, identifier:agent_cls; 74, None; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:agents; 79, list_comprehension; 79, 80; 79, 81; 79, 84; 80, identifier:a; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:a; 83, identifier:agents; 84, if_clause; 84, 85; 85, comparison_operator:is; 85, 86; 85, 90; 86, call; 86, 87; 86, 88; 87, identifier:type; 88, argument_list; 88, 89; 89, identifier:a; 90, identifier:agent_cls; 91, if_statement; 91, 92; 91, 93; 92, identifier:addr; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:agents; 97, list_comprehension; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:agent; 100, identifier:addr; 101, for_in_clause; 101, 102; 101, 103; 102, identifier:agent; 103, identifier:agents; 104, return_statement; 104, 105; 105, identifier:agents | def get_agents(self, addr=True, agent_cls=None, include_manager=False):
'''Get agents in the environment.
:param bool addr: If ``True``, returns only addresses of the agents.
:param agent_cls:
Optional, if specified returns only agents belonging to that
particular class.
:param bool include_manager:
If `True``` includes the environment's manager, i.e. the agent in
the address ``tcp://environment-host:port/0``, to the returned
list if the environment has attribute :attr:`manager`. If
environment does not have :attr:`manager`, then the parameter does
nothing.
:returns: A list of agents in the environment.
:rtype: list
.. note::
By design, manager agents are excluded from the returned lists of
agents by default.
'''
agents = list(self.agents.dict.values())
if hasattr(self, 'manager') and self.manager is not None:
if not include_manager:
agents = [a for a in agents if a.addr.rsplit('/', 1)[1] != '0']
if agent_cls is not None:
agents = [a for a in agents if type(a) is agent_cls]
if addr:
agents = [agent.addr for agent in agents]
return agents |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:tee; 3, parameters; 3, 4; 3, 6; 4, list_splat_pattern; 4, 5; 5, identifier:popenargs; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 18; 8, 40; 8, 48; 8, 76; 8, 170; 8, 184; 8, 198; 9, expression_statement; 9, 10; 10, comment; 11, import_statement; 11, 12; 11, 14; 11, 16; 12, dotted_name; 12, 13; 13, identifier:subprocess; 14, dotted_name; 14, 15; 15, identifier:select; 16, dotted_name; 16, 17; 17, identifier:sys; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:process; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:subprocess; 24, identifier:Popen; 25, argument_list; 25, 26; 25, 31; 25, 36; 25, 38; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:stdout; 28, attribute; 28, 29; 28, 30; 29, identifier:subprocess; 30, identifier:PIPE; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:stderr; 33, attribute; 33, 34; 33, 35; 34, identifier:subprocess; 35, identifier:PIPE; 36, list_splat; 36, 37; 37, identifier:popenargs; 38, dictionary_splat; 38, 39; 39, identifier:kwargs; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 45; 42, pattern_list; 42, 43; 42, 44; 43, identifier:stdout; 44, identifier:stderr; 45, expression_list; 45, 46; 45, 47; 46, string:''; 47, string:''; 48, function_definition; 48, 49; 48, 50; 48, 53; 48, 54; 49, function_name:read_stream; 50, parameters; 50, 51; 50, 52; 51, identifier:input_callback; 52, identifier:output_stream; 53, comment; 54, block; 54, 55; 54, 61; 54, 68; 54, 74; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:read; 58, call; 58, 59; 58, 60; 59, identifier:input_callback; 60, argument_list; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:output_stream; 65, identifier:write; 66, argument_list; 66, 67; 67, identifier:read; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:output_stream; 72, identifier:flush; 73, argument_list; 74, return_statement; 74, 75; 75, identifier:read; 76, while_statement; 76, 77; 76, 84; 77, comparison_operator:is; 77, 78; 77, 83; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:process; 81, identifier:poll; 82, argument_list; 83, None; 84, block; 84, 85; 84, 103; 84, 116; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:watch; 88, expression_list; 88, 89; 88, 96; 89, call; 89, 90; 89, 95; 90, attribute; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:process; 93, identifier:stdout; 94, identifier:fileno; 95, argument_list; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:process; 100, identifier:stderr; 101, identifier:fileno; 102, argument_list; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:ready; 106, subscript; 106, 107; 106, 115; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:select; 110, identifier:select; 111, argument_list; 111, 112; 111, 113; 111, 114; 112, identifier:watch; 113, list:[]; 114, list:[]; 115, integer:0; 116, for_statement; 116, 117; 116, 118; 116, 119; 117, identifier:fd; 118, identifier:ready; 119, block; 119, 120; 119, 145; 120, if_statement; 120, 121; 120, 130; 121, comparison_operator:==; 121, 122; 121, 123; 122, identifier:fd; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:process; 127, identifier:stdout; 128, identifier:fileno; 129, argument_list; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, augmented_assignment:+=; 132, 133; 132, 134; 133, identifier:stdout; 134, call; 134, 135; 134, 136; 135, identifier:read_stream; 136, argument_list; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:process; 140, identifier:stdout; 141, identifier:readline; 142, attribute; 142, 143; 142, 144; 143, identifier:sys; 144, identifier:stdout; 145, if_statement; 145, 146; 145, 155; 146, comparison_operator:==; 146, 147; 146, 148; 147, identifier:fd; 148, call; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:process; 152, identifier:stderr; 153, identifier:fileno; 154, argument_list; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, augmented_assignment:+=; 157, 158; 157, 159; 158, identifier:stderr; 159, call; 159, 160; 159, 161; 160, identifier:read_stream; 161, argument_list; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:process; 165, identifier:stderr; 166, identifier:readline; 167, attribute; 167, 168; 167, 169; 168, identifier:sys; 169, identifier:stderr; 170, expression_statement; 170, 171; 171, augmented_assignment:+=; 171, 172; 171, 173; 172, identifier:stdout; 173, call; 173, 174; 173, 175; 174, identifier:read_stream; 175, argument_list; 175, 176; 175, 181; 176, attribute; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:process; 179, identifier:stdout; 180, identifier:read; 181, attribute; 181, 182; 181, 183; 182, identifier:sys; 183, identifier:stdout; 184, expression_statement; 184, 185; 185, augmented_assignment:+=; 185, 186; 185, 187; 186, identifier:stderr; 187, call; 187, 188; 187, 189; 188, identifier:read_stream; 189, argument_list; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:process; 193, identifier:stderr; 194, identifier:read; 195, attribute; 195, 196; 195, 197; 196, identifier:sys; 197, identifier:stderr; 198, return_statement; 198, 199; 199, expression_list; 199, 200; 199, 201; 200, identifier:stdout; 201, identifier:stderr | def tee(*popenargs, **kwargs):
"""
Run a command as if it were piped though tee.
Output generated by the command is displayed in real time to the terminal.
It is also captured in strings and returned once the process terminated.
This function is very useful for logging output from cluster runs. Naive
approaches like check_output() are vulnerable to crashes (i.e. if a job
exceeds its time limit) if they hold all output until the end. This
function echos any output as soon as it's generated, so that the cluster
logging system will still work.
"""
import subprocess, select, sys
process = subprocess.Popen(
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
*popenargs, **kwargs)
stdout, stderr = '', ''
def read_stream(input_callback, output_stream): # (no fold)
read = input_callback()
output_stream.write(read)
output_stream.flush()
return read
while process.poll() is None:
watch = process.stdout.fileno(), process.stderr.fileno()
ready = select.select(watch, [], [])[0]
for fd in ready:
if fd == process.stdout.fileno():
stdout += read_stream(process.stdout.readline, sys.stdout)
if fd == process.stderr.fileno():
stderr += read_stream(process.stderr.readline, sys.stderr)
stdout += read_stream(process.stdout.read, sys.stdout)
stderr += read_stream(process.stderr.read, sys.stderr)
return stdout, stderr |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:dump_object; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:obj; 6, block; 6, 7; 6, 9; 6, 23; 6, 36; 6, 58; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 17; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 14; 13, identifier:obj; 14, attribute; 14, 15; 14, 16; 15, identifier:uuid; 16, identifier:UUID; 17, block; 17, 18; 18, return_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:str; 21, argument_list; 21, 22; 22, identifier:obj; 23, if_statement; 23, 24; 23, 29; 24, call; 24, 25; 24, 26; 25, identifier:hasattr; 26, argument_list; 26, 27; 26, 28; 27, identifier:obj; 28, string:'isoformat'; 29, block; 29, 30; 30, return_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:obj; 34, identifier:isoformat; 35, argument_list; 36, if_statement; 36, 37; 36, 45; 37, call; 37, 38; 37, 39; 38, identifier:isinstance; 39, argument_list; 39, 40; 39, 41; 40, identifier:obj; 41, tuple; 41, 42; 41, 43; 41, 44; 42, identifier:bytes; 43, identifier:bytearray; 44, identifier:memoryview; 45, block; 45, 46; 46, return_statement; 46, 47; 47, call; 47, 48; 47, 56; 48, attribute; 48, 49; 48, 55; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:base64; 52, identifier:b64encode; 53, argument_list; 53, 54; 54, identifier:obj; 55, identifier:decode; 56, argument_list; 56, 57; 57, string:'ASCII'; 58, raise_statement; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:TypeError; 61, argument_list; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, string:'{!r} is not JSON serializable'; 65, identifier:format; 66, argument_list; 66, 67; 67, identifier:obj | def dump_object(self, obj):
"""
Called to encode unrecognized object.
:param object obj: the object to encode
:return: the encoded object
:raises TypeError: when `obj` cannot be encoded
This method is passed as the ``default`` keyword parameter
to :func:`json.dumps`. It provides default representations for
a number of Python language/standard library types.
+----------------------------+---------------------------------------+
| Python Type | String Format |
+----------------------------+---------------------------------------+
| :class:`bytes`, | Base64 encoded string. |
| :class:`bytearray`, | |
| :class:`memoryview` | |
+----------------------------+---------------------------------------+
| :class:`datetime.datetime` | ISO8601 formatted timestamp in the |
| | extended format including separators, |
| | milliseconds, and the timezone |
| | designator. |
+----------------------------+---------------------------------------+
| :class:`uuid.UUID` | Same as ``str(value)`` |
+----------------------------+---------------------------------------+
"""
if isinstance(obj, uuid.UUID):
return str(obj)
if hasattr(obj, 'isoformat'):
return obj.isoformat()
if isinstance(obj, (bytes, bytearray, memoryview)):
return base64.b64encode(obj).decode('ASCII')
raise TypeError('{!r} is not JSON serializable'.format(obj)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:normalize_datum; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:datum; 6, block; 6, 7; 6, 9; 6, 16; 6, 27; 6, 43; 6, 57; 6, 72; 6, 87; 6, 98; 6, 122; 6, 158; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 10, comparison_operator:is; 10, 11; 10, 12; 11, identifier:datum; 12, None; 13, block; 13, 14; 14, return_statement; 14, 15; 15, identifier:datum; 16, if_statement; 16, 17; 16, 24; 17, call; 17, 18; 17, 19; 18, identifier:isinstance; 19, argument_list; 19, 20; 19, 21; 20, identifier:datum; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:PACKABLE_TYPES; 24, block; 24, 25; 25, return_statement; 25, 26; 26, identifier:datum; 27, if_statement; 27, 28; 27, 35; 28, call; 28, 29; 28, 30; 29, identifier:isinstance; 30, argument_list; 30, 31; 30, 32; 31, identifier:datum; 32, attribute; 32, 33; 32, 34; 33, identifier:uuid; 34, identifier:UUID; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:datum; 39, call; 39, 40; 39, 41; 40, identifier:str; 41, argument_list; 41, 42; 42, identifier:datum; 43, if_statement; 43, 44; 43, 49; 44, call; 44, 45; 44, 46; 45, identifier:isinstance; 46, argument_list; 46, 47; 46, 48; 47, identifier:datum; 48, identifier:bytearray; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:datum; 53, call; 53, 54; 53, 55; 54, identifier:bytes; 55, argument_list; 55, 56; 56, identifier:datum; 57, if_statement; 57, 58; 57, 63; 58, call; 58, 59; 58, 60; 59, identifier:isinstance; 60, argument_list; 60, 61; 60, 62; 61, identifier:datum; 62, identifier:memoryview; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:datum; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:datum; 70, identifier:tobytes; 71, argument_list; 72, if_statement; 72, 73; 72, 78; 73, call; 73, 74; 73, 75; 74, identifier:hasattr; 75, argument_list; 75, 76; 75, 77; 76, identifier:datum; 77, string:'isoformat'; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:datum; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:datum; 85, identifier:isoformat; 86, argument_list; 87, if_statement; 87, 88; 87, 95; 88, call; 88, 89; 88, 90; 89, identifier:isinstance; 90, argument_list; 90, 91; 90, 92; 91, identifier:datum; 92, tuple; 92, 93; 92, 94; 93, identifier:bytes; 94, identifier:str; 95, block; 95, 96; 96, return_statement; 96, 97; 97, identifier:datum; 98, if_statement; 98, 99; 98, 110; 99, call; 99, 100; 99, 101; 100, identifier:isinstance; 101, argument_list; 101, 102; 101, 103; 102, identifier:datum; 103, tuple; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:collections; 106, identifier:Sequence; 107, attribute; 107, 108; 107, 109; 108, identifier:collections; 109, identifier:Set; 110, block; 110, 111; 111, return_statement; 111, 112; 112, list_comprehension; 112, 113; 112, 119; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:normalize_datum; 117, argument_list; 117, 118; 118, identifier:item; 119, for_in_clause; 119, 120; 119, 121; 120, identifier:item; 121, identifier:datum; 122, if_statement; 122, 123; 122, 130; 123, call; 123, 124; 123, 125; 124, identifier:isinstance; 125, argument_list; 125, 126; 125, 127; 126, identifier:datum; 127, attribute; 127, 128; 127, 129; 128, identifier:collections; 129, identifier:Mapping; 130, block; 130, 131; 130, 135; 130, 156; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:out; 134, dictionary; 135, for_statement; 135, 136; 135, 139; 135, 144; 136, pattern_list; 136, 137; 136, 138; 137, identifier:k; 138, identifier:v; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:datum; 142, identifier:items; 143, argument_list; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:out; 149, identifier:k; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:normalize_datum; 154, argument_list; 154, 155; 155, identifier:v; 156, return_statement; 156, 157; 157, identifier:out; 158, raise_statement; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:TypeError; 161, argument_list; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, string:'{} is not msgpackable'; 165, identifier:format; 166, argument_list; 166, 167; 167, attribute; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:datum; 170, identifier:__class__; 171, identifier:__name__ | def normalize_datum(self, datum):
"""
Convert `datum` into something that umsgpack likes.
:param datum: something that we want to process with umsgpack
:return: a packable version of `datum`
:raises TypeError: if `datum` cannot be packed
This message is called by :meth:`.packb` to recursively normalize
an input value before passing it to :func:`umsgpack.packb`. Values
are normalized according to the following table.
+-------------------------------+-------------------------------+
| **Value** | **MsgPack Family** |
+-------------------------------+-------------------------------+
| :data:`None` | `nil byte`_ (0xC0) |
+-------------------------------+-------------------------------+
| :data:`True` | `true byte`_ (0xC3) |
+-------------------------------+-------------------------------+
| :data:`False` | `false byte`_ (0xC2) |
+-------------------------------+-------------------------------+
| :class:`int` | `integer family`_ |
+-------------------------------+-------------------------------+
| :class:`float` | `float family`_ |
+-------------------------------+-------------------------------+
| String | `str family`_ |
+-------------------------------+-------------------------------+
| :class:`bytes` | `bin family`_ |
+-------------------------------+-------------------------------+
| :class:`bytearray` | `bin family`_ |
+-------------------------------+-------------------------------+
| :class:`memoryview` | `bin family`_ |
+-------------------------------+-------------------------------+
| :class:`collections.Sequence` | `array family`_ |
+-------------------------------+-------------------------------+
| :class:`collections.Set` | `array family`_ |
+-------------------------------+-------------------------------+
| :class:`collections.Mapping` | `map family`_ |
+-------------------------------+-------------------------------+
| :class:`uuid.UUID` | Converted to String |
+-------------------------------+-------------------------------+
.. _nil byte: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md#formats-nil
.. _true byte: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md#bool-format-family
.. _false byte: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md#bool-format-family
.. _integer family: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md#int-format-family
.. _float family: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md#float-format-family
.. _str family: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md#str-format-family
.. _array family: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md#array-format-family
.. _map family: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md
#mapping-format-family
.. _bin family: https://github.com/msgpack/msgpack/blob/
0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md#bin-format-family
"""
if datum is None:
return datum
if isinstance(datum, self.PACKABLE_TYPES):
return datum
if isinstance(datum, uuid.UUID):
datum = str(datum)
if isinstance(datum, bytearray):
datum = bytes(datum)
if isinstance(datum, memoryview):
datum = datum.tobytes()
if hasattr(datum, 'isoformat'):
datum = datum.isoformat()
if isinstance(datum, (bytes, str)):
return datum
if isinstance(datum, (collections.Sequence, collections.Set)):
return [self.normalize_datum(item) for item in datum]
if isinstance(datum, collections.Mapping):
out = {}
for k, v in datum.items():
out[k] = self.normalize_datum(v)
return out
raise TypeError(
'{} is not msgpackable'.format(datum.__class__.__name__)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:redirect; 3, parameters; 3, 4; 3, 5; 4, identifier:endpoint; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kw; 7, block; 7, 8; 7, 10; 7, 14; 7, 133; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:_endpoint; 13, None; 14, if_statement; 14, 15; 14, 22; 14, 77; 15, call; 15, 16; 15, 17; 16, identifier:isinstance; 17, argument_list; 17, 18; 17, 19; 18, identifier:endpoint; 19, attribute; 19, 20; 19, 21; 20, identifier:six; 21, identifier:string_types; 22, block; 22, 23; 22, 27; 22, 28; 22, 29; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:_endpoint; 26, identifier:endpoint; 27, comment; 28, comment; 29, if_statement; 29, 30; 29, 33; 29, 39; 30, comparison_operator:in; 30, 31; 30, 32; 31, string:"/"; 32, identifier:endpoint; 33, block; 33, 34; 34, return_statement; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:f_redirect; 37, argument_list; 37, 38; 38, identifier:endpoint; 39, else_clause; 39, 40; 40, block; 40, 41; 41, for_statement; 41, 42; 41, 43; 41, 52; 42, identifier:r; 43, call; 43, 44; 43, 51; 44, attribute; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:Mocha; 48, identifier:_app; 49, identifier:url_map; 50, identifier:iter_rules; 51, argument_list; 52, block; 52, 53; 52, 57; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:_endpoint; 56, identifier:endpoint; 57, if_statement; 57, 58; 57, 69; 58, boolean_operator:and; 58, 59; 58, 64; 59, comparison_operator:in; 59, 60; 59, 61; 60, string:'GET'; 61, attribute; 61, 62; 61, 63; 62, identifier:r; 63, identifier:methods; 64, comparison_operator:in; 64, 65; 64, 66; 65, identifier:endpoint; 66, attribute; 66, 67; 66, 68; 67, identifier:r; 68, identifier:endpoint; 69, block; 69, 70; 69, 76; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:_endpoint; 73, attribute; 73, 74; 73, 75; 74, identifier:r; 75, identifier:endpoint; 76, break_statement; 77, else_clause; 77, 78; 77, 79; 78, comment; 79, block; 79, 80; 79, 109; 80, if_statement; 80, 81; 80, 86; 81, call; 81, 82; 81, 83; 82, identifier:isinstance; 83, argument_list; 83, 84; 83, 85; 84, identifier:endpoint; 85, identifier:Mocha; 86, block; 86, 87; 86, 101; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:fn; 90, attribute; 90, 91; 90, 100; 91, attribute; 91, 92; 91, 99; 92, attribute; 92, 93; 92, 98; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:sys; 96, identifier:_getframe; 97, argument_list; 98, identifier:f_back; 99, identifier:f_code; 100, identifier:co_name; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:endpoint; 104, call; 104, 105; 104, 106; 105, identifier:getattr; 106, argument_list; 106, 107; 106, 108; 107, identifier:endpoint; 108, identifier:fn; 109, if_statement; 109, 110; 109, 114; 110, call; 110, 111; 110, 112; 111, identifier:is_method; 112, argument_list; 112, 113; 113, identifier:endpoint; 114, block; 114, 115; 114, 122; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:_endpoint; 118, call; 118, 119; 118, 120; 119, identifier:_get_action_endpoint; 120, argument_list; 120, 121; 121, identifier:endpoint; 122, if_statement; 122, 123; 122, 125; 123, not_operator; 123, 124; 124, identifier:_endpoint; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:_endpoint; 129, call; 129, 130; 129, 131; 130, identifier:_build_endpoint_route_name; 131, argument_list; 131, 132; 132, identifier:endpoint; 133, if_statement; 133, 134; 133, 135; 133, 146; 134, identifier:_endpoint; 135, block; 135, 136; 136, return_statement; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:f_redirect; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:url_for; 142, argument_list; 142, 143; 142, 144; 143, identifier:_endpoint; 144, dictionary_splat; 144, 145; 145, identifier:kw; 146, else_clause; 146, 147; 147, block; 147, 148; 148, raise_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:exceptions; 152, identifier:MochaError; 153, argument_list; 153, 154; 154, string:"Invalid endpoint" | def redirect(endpoint, **kw):
"""
Redirect allow to redirect dynamically using the classes methods without
knowing the right endpoint.
Expecting all endpoint have GET as method, it will try to pick the first
match, based on the endpoint provided or the based on the Rule map_url
An endpoint can also be passed along with **kw
An http: or https: can also be passed, and will redirect to that site.
example:
redirect(self.hello_world)
redirect(self.other_page, name="x", value="v")
redirect("https://google.com")
redirect(views.ContactPage.index)
:param endpoint:
:return: redirect url
"""
_endpoint = None
if isinstance(endpoint, six.string_types):
_endpoint = endpoint
# valid for https:// or /path/
# Endpoint should not have slashes. Use : (colon) to build endpoint
if "/" in endpoint:
return f_redirect(endpoint)
else:
for r in Mocha._app.url_map.iter_rules():
_endpoint = endpoint
if 'GET' in r.methods and endpoint in r.endpoint:
_endpoint = r.endpoint
break
else:
# self, will refer the caller method, by getting the method name
if isinstance(endpoint, Mocha):
fn = sys._getframe().f_back.f_code.co_name
endpoint = getattr(endpoint, fn)
if is_method(endpoint):
_endpoint = _get_action_endpoint(endpoint)
if not _endpoint:
_endpoint = _build_endpoint_route_name(endpoint)
if _endpoint:
return f_redirect(url_for(_endpoint, **kw))
else:
raise exceptions.MochaError("Invalid endpoint") |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_search; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:query; 6, identifier:search_term; 7, block; 7, 8; 7, 10; 7, 18; 7, 26; 7, 35; 7, 36; 7, 84; 7, 336; 7, 337; 7, 338; 7, 339; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:criterias; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:mongoengine; 16, identifier:Q; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:rel_criterias; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:mongoengine; 24, identifier:Q; 25, argument_list; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:terms; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:shlex; 32, identifier:split; 33, argument_list; 33, 34; 34, identifier:search_term; 35, comment; 36, if_statement; 36, 37; 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:terms; 43, integer:1; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:re; 47, identifier:match; 48, argument_list; 48, 49; 48, 50; 49, identifier:RE_OBJECTID; 50, subscript; 50, 51; 50, 52; 51, identifier:terms; 52, integer:0; 53, block; 53, 54; 53, 72; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:q; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:query; 60, identifier:filter; 61, argument_list; 61, 62; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:id; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:bson; 67, identifier:ObjectId; 68, argument_list; 68, 69; 69, subscript; 69, 70; 69, 71; 70, identifier:terms; 71, integer:0; 72, if_statement; 72, 73; 72, 80; 72, 81; 73, comparison_operator:==; 73, 74; 73, 79; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:q; 77, identifier:count; 78, argument_list; 79, integer:1; 80, comment; 81, block; 81, 82; 82, return_statement; 82, 83; 83, identifier:q; 84, for_statement; 84, 85; 84, 86; 84, 87; 85, identifier:term; 86, identifier:terms; 87, block; 87, 88; 87, 97; 87, 98; 87, 107; 87, 115; 87, 332; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 93; 90, pattern_list; 90, 91; 90, 92; 91, identifier:op; 92, identifier:term; 93, call; 93, 94; 93, 95; 94, identifier:parse_like_term; 95, argument_list; 95, 96; 96, identifier:term; 97, comment; 98, if_statement; 98, 99; 98, 102; 99, comparison_operator:==; 99, 100; 99, 101; 100, identifier:op; 101, string:'contains'; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:op; 106, string:'icontains'; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:criteria; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:mongoengine; 113, identifier:Q; 114, argument_list; 115, for_statement; 115, 116; 115, 117; 115, 120; 116, identifier:field; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:_search_fields; 120, block; 120, 121; 121, if_statement; 121, 122; 121, 131; 121, 212; 121, 303; 122, call; 122, 123; 122, 124; 123, identifier:isinstance; 124, argument_list; 124, 125; 124, 126; 125, identifier:field; 126, attribute; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:mongoengine; 129, identifier:fields; 130, identifier:ReferenceField; 131, block; 131, 132; 131, 138; 131, 163; 131, 164; 131, 180; 131, 195; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:rel_model; 135, attribute; 135, 136; 135, 137; 136, identifier:field; 137, identifier:document_type; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:rel_fields; 141, parenthesized_expression; 141, 142; 142, call; 142, 143; 142, 159; 143, attribute; 143, 144; 143, 158; 144, call; 144, 145; 144, 153; 145, attribute; 145, 146; 145, 152; 146, call; 146, 147; 146, 148; 147, identifier:getattr; 148, argument_list; 148, 149; 148, 150; 148, 151; 149, identifier:self; 150, string:'column_searchable_refs'; 151, dictionary; 152, identifier:get; 153, argument_list; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:field; 156, identifier:name; 157, dictionary; 158, identifier:get; 159, argument_list; 159, 160; 159, 161; 160, string:'fields'; 161, list:['id']; 161, 162; 162, string:'id'; 163, comment; 164, if_statement; 164, 165; 164, 178; 165, boolean_operator:and; 165, 166; 165, 170; 166, comparison_operator:==; 166, 167; 166, 168; 167, identifier:rel_fields; 168, list:['id']; 168, 169; 169, string:'id'; 170, not_operator; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:re; 174, identifier:match; 175, argument_list; 175, 176; 175, 177; 176, identifier:RE_OBJECTID; 177, identifier:term; 178, block; 178, 179; 179, continue_statement; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:ids; 183, list_comprehension; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:o; 186, identifier:id; 187, for_in_clause; 187, 188; 187, 189; 188, identifier:o; 189, call; 189, 190; 189, 191; 190, identifier:search_relative_field; 191, argument_list; 191, 192; 191, 193; 191, 194; 192, identifier:rel_model; 193, identifier:rel_fields; 194, identifier:term; 195, expression_statement; 195, 196; 196, augmented_assignment:|=; 196, 197; 196, 198; 197, identifier:rel_criterias; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:mongoengine; 201, identifier:Q; 202, argument_list; 202, 203; 203, dictionary_splat; 203, 204; 204, dictionary; 204, 205; 205, pair; 205, 206; 205, 211; 206, binary_operator:%; 206, 207; 206, 208; 207, string:'%s__in'; 208, attribute; 208, 209; 208, 210; 209, identifier:field; 210, identifier:name; 211, identifier:ids; 212, elif_clause; 212, 213; 212, 222; 213, call; 213, 214; 213, 215; 214, identifier:isinstance; 215, argument_list; 215, 216; 215, 217; 216, identifier:field; 217, attribute; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:mongoengine; 220, identifier:fields; 221, identifier:ListField; 222, block; 222, 223; 222, 239; 222, 247; 222, 271; 222, 286; 223, if_statement; 223, 224; 223, 236; 224, not_operator; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:isinstance; 227, argument_list; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:field; 230, identifier:field; 231, attribute; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:mongoengine; 234, identifier:fields; 235, identifier:ReferenceField; 236, block; 236, 237; 236, 238; 237, continue_statement; 238, comment; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:rel_model; 242, attribute; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:field; 245, identifier:field; 246, identifier:document_type_obj; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:rel_fields; 250, parenthesized_expression; 250, 251; 251, call; 251, 252; 251, 268; 252, attribute; 252, 253; 252, 267; 253, call; 253, 254; 253, 262; 254, attribute; 254, 255; 254, 261; 255, call; 255, 256; 255, 257; 256, identifier:getattr; 257, argument_list; 257, 258; 257, 259; 257, 260; 258, identifier:self; 259, string:'column_searchable_refs'; 260, dictionary; 261, identifier:get; 262, argument_list; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:field; 265, identifier:name; 266, dictionary; 267, identifier:get; 268, argument_list; 268, 269; 268, 270; 269, string:'fields'; 270, string:'id'; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:ids; 274, list_comprehension; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:o; 277, identifier:id; 278, for_in_clause; 278, 279; 278, 280; 279, identifier:o; 280, call; 280, 281; 280, 282; 281, identifier:search_relative_field; 282, argument_list; 282, 283; 282, 284; 282, 285; 283, identifier:rel_model; 284, identifier:rel_fields; 285, identifier:term; 286, expression_statement; 286, 287; 287, augmented_assignment:|=; 287, 288; 287, 289; 288, identifier:rel_criterias; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:mongoengine; 292, identifier:Q; 293, argument_list; 293, 294; 294, dictionary_splat; 294, 295; 295, dictionary; 295, 296; 296, pair; 296, 297; 296, 302; 297, binary_operator:%; 297, 298; 297, 299; 298, string:'%s__in'; 299, attribute; 299, 300; 299, 301; 300, identifier:field; 301, identifier:name; 302, identifier:ids; 303, else_clause; 303, 304; 304, block; 304, 305; 304, 318; 304, 328; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:flt; 308, dictionary; 308, 309; 309, pair; 309, 310; 309, 317; 310, binary_operator:%; 310, 311; 310, 312; 311, string:'%s__%s'; 312, tuple; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:field; 315, identifier:name; 316, identifier:op; 317, identifier:term; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:q; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:mongoengine; 324, identifier:Q; 325, argument_list; 325, 326; 326, dictionary_splat; 326, 327; 327, identifier:flt; 328, expression_statement; 328, 329; 329, augmented_assignment:|=; 329, 330; 329, 331; 330, identifier:criteria; 331, identifier:q; 332, expression_statement; 332, 333; 333, augmented_assignment:&=; 333, 334; 333, 335; 334, identifier:criterias; 335, identifier:criteria; 336, comment; 337, comment; 338, comment; 339, return_statement; 339, 340; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:query; 343, identifier:filter; 344, argument_list; 344, 345; 345, binary_operator:|; 345, 346; 345, 347; 346, identifier:criterias; 347, identifier:rel_criterias | def _search(self, query, search_term):
"""
Improved search between words.
The original _search for MongoEngine dates back to November 12th, 2013 [1]_.
In this ref it's stated that there is a bug with complex Q queries preventing
multi-word searches. During this time, the MongoEngine version was earlier than
0.4 (predating PyPI) [2]_. Since then, there have been multiple releases [3]_
which appear to have fixed the query issue.
Treats id (_id) impliticly as a member of column_searchable_list, except it's
not computed in an OR/AND, a direct lookup is checked for.
References
----------
.. [1] Search for MongoEngine. 02b936b. November 23, 2013.
https://git.io/fxf8C. Accessed September, 29th, 2018.
.. [2] MongoEngine releases on PyPI.
https://pypi.org/project/mongoengine/#history. Accessed September 29th, 2018.
.. [3] MongoEngine release notes. http://docs.mongoengine.org/changelog.html.
Accessed September 29th, 2018.
"""
criterias = mongoengine.Q()
rel_criterias = mongoengine.Q()
terms = shlex.split(search_term)
# If an ObjectId pattern, see if we can get an instant lookup.
if len(terms) == 1 and re.match(RE_OBJECTID, terms[0]):
q = query.filter(id=bson.ObjectId(terms[0]))
if q.count() == 1: # Note: .get doesn't work, they need a QuerySet
return q
for term in terms:
op, term = parse_like_term(term)
# Case insensitive by default
if op == 'contains':
op = 'icontains'
criteria = mongoengine.Q()
for field in self._search_fields:
if isinstance(field, mongoengine.fields.ReferenceField):
rel_model = field.document_type
rel_fields = (
getattr(self, 'column_searchable_refs', {})
.get(field.name, {})
.get('fields', ['id'])
)
# If term isn't an ID, don't do an ID lookup
if rel_fields == ['id'] and not re.match(RE_OBJECTID, term):
continue
ids = [
o.id for o in search_relative_field(rel_model, rel_fields, term)
]
rel_criterias |= mongoengine.Q(**{'%s__in' % field.name: ids})
elif isinstance(field, mongoengine.fields.ListField):
if not isinstance(field.field, mongoengine.fields.ReferenceField):
continue # todo: support lists of other types
rel_model = field.field.document_type_obj
rel_fields = (
getattr(self, 'column_searchable_refs', {})
.get(field.name, {})
.get('fields', 'id')
)
ids = [
o.id for o in search_relative_field(rel_model, rel_fields, term)
]
rel_criterias |= mongoengine.Q(**{'%s__in' % field.name: ids})
else:
flt = {'%s__%s' % (field.name, op): term}
q = mongoengine.Q(**flt)
criteria |= q
criterias &= criteria
# import pprint
# pp = pprint.PrettyPrinter(indent=4).pprint
# print(pp(query.filter(criterias)._query))
return query.filter(criterias | rel_criterias) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:replace_seqres; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:pdb; 6, default_parameter; 6, 7; 6, 8; 7, identifier:update_atoms; 8, True; 9, block; 9, 10; 9, 12; 9, 18; 9, 22; 9, 44; 9, 48; 9, 145; 9, 263; 9, 277; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:newpdb; 15, call; 15, 16; 15, 17; 16, identifier:PDB; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:inserted_seqres; 21, False; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:entries_before_seqres; 25, call; 25, 26; 25, 27; 26, identifier:set; 27, argument_list; 27, 28; 28, list:["HEADER", "OBSLTE", "TITLE", "CAVEAT", "COMPND", "SOURCE",
"KEYWDS", "EXPDTA", "AUTHOR", "REVDAT", "SPRSDE", "JRNL",
"REMARK", "DBREF", "SEQADV"]; 28, 29; 28, 30; 28, 31; 28, 32; 28, 33; 28, 34; 28, 35; 28, 36; 28, 37; 28, 38; 28, 39; 28, 40; 28, 41; 28, 42; 28, 43; 29, string:"HEADER"; 30, string:"OBSLTE"; 31, string:"TITLE"; 32, string:"CAVEAT"; 33, string:"COMPND"; 34, string:"SOURCE"; 35, string:"KEYWDS"; 36, string:"EXPDTA"; 37, string:"AUTHOR"; 38, string:"REVDAT"; 39, string:"SPRSDE"; 40, string:"JRNL"; 41, string:"REMARK"; 42, string:"DBREF"; 43, string:"SEQADV"; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:mutated_resids; 47, dictionary; 48, if_statement; 48, 49; 48, 50; 49, identifier:update_atoms; 50, block; 50, 51; 50, 57; 50, 66; 50, 78; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:old_seqs; 54, call; 54, 55; 54, 56; 55, identifier:ChainSequences; 56, argument_list; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:chainresnums; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:old_seqs; 63, identifier:parse_atoms; 64, argument_list; 64, 65; 65, identifier:pdb; 66, assert_statement; 66, 67; 67, comparison_operator:==; 67, 68; 67, 73; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:keys; 72, argument_list; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:old_seqs; 76, identifier:keys; 77, argument_list; 78, for_statement; 78, 79; 78, 80; 78, 85; 79, identifier:chain; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:keys; 84, argument_list; 85, block; 85, 86; 85, 100; 86, assert_statement; 86, 87; 87, comparison_operator:==; 87, 88; 87, 94; 88, call; 88, 89; 88, 90; 89, identifier:len; 90, argument_list; 90, 91; 91, subscript; 91, 92; 91, 93; 92, identifier:self; 93, identifier:chain; 94, call; 94, 95; 94, 96; 95, identifier:len; 96, argument_list; 96, 97; 97, subscript; 97, 98; 97, 99; 98, identifier:old_seqs; 99, identifier:chain; 100, for_statement; 100, 101; 100, 102; 100, 111; 101, identifier:i; 102, call; 102, 103; 102, 104; 103, identifier:xrange; 104, argument_list; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:len; 107, argument_list; 107, 108; 108, subscript; 108, 109; 108, 110; 109, identifier:self; 110, identifier:chain; 111, block; 111, 112; 112, if_statement; 112, 113; 112, 124; 113, comparison_operator:!=; 113, 114; 113, 119; 114, subscript; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:self; 117, identifier:chain; 118, identifier:i; 119, subscript; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:old_seqs; 122, identifier:chain; 123, identifier:i; 124, block; 124, 125; 124, 135; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:resid; 128, binary_operator:+; 128, 129; 128, 130; 129, identifier:chain; 130, subscript; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:chainresnums; 133, identifier:chain; 134, identifier:i; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:mutated_resids; 139, identifier:resid; 140, subscript; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:self; 143, identifier:chain; 144, identifier:i; 145, for_statement; 145, 146; 145, 147; 145, 150; 146, identifier:line; 147, attribute; 147, 148; 147, 149; 148, identifier:pdb; 149, identifier:lines; 150, block; 150, 151; 150, 160; 150, 183; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:entry; 154, subscript; 154, 155; 154, 156; 155, identifier:line; 156, slice; 156, 157; 156, 158; 156, 159; 157, integer:0; 158, colon; 159, integer:6; 160, if_statement; 160, 161; 160, 168; 161, boolean_operator:and; 161, 162; 161, 165; 162, parenthesized_expression; 162, 163; 163, not_operator; 163, 164; 164, identifier:inserted_seqres; 165, comparison_operator:not; 165, 166; 165, 167; 166, identifier:entry; 167, identifier:entries_before_seqres; 168, block; 168, 169; 168, 173; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:inserted_seqres; 172, True; 173, expression_statement; 173, 174; 174, augmented_assignment:+=; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:newpdb; 177, identifier:lines; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:seqres_lines; 182, argument_list; 183, if_statement; 183, 184; 183, 189; 183, 251; 184, boolean_operator:and; 184, 185; 184, 186; 185, identifier:update_atoms; 186, comparison_operator:==; 186, 187; 186, 188; 187, identifier:entry; 188, string:"ATOM "; 189, block; 189, 190; 189, 199; 189, 212; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:resid; 193, subscript; 193, 194; 193, 195; 194, identifier:line; 195, slice; 195, 196; 195, 197; 195, 198; 196, integer:21; 197, colon; 198, integer:27; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:atom; 202, call; 202, 203; 202, 211; 203, attribute; 203, 204; 203, 210; 204, subscript; 204, 205; 204, 206; 205, identifier:line; 206, slice; 206, 207; 206, 208; 206, 209; 207, integer:12; 208, colon; 209, integer:16; 210, identifier:strip; 211, argument_list; 212, if_statement; 212, 213; 212, 220; 212, 228; 213, not_operator; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:mutated_resids; 217, identifier:has_key; 218, argument_list; 218, 219; 219, identifier:resid; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, augmented_assignment:+=; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:newpdb; 225, identifier:lines; 226, list:[line]; 226, 227; 227, identifier:line; 228, else_clause; 228, 229; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, augmented_assignment:+=; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:newpdb; 234, identifier:lines; 235, list:[line[:17] + mutated_resids[resid] + line[20:]]; 235, 236; 236, binary_operator:+; 236, 237; 236, 246; 237, binary_operator:+; 237, 238; 237, 243; 238, subscript; 238, 239; 238, 240; 239, identifier:line; 240, slice; 240, 241; 240, 242; 241, colon; 242, integer:17; 243, subscript; 243, 244; 243, 245; 244, identifier:mutated_resids; 245, identifier:resid; 246, subscript; 246, 247; 246, 248; 247, identifier:line; 248, slice; 248, 249; 248, 250; 249, integer:20; 250, colon; 251, elif_clause; 251, 252; 251, 255; 252, comparison_operator:!=; 252, 253; 252, 254; 253, identifier:entry; 254, string:"SEQRES"; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, augmented_assignment:+=; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:newpdb; 260, identifier:lines; 261, list:[line]; 261, 262; 262, identifier:line; 263, if_statement; 263, 264; 263, 265; 264, identifier:update_atoms; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:newpdb; 270, identifier:remove_nonbackbone_atoms; 271, argument_list; 271, 272; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:mutated_resids; 275, identifier:keys; 276, argument_list; 277, return_statement; 277, 278; 278, identifier:newpdb | def replace_seqres(self, pdb, update_atoms = True):
"""Replace SEQRES lines with a new sequence, optionally removing
mutated sidechains"""
newpdb = PDB()
inserted_seqres = False
entries_before_seqres = set(["HEADER", "OBSLTE", "TITLE", "CAVEAT", "COMPND", "SOURCE",
"KEYWDS", "EXPDTA", "AUTHOR", "REVDAT", "SPRSDE", "JRNL",
"REMARK", "DBREF", "SEQADV"])
mutated_resids = {}
if update_atoms:
old_seqs = ChainSequences()
chainresnums = old_seqs.parse_atoms(pdb)
assert self.keys() == old_seqs.keys()
for chain in self.keys():
assert len(self[chain]) == len(old_seqs[chain])
for i in xrange(len(self[chain])):
if self[chain][i] != old_seqs[chain][i]:
resid = chain + chainresnums[chain][i]
mutated_resids[resid] = self[chain][i]
for line in pdb.lines:
entry = line[0:6]
if (not inserted_seqres) and entry not in entries_before_seqres:
inserted_seqres = True
newpdb.lines += self.seqres_lines()
if update_atoms and entry == "ATOM ":
resid = line[21:27]
atom = line[12:16].strip()
if not mutated_resids.has_key(resid):
newpdb.lines += [line]
else:
newpdb.lines += [line[:17] + mutated_resids[resid] + line[20:]]
elif entry != "SEQRES":
newpdb.lines += [line]
if update_atoms:
newpdb.remove_nonbackbone_atoms(mutated_resids.keys())
return newpdb |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:enrich_json_objects_by_object_type; 3, parameters; 3, 4; 3, 5; 4, identifier:request; 5, identifier:value; 6, block; 6, 7; 6, 9; 6, 15; 6, 64; 6, 76; 6, 216; 6, 232; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:time_start_globally; 12, call; 12, 13; 12, 14; 13, identifier:time; 14, argument_list; 15, if_statement; 15, 16; 15, 21; 15, 41; 16, call; 16, 17; 16, 18; 17, identifier:isinstance; 18, argument_list; 18, 19; 18, 20; 19, identifier:value; 20, identifier:list; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:json; 25, list_comprehension; 25, 26; 25, 38; 26, conditional_expression:if; 26, 27; 26, 32; 26, 37; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:x; 30, identifier:to_json; 31, argument_list; 32, call; 32, 33; 32, 34; 33, identifier:hasattr; 34, argument_list; 34, 35; 34, 36; 35, identifier:x; 36, string:"to_json"; 37, identifier:x; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:x; 40, identifier:value; 41, else_clause; 41, 42; 42, block; 42, 43; 43, if_statement; 43, 44; 43, 49; 43, 54; 44, call; 44, 45; 44, 46; 45, identifier:isinstance; 46, argument_list; 46, 47; 46, 48; 47, identifier:value; 48, identifier:dict; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:json; 53, identifier:value; 54, else_clause; 54, 55; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:json; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:value; 62, identifier:to_json; 63, argument_list; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, pattern_list; 66, 67; 66, 68; 67, identifier:objects; 68, identifier:nested; 69, call; 69, 70; 69, 71; 70, identifier:_collect_json_objects; 71, argument_list; 71, 72; 71, 73; 72, identifier:json; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:by; 75, string:'object_type'; 76, for_statement; 76, 77; 76, 78; 76, 81; 77, identifier:enricher_info; 78, call; 78, 79; 78, 80; 79, identifier:_get_OBJECT_TYPE_ENRICHER_ORDER; 80, argument_list; 81, block; 81, 82; 81, 154; 82, if_statement; 82, 83; 82, 91; 82, 130; 83, comparison_operator:>; 83, 84; 83, 90; 84, call; 84, 85; 84, 86; 85, identifier:len; 86, argument_list; 86, 87; 87, subscript; 87, 88; 87, 89; 88, identifier:enricher_info; 89, string:'object_types'; 90, integer:0; 91, block; 91, 92; 91, 111; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:enricher_objects; 95, call; 95, 96; 95, 97; 96, identifier:flatten; 97, argument_list; 97, 98; 98, list_comprehension; 98, 99; 98, 106; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:objects; 102, identifier:get; 103, argument_list; 103, 104; 103, 105; 104, identifier:object_type; 105, list:[]; 106, for_in_clause; 106, 107; 106, 108; 107, identifier:object_type; 108, subscript; 108, 109; 108, 110; 109, identifier:enricher_info; 110, string:'object_types'; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:enricher_nested; 114, call; 114, 115; 114, 116; 115, identifier:any; 116, argument_list; 116, 117; 117, list_comprehension; 117, 118; 117, 125; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:nested; 121, identifier:get; 122, argument_list; 122, 123; 122, 124; 123, identifier:object_type; 124, False; 125, for_in_clause; 125, 126; 125, 127; 126, identifier:object_type; 127, subscript; 127, 128; 127, 129; 128, identifier:enricher_info; 129, string:'object_types'; 130, else_clause; 130, 131; 131, block; 131, 132; 131, 143; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:enricher_objects; 135, call; 135, 136; 135, 137; 136, identifier:flatten; 137, argument_list; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:objects; 141, identifier:values; 142, argument_list; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:enricher_nested; 146, call; 146, 147; 146, 148; 147, identifier:any; 148, argument_list; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:nested; 152, identifier:values; 153, argument_list; 154, if_statement; 154, 155; 154, 161; 155, comparison_operator:>; 155, 156; 155, 160; 156, call; 156, 157; 156, 158; 157, identifier:len; 158, argument_list; 158, 159; 159, identifier:enricher_objects; 160, integer:0; 161, block; 161, 162; 161, 168; 161, 177; 161, 196; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:time_start; 165, call; 165, 166; 165, 167; 166, identifier:time; 167, argument_list; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:enricher_info; 172, string:'enricher'; 173, argument_list; 173, 174; 173, 175; 173, 176; 174, identifier:request; 175, identifier:enricher_objects; 176, identifier:enricher_nested; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:LOGGER; 181, identifier:debug; 182, argument_list; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, string:'enrichment "{}" took {} seconds'; 186, identifier:format; 187, argument_list; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:enricher_info; 190, string:'enricher_name'; 191, binary_operator:-; 191, 192; 191, 195; 192, call; 192, 193; 192, 194; 193, identifier:time; 194, argument_list; 195, identifier:time_start; 196, if_statement; 196, 197; 196, 201; 196, 202; 196, 203; 197, not_operator; 197, 198; 198, subscript; 198, 199; 198, 200; 199, identifier:enricher_info; 200, string:'pure'; 201, comment; 202, comment; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 209; 206, pattern_list; 206, 207; 206, 208; 207, identifier:objects; 208, identifier:nested; 209, call; 209, 210; 209, 211; 210, identifier:_collect_json_objects; 211, argument_list; 211, 212; 211, 213; 212, identifier:json; 213, keyword_argument; 213, 214; 213, 215; 214, identifier:by; 215, string:'object_type'; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:LOGGER; 220, identifier:debug; 221, argument_list; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, string:'The whole enrichment of json objects by their object_type took {} seconds.'; 225, identifier:format; 226, argument_list; 226, 227; 227, binary_operator:-; 227, 228; 227, 231; 228, call; 228, 229; 228, 230; 229, identifier:time; 230, argument_list; 231, identifier:time_start_globally; 232, return_statement; 232, 233; 233, identifier:json | def enrich_json_objects_by_object_type(request, value):
"""
Take the given value and start enrichment by object_type. The va
Args:
request (django.http.request.HttpRequest): request which is currently processed
value (dict|list|django.db.models.Model):
in case of django.db.models.Model object (or list of these
objects), to_json method is invoked
Returns:
dict|list
"""
time_start_globally = time()
if isinstance(value, list):
json = [x.to_json() if hasattr(x, "to_json") else x for x in value]
else:
if isinstance(value, dict):
json = value
else:
json = value.to_json()
objects, nested = _collect_json_objects(json, by='object_type')
for enricher_info in _get_OBJECT_TYPE_ENRICHER_ORDER():
if len(enricher_info['object_types']) > 0:
enricher_objects = flatten([objects.get(object_type, []) for object_type in enricher_info['object_types']])
enricher_nested = any([nested.get(object_type, False) for object_type in enricher_info['object_types']])
else:
enricher_objects = flatten(objects.values())
enricher_nested = any(nested.values())
if len(enricher_objects) > 0:
time_start = time()
enricher_info['enricher'](request, enricher_objects, enricher_nested)
LOGGER.debug('enrichment "{}" took {} seconds'.format(enricher_info['enricher_name'], time() - time_start))
if not enricher_info['pure']:
# if the enricher modified object types we must collect objects
# again
objects, nested = _collect_json_objects(json, by='object_type')
LOGGER.debug('The whole enrichment of json objects by their object_type took {} seconds.'.format(time() - time_start_globally))
return json |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:change_parent; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:sender; 5, identifier:instance; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 19; 8, 133; 8, 260; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 17; 12, comparison_operator:is; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:instance; 15, identifier:id; 16, None; 17, block; 17, 18; 18, return_statement; 19, if_statement; 19, 20; 19, 35; 20, comparison_operator:!=; 20, 21; 20, 34; 21, call; 21, 22; 21, 23; 22, identifier:len; 23, argument_list; 23, 24; 24, binary_operator:&; 24, 25; 24, 28; 25, set; 25, 26; 25, 27; 26, string:'term'; 27, string:'term_id'; 28, call; 28, 29; 28, 30; 29, identifier:set; 30, argument_list; 30, 31; 31, attribute; 31, 32; 31, 33; 32, identifier:instance; 33, identifier:changed_fields; 34, integer:0; 35, block; 35, 36; 35, 42; 35, 59; 35, 65; 35, 112; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:diff; 39, attribute; 39, 40; 39, 41; 40, identifier:instance; 41, identifier:diff; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:parent; 45, conditional_expression:if; 45, 46; 45, 51; 45, 54; 46, subscript; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:diff; 49, string:'term'; 50, integer:0; 51, comparison_operator:in; 51, 52; 51, 53; 52, string:'term'; 53, identifier:diff; 54, subscript; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:diff; 57, string:'term_id'; 58, integer:0; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:child_id; 62, attribute; 62, 63; 62, 64; 63, identifier:instance; 64, identifier:item_id; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:parent; 68, None; 69, block; 69, 70; 69, 94; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:parent_id; 73, conditional_expression:if; 73, 74; 73, 77; 73, 82; 74, attribute; 74, 75; 74, 76; 75, identifier:parent; 76, identifier:item_id; 77, call; 77, 78; 77, 79; 78, identifier:isinstance; 79, argument_list; 79, 80; 79, 81; 80, identifier:parent; 81, identifier:Term; 82, attribute; 82, 83; 82, 93; 83, call; 83, 84; 83, 89; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:Term; 87, identifier:objects; 88, identifier:get; 89, argument_list; 89, 90; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:pk; 92, identifier:parent; 93, identifier:item_id; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 111; 96, attribute; 96, 97; 96, 110; 97, call; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:ItemRelation; 101, identifier:objects; 102, identifier:filter; 103, argument_list; 103, 104; 103, 107; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:parent_id; 106, identifier:parent_id; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:child_id; 109, identifier:child_id; 110, identifier:delete; 111, argument_list; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:ItemRelation; 117, identifier:objects; 118, identifier:get_or_create; 119, argument_list; 119, 120; 119, 127; 119, 130; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:parent_id; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:instance; 125, identifier:term; 126, identifier:item_id; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:child_id; 129, identifier:child_id; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:visible; 132, True; 133, if_statement; 133, 134; 133, 149; 134, comparison_operator:!=; 134, 135; 134, 148; 135, call; 135, 136; 135, 137; 136, identifier:len; 137, argument_list; 137, 138; 138, binary_operator:&; 138, 139; 138, 142; 139, set; 139, 140; 139, 141; 140, string:'term_secondary'; 141, string:'term_secondary_id'; 142, call; 142, 143; 142, 144; 143, identifier:set; 144, argument_list; 144, 145; 145, attribute; 145, 146; 145, 147; 146, identifier:instance; 147, identifier:changed_fields; 148, integer:0; 149, block; 149, 150; 149, 156; 149, 162; 149, 179; 149, 226; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:diff; 153, attribute; 153, 154; 153, 155; 154, identifier:instance; 155, identifier:diff; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:child_id; 159, attribute; 159, 160; 159, 161; 160, identifier:instance; 161, identifier:item_id; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:parent; 165, conditional_expression:if; 165, 166; 165, 171; 165, 174; 166, subscript; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:diff; 169, string:'term_secondary'; 170, integer:0; 171, comparison_operator:in; 171, 172; 171, 173; 172, string:'term_secondary'; 173, identifier:diff; 174, subscript; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:diff; 177, string:'term_secondary_id'; 178, integer:0; 179, if_statement; 179, 180; 179, 183; 180, comparison_operator:is; 180, 181; 180, 182; 181, identifier:parent; 182, None; 183, block; 183, 184; 183, 208; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:parent_id; 187, conditional_expression:if; 187, 188; 187, 191; 187, 196; 188, attribute; 188, 189; 188, 190; 189, identifier:parent; 190, identifier:item_id; 191, call; 191, 192; 191, 193; 192, identifier:isinstance; 193, argument_list; 193, 194; 193, 195; 194, identifier:parent; 195, identifier:Term; 196, attribute; 196, 197; 196, 207; 197, call; 197, 198; 197, 203; 198, attribute; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:Term; 201, identifier:objects; 202, identifier:get; 203, argument_list; 203, 204; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:pk; 206, identifier:parent; 207, identifier:item_id; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 225; 210, attribute; 210, 211; 210, 224; 211, call; 211, 212; 211, 217; 212, attribute; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:ItemRelation; 215, identifier:objects; 216, identifier:filter; 217, argument_list; 217, 218; 217, 221; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:parent_id; 220, identifier:parent_id; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:child_id; 223, identifier:child_id; 224, identifier:delete; 225, argument_list; 226, if_statement; 226, 227; 226, 238; 227, boolean_operator:or; 227, 228; 227, 233; 228, comparison_operator:is; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:instance; 231, identifier:term_secondary; 232, None; 233, comparison_operator:is; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:instance; 236, identifier:term_secondary_id; 237, None; 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:ItemRelation; 244, identifier:objects; 245, identifier:get_or_create; 246, argument_list; 246, 247; 246, 254; 246, 257; 247, keyword_argument; 247, 248; 247, 249; 248, identifier:parent_id; 249, attribute; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:instance; 252, identifier:term_secondary; 253, identifier:item_id; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:child_id; 256, identifier:child_id; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:visible; 259, True; 260, if_statement; 260, 261; 260, 276; 261, comparison_operator:!=; 261, 262; 261, 275; 262, call; 262, 263; 262, 264; 263, identifier:len; 264, argument_list; 264, 265; 265, binary_operator:&; 265, 266; 265, 269; 266, set; 266, 267; 266, 268; 267, string:'context'; 268, string:'context_id'; 269, call; 269, 270; 269, 271; 270, identifier:set; 271, argument_list; 271, 272; 272, attribute; 272, 273; 272, 274; 273, identifier:instance; 274, identifier:changed_fields; 275, integer:0; 276, block; 276, 277; 276, 283; 276, 300; 276, 306; 276, 353; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:diff; 280, attribute; 280, 281; 280, 282; 281, identifier:instance; 282, identifier:diff; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:parent; 286, conditional_expression:if; 286, 287; 286, 292; 286, 295; 287, subscript; 287, 288; 287, 291; 288, subscript; 288, 289; 288, 290; 289, identifier:diff; 290, string:'context'; 291, integer:0; 292, comparison_operator:in; 292, 293; 292, 294; 293, string:'context'; 294, identifier:diff; 295, subscript; 295, 296; 295, 299; 296, subscript; 296, 297; 296, 298; 297, identifier:diff; 298, string:'context_id'; 299, integer:0; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:child_id; 303, attribute; 303, 304; 303, 305; 304, identifier:instance; 305, identifier:item_id; 306, if_statement; 306, 307; 306, 310; 307, comparison_operator:is; 307, 308; 307, 309; 308, identifier:parent; 309, None; 310, block; 310, 311; 310, 335; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:parent_id; 314, conditional_expression:if; 314, 315; 314, 318; 314, 323; 315, attribute; 315, 316; 315, 317; 316, identifier:parent; 317, identifier:item_id; 318, call; 318, 319; 318, 320; 319, identifier:isinstance; 320, argument_list; 320, 321; 320, 322; 321, identifier:parent; 322, identifier:Context; 323, attribute; 323, 324; 323, 334; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:Context; 328, identifier:objects; 329, identifier:get; 330, argument_list; 330, 331; 331, keyword_argument; 331, 332; 331, 333; 332, identifier:pk; 333, identifier:parent; 334, identifier:item_id; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 352; 337, attribute; 337, 338; 337, 351; 338, call; 338, 339; 338, 344; 339, attribute; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:ItemRelation; 342, identifier:objects; 343, identifier:filter; 344, argument_list; 344, 345; 344, 348; 345, keyword_argument; 345, 346; 345, 347; 346, identifier:parent_id; 347, identifier:parent_id; 348, keyword_argument; 348, 349; 348, 350; 349, identifier:child_id; 350, identifier:child_id; 351, identifier:delete; 352, argument_list; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 360; 355, attribute; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:ItemRelation; 358, identifier:objects; 359, identifier:get_or_create; 360, argument_list; 360, 361; 360, 368; 360, 371; 361, keyword_argument; 361, 362; 361, 363; 362, identifier:parent_id; 363, attribute; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:instance; 366, identifier:context; 367, identifier:item_id; 368, keyword_argument; 368, 369; 368, 370; 369, identifier:child_id; 370, identifier:child_id; 371, keyword_argument; 371, 372; 371, 373; 372, identifier:visible; 373, True | def change_parent(sender, instance, **kwargs):
"""
When the given flashcard has changed. Look at term and context and change
the corresponding item relation.
"""
if instance.id is None:
return
if len({'term', 'term_id'} & set(instance.changed_fields)) != 0:
diff = instance.diff
parent = diff['term'][0] if 'term' in diff else diff['term_id'][0]
child_id = instance.item_id
if parent is not None:
parent_id = parent.item_id if isinstance(parent, Term) else Term.objects.get(pk=parent).item_id
ItemRelation.objects.filter(parent_id=parent_id, child_id=child_id).delete()
ItemRelation.objects.get_or_create(parent_id=instance.term.item_id, child_id=child_id, visible=True)
if len({'term_secondary', 'term_secondary_id'} & set(instance.changed_fields)) != 0:
diff = instance.diff
child_id = instance.item_id
parent = diff['term_secondary'][0] if 'term_secondary' in diff else diff['term_secondary_id'][0]
if parent is not None:
parent_id = parent.item_id if isinstance(parent, Term) else Term.objects.get(pk=parent).item_id
ItemRelation.objects.filter(parent_id=parent_id, child_id=child_id).delete()
if instance.term_secondary is not None or instance.term_secondary_id is not None:
ItemRelation.objects.get_or_create(parent_id=instance.term_secondary.item_id, child_id=child_id, visible=True)
if len({'context', 'context_id'} & set(instance.changed_fields)) != 0:
diff = instance.diff
parent = diff['context'][0] if 'context' in diff else diff['context_id'][0]
child_id = instance.item_id
if parent is not None:
parent_id = parent.item_id if isinstance(parent, Context) else Context.objects.get(pk=parent).item_id
ItemRelation.objects.filter(parent_id=parent_id, child_id=child_id).delete()
ItemRelation.objects.get_or_create(parent_id=instance.context.item_id, child_id=child_id, visible=True) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:bin_atoms; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 27; 5, 45; 5, 61; 5, 91; 5, 95; 5, 163; 5, 164; 5, 233; 5, 234; 5, 302; 5, 303; 5, 307; 5, 363; 6, expression_statement; 6, 7; 7, string:'''This function bins the Atoms into fixed-size sections of the protein space in 3D.'''; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:low_point; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:numpy; 15, identifier:array; 16, argument_list; 16, 17; 17, list:[self.min_x, self.min_y, self.min_z]; 17, 18; 17, 21; 17, 24; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:min_x; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:min_y; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:min_z; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:high_point; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:numpy; 33, identifier:array; 34, argument_list; 34, 35; 35, list:[self.max_x, self.max_y, self.max_z]; 35, 36; 35, 39; 35, 42; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:max_x; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:max_y; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:max_z; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:atom_bin_dimensions; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:numpy; 51, identifier:ceil; 52, argument_list; 52, 53; 53, binary_operator:/; 53, 54; 53, 58; 54, parenthesized_expression; 54, 55; 55, binary_operator:-; 55, 56; 55, 57; 56, identifier:high_point; 57, identifier:low_point; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:bin_size; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:atom_bin_dimensions; 66, tuple; 66, 67; 66, 75; 66, 83; 67, binary_operator:-; 67, 68; 67, 74; 68, call; 68, 69; 68, 70; 69, identifier:int; 70, argument_list; 70, 71; 71, subscript; 71, 72; 71, 73; 72, identifier:atom_bin_dimensions; 73, integer:0; 74, integer:1; 75, binary_operator:-; 75, 76; 75, 82; 76, call; 76, 77; 76, 78; 77, identifier:int; 78, argument_list; 78, 79; 79, subscript; 79, 80; 79, 81; 80, identifier:atom_bin_dimensions; 81, integer:1; 82, integer:1; 83, binary_operator:-; 83, 84; 83, 90; 84, call; 84, 85; 84, 86; 85, identifier:int; 86, argument_list; 86, 87; 87, subscript; 87, 88; 87, 89; 88, identifier:atom_bin_dimensions; 89, integer:2; 90, integer:1; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:atom_bins; 94, list:[]; 95, for_statement; 95, 96; 95, 97; 95, 106; 96, identifier:x; 97, call; 97, 98; 97, 99; 98, identifier:range; 99, argument_list; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:int; 102, argument_list; 102, 103; 103, subscript; 103, 104; 103, 105; 104, identifier:atom_bin_dimensions; 105, integer:0; 106, block; 106, 107; 106, 114; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:atom_bins; 111, identifier:append; 112, argument_list; 112, 113; 113, list:[]; 114, for_statement; 114, 115; 114, 116; 114, 125; 115, identifier:y; 116, call; 116, 117; 116, 118; 117, identifier:range; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:int; 121, argument_list; 121, 122; 122, subscript; 122, 123; 122, 124; 123, identifier:atom_bin_dimensions; 124, integer:1; 125, block; 125, 126; 125, 135; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:atom_bins; 131, identifier:x; 132, identifier:append; 133, argument_list; 133, 134; 134, list:[]; 135, for_statement; 135, 136; 135, 137; 135, 146; 136, identifier:z; 137, call; 137, 138; 137, 139; 138, identifier:range; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:int; 142, argument_list; 142, 143; 143, subscript; 143, 144; 143, 145; 144, identifier:atom_bin_dimensions; 145, integer:2; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 156; 149, attribute; 149, 150; 149, 155; 150, subscript; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:atom_bins; 153, identifier:x; 154, identifier:y; 155, identifier:append; 156, argument_list; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:Bin; 159, argument_list; 159, 160; 159, 161; 159, 162; 160, identifier:x; 161, identifier:y; 162, identifier:z; 163, comment; 164, for_statement; 164, 165; 164, 168; 164, 175; 165, pattern_list; 165, 166; 165, 167; 166, identifier:serial_number; 167, identifier:atom; 168, call; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:atoms; 173, identifier:iteritems; 174, argument_list; 175, block; 175, 176; 175, 194; 175, 219; 175, 226; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:bin_location; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:numpy; 182, identifier:trunc; 183, argument_list; 183, 184; 184, binary_operator:/; 184, 185; 184, 191; 185, parenthesized_expression; 185, 186; 186, binary_operator:-; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:atom; 189, identifier:point; 190, identifier:low_point; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:bin_size; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:bin; 197, subscript; 197, 198; 197, 213; 198, subscript; 198, 199; 198, 207; 199, subscript; 199, 200; 199, 201; 200, identifier:atom_bins; 201, call; 201, 202; 201, 203; 202, identifier:int; 203, argument_list; 203, 204; 204, subscript; 204, 205; 204, 206; 205, identifier:bin_location; 206, integer:0; 207, call; 207, 208; 207, 209; 208, identifier:int; 209, argument_list; 209, 210; 210, subscript; 210, 211; 210, 212; 211, identifier:bin_location; 212, integer:1; 213, call; 213, 214; 213, 215; 214, identifier:int; 215, argument_list; 215, 216; 216, subscript; 216, 217; 216, 218; 217, identifier:bin_location; 218, integer:2; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:bin; 223, identifier:append; 224, argument_list; 224, 225; 225, identifier:atom; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:atom; 230, identifier:set_bin; 231, argument_list; 231, 232; 232, identifier:bin; 233, comment; 234, if_statement; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:self; 237, identifier:safe_mode; 238, block; 238, 239; 238, 243; 238, 292; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:num_atoms; 242, integer:0; 243, for_statement; 243, 244; 243, 245; 243, 254; 244, identifier:x; 245, call; 245, 246; 245, 247; 246, identifier:range; 247, argument_list; 247, 248; 248, call; 248, 249; 248, 250; 249, identifier:int; 250, argument_list; 250, 251; 251, subscript; 251, 252; 251, 253; 252, identifier:atom_bin_dimensions; 253, integer:0; 254, block; 254, 255; 255, for_statement; 255, 256; 255, 257; 255, 266; 256, identifier:y; 257, call; 257, 258; 257, 259; 258, identifier:range; 259, argument_list; 259, 260; 260, call; 260, 261; 260, 262; 261, identifier:int; 262, argument_list; 262, 263; 263, subscript; 263, 264; 263, 265; 264, identifier:atom_bin_dimensions; 265, integer:1; 266, block; 266, 267; 267, for_statement; 267, 268; 267, 269; 267, 278; 268, identifier:z; 269, call; 269, 270; 269, 271; 270, identifier:range; 271, argument_list; 271, 272; 272, call; 272, 273; 272, 274; 273, identifier:int; 274, argument_list; 274, 275; 275, subscript; 275, 276; 275, 277; 276, identifier:atom_bin_dimensions; 277, integer:2; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, augmented_assignment:+=; 280, 281; 280, 282; 281, identifier:num_atoms; 282, call; 282, 283; 282, 284; 283, identifier:len; 284, argument_list; 284, 285; 285, subscript; 285, 286; 285, 291; 286, subscript; 286, 287; 286, 290; 287, subscript; 287, 288; 287, 289; 288, identifier:atom_bins; 289, identifier:x; 290, identifier:y; 291, identifier:z; 292, assert_statement; 292, 293; 293, parenthesized_expression; 293, 294; 294, comparison_operator:==; 294, 295; 294, 296; 295, identifier:num_atoms; 296, call; 296, 297; 296, 298; 297, identifier:len; 298, argument_list; 298, 299; 299, attribute; 299, 300; 299, 301; 300, identifier:self; 301, identifier:atoms; 302, comment; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:blank_section; 306, tuple; 307, for_statement; 307, 308; 307, 309; 307, 318; 308, identifier:x; 309, call; 309, 310; 309, 311; 310, identifier:range; 311, argument_list; 311, 312; 312, call; 312, 313; 312, 314; 313, identifier:int; 314, argument_list; 314, 315; 315, subscript; 315, 316; 315, 317; 316, identifier:atom_bin_dimensions; 317, integer:0; 318, block; 318, 319; 319, for_statement; 319, 320; 319, 321; 319, 330; 320, identifier:y; 321, call; 321, 322; 321, 323; 322, identifier:range; 323, argument_list; 323, 324; 324, call; 324, 325; 324, 326; 325, identifier:int; 326, argument_list; 326, 327; 327, subscript; 327, 328; 327, 329; 328, identifier:atom_bin_dimensions; 329, integer:1; 330, block; 330, 331; 331, for_statement; 331, 332; 331, 333; 331, 342; 332, identifier:z; 333, call; 333, 334; 333, 335; 334, identifier:range; 335, argument_list; 335, 336; 336, call; 336, 337; 336, 338; 337, identifier:int; 338, argument_list; 338, 339; 339, subscript; 339, 340; 339, 341; 340, identifier:atom_bin_dimensions; 341, integer:2; 342, block; 342, 343; 343, if_statement; 343, 344; 343, 352; 344, not_operator; 344, 345; 345, subscript; 345, 346; 345, 351; 346, subscript; 346, 347; 346, 350; 347, subscript; 347, 348; 347, 349; 348, identifier:atom_bins; 349, identifier:x; 350, identifier:y; 351, identifier:z; 352, block; 352, 353; 353, expression_statement; 353, 354; 354, assignment; 354, 355; 354, 362; 355, subscript; 355, 356; 355, 361; 356, subscript; 356, 357; 356, 360; 357, subscript; 357, 358; 357, 359; 358, identifier:atom_bins; 359, identifier:x; 360, identifier:y; 361, identifier:z; 362, identifier:blank_section; 363, expression_statement; 363, 364; 364, assignment; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:self; 367, identifier:atom_bins; 368, identifier:atom_bins | def bin_atoms(self):
'''This function bins the Atoms into fixed-size sections of the protein space in 3D.'''
# Create the atom bins
low_point = numpy.array([self.min_x, self.min_y, self.min_z])
high_point = numpy.array([self.max_x, self.max_y, self.max_z])
atom_bin_dimensions = numpy.ceil((high_point - low_point) / self.bin_size)
self.atom_bin_dimensions = (int(atom_bin_dimensions[0]) - 1, int(atom_bin_dimensions[1]) - 1, int(atom_bin_dimensions[2]) - 1)
atom_bins = []
for x in range(int(atom_bin_dimensions[0])):
atom_bins.append([])
for y in range(int(atom_bin_dimensions[1])):
atom_bins[x].append([])
for z in range(int(atom_bin_dimensions[2])):
atom_bins[x][y].append(Bin(x, y, z))
# Assign each Atom to a bin
for serial_number, atom in self.atoms.iteritems():
bin_location = numpy.trunc((atom.point - low_point) / self.bin_size)
bin = atom_bins[int(bin_location[0])][int(bin_location[1])][int(bin_location[2])]
bin.append(atom)
atom.set_bin(bin)
# Sanity_check
if self.safe_mode:
num_atoms = 0
for x in range(int(atom_bin_dimensions[0])):
for y in range(int(atom_bin_dimensions[1])):
for z in range(int(atom_bin_dimensions[2])):
num_atoms += len(atom_bins[x][y][z])
assert(num_atoms == len(self.atoms))
# Snip empty sections (saves a little space after garbage collection - space savings increase with the number of empty arrays in the matrix)
blank_section = ()
for x in range(int(atom_bin_dimensions[0])):
for y in range(int(atom_bin_dimensions[1])):
for z in range(int(atom_bin_dimensions[2])):
if not atom_bins[x][y][z]:
atom_bins[x][y][z] = blank_section
self.atom_bins = atom_bins |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:attr_name; 6, list_splat_pattern; 6, 7; 7, identifier:args; 8, block; 8, 9; 8, 11; 8, 26; 8, 27; 8, 42; 8, 46; 8, 55; 8, 112; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 20; 12, not_operator; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:isinstance; 15, argument_list; 15, 16; 15, 17; 16, identifier:attr_name; 17, attribute; 17, 18; 17, 19; 18, identifier:six; 19, identifier:string_types; 20, block; 20, 21; 21, raise_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:TypeError; 24, argument_list; 24, 25; 25, string:'attr_name must be a str.'; 26, comment; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:in; 28, 29; 28, 30; 29, string:'-'; 30, identifier:attr_name; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:attr_name; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:attr_name; 38, identifier:replace; 39, argument_list; 39, 40; 39, 41; 40, string:'-'; 41, string:'_'; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:parent_attr; 45, identifier:self; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:attr; 49, call; 49, 50; 49, 51; 50, identifier:getattr; 51, argument_list; 51, 52; 51, 53; 51, 54; 52, identifier:parent_attr; 53, identifier:attr_name; 54, None; 55, for_statement; 55, 56; 55, 57; 55, 58; 55, 109; 56, identifier:arg; 57, identifier:args; 58, block; 58, 59; 58, 79; 59, if_statement; 59, 60; 59, 68; 60, not_operator; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:isinstance; 63, argument_list; 63, 64; 63, 65; 64, identifier:arg; 65, attribute; 65, 66; 65, 67; 66, identifier:six; 67, identifier:string_types; 68, block; 68, 69; 69, raise_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:TypeError; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, string:'each additional argument must be a string. {0} was not a string'; 76, identifier:format; 77, argument_list; 77, 78; 78, identifier:arg; 79, if_statement; 79, 80; 79, 85; 80, call; 80, 81; 80, 82; 81, identifier:hasattr; 82, argument_list; 82, 83; 82, 84; 83, identifier:parent_attr; 84, identifier:arg; 85, block; 85, 86; 85, 94; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:parent_attr; 89, call; 89, 90; 89, 91; 90, identifier:getattr; 91, argument_list; 91, 92; 91, 93; 92, identifier:parent_attr; 93, identifier:arg; 94, if_statement; 94, 95; 94, 100; 95, call; 95, 96; 95, 97; 96, identifier:hasattr; 97, argument_list; 97, 98; 97, 99; 98, identifier:parent_attr; 99, identifier:attr_name; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:attr; 104, call; 104, 105; 104, 106; 105, identifier:getattr; 106, argument_list; 106, 107; 106, 108; 107, identifier:parent_attr; 108, identifier:attr_name; 109, else_clause; 109, 110; 110, block; 110, 111; 111, pass_statement; 112, return_statement; 112, 113; 113, identifier:attr | def get(self, attr_name, *args):
""" Get the most retrieval attribute in the configuration file. This method
will recursively look through the configuration file for the attribute specified
and return the last found value or None. The values can be referenced by
the key name provided in the configuration file or that value normalized with
snake_casing.
Usage::
>>> from freight_forwarder.config import Config
>>>
>>> config = Config()
>>> thing = config.get('thing', 'grandparent', 'parent')
:param attr_name: A :string: The configuration property name to get.
:param *args: A :tuple: if :strings: parent objects in which to look for attr. This is optional.
:return attr value:
"""
if not isinstance(attr_name, six.string_types):
raise TypeError('attr_name must be a str.')
# allow retrieval of data with alias or normalized name
if '-' in attr_name:
attr_name = attr_name.replace('-', '_')
parent_attr = self
attr = getattr(parent_attr, attr_name, None)
for arg in args:
if not isinstance(arg, six.string_types):
raise TypeError(
'each additional argument must be a string. {0} was not a string'.format(arg)
)
if hasattr(parent_attr, arg):
parent_attr = getattr(parent_attr, arg)
if hasattr(parent_attr, attr_name):
attr = getattr(parent_attr, attr_name)
else:
pass
return attr |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_load_yml_config; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:config_file; 6, block; 6, 7; 6, 9; 6, 24; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 18; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 15; 14, identifier:config_file; 15, attribute; 15, 16; 15, 17; 16, identifier:six; 17, identifier:string_types; 18, block; 18, 19; 19, raise_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:TypeError; 22, argument_list; 22, 23; 23, string:'config_file must be a str.'; 24, try_statement; 24, 25; 24, 268; 25, block; 25, 26; 25, 57; 25, 89; 25, 130; 25, 162; 25, 194; 25, 195; 25, 203; 25, 211; 25, 219; 25, 227; 25, 235; 25, 246; 25, 256; 26, function_definition; 26, 27; 26, 28; 26, 31; 27, function_name:construct_yaml_int; 28, parameters; 28, 29; 28, 30; 29, identifier:self; 30, identifier:node; 31, block; 31, 32; 31, 42; 31, 55; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:obj; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:SafeConstructor; 38, identifier:construct_yaml_int; 39, argument_list; 39, 40; 39, 41; 40, identifier:self; 41, identifier:node; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:data; 45, call; 45, 46; 45, 47; 46, identifier:ConfigInt; 47, argument_list; 47, 48; 47, 49; 47, 52; 48, identifier:obj; 49, attribute; 49, 50; 49, 51; 50, identifier:node; 51, identifier:start_mark; 52, attribute; 52, 53; 52, 54; 53, identifier:node; 54, identifier:end_mark; 55, return_statement; 55, 56; 56, identifier:data; 57, function_definition; 57, 58; 57, 59; 57, 62; 58, function_name:construct_yaml_float; 59, parameters; 59, 60; 59, 61; 60, identifier:self; 61, identifier:node; 62, block; 62, 63; 62, 74; 62, 87; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 67; 65, pattern_list; 65, 66; 66, identifier:obj; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:SafeConstructor; 70, identifier:construct_yaml_float; 71, argument_list; 71, 72; 71, 73; 72, identifier:self; 73, identifier:node; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:data; 77, call; 77, 78; 77, 79; 78, identifier:ConfigFloat; 79, argument_list; 79, 80; 79, 81; 79, 84; 80, identifier:obj; 81, attribute; 81, 82; 81, 83; 82, identifier:node; 83, identifier:start_mark; 84, attribute; 84, 85; 84, 86; 85, identifier:node; 86, identifier:end_mark; 87, return_statement; 87, 88; 88, identifier:data; 89, function_definition; 89, 90; 89, 91; 89, 94; 89, 95; 89, 96; 90, function_name:construct_yaml_str; 91, parameters; 91, 92; 91, 93; 92, identifier:self; 93, identifier:node; 94, comment; 95, comment; 96, block; 96, 97; 96, 107; 96, 115; 96, 128; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:obj; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:SafeConstructor; 103, identifier:construct_scalar; 104, argument_list; 104, 105; 104, 106; 105, identifier:self; 106, identifier:node; 107, assert_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:isinstance; 110, argument_list; 110, 111; 110, 112; 111, identifier:obj; 112, attribute; 112, 113; 112, 114; 113, identifier:six; 114, identifier:string_types; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:data; 118, call; 118, 119; 118, 120; 119, identifier:ConfigUnicode; 120, argument_list; 120, 121; 120, 122; 120, 125; 121, identifier:obj; 122, attribute; 122, 123; 122, 124; 123, identifier:node; 124, identifier:start_mark; 125, attribute; 125, 126; 125, 127; 126, identifier:node; 127, identifier:end_mark; 128, return_statement; 128, 129; 129, identifier:data; 130, function_definition; 130, 131; 130, 132; 130, 135; 131, function_name:construct_yaml_mapping; 132, parameters; 132, 133; 132, 134; 133, identifier:self; 134, identifier:node; 135, block; 135, 136; 135, 147; 135, 160; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 140; 138, pattern_list; 138, 139; 139, identifier:obj; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:SafeConstructor; 143, identifier:construct_yaml_map; 144, argument_list; 144, 145; 144, 146; 145, identifier:self; 146, identifier:node; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:data; 150, call; 150, 151; 150, 152; 151, identifier:ConfigDict; 152, argument_list; 152, 153; 152, 154; 152, 157; 153, identifier:obj; 154, attribute; 154, 155; 154, 156; 155, identifier:node; 156, identifier:start_mark; 157, attribute; 157, 158; 157, 159; 158, identifier:node; 159, identifier:end_mark; 160, return_statement; 160, 161; 161, identifier:data; 162, function_definition; 162, 163; 162, 164; 162, 167; 163, function_name:construct_yaml_seq; 164, parameters; 164, 165; 164, 166; 165, identifier:self; 166, identifier:node; 167, block; 167, 168; 167, 179; 167, 192; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 172; 170, pattern_list; 170, 171; 171, identifier:obj; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:SafeConstructor; 175, identifier:construct_yaml_seq; 176, argument_list; 176, 177; 176, 178; 177, identifier:self; 178, identifier:node; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:data; 182, call; 182, 183; 182, 184; 183, identifier:ConfigSeq; 184, argument_list; 184, 185; 184, 186; 184, 189; 185, identifier:obj; 186, attribute; 186, 187; 186, 188; 187, identifier:node; 188, identifier:start_mark; 189, attribute; 189, 190; 189, 191; 190, identifier:node; 191, identifier:end_mark; 192, return_statement; 192, 193; 193, identifier:data; 194, comment; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:SafeConstructor; 199, identifier:add_constructor; 200, argument_list; 200, 201; 200, 202; 201, string:u'tag:yaml.org,2002:float'; 202, identifier:construct_yaml_float; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:SafeConstructor; 207, identifier:add_constructor; 208, argument_list; 208, 209; 208, 210; 209, string:u'tag:yaml.org,2002:int'; 210, identifier:construct_yaml_int; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:SafeConstructor; 215, identifier:add_constructor; 216, argument_list; 216, 217; 216, 218; 217, string:u'tag:yaml.org,2002:map'; 218, identifier:construct_yaml_mapping; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:SafeConstructor; 223, identifier:add_constructor; 224, argument_list; 224, 225; 224, 226; 225, string:u'tag:yaml.org,2002:seq'; 226, identifier:construct_yaml_seq; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:SafeConstructor; 231, identifier:add_constructor; 232, argument_list; 232, 233; 232, 234; 233, string:u'tag:yaml.org,2002:str'; 234, identifier:construct_yaml_str; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:data; 238, call; 238, 239; 238, 245; 239, attribute; 239, 240; 239, 244; 240, call; 240, 241; 240, 242; 241, identifier:SafeLoader; 242, argument_list; 242, 243; 243, identifier:config_file; 244, identifier:get_data; 245, argument_list; 246, if_statement; 246, 247; 246, 250; 247, comparison_operator:is; 247, 248; 247, 249; 248, identifier:data; 249, None; 250, block; 250, 251; 251, raise_statement; 251, 252; 252, call; 252, 253; 252, 254; 253, identifier:AttributeError; 254, argument_list; 254, 255; 255, string:'The configuration file needs to have data in it.'; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:_data; 261, call; 261, 262; 261, 263; 262, identifier:normalize_keys; 263, argument_list; 263, 264; 263, 265; 264, identifier:data; 265, keyword_argument; 265, 266; 265, 267; 266, identifier:snake_case; 267, False; 268, except_clause; 268, 269; 268, 273; 269, as_pattern; 269, 270; 269, 271; 270, identifier:YAMLError; 271, as_pattern_target; 271, 272; 272, identifier:e; 273, block; 273, 274; 274, if_statement; 274, 275; 274, 280; 274, 306; 275, call; 275, 276; 275, 277; 276, identifier:hasattr; 277, argument_list; 277, 278; 277, 279; 278, identifier:e; 279, string:'problem_mark'; 280, block; 280, 281; 280, 287; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:mark; 284, attribute; 284, 285; 284, 286; 285, identifier:e; 286, identifier:problem_mark; 287, raise_statement; 287, 288; 288, call; 288, 289; 288, 290; 289, identifier:SyntaxError; 290, argument_list; 290, 291; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, string:"There is a syntax error in your freight-forwarder config file line: {0} column: {1}"; 294, identifier:format; 295, argument_list; 295, 296; 295, 301; 296, binary_operator:+; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:mark; 299, identifier:line; 300, integer:1; 301, binary_operator:+; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:mark; 304, identifier:column; 305, integer:1; 306, else_clause; 306, 307; 307, block; 307, 308; 308, raise_statement; 308, 309; 309, call; 309, 310; 309, 311; 310, identifier:SyntaxError; 311, argument_list; 311, 312; 312, string:"There is a syntax error in your freight-forwarder config." | def _load_yml_config(self, config_file):
""" loads a yaml str, creates a few constructs for pyaml, serializes and normalized the config data. Then
assigns the config data to self._data.
:param config_file: A :string: loaded from a yaml file.
"""
if not isinstance(config_file, six.string_types):
raise TypeError('config_file must be a str.')
try:
def construct_yaml_int(self, node):
obj = SafeConstructor.construct_yaml_int(self, node)
data = ConfigInt(
obj,
node.start_mark,
node.end_mark
)
return data
def construct_yaml_float(self, node):
obj, = SafeConstructor.construct_yaml_float(self, node)
data = ConfigFloat(
obj,
node.start_mark,
node.end_mark
)
return data
def construct_yaml_str(self, node):
# Override the default string handling function
# to always return unicode objects
obj = SafeConstructor.construct_scalar(self, node)
assert isinstance(obj, six.string_types)
data = ConfigUnicode(
obj,
node.start_mark,
node.end_mark
)
return data
def construct_yaml_mapping(self, node):
obj, = SafeConstructor.construct_yaml_map(self, node)
data = ConfigDict(
obj,
node.start_mark,
node.end_mark
)
return data
def construct_yaml_seq(self, node):
obj, = SafeConstructor.construct_yaml_seq(self, node)
data = ConfigSeq(
obj,
node.start_mark,
node.end_mark
)
return data
# SafeConstructor.add_constructor(u'tag:yaml.org,2002:bool', construct_yaml_bool)
SafeConstructor.add_constructor(u'tag:yaml.org,2002:float', construct_yaml_float)
SafeConstructor.add_constructor(u'tag:yaml.org,2002:int', construct_yaml_int)
SafeConstructor.add_constructor(u'tag:yaml.org,2002:map', construct_yaml_mapping)
SafeConstructor.add_constructor(u'tag:yaml.org,2002:seq', construct_yaml_seq)
SafeConstructor.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
data = SafeLoader(config_file).get_data()
if data is None:
raise AttributeError('The configuration file needs to have data in it.')
self._data = normalize_keys(data, snake_case=False)
except YAMLError as e:
if hasattr(e, 'problem_mark'):
mark = e.problem_mark
raise SyntaxError(
"There is a syntax error in your freight-forwarder config file line: {0} column: {1}".format(
mark.line + 1,
mark.column + 1
)
)
else:
raise SyntaxError("There is a syntax error in your freight-forwarder config.") |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_create_attr; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:property_key; 6, identifier:data; 7, identifier:ancestors; 8, block; 8, 9; 8, 11; 8, 34; 8, 55; 8, 59; 8, 72; 8, 76; 8, 77; 8, 162; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 20; 12, not_operator; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:isinstance; 15, argument_list; 15, 16; 15, 17; 16, identifier:property_key; 17, attribute; 17, 18; 17, 19; 18, identifier:six; 19, identifier:string_types; 20, block; 20, 21; 21, raise_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:TypeError; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, string:"property_key must be a string. type: {0} was passed."; 28, identifier:format; 29, argument_list; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:type; 32, argument_list; 32, 33; 33, identifier:property_key; 34, if_statement; 34, 35; 34, 41; 35, not_operator; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:isinstance; 38, argument_list; 38, 39; 38, 40; 39, identifier:ancestors; 40, identifier:OrderedDict; 41, block; 41, 42; 42, raise_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:TypeError; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, string:"ancestors must be an OrderedDict. type: {0} was passed."; 49, identifier:format; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:type; 53, argument_list; 53, 54; 54, identifier:ancestors; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:previous_element; 58, identifier:self; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:normalized_key; 62, call; 62, 63; 62, 69; 63, attribute; 63, 64; 63, 68; 64, call; 64, 65; 64, 66; 65, identifier:normalize_value; 66, argument_list; 66, 67; 67, identifier:property_key; 68, identifier:replace; 69, argument_list; 69, 70; 69, 71; 70, string:'-'; 71, string:'_'; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:normalized_ancestor_key; 75, None; 76, comment; 77, if_statement; 77, 78; 77, 79; 78, identifier:ancestors; 79, block; 79, 80; 80, for_statement; 80, 81; 80, 84; 80, 90; 81, pattern_list; 81, 82; 81, 83; 82, identifier:ancestor_key; 83, identifier:ancestors_value; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:six; 87, identifier:iteritems; 88, argument_list; 88, 89; 89, identifier:ancestors; 90, block; 90, 91; 90, 104; 90, 114; 90, 154; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:normalized_ancestor_key; 94, call; 94, 95; 94, 101; 95, attribute; 95, 96; 95, 100; 96, call; 96, 97; 96, 98; 97, identifier:normalize_value; 98, argument_list; 98, 99; 99, identifier:ancestor_key; 100, identifier:replace; 101, argument_list; 101, 102; 101, 103; 102, string:'-'; 103, string:'_'; 104, if_statement; 104, 105; 104, 112; 105, comparison_operator:==; 105, 106; 105, 111; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:normalized_ancestor_key; 109, identifier:lower; 110, argument_list; 111, string:'root'; 112, block; 112, 113; 113, continue_statement; 114, if_statement; 114, 115; 114, 121; 115, not_operator; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:hasattr; 118, argument_list; 118, 119; 118, 120; 119, identifier:previous_element; 120, identifier:normalized_ancestor_key; 121, block; 121, 122; 121, 135; 121, 141; 121, 147; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:config_attr; 125, call; 125, 126; 125, 127; 126, identifier:ConfigDict; 127, argument_list; 127, 128; 127, 129; 127, 132; 128, dictionary; 129, attribute; 129, 130; 129, 131; 130, identifier:ancestors_value; 131, identifier:start_mark; 132, attribute; 132, 133; 132, 134; 133, identifier:ancestors_value; 134, identifier:end_mark; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:config_attr; 139, identifier:name; 140, identifier:normalized_ancestor_key; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:config_attr; 145, identifier:alias; 146, identifier:ancestor_key; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:setattr; 150, argument_list; 150, 151; 150, 152; 150, 153; 151, identifier:previous_element; 152, identifier:normalized_ancestor_key; 153, identifier:config_attr; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:previous_element; 157, call; 157, 158; 157, 159; 158, identifier:getattr; 159, argument_list; 159, 160; 159, 161; 160, identifier:previous_element; 161, identifier:normalized_ancestor_key; 162, if_statement; 162, 163; 162, 166; 162, 168; 163, comparison_operator:==; 163, 164; 163, 165; 164, identifier:normalized_key; 165, identifier:normalized_ancestor_key; 166, block; 166, 167; 167, pass_statement; 168, else_clause; 168, 169; 169, block; 169, 170; 169, 189; 170, if_statement; 170, 171; 170, 176; 171, call; 171, 172; 171, 173; 172, identifier:isinstance; 173, argument_list; 173, 174; 173, 175; 174, identifier:data; 175, identifier:ConfigNode; 176, block; 176, 177; 176, 183; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:data; 181, identifier:name; 182, identifier:normalized_key; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:data; 187, identifier:alias; 188, identifier:property_key; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:setattr; 192, argument_list; 192, 193; 192, 194; 192, 195; 193, identifier:previous_element; 194, identifier:normalized_key; 195, identifier:data | def _create_attr(self, property_key, data, ancestors):
""" Dynamically Creates attributes on for a Config. Also adds name and alias to each Config object.
:param property_key: A :string: configuration property name.
:param data: The adds the user supplied for this specific property.
:param ancestors: A :OrderedDict: that provides a history of its ancestors.
"""
if not isinstance(property_key, six.string_types):
raise TypeError("property_key must be a string. type: {0} was passed.".format(type(property_key)))
if not isinstance(ancestors, OrderedDict):
raise TypeError("ancestors must be an OrderedDict. type: {0} was passed.".format(type(ancestors)))
previous_element = self
normalized_key = normalize_value(property_key).replace('-', '_')
normalized_ancestor_key = None
# TODO: clean up and validation
if ancestors:
for ancestor_key, ancestors_value in six.iteritems(ancestors):
normalized_ancestor_key = normalize_value(ancestor_key).replace('-', '_')
if normalized_ancestor_key.lower() == 'root':
continue
if not hasattr(previous_element, normalized_ancestor_key):
config_attr = ConfigDict({}, ancestors_value.start_mark, ancestors_value.end_mark)
config_attr.name = normalized_ancestor_key
config_attr.alias = ancestor_key
setattr(
previous_element,
normalized_ancestor_key,
config_attr
)
previous_element = getattr(previous_element, normalized_ancestor_key)
if normalized_key == normalized_ancestor_key:
pass
else:
if isinstance(data, ConfigNode):
data.name = normalized_key
data.alias = property_key
setattr(previous_element, normalized_key, data) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_collect_unrecognized_values; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:scheme; 6, identifier:data; 7, identifier:ancestors; 8, block; 8, 9; 8, 11; 8, 32; 8, 53; 8, 57; 8, 187; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 18; 12, not_operator; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:isinstance; 15, argument_list; 15, 16; 15, 17; 16, identifier:ancestors; 17, identifier:OrderedDict; 18, block; 18, 19; 19, raise_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:TypeError; 22, argument_list; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, string:"ancestors must be an OrderedDict. type: {0} was passed."; 26, identifier:format; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:type; 30, argument_list; 30, 31; 31, identifier:ancestors; 32, if_statement; 32, 33; 32, 39; 33, not_operator; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:isinstance; 36, argument_list; 36, 37; 36, 38; 37, identifier:scheme; 38, identifier:dict; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:TypeError; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, string:'scheme must be a dict. type: {0} was passed'; 47, identifier:format; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:type; 51, argument_list; 51, 52; 52, identifier:scheme; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:unrecognized_values; 56, dictionary; 57, if_statement; 57, 58; 57, 63; 57, 183; 58, call; 58, 59; 58, 60; 59, identifier:isinstance; 60, argument_list; 60, 61; 60, 62; 61, identifier:data; 62, identifier:dict; 63, block; 63, 64; 63, 86; 63, 109; 63, 118; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:pruned_scheme; 67, list_comprehension; 67, 68; 67, 69; 67, 76; 68, identifier:key; 69, for_in_clause; 69, 70; 69, 71; 70, identifier:key; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:scheme; 74, identifier:keys; 75, argument_list; 76, if_clause; 76, 77; 77, boolean_operator:and; 77, 78; 77, 81; 78, comparison_operator:not; 78, 79; 78, 80; 79, identifier:key; 80, identifier:RESERVED_SCHEME_KEYS; 81, comparison_operator:not; 81, 82; 81, 85; 82, subscript; 82, 83; 82, 84; 83, identifier:key; 84, integer:0; 85, identifier:RESERVED_SCHEME_KEYS; 86, for_statement; 86, 87; 86, 90; 86, 96; 87, pattern_list; 87, 88; 87, 89; 88, identifier:key; 89, identifier:value; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:six; 93, identifier:iteritems; 94, argument_list; 94, 95; 95, identifier:data; 96, block; 96, 97; 96, 103; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:in; 98, 99; 98, 100; 99, identifier:key; 100, identifier:pruned_scheme; 101, block; 101, 102; 102, continue_statement; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:unrecognized_values; 107, identifier:key; 108, identifier:value; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:validations; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:scheme; 115, identifier:get; 116, argument_list; 116, 117; 117, string:'is'; 118, if_statement; 118, 119; 118, 124; 119, boolean_operator:and; 119, 120; 119, 121; 120, identifier:validations; 121, comparison_operator:in; 121, 122; 121, 123; 122, string:'one_of'; 123, identifier:validations; 124, block; 124, 125; 125, for_statement; 125, 126; 125, 127; 125, 130; 126, identifier:nested_scheme; 127, subscript; 127, 128; 127, 129; 128, identifier:validations; 129, string:'one_of'; 130, block; 130, 131; 131, if_statement; 131, 132; 131, 137; 132, call; 132, 133; 132, 134; 133, identifier:isinstance; 134, argument_list; 134, 135; 134, 136; 135, identifier:nested_scheme; 136, identifier:dict; 137, block; 137, 138; 137, 148; 137, 170; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:updated_scheme; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:_update_scheme; 145, argument_list; 145, 146; 145, 147; 146, identifier:nested_scheme; 147, identifier:ancestors; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:pruned_scheme; 151, list_comprehension; 151, 152; 151, 153; 151, 160; 152, identifier:key; 153, for_in_clause; 153, 154; 153, 155; 154, identifier:key; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:updated_scheme; 158, identifier:keys; 159, argument_list; 160, if_clause; 160, 161; 161, boolean_operator:and; 161, 162; 161, 165; 162, comparison_operator:not; 162, 163; 162, 164; 163, identifier:key; 164, identifier:RESERVED_SCHEME_KEYS; 165, comparison_operator:not; 165, 166; 165, 169; 166, subscript; 166, 167; 166, 168; 167, identifier:key; 168, integer:0; 169, identifier:RESERVED_SCHEME_KEYS; 170, for_statement; 170, 171; 170, 172; 170, 173; 171, identifier:key; 172, identifier:pruned_scheme; 173, block; 173, 174; 174, if_statement; 174, 175; 174, 178; 175, comparison_operator:in; 175, 176; 175, 177; 176, identifier:key; 177, identifier:unrecognized_values; 178, block; 178, 179; 179, delete_statement; 179, 180; 180, subscript; 180, 181; 180, 182; 181, identifier:unrecognized_values; 182, identifier:key; 183, else_clause; 183, 184; 183, 185; 184, comment; 185, block; 185, 186; 186, pass_statement; 187, return_statement; 187, 188; 188, identifier:unrecognized_values | def _collect_unrecognized_values(self, scheme, data, ancestors):
""" Looks for values that aren't defined in the scheme and returns a dict with any unrecognized values found.
:param scheme: A :dict:, The scheme defining the validations.
:param data: A :dict: user supplied for this specific property.
:param ancestors: A :OrderedDict: that provides a history of its ancestors.
:rtype: A :dict: of unrecognized configuration properties.
"""
if not isinstance(ancestors, OrderedDict):
raise TypeError("ancestors must be an OrderedDict. type: {0} was passed.".format(type(ancestors)))
if not isinstance(scheme, dict):
raise TypeError('scheme must be a dict. type: {0} was passed'.format(type(scheme)))
unrecognized_values = {}
if isinstance(data, dict):
pruned_scheme = [key for key in scheme.keys() if key not in RESERVED_SCHEME_KEYS and key[0] not in RESERVED_SCHEME_KEYS]
for key, value in six.iteritems(data):
if key in pruned_scheme:
continue
unrecognized_values[key] = value
validations = scheme.get('is')
if validations and 'one_of' in validations:
for nested_scheme in validations['one_of']:
if isinstance(nested_scheme, dict):
updated_scheme = self._update_scheme(nested_scheme, ancestors)
pruned_scheme = [key for key in updated_scheme.keys() if key not in RESERVED_SCHEME_KEYS and key[0] not in RESERVED_SCHEME_KEYS]
for key in pruned_scheme:
if key in unrecognized_values:
del unrecognized_values[key]
else:
# TODO: maybe return an error?
pass
return unrecognized_values |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_update_scheme; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:scheme; 6, identifier:ancestors; 7, block; 7, 8; 7, 10; 7, 31; 7, 52; 7, 53; 7, 62; 7, 77; 7, 81; 7, 224; 7, 231; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 17; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 16; 15, identifier:ancestors; 16, identifier:OrderedDict; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:TypeError; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, string:"ancestors must be an OrderedDict. type: {0} was passed."; 25, identifier:format; 26, argument_list; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:type; 29, argument_list; 29, 30; 30, identifier:ancestors; 31, if_statement; 31, 32; 31, 38; 32, not_operator; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:isinstance; 35, argument_list; 35, 36; 35, 37; 36, identifier:scheme; 37, identifier:dict; 38, block; 38, 39; 39, raise_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:TypeError; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, string:'scheme must be a dict. type: {0} was passed'; 46, identifier:format; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:type; 50, argument_list; 50, 51; 51, identifier:scheme; 52, comment; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:definitions; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:ROOT_SCHEME; 59, identifier:get; 60, argument_list; 60, 61; 61, string:'_'; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:in; 63, 64; 63, 65; 64, string:'inherit'; 65, identifier:scheme; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:scheme; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:_scheme_propagation; 74, argument_list; 74, 75; 74, 76; 75, identifier:scheme; 76, identifier:definitions; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:updated_scheme; 80, dictionary; 81, for_statement; 81, 82; 81, 83; 81, 89; 82, identifier:scheme_key; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:six; 86, identifier:iterkeys; 87, argument_list; 87, 88; 88, identifier:scheme; 89, block; 89, 90; 89, 110; 90, if_statement; 90, 91; 90, 99; 91, not_operator; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:isinstance; 94, argument_list; 94, 95; 94, 96; 95, identifier:scheme_key; 96, attribute; 96, 97; 96, 98; 97, identifier:six; 98, identifier:string_types; 99, block; 99, 100; 100, raise_statement; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:TypeError; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, string:'scheme keys are required to be strings. type: {0} was passed.'; 107, identifier:format; 108, argument_list; 108, 109; 109, identifier:scheme_key; 110, if_statement; 110, 111; 110, 114; 110, 172; 111, comparison_operator:in; 111, 112; 111, 113; 112, string:'@'; 113, identifier:scheme_key; 114, block; 114, 115; 114, 123; 114, 134; 114, 147; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:ref; 118, subscript; 118, 119; 118, 120; 119, identifier:scheme_key; 120, slice; 120, 121; 120, 122; 121, integer:1; 122, colon; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:scheme_reference; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:_scheme_references; 131, identifier:get; 132, argument_list; 132, 133; 133, identifier:ref; 134, if_statement; 134, 135; 134, 137; 135, not_operator; 135, 136; 136, identifier:scheme_reference; 137, block; 137, 138; 138, raise_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:ConfigValidationException; 141, argument_list; 141, 142; 141, 143; 141, 144; 141, 145; 141, 146; 142, identifier:ancestors; 143, identifier:ref; 144, identifier:scheme_reference; 145, string:'required'; 146, identifier:scheme; 147, for_statement; 147, 148; 147, 149; 147, 152; 148, identifier:reference_key; 149, subscript; 149, 150; 149, 151; 150, identifier:scheme_reference; 151, string:'keys'; 152, block; 152, 153; 152, 164; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:scheme_reference; 158, string:'scheme'; 159, identifier:update; 160, argument_list; 160, 161; 161, subscript; 161, 162; 161, 163; 162, identifier:scheme; 163, identifier:scheme_key; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 169; 166, subscript; 166, 167; 166, 168; 167, identifier:updated_scheme; 168, identifier:reference_key; 169, subscript; 169, 170; 169, 171; 170, identifier:scheme_reference; 171, string:'scheme'; 172, elif_clause; 172, 173; 172, 176; 173, comparison_operator:in; 173, 174; 173, 175; 174, string:'~'; 175, identifier:scheme_key; 176, block; 176, 177; 176, 185; 176, 196; 176, 210; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:ref; 180, subscript; 180, 181; 180, 182; 181, identifier:scheme_key; 182, slice; 182, 183; 182, 184; 183, integer:1; 184, colon; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:scheme_reference; 188, call; 188, 189; 188, 194; 189, attribute; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:_scheme_references; 193, identifier:get; 194, argument_list; 194, 195; 195, identifier:ref; 196, if_statement; 196, 197; 196, 199; 197, not_operator; 197, 198; 198, identifier:scheme_reference; 199, block; 199, 200; 200, raise_statement; 200, 201; 201, call; 201, 202; 201, 203; 202, identifier:LookupError; 203, argument_list; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, string:"was unable to find {0} in scheme reference."; 207, identifier:format; 208, argument_list; 208, 209; 209, identifier:ref; 210, for_statement; 210, 211; 210, 212; 210, 215; 211, identifier:reference_key; 212, subscript; 212, 213; 212, 214; 213, identifier:scheme_reference; 214, string:'keys'; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 221; 218, subscript; 218, 219; 218, 220; 219, identifier:updated_scheme; 220, identifier:reference_key; 221, subscript; 221, 222; 221, 223; 222, identifier:scheme; 223, identifier:scheme_key; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:scheme; 228, identifier:update; 229, argument_list; 229, 230; 230, identifier:updated_scheme; 231, return_statement; 231, 232; 232, identifier:scheme | def _update_scheme(self, scheme, ancestors):
""" Updates the current scheme based off special pre-defined keys and retruns a new updated scheme.
:param scheme: A :dict:, The scheme defining the validations.
:param ancestors: A :OrderedDict: that provides a history of its ancestors.
:rtype: A new :dict: with updated scheme values.
"""
if not isinstance(ancestors, OrderedDict):
raise TypeError("ancestors must be an OrderedDict. type: {0} was passed.".format(type(ancestors)))
if not isinstance(scheme, dict):
raise TypeError('scheme must be a dict. type: {0} was passed'.format(type(scheme)))
# TODO: what if we have more than one scheme :P need to fix this.
definitions = ROOT_SCHEME.get('_')
if 'inherit' in scheme:
scheme = self._scheme_propagation(scheme, definitions)
updated_scheme = {}
for scheme_key in six.iterkeys(scheme):
if not isinstance(scheme_key, six.string_types):
raise TypeError('scheme keys are required to be strings. type: {0} was passed.'.format(scheme_key))
if '@' in scheme_key:
ref = scheme_key[1:]
scheme_reference = self._scheme_references.get(ref)
if not scheme_reference:
raise ConfigValidationException(ancestors, ref, scheme_reference, 'required', scheme)
for reference_key in scheme_reference['keys']:
scheme_reference['scheme'].update(scheme[scheme_key])
updated_scheme[reference_key] = scheme_reference['scheme']
elif '~' in scheme_key:
ref = scheme_key[1:]
scheme_reference = self._scheme_references.get(ref)
if not scheme_reference:
raise LookupError("was unable to find {0} in scheme reference.".format(ref))
for reference_key in scheme_reference['keys']:
updated_scheme[reference_key] = scheme[scheme_key]
scheme.update(updated_scheme)
return scheme |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:_walk_tree; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:self; 5, identifier:data; 6, identifier:scheme; 7, default_parameter; 7, 8; 7, 9; 8, identifier:ancestors; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:property_name; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:prefix; 15, None; 16, block; 16, 17; 16, 19; 16, 77; 16, 98; 16, 109; 16, 146; 16, 167; 16, 177; 16, 211; 16, 248; 16, 258; 16, 266; 17, expression_statement; 17, 18; 18, comment; 19, if_statement; 19, 20; 19, 23; 20, comparison_operator:is; 20, 21; 20, 22; 21, identifier:property_name; 22, None; 23, block; 23, 24; 23, 28; 23, 29; 23, 30; 23, 50; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:property_name; 27, string:'root'; 28, comment; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:order; 33, binary_operator:+; 33, 34; 33, 36; 34, list:['registries']; 34, 35; 35, string:'registries'; 36, list_comprehension; 36, 37; 36, 38; 36, 45; 37, identifier:key; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:key; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:scheme; 43, identifier:keys; 44, argument_list; 45, if_clause; 45, 46; 46, comparison_operator:not; 46, 47; 46, 48; 47, identifier:key; 48, tuple; 48, 49; 49, string:'registries'; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:scheme; 53, call; 53, 54; 53, 55; 54, identifier:OrderedDict; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:sorted; 58, argument_list; 58, 59; 58, 64; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:scheme; 62, identifier:items; 63, argument_list; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:key; 66, lambda; 66, 67; 66, 69; 67, lambda_parameters; 67, 68; 68, identifier:x; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:order; 72, identifier:index; 73, argument_list; 73, 74; 74, subscript; 74, 75; 74, 76; 75, identifier:x; 76, integer:0; 77, if_statement; 77, 78; 77, 81; 77, 83; 78, comparison_operator:is; 78, 79; 78, 80; 79, identifier:data; 80, None; 81, block; 81, 82; 82, return_statement; 83, elif_clause; 83, 84; 83, 92; 84, not_operator; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:isinstance; 87, argument_list; 87, 88; 87, 89; 88, identifier:property_name; 89, attribute; 89, 90; 89, 91; 90, identifier:six; 91, identifier:string_types; 92, block; 92, 93; 93, raise_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:TypeError; 96, argument_list; 96, 97; 97, string:'property_name must be a string.'; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:ancestors; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:_update_ancestors; 105, argument_list; 105, 106; 105, 107; 105, 108; 106, identifier:data; 107, identifier:property_name; 108, identifier:ancestors; 109, if_statement; 109, 110; 109, 115; 109, 131; 110, call; 110, 111; 110, 112; 111, identifier:isinstance; 112, argument_list; 112, 113; 112, 114; 113, identifier:ancestors; 114, identifier:OrderedDict; 115, block; 115, 116; 116, if_statement; 116, 117; 116, 125; 117, comparison_operator:!=; 117, 118; 117, 124; 118, subscript; 118, 119; 118, 123; 119, call; 119, 120; 119, 121; 120, identifier:list; 121, argument_list; 121, 122; 122, identifier:ancestors; 123, integer:0; 124, string:'root'; 125, block; 125, 126; 126, raise_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:LookupError; 129, argument_list; 129, 130; 130, string:'root must be the first item in ancestors.'; 131, else_clause; 131, 132; 132, block; 132, 133; 133, raise_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:TypeError; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, string:'ancestors must be an OrderedDict. {0} was passed'; 140, identifier:format; 141, argument_list; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:type; 144, argument_list; 144, 145; 145, identifier:ancestors; 146, if_statement; 146, 147; 146, 153; 147, not_operator; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:isinstance; 150, argument_list; 150, 151; 150, 152; 151, identifier:scheme; 152, identifier:dict; 153, block; 153, 154; 154, raise_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:TypeError; 157, argument_list; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, string:'scheme must be a dict. {0} was passed.'; 161, identifier:format; 162, argument_list; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:type; 165, argument_list; 165, 166; 166, identifier:scheme; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:scheme; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:_update_scheme; 174, argument_list; 174, 175; 174, 176; 175, identifier:scheme; 176, identifier:ancestors; 177, if_statement; 177, 178; 177, 183; 178, boolean_operator:and; 178, 179; 178, 182; 179, comparison_operator:is; 179, 180; 179, 181; 180, identifier:property_name; 181, None; 182, identifier:data; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:data; 187, conditional_expression:if; 187, 188; 187, 203; 187, 210; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:_get_cascading_attr; 192, argument_list; 192, 193; 192, 194; 193, identifier:property_name; 194, list_splat; 194, 195; 195, subscript; 195, 196; 195, 200; 196, call; 196, 197; 196, 198; 197, identifier:list; 198, argument_list; 198, 199; 199, identifier:ancestors; 200, slice; 200, 201; 200, 202; 201, integer:1; 202, colon; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:scheme; 206, identifier:get; 207, argument_list; 207, 208; 207, 209; 208, string:'cascading'; 209, False; 210, identifier:data; 211, for_statement; 211, 212; 211, 213; 211, 231; 212, identifier:err; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:__execute_validations; 217, argument_list; 217, 218; 217, 225; 217, 226; 217, 227; 217, 228; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:scheme; 221, identifier:get; 222, argument_list; 222, 223; 222, 224; 223, string:'is'; 224, dictionary; 225, identifier:data; 226, identifier:property_name; 227, identifier:ancestors; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:prefix; 230, identifier:prefix; 231, block; 231, 232; 232, if_statement; 232, 233; 232, 234; 232, 237; 233, identifier:err; 234, block; 234, 235; 235, raise_statement; 235, 236; 236, identifier:err; 237, else_clause; 237, 238; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:_create_attr; 244, argument_list; 244, 245; 244, 246; 244, 247; 245, identifier:property_name; 246, identifier:data; 247, identifier:ancestors; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:self; 252, identifier:__validate_unrecognized_values; 253, argument_list; 253, 254; 253, 255; 253, 256; 253, 257; 254, identifier:scheme; 255, identifier:data; 256, identifier:ancestors; 257, identifier:prefix; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:self; 262, identifier:__populate_scheme_references; 263, argument_list; 263, 264; 263, 265; 264, identifier:scheme; 265, identifier:property_name; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:__validate_config_properties; 271, argument_list; 271, 272; 271, 273; 271, 274; 271, 275; 272, identifier:scheme; 273, identifier:data; 274, identifier:ancestors; 275, identifier:prefix | def _walk_tree(self, data, scheme, ancestors=None, property_name=None, prefix=None):
""" This function takes configuration data and a validation scheme
then walk the configuration tree validating the configuraton data agenst
the scheme provided. Will raise error on failure otherwise return None.
Usage::
>>> self._walk_tree(
>>> OrderedDict([('root', config_data)]),
>>> registries,
>>> REGISTRIES_SCHEME
>>> )
:param ancestors: A :OrderedDict:, The first element of the dict must be 'root'.
:param data: The data that needs to be validated agents the scheme.
:param scheme: A :dict:, The scheme defining the validations.
:param property_name: A :string:, This is the name of the data getting validated.
:param prefix:
:rtype: :None: will raise error if a validation fails.
"""
if property_name is None:
property_name = 'root'
# hack until i add this to references
# reorder validates putting required first. If the data doesn't exist there is no need to continue.
order = ['registries'] + [key for key in scheme.keys() if key not in ('registries',)]
scheme = OrderedDict(sorted(scheme.items(), key=lambda x: order.index(x[0])))
if data is None:
return
elif not isinstance(property_name, six.string_types):
raise TypeError('property_name must be a string.')
ancestors = self._update_ancestors(data, property_name, ancestors)
if isinstance(ancestors, OrderedDict):
if list(ancestors)[0] != 'root':
raise LookupError('root must be the first item in ancestors.')
else:
raise TypeError('ancestors must be an OrderedDict. {0} was passed'.format(type(ancestors)))
if not isinstance(scheme, dict):
raise TypeError('scheme must be a dict. {0} was passed.'.format(type(scheme)))
scheme = self._update_scheme(scheme, ancestors)
if property_name is not None and data:
data = self._get_cascading_attr(
property_name, *list(ancestors)[1:]
) if scheme.get('cascading', False) else data
for err in self.__execute_validations(scheme.get('is', {}), data, property_name, ancestors, prefix=prefix):
if err:
raise err
else:
self._create_attr(property_name, data, ancestors)
self.__validate_unrecognized_values(scheme, data, ancestors, prefix)
self.__populate_scheme_references(scheme, property_name)
self.__validate_config_properties(scheme, data, ancestors, prefix) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:__execute_validations; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 4, identifier:self; 5, identifier:validations; 6, identifier:data; 7, identifier:property_name; 8, identifier:ancestors; 9, default_parameter; 9, 10; 9, 11; 10, identifier:negation; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:prefix; 14, None; 15, block; 15, 16; 15, 18; 15, 39; 15, 60; 15, 83; 15, 84; 15, 106; 15, 133; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 25; 19, not_operator; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:isinstance; 22, argument_list; 22, 23; 22, 24; 23, identifier:ancestors; 24, identifier:OrderedDict; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:TypeError; 29, argument_list; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, string:"ancestors must be an OrderedDict. type: {0} was passed."; 33, identifier:format; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:type; 37, argument_list; 37, 38; 38, identifier:ancestors; 39, if_statement; 39, 40; 39, 46; 40, not_operator; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 45; 44, identifier:validations; 45, identifier:dict; 46, block; 46, 47; 47, raise_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:TypeError; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, string:'validations is required to be a dict. type: {1} was passed.'; 54, identifier:format; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:type; 58, argument_list; 58, 59; 59, identifier:validations; 60, if_statement; 60, 61; 60, 69; 61, not_operator; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:isinstance; 64, argument_list; 64, 65; 64, 66; 65, identifier:property_name; 66, attribute; 66, 67; 66, 68; 67, identifier:six; 68, identifier:string_types; 69, block; 69, 70; 70, raise_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:TypeError; 73, argument_list; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, string:"property_key must be a string. type: {0} was passed."; 77, identifier:format; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:type; 81, argument_list; 81, 82; 82, identifier:property_name; 83, comment; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:order; 87, binary_operator:+; 87, 88; 87, 91; 88, list:['type', 'required']; 88, 89; 88, 90; 89, string:'type'; 90, string:'required'; 91, list_comprehension; 91, 92; 91, 93; 91, 100; 92, identifier:key; 93, for_in_clause; 93, 94; 93, 95; 94, identifier:key; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:validations; 98, identifier:keys; 99, argument_list; 100, if_clause; 100, 101; 101, comparison_operator:not; 101, 102; 101, 103; 102, identifier:key; 103, tuple; 103, 104; 103, 105; 104, string:'required'; 105, string:'type'; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:ordered_validations; 109, call; 109, 110; 109, 111; 110, identifier:OrderedDict; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:sorted; 114, argument_list; 114, 115; 114, 120; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:validations; 118, identifier:items; 119, argument_list; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:key; 122, lambda; 122, 123; 122, 125; 123, lambda_parameters; 123, 124; 124, identifier:x; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:order; 128, identifier:index; 129, argument_list; 129, 130; 130, subscript; 130, 131; 130, 132; 131, identifier:x; 132, integer:0; 133, for_statement; 133, 134; 133, 137; 133, 143; 134, pattern_list; 134, 135; 134, 136; 135, identifier:validation; 136, identifier:value; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:six; 140, identifier:iteritems; 141, argument_list; 141, 142; 142, identifier:ordered_validations; 143, block; 143, 144; 144, if_statement; 144, 145; 144, 148; 144, 197; 145, comparison_operator:in; 145, 146; 145, 147; 146, identifier:validation; 147, identifier:VALIDATORS; 148, block; 148, 149; 148, 173; 149, if_statement; 149, 150; 149, 153; 149, 154; 150, comparison_operator:==; 150, 151; 150, 152; 151, identifier:validation; 152, string:'not'; 153, comment; 154, block; 154, 155; 154, 172; 155, for_statement; 155, 156; 155, 157; 155, 168; 156, identifier:err; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:__execute_validations; 161, argument_list; 161, 162; 161, 163; 161, 164; 161, 165; 161, 166; 161, 167; 162, identifier:value; 163, identifier:data; 164, identifier:property_name; 165, identifier:ancestors; 166, identifier:negation; 167, identifier:prefix; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, yield; 170, 171; 171, identifier:err; 172, continue_statement; 173, for_statement; 173, 174; 173, 175; 173, 193; 174, identifier:err; 175, call; 175, 176; 175, 186; 176, call; 176, 177; 176, 178; 177, identifier:getattr; 178, argument_list; 178, 179; 178, 180; 179, identifier:self; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, string:'_{0}'; 183, identifier:format; 184, argument_list; 184, 185; 185, identifier:validation; 186, argument_list; 186, 187; 186, 188; 186, 189; 186, 190; 186, 191; 186, 192; 187, identifier:value; 188, identifier:data; 189, identifier:property_name; 190, identifier:ancestors; 191, identifier:negation; 192, identifier:prefix; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, yield; 195, 196; 196, identifier:err; 197, else_clause; 197, 198; 198, block; 198, 199; 199, raise_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:LookupError; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, string:"{0} isn't a validator or reserved scheme key."; 206, identifier:format; 207, argument_list; 207, 208; 208, identifier:validation | def __execute_validations(self, validations, data, property_name, ancestors, negation=False, prefix=None):
""" Validate the data for a specific configuration value. This method will look up all of the validations provided
and dynamically call any validation methods. If a validation fails a error will be thrown. If no errors are found
a attributes will be dynamically created on the Config object for the configuration value.
:param validations: A :dict: with any required validations and expected values.
:param data: the data to validate.
:param property_name: A :string:, the properties name.
:param ancestors: A :OrderedDict:, representing the ancestors of a property.
"""
if not isinstance(ancestors, OrderedDict):
raise TypeError("ancestors must be an OrderedDict. type: {0} was passed.".format(type(ancestors)))
if not isinstance(validations, dict):
raise TypeError('validations is required to be a dict. type: {1} was passed.'.format(type(validations)))
if not isinstance(property_name, six.string_types):
raise TypeError("property_key must be a string. type: {0} was passed.".format(type(property_name)))
# reorder validates putting required first. If the data doesn't exist there is no need to continue.
order = ['type', 'required'] + [key for key in validations.keys() if key not in ('required', 'type')]
ordered_validations = OrderedDict(sorted(validations.items(), key=lambda x: order.index(x[0])))
for validation, value in six.iteritems(ordered_validations):
if validation in VALIDATORS:
if validation == 'not':
# TODO: need to test to make sure this works
for err in self.__execute_validations(value, data, property_name, ancestors, negation, prefix):
yield err
continue
for err in getattr(self, '_{0}'.format(validation))(value, data, property_name, ancestors, negation, prefix):
yield err
else:
raise LookupError("{0} isn't a validator or reserved scheme key.".format(validation)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:users_feature; 3, parameters; 3, 4; 4, identifier:app; 5, block; 5, 6; 5, 8; 5, 9; 5, 28; 5, 29; 5, 37; 5, 38; 5, 45; 5, 46; 5, 53; 5, 59; 5, 65; 5, 82; 5, 83; 5, 90; 5, 97; 5, 105; 5, 143; 5, 144; 5, 151; 5, 192; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 20; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:app; 15, identifier:config; 16, identifier:get; 17, argument_list; 17, 18; 17, 19; 18, string:'USER_JWT_SECRET'; 19, None; 20, block; 20, 21; 21, raise_statement; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:x; 25, identifier:JwtSecretMissing; 26, argument_list; 26, 27; 27, string:'Please set USER_JWT_SECRET in config'; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:app; 33, identifier:session_interface; 34, call; 34, 35; 34, 36; 35, identifier:BoilerSessionInterface; 36, argument_list; 37, comment; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:user_service; 42, identifier:init; 43, argument_list; 43, 44; 44, identifier:app; 45, comment; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:login_manager; 50, identifier:init_app; 51, argument_list; 51, 52; 52, identifier:app; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:login_manager; 57, identifier:login_view; 58, string:'user.login'; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:login_manager; 63, identifier:login_message; 64, None; 65, decorated_definition; 65, 66; 65, 70; 66, decorator; 66, 67; 67, attribute; 67, 68; 67, 69; 68, identifier:login_manager; 69, identifier:user_loader; 70, function_definition; 70, 71; 70, 72; 70, 74; 71, function_name:load_user; 72, parameters; 72, 73; 73, identifier:id; 74, block; 74, 75; 75, return_statement; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:user_service; 79, identifier:get; 80, argument_list; 80, 81; 81, identifier:id; 82, comment; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:oauth; 87, identifier:init_app; 88, argument_list; 88, 89; 89, identifier:app; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:registry; 93, call; 93, 94; 93, 95; 94, identifier:OauthProviders; 95, argument_list; 95, 96; 96, identifier:app; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:providers; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:registry; 103, identifier:get_providers; 104, argument_list; 105, with_statement; 105, 106; 105, 113; 106, with_clause; 106, 107; 107, with_item; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:app; 111, identifier:app_context; 112, argument_list; 113, block; 113, 114; 114, for_statement; 114, 115; 114, 116; 114, 117; 115, identifier:provider; 116, identifier:providers; 117, block; 117, 118; 118, if_statement; 118, 119; 118, 124; 119, comparison_operator:not; 119, 120; 119, 121; 120, identifier:provider; 121, attribute; 121, 122; 121, 123; 122, identifier:oauth; 123, identifier:remote_apps; 124, block; 124, 125; 124, 136; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:oauth; 129, identifier:remote_app; 130, argument_list; 130, 131; 130, 132; 131, identifier:provider; 132, dictionary_splat; 132, 133; 133, subscript; 133, 134; 133, 135; 134, identifier:providers; 135, identifier:provider; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:registry; 140, identifier:register_token_getter; 141, argument_list; 141, 142; 142, identifier:provider; 143, comment; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:principal; 148, identifier:init_app; 149, argument_list; 149, 150; 150, identifier:app; 151, decorated_definition; 151, 152; 151, 156; 152, decorator; 152, 153; 153, attribute; 153, 154; 153, 155; 154, identifier:principal; 155, identifier:identity_loader; 156, function_definition; 156, 157; 156, 158; 156, 159; 157, function_name:load_identity; 158, parameters; 159, block; 159, 160; 159, 172; 159, 180; 159, 188; 160, if_statement; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:current_user; 163, identifier:is_authenticated; 164, block; 164, 165; 165, return_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:Identity; 168, argument_list; 168, 169; 169, attribute; 169, 170; 169, 171; 170, identifier:current_user; 171, identifier:id; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:session; 176, identifier:pop; 177, argument_list; 177, 178; 177, 179; 178, string:'identity.name'; 179, None; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:session; 184, identifier:pop; 185, argument_list; 185, 186; 185, 187; 186, string:'identity.auth_type'; 187, None; 188, return_statement; 188, 189; 189, call; 189, 190; 189, 191; 190, identifier:AnonymousIdentity; 191, argument_list; 192, decorated_definition; 192, 193; 192, 200; 193, decorator; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:identity_loaded; 197, identifier:connect_via; 198, argument_list; 198, 199; 199, identifier:app; 200, function_definition; 200, 201; 200, 202; 200, 205; 201, function_name:on_identity_loaded; 202, parameters; 202, 203; 202, 204; 203, identifier:sender; 204, identifier:identity; 205, block; 205, 206; 205, 212; 205, 219; 205, 233; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:identity; 210, identifier:user; 211, identifier:current_user; 212, if_statement; 212, 213; 212, 217; 213, not_operator; 213, 214; 214, attribute; 214, 215; 214, 216; 215, identifier:current_user; 216, identifier:is_authenticated; 217, block; 217, 218; 218, return_statement; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:identity; 224, identifier:provides; 225, identifier:add; 226, argument_list; 226, 227; 227, call; 227, 228; 227, 229; 228, identifier:UserNeed; 229, argument_list; 229, 230; 230, attribute; 230, 231; 230, 232; 231, identifier:current_user; 232, identifier:id; 233, for_statement; 233, 234; 233, 235; 233, 238; 234, identifier:role; 235, attribute; 235, 236; 235, 237; 236, identifier:current_user; 237, identifier:roles; 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:identity; 244, identifier:provides; 245, identifier:add; 246, argument_list; 246, 247; 247, call; 247, 248; 247, 249; 248, identifier:RoleNeed; 249, argument_list; 249, 250; 250, attribute; 250, 251; 250, 252; 251, identifier:role; 252, identifier:handle | def users_feature(app):
"""
Add users feature
Allows to register users and assign groups, instantiates flask login, flask principal
and oauth integration
"""
# check we have jwt secret configures
if not app.config.get('USER_JWT_SECRET', None):
raise x.JwtSecretMissing('Please set USER_JWT_SECRET in config')
# use custom session interface
app.session_interface = BoilerSessionInterface()
# init user service
user_service.init(app)
# init login manager
login_manager.init_app(app)
login_manager.login_view = 'user.login'
login_manager.login_message = None
@login_manager.user_loader
def load_user(id):
return user_service.get(id)
# init OAuth
oauth.init_app(app)
registry = OauthProviders(app)
providers = registry.get_providers()
with app.app_context():
for provider in providers:
if provider not in oauth.remote_apps:
oauth.remote_app(provider, **providers[provider])
registry.register_token_getter(provider)
# init principal
principal.init_app(app)
@principal.identity_loader
def load_identity():
if current_user.is_authenticated:
return Identity(current_user.id)
session.pop('identity.name', None)
session.pop('identity.auth_type', None)
return AnonymousIdentity()
@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
identity.user = current_user
if not current_user.is_authenticated:
return
identity.provides.add(UserNeed(current_user.id))
for role in current_user.roles:
identity.provides.add(RoleNeed(role.handle)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:match; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:other; 6, block; 6, 7; 6, 9; 6, 16; 7, expression_statement; 7, 8; 8, string:''' This is a noisy terminal-printing function at present since there is no need to make it a proper API function.'''; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:colortext; 13, identifier:message; 14, argument_list; 14, 15; 15, string:"FASTA Match"; 16, for_statement; 16, 17; 16, 20; 16, 28; 17, pattern_list; 17, 18; 17, 19; 18, identifier:frompdbID; 19, identifier:fromchains; 20, call; 20, 21; 20, 22; 21, identifier:sorted; 22, argument_list; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:iteritems; 27, argument_list; 28, block; 28, 29; 28, 33; 28, 37; 28, 118; 28, 122; 28, 134; 28, 177; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:matched_pdbs; 32, dictionary; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:matched_chains; 36, dictionary; 37, for_statement; 37, 38; 37, 41; 37, 46; 38, pattern_list; 38, 39; 38, 40; 39, identifier:fromchain; 40, identifier:fromsequence; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:fromchains; 44, identifier:iteritems; 45, argument_list; 46, block; 46, 47; 47, for_statement; 47, 48; 47, 51; 47, 56; 48, pattern_list; 48, 49; 48, 50; 49, identifier:topdbID; 50, identifier:tochains; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:other; 54, identifier:iteritems; 55, argument_list; 56, block; 56, 57; 57, for_statement; 57, 58; 57, 61; 57, 66; 58, pattern_list; 58, 59; 58, 60; 59, identifier:tochain; 60, identifier:tosequence; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:tochains; 64, identifier:iteritems; 65, argument_list; 66, block; 66, 67; 67, if_statement; 67, 68; 67, 71; 68, comparison_operator:==; 68, 69; 68, 70; 69, identifier:fromsequence; 70, identifier:tosequence; 71, block; 71, 72; 71, 86; 71, 95; 71, 107; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:matched_pdbs; 76, identifier:topdbID; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:matched_pdbs; 80, identifier:get; 81, argument_list; 81, 82; 81, 83; 82, identifier:topdbID; 83, call; 83, 84; 83, 85; 84, identifier:set; 85, argument_list; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:matched_pdbs; 91, identifier:topdbID; 92, identifier:add; 93, argument_list; 93, 94; 94, identifier:fromchain; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:matched_chains; 99, identifier:fromchain; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:matched_chains; 103, identifier:get; 104, argument_list; 104, 105; 104, 106; 105, identifier:fromchain; 106, list:[]; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 114; 109, attribute; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:matched_chains; 112, identifier:fromchain; 113, identifier:append; 114, argument_list; 114, 115; 115, tuple; 115, 116; 115, 117; 116, identifier:topdbID; 117, identifier:tochain; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:foundmatches; 121, list:[]; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:colortext; 126, identifier:printf; 127, argument_list; 127, 128; 127, 131; 128, binary_operator:%; 128, 129; 128, 130; 129, string:" %s"; 130, identifier:frompdbID; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:color; 133, string:"silver"; 134, for_statement; 134, 135; 134, 138; 134, 143; 135, pattern_list; 135, 136; 135, 137; 136, identifier:mpdbID; 137, identifier:mchains; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:matched_pdbs; 141, identifier:iteritems; 142, argument_list; 143, block; 143, 144; 144, if_statement; 144, 145; 144, 155; 145, comparison_operator:==; 145, 146; 145, 147; 146, identifier:mchains; 147, call; 147, 148; 147, 149; 148, identifier:set; 149, argument_list; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:fromchains; 153, identifier:keys; 154, argument_list; 155, block; 155, 156; 155, 163; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:foundmatches; 160, identifier:append; 161, argument_list; 161, 162; 162, identifier:mpdbID; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:colortext; 167, identifier:printf; 168, argument_list; 168, 169; 168, 174; 169, binary_operator:%; 169, 170; 169, 171; 170, string:" PDB %s matched PDB %s on all chains"; 171, tuple; 171, 172; 171, 173; 172, identifier:mpdbID; 173, identifier:frompdbID; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:color; 176, string:"white"; 177, if_statement; 177, 178; 177, 179; 177, 266; 178, identifier:foundmatches; 179, block; 179, 180; 180, for_statement; 180, 181; 180, 184; 180, 189; 181, pattern_list; 181, 182; 181, 183; 182, identifier:fromchain; 183, identifier:fromsequence; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:fromchains; 187, identifier:iteritems; 188, argument_list; 189, block; 189, 190; 189, 203; 189, 220; 189, 224; 189, 252; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:colortext; 194, identifier:printf; 195, argument_list; 195, 196; 195, 200; 196, binary_operator:%; 196, 197; 196, 198; 197, string:" %s"; 198, parenthesized_expression; 198, 199; 199, identifier:fromchain; 200, keyword_argument; 200, 201; 200, 202; 201, identifier:color; 202, string:"silver"; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:colortext; 207, identifier:printf; 208, argument_list; 208, 209; 208, 213; 209, binary_operator:%; 209, 210; 209, 211; 210, string:" %s"; 211, parenthesized_expression; 211, 212; 212, identifier:fromsequence; 213, keyword_argument; 213, 214; 213, 215; 214, identifier:color; 215, subscript; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:unique_sequences; 219, identifier:fromsequence; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:mstr; 223, list:[]; 224, for_statement; 224, 225; 224, 226; 224, 229; 225, identifier:mchain; 226, subscript; 226, 227; 226, 228; 227, identifier:matched_chains; 228, identifier:fromchain; 229, block; 229, 230; 230, if_statement; 230, 231; 230, 236; 231, comparison_operator:in; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:mchain; 234, integer:0; 235, identifier:foundmatches; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:mstr; 241, identifier:append; 242, argument_list; 242, 243; 243, binary_operator:%; 243, 244; 243, 245; 244, string:"%s chain %s"; 245, tuple; 245, 246; 245, 249; 246, subscript; 246, 247; 246, 248; 247, identifier:mchain; 248, integer:0; 249, subscript; 249, 250; 249, 251; 250, identifier:mchain; 251, integer:1; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:colortext; 256, identifier:printf; 257, argument_list; 257, 258; 258, binary_operator:%; 258, 259; 258, 260; 259, string:" Matches: %s"; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, string:", "; 263, identifier:join; 264, argument_list; 264, 265; 265, identifier:mstr; 266, else_clause; 266, 267; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:colortext; 272, identifier:error; 273, argument_list; 273, 274; 274, string:" No matches found." | def match(self, other):
''' This is a noisy terminal-printing function at present since there is no need to make it a proper API function.'''
colortext.message("FASTA Match")
for frompdbID, fromchains in sorted(self.iteritems()):
matched_pdbs = {}
matched_chains = {}
for fromchain, fromsequence in fromchains.iteritems():
for topdbID, tochains in other.iteritems():
for tochain, tosequence in tochains.iteritems():
if fromsequence == tosequence:
matched_pdbs[topdbID] = matched_pdbs.get(topdbID, set())
matched_pdbs[topdbID].add(fromchain)
matched_chains[fromchain] = matched_chains.get(fromchain, [])
matched_chains[fromchain].append((topdbID, tochain))
foundmatches = []
colortext.printf(" %s" % frompdbID, color="silver")
for mpdbID, mchains in matched_pdbs.iteritems():
if mchains == set(fromchains.keys()):
foundmatches.append(mpdbID)
colortext.printf(" PDB %s matched PDB %s on all chains" % (mpdbID, frompdbID), color="white")
if foundmatches:
for fromchain, fromsequence in fromchains.iteritems():
colortext.printf(" %s" % (fromchain), color = "silver")
colortext.printf(" %s" % (fromsequence), color = self.unique_sequences[fromsequence])
mstr = []
for mchain in matched_chains[fromchain]:
if mchain[0] in foundmatches:
mstr.append("%s chain %s" % (mchain[0], mchain[1]))
colortext.printf(" Matches: %s" % ", ".join(mstr))
else:
colortext.error(" No matches found.") |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:generateSummaryHTMLTable; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:extraLapse; 7, identifier:TYPICAL_LAPSE; 8, block; 8, 9; 8, 11; 8, 17; 8, 21; 8, 28; 8, 29; 8, 36; 8, 43; 8, 44; 8, 50; 8, 56; 8, 62; 8, 66; 8, 401; 8, 408; 9, expression_statement; 9, 10; 10, string:'''Generates a summary in HTML of the status of the expected scripts broken based on the log.
This summary is returned as a list of strings.
'''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:scriptsRun; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:scriptsRun; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:html; 20, list:[]; 21, expression_statement; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:html; 25, identifier:append; 26, argument_list; 26, 27; 27, string:"<table style='text-align:center;border:1px solid black;margin-left: auto;margin-right: auto;'>\n"; 28, comment; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:html; 33, identifier:append; 34, argument_list; 34, 35; 35, string:' <tr><td colspan="4" style="text-align:center"></td></tr>\n'; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:html; 40, identifier:append; 41, argument_list; 41, 42; 42, string:' <tr style="font-weight:bold;background-color:#cccccc;text-align:center"><td>Script</td><td>Last status</td><td>Last run</td><td>Last success</td></tr>\n'; 43, comment; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:tablestyle; 47, list:['background-color:#33dd33;', 'background-color:#33ff33;']; 47, 48; 47, 49; 48, string:'background-color:#33dd33;'; 49, string:'background-color:#33ff33;'; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:warningstyle; 53, list:['background-color:#EA8737;', 'background-color:#f5b767;']; 53, 54; 53, 55; 54, string:'background-color:#EA8737;'; 55, string:'background-color:#f5b767;'; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:failstyle; 59, list:['background-color:#dd3333;', 'background-color:#ff3333;']; 59, 60; 59, 61; 60, string:'background-color:#dd3333;'; 61, string:'background-color:#ff3333;'; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:count; 65, integer:0; 66, for_statement; 66, 67; 66, 70; 66, 78; 67, pattern_list; 67, 68; 67, 69; 68, identifier:name; 69, identifier:details; 70, call; 70, 71; 70, 72; 71, identifier:sorted; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:scriptsRun; 76, identifier:iteritems; 77, argument_list; 78, block; 78, 79; 78, 83; 78, 91; 78, 134; 78, 142; 78, 197; 78, 198; 78, 207; 78, 208; 78, 245; 78, 246; 78, 315; 78, 316; 78, 354; 78, 355; 78, 390; 78, 397; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:status; 82, None; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:rowstyle; 86, subscript; 86, 87; 86, 88; 87, identifier:tablestyle; 88, binary_operator:%; 88, 89; 88, 90; 89, identifier:count; 90, integer:2; 91, if_statement; 91, 92; 91, 102; 91, 120; 92, boolean_operator:and; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:details; 95, string:"lastSuccess"; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:expectedScripts; 99, identifier:get; 100, argument_list; 100, 101; 101, identifier:name; 102, block; 102, 103; 103, if_statement; 103, 104; 103, 115; 104, not_operator; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:expectedScripts; 108, identifier:check; 109, argument_list; 109, 110; 109, 111; 109, 114; 110, identifier:name; 111, subscript; 111, 112; 111, 113; 112, identifier:details; 113, string:"lastSuccess"; 114, identifier:extraLapse; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:status; 119, string:"STOPPED"; 120, else_clause; 120, 121; 121, block; 121, 122; 121, 130; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:rowstyle; 125, subscript; 125, 126; 125, 127; 126, identifier:failstyle; 127, binary_operator:%; 127, 128; 127, 129; 128, identifier:count; 129, integer:2; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:status; 133, string:"FAIL"; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:laststatusstyle; 137, subscript; 137, 138; 137, 139; 138, identifier:tablestyle; 139, binary_operator:%; 139, 140; 139, 141; 140, identifier:count; 141, integer:2; 142, if_statement; 142, 143; 142, 148; 142, 161; 142, 184; 143, binary_operator:&; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:details; 146, string:"status"; 147, identifier:RETROSPECT_FAIL; 148, block; 148, 149; 148, 157; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:laststatusstyle; 152, subscript; 152, 153; 152, 154; 153, identifier:failstyle; 154, binary_operator:%; 154, 155; 154, 156; 155, identifier:count; 156, integer:2; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:status; 160, string:"FAIL"; 161, elif_clause; 161, 162; 161, 171; 162, boolean_operator:and; 162, 163; 162, 166; 163, comparison_operator:!=; 163, 164; 163, 165; 164, identifier:status; 165, string:"STOPPED"; 166, binary_operator:&; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:details; 169, string:"status"; 170, identifier:RETROSPECT_WARNING; 171, block; 171, 172; 171, 180; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:laststatusstyle; 175, subscript; 175, 176; 175, 177; 176, identifier:warningstyle; 177, binary_operator:%; 177, 178; 177, 179; 178, identifier:count; 179, integer:2; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:status; 183, string:"WARNINGS"; 184, elif_clause; 184, 185; 184, 192; 185, boolean_operator:and; 185, 186; 185, 189; 186, comparison_operator:!=; 186, 187; 186, 188; 187, identifier:status; 188, string:"FAIL"; 189, comparison_operator:!=; 189, 190; 189, 191; 190, identifier:status; 191, string:"STOPPED"; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:status; 196, string:"OK"; 197, comment; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:html; 202, identifier:append; 203, argument_list; 203, 204; 204, binary_operator:%; 204, 205; 204, 206; 205, string:'<tr style="text-align:left;%s">\n'; 206, identifier:rowstyle; 207, comment; 208, if_statement; 208, 209; 208, 212; 208, 228; 209, comparison_operator:==; 209, 210; 209, 211; 210, identifier:status; 211, string:"STOPPED"; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:html; 217, identifier:append; 218, argument_list; 218, 219; 219, binary_operator:%; 219, 220; 219, 221; 220, string:'\t<td style="%s">%s</td>\n'; 221, tuple; 221, 222; 221, 227; 222, subscript; 222, 223; 222, 224; 223, identifier:failstyle; 224, binary_operator:%; 224, 225; 224, 226; 225, identifier:count; 226, integer:2; 227, identifier:name; 228, else_clause; 228, 229; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:html; 234, identifier:append; 235, argument_list; 235, 236; 236, binary_operator:%; 236, 237; 236, 238; 237, string:'\t<td style="%s">%s</td>'; 238, tuple; 238, 239; 238, 244; 239, subscript; 239, 240; 239, 241; 240, identifier:tablestyle; 241, binary_operator:%; 241, 242; 241, 243; 242, identifier:count; 243, integer:2; 244, identifier:name; 245, comment; 246, if_statement; 246, 247; 246, 250; 246, 302; 247, subscript; 247, 248; 247, 249; 248, identifier:details; 249, string:"lastRun"; 250, block; 250, 251; 251, if_statement; 251, 252; 251, 255; 251, 280; 252, comparison_operator:==; 252, 253; 252, 254; 253, identifier:status; 254, string:"STOPPED"; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:html; 260, identifier:append; 261, argument_list; 261, 262; 262, binary_operator:%; 262, 263; 262, 264; 263, string:'\t<td style="%s"><a href="#%s">%s</a></td>\n'; 264, tuple; 264, 265; 264, 270; 264, 279; 265, subscript; 265, 266; 265, 267; 266, identifier:failstyle; 267, binary_operator:%; 267, 268; 267, 269; 268, identifier:count; 269, integer:2; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:self; 273, identifier:createAnchorID; 274, argument_list; 274, 275; 274, 276; 275, identifier:name; 276, subscript; 276, 277; 276, 278; 277, identifier:details; 278, string:"lastRun"; 279, identifier:status; 280, else_clause; 280, 281; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:html; 286, identifier:append; 287, argument_list; 287, 288; 288, binary_operator:%; 288, 289; 288, 290; 289, string:'\t<td style="%s"><a href="#%s">%s</a></td>\n'; 290, tuple; 290, 291; 290, 292; 290, 301; 291, identifier:laststatusstyle; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:self; 295, identifier:createAnchorID; 296, argument_list; 296, 297; 296, 298; 297, identifier:name; 298, subscript; 298, 299; 298, 300; 299, identifier:details; 300, string:"lastRun"; 301, identifier:status; 302, else_clause; 302, 303; 303, block; 303, 304; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:html; 308, identifier:append; 309, argument_list; 309, 310; 310, binary_operator:%; 310, 311; 310, 312; 311, string:'\t<td style="%s">%s</td>\n'; 312, tuple; 312, 313; 312, 314; 313, identifier:laststatusstyle; 314, identifier:status; 315, comment; 316, if_statement; 316, 317; 316, 320; 316, 343; 317, subscript; 317, 318; 317, 319; 318, identifier:details; 319, string:"lastRun"; 320, block; 320, 321; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:html; 325, identifier:append; 326, argument_list; 326, 327; 327, binary_operator:%; 327, 328; 327, 329; 328, string:'\t<td style="%s"><a href="#%s">%s</a></td>\n'; 329, tuple; 329, 330; 329, 331; 329, 340; 330, identifier:laststatusstyle; 331, call; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:self; 334, identifier:createAnchorID; 335, argument_list; 335, 336; 335, 337; 336, identifier:name; 337, subscript; 337, 338; 337, 339; 338, identifier:details; 339, string:"lastRun"; 340, subscript; 340, 341; 340, 342; 341, identifier:details; 342, string:"lastRun"; 343, else_clause; 343, 344; 344, block; 344, 345; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:html; 349, identifier:append; 350, argument_list; 350, 351; 351, binary_operator:%; 351, 352; 351, 353; 352, string:'\t<td style="%s">none found</td>\n'; 353, identifier:laststatusstyle; 354, comment; 355, if_statement; 355, 356; 355, 359; 355, 381; 356, subscript; 356, 357; 356, 358; 357, identifier:details; 358, string:"lastSuccess"; 359, block; 359, 360; 360, expression_statement; 360, 361; 361, call; 361, 362; 361, 365; 362, attribute; 362, 363; 362, 364; 363, identifier:html; 364, identifier:append; 365, argument_list; 365, 366; 366, binary_operator:%; 366, 367; 366, 368; 367, string:'\t<td><a href="#%s">%s</a></td>\n'; 368, tuple; 368, 369; 368, 378; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:self; 372, identifier:createAnchorID; 373, argument_list; 373, 374; 373, 375; 374, identifier:name; 375, subscript; 375, 376; 375, 377; 376, identifier:details; 377, string:"lastSuccess"; 378, subscript; 378, 379; 378, 380; 379, identifier:details; 380, string:"lastSuccess"; 381, else_clause; 381, 382; 382, block; 382, 383; 383, expression_statement; 383, 384; 384, call; 384, 385; 384, 388; 385, attribute; 385, 386; 385, 387; 386, identifier:html; 387, identifier:append; 388, argument_list; 388, 389; 389, string:'\t<td>none found</td>\n'; 390, expression_statement; 390, 391; 391, call; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:html; 394, identifier:append; 395, argument_list; 395, 396; 396, string:'</tr>\n'; 397, expression_statement; 397, 398; 398, augmented_assignment:+=; 398, 399; 398, 400; 399, identifier:count; 400, integer:1; 401, expression_statement; 401, 402; 402, call; 402, 403; 402, 406; 403, attribute; 403, 404; 403, 405; 404, identifier:html; 405, identifier:append; 406, argument_list; 406, 407; 407, string:"</table>"; 408, return_statement; 408, 409; 409, identifier:html | def generateSummaryHTMLTable(self, extraLapse = TYPICAL_LAPSE):
'''Generates a summary in HTML of the status of the expected scripts broken based on the log.
This summary is returned as a list of strings.
'''
scriptsRun = self.scriptsRun
html = []
html.append("<table style='text-align:center;border:1px solid black;margin-left: auto;margin-right: auto;'>\n") # Start summary table
html.append(' <tr><td colspan="4" style="text-align:center"></td></tr>\n')
html.append(' <tr style="font-weight:bold;background-color:#cccccc;text-align:center"><td>Script</td><td>Last status</td><td>Last run</td><td>Last success</td></tr>\n')
# Alternate shades between rows
tablestyle = ['background-color:#33dd33;', 'background-color:#33ff33;']
warningstyle = ['background-color:#EA8737;', 'background-color:#f5b767;']
failstyle = ['background-color:#dd3333;', 'background-color:#ff3333;']
count = 0
for name, details in sorted(scriptsRun.iteritems()):
status = None
rowstyle = tablestyle[count % 2]
if details["lastSuccess"] and expectedScripts.get(name):
if not expectedScripts.check(name, details["lastSuccess"], extraLapse):
status = "STOPPED"
else:
rowstyle = failstyle[count % 2]
status = "FAIL"
laststatusstyle = tablestyle[count % 2]
if details["status"] & RETROSPECT_FAIL:
laststatusstyle = failstyle[count % 2]
status = "FAIL"
elif status != "STOPPED" and details["status"] & RETROSPECT_WARNING:
laststatusstyle = warningstyle[count % 2]
status = "WARNINGS"
elif status != "FAIL" and status != "STOPPED":
status = "OK"
# Start a row
html.append('<tr style="text-align:left;%s">\n' % rowstyle)
# Script name field
if status == "STOPPED":
html.append('\t<td style="%s">%s</td>\n' % (failstyle[count % 2], name))
else:
html.append('\t<td style="%s">%s</td>' % (tablestyle[count % 2], name))
# Last status field
if details["lastRun"]:
if status == "STOPPED":
html.append('\t<td style="%s"><a href="#%s">%s</a></td>\n' % (failstyle[count % 2], self.createAnchorID(name, details["lastRun"]), status))
else:
html.append('\t<td style="%s"><a href="#%s">%s</a></td>\n' % (laststatusstyle, self.createAnchorID(name, details["lastRun"]), status))
else:
html.append('\t<td style="%s">%s</td>\n' % (laststatusstyle, status))
# Last run field
if details["lastRun"]:
html.append('\t<td style="%s"><a href="#%s">%s</a></td>\n' % (laststatusstyle, self.createAnchorID(name, details["lastRun"]), details["lastRun"]))
else:
html.append('\t<td style="%s">none found</td>\n' % laststatusstyle)
# Last success field
if details["lastSuccess"]:
html.append('\t<td><a href="#%s">%s</a></td>\n' % (self.createAnchorID(name, details["lastSuccess"]), details["lastSuccess"]))
else:
html.append('\t<td>none found</td>\n')
html.append('</tr>\n')
count += 1
html.append("</table>")
return html |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:apply_quality_control_checks; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:seq; 5, default_parameter; 5, 6; 5, 7; 6, identifier:check_gen9_seqs; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:check_short_length; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:check_local_gc_content; 13, True; 14, default_parameter; 14, 15; 14, 16; 15, identifier:check_global_gc_content; 16, True; 17, block; 17, 18; 17, 20; 17, 28; 17, 32; 17, 33; 17, 58; 17, 59; 17, 81; 17, 82; 17, 152; 17, 153; 17, 197; 17, 198; 17, 271; 17, 272; 17, 312; 17, 313; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:seq; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:seq; 26, identifier:upper; 27, argument_list; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:failure_reasons; 31, list:[]; 32, comment; 33, if_statement; 33, 34; 33, 35; 34, identifier:check_short_length; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 43; 37, comparison_operator:<; 37, 38; 37, 42; 38, call; 38, 39; 38, 40; 39, identifier:len; 40, argument_list; 40, 41; 41, identifier:seq; 42, identifier:min_gene_length; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:failure_reasons; 48, identifier:append; 49, argument_list; 49, 50; 50, binary_operator:%; 50, 51; 50, 52; 51, string:'minimum_length: Sequence is %d bp long and needs to be at least %d bp'; 52, tuple; 52, 53; 52, 57; 53, call; 53, 54; 53, 55; 54, identifier:len; 55, argument_list; 55, 56; 56, identifier:seq; 57, identifier:min_gene_length; 58, comment; 59, if_statement; 59, 60; 59, 66; 60, comparison_operator:>; 60, 61; 60, 65; 61, call; 61, 62; 61, 63; 62, identifier:len; 63, argument_list; 63, 64; 64, identifier:seq; 65, identifier:max_gene_length; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:failure_reasons; 71, identifier:append; 72, argument_list; 72, 73; 73, binary_operator:%; 73, 74; 73, 75; 74, string:'maximum_length: Sequence is %d bp long and needs to be shorter than %d bp'; 75, tuple; 75, 76; 75, 80; 76, call; 76, 77; 76, 78; 77, identifier:len; 78, argument_list; 78, 79; 79, identifier:seq; 80, identifier:max_gene_length; 81, comment; 82, if_statement; 82, 83; 82, 84; 83, identifier:check_gen9_seqs; 84, block; 84, 85; 85, for_statement; 85, 86; 85, 87; 85, 88; 86, identifier:site; 87, identifier:reserved_restriction_sites; 88, block; 88, 89; 88, 98; 88, 107; 88, 116; 88, 134; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:pattern; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:dna; 95, identifier:dna_to_re; 96, argument_list; 96, 97; 97, identifier:site; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:reverse_site; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:dna; 104, identifier:reverse_complement; 105, argument_list; 105, 106; 106, identifier:site; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:reverse_pattern; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:dna; 113, identifier:dna_to_re; 114, argument_list; 114, 115; 115, identifier:reverse_site; 116, if_statement; 116, 117; 116, 123; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:pattern; 120, identifier:match; 121, argument_list; 121, 122; 122, identifier:seq; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:failure_reasons; 128, identifier:append; 129, argument_list; 129, 130; 130, binary_operator:%; 130, 131; 130, 132; 131, string:'gen9_restricted_sequences: Reserved sequence %s is present'; 132, parenthesized_expression; 132, 133; 133, identifier:site; 134, if_statement; 134, 135; 134, 141; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:reverse_pattern; 138, identifier:match; 139, argument_list; 139, 140; 140, identifier:seq; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:failure_reasons; 146, identifier:append; 147, argument_list; 147, 148; 148, binary_operator:%; 148, 149; 148, 150; 149, string:'gen9_restricted_sequences: Reverse-complement of reserved sequence %s is present'; 150, parenthesized_expression; 150, 151; 151, identifier:site; 152, comment; 153, if_statement; 153, 154; 153, 155; 154, identifier:check_global_gc_content; 155, block; 155, 156; 155, 165; 155, 181; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:gc_content; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:dna; 162, identifier:gc_content; 163, argument_list; 163, 164; 164, identifier:seq; 165, if_statement; 165, 166; 165, 169; 166, comparison_operator:<; 166, 167; 166, 168; 167, identifier:gc_content; 168, identifier:global_gc_content_min; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:failure_reasons; 174, identifier:append; 175, argument_list; 175, 176; 176, binary_operator:%; 176, 177; 176, 178; 177, string:'global_gc_content_min: Global GC content is %.3f%% and must be at least %.3f%%'; 178, tuple; 178, 179; 178, 180; 179, identifier:gc_content; 180, identifier:global_gc_content_min; 181, if_statement; 181, 182; 181, 185; 182, comparison_operator:>; 182, 183; 182, 184; 183, identifier:gc_content; 184, identifier:global_gc_content_max; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:failure_reasons; 190, identifier:append; 191, argument_list; 191, 192; 192, binary_operator:%; 192, 193; 192, 194; 193, string:'global_gc_content_max: Global GC content is %.3f%% and must be less than %.3f%%'; 194, tuple; 194, 195; 194, 196; 195, identifier:gc_content; 196, identifier:global_gc_content_max; 197, comment; 198, if_statement; 198, 199; 198, 200; 199, identifier:check_local_gc_content; 200, block; 200, 201; 200, 206; 200, 224; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:windows; 204, list:[seq]; 204, 205; 205, identifier:seq; 206, if_statement; 206, 207; 206, 213; 207, comparison_operator:<; 207, 208; 207, 209; 208, identifier:local_gc_window_size; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:seq; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:windows; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:dna; 220, identifier:sliding_window; 221, argument_list; 221, 222; 221, 223; 222, identifier:seq; 223, identifier:local_gc_window_size; 224, for_statement; 224, 225; 224, 226; 224, 227; 225, identifier:seq_window; 226, identifier:windows; 227, block; 227, 228; 227, 237; 227, 254; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:lgc_content; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:dna; 234, identifier:gc_content; 235, argument_list; 235, 236; 236, identifier:seq_window; 237, if_statement; 237, 238; 237, 241; 238, comparison_operator:<; 238, 239; 238, 240; 239, identifier:lgc_content; 240, identifier:local_gc_content_min; 241, block; 241, 242; 241, 253; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:failure_reasons; 246, identifier:append; 247, argument_list; 247, 248; 248, binary_operator:%; 248, 249; 248, 250; 249, string:'local_gc_content_min: Local GC content is %.3f%% and must be at least %.3f%%'; 250, tuple; 250, 251; 250, 252; 251, identifier:lgc_content; 252, identifier:local_gc_content_min; 253, break_statement; 254, if_statement; 254, 255; 254, 258; 255, comparison_operator:>; 255, 256; 255, 257; 256, identifier:lgc_content; 257, identifier:local_gc_content_max; 258, block; 258, 259; 258, 270; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:failure_reasons; 263, identifier:append; 264, argument_list; 264, 265; 265, binary_operator:%; 265, 266; 265, 267; 266, string:'local_gc_content_max: Local GC content is %.3f%% and must be less than %.3f%%'; 267, tuple; 267, 268; 267, 269; 268, identifier:lgc_content; 269, identifier:local_gc_content_max; 270, break_statement; 271, comment; 272, for_statement; 272, 273; 272, 274; 272, 277; 273, identifier:base; 274, attribute; 274, 275; 274, 276; 275, identifier:dna; 276, identifier:dna_bases; 277, block; 277, 278; 277, 286; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:homopolymer; 281, binary_operator:*; 281, 282; 281, 283; 282, identifier:base; 283, subscript; 283, 284; 283, 285; 284, identifier:homopolymer_max_lengths; 285, identifier:base; 286, if_statement; 286, 287; 286, 290; 287, comparison_operator:in; 287, 288; 287, 289; 288, identifier:homopolymer; 289, identifier:seq; 290, block; 290, 291; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:failure_reasons; 295, identifier:append; 296, argument_list; 296, 297; 297, binary_operator:%; 297, 298; 297, 299; 298, string:'max_%s_homopolymer: %s'; 299, tuple; 299, 300; 299, 305; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:base; 303, identifier:lower; 304, argument_list; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:dna; 308, identifier:case_highlight; 309, argument_list; 309, 310; 309, 311; 310, identifier:seq; 311, identifier:a_homopolymer; 312, comment; 313, if_statement; 313, 314; 313, 315; 314, identifier:failure_reasons; 315, block; 315, 316; 315, 320; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:intro; 319, string:"The given sequence fails following Gen9 design guidelines:"; 320, raise_statement; 320, 321; 321, call; 321, 322; 321, 323; 322, identifier:ValueError; 323, argument_list; 323, 324; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, string:'\n'; 327, identifier:join; 328, argument_list; 328, 329; 329, binary_operator:+; 329, 330; 329, 332; 330, list:[intro]; 330, 331; 331, identifier:intro; 332, identifier:failure_reasons | def apply_quality_control_checks(
seq,
check_gen9_seqs=True,
check_short_length=True,
check_local_gc_content=True,
check_global_gc_content=True):
"""
Raise a ValueError if the given sequence doesn't pass all of the Gen9
quality control design guidelines. Certain checks can be enabled or
disabled via the command line.
"""
seq = seq.upper()
failure_reasons = []
# Minimum length
if check_short_length:
if len(seq) < min_gene_length:
failure_reasons.append('minimum_length: Sequence is %d bp long and needs to be at least %d bp'%(len(seq),min_gene_length))
# Maximum length
if len(seq) > max_gene_length:
failure_reasons.append('maximum_length: Sequence is %d bp long and needs to be shorter than %d bp'%(len(seq),max_gene_length))
# Gen9 restricted sequences
if check_gen9_seqs:
for site in reserved_restriction_sites:
pattern = dna.dna_to_re(site)
reverse_site = dna.reverse_complement(site)
reverse_pattern = dna.dna_to_re(reverse_site)
if pattern.match(seq):
failure_reasons.append('gen9_restricted_sequences: Reserved sequence %s is present'%(site))
if reverse_pattern.match(seq):
failure_reasons.append('gen9_restricted_sequences: Reverse-complement of reserved sequence %s is present'%(site))
# Global GC content
if check_global_gc_content:
gc_content = dna.gc_content(seq)
if gc_content < global_gc_content_min:
failure_reasons.append('global_gc_content_min: Global GC content is %.3f%% and must be at least %.3f%%'%(gc_content,global_gc_content_min))
if gc_content > global_gc_content_max:
failure_reasons.append('global_gc_content_max: Global GC content is %.3f%% and must be less than %.3f%%'%(gc_content,global_gc_content_max))
# Local GC content (windows)
if check_local_gc_content:
windows = [seq]
if local_gc_window_size < len(seq):
windows = dna.sliding_window(seq, local_gc_window_size)
for seq_window in windows:
lgc_content = dna.gc_content(seq_window)
if lgc_content < local_gc_content_min:
failure_reasons.append('local_gc_content_min: Local GC content is %.3f%% and must be at least %.3f%%'%(lgc_content,local_gc_content_min))
break
if lgc_content > local_gc_content_max:
failure_reasons.append('local_gc_content_max: Local GC content is %.3f%% and must be less than %.3f%%'%(lgc_content,local_gc_content_max))
break
# Homopolymers
for base in dna.dna_bases:
homopolymer = base * homopolymer_max_lengths[base]
if homopolymer in seq:
failure_reasons.append('max_%s_homopolymer: %s'%(
base.lower(), dna.case_highlight(seq,a_homopolymer)))
# Make sure all the checks passed.
if failure_reasons:
intro = "The given sequence fails following Gen9 design guidelines:"
raise ValueError('\n'.join([intro] + failure_reasons)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:oauth_connect; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:provider; 6, identifier:action; 7, block; 7, 8; 7, 10; 7, 17; 7, 34; 7, 71; 7, 79; 7, 85; 7, 94; 7, 103; 7, 107; 7, 111; 7, 115; 7, 154; 7, 261; 7, 393; 7, 404; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:valid_actions; 13, list:["connect", "authorized", "test"]; 13, 14; 13, 15; 13, 16; 14, string:"connect"; 15, string:"authorized"; 16, string:"test"; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:_redirect; 20, conditional_expression:if; 20, 21; 20, 28; 20, 31; 21, attribute; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:views; 25, identifier:auth; 26, identifier:Account; 27, identifier:account_settings; 28, call; 28, 29; 28, 30; 29, identifier:is_authenticated; 30, argument_list; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:login; 34, if_statement; 34, 35; 34, 65; 35, boolean_operator:or; 35, 36; 35, 58; 35, 59; 36, boolean_operator:or; 36, 37; 36, 50; 36, 51; 37, boolean_operator:or; 37, 38; 37, 41; 37, 42; 38, comparison_operator:not; 38, 39; 38, 40; 39, identifier:action; 40, identifier:valid_actions; 41, line_continuation:\; 42, comparison_operator:not; 42, 43; 42, 44; 43, string:"oauth"; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:__options__; 47, identifier:get; 48, argument_list; 48, 49; 49, string:"registration_methods"; 50, line_continuation:\; 51, not_operator; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:__options__; 55, identifier:get; 56, argument_list; 56, 57; 57, string:"allow_registration"; 58, line_continuation:\; 59, not_operator; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:hasattr; 62, argument_list; 62, 63; 62, 64; 63, identifier:oauth; 64, identifier:provider; 65, block; 65, 66; 66, return_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:redirect; 69, argument_list; 69, 70; 70, identifier:_redirect; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:client; 74, call; 74, 75; 74, 76; 75, identifier:getattr; 76, argument_list; 76, 77; 76, 78; 77, identifier:oauth; 78, identifier:provider; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:params; 82, attribute; 82, 83; 82, 84; 83, identifier:client; 84, identifier:__params__; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:me_args; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:params; 91, identifier:get; 92, argument_list; 92, 93; 93, string:"me"; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:user_id; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:params; 100, identifier:get; 101, argument_list; 101, 102; 102, string:"user_id"; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:oauth_user_id; 106, None; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:oauth_name; 110, None; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:oauth_email; 114, None; 115, if_statement; 115, 116; 115, 119; 116, comparison_operator:==; 116, 117; 116, 118; 117, identifier:action; 118, string:"test"; 119, block; 119, 120; 119, 136; 119, 141; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:session_data; 123, dictionary; 123, 124; 123, 127; 123, 130; 123, 133; 124, pair; 124, 125; 124, 126; 125, string:"provider"; 126, string:"ensure"; 127, pair; 127, 128; 127, 129; 128, string:"user_id"; 129, string:"1234"; 130, pair; 130, 131; 130, 132; 131, string:"name"; 132, string:"Mardix"; 133, pair; 133, 134; 133, 135; 134, string:"email"; 135, string:"[email protected]"; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:set_oauth_session; 139, argument_list; 139, 140; 140, identifier:session_data; 141, return_statement; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:redirect; 144, argument_list; 144, 145; 145, call; 145, 146; 145, 147; 146, identifier:url_for; 147, argument_list; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:register; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:oauth; 153, integer:1; 154, if_statement; 154, 155; 154, 158; 154, 204; 155, comparison_operator:==; 155, 156; 155, 157; 156, identifier:action; 157, string:"connect"; 158, block; 158, 159; 158, 170; 158, 195; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:_next; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:request; 166, identifier:args; 167, identifier:get; 168, argument_list; 168, 169; 169, string:'next'; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:authorized_url; 173, call; 173, 174; 173, 175; 174, identifier:url_for; 175, argument_list; 175, 176; 175, 177; 175, 180; 175, 183; 175, 192; 176, identifier:self; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:provider; 179, identifier:provider; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:action; 182, string:"authorized"; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:next; 185, boolean_operator:or; 185, 186; 185, 191; 186, boolean_operator:or; 186, 187; 186, 188; 187, identifier:_next; 188, attribute; 188, 189; 188, 190; 189, identifier:request; 190, identifier:referrer; 191, None; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:_external; 194, True; 195, return_statement; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:client; 199, identifier:authorize; 200, argument_list; 200, 201; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:callback; 203, identifier:authorized_url; 204, elif_clause; 204, 205; 204, 208; 205, comparison_operator:==; 205, 206; 205, 207; 206, identifier:action; 207, string:"authorized"; 208, block; 208, 209; 208, 217; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:resp; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:client; 215, identifier:authorized_response; 216, argument_list; 217, if_statement; 217, 218; 217, 221; 217, 223; 217, 235; 218, comparison_operator:is; 218, 219; 218, 220; 219, identifier:resp; 220, None; 221, block; 221, 222; 222, pass_statement; 223, elif_clause; 223, 224; 223, 229; 224, call; 224, 225; 224, 226; 225, identifier:isinstance; 226, argument_list; 226, 227; 226, 228; 227, identifier:resp; 228, identifier:OAuthException; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:flash_error; 233, argument_list; 233, 234; 234, string:"Access Denied"; 235, else_clause; 235, 236; 236, block; 236, 237; 237, if_statement; 237, 238; 237, 240; 237, 250; 238, not_operator; 238, 239; 239, identifier:me_args; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:oauth_user_id; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:resp; 247, identifier:get; 248, argument_list; 248, 249; 249, identifier:user_id; 250, else_clause; 250, 251; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:me; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:client; 258, identifier:get; 259, argument_list; 259, 260; 260, identifier:me_args; 261, if_statement; 261, 262; 261, 267; 261, 386; 262, boolean_operator:and; 262, 263; 262, 266; 263, comparison_operator:==; 263, 264; 263, 265; 264, identifier:action; 265, string:"authorized"; 266, identifier:oauth_user_id; 267, block; 267, 268; 268, if_statement; 268, 269; 268, 272; 268, 323; 268, 324; 269, call; 269, 270; 269, 271; 270, identifier:is_authenticated; 271, argument_list; 272, block; 272, 273; 272, 312; 273, try_statement; 273, 274; 273, 275; 273, 299; 274, comment; 275, block; 275, 276; 275, 288; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:current_user; 280, identifier:add_federated_login; 281, argument_list; 281, 282; 281, 285; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:provider; 284, identifier:provider; 285, keyword_argument; 285, 286; 285, 287; 286, identifier:federated_id; 287, identifier:oauth_user_id; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 291; 290, identifier:flash_success; 291, argument_list; 291, 292; 292, binary_operator:%; 292, 293; 292, 294; 293, string:"You can now login with your %s account"; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:provider; 297, identifier:upper; 298, argument_list; 299, except_clause; 299, 300; 299, 304; 300, as_pattern; 300, 301; 300, 302; 301, identifier:Exception; 302, as_pattern_target; 302, 303; 303, identifier:e; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:logging; 309, identifier:exception; 310, argument_list; 310, 311; 311, identifier:e; 312, return_statement; 312, 313; 313, call; 313, 314; 313, 315; 314, identifier:redirect; 315, argument_list; 315, 316; 316, attribute; 316, 317; 316, 322; 317, attribute; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:views; 320, identifier:auth; 321, identifier:Account; 322, identifier:account_settings; 323, comment; 324, else_clause; 324, 325; 324, 326; 325, comment; 326, block; 326, 327; 326, 335; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:user; 330, call; 330, 331; 330, 332; 331, identifier:with_federation; 332, argument_list; 332, 333; 332, 334; 333, identifier:provider; 334, identifier:oauth_user_id; 335, if_statement; 335, 336; 335, 337; 335, 362; 335, 363; 336, identifier:user; 337, block; 337, 338; 337, 343; 338, expression_statement; 338, 339; 339, call; 339, 340; 339, 341; 340, identifier:create_session; 341, argument_list; 341, 342; 342, identifier:user; 343, return_statement; 343, 344; 344, call; 344, 345; 344, 346; 345, identifier:redirect; 346, argument_list; 346, 347; 347, boolean_operator:or; 347, 348; 347, 356; 348, call; 348, 349; 348, 354; 349, attribute; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:request; 352, identifier:args; 353, identifier:get; 354, argument_list; 354, 355; 355, string:"next"; 356, call; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:__options__; 359, identifier:get; 360, argument_list; 360, 361; 361, string:"login_view"; 362, comment; 363, else_clause; 363, 364; 364, block; 364, 365; 364, 381; 365, expression_statement; 365, 366; 366, assignment; 366, 367; 366, 368; 367, identifier:session_data; 368, dictionary; 368, 369; 368, 372; 368, 375; 368, 378; 369, pair; 369, 370; 369, 371; 370, string:"provider"; 371, identifier:provider; 372, pair; 372, 373; 372, 374; 373, string:"user_id"; 374, identifier:oauth_user_id; 375, pair; 375, 376; 375, 377; 376, string:"name"; 377, identifier:oauth_name; 378, pair; 378, 379; 378, 380; 379, string:"email"; 380, identifier:oauth_email; 381, expression_statement; 381, 382; 382, call; 382, 383; 382, 384; 383, identifier:set_oauth_session; 384, argument_list; 384, 385; 385, identifier:session_data; 386, else_clause; 386, 387; 387, block; 387, 388; 388, return_statement; 388, 389; 389, call; 389, 390; 389, 391; 390, identifier:redirect; 391, argument_list; 391, 392; 392, identifier:_redirect; 393, return_statement; 393, 394; 394, dictionary; 394, 395; 394, 398; 394, 401; 395, pair; 395, 396; 395, 397; 396, string:"action"; 397, identifier:action; 398, pair; 398, 399; 398, 400; 399, string:"provider"; 400, identifier:provider; 401, pair; 401, 402; 401, 403; 402, string:"authorized_url"; 403, string:""; 404, return_statement; 404, 405; 405, call; 405, 406; 405, 407; 406, identifier:redirect; 407, argument_list; 407, 408; 408, identifier:_redirect | def oauth_connect(self, provider, action):
"""
This endpoint doesn't check if user is logged in, because it has two functions
1. If the user is not logged in, it will try to signup the user
- if the social info exist, it will login
- not, it will create a new account and proceed
2. If user is logged in, it will try to create a social login entry with
the current user
**** This methods doesn't save the user token, it only retrieves the ID
to login or ID, name, email if signing up
:param provider:
:param action: connect|authorized|
- connect: to connect to the endpoint
- authorized, when coming back
"""
valid_actions = ["connect", "authorized", "test"]
_redirect = views.auth.Account.account_settings if is_authenticated() else self.login
if action not in valid_actions \
or "oauth" not in __options__.get("registration_methods") \
or not __options__.get("allow_registration") \
or not hasattr(oauth, provider):
return redirect(_redirect)
client = getattr(oauth, provider)
params = client.__params__
me_args = params.get("me")
user_id = params.get("user_id")
oauth_user_id = None
oauth_name = None
oauth_email = None
if action == "test":
session_data = {
"provider": "ensure",
"user_id": "1234",
"name": "Mardix",
"email": "[email protected]",
}
set_oauth_session(session_data)
return redirect(url_for(self.register, oauth=1))
if action == "connect":
_next = request.args.get('next')
authorized_url = url_for(self,
provider=provider,
action="authorized",
next=_next or request.referrer or None,
_external=True)
return client.authorize(callback=authorized_url)
elif action == "authorized":
resp = client.authorized_response()
if resp is None:
pass
elif isinstance(resp, OAuthException):
flash_error("Access Denied")
else:
if not me_args:
oauth_user_id = resp.get(user_id)
else:
me = client.get(me_args)
if action == "authorized" and oauth_user_id:
if is_authenticated():
try:
# Add federated login to current_user
current_user.add_federated_login(provider=provider,
federated_id=oauth_user_id)
flash_success(
"You can now login with your %s account" % provider.upper())
except Exception as e:
logging.exception(e)
return redirect(views.auth.Account.account_settings)
# User not logged in
else:
# Existing user
user = with_federation(provider, oauth_user_id)
if user:
create_session(user)
return redirect(request.args.get("next") or __options__.get(
"login_view"))
# New User
else:
session_data = {
"provider": provider,
"user_id": oauth_user_id,
"name": oauth_name,
"email": oauth_email,
}
set_oauth_session(session_data)
else:
return redirect(_redirect)
return {
"action": action,
"provider": provider,
"authorized_url": ""
}
return redirect(_redirect) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:set_neighbors; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 25; 5, 29; 5, 186; 5, 195; 5, 206; 5, 210; 5, 237; 5, 246; 5, 266; 5, 284; 5, 298; 6, expression_statement; 6, 7; 7, string:'''Set neighbors for multi-environments, their slave environments,
and agents.
'''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:t; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:time; 14, identifier:time; 15, argument_list; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:logger; 22, identifier:debug; 23, argument_list; 23, 24; 24, string:"Settings grid neighbors for the multi-environments."; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:tasks; 28, list:[]; 29, for_statement; 29, 30; 29, 31; 29, 40; 30, identifier:i; 31, call; 31, 32; 31, 33; 32, identifier:range; 33, argument_list; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:len; 36, argument_list; 36, 37; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:grid; 40, block; 40, 41; 41, for_statement; 41, 42; 41, 43; 41, 54; 42, identifier:j; 43, call; 43, 44; 43, 45; 44, identifier:range; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, subscript; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:grid; 53, integer:0; 54, block; 54, 55; 54, 65; 54, 77; 54, 94; 54, 118; 54, 135; 54, 161; 54, 179; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:addr; 58, subscript; 58, 59; 58, 64; 59, subscript; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:grid; 63, identifier:i; 64, identifier:j; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 72; 67, pattern_list; 67, 68; 67, 69; 67, 70; 67, 71; 68, identifier:N; 69, identifier:E; 70, identifier:S; 71, identifier:W; 72, expression_list; 72, 73; 72, 74; 72, 75; 72, 76; 73, None; 74, None; 75, None; 76, None; 77, if_statement; 77, 78; 77, 81; 78, comparison_operator:!=; 78, 79; 78, 80; 79, identifier:i; 80, integer:0; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:W; 85, subscript; 85, 86; 85, 93; 86, subscript; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:grid; 90, binary_operator:-; 90, 91; 90, 92; 91, identifier:i; 92, integer:1; 93, identifier:j; 94, if_statement; 94, 95; 94, 105; 95, comparison_operator:!=; 95, 96; 95, 97; 96, identifier:i; 97, binary_operator:-; 97, 98; 97, 104; 98, call; 98, 99; 98, 100; 99, identifier:len; 100, argument_list; 100, 101; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:grid; 104, integer:1; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:E; 109, subscript; 109, 110; 109, 117; 110, subscript; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:grid; 114, binary_operator:+; 114, 115; 114, 116; 115, identifier:i; 116, integer:1; 117, identifier:j; 118, if_statement; 118, 119; 118, 122; 119, comparison_operator:!=; 119, 120; 119, 121; 120, identifier:j; 121, integer:0; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:N; 126, subscript; 126, 127; 126, 132; 127, subscript; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:grid; 131, identifier:i; 132, binary_operator:-; 132, 133; 132, 134; 133, identifier:j; 134, integer:1; 135, if_statement; 135, 136; 135, 148; 136, comparison_operator:!=; 136, 137; 136, 138; 137, identifier:j; 138, binary_operator:-; 138, 139; 138, 147; 139, call; 139, 140; 139, 141; 140, identifier:len; 141, argument_list; 141, 142; 142, subscript; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:grid; 146, integer:0; 147, integer:1; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:S; 152, subscript; 152, 153; 152, 158; 153, subscript; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:grid; 157, identifier:i; 158, binary_operator:+; 158, 159; 158, 160; 159, identifier:j; 160, integer:1; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:task; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:asyncio; 167, identifier:ensure_future; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:_set_node_neighbors; 173, argument_list; 173, 174; 173, 175; 173, 176; 173, 177; 173, 178; 174, identifier:addr; 175, identifier:N; 176, identifier:E; 177, identifier:S; 178, identifier:W; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:tasks; 183, identifier:append; 184, argument_list; 184, 185; 185, identifier:task; 186, expression_statement; 186, 187; 187, await; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:asyncio; 191, identifier:gather; 192, argument_list; 192, 193; 193, list_splat; 193, 194; 194, identifier:tasks; 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:debug; 202, argument_list; 202, 203; 203, concatenated_string; 203, 204; 203, 205; 204, string:"Setting grid neighbors for the slave environments "; 205, string:"and their agents."; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:tasks; 209, list:[]; 210, for_statement; 210, 211; 210, 212; 210, 215; 211, identifier:addr; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:addrs; 215, block; 215, 216; 215, 230; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:task; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:asyncio; 222, identifier:ensure_future; 223, argument_list; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:self; 227, identifier:_set_neighbors; 228, argument_list; 228, 229; 229, identifier:addr; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:tasks; 234, identifier:append; 235, argument_list; 235, 236; 236, identifier:task; 237, expression_statement; 237, 238; 238, await; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:asyncio; 242, identifier:gather; 243, argument_list; 243, 244; 244, list_splat; 244, 245; 245, identifier:tasks; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 253; 248, attribute; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:logger; 252, identifier:debug; 253, argument_list; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, string:"All grid neighbors set in {} seconds."; 257, identifier:format; 258, argument_list; 258, 259; 259, binary_operator:-; 259, 260; 259, 265; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:time; 263, identifier:time; 264, argument_list; 265, identifier:t; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:x; 269, binary_operator:*; 269, 270; 269, 281; 270, binary_operator:*; 270, 271; 270, 276; 271, subscript; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:_ngs; 275, integer:0; 276, subscript; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:self; 279, identifier:_gs; 280, integer:0; 281, attribute; 281, 282; 281, 283; 282, identifier:self; 283, identifier:_n_slaves; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:y; 287, binary_operator:*; 287, 288; 287, 293; 288, subscript; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:self; 291, identifier:_ngs; 292, integer:1; 293, subscript; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:self; 296, identifier:_gs; 297, integer:1; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 305; 300, attribute; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:self; 303, identifier:logger; 304, identifier:info; 305, argument_list; 305, 306; 306, call; 306, 307; 306, 312; 307, attribute; 307, 308; 307, 311; 308, concatenated_string; 308, 309; 308, 310; 309, string:"Initialized a distributed grid with overall size "; 310, string:"({}, {}). Total of {} agents."; 311, identifier:format; 312, argument_list; 312, 313; 312, 314; 312, 315; 313, identifier:x; 314, identifier:y; 315, binary_operator:*; 315, 316; 315, 317; 316, identifier:x; 317, identifier:y | async def set_neighbors(self):
'''Set neighbors for multi-environments, their slave environments,
and agents.
'''
t = time.time()
self.logger.debug("Settings grid neighbors for the multi-environments.")
tasks = []
for i in range(len(self.grid)):
for j in range(len(self.grid[0])):
addr = self.grid[i][j]
N, E, S, W = None, None, None, None
if i != 0:
W = self.grid[i-1][j]
if i != len(self.grid) - 1:
E = self.grid[i+1][j]
if j != 0:
N = self.grid[i][j-1]
if j != len(self.grid[0]) - 1:
S = self.grid[i][j+1]
task = asyncio.ensure_future(self._set_node_neighbors(addr, N, E, S, W))
tasks.append(task)
await asyncio.gather(*tasks)
self.logger.debug("Setting grid neighbors for the slave environments "
"and their agents.")
tasks = []
for addr in self.addrs:
task = asyncio.ensure_future(self._set_neighbors(addr))
tasks.append(task)
await asyncio.gather(*tasks)
self.logger.debug("All grid neighbors set in {} seconds."
.format(time.time() - t))
x = self._ngs[0] * self._gs[0] * self._n_slaves
y = self._ngs[1] * self._gs[1]
self.logger.info("Initialized a distributed grid with overall size "
"({}, {}). Total of {} agents.".format(x, y, x*y)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:vectors; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:direction; 7, string:"all"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:failed; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 35; 11, 53; 11, 54; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, if_statement; 15, 16; 15, 22; 16, comparison_operator:not; 16, 17; 16, 18; 17, identifier:direction; 18, list:["all", "incoming", "outgoing"]; 18, 19; 18, 20; 18, 21; 19, string:"all"; 20, string:"incoming"; 21, string:"outgoing"; 22, block; 22, 23; 23, raise_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:ValueError; 26, argument_list; 26, 27; 27, call; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, concatenated_string; 29, 30; 29, 31; 30, string:"{} is not a valid vector direction. "; 31, string:"Must be all, incoming or outgoing."; 32, identifier:format; 33, argument_list; 33, 34; 34, identifier:direction; 35, if_statement; 35, 36; 35, 42; 36, comparison_operator:not; 36, 37; 36, 38; 37, identifier:failed; 38, list:["all", False, True]; 38, 39; 38, 40; 38, 41; 39, string:"all"; 40, False; 41, True; 42, block; 42, 43; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:ValueError; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, string:"{} is not a valid vector failed"; 50, identifier:format; 51, argument_list; 51, 52; 52, identifier:failed; 53, comment; 54, if_statement; 54, 55; 54, 58; 54, 143; 55, comparison_operator:==; 55, 56; 55, 57; 56, identifier:failed; 57, string:"all"; 58, block; 58, 59; 58, 95; 58, 119; 59, if_statement; 59, 60; 59, 63; 60, comparison_operator:==; 60, 61; 60, 62; 61, identifier:direction; 62, string:"all"; 63, block; 63, 64; 64, return_statement; 64, 65; 65, call; 65, 66; 65, 94; 66, attribute; 66, 67; 66, 92; 66, 93; 67, call; 67, 68; 67, 74; 68, attribute; 68, 69; 68, 72; 68, 73; 69, attribute; 69, 70; 69, 71; 70, identifier:Vector; 71, identifier:query; 72, line_continuation:\; 73, identifier:filter; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:or_; 77, argument_list; 77, 78; 77, 85; 78, comparison_operator:==; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:Vector; 81, identifier:destination_id; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:id; 85, comparison_operator:==; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:Vector; 88, identifier:origin_id; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:id; 92, line_continuation:\; 93, identifier:all; 94, argument_list; 95, if_statement; 95, 96; 95, 99; 96, comparison_operator:==; 96, 97; 96, 98; 97, identifier:direction; 98, string:"incoming"; 99, block; 99, 100; 100, return_statement; 100, 101; 101, call; 101, 102; 101, 118; 102, attribute; 102, 103; 102, 116; 102, 117; 103, call; 103, 104; 103, 110; 104, attribute; 104, 105; 104, 108; 104, 109; 105, attribute; 105, 106; 105, 107; 106, identifier:Vector; 107, identifier:query; 108, line_continuation:\; 109, identifier:filter_by; 110, argument_list; 110, 111; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:destination_id; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:id; 116, line_continuation:\; 117, identifier:all; 118, argument_list; 119, if_statement; 119, 120; 119, 123; 120, comparison_operator:==; 120, 121; 120, 122; 121, identifier:direction; 122, string:"outgoing"; 123, block; 123, 124; 124, return_statement; 124, 125; 125, call; 125, 126; 125, 142; 126, attribute; 126, 127; 126, 140; 126, 141; 127, call; 127, 128; 127, 134; 128, attribute; 128, 129; 128, 132; 128, 133; 129, attribute; 129, 130; 129, 131; 130, identifier:Vector; 131, identifier:query; 132, line_continuation:\; 133, identifier:filter_by; 134, argument_list; 134, 135; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:origin_id; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:id; 140, line_continuation:\; 141, identifier:all; 142, argument_list; 143, else_clause; 143, 144; 144, block; 144, 145; 144, 189; 144, 216; 145, if_statement; 145, 146; 145, 149; 146, comparison_operator:==; 146, 147; 146, 148; 147, identifier:direction; 148, string:"all"; 149, block; 149, 150; 150, return_statement; 150, 151; 151, call; 151, 152; 151, 188; 152, attribute; 152, 153; 152, 186; 152, 187; 153, call; 153, 154; 153, 160; 154, attribute; 154, 155; 154, 158; 154, 159; 155, attribute; 155, 156; 155, 157; 156, identifier:Vector; 157, identifier:query; 158, line_continuation:\; 159, identifier:filter; 160, argument_list; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:and_; 163, argument_list; 163, 164; 163, 169; 164, comparison_operator:==; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:Vector; 167, identifier:failed; 168, identifier:failed; 169, call; 169, 170; 169, 171; 170, identifier:or_; 171, argument_list; 171, 172; 171, 179; 172, comparison_operator:==; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:Vector; 175, identifier:destination_id; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:id; 179, comparison_operator:==; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:Vector; 182, identifier:origin_id; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:id; 186, line_continuation:\; 187, identifier:all; 188, argument_list; 189, if_statement; 189, 190; 189, 193; 190, comparison_operator:==; 190, 191; 190, 192; 191, identifier:direction; 192, string:"incoming"; 193, block; 193, 194; 194, return_statement; 194, 195; 195, call; 195, 196; 195, 215; 196, attribute; 196, 197; 196, 213; 196, 214; 197, call; 197, 198; 197, 204; 198, attribute; 198, 199; 198, 202; 198, 203; 199, attribute; 199, 200; 199, 201; 200, identifier:Vector; 201, identifier:query; 202, line_continuation:\; 203, identifier:filter_by; 204, argument_list; 204, 205; 204, 210; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:destination_id; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:id; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:failed; 212, identifier:failed; 213, line_continuation:\; 214, identifier:all; 215, argument_list; 216, if_statement; 216, 217; 216, 220; 217, comparison_operator:==; 217, 218; 217, 219; 218, identifier:direction; 219, string:"outgoing"; 220, block; 220, 221; 221, return_statement; 221, 222; 222, call; 222, 223; 222, 242; 223, attribute; 223, 224; 223, 240; 223, 241; 224, call; 224, 225; 224, 231; 225, attribute; 225, 226; 225, 229; 225, 230; 226, attribute; 226, 227; 226, 228; 227, identifier:Vector; 228, identifier:query; 229, line_continuation:\; 230, identifier:filter_by; 231, argument_list; 231, 232; 231, 237; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:origin_id; 234, attribute; 234, 235; 234, 236; 235, identifier:self; 236, identifier:id; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:failed; 239, identifier:failed; 240, line_continuation:\; 241, identifier:all; 242, argument_list | def vectors(self, direction="all", failed=False):
"""Get vectors that connect at this node.
Direction can be "incoming", "outgoing" or "all" (default).
Failed can be True, False or all
"""
# check direction
if direction not in ["all", "incoming", "outgoing"]:
raise ValueError(
"{} is not a valid vector direction. "
"Must be all, incoming or outgoing.".format(direction))
if failed not in ["all", False, True]:
raise ValueError("{} is not a valid vector failed".format(failed))
# get the vectors
if failed == "all":
if direction == "all":
return Vector.query\
.filter(or_(Vector.destination_id == self.id,
Vector.origin_id == self.id))\
.all()
if direction == "incoming":
return Vector.query\
.filter_by(destination_id=self.id)\
.all()
if direction == "outgoing":
return Vector.query\
.filter_by(origin_id=self.id)\
.all()
else:
if direction == "all":
return Vector.query\
.filter(and_(Vector.failed == failed,
or_(Vector.destination_id == self.id,
Vector.origin_id == self.id)))\
.all()
if direction == "incoming":
return Vector.query\
.filter_by(destination_id=self.id, failed=failed)\
.all()
if direction == "outgoing":
return Vector.query\
.filter_by(origin_id=self.id, failed=failed)\
.all() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:transmissions; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:direction; 7, string:"outgoing"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:status; 10, string:"all"; 11, default_parameter; 11, 12; 11, 13; 12, identifier:failed; 13, False; 14, block; 14, 15; 14, 17; 14, 18; 14, 39; 14, 60; 14, 78; 14, 79; 14, 174; 14, 242; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, if_statement; 18, 19; 18, 25; 19, comparison_operator:not; 19, 20; 19, 21; 20, identifier:direction; 21, list:["incoming", "outgoing", "all"]; 21, 22; 21, 23; 21, 24; 22, string:"incoming"; 23, string:"outgoing"; 24, string:"all"; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, parenthesized_expression; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:ValueError; 30, argument_list; 30, 31; 31, binary_operator:+; 31, 32; 31, 38; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, string:"You cannot get transmissions of direction {}."; 35, identifier:format; 36, argument_list; 36, 37; 37, identifier:direction; 38, string:"Type can only be incoming, outgoing or all."; 39, if_statement; 39, 40; 39, 46; 40, comparison_operator:not; 40, 41; 40, 42; 41, identifier:status; 42, list:["all", "pending", "received"]; 42, 43; 42, 44; 42, 45; 43, string:"all"; 44, string:"pending"; 45, string:"received"; 46, block; 46, 47; 47, raise_statement; 47, 48; 48, parenthesized_expression; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:ValueError; 51, argument_list; 51, 52; 52, binary_operator:+; 52, 53; 52, 59; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, string:"You cannot get transmission of status {}."; 56, identifier:format; 57, argument_list; 57, 58; 58, identifier:status; 59, string:"Status can only be pending, received or all"; 60, if_statement; 60, 61; 60, 67; 61, comparison_operator:not; 61, 62; 61, 63; 62, identifier:failed; 63, list:["all", False, True]; 63, 64; 63, 65; 63, 66; 64, string:"all"; 65, False; 66, True; 67, block; 67, 68; 68, raise_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:ValueError; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, string:"{} is not a valid transmission failed"; 75, identifier:format; 76, argument_list; 76, 77; 77, identifier:failed; 78, comment; 79, if_statement; 79, 80; 79, 83; 80, comparison_operator:==; 80, 81; 80, 82; 81, identifier:direction; 82, string:"all"; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 88; 84, 128; 85, comparison_operator:==; 85, 86; 85, 87; 86, identifier:status; 87, string:"all"; 88, block; 88, 89; 89, return_statement; 89, 90; 90, call; 90, 91; 90, 127; 91, attribute; 91, 92; 91, 125; 91, 126; 92, call; 92, 93; 92, 99; 93, attribute; 93, 94; 93, 97; 93, 98; 94, attribute; 94, 95; 94, 96; 95, identifier:Transmission; 96, identifier:query; 97, line_continuation:\; 98, identifier:filter; 99, argument_list; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:and_; 102, argument_list; 102, 103; 102, 108; 103, comparison_operator:==; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:Transmission; 106, identifier:failed; 107, False; 108, call; 108, 109; 108, 110; 109, identifier:or_; 110, argument_list; 110, 111; 110, 118; 111, comparison_operator:==; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:Transmission; 114, identifier:destination_id; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:id; 118, comparison_operator:==; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:Transmission; 121, identifier:origin_id; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:id; 125, line_continuation:\; 126, identifier:all; 127, argument_list; 128, else_clause; 128, 129; 129, block; 129, 130; 130, return_statement; 130, 131; 131, call; 131, 132; 131, 173; 132, attribute; 132, 133; 132, 171; 132, 172; 133, call; 133, 134; 133, 140; 134, attribute; 134, 135; 134, 138; 134, 139; 135, attribute; 135, 136; 135, 137; 136, identifier:Transmission; 137, identifier:query; 138, line_continuation:\; 139, identifier:filter; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:and_; 143, argument_list; 143, 144; 143, 149; 143, 154; 144, comparison_operator:==; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:Transmission; 147, identifier:failed; 148, False; 149, comparison_operator:==; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:Transmission; 152, identifier:status; 153, identifier:status; 154, call; 154, 155; 154, 156; 155, identifier:or_; 156, argument_list; 156, 157; 156, 164; 157, comparison_operator:==; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:Transmission; 160, identifier:destination_id; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:id; 164, comparison_operator:==; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:Transmission; 167, identifier:origin_id; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:id; 171, line_continuation:\; 172, identifier:all; 173, argument_list; 174, if_statement; 174, 175; 174, 178; 175, comparison_operator:==; 175, 176; 175, 177; 176, identifier:direction; 177, string:"incoming"; 178, block; 178, 179; 179, if_statement; 179, 180; 179, 183; 179, 206; 180, comparison_operator:==; 180, 181; 180, 182; 181, identifier:status; 182, string:"all"; 183, block; 183, 184; 184, return_statement; 184, 185; 185, call; 185, 186; 185, 205; 186, attribute; 186, 187; 186, 203; 186, 204; 187, call; 187, 188; 187, 194; 188, attribute; 188, 189; 188, 192; 188, 193; 189, attribute; 189, 190; 189, 191; 190, identifier:Transmission; 191, identifier:query; 192, line_continuation:\; 193, identifier:filter_by; 194, argument_list; 194, 195; 194, 198; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:failed; 197, False; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:destination_id; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:id; 203, line_continuation:\; 204, identifier:all; 205, argument_list; 206, else_clause; 206, 207; 207, block; 207, 208; 208, return_statement; 208, 209; 209, call; 209, 210; 209, 241; 210, attribute; 210, 211; 210, 239; 210, 240; 211, call; 211, 212; 211, 218; 212, attribute; 212, 213; 212, 216; 212, 217; 213, attribute; 213, 214; 213, 215; 214, identifier:Transmission; 215, identifier:query; 216, line_continuation:\; 217, identifier:filter; 218, argument_list; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:and_; 221, argument_list; 221, 222; 221, 227; 221, 234; 222, comparison_operator:==; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:Transmission; 225, identifier:failed; 226, False; 227, comparison_operator:==; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:Transmission; 230, identifier:destination_id; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:id; 234, comparison_operator:==; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:Transmission; 237, identifier:status; 238, identifier:status; 239, line_continuation:\; 240, identifier:all; 241, argument_list; 242, if_statement; 242, 243; 242, 246; 243, comparison_operator:==; 243, 244; 243, 245; 244, identifier:direction; 245, string:"outgoing"; 246, block; 246, 247; 247, if_statement; 247, 248; 247, 251; 247, 274; 248, comparison_operator:==; 248, 249; 248, 250; 249, identifier:status; 250, string:"all"; 251, block; 251, 252; 252, return_statement; 252, 253; 253, call; 253, 254; 253, 273; 254, attribute; 254, 255; 254, 271; 254, 272; 255, call; 255, 256; 255, 262; 256, attribute; 256, 257; 256, 260; 256, 261; 257, attribute; 257, 258; 257, 259; 258, identifier:Transmission; 259, identifier:query; 260, line_continuation:\; 261, identifier:filter_by; 262, argument_list; 262, 263; 262, 266; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:failed; 265, False; 266, keyword_argument; 266, 267; 266, 268; 267, identifier:origin_id; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:id; 271, line_continuation:\; 272, identifier:all; 273, argument_list; 274, else_clause; 274, 275; 275, block; 275, 276; 276, return_statement; 276, 277; 277, call; 277, 278; 277, 309; 278, attribute; 278, 279; 278, 307; 278, 308; 279, call; 279, 280; 279, 286; 280, attribute; 280, 281; 280, 284; 280, 285; 281, attribute; 281, 282; 281, 283; 282, identifier:Transmission; 283, identifier:query; 284, line_continuation:\; 285, identifier:filter; 286, argument_list; 286, 287; 287, call; 287, 288; 287, 289; 288, identifier:and_; 289, argument_list; 289, 290; 289, 295; 289, 302; 290, comparison_operator:==; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:Transmission; 293, identifier:failed; 294, False; 295, comparison_operator:==; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:Transmission; 298, identifier:origin_id; 299, attribute; 299, 300; 299, 301; 300, identifier:self; 301, identifier:id; 302, comparison_operator:==; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:Transmission; 305, identifier:status; 306, identifier:status; 307, line_continuation:\; 308, identifier:all; 309, argument_list | def transmissions(self, direction="outgoing", status="all", failed=False):
"""Get transmissions sent to or from this node.
Direction can be "all", "incoming" or "outgoing" (default).
Status can be "all" (default), "pending", or "received".
failed can be True, False or "all"
"""
# check parameters
if direction not in ["incoming", "outgoing", "all"]:
raise(ValueError("You cannot get transmissions of direction {}."
.format(direction) +
"Type can only be incoming, outgoing or all."))
if status not in ["all", "pending", "received"]:
raise(ValueError("You cannot get transmission of status {}."
.format(status) +
"Status can only be pending, received or all"))
if failed not in ["all", False, True]:
raise ValueError("{} is not a valid transmission failed"
.format(failed))
# get transmissions
if direction == "all":
if status == "all":
return Transmission.query\
.filter(and_(Transmission.failed == False,
or_(Transmission.destination_id == self.id,
Transmission.origin_id == self.id)))\
.all()
else:
return Transmission.query\
.filter(and_(Transmission.failed == False,
Transmission.status == status,
or_(Transmission.destination_id == self.id,
Transmission.origin_id == self.id)))\
.all()
if direction == "incoming":
if status == "all":
return Transmission.query\
.filter_by(failed=False, destination_id=self.id)\
.all()
else:
return Transmission.query\
.filter(and_(Transmission.failed == False,
Transmission.destination_id == self.id,
Transmission.status == status))\
.all()
if direction == "outgoing":
if status == "all":
return Transmission.query\
.filter_by(failed=False, origin_id=self.id)\
.all()
else:
return Transmission.query\
.filter(and_(Transmission.failed == False,
Transmission.origin_id == self.id,
Transmission.status == status))\
.all() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:user_stats; 3, parameters; 3, 4; 4, identifier:request; 5, block; 5, 6; 5, 8; 5, 13; 5, 17; 5, 21; 5, 46; 5, 63; 5, 82; 5, 88; 5, 104; 5, 111; 5, 118; 5, 138; 5, 150; 5, 166; 5, 176; 5, 186; 5, 252; 5, 364; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 11; 10, identifier:timer; 11, argument_list; 11, 12; 12, string:'user_stats'; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:response; 16, dictionary; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:data; 20, None; 21, if_statement; 21, 22; 21, 27; 22, comparison_operator:==; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:request; 25, identifier:method; 26, string:"POST"; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:data; 31, subscript; 31, 32; 31, 45; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:json; 35, identifier:loads; 36, argument_list; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:request; 41, identifier:body; 42, identifier:decode; 43, argument_list; 43, 44; 44, string:"utf-8"; 45, string:"filters"; 46, if_statement; 46, 47; 46, 52; 47, comparison_operator:in; 47, 48; 47, 49; 48, string:"filters"; 49, attribute; 49, 50; 49, 51; 50, identifier:request; 51, identifier:GET; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:data; 56, call; 56, 57; 56, 58; 57, identifier:load_query_json; 58, argument_list; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:request; 61, identifier:GET; 62, string:"filters"; 63, if_statement; 63, 64; 63, 67; 64, comparison_operator:is; 64, 65; 64, 66; 65, identifier:data; 66, None; 67, block; 67, 68; 68, return_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:render_json; 71, argument_list; 71, 72; 71, 73; 71, 74; 71, 77; 72, identifier:request; 73, dictionary; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:template; 76, string:'models_user_stats.html'; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:help_text; 79, attribute; 79, 80; 79, 81; 80, identifier:user_stats; 81, identifier:__doc__; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:environment; 85, call; 85, 86; 85, 87; 86, identifier:get_environment; 87, argument_list; 88, if_statement; 88, 89; 88, 93; 89, call; 89, 90; 89, 91; 90, identifier:is_time_overridden; 91, argument_list; 91, 92; 92, identifier:request; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:environment; 98, identifier:shift_time; 99, argument_list; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:get_time; 102, argument_list; 102, 103; 103, identifier:request; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:user_id; 107, call; 107, 108; 107, 109; 108, identifier:get_user_id; 109, argument_list; 109, 110; 110, identifier:request; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:language; 114, call; 114, 115; 114, 116; 115, identifier:get_language; 116, argument_list; 116, 117; 117, identifier:request; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, pattern_list; 120, 121; 120, 122; 121, identifier:filter_names; 122, identifier:filter_filters; 123, call; 123, 124; 123, 125; 124, identifier:list; 125, argument_list; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:zip; 128, argument_list; 128, 129; 129, list_splat; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:sorted; 132, argument_list; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:data; 136, identifier:items; 137, argument_list; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:reachable_leaves; 141, call; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:Item; 145, identifier:objects; 146, identifier:filter_all_reachable_leaves_many; 147, argument_list; 147, 148; 147, 149; 148, identifier:filter_filters; 149, identifier:language; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:all_leaves; 153, call; 153, 154; 153, 155; 154, identifier:sorted; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:list; 158, argument_list; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:set; 161, argument_list; 161, 162; 162, call; 162, 163; 162, 164; 163, identifier:flatten; 164, argument_list; 164, 165; 165, identifier:reachable_leaves; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:answers; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:environment; 172, identifier:number_of_answers_more_items; 173, argument_list; 173, 174; 173, 175; 174, identifier:all_leaves; 175, identifier:user_id; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:correct_answers; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:environment; 182, identifier:number_of_correct_answers_more_items; 183, argument_list; 183, 184; 183, 185; 184, identifier:all_leaves; 185, identifier:user_id; 186, if_statement; 186, 187; 186, 195; 187, call; 187, 188; 187, 193; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:request; 191, identifier:GET; 192, identifier:get; 193, argument_list; 193, 194; 194, string:"mastered"; 195, block; 195, 196; 195, 201; 195, 207; 195, 220; 195, 240; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:timer; 199, argument_list; 199, 200; 200, string:'user_stats_mastered'; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:mastery_threshold; 204, call; 204, 205; 204, 206; 205, identifier:get_mastery_trashold; 206, argument_list; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:predictions; 210, call; 210, 211; 210, 216; 211, attribute; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:Item; 214, identifier:objects; 215, identifier:predict_for_overview; 216, argument_list; 216, 217; 216, 218; 216, 219; 217, identifier:environment; 218, identifier:user_id; 219, identifier:all_leaves; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:mastered; 223, call; 223, 224; 223, 225; 224, identifier:dict; 225, argument_list; 225, 226; 226, call; 226, 227; 226, 228; 227, identifier:list; 228, argument_list; 228, 229; 229, call; 229, 230; 229, 231; 230, identifier:zip; 231, argument_list; 231, 232; 231, 233; 232, identifier:all_leaves; 233, list_comprehension; 233, 234; 233, 237; 234, comparison_operator:>=; 234, 235; 234, 236; 235, identifier:p; 236, identifier:mastery_threshold; 237, for_in_clause; 237, 238; 237, 239; 238, identifier:p; 239, identifier:predictions; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:LOGGER; 244, identifier:debug; 245, argument_list; 245, 246; 245, 247; 246, string:"user_stats - getting predictions for items took %s seconds"; 247, parenthesized_expression; 247, 248; 248, call; 248, 249; 248, 250; 249, identifier:timer; 250, argument_list; 250, 251; 251, string:'user_stats_mastered'; 252, for_statement; 252, 253; 252, 256; 252, 261; 253, pattern_list; 253, 254; 253, 255; 254, identifier:identifier; 255, identifier:items; 256, call; 256, 257; 256, 258; 257, identifier:zip; 258, argument_list; 258, 259; 258, 260; 259, identifier:filter_names; 260, identifier:reachable_leaves; 261, block; 261, 262; 262, if_statement; 262, 263; 262, 269; 262, 284; 263, comparison_operator:==; 263, 264; 263, 268; 264, call; 264, 265; 264, 266; 265, identifier:len; 266, argument_list; 266, 267; 267, identifier:items; 268, integer:0; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:response; 274, identifier:identifier; 275, dictionary; 275, 276; 275, 281; 276, pair; 276, 277; 276, 278; 277, string:"filter"; 278, subscript; 278, 279; 278, 280; 279, identifier:data; 280, identifier:identifier; 281, pair; 281, 282; 281, 283; 282, string:"number_of_items"; 283, integer:0; 284, else_clause; 284, 285; 285, block; 285, 286; 285, 338; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 291; 288, subscript; 288, 289; 288, 290; 289, identifier:response; 290, identifier:identifier; 291, dictionary; 291, 292; 291, 297; 291, 303; 291, 316; 291, 327; 292, pair; 292, 293; 292, 294; 293, string:"filter"; 294, subscript; 294, 295; 294, 296; 295, identifier:data; 296, identifier:identifier; 297, pair; 297, 298; 297, 299; 298, string:"number_of_items"; 299, call; 299, 300; 299, 301; 300, identifier:len; 301, argument_list; 301, 302; 302, identifier:items; 303, pair; 303, 304; 303, 305; 304, string:"number_of_practiced_items"; 305, call; 305, 306; 305, 307; 306, identifier:sum; 307, generator_expression; 307, 308; 307, 313; 308, comparison_operator:>; 308, 309; 308, 312; 309, subscript; 309, 310; 309, 311; 310, identifier:answers; 311, identifier:i; 312, integer:0; 313, for_in_clause; 313, 314; 313, 315; 314, identifier:i; 315, identifier:items; 316, pair; 316, 317; 316, 318; 317, string:"number_of_answers"; 318, call; 318, 319; 318, 320; 319, identifier:sum; 320, generator_expression; 320, 321; 320, 324; 321, subscript; 321, 322; 321, 323; 322, identifier:answers; 323, identifier:i; 324, for_in_clause; 324, 325; 324, 326; 325, identifier:i; 326, identifier:items; 327, pair; 327, 328; 327, 329; 328, string:"number_of_correct_answers"; 329, call; 329, 330; 329, 331; 330, identifier:sum; 331, generator_expression; 331, 332; 331, 335; 332, subscript; 332, 333; 332, 334; 333, identifier:correct_answers; 334, identifier:i; 335, for_in_clause; 335, 336; 335, 337; 336, identifier:i; 337, identifier:items; 338, if_statement; 338, 339; 338, 347; 339, call; 339, 340; 339, 345; 340, attribute; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:request; 343, identifier:GET; 344, identifier:get; 345, argument_list; 345, 346; 346, string:"mastered"; 347, block; 347, 348; 348, expression_statement; 348, 349; 349, assignment; 349, 350; 349, 355; 350, subscript; 350, 351; 350, 354; 351, subscript; 351, 352; 351, 353; 352, identifier:response; 353, identifier:identifier; 354, string:"number_of_mastered_items"; 355, call; 355, 356; 355, 357; 356, identifier:sum; 357, generator_expression; 357, 358; 357, 361; 358, subscript; 358, 359; 358, 360; 359, identifier:mastered; 360, identifier:i; 361, for_in_clause; 361, 362; 361, 363; 362, identifier:i; 363, identifier:items; 364, return_statement; 364, 365; 365, call; 365, 366; 365, 367; 366, identifier:render_json; 367, argument_list; 367, 368; 367, 369; 367, 370; 367, 373; 368, identifier:request; 369, identifier:response; 370, keyword_argument; 370, 371; 370, 372; 371, identifier:template; 372, string:'models_user_stats.html'; 373, keyword_argument; 373, 374; 373, 375; 374, identifier:help_text; 375, attribute; 375, 376; 375, 377; 376, identifier:user_stats; 377, identifier:__doc__ | def user_stats(request):
"""
Get user statistics for selected groups of items
time:
time in format '%Y-%m-%d_%H:%M:%S' used for practicing
user:
identifier of the user (only for stuff users)
username:
username of user (only for users with public profile)
filters: -- use this or body
json as in BODY
mastered:
use model to compute number of mastered items - can be slowed
language:
language of the items
BODY
json in following format:
{
"#identifier": [] -- custom identifier (str) and filter
...
}
"""
timer('user_stats')
response = {}
data = None
if request.method == "POST":
data = json.loads(request.body.decode("utf-8"))["filters"]
if "filters" in request.GET:
data = load_query_json(request.GET, "filters")
if data is None:
return render_json(request, {}, template='models_user_stats.html', help_text=user_stats.__doc__)
environment = get_environment()
if is_time_overridden(request):
environment.shift_time(get_time(request))
user_id = get_user_id(request)
language = get_language(request)
filter_names, filter_filters = list(zip(*sorted(data.items())))
reachable_leaves = Item.objects.filter_all_reachable_leaves_many(filter_filters, language)
all_leaves = sorted(list(set(flatten(reachable_leaves))))
answers = environment.number_of_answers_more_items(all_leaves, user_id)
correct_answers = environment.number_of_correct_answers_more_items(all_leaves, user_id)
if request.GET.get("mastered"):
timer('user_stats_mastered')
mastery_threshold = get_mastery_trashold()
predictions = Item.objects.predict_for_overview(environment, user_id, all_leaves)
mastered = dict(list(zip(all_leaves, [p >= mastery_threshold for p in predictions])))
LOGGER.debug("user_stats - getting predictions for items took %s seconds", (timer('user_stats_mastered')))
for identifier, items in zip(filter_names, reachable_leaves):
if len(items) == 0:
response[identifier] = {
"filter": data[identifier],
"number_of_items": 0,
}
else:
response[identifier] = {
"filter": data[identifier],
"number_of_items": len(items),
"number_of_practiced_items": sum(answers[i] > 0 for i in items),
"number_of_answers": sum(answers[i] for i in items),
"number_of_correct_answers": sum(correct_answers[i] for i in items),
}
if request.GET.get("mastered"):
response[identifier]["number_of_mastered_items"]= sum(mastered[i] for i in items)
return render_json(request, response, template='models_user_stats.html', help_text=user_stats.__doc__) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:create_resource; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 4, identifier:output_model; 5, identifier:rtype; 6, identifier:unique; 7, identifier:links; 8, default_parameter; 8, 9; 8, 10; 9, identifier:existing_ids; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:id_helper; 13, None; 14, block; 14, 15; 14, 17; 14, 62; 14, 83; 14, 97; 14, 118; 14, 127; 14, 142; 15, expression_statement; 15, 16; 16, string:'''
General-purpose routine to create a new resource in the output model, based on data provided
output_model - Versa connection to model to be updated
rtype - Type IRI for the new resource, set with Versa type
unique - list of key/value pairs for determining a unique hash for the new resource
links - list of key/value pairs for setting properties on the new resource
id_helper - If a string, a base URL for the generatd ID. If callable, a function used to return the entity. If None, set a default good enough for testing.
existing_ids - set of existing IDs to not recreate, or None, in which case a new resource will always be created
'''; 17, if_statement; 17, 18; 17, 23; 17, 31; 17, 42; 17, 54; 18, call; 18, 19; 18, 20; 19, identifier:isinstance; 20, argument_list; 20, 21; 20, 22; 21, identifier:id_helper; 22, identifier:str; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:idg; 27, call; 27, 28; 27, 29; 28, identifier:idgen; 29, argument_list; 29, 30; 30, identifier:id_helper; 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:id_helper; 36, identifier:GeneratorType; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:idg; 41, identifier:id_helper; 42, elif_clause; 42, 43; 42, 46; 43, comparison_operator:is; 43, 44; 43, 45; 44, identifier:id_helper; 45, None; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:idg; 50, call; 50, 51; 50, 52; 51, identifier:default_idgen; 52, argument_list; 52, 53; 53, None; 54, else_clause; 54, 55; 54, 56; 55, comment; 56, block; 56, 57; 57, raise_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:ValueError; 60, argument_list; 60, 61; 61, string:'id_helper must be string (URL), callable or None'; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:ctx; 65, call; 65, 66; 65, 67; 66, identifier:context; 67, argument_list; 67, 68; 67, 69; 67, 70; 67, 71; 67, 74; 67, 77; 67, 80; 68, None; 69, None; 70, identifier:output_model; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:base; 73, None; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:idgen; 76, identifier:idg; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:existing_ids; 79, identifier:existing_ids; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:extras; 82, None; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:rid; 86, call; 86, 87; 86, 88; 87, identifier:I; 88, argument_list; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:materialize_entity; 91, argument_list; 91, 92; 91, 93; 91, 94; 92, identifier:ctx; 93, identifier:rtype; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:unique; 96, identifier:unique; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:is; 98, 99; 98, 100; 99, identifier:existing_ids; 100, None; 101, block; 101, 102; 101, 111; 102, if_statement; 102, 103; 102, 106; 103, comparison_operator:in; 103, 104; 103, 105; 104, identifier:rid; 105, identifier:existing_ids; 106, block; 106, 107; 107, return_statement; 107, 108; 108, tuple; 108, 109; 108, 110; 109, False; 110, identifier:rid; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:existing_ids; 115, identifier:add; 116, argument_list; 116, 117; 117, identifier:rid; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:output_model; 122, identifier:add; 123, argument_list; 123, 124; 123, 125; 123, 126; 124, identifier:rid; 125, identifier:VTYPE_REL; 126, identifier:rtype; 127, for_statement; 127, 128; 127, 131; 127, 132; 128, pattern_list; 128, 129; 128, 130; 129, identifier:r; 130, identifier:t; 131, identifier:links; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:output_model; 137, identifier:add; 138, argument_list; 138, 139; 138, 140; 138, 141; 139, identifier:rid; 140, identifier:r; 141, identifier:t; 142, return_statement; 142, 143; 143, tuple; 143, 144; 143, 145; 144, True; 145, identifier:rid | def create_resource(output_model, rtype, unique, links, existing_ids=None, id_helper=None):
'''
General-purpose routine to create a new resource in the output model, based on data provided
output_model - Versa connection to model to be updated
rtype - Type IRI for the new resource, set with Versa type
unique - list of key/value pairs for determining a unique hash for the new resource
links - list of key/value pairs for setting properties on the new resource
id_helper - If a string, a base URL for the generatd ID. If callable, a function used to return the entity. If None, set a default good enough for testing.
existing_ids - set of existing IDs to not recreate, or None, in which case a new resource will always be created
'''
if isinstance(id_helper, str):
idg = idgen(id_helper)
elif isinstance(id_helper, GeneratorType):
idg = id_helper
elif id_helper is None:
idg = default_idgen(None)
else:
#FIXME: G11N
raise ValueError('id_helper must be string (URL), callable or None')
ctx = context(None, None, output_model, base=None, idgen=idg, existing_ids=existing_ids, extras=None)
rid = I(materialize_entity(ctx, rtype, unique=unique))
if existing_ids is not None:
if rid in existing_ids:
return (False, rid)
existing_ids.add(rid)
output_model.add(rid, VTYPE_REL, rtype)
for r, t in links:
output_model.add(rid, r, t)
return (True, rid) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:patterns; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 55; 5, 56; 5, 62; 5, 94; 5, 105; 5, 113; 5, 114; 5, 130; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 23; 10, boolean_operator:and; 10, 11; 10, 17; 11, not_operator; 11, 12; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:args; 16, identifier:patterns; 17, not_operator; 17, 18; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:args; 22, identifier:pattern_files; 23, block; 23, 24; 24, try_statement; 24, 25; 24, 46; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 35; 28, attribute; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:args; 33, identifier:patterns; 34, identifier:append; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 44; 37, attribute; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:args; 42, identifier:files; 43, identifier:pop; 44, argument_list; 44, 45; 45, integer:0; 46, except_clause; 46, 47; 46, 48; 47, identifier:IndexError; 48, block; 48, 49; 49, raise_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:LogRaptorArgumentError; 52, argument_list; 52, 53; 52, 54; 53, string:'PATTERN'; 54, string:'no search pattern'; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:patterns; 59, call; 59, 60; 59, 61; 60, identifier:set; 61, argument_list; 62, if_statement; 62, 63; 62, 68; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:args; 67, identifier:pattern_files; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:patterns; 73, identifier:update; 74, argument_list; 74, 75; 75, list_comprehension; 75, 76; 75, 82; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:p; 79, identifier:rstrip; 80, argument_list; 80, 81; 81, string:'\n'; 82, for_in_clause; 82, 83; 82, 84; 83, identifier:p; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:fileinput; 87, identifier:input; 88, argument_list; 88, 89; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:args; 93, identifier:pattern_files; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:patterns; 98, identifier:update; 99, argument_list; 99, 100; 100, attribute; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:args; 104, identifier:patterns; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:logger; 109, identifier:debug; 110, argument_list; 110, 111; 110, 112; 111, string:"search patterns to be processed: %r"; 112, identifier:patterns; 113, comment; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:in; 115, 116; 115, 117; 116, string:''; 117, identifier:patterns; 118, block; 118, 119; 118, 126; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:logger; 123, identifier:info; 124, argument_list; 124, 125; 125, string:"an empty pattern provided: match all strings!"; 126, return_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:tuple; 129, argument_list; 130, try_statement; 130, 131; 130, 179; 131, block; 131, 132; 131, 149; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:flags; 135, conditional_expression:if; 135, 136; 135, 139; 135, 144; 136, attribute; 136, 137; 136, 138; 137, identifier:re; 138, identifier:IGNORECASE; 139, attribute; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:args; 143, identifier:case; 144, binary_operator:|; 144, 145; 144, 146; 145, integer:0; 146, attribute; 146, 147; 146, 148; 147, identifier:re; 148, identifier:UNICODE; 149, return_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:tuple; 152, argument_list; 152, 153; 153, list_comprehension; 153, 154; 153, 174; 153, 177; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:re; 157, identifier:compile; 158, argument_list; 158, 159; 158, 171; 159, conditional_expression:if; 159, 160; 159, 163; 159, 168; 160, binary_operator:%; 160, 161; 160, 162; 161, string:r'(\b%s\b)'; 162, identifier:pat; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:args; 167, identifier:word; 168, binary_operator:%; 168, 169; 168, 170; 169, string:'(%s)'; 170, identifier:pat; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:flags; 173, identifier:flags; 174, for_in_clause; 174, 175; 174, 176; 175, identifier:pat; 176, identifier:patterns; 177, if_clause; 177, 178; 178, identifier:pat; 179, except_clause; 179, 180; 179, 186; 180, as_pattern; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:re; 183, identifier:error; 184, as_pattern_target; 184, 185; 185, identifier:err; 186, block; 186, 187; 187, raise_statement; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:LogRaptorArgumentError; 190, argument_list; 190, 191; 191, binary_operator:%; 191, 192; 191, 193; 192, string:'wrong regex syntax for pattern: %r'; 193, identifier:err | def patterns(self):
"""
A tuple with re.RegexObject objects created from regex pattern arguments.
"""
# No explicit argument for patterns ==> consider the first source argument as pattern.
if not self.args.patterns and not self.args.pattern_files:
try:
self.args.patterns.append(self.args.files.pop(0))
except IndexError:
raise LogRaptorArgumentError('PATTERN', 'no search pattern')
# Get the patterns from arguments and files
patterns = set()
if self.args.pattern_files:
patterns.update([p.rstrip('\n') for p in fileinput.input(self.args.pattern_files)])
patterns.update(self.args.patterns)
logger.debug("search patterns to be processed: %r", patterns)
# If one pattern is empty then skip the other patterns
if '' in patterns:
logger.info("an empty pattern provided: match all strings!")
return tuple()
try:
flags = re.IGNORECASE if self.args.case else 0 | re.UNICODE
return tuple([
re.compile(r'(\b%s\b)' % pat if self.args.word else '(%s)' % pat, flags=flags)
for pat in patterns if pat
])
except re.error as err:
raise LogRaptorArgumentError('wrong regex syntax for pattern: %r' % err) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:qualify; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:qualification; 5, identifier:value; 6, identifier:worker; 7, block; 7, 8; 7, 10; 7, 11; 7, 18; 7, 24; 7, 30; 7, 40; 7, 50; 7, 58; 7, 126; 7, 133; 7, 143; 7, 144; 7, 158; 7, 187; 7, 197; 7, 198; 7, 205; 7, 221; 7, 231; 7, 247; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, import_from_statement; 11, 12; 11, 16; 12, dotted_name; 12, 13; 12, 14; 12, 15; 13, identifier:boto; 14, identifier:mturk; 15, identifier:connection; 16, dotted_name; 16, 17; 17, identifier:MTurkConnection; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:config; 21, call; 21, 22; 21, 23; 22, identifier:PsiturkConfig; 23, argument_list; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:config; 28, identifier:load_config; 29, argument_list; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:aws_access_key_id; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:config; 36, identifier:get; 37, argument_list; 37, 38; 37, 39; 38, string:'AWS Access'; 39, string:'aws_access_key_id'; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:aws_secret_access_key; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:config; 46, identifier:get; 47, argument_list; 47, 48; 47, 49; 48, string:'AWS Access'; 49, string:'aws_secret_access_key'; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:conn; 53, call; 53, 54; 53, 55; 54, identifier:MTurkConnection; 55, argument_list; 55, 56; 55, 57; 56, identifier:aws_access_key_id; 57, identifier:aws_secret_access_key; 58, function_definition; 58, 59; 58, 60; 58, 62; 59, function_name:get_workers_with_qualification; 60, parameters; 60, 61; 61, identifier:qualification; 62, block; 62, 63; 62, 65; 62, 69; 62, 73; 62, 77; 62, 124; 63, expression_statement; 63, 64; 64, comment; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:results; 68, list:[]; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:continue_flag; 72, True; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:page; 76, integer:1; 77, while_statement; 77, 78; 77, 80; 78, parenthesized_expression; 78, 79; 79, identifier:continue_flag; 80, block; 80, 81; 80, 96; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:new_results; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:conn; 87, identifier:get_qualifications_for_qualification_type; 88, argument_list; 88, 89; 88, 90; 88, 93; 89, identifier:qualification; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:page_size; 92, integer:100; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:page_number; 95, identifier:page; 96, if_statement; 96, 97; 96, 104; 96, 109; 97, parenthesized_expression; 97, 98; 98, comparison_operator:==; 98, 99; 98, 103; 99, call; 99, 100; 99, 101; 100, identifier:len; 101, argument_list; 101, 102; 102, identifier:new_results; 103, integer:0; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:continue_flag; 108, False; 109, else_clause; 109, 110; 110, block; 110, 111; 110, 118; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:results; 115, identifier:extend; 116, argument_list; 116, 117; 117, identifier:new_results; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:page; 121, binary_operator:+; 121, 122; 121, 123; 122, identifier:page; 123, integer:1; 124, return_statement; 124, 125; 125, identifier:results; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:results; 129, call; 129, 130; 129, 131; 130, identifier:get_workers_with_qualification; 131, argument_list; 131, 132; 132, identifier:qualification; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:workers; 136, list_comprehension; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:x; 139, identifier:SubjectId; 140, for_in_clause; 140, 141; 140, 142; 141, identifier:x; 142, identifier:results; 143, comment; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:click; 148, identifier:echo; 149, argument_list; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, string:"Assigning qualification {} with value {} to worker {}"; 153, identifier:format; 154, argument_list; 154, 155; 154, 156; 154, 157; 155, identifier:qualification; 156, identifier:value; 157, identifier:worker; 158, if_statement; 158, 159; 158, 162; 158, 174; 159, comparison_operator:in; 159, 160; 159, 161; 160, identifier:worker; 161, identifier:workers; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:result; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:conn; 169, identifier:update_qualification_score; 170, argument_list; 170, 171; 170, 172; 170, 173; 171, identifier:qualification; 172, identifier:worker; 173, identifier:value; 174, else_clause; 174, 175; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:result; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:conn; 182, identifier:assign_qualification; 183, argument_list; 183, 184; 183, 185; 183, 186; 184, identifier:qualification; 185, identifier:worker; 186, identifier:value; 187, if_statement; 187, 188; 187, 189; 188, identifier:result; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:click; 194, identifier:echo; 195, argument_list; 195, 196; 196, identifier:result; 197, comment; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:results; 201, call; 201, 202; 201, 203; 202, identifier:get_workers_with_qualification; 203, argument_list; 203, 204; 204, identifier:qualification; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:click; 209, identifier:echo; 210, argument_list; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, string:"{} workers with qualification {}:"; 214, identifier:format; 215, argument_list; 215, 216; 215, 220; 216, call; 216, 217; 216, 218; 217, identifier:len; 218, argument_list; 218, 219; 219, identifier:results; 220, identifier:qualification; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:values; 224, list_comprehension; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:r; 227, identifier:IntegerValue; 228, for_in_clause; 228, 229; 228, 230; 229, identifier:r; 230, identifier:results; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:unique_values; 234, call; 234, 235; 234, 236; 235, identifier:list; 236, argument_list; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:set; 239, argument_list; 239, 240; 240, list_comprehension; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:r; 243, identifier:IntegerValue; 244, for_in_clause; 244, 245; 244, 246; 245, identifier:r; 246, identifier:results; 247, for_statement; 247, 248; 247, 249; 247, 250; 248, identifier:v; 249, identifier:unique_values; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:click; 255, identifier:echo; 256, argument_list; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, string:"{} with value {}"; 260, identifier:format; 261, argument_list; 261, 262; 261, 274; 262, call; 262, 263; 262, 264; 263, identifier:len; 264, argument_list; 264, 265; 265, list_comprehension; 265, 266; 265, 267; 265, 270; 266, identifier:val; 267, for_in_clause; 267, 268; 267, 269; 268, identifier:val; 269, identifier:values; 270, if_clause; 270, 271; 271, comparison_operator:==; 271, 272; 271, 273; 272, identifier:val; 273, identifier:v; 274, identifier:v | def qualify(qualification, value, worker):
"""Assign a qualification to a worker."""
# create connection to AWS
from boto.mturk.connection import MTurkConnection
config = PsiturkConfig()
config.load_config()
aws_access_key_id = config.get('AWS Access', 'aws_access_key_id')
aws_secret_access_key = config.get('AWS Access', 'aws_secret_access_key')
conn = MTurkConnection(aws_access_key_id, aws_secret_access_key)
def get_workers_with_qualification(qualification):
"""Get workers with the given qualification."""
results = []
continue_flag = True
page = 1
while(continue_flag):
new_results = conn.get_qualifications_for_qualification_type(
qualification,
page_size=100,
page_number=page)
if(len(new_results) == 0):
continue_flag = False
else:
results.extend(new_results)
page = page + 1
return results
results = get_workers_with_qualification(qualification)
workers = [x.SubjectId for x in results]
# assign the qualification
click.echo(
"Assigning qualification {} with value {} to worker {}".format(
qualification,
value,
worker))
if worker in workers:
result = conn.update_qualification_score(qualification, worker, value)
else:
result = conn.assign_qualification(qualification, worker, value)
if result:
click.echo(result)
# print out the current set of workers with the qualification
results = get_workers_with_qualification(qualification)
click.echo("{} workers with qualification {}:".format(
len(results),
qualification))
values = [r.IntegerValue for r in results]
unique_values = list(set([r.IntegerValue for r in results]))
for v in unique_values:
click.echo("{} with value {}".format(
len([val for val in values if val == v]),
v)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:setup_jobs; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:outpath; 5, identifier:options; 6, identifier:input_files; 7, block; 7, 8; 7, 10; 7, 14; 7, 18; 7, 22; 7, 23; 7, 24; 7, 25; 7, 26; 7, 27; 7, 28; 7, 149; 7, 150; 7, 161; 7, 169; 7, 177; 7, 178; 7, 182; 7, 218; 7, 219; 7, 230; 7, 231; 7, 266; 7, 267; 7, 298; 7, 313; 7, 314; 7, 351; 8, expression_statement; 8, 9; 9, string:''' This function sets up the jobs by creating the necessary input files as expected.
- outpath is where the output is to be stored.
- options is the optparse options object.
- input_files is a list of paths to input files.
'''; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:job_inputs; 13, None; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:reverse_mapping; 17, None; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:fasta_file_contents; 21, dictionary; 22, comment; 23, comment; 24, comment; 25, comment; 26, comment; 27, comment; 28, for_statement; 28, 29; 28, 30; 28, 31; 29, identifier:input_file; 30, identifier:input_files; 31, block; 31, 32; 31, 42; 32, assert_statement; 32, 33; 33, parenthesized_expression; 33, 34; 34, not_operator; 34, 35; 35, parenthesized_expression; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:fasta_file_contents; 39, identifier:get; 40, argument_list; 40, 41; 41, identifier:input_file; 42, if_statement; 42, 43; 42, 54; 42, 136; 43, call; 43, 44; 43, 45; 44, identifier:any; 45, generator_expression; 45, 46; 45, 51; 46, call; 46, 47; 46, 48; 47, identifier:fnmatch; 48, argument_list; 48, 49; 48, 50; 49, identifier:input_file; 50, identifier:x; 51, for_in_clause; 51, 52; 51, 53; 52, identifier:x; 53, identifier:pdb_file_wildcards; 54, block; 54, 55; 54, 67; 54, 87; 54, 121; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:pdb; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:PDB; 61, identifier:from_filepath; 62, argument_list; 62, 63; 62, 64; 63, identifier:input_file; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:strict; 66, True; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:pdb; 71, identifier:pdb_id; 72, subscript; 72, 73; 72, 86; 73, call; 73, 74; 73, 84; 74, attribute; 74, 75; 74, 83; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:os; 79, identifier:path; 80, identifier:basename; 81, argument_list; 81, 82; 82, identifier:input_file; 83, identifier:split; 84, argument_list; 84, 85; 85, string:'.'; 86, integer:0; 87, if_statement; 87, 88; 87, 105; 87, 106; 88, boolean_operator:and; 88, 89; 88, 97; 89, call; 89, 90; 89, 95; 90, attribute; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:pdb; 93, identifier:pdb_id; 94, identifier:startswith; 95, argument_list; 95, 96; 96, string:'pdb'; 97, comparison_operator:>=; 97, 98; 97, 104; 98, call; 98, 99; 98, 100; 99, identifier:len; 100, argument_list; 100, 101; 101, attribute; 101, 102; 101, 103; 102, identifier:pdb; 103, identifier:pdb_id; 104, integer:7; 105, comment; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:pdb; 111, identifier:pdb_id; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:pdb; 116, identifier:pdb_id; 117, identifier:replace; 118, argument_list; 118, 119; 118, 120; 119, string:'pdb'; 120, string:''; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, subscript; 123, 124; 123, 125; 124, identifier:fasta_file_contents; 125, identifier:input_file; 126, tuple; 126, 127; 126, 135; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:pdb; 130, identifier:create_fasta; 131, argument_list; 131, 132; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:prefer_seqres_order; 134, False; 135, string:'PDB'; 136, else_clause; 136, 137; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 143; 140, subscript; 140, 141; 140, 142; 141, identifier:fasta_file_contents; 142, identifier:input_file; 143, tuple; 143, 144; 143, 148; 144, call; 144, 145; 144, 146; 145, identifier:read_file; 146, argument_list; 146, 147; 147, identifier:input_file; 148, string:'FASTA'; 149, comment; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 156; 152, pattern_list; 152, 153; 152, 154; 152, 155; 153, identifier:found_sequences; 154, identifier:reverse_mapping; 155, identifier:errors; 156, call; 156, 157; 156, 158; 157, identifier:get_sequences; 158, argument_list; 158, 159; 158, 160; 159, identifier:options; 160, identifier:fasta_file_contents; 161, if_statement; 161, 162; 161, 163; 162, identifier:found_sequences; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:reformat; 167, argument_list; 167, 168; 168, identifier:found_sequences; 169, if_statement; 169, 170; 169, 171; 170, identifier:errors; 171, block; 171, 172; 172, return_statement; 172, 173; 173, expression_list; 173, 174; 173, 175; 173, 176; 174, None; 175, False; 176, identifier:errors; 177, comment; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:desired_sequences; 181, dictionary; 182, for_statement; 182, 183; 182, 186; 182, 191; 183, pattern_list; 183, 184; 183, 185; 184, identifier:key; 185, identifier:sequence; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:found_sequences; 189, identifier:iteritems; 190, argument_list; 191, block; 191, 192; 191, 199; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 198; 194, pattern_list; 194, 195; 194, 196; 194, 197; 195, identifier:pdb_id; 196, identifier:chain; 197, identifier:file_name; 198, identifier:key; 199, if_statement; 199, 200; 199, 211; 200, boolean_operator:or; 200, 201; 200, 206; 201, comparison_operator:is; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:options; 204, identifier:chain; 205, None; 206, comparison_operator:==; 206, 207; 206, 208; 207, identifier:chain; 208, attribute; 208, 209; 208, 210; 209, identifier:options; 210, identifier:chain; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 217; 214, subscript; 214, 215; 214, 216; 215, identifier:desired_sequences; 216, identifier:key; 217, identifier:sequence; 218, comment; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 224; 221, pattern_list; 221, 222; 221, 223; 222, identifier:job_inputs; 223, identifier:errors; 224, call; 224, 225; 224, 226; 225, identifier:create_inputs; 226, argument_list; 226, 227; 226, 228; 226, 229; 227, identifier:options; 228, identifier:outpath; 229, identifier:desired_sequences; 230, comment; 231, if_statement; 231, 232; 231, 233; 232, identifier:reverse_mapping; 233, block; 233, 234; 233, 246; 233, 255; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:segment_mapping_file; 237, call; 237, 238; 237, 243; 238, attribute; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:os; 241, identifier:path; 242, identifier:join; 243, argument_list; 243, 244; 243, 245; 244, identifier:outpath; 245, string:"segment_map.json"; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:colorprinter; 250, identifier:message; 251, argument_list; 251, 252; 252, binary_operator:%; 252, 253; 252, 254; 253, string:"Creating a reverse mapping file %s."; 254, identifier:segment_mapping_file; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 258; 257, identifier:write_file; 258, argument_list; 258, 259; 258, 260; 259, identifier:segment_mapping_file; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:json; 263, identifier:dumps; 264, argument_list; 264, 265; 265, identifier:reverse_mapping; 266, comment; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:post_processing_script; 270, call; 270, 271; 270, 272; 271, identifier:read_file; 272, argument_list; 272, 273; 273, call; 273, 274; 273, 279; 274, attribute; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:os; 277, identifier:path; 278, identifier:join; 279, argument_list; 279, 280; 279, 297; 280, subscript; 280, 281; 280, 296; 281, call; 281, 282; 281, 287; 282, attribute; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:os; 285, identifier:path; 286, identifier:split; 287, argument_list; 287, 288; 288, call; 288, 289; 288, 294; 289, attribute; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:os; 292, identifier:path; 293, identifier:realpath; 294, argument_list; 294, 295; 295, identifier:__file__; 296, integer:0; 297, string:'post_processing.py'; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 301; 300, identifier:write_file; 301, argument_list; 301, 302; 301, 311; 301, 312; 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:join; 308, argument_list; 308, 309; 308, 310; 309, identifier:outpath; 310, string:'post_processing.py'; 311, identifier:post_processing_script; 312, string:'w'; 313, comment; 314, if_statement; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:options; 317, identifier:secondary_structure_file; 318, block; 318, 319; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 322; 321, identifier:write_file; 322, argument_list; 322, 323; 322, 332; 322, 350; 323, call; 323, 324; 323, 329; 324, attribute; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:os; 327, identifier:path; 328, identifier:join; 329, argument_list; 329, 330; 329, 331; 330, identifier:outpath; 331, string:'ss_filter.json'; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:json; 335, identifier:dumps; 336, argument_list; 336, 337; 337, dictionary; 337, 338; 338, pair; 338, 339; 338, 340; 339, string:'secondary_structure_filter'; 340, attribute; 340, 341; 340, 349; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:SecondaryStructureDefinition; 344, identifier:from_filepath; 345, argument_list; 345, 346; 346, attribute; 346, 347; 346, 348; 347, identifier:options; 348, identifier:secondary_structure_file; 349, identifier:data; 350, string:'w'; 351, return_statement; 351, 352; 352, expression_list; 352, 353; 352, 354; 352, 357; 353, identifier:job_inputs; 354, comparison_operator:!=; 354, 355; 354, 356; 355, identifier:reverse_mapping; 356, None; 357, identifier:errors | def setup_jobs(outpath, options, input_files):
''' This function sets up the jobs by creating the necessary input files as expected.
- outpath is where the output is to be stored.
- options is the optparse options object.
- input_files is a list of paths to input files.
'''
job_inputs = None
reverse_mapping = None
fasta_file_contents = {}
# Generate FASTA files for PDB inputs
# fasta_file_contents is a mapping from a file path to a pair (FASTA contents, file type). We remember the file type
# since we offset residue IDs depending on file type i.e. for FASTA files, we treat each sequence separately and do
# not renumber the fragments in postprocessing. For PDB files, however, we need to respect the order and length of
# sequences so that we renumber the fragments appropriately in postprocessing - we assume that if a PDB file is passed in
# then all chains (protein, RNA, or DNA) will be used in a Rosetta run.
for input_file in input_files:
assert(not(fasta_file_contents.get(input_file)))
if any(fnmatch(input_file, x) for x in pdb_file_wildcards):
pdb = PDB.from_filepath(input_file, strict=True)
pdb.pdb_id = os.path.basename(input_file).split('.')[0]
if pdb.pdb_id.startswith('pdb') and len(pdb.pdb_id) >= 7:
# Hack to rename FASTA identifiers for pdb*.ent files which are present in mirrors of the PDB
pdb.pdb_id = pdb.pdb_id.replace('pdb', '')
fasta_file_contents[input_file] = (pdb.create_fasta(prefer_seqres_order = False), 'PDB')
else:
fasta_file_contents[input_file] = (read_file(input_file), 'FASTA')
# Extract sequences from the input FASTA files.
found_sequences, reverse_mapping, errors = get_sequences(options, fasta_file_contents)
if found_sequences:
reformat(found_sequences)
if errors:
return None, False, errors
# Discard sequences that are the wrong chain.
desired_sequences = {}
for key, sequence in found_sequences.iteritems():
pdb_id, chain, file_name = key
if options.chain is None or chain == options.chain:
desired_sequences[key] = sequence
# Create the input FASTA and script files.
job_inputs, errors = create_inputs(options, outpath, desired_sequences)
# Create the reverse mapping file
if reverse_mapping:
segment_mapping_file = os.path.join(outpath, "segment_map.json")
colorprinter.message("Creating a reverse mapping file %s." % segment_mapping_file)
write_file(segment_mapping_file, json.dumps(reverse_mapping))
# Create the post-processing script file
post_processing_script = read_file(os.path.join(os.path.split(os.path.realpath(__file__))[0], 'post_processing.py'))
write_file(os.path.join(outpath, 'post_processing.py'), post_processing_script, 'w')
# Create the secondary structure filter file
if options.secondary_structure_file:
write_file(os.path.join(outpath, 'ss_filter.json'), json.dumps({'secondary_structure_filter' : SecondaryStructureDefinition.from_filepath(options.secondary_structure_file).data}), 'w')
return job_inputs, reverse_mapping != None, errors |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:load_stdlib; 3, parameters; 4, block; 4, 5; 4, 7; 4, 12; 4, 49; 4, 74; 4, 96; 4, 105; 4, 192; 5, expression_statement; 5, 6; 6, string:'''Scans sys.path for standard library modules.
'''; 7, if_statement; 7, 8; 7, 9; 8, identifier:_stdlib; 9, block; 9, 10; 10, return_statement; 10, 11; 11, identifier:_stdlib; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:prefixes; 15, call; 15, 16; 15, 17; 16, identifier:tuple; 17, argument_list; 17, 18; 18, set_comprehension; 18, 19; 18, 27; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:os; 23, identifier:path; 24, identifier:abspath; 25, argument_list; 25, 26; 26, identifier:p; 27, for_in_clause; 27, 28; 27, 29; 28, identifier:p; 29, tuple; 29, 30; 29, 33; 29, 41; 30, attribute; 30, 31; 30, 32; 31, identifier:sys; 32, identifier:prefix; 33, call; 33, 34; 33, 35; 34, identifier:getattr; 35, argument_list; 35, 36; 35, 37; 35, 38; 36, identifier:sys; 37, string:'real_prefix'; 38, attribute; 38, 39; 38, 40; 39, identifier:sys; 40, identifier:prefix; 41, call; 41, 42; 41, 43; 42, identifier:getattr; 43, argument_list; 43, 44; 43, 45; 43, 46; 44, identifier:sys; 45, string:'base_prefix'; 46, attribute; 46, 47; 46, 48; 47, identifier:sys; 48, identifier:prefix; 49, for_statement; 49, 50; 49, 51; 49, 54; 50, identifier:sp; 51, attribute; 51, 52; 51, 53; 52, identifier:sys; 53, identifier:path; 54, block; 54, 55; 54, 60; 55, if_statement; 55, 56; 55, 58; 56, not_operator; 56, 57; 57, identifier:sp; 58, block; 58, 59; 59, continue_statement; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:_import_paths; 64, identifier:append; 65, argument_list; 65, 66; 66, call; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:os; 70, identifier:path; 71, identifier:abspath; 72, argument_list; 72, 73; 73, identifier:sp; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:stdpaths; 77, call; 77, 78; 77, 79; 78, identifier:tuple; 79, argument_list; 79, 80; 80, set_comprehension; 80, 81; 80, 82; 80, 85; 81, identifier:p; 82, for_in_clause; 82, 83; 82, 84; 83, identifier:p; 84, identifier:_import_paths; 85, if_clause; 85, 86; 86, boolean_operator:and; 86, 87; 86, 93; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:p; 90, identifier:startswith; 91, argument_list; 91, 92; 92, identifier:prefixes; 93, comparison_operator:not; 93, 94; 93, 95; 94, string:'site-packages'; 95, identifier:p; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:_stdlib; 100, identifier:update; 101, argument_list; 101, 102; 102, attribute; 102, 103; 102, 104; 103, identifier:sys; 104, identifier:builtin_module_names; 105, for_statement; 105, 106; 105, 107; 105, 108; 106, identifier:stdpath; 107, identifier:stdpaths; 108, block; 108, 109; 108, 121; 109, if_statement; 109, 110; 109, 119; 110, not_operator; 110, 111; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:os; 115, identifier:path; 116, identifier:isdir; 117, argument_list; 117, 118; 118, identifier:stdpath; 119, block; 119, 120; 120, continue_statement; 121, for_statement; 121, 122; 121, 123; 121, 129; 122, identifier:item; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:os; 126, identifier:listdir; 127, argument_list; 127, 128; 128, identifier:stdpath; 129, block; 129, 130; 129, 143; 129, 155; 129, 177; 130, if_statement; 130, 131; 130, 141; 131, boolean_operator:or; 131, 132; 131, 138; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:item; 135, identifier:startswith; 136, argument_list; 136, 137; 137, string:'.'; 138, comparison_operator:==; 138, 139; 138, 140; 139, identifier:item; 140, string:'site-packages'; 141, block; 141, 142; 142, continue_statement; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:p; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:os; 150, identifier:path; 151, identifier:join; 152, argument_list; 152, 153; 152, 154; 153, identifier:stdpath; 154, identifier:item; 155, if_statement; 155, 156; 155, 175; 156, boolean_operator:and; 156, 157; 156, 166; 157, not_operator; 157, 158; 158, call; 158, 159; 158, 164; 159, attribute; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:os; 162, identifier:path; 163, identifier:isdir; 164, argument_list; 164, 165; 165, identifier:p; 166, not_operator; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:item; 170, identifier:endswith; 171, argument_list; 171, 172; 172, tuple; 172, 173; 172, 174; 173, string:'.py'; 174, string:'.so'; 175, block; 175, 176; 176, continue_statement; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:_stdlib; 181, identifier:add; 182, argument_list; 182, 183; 183, subscript; 183, 184; 183, 191; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:item; 187, identifier:split; 188, argument_list; 188, 189; 188, 190; 189, string:'.'; 190, integer:1; 191, integer:0; 192, return_statement; 192, 193; 193, identifier:_stdlib | def load_stdlib():
'''Scans sys.path for standard library modules.
'''
if _stdlib:
return _stdlib
prefixes = tuple({os.path.abspath(p) for p in (
sys.prefix,
getattr(sys, 'real_prefix', sys.prefix),
getattr(sys, 'base_prefix', sys.prefix),
)})
for sp in sys.path:
if not sp:
continue
_import_paths.append(os.path.abspath(sp))
stdpaths = tuple({p for p in _import_paths
if p.startswith(prefixes) and 'site-packages' not in p})
_stdlib.update(sys.builtin_module_names)
for stdpath in stdpaths:
if not os.path.isdir(stdpath):
continue
for item in os.listdir(stdpath):
if item.startswith('.') or item == 'site-packages':
continue
p = os.path.join(stdpath, item)
if not os.path.isdir(p) and not item.endswith(('.py', '.so')):
continue
_stdlib.add(item.split('.', 1)[0])
return _stdlib |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:is_python_script; 3, parameters; 3, 4; 4, identifier:filename; 5, block; 5, 6; 5, 8; 5, 22; 5, 35; 5, 80; 6, expression_statement; 6, 7; 7, string:'''Checks a file to see if it's a python script of some sort.
'''; 8, if_statement; 8, 9; 8, 19; 9, call; 9, 10; 9, 17; 10, attribute; 10, 11; 10, 16; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:filename; 14, identifier:lower; 15, argument_list; 16, identifier:endswith; 17, argument_list; 17, 18; 18, string:'.py'; 19, block; 19, 20; 20, return_statement; 20, 21; 21, True; 22, if_statement; 22, 23; 22, 32; 23, not_operator; 23, 24; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:os; 28, identifier:path; 29, identifier:isfile; 30, argument_list; 30, 31; 31, identifier:filename; 32, block; 32, 33; 33, return_statement; 33, 34; 34, False; 35, try_statement; 35, 36; 35, 76; 36, block; 36, 37; 37, with_statement; 37, 38; 37, 48; 38, with_clause; 38, 39; 39, with_item; 39, 40; 40, as_pattern; 40, 41; 40, 46; 41, call; 41, 42; 41, 43; 42, identifier:open; 43, argument_list; 43, 44; 43, 45; 44, identifier:filename; 45, string:'rb'; 46, as_pattern_target; 46, 47; 47, identifier:fp; 48, block; 48, 49; 48, 61; 49, if_statement; 49, 50; 49, 58; 50, comparison_operator:!=; 50, 51; 50, 57; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:fp; 54, identifier:read; 55, argument_list; 55, 56; 56, integer:2; 57, string:b'#!'; 58, block; 58, 59; 59, return_statement; 59, 60; 60, False; 61, return_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:re; 65, identifier:match; 66, argument_list; 66, 67; 66, 68; 67, string:r'.*python'; 68, call; 68, 69; 68, 70; 69, identifier:str_; 70, argument_list; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:fp; 74, identifier:readline; 75, argument_list; 76, except_clause; 76, 77; 76, 78; 77, identifier:IOError; 78, block; 78, 79; 79, pass_statement; 80, return_statement; 80, 81; 81, False | def is_python_script(filename):
'''Checks a file to see if it's a python script of some sort.
'''
if filename.lower().endswith('.py'):
return True
if not os.path.isfile(filename):
return False
try:
with open(filename, 'rb') as fp:
if fp.read(2) != b'#!':
return False
return re.match(r'.*python', str_(fp.readline()))
except IOError:
pass
return False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:fit; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:dataset; 5, default_parameter; 5, 6; 5, 7; 6, identifier:alpha; 7, float:1e-8; 8, default_parameter; 8, 9; 8, 10; 9, identifier:max_iterations; 10, integer:10; 11, default_parameter; 11, 12; 11, 13; 12, identifier:save_results; 13, True; 14, default_parameter; 14, 15; 14, 16; 15, identifier:show; 16, False; 17, block; 17, 18; 17, 20; 17, 30; 17, 38; 17, 43; 17, 57; 17, 91; 17, 92; 17, 110; 17, 115; 17, 116; 17, 291; 18, expression_statement; 18, 19; 19, comment; 20, import_from_statement; 20, 21; 20, 26; 20, 28; 21, dotted_name; 21, 22; 21, 23; 21, 24; 21, 25; 22, identifier:disco; 23, identifier:worker; 24, identifier:pipeline; 25, identifier:worker; 26, dotted_name; 26, 27; 27, identifier:Worker; 28, dotted_name; 28, 29; 29, identifier:Stage; 30, import_from_statement; 30, 31; 30, 34; 30, 36; 31, dotted_name; 31, 32; 31, 33; 32, identifier:disco; 33, identifier:core; 34, dotted_name; 34, 35; 35, identifier:Job; 36, dotted_name; 36, 37; 37, identifier:result_iterator; 38, import_statement; 38, 39; 39, aliased_import; 39, 40; 39, 42; 40, dotted_name; 40, 41; 41, identifier:numpy; 42, identifier:np; 43, if_statement; 43, 44; 43, 51; 44, comparison_operator:==; 44, 45; 44, 50; 45, subscript; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:dataset; 48, identifier:params; 49, string:"y_map"; 50, list:[]; 51, block; 51, 52; 52, raise_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:Exception; 55, argument_list; 55, 56; 56, string:"Logistic regression requires a target label mapping parameter."; 57, try_statement; 57, 58; 57, 83; 58, block; 58, 59; 58, 66; 58, 73; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:alpha; 62, call; 62, 63; 62, 64; 63, identifier:float; 64, argument_list; 64, 65; 65, identifier:alpha; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:max_iterations; 69, call; 69, 70; 69, 71; 70, identifier:int; 71, argument_list; 71, 72; 72, identifier:max_iterations; 73, if_statement; 73, 74; 73, 77; 74, comparison_operator:<; 74, 75; 74, 76; 75, identifier:max_iterations; 76, integer:1; 77, block; 77, 78; 78, raise_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:Exception; 81, argument_list; 81, 82; 82, string:"Parameter max_iterations should be greater than 0."; 83, except_clause; 83, 84; 83, 85; 84, identifier:ValueError; 85, block; 85, 86; 86, raise_statement; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:Exception; 89, argument_list; 89, 90; 90, string:"Parameters should be numerical."; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:thetas; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:np; 98, identifier:zeros; 99, argument_list; 99, 100; 100, binary_operator:+; 100, 101; 100, 109; 101, call; 101, 102; 101, 103; 102, identifier:len; 103, argument_list; 103, 104; 104, subscript; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:dataset; 107, identifier:params; 108, string:"X_indices"; 109, integer:1; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:J; 113, list:[0]; 113, 114; 114, integer:0; 115, comment; 116, for_statement; 116, 117; 116, 118; 116, 122; 117, identifier:i; 118, call; 118, 119; 118, 120; 119, identifier:range; 120, argument_list; 120, 121; 121, identifier:max_iterations; 122, block; 122, 123; 122, 137; 122, 138; 122, 178; 122, 186; 122, 187; 122, 195; 122, 196; 122, 217; 122, 228; 122, 258; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:job; 126, call; 126, 127; 126, 128; 127, identifier:Job; 128, argument_list; 128, 129; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:worker; 131, call; 131, 132; 131, 133; 132, identifier:Worker; 133, argument_list; 133, 134; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:save_results; 136, identifier:save_results; 137, comment; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:job; 142, identifier:pipeline; 143, list:[
("split", Stage("map", input_chain=dataset.params["input_chain"], init=simple_init, process=map_fit)),
('group_all', Stage("reduce", init=simple_init, process=reduce_fit, combine=True))]; 143, 144; 143, 163; 144, tuple; 144, 145; 144, 146; 145, string:"split"; 146, call; 146, 147; 146, 148; 147, identifier:Stage; 148, argument_list; 148, 149; 148, 150; 148, 157; 148, 160; 149, string:"map"; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:input_chain; 152, subscript; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:dataset; 155, identifier:params; 156, string:"input_chain"; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:init; 159, identifier:simple_init; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:process; 162, identifier:map_fit; 163, tuple; 163, 164; 163, 165; 164, string:'group_all'; 165, call; 165, 166; 165, 167; 166, identifier:Stage; 167, argument_list; 167, 168; 167, 169; 167, 172; 167, 175; 168, string:"reduce"; 169, keyword_argument; 169, 170; 169, 171; 170, identifier:init; 171, identifier:simple_init; 172, keyword_argument; 172, 173; 172, 174; 173, identifier:process; 174, identifier:reduce_fit; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:combine; 177, True; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:job; 182, identifier:params; 183, attribute; 183, 184; 183, 185; 184, identifier:dataset; 185, identifier:params; 186, comment; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 194; 189, subscript; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:job; 192, identifier:params; 193, string:"thetas"; 194, identifier:thetas; 195, comment; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:job; 200, identifier:run; 201, argument_list; 201, 202; 201, 210; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:name; 204, binary_operator:%; 204, 205; 204, 206; 205, string:"logreg_fit_iter_%d"; 206, parenthesized_expression; 206, 207; 207, binary_operator:+; 207, 208; 207, 209; 208, identifier:i; 209, integer:1; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:input; 212, subscript; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:dataset; 215, identifier:params; 216, string:"data_tag"; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:fitmodel_url; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:job; 223, identifier:wait; 224, argument_list; 224, 225; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:show; 227, identifier:show; 228, for_statement; 228, 229; 228, 232; 228, 236; 229, pattern_list; 229, 230; 229, 231; 230, identifier:k; 231, identifier:v; 232, call; 232, 233; 232, 234; 233, identifier:result_iterator; 234, argument_list; 234, 235; 235, identifier:fitmodel_url; 236, block; 236, 237; 237, if_statement; 237, 238; 237, 241; 237, 242; 237, 251; 238, comparison_operator:==; 238, 239; 238, 240; 239, identifier:k; 240, string:"J"; 241, comment; 242, block; 242, 243; 242, 250; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:J; 247, identifier:append; 248, argument_list; 248, 249; 249, identifier:v; 250, comment; 251, else_clause; 251, 252; 252, block; 252, 253; 252, 257; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:thetas; 256, identifier:v; 257, comment; 258, if_statement; 258, 259; 258, 275; 258, 276; 259, comparison_operator:<; 259, 260; 259, 274; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:np; 263, identifier:abs; 264, argument_list; 264, 265; 265, binary_operator:-; 265, 266; 265, 270; 266, subscript; 266, 267; 266, 268; 267, identifier:J; 268, unary_operator:-; 268, 269; 269, integer:2; 270, subscript; 270, 271; 270, 272; 271, identifier:J; 272, unary_operator:-; 272, 273; 273, integer:1; 274, identifier:alpha; 275, comment; 276, block; 276, 277; 276, 290; 277, if_statement; 277, 278; 277, 279; 278, identifier:show; 279, block; 279, 280; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 283; 282, identifier:print; 283, argument_list; 283, 284; 284, binary_operator:%; 284, 285; 284, 286; 285, string:"Converged at iteration %d"; 286, parenthesized_expression; 286, 287; 287, binary_operator:+; 287, 288; 287, 289; 288, identifier:i; 289, integer:1; 290, break_statement; 291, return_statement; 291, 292; 292, dictionary; 292, 293; 293, pair; 293, 294; 293, 295; 294, string:"logreg_fitmodel"; 295, identifier:fitmodel_url | def fit(dataset, alpha=1e-8, max_iterations=10, save_results=True, show=False):
"""
Function starts a job for calculation of theta parameters
Parameters
----------
input - dataset object with input urls and other parameters
alpha - convergence value
max_iterations - define maximum number of iterations
save_results - save results to ddfs
show - show info about job execution
Returns
-------
Urls of fit model results on ddfs
"""
from disco.worker.pipeline.worker import Worker, Stage
from disco.core import Job, result_iterator
import numpy as np
if dataset.params["y_map"] == []:
raise Exception("Logistic regression requires a target label mapping parameter.")
try:
alpha = float(alpha)
max_iterations = int(max_iterations)
if max_iterations < 1:
raise Exception("Parameter max_iterations should be greater than 0.")
except ValueError:
raise Exception("Parameters should be numerical.")
# initialize thetas to 0 and add intercept term
thetas = np.zeros(len(dataset.params["X_indices"]) + 1)
J = [0] # J cost function values for every iteration
for i in range(max_iterations):
job = Job(worker=Worker(save_results=save_results))
# job parallelizes mappers and joins them with one reducer
job.pipeline = [
("split", Stage("map", input_chain=dataset.params["input_chain"], init=simple_init, process=map_fit)),
('group_all', Stage("reduce", init=simple_init, process=reduce_fit, combine=True))]
job.params = dataset.params # job parameters (dataset object)
job.params["thetas"] = thetas # every iteration set new thetas
job.run(name="logreg_fit_iter_%d" % (i + 1), input=dataset.params["data_tag"])
fitmodel_url = job.wait(show=show)
for k, v in result_iterator(fitmodel_url):
if k == "J": #
J.append(v) # save value of J cost function
else:
thetas = v # save new thetas
if np.abs(J[-2] - J[-1]) < alpha: # check for convergence
if show:
print("Converged at iteration %d" % (i + 1))
break
return {"logreg_fitmodel": fitmodel_url} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:draw_image; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:image; 6, default_parameter; 6, 7; 6, 8; 7, identifier:xmin; 8, integer:0; 9, default_parameter; 9, 10; 9, 11; 10, identifier:ymin; 11, integer:0; 12, default_parameter; 12, 13; 12, 14; 13, identifier:xmax; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:ymax; 17, None; 18, block; 18, 19; 18, 21; 18, 36; 18, 51; 18, 75; 18, 76; 18, 96; 18, 116; 18, 136; 18, 156; 18, 164; 19, expression_statement; 19, 20; 20, comment; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:xmax; 24, None; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:xmax; 29, binary_operator:+; 29, 30; 29, 31; 30, identifier:xmin; 31, subscript; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:image; 34, identifier:size; 35, integer:0; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:ymax; 39, None; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:ymax; 44, binary_operator:+; 44, 45; 44, 46; 45, identifier:ymin; 46, subscript; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:image; 49, identifier:size; 50, integer:1; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:bitmap_list; 57, identifier:append; 58, argument_list; 58, 59; 59, dictionary; 59, 60; 59, 63; 59, 66; 59, 69; 59, 72; 60, pair; 60, 61; 60, 62; 61, string:'image'; 62, identifier:image; 63, pair; 63, 64; 63, 65; 64, string:'xmin'; 65, identifier:xmin; 66, pair; 66, 67; 66, 68; 67, string:'xmax'; 68, identifier:xmax; 69, pair; 69, 70; 69, 71; 70, string:'ymin'; 71, identifier:ymin; 72, pair; 72, 73; 72, 74; 73, string:'ymax'; 74, identifier:ymax; 75, comment; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:xmin; 79, call; 79, 80; 79, 81; 80, identifier:min; 81, generator_expression; 81, 82; 81, 83; 81, 92; 82, identifier:x; 83, for_in_clause; 83, 84; 83, 85; 84, identifier:x; 85, tuple; 85, 86; 85, 87; 86, identifier:xmin; 87, subscript; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:limits; 91, string:'xmin'; 92, if_clause; 92, 93; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:x; 95, None; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:ymin; 99, call; 99, 100; 99, 101; 100, identifier:min; 101, generator_expression; 101, 102; 101, 103; 101, 112; 102, identifier:y; 103, for_in_clause; 103, 104; 103, 105; 104, identifier:y; 105, tuple; 105, 106; 105, 107; 106, identifier:ymin; 107, subscript; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:limits; 111, string:'ymin'; 112, if_clause; 112, 113; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:y; 115, None; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:xmax; 119, call; 119, 120; 119, 121; 120, identifier:max; 121, generator_expression; 121, 122; 121, 123; 121, 132; 122, identifier:x; 123, for_in_clause; 123, 124; 123, 125; 124, identifier:x; 125, tuple; 125, 126; 125, 127; 126, identifier:xmax; 127, subscript; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:limits; 131, string:'xmax'; 132, if_clause; 132, 133; 133, comparison_operator:is; 133, 134; 133, 135; 134, identifier:x; 135, None; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:ymax; 139, call; 139, 140; 139, 141; 140, identifier:max; 141, generator_expression; 141, 142; 141, 143; 141, 152; 142, identifier:y; 143, for_in_clause; 143, 144; 143, 145; 144, identifier:y; 145, tuple; 145, 146; 145, 147; 146, identifier:ymax; 147, subscript; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:limits; 151, string:'ymax'; 152, if_clause; 152, 153; 153, comparison_operator:is; 153, 154; 153, 155; 154, identifier:y; 155, None; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:set_xlimits; 161, argument_list; 161, 162; 161, 163; 162, identifier:xmin; 163, identifier:xmax; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:set_ylimits; 169, argument_list; 169, 170; 169, 171; 170, identifier:ymin; 171, identifier:ymax | def draw_image(self, image, xmin=0, ymin=0, xmax=None, ymax=None):
"""Draw an image.
Do not forget to use :meth:`set_axis_equal` to preserve the
aspect ratio of the image, or change the aspect ratio of the
plot to the aspect ratio of the image.
:param image: Pillow Image object.
:param xmin,ymin,xmax,ymax: the x, y image bounds.
Example::
>>> from PIL import Image
>>> image = Image.open('background.png')
>>> plot = artist.Plot()
>>> plot.set_axis_equal()
>>> plot.draw_image(image)
"""
if xmax is None:
xmax = xmin + image.size[0]
if ymax is None:
ymax = ymin + image.size[1]
self.bitmap_list.append({'image': image,
'xmin': xmin,
'xmax': xmax,
'ymin': ymin,
'ymax': ymax})
# Set limits unless lower/higher limits are already set.
xmin = min(x for x in (xmin, self.limits['xmin'])
if x is not None)
ymin = min(y for y in (ymin, self.limits['ymin'])
if y is not None)
xmax = max(x for x in (xmax, self.limits['xmax'])
if x is not None)
ymax = max(y for y in (ymax, self.limits['ymax'])
if y is not None)
self.set_xlimits(xmin, xmax)
self.set_ylimits(ymin, ymax) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_prepare_data; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 24; 5, 30; 5, 154; 5, 160; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, pattern_list; 10, 11; 10, 12; 11, identifier:xmin; 12, identifier:xmax; 13, expression_list; 13, 14; 13, 19; 14, subscript; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:limits; 18, string:'xmin'; 19, subscript; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:limits; 23, string:'xmax'; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:prepared_plot_series_list; 29, list:[]; 30, for_statement; 30, 31; 30, 32; 30, 35; 31, identifier:series; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:plot_series_list; 35, block; 35, 36; 35, 44; 35, 50; 35, 62; 35, 63; 35, 145; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:prepared_series; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:series; 42, identifier:copy; 43, argument_list; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:data; 47, subscript; 47, 48; 47, 49; 48, identifier:prepared_series; 49, string:'data'; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 57; 52, pattern_list; 52, 53; 52, 54; 52, 55; 52, 56; 53, identifier:x; 54, identifier:_; 55, identifier:_; 56, identifier:_; 57, call; 57, 58; 57, 59; 58, identifier:zip; 59, argument_list; 59, 60; 60, list_splat; 60, 61; 61, identifier:data; 62, comment; 63, if_statement; 63, 64; 63, 73; 64, comparison_operator:==; 64, 65; 64, 69; 65, call; 65, 66; 65, 67; 66, identifier:sorted; 67, argument_list; 67, 68; 68, identifier:x; 69, call; 69, 70; 69, 71; 70, identifier:list; 71, argument_list; 71, 72; 72, identifier:x; 73, block; 73, 74; 73, 83; 73, 112; 73, 134; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:x; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:np; 80, identifier:array; 81, argument_list; 81, 82; 82, identifier:x; 83, if_statement; 83, 84; 83, 87; 83, 106; 84, comparison_operator:is; 84, 85; 84, 86; 85, identifier:xmin; 86, None; 87, block; 87, 88; 87, 97; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:min_idx; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:x; 94, identifier:searchsorted; 95, argument_list; 95, 96; 96, identifier:xmin; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:>; 98, 99; 98, 100; 99, identifier:min_idx; 100, integer:0; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, augmented_assignment:-=; 103, 104; 103, 105; 104, identifier:min_idx; 105, integer:1; 106, else_clause; 106, 107; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:min_idx; 111, None; 112, if_statement; 112, 113; 112, 116; 112, 128; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:xmax; 115, None; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:max_idx; 120, binary_operator:+; 120, 121; 120, 127; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:x; 124, identifier:searchsorted; 125, argument_list; 125, 126; 126, identifier:xmax; 127, integer:1; 128, else_clause; 128, 129; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:max_idx; 133, None; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:prepared_series; 138, string:'data'; 139, subscript; 139, 140; 139, 141; 140, identifier:data; 141, slice; 141, 142; 141, 143; 141, 144; 142, identifier:min_idx; 143, colon; 144, identifier:max_idx; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:prepared_plot_series_list; 151, identifier:append; 152, argument_list; 152, 153; 153, identifier:prepared_series; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:prepared_shaded_regions_list; 159, list:[]; 160, for_statement; 160, 161; 160, 162; 160, 165; 161, identifier:series; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:shaded_regions_list; 165, block; 165, 166; 165, 174; 165, 180; 165, 191; 165, 192; 165, 274; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:prepared_series; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:series; 172, identifier:copy; 173, argument_list; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:data; 177, subscript; 177, 178; 177, 179; 178, identifier:prepared_series; 179, string:'data'; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 186; 182, pattern_list; 182, 183; 182, 184; 182, 185; 183, identifier:x; 184, identifier:_; 185, identifier:_; 186, call; 186, 187; 186, 188; 187, identifier:zip; 188, argument_list; 188, 189; 189, list_splat; 189, 190; 190, identifier:data; 191, comment; 192, if_statement; 192, 193; 192, 202; 193, comparison_operator:==; 193, 194; 193, 198; 194, call; 194, 195; 194, 196; 195, identifier:sorted; 196, argument_list; 196, 197; 197, identifier:x; 198, call; 198, 199; 198, 200; 199, identifier:list; 200, argument_list; 200, 201; 201, identifier:x; 202, block; 202, 203; 202, 212; 202, 241; 202, 263; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:x; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:np; 209, identifier:array; 210, argument_list; 210, 211; 211, identifier:x; 212, if_statement; 212, 213; 212, 216; 212, 235; 213, comparison_operator:is; 213, 214; 213, 215; 214, identifier:xmin; 215, None; 216, block; 216, 217; 216, 226; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:min_idx; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:x; 223, identifier:searchsorted; 224, argument_list; 224, 225; 225, identifier:xmin; 226, if_statement; 226, 227; 226, 230; 227, comparison_operator:>; 227, 228; 227, 229; 228, identifier:min_idx; 229, integer:0; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, augmented_assignment:-=; 232, 233; 232, 234; 233, identifier:min_idx; 234, integer:1; 235, else_clause; 235, 236; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:min_idx; 240, None; 241, if_statement; 241, 242; 241, 245; 241, 257; 242, comparison_operator:is; 242, 243; 242, 244; 243, identifier:xmax; 244, None; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:max_idx; 249, binary_operator:+; 249, 250; 249, 256; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:x; 253, identifier:searchsorted; 254, argument_list; 254, 255; 255, identifier:xmax; 256, integer:1; 257, else_clause; 257, 258; 258, block; 258, 259; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:max_idx; 262, None; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 268; 265, subscript; 265, 266; 265, 267; 266, identifier:prepared_series; 267, string:'data'; 268, subscript; 268, 269; 268, 270; 269, identifier:data; 270, slice; 270, 271; 270, 272; 270, 273; 271, identifier:min_idx; 272, colon; 273, identifier:max_idx; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 281; 276, attribute; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:self; 279, identifier:prepared_shaded_regions_list; 280, identifier:append; 281, argument_list; 281, 282; 282, identifier:prepared_series | def _prepare_data(self):
"""Prepare data before rendering
When plotting very large datasets, we don't want to include data points
which are outside the x-axis limits. LaTeX is very slow and consumes a
lot of memory otherwise. Limiting the data points is only (easily)
possible when the data are sorted.
"""
xmin, xmax = self.limits['xmin'], self.limits['xmax']
self.prepared_plot_series_list = []
for series in self.plot_series_list:
prepared_series = series.copy()
data = prepared_series['data']
x, _, _, _ = zip(*data)
# only limit data when the data is sorted
if sorted(x) == list(x):
x = np.array(x)
if xmin is not None:
min_idx = x.searchsorted(xmin)
if min_idx > 0:
min_idx -= 1
else:
min_idx = None
if xmax is not None:
max_idx = x.searchsorted(xmax) + 1
else:
max_idx = None
prepared_series['data'] = data[min_idx:max_idx]
self.prepared_plot_series_list.append(prepared_series)
self.prepared_shaded_regions_list = []
for series in self.shaded_regions_list:
prepared_series = series.copy()
data = prepared_series['data']
x, _, _ = zip(*data)
# only limit data when the data is sorted
if sorted(x) == list(x):
x = np.array(x)
if xmin is not None:
min_idx = x.searchsorted(xmin)
if min_idx > 0:
min_idx -= 1
else:
min_idx = None
if xmax is not None:
max_idx = x.searchsorted(xmax) + 1
else:
max_idx = None
prepared_series['data'] = data[min_idx:max_idx]
self.prepared_shaded_regions_list.append(prepared_series) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_departures; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:stop_id; 6, identifier:route; 7, identifier:destination; 8, identifier:api_key; 9, block; 9, 10; 9, 12; 9, 18; 9, 24; 9, 30; 9, 36; 9, 37; 9, 52; 9, 60; 9, 70; 9, 71; 9, 101; 9, 102; 9, 120; 9, 121; 9, 129; 9, 130; 9, 150; 9, 151; 9, 155; 9, 159; 9, 335; 9, 391; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:stop_id; 17, identifier:stop_id; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:route; 23, identifier:route; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:destination; 29, identifier:destination; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:api_key; 35, identifier:api_key; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:url; 40, binary_operator:+; 40, 41; 40, 50; 40, 51; 41, binary_operator:+; 41, 42; 41, 46; 41, 47; 42, concatenated_string; 42, 43; 42, 44; 42, 45; 43, string:'https://api.transport.nsw.gov.au/v1/tp/departure_mon?'; 44, string:'outputFormat=rapidJSON&coordOutputFormat=EPSG%3A4326&'; 45, string:'mode=direct&type_dm=stop&name_dm='; 46, line_continuation:\; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:stop_id; 50, line_continuation:\; 51, string:'&departureMonitorMacro=true&TfNSWDM=true&version=10.2.1.42'; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:auth; 55, binary_operator:+; 55, 56; 55, 57; 56, string:'apikey '; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:api_key; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:header; 63, dictionary; 63, 64; 63, 67; 64, pair; 64, 65; 64, 66; 65, string:'Accept'; 66, string:'application/json'; 67, pair; 67, 68; 67, 69; 68, string:'Authorization'; 69, identifier:auth; 70, comment; 71, try_statement; 71, 72; 71, 88; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:response; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:requests; 79, identifier:get; 80, argument_list; 80, 81; 80, 82; 80, 85; 81, identifier:url; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:headers; 84, identifier:header; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:timeout; 87, integer:10; 88, except_clause; 88, 89; 89, block; 89, 90; 89, 97; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:logger; 94, identifier:warning; 95, argument_list; 95, 96; 96, string:"Network or Timeout error"; 97, return_statement; 97, 98; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:info; 101, comment; 102, if_statement; 102, 103; 102, 108; 103, comparison_operator:!=; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:response; 106, identifier:status_code; 107, integer:200; 108, block; 108, 109; 108, 116; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:logger; 113, identifier:warning; 114, argument_list; 114, 115; 115, string:"Error with the request sent; check api key"; 116, return_statement; 116, 117; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:info; 120, comment; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:result; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:response; 127, identifier:json; 128, argument_list; 129, comment; 130, try_statement; 130, 131; 130, 136; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, subscript; 133, 134; 133, 135; 134, identifier:result; 135, string:'stopEvents'; 136, except_clause; 136, 137; 136, 138; 137, identifier:KeyError; 138, block; 138, 139; 138, 146; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:logger; 143, identifier:warning; 144, argument_list; 144, 145; 145, string:"No stop events for this query"; 146, return_statement; 146, 147; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:info; 150, comment; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:maxresults; 154, integer:1; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:monitor; 158, list:[]; 159, if_statement; 159, 160; 159, 165; 159, 231; 159, 302; 160, comparison_operator:!=; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:destination; 164, string:''; 165, block; 165, 166; 166, for_statement; 166, 167; 166, 168; 166, 177; 167, identifier:i; 168, call; 168, 169; 168, 170; 169, identifier:range; 170, argument_list; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:len; 173, argument_list; 173, 174; 174, subscript; 174, 175; 174, 176; 175, identifier:result; 176, string:'stopEvents'; 177, block; 177, 178; 177, 192; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:destination; 181, subscript; 181, 182; 181, 191; 182, subscript; 182, 183; 182, 190; 183, subscript; 183, 184; 183, 189; 184, subscript; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:result; 187, string:'stopEvents'; 188, identifier:i; 189, string:'transportation'; 190, string:'destination'; 191, string:'name'; 192, if_statement; 192, 193; 192, 198; 193, comparison_operator:==; 193, 194; 193, 195; 194, identifier:destination; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:destination; 198, block; 198, 199; 198, 209; 198, 221; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:event; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:parseEvent; 206, argument_list; 206, 207; 206, 208; 207, identifier:result; 208, identifier:i; 209, if_statement; 209, 210; 209, 213; 210, comparison_operator:!=; 210, 211; 210, 212; 211, identifier:event; 212, None; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:monitor; 218, identifier:append; 219, argument_list; 219, 220; 220, identifier:event; 221, if_statement; 221, 222; 221, 228; 221, 229; 222, comparison_operator:>=; 222, 223; 222, 227; 223, call; 223, 224; 223, 225; 224, identifier:len; 225, argument_list; 225, 226; 226, identifier:monitor; 227, identifier:maxresults; 228, comment; 229, block; 229, 230; 230, break_statement; 231, elif_clause; 231, 232; 231, 237; 231, 238; 232, comparison_operator:!=; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:route; 236, string:''; 237, comment; 238, block; 238, 239; 239, for_statement; 239, 240; 239, 241; 239, 250; 240, identifier:i; 241, call; 241, 242; 241, 243; 242, identifier:range; 243, argument_list; 243, 244; 244, call; 244, 245; 244, 246; 245, identifier:len; 246, argument_list; 246, 247; 247, subscript; 247, 248; 247, 249; 248, identifier:result; 249, string:'stopEvents'; 250, block; 250, 251; 250, 263; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:number; 254, subscript; 254, 255; 254, 262; 255, subscript; 255, 256; 255, 261; 256, subscript; 256, 257; 256, 260; 257, subscript; 257, 258; 257, 259; 258, identifier:result; 259, string:'stopEvents'; 260, identifier:i; 261, string:'transportation'; 262, string:'number'; 263, if_statement; 263, 264; 263, 269; 264, comparison_operator:==; 264, 265; 264, 266; 265, identifier:number; 266, attribute; 266, 267; 266, 268; 267, identifier:self; 268, identifier:route; 269, block; 269, 270; 269, 280; 269, 292; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:event; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:self; 276, identifier:parseEvent; 277, argument_list; 277, 278; 277, 279; 278, identifier:result; 279, identifier:i; 280, if_statement; 280, 281; 280, 284; 281, comparison_operator:!=; 281, 282; 281, 283; 282, identifier:event; 283, None; 284, block; 284, 285; 285, expression_statement; 285, 286; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:monitor; 289, identifier:append; 290, argument_list; 290, 291; 291, identifier:event; 292, if_statement; 292, 293; 292, 299; 292, 300; 293, comparison_operator:>=; 293, 294; 293, 298; 294, call; 294, 295; 294, 296; 295, identifier:len; 296, argument_list; 296, 297; 297, identifier:monitor; 298, identifier:maxresults; 299, comment; 300, block; 300, 301; 301, break_statement; 302, else_clause; 302, 303; 302, 304; 303, comment; 304, block; 304, 305; 305, for_statement; 305, 306; 305, 307; 305, 312; 306, identifier:i; 307, call; 307, 308; 307, 309; 308, identifier:range; 309, argument_list; 309, 310; 309, 311; 310, integer:0; 311, identifier:maxresults; 312, block; 312, 313; 312, 323; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:event; 316, call; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:self; 319, identifier:parseEvent; 320, argument_list; 320, 321; 320, 322; 321, identifier:result; 322, identifier:i; 323, if_statement; 323, 324; 323, 327; 324, comparison_operator:!=; 324, 325; 324, 326; 325, identifier:event; 326, None; 327, block; 327, 328; 328, expression_statement; 328, 329; 329, call; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:monitor; 332, identifier:append; 333, argument_list; 333, 334; 334, identifier:event; 335, if_statement; 335, 336; 335, 337; 336, identifier:monitor; 337, block; 337, 338; 338, expression_statement; 338, 339; 339, assignment; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:self; 342, identifier:info; 343, dictionary; 343, 344; 343, 349; 343, 356; 343, 363; 343, 370; 343, 377; 343, 384; 344, pair; 344, 345; 344, 346; 345, identifier:ATTR_STOP_ID; 346, attribute; 346, 347; 346, 348; 347, identifier:self; 348, identifier:stop_id; 349, pair; 349, 350; 349, 351; 350, identifier:ATTR_ROUTE; 351, subscript; 351, 352; 351, 355; 352, subscript; 352, 353; 352, 354; 353, identifier:monitor; 354, integer:0; 355, integer:0; 356, pair; 356, 357; 356, 358; 357, identifier:ATTR_DUE_IN; 358, subscript; 358, 359; 358, 362; 359, subscript; 359, 360; 359, 361; 360, identifier:monitor; 361, integer:0; 362, integer:1; 363, pair; 363, 364; 363, 365; 364, identifier:ATTR_DELAY; 365, subscript; 365, 366; 365, 369; 366, subscript; 366, 367; 366, 368; 367, identifier:monitor; 368, integer:0; 369, integer:2; 370, pair; 370, 371; 370, 372; 371, identifier:ATTR_REALTIME; 372, subscript; 372, 373; 372, 376; 373, subscript; 373, 374; 373, 375; 374, identifier:monitor; 375, integer:0; 376, integer:5; 377, pair; 377, 378; 377, 379; 378, identifier:ATTR_DESTINATION; 379, subscript; 379, 380; 379, 383; 380, subscript; 380, 381; 380, 382; 381, identifier:monitor; 382, integer:0; 383, integer:6; 384, pair; 384, 385; 384, 386; 385, identifier:ATTR_MODE; 386, subscript; 386, 387; 386, 390; 387, subscript; 387, 388; 387, 389; 388, identifier:monitor; 389, integer:0; 390, integer:7; 391, return_statement; 391, 392; 392, attribute; 392, 393; 392, 394; 393, identifier:self; 394, identifier:info | def get_departures(self, stop_id, route, destination, api_key):
"""Get the latest data from Transport NSW."""
self.stop_id = stop_id
self.route = route
self.destination = destination
self.api_key = api_key
# Build the URL including the STOP_ID and the API key
url = \
'https://api.transport.nsw.gov.au/v1/tp/departure_mon?' \
'outputFormat=rapidJSON&coordOutputFormat=EPSG%3A4326&' \
'mode=direct&type_dm=stop&name_dm=' \
+ self.stop_id \
+ '&departureMonitorMacro=true&TfNSWDM=true&version=10.2.1.42'
auth = 'apikey ' + self.api_key
header = {'Accept': 'application/json', 'Authorization': auth}
# Send query or return error
try:
response = requests.get(url, headers=header, timeout=10)
except:
logger.warning("Network or Timeout error")
return self.info
# If there is no valid request
if response.status_code != 200:
logger.warning("Error with the request sent; check api key")
return self.info
# Parse the result as a JSON object
result = response.json()
# If there is no stop events for the query
try:
result['stopEvents']
except KeyError:
logger.warning("No stop events for this query")
return self.info
# Set variables
maxresults = 1
monitor = []
if self.destination != '':
for i in range(len(result['stopEvents'])):
destination = result['stopEvents'][i]['transportation']['destination']['name']
if destination == self.destination:
event = self.parseEvent(result, i)
if event != None:
monitor.append(event)
if len(monitor) >= maxresults:
# We found enough results, lets stop
break
elif self.route != '':
# Find the next stop events for a specific route
for i in range(len(result['stopEvents'])):
number = result['stopEvents'][i]['transportation']['number']
if number == self.route:
event = self.parseEvent(result, i)
if event != None:
monitor.append(event)
if len(monitor) >= maxresults:
# We found enough results, lets stop
break
else:
# No route defined, find any route leaving next
for i in range(0, maxresults):
event = self.parseEvent(result, i)
if event != None:
monitor.append(event)
if monitor:
self.info = {
ATTR_STOP_ID: self.stop_id,
ATTR_ROUTE: monitor[0][0],
ATTR_DUE_IN: monitor[0][1],
ATTR_DELAY: monitor[0][2],
ATTR_REALTIME: monitor[0][5],
ATTR_DESTINATION: monitor[0][6],
ATTR_MODE: monitor[0][7]
}
return self.info |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:pw; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 4, identifier:ctx; 5, identifier:key_pattern; 6, identifier:user_pattern; 7, identifier:mode; 8, identifier:strict_flag; 9, identifier:user_flag; 10, identifier:file; 11, identifier:edit_subcommand; 12, identifier:gen_subcommand; 13, block; 13, 14; 13, 16; 13, 17; 13, 36; 13, 46; 13, 47; 13, 77; 13, 78; 13, 108; 13, 109; 13, 118; 13, 119; 13, 135; 13, 136; 13, 146; 13, 153; 13, 154; 13, 181; 13, 182; 13, 208; 13, 209; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, function_definition; 17, 18; 17, 19; 17, 22; 18, function_name:handle_sigint; 19, parameters; 19, 20; 20, list_splat_pattern; 20, 21; 21, identifier:_; 22, block; 22, 23; 22, 29; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:click; 27, identifier:echo; 28, argument_list; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:ctx; 33, identifier:exit; 34, argument_list; 34, 35; 35, integer:1; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:signal; 40, identifier:signal; 41, argument_list; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:signal; 44, identifier:SIGINT; 45, identifier:handle_sigint; 46, comment; 47, if_statement; 47, 48; 47, 49; 47, 67; 48, identifier:gen_subcommand; 49, block; 49, 50; 49, 60; 49, 66; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:length; 53, conditional_expression:if; 53, 54; 53, 58; 53, 59; 54, call; 54, 55; 54, 56; 55, identifier:int; 56, argument_list; 56, 57; 57, identifier:key_pattern; 58, identifier:key_pattern; 59, None; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:generate_password; 63, argument_list; 63, 64; 63, 65; 64, identifier:mode; 65, identifier:length; 66, return_statement; 67, elif_clause; 67, 68; 67, 69; 68, identifier:edit_subcommand; 69, block; 69, 70; 69, 76; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:launch_editor; 73, argument_list; 73, 74; 73, 75; 74, identifier:ctx; 75, identifier:file; 76, return_statement; 77, comment; 78, if_statement; 78, 79; 78, 88; 79, not_operator; 79, 80; 80, call; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:os; 84, identifier:path; 85, identifier:exists; 86, argument_list; 86, 87; 87, identifier:file; 88, block; 88, 89; 88, 101; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:click; 93, identifier:echo; 94, argument_list; 94, 95; 94, 98; 95, binary_operator:%; 95, 96; 95, 97; 96, string:"error: password store not found at '%s'"; 97, identifier:file; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:err; 100, True; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:ctx; 105, identifier:exit; 106, argument_list; 106, 107; 107, integer:1; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:store; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:Store; 115, identifier:load; 116, argument_list; 116, 117; 117, identifier:file; 118, comment; 119, if_statement; 119, 120; 119, 122; 120, not_operator; 120, 121; 121, identifier:user_pattern; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 129; 125, pattern_list; 125, 126; 125, 127; 125, 128; 126, identifier:user_pattern; 127, identifier:_; 128, identifier:key_pattern; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:key_pattern; 132, identifier:rpartition; 133, argument_list; 133, 134; 134, string:"@"; 135, comment; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:results; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:store; 142, identifier:search; 143, argument_list; 143, 144; 143, 145; 144, identifier:key_pattern; 145, identifier:user_pattern; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:results; 149, call; 149, 150; 149, 151; 150, identifier:list; 151, argument_list; 151, 152; 152, identifier:results; 153, comment; 154, if_statement; 154, 155; 154, 163; 155, boolean_operator:and; 155, 156; 155, 157; 156, identifier:strict_flag; 157, comparison_operator:!=; 157, 158; 157, 162; 158, call; 158, 159; 158, 160; 159, identifier:len; 160, argument_list; 160, 161; 161, identifier:results; 162, integer:1; 163, block; 163, 164; 163, 174; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:click; 168, identifier:echo; 169, argument_list; 169, 170; 169, 171; 170, string:"error: multiple or no records found (but using --strict flag)"; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:err; 173, True; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:ctx; 178, identifier:exit; 179, argument_list; 179, 180; 180, integer:2; 181, comment; 182, if_statement; 182, 183; 182, 188; 183, comparison_operator:==; 183, 184; 183, 185; 184, identifier:mode; 185, attribute; 185, 186; 185, 187; 186, identifier:Mode; 187, identifier:RAW; 188, block; 188, 189; 188, 207; 189, for_statement; 189, 190; 189, 191; 189, 192; 190, identifier:entry; 191, identifier:results; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:click; 197, identifier:echo; 198, argument_list; 198, 199; 199, conditional_expression:if; 199, 200; 199, 203; 199, 204; 200, attribute; 200, 201; 200, 202; 201, identifier:entry; 202, identifier:user; 203, identifier:user_flag; 204, attribute; 204, 205; 204, 206; 205, identifier:entry; 206, identifier:password; 207, return_statement; 208, comment; 209, for_statement; 209, 210; 209, 213; 209, 217; 209, 218; 210, pattern_list; 210, 211; 210, 212; 211, identifier:idx; 212, identifier:entry; 213, call; 213, 214; 213, 215; 214, identifier:enumerate; 215, argument_list; 215, 216; 216, identifier:results; 217, comment; 218, block; 218, 219; 218, 229; 218, 246; 218, 247; 218, 327; 218, 328; 218, 394; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:line; 222, call; 222, 223; 222, 224; 223, identifier:highlight_match; 224, argument_list; 224, 225; 224, 226; 225, identifier:key_pattern; 226, attribute; 226, 227; 226, 228; 227, identifier:entry; 228, identifier:key; 229, if_statement; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:entry; 232, identifier:user; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, augmented_assignment:+=; 235, 236; 235, 237; 236, identifier:line; 237, binary_operator:+; 237, 238; 237, 239; 238, string:": "; 239, call; 239, 240; 239, 241; 240, identifier:highlight_match; 241, argument_list; 241, 242; 241, 243; 242, identifier:user_pattern; 243, attribute; 243, 244; 243, 245; 244, identifier:entry; 245, identifier:user; 246, comment; 247, if_statement; 247, 248; 247, 256; 247, 268; 248, boolean_operator:and; 248, 249; 248, 254; 249, comparison_operator:==; 249, 250; 249, 251; 250, identifier:mode; 251, attribute; 251, 252; 251, 253; 252, identifier:Mode; 253, identifier:ECHO; 254, not_operator; 254, 255; 255, identifier:user_flag; 256, block; 256, 257; 257, expression_statement; 257, 258; 258, augmented_assignment:+=; 258, 259; 258, 260; 259, identifier:line; 260, binary_operator:+; 260, 261; 260, 262; 261, string:" | "; 262, call; 262, 263; 262, 264; 263, identifier:style_password; 264, argument_list; 264, 265; 265, attribute; 265, 266; 265, 267; 266, identifier:entry; 267, identifier:password; 268, elif_clause; 268, 269; 268, 278; 269, boolean_operator:and; 269, 270; 269, 275; 270, comparison_operator:==; 270, 271; 270, 272; 271, identifier:mode; 272, attribute; 272, 273; 272, 274; 273, identifier:Mode; 274, identifier:COPY; 275, comparison_operator:==; 275, 276; 275, 277; 276, identifier:idx; 277, integer:0; 278, block; 278, 279; 278, 321; 279, try_statement; 279, 280; 279, 311; 280, block; 280, 281; 280, 284; 280, 298; 281, import_statement; 281, 282; 282, dotted_name; 282, 283; 283, identifier:pyperclip; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:pyperclip; 288, identifier:copy; 289, argument_list; 289, 290; 290, conditional_expression:if; 290, 291; 290, 294; 290, 295; 291, attribute; 291, 292; 291, 293; 292, identifier:entry; 293, identifier:user; 294, identifier:user_flag; 295, attribute; 295, 296; 295, 297; 296, identifier:entry; 297, identifier:password; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:result; 301, call; 301, 302; 301, 303; 302, identifier:style_success; 303, argument_list; 303, 304; 304, binary_operator:%; 304, 305; 304, 306; 305, string:"*** %s COPIED TO CLIPBOARD ***"; 306, parenthesized_expression; 306, 307; 307, conditional_expression:if; 307, 308; 307, 309; 307, 310; 308, string:"USERNAME"; 309, identifier:user_flag; 310, string:"PASSWORD"; 311, except_clause; 311, 312; 311, 313; 312, identifier:ImportError; 313, block; 313, 314; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:result; 317, call; 317, 318; 317, 319; 318, identifier:style_error; 319, argument_list; 319, 320; 320, string:'*** PYTHON PACKAGE "PYPERCLIP" NOT FOUND ***'; 321, expression_statement; 321, 322; 322, augmented_assignment:+=; 322, 323; 322, 324; 323, identifier:line; 324, binary_operator:+; 324, 325; 324, 326; 325, string:" | "; 326, identifier:result; 327, comment; 328, if_statement; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:entry; 331, identifier:notes; 332, block; 332, 333; 333, if_statement; 333, 334; 333, 337; 333, 362; 334, comparison_operator:==; 334, 335; 334, 336; 335, identifier:idx; 336, integer:0; 337, block; 337, 338; 337, 342; 338, expression_statement; 338, 339; 339, augmented_assignment:+=; 339, 340; 339, 341; 340, identifier:line; 341, string:"\n"; 342, expression_statement; 342, 343; 343, augmented_assignment:+=; 343, 344; 343, 345; 344, identifier:line; 345, call; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, string:"\n"; 348, identifier:join; 349, generator_expression; 349, 350; 349, 353; 350, binary_operator:+; 350, 351; 350, 352; 351, string:" "; 352, identifier:line; 353, for_in_clause; 353, 354; 353, 355; 354, identifier:line; 355, call; 355, 356; 355, 361; 356, attribute; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:entry; 359, identifier:notes; 360, identifier:splitlines; 361, argument_list; 362, else_clause; 362, 363; 363, block; 363, 364; 363, 374; 363, 382; 364, expression_statement; 364, 365; 365, assignment; 365, 366; 365, 367; 366, identifier:lines; 367, call; 367, 368; 367, 373; 368, attribute; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:entry; 371, identifier:notes; 372, identifier:splitlines; 373, argument_list; 374, expression_statement; 374, 375; 375, augmented_assignment:+=; 375, 376; 375, 377; 376, identifier:line; 377, binary_operator:+; 377, 378; 377, 379; 378, string:" | "; 379, subscript; 379, 380; 379, 381; 380, identifier:lines; 381, integer:0; 382, if_statement; 382, 383; 382, 389; 383, comparison_operator:>; 383, 384; 383, 388; 384, call; 384, 385; 384, 386; 385, identifier:len; 386, argument_list; 386, 387; 387, identifier:lines; 388, integer:1; 389, block; 389, 390; 390, expression_statement; 390, 391; 391, augmented_assignment:+=; 391, 392; 391, 393; 392, identifier:line; 393, string:" (...)"; 394, expression_statement; 394, 395; 395, call; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:click; 398, identifier:echo; 399, argument_list; 399, 400; 400, identifier:line | def pw(
ctx,
key_pattern,
user_pattern,
mode,
strict_flag,
user_flag,
file,
edit_subcommand,
gen_subcommand,
):
"""Search for USER and KEY in GPG-encrypted password file."""
# install silent Ctrl-C handler
def handle_sigint(*_):
click.echo()
ctx.exit(1)
signal.signal(signal.SIGINT, handle_sigint)
# invoke a subcommand?
if gen_subcommand:
length = int(key_pattern) if key_pattern else None
generate_password(mode, length)
return
elif edit_subcommand:
launch_editor(ctx, file)
return
# verify that database file is present
if not os.path.exists(file):
click.echo("error: password store not found at '%s'" % file, err=True)
ctx.exit(1)
# load database
store = Store.load(file)
# if no user query provided, split key query according to right-most "@" sign (since usernames are typically email addresses)
if not user_pattern:
user_pattern, _, key_pattern = key_pattern.rpartition("@")
# search database
results = store.search(key_pattern, user_pattern)
results = list(results)
# if strict flag is enabled, check that precisely a single record was found
if strict_flag and len(results) != 1:
click.echo(
"error: multiple or no records found (but using --strict flag)", err=True
)
ctx.exit(2)
# raw mode?
if mode == Mode.RAW:
for entry in results:
click.echo(entry.user if user_flag else entry.password)
return
# print results
for idx, entry in enumerate(results):
# start with key and user
line = highlight_match(key_pattern, entry.key)
if entry.user:
line += ": " + highlight_match(user_pattern, entry.user)
# add password or copy&paste sucess message
if mode == Mode.ECHO and not user_flag:
line += " | " + style_password(entry.password)
elif mode == Mode.COPY and idx == 0:
try:
import pyperclip
pyperclip.copy(entry.user if user_flag else entry.password)
result = style_success(
"*** %s COPIED TO CLIPBOARD ***"
% ("USERNAME" if user_flag else "PASSWORD")
)
except ImportError:
result = style_error('*** PYTHON PACKAGE "PYPERCLIP" NOT FOUND ***')
line += " | " + result
# add notes
if entry.notes:
if idx == 0:
line += "\n"
line += "\n".join(" " + line for line in entry.notes.splitlines())
else:
lines = entry.notes.splitlines()
line += " | " + lines[0]
if len(lines) > 1:
line += " (...)"
click.echo(line) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:launch_editor; 3, parameters; 3, 4; 3, 5; 4, identifier:ctx; 5, identifier:file; 6, block; 6, 7; 6, 9; 6, 10; 6, 21; 6, 39; 6, 40; 6, 70; 6, 71; 6, 80; 6, 106; 6, 107; 6, 139; 6, 140; 6, 149; 6, 172; 6, 185; 6, 194; 6, 195; 6, 219; 6, 220; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:editor; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:os; 17, identifier:environ; 18, identifier:get; 19, argument_list; 19, 20; 20, string:"PW_EDITOR"; 21, if_statement; 21, 22; 21, 24; 22, not_operator; 22, 23; 23, identifier:editor; 24, block; 24, 25; 24, 32; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:click; 29, identifier:echo; 30, argument_list; 30, 31; 31, string:"error: no editor set in PW_EDITOR environment variables"; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:ctx; 36, identifier:exit; 37, argument_list; 37, 38; 38, integer:1; 39, comment; 40, if_statement; 40, 41; 40, 50; 41, not_operator; 41, 42; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:os; 46, identifier:path; 47, identifier:exists; 48, argument_list; 48, 49; 49, identifier:file; 50, block; 50, 51; 50, 63; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:click; 55, identifier:echo; 56, argument_list; 56, 57; 56, 60; 57, binary_operator:%; 57, 58; 57, 59; 58, string:"error: password store not found at '%s'"; 59, identifier:file; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:err; 62, True; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:ctx; 67, identifier:exit; 68, argument_list; 68, 69; 69, integer:1; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:is_encrypted; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:_gpg; 77, identifier:is_encrypted; 78, argument_list; 78, 79; 79, identifier:file; 80, if_statement; 80, 81; 80, 82; 80, 92; 81, identifier:is_encrypted; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:original; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:_gpg; 89, identifier:decrypt; 90, argument_list; 90, 91; 91, identifier:file; 92, else_clause; 92, 93; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:original; 97, call; 97, 98; 97, 105; 98, attribute; 98, 99; 98, 104; 99, call; 99, 100; 99, 101; 100, identifier:open; 101, argument_list; 101, 102; 101, 103; 102, identifier:file; 103, string:"rb"; 104, identifier:read; 105, argument_list; 106, comment; 107, if_statement; 107, 108; 107, 109; 108, identifier:is_encrypted; 109, block; 109, 110; 109, 121; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:recipient; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:os; 117, identifier:environ; 118, identifier:get; 119, argument_list; 119, 120; 120, string:"PW_GPG_RECIPIENT"; 121, if_statement; 121, 122; 121, 124; 122, not_operator; 122, 123; 123, identifier:recipient; 124, block; 124, 125; 124, 132; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:click; 129, identifier:echo; 130, argument_list; 130, 131; 131, string:"error: no recipient set in PW_GPG_RECIPIENT environment variables"; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:ctx; 136, identifier:exit; 137, argument_list; 137, 138; 138, integer:1; 139, comment; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:ext; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:_gpg; 146, identifier:unencrypted_ext; 147, argument_list; 147, 148; 148, identifier:file; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:modified; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:click; 155, identifier:edit; 156, argument_list; 156, 157; 156, 163; 156, 166; 156, 169; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:original; 160, identifier:decode; 161, argument_list; 161, 162; 162, string:"utf-8"; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:editor; 165, identifier:editor; 166, keyword_argument; 166, 167; 166, 168; 167, identifier:require_save; 168, True; 169, keyword_argument; 169, 170; 169, 171; 170, identifier:extension; 171, identifier:ext; 172, if_statement; 172, 173; 172, 176; 173, comparison_operator:is; 173, 174; 173, 175; 174, identifier:modified; 175, None; 176, block; 176, 177; 176, 184; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:click; 181, identifier:echo; 182, argument_list; 182, 183; 183, string:"not modified"; 184, return_statement; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:modified; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:modified; 191, identifier:encode; 192, argument_list; 192, 193; 193, string:"utf-8"; 194, comment; 195, if_statement; 195, 196; 195, 198; 196, not_operator; 196, 197; 197, identifier:is_encrypted; 198, block; 198, 199; 198, 218; 199, with_statement; 199, 200; 199, 210; 200, with_clause; 200, 201; 201, with_item; 201, 202; 202, as_pattern; 202, 203; 202, 208; 203, call; 203, 204; 203, 205; 204, identifier:open; 205, argument_list; 205, 206; 205, 207; 206, identifier:file; 207, string:"wb"; 208, as_pattern_target; 208, 209; 209, identifier:fp; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:fp; 215, identifier:write; 216, argument_list; 216, 217; 217, identifier:modified; 218, return_statement; 219, comment; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:_gpg; 224, identifier:encrypt; 225, argument_list; 225, 226; 225, 229; 225, 232; 226, keyword_argument; 226, 227; 226, 228; 227, identifier:recipient; 228, identifier:recipient; 229, keyword_argument; 229, 230; 229, 231; 230, identifier:dest_path; 231, identifier:file; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:content; 234, identifier:modified | def launch_editor(ctx, file):
"""launch editor with decrypted password database"""
# do not use EDITOR environment variable (rather force user to make a concious choice)
editor = os.environ.get("PW_EDITOR")
if not editor:
click.echo("error: no editor set in PW_EDITOR environment variables")
ctx.exit(1)
# verify that database file is present
if not os.path.exists(file):
click.echo("error: password store not found at '%s'" % file, err=True)
ctx.exit(1)
# load source (decrypting if necessary)
is_encrypted = _gpg.is_encrypted(file)
if is_encrypted:
original = _gpg.decrypt(file)
else:
original = open(file, "rb").read()
# if encrypted, determine recipient
if is_encrypted:
recipient = os.environ.get("PW_GPG_RECIPIENT")
if not recipient:
click.echo(
"error: no recipient set in PW_GPG_RECIPIENT environment variables"
)
ctx.exit(1)
# launch the editor
ext = _gpg.unencrypted_ext(file)
modified = click.edit(
original.decode("utf-8"), editor=editor, require_save=True, extension=ext
)
if modified is None:
click.echo("not modified")
return
modified = modified.encode("utf-8")
# not encrypted? simply overwrite file
if not is_encrypted:
with open(file, "wb") as fp:
fp.write(modified)
return
# otherwise, the process is somewhat more complicated
_gpg.encrypt(recipient=recipient, dest_path=file, content=modified) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:fit; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:sim_mat; 5, identifier:D_len; 6, identifier:cidx; 7, block; 7, 8; 7, 10; 7, 16; 7, 173; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:min_energy; 13, attribute; 13, 14; 13, 15; 14, identifier:np; 15, identifier:inf; 16, for_statement; 16, 17; 16, 18; 16, 22; 16, 23; 17, identifier:j; 18, call; 18, 19; 18, 20; 19, identifier:range; 20, argument_list; 20, 21; 21, integer:3; 22, comment; 23, block; 23, 24; 23, 56; 23, 60; 23, 64; 23, 65; 23, 158; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:inds; 27, list_comprehension; 27, 28; 27, 46; 27, 52; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:np; 31, identifier:argmin; 32, argument_list; 32, 33; 33, list_comprehension; 33, 34; 33, 43; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, subscript; 36, 37; 36, 38; 37, identifier:sim_mat; 38, identifier:idy; 39, identifier:get; 40, argument_list; 40, 41; 40, 42; 41, identifier:idx; 42, integer:0; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:idx; 45, identifier:cidx; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:idy; 48, call; 48, 49; 48, 50; 49, identifier:range; 50, argument_list; 50, 51; 51, identifier:D_len; 52, if_clause; 52, 53; 53, comparison_operator:in; 53, 54; 53, 55; 54, identifier:idy; 55, identifier:sim_mat; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:cidx; 59, list:[]; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:energy; 63, integer:0; 64, comment; 65, for_statement; 65, 66; 65, 67; 65, 73; 66, identifier:i; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:np; 70, identifier:unique; 71, argument_list; 71, 72; 72, identifier:inds; 73, block; 73, 74; 73, 87; 73, 88; 73, 96; 73, 144; 73, 148; 73, 157; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:indsi; 77, subscript; 77, 78; 77, 86; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:np; 81, identifier:where; 82, argument_list; 82, 83; 83, comparison_operator:==; 83, 84; 83, 85; 84, identifier:inds; 85, identifier:i; 86, integer:0; 87, comment; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 93; 90, pattern_list; 90, 91; 90, 92; 91, identifier:minind; 92, identifier:min_value; 93, expression_list; 93, 94; 93, 95; 94, integer:0; 95, integer:0; 96, for_statement; 96, 97; 96, 100; 96, 104; 97, pattern_list; 97, 98; 97, 99; 98, identifier:index; 99, identifier:idy; 100, call; 100, 101; 100, 102; 101, identifier:enumerate; 102, argument_list; 102, 103; 103, identifier:indsi; 104, block; 104, 105; 105, if_statement; 105, 106; 105, 109; 105, 110; 106, comparison_operator:in; 106, 107; 106, 108; 107, identifier:idy; 108, identifier:sim_mat; 109, comment; 110, block; 110, 111; 110, 115; 110, 131; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:value; 114, integer:0; 115, for_statement; 115, 116; 115, 117; 115, 118; 116, identifier:idx; 117, identifier:indsi; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, augmented_assignment:+=; 120, 121; 120, 122; 121, identifier:value; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:sim_mat; 126, identifier:idy; 127, identifier:get; 128, argument_list; 128, 129; 128, 130; 129, identifier:idx; 130, integer:0; 131, if_statement; 131, 132; 131, 135; 132, comparison_operator:<; 132, 133; 132, 134; 133, identifier:value; 134, identifier:min_value; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, pattern_list; 138, 139; 138, 140; 139, identifier:minind; 140, identifier:min_value; 141, expression_list; 141, 142; 141, 143; 142, identifier:index; 143, identifier:value; 144, expression_statement; 144, 145; 145, augmented_assignment:+=; 145, 146; 145, 147; 146, identifier:energy; 147, identifier:min_value; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:cidx; 152, identifier:append; 153, argument_list; 153, 154; 154, subscript; 154, 155; 154, 156; 155, identifier:indsi; 156, identifier:minind; 157, comment; 158, if_statement; 158, 159; 158, 162; 159, comparison_operator:<; 159, 160; 159, 161; 160, identifier:energy; 161, identifier:min_energy; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 169; 165, pattern_list; 165, 166; 165, 167; 165, 168; 166, identifier:min_energy; 167, identifier:inds_min; 168, identifier:cidx_min; 169, expression_list; 169, 170; 169, 171; 169, 172; 170, identifier:energy; 171, identifier:inds; 172, identifier:cidx; 173, return_statement; 173, 174; 174, expression_list; 174, 175; 174, 176; 175, identifier:inds_min; 176, identifier:cidx_min | def fit(sim_mat, D_len, cidx):
"""
Algorithm maximizes energy between clusters, which is distinction in this algorithm. Distance matrix contains mostly 0, which are overlooked due to search of maximal distances. Algorithm does not try to retain k clusters.
D: numpy array - Symmetric distance matrix
k: int - number of clusters
"""
min_energy = np.inf
for j in range(3):
# select indices in each sample that maximizes its dimension
inds = [np.argmin([sim_mat[idy].get(idx, 0) for idx in cidx]) for idy in range(D_len) if idy in sim_mat]
cidx = []
energy = 0 # current enengy
for i in np.unique(inds):
indsi = np.where(inds == i)[0] # find indices for every cluster
minind, min_value = 0, 0
for index, idy in enumerate(indsi):
if idy in sim_mat:
# value = sum([sim_mat[idy].get(idx,0) for idx in indsi])
value = 0
for idx in indsi:
value += sim_mat[idy].get(idx, 0)
if value < min_value:
minind, min_value = index, value
energy += min_value
cidx.append(indsi[minind]) # new centers
if energy < min_energy:
min_energy, inds_min, cidx_min = energy, inds, cidx
return inds_min, cidx_min |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 1, 10; 1, 11; 2, function_name:authenticate; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:request; 6, default_parameter; 6, 7; 6, 8; 7, identifier:remote_user; 8, None; 9, comment; 10, comment; 11, block; 11, 12; 11, 14; 11, 22; 11, 28; 11, 32; 11, 41; 11, 290; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 17; 15, not_operator; 15, 16; 16, identifier:remote_user; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:remote_user; 21, identifier:request; 22, if_statement; 22, 23; 22, 25; 23, not_operator; 23, 24; 24, identifier:remote_user; 25, block; 25, 26; 26, return_statement; 26, 27; 27, None; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:user; 31, None; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:username; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:clean_username; 39, argument_list; 39, 40; 40, identifier:remote_user; 41, try_statement; 41, 42; 41, 43; 41, 181; 42, comment; 43, block; 43, 44; 44, if_statement; 44, 45; 44, 48; 44, 160; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:create_unknown_user; 48, block; 48, 49; 48, 53; 48, 118; 48, 140; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:defaults; 52, dictionary; 53, if_statement; 53, 54; 53, 59; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 58; 57, identifier:request; 58, identifier:dict; 59, block; 59, 60; 59, 64; 59, 94; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:session_data; 63, identifier:request; 64, if_statement; 64, 65; 64, 68; 65, comparison_operator:in; 65, 66; 65, 67; 66, string:'full_name'; 67, identifier:session_data; 68, block; 68, 69; 68, 81; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 75; 71, pattern_list; 71, 72; 71, 73; 71, 74; 72, identifier:first_name; 73, identifier:_; 74, identifier:last_name; 75, call; 75, 76; 75, 77; 76, identifier:full_name_natural_split; 77, argument_list; 77, 78; 78, subscript; 78, 79; 78, 80; 79, identifier:session_data; 80, string:'full_name'; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:defaults; 85, identifier:update; 86, argument_list; 86, 87; 87, dictionary; 87, 88; 87, 91; 88, pair; 88, 89; 88, 90; 89, string:'first_name'; 90, identifier:first_name; 91, pair; 91, 92; 91, 93; 92, string:'last_name'; 93, identifier:last_name; 94, for_statement; 94, 95; 94, 96; 94, 100; 95, identifier:key; 96, tuple; 96, 97; 96, 98; 96, 99; 97, string:'email'; 98, string:'first_name'; 99, string:'last_name'; 100, block; 100, 101; 101, if_statement; 101, 102; 101, 105; 102, comparison_operator:in; 102, 103; 102, 104; 103, identifier:key; 104, identifier:session_data; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:defaults; 110, identifier:update; 111, argument_list; 111, 112; 112, dictionary; 112, 113; 113, pair; 113, 114; 113, 115; 114, identifier:key; 115, subscript; 115, 116; 115, 117; 116, identifier:session_data; 117, identifier:key; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, pattern_list; 120, 121; 120, 122; 121, identifier:user; 122, identifier:created; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:UserModel; 127, identifier:_default_manager; 128, identifier:get_or_create; 129, argument_list; 129, 130; 130, dictionary_splat; 130, 131; 131, dictionary; 131, 132; 131, 137; 132, pair; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:UserModel; 135, identifier:USERNAME_FIELD; 136, identifier:username; 137, pair; 137, 138; 137, 139; 138, string:'defaults'; 139, identifier:defaults; 140, if_statement; 140, 141; 140, 142; 141, identifier:created; 142, block; 142, 143; 142, 151; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:LOGGER; 147, identifier:debug; 148, argument_list; 148, 149; 148, 150; 149, string:"created user '%s' in database."; 150, identifier:username; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:user; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:configure_user; 158, argument_list; 158, 159; 159, identifier:user; 160, else_clause; 160, 161; 161, block; 161, 162; 162, try_statement; 162, 163; 162, 175; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:user; 167, call; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:UserModel; 171, identifier:_default_manager; 172, identifier:get_by_natural_key; 173, argument_list; 173, 174; 174, identifier:username; 175, except_clause; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:UserModel; 178, identifier:DoesNotExist; 179, block; 179, 180; 180, pass_statement; 181, except_clause; 181, 182; 181, 186; 182, as_pattern; 182, 183; 182, 184; 183, identifier:DatabaseError; 184, as_pattern_target; 184, 185; 185, identifier:err; 186, block; 186, 187; 186, 195; 186, 196; 186, 243; 186, 244; 186, 267; 186, 280; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:LOGGER; 191, identifier:debug; 192, argument_list; 192, 193; 192, 194; 193, string:"User table missing from database? (err:%s)"; 194, identifier:err; 195, comment; 196, for_statement; 196, 197; 196, 198; 196, 206; 197, identifier:user; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:six; 201, identifier:itervalues; 202, argument_list; 202, 203; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:users; 206, block; 206, 207; 206, 221; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:LOGGER; 211, identifier:debug; 212, argument_list; 212, 213; 212, 214; 212, 215; 212, 218; 213, string:"match %s with User(id=%d, username=%s)"; 214, identifier:username; 215, attribute; 215, 216; 215, 217; 216, identifier:user; 217, identifier:id; 218, attribute; 218, 219; 218, 220; 219, identifier:user; 220, identifier:username; 221, if_statement; 221, 222; 221, 227; 222, comparison_operator:==; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:user; 225, identifier:username; 226, identifier:username; 227, block; 227, 228; 227, 241; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:LOGGER; 232, identifier:debug; 233, argument_list; 233, 234; 233, 235; 233, 238; 234, string:"found %d %s"; 235, attribute; 235, 236; 235, 237; 236, identifier:user; 237, identifier:id; 238, attribute; 238, 239; 238, 240; 239, identifier:user; 240, identifier:username; 241, return_statement; 241, 242; 242, identifier:user; 243, comment; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:user; 247, call; 247, 248; 247, 249; 248, identifier:UserModel; 249, argument_list; 249, 250; 249, 264; 250, keyword_argument; 250, 251; 250, 252; 251, identifier:id; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:random; 255, identifier:randint; 256, argument_list; 256, 257; 256, 258; 257, integer:1; 258, binary_operator:-; 258, 259; 258, 263; 259, parenthesized_expression; 259, 260; 260, binary_operator:<<; 260, 261; 260, 262; 261, integer:1; 262, integer:32; 263, integer:1; 264, keyword_argument; 264, 265; 264, 266; 265, identifier:username; 266, identifier:username; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:LOGGER; 271, identifier:debug; 272, argument_list; 272, 273; 272, 274; 272, 277; 273, string:"add User(id=%d, username=%s) to cache."; 274, attribute; 274, 275; 274, 276; 275, identifier:user; 276, identifier:id; 277, attribute; 277, 278; 277, 279; 278, identifier:user; 279, identifier:username; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 289; 282, subscript; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:self; 285, identifier:users; 286, attribute; 286, 287; 286, 288; 287, identifier:user; 288, identifier:id; 289, identifier:user; 290, return_statement; 290, 291; 291, conditional_expression:if; 291, 292; 291, 293; 291, 299; 292, identifier:user; 293, call; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:self; 296, identifier:user_can_authenticate; 297, argument_list; 297, 298; 298, identifier:user; 299, None | def authenticate(self, request, remote_user=None):
#pylint:disable=arguments-differ
# Django <=1.8 and >=1.9 have different signatures.
"""
The ``username`` passed here is considered trusted. This
method simply returns the ``User`` object with the given username.
In order to support older Django versions
(before commit 4b9330ccc04575f9e5126529ec355a450d12e77c), if username
is None, we will assume request is the ``remote_user`` parameter.
"""
if not remote_user:
remote_user = request
if not remote_user:
return None
user = None
username = self.clean_username(remote_user)
try:
#pylint:disable=protected-access
if self.create_unknown_user:
defaults = {}
if isinstance(request, dict):
session_data = request
if 'full_name' in session_data:
first_name, _, last_name = full_name_natural_split(
session_data['full_name'])
defaults.update({
'first_name': first_name,
'last_name': last_name
})
for key in ('email', 'first_name', 'last_name'):
if key in session_data:
defaults.update({key: session_data[key]})
user, created = UserModel._default_manager.get_or_create(**{
UserModel.USERNAME_FIELD: username,
'defaults': defaults,
})
if created:
LOGGER.debug("created user '%s' in database.", username)
user = self.configure_user(user)
else:
try:
user = UserModel._default_manager.get_by_natural_key(
username)
except UserModel.DoesNotExist:
pass
except DatabaseError as err:
LOGGER.debug("User table missing from database? (err:%s)", err)
# We don't have a auth_user table, so let's build a hash in memory.
for user in six.itervalues(self.users):
LOGGER.debug("match %s with User(id=%d, username=%s)",
username, user.id, user.username)
if user.username == username:
LOGGER.debug("found %d %s", user.id, user.username)
return user
# Not found in memory dict
user = UserModel(
id=random.randint(1, (1 << 32) - 1), username=username)
LOGGER.debug("add User(id=%d, username=%s) to cache.",
user.id, user.username)
self.users[user.id] = user
return user if self.user_can_authenticate(user) else None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:bc2pg; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 4, identifier:dataset; 5, identifier:db_url; 6, identifier:table; 7, identifier:schema; 8, identifier:query; 9, identifier:append; 10, identifier:pagesize; 11, identifier:sortby; 12, identifier:max_workers; 13, block; 13, 14; 13, 16; 13, 25; 13, 44; 13, 52; 13, 60; 13, 61; 13, 70; 13, 96; 13, 97; 13, 115; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:src; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:bcdata; 22, identifier:validate_name; 23, argument_list; 23, 24; 24, identifier:dataset; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 30; 27, pattern_list; 27, 28; 27, 29; 28, identifier:src_schema; 29, identifier:src_table; 30, list_comprehension; 30, 31; 30, 36; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:i; 34, identifier:lower; 35, argument_list; 36, for_in_clause; 36, 37; 36, 38; 37, identifier:i; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:src; 41, identifier:split; 42, argument_list; 42, 43; 43, string:"."; 44, if_statement; 44, 45; 44, 47; 45, not_operator; 45, 46; 46, identifier:schema; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:schema; 51, identifier:src_schema; 52, if_statement; 52, 53; 52, 55; 53, not_operator; 53, 54; 54, identifier:table; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:table; 59, identifier:src_table; 60, comment; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:conn; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:pgdata; 67, identifier:connect; 68, argument_list; 68, 69; 69, identifier:db_url; 70, if_statement; 70, 71; 70, 76; 71, comparison_operator:not; 71, 72; 71, 73; 72, identifier:schema; 73, attribute; 73, 74; 73, 75; 74, identifier:conn; 75, identifier:schemas; 76, block; 76, 77; 76, 89; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:click; 81, identifier:echo; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, string:"Schema {} does not exist, creating it"; 86, identifier:format; 87, argument_list; 87, 88; 88, identifier:schema; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:conn; 93, identifier:create_schema; 94, argument_list; 94, 95; 95, identifier:schema; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:param_dicts; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:bcdata; 103, identifier:define_request; 104, argument_list; 104, 105; 104, 106; 104, 109; 104, 112; 105, identifier:dataset; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:query; 108, identifier:query; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:sortby; 111, identifier:sortby; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:pagesize; 114, identifier:pagesize; 115, try_statement; 115, 116; 115, 117; 115, 386; 116, comment; 117, block; 117, 118; 117, 130; 117, 140; 117, 147; 117, 175; 117, 176; 117, 223; 117, 224; 117, 368; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:payload; 121, call; 121, 122; 121, 123; 122, identifier:urlencode; 123, argument_list; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:param_dicts; 126, integer:0; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:doseq; 129, True; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:url; 133, binary_operator:+; 133, 134; 133, 139; 134, binary_operator:+; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:bcdata; 137, identifier:WFS_URL; 138, string:"?"; 139, identifier:payload; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:db; 143, call; 143, 144; 143, 145; 144, identifier:parse_db_url; 145, argument_list; 145, 146; 146, identifier:db_url; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:db_string; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, string:"PG:host={h} user={u} dbname={db} password={pwd}"; 153, identifier:format; 154, argument_list; 154, 155; 154, 160; 154, 165; 154, 170; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:h; 157, subscript; 157, 158; 157, 159; 158, identifier:db; 159, string:"host"; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:u; 162, subscript; 162, 163; 162, 164; 163, identifier:db; 164, string:"user"; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:db; 167, subscript; 167, 168; 167, 169; 168, identifier:db; 169, string:"database"; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:pwd; 172, subscript; 172, 173; 172, 174; 173, identifier:db; 174, string:"password"; 175, comment; 176, if_statement; 176, 177; 176, 179; 177, not_operator; 177, 178; 178, identifier:append; 179, block; 179, 180; 179, 204; 179, 216; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:command; 183, list:[
"ogr2ogr",
"-lco",
"OVERWRITE=YES",
"-lco",
"SCHEMA={}".format(schema),
"-lco",
"GEOMETRY_NAME=geom",
"-f",
"PostgreSQL",
db_string,
"-t_srs",
"EPSG:3005",
"-nln",
table,
url,
]; 183, 184; 183, 185; 183, 186; 183, 187; 183, 188; 183, 194; 183, 195; 183, 196; 183, 197; 183, 198; 183, 199; 183, 200; 183, 201; 183, 202; 183, 203; 184, string:"ogr2ogr"; 185, string:"-lco"; 186, string:"OVERWRITE=YES"; 187, string:"-lco"; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, string:"SCHEMA={}"; 191, identifier:format; 192, argument_list; 192, 193; 193, identifier:schema; 194, string:"-lco"; 195, string:"GEOMETRY_NAME=geom"; 196, string:"-f"; 197, string:"PostgreSQL"; 198, identifier:db_string; 199, string:"-t_srs"; 200, string:"EPSG:3005"; 201, string:"-nln"; 202, identifier:table; 203, identifier:url; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:click; 208, identifier:echo; 209, argument_list; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, string:" "; 213, identifier:join; 214, argument_list; 214, 215; 215, identifier:command; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:subprocess; 220, identifier:run; 221, argument_list; 221, 222; 222, identifier:command; 223, comment; 224, if_statement; 224, 225; 224, 233; 224, 234; 225, boolean_operator:or; 225, 226; 225, 232; 226, comparison_operator:>; 226, 227; 226, 231; 227, call; 227, 228; 227, 229; 228, identifier:len; 229, argument_list; 229, 230; 230, identifier:param_dicts; 231, integer:1; 232, identifier:append; 233, comment; 234, block; 234, 235; 234, 248; 234, 252; 234, 311; 234, 312; 234, 319; 235, if_statement; 235, 236; 235, 237; 235, 242; 236, identifier:append; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:idx; 241, integer:0; 242, else_clause; 242, 243; 243, block; 243, 244; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:idx; 247, integer:1; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:commands; 251, list:[]; 252, for_statement; 252, 253; 252, 256; 252, 264; 253, pattern_list; 253, 254; 253, 255; 254, identifier:chunk; 255, identifier:paramdict; 256, call; 256, 257; 256, 258; 257, identifier:enumerate; 258, argument_list; 258, 259; 259, subscript; 259, 260; 259, 261; 260, identifier:param_dicts; 261, slice; 261, 262; 261, 263; 262, identifier:idx; 263, colon; 264, block; 264, 265; 264, 275; 264, 285; 264, 304; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:payload; 268, call; 268, 269; 268, 270; 269, identifier:urlencode; 270, argument_list; 270, 271; 270, 272; 271, identifier:paramdict; 272, keyword_argument; 272, 273; 272, 274; 273, identifier:doseq; 274, True; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:url; 278, binary_operator:+; 278, 279; 278, 284; 279, binary_operator:+; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:bcdata; 282, identifier:WFS_URL; 283, string:"?"; 284, identifier:payload; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:command; 288, list:[
"ogr2ogr",
"-update",
"-append",
"-f",
"PostgreSQL",
db_string + " active_schema=" + schema,
"-t_srs",
"EPSG:3005",
"-nln",
table,
url,
]; 288, 289; 288, 290; 288, 291; 288, 292; 288, 293; 288, 294; 288, 299; 288, 300; 288, 301; 288, 302; 288, 303; 289, string:"ogr2ogr"; 290, string:"-update"; 291, string:"-append"; 292, string:"-f"; 293, string:"PostgreSQL"; 294, binary_operator:+; 294, 295; 294, 298; 295, binary_operator:+; 295, 296; 295, 297; 296, identifier:db_string; 297, string:" active_schema="; 298, identifier:schema; 299, string:"-t_srs"; 300, string:"EPSG:3005"; 301, string:"-nln"; 302, identifier:table; 303, identifier:url; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:commands; 308, identifier:append; 309, argument_list; 309, 310; 310, identifier:command; 311, comment; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:pool; 315, call; 315, 316; 315, 317; 316, identifier:Pool; 317, argument_list; 317, 318; 318, identifier:max_workers; 319, with_statement; 319, 320; 319, 346; 320, with_clause; 320, 321; 321, with_item; 321, 322; 322, as_pattern; 322, 323; 322, 344; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:click; 326, identifier:progressbar; 327, argument_list; 327, 328; 327, 338; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:pool; 331, identifier:imap; 332, argument_list; 332, 333; 332, 337; 333, call; 333, 334; 333, 335; 334, identifier:partial; 335, argument_list; 335, 336; 336, identifier:call; 337, identifier:commands; 338, keyword_argument; 338, 339; 338, 340; 339, identifier:length; 340, call; 340, 341; 340, 342; 341, identifier:len; 342, argument_list; 342, 343; 343, identifier:param_dicts; 344, as_pattern_target; 344, 345; 345, identifier:bar; 346, block; 346, 347; 347, for_statement; 347, 348; 347, 349; 347, 350; 348, identifier:returncode; 349, identifier:bar; 350, block; 350, 351; 351, if_statement; 351, 352; 351, 355; 352, comparison_operator:!=; 352, 353; 352, 354; 353, identifier:returncode; 354, integer:0; 355, block; 355, 356; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:click; 360, identifier:echo; 361, argument_list; 361, 362; 362, call; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, string:"Command failed: {}"; 365, identifier:format; 366, argument_list; 366, 367; 367, identifier:returncode; 368, expression_statement; 368, 369; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:click; 372, identifier:echo; 373, argument_list; 373, 374; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, string:"Load of {} to {} in {} complete"; 377, identifier:format; 378, argument_list; 378, 379; 378, 380; 378, 385; 379, identifier:src; 380, binary_operator:+; 380, 381; 380, 384; 381, binary_operator:+; 381, 382; 381, 383; 382, identifier:schema; 383, string:"."; 384, identifier:table; 385, identifier:db_url; 386, except_clause; 386, 387; 386, 388; 387, identifier:Exception; 388, block; 388, 389; 388, 396; 389, expression_statement; 389, 390; 390, call; 390, 391; 390, 394; 391, attribute; 391, 392; 391, 393; 392, identifier:click; 393, identifier:echo; 394, argument_list; 394, 395; 395, string:"Data load failed"; 396, raise_statement; 396, 397; 397, call; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, identifier:click; 400, identifier:Abort; 401, argument_list | def bc2pg(dataset, db_url, table, schema, query, append, pagesize, sortby, max_workers):
"""Download a DataBC WFS layer to postgres - an ogr2ogr wrapper.
\b
$ bcdata bc2pg bc-airports --db_url postgresql://postgres:postgres@localhost:5432/postgis
The default target database can be specified by setting the $DATABASE_URL
environment variable.
https://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls
"""
src = bcdata.validate_name(dataset)
src_schema, src_table = [i.lower() for i in src.split(".")]
if not schema:
schema = src_schema
if not table:
table = src_table
# create schema if it does not exist
conn = pgdata.connect(db_url)
if schema not in conn.schemas:
click.echo("Schema {} does not exist, creating it".format(schema))
conn.create_schema(schema)
# build parameters for each required request
param_dicts = bcdata.define_request(
dataset, query=query, sortby=sortby, pagesize=pagesize
)
try:
# run the first request / load
payload = urlencode(param_dicts[0], doseq=True)
url = bcdata.WFS_URL + "?" + payload
db = parse_db_url(db_url)
db_string = "PG:host={h} user={u} dbname={db} password={pwd}".format(
h=db["host"], u=db["user"], db=db["database"], pwd=db["password"]
)
# create the table
if not append:
command = [
"ogr2ogr",
"-lco",
"OVERWRITE=YES",
"-lco",
"SCHEMA={}".format(schema),
"-lco",
"GEOMETRY_NAME=geom",
"-f",
"PostgreSQL",
db_string,
"-t_srs",
"EPSG:3005",
"-nln",
table,
url,
]
click.echo(" ".join(command))
subprocess.run(command)
# append to table when append specified or processing many chunks
if len(param_dicts) > 1 or append:
# define starting index in list of requests
if append:
idx = 0
else:
idx = 1
commands = []
for chunk, paramdict in enumerate(param_dicts[idx:]):
payload = urlencode(paramdict, doseq=True)
url = bcdata.WFS_URL + "?" + payload
command = [
"ogr2ogr",
"-update",
"-append",
"-f",
"PostgreSQL",
db_string + " active_schema=" + schema,
"-t_srs",
"EPSG:3005",
"-nln",
table,
url,
]
commands.append(command)
# https://stackoverflow.com/questions/14533458
pool = Pool(max_workers)
with click.progressbar(
pool.imap(partial(call), commands), length=len(param_dicts)
) as bar:
for returncode in bar:
if returncode != 0:
click.echo("Command failed: {}".format(returncode))
click.echo(
"Load of {} to {} in {} complete".format(src, schema + "." + table, db_url)
)
except Exception:
click.echo("Data load failed")
raise click.Abort() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:__parseFormat; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:fmt; 6, identifier:content; 7, default_parameter; 7, 8; 7, 9; 8, identifier:fps; 9, integer:25; 10, block; 10, 11; 10, 13; 10, 17; 10, 21; 11, expression_statement; 11, 12; 12, string:'''Actual parser. Please note that time_to is not required to process as not all subtitles
provide it.'''; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:headerFound; 16, False; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:subSection; 20, string:''; 21, for_statement; 21, 22; 21, 25; 21, 29; 22, pattern_list; 22, 23; 22, 24; 23, identifier:lineNo; 24, identifier:line; 25, call; 25, 26; 25, 27; 26, identifier:enumerate; 27, argument_list; 27, 28; 28, identifier:content; 29, block; 29, 30; 29, 40; 29, 58; 29, 69; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:line; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:_initialLinePrepare; 37, argument_list; 37, 38; 37, 39; 38, identifier:line; 39, identifier:lineNo; 40, if_statement; 40, 41; 40, 56; 41, boolean_operator:and; 41, 42; 41, 51; 42, boolean_operator:and; 42, 43; 42, 47; 43, not_operator; 43, 44; 44, attribute; 44, 45; 44, 46; 45, identifier:fmt; 46, identifier:WITH_HEADER; 47, not_operator; 47, 48; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:_formatFound; 51, comparison_operator:>; 51, 52; 51, 53; 52, identifier:lineNo; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:_maxFmtSearch; 56, block; 56, 57; 57, return_statement; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:subSection; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, string:''; 64, identifier:join; 65, argument_list; 65, 66; 66, list:[subSection, line]; 66, 67; 66, 68; 67, identifier:subSection; 68, identifier:line; 69, if_statement; 69, 70; 69, 76; 69, 114; 70, boolean_operator:and; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:fmt; 73, identifier:WITH_HEADER; 74, not_operator; 74, 75; 75, identifier:headerFound; 76, block; 76, 77; 76, 85; 76, 101; 77, if_statement; 77, 78; 77, 83; 78, comparison_operator:>; 78, 79; 78, 80; 79, identifier:lineNo; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:_maxHeaderLen; 83, block; 83, 84; 84, return_statement; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:headerFound; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:fmt; 91, identifier:addHeaderInfo; 92, argument_list; 92, 93; 92, 94; 93, identifier:subSection; 94, call; 94, 95; 94, 100; 95, attribute; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:_subtitles; 99, identifier:header; 100, argument_list; 101, if_statement; 101, 102; 101, 103; 102, identifier:headerFound; 103, block; 103, 104; 103, 110; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:_formatFound; 109, True; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:subSection; 113, string:''; 114, elif_clause; 114, 115; 114, 131; 115, boolean_operator:or; 115, 116; 115, 122; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:fmt; 119, identifier:subtitleEnds; 120, argument_list; 120, 121; 121, identifier:line; 122, comparison_operator:==; 122, 123; 122, 127; 123, parenthesized_expression; 123, 124; 124, binary_operator:+; 124, 125; 124, 126; 125, identifier:lineNo; 126, integer:1; 127, call; 127, 128; 127, 129; 128, identifier:len; 129, argument_list; 129, 130; 130, identifier:content; 131, block; 131, 132; 131, 142; 131, 183; 131, 184; 131, 236; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:subtitle; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:fmt; 138, identifier:createSubtitle; 139, argument_list; 139, 140; 139, 141; 140, identifier:fps; 141, identifier:subSection; 142, if_statement; 142, 143; 142, 146; 143, comparison_operator:is; 143, 144; 143, 145; 144, identifier:subtitle; 145, None; 146, block; 146, 147; 147, if_statement; 147, 148; 147, 154; 147, 160; 147, 180; 148, comparison_operator:in; 148, 149; 148, 150; 149, identifier:subSection; 150, tuple; 150, 151; 150, 152; 150, 153; 151, string:'\n'; 152, string:'\r\n'; 153, string:'\r'; 154, block; 154, 155; 154, 159; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:subSection; 158, string:''; 159, continue_statement; 160, elif_clause; 160, 161; 160, 170; 161, comparison_operator:>; 161, 162; 161, 169; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:_subtitles; 167, identifier:size; 168, argument_list; 169, integer:0; 170, block; 170, 171; 171, raise_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:SubParsingError; 174, argument_list; 174, 175; 174, 179; 175, call; 175, 176; 175, 177; 176, identifier:_; 177, argument_list; 177, 178; 178, string:"Parsing error"; 179, identifier:lineNo; 180, else_clause; 180, 181; 181, block; 181, 182; 182, return_statement; 183, comment; 184, if_statement; 184, 185; 184, 192; 184, 222; 184, 233; 185, boolean_operator:and; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:subtitle; 188, identifier:start; 189, attribute; 189, 190; 189, 191; 190, identifier:subtitle; 191, identifier:text; 192, block; 192, 193; 192, 199; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:_formatFound; 198, True; 199, try_statement; 199, 200; 199, 210; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:_subtitles; 207, identifier:append; 208, argument_list; 208, 209; 209, identifier:subtitle; 210, except_clause; 210, 211; 210, 215; 211, as_pattern; 211, 212; 211, 213; 212, identifier:SubException; 213, as_pattern_target; 213, 214; 214, identifier:msg; 215, block; 215, 216; 216, raise_statement; 216, 217; 217, call; 217, 218; 217, 219; 218, identifier:SubParsingError; 219, argument_list; 219, 220; 219, 221; 220, identifier:msg; 221, identifier:lineNo; 222, elif_clause; 222, 223; 222, 231; 223, boolean_operator:and; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:subtitle; 226, identifier:start; 227, not_operator; 227, 228; 228, attribute; 228, 229; 228, 230; 229, identifier:subtitle; 230, identifier:text; 231, block; 231, 232; 232, pass_statement; 233, else_clause; 233, 234; 234, block; 234, 235; 235, return_statement; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:subSection; 239, string:'' | def __parseFormat(self, fmt, content, fps = 25):
'''Actual parser. Please note that time_to is not required to process as not all subtitles
provide it.'''
headerFound = False
subSection = ''
for lineNo, line in enumerate(content):
line = self._initialLinePrepare(line, lineNo)
if not fmt.WITH_HEADER and not self._formatFound and lineNo > self._maxFmtSearch:
return
subSection = ''.join([subSection, line])
if fmt.WITH_HEADER and not headerFound:
if lineNo > self._maxHeaderLen:
return
headerFound = fmt.addHeaderInfo(subSection, self._subtitles.header())
if headerFound:
self._formatFound = True
subSection = ''
elif fmt.subtitleEnds(line) or (lineNo + 1) == len(content):
subtitle = fmt.createSubtitle(fps, subSection)
if subtitle is None:
if subSection in ('\n', '\r\n', '\r'):
subSection = ''
continue
elif self._subtitles.size() > 0:
raise SubParsingError(_("Parsing error"), lineNo)
else:
return
# store parsing result if new end marker occurred, then clear results
if subtitle.start and subtitle.text:
self._formatFound = True
try:
self._subtitles.append(subtitle)
except SubException as msg:
raise SubParsingError(msg, lineNo)
elif subtitle.start and not subtitle.text:
pass
else:
return
subSection = '' |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:_get_json; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:model; 6, default_parameter; 6, 7; 6, 8; 7, identifier:space; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:rel_path; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:extra_params; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:get_all; 17, None; 18, block; 18, 19; 18, 21; 18, 22; 18, 23; 18, 41; 18, 49; 18, 50; 18, 62; 18, 63; 18, 88; 18, 89; 18, 155; 19, expression_statement; 19, 20; 20, comment; 21, comment; 22, comment; 23, if_statement; 23, 24; 23, 33; 24, boolean_operator:and; 24, 25; 24, 28; 25, comparison_operator:is; 25, 26; 25, 27; 26, identifier:space; 27, None; 28, comparison_operator:not; 28, 29; 28, 30; 29, identifier:model; 30, tuple; 30, 31; 30, 32; 31, identifier:Space; 32, identifier:Event; 33, block; 33, 34; 34, raise_statement; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:Exception; 37, argument_list; 37, 38; 38, concatenated_string; 38, 39; 38, 40; 39, string:'In general, `API._get_json` should always '; 40, string:'be called with a `space` argument.'; 41, if_statement; 41, 42; 41, 44; 42, not_operator; 42, 43; 43, identifier:extra_params; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:extra_params; 48, dictionary; 49, comment; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:extra_params; 54, string:'page'; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:extra_params; 58, identifier:get; 59, argument_list; 59, 60; 59, 61; 60, string:'page'; 61, integer:1; 62, comment; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:url; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, string:'{0}/{1}/{2}.json?{3}'; 69, identifier:format; 70, argument_list; 70, 71; 70, 74; 70, 77; 70, 82; 71, attribute; 71, 72; 71, 73; 72, identifier:settings; 73, identifier:API_ROOT_PATH; 74, attribute; 74, 75; 74, 76; 75, identifier:settings; 76, identifier:API_VERSION; 77, boolean_operator:or; 77, 78; 77, 79; 78, identifier:rel_path; 79, attribute; 79, 80; 79, 81; 80, identifier:model; 81, identifier:rel_path; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:urllib; 85, identifier:urlencode; 86, argument_list; 86, 87; 87, identifier:extra_params; 88, comment; 89, if_statement; 89, 90; 89, 99; 89, 108; 90, boolean_operator:and; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:cache_responses; 94, comparison_operator:in; 94, 95; 94, 96; 95, identifier:url; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:cache; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:response; 103, subscript; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:cache; 107, identifier:url; 108, else_clause; 108, 109; 108, 110; 109, comment; 110, block; 110, 111; 110, 125; 110, 141; 110, 142; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:headers; 114, dictionary; 114, 115; 114, 120; 115, pair; 115, 116; 115, 117; 116, string:'X-Api-Key'; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:key; 120, pair; 120, 121; 120, 122; 121, string:'X-Api-Secret'; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:secret; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:response; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:session; 133, identifier:get; 134, argument_list; 134, 135; 134, 138; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:url; 137, identifier:url; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:headers; 140, identifier:headers; 141, comment; 142, if_statement; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:cache_responses; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 154; 149, subscript; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:cache; 153, identifier:url; 154, identifier:response; 155, if_statement; 155, 156; 155, 161; 155, 162; 155, 265; 155, 275; 156, comparison_operator:==; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:response; 159, identifier:status_code; 160, integer:200; 161, comment; 162, block; 162, 163; 162, 167; 162, 175; 162, 210; 162, 211; 162, 212; 162, 222; 162, 263; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:results; 166, list:[]; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:json_response; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:response; 173, identifier:json; 174, argument_list; 175, for_statement; 175, 176; 175, 177; 175, 178; 176, identifier:obj; 177, identifier:json_response; 178, block; 178, 179; 178, 188; 178, 194; 178, 203; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:instance; 182, call; 182, 183; 182, 184; 183, identifier:model; 184, argument_list; 184, 185; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:data; 187, identifier:obj; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:instance; 192, identifier:api; 193, identifier:self; 194, if_statement; 194, 195; 194, 196; 195, identifier:space; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:instance; 201, identifier:space; 202, identifier:space; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:results; 207, identifier:append; 208, argument_list; 208, 209; 209, identifier:instance; 210, comment; 211, comment; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:per_page; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:extra_params; 218, identifier:get; 219, argument_list; 219, 220; 219, 221; 220, string:'per_page'; 221, None; 222, if_statement; 222, 223; 222, 239; 223, parenthesized_expression; 223, 224; 224, boolean_operator:and; 224, 225; 224, 233; 225, boolean_operator:and; 225, 226; 225, 229; 226, boolean_operator:and; 226, 227; 226, 228; 227, identifier:get_all; 228, identifier:per_page; 229, call; 229, 230; 229, 231; 230, identifier:len; 231, argument_list; 231, 232; 232, identifier:json_response; 233, comparison_operator:==; 233, 234; 233, 235; 234, identifier:per_page; 235, call; 235, 236; 235, 237; 236, identifier:len; 237, argument_list; 237, 238; 238, identifier:json_response; 239, block; 239, 240; 239, 246; 240, expression_statement; 240, 241; 241, augmented_assignment:+=; 241, 242; 241, 245; 242, subscript; 242, 243; 242, 244; 243, identifier:extra_params; 244, string:'page'; 245, integer:1; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:results; 249, binary_operator:+; 249, 250; 249, 251; 250, identifier:results; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:_get_json; 255, argument_list; 255, 256; 255, 257; 255, 258; 255, 259; 255, 260; 256, identifier:model; 257, identifier:space; 258, identifier:rel_path; 259, identifier:extra_params; 260, keyword_argument; 260, 261; 260, 262; 261, identifier:get_all; 262, identifier:get_all; 263, return_statement; 263, 264; 264, identifier:results; 265, elif_clause; 265, 266; 265, 271; 265, 272; 266, comparison_operator:==; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:response; 269, identifier:status_code; 270, integer:204; 271, comment; 272, block; 272, 273; 273, return_statement; 273, 274; 274, list:[]; 275, else_clause; 275, 276; 275, 277; 276, comment; 277, block; 277, 278; 278, raise_statement; 278, 279; 279, call; 279, 280; 279, 281; 280, identifier:Exception; 281, argument_list; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, string:'Code {0} returned from `{1}`. Response text: "{2}".'; 285, identifier:format; 286, argument_list; 286, 287; 286, 290; 286, 291; 287, attribute; 287, 288; 287, 289; 288, identifier:response; 289, identifier:status_code; 290, identifier:url; 291, attribute; 291, 292; 291, 293; 292, identifier:response; 293, identifier:text | def _get_json(self, model, space=None, rel_path=None, extra_params=None, get_all=None):
"""
Base level method for fetching data from the API
"""
# Only API.spaces and API.event should not provide
# the `space argument
if space is None and model not in (Space, Event):
raise Exception(
'In general, `API._get_json` should always '
'be called with a `space` argument.'
)
if not extra_params:
extra_params = {}
# Handle pagination for requests carrying large amounts of data
extra_params['page'] = extra_params.get('page', 1)
# Generate the url to hit
url = '{0}/{1}/{2}.json?{3}'.format(
settings.API_ROOT_PATH,
settings.API_VERSION,
rel_path or model.rel_path,
urllib.urlencode(extra_params),
)
# If the cache is being used and the url has been hit already
if self.cache_responses and url in self.cache:
response = self.cache[url]
else:
# Fetch the data
headers = {
'X-Api-Key': self.key,
'X-Api-Secret': self.secret,
}
response = self.session.get(url=url, headers=headers)
# If the cache is being used, update it
if self.cache_responses:
self.cache[url] = response
if response.status_code == 200: # OK
results = []
json_response = response.json()
for obj in json_response:
instance = model(data=obj)
instance.api = self
if space:
instance.space = space
results.append(instance)
# If it looks like there are more pages to fetch,
# try and fetch the next one
per_page = extra_params.get('per_page', None)
if (
get_all
and per_page
and len(json_response)
and per_page == len(json_response)
):
extra_params['page'] += 1
results = results + self._get_json(model, space, rel_path, extra_params, get_all=get_all)
return results
elif response.status_code == 204: # No Content
return []
else: # Most likely a 404 Not Found
raise Exception(
'Code {0} returned from `{1}`. Response text: "{2}".'.format(
response.status_code,
url,
response.text
)
) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:load_config; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:app_name; 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, 16; 9, 17; 9, 27; 9, 37; 9, 47; 9, 75; 9, 79; 9, 99; 9, 124; 9, 128; 9, 397; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:configure_logging; 15, argument_list; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:prefix; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:kwargs; 23, identifier:get; 24, argument_list; 24, 25; 24, 26; 25, string:'prefix'; 26, string:'etc'; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:verbose; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:kwargs; 33, identifier:get; 34, argument_list; 34, 35; 34, 36; 35, string:'verbose'; 36, False; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:location; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:kwargs; 43, identifier:get; 44, argument_list; 44, 45; 44, 46; 45, string:'location'; 46, None; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:passphrase; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:kwargs; 53, identifier:get; 54, argument_list; 54, 55; 54, 56; 55, string:'passphrase'; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:os; 59, identifier:getenv; 60, argument_list; 60, 61; 60, 68; 61, binary_operator:%; 61, 62; 61, 63; 62, string:"%s_SETTINGS_CRYPT_KEY"; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:app_name; 66, identifier:upper; 67, argument_list; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:os; 71, identifier:getenv; 72, argument_list; 72, 73; 72, 74; 73, string:"SETTINGS_CRYPT_KEY"; 74, None; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:confnames; 78, identifier:args; 79, if_statement; 79, 80; 79, 82; 80, not_operator; 80, 81; 81, identifier:location; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:location; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:os; 89, identifier:getenv; 90, argument_list; 90, 91; 90, 98; 91, binary_operator:%; 91, 92; 91, 93; 92, string:"%s_SETTINGS_LOCATION"; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:app_name; 96, identifier:upper; 97, argument_list; 98, None; 99, if_statement; 99, 100; 99, 102; 100, not_operator; 100, 101; 101, identifier:location; 102, block; 102, 103; 102, 113; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:location; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:os; 109, identifier:getenv; 110, argument_list; 110, 111; 110, 112; 111, string:"SETTINGS_LOCATION"; 112, None; 113, if_statement; 113, 114; 113, 115; 114, identifier:location; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:location; 119, binary_operator:%; 119, 120; 119, 121; 120, string:"%s/%s"; 121, tuple; 121, 122; 121, 123; 122, identifier:location; 123, identifier:app_name; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:config; 127, dictionary; 128, for_statement; 128, 129; 128, 130; 128, 131; 129, identifier:confname; 130, identifier:confnames; 131, block; 131, 132; 131, 136; 131, 242; 131, 243; 131, 287; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:content; 135, None; 136, if_statement; 136, 137; 136, 145; 137, boolean_operator:and; 137, 138; 137, 139; 138, identifier:location; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:location; 142, identifier:startswith; 143, argument_list; 143, 144; 144, string:'s3://'; 145, block; 145, 146; 146, try_statement; 146, 147; 146, 238; 147, block; 147, 148; 147, 151; 147, 165; 148, import_statement; 148, 149; 149, dotted_name; 149, 150; 150, identifier:boto; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 157; 153, pattern_list; 153, 154; 153, 155; 153, 156; 154, identifier:_; 155, identifier:bucket_name; 156, identifier:prefix; 157, subscript; 157, 158; 157, 162; 158, call; 158, 159; 158, 160; 159, identifier:urlparse; 160, argument_list; 160, 161; 161, identifier:location; 162, slice; 162, 163; 162, 164; 163, colon; 164, integer:3; 165, try_statement; 165, 166; 165, 221; 166, block; 166, 167; 166, 175; 166, 184; 166, 192; 166, 201; 166, 209; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:conn; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:boto; 173, identifier:connect_s3; 174, argument_list; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:bucket; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:conn; 181, identifier:get_bucket; 182, argument_list; 182, 183; 183, identifier:bucket_name; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:key_name; 187, binary_operator:%; 187, 188; 187, 189; 188, string:'%s/%s'; 189, tuple; 189, 190; 189, 191; 190, identifier:prefix; 191, identifier:confname; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:key; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:bucket; 198, identifier:get_key; 199, argument_list; 199, 200; 200, identifier:key_name; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:content; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:key; 207, identifier:get_contents_as_string; 208, argument_list; 209, if_statement; 209, 210; 209, 211; 210, identifier:verbose; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:LOGGER; 216, identifier:info; 217, argument_list; 217, 218; 217, 219; 217, 220; 218, string:"config loaded from 's3://%s/%s'"; 219, identifier:bucket_name; 220, identifier:key_name; 221, except_clause; 221, 222; 221, 236; 222, as_pattern; 222, 223; 222, 234; 223, tuple; 223, 224; 223, 229; 224, attribute; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:boto; 227, identifier:exception; 228, identifier:NoAuthHandlerFound; 229, attribute; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:boto; 232, identifier:exception; 233, identifier:S3ResponseError; 234, as_pattern_target; 234, 235; 235, identifier:_; 236, block; 236, 237; 237, pass_statement; 238, except_clause; 238, 239; 238, 240; 239, identifier:ImportError; 240, block; 240, 241; 241, pass_statement; 242, comment; 243, if_statement; 243, 244; 243, 246; 244, not_operator; 244, 245; 245, identifier:content; 246, block; 246, 247; 246, 264; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:confpath; 250, call; 250, 251; 250, 252; 251, identifier:locate_config; 252, argument_list; 252, 253; 252, 254; 252, 255; 252, 258; 252, 261; 253, identifier:confname; 254, identifier:app_name; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:location; 257, identifier:location; 258, keyword_argument; 258, 259; 258, 260; 259, identifier:prefix; 260, identifier:prefix; 261, keyword_argument; 261, 262; 261, 263; 262, identifier:verbose; 263, identifier:verbose; 264, if_statement; 264, 265; 264, 266; 265, identifier:confpath; 266, block; 266, 267; 267, with_statement; 267, 268; 267, 278; 268, with_clause; 268, 269; 269, with_item; 269, 270; 270, as_pattern; 270, 271; 270, 276; 271, call; 271, 272; 271, 273; 272, identifier:open; 273, argument_list; 273, 274; 273, 275; 274, identifier:confpath; 275, string:'rb'; 276, as_pattern_target; 276, 277; 277, identifier:conffile; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:content; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:conffile; 285, identifier:read; 286, argument_list; 287, if_statement; 287, 288; 287, 289; 288, identifier:content; 289, block; 289, 290; 289, 303; 289, 319; 290, if_statement; 290, 291; 290, 292; 291, identifier:passphrase; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:content; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:crypt; 299, identifier:decrypt; 300, argument_list; 300, 301; 300, 302; 301, identifier:content; 302, identifier:passphrase; 303, if_statement; 303, 304; 303, 309; 304, call; 304, 305; 304, 306; 305, identifier:hasattr; 306, argument_list; 306, 307; 306, 308; 307, identifier:content; 308, string:'decode'; 309, block; 309, 310; 310, expression_statement; 310, 311; 311, assignment; 311, 312; 311, 313; 312, identifier:content; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:content; 316, identifier:decode; 317, argument_list; 317, 318; 318, string:'utf-8'; 319, for_statement; 319, 320; 319, 321; 319, 327; 320, identifier:line; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:content; 324, identifier:split; 325, argument_list; 325, 326; 326, string:'\n'; 327, block; 327, 328; 328, if_statement; 328, 329; 328, 336; 329, not_operator; 329, 330; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:line; 333, identifier:startswith; 334, argument_list; 334, 335; 335, string:'#'; 336, block; 336, 337; 336, 347; 337, expression_statement; 337, 338; 338, assignment; 338, 339; 338, 340; 339, identifier:look; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:re; 343, identifier:match; 344, argument_list; 344, 345; 344, 346; 345, string:r'(\w+)\s*=\s*(.*)'; 346, identifier:line; 347, if_statement; 347, 348; 347, 349; 348, identifier:look; 349, block; 349, 350; 350, try_statement; 350, 351; 350, 352; 350, 353; 350, 354; 350, 355; 350, 356; 350, 357; 350, 358; 350, 359; 350, 360; 350, 361; 350, 362; 350, 363; 350, 393; 351, comment; 352, comment; 353, comment; 354, comment; 355, comment; 356, comment; 357, comment; 358, comment; 359, comment; 360, comment; 361, comment; 362, comment; 363, block; 363, 364; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:config; 368, identifier:update; 369, argument_list; 369, 370; 370, dictionary; 370, 371; 371, pair; 371, 372; 371, 382; 372, call; 372, 373; 372, 381; 373, attribute; 373, 374; 373, 380; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:look; 377, identifier:group; 378, argument_list; 378, 379; 379, integer:1; 380, identifier:upper; 381, argument_list; 382, call; 382, 383; 382, 384; 383, identifier:eval; 384, argument_list; 384, 385; 384, 391; 384, 392; 385, call; 385, 386; 385, 389; 386, attribute; 386, 387; 386, 388; 387, identifier:look; 388, identifier:group; 389, argument_list; 389, 390; 390, integer:2; 391, dictionary; 392, dictionary; 393, except_clause; 393, 394; 393, 395; 394, identifier:Exception; 395, block; 395, 396; 396, raise_statement; 397, return_statement; 397, 398; 398, identifier:config | def load_config(app_name, *args, **kwargs):
"""
Given a path to a file, parse its lines in ini-like format, and then
set them in the current namespace.
Quiet by default. Set verbose to True to see the absolute path to the config
files printed on stderr.
"""
configure_logging()
# compatible with Python 2 and 3.
prefix = kwargs.get('prefix', 'etc')
verbose = kwargs.get('verbose', False)
location = kwargs.get('location', None)
passphrase = kwargs.get('passphrase',
os.getenv("%s_SETTINGS_CRYPT_KEY" % app_name.upper(),
os.getenv("SETTINGS_CRYPT_KEY", None)))
confnames = args
if not location:
location = os.getenv("%s_SETTINGS_LOCATION" % app_name.upper(), None)
if not location:
location = os.getenv("SETTINGS_LOCATION", None)
if location:
location = "%s/%s" % (location, app_name)
config = {}
for confname in confnames:
content = None
if location and location.startswith('s3://'):
try:
import boto
_, bucket_name, prefix = urlparse(location)[:3]
try:
conn = boto.connect_s3()
bucket = conn.get_bucket(bucket_name)
key_name = '%s/%s' % (prefix, confname)
key = bucket.get_key(key_name)
content = key.get_contents_as_string()
if verbose:
LOGGER.info("config loaded from 's3://%s/%s'",
bucket_name, key_name)
except (boto.exception.NoAuthHandlerFound,
boto.exception.S3ResponseError) as _:
pass
except ImportError:
pass
# We cannot find a deployutils S3 bucket. Let's look on the filesystem.
if not content:
confpath = locate_config(
confname, app_name, location=location,
prefix=prefix, verbose=verbose)
if confpath:
with open(confpath, 'rb') as conffile:
content = conffile.read()
if content:
if passphrase:
content = crypt.decrypt(content, passphrase)
if hasattr(content, 'decode'):
content = content.decode('utf-8')
for line in content.split('\n'):
if not line.startswith('#'):
look = re.match(r'(\w+)\s*=\s*(.*)', line)
if look:
try:
# We used to parse the file line by line.
# Once Django 1.5 introduced ALLOWED_HOSTS
# (a tuple that definitely belongs to the site.conf
# set), we had no choice other than resort
# to eval(value, {}, {}).
# We are not resorting to import conf module yet
# but that might be necessary once we use
# dictionary configs for some of the apps...
# TODO: consider using something like ConfigObj
# for this:
# http://www.voidspace.org.uk/python/configobj.html
#pylint:disable=eval-used
config.update({look.group(1).upper():
eval(look.group(2), {}, {})})
except Exception:
raise
return config |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:predict; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:tree; 5, identifier:x; 6, default_parameter; 6, 7; 6, 8; 7, identifier:y; 8, list:[]; 9, default_parameter; 9, 10; 9, 11; 10, identifier:dist; 11, False; 12, block; 12, 13; 12, 15; 12, 16; 12, 20; 12, 21; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:node_id; 19, integer:1; 20, comment; 21, while_statement; 21, 22; 21, 23; 22, integer:1; 23, block; 23, 24; 23, 30; 23, 223; 23, 234; 23, 278; 23, 298; 23, 305; 23, 360; 23, 408; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:nodes; 27, subscript; 27, 28; 27, 29; 28, identifier:tree; 29, identifier:node_id; 30, if_statement; 30, 31; 30, 38; 30, 82; 31, comparison_operator:==; 31, 32; 31, 37; 32, subscript; 32, 33; 32, 36; 33, subscript; 33, 34; 33, 35; 34, identifier:nodes; 35, integer:0; 36, integer:5; 37, string:"c"; 38, block; 38, 39; 39, if_statement; 39, 40; 39, 53; 39, 67; 40, comparison_operator:<=; 40, 41; 40, 48; 41, subscript; 41, 42; 41, 43; 42, identifier:x; 43, subscript; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:nodes; 46, integer:0; 47, integer:1; 48, subscript; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:nodes; 51, integer:0; 52, integer:2; 53, block; 53, 54; 53, 66; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, pattern_list; 56, 57; 56, 58; 57, identifier:index; 58, identifier:node_id; 59, expression_list; 59, 60; 59, 61; 60, integer:0; 61, subscript; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:nodes; 64, integer:0; 65, integer:0; 66, comment; 67, else_clause; 67, 68; 68, block; 68, 69; 68, 81; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, pattern_list; 71, 72; 71, 73; 72, identifier:index; 73, identifier:node_id; 74, expression_list; 74, 75; 74, 76; 75, integer:1; 76, subscript; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:nodes; 79, integer:1; 80, integer:0; 81, comment; 82, else_clause; 82, 83; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 98; 84, 112; 84, 140; 85, comparison_operator:in; 85, 86; 85, 93; 86, subscript; 86, 87; 86, 88; 87, identifier:x; 88, subscript; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:nodes; 91, integer:0; 92, integer:1; 93, subscript; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:nodes; 96, integer:0; 97, integer:2; 98, block; 98, 99; 98, 111; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 104; 101, pattern_list; 101, 102; 101, 103; 102, identifier:index; 103, identifier:node_id; 104, expression_list; 104, 105; 104, 106; 105, integer:0; 106, subscript; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:nodes; 109, integer:0; 110, integer:0; 111, comment; 112, elif_clause; 112, 113; 112, 126; 113, comparison_operator:in; 113, 114; 113, 121; 114, subscript; 114, 115; 114, 116; 115, identifier:x; 116, subscript; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:nodes; 119, integer:1; 120, integer:1; 121, subscript; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:nodes; 124, integer:1; 125, integer:2; 126, block; 126, 127; 126, 139; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 132; 129, pattern_list; 129, 130; 129, 131; 130, identifier:index; 131, identifier:node_id; 132, expression_list; 132, 133; 132, 134; 133, integer:1; 134, subscript; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:nodes; 137, integer:1; 138, integer:0; 139, comment; 140, else_clause; 140, 141; 140, 142; 140, 143; 141, comment; 142, comment; 143, block; 143, 144; 143, 166; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:node_id; 147, binary_operator:+; 147, 148; 147, 158; 148, binary_operator:+; 148, 149; 148, 157; 149, call; 149, 150; 149, 151; 150, identifier:str; 151, argument_list; 151, 152; 152, subscript; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:nodes; 155, integer:0; 156, integer:0; 157, string:","; 158, call; 158, 159; 158, 160; 159, identifier:str; 160, argument_list; 160, 161; 161, subscript; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:nodes; 164, integer:1; 165, integer:0; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 171; 168, pattern_list; 168, 169; 168, 170; 169, identifier:index; 170, identifier:nodes; 171, expression_list; 171, 172; 171, 173; 172, integer:0; 173, list:[[0, 0, 0, {k: nodes[0][3].get(k, 0) + nodes[1][3].get(k, 0) for k in
set(nodes[0][3]) | set(nodes[1][3])}]]; 173, 174; 174, list:[0, 0, 0, {k: nodes[0][3].get(k, 0) + nodes[1][3].get(k, 0) for k in
set(nodes[0][3]) | set(nodes[1][3])}]; 174, 175; 174, 176; 174, 177; 174, 178; 175, integer:0; 176, integer:0; 177, integer:0; 178, dictionary_comprehension; 178, 179; 178, 204; 179, pair; 179, 180; 179, 181; 180, identifier:k; 181, binary_operator:+; 181, 182; 181, 193; 182, call; 182, 183; 182, 190; 183, attribute; 183, 184; 183, 189; 184, subscript; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:nodes; 187, integer:0; 188, integer:3; 189, identifier:get; 190, argument_list; 190, 191; 190, 192; 191, identifier:k; 192, integer:0; 193, call; 193, 194; 193, 201; 194, attribute; 194, 195; 194, 200; 195, subscript; 195, 196; 195, 199; 196, subscript; 196, 197; 196, 198; 197, identifier:nodes; 198, integer:1; 199, integer:3; 200, identifier:get; 201, argument_list; 201, 202; 201, 203; 202, identifier:k; 203, integer:0; 204, for_in_clause; 204, 205; 204, 206; 205, identifier:k; 206, binary_operator:|; 206, 207; 206, 215; 207, call; 207, 208; 207, 209; 208, identifier:set; 209, argument_list; 209, 210; 210, subscript; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:nodes; 213, integer:0; 214, integer:3; 215, call; 215, 216; 215, 217; 216, identifier:set; 217, argument_list; 217, 218; 218, subscript; 218, 219; 218, 222; 219, subscript; 219, 220; 219, 221; 220, identifier:nodes; 221, integer:1; 222, integer:3; 223, if_statement; 223, 224; 223, 231; 223, 232; 224, comparison_operator:in; 224, 225; 224, 226; 225, identifier:node_id; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:tree; 229, identifier:keys; 230, argument_list; 231, comment; 232, block; 232, 233; 233, continue_statement; 234, if_statement; 234, 235; 234, 236; 235, identifier:dist; 236, block; 236, 237; 236, 252; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:suma; 240, call; 240, 241; 240, 242; 241, identifier:sum; 242, argument_list; 242, 243; 243, call; 243, 244; 243, 251; 244, attribute; 244, 245; 244, 250; 245, subscript; 245, 246; 245, 249; 246, subscript; 246, 247; 246, 248; 247, identifier:nodes; 248, identifier:index; 249, integer:3; 250, identifier:values; 251, argument_list; 252, return_statement; 252, 253; 253, call; 253, 254; 253, 255; 254, identifier:Counter; 255, argument_list; 255, 256; 256, dictionary_comprehension; 256, 257; 256, 265; 257, pair; 257, 258; 257, 259; 258, identifier:k; 259, binary_operator:/; 259, 260; 259, 261; 260, identifier:v; 261, call; 261, 262; 261, 263; 262, identifier:float; 263, argument_list; 263, 264; 264, identifier:suma; 265, for_in_clause; 265, 266; 265, 269; 266, pattern_list; 266, 267; 266, 268; 267, identifier:k; 268, identifier:v; 269, call; 269, 270; 269, 277; 270, attribute; 270, 271; 270, 276; 271, subscript; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:nodes; 274, identifier:index; 275, integer:3; 276, identifier:iteritems; 277, argument_list; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:prediction; 281, call; 281, 282; 281, 283; 282, identifier:max; 283, argument_list; 283, 284; 283, 289; 284, subscript; 284, 285; 284, 288; 285, subscript; 285, 286; 285, 287; 286, identifier:nodes; 287, identifier:index; 288, integer:3; 289, keyword_argument; 289, 290; 289, 291; 290, identifier:key; 291, attribute; 291, 292; 291, 297; 292, subscript; 292, 293; 292, 296; 293, subscript; 293, 294; 293, 295; 294, identifier:nodes; 295, identifier:index; 296, integer:3; 297, identifier:get; 298, if_statement; 298, 299; 298, 302; 299, comparison_operator:==; 299, 300; 299, 301; 300, identifier:y; 301, list:[]; 302, block; 302, 303; 303, return_statement; 303, 304; 304, identifier:prediction; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:probs; 308, call; 308, 309; 308, 310; 309, identifier:sorted; 310, argument_list; 310, 311; 310, 351; 310, 357; 311, call; 311, 312; 311, 313; 312, identifier:zip; 313, argument_list; 313, 314; 313, 323; 314, call; 314, 315; 314, 322; 315, attribute; 315, 316; 315, 321; 316, subscript; 316, 317; 316, 320; 317, subscript; 317, 318; 317, 319; 318, identifier:nodes; 319, identifier:index; 320, integer:3; 321, identifier:keys; 322, argument_list; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:np; 326, identifier:true_divide; 327, argument_list; 327, 328; 327, 337; 328, call; 328, 329; 328, 336; 329, attribute; 329, 330; 329, 335; 330, subscript; 330, 331; 330, 334; 331, subscript; 331, 332; 331, 333; 332, identifier:nodes; 333, identifier:index; 334, integer:3; 335, identifier:values; 336, argument_list; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:np; 340, identifier:sum; 341, argument_list; 341, 342; 342, call; 342, 343; 342, 350; 343, attribute; 343, 344; 343, 349; 344, subscript; 344, 345; 344, 348; 345, subscript; 345, 346; 345, 347; 346, identifier:nodes; 347, identifier:index; 348, integer:3; 349, identifier:values; 350, argument_list; 351, keyword_argument; 351, 352; 351, 353; 352, identifier:key; 353, call; 353, 354; 353, 355; 354, identifier:itemgetter; 355, argument_list; 355, 356; 356, integer:1; 357, keyword_argument; 357, 358; 357, 359; 358, identifier:reverse; 359, True; 360, if_statement; 360, 361; 360, 364; 360, 387; 361, comparison_operator:==; 361, 362; 361, 363; 362, identifier:prediction; 363, identifier:y; 364, block; 364, 365; 365, expression_statement; 365, 366; 366, assignment; 366, 367; 366, 368; 367, identifier:margin; 368, conditional_expression:if; 368, 369; 368, 380; 368, 386; 369, binary_operator:-; 369, 370; 369, 375; 370, subscript; 370, 371; 370, 374; 371, subscript; 371, 372; 371, 373; 372, identifier:probs; 373, integer:0; 374, integer:1; 375, subscript; 375, 376; 375, 379; 376, subscript; 376, 377; 376, 378; 377, identifier:probs; 378, integer:1; 379, integer:1; 380, comparison_operator:>; 380, 381; 380, 385; 381, call; 381, 382; 381, 383; 382, identifier:len; 383, argument_list; 383, 384; 384, identifier:probs; 385, integer:1; 386, integer:1; 387, else_clause; 387, 388; 388, block; 388, 389; 389, expression_statement; 389, 390; 390, assignment; 390, 391; 390, 392; 391, identifier:margin; 392, binary_operator:-; 392, 393; 392, 403; 393, call; 393, 394; 393, 400; 394, attribute; 394, 395; 394, 399; 395, call; 395, 396; 395, 397; 396, identifier:dict; 397, argument_list; 397, 398; 398, identifier:probs; 399, identifier:get; 400, argument_list; 400, 401; 400, 402; 401, identifier:y; 402, integer:0; 403, subscript; 403, 404; 403, 407; 404, subscript; 404, 405; 404, 406; 405, identifier:probs; 406, integer:0; 407, integer:1; 408, return_statement; 408, 409; 409, expression_list; 409, 410; 409, 411; 410, identifier:node_id; 411, identifier:margin | def predict(tree, x, y=[], dist=False):
"""
Function makes a prediction of one sample with a tree model. If y label is defined it returns node identifier and margin.
tree: dictionary - tree model
x: numpy array - one sample from the dataset
y: string, integer or float - sample label
"""
# conditions of continuous and discrete features
node_id = 1 # initialize node identifier as first node under the root
while 1:
nodes = tree[node_id]
if nodes[0][5] == "c":
if x[nodes[0][1]] <= nodes[0][2]:
index, node_id = 0, nodes[0][0] # set identifier of child node
else:
index, node_id = 1, nodes[1][0] # set identifier of child node
else:
if x[nodes[0][1]] in nodes[0][2]:
index, node_id = 0, nodes[0][0] # set identifier of child node
elif x[nodes[1][1]] in nodes[1][2]:
index, node_id = 1, nodes[1][0] # set identifier of child node
else:
# value is not in left or right branch. Get label distributions of left and right child
# sum labels distribution to get parent label distribution
node_id = str(nodes[0][0]) + "," + str(nodes[1][0])
index, nodes = 0, [[0, 0, 0, {k: nodes[0][3].get(k, 0) + nodes[1][3].get(k, 0) for k in
set(nodes[0][3]) | set(nodes[1][3])}]]
if node_id in tree.keys(): # check if tree can be traversed further
continue
if dist:
suma = sum(nodes[index][3].values())
return Counter({k: v / float(suma) for k, v in nodes[index][3].iteritems()})
prediction = max(nodes[index][3], key=nodes[index][3].get)
if y == []:
return prediction
probs = sorted(
zip(nodes[index][3].keys(), np.true_divide(nodes[index][3].values(), np.sum(nodes[index][3].values()))),
key=itemgetter(1), reverse=True)
if prediction == y:
margin = probs[0][1] - probs[1][1] if len(probs) > 1 else 1
else:
margin = dict(probs).get(y, 0) - probs[0][1]
return node_id, margin |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 11; 2, function_name:pay_with_account_credit_cards; 3, parameters; 3, 4; 4, identifier:invoice_id; 5, type; 5, 6; 6, generic_type; 6, 7; 6, 8; 7, identifier:Optional; 8, type_parameter; 8, 9; 9, type; 9, 10; 10, identifier:Transaction; 11, block; 11, 12; 11, 14; 11, 24; 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:'invoice-payment-started'; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:invoice_id; 23, identifier:invoice_id; 24, with_statement; 24, 25; 24, 32; 25, with_clause; 25, 26; 26, with_item; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:transaction; 30, identifier:atomic; 31, argument_list; 32, block; 32, 33; 32, 50; 32, 51; 32, 52; 32, 53; 32, 71; 32, 72; 32, 73; 32, 74; 32, 86; 32, 99; 32, 112; 32, 118; 32, 130; 32, 131; 32, 132; 32, 133; 32, 157; 32, 166; 32, 300; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:invoice; 36, call; 36, 37; 36, 46; 37, attribute; 37, 38; 37, 45; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:Invoice; 42, identifier:objects; 43, identifier:select_for_update; 44, argument_list; 45, identifier:get; 46, argument_list; 46, 47; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:pk; 49, identifier:invoice_id; 50, comment; 51, comment; 52, comment; 53, if_statement; 53, 54; 53, 58; 54, not_operator; 54, 55; 55, attribute; 55, 56; 55, 57; 56, identifier:invoice; 57, identifier:in_payable_state; 58, block; 58, 59; 59, raise_statement; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:PreconditionError; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, string:'Cannot pay invoice with status {}.'; 66, identifier:format; 67, argument_list; 67, 68; 68, attribute; 68, 69; 68, 70; 69, identifier:invoice; 70, identifier:status; 71, comment; 72, comment; 73, comment; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:due; 77, call; 77, 78; 77, 85; 78, attribute; 78, 79; 78, 84; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:invoice; 82, identifier:due; 83, argument_list; 84, identifier:monies; 85, argument_list; 86, if_statement; 86, 87; 86, 93; 87, comparison_operator:==; 87, 88; 87, 92; 88, call; 88, 89; 88, 90; 89, identifier:len; 90, argument_list; 90, 91; 91, identifier:due; 92, integer:0; 93, block; 93, 94; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:PreconditionError; 97, argument_list; 97, 98; 98, string:'Cannot pay empty invoice.'; 99, if_statement; 99, 100; 99, 106; 100, comparison_operator:>; 100, 101; 100, 105; 101, call; 101, 102; 101, 103; 102, identifier:len; 103, argument_list; 103, 104; 104, identifier:due; 105, integer:1; 106, block; 106, 107; 107, raise_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:PreconditionError; 110, argument_list; 110, 111; 111, string:'Cannot pay invoice with more than one currency.'; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:amount; 115, subscript; 115, 116; 115, 117; 116, identifier:due; 117, integer:0; 118, if_statement; 118, 119; 118, 124; 119, comparison_operator:<=; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:amount; 122, identifier:amount; 123, integer:0; 124, block; 124, 125; 125, raise_statement; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:PreconditionError; 128, argument_list; 128, 129; 129, string:'Cannot pay invoice with non-positive amount.'; 130, comment; 131, comment; 132, comment; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:valid_credit_cards; 136, call; 136, 137; 136, 155; 137, attribute; 137, 138; 137, 154; 138, call; 138, 139; 138, 148; 139, attribute; 139, 140; 139, 147; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:CreditCard; 144, identifier:objects; 145, identifier:valid; 146, argument_list; 147, identifier:filter; 148, argument_list; 148, 149; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:account; 151, attribute; 151, 152; 151, 153; 152, identifier:invoice; 153, identifier:account; 154, identifier:order_by; 155, argument_list; 155, 156; 156, string:'status'; 157, if_statement; 157, 158; 157, 160; 158, not_operator; 158, 159; 159, identifier:valid_credit_cards; 160, block; 160, 161; 161, raise_statement; 161, 162; 162, call; 162, 163; 162, 164; 163, identifier:PreconditionError; 164, argument_list; 164, 165; 165, string:'No valid credit card on account.'; 166, for_statement; 166, 167; 166, 168; 166, 169; 167, identifier:credit_card; 168, identifier:valid_credit_cards; 169, block; 169, 170; 170, try_statement; 170, 171; 170, 278; 171, block; 171, 172; 171, 196; 171, 233; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 177; 174, pattern_list; 174, 175; 174, 176; 175, identifier:success; 176, identifier:payment_psp_object; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:psp; 180, identifier:charge_credit_card; 181, argument_list; 181, 182; 181, 187; 181, 190; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:credit_card_psp_object; 184, attribute; 184, 185; 184, 186; 185, identifier:credit_card; 186, identifier:psp_object; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:amount; 189, identifier:amount; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:client_ref; 192, call; 192, 193; 192, 194; 193, identifier:str; 194, argument_list; 194, 195; 195, identifier:invoice_id; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:payment; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:Transaction; 203, identifier:objects; 204, identifier:create; 205, argument_list; 205, 206; 205, 211; 205, 214; 205, 217; 205, 220; 205, 225; 205, 230; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:account; 208, attribute; 208, 209; 208, 210; 209, identifier:invoice; 210, identifier:account; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:invoice; 213, identifier:invoice; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:amount; 216, identifier:amount; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:success; 219, identifier:success; 220, keyword_argument; 220, 221; 220, 222; 221, identifier:payment_method; 222, attribute; 222, 223; 222, 224; 223, identifier:credit_card; 224, identifier:type; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:credit_card_number; 227, attribute; 227, 228; 227, 229; 228, identifier:credit_card; 229, identifier:number; 230, keyword_argument; 230, 231; 230, 232; 231, identifier:psp_object; 232, identifier:payment_psp_object; 233, if_statement; 233, 234; 233, 235; 233, 263; 234, identifier:success; 235, block; 235, 236; 235, 242; 235, 248; 235, 261; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:invoice; 240, identifier:pay; 241, argument_list; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:invoice; 246, identifier:save; 247, argument_list; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:logger; 252, identifier:info; 253, argument_list; 253, 254; 253, 255; 253, 258; 254, string:'invoice-payment-success'; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:invoice; 257, identifier:invoice_id; 258, keyword_argument; 258, 259; 258, 260; 259, identifier:payment; 260, identifier:payment; 261, return_statement; 261, 262; 262, identifier:payment; 263, else_clause; 263, 264; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:logger; 269, identifier:info; 270, argument_list; 270, 271; 270, 272; 270, 275; 271, string:'invoice-payment-failure'; 272, keyword_argument; 272, 273; 272, 274; 273, identifier:invoice; 274, identifier:invoice_id; 275, keyword_argument; 275, 276; 275, 277; 276, identifier:payment; 277, identifier:payment; 278, except_clause; 278, 279; 278, 283; 279, as_pattern; 279, 280; 279, 281; 280, identifier:Exception; 281, as_pattern_target; 281, 282; 282, identifier:e; 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; 289, 291; 289, 294; 289, 297; 290, string:'invoice-payment-error'; 291, keyword_argument; 291, 292; 291, 293; 292, identifier:invoice_id; 293, identifier:invoice_id; 294, keyword_argument; 294, 295; 294, 296; 295, identifier:credit_card; 296, identifier:credit_card; 297, keyword_argument; 297, 298; 297, 299; 298, identifier:exc_info; 299, identifier:e; 300, return_statement; 300, 301; 301, None | def pay_with_account_credit_cards(invoice_id) -> Optional[Transaction]:
"""
Get paid for the invoice, trying the valid credit cards on record for the account.
If successful attaches the payment to the invoice and marks the invoice as paid.
:param invoice_id: the id of the invoice to pay.
:return: A successful transaction, or None if we weren't able to pay the invoice.
"""
logger.debug('invoice-payment-started', invoice_id=invoice_id)
with transaction.atomic():
invoice = Invoice.objects.select_for_update().get(pk=invoice_id)
#
# Precondition: Invoice should be in a state that allows payment
#
if not invoice.in_payable_state:
raise PreconditionError('Cannot pay invoice with status {}.'.format(invoice.status))
#
# Precondition: The due amount must be positive, in a single currency
#
due = invoice.due().monies()
if len(due) == 0:
raise PreconditionError('Cannot pay empty invoice.')
if len(due) > 1:
raise PreconditionError('Cannot pay invoice with more than one currency.')
amount = due[0]
if amount.amount <= 0:
raise PreconditionError('Cannot pay invoice with non-positive amount.')
#
# Try valid credit cards until one works. Start with the active ones
#
valid_credit_cards = CreditCard.objects.valid().filter(account=invoice.account).order_by('status')
if not valid_credit_cards:
raise PreconditionError('No valid credit card on account.')
for credit_card in valid_credit_cards:
try:
success, payment_psp_object = psp.charge_credit_card(
credit_card_psp_object=credit_card.psp_object,
amount=amount,
client_ref=str(invoice_id))
payment = Transaction.objects.create(
account=invoice.account,
invoice=invoice,
amount=amount,
success=success,
payment_method=credit_card.type,
credit_card_number=credit_card.number,
psp_object=payment_psp_object)
if success:
invoice.pay()
invoice.save()
logger.info('invoice-payment-success', invoice=invoice_id, payment=payment)
return payment
else:
logger.info('invoice-payment-failure', invoice=invoice_id, payment=payment)
except Exception as e:
logger.error('invoice-payment-error', invoice_id=invoice_id, credit_card=credit_card, exc_info=e)
return None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:handle_django_settings; 3, parameters; 3, 4; 4, identifier:filename; 5, block; 5, 6; 5, 8; 5, 17; 5, 28; 5, 39; 5, 47; 5, 66; 5, 83; 5, 90; 5, 99; 5, 107; 5, 131; 5, 148; 5, 154; 5, 160; 5, 166; 5, 295; 5, 309; 5, 422; 5, 423; 5, 432; 5, 439; 5, 461; 6, expression_statement; 6, 7; 7, string:'''Attempts to load a Django project and get package dependencies from
settings.
Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
the other versions.
'''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:old_sys_path; 11, subscript; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:sys; 14, identifier:path; 15, slice; 15, 16; 16, colon; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:dirpath; 20, call; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:os; 24, identifier:path; 25, identifier:dirname; 26, argument_list; 26, 27; 27, identifier:filename; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:project; 31, call; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:os; 35, identifier:path; 36, identifier:basename; 37, argument_list; 37, 38; 38, identifier:dirpath; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:cwd; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:os; 45, identifier:getcwd; 46, argument_list; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:project_path; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:os; 54, identifier:path; 55, identifier:normpath; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:os; 61, identifier:path; 62, identifier:join; 63, argument_list; 63, 64; 63, 65; 64, identifier:dirpath; 65, string:'..'; 66, if_statement; 66, 67; 66, 72; 67, comparison_operator:not; 67, 68; 67, 69; 68, identifier:project_path; 69, attribute; 69, 70; 69, 71; 70, identifier:sys; 71, identifier:path; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:sys; 78, identifier:path; 79, identifier:insert; 80, argument_list; 80, 81; 80, 82; 81, integer:0; 82, identifier:project_path; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:os; 87, identifier:chdir; 88, argument_list; 88, 89; 89, identifier:project_path; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:project_settings; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, string:'{}.settings'; 96, identifier:format; 97, argument_list; 97, 98; 98, identifier:project; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 106; 101, subscript; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:os; 104, identifier:environ; 105, string:'DJANGO_SETTINGS_MODULE'; 106, identifier:project_settings; 107, try_statement; 107, 108; 107, 120; 108, block; 108, 109; 108, 112; 108, 113; 109, import_statement; 109, 110; 110, dotted_name; 110, 111; 111, identifier:django; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:django; 117, identifier:setup; 118, lambda; 118, 119; 119, False; 120, except_clause; 120, 121; 120, 122; 121, identifier:ImportError; 122, block; 122, 123; 122, 130; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:log; 127, identifier:error; 128, argument_list; 128, 129; 129, string:'Found Django settings, but Django is not installed.'; 130, return_statement; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:log; 135, identifier:warn; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, string:'Loading Django Settings (Using {}): {}'; 140, identifier:format; 141, argument_list; 141, 142; 141, 147; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:django; 145, identifier:get_version; 146, argument_list; 147, identifier:filename; 148, import_from_statement; 148, 149; 148, 152; 149, dotted_name; 149, 150; 149, 151; 150, identifier:django; 151, identifier:conf; 152, dotted_name; 152, 153; 153, identifier:LazySettings; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:installed_apps; 157, call; 157, 158; 157, 159; 158, identifier:set; 159, argument_list; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:settings_imports; 163, call; 163, 164; 163, 165; 164, identifier:set; 165, argument_list; 166, try_statement; 166, 167; 166, 270; 167, block; 167, 168; 167, 174; 167, 180; 167, 215; 167, 216; 167, 217; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:settings; 171, call; 171, 172; 171, 173; 172, identifier:LazySettings; 173, argument_list; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:settings; 178, identifier:_setup; 179, argument_list; 180, for_statement; 180, 181; 180, 184; 180, 194; 181, pattern_list; 181, 182; 181, 183; 182, identifier:k; 183, identifier:v; 184, call; 184, 185; 184, 193; 185, attribute; 185, 186; 185, 192; 186, call; 186, 187; 186, 188; 187, identifier:vars; 188, argument_list; 188, 189; 189, attribute; 189, 190; 189, 191; 190, identifier:settings; 191, identifier:_wrapped; 192, identifier:items; 193, argument_list; 194, block; 194, 195; 195, if_statement; 195, 196; 195, 207; 195, 208; 196, boolean_operator:and; 196, 197; 196, 200; 197, comparison_operator:not; 197, 198; 197, 199; 198, identifier:k; 199, identifier:_excluded_settings; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:re; 203, identifier:match; 204, argument_list; 204, 205; 204, 206; 205, string:r'^[A-Z_]+$'; 206, identifier:k; 207, comment; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 212; 211, identifier:scan_django_settings; 212, argument_list; 212, 213; 212, 214; 213, identifier:v; 214, identifier:settings_imports; 215, comment; 216, comment; 217, for_statement; 217, 218; 217, 219; 217, 225; 218, identifier:app; 219, call; 219, 220; 219, 221; 220, identifier:getattr; 221, argument_list; 221, 222; 221, 223; 221, 224; 222, identifier:settings; 223, string:'INSTALLED_APPS'; 224, list:[]; 225, block; 225, 226; 226, if_statement; 226, 227; 226, 238; 226, 261; 227, boolean_operator:and; 227, 228; 227, 233; 228, call; 228, 229; 228, 230; 229, identifier:hasattr; 230, argument_list; 230, 231; 230, 232; 231, identifier:app; 232, string:'__file__'; 233, call; 233, 234; 233, 235; 234, identifier:getattr; 235, argument_list; 235, 236; 235, 237; 236, identifier:app; 237, string:'__file__'; 238, block; 238, 239; 238, 254; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, pattern_list; 241, 242; 241, 243; 242, identifier:imp; 243, identifier:_; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:utils; 247, identifier:import_path_from_file; 248, argument_list; 248, 249; 249, call; 249, 250; 249, 251; 250, identifier:getattr; 251, argument_list; 251, 252; 251, 253; 252, identifier:app; 253, string:'__file__'; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:installed_apps; 258, identifier:add; 259, argument_list; 259, 260; 260, identifier:imp; 261, else_clause; 261, 262; 262, block; 262, 263; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:installed_apps; 267, identifier:add; 268, argument_list; 268, 269; 269, identifier:app; 270, except_clause; 270, 271; 270, 275; 271, as_pattern; 271, 272; 271, 273; 272, identifier:Exception; 273, as_pattern_target; 273, 274; 274, identifier:e; 275, block; 275, 276; 275, 284; 275, 294; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:log; 280, identifier:error; 281, argument_list; 281, 282; 281, 283; 282, string:'Could not load Django settings: %s'; 283, identifier:e; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:log; 288, identifier:debug; 289, argument_list; 289, 290; 289, 291; 290, string:''; 291, keyword_argument; 291, 292; 291, 293; 292, identifier:exc_info; 293, True; 294, return_statement; 295, if_statement; 295, 296; 295, 301; 296, boolean_operator:or; 296, 297; 296, 299; 297, not_operator; 297, 298; 298, identifier:installed_apps; 299, not_operator; 299, 300; 300, identifier:settings_imports; 301, block; 301, 302; 302, expression_statement; 302, 303; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:log; 306, identifier:error; 307, argument_list; 307, 308; 308, string:'Got empty settings values from Django settings.'; 309, try_statement; 309, 310; 309, 404; 310, block; 310, 311; 310, 322; 310, 323; 310, 324; 310, 346; 310, 354; 311, import_from_statement; 311, 312; 311, 316; 311, 318; 311, 320; 312, dotted_name; 312, 313; 312, 314; 312, 315; 313, identifier:django; 314, identifier:apps; 315, identifier:registry; 316, dotted_name; 316, 317; 317, identifier:apps; 318, dotted_name; 318, 319; 319, identifier:Apps; 320, dotted_name; 320, 321; 321, identifier:AppRegistryNotReady; 322, comment; 323, comment; 324, if_statement; 324, 325; 324, 329; 324, 337; 325, not_operator; 325, 326; 326, attribute; 326, 327; 326, 328; 327, identifier:apps; 328, identifier:apps_ready; 329, block; 329, 330; 330, expression_statement; 330, 331; 331, call; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:apps; 334, identifier:populate; 335, argument_list; 335, 336; 336, identifier:installed_apps; 337, else_clause; 337, 338; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:apps; 342, call; 342, 343; 342, 344; 343, identifier:Apps; 344, argument_list; 344, 345; 345, identifier:installed_apps; 346, expression_statement; 346, 347; 347, assignment; 347, 348; 347, 349; 348, identifier:start; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:time; 352, identifier:time; 353, argument_list; 354, while_statement; 354, 355; 354, 356; 355, True; 356, block; 356, 357; 356, 403; 357, try_statement; 357, 358; 357, 376; 358, block; 358, 359; 359, for_statement; 359, 360; 359, 361; 359, 366; 360, identifier:app; 361, call; 361, 362; 361, 365; 362, attribute; 362, 363; 362, 364; 363, identifier:apps; 364, identifier:get_app_configs; 365, argument_list; 366, block; 366, 367; 367, expression_statement; 367, 368; 368, call; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:installed_apps; 371, identifier:add; 372, argument_list; 372, 373; 373, attribute; 373, 374; 373, 375; 374, identifier:app; 375, identifier:name; 376, except_clause; 376, 377; 376, 378; 377, identifier:AppRegistryNotReady; 378, block; 378, 379; 378, 395; 378, 402; 379, if_statement; 379, 380; 379, 389; 380, comparison_operator:>; 380, 381; 380, 388; 381, binary_operator:-; 381, 382; 381, 387; 382, call; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, identifier:time; 385, identifier:time; 386, argument_list; 387, identifier:start; 388, integer:10; 389, block; 389, 390; 390, raise_statement; 390, 391; 391, call; 391, 392; 391, 393; 392, identifier:Exception; 393, argument_list; 393, 394; 394, string:'Bail out of waiting for Django'; 395, expression_statement; 395, 396; 396, call; 396, 397; 396, 400; 397, attribute; 397, 398; 397, 399; 398, identifier:log; 399, identifier:debug; 400, argument_list; 400, 401; 401, string:'Waiting for apps to load...'; 402, continue_statement; 403, break_statement; 404, except_clause; 404, 405; 404, 409; 405, as_pattern; 405, 406; 405, 407; 406, identifier:Exception; 407, as_pattern_target; 407, 408; 408, identifier:e; 409, block; 409, 410; 410, expression_statement; 410, 411; 411, call; 411, 412; 411, 415; 412, attribute; 412, 413; 412, 414; 413, identifier:log; 414, identifier:debug; 415, argument_list; 415, 416; 416, call; 416, 417; 416, 420; 417, attribute; 417, 418; 417, 419; 418, string:'Could not use AppConfig: {}'; 419, identifier:format; 420, argument_list; 420, 421; 421, identifier:e; 422, comment; 423, expression_statement; 423, 424; 424, assignment; 424, 425; 424, 431; 425, subscript; 425, 426; 425, 429; 426, attribute; 426, 427; 426, 428; 427, identifier:sys; 428, identifier:path; 429, slice; 429, 430; 430, colon; 431, identifier:old_sys_path; 432, expression_statement; 432, 433; 433, call; 433, 434; 433, 437; 434, attribute; 434, 435; 434, 436; 435, identifier:os; 436, identifier:chdir; 437, argument_list; 437, 438; 438, identifier:cwd; 439, for_statement; 439, 440; 439, 441; 439, 442; 440, identifier:item; 441, identifier:settings_imports; 442, block; 442, 443; 442, 452; 443, expression_statement; 443, 444; 444, assignment; 444, 445; 444, 446; 445, identifier:need_scan; 446, call; 446, 447; 446, 450; 447, attribute; 447, 448; 447, 449; 448, identifier:item; 449, identifier:startswith; 450, argument_list; 450, 451; 451, identifier:_filescan_modules; 452, expression_statement; 452, 453; 453, yield; 453, 454; 454, tuple; 454, 455; 454, 456; 454, 457; 455, string:'django'; 456, identifier:item; 457, conditional_expression:if; 457, 458; 457, 459; 457, 460; 458, identifier:project_path; 459, identifier:need_scan; 460, None; 461, for_statement; 461, 462; 461, 463; 461, 464; 462, identifier:app; 463, identifier:installed_apps; 464, block; 464, 465; 464, 474; 465, expression_statement; 465, 466; 466, assignment; 466, 467; 466, 468; 467, identifier:need_scan; 468, call; 468, 469; 468, 472; 469, attribute; 469, 470; 469, 471; 470, identifier:app; 471, identifier:startswith; 472, argument_list; 472, 473; 473, identifier:project; 474, expression_statement; 474, 475; 475, yield; 475, 476; 476, tuple; 476, 477; 476, 478; 476, 479; 477, string:'django'; 478, identifier:app; 479, conditional_expression:if; 479, 480; 479, 481; 479, 482; 480, identifier:project_path; 481, identifier:need_scan; 482, None | def handle_django_settings(filename):
'''Attempts to load a Django project and get package dependencies from
settings.
Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
the other versions.
'''
old_sys_path = sys.path[:]
dirpath = os.path.dirname(filename)
project = os.path.basename(dirpath)
cwd = os.getcwd()
project_path = os.path.normpath(os.path.join(dirpath, '..'))
if project_path not in sys.path:
sys.path.insert(0, project_path)
os.chdir(project_path)
project_settings = '{}.settings'.format(project)
os.environ['DJANGO_SETTINGS_MODULE'] = project_settings
try:
import django
# Sanity
django.setup = lambda: False
except ImportError:
log.error('Found Django settings, but Django is not installed.')
return
log.warn('Loading Django Settings (Using {}): {}'
.format(django.get_version(), filename))
from django.conf import LazySettings
installed_apps = set()
settings_imports = set()
try:
settings = LazySettings()
settings._setup()
for k, v in vars(settings._wrapped).items():
if k not in _excluded_settings and re.match(r'^[A-Z_]+$', k):
# log.debug('Scanning Django setting: %s', k)
scan_django_settings(v, settings_imports)
# Manually scan INSTALLED_APPS since the broad scan won't include
# strings without a period in it .
for app in getattr(settings, 'INSTALLED_APPS', []):
if hasattr(app, '__file__') and getattr(app, '__file__'):
imp, _ = utils.import_path_from_file(getattr(app, '__file__'))
installed_apps.add(imp)
else:
installed_apps.add(app)
except Exception as e:
log.error('Could not load Django settings: %s', e)
log.debug('', exc_info=True)
return
if not installed_apps or not settings_imports:
log.error('Got empty settings values from Django settings.')
try:
from django.apps.registry import apps, Apps, AppRegistryNotReady
# Django doesn't like it when the initial instance of `apps` is reused,
# but it has to be populated before other instances can be created.
if not apps.apps_ready:
apps.populate(installed_apps)
else:
apps = Apps(installed_apps)
start = time.time()
while True:
try:
for app in apps.get_app_configs():
installed_apps.add(app.name)
except AppRegistryNotReady:
if time.time() - start > 10:
raise Exception('Bail out of waiting for Django')
log.debug('Waiting for apps to load...')
continue
break
except Exception as e:
log.debug('Could not use AppConfig: {}'.format(e))
# Restore before sub scans can occur
sys.path[:] = old_sys_path
os.chdir(cwd)
for item in settings_imports:
need_scan = item.startswith(_filescan_modules)
yield ('django', item, project_path if need_scan else None)
for app in installed_apps:
need_scan = app.startswith(project)
yield ('django', app, project_path if need_scan else None) |
Subsets and Splits