sequence
stringlengths 546
16.2k
| code
stringlengths 108
19.3k
|
---|---|
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 30; 2, function_name:send_array; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:socket; 5, default_parameter; 5, 6; 5, 7; 6, identifier:A; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:metadata; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:flags; 13, integer:0; 14, default_parameter; 14, 15; 14, 16; 15, identifier:copy; 16, False; 17, default_parameter; 17, 18; 17, 19; 18, identifier:track; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:compress; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:chunksize; 25, binary_operator:*; 25, 26; 25, 29; 26, binary_operator:*; 26, 27; 26, 28; 27, integer:50; 28, integer:1000; 29, integer:1000; 30, block; 30, 31; 30, 33; 30, 34; 30, 38; 30, 39; 30, 55; 30, 56; 30, 66; 30, 67; 30, 89; 30, 90; 30, 112; 30, 113; 30, 124; 30, 132; 30, 133; 30, 153; 30, 184; 30, 185; 30, 197; 30, 198; 30, 199; 30, 298; 31, expression_statement; 31, 32; 32, comment; 33, comment; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:md; 37, dictionary; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:md; 43, string:'timestamp'; 44, call; 44, 45; 44, 54; 45, attribute; 45, 46; 45, 53; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:datetime; 50, identifier:datetime; 51, identifier:now; 52, argument_list; 53, identifier:isoformat; 54, argument_list; 55, comment; 56, if_statement; 56, 57; 56, 58; 57, identifier:metadata; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:md; 63, identifier:update; 64, argument_list; 64, 65; 65, identifier:metadata; 66, comment; 67, if_statement; 67, 68; 67, 71; 67, 72; 68, comparison_operator:is; 68, 69; 68, 70; 69, identifier:A; 70, None; 71, comment; 72, block; 72, 73; 72, 79; 72, 87; 72, 88; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:md; 77, string:'parts'; 78, integer:0; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:socket; 83, identifier:send_json; 84, argument_list; 84, 85; 84, 86; 85, identifier:md; 86, identifier:flags; 87, comment; 88, return_statement; 89, comment; 90, if_statement; 90, 91; 90, 102; 91, boolean_operator:or; 91, 92; 91, 97; 92, call; 92, 93; 92, 94; 93, identifier:isinstance; 94, argument_list; 94, 95; 94, 96; 95, identifier:A; 96, identifier:float; 97, call; 97, 98; 97, 99; 98, identifier:isinstance; 99, argument_list; 99, 100; 99, 101; 100, identifier:A; 101, identifier:int; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:A; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:np; 109, identifier:asarray; 110, argument_list; 110, 111; 111, identifier:A; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:md; 117, string:'dtype'; 118, call; 118, 119; 118, 120; 119, identifier:str; 120, argument_list; 120, 121; 121, attribute; 121, 122; 121, 123; 122, identifier:A; 123, identifier:dtype; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:md; 128, string:'shape'; 129, attribute; 129, 130; 129, 131; 130, identifier:A; 131, identifier:shape; 132, comment; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:md; 137, string:'parts'; 138, call; 138, 139; 138, 140; 139, identifier:int; 140, argument_list; 140, 141; 141, binary_operator:+; 141, 142; 141, 152; 142, binary_operator://; 142, 143; 142, 151; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:np; 146, identifier:prod; 147, argument_list; 147, 148; 148, attribute; 148, 149; 148, 150; 149, identifier:A; 150, identifier:shape; 151, identifier:chunksize; 152, integer:1; 153, try_statement; 153, 154; 153, 155; 153, 156; 153, 157; 153, 179; 154, comment; 155, comment; 156, comment; 157, block; 157, 158; 157, 171; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:md; 162, string:'fill_value'; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:np; 166, identifier:asscalar; 167, argument_list; 167, 168; 168, attribute; 168, 169; 168, 170; 169, identifier:A; 170, identifier:fill_value; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:A; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:A; 177, identifier:filled; 178, argument_list; 179, except_clause; 179, 180; 179, 181; 179, 182; 180, identifier:AttributeError; 181, comment; 182, block; 182, 183; 183, pass_statement; 184, comment; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:socket; 189, identifier:send_json; 190, argument_list; 190, 191; 190, 192; 191, identifier:md; 192, binary_operator:|; 192, 193; 192, 194; 193, identifier:flags; 194, attribute; 194, 195; 194, 196; 195, identifier:zmq; 196, identifier:SNDMORE; 197, comment; 198, comment; 199, if_statement; 199, 200; 199, 205; 199, 232; 200, comparison_operator:==; 200, 201; 200, 204; 201, subscript; 201, 202; 201, 203; 202, identifier:md; 203, string:'parts'; 204, integer:1; 205, block; 205, 206; 205, 218; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:msg; 209, call; 209, 210; 209, 211; 210, identifier:memoryview; 211, argument_list; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:np; 215, identifier:ascontiguousarray; 216, argument_list; 216, 217; 217, identifier:A; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:socket; 222, identifier:send; 223, argument_list; 223, 224; 223, 225; 223, 226; 223, 229; 224, identifier:msg; 225, identifier:flags; 226, keyword_argument; 226, 227; 226, 228; 227, identifier:copy; 228, identifier:copy; 229, keyword_argument; 229, 230; 229, 231; 230, identifier:track; 231, identifier:track; 232, else_clause; 232, 233; 232, 234; 233, comment; 234, block; 234, 235; 235, for_statement; 235, 236; 235, 239; 235, 251; 235, 252; 236, pattern_list; 236, 237; 236, 238; 237, identifier:i; 238, identifier:a; 239, call; 239, 240; 239, 241; 240, identifier:enumerate; 241, argument_list; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:np; 245, identifier:array_split; 246, argument_list; 246, 247; 246, 248; 247, identifier:A; 248, subscript; 248, 249; 248, 250; 249, identifier:md; 250, string:'parts'; 251, comment; 252, block; 252, 253; 252, 265; 252, 269; 252, 284; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:msg; 256, call; 256, 257; 256, 258; 257, identifier:memoryview; 258, argument_list; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:np; 262, identifier:ascontiguousarray; 263, argument_list; 263, 264; 264, identifier:a; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:flags_; 268, identifier:flags; 269, if_statement; 269, 270; 269, 277; 270, comparison_operator:!=; 270, 271; 270, 272; 271, identifier:i; 272, binary_operator:-; 272, 273; 272, 276; 273, subscript; 273, 274; 273, 275; 274, identifier:md; 275, string:'parts'; 276, integer:1; 277, block; 277, 278; 278, expression_statement; 278, 279; 279, augmented_assignment:|=; 279, 280; 279, 281; 280, identifier:flags_; 281, attribute; 281, 282; 281, 283; 282, identifier:zmq; 283, identifier:SNDMORE; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:socket; 288, identifier:send; 289, argument_list; 289, 290; 289, 291; 289, 292; 289, 295; 290, identifier:msg; 291, identifier:flags_; 292, keyword_argument; 292, 293; 292, 294; 293, identifier:copy; 294, identifier:copy; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:track; 297, identifier:track; 298, return_statement | def send_array(
socket, A=None, metadata=None, flags=0,
copy=False, track=False, compress=None,
chunksize=50 * 1000 * 1000
):
"""send a numpy array with metadata over zmq
message is mostly multipart:
metadata | array part 1 | array part 2, etc
only metadata:
metadata
the chunksize roughly determines the size of the parts being sent
if the chunksize is too big, you get an error like:
zmq.error.Again: Resource temporarily unavailable
"""
# create a metadata dictionary for the message
md = {}
# always add a timestamp
md['timestamp'] = datetime.datetime.now().isoformat()
# copy extra metadata
if metadata:
md.update(metadata)
# if we don't have an array
if A is None:
# send only json
md['parts'] = 0
socket.send_json(md, flags)
# and we're done
return
# support single values (empty shape)
if isinstance(A, float) or isinstance(A, int):
A = np.asarray(A)
# add array metadata
md['dtype'] = str(A.dtype)
md['shape'] = A.shape
# determine number of parts
md['parts'] = int(np.prod(A.shape) // chunksize + 1)
try:
# If an array has a fill value assume it's an array with missings
# store the fill_Value in the metadata and fill the array before sending.
# asscalar should work for scalar, 0d array or nd array of size 1
md['fill_value'] = np.asscalar(A.fill_value)
A = A.filled()
except AttributeError:
# no masked array, nothing to do
pass
# send json, followed by array (in x parts)
socket.send_json(md, flags | zmq.SNDMORE)
# although the check is not strictly necessary, we try to maintain fast
# pointer transfer when there is only 1 part
if md['parts'] == 1:
msg = memoryview(np.ascontiguousarray(A))
socket.send(msg, flags, copy=copy, track=track)
else:
# split array at first dimension and send parts
for i, a in enumerate(np.array_split(A, md['parts'])):
# Make a copy if required and pass along the memoryview
msg = memoryview(np.ascontiguousarray(a))
flags_ = flags
if i != md['parts'] - 1:
flags_ |= zmq.SNDMORE
socket.send(msg, flags_, copy=copy, track=track)
return |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:recv_array; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:socket; 5, default_parameter; 5, 6; 5, 7; 6, identifier:flags; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:copy; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:track; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:poll; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:poll_timeout; 19, integer:10000; 20, block; 20, 21; 20, 23; 20, 89; 20, 439; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 27; 23, 39; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:poll; 26, None; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:md; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:socket; 34, identifier:recv_json; 35, argument_list; 35, 36; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:flags; 38, identifier:flags; 39, else_clause; 39, 40; 39, 41; 40, comment; 41, block; 41, 42; 41, 54; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:socks; 45, call; 45, 46; 45, 47; 46, identifier:dict; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:poll; 51, identifier:poll; 52, argument_list; 52, 53; 53, identifier:poll_timeout; 54, if_statement; 54, 55; 54, 65; 54, 82; 55, comparison_operator:==; 55, 56; 55, 62; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:socks; 59, identifier:get; 60, argument_list; 60, 61; 61, identifier:socket; 62, attribute; 62, 63; 62, 64; 63, identifier:zmq; 64, identifier:POLLIN; 65, block; 65, 66; 65, 77; 65, 78; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:reply; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:socket; 72, identifier:recv_json; 73, argument_list; 73, 74; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:flags; 76, identifier:flags; 77, comment; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:md; 81, identifier:reply; 82, else_clause; 82, 83; 83, block; 83, 84; 84, raise_statement; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:NoResponseException; 87, argument_list; 87, 88; 88, string:"Recv_array got no response within timeout (1)"; 89, if_statement; 89, 90; 89, 95; 89, 96; 89, 101; 89, 239; 90, comparison_operator:==; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:md; 93, string:'parts'; 94, integer:0; 95, comment; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:A; 100, None; 101, elif_clause; 101, 102; 101, 107; 101, 108; 101, 109; 102, comparison_operator:==; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:md; 105, string:'parts'; 106, integer:1; 107, comment; 108, comment; 109, block; 109, 110; 109, 188; 109, 195; 109, 209; 109, 220; 110, if_statement; 110, 111; 110, 114; 110, 132; 111, comparison_operator:is; 111, 112; 111, 113; 112, identifier:poll; 113, None; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:msg; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:socket; 121, identifier:recv; 122, argument_list; 122, 123; 122, 126; 122, 129; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:flags; 125, identifier:flags; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:copy; 128, identifier:copy; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:track; 131, identifier:track; 132, else_clause; 132, 133; 132, 134; 133, comment; 134, block; 134, 135; 134, 147; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:socks; 138, call; 138, 139; 138, 140; 139, identifier:dict; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:poll; 144, identifier:poll; 145, argument_list; 145, 146; 146, identifier:poll_timeout; 147, if_statement; 147, 148; 147, 158; 147, 181; 148, comparison_operator:==; 148, 149; 148, 155; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:socks; 152, identifier:get; 153, argument_list; 153, 154; 154, identifier:socket; 155, attribute; 155, 156; 155, 157; 156, identifier:zmq; 157, identifier:POLLIN; 158, block; 158, 159; 158, 176; 158, 177; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:reply; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:socket; 165, identifier:recv; 166, argument_list; 166, 167; 166, 170; 166, 173; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:flags; 169, identifier:flags; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:copy; 172, identifier:copy; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:track; 175, identifier:track; 176, comment; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:msg; 180, identifier:reply; 181, else_clause; 181, 182; 182, block; 182, 183; 183, raise_statement; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:NoResponseException; 186, argument_list; 186, 187; 187, string:"Recv_array got no response within timeout (2)"; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:buf; 191, call; 191, 192; 191, 193; 192, identifier:buffer; 193, argument_list; 193, 194; 194, identifier:msg; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:A; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:np; 201, identifier:frombuffer; 202, argument_list; 202, 203; 202, 204; 203, identifier:buf; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:dtype; 206, subscript; 206, 207; 206, 208; 207, identifier:md; 208, string:'dtype'; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:A; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:A; 215, identifier:reshape; 216, argument_list; 216, 217; 217, subscript; 217, 218; 217, 219; 218, identifier:md; 219, string:'shape'; 220, if_statement; 220, 221; 220, 224; 221, comparison_operator:in; 221, 222; 221, 223; 222, string:'fill_value'; 223, identifier:md; 224, block; 224, 225; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:A; 228, call; 228, 229; 228, 234; 229, attribute; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:np; 232, identifier:ma; 233, identifier:masked_equal; 234, argument_list; 234, 235; 234, 236; 235, identifier:A; 236, subscript; 236, 237; 236, 238; 237, identifier:md; 238, string:'fill_value'; 239, else_clause; 239, 240; 239, 241; 240, comment; 241, block; 241, 242; 241, 263; 241, 267; 241, 409; 241, 420; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:A; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:np; 248, identifier:zeros; 249, argument_list; 249, 250; 249, 258; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:np; 253, identifier:prod; 254, argument_list; 254, 255; 255, subscript; 255, 256; 255, 257; 256, identifier:md; 257, string:'shape'; 258, keyword_argument; 258, 259; 258, 260; 259, identifier:dtype; 260, subscript; 260, 261; 260, 262; 261, identifier:md; 262, string:'dtype'; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:arr_position; 266, integer:0; 267, for_statement; 267, 268; 267, 269; 267, 275; 268, identifier:i; 269, call; 269, 270; 269, 271; 270, identifier:range; 271, argument_list; 271, 272; 272, subscript; 272, 273; 272, 274; 273, identifier:md; 274, string:'parts'; 275, block; 275, 276; 275, 362; 275, 369; 275, 383; 275, 401; 276, if_statement; 276, 277; 276, 280; 276, 298; 277, comparison_operator:is; 277, 278; 277, 279; 278, identifier:poll; 279, None; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:msg; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:socket; 287, identifier:recv; 288, argument_list; 288, 289; 288, 292; 288, 295; 289, keyword_argument; 289, 290; 289, 291; 290, identifier:flags; 291, identifier:flags; 292, keyword_argument; 292, 293; 292, 294; 293, identifier:copy; 294, identifier:copy; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:track; 297, identifier:track; 298, else_clause; 298, 299; 298, 300; 299, comment; 300, block; 300, 301; 300, 313; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:socks; 304, call; 304, 305; 304, 306; 305, identifier:dict; 306, argument_list; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:poll; 310, identifier:poll; 311, argument_list; 311, 312; 312, identifier:poll_timeout; 313, if_statement; 313, 314; 313, 324; 313, 355; 314, comparison_operator:==; 314, 315; 314, 321; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:socks; 318, identifier:get; 319, argument_list; 319, 320; 320, identifier:socket; 321, attribute; 321, 322; 321, 323; 322, identifier:zmq; 323, identifier:POLLIN; 324, block; 324, 325; 324, 342; 324, 351; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:reply; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:socket; 331, identifier:recv; 332, argument_list; 332, 333; 332, 336; 332, 339; 333, keyword_argument; 333, 334; 333, 335; 334, identifier:flags; 335, identifier:flags; 336, keyword_argument; 336, 337; 336, 338; 337, identifier:copy; 338, identifier:copy; 339, keyword_argument; 339, 340; 339, 341; 340, identifier:track; 341, identifier:track; 342, if_statement; 342, 343; 342, 345; 343, not_operator; 343, 344; 344, identifier:reply; 345, block; 345, 346; 346, raise_statement; 346, 347; 347, call; 347, 348; 347, 349; 348, identifier:EmptyResponseException; 349, argument_list; 349, 350; 350, string:"Recv_array got an empty response (2)"; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 354; 353, identifier:msg; 354, identifier:reply; 355, else_clause; 355, 356; 356, block; 356, 357; 357, raise_statement; 357, 358; 358, call; 358, 359; 358, 360; 359, identifier:NoResponseException; 360, argument_list; 360, 361; 361, string:"Recv_array got no response within timeout (2)"; 362, expression_statement; 362, 363; 363, assignment; 363, 364; 363, 365; 364, identifier:buf; 365, call; 365, 366; 365, 367; 366, identifier:buffer; 367, argument_list; 367, 368; 368, identifier:msg; 369, expression_statement; 369, 370; 370, assignment; 370, 371; 370, 372; 371, identifier:a; 372, call; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, identifier:np; 375, identifier:frombuffer; 376, argument_list; 376, 377; 376, 378; 377, identifier:buf; 378, keyword_argument; 378, 379; 378, 380; 379, identifier:dtype; 380, subscript; 380, 381; 380, 382; 381, identifier:md; 382, string:'dtype'; 383, expression_statement; 383, 384; 384, assignment; 384, 385; 384, 397; 385, subscript; 385, 386; 385, 387; 386, identifier:A; 387, slice; 387, 388; 387, 389; 387, 390; 388, identifier:arr_position; 389, colon; 390, binary_operator:+; 390, 391; 390, 392; 391, identifier:arr_position; 392, subscript; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:a; 395, identifier:shape; 396, integer:0; 397, subscript; 397, 398; 397, 399; 398, identifier:a; 399, slice; 399, 400; 400, colon; 401, expression_statement; 401, 402; 402, augmented_assignment:+=; 402, 403; 402, 404; 403, identifier:arr_position; 404, subscript; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, identifier:a; 407, identifier:shape; 408, integer:0; 409, expression_statement; 409, 410; 410, assignment; 410, 411; 410, 412; 411, identifier:A; 412, call; 412, 413; 412, 416; 413, attribute; 413, 414; 413, 415; 414, identifier:A; 415, identifier:reshape; 416, argument_list; 416, 417; 417, subscript; 417, 418; 417, 419; 418, identifier:md; 419, string:'shape'; 420, if_statement; 420, 421; 420, 424; 421, comparison_operator:in; 421, 422; 421, 423; 422, string:'fill_value'; 423, identifier:md; 424, block; 424, 425; 425, expression_statement; 425, 426; 426, assignment; 426, 427; 426, 428; 427, identifier:A; 428, call; 428, 429; 428, 434; 429, attribute; 429, 430; 429, 433; 430, attribute; 430, 431; 430, 432; 431, identifier:np; 432, identifier:ma; 433, identifier:masked_equal; 434, argument_list; 434, 435; 434, 436; 435, identifier:A; 436, subscript; 436, 437; 436, 438; 437, identifier:md; 438, string:'fill_value'; 439, return_statement; 439, 440; 440, expression_list; 440, 441; 440, 442; 441, identifier:A; 442, identifier:md | def recv_array(
socket,
flags=0,
copy=False,
track=False,
poll=None,
poll_timeout=10000
):
"""recv a metadata and an optional numpy array from a zmq socket
Optionally provide poll object to use recv_array with timeout
poll_timeout is in millis
"""
if poll is None:
md = socket.recv_json(flags=flags)
else:
# one-try "Lazy Pirate" method: http://zguide.zeromq.org/php:chapter4
socks = dict(poll.poll(poll_timeout))
if socks.get(socket) == zmq.POLLIN:
reply = socket.recv_json(flags=flags)
# note that reply can be an empty array
md = reply
else:
raise NoResponseException(
"Recv_array got no response within timeout (1)")
if md['parts'] == 0:
# No array expected
A = None
elif md['parts'] == 1:
# although the check is not strictly necessary, we try to maintain fast
# pointer transfer when there is only 1 part
if poll is None:
msg = socket.recv(flags=flags, copy=copy, track=track)
else:
# one-try "Lazy Pirate" method: http://zguide.zeromq.org/php:chapter4
socks = dict(poll.poll(poll_timeout))
if socks.get(socket) == zmq.POLLIN:
reply = socket.recv(flags=flags, copy=copy, track=track)
# note that reply can be an empty array
msg = reply
else:
raise NoResponseException(
"Recv_array got no response within timeout (2)")
buf = buffer(msg)
A = np.frombuffer(buf, dtype=md['dtype'])
A = A.reshape(md['shape'])
if 'fill_value' in md:
A = np.ma.masked_equal(A, md['fill_value'])
else:
# multi part array
A = np.zeros(np.prod(md['shape']), dtype=md['dtype'])
arr_position = 0
for i in range(md['parts']):
if poll is None:
msg = socket.recv(flags=flags, copy=copy, track=track)
else:
# one-try "Lazy Pirate" method: http://zguide.zeromq.org/php:chapter4
socks = dict(poll.poll(poll_timeout))
if socks.get(socket) == zmq.POLLIN:
reply = socket.recv(flags=flags, copy=copy, track=track)
if not reply:
raise EmptyResponseException(
"Recv_array got an empty response (2)")
msg = reply
else:
raise NoResponseException(
"Recv_array got no response within timeout (2)")
buf = buffer(msg)
a = np.frombuffer(buf, dtype=md['dtype'])
A[arr_position:arr_position + a.shape[0]] = a[:]
arr_position += a.shape[0]
A = A.reshape(md['shape'])
if 'fill_value' in md:
A = np.ma.masked_equal(A, md['fill_value'])
return A, md |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:reduce_fit; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:interface; 5, identifier:state; 6, identifier:label; 7, identifier:inp; 8, block; 8, 9; 8, 11; 8, 17; 8, 18; 8, 23; 8, 32; 8, 33; 8, 34; 8, 35; 8, 50; 8, 54; 8, 55; 8, 63; 8, 67; 8, 321; 8, 322; 8, 416; 8, 417; 8, 436; 8, 454; 8, 467; 8, 480; 9, expression_statement; 9, 10; 10, comment; 11, import_from_statement; 11, 12; 11, 15; 12, dotted_name; 12, 13; 12, 14; 13, identifier:disco; 14, identifier:util; 15, dotted_name; 15, 16; 16, identifier:kvgroup; 17, comment; 18, import_statement; 18, 19; 19, aliased_import; 19, 20; 19, 22; 20, dotted_name; 20, 21; 21, identifier:numpy; 22, identifier:np; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:out; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:interface; 29, identifier:output; 30, argument_list; 30, 31; 31, integer:0; 32, comment; 33, comment; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:fit_model; 38, dictionary; 38, 39; 38, 42; 38, 45; 39, pair; 39, 40; 39, 41; 40, string:"y_labels"; 41, list:[]; 42, pair; 42, 43; 42, 44; 43, string:"y_sum"; 44, integer:0; 45, pair; 45, 46; 45, 47; 46, string:"iv"; 47, call; 47, 48; 47, 49; 48, identifier:set; 49, argument_list; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:combiner; 53, dictionary; 54, comment; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 60; 57, pattern_list; 57, 58; 57, 59; 58, identifier:means; 59, identifier:variances; 60, expression_list; 60, 61; 60, 62; 61, list:[]; 62, list:[]; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:k_prev; 66, string:""; 67, for_statement; 67, 68; 67, 71; 67, 75; 67, 76; 68, pattern_list; 68, 69; 68, 70; 69, identifier:key; 70, identifier:value; 71, call; 71, 72; 71, 73; 72, identifier:kvgroup; 73, argument_list; 73, 74; 74, identifier:inp; 75, comment; 76, block; 76, 77; 76, 88; 76, 89; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:k_split; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:key; 83, identifier:split; 84, argument_list; 84, 85; 85, subscript; 85, 86; 85, 87; 86, identifier:state; 87, string:"delimiter"; 88, comment; 89, if_statement; 89, 90; 89, 96; 89, 97; 89, 98; 89, 130; 89, 289; 90, comparison_operator:==; 90, 91; 90, 95; 91, call; 91, 92; 91, 93; 92, identifier:len; 93, argument_list; 93, 94; 94, identifier:k_split; 95, integer:3; 96, comment; 97, comment; 98, block; 98, 99; 98, 115; 98, 116; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:fit_model; 104, string:"iv"; 105, identifier:add; 106, argument_list; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:tuple; 109, argument_list; 109, 110; 110, subscript; 110, 111; 110, 112; 111, identifier:k_split; 112, slice; 112, 113; 112, 114; 113, integer:1; 114, colon; 115, comment; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:out; 120, identifier:add; 121, argument_list; 121, 122; 121, 126; 122, call; 122, 123; 122, 124; 123, identifier:tuple; 124, argument_list; 124, 125; 125, identifier:k_split; 126, call; 126, 127; 126, 128; 127, identifier:sum; 128, argument_list; 128, 129; 129, identifier:value; 130, elif_clause; 130, 131; 130, 137; 130, 138; 130, 139; 130, 140; 131, comparison_operator:==; 131, 132; 131, 136; 132, call; 132, 133; 132, 134; 133, identifier:len; 134, argument_list; 134, 135; 135, identifier:k_split; 136, integer:2; 137, comment; 138, comment; 139, comment; 140, block; 140, 141; 140, 189; 140, 190; 140, 198; 140, 199; 140, 267; 140, 268; 140, 283; 141, if_statement; 141, 142; 141, 151; 142, boolean_operator:and; 142, 143; 142, 148; 143, comparison_operator:!=; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:k_split; 146, integer:0; 147, identifier:k_prev; 148, comparison_operator:!=; 148, 149; 148, 150; 149, identifier:k_prev; 150, string:""; 151, block; 151, 152; 151, 175; 151, 182; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 157; 154, pattern_list; 154, 155; 154, 156; 155, identifier:mean; 156, identifier:var; 157, call; 157, 158; 157, 159; 158, identifier:zip; 159, argument_list; 159, 160; 160, list_splat; 160, 161; 161, list_comprehension; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:combiner; 164, identifier:key; 165, for_in_clause; 165, 166; 165, 167; 166, identifier:key; 167, call; 167, 168; 167, 169; 168, identifier:sorted; 169, argument_list; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:combiner; 173, identifier:keys; 174, argument_list; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:means; 179, identifier:append; 180, argument_list; 180, 181; 181, identifier:mean; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:variances; 186, identifier:append; 187, argument_list; 187, 188; 188, identifier:var; 189, comment; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:n_a; 193, assignment; 193, 194; 193, 195; 194, identifier:mean_a; 195, assignment; 195, 196; 195, 197; 196, identifier:var_a; 197, integer:0; 198, comment; 199, for_statement; 199, 200; 199, 204; 199, 205; 200, pattern_list; 200, 201; 200, 202; 200, 203; 201, identifier:n_b; 202, identifier:mean_b; 203, identifier:var_b; 204, identifier:value; 205, block; 205, 206; 205, 212; 205, 247; 205, 263; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:n_ab; 209, binary_operator:+; 209, 210; 209, 211; 210, identifier:n_a; 211, identifier:n_b; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:var_a; 215, binary_operator:+; 215, 216; 215, 230; 216, parenthesized_expression; 216, 217; 217, binary_operator:/; 217, 218; 217, 226; 218, parenthesized_expression; 218, 219; 219, binary_operator:+; 219, 220; 219, 223; 220, binary_operator:*; 220, 221; 220, 222; 221, identifier:n_a; 222, identifier:var_a; 223, binary_operator:*; 223, 224; 223, 225; 224, identifier:n_b; 225, identifier:var_b; 226, call; 226, 227; 226, 228; 227, identifier:float; 228, argument_list; 228, 229; 229, identifier:n_ab; 230, parenthesized_expression; 230, 231; 231, binary_operator:*; 231, 232; 231, 235; 232, binary_operator:*; 232, 233; 232, 234; 233, identifier:n_a; 234, identifier:n_b; 235, binary_operator:**; 235, 236; 235, 246; 236, parenthesized_expression; 236, 237; 237, binary_operator:/; 237, 238; 237, 242; 238, parenthesized_expression; 238, 239; 239, binary_operator:-; 239, 240; 239, 241; 240, identifier:mean_b; 241, identifier:mean_a; 242, call; 242, 243; 242, 244; 243, identifier:float; 244, argument_list; 244, 245; 245, identifier:n_ab; 246, integer:2; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:mean_a; 250, binary_operator:/; 250, 251; 250, 259; 251, parenthesized_expression; 251, 252; 252, binary_operator:+; 252, 253; 252, 256; 253, binary_operator:*; 253, 254; 253, 255; 254, identifier:n_a; 255, identifier:mean_a; 256, binary_operator:*; 256, 257; 256, 258; 257, identifier:n_b; 258, identifier:mean_b; 259, call; 259, 260; 259, 261; 260, identifier:float; 261, argument_list; 261, 262; 262, identifier:n_ab; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:n_a; 266, identifier:n_ab; 267, comment; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 278; 270, subscript; 270, 271; 270, 272; 271, identifier:combiner; 272, call; 272, 273; 272, 274; 273, identifier:int; 274, argument_list; 274, 275; 275, subscript; 275, 276; 275, 277; 276, identifier:k_split; 277, integer:1; 278, tuple; 278, 279; 278, 280; 279, identifier:mean_a; 280, binary_operator:+; 280, 281; 280, 282; 281, identifier:var_a; 282, float:1e-9; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:k_prev; 286, subscript; 286, 287; 286, 288; 287, identifier:k_split; 288, integer:0; 289, else_clause; 289, 290; 289, 291; 290, comment; 291, block; 291, 292; 291, 303; 291, 311; 291, 312; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 297; 294, subscript; 294, 295; 294, 296; 295, identifier:fit_model; 296, identifier:key; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:np; 300, identifier:sum; 301, argument_list; 301, 302; 302, identifier:value; 303, expression_statement; 303, 304; 304, augmented_assignment:+=; 304, 305; 304, 308; 305, subscript; 305, 306; 305, 307; 306, identifier:fit_model; 307, string:"y_sum"; 308, subscript; 308, 309; 308, 310; 309, identifier:fit_model; 310, identifier:key; 311, comment; 312, expression_statement; 312, 313; 313, call; 313, 314; 313, 319; 314, attribute; 314, 315; 314, 318; 315, subscript; 315, 316; 315, 317; 316, identifier:fit_model; 317, string:"y_labels"; 318, identifier:append; 319, argument_list; 319, 320; 320, identifier:key; 321, comment; 322, if_statement; 322, 323; 322, 329; 323, comparison_operator:>; 323, 324; 323, 328; 324, call; 324, 325; 324, 326; 325, identifier:len; 326, argument_list; 326, 327; 327, identifier:means; 328, integer:0; 329, block; 329, 330; 329, 353; 329, 374; 329, 391; 329, 399; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 335; 332, pattern_list; 332, 333; 332, 334; 333, identifier:mean; 334, identifier:var; 335, call; 335, 336; 335, 337; 336, identifier:zip; 337, argument_list; 337, 338; 338, list_splat; 338, 339; 339, list_comprehension; 339, 340; 339, 343; 340, subscript; 340, 341; 340, 342; 341, identifier:combiner; 342, identifier:key; 343, for_in_clause; 343, 344; 343, 345; 344, identifier:key; 345, call; 345, 346; 345, 347; 346, identifier:sorted; 347, argument_list; 347, 348; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:combiner; 351, identifier:keys; 352, argument_list; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:out; 357, identifier:add; 358, argument_list; 358, 359; 358, 360; 359, string:"mean"; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:np; 363, identifier:array; 364, argument_list; 364, 365; 364, 369; 365, binary_operator:+; 365, 366; 365, 367; 366, identifier:means; 367, list:[mean]; 367, 368; 368, identifier:mean; 369, keyword_argument; 369, 370; 369, 371; 370, identifier:dtype; 371, attribute; 371, 372; 371, 373; 372, identifier:np; 373, identifier:float32; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 377; 376, identifier:variances; 377, call; 377, 378; 377, 381; 378, attribute; 378, 379; 378, 380; 379, identifier:np; 380, identifier:array; 381, argument_list; 381, 382; 381, 386; 382, binary_operator:+; 382, 383; 382, 384; 383, identifier:variances; 384, list:[var]; 384, 385; 385, identifier:var; 386, keyword_argument; 386, 387; 386, 388; 387, identifier:dtype; 388, attribute; 388, 389; 388, 390; 389, identifier:np; 390, identifier:float32; 391, expression_statement; 391, 392; 392, call; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:out; 395, identifier:add; 396, argument_list; 396, 397; 396, 398; 397, string:"var"; 398, identifier:variances; 399, expression_statement; 399, 400; 400, call; 400, 401; 400, 404; 401, attribute; 401, 402; 401, 403; 402, identifier:out; 403, identifier:add; 404, argument_list; 404, 405; 404, 406; 405, string:"var_log"; 406, call; 406, 407; 406, 410; 407, attribute; 407, 408; 407, 409; 408, identifier:np; 409, identifier:log; 410, argument_list; 410, 411; 411, binary_operator:*; 411, 412; 411, 415; 412, attribute; 412, 413; 412, 414; 413, identifier:np; 414, identifier:pi; 415, identifier:variances; 416, comment; 417, expression_statement; 417, 418; 418, assignment; 418, 419; 418, 420; 419, identifier:prior; 420, list_comprehension; 420, 421; 420, 431; 421, binary_operator:/; 421, 422; 421, 425; 422, subscript; 422, 423; 422, 424; 423, identifier:fit_model; 424, identifier:y_label; 425, call; 425, 426; 425, 427; 426, identifier:float; 427, argument_list; 427, 428; 428, subscript; 428, 429; 428, 430; 429, identifier:fit_model; 430, string:"y_sum"; 431, for_in_clause; 431, 432; 431, 433; 432, identifier:y_label; 433, subscript; 433, 434; 433, 435; 434, identifier:fit_model; 435, string:"y_labels"; 436, expression_statement; 436, 437; 437, call; 437, 438; 437, 441; 438, attribute; 438, 439; 438, 440; 439, identifier:out; 440, identifier:add; 441, argument_list; 441, 442; 441, 443; 442, string:"prior"; 443, call; 443, 444; 443, 447; 444, attribute; 444, 445; 444, 446; 445, identifier:np; 446, identifier:array; 447, argument_list; 447, 448; 447, 449; 448, identifier:prior; 449, keyword_argument; 449, 450; 449, 451; 450, identifier:dtype; 451, attribute; 451, 452; 451, 453; 452, identifier:np; 453, identifier:float32; 454, expression_statement; 454, 455; 455, call; 455, 456; 455, 459; 456, attribute; 456, 457; 456, 458; 457, identifier:out; 458, identifier:add; 459, argument_list; 459, 460; 459, 461; 460, string:"prior_log"; 461, call; 461, 462; 461, 465; 462, attribute; 462, 463; 462, 464; 463, identifier:np; 464, identifier:log; 465, argument_list; 465, 466; 466, identifier:prior; 467, expression_statement; 467, 468; 468, call; 468, 469; 468, 472; 469, attribute; 469, 470; 469, 471; 470, identifier:out; 471, identifier:add; 472, argument_list; 472, 473; 472, 474; 473, string:"iv"; 474, call; 474, 475; 474, 476; 475, identifier:list; 476, argument_list; 476, 477; 477, subscript; 477, 478; 477, 479; 478, identifier:fit_model; 479, string:"iv"; 480, expression_statement; 480, 481; 481, call; 481, 482; 481, 485; 482, attribute; 482, 483; 482, 484; 483, identifier:out; 484, identifier:add; 485, argument_list; 485, 486; 485, 487; 486, string:"y_labels"; 487, subscript; 487, 488; 487, 489; 488, identifier:fit_model; 489, string:"y_labels" | def reduce_fit(interface, state, label, inp):
"""
Function separates aggregation of continuous and discrete features.
For continuous features it aggregates partially calculated means and variances and returns them. For discrete
features it aggregates pairs and returns them. Pairs with label occurrences are used to calculate prior probabilities
"""
from disco.util import kvgroup # function for grouping values by key
import numpy as np
out = interface.output(0) # all outputted pairs have the same output label
# model of naive Bayes stores label names, sum of all label occurrences and pairs
# (feature index, feature values) for discrete features which are needed to optimize predict phase.
fit_model = {"y_labels": [], "y_sum": 0, "iv": set()}
combiner = {} # combiner maintains correct order of means and variances.
means, variances = [], []
k_prev = ""
for key, value in kvgroup(inp): # input pairs are sorted and grouped by key
k_split = key.split(state["delimiter"]) # pair is split
if len(k_split) == 3: # discrete features
# store pair (feature index, feature value)
fit_model["iv"].add(tuple(k_split[1:]))
# aggregate and output occurrences of a pair
out.add(tuple(k_split), sum(value))
elif len(k_split) == 2: # continuous features
# if label is different than previous.
# This enables calculation of all variances and means for every feature for current label.
if k_split[0] != k_prev and k_prev != "":
mean, var = zip(*[combiner[key] for key in sorted(combiner.keys())])
means.append(mean)
variances.append(var)
# number of elements, partial mean, partial variance.
n_a = mean_a = var_a = 0
# code aggregates partially calculated means and variances
for n_b, mean_b, var_b in value:
n_ab = n_a + n_b
var_a = ((n_a * var_a + n_b * var_b) / float(n_ab)) + (
n_a * n_b * ((mean_b - mean_a) / float(n_ab)) ** 2)
mean_a = (n_a * mean_a + n_b * mean_b) / float(n_ab)
n_a = n_ab
# maintains correct order of statistics for every feature
combiner[int(k_split[1])] = (mean_a, var_a + 1e-9)
k_prev = k_split[0]
else: # aggregates label occurrences
fit_model[key] = np.sum(value)
fit_model["y_sum"] += fit_model[key] # sum of all label occurrences
fit_model["y_labels"].append(key)
# if statistics for continuous features were not output in last iteration
if len(means) > 0:
mean, var = zip(*[combiner[key] for key in sorted(combiner.keys())])
out.add("mean", np.array(means + [mean], dtype=np.float32))
variances = np.array(variances + [var], dtype=np.float32)
out.add("var", variances)
out.add("var_log", np.log(np.pi * variances))
# calculation of prior probabilities
prior = [fit_model[y_label] / float(fit_model["y_sum"]) for y_label in fit_model["y_labels"]]
out.add("prior", np.array(prior, dtype=np.float32))
out.add("prior_log", np.log(prior))
out.add("iv", list(fit_model["iv"]))
out.add("y_labels", fit_model["y_labels"]) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:map_predict; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:interface; 5, identifier:state; 6, identifier:label; 7, identifier:inp; 8, block; 8, 9; 8, 11; 8, 16; 8, 25; 8, 48; 8, 49; 8, 72; 8, 73; 8, 85; 8, 86; 8, 98; 8, 99; 9, expression_statement; 9, 10; 10, comment; 11, import_statement; 11, 12; 12, aliased_import; 12, 13; 12, 15; 13, dotted_name; 13, 14; 14, identifier:numpy; 15, identifier:np; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:out; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:interface; 22, identifier:output; 23, argument_list; 23, 24; 24, integer:0; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:continuous; 28, list_comprehension; 28, 29; 28, 30; 28, 40; 29, identifier:j; 30, for_in_clause; 30, 31; 30, 34; 31, pattern_list; 31, 32; 31, 33; 32, identifier:i; 33, identifier:j; 34, call; 34, 35; 34, 36; 35, identifier:enumerate; 36, argument_list; 36, 37; 37, subscript; 37, 38; 37, 39; 38, identifier:state; 39, string:"X_indices"; 40, if_clause; 40, 41; 41, comparison_operator:==; 41, 42; 41, 47; 42, subscript; 42, 43; 42, 46; 43, subscript; 43, 44; 43, 45; 44, identifier:state; 45, string:"X_meta"; 46, identifier:i; 47, string:"c"; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:discrete; 52, list_comprehension; 52, 53; 52, 54; 52, 64; 53, identifier:j; 54, for_in_clause; 54, 55; 54, 58; 55, pattern_list; 55, 56; 55, 57; 56, identifier:i; 57, identifier:j; 58, call; 58, 59; 58, 60; 59, identifier:enumerate; 60, argument_list; 60, 61; 61, subscript; 61, 62; 61, 63; 62, identifier:state; 63, string:"X_indices"; 64, if_clause; 64, 65; 65, comparison_operator:==; 65, 66; 65, 71; 66, subscript; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:state; 69, string:"X_meta"; 70, identifier:i; 71, string:"d"; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:cont; 76, conditional_expression:if; 76, 77; 76, 78; 76, 84; 77, True; 78, comparison_operator:>; 78, 79; 78, 83; 79, call; 79, 80; 79, 81; 80, identifier:len; 81, argument_list; 81, 82; 82, identifier:continuous; 83, integer:0; 84, False; 85, comment; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:disc; 89, conditional_expression:if; 89, 90; 89, 91; 89, 97; 90, True; 91, comparison_operator:>; 91, 92; 91, 96; 92, call; 92, 93; 92, 94; 93, identifier:len; 94, argument_list; 94, 95; 95, identifier:discrete; 96, integer:0; 97, False; 98, comment; 99, for_statement; 99, 100; 99, 101; 99, 102; 100, identifier:row; 101, identifier:inp; 102, block; 102, 103; 102, 118; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:row; 106, call; 106, 107; 106, 114; 107, attribute; 107, 108; 107, 113; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:row; 111, identifier:strip; 112, argument_list; 113, identifier:split; 114, argument_list; 114, 115; 115, subscript; 115, 116; 115, 117; 116, identifier:state; 117, string:"delimiter"; 118, if_statement; 118, 119; 118, 125; 118, 126; 118, 127; 119, comparison_operator:>; 119, 120; 119, 124; 120, call; 120, 121; 120, 122; 121, identifier:len; 122, argument_list; 122, 123; 123, identifier:row; 124, integer:1; 125, comment; 126, comment; 127, block; 127, 128; 127, 144; 127, 145; 127, 153; 127, 228; 127, 281; 127, 282; 127, 301; 127, 317; 127, 318; 127, 335; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:x_id; 131, conditional_expression:if; 131, 132; 131, 133; 131, 139; 132, string:""; 133, comparison_operator:==; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:state; 136, string:"id_index"; 137, unary_operator:-; 137, 138; 138, integer:1; 139, subscript; 139, 140; 139, 141; 140, identifier:row; 141, subscript; 141, 142; 141, 143; 142, identifier:state; 143, string:"id_index"; 144, comment; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:probs; 148, subscript; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:state; 151, string:"fit_model"; 152, string:"prior_log"; 153, if_statement; 153, 154; 153, 155; 153, 156; 154, identifier:cont; 155, comment; 156, block; 156, 157; 156, 185; 156, 186; 156, 187; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:x; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:np; 163, identifier:array; 164, argument_list; 164, 165; 165, list_comprehension; 165, 166; 165, 182; 166, parenthesized_expression; 166, 167; 167, conditional_expression:if; 167, 168; 167, 169; 167, 176; 168, integer:0; 169, comparison_operator:in; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:row; 172, identifier:j; 173, subscript; 173, 174; 173, 175; 174, identifier:state; 175, string:"missing_vals"; 176, call; 176, 177; 176, 178; 177, identifier:float; 178, argument_list; 178, 179; 179, subscript; 179, 180; 179, 181; 180, identifier:row; 181, identifier:j; 182, for_in_clause; 182, 183; 182, 184; 183, identifier:j; 184, identifier:continuous; 185, comment; 186, comment; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:probs; 190, binary_operator:-; 190, 191; 190, 192; 191, identifier:probs; 192, binary_operator:*; 192, 193; 192, 194; 193, float:0.5; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:np; 197, identifier:sum; 198, argument_list; 198, 199; 198, 225; 199, binary_operator:+; 199, 200; 199, 220; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:np; 203, identifier:true_divide; 204, argument_list; 204, 205; 204, 215; 205, binary_operator:**; 205, 206; 205, 214; 206, parenthesized_expression; 206, 207; 207, binary_operator:-; 207, 208; 207, 209; 208, identifier:x; 209, subscript; 209, 210; 209, 213; 210, subscript; 210, 211; 210, 212; 211, identifier:state; 212, string:"fit_model"; 213, string:"mean"; 214, integer:2; 215, subscript; 215, 216; 215, 219; 216, subscript; 216, 217; 216, 218; 217, identifier:state; 218, string:"fit_model"; 219, string:"var"; 220, subscript; 220, 221; 220, 224; 221, subscript; 221, 222; 221, 223; 222, identifier:state; 223, string:"fit_model"; 224, string:"var_log"; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:axis; 227, integer:1; 228, if_statement; 228, 229; 228, 230; 228, 231; 228, 232; 229, identifier:disc; 230, comment; 231, comment; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:probs; 236, binary_operator:+; 236, 237; 236, 238; 237, identifier:probs; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:np; 241, identifier:sum; 242, argument_list; 242, 243; 242, 278; 243, list_comprehension; 243, 244; 243, 275; 244, parenthesized_expression; 244, 245; 245, conditional_expression:if; 245, 246; 245, 247; 245, 254; 246, integer:0; 247, comparison_operator:in; 247, 248; 247, 251; 248, subscript; 248, 249; 248, 250; 249, identifier:row; 250, identifier:i; 251, subscript; 251, 252; 251, 253; 252, identifier:state; 253, string:"missing_vals"; 254, call; 254, 255; 254, 260; 255, attribute; 255, 256; 255, 259; 256, subscript; 256, 257; 256, 258; 257, identifier:state; 258, string:"fit_model"; 259, identifier:get; 260, argument_list; 260, 261; 260, 269; 261, tuple; 261, 262; 261, 266; 262, call; 262, 263; 262, 264; 263, identifier:str; 264, argument_list; 264, 265; 265, identifier:i; 266, subscript; 266, 267; 266, 268; 267, identifier:row; 268, identifier:i; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:np; 272, identifier:zeros; 273, argument_list; 273, 274; 274, integer:1; 275, for_in_clause; 275, 276; 275, 277; 276, identifier:i; 277, identifier:discrete; 278, keyword_argument; 278, 279; 278, 280; 279, identifier:axis; 280, integer:0; 281, comment; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:log_prob_x; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:np; 288, identifier:log; 289, argument_list; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:np; 293, identifier:sum; 294, argument_list; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:np; 298, identifier:exp; 299, argument_list; 299, 300; 300, identifier:probs; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:probs; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:np; 307, identifier:exp; 308, argument_list; 308, 309; 309, binary_operator:-; 309, 310; 309, 316; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:np; 313, identifier:array; 314, argument_list; 314, 315; 315, identifier:probs; 316, identifier:log_prob_x; 317, comment; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:y_predicted; 321, subscript; 321, 322; 321, 334; 322, call; 322, 323; 322, 324; 323, identifier:max; 324, argument_list; 324, 325; 325, call; 325, 326; 325, 327; 326, identifier:zip; 327, argument_list; 327, 328; 327, 329; 328, identifier:probs; 329, subscript; 329, 330; 329, 333; 330, subscript; 330, 331; 330, 332; 331, identifier:state; 332, string:"fit_model"; 333, string:"y_labels"; 334, integer:1; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:out; 339, identifier:add; 340, argument_list; 340, 341; 340, 342; 341, identifier:x_id; 342, tuple; 342, 343; 342, 344; 343, identifier:y_predicted; 344, call; 344, 345; 344, 348; 345, attribute; 345, 346; 345, 347; 346, identifier:probs; 347, identifier:tolist; 348, argument_list | def map_predict(interface, state, label, inp):
"""
Function makes a predictions of samples with given model. It calculates probabilities with multinomial and Gaussian distribution.
"""
import numpy as np
out = interface.output(0)
continuous = [j for i, j in enumerate(state["X_indices"]) if
state["X_meta"][i] == "c"] # indices of continuous features
discrete = [j for i, j in enumerate(state["X_indices"]) if
state["X_meta"][i] == "d"] # indices of discrete features
cont = True if len(continuous) > 0 else False # enables calculation of Gaussian probabilities
disc = True if len(discrete) > 0 else False # enables calculation of multinomial probabilities.
for row in inp:
row = row.strip().split(state["delimiter"])
if len(row) > 1: # if row is empty
# set id of a sample
x_id = "" if state["id_index"] == -1 else row[state["id_index"]]
# initialize prior probability for all labels
probs = state["fit_model"]["prior_log"]
if cont: # continuous features
x = np.array([(0 if row[j] in state["missing_vals"] else float(row[j])) for j in
continuous]) # sets selected features of the sample
# Gaussian distribution
probs = probs - 0.5 * np.sum(
np.true_divide((x - state["fit_model"]["mean"]) ** 2, state["fit_model"]["var"]) +
state["fit_model"]["var_log"], axis=1)
if disc: # discrete features
# multinomial distribution
probs = probs + np.sum(
[(0 if row[i] in state["missing_vals"] else state["fit_model"].get((str(i), row[i]), np.zeros(1)))
for i in discrete], axis=0)
# normalize by P(x) = P(f_1, ..., f_n)
log_prob_x = np.log(np.sum(np.exp(probs)))
probs = np.exp(np.array(probs) - log_prob_x)
# Predicted label is the one with highest probability
y_predicted = max(zip(probs, state["fit_model"]["y_labels"]))[1]
out.add(x_id, (y_predicted, probs.tolist())) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:json_dumps; 3, parameters; 3, 4; 4, identifier:obj; 5, block; 5, 6; 5, 8; 5, 30; 5, 31; 5, 32; 5, 33; 5, 34; 5, 52; 5, 61; 5, 172; 6, expression_statement; 6, 7; 7, comment; 8, try_statement; 8, 9; 8, 26; 9, block; 9, 10; 10, return_statement; 10, 11; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:json; 14, identifier:dumps; 15, argument_list; 15, 16; 15, 17; 15, 20; 15, 23; 16, identifier:obj; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:indent; 19, integer:2; 20, keyword_argument; 20, 21; 20, 22; 21, identifier:sort_keys; 22, True; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:allow_nan; 25, False; 26, except_clause; 26, 27; 26, 28; 27, identifier:ValueError; 28, block; 28, 29; 29, pass_statement; 30, comment; 31, comment; 32, comment; 33, comment; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:json_str; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:json; 40, identifier:dumps; 41, argument_list; 41, 42; 41, 43; 41, 46; 41, 49; 42, identifier:obj; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:indent; 45, integer:2; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:sort_keys; 48, True; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:allow_nan; 51, True; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:json_obj; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:json; 58, identifier:loads; 59, argument_list; 59, 60; 60, identifier:json_str; 61, function_definition; 61, 62; 61, 63; 61, 65; 62, function_name:do_map; 63, parameters; 63, 64; 64, identifier:obj; 65, block; 65, 66; 65, 73; 65, 82; 65, 114; 65, 143; 65, 144; 65, 145; 65, 155; 65, 170; 66, if_statement; 66, 67; 66, 70; 67, comparison_operator:is; 67, 68; 67, 69; 68, identifier:obj; 69, None; 70, block; 70, 71; 71, return_statement; 71, 72; 72, None; 73, if_statement; 73, 74; 73, 79; 74, call; 74, 75; 74, 76; 75, identifier:isinstance; 76, argument_list; 76, 77; 76, 78; 77, identifier:obj; 78, identifier:basestring; 79, block; 79, 80; 80, return_statement; 80, 81; 81, identifier:obj; 82, if_statement; 82, 83; 82, 88; 83, call; 83, 84; 83, 85; 84, identifier:isinstance; 85, argument_list; 85, 86; 85, 87; 86, identifier:obj; 87, identifier:dict; 88, block; 88, 89; 88, 93; 88, 112; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:res; 92, dictionary; 93, for_statement; 93, 94; 93, 97; 93, 102; 94, tuple_pattern; 94, 95; 94, 96; 95, identifier:key; 96, identifier:value; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:obj; 100, identifier:items; 101, argument_list; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:res; 107, identifier:key; 108, call; 108, 109; 108, 110; 109, identifier:do_map; 110, argument_list; 110, 111; 111, identifier:value; 112, return_statement; 112, 113; 113, identifier:res; 114, if_statement; 114, 115; 114, 122; 115, call; 115, 116; 115, 117; 116, identifier:isinstance; 117, argument_list; 117, 118; 117, 119; 118, identifier:obj; 119, attribute; 119, 120; 119, 121; 120, identifier:collections; 121, identifier:Iterable; 122, block; 122, 123; 122, 127; 122, 141; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:res; 126, list:[]; 127, for_statement; 127, 128; 127, 129; 127, 130; 128, identifier:el; 129, identifier:obj; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:res; 135, identifier:append; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:do_map; 139, argument_list; 139, 140; 140, identifier:el; 141, return_statement; 141, 142; 142, identifier:res; 143, comment; 144, comment; 145, if_statement; 145, 146; 145, 152; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:math; 149, identifier:isnan; 150, argument_list; 150, 151; 151, identifier:obj; 152, block; 152, 153; 153, return_statement; 153, 154; 154, string:"NaN"; 155, if_statement; 155, 156; 155, 162; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:math; 159, identifier:isinf; 160, argument_list; 160, 161; 161, identifier:obj; 162, block; 162, 163; 163, return_statement; 163, 164; 164, conditional_expression:if; 164, 165; 164, 166; 164, 169; 165, string:"Infinity"; 166, comparison_operator:>; 166, 167; 166, 168; 167, identifier:obj; 168, integer:0; 169, string:"-Infinity"; 170, return_statement; 170, 171; 171, identifier:obj; 172, return_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:json; 176, identifier:dumps; 177, argument_list; 177, 178; 177, 182; 177, 185; 177, 188; 178, call; 178, 179; 178, 180; 179, identifier:do_map; 180, argument_list; 180, 181; 181, identifier:json_obj; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:indent; 184, integer:2; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:sort_keys; 187, True; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:allow_nan; 190, False | def json_dumps(obj):
"""A safe JSON dump function that provides correct diverging numbers for a
ECMAscript consumer.
"""
try:
return json.dumps(obj, indent=2, sort_keys=True, allow_nan=False)
except ValueError:
pass
# we don't want to call do_map on the original object since it can
# contain objects that need to be converted for JSON. after reading
# in the created JSON we get a limited set of possible types we
# can encounter
json_str = json.dumps(obj, indent=2, sort_keys=True, allow_nan=True)
json_obj = json.loads(json_str)
def do_map(obj):
if obj is None:
return None
if isinstance(obj, basestring):
return obj
if isinstance(obj, dict):
res = {}
for (key, value) in obj.items():
res[key] = do_map(value)
return res
if isinstance(obj, collections.Iterable):
res = []
for el in obj:
res.append(do_map(el))
return res
# diverging numbers need to be passed as strings otherwise it
# will throw a parsing error on the ECMAscript consumer side
if math.isnan(obj):
return "NaN"
if math.isinf(obj):
return "Infinity" if obj > 0 else "-Infinity"
return obj
return json.dumps(
do_map(json_obj), indent=2, sort_keys=True, allow_nan=False) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:add_json_mask; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:start; 6, identifier:method_str; 7, identifier:json_producer; 8, block; 8, 9; 8, 11; 8, 277; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 16; 12, function_name:send_json; 13, parameters; 13, 14; 13, 15; 14, identifier:drh; 15, identifier:rem_path; 16, block; 16, 17; 16, 25; 16, 40; 16, 49; 16, 55; 16, 61; 16, 76; 16, 82; 16, 89; 16, 122; 16, 129; 16, 135; 16, 143; 16, 150; 16, 151; 16, 246; 16, 253; 16, 261; 16, 269; 16, 275; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:obj; 20, call; 20, 21; 20, 22; 21, identifier:json_producer; 22, argument_list; 22, 23; 22, 24; 23, identifier:drh; 24, identifier:rem_path; 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:obj; 31, identifier:Response; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:obj; 36, call; 36, 37; 36, 38; 37, identifier:Response; 38, argument_list; 38, 39; 39, identifier:obj; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:ctype; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:obj; 46, identifier:get_ctype; 47, argument_list; 47, 48; 48, string:"application/json"; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:code; 52, attribute; 52, 53; 52, 54; 53, identifier:obj; 54, identifier:code; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:obj; 58, attribute; 58, 59; 58, 60; 59, identifier:obj; 60, identifier:response; 61, if_statement; 61, 62; 61, 65; 62, comparison_operator:is; 62, 63; 62, 64; 63, identifier:obj; 64, None; 65, block; 65, 66; 65, 74; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:drh; 70, identifier:send_error; 71, argument_list; 71, 72; 71, 73; 72, integer:404; 73, string:"File not found"; 74, return_statement; 74, 75; 75, None; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:f; 79, call; 79, 80; 79, 81; 80, identifier:BytesIO; 81, argument_list; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:json_str; 85, call; 85, 86; 85, 87; 86, identifier:json_dumps; 87, argument_list; 87, 88; 88, identifier:obj; 89, if_statement; 89, 90; 89, 97; 90, call; 90, 91; 90, 92; 91, identifier:isinstance; 92, argument_list; 92, 93; 92, 94; 93, identifier:json_str; 94, tuple; 94, 95; 94, 96; 95, identifier:str; 96, identifier:unicode; 97, block; 97, 98; 97, 113; 98, try_statement; 98, 99; 98, 109; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:json_str; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:json_str; 106, identifier:decode; 107, argument_list; 107, 108; 108, string:'utf8'; 109, except_clause; 109, 110; 109, 111; 110, identifier:AttributeError; 111, block; 111, 112; 112, pass_statement; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:json_str; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:json_str; 119, identifier:encode; 120, argument_list; 120, 121; 121, string:'utf8'; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:f; 126, identifier:write; 127, argument_list; 127, 128; 128, identifier:json_str; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:f; 133, identifier:flush; 134, argument_list; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:size; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:f; 141, identifier:tell; 142, argument_list; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:f; 147, identifier:seek; 148, argument_list; 148, 149; 149, integer:0; 150, comment; 151, if_statement; 151, 152; 151, 157; 152, comparison_operator:>=; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:drh; 155, identifier:request_version; 156, string:"HTTP/1.1"; 157, block; 157, 158; 157, 178; 157, 185; 157, 195; 157, 217; 157, 228; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:e_tag; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, string:"{0:x}"; 164, identifier:format; 165, argument_list; 165, 166; 166, binary_operator:&; 166, 167; 166, 177; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:zlib; 170, identifier:crc32; 171, argument_list; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:f; 175, identifier:read; 176, argument_list; 177, integer:0xFFFFFFFF; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:f; 182, identifier:seek; 183, argument_list; 183, 184; 184, integer:0; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:match; 188, call; 188, 189; 188, 190; 189, identifier:_getheader; 190, argument_list; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:drh; 193, identifier:headers; 194, string:'if-none-match'; 195, if_statement; 195, 196; 195, 199; 196, comparison_operator:is; 196, 197; 196, 198; 197, identifier:match; 198, None; 199, block; 199, 200; 200, if_statement; 200, 201; 200, 208; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:drh; 204, identifier:check_cache; 205, argument_list; 205, 206; 205, 207; 206, identifier:e_tag; 207, identifier:match; 208, block; 208, 209; 208, 215; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:f; 213, identifier:close; 214, argument_list; 215, return_statement; 215, 216; 216, None; 217, expression_statement; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:drh; 221, identifier:send_header; 222, argument_list; 222, 223; 222, 224; 222, 225; 223, string:"ETag"; 224, identifier:e_tag; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:end_header; 227, True; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:drh; 232, identifier:send_header; 233, argument_list; 233, 234; 233, 235; 233, 243; 234, string:"Cache-Control"; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, string:"max-age={0}"; 238, identifier:format; 239, argument_list; 239, 240; 240, attribute; 240, 241; 240, 242; 241, identifier:self; 242, identifier:max_age; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:end_header; 245, True; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:drh; 250, identifier:send_response; 251, argument_list; 251, 252; 252, identifier:code; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:drh; 257, identifier:send_header; 258, argument_list; 258, 259; 258, 260; 259, string:"Content-Type"; 260, identifier:ctype; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:drh; 265, identifier:send_header; 266, argument_list; 266, 267; 266, 268; 267, string:"Content-Length"; 268, identifier:size; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:drh; 273, identifier:end_headers; 274, argument_list; 275, return_statement; 275, 276; 276, identifier:f; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:self; 281, identifier:_add_file_mask; 282, argument_list; 282, 283; 282, 284; 282, 285; 283, identifier:start; 284, identifier:method_str; 285, identifier:send_json | def add_json_mask(self, start, method_str, json_producer):
"""Adds a handler that produces a JSON response.
Parameters
----------
start : string
The URL prefix that must be matched to perform this request.
method_str : string
The HTTP method for which to trigger the request.
json_producer : function(esrh, args)
A function returning an object that can be converted to JSON. The
function takes two arguments. esrh is the QuickServerRequestHandler
object that called the function. args is a map containing the
arguments to the request (i.e., the rest of the URL as path segment
array 'paths', a map of all query fields / flags 'query', the
fragment string 'fragment', and if the method was a POST the JSON
form content 'post'). If the result is None a 404 error is sent.
"""
def send_json(drh, rem_path):
obj = json_producer(drh, rem_path)
if not isinstance(obj, Response):
obj = Response(obj)
ctype = obj.get_ctype("application/json")
code = obj.code
obj = obj.response
if obj is None:
drh.send_error(404, "File not found")
return None
f = BytesIO()
json_str = json_dumps(obj)
if isinstance(json_str, (str, unicode)):
try:
json_str = json_str.decode('utf8')
except AttributeError:
pass
json_str = json_str.encode('utf8')
f.write(json_str)
f.flush()
size = f.tell()
f.seek(0)
# handle ETag caching
if drh.request_version >= "HTTP/1.1":
e_tag = "{0:x}".format(zlib.crc32(f.read()) & 0xFFFFFFFF)
f.seek(0)
match = _getheader(drh.headers, 'if-none-match')
if match is not None:
if drh.check_cache(e_tag, match):
f.close()
return None
drh.send_header("ETag", e_tag, end_header=True)
drh.send_header("Cache-Control",
"max-age={0}".format(self.max_age),
end_header=True)
drh.send_response(code)
drh.send_header("Content-Type", ctype)
drh.send_header("Content-Length", size)
drh.end_headers()
return f
self._add_file_mask(start, method_str, send_json) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:add_text_mask; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:start; 6, identifier:method_str; 7, identifier:text_producer; 8, block; 8, 9; 8, 11; 8, 270; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 16; 12, function_name:send_text; 13, parameters; 13, 14; 13, 15; 14, identifier:drh; 15, identifier:rem_path; 16, block; 16, 17; 16, 25; 16, 40; 16, 49; 16, 55; 16, 61; 16, 76; 16, 82; 16, 115; 16, 122; 16, 128; 16, 136; 16, 143; 16, 144; 16, 239; 16, 246; 16, 254; 16, 262; 16, 268; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:text; 20, call; 20, 21; 20, 22; 21, identifier:text_producer; 22, argument_list; 22, 23; 22, 24; 23, identifier:drh; 24, identifier:rem_path; 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:text; 31, identifier:Response; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:text; 36, call; 36, 37; 36, 38; 37, identifier:Response; 38, argument_list; 38, 39; 39, identifier:text; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:ctype; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:text; 46, identifier:get_ctype; 47, argument_list; 47, 48; 48, string:"text/plain"; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:code; 52, attribute; 52, 53; 52, 54; 53, identifier:text; 54, identifier:code; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:text; 58, attribute; 58, 59; 58, 60; 59, identifier:text; 60, identifier:response; 61, if_statement; 61, 62; 61, 65; 62, comparison_operator:is; 62, 63; 62, 64; 63, identifier:text; 64, None; 65, block; 65, 66; 65, 74; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:drh; 70, identifier:send_error; 71, argument_list; 71, 72; 71, 73; 72, integer:404; 73, string:"File not found"; 74, return_statement; 74, 75; 75, None; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:f; 79, call; 79, 80; 79, 81; 80, identifier:BytesIO; 81, argument_list; 82, if_statement; 82, 83; 82, 90; 83, call; 83, 84; 83, 85; 84, identifier:isinstance; 85, argument_list; 85, 86; 85, 87; 86, identifier:text; 87, tuple; 87, 88; 87, 89; 88, identifier:str; 89, identifier:unicode; 90, block; 90, 91; 90, 106; 91, try_statement; 91, 92; 91, 102; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:text; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:text; 99, identifier:decode; 100, argument_list; 100, 101; 101, string:'utf8'; 102, except_clause; 102, 103; 102, 104; 103, identifier:AttributeError; 104, block; 104, 105; 105, pass_statement; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:text; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:text; 112, identifier:encode; 113, argument_list; 113, 114; 114, string:'utf8'; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:f; 119, identifier:write; 120, argument_list; 120, 121; 121, identifier:text; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:f; 126, identifier:flush; 127, argument_list; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:size; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:f; 134, identifier:tell; 135, argument_list; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:f; 140, identifier:seek; 141, argument_list; 141, 142; 142, integer:0; 143, comment; 144, if_statement; 144, 145; 144, 150; 145, comparison_operator:>=; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:drh; 148, identifier:request_version; 149, string:"HTTP/1.1"; 150, block; 150, 151; 150, 171; 150, 178; 150, 188; 150, 210; 150, 221; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:e_tag; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, string:"{0:x}"; 157, identifier:format; 158, argument_list; 158, 159; 159, binary_operator:&; 159, 160; 159, 170; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:zlib; 163, identifier:crc32; 164, argument_list; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:f; 168, identifier:read; 169, argument_list; 170, integer:0xFFFFFFFF; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:f; 175, identifier:seek; 176, argument_list; 176, 177; 177, integer:0; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:match; 181, call; 181, 182; 181, 183; 182, identifier:_getheader; 183, argument_list; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:drh; 186, identifier:headers; 187, string:'if-none-match'; 188, if_statement; 188, 189; 188, 192; 189, comparison_operator:is; 189, 190; 189, 191; 190, identifier:match; 191, None; 192, block; 192, 193; 193, if_statement; 193, 194; 193, 201; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:drh; 197, identifier:check_cache; 198, argument_list; 198, 199; 198, 200; 199, identifier:e_tag; 200, identifier:match; 201, block; 201, 202; 201, 208; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:f; 206, identifier:close; 207, argument_list; 208, return_statement; 208, 209; 209, None; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:drh; 214, identifier:send_header; 215, argument_list; 215, 216; 215, 217; 215, 218; 216, string:"ETag"; 217, identifier:e_tag; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:end_header; 220, True; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:drh; 225, identifier:send_header; 226, argument_list; 226, 227; 226, 228; 226, 236; 227, string:"Cache-Control"; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, string:"max-age={0}"; 231, identifier:format; 232, argument_list; 232, 233; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:max_age; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:end_header; 238, True; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:drh; 243, identifier:send_response; 244, argument_list; 244, 245; 245, identifier:code; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:drh; 250, identifier:send_header; 251, argument_list; 251, 252; 251, 253; 252, string:"Content-Type"; 253, identifier:ctype; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:drh; 258, identifier:send_header; 259, argument_list; 259, 260; 259, 261; 260, string:"Content-Length"; 261, identifier:size; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:drh; 266, identifier:end_headers; 267, argument_list; 268, return_statement; 268, 269; 269, identifier:f; 270, expression_statement; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:_add_file_mask; 275, argument_list; 275, 276; 275, 277; 275, 278; 276, identifier:start; 277, identifier:method_str; 278, identifier:send_text | def add_text_mask(self, start, method_str, text_producer):
"""Adds a handler that produces a plain text response.
Parameters
----------
start : string
The URL prefix that must be matched to perform this request.
method_str : string
The HTTP method for which to trigger the request.
text_producer : function(esrh, args)
A function returning a string. The function takes two arguments.
esrh is the QuickServerRequestHandler object that called the
function. args is a map containing the arguments to the request
(i.e., the rest of the URL as path segment array 'paths', a map of
all query fields / flags 'query', the fragment string 'fragment',
and if the method was a POST the JSON form content 'post'). If the
result is None a 404 error is sent.
"""
def send_text(drh, rem_path):
text = text_producer(drh, rem_path)
if not isinstance(text, Response):
text = Response(text)
ctype = text.get_ctype("text/plain")
code = text.code
text = text.response
if text is None:
drh.send_error(404, "File not found")
return None
f = BytesIO()
if isinstance(text, (str, unicode)):
try:
text = text.decode('utf8')
except AttributeError:
pass
text = text.encode('utf8')
f.write(text)
f.flush()
size = f.tell()
f.seek(0)
# handle ETag caching
if drh.request_version >= "HTTP/1.1":
e_tag = "{0:x}".format(zlib.crc32(f.read()) & 0xFFFFFFFF)
f.seek(0)
match = _getheader(drh.headers, 'if-none-match')
if match is not None:
if drh.check_cache(e_tag, match):
f.close()
return None
drh.send_header("ETag", e_tag, end_header=True)
drh.send_header("Cache-Control",
"max-age={0}".format(self.max_age),
end_header=True)
drh.send_response(code)
drh.send_header("Content-Type", ctype)
drh.send_header("Content-Length", size)
drh.end_headers()
return f
self._add_file_mask(start, method_str, send_text) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_token_obj; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:token; 6, default_parameter; 6, 7; 6, 8; 7, identifier:expire; 8, identifier:_token_default; 9, block; 9, 10; 9, 12; 9, 25; 9, 31; 9, 42; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:==; 13, 14; 13, 15; 14, identifier:expire; 15, identifier:_token_default; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:expire; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:get_default_token_expiration; 24, argument_list; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:now; 28, call; 28, 29; 28, 30; 29, identifier:get_time; 30, argument_list; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:until; 34, conditional_expression:if; 34, 35; 34, 38; 34, 41; 35, binary_operator:+; 35, 36; 35, 37; 36, identifier:now; 37, identifier:expire; 38, comparison_operator:is; 38, 39; 38, 40; 39, identifier:expire; 40, None; 41, None; 42, with_statement; 42, 43; 42, 48; 42, 49; 43, with_clause; 43, 44; 44, with_item; 44, 45; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:_token_lock; 48, comment; 49, block; 49, 50; 49, 54; 49, 89; 49, 136; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:first_valid; 53, None; 54, for_statement; 54, 55; 54, 58; 54, 64; 55, tuple_pattern; 55, 56; 55, 57; 56, identifier:pos; 57, identifier:k; 58, call; 58, 59; 58, 60; 59, identifier:enumerate; 60, argument_list; 60, 61; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:_token_timings; 64, block; 64, 65; 64, 75; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:t; 68, subscript; 68, 69; 68, 74; 69, subscript; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:_token_map; 73, identifier:k; 74, integer:0; 75, if_statement; 75, 76; 75, 83; 76, boolean_operator:or; 76, 77; 76, 80; 77, comparison_operator:is; 77, 78; 77, 79; 78, identifier:t; 79, None; 80, comparison_operator:>; 80, 81; 80, 82; 81, identifier:t; 82, identifier:now; 83, block; 83, 84; 83, 88; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:first_valid; 87, identifier:pos; 88, break_statement; 89, if_statement; 89, 90; 89, 93; 89, 106; 90, comparison_operator:is; 90, 91; 90, 92; 91, identifier:first_valid; 92, None; 93, block; 93, 94; 93, 100; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:_token_map; 99, dictionary; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:_token_timings; 105, list:[]; 106, else_clause; 106, 107; 107, block; 107, 108; 107, 124; 108, for_statement; 108, 109; 108, 110; 108, 117; 109, identifier:k; 110, subscript; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:_token_timings; 114, slice; 114, 115; 114, 116; 115, colon; 116, identifier:first_valid; 117, block; 117, 118; 118, delete_statement; 118, 119; 119, subscript; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:_token_map; 123, identifier:k; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:_token_timings; 129, subscript; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:_token_timings; 133, slice; 133, 134; 133, 135; 134, identifier:first_valid; 135, colon; 136, if_statement; 136, 137; 136, 144; 136, 230; 137, boolean_operator:or; 137, 138; 137, 141; 138, comparison_operator:is; 138, 139; 138, 140; 139, identifier:until; 140, None; 141, comparison_operator:>; 141, 142; 141, 143; 142, identifier:until; 143, identifier:now; 144, block; 144, 145; 144, 189; 144, 222; 145, if_statement; 145, 146; 145, 151; 145, 171; 146, comparison_operator:not; 146, 147; 146, 148; 147, identifier:token; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:_token_map; 151, block; 151, 152; 151, 162; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 159; 154, subscript; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:_token_map; 158, identifier:token; 159, tuple; 159, 160; 159, 161; 160, identifier:until; 161, dictionary; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:_token_timings; 168, identifier:append; 169, argument_list; 169, 170; 170, identifier:token; 171, else_clause; 171, 172; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 180; 175, subscript; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:_token_map; 179, identifier:token; 180, tuple; 180, 181; 180, 182; 181, identifier:until; 182, subscript; 182, 183; 182, 188; 183, subscript; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:_token_map; 187, identifier:token; 188, integer:1; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 196; 191, attribute; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:_token_timings; 195, identifier:sort; 196, argument_list; 196, 197; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:key; 199, lambda; 199, 200; 199, 202; 200, lambda_parameters; 200, 201; 201, identifier:k; 202, tuple; 202, 203; 202, 215; 203, conditional_expression:if; 203, 204; 203, 205; 203, 214; 204, integer:1; 205, comparison_operator:is; 205, 206; 205, 213; 206, subscript; 206, 207; 206, 212; 207, subscript; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:_token_map; 211, identifier:k; 212, integer:0; 213, None; 214, integer:0; 215, subscript; 215, 216; 215, 221; 216, subscript; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:self; 219, identifier:_token_map; 220, identifier:k; 221, integer:0; 222, return_statement; 222, 223; 223, subscript; 223, 224; 223, 229; 224, subscript; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:self; 227, identifier:_token_map; 228, identifier:token; 229, integer:1; 230, else_clause; 230, 231; 231, block; 231, 232; 231, 261; 232, if_statement; 232, 233; 232, 238; 233, comparison_operator:in; 233, 234; 233, 235; 234, identifier:token; 235, attribute; 235, 236; 235, 237; 236, identifier:self; 237, identifier:_token_map; 238, block; 238, 239; 238, 255; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:_token_timings; 244, list_comprehension; 244, 245; 244, 246; 244, 251; 245, identifier:k; 246, for_in_clause; 246, 247; 246, 248; 247, identifier:k; 248, attribute; 248, 249; 248, 250; 249, identifier:self; 250, identifier:_token_timings; 251, if_clause; 251, 252; 252, comparison_operator:!=; 252, 253; 252, 254; 253, identifier:k; 254, identifier:token; 255, delete_statement; 255, 256; 256, subscript; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:self; 259, identifier:_token_map; 260, identifier:token; 261, return_statement; 261, 262; 262, dictionary | def get_token_obj(self, token, expire=_token_default):
"""Returns or creates the object associaten with the given token.
Parameters
----------
token : string
The token for the object as returned by `create_token`.
expire : number or None
The number of seconds until the object associated with the token
expires or `None` if it should not expire. If the argument is
omitted the value returned by `get_default_token_expiration` is
used. The expiration of an object is lazy. That means the memory
of the expired object is not freed until the next call of
`get_token_obj`. An expiration of 0 or less immediately frees
the memory of the token.
"""
if expire == _token_default:
expire = self.get_default_token_expiration()
now = get_time()
until = now + expire if expire is not None else None
with self._token_lock:
# _token_timings is keys sorted by time
first_valid = None
for (pos, k) in enumerate(self._token_timings):
t = self._token_map[k][0]
if t is None or t > now:
first_valid = pos
break
if first_valid is None:
self._token_map = {}
self._token_timings = []
else:
for k in self._token_timings[:first_valid]:
del self._token_map[k]
self._token_timings = self._token_timings[first_valid:]
if until is None or until > now:
if token not in self._token_map:
self._token_map[token] = (until, {})
self._token_timings.append(token)
else:
self._token_map[token] = (until, self._token_map[token][1])
self._token_timings.sort(key=lambda k: (
1 if self._token_map[k][0] is None else 0,
self._token_map[k][0]
))
return self._token_map[token][1]
else:
if token in self._token_map:
self._token_timings = [
k for k in self._token_timings if k != token
]
del self._token_map[token]
return {} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:handle_cmd; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:cmd; 6, block; 6, 7; 6, 9; 6, 17; 6, 21; 6, 47; 6, 51; 6, 59; 6, 60; 6, 138; 6, 139; 6, 150; 6, 172; 6, 179; 6, 231; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:cmd; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:cmd; 15, identifier:strip; 16, argument_list; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:segments; 20, list:[]; 21, for_statement; 21, 22; 21, 23; 21, 28; 21, 29; 22, identifier:s; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:cmd; 26, identifier:split; 27, argument_list; 28, comment; 29, block; 29, 30; 29, 39; 29, 40; 30, if_statement; 30, 31; 30, 37; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:s; 34, identifier:startswith; 35, argument_list; 35, 36; 36, string:'#'; 37, block; 37, 38; 38, break_statement; 39, comment; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:segments; 44, identifier:append; 45, argument_list; 45, 46; 46, identifier:s; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:args; 50, list:[]; 51, if_statement; 51, 52; 51, 57; 52, not_operator; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:len; 55, argument_list; 55, 56; 56, identifier:segments; 57, block; 57, 58; 58, return_statement; 59, comment; 60, while_statement; 60, 61; 60, 62; 61, identifier:segments; 62, block; 62, 63; 62, 72; 62, 126; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:cur_cmd; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, string:"_"; 69, identifier:join; 70, argument_list; 70, 71; 71, identifier:segments; 72, if_statement; 72, 73; 72, 78; 73, comparison_operator:in; 73, 74; 73, 75; 74, identifier:cur_cmd; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:_cmd_methods; 78, block; 78, 79; 78, 87; 78, 116; 78, 125; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:argc; 82, subscript; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:_cmd_argc; 86, identifier:cur_cmd; 87, if_statement; 87, 88; 87, 98; 88, boolean_operator:and; 88, 89; 88, 92; 89, comparison_operator:is; 89, 90; 89, 91; 90, identifier:argc; 91, None; 92, comparison_operator:!=; 92, 93; 92, 97; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, identifier:args; 97, identifier:argc; 98, block; 98, 99; 98, 115; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:msg; 102, argument_list; 102, 103; 102, 104; 102, 110; 102, 111; 103, string:'command {0} expects {1} argument(s), got {2}'; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, string:" "; 107, identifier:join; 108, argument_list; 108, 109; 109, identifier:segments; 110, identifier:argc; 111, call; 111, 112; 111, 113; 112, identifier:len; 113, argument_list; 113, 114; 114, identifier:args; 115, return_statement; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 123; 118, subscript; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_cmd_methods; 122, identifier:cur_cmd; 123, argument_list; 123, 124; 124, identifier:args; 125, return_statement; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:args; 130, identifier:insert; 131, argument_list; 131, 132; 131, 133; 132, integer:0; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:segments; 136, identifier:pop; 137, argument_list; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:prefix; 142, binary_operator:+; 142, 143; 142, 149; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, string:'_'; 146, identifier:join; 147, argument_list; 147, 148; 148, identifier:args; 149, string:'_'; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:matches; 153, call; 153, 154; 153, 155; 154, identifier:filter; 155, argument_list; 155, 156; 155, 165; 156, lambda; 156, 157; 156, 159; 157, lambda_parameters; 157, 158; 158, identifier:cmd; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:cmd; 162, identifier:startswith; 163, argument_list; 163, 164; 164, identifier:prefix; 165, call; 165, 166; 165, 171; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:_cmd_methods; 170, identifier:keys; 171, argument_list; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:candidates; 175, call; 175, 176; 175, 177; 176, identifier:set; 177, argument_list; 177, 178; 178, list:[]; 179, for_statement; 179, 180; 179, 181; 179, 182; 180, identifier:m; 181, identifier:matches; 182, block; 182, 183; 182, 195; 182, 206; 182, 224; 183, if_statement; 183, 184; 183, 193; 184, comparison_operator:<=; 184, 185; 184, 189; 185, call; 185, 186; 185, 187; 186, identifier:len; 187, argument_list; 187, 188; 188, identifier:m; 189, call; 189, 190; 189, 191; 190, identifier:len; 191, argument_list; 191, 192; 192, identifier:prefix; 193, block; 193, 194; 194, continue_statement; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:m; 198, subscript; 198, 199; 198, 200; 199, identifier:m; 200, slice; 200, 201; 200, 205; 201, call; 201, 202; 201, 203; 202, identifier:len; 203, argument_list; 203, 204; 204, identifier:prefix; 205, colon; 206, if_statement; 206, 207; 206, 210; 207, comparison_operator:in; 207, 208; 207, 209; 208, string:'_'; 209, identifier:m; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:m; 214, subscript; 214, 215; 214, 216; 215, identifier:m; 216, slice; 216, 217; 216, 218; 217, colon; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:m; 221, identifier:index; 222, argument_list; 222, 223; 223, string:'_'; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:candidates; 228, identifier:add; 229, argument_list; 229, 230; 230, identifier:m; 231, if_statement; 231, 232; 231, 236; 231, 258; 232, call; 232, 233; 232, 234; 233, identifier:len; 234, argument_list; 234, 235; 235, identifier:candidates; 236, block; 236, 237; 236, 248; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 240; 239, identifier:msg; 240, argument_list; 240, 241; 240, 242; 241, string:'command "{0}" needs more arguments:'; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, string:' '; 245, identifier:join; 246, argument_list; 246, 247; 247, identifier:args; 248, for_statement; 248, 249; 248, 250; 248, 251; 249, identifier:c; 250, identifier:candidates; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 255; 254, identifier:msg; 255, argument_list; 255, 256; 255, 257; 256, string:' {0}'; 257, identifier:c; 258, else_clause; 258, 259; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 263; 262, identifier:msg; 263, argument_list; 263, 264; 263, 267; 264, binary_operator:+; 264, 265; 264, 266; 265, string:'command "{0}" invalid; type '; 266, string:'help or use <TAB> for a list of commands'; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, string:' '; 270, identifier:join; 271, argument_list; 271, 272; 272, identifier:args | def handle_cmd(self, cmd):
"""Handles a single server command."""
cmd = cmd.strip()
segments = []
for s in cmd.split():
# remove bash-like comments
if s.startswith('#'):
break
# TODO implement escape sequences (also for \#)
segments.append(s)
args = []
if not len(segments):
return
# process more specific commands first
while segments:
cur_cmd = "_".join(segments)
if cur_cmd in self._cmd_methods:
argc = self._cmd_argc[cur_cmd]
if argc is not None and len(args) != argc:
msg('command {0} expects {1} argument(s), got {2}',
" ".join(segments), argc, len(args))
return
self._cmd_methods[cur_cmd](args)
return
args.insert(0, segments.pop())
# invalid command
prefix = '_'.join(args) + '_'
matches = filter(
lambda cmd: cmd.startswith(prefix), self._cmd_methods.keys())
candidates = set([])
for m in matches:
if len(m) <= len(prefix):
continue
m = m[len(prefix):]
if '_' in m:
m = m[:m.index('_')]
candidates.add(m)
if len(candidates):
msg('command "{0}" needs more arguments:', ' '.join(args))
for c in candidates:
msg(' {0}', c)
else:
msg('command "{0}" invalid; type ' +
'help or use <TAB> for a list of commands',
' '.join(args)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:handle_request; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 18; 5, 46; 5, 52; 5, 56; 5, 62; 5, 81; 5, 175; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:timeout; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:socket; 16, identifier:gettimeout; 17, argument_list; 18, if_statement; 18, 19; 18, 22; 18, 29; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:timeout; 21, None; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:timeout; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:timeout; 29, elif_clause; 29, 30; 29, 35; 30, comparison_operator:is; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:timeout; 34, None; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:timeout; 39, call; 39, 40; 39, 41; 40, identifier:min; 41, argument_list; 41, 42; 41, 43; 42, identifier:timeout; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:timeout; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:ctime; 49, call; 49, 50; 49, 51; 50, identifier:get_time; 51, argument_list; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:done_req; 55, False; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:shutdown_latency; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:shutdown_latency; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:is; 63, 64; 63, 65; 64, identifier:timeout; 65, None; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:shutdown_latency; 70, conditional_expression:if; 70, 71; 70, 76; 70, 77; 70, 80; 71, call; 71, 72; 71, 73; 72, identifier:min; 73, argument_list; 73, 74; 73, 75; 74, identifier:shutdown_latency; 75, identifier:timeout; 76, line_continuation:\; 77, comparison_operator:is; 77, 78; 77, 79; 78, identifier:shutdown_latency; 79, None; 80, identifier:timeout; 81, while_statement; 81, 82; 81, 107; 82, boolean_operator:and; 82, 83; 82, 90; 83, not_operator; 83, 84; 84, parenthesized_expression; 84, 85; 85, boolean_operator:or; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:done; 89, identifier:done_req; 90, parenthesized_expression; 90, 91; 91, boolean_operator:or; 91, 92; 91, 99; 92, boolean_operator:or; 92, 93; 92, 96; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:timeout; 95, None; 96, comparison_operator:==; 96, 97; 96, 98; 97, identifier:timeout; 98, integer:0; 99, comparison_operator:<; 99, 100; 99, 106; 100, parenthesized_expression; 100, 101; 101, binary_operator:-; 101, 102; 101, 105; 102, call; 102, 103; 102, 104; 103, identifier:get_time; 104, argument_list; 105, identifier:ctime; 106, identifier:timeout; 107, block; 107, 108; 107, 153; 107, 169; 108, try_statement; 108, 109; 108, 123; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:fd_sets; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:select; 116, identifier:select; 117, argument_list; 117, 118; 117, 120; 117, 121; 117, 122; 118, list:[self]; 118, 119; 119, identifier:self; 120, list:[]; 121, list:[]; 122, identifier:shutdown_latency; 123, except_clause; 123, 124; 123, 132; 124, as_pattern; 124, 125; 124, 130; 125, tuple; 125, 126; 125, 127; 126, identifier:OSError; 127, attribute; 127, 128; 127, 129; 128, identifier:select; 129, identifier:error; 130, as_pattern_target; 130, 131; 131, identifier:e; 132, block; 132, 133; 132, 145; 132, 146; 133, if_statement; 133, 134; 133, 143; 134, comparison_operator:!=; 134, 135; 134, 140; 135, subscript; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:e; 138, identifier:args; 139, integer:0; 140, attribute; 140, 141; 140, 142; 141, identifier:errno; 142, identifier:EINTR; 143, block; 143, 144; 144, raise_statement; 145, comment; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:fd_sets; 149, list:[[], [], []]; 149, 150; 149, 151; 149, 152; 150, list:[]; 151, list:[]; 152, list:[]; 153, for_statement; 153, 154; 153, 155; 153, 158; 154, identifier:_fd; 155, subscript; 155, 156; 155, 157; 156, identifier:fd_sets; 157, integer:0; 158, block; 158, 159; 158, 163; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:done_req; 162, True; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:_handle_request_noblock; 168, argument_list; 169, if_statement; 169, 170; 169, 173; 170, comparison_operator:==; 170, 171; 170, 172; 171, identifier:timeout; 172, integer:0; 173, block; 173, 174; 174, break_statement; 175, if_statement; 175, 176; 175, 183; 175, 184; 176, not_operator; 176, 177; 177, parenthesized_expression; 177, 178; 178, boolean_operator:or; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:done; 182, identifier:done_req; 183, comment; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:handle_timeout; 190, argument_list | def handle_request(self):
"""Handles an HTTP request.The actual HTTP request is handled using a
different thread.
"""
timeout = self.socket.gettimeout()
if timeout is None:
timeout = self.timeout
elif self.timeout is not None:
timeout = min(timeout, self.timeout)
ctime = get_time()
done_req = False
shutdown_latency = self.shutdown_latency
if timeout is not None:
shutdown_latency = min(shutdown_latency, timeout) \
if shutdown_latency is not None else timeout
while not (self.done or done_req) and (timeout is None or
timeout == 0 or
(get_time() - ctime) < timeout):
try:
fd_sets = select.select([self], [], [], shutdown_latency)
except (OSError, select.error) as e:
if e.args[0] != errno.EINTR:
raise
# treat EINTR as shutdown_latency timeout
fd_sets = [[], [], []]
for _fd in fd_sets[0]:
done_req = True
self._handle_request_noblock()
if timeout == 0:
break
if not (self.done or done_req):
# don't handle timeouts if we should shut down the server instead
self.handle_timeout() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:info_gain_nominal; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:x; 5, identifier:y; 6, identifier:separate_max; 7, block; 7, 8; 7, 10; 7, 19; 7, 20; 7, 31; 7, 38; 7, 39; 7, 50; 7, 51; 7, 52; 7, 65; 7, 103; 7, 112; 7, 122; 7, 322; 7, 323; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:x_vals; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:np; 16, identifier:unique; 17, argument_list; 17, 18; 18, identifier:x; 19, comment; 20, if_statement; 20, 21; 20, 27; 20, 28; 21, comparison_operator:<; 21, 22; 21, 26; 22, call; 22, 23; 22, 24; 23, identifier:len; 24, argument_list; 24, 25; 25, identifier:x_vals; 26, integer:3; 27, comment; 28, block; 28, 29; 29, return_statement; 29, 30; 30, None; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:y_dist; 34, call; 34, 35; 34, 36; 35, identifier:Counter; 36, argument_list; 36, 37; 37, identifier:y; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:h_y; 42, call; 42, 43; 42, 44; 43, identifier:h; 44, argument_list; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:y_dist; 48, identifier:values; 49, argument_list; 50, comment; 51, comment; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, pattern_list; 54, 55; 54, 56; 55, identifier:dist; 56, identifier:splits; 57, call; 57, 58; 57, 59; 58, identifier:nominal_splits; 59, argument_list; 59, 60; 59, 61; 59, 62; 59, 63; 59, 64; 60, identifier:x; 61, identifier:y; 62, identifier:x_vals; 63, identifier:y_dist; 64, identifier:separate_max; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, pattern_list; 67, 68; 67, 69; 68, identifier:indices; 69, identifier:repeat; 70, conditional_expression:if; 70, 71; 70, 81; 70, 87; 71, tuple; 71, 72; 71, 80; 72, call; 72, 73; 72, 74; 73, identifier:range; 74, argument_list; 74, 75; 74, 76; 75, integer:1; 76, call; 76, 77; 76, 78; 77, identifier:len; 78, argument_list; 78, 79; 79, identifier:dist; 80, integer:1; 81, comparison_operator:<; 81, 82; 81, 86; 82, call; 82, 83; 82, 84; 83, identifier:len; 84, argument_list; 84, 85; 85, identifier:dist; 86, integer:50; 87, tuple; 87, 88; 87, 102; 88, call; 88, 89; 88, 90; 89, identifier:range; 90, argument_list; 90, 91; 90, 92; 90, 96; 91, integer:1; 92, call; 92, 93; 92, 94; 93, identifier:len; 94, argument_list; 94, 95; 95, identifier:dist; 96, binary_operator:/; 96, 97; 96, 101; 97, call; 97, 98; 97, 99; 98, identifier:len; 99, argument_list; 99, 100; 100, identifier:dist; 101, integer:10; 102, integer:3; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:interval; 106, binary_operator:/; 106, 107; 106, 111; 107, call; 107, 108; 107, 109; 108, identifier:len; 109, argument_list; 109, 110; 110, identifier:dist; 111, integer:10; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 118; 114, pattern_list; 114, 115; 114, 116; 114, 117; 115, identifier:max_ig; 116, identifier:max_i; 117, identifier:iteration; 118, expression_list; 118, 119; 118, 120; 118, 121; 119, integer:0; 120, integer:1; 121, integer:0; 122, while_statement; 122, 123; 122, 126; 123, comparison_operator:<; 123, 124; 123, 125; 124, identifier:iteration; 125, identifier:repeat; 126, block; 126, 127; 126, 243; 126, 247; 127, for_statement; 127, 128; 127, 129; 127, 130; 128, identifier:i; 129, identifier:indices; 130, block; 130, 131; 130, 148; 130, 149; 130, 166; 130, 167; 130, 200; 130, 228; 130, 229; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:dist0; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:np; 137, identifier:sum; 138, argument_list; 138, 139; 139, list_comprehension; 139, 140; 139, 141; 140, identifier:el; 141, for_in_clause; 141, 142; 141, 143; 142, identifier:el; 143, subscript; 143, 144; 143, 145; 144, identifier:dist; 145, slice; 145, 146; 145, 147; 146, colon; 147, identifier:i; 148, comment; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:dist1; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:np; 155, identifier:sum; 156, argument_list; 156, 157; 157, list_comprehension; 157, 158; 157, 159; 158, identifier:el; 159, for_in_clause; 159, 160; 159, 161; 160, identifier:el; 161, subscript; 161, 162; 161, 163; 162, identifier:dist; 163, slice; 163, 164; 163, 165; 164, identifier:i; 165, colon; 166, comment; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:coef; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:np; 173, identifier:true_divide; 174, argument_list; 174, 175; 174, 196; 175, list:[np.sum(dist0.values()), np.sum(dist1.values())]; 175, 176; 175, 186; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:np; 179, identifier:sum; 180, argument_list; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:dist0; 184, identifier:values; 185, argument_list; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:np; 189, identifier:sum; 190, argument_list; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:dist1; 194, identifier:values; 195, argument_list; 196, call; 196, 197; 196, 198; 197, identifier:len; 198, argument_list; 198, 199; 199, identifier:y; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:ig; 203, binary_operator:-; 203, 204; 203, 205; 204, identifier:h_y; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:np; 208, identifier:dot; 209, argument_list; 209, 210; 209, 211; 210, identifier:coef; 211, list:[h(dist0.values()), h(dist1.values())]; 211, 212; 211, 220; 212, call; 212, 213; 212, 214; 213, identifier:h; 214, argument_list; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:dist0; 218, identifier:values; 219, argument_list; 220, call; 220, 221; 220, 222; 221, identifier:h; 222, argument_list; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:dist1; 226, identifier:values; 227, argument_list; 228, comment; 229, if_statement; 229, 230; 229, 233; 230, comparison_operator:>; 230, 231; 230, 232; 231, identifier:ig; 232, identifier:max_ig; 233, block; 233, 234; 233, 242; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 239; 236, pattern_list; 236, 237; 236, 238; 237, identifier:max_ig; 238, identifier:max_i; 239, expression_list; 239, 240; 239, 241; 240, identifier:ig; 241, identifier:i; 242, comment; 243, expression_statement; 243, 244; 244, augmented_assignment:+=; 244, 245; 244, 246; 245, identifier:iteration; 246, integer:1; 247, if_statement; 247, 248; 247, 251; 248, comparison_operator:>; 248, 249; 248, 250; 249, identifier:repeat; 250, integer:1; 251, block; 251, 252; 251, 261; 251, 282; 251, 293; 251, 309; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:interval; 255, call; 255, 256; 255, 257; 256, identifier:int; 257, argument_list; 257, 258; 258, binary_operator:*; 258, 259; 258, 260; 259, identifier:interval; 260, float:0.5; 261, if_statement; 261, 262; 261, 269; 261, 279; 262, boolean_operator:and; 262, 263; 262, 266; 263, comparison_operator:in; 263, 264; 263, 265; 264, identifier:max_i; 265, identifier:indices; 266, comparison_operator:>; 266, 267; 266, 268; 267, identifier:interval; 268, integer:0; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:middle_index; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:indices; 276, identifier:index; 277, argument_list; 277, 278; 278, identifier:max_i; 279, else_clause; 279, 280; 280, block; 280, 281; 281, break_statement; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:min_index; 285, conditional_expression:if; 285, 286; 285, 287; 285, 290; 286, identifier:middle_index; 287, comparison_operator:==; 287, 288; 287, 289; 288, identifier:middle_index; 289, integer:0; 290, binary_operator:-; 290, 291; 290, 292; 291, identifier:middle_index; 292, integer:1; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:max_index; 296, conditional_expression:if; 296, 297; 296, 298; 296, 306; 297, identifier:middle_index; 298, comparison_operator:==; 298, 299; 298, 300; 299, identifier:middle_index; 300, binary_operator:-; 300, 301; 300, 305; 301, call; 301, 302; 301, 303; 302, identifier:len; 303, argument_list; 303, 304; 304, identifier:indices; 305, integer:1; 306, binary_operator:+; 306, 307; 306, 308; 307, identifier:middle_index; 308, integer:1; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:indices; 312, call; 312, 313; 312, 314; 313, identifier:range; 314, argument_list; 314, 315; 314, 318; 314, 321; 315, subscript; 315, 316; 315, 317; 316, identifier:indices; 317, identifier:min_index; 318, subscript; 318, 319; 318, 320; 319, identifier:indices; 320, identifier:max_index; 321, identifier:interval; 322, comment; 323, return_statement; 323, 324; 324, expression_list; 324, 325; 324, 329; 325, call; 325, 326; 325, 327; 326, identifier:float; 327, argument_list; 327, 328; 328, identifier:max_ig; 329, list:[splits[:max_i], splits[max_i:]]; 329, 330; 329, 335; 330, subscript; 330, 331; 330, 332; 331, identifier:splits; 332, slice; 332, 333; 332, 334; 333, colon; 334, identifier:max_i; 335, subscript; 335, 336; 335, 337; 336, identifier:splits; 337, slice; 337, 338; 337, 339; 338, identifier:max_i; 339, colon | def info_gain_nominal(x, y, separate_max):
"""
Function calculates information gain for discrete features. If feature is continuous it is firstly discretized.
x: numpy array - numerical or discrete feature
y: numpy array - labels
ft: string - feature type ("c" - continuous, "d" - discrete)
split_fun: function - function for discretization of numerical features
"""
x_vals = np.unique(x) # unique values
if len(x_vals) < 3: # if there is just one unique value
return None
y_dist = Counter(y) # label distribution
h_y = h(y_dist.values()) # class entropy
# calculate distributions and splits in accordance with feature type
dist, splits = nominal_splits(x, y, x_vals, y_dist, separate_max)
indices, repeat = (range(1, len(dist)), 1) if len(dist) < 50 else (range(1, len(dist), len(dist) / 10), 3)
interval = len(dist) / 10
max_ig, max_i, iteration = 0, 1, 0
while iteration < repeat:
for i in indices:
dist0 = np.sum([el for el in dist[:i]]) # iter 0: take first distribution
dist1 = np.sum([el for el in dist[i:]]) # iter 0: take the other distributions without first
coef = np.true_divide([np.sum(dist0.values()), np.sum(dist1.values())], len(y))
ig = h_y - np.dot(coef, [h(dist0.values()), h(dist1.values())]) # calculate information gain
if ig > max_ig:
max_ig, max_i = ig, i # store index and value of maximal information gain
iteration += 1
if repeat > 1:
interval = int(interval * 0.5)
if max_i in indices and interval > 0:
middle_index = indices.index(max_i)
else:
break
min_index = middle_index if middle_index == 0 else middle_index - 1
max_index = middle_index if middle_index == len(indices) - 1 else middle_index + 1
indices = range(indices[min_index], indices[max_index], interval)
# store splits of maximal information gain in accordance with feature type
return float(max_ig), [splits[:max_i], splits[max_i:]] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_endpoints_using_catalog_api; 3, parameters; 3, 4; 3, 5; 4, identifier:domain; 5, identifier:token; 6, block; 6, 7; 6, 9; 6, 10; 6, 17; 6, 18; 6, 19; 6, 20; 6, 21; 6, 22; 6, 23; 6, 24; 6, 25; 6, 26; 6, 27; 6, 31; 6, 35; 6, 41; 6, 45; 6, 173; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:headers; 13, dictionary; 13, 14; 14, pair; 14, 15; 14, 16; 15, string:"X-App-Token"; 16, identifier:token; 17, comment; 18, comment; 19, comment; 20, comment; 21, comment; 22, comment; 23, comment; 24, comment; 25, comment; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:uri; 30, string:"http://api.us.socrata.com/api/catalog/v1?domains={0}&offset={1}&limit=1000"; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:ret; 34, list:[]; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:endpoints_thus_far; 38, call; 38, 39; 38, 40; 39, identifier:set; 40, argument_list; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:offset; 44, integer:0; 45, while_statement; 45, 46; 45, 47; 46, True; 47, block; 47, 48; 47, 91; 47, 99; 47, 113; 47, 122; 48, try_statement; 48, 49; 48, 74; 49, block; 49, 50; 49, 68; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:r; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:requests; 56, identifier:get; 57, argument_list; 57, 58; 57, 65; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:uri; 61, identifier:format; 62, argument_list; 62, 63; 62, 64; 63, identifier:domain; 64, identifier:offset; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:headers; 67, identifier:headers; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:r; 72, identifier:raise_for_status; 73, argument_list; 74, except_clause; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:requests; 77, identifier:HTTPError; 78, block; 78, 79; 79, raise_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:requests; 83, identifier:HTTPError; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, string:"An HTTP error was raised during Socrata API ingestion."; 88, identifier:format; 89, argument_list; 89, 90; 90, identifier:domain; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:data; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:r; 97, identifier:json; 98, argument_list; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:endpoints_returned; 102, set_comprehension; 102, 103; 102, 108; 103, subscript; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:r; 106, string:'resource'; 107, string:'id'; 108, for_in_clause; 108, 109; 108, 110; 109, identifier:r; 110, subscript; 110, 111; 110, 112; 111, identifier:data; 112, string:'results'; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:new_endpoints; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:endpoints_returned; 119, identifier:difference; 120, argument_list; 120, 121; 121, identifier:endpoints_thus_far; 122, if_statement; 122, 123; 122, 129; 122, 130; 122, 131; 122, 132; 122, 151; 123, comparison_operator:>=; 123, 124; 123, 128; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, identifier:new_endpoints; 128, integer:999; 129, comment; 130, comment; 131, comment; 132, block; 132, 133; 132, 139; 132, 146; 132, 150; 133, expression_statement; 133, 134; 134, augmented_assignment:+=; 134, 135; 134, 136; 135, identifier:ret; 136, subscript; 136, 137; 136, 138; 137, identifier:data; 138, string:'results'; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:endpoints_thus_far; 143, identifier:update; 144, argument_list; 144, 145; 145, identifier:new_endpoints; 146, expression_statement; 146, 147; 147, augmented_assignment:+=; 147, 148; 147, 149; 148, identifier:offset; 149, integer:1000; 150, continue_statement; 151, else_clause; 151, 152; 151, 153; 152, comment; 153, block; 153, 154; 153, 172; 154, expression_statement; 154, 155; 155, augmented_assignment:+=; 155, 156; 155, 157; 156, identifier:ret; 157, list_comprehension; 157, 158; 157, 159; 157, 164; 158, identifier:r; 159, for_in_clause; 159, 160; 159, 161; 160, identifier:r; 161, subscript; 161, 162; 161, 163; 162, identifier:data; 163, string:'results'; 164, if_clause; 164, 165; 165, comparison_operator:in; 165, 166; 165, 171; 166, subscript; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:r; 169, string:'resource'; 170, string:'id'; 171, identifier:new_endpoints; 172, break_statement; 173, return_statement; 173, 174; 174, identifier:ret | def get_endpoints_using_catalog_api(domain, token):
"""
Implements a raw HTTP GET against the entire Socrata portal for the domain in question. This method uses the
second of the two ways of getting this information, the catalog API.
Parameters
----------
domain: str
A Socrata data portal domain. "data.seattle.gov" or "data.cityofnewyork.us" for example.
token: str
A Socrata application token. Application tokens can be registered by going onto the Socrata portal in
question, creating an account, logging in, going to developer tools, and spawning a token.
Returns
-------
Portal dataset metadata from the catalog API.
"""
# Token required for all requests. Providing login info instead is also possible but I didn't implement it.
headers = {"X-App-Token": token}
# The API will return only 100 requests at a time by default. We can ask for more, but the server seems to start
# to lag after a certain N requested. Instead, let's pick a less conservative pagination limit and spool up with
# offsets.
#
# At the time this library was written, Socrata would return all of its results in a contiguous list. Once you
# maxed out, you wouldn't get any more list items. Later on this was changed so that now if you exhaust portal
# entities, it will actually take you back to the beginning of the list again!
#
# As a result we need to perform our own set-wise check to make sure that what we get isn't just a bit of the
# same list all over again.
uri = "http://api.us.socrata.com/api/catalog/v1?domains={0}&offset={1}&limit=1000"
ret = []
endpoints_thus_far = set()
offset = 0
while True:
try:
r = requests.get(uri.format(domain, offset), headers=headers)
r.raise_for_status()
except requests.HTTPError:
raise requests.HTTPError("An HTTP error was raised during Socrata API ingestion.".format(domain))
data = r.json()
endpoints_returned = {r['resource']['id'] for r in data['results']}
new_endpoints = endpoints_returned.difference(endpoints_thus_far)
if len(new_endpoints) >= 999: # we are continuing to stream
# TODO: 999 not 1000 b/c the API suffers off-by-one errors. Can also do worse, however. Compensate?
# cf. https://github.com/ResidentMario/pysocrata/issues/1
ret += data['results']
endpoints_thus_far.update(new_endpoints)
offset += 1000
continue
else: # we are ending on a stream with some old endpoints on it
ret += [r for r in data['results'] if r['resource']['id'] in new_endpoints]
break
return ret |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:stratify_by_scores; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:scores; 5, default_parameter; 5, 6; 5, 7; 6, identifier:goal_n_strata; 7, string:'auto'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:method; 10, string:'cum_sqrt_F'; 11, default_parameter; 11, 12; 11, 13; 12, identifier:n_bins; 13, string:'auto'; 14, block; 14, 15; 14, 17; 14, 23; 14, 33; 14, 182; 14, 183; 14, 289; 14, 483; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:available_methods; 20, list:['equal_size', 'cum_sqrt_F']; 20, 21; 20, 22; 21, string:'equal_size'; 22, string:'cum_sqrt_F'; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:not; 24, 25; 24, 26; 25, identifier:method; 26, identifier:available_methods; 27, block; 27, 28; 28, raise_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:ValueError; 31, argument_list; 31, 32; 32, string:"method argument is invalid"; 33, if_statement; 33, 34; 33, 43; 33, 44; 33, 45; 34, boolean_operator:or; 34, 35; 34, 39; 35, parenthesized_expression; 35, 36; 36, comparison_operator:==; 36, 37; 36, 38; 37, identifier:method; 38, string:'cum_sqrt_F'; 39, parenthesized_expression; 39, 40; 40, comparison_operator:==; 40, 41; 40, 42; 41, identifier:goal_n_strata; 42, string:'auto'; 43, comment; 44, comment; 45, block; 45, 46; 45, 90; 45, 91; 45, 105; 45, 106; 45, 115; 45, 124; 46, if_statement; 46, 47; 46, 50; 46, 51; 47, comparison_operator:==; 47, 48; 47, 49; 48, identifier:n_bins; 49, string:'auto'; 50, comment; 51, block; 51, 52; 51, 59; 51, 80; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:width_score; 55, call; 55, 56; 55, 57; 56, identifier:_heuristic_bin_width; 57, argument_list; 57, 58; 58, identifier:scores; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:n_bins; 62, call; 62, 63; 62, 78; 63, attribute; 63, 64; 63, 77; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:np; 67, identifier:ceil; 68, argument_list; 68, 69; 69, binary_operator:/; 69, 70; 69, 76; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:sp; 73, identifier:ptp; 74, argument_list; 74, 75; 75, identifier:scores; 76, identifier:width_score; 77, identifier:astype; 78, argument_list; 78, 79; 79, identifier:int; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:print; 83, argument_list; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, string:"Automatically setting n_bins = {}."; 87, identifier:format; 88, argument_list; 88, 89; 89, identifier:n_bins; 90, comment; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 96; 93, pattern_list; 93, 94; 93, 95; 94, identifier:counts; 95, identifier:score_bins; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:np; 99, identifier:histogram; 100, argument_list; 100, 101; 100, 102; 101, identifier:scores; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:bins; 104, identifier:n_bins; 105, comment; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:sqrt_counts; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:np; 112, identifier:sqrt; 113, argument_list; 113, 114; 114, identifier:counts; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:csf; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:np; 121, identifier:cumsum; 122, argument_list; 122, 123; 123, identifier:sqrt_counts; 124, if_statement; 124, 125; 124, 128; 124, 129; 124, 168; 125, comparison_operator:==; 125, 126; 125, 127; 126, identifier:goal_n_strata; 127, string:'auto'; 128, comment; 129, block; 129, 130; 129, 137; 129, 158; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:width_csf; 133, call; 133, 134; 133, 135; 134, identifier:_heuristic_bin_width; 135, argument_list; 135, 136; 136, identifier:csf; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:goal_n_strata; 140, call; 140, 141; 140, 156; 141, attribute; 141, 142; 141, 155; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:np; 145, identifier:ceil; 146, argument_list; 146, 147; 147, binary_operator:/; 147, 148; 147, 154; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:sp; 151, identifier:ptp; 152, argument_list; 152, 153; 153, identifier:csf; 154, identifier:width_csf; 155, identifier:astype; 156, argument_list; 156, 157; 157, identifier:int; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:print; 161, argument_list; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, string:"Automatically setting goal_n_strata = {}."; 165, identifier:format; 166, argument_list; 166, 167; 167, identifier:goal_n_strata; 168, elif_clause; 168, 169; 168, 172; 169, comparison_operator:==; 169, 170; 169, 171; 170, identifier:method; 171, string:'cum_sqrt_F'; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:width_csf; 176, binary_operator:/; 176, 177; 176, 181; 177, subscript; 177, 178; 177, 179; 178, identifier:csf; 179, unary_operator:-; 179, 180; 180, integer:1; 181, identifier:goal_n_strata; 182, comment; 183, if_statement; 183, 184; 183, 187; 184, comparison_operator:==; 184, 185; 184, 186; 185, identifier:method; 186, string:'equal_size'; 187, block; 187, 188; 187, 196; 187, 203; 187, 209; 187, 215; 187, 227; 187, 238; 187, 251; 187, 255; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:sorted_ids; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:scores; 194, identifier:argsort; 195, argument_list; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:n_items; 199, call; 199, 200; 199, 201; 200, identifier:len; 201, argument_list; 201, 202; 202, identifier:sorted_ids; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:quotient; 206, binary_operator://; 206, 207; 206, 208; 207, identifier:n_items; 208, identifier:goal_n_strata; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:remainder; 212, binary_operator:%; 212, 213; 212, 214; 213, identifier:n_items; 214, identifier:goal_n_strata; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:allocations; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:np; 221, identifier:empty; 222, argument_list; 222, 223; 222, 224; 223, identifier:n_items; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:dtype; 226, string:'int'; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:st_pops; 230, list_comprehension; 230, 231; 230, 232; 231, identifier:quotient; 232, for_in_clause; 232, 233; 232, 234; 233, identifier:i; 234, call; 234, 235; 234, 236; 235, identifier:range; 236, argument_list; 236, 237; 237, identifier:goal_n_strata; 238, for_statement; 238, 239; 238, 240; 238, 244; 239, identifier:i; 240, call; 240, 241; 240, 242; 241, identifier:range; 242, argument_list; 242, 243; 243, identifier:remainder; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, augmented_assignment:+=; 246, 247; 246, 250; 247, subscript; 247, 248; 247, 249; 248, identifier:st_pops; 249, identifier:i; 250, integer:1; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:j; 254, integer:0; 255, for_statement; 255, 256; 255, 259; 255, 263; 256, pattern_list; 256, 257; 256, 258; 257, identifier:k; 258, identifier:nk; 259, call; 259, 260; 259, 261; 260, identifier:enumerate; 261, argument_list; 261, 262; 262, identifier:st_pops; 263, block; 263, 264; 263, 268; 263, 274; 263, 285; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:start; 267, identifier:j; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:end; 271, binary_operator:+; 271, 272; 271, 273; 272, identifier:j; 273, identifier:nk; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 284; 276, subscript; 276, 277; 276, 278; 277, identifier:allocations; 278, subscript; 278, 279; 278, 280; 279, identifier:sorted_ids; 280, slice; 280, 281; 280, 282; 280, 283; 281, identifier:start; 282, colon; 283, identifier:end; 284, identifier:k; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:j; 288, identifier:end; 289, if_statement; 289, 290; 289, 293; 290, comparison_operator:==; 290, 291; 290, 292; 291, identifier:method; 292, string:'cum_sqrt_F'; 293, block; 293, 294; 293, 308; 293, 309; 293, 326; 293, 327; 293, 331; 293, 335; 293, 396; 293, 402; 293, 409; 293, 410; 293, 427; 293, 428; 293, 437; 293, 444; 293, 453; 293, 466; 294, if_statement; 294, 295; 294, 298; 295, comparison_operator:>; 295, 296; 295, 297; 296, identifier:goal_n_strata; 297, identifier:n_bins; 298, block; 298, 299; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:warnings; 303, identifier:warn; 304, argument_list; 304, 305; 305, concatenated_string; 305, 306; 305, 307; 306, string:"goal_n_strata > n_bins. "; 307, string:"Consider increasing n_bins."; 308, comment; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:csf_bins; 312, list_comprehension; 312, 313; 312, 316; 313, binary_operator:*; 313, 314; 313, 315; 314, identifier:x; 315, identifier:width_csf; 316, for_in_clause; 316, 317; 316, 318; 317, identifier:x; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:np; 321, identifier:arange; 322, argument_list; 322, 323; 323, binary_operator:+; 323, 324; 323, 325; 324, identifier:goal_n_strata; 325, integer:1; 326, comment; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:j; 330, integer:0; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 334; 333, identifier:new_bins; 334, list:[]; 335, for_statement; 335, 336; 335, 339; 335, 343; 336, tuple_pattern; 336, 337; 336, 338; 337, identifier:idx; 338, identifier:value; 339, call; 339, 340; 339, 341; 340, identifier:enumerate; 341, argument_list; 341, 342; 342, identifier:csf; 343, block; 343, 344; 343, 376; 344, if_statement; 344, 345; 344, 364; 345, boolean_operator:or; 345, 346; 345, 355; 346, comparison_operator:==; 346, 347; 346, 348; 347, identifier:j; 348, parenthesized_expression; 348, 349; 349, binary_operator:-; 349, 350; 349, 354; 350, call; 350, 351; 350, 352; 351, identifier:len; 352, argument_list; 352, 353; 353, identifier:csf_bins; 354, integer:1; 355, comparison_operator:==; 355, 356; 355, 357; 356, identifier:idx; 357, parenthesized_expression; 357, 358; 358, binary_operator:-; 358, 359; 358, 363; 359, call; 359, 360; 359, 361; 360, identifier:len; 361, argument_list; 361, 362; 362, identifier:csf; 363, integer:1; 364, block; 364, 365; 364, 375; 365, expression_statement; 365, 366; 366, call; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, identifier:new_bins; 369, identifier:append; 370, argument_list; 370, 371; 371, subscript; 371, 372; 371, 373; 372, identifier:score_bins; 373, unary_operator:-; 373, 374; 374, integer:1; 375, break_statement; 376, if_statement; 376, 377; 376, 382; 377, comparison_operator:>=; 377, 378; 377, 379; 378, identifier:value; 379, subscript; 379, 380; 379, 381; 380, identifier:csf_bins; 381, identifier:j; 382, block; 382, 383; 382, 392; 383, expression_statement; 383, 384; 384, call; 384, 385; 384, 388; 385, attribute; 385, 386; 385, 387; 386, identifier:new_bins; 387, identifier:append; 388, argument_list; 388, 389; 389, subscript; 389, 390; 389, 391; 390, identifier:score_bins; 391, identifier:idx; 392, expression_statement; 392, 393; 393, augmented_assignment:+=; 393, 394; 393, 395; 394, identifier:j; 395, integer:1; 396, expression_statement; 396, 397; 397, augmented_assignment:-=; 397, 398; 397, 401; 398, subscript; 398, 399; 398, 400; 399, identifier:new_bins; 400, integer:0; 401, float:0.01; 402, expression_statement; 402, 403; 403, augmented_assignment:+=; 403, 404; 403, 408; 404, subscript; 404, 405; 404, 406; 405, identifier:new_bins; 406, unary_operator:-; 406, 407; 407, integer:1; 408, float:0.01; 409, comment; 410, expression_statement; 410, 411; 411, assignment; 411, 412; 411, 413; 412, identifier:allocations; 413, binary_operator:-; 413, 414; 413, 426; 414, call; 414, 415; 414, 418; 415, attribute; 415, 416; 415, 417; 416, identifier:np; 417, identifier:digitize; 418, argument_list; 418, 419; 418, 420; 418, 423; 419, identifier:scores; 420, keyword_argument; 420, 421; 420, 422; 421, identifier:bins; 422, identifier:new_bins; 423, keyword_argument; 423, 424; 423, 425; 424, identifier:right; 425, True; 426, integer:1; 427, comment; 428, expression_statement; 428, 429; 429, assignment; 429, 430; 429, 431; 430, identifier:nonempty_ids; 431, call; 431, 432; 431, 435; 432, attribute; 432, 433; 432, 434; 433, identifier:np; 434, identifier:unique; 435, argument_list; 435, 436; 436, identifier:allocations; 437, expression_statement; 437, 438; 438, assignment; 438, 439; 438, 440; 439, identifier:n_strata; 440, call; 440, 441; 440, 442; 441, identifier:len; 442, argument_list; 442, 443; 443, identifier:nonempty_ids; 444, expression_statement; 444, 445; 445, assignment; 445, 446; 445, 447; 446, identifier:indices; 447, call; 447, 448; 447, 451; 448, attribute; 448, 449; 448, 450; 449, identifier:np; 450, identifier:arange; 451, argument_list; 451, 452; 452, identifier:n_strata; 453, expression_statement; 453, 454; 454, assignment; 454, 455; 454, 456; 455, identifier:allocations; 456, call; 456, 457; 456, 460; 457, attribute; 457, 458; 457, 459; 458, identifier:np; 459, identifier:digitize; 460, argument_list; 460, 461; 460, 462; 460, 463; 461, identifier:allocations; 462, identifier:nonempty_ids; 463, keyword_argument; 463, 464; 463, 465; 464, identifier:right; 465, True; 466, if_statement; 466, 467; 466, 470; 467, comparison_operator:<; 467, 468; 467, 469; 468, identifier:n_strata; 469, identifier:goal_n_strata; 470, block; 470, 471; 471, expression_statement; 471, 472; 472, call; 472, 473; 472, 476; 473, attribute; 473, 474; 473, 475; 474, identifier:warnings; 475, identifier:warn; 476, argument_list; 476, 477; 477, call; 477, 478; 477, 481; 478, attribute; 478, 479; 478, 480; 479, string:"Failed to create {} strata"; 480, identifier:format; 481, argument_list; 481, 482; 482, identifier:goal_n_strata; 483, return_statement; 483, 484; 484, call; 484, 485; 484, 486; 485, identifier:Strata; 486, argument_list; 486, 487; 487, identifier:allocations | def stratify_by_scores(scores, goal_n_strata='auto', method='cum_sqrt_F',
n_bins = 'auto'):
"""Stratify by binning the items based on their scores
Parameters
----------
scores : array-like, shape=(n_items,)
ordered array of scores which quantify the classifier confidence for
the items in the pool. High scores indicate a high confidence that
the true label is a "1" (and vice versa for label "0").
goal_n_strata : int or 'auto', optional, default 'auto'
desired number of strata. If set to 'auto', the number is selected
using the Freedman-Diaconis rule. Note that for the 'cum_sqrt_F' method
this number is a goal -- the actual number of strata created may be
less than the goal.
method : {'cum_sqrt_F' or 'equal_size'}, optional, default 'cum_sqrt_F'
stratification method to use. 'equal_size' aims to create s
Other Parameters
----------------
n_bins : int or 'auto', optional, default 'auto'
specify the number of bins to use when estimating the distribution of
the score function. This is used when ``goal_n_strata = 'auto'``
and/or when ``method = 'cum_sqrt_F'``. If set to 'auto', the number is
selected using the Freedman-Diaconis rule.
Returns
-------
Strata instance
"""
available_methods = ['equal_size', 'cum_sqrt_F']
if method not in available_methods:
raise ValueError("method argument is invalid")
if (method == 'cum_sqrt_F') or (goal_n_strata == 'auto'):
# computation below is needed for cum_sqrt_F method OR if we need to
# determine the number of strata for equal_size method automatically
if n_bins == 'auto':
# choose n_bins heuristically
width_score = _heuristic_bin_width(scores)
n_bins = np.ceil(sp.ptp(scores)/width_score).astype(int)
print("Automatically setting n_bins = {}.".format(n_bins))
# approx distribution of scores -- called F
counts, score_bins = np.histogram(scores, bins=n_bins)
# generate cumulative dist of sqrt(F)
sqrt_counts = np.sqrt(counts)
csf = np.cumsum(sqrt_counts)
if goal_n_strata == 'auto':
# choose heuristically
width_csf = _heuristic_bin_width(csf)
goal_n_strata = np.ceil(sp.ptp(csf)/width_csf).astype(int)
print("Automatically setting goal_n_strata = {}.".format(goal_n_strata))
elif method == 'cum_sqrt_F':
width_csf = csf[-1]/goal_n_strata
# goal_n_strata is now guaranteed to have a valid integer value
if method == 'equal_size':
sorted_ids = scores.argsort()
n_items = len(sorted_ids)
quotient = n_items // goal_n_strata
remainder = n_items % goal_n_strata
allocations = np.empty(n_items, dtype='int')
st_pops = [quotient for i in range(goal_n_strata)]
for i in range(remainder):
st_pops[i] += 1
j = 0
for k,nk in enumerate(st_pops):
start = j
end = j + nk
allocations[sorted_ids[start:end]] = k
j = end
if method == 'cum_sqrt_F':
if goal_n_strata > n_bins:
warnings.warn("goal_n_strata > n_bins. "
"Consider increasing n_bins.")
# calculate roughly equal bins on cum sqrt(F) scale
csf_bins = [x * width_csf for x in np.arange(goal_n_strata + 1)]
# map cum sqrt(F) bins to score bins
j = 0
new_bins = []
for (idx,value) in enumerate(csf):
if j == (len(csf_bins) - 1) or idx == (len(csf) - 1):
new_bins.append(score_bins[-1])
break
if value >= csf_bins[j]:
new_bins.append(score_bins[idx])
j += 1
new_bins[0] -= 0.01
new_bins[-1] += 0.01
# bin scores based on new_bins
allocations = np.digitize(scores, bins=new_bins, right=True) - 1
# remove empty strata
nonempty_ids = np.unique(allocations)
n_strata = len(nonempty_ids)
indices = np.arange(n_strata)
allocations = np.digitize(allocations, nonempty_ids, right=True)
if n_strata < goal_n_strata:
warnings.warn("Failed to create {} strata".format(goal_n_strata))
return Strata(allocations) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_get_target; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:target; 6, block; 6, 7; 6, 9; 6, 20; 6, 30; 6, 52; 6, 104; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:depth; 12, binary_operator:+; 12, 13; 12, 19; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:target; 16, identifier:count; 17, argument_list; 17, 18; 18, string:'.'; 19, integer:1; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:parts; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:target; 26, identifier:split; 27, argument_list; 27, 28; 27, 29; 28, string:'.'; 29, integer:1; 30, for_statement; 30, 31; 30, 32; 30, 35; 31, identifier:m; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:modules; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 44; 37, comparison_operator:==; 37, 38; 37, 41; 38, subscript; 38, 39; 38, 40; 39, identifier:parts; 40, integer:0; 41, attribute; 41, 42; 41, 43; 42, identifier:m; 43, identifier:name; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 49; 46, comparison_operator:<; 46, 47; 46, 48; 47, identifier:depth; 48, integer:3; 49, block; 49, 50; 50, return_statement; 50, 51; 51, identifier:m; 52, for_statement; 52, 53; 52, 54; 52, 57; 53, identifier:p; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:packages; 57, block; 57, 58; 58, if_statement; 58, 59; 58, 66; 59, comparison_operator:==; 59, 60; 59, 63; 60, subscript; 60, 61; 60, 62; 61, identifier:parts; 62, integer:0; 63, attribute; 63, 64; 63, 65; 64, identifier:p; 65, identifier:name; 66, block; 66, 67; 66, 74; 66, 75; 66, 86; 66, 91; 66, 92; 66, 93; 66, 94; 66, 95; 66, 96; 66, 97; 67, if_statement; 67, 68; 67, 71; 68, comparison_operator:==; 68, 69; 68, 70; 69, identifier:depth; 70, integer:1; 71, block; 71, 72; 72, return_statement; 72, 73; 73, identifier:p; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:target; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:p; 81, identifier:_get_target; 82, argument_list; 82, 83; 83, subscript; 83, 84; 83, 85; 84, identifier:parts; 85, integer:1; 86, if_statement; 86, 87; 86, 88; 87, identifier:target; 88, block; 88, 89; 89, return_statement; 89, 90; 90, identifier:target; 91, comment; 92, comment; 93, comment; 94, comment; 95, comment; 96, comment; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:<; 98, 99; 98, 100; 99, identifier:depth; 100, integer:3; 101, block; 101, 102; 102, return_statement; 102, 103; 103, identifier:p; 104, return_statement; 104, 105; 105, None | def _get_target(self, target):
"""
Get the Package or Module related to given target.
Args:
target (str): target to find.
Returns:
Package/Module: package containing target or corresponding module.
"""
depth = target.count('.') + 1
parts = target.split('.', 1)
for m in self.modules:
if parts[0] == m.name:
if depth < 3:
return m
for p in self.packages:
if parts[0] == p.name:
if depth == 1:
return p
# pylama:ignore=W0212
target = p._get_target(parts[1])
if target:
return target
# FIXME: can lead to internal dep instead of external
# see example with django.contrib.auth.forms
# importing forms from django
# Idea: when parsing files with ast, record what objects
# are defined in the module. Then check here if the given
# part is one of these objects.
if depth < 3:
return p
return None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:by_user; 3, parameters; 3, 4; 4, identifier:config; 5, block; 5, 6; 5, 8; 5, 14; 5, 20; 5, 27; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:client; 11, call; 11, 12; 11, 13; 12, identifier:Client; 13, argument_list; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:client; 18, identifier:prepare_connection; 19, argument_list; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:audit_api; 23, call; 23, 24; 23, 25; 24, identifier:API; 25, argument_list; 25, 26; 26, identifier:client; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:CLI; 31, identifier:parse_membership; 32, argument_list; 32, 33; 32, 34; 33, string:'Groups by User'; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:audit_api; 37, identifier:by_user; 38, argument_list | def by_user(config):
"""Display LDAP group membership sorted by user."""
client = Client()
client.prepare_connection()
audit_api = API(client)
CLI.parse_membership('Groups by User', audit_api.by_user()) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_sortkey; 3, parameters; 3, 4; 4, identifier:table; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 11; 5, 25; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:wfs; 14, call; 14, 15; 14, 16; 15, identifier:WebFeatureService; 16, argument_list; 16, 17; 16, 22; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:url; 19, attribute; 19, 20; 19, 21; 20, identifier:bcdata; 21, identifier:OWS_URL; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:version; 24, string:"2.0.0"; 25, return_statement; 25, 26; 26, subscript; 26, 27; 26, 44; 27, call; 27, 28; 27, 29; 28, identifier:sorted; 29, argument_list; 29, 30; 30, call; 30, 31; 30, 43; 31, attribute; 31, 32; 31, 42; 32, subscript; 32, 33; 32, 41; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:wfs; 36, identifier:get_schema; 37, argument_list; 37, 38; 38, binary_operator:+; 38, 39; 38, 40; 39, string:"pub:"; 40, identifier:table; 41, string:"properties"; 42, identifier:keys; 43, argument_list; 44, integer:0 | def get_sortkey(table):
"""Get a field to sort by
"""
# Just pick the first column in the table in alphabetical order.
# Ideally we would get the primary key from bcdc api, but it doesn't
# seem to be available
wfs = WebFeatureService(url=bcdata.OWS_URL, version="2.0.0")
return sorted(wfs.get_schema("pub:" + table)["properties"].keys())[0] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:define_request; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:dataset; 5, default_parameter; 5, 6; 5, 7; 6, identifier:query; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:crs; 10, string:"epsg:4326"; 11, default_parameter; 11, 12; 11, 13; 12, identifier:bounds; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sortby; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:pagesize; 19, integer:10000; 20, block; 20, 21; 20, 23; 20, 24; 20, 31; 20, 43; 20, 44; 20, 45; 20, 46; 20, 47; 20, 48; 20, 49; 20, 60; 20, 61; 20, 76; 20, 77; 20, 81; 20, 175; 21, expression_statement; 21, 22; 22, comment; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:table; 27, call; 27, 28; 27, 29; 28, identifier:validate_name; 29, argument_list; 29, 30; 30, identifier:dataset; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:n; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:bcdata; 37, identifier:get_count; 38, argument_list; 38, 39; 38, 40; 39, identifier:table; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:query; 42, identifier:query; 43, comment; 44, comment; 45, comment; 46, comment; 47, comment; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:chunks; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:math; 55, identifier:ceil; 56, argument_list; 56, 57; 57, binary_operator:/; 57, 58; 57, 59; 58, identifier:n; 59, identifier:pagesize; 60, comment; 61, if_statement; 61, 62; 61, 68; 62, boolean_operator:and; 62, 63; 62, 66; 63, comparison_operator:>; 63, 64; 63, 65; 64, identifier:chunks; 65, integer:1; 66, not_operator; 66, 67; 67, identifier:sortby; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:sortby; 72, call; 72, 73; 72, 74; 73, identifier:get_sortkey; 74, argument_list; 74, 75; 75, identifier:table; 76, comment; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:param_dicts; 80, list:[]; 81, for_statement; 81, 82; 81, 83; 81, 87; 82, identifier:i; 83, call; 83, 84; 83, 85; 84, identifier:range; 85, argument_list; 85, 86; 86, identifier:chunks; 87, block; 87, 88; 87, 110; 87, 119; 87, 128; 87, 149; 87, 168; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:request; 91, dictionary; 91, 92; 91, 95; 91, 98; 91, 101; 91, 104; 91, 107; 92, pair; 92, 93; 92, 94; 93, string:"service"; 94, string:"WFS"; 95, pair; 95, 96; 95, 97; 96, string:"version"; 97, string:"2.0.0"; 98, pair; 98, 99; 98, 100; 99, string:"request"; 100, string:"GetFeature"; 101, pair; 101, 102; 101, 103; 102, string:"typeName"; 103, identifier:table; 104, pair; 104, 105; 104, 106; 105, string:"outputFormat"; 106, string:"json"; 107, pair; 107, 108; 107, 109; 108, string:"SRSNAME"; 109, identifier:crs; 110, if_statement; 110, 111; 110, 112; 111, identifier:sortby; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:request; 117, string:"sortby"; 118, identifier:sortby; 119, if_statement; 119, 120; 119, 121; 120, identifier:query; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:request; 126, string:"CQL_FILTER"; 127, identifier:query; 128, if_statement; 128, 129; 128, 130; 129, identifier:bounds; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:request; 135, string:"bbox"; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, string:","; 139, identifier:join; 140, argument_list; 140, 141; 141, list_comprehension; 141, 142; 141, 146; 142, call; 142, 143; 142, 144; 143, identifier:str; 144, argument_list; 144, 145; 145, identifier:b; 146, for_in_clause; 146, 147; 146, 148; 147, identifier:b; 148, identifier:bounds; 149, if_statement; 149, 150; 149, 153; 150, comparison_operator:>; 150, 151; 150, 152; 151, identifier:chunks; 152, integer:1; 153, block; 153, 154; 153, 162; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:request; 158, string:"startIndex"; 159, binary_operator:*; 159, 160; 159, 161; 160, identifier:i; 161, identifier:pagesize; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:request; 166, string:"count"; 167, identifier:pagesize; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:param_dicts; 172, identifier:append; 173, argument_list; 173, 174; 174, identifier:request; 175, return_statement; 175, 176; 176, identifier:param_dicts | def define_request(
dataset, query=None, crs="epsg:4326", bounds=None, sortby=None, pagesize=10000
):
"""Define the getfeature request parameters required to download a dataset
References:
- http://www.opengeospatial.org/standards/wfs
- http://docs.geoserver.org/stable/en/user/services/wfs/vendor.html
- http://docs.geoserver.org/latest/en/user/tutorials/cql/cql_tutorial.html
"""
# validate the table name and find out how many features it holds
table = validate_name(dataset)
n = bcdata.get_count(table, query=query)
# DataBC WFS getcapabilities says that it supports paging,
# and the spec says that responses should include 'next URI'
# (section 7.7.4.4.1)....
# But I do not see any next uri in the responses. Instead of following
# the paged urls, for datasets with >10k records, just generate urls
# based on number of features in the dataset.
chunks = math.ceil(n / pagesize)
# if making several requests, we need to sort by something
if chunks > 1 and not sortby:
sortby = get_sortkey(table)
# build the request parameters for each chunk
param_dicts = []
for i in range(chunks):
request = {
"service": "WFS",
"version": "2.0.0",
"request": "GetFeature",
"typeName": table,
"outputFormat": "json",
"SRSNAME": crs,
}
if sortby:
request["sortby"] = sortby
if query:
request["CQL_FILTER"] = query
if bounds:
request["bbox"] = ",".join([str(b) for b in bounds])
if chunks > 1:
request["startIndex"] = i * pagesize
request["count"] = pagesize
param_dicts.append(request)
return param_dicts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_get_sorted; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:resources; 6, block; 6, 7; 6, 9; 6, 13; 6, 51; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:tmp; 12, list:[]; 13, for_statement; 13, 14; 13, 15; 13, 16; 14, identifier:resource; 15, identifier:resources; 16, block; 16, 17; 16, 23; 16, 24; 16, 42; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:path; 20, attribute; 20, 21; 20, 22; 21, identifier:resource; 22, identifier:_path; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:priority; 27, binary_operator:-; 27, 28; 27, 36; 28, binary_operator:*; 28, 29; 28, 35; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:path; 32, identifier:count; 33, argument_list; 33, 34; 34, string:'/'; 35, integer:10; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:path; 39, identifier:count; 40, argument_list; 40, 41; 41, string:'{'; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:tmp; 46, identifier:append; 47, argument_list; 47, 48; 48, tuple; 48, 49; 48, 50; 49, identifier:priority; 50, identifier:resource; 51, return_statement; 51, 52; 52, list_comprehension; 52, 53; 52, 54; 53, identifier:resource; 54, for_in_clause; 54, 55; 54, 58; 55, pattern_list; 55, 56; 55, 57; 56, identifier:prio; 57, identifier:resource; 58, call; 58, 59; 58, 60; 59, identifier:reversed; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:sorted; 63, argument_list; 63, 64; 64, identifier:tmp | def _get_sorted(self, resources):
"""Order the resources by priority - the most specific paths come
first.
:param resources: List of :class:`wsgiservice.resource.Resource`
classes to be served by this application.
"""
tmp = []
for resource in resources:
path = resource._path
# Each slash counts as 10 priority, each variable takes one away
priority = path.count('/') * 10 - path.count('{')
tmp.append((priority, resource))
return [resource for prio, resource in reversed(sorted(tmp))] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:find_new_posts_and_pages; 3, parameters; 3, 4; 4, identifier:db; 5, block; 5, 6; 5, 8; 5, 14; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:Q; 11, call; 11, 12; 11, 13; 12, identifier:Query; 13, argument_list; 14, for_statement; 14, 15; 14, 19; 14, 27; 15, pattern_list; 15, 16; 15, 17; 15, 18; 16, identifier:root; 17, identifier:dirs; 18, identifier:files; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:os; 22, identifier:walk; 23, argument_list; 23, 24; 24, subscript; 24, 25; 24, 26; 25, identifier:CONFIG; 26, string:'content_root'; 27, block; 27, 28; 28, for_statement; 28, 29; 28, 30; 28, 47; 29, identifier:filename; 30, call; 30, 31; 30, 32; 31, identifier:sorted; 32, argument_list; 32, 33; 33, list_comprehension; 33, 34; 33, 35; 33, 38; 34, identifier:f; 35, for_in_clause; 35, 36; 35, 37; 36, identifier:f; 37, identifier:files; 38, if_clause; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:f; 42, identifier:endswith; 43, argument_list; 43, 44; 44, tuple; 44, 45; 44, 46; 45, string:'md'; 46, string:'markdown'; 47, block; 47, 48; 47, 60; 47, 79; 47, 93; 47, 101; 47, 163; 47, 174; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:fullpath; 51, call; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:os; 55, identifier:path; 56, identifier:join; 57, argument_list; 57, 58; 57, 59; 58, identifier:root; 59, identifier:filename; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:_p; 63, call; 63, 64; 63, 77; 64, attribute; 64, 65; 64, 76; 65, subscript; 65, 66; 65, 74; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:fullpath; 69, identifier:split; 70, argument_list; 70, 71; 71, subscript; 71, 72; 71, 73; 72, identifier:CONFIG; 73, string:'content_root'; 74, unary_operator:-; 74, 75; 75, integer:1; 76, identifier:lstrip; 77, argument_list; 77, 78; 78, string:'/'; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:new_mtime; 82, call; 82, 83; 82, 84; 83, identifier:int; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:os; 89, identifier:path; 90, identifier:getmtime; 91, argument_list; 91, 92; 92, identifier:fullpath; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, pattern_list; 95, 96; 95, 97; 96, identifier:e; 97, identifier:item; 98, expression_list; 98, 99; 98, 100; 99, None; 100, None; 101, for_statement; 101, 102; 101, 103; 101, 106; 102, identifier:collection; 103, list:['posts', 'pages']; 103, 104; 103, 105; 104, string:'posts'; 105, string:'pages'; 106, block; 106, 107; 106, 122; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:item; 110, call; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, subscript; 112, 113; 112, 114; 113, identifier:db; 114, identifier:collection; 115, identifier:get; 116, argument_list; 116, 117; 117, comparison_operator:==; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:Q; 120, identifier:filename; 121, identifier:_p; 122, if_statement; 122, 123; 122, 124; 123, identifier:item; 124, block; 124, 125; 124, 162; 125, if_statement; 125, 126; 125, 131; 126, comparison_operator:>; 126, 127; 126, 128; 127, identifier:new_mtime; 128, subscript; 128, 129; 128, 130; 129, identifier:item; 130, string:'mtime'; 131, block; 131, 132; 131, 150; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:db; 137, identifier:collection; 138, identifier:update; 139, argument_list; 139, 140; 139, 144; 140, dictionary; 140, 141; 141, pair; 141, 142; 141, 143; 142, string:'mtime'; 143, identifier:new_mtime; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:doc_ids; 146, list:[item.doc_id]; 146, 147; 147, attribute; 147, 148; 147, 149; 148, identifier:item; 149, identifier:doc_id; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:e; 153, call; 153, 154; 153, 155; 154, identifier:Entry; 155, argument_list; 155, 156; 155, 157; 156, identifier:fullpath; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:doc_id; 159, attribute; 159, 160; 159, 161; 160, identifier:item; 161, identifier:doc_id; 162, break_statement; 163, if_statement; 163, 164; 163, 166; 164, not_operator; 164, 165; 165, identifier:item; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:e; 170, call; 170, 171; 170, 172; 171, identifier:Entry; 172, argument_list; 172, 173; 173, identifier:fullpath; 174, if_statement; 174, 175; 174, 176; 175, identifier:e; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, yield; 178, 179; 179, expression_list; 179, 180; 179, 181; 180, identifier:e; 181, attribute; 181, 182; 181, 183; 182, identifier:e; 183, identifier:id | def find_new_posts_and_pages(db):
"""Walk content dir, put each post and page in the database"""
Q = Query()
for root, dirs, files in os.walk(CONFIG['content_root']):
for filename in sorted([f for f in files if
f.endswith(('md', 'markdown'))]):
fullpath = os.path.join(root, filename)
_p = fullpath.split(CONFIG['content_root'])[-1].lstrip('/')
new_mtime = int(os.path.getmtime(fullpath))
e, item = None, None
for collection in ['posts', 'pages']:
item = db[collection].get(Q.filename == _p)
if item:
if new_mtime > item['mtime']:
db[collection].update({'mtime': new_mtime},
doc_ids=[item.doc_id])
e = Entry(fullpath, doc_id=item.doc_id)
break
if not item:
e = Entry(fullpath)
if e:
yield e, e.id |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_locations; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filter_to_my_group; 7, False; 8, block; 8, 9; 8, 11; 8, 21; 8, 25; 8, 62; 8, 66; 8, 201; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:resp; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:_post; 18, argument_list; 18, 19; 18, 20; 19, string:'general-retrievelocations'; 20, string:'locations'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:groups; 24, dictionary; 25, if_statement; 25, 26; 25, 29; 26, subscript; 26, 27; 26, 28; 27, identifier:resp; 28, string:'groupinfo'; 29, block; 29, 30; 30, for_statement; 30, 31; 30, 32; 30, 35; 31, identifier:group; 32, subscript; 32, 33; 32, 34; 33, identifier:resp; 34, string:'groupinfo'; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 43; 38, subscript; 38, 39; 38, 40; 39, identifier:groups; 40, subscript; 40, 41; 40, 42; 41, identifier:group; 42, string:'id'; 43, call; 43, 44; 43, 45; 44, identifier:Group; 45, argument_list; 45, 46; 45, 54; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:name; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:group; 51, identifier:get; 52, argument_list; 52, 53; 53, string:'name'; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:inventory_id; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:group; 59, identifier:get; 60, argument_list; 60, 61; 61, string:'id'; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:final_resp; 65, list:[]; 66, if_statement; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:resp; 69, string:'data'; 70, block; 70, 71; 70, 91; 71, if_statement; 71, 72; 71, 73; 72, identifier:filter_to_my_group; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:resp; 78, string:'data'; 79, dictionary; 79, 80; 80, pair; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:groupid; 84, subscript; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:resp; 87, string:'data'; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:groupid; 91, for_statement; 91, 92; 91, 95; 91, 102; 92, pattern_list; 92, 93; 92, 94; 93, identifier:groupid; 94, identifier:sublocation; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:resp; 99, string:'data'; 100, identifier:items; 101, argument_list; 102, block; 102, 103; 102, 132; 102, 147; 103, if_statement; 103, 104; 103, 110; 104, comparison_operator:is; 104, 105; 104, 109; 105, call; 105, 106; 105, 107; 106, identifier:type; 107, argument_list; 107, 108; 108, identifier:sublocation; 109, identifier:dict; 110, block; 110, 111; 110, 125; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:sublocation; 114, list_comprehension; 114, 115; 114, 116; 115, identifier:loc; 116, for_in_clause; 116, 117; 116, 120; 117, pattern_list; 117, 118; 117, 119; 118, identifier:_; 119, identifier:loc; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:sublocation; 123, identifier:items; 124, argument_list; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:sublocation; 128, call; 128, 129; 128, 130; 129, identifier:flatten_list; 130, argument_list; 130, 131; 131, identifier:sublocation; 132, if_statement; 132, 133; 132, 139; 133, comparison_operator:is; 133, 134; 133, 138; 134, call; 134, 135; 134, 136; 135, identifier:type; 136, argument_list; 136, 137; 137, identifier:sublocation; 138, identifier:list; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:sublocation; 143, call; 143, 144; 143, 145; 144, identifier:flatten_list; 145, argument_list; 145, 146; 146, identifier:sublocation; 147, for_statement; 147, 148; 147, 149; 147, 150; 148, identifier:location; 149, identifier:sublocation; 150, block; 150, 151; 150, 157; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:group; 154, subscript; 154, 155; 154, 156; 155, identifier:groups; 156, identifier:groupid; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:final_resp; 161, identifier:append; 162, argument_list; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:Location; 165, argument_list; 165, 166; 165, 174; 165, 182; 165, 190; 165, 193; 166, keyword_argument; 166, 167; 166, 168; 167, identifier:name; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:location; 171, identifier:get; 172, argument_list; 172, 173; 173, string:'name'; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:inventory_id; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:location; 179, identifier:get; 180, argument_list; 180, 181; 181, string:'id'; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:parent; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:location; 187, identifier:get; 188, argument_list; 188, 189; 189, string:'parent'; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:group; 192, identifier:group; 193, keyword_argument; 193, 194; 193, 195; 194, identifier:barcode; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:location; 198, identifier:get; 199, argument_list; 199, 200; 200, string:'barcode'; 201, return_statement; 201, 202; 202, identifier:final_resp | def get_locations(self, filter_to_my_group=False):
"""Retrieve Locations listed in ChemInventory"""
resp = self._post('general-retrievelocations', 'locations')
groups = {}
if resp['groupinfo']:
for group in resp['groupinfo']:
groups[group['id']] = Group(
name=group.get('name'),
inventory_id=group.get('id')
)
final_resp = []
if resp['data']:
if filter_to_my_group:
resp['data'] = {self.groupid: resp['data'][self.groupid]}
for groupid, sublocation in resp['data'].items():
if type(sublocation) is dict:
sublocation = [loc for _, loc in sublocation.items()]
sublocation = flatten_list(sublocation)
if type(sublocation) is list:
sublocation = flatten_list(sublocation)
for location in sublocation:
group = groups[groupid]
final_resp.append(Location(
name=location.get('name'),
inventory_id=location.get('id'),
parent=location.get('parent'),
group=group,
barcode=location.get('barcode')
))
return final_resp |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:_image_url; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:image; 5, default_parameter; 5, 6; 5, 7; 6, identifier:dst_color; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:src_color; 10, None; 11, block; 11, 12; 11, 14; 11, 28; 11, 37; 11, 41; 11, 161; 11, 165; 11, 458; 11, 467; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 18; 15, boolean_operator:and; 15, 16; 15, 17; 16, identifier:src_color; 17, identifier:dst_color; 18, block; 18, 19; 19, if_statement; 19, 20; 19, 22; 20, not_operator; 20, 21; 21, identifier:Image; 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:"Images manipulation require PIL"; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:file; 31, attribute; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:StringValue; 34, argument_list; 34, 35; 35, identifier:image; 36, identifier:value; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:path; 40, None; 41, if_statement; 41, 42; 41, 46; 41, 106; 42, call; 42, 43; 42, 44; 43, identifier:callable; 44, argument_list; 44, 45; 45, identifier:STATIC_ROOT; 46, block; 46, 47; 47, try_statement; 47, 48; 47, 100; 48, block; 48, 49; 48, 63; 48, 72; 48, 88; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 54; 51, pattern_list; 51, 52; 51, 53; 52, identifier:_file; 53, identifier:_storage; 54, subscript; 54, 55; 54, 62; 55, call; 55, 56; 55, 57; 56, identifier:list; 57, argument_list; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:STATIC_ROOT; 60, argument_list; 60, 61; 61, identifier:file; 62, integer:0; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:d_obj; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:_storage; 69, identifier:modified_time; 70, argument_list; 70, 71; 71, identifier:_file; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:filetime; 75, call; 75, 76; 75, 77; 76, identifier:int; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:time; 81, identifier:mktime; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:d_obj; 86, identifier:timetuple; 87, argument_list; 88, if_statement; 88, 89; 88, 90; 89, identifier:dst_color; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:path; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:_storage; 97, identifier:open; 98, argument_list; 98, 99; 99, identifier:_file; 100, except_clause; 100, 101; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:filetime; 105, string:'NA'; 106, else_clause; 106, 107; 107, block; 107, 108; 107, 120; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:_path; 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:join; 117, argument_list; 117, 118; 117, 119; 118, identifier:STATIC_ROOT; 119, identifier:file; 120, if_statement; 120, 121; 120, 129; 120, 155; 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:exists; 127, argument_list; 127, 128; 128, identifier:_path; 129, block; 129, 130; 129, 144; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:filetime; 133, call; 133, 134; 133, 135; 134, identifier:int; 135, argument_list; 135, 136; 136, call; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:os; 140, identifier:path; 141, identifier:getmtime; 142, argument_list; 142, 143; 143, identifier:_path; 144, if_statement; 144, 145; 144, 146; 145, identifier:dst_color; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:path; 150, call; 150, 151; 150, 152; 151, identifier:open; 152, argument_list; 152, 153; 152, 154; 153, identifier:_path; 154, string:'rb'; 155, else_clause; 155, 156; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:filetime; 160, string:'NA'; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:BASE_URL; 164, identifier:STATIC_URL; 165, if_statement; 165, 166; 165, 167; 166, identifier:path; 167, block; 167, 168; 167, 199; 167, 222; 167, 254; 167, 261; 167, 297; 167, 303; 167, 315; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:src_color; 171, conditional_expression:if; 171, 172; 171, 194; 171, 195; 172, call; 172, 173; 172, 174; 173, identifier:tuple; 174, generator_expression; 174, 175; 174, 182; 175, call; 175, 176; 175, 177; 176, identifier:int; 177, argument_list; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:round; 180, argument_list; 180, 181; 181, identifier:c; 182, for_in_clause; 182, 183; 182, 184; 183, identifier:c; 184, subscript; 184, 185; 184, 191; 185, attribute; 185, 186; 185, 190; 186, call; 186, 187; 186, 188; 187, identifier:ColorValue; 188, argument_list; 188, 189; 189, identifier:src_color; 190, identifier:value; 191, slice; 191, 192; 191, 193; 192, colon; 193, integer:3; 194, identifier:src_color; 195, tuple; 195, 196; 195, 197; 195, 198; 196, integer:0; 197, integer:0; 198, integer:0; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:dst_color; 202, list_comprehension; 202, 203; 202, 210; 203, call; 203, 204; 203, 205; 204, identifier:int; 205, argument_list; 205, 206; 206, call; 206, 207; 206, 208; 207, identifier:round; 208, argument_list; 208, 209; 209, identifier:c; 210, for_in_clause; 210, 211; 210, 212; 211, identifier:c; 212, subscript; 212, 213; 212, 219; 213, attribute; 213, 214; 213, 218; 214, call; 214, 215; 214, 216; 215, identifier:ColorValue; 216, argument_list; 216, 217; 217, identifier:dst_color; 218, identifier:value; 219, slice; 219, 220; 219, 221; 220, colon; 221, integer:3; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 227; 224, pattern_list; 224, 225; 224, 226; 225, identifier:file_name; 226, identifier:file_ext; 227, call; 227, 228; 227, 233; 228, attribute; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:os; 231, identifier:path; 232, identifier:splitext; 233, argument_list; 233, 234; 234, call; 234, 235; 234, 251; 235, attribute; 235, 236; 235, 250; 236, call; 236, 237; 236, 247; 237, attribute; 237, 238; 237, 246; 238, call; 238, 239; 238, 244; 239, attribute; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:os; 242, identifier:path; 243, identifier:normpath; 244, argument_list; 244, 245; 245, identifier:file; 246, identifier:replace; 247, argument_list; 247, 248; 247, 249; 248, string:'\\'; 249, string:'_'; 250, identifier:replace; 251, argument_list; 251, 252; 251, 253; 252, string:'/'; 253, string:'_'; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:key; 257, tuple; 257, 258; 257, 259; 257, 260; 258, identifier:filetime; 259, identifier:src_color; 260, identifier:dst_color; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:key; 264, binary_operator:+; 264, 265; 264, 268; 265, binary_operator:+; 265, 266; 265, 267; 266, identifier:file_name; 267, string:'-'; 268, call; 268, 269; 268, 294; 269, attribute; 269, 270; 269, 293; 270, call; 270, 271; 270, 291; 271, attribute; 271, 272; 271, 290; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:base64; 275, identifier:urlsafe_b64encode; 276, argument_list; 276, 277; 277, call; 277, 278; 277, 289; 278, attribute; 278, 279; 278, 288; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:hashlib; 282, identifier:md5; 283, argument_list; 283, 284; 284, call; 284, 285; 284, 286; 285, identifier:repr; 286, argument_list; 286, 287; 287, identifier:key; 288, identifier:digest; 289, argument_list; 290, identifier:rstrip; 291, argument_list; 291, 292; 292, string:'='; 293, identifier:replace; 294, argument_list; 294, 295; 294, 296; 295, string:'-'; 296, string:'_'; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 300; 299, identifier:asset_file; 300, binary_operator:+; 300, 301; 300, 302; 301, identifier:key; 302, identifier:file_ext; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:asset_path; 306, call; 306, 307; 306, 312; 307, attribute; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:os; 310, identifier:path; 311, identifier:join; 312, argument_list; 312, 313; 312, 314; 313, identifier:ASSETS_ROOT; 314, identifier:asset_file; 315, if_statement; 315, 316; 315, 324; 315, 347; 316, call; 316, 317; 316, 322; 317, attribute; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:os; 320, identifier:path; 321, identifier:exists; 322, argument_list; 322, 323; 323, identifier:asset_path; 324, block; 324, 325; 324, 329; 324, 333; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:file; 328, identifier:asset_file; 329, expression_statement; 329, 330; 330, assignment; 330, 331; 330, 332; 331, identifier:BASE_URL; 332, identifier:ASSETS_URL; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:filetime; 336, call; 336, 337; 336, 338; 337, identifier:int; 338, argument_list; 338, 339; 339, call; 339, 340; 339, 345; 340, attribute; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:os; 343, identifier:path; 344, identifier:getmtime; 345, argument_list; 345, 346; 346, identifier:asset_path; 347, else_clause; 347, 348; 348, block; 348, 349; 348, 358; 348, 367; 348, 375; 348, 431; 349, expression_statement; 349, 350; 350, assignment; 350, 351; 350, 352; 351, identifier:image; 352, call; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:Image; 355, identifier:open; 356, argument_list; 356, 357; 357, identifier:path; 358, expression_statement; 358, 359; 359, assignment; 359, 360; 359, 361; 360, identifier:image; 361, call; 361, 362; 361, 365; 362, attribute; 362, 363; 362, 364; 363, identifier:image; 364, identifier:convert; 365, argument_list; 365, 366; 366, string:"RGBA"; 367, expression_statement; 367, 368; 368, assignment; 368, 369; 368, 370; 369, identifier:pixdata; 370, call; 370, 371; 370, 374; 371, attribute; 371, 372; 371, 373; 372, identifier:image; 373, identifier:load; 374, argument_list; 375, for_statement; 375, 376; 375, 377; 375, 385; 376, identifier:y; 377, call; 377, 378; 377, 379; 378, identifier:xrange; 379, argument_list; 379, 380; 380, subscript; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:image; 383, identifier:size; 384, integer:1; 385, block; 385, 386; 386, for_statement; 386, 387; 386, 388; 386, 396; 387, identifier:x; 388, call; 388, 389; 388, 390; 389, identifier:xrange; 390, argument_list; 390, 391; 391, subscript; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:image; 394, identifier:size; 395, integer:0; 396, block; 396, 397; 397, if_statement; 397, 398; 397, 408; 398, comparison_operator:==; 398, 399; 398, 407; 399, subscript; 399, 400; 399, 404; 400, subscript; 400, 401; 400, 402; 400, 403; 401, identifier:pixdata; 402, identifier:x; 403, identifier:y; 404, slice; 404, 405; 404, 406; 405, colon; 406, integer:3; 407, identifier:src_color; 408, block; 408, 409; 408, 424; 409, expression_statement; 409, 410; 410, assignment; 410, 411; 410, 412; 411, identifier:new_color; 412, call; 412, 413; 412, 414; 413, identifier:tuple; 414, argument_list; 414, 415; 415, binary_operator:+; 415, 416; 415, 417; 416, identifier:dst_color; 417, list:[pixdata[x, y][3]]; 417, 418; 418, subscript; 418, 419; 418, 423; 419, subscript; 419, 420; 419, 421; 419, 422; 420, identifier:pixdata; 421, identifier:x; 422, identifier:y; 423, integer:3; 424, expression_statement; 424, 425; 425, assignment; 425, 426; 425, 430; 426, subscript; 426, 427; 426, 428; 426, 429; 427, identifier:pixdata; 428, identifier:x; 429, identifier:y; 430, identifier:new_color; 431, try_statement; 431, 432; 431, 448; 432, block; 432, 433; 432, 440; 432, 444; 433, expression_statement; 433, 434; 434, call; 434, 435; 434, 438; 435, attribute; 435, 436; 435, 437; 436, identifier:image; 437, identifier:save; 438, argument_list; 438, 439; 439, identifier:asset_path; 440, expression_statement; 440, 441; 441, assignment; 441, 442; 441, 443; 442, identifier:file; 443, identifier:asset_file; 444, expression_statement; 444, 445; 445, assignment; 445, 446; 445, 447; 446, identifier:BASE_URL; 447, identifier:ASSETS_URL; 448, except_clause; 448, 449; 448, 450; 449, identifier:IOError; 450, block; 450, 451; 451, expression_statement; 451, 452; 452, call; 452, 453; 452, 456; 453, attribute; 453, 454; 453, 455; 454, identifier:log; 455, identifier:exception; 456, argument_list; 456, 457; 457, string:"Error while saving image"; 458, expression_statement; 458, 459; 459, assignment; 459, 460; 459, 461; 460, identifier:url; 461, binary_operator:%; 461, 462; 461, 463; 462, string:'url("%s%s?_=%s")'; 463, tuple; 463, 464; 463, 465; 463, 466; 464, identifier:BASE_URL; 465, identifier:file; 466, identifier:filetime; 467, return_statement; 467, 468; 468, call; 468, 469; 468, 470; 469, identifier:StringValue; 470, argument_list; 470, 471; 471, identifier:url | def _image_url(image, dst_color=None, src_color=None):
"""
Generates a path to an asset found relative to the project's images
directory.
"""
if src_color and dst_color:
if not Image:
raise Exception("Images manipulation require PIL")
file = StringValue(image).value
path = None
if callable(STATIC_ROOT):
try:
_file, _storage = list(STATIC_ROOT(file))[0]
d_obj = _storage.modified_time(_file)
filetime = int(time.mktime(d_obj.timetuple()))
if dst_color:
path = _storage.open(_file)
except:
filetime = 'NA'
else:
_path = os.path.join(STATIC_ROOT, file)
if os.path.exists(_path):
filetime = int(os.path.getmtime(_path))
if dst_color:
path = open(_path, 'rb')
else:
filetime = 'NA'
BASE_URL = STATIC_URL
if path:
src_color = tuple(int(round(c)) for c in ColorValue(
src_color).value[:3]) if src_color else (0, 0, 0)
dst_color = [int(round(c)) for c in ColorValue(dst_color).value[:3]]
file_name, file_ext = os.path.splitext(
os.path.normpath(file).replace('\\', '_').replace('/', '_'))
key = (filetime, src_color, dst_color)
key = file_name + '-' + base64.urlsafe_b64encode(
hashlib.md5(repr(key)).digest()).rstrip('=').replace('-', '_')
asset_file = key + file_ext
asset_path = os.path.join(ASSETS_ROOT, asset_file)
if os.path.exists(asset_path):
file = asset_file
BASE_URL = ASSETS_URL
filetime = int(os.path.getmtime(asset_path))
else:
image = Image.open(path)
image = image.convert("RGBA")
pixdata = image.load()
for y in xrange(image.size[1]):
for x in xrange(image.size[0]):
if pixdata[x, y][:3] == src_color:
new_color = tuple(dst_color + [pixdata[x, y][3]])
pixdata[x, y] = new_color
try:
image.save(asset_path)
file = asset_file
BASE_URL = ASSETS_URL
except IOError:
log.exception("Error while saving image")
url = 'url("%s%s?_=%s")' % (BASE_URL, file, filetime)
return StringValue(url) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:normalize_selectors; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:_selectors; 6, default_parameter; 6, 7; 6, 8; 7, identifier:extra_selectors; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:extra_parents; 11, None; 12, block; 12, 13; 12, 15; 12, 16; 12, 26; 12, 42; 12, 58; 12, 64; 12, 171; 12, 194; 12, 201; 12, 207; 12, 230; 12, 237; 12, 262; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:_selectors; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:_spaces_re; 22, identifier:sub; 23, argument_list; 23, 24; 23, 25; 24, string:' '; 25, identifier:_selectors; 26, if_statement; 26, 27; 26, 32; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 31; 30, identifier:extra_selectors; 31, identifier:basestring; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:extra_selectors; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:extra_selectors; 39, identifier:split; 40, argument_list; 40, 41; 41, string:','; 42, if_statement; 42, 43; 42, 48; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:extra_parents; 47, identifier:basestring; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:extra_parents; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:extra_parents; 55, identifier:split; 56, argument_list; 56, 57; 57, string:'&'; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:parents; 61, call; 61, 62; 61, 63; 62, identifier:set; 63, argument_list; 64, if_statement; 64, 65; 64, 68; 64, 144; 65, comparison_operator:in; 65, 66; 65, 67; 66, string:' extends '; 67, identifier:_selectors; 68, block; 68, 69; 68, 75; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:selectors; 72, call; 72, 73; 72, 74; 73, identifier:set; 74, argument_list; 75, for_statement; 75, 76; 75, 77; 75, 83; 76, identifier:key; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:_selectors; 80, identifier:split; 81, argument_list; 81, 82; 82, string:','; 83, block; 83, 84; 83, 96; 83, 104; 83, 112; 83, 119; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 90; 86, pattern_list; 86, 87; 86, 88; 86, 89; 87, identifier:child; 88, identifier:_; 89, identifier:parent; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:key; 93, identifier:partition; 94, argument_list; 94, 95; 95, string:' extends '; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:child; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:child; 102, identifier:strip; 103, argument_list; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:parent; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:parent; 110, identifier:strip; 111, argument_list; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:selectors; 116, identifier:add; 117, argument_list; 117, 118; 118, identifier:child; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:parents; 123, identifier:update; 124, generator_expression; 124, 125; 124, 130; 124, 138; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:s; 128, identifier:strip; 129, argument_list; 130, for_in_clause; 130, 131; 130, 132; 131, identifier:s; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:parent; 135, identifier:split; 136, argument_list; 136, 137; 137, string:'&'; 138, if_clause; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:s; 142, identifier:strip; 143, argument_list; 144, else_clause; 144, 145; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:selectors; 149, call; 149, 150; 149, 151; 150, identifier:set; 151, generator_expression; 151, 152; 151, 157; 151, 165; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:s; 155, identifier:strip; 156, argument_list; 157, for_in_clause; 157, 158; 157, 159; 158, identifier:s; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:_selectors; 162, identifier:split; 163, argument_list; 163, 164; 164, string:','; 165, if_clause; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:s; 169, identifier:strip; 170, argument_list; 171, if_statement; 171, 172; 171, 173; 172, identifier:extra_selectors; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:selectors; 178, identifier:update; 179, generator_expression; 179, 180; 179, 185; 179, 188; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:s; 183, identifier:strip; 184, argument_list; 185, for_in_clause; 185, 186; 185, 187; 186, identifier:s; 187, identifier:extra_selectors; 188, if_clause; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:s; 192, identifier:strip; 193, argument_list; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:selectors; 198, identifier:discard; 199, argument_list; 199, 200; 200, string:''; 201, if_statement; 201, 202; 201, 204; 202, not_operator; 202, 203; 203, identifier:selectors; 204, block; 204, 205; 205, return_statement; 205, 206; 206, string:''; 207, if_statement; 207, 208; 207, 209; 208, identifier:extra_parents; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:parents; 214, identifier:update; 215, generator_expression; 215, 216; 215, 221; 215, 224; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:s; 219, identifier:strip; 220, argument_list; 221, for_in_clause; 221, 222; 221, 223; 222, identifier:s; 223, identifier:extra_parents; 224, if_clause; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:s; 228, identifier:strip; 229, argument_list; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:parents; 234, identifier:discard; 235, argument_list; 235, 236; 236, string:''; 237, if_statement; 237, 238; 237, 239; 238, identifier:parents; 239, block; 239, 240; 240, return_statement; 240, 241; 241, binary_operator:+; 241, 242; 241, 253; 242, binary_operator:+; 242, 243; 242, 252; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, string:','; 246, identifier:join; 247, argument_list; 247, 248; 248, call; 248, 249; 248, 250; 249, identifier:sorted; 250, argument_list; 250, 251; 251, identifier:selectors; 252, string:' extends '; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, string:'&'; 256, identifier:join; 257, argument_list; 257, 258; 258, call; 258, 259; 258, 260; 259, identifier:sorted; 260, argument_list; 260, 261; 261, identifier:parents; 262, return_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, string:','; 266, identifier:join; 267, argument_list; 267, 268; 268, call; 268, 269; 268, 270; 269, identifier:sorted; 270, argument_list; 270, 271; 271, identifier:selectors | def normalize_selectors(self, _selectors, extra_selectors=None, extra_parents=None):
"""
Normalizes or extends selectors in a string.
An optional extra parameter that can be a list of extra selectors to be
added to the final normalized selectors string.
"""
# Fixe tabs and spaces in selectors
_selectors = _spaces_re.sub(' ', _selectors)
if isinstance(extra_selectors, basestring):
extra_selectors = extra_selectors.split(',')
if isinstance(extra_parents, basestring):
extra_parents = extra_parents.split('&')
parents = set()
if ' extends ' in _selectors:
selectors = set()
for key in _selectors.split(','):
child, _, parent = key.partition(' extends ')
child = child.strip()
parent = parent.strip()
selectors.add(child)
parents.update(
s.strip() for s in parent.split('&') if s.strip())
else:
selectors = set(
s.strip() for s in _selectors.split(',') if s.strip())
if extra_selectors:
selectors.update(s.strip() for s in extra_selectors if s.strip())
selectors.discard('')
if not selectors:
return ''
if extra_parents:
parents.update(s.strip() for s in extra_parents if s.strip())
parents.discard('')
if parents:
return ','.join(sorted(selectors)) + ' extends ' + '&'.join(sorted(parents))
return ','.join(sorted(selectors)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:_get_properties; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 3, 13; 4, identifier:self; 5, identifier:rule; 6, identifier:p_selectors; 7, identifier:p_parents; 8, identifier:p_children; 9, identifier:scope; 10, identifier:media; 11, identifier:c_lineno; 12, identifier:c_property; 13, identifier:c_codestr; 14, block; 14, 15; 14, 17; 14, 37; 14, 58; 14, 66; 14, 83; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:prop; 21, identifier:value; 22, subscript; 22, 23; 22, 34; 23, parenthesized_expression; 23, 24; 24, binary_operator:+; 24, 25; 24, 32; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:_prop_split_re; 28, identifier:split; 29, argument_list; 29, 30; 29, 31; 30, identifier:c_property; 31, integer:1; 32, list:[None]; 32, 33; 33, None; 34, slice; 34, 35; 34, 36; 35, colon; 36, integer:2; 37, try_statement; 37, 38; 37, 51; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:is_var; 42, parenthesized_expression; 42, 43; 43, comparison_operator:==; 43, 44; 43, 50; 44, subscript; 44, 45; 44, 46; 45, identifier:c_property; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, identifier:prop; 50, string:'='; 51, except_clause; 51, 52; 51, 53; 52, identifier:IndexError; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:is_var; 57, False; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:prop; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:prop; 64, identifier:strip; 65, argument_list; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:prop; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:do_glob_math; 73, argument_list; 73, 74; 73, 75; 73, 78; 73, 81; 73, 82; 74, identifier:prop; 75, subscript; 75, 76; 75, 77; 76, identifier:rule; 77, identifier:CONTEXT; 78, subscript; 78, 79; 78, 80; 79, identifier:rule; 80, identifier:OPTIONS; 81, identifier:rule; 82, True; 83, if_statement; 83, 84; 83, 85; 84, identifier:prop; 85, block; 85, 86; 85, 113; 85, 122; 86, if_statement; 86, 87; 86, 88; 87, identifier:value; 88, block; 88, 89; 88, 97; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:value; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:value; 95, identifier:strip; 96, argument_list; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:value; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:calculate; 104, argument_list; 104, 105; 104, 106; 104, 109; 104, 112; 105, identifier:value; 106, subscript; 106, 107; 106, 108; 107, identifier:rule; 108, identifier:CONTEXT; 109, subscript; 109, 110; 109, 111; 110, identifier:rule; 111, identifier:OPTIONS; 112, identifier:rule; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:_prop; 116, binary_operator:+; 116, 117; 116, 121; 117, parenthesized_expression; 117, 118; 118, boolean_operator:or; 118, 119; 118, 120; 119, identifier:scope; 120, string:''; 121, identifier:prop; 122, if_statement; 122, 123; 122, 135; 122, 261; 123, boolean_operator:or; 123, 124; 123, 125; 124, identifier:is_var; 125, boolean_operator:and; 125, 126; 125, 132; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:prop; 129, identifier:startswith; 130, argument_list; 130, 131; 131, string:'$'; 132, comparison_operator:is; 132, 133; 132, 134; 133, identifier:value; 134, None; 135, block; 135, 136; 135, 248; 136, if_statement; 136, 137; 136, 142; 136, 181; 137, call; 137, 138; 137, 139; 138, identifier:isinstance; 139, argument_list; 139, 140; 139, 141; 140, identifier:value; 141, identifier:basestring; 142, block; 142, 143; 143, if_statement; 143, 144; 143, 147; 144, comparison_operator:in; 144, 145; 144, 146; 145, string:'!default'; 146, identifier:value; 147, block; 147, 148; 148, if_statement; 148, 149; 148, 154; 148, 159; 149, comparison_operator:in; 149, 150; 149, 151; 150, identifier:_prop; 151, subscript; 151, 152; 151, 153; 152, identifier:rule; 153, identifier:CONTEXT; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:value; 158, None; 159, else_clause; 159, 160; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:value; 164, call; 164, 165; 164, 180; 165, attribute; 165, 166; 165, 179; 166, call; 166, 167; 166, 176; 167, attribute; 167, 168; 167, 175; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:value; 171, identifier:replace; 172, argument_list; 172, 173; 172, 174; 173, string:'!default'; 174, string:''; 175, identifier:replace; 176, argument_list; 176, 177; 176, 178; 177, string:' '; 178, string:' '; 179, identifier:strip; 180, argument_list; 181, elif_clause; 181, 182; 181, 187; 182, call; 182, 183; 182, 184; 183, identifier:isinstance; 184, argument_list; 184, 185; 184, 186; 185, identifier:value; 186, identifier:ListValue; 187, block; 187, 188; 187, 195; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:value; 191, call; 191, 192; 191, 193; 192, identifier:ListValue; 193, argument_list; 193, 194; 194, identifier:value; 195, for_statement; 195, 196; 195, 199; 195, 206; 196, pattern_list; 196, 197; 196, 198; 197, identifier:k; 198, identifier:v; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:value; 203, identifier:value; 204, identifier:items; 205, argument_list; 206, block; 206, 207; 207, if_statement; 207, 208; 207, 211; 208, comparison_operator:==; 208, 209; 208, 210; 209, identifier:v; 210, string:'!default'; 211, block; 211, 212; 211, 247; 212, if_statement; 212, 213; 212, 218; 212, 223; 213, comparison_operator:in; 213, 214; 213, 215; 214, identifier:_prop; 215, subscript; 215, 216; 215, 217; 216, identifier:rule; 217, identifier:CONTEXT; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:value; 222, None; 223, else_clause; 223, 224; 224, block; 224, 225; 224, 231; 225, delete_statement; 225, 226; 226, subscript; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:value; 229, identifier:value; 230, identifier:k; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:value; 234, conditional_expression:if; 234, 235; 234, 240; 234, 246; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:value; 238, identifier:first; 239, argument_list; 240, comparison_operator:==; 240, 241; 240, 245; 241, call; 241, 242; 241, 243; 242, identifier:len; 243, argument_list; 243, 244; 244, identifier:value; 245, integer:1; 246, identifier:value; 247, break_statement; 248, if_statement; 248, 249; 248, 252; 249, comparison_operator:is; 249, 250; 249, 251; 250, identifier:value; 251, None; 252, block; 252, 253; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 260; 255, subscript; 255, 256; 255, 259; 256, subscript; 256, 257; 256, 258; 257, identifier:rule; 258, identifier:CONTEXT; 259, identifier:_prop; 260, identifier:value; 261, else_clause; 261, 262; 262, block; 262, 263; 262, 280; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:_prop; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:apply_vars; 270, argument_list; 270, 271; 270, 272; 270, 275; 270, 278; 270, 279; 271, identifier:_prop; 272, subscript; 272, 273; 272, 274; 273, identifier:rule; 274, identifier:CONTEXT; 275, subscript; 275, 276; 275, 277; 276, identifier:rule; 277, identifier:OPTIONS; 278, identifier:rule; 279, True; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 287; 282, attribute; 282, 283; 282, 286; 283, subscript; 283, 284; 283, 285; 284, identifier:rule; 285, identifier:PROPERTIES; 286, identifier:append; 287, argument_list; 287, 288; 288, tuple; 288, 289; 288, 290; 288, 291; 289, identifier:c_lineno; 290, identifier:_prop; 291, conditional_expression:if; 291, 292; 291, 296; 291, 299; 292, call; 292, 293; 292, 294; 293, identifier:to_str; 294, argument_list; 294, 295; 295, identifier:value; 296, comparison_operator:is; 296, 297; 296, 298; 297, identifier:value; 298, None; 299, None | def _get_properties(self, rule, p_selectors, p_parents, p_children, scope, media, c_lineno, c_property, c_codestr):
"""
Implements properties and variables extraction
"""
prop, value = (_prop_split_re.split(c_property, 1) + [None])[:2]
try:
is_var = (c_property[len(prop)] == '=')
except IndexError:
is_var = False
prop = prop.strip()
prop = self.do_glob_math(
prop, rule[CONTEXT], rule[OPTIONS], rule, True)
if prop:
if value:
value = value.strip()
value = self.calculate(
value, rule[CONTEXT], rule[OPTIONS], rule)
_prop = (scope or '') + prop
if is_var or prop.startswith('$') and value is not None:
if isinstance(value, basestring):
if '!default' in value:
if _prop in rule[CONTEXT]:
value = None
else:
value = value.replace(
'!default', '').replace(' ', ' ').strip()
elif isinstance(value, ListValue):
value = ListValue(value)
for k, v in value.value.items():
if v == '!default':
if _prop in rule[CONTEXT]:
value = None
else:
del value.value[k]
value = value.first(
) if len(value) == 1 else value
break
if value is not None:
rule[CONTEXT][_prop] = value
else:
_prop = self.apply_vars(
_prop, rule[CONTEXT], rule[OPTIONS], rule, True)
rule[PROPERTIES].append((c_lineno, _prop,
to_str(value) if value is not None else None)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:link_with_parents; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:parent; 6, identifier:c_selectors; 7, identifier:c_rules; 8, block; 8, 9; 8, 11; 8, 15; 8, 324; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:parent_found; 14, None; 15, for_statement; 15, 16; 15, 19; 15, 26; 16, pattern_list; 16, 17; 16, 18; 17, identifier:p_selectors; 18, identifier:p_rules; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:parts; 24, identifier:items; 25, argument_list; 26, block; 26, 27; 26, 39; 26, 48; 26, 54; 26, 58; 26, 59; 26, 60; 26, 61; 26, 62; 26, 63; 26, 64; 26, 65; 26, 211; 26, 228; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 33; 29, pattern_list; 29, 30; 29, 31; 29, 32; 30, identifier:_p_selectors; 31, identifier:_; 32, identifier:_; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:p_selectors; 36, identifier:partition; 37, argument_list; 37, 38; 38, string:' extends '; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:_p_selectors; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:_p_selectors; 45, identifier:split; 46, argument_list; 46, 47; 47, string:','; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:new_selectors; 51, call; 51, 52; 51, 53; 52, identifier:set; 53, argument_list; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:found; 57, False; 58, comment; 59, comment; 60, comment; 61, comment; 62, comment; 63, comment; 64, comment; 65, for_statement; 65, 66; 65, 67; 65, 68; 66, identifier:p_selector; 67, identifier:_p_selectors; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 73; 69, 74; 69, 75; 69, 76; 69, 77; 70, comparison_operator:in; 70, 71; 70, 72; 71, identifier:parent; 72, identifier:p_selector; 73, comment; 74, comment; 75, comment; 76, comment; 77, block; 77, 78; 78, for_statement; 78, 79; 78, 80; 78, 86; 78, 87; 79, identifier:c_selector; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:c_selectors; 83, identifier:split; 84, argument_list; 84, 85; 85, string:','; 86, comment; 87, block; 87, 88; 87, 96; 87, 106; 87, 125; 87, 135; 87, 156; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 93; 90, pattern_list; 90, 91; 90, 92; 91, identifier:_c_selector; 92, identifier:_parent; 93, expression_list; 93, 94; 93, 95; 94, identifier:c_selector; 95, identifier:parent; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:lcp; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:longest_common_prefix; 103, argument_list; 103, 104; 103, 105; 104, identifier:_c_selector; 105, identifier:_parent; 106, if_statement; 106, 107; 106, 108; 107, identifier:lcp; 108, block; 108, 109; 108, 117; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:_c_selector; 112, subscript; 112, 113; 112, 114; 113, identifier:_c_selector; 114, slice; 114, 115; 114, 116; 115, identifier:lcp; 116, colon; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:_parent; 120, subscript; 120, 121; 120, 122; 121, identifier:_parent; 122, slice; 122, 123; 122, 124; 123, identifier:lcp; 124, colon; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:lcs; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:longest_common_suffix; 132, argument_list; 132, 133; 132, 134; 133, identifier:_c_selector; 134, identifier:_parent; 135, if_statement; 135, 136; 135, 137; 136, identifier:lcs; 137, block; 137, 138; 137, 147; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:_c_selector; 141, subscript; 141, 142; 141, 143; 142, identifier:_c_selector; 143, slice; 143, 144; 143, 145; 144, colon; 145, unary_operator:-; 145, 146; 146, identifier:lcs; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:_parent; 150, subscript; 150, 151; 150, 152; 151, identifier:_parent; 152, slice; 152, 153; 152, 154; 153, colon; 154, unary_operator:-; 154, 155; 155, identifier:lcs; 156, if_statement; 156, 157; 156, 160; 156, 161; 157, boolean_operator:and; 157, 158; 157, 159; 158, identifier:_c_selector; 159, identifier:_parent; 160, comment; 161, block; 161, 162; 161, 176; 161, 180; 161, 195; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:prev_symbol; 165, conditional_expression:if; 165, 166; 165, 167; 165, 175; 166, string:'(?<![#.:])'; 167, comparison_operator:in; 167, 168; 167, 171; 168, subscript; 168, 169; 168, 170; 169, identifier:_parent; 170, integer:0; 171, tuple; 171, 172; 171, 173; 171, 174; 172, string:'#'; 173, string:'.'; 174, string:':'; 175, string:r'(?<![-\w#.:])'; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:post_symbol; 179, string:r'(?![-\w])'; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:new_parent; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:re; 186, identifier:sub; 187, argument_list; 187, 188; 187, 193; 187, 194; 188, binary_operator:+; 188, 189; 188, 192; 189, binary_operator:+; 189, 190; 189, 191; 190, identifier:prev_symbol; 191, identifier:_parent; 192, identifier:post_symbol; 193, identifier:_c_selector; 194, identifier:p_selector; 195, if_statement; 195, 196; 195, 199; 196, comparison_operator:!=; 196, 197; 196, 198; 197, identifier:p_selector; 198, identifier:new_parent; 199, block; 199, 200; 199, 207; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:new_selectors; 204, identifier:add; 205, argument_list; 205, 206; 206, identifier:new_parent; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:found; 210, True; 211, if_statement; 211, 212; 211, 213; 211, 214; 212, identifier:found; 213, comment; 214, block; 214, 215; 214, 221; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:parent_found; 218, boolean_operator:or; 218, 219; 218, 220; 219, identifier:parent_found; 220, list:[]; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:parent_found; 225, identifier:extend; 226, argument_list; 226, 227; 227, identifier:p_rules; 228, if_statement; 228, 229; 228, 230; 229, identifier:new_selectors; 230, block; 230, 231; 230, 241; 230, 242; 230, 274; 230, 280; 230, 281; 230, 303; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:new_selectors; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:self; 237, identifier:normalize_selectors; 238, argument_list; 238, 239; 238, 240; 239, identifier:p_selectors; 240, identifier:new_selectors; 241, comment; 242, if_statement; 242, 243; 242, 246; 243, comparison_operator:!=; 243, 244; 243, 245; 244, identifier:new_selectors; 245, identifier:p_selectors; 246, block; 246, 247; 246, 253; 246, 263; 247, delete_statement; 247, 248; 248, subscript; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:parts; 252, identifier:p_selectors; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 260; 255, attribute; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:parts; 259, identifier:setdefault; 260, argument_list; 260, 261; 260, 262; 261, identifier:new_selectors; 262, list:[]; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 272; 265, attribute; 265, 266; 265, 271; 266, subscript; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:parts; 270, identifier:new_selectors; 271, identifier:extend; 272, argument_list; 272, 273; 273, identifier:p_rules; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:deps; 277, call; 277, 278; 277, 279; 278, identifier:set; 279, argument_list; 280, comment; 281, for_statement; 281, 282; 281, 283; 281, 286; 282, identifier:c_rule; 283, boolean_operator:or; 283, 284; 283, 285; 284, identifier:c_rules; 285, list:[]; 286, block; 286, 287; 286, 293; 286, 294; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 292; 289, subscript; 289, 290; 289, 291; 290, identifier:c_rule; 291, identifier:SELECTORS; 292, identifier:c_selectors; 293, comment; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:deps; 298, identifier:add; 299, argument_list; 299, 300; 300, subscript; 300, 301; 300, 302; 301, identifier:c_rule; 302, identifier:POSITION; 303, for_statement; 303, 304; 303, 305; 303, 306; 304, identifier:p_rule; 305, identifier:p_rules; 306, block; 306, 307; 306, 313; 306, 314; 306, 323; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 312; 309, subscript; 309, 310; 309, 311; 310, identifier:p_rule; 311, identifier:SELECTORS; 312, identifier:new_selectors; 313, comment; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 321; 316, attribute; 316, 317; 316, 320; 317, subscript; 317, 318; 317, 319; 318, identifier:p_rule; 319, identifier:DEPS; 320, identifier:update; 321, argument_list; 321, 322; 322, identifier:deps; 323, comment; 324, return_statement; 324, 325; 325, identifier:parent_found | def link_with_parents(self, parent, c_selectors, c_rules):
"""
Link with a parent for the current child rule.
If parents found, returns a list of parent rules to the child
"""
parent_found = None
for p_selectors, p_rules in self.parts.items():
_p_selectors, _, _ = p_selectors.partition(' extends ')
_p_selectors = _p_selectors.split(',')
new_selectors = set()
found = False
# Finds all the parent selectors and parent selectors with another
# bind selectors behind. For example, if `.specialClass extends
# .baseClass`,
# and there is a `.baseClass` selector, the extension should create
# `.specialClass` for that rule, but if there's also a `.baseClass
# a`
# it also should create `.specialClass a`
for p_selector in _p_selectors:
if parent in p_selector:
# get the new child selector to add (same as the parent
# selector but with the child name)
# since selectors can be together, separated with # or .
# (i.e. something.parent) check that too:
for c_selector in c_selectors.split(','):
# Get whatever is different between the two selectors:
_c_selector, _parent = c_selector, parent
lcp = self.longest_common_prefix(_c_selector, _parent)
if lcp:
_c_selector = _c_selector[lcp:]
_parent = _parent[lcp:]
lcs = self.longest_common_suffix(_c_selector, _parent)
if lcs:
_c_selector = _c_selector[:-lcs]
_parent = _parent[:-lcs]
if _c_selector and _parent:
# Get the new selectors:
prev_symbol = '(?<![#.:])' if _parent[
0] in ('#', '.', ':') else r'(?<![-\w#.:])'
post_symbol = r'(?![-\w])'
new_parent = re.sub(prev_symbol + _parent +
post_symbol, _c_selector, p_selector)
if p_selector != new_parent:
new_selectors.add(new_parent)
found = True
if found:
# add parent:
parent_found = parent_found or []
parent_found.extend(p_rules)
if new_selectors:
new_selectors = self.normalize_selectors(
p_selectors, new_selectors)
# rename node:
if new_selectors != p_selectors:
del self.parts[p_selectors]
self.parts.setdefault(new_selectors, [])
self.parts[new_selectors].extend(p_rules)
deps = set()
# save child dependencies:
for c_rule in c_rules or []:
c_rule[SELECTORS] = c_selectors # re-set the SELECTORS for the rules
deps.add(c_rule[POSITION])
for p_rule in p_rules:
p_rule[SELECTORS] = new_selectors # re-set the SELECTORS for the rules
p_rule[DEPS].update(
deps) # position is the "index" of the object
return parent_found |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_extends; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 11; 5, 93; 5, 97; 5, 101; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, comment; 11, for_statement; 11, 12; 11, 15; 11, 22; 12, pattern_list; 12, 13; 12, 14; 13, identifier:_selectors; 14, identifier:rules; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:parts; 20, identifier:items; 21, argument_list; 22, block; 22, 23; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:in; 24, 25; 24, 26; 25, string:' extends '; 26, identifier:_selectors; 27, block; 27, 28; 27, 40; 27, 49; 27, 55; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 34; 30, pattern_list; 30, 31; 30, 32; 30, 33; 31, identifier:selectors; 32, identifier:_; 33, identifier:parent; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:_selectors; 37, identifier:partition; 38, argument_list; 38, 39; 39, string:' extends '; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:parents; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:parent; 46, identifier:split; 47, argument_list; 47, 48; 48, string:'&'; 49, delete_statement; 49, 50; 50, subscript; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:parts; 54, identifier:_selectors; 55, for_statement; 55, 56; 55, 57; 55, 58; 56, identifier:parent; 57, identifier:parents; 58, block; 58, 59; 58, 67; 58, 77; 58, 88; 58, 92; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:new_selectors; 62, binary_operator:+; 62, 63; 62, 66; 63, binary_operator:+; 63, 64; 63, 65; 64, identifier:selectors; 65, string:' extends '; 66, identifier:parent; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:parts; 73, identifier:setdefault; 74, argument_list; 74, 75; 74, 76; 75, identifier:new_selectors; 76, list:[]; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 86; 79, attribute; 79, 80; 79, 85; 80, subscript; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:parts; 84, identifier:new_selectors; 85, identifier:extend; 86, argument_list; 86, 87; 87, identifier:rules; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:rules; 91, list:[]; 92, comment; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:cnt; 96, integer:0; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:parents_left; 100, True; 101, while_statement; 101, 102; 101, 107; 102, boolean_operator:and; 102, 103; 102, 104; 103, identifier:parents_left; 104, comparison_operator:<; 104, 105; 104, 106; 105, identifier:cnt; 106, integer:10; 107, block; 107, 108; 107, 112; 107, 116; 108, expression_statement; 108, 109; 109, augmented_assignment:+=; 109, 110; 109, 111; 110, identifier:cnt; 111, integer:1; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:parents_left; 115, False; 116, for_statement; 116, 117; 116, 118; 116, 125; 117, identifier:_selectors; 118, call; 118, 119; 118, 124; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:parts; 123, identifier:keys; 124, argument_list; 125, block; 125, 126; 125, 138; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 132; 128, pattern_list; 128, 129; 128, 130; 128, 131; 129, identifier:selectors; 130, identifier:_; 131, identifier:parent; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:_selectors; 135, identifier:partition; 136, argument_list; 136, 137; 137, string:' extends '; 138, if_statement; 138, 139; 138, 140; 139, identifier:parent; 140, block; 140, 141; 140, 145; 140, 154; 140, 162; 140, 168; 140, 178; 140, 189; 140, 200; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:parents_left; 144, True; 145, if_statement; 145, 146; 145, 151; 146, comparison_operator:not; 146, 147; 146, 148; 147, identifier:_selectors; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:parts; 151, block; 151, 152; 151, 153; 152, continue_statement; 153, comment; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:rules; 157, subscript; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:parts; 161, identifier:_selectors; 162, delete_statement; 162, 163; 163, subscript; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:parts; 167, identifier:_selectors; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 175; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:parts; 174, identifier:setdefault; 175, argument_list; 175, 176; 175, 177; 176, identifier:selectors; 177, list:[]; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 187; 180, attribute; 180, 181; 180, 186; 181, subscript; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:parts; 185, identifier:selectors; 186, identifier:extend; 187, argument_list; 187, 188; 188, identifier:rules; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:parents; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:link_with_parents; 196, argument_list; 196, 197; 196, 198; 196, 199; 197, identifier:parent; 198, identifier:selectors; 199, identifier:rules; 200, if_statement; 200, 201; 200, 204; 200, 213; 201, comparison_operator:is; 201, 202; 201, 203; 202, identifier:parents; 203, None; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:log; 209, identifier:warn; 210, argument_list; 210, 211; 210, 212; 211, string:"Parent rule not found: %s"; 212, identifier:parent; 213, else_clause; 213, 214; 213, 215; 214, comment; 215, block; 215, 216; 215, 220; 215, 224; 215, 246; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:new_context; 219, dictionary; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:new_options; 223, dictionary; 224, for_statement; 224, 225; 224, 226; 224, 227; 225, identifier:parent; 226, identifier:parents; 227, block; 227, 228; 227, 237; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:new_context; 232, identifier:update; 233, argument_list; 233, 234; 234, subscript; 234, 235; 234, 236; 235, identifier:parent; 236, identifier:CONTEXT; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:new_options; 241, identifier:update; 242, argument_list; 242, 243; 243, subscript; 243, 244; 243, 245; 244, identifier:parent; 245, identifier:OPTIONS; 246, for_statement; 246, 247; 246, 248; 246, 249; 247, identifier:rule; 248, identifier:rules; 249, block; 249, 250; 249, 258; 249, 267; 249, 273; 249, 281; 249, 290; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:_new_context; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:new_context; 256, identifier:copy; 257, argument_list; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:_new_context; 262, identifier:update; 263, argument_list; 263, 264; 264, subscript; 264, 265; 264, 266; 265, identifier:rule; 266, identifier:CONTEXT; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 272; 269, subscript; 269, 270; 269, 271; 270, identifier:rule; 271, identifier:CONTEXT; 272, identifier:_new_context; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:_new_options; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:new_options; 279, identifier:copy; 280, argument_list; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:_new_options; 285, identifier:update; 286, argument_list; 286, 287; 287, subscript; 287, 288; 287, 289; 288, identifier:rule; 289, identifier:OPTIONS; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 295; 292, subscript; 292, 293; 292, 294; 293, identifier:rule; 294, identifier:OPTIONS; 295, identifier:_new_options | def parse_extends(self):
"""
For each part, create the inheritance parts from the ' extends '
"""
# To be able to manage multiple extends, you need to
# destroy the actual node and create many nodes that have
# mono extend. The first one gets all the css rules
for _selectors, rules in self.parts.items():
if ' extends ' in _selectors:
selectors, _, parent = _selectors.partition(' extends ')
parents = parent.split('&')
del self.parts[_selectors]
for parent in parents:
new_selectors = selectors + ' extends ' + parent
self.parts.setdefault(new_selectors, [])
self.parts[new_selectors].extend(rules)
rules = []
# further rules extending other parents will be empty
cnt = 0
parents_left = True
while parents_left and cnt < 10:
cnt += 1
parents_left = False
for _selectors in self.parts.keys():
selectors, _, parent = _selectors.partition(' extends ')
if parent:
parents_left = True
if _selectors not in self.parts:
continue # Nodes might have been renamed while linking parents...
rules = self.parts[_selectors]
del self.parts[_selectors]
self.parts.setdefault(selectors, [])
self.parts[selectors].extend(rules)
parents = self.link_with_parents(parent, selectors, rules)
if parents is None:
log.warn("Parent rule not found: %s", parent)
else:
# from the parent, inherit the context and the options:
new_context = {}
new_options = {}
for parent in parents:
new_context.update(parent[CONTEXT])
new_options.update(parent[OPTIONS])
for rule in rules:
_new_context = new_context.copy()
_new_context.update(rule[CONTEXT])
rule[CONTEXT] = _new_context
_new_options = new_options.copy()
_new_options.update(rule[OPTIONS])
rule[OPTIONS] = _new_options |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:scan; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:restrict; 6, block; 6, 7; 6, 9; 6, 10; 6, 202; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, while_statement; 10, 11; 10, 12; 10, 13; 10, 14; 11, True; 12, comment; 13, comment; 14, block; 14, 15; 14, 19; 14, 23; 14, 81; 14, 82; 14, 113; 14, 114; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:best_pat; 18, None; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:best_pat_len; 22, integer:0; 23, for_statement; 23, 24; 23, 27; 23, 30; 23, 31; 24, pattern_list; 24, 25; 24, 26; 25, identifier:p; 26, identifier:regexp; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:patterns; 30, comment; 31, block; 31, 32; 31, 46; 31, 60; 32, if_statement; 32, 33; 32, 44; 33, boolean_operator:and; 33, 34; 33, 39; 34, boolean_operator:and; 34, 35; 34, 36; 35, identifier:restrict; 36, comparison_operator:not; 36, 37; 36, 38; 37, identifier:p; 38, identifier:restrict; 39, comparison_operator:not; 39, 40; 39, 41; 40, identifier:p; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:ignore; 44, block; 44, 45; 45, continue_statement; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:m; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:regexp; 52, identifier:match; 53, argument_list; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:input; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:pos; 60, if_statement; 60, 61; 60, 62; 60, 63; 61, identifier:m; 62, comment; 63, block; 63, 64; 63, 68; 63, 80; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:best_pat; 67, identifier:p; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:best_pat_len; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:m; 77, identifier:group; 78, argument_list; 78, 79; 79, integer:0; 80, break_statement; 81, comment; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:best_pat; 85, None; 86, block; 86, 87; 86, 91; 86, 105; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:msg; 90, string:"Bad Token"; 91, if_statement; 91, 92; 91, 93; 92, identifier:restrict; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:msg; 97, binary_operator:+; 97, 98; 97, 99; 98, string:"Trying to find one of "; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, string:", "; 102, identifier:join; 103, argument_list; 103, 104; 104, identifier:restrict; 105, raise_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:SyntaxError; 108, argument_list; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:pos; 112, identifier:msg; 113, comment; 114, if_statement; 114, 115; 114, 120; 114, 121; 114, 128; 115, comparison_operator:in; 115, 116; 115, 117; 116, identifier:best_pat; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:ignore; 120, comment; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, augmented_assignment:+=; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:pos; 127, identifier:best_pat_len; 128, else_clause; 128, 129; 129, block; 129, 130; 129, 138; 129, 139; 129, 158; 129, 164; 129, 165; 129, 166; 129, 201; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:end_pos; 133, binary_operator:+; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:pos; 137, identifier:best_pat_len; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:token; 142, tuple; 142, 143; 142, 146; 142, 147; 142, 148; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:pos; 146, identifier:end_pos; 147, identifier:best_pat; 148, subscript; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:input; 152, slice; 152, 153; 152, 156; 152, 157; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:pos; 156, colon; 157, identifier:end_pos; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:pos; 163, identifier:end_pos; 164, comment; 165, comment; 166, if_statement; 166, 167; 166, 180; 167, boolean_operator:or; 167, 168; 167, 172; 168, not_operator; 168, 169; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:tokens; 172, comparison_operator:!=; 172, 173; 172, 174; 173, identifier:token; 174, subscript; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:tokens; 178, unary_operator:-; 178, 179; 179, integer:1; 180, block; 180, 181; 180, 190; 180, 199; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:tokens; 187, identifier:append; 188, argument_list; 188, 189; 189, identifier:token; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:restrictions; 196, identifier:append; 197, argument_list; 197, 198; 198, identifier:restrict; 199, return_statement; 199, 200; 200, integer:1; 201, break_statement; 202, return_statement; 202, 203; 203, integer:0 | def scan(self, restrict):
"""
Should scan another token and add it to the list, self.tokens,
and add the restriction to self.restrictions
"""
# Keep looking for a token, ignoring any in self.ignore
while True:
# Search the patterns for a match, with earlier
# tokens in the list having preference
best_pat = None
best_pat_len = 0
for p, regexp in self.patterns:
# First check to see if we're restricting to this token
if restrict and p not in restrict and p not in self.ignore:
continue
m = regexp.match(self.input, self.pos)
if m:
# We got a match
best_pat = p
best_pat_len = len(m.group(0))
break
# If we didn't find anything, raise an error
if best_pat is None:
msg = "Bad Token"
if restrict:
msg = "Trying to find one of " + ", ".join(restrict)
raise SyntaxError(self.pos, msg)
# If we found something that isn't to be ignored, return it
if best_pat in self.ignore:
# This token should be ignored ..
self.pos += best_pat_len
else:
end_pos = self.pos + best_pat_len
# Create a token with this data
token = (
self.pos,
end_pos,
best_pat,
self.input[self.pos:end_pos]
)
self.pos = end_pos
# Only add this token if it's not in the list
# (to prevent looping)
if not self.tokens or token != self.tokens[-1]:
self.tokens.append(token)
self.restrictions.append(restrict)
return 1
break
return 0 |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:read_form_data; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 20; 5, 26; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 9, attribute; 9, 10; 9, 11; 10, identifier:self; 11, identifier:processed_data; 12, block; 12, 13; 13, raise_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:exceptions; 17, identifier:AlreadyProcessed; 18, argument_list; 18, 19; 19, string:'The data has already been processed for this form'; 20, if_statement; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:readonly; 24, block; 24, 25; 25, return_statement; 26, if_statement; 26, 27; 26, 34; 27, comparison_operator:==; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:request; 30, identifier:method; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:method; 34, block; 34, 35; 34, 56; 35, if_statement; 35, 36; 35, 41; 35, 48; 36, comparison_operator:==; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:method; 40, string:'POST'; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:data; 45, attribute; 45, 46; 45, 47; 46, identifier:request; 47, identifier:form; 48, else_clause; 48, 49; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:data; 53, attribute; 53, 54; 53, 55; 54, identifier:request; 55, identifier:args; 56, if_statement; 56, 57; 56, 62; 56, 63; 57, comparison_operator:in; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:submitted_hidden_input_name; 61, identifier:data; 62, comment; 63, block; 63, 64; 63, 70; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:processed_data; 69, True; 70, for_statement; 70, 71; 70, 72; 70, 75; 70, 76; 71, identifier:field; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:all_fields; 75, comment; 76, block; 76, 77; 77, if_statement; 77, 78; 77, 81; 77, 83; 78, attribute; 78, 79; 78, 80; 79, identifier:field; 80, identifier:readonly; 81, block; 81, 82; 82, pass_statement; 83, else_clause; 83, 84; 84, block; 84, 85; 84, 92; 84, 93; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:field; 89, identifier:extract_value; 90, argument_list; 90, 91; 91, identifier:data; 92, comment; 93, if_statement; 93, 94; 93, 100; 94, not_operator; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:field; 98, identifier:validate; 99, argument_list; 100, block; 100, 101; 100, 116; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:log; 105, identifier:debug; 106, argument_list; 106, 107; 107, binary_operator:%; 107, 108; 107, 109; 108, string:'Validation error in field \'%s\': %s'; 109, tuple; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:field; 112, identifier:name; 113, attribute; 113, 114; 113, 115; 114, identifier:field; 115, identifier:error; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:has_errors; 121, True | def read_form_data(self):
"""Attempt to read the form data from the request"""
if self.processed_data:
raise exceptions.AlreadyProcessed('The data has already been processed for this form')
if self.readonly:
return
if request.method == self.method:
if self.method == 'POST':
data = request.form
else:
data = request.args
if self.submitted_hidden_input_name in data:
# The form has been submitted
self.processed_data = True
for field in self.all_fields:
# We need to skip readonly fields
if field.readonly:
pass
else:
field.extract_value(data)
# Validate the field
if not field.validate():
log.debug('Validation error in field \'%s\': %s' % (field.name, field.error))
self.has_errors = True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_imm_trans_setattr; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:name; 6, identifier:value; 7, block; 7, 8; 7, 10; 7, 17; 7, 27; 8, expression_statement; 8, 9; 9, string:'''
An immutable's transient setattr allows params to be set, and runs checks as they are.
'''; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:params; 13, call; 13, 14; 13, 15; 14, identifier:_imm_param_data; 15, argument_list; 15, 16; 16, identifier:self; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:dd; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:object; 23, identifier:__getattribute__; 24, argument_list; 24, 25; 24, 26; 25, identifier:self; 26, string:'__dict__'; 27, if_statement; 27, 28; 27, 31; 27, 171; 28, comparison_operator:in; 28, 29; 28, 30; 29, identifier:name; 30, identifier:params; 31, block; 31, 32; 31, 43; 31, 55; 31, 59; 31, 65; 31, 66; 31, 67; 31, 88; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 40; 34, tuple_pattern; 34, 35; 34, 36; 34, 37; 34, 38; 34, 39; 35, identifier:_; 36, identifier:tx_fn; 37, identifier:arg_lists; 38, identifier:check_fns; 39, identifier:deps; 40, subscript; 40, 41; 40, 42; 41, identifier:params; 42, identifier:name; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:value; 46, conditional_expression:if; 46, 47; 46, 48; 46, 51; 47, identifier:value; 48, comparison_operator:is; 48, 49; 48, 50; 49, identifier:tx_fn; 50, None; 51, call; 51, 52; 51, 53; 52, identifier:tx_fn; 53, argument_list; 53, 54; 54, identifier:value; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:old_deps; 58, dictionary; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:orig_value; 62, subscript; 62, 63; 62, 64; 63, identifier:dd; 64, identifier:name; 65, comment; 66, comment; 67, for_statement; 67, 68; 67, 69; 67, 70; 68, identifier:dep; 69, identifier:deps; 70, block; 70, 71; 71, if_statement; 71, 72; 71, 75; 72, comparison_operator:in; 72, 73; 72, 74; 73, identifier:dep; 74, identifier:dd; 75, block; 75, 76; 75, 84; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:old_deps; 80, identifier:dep; 81, subscript; 81, 82; 81, 83; 82, identifier:dd; 83, identifier:dep; 84, delete_statement; 84, 85; 85, subscript; 85, 86; 85, 87; 86, identifier:dd; 87, identifier:dep; 88, try_statement; 88, 89; 88, 141; 89, block; 89, 90; 89, 96; 89, 136; 89, 137; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:dd; 94, identifier:name; 95, identifier:value; 96, for_statement; 96, 97; 96, 100; 96, 105; 97, tuple_pattern; 97, 98; 97, 99; 98, identifier:args; 99, identifier:check_fn; 100, call; 100, 101; 100, 102; 101, identifier:zip; 102, argument_list; 102, 103; 102, 104; 103, identifier:arg_lists; 104, identifier:check_fns; 105, block; 105, 106; 106, if_statement; 106, 107; 106, 121; 107, not_operator; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:check_fn; 110, argument_list; 110, 111; 111, list_splat; 111, 112; 112, list_comprehension; 112, 113; 112, 118; 113, call; 113, 114; 113, 115; 114, identifier:getattr; 115, argument_list; 115, 116; 115, 117; 116, identifier:self; 117, identifier:arg; 118, for_in_clause; 118, 119; 118, 120; 119, identifier:arg; 120, identifier:args; 121, block; 121, 122; 122, raise_statement; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:RuntimeError; 125, argument_list; 125, 126; 126, binary_operator:%; 126, 127; 126, 131; 127, parenthesized_expression; 127, 128; 128, binary_operator:+; 128, 129; 128, 130; 129, string:'Changing value of immutable attribute \'%s\''; 130, string:' caused validation failure: %s'; 131, tuple; 131, 132; 131, 133; 132, identifier:name; 133, tuple; 133, 134; 133, 135; 134, identifier:args; 135, identifier:check_fn; 136, comment; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:old_deps; 140, None; 141, finally_clause; 141, 142; 142, block; 142, 143; 143, if_statement; 143, 144; 143, 145; 143, 146; 143, 147; 144, identifier:old_deps; 145, comment; 146, comment; 147, block; 147, 148; 147, 165; 148, for_statement; 148, 149; 148, 152; 148, 158; 149, tuple_pattern; 149, 150; 149, 151; 150, identifier:dep; 151, identifier:val; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:six; 155, identifier:iteritems; 156, argument_list; 156, 157; 157, identifier:old_deps; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:dd; 163, identifier:dep; 164, identifier:val; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:dd; 169, identifier:name; 170, identifier:orig_value; 171, else_clause; 171, 172; 172, block; 172, 173; 173, raise_statement; 173, 174; 174, call; 174, 175; 174, 176; 175, identifier:TypeError; 176, argument_list; 176, 177; 177, binary_operator:%; 177, 178; 177, 179; 178, string:'Attempt to change non-parameter member \'%s\' of transient immutable'; 179, identifier:name | def _imm_trans_setattr(self, name, value):
'''
An immutable's transient setattr allows params to be set, and runs checks as they are.
'''
params = _imm_param_data(self)
dd = object.__getattribute__(self, '__dict__')
if name in params:
(_, tx_fn, arg_lists, check_fns, deps) = params[name]
value = value if tx_fn is None else tx_fn(value)
old_deps = {}
orig_value = dd[name]
# clear the dependencies before we run the checks; save them in case the checks fail and we
# go back to how things were...
for dep in deps:
if dep in dd:
old_deps[dep] = dd[dep]
del dd[dep]
try:
dd[name] = value
for (args, check_fn) in zip(arg_lists, check_fns):
if not check_fn(*[getattr(self, arg) for arg in args]):
raise RuntimeError(
('Changing value of immutable attribute \'%s\'' +
' caused validation failure: %s') % (name, (args, check_fn)))
# if all the checks ran, we don't return the old deps; they are now invalid
old_deps = None
finally:
if old_deps:
# in this case, something didn't check-out, so we return the old deps and let the
# exception ride; we also return the original value of the edited param
for (dep,val) in six.iteritems(old_deps):
dd[dep] = val
dd[name] = orig_value
else:
raise TypeError(
'Attempt to change non-parameter member \'%s\' of transient immutable' % name) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:verify_consistency; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:predictions; 5, identifier:scores; 6, identifier:proba; 7, identifier:opt_class; 8, block; 8, 9; 8, 11; 8, 27; 8, 42; 8, 43; 8, 58; 8, 59; 8, 74; 8, 75; 8, 90; 8, 105; 8, 120; 8, 137; 8, 154; 8, 212; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 19; 12, comparison_operator:!=; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:predictions; 15, identifier:shape; 16, attribute; 16, 17; 16, 18; 17, identifier:scores; 18, identifier:shape; 19, block; 19, 20; 20, raise_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:ValueError; 23, argument_list; 23, 24; 24, binary_operator:+; 24, 25; 24, 26; 25, string:"predictions and scores arrays have inconsistent "; 26, string:"dimensions."; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:n_class; 30, conditional_expression:if; 30, 31; 30, 36; 30, 41; 31, subscript; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:scores; 34, identifier:shape; 35, integer:1; 36, comparison_operator:>; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:scores; 39, identifier:ndim; 40, integer:1; 41, integer:1; 42, comment; 43, if_statement; 43, 44; 43, 47; 44, comparison_operator:is; 44, 45; 44, 46; 45, identifier:proba; 46, None; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:proba; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:np; 54, identifier:repeat; 55, argument_list; 55, 56; 55, 57; 56, False; 57, identifier:n_class; 58, comment; 59, if_statement; 59, 60; 59, 63; 60, comparison_operator:is; 60, 61; 60, 62; 61, identifier:opt_class; 62, None; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:opt_class; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:np; 70, identifier:repeat; 71, argument_list; 71, 72; 71, 73; 72, True; 73, identifier:n_class; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:proba; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:np; 81, identifier:array; 82, argument_list; 82, 83; 82, 84; 82, 87; 83, identifier:proba; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:dtype; 86, identifier:bool; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:ndmin; 89, integer:1; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:opt_class; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:np; 96, identifier:array; 97, argument_list; 97, 98; 97, 99; 97, 102; 98, identifier:opt_class; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:dtype; 101, identifier:bool; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:ndmin; 104, integer:1; 105, if_statement; 105, 106; 105, 114; 106, comparison_operator:<; 106, 107; 106, 113; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:np; 110, identifier:sum; 111, argument_list; 111, 112; 112, identifier:opt_class; 113, integer:1; 114, block; 114, 115; 115, raise_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:ValueError; 118, argument_list; 118, 119; 119, string:"opt_class should contain at least one True value."; 120, if_statement; 120, 121; 120, 131; 121, comparison_operator:!=; 121, 122; 121, 127; 122, subscript; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:predictions; 125, identifier:shape; 126, integer:1; 127, call; 127, 128; 127, 129; 128, identifier:len; 129, argument_list; 129, 130; 130, identifier:proba; 131, block; 131, 132; 132, raise_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:ValueError; 135, argument_list; 135, 136; 136, string:"mismatch in shape of proba and predictions."; 137, if_statement; 137, 138; 137, 148; 138, comparison_operator:!=; 138, 139; 138, 144; 139, subscript; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:predictions; 142, identifier:shape; 143, integer:1; 144, call; 144, 145; 144, 146; 145, identifier:len; 146, argument_list; 146, 147; 147, identifier:opt_class; 148, block; 148, 149; 149, raise_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:ValueError; 152, argument_list; 152, 153; 153, string:"mismatch in shape of opt_class and predictions."; 154, for_statement; 154, 155; 154, 156; 154, 160; 155, identifier:m; 156, call; 156, 157; 156, 158; 157, identifier:range; 158, argument_list; 158, 159; 159, identifier:n_class; 160, block; 160, 161; 161, if_statement; 161, 162; 161, 191; 162, parenthesized_expression; 162, 163; 163, boolean_operator:and; 163, 164; 163, 188; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:np; 167, identifier:any; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:np; 172, identifier:logical_or; 173, argument_list; 173, 174; 173, 181; 174, comparison_operator:<; 174, 175; 174, 180; 175, subscript; 175, 176; 175, 177; 175, 179; 176, identifier:scores; 177, slice; 177, 178; 178, colon; 179, identifier:m; 180, integer:0; 181, comparison_operator:>; 181, 182; 181, 187; 182, subscript; 182, 183; 182, 184; 182, 186; 183, identifier:scores; 184, slice; 184, 185; 185, colon; 186, identifier:m; 187, integer:1; 188, subscript; 188, 189; 188, 190; 189, identifier:proba; 190, identifier:m; 191, block; 191, 192; 191, 206; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:warnings; 196, identifier:warn; 197, argument_list; 197, 198; 198, binary_operator:+; 198, 199; 198, 200; 199, string:"scores fall outside the [0,1] interval for "; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, string:"classifier {}. Setting proba[m]=False."; 203, identifier:format; 204, argument_list; 204, 205; 205, identifier:m; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 211; 208, subscript; 208, 209; 208, 210; 209, identifier:proba; 210, identifier:m; 211, False; 212, return_statement; 212, 213; 213, expression_list; 213, 214; 213, 215; 214, identifier:proba; 215, identifier:opt_class | def verify_consistency(predictions, scores, proba, opt_class):
"""Verifies that all arrays have consistent dimensions. Also verifies
that the scores are consistent with proba.
Returns
-------
proba, opt_class
"""
if predictions.shape != scores.shape:
raise ValueError("predictions and scores arrays have inconsistent " +
"dimensions.")
n_class = scores.shape[1] if scores.ndim > 1 else 1
# If proba not given, default to False for all classifiers
if proba is None:
proba = np.repeat(False, n_class)
# If opt_class is not given, default to True for all classifiers
if opt_class is None:
opt_class = np.repeat(True, n_class)
# Convert to numpy arrays if necessary
proba = np.array(proba, dtype=bool, ndmin=1)
opt_class = np.array(opt_class, dtype=bool, ndmin=1)
if np.sum(opt_class) < 1:
raise ValueError("opt_class should contain at least one True value.")
if predictions.shape[1] != len(proba):
raise ValueError("mismatch in shape of proba and predictions.")
if predictions.shape[1] != len(opt_class):
raise ValueError("mismatch in shape of opt_class and predictions.")
for m in range(n_class):
if (np.any(np.logical_or(scores[:,m] < 0, scores[:,m] > 1)) and proba[m]):
warnings.warn("scores fall outside the [0,1] interval for " +
"classifier {}. Setting proba[m]=False.".format(m))
proba[m] = False
return proba, opt_class |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_call; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:context; 6, block; 6, 7; 6, 9; 6, 15; 6, 30; 6, 36; 6, 47; 6, 56; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:req; 12, attribute; 12, 13; 12, 14; 13, identifier:context; 14, identifier:request; 15, if_statement; 15, 16; 15, 23; 16, not_operator; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:req; 20, identifier:has_key; 21, argument_list; 21, 22; 22, string:"method"; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:RpcException; 27, argument_list; 27, 28; 27, 29; 28, identifier:ERR_INVALID_REQ; 29, string:"Invalid Request. No 'method'."; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:method; 33, subscript; 33, 34; 33, 35; 34, identifier:req; 35, string:"method"; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:==; 37, 38; 37, 39; 38, identifier:method; 39, string:"barrister-idl"; 40, block; 40, 41; 41, return_statement; 41, 42; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:contract; 46, identifier:idl_parsed; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, pattern_list; 49, 50; 49, 51; 50, identifier:iface_name; 51, identifier:func_name; 52, call; 52, 53; 52, 54; 53, identifier:unpack_method; 54, argument_list; 54, 55; 55, identifier:method; 56, if_statement; 56, 57; 56, 65; 56, 194; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:handlers; 62, identifier:has_key; 63, argument_list; 63, 64; 64, identifier:iface_name; 65, block; 65, 66; 65, 74; 65, 82; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:iface_impl; 69, subscript; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:handlers; 73, identifier:iface_name; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:func; 77, call; 77, 78; 77, 79; 78, identifier:getattr; 79, argument_list; 79, 80; 79, 81; 80, identifier:iface_impl; 81, identifier:func_name; 82, if_statement; 82, 83; 82, 84; 82, 179; 83, identifier:func; 84, block; 84, 85; 84, 105; 84, 121; 84, 142; 84, 161; 84, 177; 85, if_statement; 85, 86; 85, 92; 85, 99; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:req; 89, identifier:has_key; 90, argument_list; 90, 91; 91, string:"params"; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:params; 96, subscript; 96, 97; 96, 98; 97, identifier:req; 98, string:"params"; 99, else_clause; 99, 100; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:params; 104, list:[ ]; 105, if_statement; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:validate_req; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:contract; 116, identifier:validate_request; 117, argument_list; 117, 118; 117, 119; 117, 120; 118, identifier:iface_name; 119, identifier:func_name; 120, identifier:params; 121, if_statement; 121, 122; 121, 127; 122, call; 122, 123; 122, 124; 123, identifier:hasattr; 124, argument_list; 124, 125; 124, 126; 125, identifier:iface_impl; 126, string:"barrister_pre"; 127, block; 127, 128; 127, 136; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:pre_hook; 131, call; 131, 132; 131, 133; 132, identifier:getattr; 133, argument_list; 133, 134; 133, 135; 134, identifier:iface_impl; 135, string:"barrister_pre"; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:pre_hook; 139, argument_list; 139, 140; 139, 141; 140, identifier:context; 141, identifier:params; 142, if_statement; 142, 143; 142, 144; 142, 153; 143, identifier:params; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:result; 148, call; 148, 149; 148, 150; 149, identifier:func; 150, argument_list; 150, 151; 151, list_splat; 151, 152; 152, identifier:params; 153, else_clause; 153, 154; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:result; 158, call; 158, 159; 158, 160; 159, identifier:func; 160, argument_list; 161, if_statement; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:validate_resp; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:contract; 172, identifier:validate_response; 173, argument_list; 173, 174; 173, 175; 173, 176; 174, identifier:iface_name; 175, identifier:func_name; 176, identifier:result; 177, return_statement; 177, 178; 178, identifier:result; 179, else_clause; 179, 180; 180, block; 180, 181; 180, 188; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:msg; 184, binary_operator:%; 184, 185; 184, 186; 185, string:"Method '%s' not found"; 186, parenthesized_expression; 186, 187; 187, identifier:method; 188, raise_statement; 188, 189; 189, call; 189, 190; 189, 191; 190, identifier:RpcException; 191, argument_list; 191, 192; 191, 193; 192, identifier:ERR_METHOD_NOT_FOUND; 193, identifier:msg; 194, else_clause; 194, 195; 195, block; 195, 196; 195, 203; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:msg; 199, binary_operator:%; 199, 200; 199, 201; 200, string:"No implementation of '%s' found"; 201, parenthesized_expression; 201, 202; 202, identifier:iface_name; 203, raise_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:RpcException; 206, argument_list; 206, 207; 206, 208; 207, identifier:ERR_METHOD_NOT_FOUND; 208, identifier:msg | def _call(self, context):
"""
Executes a single request against a handler. If the req.method == 'barrister-idl', the
Contract IDL JSON structure is returned. Otherwise the method is resolved to a handler
based on the interface name, and the appropriate function is called on the handler.
:Parameter:
req
A dict representing a valid JSON-RPC 2.0 request. 'method' must be provided.
"""
req = context.request
if not req.has_key("method"):
raise RpcException(ERR_INVALID_REQ, "Invalid Request. No 'method'.")
method = req["method"]
if method == "barrister-idl":
return self.contract.idl_parsed
iface_name, func_name = unpack_method(method)
if self.handlers.has_key(iface_name):
iface_impl = self.handlers[iface_name]
func = getattr(iface_impl, func_name)
if func:
if req.has_key("params"):
params = req["params"]
else:
params = [ ]
if self.validate_req:
self.contract.validate_request(iface_name, func_name, params)
if hasattr(iface_impl, "barrister_pre"):
pre_hook = getattr(iface_impl, "barrister_pre")
pre_hook(context, params)
if params:
result = func(*params)
else:
result = func()
if self.validate_resp:
self.contract.validate_response(iface_name, func_name, result)
return result
else:
msg = "Method '%s' not found" % (method)
raise RpcException(ERR_METHOD_NOT_FOUND, msg)
else:
msg = "No implementation of '%s' found" % (iface_name)
raise RpcException(ERR_METHOD_NOT_FOUND, msg) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:validate; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:expected_type; 6, identifier:is_array; 7, identifier:val; 8, block; 8, 9; 8, 11; 8, 192; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 11, 31; 11, 77; 11, 102; 11, 128; 11, 151; 11, 176; 12, comparison_operator:==; 12, 13; 12, 14; 13, identifier:val; 14, None; 15, block; 15, 16; 16, if_statement; 16, 17; 16, 20; 16, 25; 17, attribute; 17, 18; 17, 19; 18, identifier:expected_type; 19, identifier:optional; 20, block; 20, 21; 21, return_statement; 21, 22; 22, expression_list; 22, 23; 22, 24; 23, True; 24, None; 25, else_clause; 25, 26; 26, block; 26, 27; 27, return_statement; 27, 28; 28, expression_list; 28, 29; 28, 30; 29, False; 30, string:"Value cannot be null"; 31, elif_clause; 31, 32; 31, 33; 32, identifier:is_array; 33, block; 33, 34; 34, if_statement; 34, 35; 34, 41; 34, 50; 35, not_operator; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:isinstance; 38, argument_list; 38, 39; 38, 40; 39, identifier:val; 40, identifier:list; 41, block; 41, 42; 42, return_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:_type_err; 47, argument_list; 47, 48; 47, 49; 48, identifier:val; 49, string:"list"; 50, else_clause; 50, 51; 51, block; 51, 52; 52, for_statement; 52, 53; 52, 54; 52, 55; 53, identifier:v; 54, identifier:val; 55, block; 55, 56; 55, 69; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, pattern_list; 58, 59; 58, 60; 59, identifier:ok; 60, identifier:msg; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:validate; 65, argument_list; 65, 66; 65, 67; 65, 68; 66, identifier:expected_type; 67, False; 68, identifier:v; 69, if_statement; 69, 70; 69, 72; 70, not_operator; 70, 71; 71, identifier:ok; 72, block; 72, 73; 73, return_statement; 73, 74; 74, expression_list; 74, 75; 74, 76; 75, identifier:ok; 76, identifier:msg; 77, elif_clause; 77, 78; 77, 83; 78, comparison_operator:==; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:expected_type; 81, identifier:type; 82, string:"int"; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 93; 85, not_operator; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:isinstance; 88, argument_list; 88, 89; 88, 90; 89, identifier:val; 90, tuple; 90, 91; 90, 92; 91, identifier:long; 92, identifier:int; 93, block; 93, 94; 94, return_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:_type_err; 99, argument_list; 99, 100; 99, 101; 100, identifier:val; 101, string:"int"; 102, elif_clause; 102, 103; 102, 108; 103, comparison_operator:==; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:expected_type; 106, identifier:type; 107, string:"float"; 108, block; 108, 109; 109, if_statement; 109, 110; 109, 119; 110, not_operator; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:isinstance; 113, argument_list; 113, 114; 113, 115; 114, identifier:val; 115, tuple; 115, 116; 115, 117; 115, 118; 116, identifier:float; 117, identifier:int; 118, identifier:long; 119, block; 119, 120; 120, return_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:_type_err; 125, argument_list; 125, 126; 125, 127; 126, identifier:val; 127, string:"float"; 128, elif_clause; 128, 129; 128, 134; 129, comparison_operator:==; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:expected_type; 132, identifier:type; 133, string:"bool"; 134, block; 134, 135; 135, if_statement; 135, 136; 135, 142; 136, not_operator; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:isinstance; 139, argument_list; 139, 140; 139, 141; 140, identifier:val; 141, identifier:bool; 142, block; 142, 143; 143, return_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:_type_err; 148, argument_list; 148, 149; 148, 150; 149, identifier:val; 150, string:"bool"; 151, elif_clause; 151, 152; 151, 157; 152, comparison_operator:==; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:expected_type; 155, identifier:type; 156, string:"string"; 157, block; 157, 158; 158, if_statement; 158, 159; 158, 167; 159, not_operator; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:isinstance; 162, argument_list; 162, 163; 162, 164; 163, identifier:val; 164, tuple; 164, 165; 164, 166; 165, identifier:str; 166, identifier:unicode; 167, block; 167, 168; 168, return_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:_type_err; 173, argument_list; 173, 174; 173, 175; 174, identifier:val; 175, string:"string"; 176, else_clause; 176, 177; 177, block; 177, 178; 178, return_statement; 178, 179; 179, call; 179, 180; 179, 190; 180, attribute; 180, 181; 180, 189; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:get; 185, argument_list; 185, 186; 186, attribute; 186, 187; 186, 188; 187, identifier:expected_type; 188, identifier:type; 189, identifier:validate; 190, argument_list; 190, 191; 191, identifier:val; 192, return_statement; 192, 193; 193, expression_list; 193, 194; 193, 195; 194, True; 195, None | def validate(self, expected_type, is_array, val):
"""
Validates that the expected type matches the value
Returns two element tuple: (bool, string)
- `bool` - True if valid, False if not
- `string` - Description of validation error, or None if valid
:Parameters:
expected_type
string name of the type expected. This may be a Barrister primitive, or a user defined type.
is_array
If True then require that the val be a list
val
Value to validate against the expected type
"""
if val == None:
if expected_type.optional:
return True, None
else:
return False, "Value cannot be null"
elif is_array:
if not isinstance(val, list):
return self._type_err(val, "list")
else:
for v in val:
ok, msg = self.validate(expected_type, False, v)
if not ok:
return ok, msg
elif expected_type.type == "int":
if not isinstance(val, (long, int)):
return self._type_err(val, "int")
elif expected_type.type == "float":
if not isinstance(val, (float, int, long)):
return self._type_err(val, "float")
elif expected_type.type == "bool":
if not isinstance(val, bool):
return self._type_err(val, "bool")
elif expected_type.type == "string":
if not isinstance(val, (str, unicode)):
return self._type_err(val, "string")
else:
return self.get(expected_type.type).validate(val)
return True, None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:validate; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:val; 6, block; 6, 7; 6, 9; 6, 27; 6, 92; 6, 101; 6, 134; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 16; 10, comparison_operator:is; 10, 11; 10, 15; 11, call; 11, 12; 11, 13; 12, identifier:type; 13, argument_list; 13, 14; 14, identifier:val; 15, identifier:dict; 16, block; 16, 17; 17, return_statement; 17, 18; 18, expression_list; 18, 19; 18, 20; 19, False; 20, binary_operator:%; 20, 21; 20, 22; 21, string:"%s is not a dict"; 22, parenthesized_expression; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:str; 25, argument_list; 25, 26; 26, identifier:val; 27, for_statement; 27, 28; 27, 31; 27, 36; 28, pattern_list; 28, 29; 28, 30; 29, identifier:k; 30, identifier:v; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:val; 34, identifier:items; 35, argument_list; 36, block; 36, 37; 36, 46; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:field; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:field; 44, argument_list; 44, 45; 45, identifier:k; 46, if_statement; 46, 47; 46, 48; 46, 80; 47, identifier:field; 48, block; 48, 49; 48, 66; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 54; 51, pattern_list; 51, 52; 51, 53; 52, identifier:ok; 53, identifier:msg; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:contract; 59, identifier:validate; 60, argument_list; 60, 61; 60, 62; 60, 65; 61, identifier:field; 62, attribute; 62, 63; 62, 64; 63, identifier:field; 64, identifier:is_array; 65, identifier:v; 66, if_statement; 66, 67; 66, 69; 67, not_operator; 67, 68; 68, identifier:ok; 69, block; 69, 70; 70, return_statement; 70, 71; 71, expression_list; 71, 72; 71, 73; 72, False; 73, binary_operator:%; 73, 74; 73, 75; 74, string:"field '%s': %s"; 75, tuple; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:field; 78, identifier:name; 79, identifier:msg; 80, else_clause; 80, 81; 81, block; 81, 82; 82, return_statement; 82, 83; 83, expression_list; 83, 84; 83, 85; 84, False; 85, binary_operator:%; 85, 86; 85, 87; 86, string:"field '%s' not found in struct %s"; 87, tuple; 87, 88; 87, 89; 88, identifier:k; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:name; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:all_fields; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:get_all_fields; 99, argument_list; 99, 100; 100, list:[]; 101, for_statement; 101, 102; 101, 103; 101, 104; 102, identifier:field; 103, identifier:all_fields; 104, block; 104, 105; 105, if_statement; 105, 106; 105, 120; 106, boolean_operator:and; 106, 107; 106, 116; 107, not_operator; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:val; 111, identifier:has_key; 112, argument_list; 112, 113; 113, attribute; 113, 114; 113, 115; 114, identifier:field; 115, identifier:name; 116, not_operator; 116, 117; 117, attribute; 117, 118; 117, 119; 118, identifier:field; 119, identifier:optional; 120, block; 120, 121; 121, return_statement; 121, 122; 122, expression_list; 122, 123; 122, 124; 123, False; 124, binary_operator:%; 124, 125; 124, 126; 125, string:"field '%s' missing from: %s"; 126, tuple; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:field; 129, identifier:name; 130, call; 130, 131; 130, 132; 131, identifier:str; 132, argument_list; 132, 133; 133, identifier:val; 134, return_statement; 134, 135; 135, expression_list; 135, 136; 135, 137; 136, True; 137, None | def validate(self, val):
"""
Validates that the val matches the expected fields for this struct.
val must be a dict, and must contain only fields represented by this struct and its
ancestors.
Returns two element tuple: (bool, string)
- `bool` - True if valid, False if not
- `string` - Description of validation error, or None if valid
:Parameters:
val
Value to validate. Must be a dict
"""
if type(val) is not dict:
return False, "%s is not a dict" % (str(val))
for k, v in val.items():
field = self.field(k)
if field:
ok, msg = self.contract.validate(field, field.is_array, v)
if not ok:
return False, "field '%s': %s" % (field.name, msg)
else:
return False, "field '%s' not found in struct %s" % (k, self.name)
all_fields = self.get_all_fields([])
for field in all_fields:
if not val.has_key(field.name) and not field.optional:
return False, "field '%s' missing from: %s" % (field.name, str(val))
return True, None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_struct; 3, parameters; 3, 4; 4, identifier:s; 5, block; 5, 6; 5, 8; 5, 14; 5, 22; 5, 35; 5, 39; 5, 43; 5, 47; 5, 94; 5, 98; 5, 102; 5, 106; 5, 110; 5, 209; 5, 213; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:docs; 11, subscript; 11, 12; 11, 13; 12, identifier:s; 13, string:'comment'; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:code; 17, binary_operator:%; 17, 18; 17, 19; 18, string:'<span class="k">struct</span> <span class="gs">%s</span>'; 19, subscript; 19, 20; 19, 21; 20, identifier:s; 21, string:'name'; 22, if_statement; 22, 23; 22, 26; 23, subscript; 23, 24; 23, 25; 24, identifier:s; 25, string:'extends'; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, augmented_assignment:+=; 28, 29; 28, 30; 29, identifier:code; 30, binary_operator:%; 30, 31; 30, 32; 31, string:' extends <span class="gs">%s</span>'; 32, subscript; 32, 33; 32, 34; 33, identifier:s; 34, string:'extends'; 35, expression_statement; 35, 36; 36, augmented_assignment:+=; 36, 37; 36, 38; 37, identifier:code; 38, string:' {\n'; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:namelen; 42, integer:0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:typelen; 46, integer:0; 47, for_statement; 47, 48; 47, 49; 47, 52; 48, identifier:v; 49, subscript; 49, 50; 49, 51; 50, identifier:s; 51, string:"fields"; 52, block; 52, 53; 52, 66; 52, 85; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:tlen; 56, call; 56, 57; 56, 58; 57, identifier:len; 58, argument_list; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:format_type; 61, argument_list; 61, 62; 61, 63; 62, identifier:v; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:includeOptional; 65, False; 66, if_statement; 66, 67; 66, 75; 67, comparison_operator:>; 67, 68; 67, 74; 68, call; 68, 69; 68, 70; 69, identifier:len; 70, argument_list; 70, 71; 71, subscript; 71, 72; 71, 73; 72, identifier:v; 73, string:'name'; 74, identifier:namelen; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:namelen; 79, call; 79, 80; 79, 81; 80, identifier:len; 81, argument_list; 81, 82; 82, subscript; 82, 83; 82, 84; 83, identifier:v; 84, string:'name'; 85, if_statement; 85, 86; 85, 89; 86, comparison_operator:>; 86, 87; 86, 88; 87, identifier:tlen; 88, identifier:typelen; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:typelen; 93, identifier:tlen; 94, expression_statement; 94, 95; 95, augmented_assignment:+=; 95, 96; 95, 97; 96, identifier:namelen; 97, integer:1; 98, expression_statement; 98, 99; 99, augmented_assignment:+=; 99, 100; 99, 101; 100, identifier:typelen; 101, integer:1; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:formatstr; 105, string:' <span class="nv">%s</span><span class="kt">%s %s</span>\n'; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:i; 109, integer:0; 110, for_statement; 110, 111; 110, 112; 110, 115; 111, identifier:v; 112, subscript; 112, 113; 112, 114; 113, identifier:s; 114, string:"fields"; 115, block; 115, 116; 115, 154; 115, 158; 115, 176; 115, 205; 116, if_statement; 116, 117; 116, 127; 117, boolean_operator:and; 117, 118; 117, 124; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:v; 121, identifier:has_key; 122, argument_list; 122, 123; 123, string:'comment'; 124, subscript; 124, 125; 124, 126; 125, identifier:v; 126, string:'comment'; 127, block; 127, 128; 127, 137; 128, if_statement; 128, 129; 128, 132; 129, comparison_operator:>; 129, 130; 129, 131; 130, identifier:i; 131, integer:0; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, augmented_assignment:+=; 134, 135; 134, 136; 135, identifier:code; 136, string:"\n"; 137, for_statement; 137, 138; 137, 139; 137, 147; 138, identifier:line; 139, call; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:v; 143, string:'comment'; 144, identifier:split; 145, argument_list; 145, 146; 146, string:"\n"; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, augmented_assignment:+=; 149, 150; 149, 151; 150, identifier:code; 151, binary_operator:%; 151, 152; 151, 153; 152, string:' <span class="c1">// %s</span>\n'; 153, identifier:line; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:opt; 157, string:""; 158, if_statement; 158, 159; 158, 171; 159, boolean_operator:and; 159, 160; 159, 166; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:v; 163, identifier:has_key; 164, argument_list; 164, 165; 165, string:'optional'; 166, comparison_operator:==; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:v; 169, string:'optional'; 170, True; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:opt; 175, string:" [optional]"; 176, expression_statement; 176, 177; 177, augmented_assignment:+=; 177, 178; 177, 179; 178, identifier:code; 179, binary_operator:%; 179, 180; 179, 181; 180, identifier:formatstr; 181, tuple; 181, 182; 181, 191; 181, 204; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:string; 185, identifier:ljust; 186, argument_list; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:v; 189, string:'name'; 190, identifier:namelen; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:string; 194, identifier:ljust; 195, argument_list; 195, 196; 195, 203; 196, call; 196, 197; 196, 198; 197, identifier:format_type; 198, argument_list; 198, 199; 198, 200; 199, identifier:v; 200, keyword_argument; 200, 201; 200, 202; 201, identifier:includeOptional; 202, False; 203, identifier:typelen; 204, identifier:opt; 205, expression_statement; 205, 206; 206, augmented_assignment:+=; 206, 207; 206, 208; 207, identifier:i; 208, integer:1; 209, expression_statement; 209, 210; 210, augmented_assignment:+=; 210, 211; 210, 212; 211, identifier:code; 212, string:"}"; 213, return_statement; 213, 214; 214, call; 214, 215; 214, 216; 215, identifier:to_section; 216, argument_list; 216, 217; 216, 218; 217, identifier:docs; 218, identifier:code | def parse_struct(s):
"""
Returns a docco section for the given struct.
:Parameters:
s
Parsed IDL struct dict. Keys: 'comment', 'name', 'extends', 'fields'
"""
docs = s['comment']
code = '<span class="k">struct</span> <span class="gs">%s</span>' % s['name']
if s['extends']:
code += ' extends <span class="gs">%s</span>' % s['extends']
code += ' {\n'
namelen = 0
typelen = 0
for v in s["fields"]:
tlen = len(format_type(v, includeOptional=False))
if len(v['name']) > namelen:
namelen = len(v['name'])
if tlen > typelen:
typelen = tlen
namelen += 1
typelen += 1
formatstr = ' <span class="nv">%s</span><span class="kt">%s %s</span>\n'
i = 0
for v in s["fields"]:
if v.has_key('comment') and v['comment']:
if i > 0: code += "\n"
for line in v['comment'].split("\n"):
code += ' <span class="c1">// %s</span>\n' % line
opt = ""
if v.has_key('optional') and v['optional'] == True:
opt = " [optional]"
code += formatstr % (string.ljust(v['name'], namelen), string.ljust(format_type(v, includeOptional=False), typelen), opt)
i += 1
code += "}"
return to_section(docs, code) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_interface; 3, parameters; 3, 4; 4, identifier:iface; 5, block; 5, 6; 5, 8; 5, 12; 5, 18; 5, 26; 5, 131; 5, 135; 5, 146; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:sections; 11, list:[ ]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:docs; 15, subscript; 15, 16; 15, 17; 16, identifier:iface; 17, string:'comment'; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:code; 21, binary_operator:%; 21, 22; 21, 23; 22, string:'<span class="k">interface</span> <span class="gs">%s</span> {\n'; 23, subscript; 23, 24; 23, 25; 24, identifier:iface; 25, string:'name'; 26, for_statement; 26, 27; 26, 28; 26, 31; 27, identifier:v; 28, subscript; 28, 29; 28, 30; 29, identifier:iface; 30, string:"functions"; 31, block; 31, 32; 31, 40; 31, 44; 31, 78; 31, 89; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:func_code; 35, binary_operator:%; 35, 36; 35, 37; 36, string:' <span class="nf">%s</span>('; 37, subscript; 37, 38; 37, 39; 38, identifier:v; 39, string:'name'; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:i; 43, integer:0; 44, for_statement; 44, 45; 44, 46; 44, 49; 45, identifier:p; 46, subscript; 46, 47; 46, 48; 47, identifier:v; 48, string:"params"; 49, block; 49, 50; 49, 65; 50, if_statement; 50, 51; 50, 54; 50, 59; 51, comparison_operator:==; 51, 52; 51, 53; 52, identifier:i; 53, integer:0; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:i; 58, integer:1; 59, else_clause; 59, 60; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, augmented_assignment:+=; 62, 63; 62, 64; 63, identifier:func_code; 64, string:", "; 65, expression_statement; 65, 66; 66, augmented_assignment:+=; 66, 67; 66, 68; 67, identifier:func_code; 68, binary_operator:%; 68, 69; 68, 70; 69, string:'<span class="na">%s</span> <span class="kt">%s</span>'; 70, tuple; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:p; 73, string:'name'; 74, call; 74, 75; 74, 76; 75, identifier:format_type; 76, argument_list; 76, 77; 77, identifier:p; 78, expression_statement; 78, 79; 79, augmented_assignment:+=; 79, 80; 79, 81; 80, identifier:func_code; 81, binary_operator:%; 81, 82; 81, 83; 82, string:') <span class="kt">%s</span>\n'; 83, call; 83, 84; 83, 85; 84, identifier:format_type; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:v; 88, string:'returns'; 89, if_statement; 89, 90; 89, 100; 89, 125; 90, boolean_operator:and; 90, 91; 90, 97; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:v; 94, identifier:has_key; 95, argument_list; 95, 96; 96, string:'comment'; 97, subscript; 97, 98; 97, 99; 98, identifier:v; 99, string:'comment'; 100, block; 100, 101; 100, 115; 100, 121; 101, if_statement; 101, 102; 101, 103; 102, identifier:code; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:sections; 108, identifier:append; 109, argument_list; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:to_section; 112, argument_list; 112, 113; 112, 114; 113, identifier:docs; 114, identifier:code; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:docs; 118, subscript; 118, 119; 118, 120; 119, identifier:v; 120, string:'comment'; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:code; 124, identifier:func_code; 125, else_clause; 125, 126; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, augmented_assignment:+=; 128, 129; 128, 130; 129, identifier:code; 130, identifier:func_code; 131, expression_statement; 131, 132; 132, augmented_assignment:+=; 132, 133; 132, 134; 133, identifier:code; 134, string:"}"; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:sections; 139, identifier:append; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:to_section; 143, argument_list; 143, 144; 143, 145; 144, identifier:docs; 145, identifier:code; 146, return_statement; 146, 147; 147, identifier:sections | def parse_interface(iface):
"""
Returns a docco section for the given interface.
:Parameters:
iface
Parsed IDL interface dict. Keys: 'comment', 'name', 'returns', 'params'
"""
sections = [ ]
docs = iface['comment']
code = '<span class="k">interface</span> <span class="gs">%s</span> {\n' % iface['name']
for v in iface["functions"]:
func_code = ' <span class="nf">%s</span>(' % v['name']
i = 0
for p in v["params"]:
if i == 0: i = 1
else: func_code += ", "
func_code += '<span class="na">%s</span> <span class="kt">%s</span>' % (p['name'], format_type(p))
func_code += ') <span class="kt">%s</span>\n' % format_type(v['returns'])
if v.has_key('comment') and v['comment']:
if code:
sections.append(to_section(docs, code))
docs = v['comment']
code = func_code
else:
code += func_code
code += "}"
sections.append(to_section(docs, code))
return sections |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:run; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 20; 5, 26; 5, 32; 5, 39; 5, 62; 5, 82; 5, 88; 5, 89; 5, 97; 5, 104; 5, 220; 5, 227; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:model; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:model; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:configfile; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:configfile; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:interval; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:interval; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:sockets; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:sockets; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:model; 36, identifier:initialize; 37, argument_list; 37, 38; 38, identifier:configfile; 39, if_statement; 39, 40; 39, 45; 39, 53; 40, comparison_operator:==; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:model; 43, identifier:state; 44, string:'pause'; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:logger; 50, identifier:info; 51, argument_list; 51, 52; 52, string:"model initialized and started in pause mode, waiting for requests"; 53, else_clause; 53, 54; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:logger; 59, identifier:info; 60, argument_list; 60, 61; 61, string:"model started and initialized, running"; 62, if_statement; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:tracker; 66, block; 66, 67; 66, 73; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:register; 72, argument_list; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:atexit; 77, identifier:register; 78, argument_list; 78, 79; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:unregister; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:process_incoming; 87, argument_list; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:counter; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:itertools; 95, identifier:count; 96, argument_list; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:logger; 101, identifier:info; 102, argument_list; 102, 103; 103, string:"Entering timeloop..."; 104, for_statement; 104, 105; 104, 106; 104, 107; 105, identifier:i; 106, identifier:counter; 107, block; 107, 108; 107, 132; 107, 140; 107, 141; 107, 152; 107, 159; 107, 160; 107, 166; 108, while_statement; 108, 109; 108, 114; 108, 115; 108, 116; 108, 123; 109, comparison_operator:==; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:model; 112, identifier:state; 113, string:"pause"; 114, comment; 115, comment; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:process_incoming; 122, argument_list; 123, else_clause; 123, 124; 123, 125; 124, comment; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:process_incoming; 131, argument_list; 132, if_statement; 132, 133; 132, 138; 133, comparison_operator:==; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:model; 136, identifier:state; 137, string:"quit"; 138, block; 138, 139; 139, break_statement; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:dt; 144, boolean_operator:or; 144, 145; 144, 150; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:model; 148, identifier:get_time_step; 149, argument_list; 150, unary_operator:-; 150, 151; 151, integer:1; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:model; 156, identifier:update; 157, argument_list; 157, 158; 158, identifier:dt; 159, comment; 160, if_statement; 160, 161; 160, 164; 161, binary_operator:%; 161, 162; 161, 163; 162, identifier:i; 163, identifier:interval; 164, block; 164, 165; 165, continue_statement; 166, for_statement; 166, 167; 166, 168; 166, 171; 167, identifier:key; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:output_vars; 171, block; 171, 172; 171, 181; 171, 191; 171, 192; 171, 204; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:value; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:model; 178, identifier:get_var; 179, argument_list; 179, 180; 180, identifier:key; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:metadata; 184, dictionary; 184, 185; 184, 188; 185, pair; 185, 186; 185, 187; 186, string:'name'; 187, identifier:key; 188, pair; 188, 189; 188, 190; 189, string:'iteration'; 190, identifier:i; 191, comment; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:logger; 196, identifier:debug; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, string:"sending {}"; 201, identifier:format; 202, argument_list; 202, 203; 203, identifier:metadata; 204, if_statement; 204, 205; 204, 208; 205, comparison_operator:in; 205, 206; 205, 207; 206, string:'pub'; 207, identifier:sockets; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 212; 211, identifier:send_array; 212, argument_list; 212, 213; 212, 216; 212, 217; 213, subscript; 213, 214; 213, 215; 214, identifier:sockets; 215, string:'pub'; 216, identifier:value; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:metadata; 219, identifier:metadata; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:logger; 224, identifier:info; 225, argument_list; 225, 226; 226, string:"Finalizing..."; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:model; 231, identifier:finalize; 232, argument_list | def run(self):
"""run the model"""
model = self.model
configfile = self.configfile
interval = self.interval
sockets = self.sockets
model.initialize(configfile)
if model.state == 'pause':
logger.info(
"model initialized and started in pause mode, waiting for requests"
)
else:
logger.info("model started and initialized, running")
if self.tracker:
self.register()
atexit.register(self.unregister)
self.process_incoming()
# Keep on counting indefinitely
counter = itertools.count()
logger.info("Entering timeloop...")
for i in counter:
while model.state == "pause":
# keep waiting for messages when paused
# process_incoming should set model.state to play
self.process_incoming()
else:
# otherwise process messages once and continue
self.process_incoming()
if model.state == "quit":
break
# lookup dt or use -1 (default)
dt = model.get_time_step() or -1
model.update(dt)
# check counter, if not a multiple of interval, skip this step
if i % interval:
continue
for key in self.output_vars:
value = model.get_var(key)
metadata = {'name': key, 'iteration': i}
# 4ms for 1M doubles
logger.debug("sending {}".format(metadata))
if 'pub' in sockets:
send_array(sockets['pub'], value, metadata=metadata)
logger.info("Finalizing...")
model.finalize() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:save; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:self; 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, 187; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:publication; 16, block; 16, 17; 16, 23; 16, 37; 16, 88; 16, 102; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:publication; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:publication; 23, if_statement; 23, 24; 23, 28; 24, not_operator; 24, 25; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:title; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:title; 34, attribute; 34, 35; 34, 36; 35, identifier:publication; 36, identifier:title; 37, if_statement; 37, 38; 37, 42; 38, not_operator; 38, 39; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:subtitle; 42, block; 42, 43; 42, 49; 42, 71; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:first_author; 46, attribute; 46, 47; 46, 48; 47, identifier:publication; 48, identifier:first_author; 49, if_statement; 49, 50; 49, 55; 49, 60; 50, comparison_operator:==; 50, 51; 50, 52; 51, identifier:first_author; 52, attribute; 52, 53; 52, 54; 53, identifier:publication; 54, identifier:last_author; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:authors; 59, identifier:first_author; 60, else_clause; 60, 61; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:authors; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, string:'{} et al.'; 68, identifier:format; 69, argument_list; 69, 70; 70, identifier:first_author; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:subtitle; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, string:'{}, {} ({})'; 79, identifier:format; 80, argument_list; 80, 81; 80, 82; 80, 85; 81, identifier:authors; 82, attribute; 82, 83; 82, 84; 83, identifier:publication; 84, identifier:journal; 85, attribute; 85, 86; 85, 87; 86, identifier:publication; 87, identifier:year; 88, if_statement; 88, 89; 88, 93; 89, not_operator; 89, 90; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:description; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:description; 99, attribute; 99, 100; 99, 101; 100, identifier:publication; 101, identifier:abstract; 102, if_statement; 102, 103; 102, 113; 103, boolean_operator:and; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:publication; 108, identifier:year; 109, not_operator; 109, 110; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:pk; 113, block; 113, 114; 113, 126; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:delta; 117, binary_operator:-; 117, 118; 117, 123; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:timezone; 121, identifier:now; 122, argument_list; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:publish_datetime; 126, if_statement; 126, 127; 126, 142; 127, boolean_operator:and; 127, 128; 127, 137; 128, comparison_operator:<=; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:self; 131, identifier:publish_datetime; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:timezone; 135, identifier:now; 136, argument_list; 137, comparison_operator:==; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:delta; 140, identifier:days; 141, integer:0; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:publish_datetime; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:datetime; 151, identifier:datetime; 152, argument_list; 152, 153; 152, 163; 152, 175; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:year; 155, call; 155, 156; 155, 157; 156, identifier:int; 157, argument_list; 157, 158; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:publication; 162, identifier:year; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:month; 165, call; 165, 166; 165, 167; 166, identifier:int; 167, argument_list; 167, 168; 168, boolean_operator:or; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:publication; 173, identifier:month; 174, integer:1; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:day; 177, call; 177, 178; 177, 179; 178, identifier:int; 179, argument_list; 179, 180; 180, boolean_operator:or; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:publication; 185, identifier:day; 186, integer:1; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 194; 189, attribute; 189, 190; 189, 193; 190, call; 190, 191; 190, 192; 191, identifier:super; 192, argument_list; 193, identifier:save; 194, argument_list; 194, 195; 194, 197; 195, list_splat; 195, 196; 196, identifier:args; 197, dictionary_splat; 197, 198; 198, identifier:kwargs | def save(self, *args, **kwargs):
"""
Before saving, if slide is for a publication, use publication info
for slide's title, subtitle, description.
"""
if self.publication:
publication = self.publication
if not self.title:
self.title = publication.title
if not self.subtitle:
first_author = publication.first_author
if first_author == publication.last_author:
authors = first_author
else:
authors = '{} et al.'.format(first_author)
self.subtitle = '{}, {} ({})'.format(authors,
publication.journal, publication.year)
if not self.description:
self.description = publication.abstract
if self.publication.year and not self.pk:
delta = timezone.now() - self.publish_datetime
if self.publish_datetime <= timezone.now() and delta.days == 0:
self.publish_datetime = datetime.datetime(
year=int(self.publication.year),
month=int(self.publication.month or 1),
day=int(self.publication.day or 1),
)
super().save(*args, **kwargs) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:cmd; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:send; 5, identifier:msg; 6, identifier:args; 7, block; 7, 8; 7, 10; 7, 22; 7, 28; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:implements; 13, list:['the golden gate bridge', 'a large trout', 'a clue-by-four', 'a fresh haddock', 'moon', 'an Itanium', 'fwilson', 'a wombat']; 13, 14; 13, 15; 13, 16; 13, 17; 13, 18; 13, 19; 13, 20; 13, 21; 14, string:'the golden gate bridge'; 15, string:'a large trout'; 16, string:'a clue-by-four'; 17, string:'a fresh haddock'; 18, string:'moon'; 19, string:'an Itanium'; 20, string:'fwilson'; 21, string:'a wombat'; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:methods; 25, list:['around a bit', 'upside the head']; 25, 26; 25, 27; 26, string:'around a bit'; 27, string:'upside the head'; 28, if_statement; 28, 29; 28, 31; 28, 101; 29, not_operator; 29, 30; 30, identifier:msg; 31, block; 31, 32; 31, 51; 31, 77; 31, 81; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:channel; 35, conditional_expression:if; 35, 36; 35, 39; 35, 44; 36, subscript; 36, 37; 36, 38; 37, identifier:args; 38, string:'target'; 39, comparison_operator:!=; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:args; 42, string:'target'; 43, string:'private'; 44, subscript; 44, 45; 44, 50; 45, subscript; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:args; 48, string:'config'; 49, string:'core'; 50, string:'channel'; 51, with_statement; 51, 52; 51, 59; 52, with_clause; 52, 53; 53, with_item; 53, 54; 54, attribute; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:args; 57, string:'handler'; 58, identifier:data_lock; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:users; 63, call; 63, 64; 63, 65; 64, identifier:list; 65, argument_list; 65, 66; 66, call; 66, 67; 66, 76; 67, attribute; 67, 68; 67, 75; 68, subscript; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:args; 72, string:'handler'; 73, identifier:channels; 74, identifier:channel; 75, identifier:users; 76, argument_list; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:slap; 80, string:'slaps %s %s with %s'; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:send; 84, argument_list; 84, 85; 84, 100; 85, binary_operator:%; 85, 86; 85, 87; 86, identifier:slap; 87, tuple; 87, 88; 87, 92; 87, 96; 88, call; 88, 89; 88, 90; 89, identifier:choice; 90, argument_list; 90, 91; 91, identifier:users; 92, call; 92, 93; 92, 94; 93, identifier:choice; 94, argument_list; 94, 95; 95, identifier:methods; 96, call; 96, 97; 96, 98; 97, identifier:choice; 98, argument_list; 98, 99; 99, identifier:implements; 100, string:'action'; 101, else_clause; 101, 102; 102, block; 102, 103; 102, 107; 102, 114; 102, 118; 102, 126; 102, 132; 102, 133; 102, 137; 102, 141; 102, 289; 102, 300; 102, 324; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:reason; 106, string:''; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:method; 110, call; 110, 111; 110, 112; 111, identifier:choice; 112, argument_list; 112, 113; 113, identifier:methods; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:implement; 117, string:''; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:msg; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:msg; 124, identifier:split; 125, argument_list; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:slapee; 129, subscript; 129, 130; 129, 131; 130, identifier:msg; 131, integer:0; 132, comment; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:i; 136, integer:1; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:args; 140, False; 141, while_statement; 141, 142; 141, 148; 142, comparison_operator:<; 142, 143; 142, 144; 143, identifier:i; 144, call; 144, 145; 144, 146; 145, identifier:len; 146, argument_list; 146, 147; 147, identifier:msg; 148, block; 148, 149; 148, 285; 149, if_statement; 149, 150; 149, 155; 149, 211; 149, 273; 150, comparison_operator:==; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:msg; 153, identifier:i; 154, string:'for'; 155, block; 155, 156; 155, 160; 155, 169; 155, 173; 155, 203; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:args; 159, True; 160, if_statement; 160, 161; 160, 162; 161, identifier:reason; 162, block; 162, 163; 162, 168; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:send; 166, argument_list; 166, 167; 167, string:"Invalid Syntax: You can only have one for clause!"; 168, return_statement; 169, expression_statement; 169, 170; 170, augmented_assignment:+=; 170, 171; 170, 172; 171, identifier:i; 172, integer:1; 173, while_statement; 173, 174; 173, 180; 174, comparison_operator:<; 174, 175; 174, 176; 175, identifier:i; 176, call; 176, 177; 176, 178; 177, identifier:len; 178, argument_list; 178, 179; 179, identifier:msg; 180, block; 180, 181; 180, 189; 180, 193; 180, 199; 181, if_statement; 181, 182; 181, 187; 182, comparison_operator:==; 182, 183; 182, 186; 183, subscript; 183, 184; 183, 185; 184, identifier:msg; 185, identifier:i; 186, string:'with'; 187, block; 187, 188; 188, break_statement; 189, expression_statement; 189, 190; 190, augmented_assignment:+=; 190, 191; 190, 192; 191, identifier:reason; 192, string:" "; 193, expression_statement; 193, 194; 194, augmented_assignment:+=; 194, 195; 194, 196; 195, identifier:reason; 196, subscript; 196, 197; 196, 198; 197, identifier:msg; 198, identifier:i; 199, expression_statement; 199, 200; 200, augmented_assignment:+=; 200, 201; 200, 202; 201, identifier:i; 202, integer:1; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:reason; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:reason; 209, identifier:strip; 210, argument_list; 211, elif_clause; 211, 212; 211, 217; 212, comparison_operator:==; 212, 213; 212, 216; 213, subscript; 213, 214; 213, 215; 214, identifier:msg; 215, identifier:i; 216, string:'with'; 217, block; 217, 218; 217, 222; 217, 231; 217, 235; 217, 265; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:args; 221, True; 222, if_statement; 222, 223; 222, 224; 223, identifier:implement; 224, block; 224, 225; 224, 230; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 228; 227, identifier:send; 228, argument_list; 228, 229; 229, string:"Invalid Synatx: You can only have one with clause!"; 230, return_statement; 231, expression_statement; 231, 232; 232, augmented_assignment:+=; 232, 233; 232, 234; 233, identifier:i; 234, integer:1; 235, while_statement; 235, 236; 235, 242; 236, comparison_operator:<; 236, 237; 236, 238; 237, identifier:i; 238, call; 238, 239; 238, 240; 239, identifier:len; 240, argument_list; 240, 241; 241, identifier:msg; 242, block; 242, 243; 242, 251; 242, 257; 242, 261; 243, if_statement; 243, 244; 243, 249; 244, comparison_operator:==; 244, 245; 244, 248; 245, subscript; 245, 246; 245, 247; 246, identifier:msg; 247, identifier:i; 248, string:'for'; 249, block; 249, 250; 250, break_statement; 251, expression_statement; 251, 252; 252, augmented_assignment:+=; 252, 253; 252, 254; 253, identifier:implement; 254, subscript; 254, 255; 254, 256; 255, identifier:msg; 256, identifier:i; 257, expression_statement; 257, 258; 258, augmented_assignment:+=; 258, 259; 258, 260; 259, identifier:implement; 260, string:' '; 261, expression_statement; 261, 262; 262, augmented_assignment:+=; 262, 263; 262, 264; 263, identifier:i; 264, integer:1; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:implement; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:implement; 271, identifier:strip; 272, argument_list; 273, elif_clause; 273, 274; 273, 276; 274, not_operator; 274, 275; 275, identifier:args; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, augmented_assignment:+=; 278, 279; 278, 280; 279, identifier:slapee; 280, binary_operator:+; 280, 281; 280, 282; 281, string:' '; 282, subscript; 282, 283; 282, 284; 283, identifier:msg; 284, identifier:i; 285, expression_statement; 285, 286; 286, augmented_assignment:+=; 286, 287; 286, 288; 287, identifier:i; 288, integer:1; 289, if_statement; 289, 290; 289, 292; 290, not_operator; 290, 291; 291, identifier:implement; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:implement; 296, call; 296, 297; 296, 298; 297, identifier:choice; 298, argument_list; 298, 299; 299, identifier:implements; 300, if_statement; 300, 301; 300, 302; 300, 313; 301, identifier:reason; 302, block; 302, 303; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:slap; 306, binary_operator:%; 306, 307; 306, 308; 307, string:'slaps %s %s with %s for %s'; 308, tuple; 308, 309; 308, 310; 308, 311; 308, 312; 309, identifier:slapee; 310, identifier:method; 311, identifier:implement; 312, identifier:reason; 313, else_clause; 313, 314; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:slap; 318, binary_operator:%; 318, 319; 318, 320; 319, string:'slaps %s %s with %s'; 320, tuple; 320, 321; 320, 322; 320, 323; 321, identifier:slapee; 322, identifier:method; 323, identifier:implement; 324, expression_statement; 324, 325; 325, call; 325, 326; 325, 327; 326, identifier:send; 327, argument_list; 327, 328; 327, 329; 328, identifier:slap; 329, string:'action' | def cmd(send, msg, args):
"""Slap somebody.
Syntax: {command} <nick> [for <reason>]
"""
implements = ['the golden gate bridge', 'a large trout', 'a clue-by-four', 'a fresh haddock', 'moon', 'an Itanium', 'fwilson', 'a wombat']
methods = ['around a bit', 'upside the head']
if not msg:
channel = args['target'] if args['target'] != 'private' else args['config']['core']['channel']
with args['handler'].data_lock:
users = list(args['handler'].channels[channel].users())
slap = 'slaps %s %s with %s'
send(slap % (choice(users), choice(methods), choice(implements)), 'action')
else:
reason = ''
method = choice(methods)
implement = ''
msg = msg.split()
slapee = msg[0]
# Basic and stupid NLP!
i = 1
args = False
while i < len(msg):
if msg[i] == 'for':
args = True
if reason:
send("Invalid Syntax: You can only have one for clause!")
return
i += 1
while i < len(msg):
if msg[i] == 'with':
break
reason += " "
reason += msg[i]
i += 1
reason = reason.strip()
elif msg[i] == 'with':
args = True
if implement:
send("Invalid Synatx: You can only have one with clause!")
return
i += 1
while i < len(msg):
if msg[i] == 'for':
break
implement += msg[i]
implement += ' '
i += 1
implement = implement.strip()
elif not args:
slapee += ' ' + msg[i]
i += 1
if not implement:
implement = choice(implements)
if reason:
slap = 'slaps %s %s with %s for %s' % (slapee, method, implement, reason)
else:
slap = 'slaps %s %s with %s' % (slapee, method, implement)
send(slap, 'action') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:cmd; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:send; 5, identifier:msg; 6, identifier:args; 7, block; 7, 8; 7, 10; 7, 20; 7, 26; 7, 55; 7, 56; 7, 82; 7, 83; 7, 100; 7, 117; 7, 123; 7, 129; 7, 148; 7, 159; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 13; 11, not_operator; 11, 12; 12, identifier:msg; 13, block; 13, 14; 13, 19; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:send; 17, argument_list; 17, 18; 18, string:"Invalid Syntax."; 19, return_statement; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:char; 23, subscript; 23, 24; 23, 25; 24, identifier:msg; 25, integer:0; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:msg; 29, list_comprehension; 29, 30; 29, 37; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:x; 33, identifier:replace; 34, argument_list; 34, 35; 34, 36; 35, string:r'\/'; 36, string:'/'; 37, for_in_clause; 37, 38; 37, 39; 38, identifier:x; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:re; 42, identifier:split; 43, argument_list; 43, 44; 43, 47; 43, 52; 44, binary_operator:%; 44, 45; 44, 46; 45, string:r'(?<!\\)\%s'; 46, identifier:char; 47, subscript; 47, 48; 47, 49; 48, identifier:msg; 49, slice; 49, 50; 49, 51; 50, integer:1; 51, colon; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:maxsplit; 54, integer:2; 55, comment; 56, if_statement; 56, 57; 56, 74; 57, boolean_operator:and; 57, 58; 57, 64; 58, comparison_operator:==; 58, 59; 58, 63; 59, call; 59, 60; 59, 61; 60, identifier:len; 61, argument_list; 61, 62; 62, identifier:msg; 63, integer:2; 64, call; 64, 65; 64, 72; 65, attribute; 65, 66; 65, 71; 66, subscript; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:args; 69, string:'config'; 70, string:'feature'; 71, identifier:getboolean; 72, argument_list; 72, 73; 73, string:'lazyregex'; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:msg; 79, identifier:append; 80, argument_list; 80, 81; 81, string:''; 82, comment; 83, if_statement; 83, 84; 83, 93; 84, boolean_operator:or; 84, 85; 84, 87; 85, not_operator; 85, 86; 86, identifier:msg; 87, comparison_operator:<; 87, 88; 87, 92; 88, call; 88, 89; 88, 90; 89, identifier:len; 90, argument_list; 90, 91; 91, identifier:msg; 92, integer:3; 93, block; 93, 94; 93, 99; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:send; 97, argument_list; 97, 98; 98, string:"Invalid Syntax."; 99, return_statement; 100, if_statement; 100, 101; 100, 106; 101, comparison_operator:==; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:args; 104, string:'type'; 105, string:'privmsg'; 106, block; 106, 107; 106, 116; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:send; 110, argument_list; 110, 111; 111, binary_operator:%; 111, 112; 111, 113; 112, string:"Don't worry, %s is not a grammar Nazi."; 113, subscript; 113, 114; 113, 115; 114, identifier:args; 115, string:'botnick'; 116, return_statement; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:string; 120, subscript; 120, 121; 120, 122; 121, identifier:msg; 122, integer:0; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:replacement; 126, subscript; 126, 127; 126, 128; 127, identifier:msg; 128, integer:1; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:modifiers; 132, call; 132, 133; 132, 134; 133, identifier:get_modifiers; 134, argument_list; 134, 135; 134, 138; 134, 141; 135, subscript; 135, 136; 135, 137; 136, identifier:msg; 137, integer:2; 138, subscript; 138, 139; 138, 140; 139, identifier:args; 140, string:'nick'; 141, subscript; 141, 142; 141, 147; 142, subscript; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:args; 145, string:'config'; 146, string:'core'; 147, string:'nickregex'; 148, if_statement; 148, 149; 148, 152; 149, comparison_operator:is; 149, 150; 149, 151; 150, identifier:modifiers; 151, None; 152, block; 152, 153; 152, 158; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:send; 156, argument_list; 156, 157; 157, string:"Invalid modifiers."; 158, return_statement; 159, try_statement; 159, 160; 159, 268; 160, block; 160, 161; 160, 183; 160, 198; 160, 206; 160, 225; 160, 253; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:regex; 164, conditional_expression:if; 164, 165; 164, 174; 164, 177; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:re; 168, identifier:compile; 169, argument_list; 169, 170; 169, 171; 170, identifier:string; 171, attribute; 171, 172; 171, 173; 172, identifier:re; 173, identifier:IGNORECASE; 174, subscript; 174, 175; 174, 176; 175, identifier:modifiers; 176, string:'ignorecase'; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:re; 180, identifier:compile; 181, argument_list; 181, 182; 182, identifier:string; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:log; 186, call; 186, 187; 186, 188; 187, identifier:get_log; 188, argument_list; 188, 189; 188, 192; 188, 195; 189, subscript; 189, 190; 189, 191; 190, identifier:args; 191, string:'db'; 192, subscript; 192, 193; 192, 194; 193, identifier:args; 194, string:'target'; 195, subscript; 195, 196; 195, 197; 196, identifier:modifiers; 197, string:'nick'; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:workers; 201, attribute; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:args; 204, string:'handler'; 205, identifier:workers; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:result; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:workers; 212, identifier:run_pool; 213, argument_list; 213, 214; 213, 215; 214, identifier:do_replace; 215, list:[log, args['config']['core'], char, regex, replacement]; 215, 216; 215, 217; 215, 222; 215, 223; 215, 224; 216, identifier:log; 217, subscript; 217, 218; 217, 221; 218, subscript; 218, 219; 218, 220; 219, identifier:args; 220, string:'config'; 221, string:'core'; 222, identifier:char; 223, identifier:regex; 224, identifier:replacement; 225, try_statement; 225, 226; 225, 236; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:msg; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:result; 233, identifier:get; 234, argument_list; 234, 235; 235, integer:5; 236, except_clause; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:multiprocessing; 239, identifier:TimeoutError; 240, block; 240, 241; 240, 247; 240, 252; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:workers; 245, identifier:restart_pool; 246, argument_list; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 250; 249, identifier:send; 250, argument_list; 250, 251; 251, string:"Sed regex timed out."; 252, return_statement; 253, if_statement; 253, 254; 253, 255; 253, 261; 254, identifier:msg; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 259; 258, identifier:send; 259, argument_list; 259, 260; 260, identifier:msg; 261, else_clause; 261, 262; 262, block; 262, 263; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:send; 266, argument_list; 266, 267; 267, string:"No match found."; 268, except_clause; 268, 269; 268, 275; 269, as_pattern; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:sre_constants; 272, identifier:error; 273, as_pattern_target; 273, 274; 274, identifier:ex; 275, block; 275, 276; 276, raise_statement; 276, 277; 277, call; 277, 278; 277, 279; 278, identifier:CommandFailedException; 279, argument_list; 279, 280; 280, identifier:ex | def cmd(send, msg, args):
"""Corrects a previous message.
Syntax: {command}/<msg>/<replacement>/<ig|nick>
"""
if not msg:
send("Invalid Syntax.")
return
char = msg[0]
msg = [x.replace(r'\/', '/') for x in re.split(r'(?<!\\)\%s' % char, msg[1:], maxsplit=2)]
# fix for people who forget a trailing slash
if len(msg) == 2 and args['config']['feature'].getboolean('lazyregex'):
msg.append('')
# not a valid sed statement.
if not msg or len(msg) < 3:
send("Invalid Syntax.")
return
if args['type'] == 'privmsg':
send("Don't worry, %s is not a grammar Nazi." % args['botnick'])
return
string = msg[0]
replacement = msg[1]
modifiers = get_modifiers(msg[2], args['nick'], args['config']['core']['nickregex'])
if modifiers is None:
send("Invalid modifiers.")
return
try:
regex = re.compile(string, re.IGNORECASE) if modifiers['ignorecase'] else re.compile(string)
log = get_log(args['db'], args['target'], modifiers['nick'])
workers = args['handler'].workers
result = workers.run_pool(do_replace, [log, args['config']['core'], char, regex, replacement])
try:
msg = result.get(5)
except multiprocessing.TimeoutError:
workers.restart_pool()
send("Sed regex timed out.")
return
if msg:
send(msg)
else:
send("No match found.")
except sre_constants.error as ex:
raise CommandFailedException(ex) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_add_single_session_to_to_ordered_dict; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:d; 6, identifier:dataset_index; 7, identifier:recommended_only; 8, block; 8, 9; 8, 11; 9, expression_statement; 9, 10; 10, comment; 11, for_statement; 11, 12; 11, 15; 11, 21; 11, 22; 11, 23; 12, pattern_list; 12, 13; 12, 14; 13, identifier:model_index; 14, identifier:model; 15, call; 15, 16; 15, 17; 16, identifier:enumerate; 17, argument_list; 17, 18; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:models; 21, comment; 22, comment; 23, block; 23, 24; 23, 28; 23, 80; 23, 89; 23, 100; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:show_null; 27, False; 28, if_statement; 28, 29; 28, 30; 29, identifier:recommended_only; 30, block; 30, 31; 31, if_statement; 31, 32; 31, 35; 31, 66; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:recommendation_enabled; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 42; 36, 55; 36, 63; 37, comparison_operator:is; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:recommended_model; 41, None; 42, block; 42, 43; 43, if_statement; 43, 44; 43, 47; 43, 52; 44, comparison_operator:==; 44, 45; 44, 46; 45, identifier:model_index; 46, integer:0; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:show_null; 51, True; 52, else_clause; 52, 53; 53, block; 53, 54; 54, continue_statement; 55, elif_clause; 55, 56; 55, 61; 56, comparison_operator:==; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:recommended_model; 60, identifier:model; 61, block; 61, 62; 62, pass_statement; 63, else_clause; 63, 64; 64, block; 64, 65; 65, continue_statement; 66, else_clause; 66, 67; 67, block; 67, 68; 68, if_statement; 68, 69; 68, 72; 68, 77; 69, comparison_operator:==; 69, 70; 69, 71; 70, identifier:model_index; 71, integer:0; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:show_null; 76, True; 77, else_clause; 77, 78; 78, block; 78, 79; 79, continue_statement; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:d; 85, string:"dataset_index"; 86, identifier:append; 87, argument_list; 87, 88; 88, identifier:dataset_index; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:d; 94, string:"doses_dropped"; 95, identifier:append; 96, argument_list; 96, 97; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:doses_dropped; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:model; 104, identifier:_to_df; 105, argument_list; 105, 106; 105, 107; 105, 108; 106, identifier:d; 107, identifier:model_index; 108, identifier:show_null | def _add_single_session_to_to_ordered_dict(self, d, dataset_index, recommended_only):
"""
Save a single session to an ordered dictionary.
"""
for model_index, model in enumerate(self.models):
# determine if model should be presented, or if a null-model should
# be presented (if no model is recommended.)
show_null = False
if recommended_only:
if self.recommendation_enabled:
if self.recommended_model is None:
if model_index == 0:
show_null = True
else:
continue
elif self.recommended_model == model:
pass
else:
continue
else:
if model_index == 0:
show_null = True
else:
continue
d["dataset_index"].append(dataset_index)
d["doses_dropped"].append(self.doses_dropped)
model._to_df(d, model_index, show_null) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_group_models; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 15; 5, 16; 5, 103; 5, 104; 5, 132; 5, 151; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:od; 11, call; 11, 12; 11, 13; 12, identifier:OrderedDict; 13, argument_list; 14, comment; 15, comment; 16, for_statement; 16, 17; 16, 20; 16, 26; 17, pattern_list; 17, 18; 17, 19; 18, identifier:i; 19, identifier:model; 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:models; 26, block; 26, 27; 26, 36; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:output; 30, call; 30, 31; 30, 32; 31, identifier:getattr; 32, argument_list; 32, 33; 32, 34; 32, 35; 33, identifier:model; 34, string:"output"; 35, dictionary; 36, if_statement; 36, 37; 36, 56; 36, 94; 37, boolean_operator:and; 37, 38; 37, 51; 38, boolean_operator:and; 38, 39; 38, 45; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:output; 42, identifier:get; 43, argument_list; 43, 44; 44, string:"AIC"; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:output; 48, identifier:get; 49, argument_list; 49, 50; 50, string:"BMD"; 51, comparison_operator:>; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:output; 54, string:"BMD"; 55, integer:0; 56, block; 56, 57; 56, 71; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:key; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, string:"{}-{}"; 63, identifier:format; 64, argument_list; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:output; 67, string:"AIC"; 68, subscript; 68, 69; 68, 70; 69, identifier:output; 70, string:"BMD"; 71, if_statement; 71, 72; 71, 75; 71, 85; 72, comparison_operator:in; 72, 73; 72, 74; 73, identifier:key; 74, identifier:od; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:od; 81, identifier:key; 82, identifier:append; 83, argument_list; 83, 84; 84, identifier:model; 85, else_clause; 85, 86; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:od; 91, identifier:key; 92, list:[model]; 92, 93; 93, identifier:model; 94, else_clause; 94, 95; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:od; 100, identifier:i; 101, list:[model]; 101, 102; 102, identifier:model; 103, comment; 104, function_definition; 104, 105; 104, 106; 104, 108; 105, function_name:_get_num_params; 106, parameters; 106, 107; 107, identifier:model; 108, block; 108, 109; 109, return_statement; 109, 110; 110, parenthesized_expression; 110, 111; 111, conditional_expression:if; 111, 112; 111, 120; 111, 131; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, subscript; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:model; 118, identifier:output; 119, string:"parameters"; 120, boolean_operator:and; 120, 121; 120, 126; 121, call; 121, 122; 121, 123; 122, identifier:hasattr; 123, argument_list; 123, 124; 123, 125; 124, identifier:model; 125, string:"output"; 126, comparison_operator:in; 126, 127; 126, 128; 127, string:"parameters"; 128, attribute; 128, 129; 128, 130; 129, identifier:model; 130, identifier:output; 131, integer:0; 132, for_statement; 132, 133; 132, 136; 132, 141; 133, pattern_list; 133, 134; 133, 135; 134, identifier:key; 135, identifier:_models; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:od; 139, identifier:items; 140, argument_list; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:_models; 146, identifier:sort; 147, argument_list; 147, 148; 148, keyword_argument; 148, 149; 148, 150; 149, identifier:key; 150, identifier:_get_num_params; 151, return_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:list; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:od; 158, identifier:values; 159, argument_list | def _group_models(self):
"""
If AIC and BMD are numeric and identical, then treat models as
identical. Returns a list of lists. The outer list is a list of related
models, the inner list contains each individual model, sorted by the
number of parameters in ascending order.
This is required because in some cases, a higher-order model may not use
some parameters and can effectively collapse to a lower order model
(for example, a 2nd order polynomial and power model may collapse to a
linear model). In summary outputs, we may want to present all models in
one row, since they are the same model effectively.
"""
od = OrderedDict()
# Add models to appropriate list. We only aggregate models which
# completed successfully and have a valid AIC and BMD.
for i, model in enumerate(self.models):
output = getattr(model, "output", {})
if output.get("AIC") and output.get("BMD") and output["BMD"] > 0:
key = "{}-{}".format(output["AIC"], output["BMD"])
if key in od:
od[key].append(model)
else:
od[key] = [model]
else:
od[i] = [model]
# Sort each list by the number of parameters
def _get_num_params(model):
return (
len(model.output["parameters"])
if hasattr(model, "output") and "parameters" in model.output
else 0
)
for key, _models in od.items():
_models.sort(key=_get_num_params)
return list(od.values()) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:random_stats; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:all_stats; 6, identifier:race; 7, identifier:ch_class; 8, block; 8, 9; 8, 11; 8, 12; 8, 16; 8, 20; 8, 41; 8, 45; 8, 142; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:stats; 15, list:[]; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:res; 19, dictionary; 20, for_statement; 20, 21; 20, 22; 20, 23; 21, identifier:s; 22, identifier:all_stats; 23, block; 23, 24; 23, 33; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:stats; 28, identifier:append; 29, argument_list; 29, 30; 30, subscript; 30, 31; 30, 32; 31, identifier:s; 32, string:'stat'; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 40; 35, subscript; 35, 36; 35, 37; 36, identifier:res; 37, subscript; 37, 38; 37, 39; 38, identifier:s; 39, string:'stat'; 40, integer:0; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:cur_stat; 44, integer:0; 45, for_statement; 45, 46; 45, 47; 45, 48; 46, identifier:stat; 47, identifier:stats; 48, block; 48, 49; 48, 79; 48, 109; 48, 110; 48, 136; 49, for_statement; 49, 50; 49, 53; 49, 61; 50, pattern_list; 50, 51; 50, 52; 51, identifier:ndx; 52, identifier:i; 53, call; 53, 54; 53, 55; 54, identifier:enumerate; 55, argument_list; 55, 56; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:classes; 60, identifier:dat; 61, block; 61, 62; 62, if_statement; 62, 63; 62, 68; 63, comparison_operator:==; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:i; 66, string:'name'; 67, identifier:ch_class; 68, block; 68, 69; 68, 78; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:cur_stat; 72, call; 72, 73; 72, 74; 73, identifier:int; 74, argument_list; 74, 75; 75, subscript; 75, 76; 75, 77; 76, identifier:i; 77, identifier:stat; 78, comment; 79, for_statement; 79, 80; 79, 83; 79, 91; 80, pattern_list; 80, 81; 80, 82; 81, identifier:ndx; 82, identifier:i; 83, call; 83, 84; 83, 85; 84, identifier:enumerate; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:races; 90, identifier:dat; 91, block; 91, 92; 92, if_statement; 92, 93; 92, 98; 93, comparison_operator:==; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:i; 96, string:'name'; 97, identifier:race; 98, block; 98, 99; 98, 108; 99, expression_statement; 99, 100; 100, augmented_assignment:+=; 100, 101; 100, 102; 101, identifier:cur_stat; 102, call; 102, 103; 102, 104; 103, identifier:int; 104, argument_list; 104, 105; 105, subscript; 105, 106; 105, 107; 106, identifier:i; 107, identifier:stat; 108, comment; 109, comment; 110, if_statement; 110, 111; 110, 114; 110, 119; 111, comparison_operator:<; 111, 112; 111, 113; 112, identifier:cur_stat; 113, integer:1; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:cur_stat; 118, integer:1; 119, elif_clause; 119, 120; 119, 123; 120, comparison_operator:>; 120, 121; 120, 122; 121, identifier:cur_stat; 122, integer:10; 123, block; 123, 124; 124, if_statement; 124, 125; 124, 130; 124, 131; 125, comparison_operator:not; 125, 126; 125, 127; 126, identifier:stat; 127, tuple; 127, 128; 127, 129; 128, string:'Health'; 129, string:'max_health'; 130, comment; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:cur_stat; 135, integer:10; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:res; 140, identifier:stat; 141, identifier:cur_stat; 142, return_statement; 142, 143; 143, identifier:res | def random_stats(self, all_stats, race, ch_class):
"""
create random stats based on the characters class and race
This looks up the tables from CharacterCollection to get
base stats and applies a close random fit
"""
# create blank list of stats to be generated
stats = []
res = {}
for s in all_stats:
stats.append(s['stat'])
res[s['stat']] = 0
cur_stat = 0
for stat in stats:
for ndx, i in enumerate(self.classes.dat):
if i['name'] == ch_class:
cur_stat = int(i[stat]) # use stats for this class as baseline
for ndx, i in enumerate(self.races.dat):
if i['name'] == race:
cur_stat += int(i[stat]) # use stats for this race to modify base stats
#print(stat, cur_stat)
if cur_stat < 1:
cur_stat = 1
elif cur_stat > 10:
if stat not in ('Health', 'max_health'): # dont trim down health
cur_stat = 10
res[stat] = cur_stat
return res |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:palette; 3, parameters; 3, 4; 3, 5; 4, identifier:fg; 5, default_parameter; 5, 6; 5, 7; 6, identifier:bg; 7, unary_operator:-; 7, 8; 8, integer:1; 9, block; 9, 10; 9, 12; 9, 26; 9, 40; 9, 54; 9, 78; 9, 79; 9, 97; 9, 137; 9, 177; 9, 190; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 19; 13, not_operator; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:hasattr; 16, argument_list; 16, 17; 16, 18; 17, identifier:palette; 18, string:"counter"; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:palette; 24, identifier:counter; 25, integer:1; 26, if_statement; 26, 27; 26, 33; 27, not_operator; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:hasattr; 30, argument_list; 30, 31; 30, 32; 31, identifier:palette; 32, string:"selections"; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:palette; 38, identifier:selections; 39, dictionary; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:selection; 43, binary_operator:%; 43, 44; 43, 45; 44, string:"%s%s"; 45, tuple; 45, 46; 45, 50; 46, call; 46, 47; 46, 48; 47, identifier:str; 48, argument_list; 48, 49; 49, identifier:fg; 50, call; 50, 51; 50, 52; 51, identifier:str; 52, argument_list; 52, 53; 53, identifier:bg; 54, if_statement; 54, 55; 54, 61; 55, not_operator; 55, 56; 56, comparison_operator:in; 56, 57; 56, 58; 57, identifier:selection; 58, attribute; 58, 59; 58, 60; 59, identifier:palette; 60, identifier:selections; 61, block; 61, 62; 61, 72; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 69; 64, subscript; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:palette; 67, identifier:selections; 68, identifier:selection; 69, attribute; 69, 70; 69, 71; 70, identifier:palette; 71, identifier:counter; 72, expression_statement; 72, 73; 73, augmented_assignment:+=; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:palette; 76, identifier:counter; 77, integer:1; 78, comment; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:colors; 82, list_comprehension; 82, 83; 82, 84; 82, 90; 83, identifier:c; 84, for_in_clause; 84, 85; 84, 86; 85, identifier:c; 86, call; 86, 87; 86, 88; 87, identifier:dir; 88, argument_list; 88, 89; 89, identifier:_curses; 90, if_clause; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:c; 94, identifier:startswith; 95, argument_list; 95, 96; 96, string:'COLOR'; 97, if_statement; 97, 98; 97, 103; 98, call; 98, 99; 98, 100; 99, identifier:isinstance; 100, argument_list; 100, 101; 100, 102; 101, identifier:fg; 102, identifier:str; 103, block; 103, 104; 104, if_statement; 104, 105; 104, 115; 104, 121; 105, not_operator; 105, 106; 106, comparison_operator:in; 106, 107; 106, 114; 107, binary_operator:+; 107, 108; 107, 109; 108, string:"COLOR_"; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:fg; 112, identifier:upper; 113, argument_list; 114, identifier:colors; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:fg; 119, unary_operator:-; 119, 120; 120, integer:1; 121, else_clause; 121, 122; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:fg; 126, call; 126, 127; 126, 128; 127, identifier:getattr; 128, argument_list; 128, 129; 128, 130; 129, identifier:_curses; 130, binary_operator:+; 130, 131; 130, 132; 131, string:"COLOR_"; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:fg; 135, identifier:upper; 136, argument_list; 137, if_statement; 137, 138; 137, 143; 138, call; 138, 139; 138, 140; 139, identifier:isinstance; 140, argument_list; 140, 141; 140, 142; 141, identifier:bg; 142, identifier:str; 143, block; 143, 144; 144, if_statement; 144, 145; 144, 155; 144, 161; 145, not_operator; 145, 146; 146, comparison_operator:in; 146, 147; 146, 154; 147, binary_operator:+; 147, 148; 147, 149; 148, string:"COLOR_"; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:bg; 152, identifier:upper; 153, argument_list; 154, identifier:colors; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:bg; 159, unary_operator:-; 159, 160; 160, integer:1; 161, else_clause; 161, 162; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:bg; 166, call; 166, 167; 166, 168; 167, identifier:getattr; 168, argument_list; 168, 169; 168, 170; 169, identifier:_curses; 170, binary_operator:+; 170, 171; 170, 172; 171, string:"COLOR_"; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:bg; 175, identifier:upper; 176, argument_list; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:_curses; 181, identifier:init_pair; 182, argument_list; 182, 183; 182, 188; 182, 189; 183, subscript; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:palette; 186, identifier:selections; 187, identifier:selection; 188, identifier:fg; 189, identifier:bg; 190, return_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:_curses; 194, identifier:color_pair; 195, argument_list; 195, 196; 196, subscript; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:palette; 199, identifier:selections; 200, identifier:selection | def palette(fg, bg=-1):
"""
Since curses only supports a finite amount of initialised colour pairs
we memoise any selections you've made as an attribute on this function
"""
if not hasattr(palette, "counter"):
palette.counter = 1
if not hasattr(palette, "selections"):
palette.selections = {}
selection = "%s%s" % (str(fg), str(bg))
if not selection in palette.selections:
palette.selections[selection] = palette.counter
palette.counter += 1
# Get available colours
colors = [c for c in dir(_curses) if c.startswith('COLOR')]
if isinstance(fg, str):
if not "COLOR_"+fg.upper() in colors:
fg = -1
else:
fg = getattr(_curses, "COLOR_"+fg.upper())
if isinstance(bg, str):
if not "COLOR_"+bg.upper() in colors:
bg = -1
else:
bg = getattr(_curses, "COLOR_"+bg.upper())
_curses.init_pair(palette.selections[selection], fg, bg)
return _curses.color_pair(palette.selections[selection]) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:coordinate; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:panes; 7, list:[]; 8, default_parameter; 8, 9; 8, 10; 9, identifier:index; 10, integer:0; 11, block; 11, 12; 11, 14; 11, 18; 11, 19; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:y; 17, integer:0; 18, comment; 19, for_statement; 19, 20; 19, 23; 19, 29; 20, pattern_list; 20, 21; 20, 22; 21, identifier:i; 22, identifier:element; 23, call; 23, 24; 23, 25; 24, identifier:enumerate; 25, argument_list; 25, 26; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:panes; 29, block; 29, 30; 29, 34; 29, 35; 29, 217; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:x; 33, integer:0; 34, comment; 35, if_statement; 35, 36; 35, 41; 35, 137; 36, call; 36, 37; 36, 38; 37, identifier:isinstance; 38, argument_list; 38, 39; 38, 40; 39, identifier:element; 40, identifier:list; 41, block; 41, 42; 41, 46; 41, 125; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:current_height; 45, integer:0; 46, for_statement; 46, 47; 46, 50; 46, 54; 47, pattern_list; 47, 48; 47, 49; 48, identifier:j; 49, identifier:pane; 50, call; 50, 51; 50, 52; 51, identifier:enumerate; 52, argument_list; 52, 53; 53, identifier:element; 54, block; 54, 55; 54, 61; 54, 67; 54, 73; 54, 85; 54, 113; 54, 121; 55, if_statement; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:pane; 58, identifier:hidden; 59, block; 59, 60; 60, continue_statement; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:current_width; 64, attribute; 64, 65; 64, 66; 65, identifier:pane; 66, identifier:width; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:current_height; 70, attribute; 70, 71; 70, 72; 71, identifier:pane; 72, identifier:height; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:upper; 76, tuple; 76, 77; 76, 80; 77, tuple; 77, 78; 77, 79; 78, identifier:y; 79, identifier:x; 80, tuple; 80, 81; 80, 82; 81, identifier:y; 82, binary_operator:+; 82, 83; 82, 84; 83, identifier:x; 84, identifier:current_width; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:lower; 88, tuple; 88, 89; 88, 100; 89, tuple; 89, 90; 89, 99; 90, binary_operator:+; 90, 91; 90, 92; 91, identifier:y; 92, parenthesized_expression; 92, 93; 93, conditional_expression:if; 93, 94; 93, 95; 93, 98; 94, identifier:current_height; 95, comparison_operator:>; 95, 96; 95, 97; 96, identifier:current_height; 97, integer:1; 98, integer:0; 99, identifier:x; 100, tuple; 100, 101; 100, 110; 101, binary_operator:+; 101, 102; 101, 103; 102, identifier:y; 103, parenthesized_expression; 103, 104; 104, conditional_expression:if; 104, 105; 104, 106; 104, 109; 105, identifier:current_height; 106, comparison_operator:>; 106, 107; 106, 108; 107, identifier:current_height; 108, integer:1; 109, integer:0; 110, binary_operator:+; 110, 111; 110, 112; 111, identifier:x; 112, identifier:current_width; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:pane; 117, identifier:coords; 118, list:[upper, lower]; 118, 119; 118, 120; 119, identifier:upper; 120, identifier:lower; 121, expression_statement; 121, 122; 122, augmented_assignment:+=; 122, 123; 122, 124; 123, identifier:x; 124, identifier:current_width; 125, expression_statement; 125, 126; 126, augmented_assignment:+=; 126, 127; 126, 128; 127, identifier:y; 128, parenthesized_expression; 128, 129; 129, conditional_expression:if; 129, 130; 129, 133; 129, 136; 130, binary_operator:+; 130, 131; 130, 132; 131, identifier:current_height; 132, integer:1; 133, comparison_operator:>; 133, 134; 133, 135; 134, identifier:current_height; 135, integer:1; 136, integer:1; 137, else_clause; 137, 138; 138, block; 138, 139; 138, 145; 138, 151; 138, 157; 138, 169; 138, 197; 138, 205; 139, if_statement; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:element; 142, identifier:hidden; 143, block; 143, 144; 144, continue_statement; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:current_width; 148, attribute; 148, 149; 148, 150; 149, identifier:element; 150, identifier:width; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:current_height; 154, attribute; 154, 155; 154, 156; 155, identifier:element; 156, identifier:height; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:upper; 160, tuple; 160, 161; 160, 164; 161, tuple; 161, 162; 161, 163; 162, identifier:y; 163, identifier:x; 164, tuple; 164, 165; 164, 166; 165, identifier:y; 166, binary_operator:+; 166, 167; 166, 168; 167, identifier:x; 168, identifier:current_width; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:lower; 172, tuple; 172, 173; 172, 184; 173, tuple; 173, 174; 173, 183; 174, binary_operator:+; 174, 175; 174, 176; 175, identifier:y; 176, parenthesized_expression; 176, 177; 177, conditional_expression:if; 177, 178; 177, 179; 177, 182; 178, identifier:current_height; 179, comparison_operator:>; 179, 180; 179, 181; 180, identifier:current_height; 181, integer:1; 182, integer:0; 183, identifier:x; 184, tuple; 184, 185; 184, 194; 185, binary_operator:+; 185, 186; 185, 187; 186, identifier:y; 187, parenthesized_expression; 187, 188; 188, conditional_expression:if; 188, 189; 188, 190; 188, 193; 189, identifier:current_height; 190, comparison_operator:>; 190, 191; 190, 192; 191, identifier:current_height; 192, integer:1; 193, integer:0; 194, binary_operator:+; 194, 195; 194, 196; 195, identifier:x; 196, identifier:current_width; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:element; 201, identifier:coords; 202, list:[upper, lower]; 202, 203; 202, 204; 203, identifier:upper; 204, identifier:lower; 205, expression_statement; 205, 206; 206, augmented_assignment:+=; 206, 207; 206, 208; 207, identifier:y; 208, parenthesized_expression; 208, 209; 209, conditional_expression:if; 209, 210; 209, 213; 209, 216; 210, binary_operator:+; 210, 211; 210, 212; 211, identifier:current_height; 212, integer:1; 213, comparison_operator:>; 213, 214; 213, 215; 214, identifier:current_height; 215, integer:1; 216, integer:1; 217, if_statement; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:debug; 221, block; 221, 222; 221, 237; 221, 263; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:coordinates; 225, binary_operator:+; 225, 226; 225, 227; 226, string:"Coordinates: "; 227, call; 227, 228; 227, 229; 228, identifier:str; 229, argument_list; 229, 230; 230, list_comprehension; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:p; 233, identifier:coords; 234, for_in_clause; 234, 235; 234, 236; 235, identifier:p; 236, identifier:self; 237, if_statement; 237, 238; 237, 246; 238, comparison_operator:>; 238, 239; 238, 243; 239, call; 239, 240; 239, 241; 240, identifier:len; 241, argument_list; 241, 242; 242, identifier:coordinates; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:width; 246, block; 246, 247; 246, 259; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:coordinates; 250, subscript; 250, 251; 250, 252; 251, identifier:coordinates; 252, slice; 252, 253; 252, 254; 253, colon; 254, binary_operator:-; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:width; 258, integer:3; 259, expression_statement; 259, 260; 260, augmented_assignment:+=; 260, 261; 260, 262; 261, identifier:coordinates; 262, string:'...'; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:self; 267, identifier:addstr; 268, argument_list; 268, 269; 268, 274; 268, 275; 269, binary_operator:-; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:height; 273, integer:3; 274, integer:0; 275, identifier:coordinates | def coordinate(self, panes=[], index=0):
"""
Update pane coordinate tuples based on their height and width relative to other panes
within the dimensions of the current window.
We account for panes with a height of 1 where the bottom coordinates are the same as the top.
Account for floating panes and self-coordinating panes adjacent to panes set to EXPAND.
Coordinates are of the form:
[
((top-left-from-top, top-left-from-left),
(top-right-from-top, top-right-from-left)),
((bottom-left-from-top, bottom-left-from-left),
(bottom-right-from-top, bottom-right-from-left))
]
We can then use these to determine things such as whether corners are inverted and how
many characters may be drawn
"""
y = 0 # height
for i, element in enumerate(self.panes):
x = 0 # width
if isinstance(element, list):
current_height = 0
for j, pane in enumerate(element):
if pane.hidden: continue
current_width = pane.width
current_height = pane.height
upper = ((y, x), (y, x+current_width))
lower = ((y+(current_height if current_height > 1 else 0), x),
(y+(current_height if current_height > 1 else 0), x+current_width))
pane.coords = [upper, lower]
x += current_width
y += (current_height+1 if current_height > 1 else 1)
else:
if element.hidden: continue
current_width = element.width
current_height = element.height
upper = ((y, x), (y, x+current_width))
lower = ((y+(current_height if current_height > 1 else 0), x),
(y+(current_height if current_height > 1 else 0), x+current_width))
element.coords = [upper, lower]
y += (current_height+1 if current_height > 1 else 1)
if self.debug:
coordinates = "Coordinates: " + str([p.coords for p in self])
if len(coordinates) > self.width:
coordinates = coordinates[:self.width - 3]
coordinates += '...'
self.addstr(self.height-3, 0, coordinates) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:_MakePackagePages; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:package; 6, default_parameter; 6, 7; 6, 8; 7, identifier:showprivate; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:nested; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:showinh; 14, False; 15, block; 15, 16; 15, 18; 15, 76; 15, 77; 15, 89; 15, 90; 15, 100; 15, 147; 15, 154; 15, 155; 15, 156; 15, 160; 15, 164; 15, 278; 15, 394; 15, 395; 15, 413; 15, 425; 16, expression_statement; 16, 17; 17, comment; 18, function_definition; 18, 19; 18, 20; 18, 22; 19, function_name:checkNoNested; 20, parameters; 20, 21; 21, identifier:mod; 22, block; 22, 23; 22, 36; 22, 48; 22, 64; 22, 74; 23, try_statement; 23, 24; 23, 31; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:all; 28, attribute; 28, 29; 28, 30; 29, identifier:mod; 30, identifier:__all__; 31, except_clause; 31, 32; 31, 33; 32, identifier:AttributeError; 33, block; 33, 34; 34, return_statement; 34, 35; 35, False; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:mems; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:inspect; 42, identifier:getmembers; 43, argument_list; 43, 44; 43, 45; 44, identifier:mod; 45, attribute; 45, 46; 45, 47; 46, identifier:inspect; 47, identifier:ismodule; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:mems; 51, list_comprehension; 51, 52; 51, 53; 51, 56; 52, identifier:m; 53, for_in_clause; 53, 54; 53, 55; 54, identifier:m; 55, identifier:mems; 56, if_clause; 56, 57; 57, comparison_operator:in; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:m; 60, integer:0; 61, attribute; 61, 62; 61, 63; 62, identifier:mod; 63, identifier:__all__; 64, if_statement; 64, 65; 64, 71; 65, comparison_operator:>; 65, 66; 65, 70; 66, call; 66, 67; 66, 68; 67, identifier:len; 68, argument_list; 68, 69; 69, identifier:mems; 70, integer:0; 71, block; 71, 72; 72, return_statement; 72, 73; 73, False; 74, return_statement; 74, 75; 75, True; 76, comment; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:mods; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:inspect; 83, identifier:getmembers; 84, argument_list; 84, 85; 84, 86; 85, identifier:package; 86, attribute; 86, 87; 86, 88; 87, identifier:inspect; 88, identifier:ismodule; 89, comment; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 96; 92, pattern_list; 92, 93; 92, 94; 92, 95; 93, identifier:nmods; 94, identifier:pvt; 95, identifier:npkgs; 96, expression_list; 96, 97; 96, 98; 96, 99; 97, list:[]; 98, list:[]; 99, list:[]; 100, for_statement; 100, 101; 100, 102; 100, 103; 100, 104; 101, identifier:mod; 102, identifier:mods; 103, comment; 104, block; 104, 105; 105, if_statement; 105, 106; 105, 112; 105, 138; 106, call; 106, 107; 106, 108; 107, identifier:checkNoNested; 108, argument_list; 108, 109; 109, subscript; 109, 110; 109, 111; 110, identifier:mod; 111, integer:1; 112, block; 112, 113; 113, if_statement; 113, 114; 113, 121; 113, 129; 114, comparison_operator:==; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:mod; 118, integer:0; 119, integer:0; 120, string:'_'; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:pvt; 126, identifier:append; 127, argument_list; 127, 128; 128, identifier:mod; 129, else_clause; 129, 130; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:nmods; 135, identifier:append; 136, argument_list; 136, 137; 137, identifier:mod; 138, else_clause; 138, 139; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:npkgs; 144, identifier:append; 145, argument_list; 145, 146; 146, identifier:mod; 147, if_statement; 147, 148; 147, 149; 148, identifier:showprivate; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, augmented_assignment:+=; 151, 152; 151, 153; 152, identifier:nmods; 153, identifier:pvt; 154, comment; 155, comment; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:files; 159, list:[]; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:ignore; 163, list:[]; 164, for_statement; 164, 165; 164, 166; 164, 167; 165, identifier:pkg; 166, identifier:npkgs; 167, block; 167, 168; 167, 199; 167, 216; 167, 223; 167, 234; 167, 254; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:pt; 171, binary_operator:%; 171, 172; 171, 173; 172, string:'%s/%s/%s'; 173, tuple; 173, 174; 173, 177; 173, 186; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:path; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:package; 181, identifier:__name__; 182, identifier:replace; 183, argument_list; 183, 184; 183, 185; 184, string:'.'; 185, string:'/'; 186, subscript; 186, 187; 186, 197; 187, call; 187, 188; 187, 195; 188, attribute; 188, 189; 188, 194; 189, attribute; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:pkg; 192, integer:1; 193, identifier:__name__; 194, identifier:split; 195, argument_list; 195, 196; 196, string:'.'; 197, unary_operator:-; 197, 198; 198, integer:1; 199, if_statement; 199, 200; 199, 208; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:os; 204, identifier:path; 205, identifier:exists; 206, argument_list; 206, 207; 207, identifier:pt; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:shutil; 213, identifier:rmtree; 214, argument_list; 214, 215; 215, identifier:pt; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:os; 220, identifier:makedirs; 221, argument_list; 221, 222; 222, identifier:pt; 223, expression_statement; 223, 224; 224, augmented_assignment:+=; 224, 225; 224, 226; 225, identifier:ignore; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:inspect; 229, identifier:getmembers; 230, argument_list; 230, 231; 231, subscript; 231, 232; 231, 233; 232, identifier:pkg; 233, integer:1; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:f; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:_MakePackagePages; 241, argument_list; 241, 242; 241, 245; 241, 248; 241, 251; 242, subscript; 242, 243; 242, 244; 243, identifier:pkg; 244, integer:1; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:showprivate; 247, identifier:showprivate; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:nested; 250, True; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:showinh; 253, identifier:showinh; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:files; 258, identifier:append; 259, argument_list; 259, 260; 260, subscript; 260, 261; 260, 277; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:f; 264, identifier:split; 265, argument_list; 265, 266; 266, binary_operator:+; 266, 267; 266, 276; 267, call; 267, 268; 267, 273; 268, attribute; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:package; 271, identifier:__name__; 272, identifier:replace; 273, argument_list; 273, 274; 273, 275; 274, string:'.'; 275, string:'/'; 276, string:'/'; 277, integer:1; 278, if_statement; 278, 279; 278, 280; 279, identifier:nested; 280, block; 280, 281; 280, 298; 280, 299; 280, 312; 280, 313; 280, 322; 280, 323; 280, 340; 280, 355; 280, 356; 280, 389; 280, 390; 281, try_statement; 281, 282; 281, 289; 282, block; 282, 283; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:name; 286, attribute; 286, 287; 286, 288; 287, identifier:package; 288, identifier:__displayname__; 289, except_clause; 289, 290; 289, 291; 290, identifier:AttributeError; 291, block; 291, 292; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:name; 295, attribute; 295, 296; 295, 297; 296, identifier:package; 297, identifier:__name__; 298, comment; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:index; 302, binary_operator:%; 302, 303; 302, 304; 303, string:r'''
%s
%s
.. toctree::
:maxdepth: 5
'''; 304, tuple; 304, 305; 304, 306; 305, identifier:name; 306, binary_operator:*; 306, 307; 306, 308; 307, string:'*'; 308, call; 308, 309; 308, 310; 309, identifier:len; 310, argument_list; 310, 311; 311, identifier:name; 312, comment; 313, expression_statement; 313, 314; 314, augmented_assignment:+=; 314, 315; 314, 316; 315, identifier:index; 316, call; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, string:'\n '; 319, identifier:join; 320, argument_list; 320, 321; 321, identifier:files; 322, comment; 323, expression_statement; 323, 324; 324, augmented_assignment:+=; 324, 325; 324, 326; 325, identifier:index; 326, binary_operator:+; 326, 327; 326, 328; 327, string:'\n '; 328, call; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:self; 331, identifier:_ProduceContent; 332, argument_list; 332, 333; 332, 334; 332, 337; 333, identifier:nmods; 334, keyword_argument; 334, 335; 334, 336; 335, identifier:showprivate; 336, identifier:showprivate; 337, keyword_argument; 337, 338; 337, 339; 338, identifier:showinh; 339, identifier:showinh; 340, expression_statement; 340, 341; 341, assignment; 341, 342; 341, 343; 342, identifier:findex; 343, binary_operator:%; 343, 344; 343, 345; 344, string:'content/%s/index.rst'; 345, parenthesized_expression; 345, 346; 346, call; 346, 347; 346, 352; 347, attribute; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:package; 350, identifier:__name__; 351, identifier:replace; 352, argument_list; 352, 353; 352, 354; 353, string:'.'; 354, string:'/'; 355, comment; 356, with_statement; 356, 357; 356, 367; 357, with_clause; 357, 358; 358, with_item; 358, 359; 359, as_pattern; 359, 360; 359, 365; 360, call; 360, 361; 360, 362; 361, identifier:open; 362, argument_list; 362, 363; 362, 364; 363, identifier:findex; 364, string:'w'; 365, as_pattern_target; 365, 366; 366, identifier:f; 367, block; 367, 368; 367, 382; 368, if_statement; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:package; 371, identifier:__doc__; 372, block; 372, 373; 373, expression_statement; 373, 374; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:f; 377, identifier:write; 378, argument_list; 378, 379; 379, attribute; 379, 380; 379, 381; 380, identifier:package; 381, identifier:__doc__; 382, expression_statement; 382, 383; 383, call; 383, 384; 383, 387; 384, attribute; 384, 385; 384, 386; 385, identifier:f; 386, identifier:write; 387, argument_list; 387, 388; 388, identifier:index; 389, comment; 390, return_statement; 390, 391; 391, binary_operator:+; 391, 392; 391, 393; 392, string:'\n '; 393, identifier:findex; 394, comment; 395, expression_statement; 395, 396; 396, assignment; 396, 397; 396, 398; 397, identifier:names; 398, binary_operator:%; 398, 399; 398, 400; 399, string:'\n %s/%s/'; 400, tuple; 400, 401; 400, 404; 401, attribute; 401, 402; 401, 403; 402, identifier:self; 403, identifier:path; 404, call; 404, 405; 404, 410; 405, attribute; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:package; 408, identifier:__name__; 409, identifier:replace; 410, argument_list; 410, 411; 410, 412; 411, string:'.'; 412, string:'/'; 413, expression_statement; 413, 414; 414, assignment; 414, 415; 414, 416; 415, identifier:nmods; 416, list_comprehension; 416, 417; 416, 418; 416, 421; 417, identifier:m; 418, for_in_clause; 418, 419; 418, 420; 419, identifier:m; 420, identifier:nmods; 421, if_clause; 421, 422; 422, comparison_operator:not; 422, 423; 422, 424; 423, identifier:m; 424, identifier:ignore; 425, return_statement; 425, 426; 426, call; 426, 427; 426, 430; 427, attribute; 427, 428; 427, 429; 428, identifier:names; 429, identifier:join; 430, argument_list; 430, 431; 431, binary_operator:+; 431, 432; 431, 449; 432, call; 432, 433; 432, 447; 433, attribute; 433, 434; 433, 446; 434, call; 434, 435; 434, 438; 435, attribute; 435, 436; 435, 437; 436, identifier:self; 437, identifier:_ProduceContent; 438, argument_list; 438, 439; 438, 440; 438, 443; 439, identifier:nmods; 440, keyword_argument; 440, 441; 440, 442; 441, identifier:showprivate; 442, identifier:showprivate; 443, keyword_argument; 443, 444; 443, 445; 444, identifier:showinh; 445, identifier:showinh; 446, identifier:split; 447, argument_list; 447, 448; 448, string:'\n '; 449, identifier:files | def _MakePackagePages(self, package, showprivate=False, nested=False, showinh=False):
"""An internal helper to generate all of the pages for a given package
Args:
package (module): The top-level package to document
showprivate (bool): A flag for whether or not to display private members
nested (bool): Foor internal use ONLY
Returns:
str: The file names ready to be appended to a top-level toctree
"""
def checkNoNested(mod):
try:
all = mod.__all__
except AttributeError:
return False
mems = inspect.getmembers(mod, inspect.ismodule)
mems = [m for m in mems if m[0] in mod.__all__]
if len(mems) > 0:
return False
return True
# Get package module members
mods = inspect.getmembers(package, inspect.ismodule)
# Split into modules and sub-packages
nmods, pvt, npkgs = [], [], []
for mod in mods:
# Deal with private modules
if checkNoNested(mod[1]):
if mod[0][0] == '_': pvt.append(mod)
else: nmods.append(mod)
else: npkgs.append(mod)
if showprivate: nmods += pvt
# for each member that has a nested module
# recurse and keep track of index files for that package
files = []
ignore = []
for pkg in npkgs:
pt = '%s/%s/%s' % (self.path, package.__name__.replace('.', '/'), pkg[1].__name__.split('.')[-1])
if os.path.exists(pt): shutil.rmtree(pt)
os.makedirs(pt)
ignore += inspect.getmembers(pkg[1])
f = self._MakePackagePages(pkg[1], showprivate=showprivate, nested=True, showinh=showinh)
files.append(f.split(package.__name__.replace('.', '/')+'/')[1])
if nested:
try:
name = package.__displayname__
except AttributeError:
name = package.__name__
# Create index file here
index = r'''
%s
%s
.. toctree::
:maxdepth: 5
''' % (name, '*' * len(name))
# include sub packages first
index += '\n '.join(files)
# then include modules
index += '\n ' + self._ProduceContent(nmods, showprivate=showprivate, showinh=showinh)
findex = 'content/%s/index.rst' % (package.__name__.replace('.', '/'))
# Write the file
with open(findex, 'w') as f:
if package.__doc__: f.write(package.__doc__)
f.write(index)
# return filename for index file at package level
return '\n ' + findex
# Not nested: return all files
names = '\n %s/%s/' % ( self.path, package.__name__.replace('.', '/'))
nmods = [m for m in nmods if m not in ignore]
return names.join(self._ProduceContent(nmods, showprivate=showprivate, showinh=showinh).split('\n ')+files) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:_DocPackageFromTop; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:packages; 6, default_parameter; 6, 7; 6, 8; 7, identifier:showprivate; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:showinh; 11, False; 12, block; 12, 13; 12, 15; 12, 19; 12, 32; 12, 49; 12, 56; 12, 63; 12, 64; 12, 311; 12, 312; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:appIndex; 18, string:''; 19, if_statement; 19, 20; 19, 26; 20, not_operator; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:isinstance; 23, argument_list; 23, 24; 23, 25; 24, identifier:packages; 25, identifier:list; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:packages; 30, list:[packages]; 30, 31; 31, identifier:packages; 32, if_statement; 32, 33; 32, 41; 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:exists; 39, argument_list; 39, 40; 40, string:'content'; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:shutil; 46, identifier:rmtree; 47, argument_list; 47, 48; 48, string:'content'; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:os; 53, identifier:makedirs; 54, argument_list; 54, 55; 55, string:'content'; 56, expression_statement; 56, 57; 57, augmented_assignment:+=; 57, 58; 57, 59; 58, identifier:appIndex; 59, binary_operator:%; 59, 60; 59, 61; 60, string:r'''
.. toctree::
:maxdepth: 5
:hidden:
:caption: %s:
'''; 61, parenthesized_expression; 61, 62; 62, string:'API Index'; 63, comment; 64, for_statement; 64, 65; 64, 66; 64, 73; 64, 74; 65, identifier:i; 66, call; 66, 67; 66, 68; 67, identifier:range; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:len; 71, argument_list; 71, 72; 72, identifier:packages; 73, comment; 74, block; 74, 75; 74, 81; 74, 98; 74, 99; 74, 107; 74, 124; 74, 131; 74, 132; 74, 133; 74, 134; 74, 149; 74, 158; 74, 167; 74, 176; 74, 185; 74, 194; 74, 203; 74, 212; 74, 221; 74, 229; 74, 236; 74, 251; 74, 263; 74, 305; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:package; 78, subscript; 78, 79; 78, 80; 79, identifier:packages; 80, identifier:i; 81, try_statement; 81, 82; 81, 89; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:name; 86, attribute; 86, 87; 86, 88; 87, identifier:package; 88, identifier:__displayname__; 89, except_clause; 89, 90; 89, 91; 90, identifier:AttributeError; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:name; 95, attribute; 95, 96; 95, 97; 96, identifier:package; 97, identifier:__name__; 98, comment; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:path; 102, binary_operator:%; 102, 103; 102, 104; 103, string:'content/%s'; 104, attribute; 104, 105; 104, 106; 105, identifier:package; 106, identifier:__name__; 107, if_statement; 107, 108; 107, 116; 108, call; 108, 109; 108, 114; 109, attribute; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:os; 112, identifier:path; 113, identifier:exists; 114, argument_list; 114, 115; 115, identifier:path; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:shutil; 121, identifier:rmtree; 122, argument_list; 122, 123; 123, identifier:path; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:os; 128, identifier:makedirs; 129, argument_list; 129, 130; 130, identifier:path; 131, comment; 132, comment; 133, comment; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:meta; 137, binary_operator:%; 137, 138; 137, 139; 138, string:'About %s\n%s\n'; 139, tuple; 139, 140; 139, 141; 140, identifier:name; 141, binary_operator:*; 141, 142; 141, 143; 142, string:'='; 143, call; 143, 144; 143, 145; 144, identifier:len; 145, argument_list; 145, 146; 146, binary_operator:+; 146, 147; 146, 148; 147, string:'About '; 148, identifier:name; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:author; 152, call; 152, 153; 152, 154; 153, identifier:getattr; 154, argument_list; 154, 155; 154, 156; 154, 157; 155, identifier:package; 156, string:"__author__"; 157, None; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:license; 161, call; 161, 162; 161, 163; 162, identifier:getattr; 163, argument_list; 163, 164; 163, 165; 163, 166; 164, identifier:package; 165, string:"__license__"; 166, None; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:copyright; 170, call; 170, 171; 170, 172; 171, identifier:getattr; 172, argument_list; 172, 173; 172, 174; 172, 175; 173, identifier:package; 174, string:"__copyright__"; 175, None; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:version; 179, call; 179, 180; 179, 181; 180, identifier:getattr; 181, argument_list; 181, 182; 181, 183; 181, 184; 182, identifier:package; 183, string:"__version__"; 184, None; 185, if_statement; 185, 186; 185, 187; 186, identifier:author; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, augmented_assignment:+=; 189, 190; 189, 191; 190, identifier:meta; 191, binary_operator:%; 191, 192; 191, 193; 192, string:'\n* Author: %s'; 193, identifier:author; 194, if_statement; 194, 195; 194, 196; 195, identifier:license; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, augmented_assignment:+=; 198, 199; 198, 200; 199, identifier:meta; 200, binary_operator:%; 200, 201; 200, 202; 201, string:'\n* License: %s'; 202, identifier:license; 203, if_statement; 203, 204; 203, 205; 204, identifier:copyright; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, augmented_assignment:+=; 207, 208; 207, 209; 208, identifier:meta; 209, binary_operator:%; 209, 210; 209, 211; 210, string:'\n* Copyright: %s'; 211, identifier:copyright; 212, if_statement; 212, 213; 212, 214; 213, identifier:version; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, augmented_assignment:+=; 216, 217; 216, 218; 217, identifier:meta; 218, binary_operator:%; 218, 219; 218, 220; 219, string:'\n* Version: %s'; 220, identifier:version; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:about; 224, binary_operator:%; 224, 225; 224, 226; 225, string:'%s/%s'; 226, tuple; 226, 227; 226, 228; 227, identifier:path; 228, string:'index.rst'; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:this_toc; 232, binary_operator:%; 232, 233; 232, 234; 233, string:r'''
.. toctree::
:maxdepth: 5
:caption: %s:
'''; 234, parenthesized_expression; 234, 235; 235, identifier:name; 236, expression_statement; 236, 237; 237, augmented_assignment:+=; 237, 238; 237, 239; 238, identifier:this_toc; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:self; 242, identifier:_MakePackagePages; 243, argument_list; 243, 244; 243, 245; 243, 248; 244, identifier:package; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:showprivate; 247, identifier:showprivate; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:showinh; 250, identifier:showinh; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:this_toc; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:this_toc; 257, identifier:replace; 258, argument_list; 258, 259; 258, 262; 259, binary_operator:%; 259, 260; 259, 261; 260, string:'%s/'; 261, identifier:path; 262, string:''; 263, with_statement; 263, 264; 263, 274; 264, with_clause; 264, 265; 265, with_item; 265, 266; 266, as_pattern; 266, 267; 266, 272; 267, call; 267, 268; 267, 269; 268, identifier:open; 269, argument_list; 269, 270; 269, 271; 270, identifier:about; 271, string:'w'; 272, as_pattern_target; 272, 273; 273, identifier:f; 274, block; 274, 275; 274, 284; 274, 298; 275, expression_statement; 275, 276; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:f; 279, identifier:write; 280, argument_list; 280, 281; 281, binary_operator:%; 281, 282; 281, 283; 282, string:'%s\n\n'; 283, identifier:meta; 284, if_statement; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:package; 287, identifier:__doc__; 288, block; 288, 289; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:f; 293, identifier:write; 294, argument_list; 294, 295; 295, attribute; 295, 296; 295, 297; 296, identifier:package; 297, identifier:__doc__; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:f; 302, identifier:write; 303, argument_list; 303, 304; 304, identifier:this_toc; 305, expression_statement; 305, 306; 306, augmented_assignment:+=; 306, 307; 306, 308; 307, identifier:appIndex; 308, binary_operator:%; 308, 309; 308, 310; 309, string:'\n %s'; 310, identifier:about; 311, comment; 312, return_statement; 312, 313; 313, identifier:appIndex | def _DocPackageFromTop(self, packages, showprivate=False, showinh=False):
"""Generates all of the documentation for given packages and
appends new tocrees to the index. All documentation pages will be under the
set relative path.
Args:
packages (list(module)): A package or list of packages that contain submodules to document
showprivate (bool): A flag for whether or not to display private members
Returns:
str: The new content to append to the index
"""
appIndex = ''
if not isinstance(packages, list):
packages = [packages]
if os.path.exists('content'):
shutil.rmtree('content')
os.makedirs('content')
appIndex += r'''
.. toctree::
:maxdepth: 5
:hidden:
:caption: %s:
''' % ('API Index')
# Iterate over each package and generate appropriate pages
for i in range(len(packages)):
# The package to document and its path
package = packages[i]
try:
name = package.__displayname__
except AttributeError:
name = package.__name__
# Make sure paths are ready
path = 'content/%s' % package.__name__
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path)
# Check if there is top level documentation
# if package.__doc__:
# Get metadata
meta = 'About %s\n%s\n' % (name, '='*len('About ' + name))
author = getattr(package, "__author__", None)
license = getattr(package, "__license__", None)
copyright = getattr(package, "__copyright__", None)
version = getattr(package, "__version__", None)
if author: meta += '\n* Author: %s' % author
if license: meta += '\n* License: %s' % license
if copyright: meta += '\n* Copyright: %s' % copyright
if version: meta += '\n* Version: %s' % version
about = '%s/%s' % (path, 'index.rst')
this_toc = r'''
.. toctree::
:maxdepth: 5
:caption: %s:
''' % (name)
this_toc += self._MakePackagePages(package, showprivate=showprivate, showinh=showinh)
this_toc = this_toc.replace('%s/' % path, '')
with open(about, 'w') as f:
f.write('%s\n\n' % meta)
if package.__doc__:
f.write(package.__doc__)
f.write(this_toc)
appIndex += '\n %s' % about
# Return the new content to append
return appIndex |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:cmd; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:send; 5, identifier:msg; 6, identifier:args; 7, block; 7, 8; 7, 10; 7, 29; 7, 42; 7, 83; 7, 95; 7, 99; 7, 103; 7, 165; 7, 180; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 22; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 20; 13, attribute; 13, 14; 13, 19; 14, subscript; 14, 15; 14, 18; 15, subscript; 15, 16; 15, 17; 16, identifier:args; 17, string:'config'; 18, string:'feature'; 19, identifier:getboolean; 20, argument_list; 20, 21; 21, string:'hooks'; 22, block; 22, 23; 22, 28; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:send; 26, argument_list; 26, 27; 27, string:"Hooks are disabled, and this command depends on hooks. Please contact the bot admin(s)."; 28, return_statement; 29, if_statement; 29, 30; 29, 35; 30, comparison_operator:==; 30, 31; 30, 34; 31, subscript; 31, 32; 31, 33; 32, identifier:args; 33, string:'type'; 34, string:'privmsg'; 35, block; 35, 36; 35, 41; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:send; 39, argument_list; 39, 40; 40, string:"Note-passing should be done in public."; 41, return_statement; 42, try_statement; 42, 43; 42, 74; 43, block; 43, 44; 43, 57; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 49; 46, pattern_list; 46, 47; 46, 48; 47, identifier:nick; 48, identifier:note; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:msg; 52, identifier:split; 53, argument_list; 53, 54; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:maxsplit; 56, integer:1; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:nicks; 60, call; 60, 61; 60, 62; 61, identifier:set; 62, generator_expression; 62, 63; 62, 64; 62, 72; 63, identifier:x; 64, for_in_clause; 64, 65; 64, 66; 65, identifier:x; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:nick; 69, identifier:split; 70, argument_list; 70, 71; 71, string:','; 72, if_clause; 72, 73; 73, identifier:x; 74, except_clause; 74, 75; 74, 76; 75, identifier:ValueError; 76, block; 76, 77; 76, 82; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:send; 80, argument_list; 80, 81; 81, string:"Not enough arguments."; 82, return_statement; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:nickregex; 86, binary_operator:+; 86, 87; 86, 94; 87, subscript; 87, 88; 87, 93; 88, subscript; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:args; 91, string:'config'; 92, string:'core'; 93, string:'nickregex'; 94, string:'+$'; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:successful_nicks; 98, list:[]; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:failed_nicks; 102, list:[]; 103, for_statement; 103, 104; 103, 105; 103, 106; 104, identifier:nick; 105, identifier:nicks; 106, block; 106, 107; 107, if_statement; 107, 108; 107, 115; 107, 156; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:re; 111, identifier:match; 112, argument_list; 112, 113; 112, 114; 113, identifier:nickregex; 114, identifier:nick; 115, block; 115, 116; 115, 140; 115, 149; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:row; 119, call; 119, 120; 119, 121; 120, identifier:Notes; 121, argument_list; 121, 122; 121, 125; 121, 130; 121, 133; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:note; 124, identifier:note; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:submitter; 127, subscript; 127, 128; 127, 129; 128, identifier:args; 129, string:'nick'; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:nick; 132, identifier:nick; 133, keyword_argument; 133, 134; 133, 135; 134, identifier:time; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:datetime; 138, identifier:now; 139, argument_list; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:args; 145, string:'db'; 146, identifier:add; 147, argument_list; 147, 148; 148, identifier:row; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:successful_nicks; 153, identifier:append; 154, argument_list; 154, 155; 155, identifier:nick; 156, else_clause; 156, 157; 157, block; 157, 158; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:failed_nicks; 162, identifier:append; 163, argument_list; 163, 164; 164, identifier:nick; 165, if_statement; 165, 166; 165, 167; 166, identifier:successful_nicks; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:send; 171, argument_list; 171, 172; 172, binary_operator:%; 172, 173; 172, 174; 173, string:"Note left for %s."; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, string:", "; 177, identifier:join; 178, argument_list; 178, 179; 179, identifier:successful_nicks; 180, if_statement; 180, 181; 180, 182; 181, identifier:failed_nicks; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:send; 186, argument_list; 186, 187; 187, binary_operator:%; 187, 188; 187, 189; 188, string:"Invalid nick(s): %s."; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, string:", "; 192, identifier:join; 193, argument_list; 193, 194; 194, identifier:failed_nicks | def cmd(send, msg, args):
"""Leaves a note for a user or users.
Syntax: {command} <nick>[,nick2,...] <note>
"""
if not args['config']['feature'].getboolean('hooks'):
send("Hooks are disabled, and this command depends on hooks. Please contact the bot admin(s).")
return
if args['type'] == 'privmsg':
send("Note-passing should be done in public.")
return
try:
nick, note = msg.split(maxsplit=1)
nicks = set(x for x in nick.split(',') if x)
except ValueError:
send("Not enough arguments.")
return
nickregex = args['config']['core']['nickregex'] + '+$'
successful_nicks = []
failed_nicks = []
for nick in nicks:
if re.match(nickregex, nick):
row = Notes(note=note, submitter=args['nick'], nick=nick, time=datetime.now())
args['db'].add(row)
successful_nicks.append(nick)
else:
failed_nicks.append(nick)
if successful_nicks:
send("Note left for %s." % ", ".join(successful_nicks))
if failed_nicks:
send("Invalid nick(s): %s." % ", ".join(failed_nicks)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:add_category; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:category; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, string:'''
Add unicode category to set
Unicode categories are strings like 'Ll', 'Lu', 'Nd', etc.
See `unicodedata.category()`
'''; 9, if_statement; 9, 10; 9, 15; 9, 22; 9, 37; 9, 69; 9, 103; 9, 145; 10, comparison_operator:==; 10, 11; 10, 12; 11, identifier:category; 12, attribute; 12, 13; 12, 14; 13, identifier:sre; 14, identifier:CATEGORY_DIGIT; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, augmented_assignment:|=; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:_categories; 21, identifier:UNICODE_DIGIT_CATEGORIES; 22, elif_clause; 22, 23; 22, 28; 23, comparison_operator:==; 23, 24; 23, 25; 24, identifier:category; 25, attribute; 25, 26; 25, 27; 26, identifier:sre; 27, identifier:CATEGORY_NOT_DIGIT; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, augmented_assignment:|=; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:_categories; 34, binary_operator:-; 34, 35; 34, 36; 35, identifier:UNICODE_CATEGORIES; 36, identifier:UNICODE_DIGIT_CATEGORIES; 37, elif_clause; 37, 38; 37, 43; 38, comparison_operator:==; 38, 39; 38, 40; 39, identifier:category; 40, attribute; 40, 41; 40, 42; 41, identifier:sre; 42, identifier:CATEGORY_SPACE; 43, block; 43, 44; 43, 50; 44, expression_statement; 44, 45; 45, augmented_assignment:|=; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:_categories; 49, identifier:UNICODE_SPACE_CATEGORIES; 50, for_statement; 50, 51; 50, 52; 50, 59; 51, identifier:c; 52, parenthesized_expression; 52, 53; 53, conditional_expression:if; 53, 54; 53, 55; 53, 58; 54, identifier:UNICODE_SPACE_CHARS; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:_unicode; 58, identifier:SPACE_CHARS; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:_whitelist_chars; 66, identifier:add; 67, argument_list; 67, 68; 68, identifier:c; 69, elif_clause; 69, 70; 69, 75; 70, comparison_operator:==; 70, 71; 70, 72; 71, identifier:category; 72, attribute; 72, 73; 72, 74; 73, identifier:sre; 74, identifier:CATEGORY_NOT_SPACE; 75, block; 75, 76; 75, 84; 76, expression_statement; 76, 77; 77, augmented_assignment:|=; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:_categories; 81, binary_operator:-; 81, 82; 81, 83; 82, identifier:UNICODE_CATEGORIES; 83, identifier:UNICODE_SPACE_CATEGORIES; 84, for_statement; 84, 85; 84, 86; 84, 93; 85, identifier:c; 86, parenthesized_expression; 86, 87; 87, conditional_expression:if; 87, 88; 87, 89; 87, 92; 88, identifier:UNICODE_SPACE_CHARS; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:_unicode; 92, identifier:SPACE_CHARS; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:_blacklist_chars; 100, identifier:add; 101, argument_list; 101, 102; 102, identifier:c; 103, elif_clause; 103, 104; 103, 109; 104, comparison_operator:==; 104, 105; 104, 106; 105, identifier:category; 106, attribute; 106, 107; 106, 108; 107, identifier:sre; 108, identifier:CATEGORY_WORD; 109, block; 109, 110; 109, 116; 109, 125; 110, expression_statement; 110, 111; 111, augmented_assignment:|=; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:_categories; 115, identifier:UNICODE_WORD_CATEGORIES; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_whitelist_chars; 122, identifier:add; 123, argument_list; 123, 124; 124, string:u'_'; 125, if_statement; 125, 126; 125, 131; 126, boolean_operator:and; 126, 127; 126, 128; 127, identifier:HAS_WEIRD_WORD_CHARS; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:_unicode; 131, block; 131, 132; 132, for_statement; 132, 133; 132, 134; 132, 135; 133, identifier:c; 134, identifier:UNICODE_WEIRD_NONWORD_CHARS; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:_blacklist_chars; 142, identifier:add; 143, argument_list; 143, 144; 144, identifier:c; 145, elif_clause; 145, 146; 145, 151; 146, comparison_operator:==; 146, 147; 146, 148; 147, identifier:category; 148, attribute; 148, 149; 148, 150; 149, identifier:sre; 150, identifier:CATEGORY_NOT_WORD; 151, block; 151, 152; 151, 160; 151, 169; 152, expression_statement; 152, 153; 153, augmented_assignment:|=; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:_categories; 157, binary_operator:-; 157, 158; 157, 159; 158, identifier:UNICODE_CATEGORIES; 159, identifier:UNICODE_WORD_CATEGORIES; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:_blacklist_chars; 166, identifier:add; 167, argument_list; 167, 168; 168, string:u'_'; 169, if_statement; 169, 170; 169, 175; 170, boolean_operator:and; 170, 171; 170, 172; 171, identifier:HAS_WEIRD_WORD_CHARS; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:_unicode; 175, block; 175, 176; 176, for_statement; 176, 177; 176, 178; 176, 179; 177, identifier:c; 178, identifier:UNICODE_WEIRD_NONWORD_CHARS; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:_whitelist_chars; 186, identifier:add; 187, argument_list; 187, 188; 188, identifier:c | def add_category(self, category):
'''
Add unicode category to set
Unicode categories are strings like 'Ll', 'Lu', 'Nd', etc.
See `unicodedata.category()`
'''
if category == sre.CATEGORY_DIGIT:
self._categories |= UNICODE_DIGIT_CATEGORIES
elif category == sre.CATEGORY_NOT_DIGIT:
self._categories |= UNICODE_CATEGORIES - UNICODE_DIGIT_CATEGORIES
elif category == sre.CATEGORY_SPACE:
self._categories |= UNICODE_SPACE_CATEGORIES
for c in (UNICODE_SPACE_CHARS if self._unicode else SPACE_CHARS):
self._whitelist_chars.add(c)
elif category == sre.CATEGORY_NOT_SPACE:
self._categories |= UNICODE_CATEGORIES - UNICODE_SPACE_CATEGORIES
for c in (UNICODE_SPACE_CHARS if self._unicode else SPACE_CHARS):
self._blacklist_chars.add(c)
elif category == sre.CATEGORY_WORD:
self._categories |= UNICODE_WORD_CATEGORIES
self._whitelist_chars.add(u'_')
if HAS_WEIRD_WORD_CHARS and self._unicode:
for c in UNICODE_WEIRD_NONWORD_CHARS:
self._blacklist_chars.add(c)
elif category == sre.CATEGORY_NOT_WORD:
self._categories |= UNICODE_CATEGORIES - UNICODE_WORD_CATEGORIES
self._blacklist_chars.add(u'_')
if HAS_WEIRD_WORD_CHARS and self._unicode:
for c in UNICODE_WEIRD_NONWORD_CHARS:
self._whitelist_chars.add(c) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_estimate_free; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 19; 5, 35; 5, 43; 5, 50; 5, 63; 5, 70; 5, 71; 5, 84; 5, 101; 5, 102; 5, 103; 5, 111; 5, 123; 5, 129; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:capacity_deferred; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:channel; 17, identifier:total_capacity; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:open_tasks_deferred; 22, call; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:channel; 27, identifier:tasks; 28, argument_list; 28, 29; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:state; 31, list:[task_states.OPEN]; 31, 32; 32, attribute; 32, 33; 32, 34; 33, identifier:task_states; 34, identifier:OPEN; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:avg_delta_deferred; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:estimate_duration; 42, argument_list; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:deferreds; 46, list:[capacity_deferred,
open_tasks_deferred,
avg_delta_deferred]; 46, 47; 46, 48; 46, 49; 47, identifier:capacity_deferred; 48, identifier:open_tasks_deferred; 49, identifier:avg_delta_deferred; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:results; 53, yield; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:defer; 57, identifier:gatherResults; 58, argument_list; 58, 59; 58, 60; 59, identifier:deferreds; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:consumeErrors; 62, True; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 69; 65, pattern_list; 65, 66; 65, 67; 65, 68; 66, identifier:capacity; 67, identifier:open_tasks; 68, identifier:avg_delta; 69, identifier:results; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:open_weight; 74, call; 74, 75; 74, 76; 75, identifier:sum; 76, argument_list; 76, 77; 77, list_comprehension; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:task; 80, identifier:weight; 81, for_in_clause; 81, 82; 81, 83; 82, identifier:task; 83, identifier:open_tasks; 84, if_statement; 84, 85; 84, 88; 84, 89; 84, 90; 84, 91; 85, comparison_operator:>=; 85, 86; 85, 87; 86, identifier:open_weight; 87, identifier:capacity; 88, comment; 89, comment; 90, comment; 91, block; 91, 92; 92, raise_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:NotImplementedError; 95, argument_list; 95, 96; 96, binary_operator:%; 96, 97; 96, 98; 97, string:'channel %d is at capacity'; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:channel_id; 101, comment; 102, comment; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:start_time; 106, binary_operator:+; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:created; 110, identifier:SLEEPTIME; 111, if_statement; 111, 112; 111, 115; 112, comparison_operator:is; 112, 113; 112, 114; 113, identifier:avg_delta; 114, None; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:defer; 120, identifier:returnValue; 121, argument_list; 121, 122; 122, None; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:est_completion; 126, binary_operator:+; 126, 127; 126, 128; 127, identifier:start_time; 128, identifier:avg_delta; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:defer; 133, identifier:returnValue; 134, argument_list; 134, 135; 135, identifier:est_completion | def _estimate_free(self):
"""
Estimate completion time for a free task.
:returns: deferred that when fired returns a datetime object for the
estimated, or the actual datetime, or None if we could not
estimate a time for this task method.
"""
# Query the information we need for this task's channel and package.
capacity_deferred = self.channel.total_capacity()
open_tasks_deferred = self.channel.tasks(state=[task_states.OPEN])
avg_delta_deferred = self.estimate_duration()
deferreds = [capacity_deferred,
open_tasks_deferred,
avg_delta_deferred]
results = yield defer.gatherResults(deferreds, consumeErrors=True)
capacity, open_tasks, avg_delta = results
# Ensure this task's channel has spare capacity for this task.
open_weight = sum([task.weight for task in open_tasks])
if open_weight >= capacity:
# TODO: Evaluate all tasks in the channel and
# determine when enough OPEN tasks will complete so that we can
# get to OPEN.
raise NotImplementedError('channel %d is at capacity' %
self.channel_id)
# A builder will pick up this task and start it within SLEEPTIME.
# start_time is the maximum amount of time we expect to wait here.
start_time = self.created + SLEEPTIME
if avg_delta is None:
defer.returnValue(None)
est_completion = start_time + avg_delta
defer.returnValue(est_completion) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:package; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 23; 5, 39; 5, 54; 5, 55; 5, 70; 5, 71; 5, 79; 5, 86; 5, 87; 5, 172; 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:method; 13, string:'buildNotification'; 14, block; 14, 15; 15, return_statement; 15, 16; 16, subscript; 16, 17; 16, 22; 17, subscript; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:params; 21, integer:1; 22, string:'name'; 23, if_statement; 23, 24; 23, 32; 24, comparison_operator:in; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:method; 28, tuple; 28, 29; 28, 30; 28, 31; 29, string:'createImage'; 30, string:'image'; 31, string:'livecd'; 32, block; 32, 33; 33, return_statement; 33, 34; 34, subscript; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:params; 38, integer:0; 39, if_statement; 39, 40; 39, 45; 40, comparison_operator:==; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:method; 44, string:'indirectionimage'; 45, block; 45, 46; 46, return_statement; 46, 47; 47, subscript; 47, 48; 47, 53; 48, subscript; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:params; 52, integer:0; 53, string:'name'; 54, comment; 55, if_statement; 55, 56; 55, 67; 56, comparison_operator:not; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:method; 60, tuple; 60, 61; 60, 62; 60, 63; 60, 64; 60, 65; 60, 66; 61, string:'build'; 62, string:'buildArch'; 63, string:'buildContainer'; 64, string:'buildMaven'; 65, string:'buildSRPMFromSCM'; 66, string:'maven'; 67, block; 67, 68; 68, return_statement; 68, 69; 69, None; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:source; 74, subscript; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:params; 78, integer:0; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:o; 82, call; 82, 83; 82, 84; 83, identifier:urlparse; 84, argument_list; 84, 85; 85, identifier:source; 86, comment; 87, if_statement; 87, 88; 87, 94; 87, 123; 87, 124; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:source; 91, identifier:endswith; 92, argument_list; 92, 93; 93, string:'.src.rpm'; 94, block; 94, 95; 94, 106; 94, 119; 94, 120; 94, 121; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:srpm; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:os; 102, identifier:path; 103, identifier:basename; 104, argument_list; 104, 105; 105, identifier:source; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 112; 108, tuple_pattern; 108, 109; 108, 110; 108, 111; 109, identifier:name; 110, identifier:version; 111, identifier:release; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:srpm; 115, identifier:rsplit; 116, argument_list; 116, 117; 116, 118; 117, string:'-'; 118, integer:2; 119, comment; 120, comment; 121, return_statement; 121, 122; 122, identifier:name; 123, comment; 124, elif_clause; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:o; 127, identifier:scheme; 128, block; 128, 129; 128, 142; 128, 159; 128, 170; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:package; 132, call; 132, 133; 132, 138; 133, attribute; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:os; 136, identifier:path; 137, identifier:basename; 138, argument_list; 138, 139; 139, attribute; 139, 140; 139, 141; 140, identifier:o; 141, identifier:path; 142, if_statement; 142, 143; 142, 149; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:package; 146, identifier:endswith; 147, argument_list; 147, 148; 148, string:'.git'; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:package; 153, subscript; 153, 154; 153, 155; 154, identifier:package; 155, slice; 155, 156; 155, 157; 156, colon; 157, unary_operator:-; 157, 158; 158, integer:4; 159, if_statement; 159, 160; 159, 165; 160, comparison_operator:==; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:method; 164, string:'buildContainer'; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, augmented_assignment:+=; 167, 168; 167, 169; 168, identifier:package; 169, string:'-container'; 170, return_statement; 170, 171; 171, identifier:package; 172, raise_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:ValueError; 175, argument_list; 175, 176; 176, binary_operator:%; 176, 177; 176, 178; 177, string:'could not parse source "%s"'; 178, identifier:source | def package(self):
"""
Find a package name from a build task's parameters.
:returns: name of the package this build task is building.
:raises: ValueError if we could not parse this tasks's request params.
"""
if self.method == 'buildNotification':
return self.params[1]['name']
if self.method in ('createImage', 'image', 'livecd'):
return self.params[0]
if self.method == 'indirectionimage':
return self.params[0]['name']
# params[0] is the source URL for these tasks:
if self.method not in ('build', 'buildArch', 'buildContainer',
'buildMaven', 'buildSRPMFromSCM', 'maven'):
return None
# (I wish there was a better way to do this.)
source = self.params[0]
o = urlparse(source)
# build tasks can load an SRPM from a "cli-build" tmpdir:
if source.endswith('.src.rpm'):
srpm = os.path.basename(source)
(name, version, release) = srpm.rsplit('-', 2)
# Note we're throwing away version and release here. They could be
# useful eventually, maybe in a "Package" class.
return name
# or an allowed SCM:
elif o.scheme:
package = os.path.basename(o.path)
if package.endswith('.git'):
package = package[:-4]
if self.method == 'buildContainer':
package += '-container'
return package
raise ValueError('could not parse source "%s"' % source) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:is_admin; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:send; 6, identifier:nick; 7, default_parameter; 7, 8; 7, 9; 8, identifier:required_role; 9, string:'admin'; 10, block; 10, 11; 10, 13; 10, 14; 10, 20; 10, 21; 11, expression_statement; 11, 12; 12, comment; 13, comment; 14, if_statement; 14, 15; 14, 17; 15, not_operator; 15, 16; 16, identifier:required_role; 17, block; 17, 18; 18, return_statement; 18, 19; 19, True; 20, comment; 21, with_statement; 21, 22; 21, 34; 22, with_clause; 22, 23; 23, with_item; 23, 24; 24, as_pattern; 24, 25; 24, 32; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:db; 30, identifier:session_scope; 31, argument_list; 32, as_pattern_target; 32, 33; 33, identifier:session; 34, block; 34, 35; 34, 61; 34, 68; 34, 69; 34, 82; 34, 83; 34, 98; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:admin; 38, call; 38, 39; 38, 60; 39, attribute; 39, 40; 39, 59; 40, call; 40, 41; 40, 51; 41, attribute; 41, 42; 41, 50; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:session; 45, identifier:query; 46, argument_list; 46, 47; 47, attribute; 47, 48; 47, 49; 48, identifier:orm; 49, identifier:Permissions; 50, identifier:filter; 51, argument_list; 51, 52; 52, comparison_operator:==; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:orm; 56, identifier:Permissions; 57, identifier:nick; 58, identifier:nick; 59, identifier:first; 60, argument_list; 61, if_statement; 61, 62; 61, 65; 62, comparison_operator:is; 62, 63; 62, 64; 63, identifier:admin; 64, None; 65, block; 65, 66; 66, return_statement; 66, 67; 67, False; 68, comment; 69, if_statement; 69, 70; 69, 79; 70, boolean_operator:and; 70, 71; 70, 74; 71, comparison_operator:==; 71, 72; 71, 73; 72, identifier:required_role; 73, string:"owner"; 74, comparison_operator:!=; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:admin; 77, identifier:role; 78, string:"owner"; 79, block; 79, 80; 80, return_statement; 80, 81; 81, False; 82, comment; 83, if_statement; 83, 84; 83, 95; 84, not_operator; 84, 85; 85, call; 85, 86; 85, 93; 86, attribute; 86, 87; 86, 92; 87, subscript; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:config; 91, string:'feature'; 92, identifier:getboolean; 93, argument_list; 93, 94; 94, string:'nickserv'; 95, block; 95, 96; 96, return_statement; 96, 97; 97, True; 98, if_statement; 98, 99; 98, 103; 98, 135; 99, not_operator; 99, 100; 100, attribute; 100, 101; 100, 102; 101, identifier:admin; 102, identifier:registered; 103, block; 103, 104; 103, 111; 103, 112; 103, 133; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:update_authstatus; 109, argument_list; 109, 110; 110, identifier:nick; 111, comment; 112, if_statement; 112, 113; 112, 116; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:send; 115, None; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:send; 120, argument_list; 120, 121; 120, 124; 121, binary_operator:%; 121, 122; 121, 123; 122, string:"Unverified admin: %s"; 123, identifier:nick; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:target; 126, subscript; 126, 127; 126, 132; 127, subscript; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:config; 131, string:'core'; 132, string:'channel'; 133, return_statement; 133, 134; 134, False; 135, else_clause; 135, 136; 136, block; 136, 137; 136, 171; 137, if_statement; 137, 138; 137, 144; 137, 145; 138, not_operator; 138, 139; 139, subscript; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:features; 143, string:'account-notify'; 144, comment; 145, block; 145, 146; 146, if_statement; 146, 147; 146, 163; 147, comparison_operator:>; 147, 148; 147, 157; 148, binary_operator:-; 148, 149; 148, 154; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:datetime; 152, identifier:now; 153, argument_list; 154, attribute; 154, 155; 154, 156; 155, identifier:admin; 156, identifier:time; 157, call; 157, 158; 157, 159; 158, identifier:timedelta; 159, argument_list; 159, 160; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:minutes; 162, integer:5; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:update_authstatus; 169, argument_list; 169, 170; 170, identifier:nick; 171, return_statement; 171, 172; 172, True | def is_admin(self, send, nick, required_role='admin'):
"""Checks if a nick is a admin.
If NickServ hasn't responded yet, then the admin is unverified,
so assume they aren't a admin.
"""
# If the required role is None, bypass checks.
if not required_role:
return True
# Current roles are admin and owner, which is a superset of admin.
with self.db.session_scope() as session:
admin = session.query(orm.Permissions).filter(orm.Permissions.nick == nick).first()
if admin is None:
return False
# owner implies admin, but not the other way around.
if required_role == "owner" and admin.role != "owner":
return False
# no nickserv support, assume people are who they say they are.
if not self.config['feature'].getboolean('nickserv'):
return True
if not admin.registered:
self.update_authstatus(nick)
# We don't necessarily want to complain in all cases.
if send is not None:
send("Unverified admin: %s" % nick, target=self.config['core']['channel'])
return False
else:
if not self.features['account-notify']:
# reverify every 5min if we don't have the notification feature.
if datetime.now() - admin.time > timedelta(minutes=5):
self.update_authstatus(nick)
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:do_mode; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:target; 6, identifier:msg; 7, identifier:nick; 8, identifier:send; 9, block; 9, 10; 9, 12; 9, 23; 9, 86; 9, 87; 9, 88; 9, 143; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:mode_changes; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:irc; 19, identifier:modes; 20, identifier:parse_channel_modes; 21, argument_list; 21, 22; 22, identifier:msg; 23, with_statement; 23, 24; 23, 29; 24, with_clause; 24, 25; 25, with_item; 25, 26; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:data_lock; 29, block; 29, 30; 30, for_statement; 30, 31; 30, 32; 30, 33; 31, identifier:change; 32, identifier:mode_changes; 33, block; 33, 34; 33, 60; 34, if_statement; 34, 35; 34, 40; 35, comparison_operator:==; 35, 36; 35, 39; 36, subscript; 36, 37; 36, 38; 37, identifier:change; 38, integer:1; 39, string:'v'; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 52; 43, subscript; 43, 44; 43, 49; 44, subscript; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:voiced; 48, identifier:target; 49, subscript; 49, 50; 49, 51; 50, identifier:change; 51, integer:2; 52, conditional_expression:if; 52, 53; 52, 54; 52, 59; 53, True; 54, comparison_operator:==; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:change; 57, integer:0; 58, string:'+'; 59, False; 60, if_statement; 60, 61; 60, 66; 61, comparison_operator:==; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:change; 64, integer:1; 65, string:'o'; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 78; 69, subscript; 69, 70; 69, 75; 70, subscript; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:opers; 74, identifier:target; 75, subscript; 75, 76; 75, 77; 76, identifier:change; 77, integer:2; 78, conditional_expression:if; 78, 79; 78, 80; 78, 85; 79, True; 80, comparison_operator:==; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:change; 83, integer:0; 84, string:'+'; 85, False; 86, comment; 87, comment; 88, if_statement; 88, 89; 88, 101; 89, list_comprehension; 89, 90; 89, 91; 89, 94; 90, identifier:x; 91, for_in_clause; 91, 92; 91, 93; 92, identifier:x; 93, identifier:mode_changes; 94, if_clause; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:check_mode; 99, argument_list; 99, 100; 100, identifier:x; 101, block; 101, 102; 101, 112; 101, 113; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:send; 105, argument_list; 105, 106; 105, 109; 106, binary_operator:%; 106, 107; 106, 108; 107, string:"%s: :("; 108, identifier:nick; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:target; 111, identifier:target; 112, comment; 113, if_statement; 113, 114; 113, 122; 114, not_operator; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:is_admin; 119, argument_list; 119, 120; 119, 121; 120, None; 121, identifier:nick; 122, block; 122, 123; 122, 133; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:send; 126, argument_list; 126, 127; 126, 130; 127, binary_operator:%; 127, 128; 127, 129; 128, string:"OP %s"; 129, identifier:target; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:target; 132, string:'ChanServ'; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:send; 136, argument_list; 136, 137; 136, 140; 137, binary_operator:%; 137, 138; 137, 139; 138, string:"UNBAN %s"; 139, identifier:target; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:target; 142, string:'ChanServ'; 143, if_statement; 143, 144; 143, 152; 143, 153; 144, comparison_operator:>; 144, 145; 144, 151; 145, call; 145, 146; 145, 147; 146, identifier:len; 147, argument_list; 147, 148; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:guarded; 151, integer:0; 152, comment; 153, block; 153, 154; 153, 167; 153, 177; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:regex; 157, binary_operator:%; 157, 158; 157, 159; 158, string:r"(.*(-v|-o|\+q|\+b)[^ ]*) (%s)"; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, string:"|"; 162, identifier:join; 163, argument_list; 163, 164; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:guarded; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:match; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:re; 173, identifier:search; 174, argument_list; 174, 175; 174, 176; 175, identifier:regex; 176, identifier:msg; 177, if_statement; 177, 178; 177, 194; 178, boolean_operator:and; 178, 179; 178, 180; 179, identifier:match; 180, comparison_operator:not; 180, 181; 180, 182; 181, identifier:nick; 182, list:[match.group(3), self.connection.real_nickname]; 182, 183; 182, 189; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:match; 186, identifier:group; 187, argument_list; 187, 188; 188, integer:3; 189, attribute; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:connection; 193, identifier:real_nickname; 194, block; 194, 195; 194, 215; 194, 225; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:modestring; 198, binary_operator:%; 198, 199; 198, 200; 199, string:"+voe-qb %s"; 200, parenthesized_expression; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, string:" "; 204, identifier:join; 205, argument_list; 205, 206; 206, binary_operator:*; 206, 207; 206, 214; 207, list:[match.group(3)]; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:match; 211, identifier:group; 212, argument_list; 212, 213; 213, integer:3; 214, integer:5; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 222; 217, attribute; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:connection; 221, identifier:mode; 222, argument_list; 222, 223; 222, 224; 223, identifier:target; 224, identifier:modestring; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 228; 227, identifier:send; 228, argument_list; 228, 229; 228, 234; 229, binary_operator:%; 229, 230; 229, 231; 230, string:'Mode %s on %s by the guard system'; 231, tuple; 231, 232; 231, 233; 232, identifier:modestring; 233, identifier:target; 234, keyword_argument; 234, 235; 234, 236; 235, identifier:target; 236, subscript; 236, 237; 236, 242; 237, subscript; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:config; 241, string:'core'; 242, string:'ctrlchan' | def do_mode(self, target, msg, nick, send):
"""reop and handle guard violations."""
mode_changes = irc.modes.parse_channel_modes(msg)
with self.data_lock:
for change in mode_changes:
if change[1] == 'v':
self.voiced[target][change[2]] = True if change[0] == '+' else False
if change[1] == 'o':
self.opers[target][change[2]] = True if change[0] == '+' else False
# reop
# FIXME: handle -o+o msbobBot msbobBot
if [x for x in mode_changes if self.check_mode(x)]:
send("%s: :(" % nick, target=target)
# Assume bot admins know what they're doing.
if not self.is_admin(None, nick):
send("OP %s" % target, target='ChanServ')
send("UNBAN %s" % target, target='ChanServ')
if len(self.guarded) > 0:
# if user is guarded and quieted, devoiced, or deopped, fix that
regex = r"(.*(-v|-o|\+q|\+b)[^ ]*) (%s)" % "|".join(self.guarded)
match = re.search(regex, msg)
if match and nick not in [match.group(3), self.connection.real_nickname]:
modestring = "+voe-qb %s" % (" ".join([match.group(3)] * 5))
self.connection.mode(target, modestring)
send('Mode %s on %s by the guard system' % (modestring, target), target=self.config['core']['ctrlchan']) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:do_kick; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:self; 5, identifier:send; 6, identifier:target; 7, identifier:nick; 8, identifier:msg; 9, default_parameter; 9, 10; 9, 11; 10, identifier:slogan; 11, True; 12, block; 12, 13; 12, 15; 12, 22; 12, 37; 12, 64; 12, 74; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 20; 16, not_operator; 16, 17; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:kick_enabled; 20, block; 20, 21; 21, return_statement; 22, if_statement; 22, 23; 22, 28; 23, comparison_operator:not; 23, 24; 23, 25; 24, identifier:target; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:channels; 28, block; 28, 29; 28, 36; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:send; 32, argument_list; 32, 33; 33, binary_operator:%; 33, 34; 33, 35; 34, string:"%s: you're lucky, private message kicking hasn't been implemented yet."; 35, identifier:nick; 36, return_statement; 37, with_statement; 37, 38; 37, 43; 38, with_clause; 38, 39; 39, with_item; 39, 40; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:data_lock; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:ops; 47, list_comprehension; 47, 48; 47, 49; 47, 62; 48, identifier:k; 49, for_in_clause; 49, 50; 49, 53; 50, pattern_list; 50, 51; 50, 52; 51, identifier:k; 52, identifier:v; 53, call; 53, 54; 53, 61; 54, attribute; 54, 55; 54, 60; 55, subscript; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:opers; 59, identifier:target; 60, identifier:items; 61, argument_list; 62, if_clause; 62, 63; 63, identifier:v; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:botnick; 67, subscript; 67, 68; 67, 73; 68, subscript; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:config; 72, string:'core'; 73, string:'nick'; 74, if_statement; 74, 75; 74, 78; 74, 108; 74, 149; 75, comparison_operator:not; 75, 76; 75, 77; 76, identifier:botnick; 77, identifier:ops; 78, block; 78, 79; 78, 88; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:ops; 82, conditional_expression:if; 82, 83; 82, 85; 82, 87; 83, list:['someone']; 83, 84; 84, string:'someone'; 85, not_operator; 85, 86; 86, identifier:ops; 87, identifier:ops; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:send; 91, argument_list; 91, 92; 91, 105; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:textutils; 95, identifier:gen_creffett; 96, argument_list; 96, 97; 97, binary_operator:%; 97, 98; 97, 99; 98, string:"%s: /op the bot"; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:random; 102, identifier:choice; 103, argument_list; 103, 104; 104, identifier:ops; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:target; 107, identifier:target; 108, elif_clause; 108, 109; 108, 120; 109, boolean_operator:and; 109, 110; 109, 117; 110, comparison_operator:<; 110, 111; 110, 116; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:random; 114, identifier:random; 115, argument_list; 116, float:0.01; 117, comparison_operator:==; 117, 118; 117, 119; 118, identifier:msg; 119, string:"shutting caps lock off"; 120, block; 120, 121; 121, if_statement; 121, 122; 121, 125; 121, 136; 122, comparison_operator:in; 122, 123; 122, 124; 123, identifier:nick; 124, identifier:ops; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:send; 129, argument_list; 129, 130; 129, 133; 130, binary_operator:%; 130, 131; 130, 132; 131, string:"%s: HUEHUEHUE GIBE CAPSLOCK PLS I REPORT U"; 132, identifier:nick; 133, keyword_argument; 133, 134; 133, 135; 134, identifier:target; 135, identifier:target; 136, else_clause; 136, 137; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:connection; 144, identifier:kick; 145, argument_list; 145, 146; 145, 147; 145, 148; 146, identifier:target; 147, identifier:nick; 148, string:"HUEHUEHUE GIBE CAPSLOCK PLS I REPORT U"; 149, else_clause; 149, 150; 150, block; 150, 151; 150, 167; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:msg; 154, conditional_expression:if; 154, 155; 154, 165; 154, 166; 155, call; 155, 156; 155, 164; 156, attribute; 156, 157; 156, 163; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:textutils; 160, identifier:gen_slogan; 161, argument_list; 161, 162; 162, identifier:msg; 163, identifier:upper; 164, argument_list; 165, identifier:slogan; 166, identifier:msg; 167, if_statement; 167, 168; 167, 171; 167, 184; 168, comparison_operator:in; 168, 169; 168, 170; 169, identifier:nick; 170, identifier:ops; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:send; 175, argument_list; 175, 176; 175, 181; 176, binary_operator:%; 176, 177; 176, 178; 177, string:"%s: %s"; 178, tuple; 178, 179; 178, 180; 179, identifier:nick; 180, identifier:msg; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:target; 183, identifier:target; 184, else_clause; 184, 185; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 193; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:connection; 192, identifier:kick; 193, argument_list; 193, 194; 193, 195; 193, 196; 194, identifier:target; 195, identifier:nick; 196, identifier:msg | def do_kick(self, send, target, nick, msg, slogan=True):
"""Kick users.
- If kick is disabled, don't do anything.
- If the bot is not a op, rage at a op.
- Kick the user.
"""
if not self.kick_enabled:
return
if target not in self.channels:
send("%s: you're lucky, private message kicking hasn't been implemented yet." % nick)
return
with self.data_lock:
ops = [k for k, v in self.opers[target].items() if v]
botnick = self.config['core']['nick']
if botnick not in ops:
ops = ['someone'] if not ops else ops
send(textutils.gen_creffett("%s: /op the bot" % random.choice(ops)), target=target)
elif random.random() < 0.01 and msg == "shutting caps lock off":
if nick in ops:
send("%s: HUEHUEHUE GIBE CAPSLOCK PLS I REPORT U" % nick, target=target)
else:
self.connection.kick(target, nick, "HUEHUEHUE GIBE CAPSLOCK PLS I REPORT U")
else:
msg = textutils.gen_slogan(msg).upper() if slogan else msg
if nick in ops:
send("%s: %s" % (nick, msg), target=target)
else:
self.connection.kick(target, nick, msg) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:handle_msg; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:c; 6, identifier:e; 7, block; 7, 8; 7, 10; 7, 38; 7, 66; 7, 67; 7, 80; 7, 109; 7, 140; 7, 141; 7, 152; 7, 164; 7, 182; 7, 200; 7, 231; 7, 249; 7, 280; 7, 298; 7, 309; 7, 310; 7, 318; 7, 334; 7, 345; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 21; 10, 30; 11, comparison_operator:not; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:e; 14, identifier:type; 15, list:['authenticate', 'error', 'join', 'part', 'quit']; 15, 16; 15, 17; 15, 18; 15, 19; 15, 20; 16, string:'authenticate'; 17, string:'error'; 18, string:'join'; 19, string:'part'; 20, string:'quit'; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:nick; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:e; 28, identifier:source; 29, identifier:nick; 30, else_clause; 30, 31; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:nick; 35, attribute; 35, 36; 35, 37; 36, identifier:e; 37, identifier:source; 38, if_statement; 38, 39; 38, 44; 38, 49; 39, comparison_operator:is; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:e; 42, identifier:arguments; 43, None; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:msg; 48, string:""; 49, else_clause; 49, 50; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:msg; 54, call; 54, 55; 54, 65; 55, attribute; 55, 56; 55, 64; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, string:" "; 59, identifier:join; 60, argument_list; 60, 61; 61, attribute; 61, 62; 61, 63; 62, identifier:e; 63, identifier:arguments; 64, identifier:strip; 65, argument_list; 66, comment; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:target; 70, conditional_expression:if; 70, 71; 70, 72; 70, 77; 71, identifier:nick; 72, comparison_operator:==; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:e; 75, identifier:type; 76, string:'privmsg'; 77, attribute; 77, 78; 77, 79; 78, identifier:e; 79, identifier:target; 80, function_definition; 80, 81; 80, 82; 80, 93; 81, function_name:send; 82, parameters; 82, 83; 82, 84; 82, 87; 82, 90; 83, identifier:msg; 84, default_parameter; 84, 85; 84, 86; 85, identifier:mtype; 86, string:'privmsg'; 87, default_parameter; 87, 88; 87, 89; 88, identifier:target; 89, identifier:target; 90, default_parameter; 90, 91; 90, 92; 91, identifier:ignore_length; 92, False; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:send; 99, argument_list; 99, 100; 99, 101; 99, 106; 99, 107; 99, 108; 100, identifier:target; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:connection; 105, identifier:real_nickname; 106, identifier:msg; 107, identifier:mtype; 108, identifier:ignore_length; 109, if_statement; 109, 110; 109, 128; 110, comparison_operator:in; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:e; 113, identifier:type; 114, list:[
'account', 'authenticate', 'bannedfromchan', 'cap', 'ctcpreply', 'error', 'featurelist', 'nosuchnick', 'nick', 'nicknameinuse',
'privnotice', 'welcome', 'whospcrpl'
]; 114, 115; 114, 116; 114, 117; 114, 118; 114, 119; 114, 120; 114, 121; 114, 122; 114, 123; 114, 124; 114, 125; 114, 126; 114, 127; 115, string:'account'; 116, string:'authenticate'; 117, string:'bannedfromchan'; 118, string:'cap'; 119, string:'ctcpreply'; 120, string:'error'; 121, string:'featurelist'; 122, string:'nosuchnick'; 123, string:'nick'; 124, string:'nicknameinuse'; 125, string:'privnotice'; 126, string:'welcome'; 127, string:'whospcrpl'; 128, block; 128, 129; 128, 139; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:handle_event; 134, argument_list; 134, 135; 134, 136; 134, 137; 134, 138; 135, identifier:msg; 136, identifier:send; 137, identifier:c; 138, identifier:e; 139, return_statement; 140, comment; 141, if_statement; 141, 142; 141, 150; 142, boolean_operator:and; 142, 143; 142, 145; 143, not_operator; 143, 144; 144, identifier:msg; 145, comparison_operator:!=; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:e; 148, identifier:type; 149, string:'join'; 150, block; 150, 151; 151, return_statement; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:do_log; 157, argument_list; 157, 158; 157, 159; 157, 160; 157, 161; 158, identifier:target; 159, identifier:nick; 160, identifier:msg; 161, attribute; 161, 162; 161, 163; 162, identifier:e; 163, identifier:type; 164, if_statement; 164, 165; 164, 170; 165, comparison_operator:==; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:e; 168, identifier:type; 169, string:'mode'; 170, block; 170, 171; 170, 181; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:do_mode; 176, argument_list; 176, 177; 176, 178; 176, 179; 176, 180; 177, identifier:target; 178, identifier:msg; 179, identifier:nick; 180, identifier:send; 181, return_statement; 182, if_statement; 182, 183; 182, 188; 183, comparison_operator:==; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:e; 186, identifier:type; 187, string:'join'; 188, block; 188, 189; 188, 199; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:handle_join; 194, argument_list; 194, 195; 194, 196; 194, 197; 194, 198; 195, identifier:c; 196, identifier:e; 197, identifier:target; 198, identifier:send; 199, return_statement; 200, if_statement; 200, 201; 200, 206; 201, comparison_operator:==; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:e; 204, identifier:type; 205, string:'part'; 206, block; 206, 207; 206, 230; 207, if_statement; 207, 208; 207, 213; 208, comparison_operator:==; 208, 209; 208, 210; 209, identifier:nick; 210, attribute; 210, 211; 210, 212; 211, identifier:c; 212, identifier:real_nickname; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:send; 217, argument_list; 217, 218; 217, 221; 218, binary_operator:%; 218, 219; 218, 220; 219, string:"Parted channel %s"; 220, identifier:target; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:target; 223, subscript; 223, 224; 223, 229; 224, subscript; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:self; 227, identifier:config; 228, string:'core'; 229, string:'ctrlchan'; 230, return_statement; 231, if_statement; 231, 232; 231, 237; 232, comparison_operator:==; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:e; 235, identifier:type; 236, string:'kick'; 237, block; 237, 238; 237, 248; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:self; 242, identifier:handle_kick; 243, argument_list; 243, 244; 243, 245; 243, 246; 243, 247; 244, identifier:c; 245, identifier:e; 246, identifier:target; 247, identifier:send; 248, return_statement; 249, if_statement; 249, 250; 249, 269; 250, boolean_operator:and; 250, 251; 250, 262; 251, comparison_operator:==; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:e; 254, identifier:target; 255, subscript; 255, 256; 255, 261; 256, subscript; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:self; 259, identifier:config; 260, string:'core'; 261, string:'ctrlchan'; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:self; 265, identifier:is_admin; 266, argument_list; 266, 267; 266, 268; 267, None; 268, identifier:nick; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:control; 274, identifier:handle_ctrlchan; 275, argument_list; 275, 276; 275, 277; 275, 278; 275, 279; 276, identifier:self; 277, identifier:msg; 278, identifier:nick; 279, identifier:send; 280, if_statement; 280, 281; 280, 296; 281, boolean_operator:and; 281, 282; 281, 288; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:self; 285, identifier:is_ignored; 286, argument_list; 286, 287; 287, identifier:nick; 288, not_operator; 288, 289; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:is_admin; 293, argument_list; 293, 294; 293, 295; 294, None; 295, identifier:nick; 296, block; 296, 297; 297, return_statement; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:self; 302, identifier:handle_hooks; 303, argument_list; 303, 304; 303, 305; 303, 306; 303, 307; 303, 308; 304, identifier:send; 305, identifier:nick; 306, identifier:target; 307, identifier:e; 308, identifier:msg; 309, comment; 310, if_statement; 310, 311; 310, 316; 311, comparison_operator:==; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:e; 314, identifier:type; 315, string:'pubnotice'; 316, block; 316, 317; 317, return_statement; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:msg; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:misc; 324, identifier:get_cmdchar; 325, argument_list; 325, 326; 325, 329; 325, 330; 325, 331; 326, attribute; 326, 327; 326, 328; 327, identifier:self; 328, identifier:config; 329, identifier:c; 330, identifier:msg; 331, attribute; 331, 332; 331, 333; 332, identifier:e; 333, identifier:type; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 339; 336, pattern_list; 336, 337; 336, 338; 337, identifier:cmd_name; 338, identifier:cmdargs; 339, call; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:self; 342, identifier:get_cmd; 343, argument_list; 343, 344; 344, identifier:msg; 345, if_statement; 345, 346; 345, 354; 345, 367; 345, 368; 346, call; 346, 347; 346, 352; 347, attribute; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:registry; 350, identifier:command_registry; 351, identifier:is_registered; 352, argument_list; 352, 353; 353, identifier:cmd_name; 354, block; 354, 355; 355, expression_statement; 355, 356; 356, call; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:self; 359, identifier:run_cmd; 360, argument_list; 360, 361; 360, 362; 360, 363; 360, 364; 360, 365; 360, 366; 361, identifier:send; 362, identifier:nick; 363, identifier:target; 364, identifier:cmd_name; 365, identifier:cmdargs; 366, identifier:e; 367, comment; 368, elif_clause; 368, 369; 368, 372; 369, comparison_operator:==; 369, 370; 369, 371; 370, identifier:cmd_name; 371, string:'reload'; 372, block; 372, 373; 373, with_statement; 373, 374; 373, 386; 374, with_clause; 374, 375; 375, with_item; 375, 376; 376, as_pattern; 376, 377; 376, 384; 377, call; 377, 378; 377, 383; 378, attribute; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:self; 381, identifier:db; 382, identifier:session_scope; 383, argument_list; 384, as_pattern_target; 384, 385; 385, identifier:session; 386, block; 386, 387; 387, if_statement; 387, 388; 387, 411; 388, call; 388, 389; 388, 410; 389, attribute; 389, 390; 389, 409; 390, call; 390, 391; 390, 401; 391, attribute; 391, 392; 391, 400; 392, call; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:session; 395, identifier:query; 396, argument_list; 396, 397; 397, attribute; 397, 398; 397, 399; 398, identifier:orm; 399, identifier:Permissions; 400, identifier:filter; 401, argument_list; 401, 402; 402, comparison_operator:==; 402, 403; 402, 408; 403, attribute; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:orm; 406, identifier:Permissions; 407, identifier:nick; 408, identifier:nick; 409, identifier:count; 410, argument_list; 411, block; 411, 412; 412, expression_statement; 412, 413; 413, call; 413, 414; 413, 415; 414, identifier:send; 415, argument_list; 415, 416; 416, string:"Aye Aye Capt'n" | def handle_msg(self, c, e):
"""The Heart and Soul of IrcBot."""
if e.type not in ['authenticate', 'error', 'join', 'part', 'quit']:
nick = e.source.nick
else:
nick = e.source
if e.arguments is None:
msg = ""
else:
msg = " ".join(e.arguments).strip()
# Send the response to private messages to the sending nick.
target = nick if e.type == 'privmsg' else e.target
def send(msg, mtype='privmsg', target=target, ignore_length=False):
self.send(target, self.connection.real_nickname, msg, mtype, ignore_length)
if e.type in [
'account', 'authenticate', 'bannedfromchan', 'cap', 'ctcpreply', 'error', 'featurelist', 'nosuchnick', 'nick', 'nicknameinuse',
'privnotice', 'welcome', 'whospcrpl'
]:
self.handle_event(msg, send, c, e)
return
# ignore empty messages
if not msg and e.type != 'join':
return
self.do_log(target, nick, msg, e.type)
if e.type == 'mode':
self.do_mode(target, msg, nick, send)
return
if e.type == 'join':
self.handle_join(c, e, target, send)
return
if e.type == 'part':
if nick == c.real_nickname:
send("Parted channel %s" % target, target=self.config['core']['ctrlchan'])
return
if e.type == 'kick':
self.handle_kick(c, e, target, send)
return
if e.target == self.config['core']['ctrlchan'] and self.is_admin(None, nick):
control.handle_ctrlchan(self, msg, nick, send)
if self.is_ignored(nick) and not self.is_admin(None, nick):
return
self.handle_hooks(send, nick, target, e, msg)
# We only process hooks for notices, not commands.
if e.type == 'pubnotice':
return
msg = misc.get_cmdchar(self.config, c, msg, e.type)
cmd_name, cmdargs = self.get_cmd(msg)
if registry.command_registry.is_registered(cmd_name):
self.run_cmd(send, nick, target, cmd_name, cmdargs, e)
# special commands
elif cmd_name == 'reload':
with self.db.session_scope() as session:
if session.query(orm.Permissions).filter(orm.Permissions.nick == nick).count():
send("Aye Aye Capt'n") |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:cmd; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:send; 5, identifier:msg; 6, identifier:args; 7, block; 7, 8; 7, 10; 7, 16; 7, 35; 7, 69; 7, 79; 7, 89; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:nick; 13, subscript; 13, 14; 13, 15; 14, identifier:args; 15, string:'nick'; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:channel; 19, conditional_expression:if; 19, 20; 19, 23; 19, 28; 20, subscript; 20, 21; 20, 22; 21, identifier:args; 22, string:'target'; 23, comparison_operator:!=; 23, 24; 23, 27; 24, subscript; 24, 25; 24, 26; 25, identifier:args; 26, string:'target'; 27, string:'private'; 28, subscript; 28, 29; 28, 34; 29, subscript; 29, 30; 29, 33; 30, subscript; 30, 31; 30, 32; 31, identifier:args; 32, string:'config'; 33, string:'core'; 34, string:'channel'; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:levels; 38, dictionary; 38, 39; 38, 42; 38, 45; 38, 48; 38, 51; 38, 54; 38, 57; 38, 60; 38, 63; 38, 66; 39, pair; 39, 40; 39, 41; 40, integer:1; 41, string:'Whirr...'; 42, pair; 42, 43; 42, 44; 43, integer:2; 44, string:'Vrrm...'; 45, pair; 45, 46; 45, 47; 46, integer:3; 47, string:'Zzzzhhhh...'; 48, pair; 48, 49; 48, 50; 49, integer:4; 50, string:'SHFRRRRM...'; 51, pair; 51, 52; 51, 53; 52, integer:5; 53, string:'GEEEEZZSH...'; 54, pair; 54, 55; 54, 56; 55, integer:6; 56, string:'PLAAAAIIID...'; 57, pair; 57, 58; 57, 59; 58, integer:7; 59, string:'KKKRRRAAKKKAAKRAKKGGARGHGIZZZZ...'; 60, pair; 60, 61; 60, 62; 61, integer:8; 62, string:'Nuke'; 63, pair; 63, 64; 63, 65; 64, integer:9; 65, string:'nneeeaaaooowwwwww..... BOOOOOSH BLAM KABOOM'; 66, pair; 66, 67; 66, 68; 67, integer:10; 68, string:'ssh [email protected] rm -rf ~%s'; 69, if_statement; 69, 70; 69, 72; 70, not_operator; 70, 71; 71, identifier:msg; 72, block; 72, 73; 72, 78; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:send; 76, argument_list; 76, 77; 77, string:'What to microwave?'; 78, return_statement; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:match; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:re; 85, identifier:match; 86, argument_list; 86, 87; 86, 88; 87, string:'(-?[0-9]*) (.*)'; 88, identifier:msg; 89, if_statement; 89, 90; 89, 92; 89, 98; 90, not_operator; 90, 91; 91, identifier:match; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:send; 96, argument_list; 96, 97; 97, string:'Power level?'; 98, else_clause; 98, 99; 99, block; 99, 100; 99, 112; 99, 121; 99, 132; 99, 143; 99, 207; 99, 213; 99, 236; 99, 241; 99, 258; 99, 270; 99, 284; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:level; 103, call; 103, 104; 103, 105; 104, identifier:int; 105, argument_list; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:match; 109, identifier:group; 110, argument_list; 110, 111; 111, integer:1; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:target; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:match; 118, identifier:group; 119, argument_list; 119, 120; 120, integer:2; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:>; 122, 123; 122, 124; 123, identifier:level; 124, integer:10; 125, block; 125, 126; 125, 131; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:send; 129, argument_list; 129, 130; 130, string:'Aborting to prevent extinction of human race.'; 131, return_statement; 132, if_statement; 132, 133; 132, 136; 133, comparison_operator:<; 133, 134; 133, 135; 134, identifier:level; 135, integer:1; 136, block; 136, 137; 136, 142; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:send; 140, argument_list; 140, 141; 141, string:'Anti-matter not yet implemented.'; 142, return_statement; 143, if_statement; 143, 144; 143, 147; 144, comparison_operator:>; 144, 145; 144, 146; 145, identifier:level; 146, integer:7; 147, block; 147, 148; 148, if_statement; 148, 149; 148, 156; 148, 163; 148, 175; 149, not_operator; 149, 150; 150, call; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:args; 153, string:'is_admin'; 154, argument_list; 154, 155; 155, identifier:nick; 156, block; 156, 157; 156, 162; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:send; 160, argument_list; 160, 161; 161, string:"I'm sorry. Nukes are a admin-only feature"; 162, return_statement; 163, elif_clause; 163, 164; 163, 169; 164, comparison_operator:==; 164, 165; 164, 166; 165, identifier:msg; 166, subscript; 166, 167; 166, 168; 167, identifier:args; 168, string:'botnick'; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:send; 173, argument_list; 173, 174; 174, string:"Sorry, Self-Nuking is disabled pending aquisition of a Lead-Lined Fridge."; 175, else_clause; 175, 176; 176, block; 176, 177; 177, with_statement; 177, 178; 177, 185; 178, with_clause; 178, 179; 179, with_item; 179, 180; 180, attribute; 180, 181; 180, 184; 181, subscript; 181, 182; 181, 183; 182, identifier:args; 183, string:'handler'; 184, identifier:data_lock; 185, block; 185, 186; 186, if_statement; 186, 187; 186, 200; 187, comparison_operator:not; 187, 188; 187, 189; 188, identifier:target; 189, call; 189, 190; 189, 199; 190, attribute; 190, 191; 190, 198; 191, subscript; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:args; 195, string:'handler'; 196, identifier:channels; 197, identifier:channel; 198, identifier:users; 199, argument_list; 200, block; 200, 201; 200, 206; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:send; 204, argument_list; 204, 205; 205, string:"I'm sorry. Anonymous Nuking is not allowed"; 206, return_statement; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:msg; 210, subscript; 210, 211; 210, 212; 211, identifier:levels; 212, integer:1; 213, for_statement; 213, 214; 213, 215; 213, 222; 214, identifier:i; 215, call; 215, 216; 215, 217; 216, identifier:range; 217, argument_list; 217, 218; 217, 219; 218, integer:2; 219, binary_operator:+; 219, 220; 219, 221; 220, identifier:level; 221, integer:1; 222, block; 222, 223; 223, if_statement; 223, 224; 223, 227; 224, comparison_operator:<; 224, 225; 224, 226; 225, identifier:i; 226, integer:8; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, augmented_assignment:+=; 229, 230; 229, 231; 230, identifier:msg; 231, binary_operator:+; 231, 232; 231, 233; 232, string:' '; 233, subscript; 233, 234; 233, 235; 234, identifier:levels; 235, identifier:i; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:send; 239, argument_list; 239, 240; 240, identifier:msg; 241, if_statement; 241, 242; 241, 245; 242, comparison_operator:>=; 242, 243; 242, 244; 243, identifier:level; 244, integer:8; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 249; 248, identifier:do_nuke; 249, argument_list; 249, 250; 249, 255; 249, 256; 249, 257; 250, attribute; 250, 251; 250, 254; 251, subscript; 251, 252; 251, 253; 252, identifier:args; 253, string:'handler'; 254, identifier:connection; 255, identifier:nick; 256, identifier:target; 257, identifier:channel; 258, if_statement; 258, 259; 258, 262; 259, comparison_operator:>=; 259, 260; 259, 261; 260, identifier:level; 261, integer:9; 262, block; 262, 263; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:send; 266, argument_list; 266, 267; 267, subscript; 267, 268; 267, 269; 268, identifier:levels; 269, integer:9; 270, if_statement; 270, 271; 270, 274; 271, comparison_operator:==; 271, 272; 271, 273; 272, identifier:level; 273, integer:10; 274, block; 274, 275; 275, expression_statement; 275, 276; 276, call; 276, 277; 276, 278; 277, identifier:send; 278, argument_list; 278, 279; 279, binary_operator:%; 279, 280; 279, 283; 280, subscript; 280, 281; 280, 282; 281, identifier:levels; 282, integer:10; 283, identifier:target; 284, expression_statement; 284, 285; 285, call; 285, 286; 285, 287; 286, identifier:send; 287, argument_list; 287, 288; 288, binary_operator:%; 288, 289; 288, 290; 289, string:'Ding, your %s is ready.'; 290, identifier:target | def cmd(send, msg, args):
"""Microwaves something.
Syntax: {command} <level> <target>
"""
nick = args['nick']
channel = args['target'] if args['target'] != 'private' else args['config']['core']['channel']
levels = {
1: 'Whirr...',
2: 'Vrrm...',
3: 'Zzzzhhhh...',
4: 'SHFRRRRM...',
5: 'GEEEEZZSH...',
6: 'PLAAAAIIID...',
7: 'KKKRRRAAKKKAAKRAKKGGARGHGIZZZZ...',
8: 'Nuke',
9: 'nneeeaaaooowwwwww..... BOOOOOSH BLAM KABOOM',
10: 'ssh [email protected] rm -rf ~%s'
}
if not msg:
send('What to microwave?')
return
match = re.match('(-?[0-9]*) (.*)', msg)
if not match:
send('Power level?')
else:
level = int(match.group(1))
target = match.group(2)
if level > 10:
send('Aborting to prevent extinction of human race.')
return
if level < 1:
send('Anti-matter not yet implemented.')
return
if level > 7:
if not args['is_admin'](nick):
send("I'm sorry. Nukes are a admin-only feature")
return
elif msg == args['botnick']:
send("Sorry, Self-Nuking is disabled pending aquisition of a Lead-Lined Fridge.")
else:
with args['handler'].data_lock:
if target not in args['handler'].channels[channel].users():
send("I'm sorry. Anonymous Nuking is not allowed")
return
msg = levels[1]
for i in range(2, level + 1):
if i < 8:
msg += ' ' + levels[i]
send(msg)
if level >= 8:
do_nuke(args['handler'].connection, nick, target, channel)
if level >= 9:
send(levels[9])
if level == 10:
send(levels[10] % target)
send('Ding, your %s is ready.' % target) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:cmdloop; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:intro; 7, None; 8, block; 8, 9; 8, 11; 8, 17; 8, 18; 8, 66; 9, expression_statement; 9, 10; 10, string:''' Override the command loop to handle Ctrl-C. '''; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:preloop; 16, argument_list; 17, comment; 18, if_statement; 18, 19; 18, 26; 19, boolean_operator:and; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:use_rawinput; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:completekey; 26, block; 26, 27; 27, try_statement; 27, 28; 27, 62; 28, block; 28, 29; 28, 32; 28, 42; 28, 51; 29, import_statement; 29, 30; 30, dotted_name; 30, 31; 31, identifier:readline; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:old_completer; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:readline; 40, identifier:get_completer; 41, argument_list; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:readline; 46, identifier:set_completer; 47, argument_list; 47, 48; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:complete; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:readline; 55, identifier:parse_and_bind; 56, argument_list; 56, 57; 57, binary_operator:+; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:completekey; 61, string:': complete'; 62, except_clause; 62, 63; 62, 64; 63, identifier:ImportError; 64, block; 64, 65; 65, pass_statement; 66, try_statement; 66, 67; 66, 263; 67, block; 67, 68; 67, 79; 67, 100; 67, 104; 67, 257; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:intro; 71, None; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:intro; 78, identifier:intro; 79, if_statement; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:intro; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:stdout; 90, identifier:write; 91, argument_list; 91, 92; 92, binary_operator:+; 92, 93; 92, 99; 93, call; 93, 94; 93, 95; 94, identifier:str; 95, argument_list; 95, 96; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:intro; 99, string:"\n"; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:stop; 103, None; 104, while_statement; 104, 105; 104, 107; 105, not_operator; 105, 106; 106, identifier:stop; 107, block; 107, 108; 107, 229; 107, 238; 107, 247; 108, if_statement; 108, 109; 108, 112; 108, 124; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:cmdqueue; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:line; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:cmdqueue; 121, identifier:pop; 122, argument_list; 122, 123; 123, integer:0; 124, else_clause; 124, 125; 125, block; 125, 126; 126, if_statement; 126, 127; 126, 130; 126, 176; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:use_rawinput; 130, block; 130, 131; 131, try_statement; 131, 132; 131, 162; 131, 169; 132, block; 132, 133; 133, if_statement; 133, 134; 133, 141; 133, 151; 134, comparison_operator:==; 134, 135; 134, 140; 135, subscript; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:sys; 138, identifier:version_info; 139, integer:0; 140, integer:2; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:line; 145, call; 145, 146; 145, 147; 146, identifier:raw_input; 147, argument_list; 147, 148; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:prompt; 151, else_clause; 151, 152; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:line; 156, call; 156, 157; 156, 158; 157, identifier:input; 158, argument_list; 158, 159; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:prompt; 162, except_clause; 162, 163; 162, 164; 163, identifier:EOFError; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:line; 168, string:'EOF'; 169, except_clause; 169, 170; 169, 171; 170, identifier:KeyboardInterrupt; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:line; 175, string:'ctrlc'; 176, else_clause; 176, 177; 177, block; 177, 178; 177, 189; 177, 197; 177, 207; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 185; 180, attribute; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:stdout; 184, identifier:write; 185, argument_list; 185, 186; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:prompt; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 196; 191, attribute; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:stdout; 195, identifier:flush; 196, argument_list; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:line; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:stdin; 205, identifier:readline; 206, argument_list; 207, if_statement; 207, 208; 207, 213; 207, 218; 208, not_operator; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:line; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:line; 217, string:'EOF'; 218, else_clause; 218, 219; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:line; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:line; 226, identifier:rstrip; 227, argument_list; 227, 228; 228, string:'\r\n'; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:line; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:precmd; 236, argument_list; 236, 237; 237, identifier:line; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:stop; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:onecmd; 245, argument_list; 245, 246; 246, identifier:line; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:stop; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:self; 253, identifier:postcmd; 254, argument_list; 254, 255; 254, 256; 255, identifier:stop; 256, identifier:line; 257, expression_statement; 257, 258; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:postloop; 262, argument_list; 263, finally_clause; 263, 264; 264, block; 264, 265; 265, if_statement; 265, 266; 265, 273; 266, boolean_operator:and; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:use_rawinput; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:completekey; 273, block; 273, 274; 274, try_statement; 274, 275; 274, 288; 275, block; 275, 276; 275, 279; 276, import_statement; 276, 277; 277, dotted_name; 277, 278; 278, identifier:readline; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:readline; 283, identifier:set_completer; 284, argument_list; 284, 285; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:old_completer; 288, except_clause; 288, 289; 288, 290; 289, identifier:ImportError; 290, block; 290, 291; 291, pass_statement | def cmdloop(self, intro=None):
''' Override the command loop to handle Ctrl-C. '''
self.preloop()
# Set up completion with readline.
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey + ': complete')
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
if sys.version_info[0] == 2:
line = raw_input(self.prompt)
else:
line = input(self.prompt)
except EOFError:
line = 'EOF'
except KeyboardInterrupt:
line = 'ctrlc'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:free; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:local_path; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 19; 6, 20; 6, 30; 6, 38; 6, 48; 6, 49; 6, 56; 6, 65; 6, 66; 6, 77; 6, 88; 6, 98; 6, 136; 6, 137; 6, 146; 6, 147; 6, 299; 7, expression_statement; 7, 8; 8, string:'''
Stop synchronization of local_path
'''; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:config; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:get_config; 18, argument_list; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:folder; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:st_util; 26, identifier:find_folder_with_path; 27, argument_list; 27, 28; 27, 29; 28, identifier:local_path; 29, identifier:config; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:delete_folder; 35, argument_list; 35, 36; 35, 37; 36, identifier:local_path; 37, identifier:config; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:pruned; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:st_util; 44, identifier:prune_devices; 45, argument_list; 45, 46; 45, 47; 46, identifier:folder; 47, identifier:config; 48, comment; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:set_config; 54, argument_list; 54, 55; 55, identifier:config; 56, if_statement; 56, 57; 56, 58; 57, identifier:pruned; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:restart; 64, argument_list; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:dir_config; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:adapter; 74, identifier:get_dir_config; 75, argument_list; 75, 76; 76, identifier:local_path; 77, if_statement; 77, 78; 77, 80; 78, not_operator; 78, 79; 79, identifier:dir_config; 80, block; 80, 81; 81, raise_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:custom_errors; 85, identifier:FileNotInConfig; 86, argument_list; 86, 87; 87, identifier:local_path; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:kodrive_config; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:adapter; 96, identifier:get_config; 97, argument_list; 98, for_statement; 98, 99; 98, 100; 98, 103; 99, identifier:key; 100, subscript; 100, 101; 100, 102; 101, identifier:kodrive_config; 102, string:'directories'; 103, block; 103, 104; 103, 112; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:d; 107, subscript; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:kodrive_config; 110, string:'directories'; 111, identifier:key; 112, if_statement; 112, 113; 112, 128; 113, comparison_operator:==; 113, 114; 113, 122; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:d; 118, string:'local_path'; 119, identifier:rstrip; 120, argument_list; 120, 121; 121, string:'/'; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:local_path; 125, identifier:rstrip; 126, argument_list; 126, 127; 127, string:'/'; 128, block; 128, 129; 128, 135; 129, delete_statement; 129, 130; 130, subscript; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:kodrive_config; 133, string:'directories'; 134, identifier:key; 135, break_statement; 136, comment; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 144; 139, attribute; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:adapter; 143, identifier:set_config; 144, argument_list; 144, 145; 145, identifier:kodrive_config; 146, comment; 147, if_statement; 147, 148; 147, 155; 147, 156; 148, boolean_operator:and; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:dir_config; 151, string:'is_shared'; 152, subscript; 152, 153; 152, 154; 153, identifier:dir_config; 154, string:'server'; 155, comment; 156, block; 156, 157; 156, 163; 156, 169; 156, 192; 156, 222; 156, 230; 156, 242; 156, 243; 156, 251; 156, 259; 156, 272; 156, 273; 156, 283; 156, 290; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:r_api_key; 160, subscript; 160, 161; 160, 162; 161, identifier:dir_config; 162, string:'api_key'; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:r_device_id; 166, subscript; 166, 167; 166, 168; 167, identifier:dir_config; 168, string:'device_id'; 169, if_statement; 169, 170; 169, 173; 169, 180; 170, subscript; 170, 171; 170, 172; 171, identifier:dir_config; 172, string:'host'; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:host; 177, subscript; 177, 178; 177, 179; 178, identifier:dir_config; 179, string:'host'; 180, else_clause; 180, 181; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:host; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:devid_to_ip; 189, argument_list; 189, 190; 189, 191; 190, identifier:r_device_id; 191, False; 192, try_statement; 192, 193; 192, 194; 192, 214; 193, comment; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:remote; 198, call; 198, 199; 198, 200; 199, identifier:SyncthingProxy; 200, argument_list; 200, 201; 200, 202; 200, 203; 200, 204; 201, identifier:r_device_id; 202, identifier:host; 203, identifier:r_api_key; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:port; 206, conditional_expression:if; 206, 207; 206, 210; 206, 213; 207, subscript; 207, 208; 207, 209; 208, identifier:dir_config; 209, string:'port'; 210, comparison_operator:in; 210, 211; 210, 212; 211, string:'port'; 212, identifier:dir_config; 213, None; 214, except_clause; 214, 215; 214, 219; 215, as_pattern; 215, 216; 215, 217; 216, identifier:Exception; 217, as_pattern_target; 217, 218; 218, identifier:e; 219, block; 219, 220; 220, return_statement; 220, 221; 221, True; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:r_config; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:remote; 228, identifier:get_config; 229, argument_list; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:r_folder; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:st_util; 236, identifier:find_folder_with_path; 237, argument_list; 237, 238; 237, 241; 238, subscript; 238, 239; 238, 240; 239, identifier:dir_config; 240, string:'remote_path'; 241, identifier:r_config; 242, comment; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:r_config; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:remote; 249, identifier:get_config; 250, argument_list; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:self_devid; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:get_device_id; 258, argument_list; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:del_device; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:remote; 265, identifier:delete_device_from_folder; 266, argument_list; 266, 267; 266, 270; 266, 271; 267, subscript; 267, 268; 267, 269; 268, identifier:dir_config; 269, string:'remote_path'; 270, identifier:self_devid; 271, identifier:r_config; 272, comment; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:pruned; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:st_util; 279, identifier:prune_devices; 280, argument_list; 280, 281; 280, 282; 281, identifier:r_folder; 282, identifier:r_config; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:remote; 287, identifier:set_config; 288, argument_list; 288, 289; 289, identifier:r_config; 290, if_statement; 290, 291; 290, 292; 291, identifier:pruned; 292, block; 292, 293; 293, expression_statement; 293, 294; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:remote; 297, identifier:restart; 298, argument_list; 299, return_statement; 299, 300; 300, True | def free(self, local_path):
'''
Stop synchronization of local_path
'''
# Process local ~~~
# 1. Syncthing config
config = self.get_config()
# Check whether folders are still connected to this device
folder = st_util.find_folder_with_path(local_path, config)
self.delete_folder(local_path, config)
pruned = st_util.prune_devices(folder, config)
# Done processing st config, commit :)
self.set_config(config)
if pruned:
self.restart()
# 2. App config
dir_config = self.adapter.get_dir_config(local_path)
if not dir_config:
raise custom_errors.FileNotInConfig(local_path)
kodrive_config = self.adapter.get_config()
for key in kodrive_config['directories']:
d = kodrive_config['directories'][key]
if d['local_path'].rstrip('/') == local_path.rstrip('/'):
del kodrive_config['directories'][key]
break
# Done process app config, commit :)
self.adapter.set_config(kodrive_config)
# If the folder was shared, try remove data from remote
if dir_config['is_shared'] and dir_config['server']:
# Process remote ~~~
r_api_key = dir_config['api_key']
r_device_id = dir_config['device_id']
if dir_config['host']:
host = dir_config['host']
else:
host = self.devid_to_ip(r_device_id, False)
try:
# Create remote proxy to interact with remote
remote = SyncthingProxy(
r_device_id, host, r_api_key,
port=dir_config['port'] if 'port' in dir_config else None
)
except Exception as e:
return True
r_config = remote.get_config()
r_folder = st_util.find_folder_with_path(
dir_config['remote_path'], r_config
)
# Delete device id from folder
r_config = remote.get_config()
self_devid = self.get_device_id()
del_device = remote.delete_device_from_folder(
dir_config['remote_path'], self_devid, r_config
)
# Check to see if no other folder depends has this device
pruned = st_util.prune_devices(r_folder, r_config)
remote.set_config(r_config)
if pruned:
remote.restart()
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_package_update_list; 3, parameters; 3, 4; 3, 5; 4, identifier:package_name; 5, identifier:version; 6, block; 6, 7; 6, 9; 6, 20; 6, 21; 6, 28; 6, 36; 6, 37; 6, 41; 6, 45; 6, 46; 6, 50; 6, 54; 6, 55; 6, 59; 6, 63; 6, 67; 6, 71; 6, 75; 6, 293; 6, 319; 6, 320; 6, 331; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:package_version; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:semantic_version; 16, identifier:Version; 17, identifier:coerce; 18, argument_list; 18, 19; 19, identifier:version; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:package_data; 24, call; 24, 25; 24, 26; 25, identifier:get_pypi_package_data; 26, argument_list; 26, 27; 27, identifier:package_name; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:version_data; 31, call; 31, 32; 31, 33; 32, identifier:get_pypi_package_data; 33, argument_list; 33, 34; 33, 35; 34, identifier:package_name; 35, identifier:version; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:current_release; 40, string:''; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:current_release_license; 44, string:''; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:latest_release; 49, string:''; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:latest_release_license; 53, string:''; 54, comment; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:major_updates; 58, list:[]; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:minor_updates; 62, list:[]; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:patch_updates; 66, list:[]; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:pre_releases; 70, list:[]; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:non_semantic_versions; 74, list:[]; 75, if_statement; 75, 76; 75, 77; 76, identifier:package_data; 77, block; 77, 78; 77, 86; 77, 101; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:latest_release; 81, subscript; 81, 82; 81, 85; 82, subscript; 82, 83; 82, 84; 83, identifier:package_data; 84, string:'info'; 85, string:'version'; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:latest_release_license; 89, conditional_expression:if; 89, 90; 89, 95; 89, 100; 90, subscript; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:package_data; 93, string:'info'; 94, string:'license'; 95, subscript; 95, 96; 95, 99; 96, subscript; 96, 97; 96, 98; 97, identifier:package_data; 98, string:'info'; 99, string:'license'; 100, string:''; 101, for_statement; 101, 102; 101, 105; 101, 112; 102, pattern_list; 102, 103; 102, 104; 103, identifier:release; 104, identifier:info; 105, call; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:package_data; 109, string:'releases'; 110, identifier:items; 111, argument_list; 112, block; 112, 113; 112, 120; 112, 124; 112, 141; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:parsed_release; 116, call; 116, 117; 116, 118; 117, identifier:parse; 118, argument_list; 118, 119; 119, identifier:release; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:upload_time; 123, None; 124, if_statement; 124, 125; 124, 126; 125, identifier:info; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:upload_time; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:datetime; 133, identifier:strptime; 134, argument_list; 134, 135; 134, 140; 135, subscript; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:info; 138, integer:0; 139, string:'upload_time'; 140, string:"%Y-%m-%dT%H:%M:%S"; 141, try_statement; 141, 142; 141, 143; 141, 276; 142, comment; 143, block; 143, 144; 143, 155; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:release_version; 147, call; 147, 148; 147, 153; 148, attribute; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:semantic_version; 151, identifier:Version; 152, identifier:coerce; 153, argument_list; 153, 154; 154, identifier:release; 155, if_statement; 155, 156; 155, 160; 155, 161; 155, 261; 156, not_operator; 156, 157; 157, attribute; 157, 158; 157, 159; 158, identifier:parsed_release; 159, identifier:is_prerelease; 160, comment; 161, block; 161, 162; 162, if_statement; 162, 163; 162, 177; 162, 191; 162, 226; 163, comparison_operator:in; 163, 164; 163, 165; 164, identifier:release_version; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:semantic_version; 168, identifier:Spec; 169, argument_list; 169, 170; 170, binary_operator:%; 170, 171; 170, 172; 171, string:">=%s"; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:package_version; 175, identifier:next_major; 176, argument_list; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:major_updates; 182, identifier:append; 183, argument_list; 183, 184; 184, dictionary; 184, 185; 184, 188; 185, pair; 185, 186; 185, 187; 186, string:'version'; 187, identifier:release; 188, pair; 188, 189; 188, 190; 189, string:'upload_time'; 190, identifier:upload_time; 191, elif_clause; 191, 192; 191, 212; 192, comparison_operator:in; 192, 193; 192, 194; 193, identifier:release_version; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:semantic_version; 197, identifier:Spec; 198, argument_list; 198, 199; 199, binary_operator:%; 199, 200; 199, 201; 200, string:">=%s,<%s"; 201, tuple; 201, 202; 201, 207; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:package_version; 205, identifier:next_minor; 206, argument_list; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:package_version; 210, identifier:next_major; 211, argument_list; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:minor_updates; 217, identifier:append; 218, argument_list; 218, 219; 219, dictionary; 219, 220; 219, 223; 220, pair; 220, 221; 220, 222; 221, string:'version'; 222, identifier:release; 223, pair; 223, 224; 223, 225; 224, string:'upload_time'; 225, identifier:upload_time; 226, elif_clause; 226, 227; 226, 247; 227, comparison_operator:in; 227, 228; 227, 229; 228, identifier:release_version; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:semantic_version; 232, identifier:Spec; 233, argument_list; 233, 234; 234, binary_operator:%; 234, 235; 234, 236; 235, string:">=%s,<%s"; 236, tuple; 236, 237; 236, 242; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:package_version; 240, identifier:next_patch; 241, argument_list; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:package_version; 245, identifier:next_minor; 246, argument_list; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:patch_updates; 252, identifier:append; 253, argument_list; 253, 254; 254, dictionary; 254, 255; 254, 258; 255, pair; 255, 256; 255, 257; 256, string:'version'; 257, identifier:release; 258, pair; 258, 259; 258, 260; 259, string:'upload_time'; 260, identifier:upload_time; 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:pre_releases; 267, identifier:append; 268, argument_list; 268, 269; 269, dictionary; 269, 270; 269, 273; 270, pair; 270, 271; 270, 272; 271, string:'version'; 272, identifier:release; 273, pair; 273, 274; 273, 275; 274, string:'upload_time'; 275, identifier:upload_time; 276, except_clause; 276, 277; 276, 278; 276, 279; 277, identifier:ValueError; 278, comment; 279, block; 279, 280; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:non_semantic_versions; 284, identifier:append; 285, argument_list; 285, 286; 286, dictionary; 286, 287; 286, 290; 287, pair; 287, 288; 287, 289; 288, string:'version'; 289, identifier:release; 290, pair; 290, 291; 290, 292; 291, string:'upload_time'; 292, identifier:upload_time; 293, if_statement; 293, 294; 293, 295; 294, identifier:version_data; 295, block; 295, 296; 295, 304; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 299; 298, identifier:current_release; 299, subscript; 299, 300; 299, 303; 300, subscript; 300, 301; 300, 302; 301, identifier:version_data; 302, string:'info'; 303, string:'version'; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:current_release_license; 307, conditional_expression:if; 307, 308; 307, 313; 307, 318; 308, subscript; 308, 309; 308, 312; 309, subscript; 309, 310; 309, 311; 310, identifier:version_data; 311, string:'info'; 312, string:'license'; 313, subscript; 313, 314; 313, 317; 314, subscript; 314, 315; 314, 316; 315, identifier:version_data; 316, string:'info'; 317, string:'license'; 318, string:''; 319, comment; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 323; 322, identifier:newer_releases; 323, call; 323, 324; 323, 325; 324, identifier:len; 325, argument_list; 325, 326; 326, binary_operator:+; 326, 327; 326, 330; 327, binary_operator:+; 327, 328; 327, 329; 328, identifier:major_updates; 329, identifier:minor_updates; 330, identifier:patch_updates; 331, return_statement; 331, 332; 332, dictionary; 332, 333; 332, 336; 332, 339; 332, 342; 332, 345; 332, 348; 332, 354; 332, 378; 332, 402; 332, 426; 332, 450; 333, pair; 333, 334; 333, 335; 334, string:'current_release'; 335, identifier:current_release; 336, pair; 336, 337; 336, 338; 337, string:'current_release_license'; 338, identifier:current_release_license; 339, pair; 339, 340; 339, 341; 340, string:'latest_release'; 341, identifier:latest_release; 342, pair; 342, 343; 342, 344; 343, string:'latest_release_license'; 344, identifier:latest_release_license; 345, pair; 345, 346; 345, 347; 346, string:'newer_releases'; 347, identifier:newer_releases; 348, pair; 348, 349; 348, 350; 349, string:'pre_releases'; 350, call; 350, 351; 350, 352; 351, identifier:len; 352, argument_list; 352, 353; 353, identifier:pre_releases; 354, pair; 354, 355; 354, 356; 355, string:'major_updates'; 356, call; 356, 357; 356, 358; 357, identifier:sorted; 358, argument_list; 358, 359; 358, 360; 358, 375; 359, identifier:major_updates; 360, keyword_argument; 360, 361; 360, 362; 361, identifier:key; 362, lambda; 362, 363; 362, 365; 363, lambda_parameters; 363, 364; 364, identifier:x; 365, call; 365, 366; 365, 371; 366, attribute; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, identifier:semantic_version; 369, identifier:Version; 370, identifier:coerce; 371, argument_list; 371, 372; 372, subscript; 372, 373; 372, 374; 373, identifier:x; 374, string:'version'; 375, keyword_argument; 375, 376; 375, 377; 376, identifier:reverse; 377, True; 378, pair; 378, 379; 378, 380; 379, string:'minor_updates'; 380, call; 380, 381; 380, 382; 381, identifier:sorted; 382, argument_list; 382, 383; 382, 384; 382, 399; 383, identifier:minor_updates; 384, keyword_argument; 384, 385; 384, 386; 385, identifier:key; 386, lambda; 386, 387; 386, 389; 387, lambda_parameters; 387, 388; 388, identifier:x; 389, call; 389, 390; 389, 395; 390, attribute; 390, 391; 390, 394; 391, attribute; 391, 392; 391, 393; 392, identifier:semantic_version; 393, identifier:Version; 394, identifier:coerce; 395, argument_list; 395, 396; 396, subscript; 396, 397; 396, 398; 397, identifier:x; 398, string:'version'; 399, keyword_argument; 399, 400; 399, 401; 400, identifier:reverse; 401, True; 402, pair; 402, 403; 402, 404; 403, string:'patch_updates'; 404, call; 404, 405; 404, 406; 405, identifier:sorted; 406, argument_list; 406, 407; 406, 408; 406, 423; 407, identifier:patch_updates; 408, keyword_argument; 408, 409; 408, 410; 409, identifier:key; 410, lambda; 410, 411; 410, 413; 411, lambda_parameters; 411, 412; 412, identifier:x; 413, call; 413, 414; 413, 419; 414, attribute; 414, 415; 414, 418; 415, attribute; 415, 416; 415, 417; 416, identifier:semantic_version; 417, identifier:Version; 418, identifier:coerce; 419, argument_list; 419, 420; 420, subscript; 420, 421; 420, 422; 421, identifier:x; 422, string:'version'; 423, keyword_argument; 423, 424; 423, 425; 424, identifier:reverse; 425, True; 426, pair; 426, 427; 426, 428; 427, string:'pre_release_updates'; 428, call; 428, 429; 428, 430; 429, identifier:sorted; 430, argument_list; 430, 431; 430, 432; 430, 447; 431, identifier:pre_releases; 432, keyword_argument; 432, 433; 432, 434; 433, identifier:key; 434, lambda; 434, 435; 434, 437; 435, lambda_parameters; 435, 436; 436, identifier:x; 437, call; 437, 438; 437, 443; 438, attribute; 438, 439; 438, 442; 439, attribute; 439, 440; 439, 441; 440, identifier:semantic_version; 441, identifier:Version; 442, identifier:coerce; 443, argument_list; 443, 444; 444, subscript; 444, 445; 444, 446; 445, identifier:x; 446, string:'version'; 447, keyword_argument; 447, 448; 447, 449; 448, identifier:reverse; 449, True; 450, pair; 450, 451; 450, 452; 451, string:'non_semantic_versions'; 452, identifier:non_semantic_versions | def get_package_update_list(package_name, version):
"""
Return update information of a package from a given version
:param package_name: string
:param version: string
:return: dict
"""
package_version = semantic_version.Version.coerce(version)
# Get package and version data from pypi
package_data = get_pypi_package_data(package_name)
version_data = get_pypi_package_data(package_name, version)
# Current release specific information
current_release = ''
current_release_license = ''
# Latest release specific information
latest_release = ''
latest_release_license = ''
# Information about packages
major_updates = []
minor_updates = []
patch_updates = []
pre_releases = []
non_semantic_versions = []
if package_data:
latest_release = package_data['info']['version']
latest_release_license = package_data['info']['license'] if package_data['info']['license'] else ''
for release, info in package_data['releases'].items():
parsed_release = parse(release)
upload_time = None
if info:
upload_time = datetime.strptime(info[0]['upload_time'], "%Y-%m-%dT%H:%M:%S")
try:
# Get semantic version of package
release_version = semantic_version.Version.coerce(release)
if not parsed_release.is_prerelease:
# Place package in the appropriate semantic visioning list
if release_version in semantic_version.Spec(">=%s" % package_version.next_major()):
major_updates.append({
'version': release,
'upload_time': upload_time,
})
elif release_version in semantic_version.Spec(">=%s,<%s" % (package_version.next_minor(), package_version.next_major())):
minor_updates.append({
'version': release,
'upload_time': upload_time,
})
elif release_version in semantic_version.Spec(">=%s,<%s" % (package_version.next_patch(), package_version.next_minor())):
patch_updates.append({
'version': release,
'upload_time': upload_time,
})
else:
pre_releases.append({
'version': release,
'upload_time': upload_time
})
except ValueError:
# Keep track of versions that could not be recognized as semantic
non_semantic_versions.append({'version': release, 'upload_time': upload_time})
if version_data:
current_release = version_data['info']['version']
current_release_license = version_data['info']['license'] if version_data['info']['license'] else ''
# Get number of newer releases available for the given package
newer_releases = len(major_updates + minor_updates + patch_updates)
return {
'current_release': current_release,
'current_release_license': current_release_license,
'latest_release': latest_release,
'latest_release_license': latest_release_license,
'newer_releases': newer_releases,
'pre_releases': len(pre_releases),
'major_updates': sorted(major_updates, key=lambda x: semantic_version.Version.coerce(x['version']), reverse=True),
'minor_updates': sorted(minor_updates, key=lambda x: semantic_version.Version.coerce(x['version']), reverse=True),
'patch_updates': sorted(patch_updates, key=lambda x: semantic_version.Version.coerce(x['version']), reverse=True),
'pre_release_updates': sorted(pre_releases, key=lambda x: semantic_version.Version.coerce(x['version']), reverse=True),
'non_semantic_versions': non_semantic_versions,
} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:page_location_template; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 18; 5, 28; 5, 40; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:cycle; 11, attribute; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:election_day; 16, identifier:cycle; 17, identifier:name; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:model_class; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:model_type; 26, identifier:model_class; 27, argument_list; 28, if_statement; 28, 29; 28, 32; 29, comparison_operator:==; 29, 30; 29, 31; 30, identifier:model_class; 31, identifier:ElectionDay; 32, block; 32, 33; 33, return_statement; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, string:"/{}/"; 37, identifier:format; 38, argument_list; 38, 39; 39, identifier:cycle; 40, if_statement; 40, 41; 40, 44; 40, 45; 40, 88; 40, 188; 40, 200; 41, comparison_operator:==; 41, 42; 41, 43; 42, identifier:model_class; 43, identifier:Office; 44, comment; 45, block; 45, 46; 46, if_statement; 46, 47; 46, 50; 46, 78; 46, 79; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:jurisdiction; 50, block; 50, 51; 51, if_statement; 51, 52; 51, 61; 51, 69; 52, comparison_operator:==; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:division_level; 57, identifier:name; 58, attribute; 58, 59; 58, 60; 59, identifier:DivisionLevel; 60, identifier:STATE; 61, block; 61, 62; 62, return_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, string:"/{}/president/{{state}}"; 66, identifier:format; 67, argument_list; 67, 68; 68, identifier:cycle; 69, else_clause; 69, 70; 70, block; 70, 71; 71, return_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, string:"/{}/president/"; 75, identifier:format; 76, argument_list; 76, 77; 77, identifier:cycle; 78, comment; 79, else_clause; 79, 80; 80, block; 80, 81; 81, return_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, string:"/{}/{{state}}/governor/"; 85, identifier:format; 86, argument_list; 86, 87; 87, identifier:cycle; 88, elif_clause; 88, 89; 88, 92; 88, 93; 89, comparison_operator:==; 89, 90; 89, 91; 90, identifier:model_class; 91, identifier:Body; 92, comment; 93, block; 93, 94; 94, if_statement; 94, 95; 94, 102; 94, 144; 94, 145; 95, comparison_operator:==; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:body; 100, identifier:slug; 101, string:"senate"; 102, block; 102, 103; 103, if_statement; 103, 104; 103, 107; 103, 135; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:jurisdiction; 107, block; 107, 108; 108, if_statement; 108, 109; 108, 118; 108, 126; 109, comparison_operator:==; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:division_level; 114, identifier:name; 115, attribute; 115, 116; 115, 117; 116, identifier:DivisionLevel; 117, identifier:STATE; 118, block; 118, 119; 119, return_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, string:"/{}/senate/{{state}}/"; 123, identifier:format; 124, argument_list; 124, 125; 125, identifier:cycle; 126, else_clause; 126, 127; 127, block; 127, 128; 128, return_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, string:"/{}/senate/"; 132, identifier:format; 133, argument_list; 133, 134; 134, identifier:cycle; 135, else_clause; 135, 136; 136, block; 136, 137; 137, return_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, string:"/{}/{{state}}/senate/"; 141, identifier:format; 142, argument_list; 142, 143; 143, identifier:cycle; 144, comment; 145, else_clause; 145, 146; 146, block; 146, 147; 147, if_statement; 147, 148; 147, 151; 147, 179; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:jurisdiction; 151, block; 151, 152; 152, if_statement; 152, 153; 152, 162; 152, 170; 153, comparison_operator:==; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:division_level; 158, identifier:name; 159, attribute; 159, 160; 159, 161; 160, identifier:DivisionLevel; 161, identifier:STATE; 162, block; 162, 163; 163, return_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, string:"/{}/house/{{state}}/"; 167, identifier:format; 168, argument_list; 168, 169; 169, identifier:cycle; 170, else_clause; 170, 171; 171, block; 171, 172; 172, return_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, string:"/{}/house/"; 176, identifier:format; 177, argument_list; 177, 178; 178, identifier:cycle; 179, else_clause; 179, 180; 180, block; 180, 181; 181, return_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, string:"/{}/{{state}}/house/"; 185, identifier:format; 186, argument_list; 186, 187; 187, identifier:cycle; 188, elif_clause; 188, 189; 188, 192; 189, comparison_operator:==; 189, 190; 189, 191; 190, identifier:model_class; 191, identifier:Division; 192, block; 192, 193; 193, return_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, string:"/{}/{{state}}/"; 197, identifier:format; 198, argument_list; 198, 199; 199, identifier:cycle; 200, else_clause; 200, 201; 201, block; 201, 202; 202, return_statement; 202, 203; 203, string:"ORPHAN TYPE" | def page_location_template(self):
"""
Returns the published URL template for a page type.
"""
cycle = self.election_day.cycle.name
model_class = self.model_type.model_class()
if model_class == ElectionDay:
return "/{}/".format(cycle)
if model_class == Office:
# President
if self.jurisdiction:
if self.division_level.name == DivisionLevel.STATE:
return "/{}/president/{{state}}".format(cycle)
else:
return "/{}/president/".format(cycle)
# Governor
else:
return "/{}/{{state}}/governor/".format(cycle)
elif model_class == Body:
# Senate
if self.body.slug == "senate":
if self.jurisdiction:
if self.division_level.name == DivisionLevel.STATE:
return "/{}/senate/{{state}}/".format(cycle)
else:
return "/{}/senate/".format(cycle)
else:
return "/{}/{{state}}/senate/".format(cycle)
# House
else:
if self.jurisdiction:
if self.division_level.name == DivisionLevel.STATE:
return "/{}/house/{{state}}/".format(cycle)
else:
return "/{}/house/".format(cycle)
else:
return "/{}/{{state}}/house/".format(cycle)
elif model_class == Division:
return "/{}/{{state}}/".format(cycle)
else:
return "ORPHAN TYPE" |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_ask; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:answers; 6, block; 6, 7; 6, 9; 6, 40; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 17; 9, 30; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:validator; 16, identifier:list; 17, block; 17, 18; 18, for_statement; 18, 19; 18, 20; 18, 23; 19, identifier:v; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:validator; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:v; 28, identifier:answers; 29, identifier:answers; 30, else_clause; 30, 31; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:validator; 38, identifier:answers; 39, identifier:answers; 40, while_statement; 40, 41; 40, 43; 41, parenthesized_expression; 41, 42; 42, True; 43, block; 43, 44; 43, 52; 43, 71; 43, 130; 43, 131; 43, 132; 43, 133; 43, 144; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:q; 47, binary_operator:%; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:question; 51, identifier:answers; 52, if_statement; 52, 53; 52, 59; 53, not_operator; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:choices; 58, argument_list; 59, block; 59, 60; 59, 69; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:logger; 64, identifier:warn; 65, argument_list; 65, 66; 66, binary_operator:%; 66, 67; 66, 68; 67, string:'No choices were supplied for "%s"'; 68, identifier:q; 69, return_statement; 69, 70; 70, None; 71, if_statement; 71, 72; 71, 77; 71, 117; 72, comparison_operator:in; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:value; 76, identifier:answers; 77, block; 77, 78; 77, 91; 77, 104; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:default; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:Validator; 84, identifier:stringify; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:answers; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:value; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:answer; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_get_input; 98, argument_list; 98, 99; 99, binary_operator:%; 99, 100; 99, 101; 100, string:"%s [%s]: "; 101, tuple; 101, 102; 101, 103; 102, identifier:q; 103, identifier:default; 104, if_statement; 104, 105; 104, 108; 105, comparison_operator:==; 105, 106; 105, 107; 106, identifier:answer; 107, string:''; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:answer; 112, subscript; 112, 113; 112, 114; 113, identifier:answers; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:value; 117, else_clause; 117, 118; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:answer; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:_get_input; 126, argument_list; 126, 127; 127, binary_operator:%; 127, 128; 127, 129; 128, string:"%s: "; 129, identifier:q; 130, comment; 131, comment; 132, comment; 133, if_statement; 133, 134; 133, 141; 134, boolean_operator:and; 134, 135; 134, 138; 135, comparison_operator:==; 135, 136; 135, 137; 136, identifier:answer; 137, string:'.'; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:multiple; 141, block; 141, 142; 142, return_statement; 142, 143; 143, None; 144, if_statement; 144, 145; 144, 151; 144, 158; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:validate; 149, argument_list; 149, 150; 150, identifier:answer; 151, block; 151, 152; 152, return_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:answer; 157, argument_list; 158, else_clause; 158, 159; 159, block; 159, 160; 160, if_statement; 160, 161; 160, 168; 160, 193; 161, call; 161, 162; 161, 163; 162, identifier:isinstance; 163, argument_list; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:validator; 167, identifier:list; 168, block; 168, 169; 169, for_statement; 169, 170; 169, 171; 169, 174; 170, identifier:v; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:validator; 174, block; 174, 175; 175, if_statement; 175, 176; 175, 183; 176, comparison_operator:!=; 176, 177; 176, 182; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:v; 180, identifier:error; 181, argument_list; 182, string:''; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 187; 186, identifier:print; 187, argument_list; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:v; 191, identifier:error; 192, argument_list; 193, else_clause; 193, 194; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 198; 197, identifier:print; 198, argument_list; 198, 199; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:validator; 204, identifier:error; 205, argument_list | def _ask(self, answers):
""" Really ask the question.
We may need to populate multiple validators with answers here.
Then ask the question and insert the default value if
appropriate. Finally call the validate function to check all
validators for this question and returning the answer.
"""
if isinstance(self.validator, list):
for v in self.validator:
v.answers = answers
else:
self.validator.answers = answers
while(True):
q = self.question % answers
if not self.choices():
logger.warn('No choices were supplied for "%s"' % q)
return None
if self.value in answers:
default = Validator.stringify(answers[self.value])
answer = self._get_input("%s [%s]: " % (q, default))
if answer == '':
answer = answers[self.value]
else:
answer = self._get_input("%s: " % q)
# if we are in multiple mode and the answer is just the empty
# string (enter/return pressed) then we will just answer None
# to indicate we are done
if answer == '.' and self.multiple:
return None
if self.validate(answer):
return self.answer()
else:
if isinstance(self.validator, list):
for v in self.validator:
if v.error() != '':
print(v.error())
else:
print(self.validator.error()) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:_annotate_fn_args; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 11; 4, identifier:stack; 5, identifier:fn_opname; 6, identifier:nargs; 7, default_parameter; 7, 8; 7, 9; 8, identifier:nkw; 9, unary_operator:-; 9, 10; 10, integer:1; 11, default_parameter; 11, 12; 11, 13; 12, identifier:consume_fn_name; 13, True; 14, block; 14, 15; 14, 17; 14, 21; 14, 151; 14, 347; 14, 461; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:kwarg_names; 20, list:[]; 21, if_statement; 21, 22; 21, 26; 22, comparison_operator:==; 22, 23; 22, 24; 23, identifier:nkw; 24, unary_operator:-; 24, 25; 25, integer:1; 26, block; 26, 27; 27, if_statement; 27, 28; 27, 35; 27, 36; 27, 51; 28, comparison_operator:<; 28, 29; 28, 34; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:sys; 32, identifier:version_info; 33, integer:0; 34, integer:3; 35, comment; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 42; 39, pattern_list; 39, 40; 39, 41; 40, identifier:nargs; 41, identifier:nkw; 42, tuple; 42, 43; 42, 46; 43, binary_operator:%; 43, 44; 43, 45; 44, identifier:nargs; 45, integer:256; 46, binary_operator://; 46, 47; 46, 50; 47, binary_operator:*; 47, 48; 47, 49; 48, integer:2; 49, identifier:nargs; 50, integer:256; 51, else_clause; 51, 52; 52, block; 52, 53; 53, if_statement; 53, 54; 53, 57; 53, 145; 54, comparison_operator:==; 54, 55; 54, 56; 55, identifier:fn_opname; 56, string:'CALL_FUNCTION_KW'; 57, block; 57, 58; 57, 77; 57, 94; 57, 102; 57, 114; 57, 121; 57, 128; 57, 132; 58, if_statement; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:qj; 61, identifier:_DEBUG_QJ; 62, block; 62, 63; 63, assert_statement; 63, 64; 64, boolean_operator:and; 64, 65; 64, 69; 65, call; 65, 66; 65, 67; 66, identifier:len; 67, argument_list; 67, 68; 68, identifier:stack; 69, comparison_operator:==; 69, 70; 69, 76; 70, attribute; 70, 71; 70, 75; 71, subscript; 71, 72; 71, 73; 72, identifier:stack; 73, unary_operator:-; 73, 74; 74, integer:1; 75, identifier:opname; 76, string:'LOAD_CONST'; 77, if_statement; 77, 78; 77, 92; 78, boolean_operator:or; 78, 79; 78, 84; 79, not_operator; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:len; 82, argument_list; 82, 83; 83, identifier:stack; 84, comparison_operator:!=; 84, 85; 84, 91; 85, attribute; 85, 86; 85, 90; 86, subscript; 86, 87; 86, 88; 87, identifier:stack; 88, unary_operator:-; 88, 89; 89, integer:1; 90, identifier:opname; 91, string:'LOAD_CONST'; 92, block; 92, 93; 93, return_statement; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:se; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:stack; 100, identifier:pop; 101, argument_list; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:kwarg_names; 105, subscript; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:se; 108, identifier:oparg_repr; 109, slice; 109, 110; 109, 111; 109, 112; 110, colon; 111, colon; 112, unary_operator:-; 112, 113; 113, integer:1; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:se; 118, identifier:oparg_repr; 119, list:['']; 119, 120; 120, string:''; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:nkw; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, identifier:kwarg_names; 128, expression_statement; 128, 129; 129, augmented_assignment:-=; 129, 130; 129, 131; 130, identifier:nargs; 131, identifier:nkw; 132, if_statement; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:qj; 135, identifier:_DEBUG_QJ; 136, block; 136, 137; 137, assert_statement; 137, 138; 138, boolean_operator:and; 138, 139; 138, 142; 139, comparison_operator:>=; 139, 140; 139, 141; 140, identifier:nargs; 141, integer:0; 142, comparison_operator:>; 142, 143; 142, 144; 143, identifier:nkw; 144, integer:0; 145, else_clause; 145, 146; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:nkw; 150, integer:0; 151, for_statement; 151, 152; 151, 153; 151, 157; 152, identifier:i; 153, call; 153, 154; 153, 155; 154, identifier:range; 155, argument_list; 155, 156; 156, identifier:nkw; 157, block; 157, 158; 157, 166; 157, 193; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:se; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:stack; 164, identifier:pop; 165, argument_list; 166, if_statement; 166, 167; 166, 190; 166, 191; 167, boolean_operator:and; 167, 168; 167, 173; 168, comparison_operator:==; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:se; 171, identifier:stack_depth; 172, integer:0; 173, parenthesized_expression; 173, 174; 174, boolean_operator:or; 174, 175; 174, 183; 175, comparison_operator:==; 175, 176; 175, 182; 176, call; 176, 177; 176, 178; 177, identifier:len; 178, argument_list; 178, 179; 179, attribute; 179, 180; 179, 181; 180, identifier:se; 181, identifier:oparg_repr; 182, integer:0; 183, comparison_operator:==; 183, 184; 183, 189; 184, subscript; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:se; 187, identifier:oparg_repr; 188, integer:0; 189, string:''; 190, comment; 191, block; 191, 192; 192, continue_statement; 193, if_statement; 193, 194; 193, 207; 193, 234; 194, boolean_operator:and; 194, 195; 194, 200; 195, comparison_operator:==; 195, 196; 195, 199; 196, binary_operator:%; 196, 197; 196, 198; 197, identifier:i; 198, integer:2; 199, integer:1; 200, comparison_operator:<; 200, 201; 200, 206; 201, subscript; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:sys; 204, identifier:version_info; 205, integer:0; 206, integer:3; 207, block; 207, 208; 207, 219; 208, if_statement; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:qj; 211, identifier:_DEBUG_QJ; 212, block; 212, 213; 213, assert_statement; 213, 214; 214, comparison_operator:==; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:se; 217, identifier:opname; 218, string:'LOAD_CONST'; 219, if_statement; 219, 220; 219, 225; 219, 226; 220, comparison_operator:==; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:se; 223, identifier:opname; 224, string:'LOAD_CONST'; 225, comment; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, augmented_assignment:+=; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:se; 231, identifier:oparg_repr; 232, list:['=']; 232, 233; 233, string:'='; 234, else_clause; 234, 235; 235, block; 235, 236; 235, 240; 235, 267; 235, 291; 235, 311; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:pops; 239, list:[]; 240, if_statement; 240, 241; 240, 249; 241, call; 241, 242; 241, 247; 242, attribute; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:se; 245, identifier:opname; 246, identifier:startswith; 247, argument_list; 247, 248; 248, string:'CALL_FUNCTION'; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:_annotate_fn_args; 253, argument_list; 253, 254; 253, 258; 253, 261; 253, 264; 253, 266; 254, subscript; 254, 255; 254, 256; 255, identifier:stack; 256, slice; 256, 257; 257, colon; 258, attribute; 258, 259; 258, 260; 259, identifier:se; 260, identifier:opname; 261, attribute; 261, 262; 261, 263; 262, identifier:se; 263, identifier:oparg; 264, unary_operator:-; 264, 265; 265, integer:1; 266, True; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:pops; 270, call; 270, 271; 270, 272; 271, identifier:_collect_pops; 272, argument_list; 272, 273; 272, 274; 272, 289; 272, 290; 273, identifier:stack; 274, conditional_expression:if; 274, 275; 274, 280; 274, 288; 275, binary_operator:-; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:se; 278, identifier:stack_depth; 279, integer:1; 280, call; 280, 281; 280, 286; 281, attribute; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:se; 284, identifier:opname; 285, identifier:startswith; 286, argument_list; 286, 287; 287, string:'CALL_FUNCTION'; 288, integer:0; 289, list:[]; 290, False; 291, if_statement; 291, 292; 291, 300; 292, boolean_operator:and; 292, 293; 292, 296; 293, comparison_operator:>; 293, 294; 293, 295; 294, identifier:i; 295, integer:1; 296, call; 296, 297; 296, 298; 297, identifier:len; 298, argument_list; 298, 299; 299, identifier:pops; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, augmented_assignment:+=; 302, 303; 302, 309; 303, attribute; 303, 304; 303, 308; 304, subscript; 304, 305; 304, 306; 305, identifier:pops; 306, unary_operator:-; 306, 307; 307, integer:1; 308, identifier:oparg_repr; 309, list:[',']; 309, 310; 310, string:','; 311, if_statement; 311, 312; 311, 319; 312, comparison_operator:>=; 312, 313; 312, 318; 313, subscript; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:sys; 316, identifier:version_info; 317, integer:0; 318, integer:3; 319, block; 319, 320; 319, 333; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 323; 322, identifier:target_se; 323, conditional_expression:if; 323, 324; 323, 328; 323, 332; 324, subscript; 324, 325; 324, 326; 325, identifier:pops; 326, unary_operator:-; 326, 327; 327, integer:1; 328, call; 328, 329; 328, 330; 329, identifier:len; 330, argument_list; 330, 331; 331, identifier:pops; 332, identifier:se; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:target_se; 337, identifier:oparg_repr; 338, binary_operator:+; 338, 339; 338, 344; 339, list:[kwarg_names[i], '=']; 339, 340; 339, 343; 340, subscript; 340, 341; 340, 342; 341, identifier:kwarg_names; 342, identifier:i; 343, string:'='; 344, attribute; 344, 345; 344, 346; 345, identifier:target_se; 346, identifier:oparg_repr; 347, for_statement; 347, 348; 347, 349; 347, 353; 348, identifier:i; 349, call; 349, 350; 349, 351; 350, identifier:range; 351, argument_list; 351, 352; 352, identifier:nargs; 353, block; 353, 354; 353, 362; 354, expression_statement; 354, 355; 355, assignment; 355, 356; 355, 357; 356, identifier:se; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:stack; 360, identifier:pop; 361, argument_list; 362, if_statement; 362, 363; 362, 371; 362, 386; 362, 422; 363, call; 363, 364; 363, 369; 364, attribute; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:se; 367, identifier:opname; 368, identifier:startswith; 369, argument_list; 369, 370; 370, string:'CALL_FUNCTION'; 371, block; 371, 372; 372, expression_statement; 372, 373; 373, call; 373, 374; 373, 375; 374, identifier:_annotate_fn_args; 375, argument_list; 375, 376; 375, 377; 375, 380; 375, 383; 375, 385; 376, identifier:stack; 377, attribute; 377, 378; 377, 379; 378, identifier:se; 379, identifier:opname; 380, attribute; 380, 381; 380, 382; 381, identifier:se; 382, identifier:oparg; 383, unary_operator:-; 383, 384; 384, integer:1; 385, True; 386, elif_clause; 386, 387; 386, 404; 387, boolean_operator:and; 387, 388; 387, 394; 388, call; 388, 389; 388, 390; 389, identifier:len; 390, argument_list; 390, 391; 391, attribute; 391, 392; 391, 393; 392, identifier:se; 393, identifier:oparg_repr; 394, comparison_operator:in; 394, 395; 394, 400; 395, subscript; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:se; 398, identifier:oparg_repr; 399, integer:0; 400, set; 400, 401; 400, 402; 400, 403; 401, string:']'; 402, string:'}'; 403, string:')'; 404, block; 404, 405; 405, if_statement; 405, 406; 405, 414; 406, parenthesized_expression; 406, 407; 407, boolean_operator:or; 407, 408; 407, 411; 408, comparison_operator:>; 408, 409; 408, 410; 409, identifier:i; 410, integer:0; 411, comparison_operator:>; 411, 412; 411, 413; 412, identifier:nkw; 413, integer:0; 414, block; 414, 415; 415, expression_statement; 415, 416; 416, augmented_assignment:+=; 416, 417; 416, 420; 417, attribute; 417, 418; 417, 419; 418, identifier:se; 419, identifier:oparg_repr; 420, list:[',']; 420, 421; 421, string:','; 422, else_clause; 422, 423; 423, block; 423, 424; 423, 436; 424, expression_statement; 424, 425; 425, assignment; 425, 426; 425, 427; 426, identifier:pops; 427, call; 427, 428; 427, 429; 428, identifier:_collect_pops; 429, argument_list; 429, 430; 429, 431; 429, 434; 429, 435; 430, identifier:stack; 431, attribute; 431, 432; 431, 433; 432, identifier:se; 433, identifier:stack_depth; 434, list:[]; 435, False; 436, if_statement; 436, 437; 436, 450; 437, boolean_operator:and; 437, 438; 437, 446; 438, parenthesized_expression; 438, 439; 439, boolean_operator:or; 439, 440; 439, 443; 440, comparison_operator:>; 440, 441; 440, 442; 441, identifier:i; 442, integer:0; 443, comparison_operator:>; 443, 444; 443, 445; 444, identifier:nkw; 445, integer:0; 446, call; 446, 447; 446, 448; 447, identifier:len; 448, argument_list; 448, 449; 449, identifier:pops; 450, block; 450, 451; 451, expression_statement; 451, 452; 452, augmented_assignment:+=; 452, 453; 452, 459; 453, attribute; 453, 454; 453, 458; 454, subscript; 454, 455; 454, 456; 455, identifier:pops; 456, unary_operator:-; 456, 457; 457, integer:1; 458, identifier:oparg_repr; 459, list:[',']; 459, 460; 460, string:','; 461, if_statement; 461, 462; 461, 463; 462, identifier:consume_fn_name; 463, block; 463, 464; 464, expression_statement; 464, 465; 465, call; 465, 466; 465, 467; 466, identifier:_collect_pops; 467, argument_list; 467, 468; 467, 469; 467, 471; 467, 472; 468, identifier:stack; 469, unary_operator:-; 469, 470; 470, integer:1; 471, list:[]; 472, False | def _annotate_fn_args(stack, fn_opname, nargs, nkw=-1, consume_fn_name=True):
"""Add commas and equals as appropriate to function argument lists in the stack."""
kwarg_names = []
if nkw == -1:
if sys.version_info[0] < 3:
# Compute nkw and nargs from nargs for python 2.7
nargs, nkw = (nargs % 256, 2 * nargs // 256)
else:
if fn_opname == 'CALL_FUNCTION_KW':
if qj._DEBUG_QJ:
assert len(stack) and stack[-1].opname == 'LOAD_CONST'
if not len(stack) or stack[-1].opname != 'LOAD_CONST':
return
se = stack.pop()
kwarg_names = se.oparg_repr[::-1]
se.oparg_repr = ['']
nkw = len(kwarg_names)
nargs -= nkw
if qj._DEBUG_QJ:
assert nargs >= 0 and nkw > 0
else:
nkw = 0
for i in range(nkw):
se = stack.pop()
if se.stack_depth == 0 and (len(se.oparg_repr) == 0 or se.oparg_repr[0] == ''):
# Skip stack entries that don't have any effect on the stack
continue
if i % 2 == 1 and sys.version_info[0] < 3:
if qj._DEBUG_QJ:
assert se.opname == 'LOAD_CONST'
if se.opname == 'LOAD_CONST':
# kwargs are pairs of key=value in code
se.oparg_repr += ['=']
else:
pops = []
if se.opname.startswith('CALL_FUNCTION'):
_annotate_fn_args(stack[:], se.opname, se.oparg, -1, True)
pops = _collect_pops(stack, se.stack_depth - 1 if se.opname.startswith('CALL_FUNCTION') else 0, [], False)
if i > 1 and len(pops):
pops[-1].oparg_repr += [',']
if sys.version_info[0] >= 3:
target_se = pops[-1] if len(pops) else se
target_se.oparg_repr = [kwarg_names[i], '='] + target_se.oparg_repr
for i in range(nargs):
se = stack.pop()
if se.opname.startswith('CALL_FUNCTION'):
_annotate_fn_args(stack, se.opname, se.oparg, -1, True)
elif len(se.oparg_repr) and se.oparg_repr[0] in {']', '}', ')'}:
if (i > 0 or nkw > 0):
se.oparg_repr += [',']
else:
pops = _collect_pops(stack, se.stack_depth, [], False)
if (i > 0 or nkw > 0) and len(pops):
pops[-1].oparg_repr += [',']
if consume_fn_name:
_collect_pops(stack, -1, [], False) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:generate_mediation_matrix; 3, parameters; 3, 4; 4, identifier:dsm; 5, block; 5, 6; 5, 8; 5, 14; 5, 20; 5, 28; 5, 39; 5, 54; 5, 55; 5, 73; 5, 394; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:cat; 11, attribute; 11, 12; 11, 13; 12, identifier:dsm; 13, identifier:categories; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:ent; 17, attribute; 17, 18; 17, 19; 18, identifier:dsm; 19, identifier:entities; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:size; 23, subscript; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:dsm; 26, identifier:size; 27, integer:0; 28, if_statement; 28, 29; 28, 31; 29, not_operator; 29, 30; 30, identifier:cat; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:cat; 35, binary_operator:*; 35, 36; 35, 38; 36, list:['appmodule']; 36, 37; 37, string:'appmodule'; 38, identifier:size; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:packages; 42, list_comprehension; 42, 43; 42, 51; 43, subscript; 43, 44; 43, 50; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:e; 47, identifier:split; 48, argument_list; 48, 49; 49, string:'.'; 50, integer:0; 51, for_in_clause; 51, 52; 51, 53; 52, identifier:e; 53, identifier:ent; 54, comment; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:mediation_matrix; 58, list_comprehension; 58, 59; 58, 67; 59, list_comprehension; 59, 60; 59, 61; 60, integer:0; 61, for_in_clause; 61, 62; 61, 63; 62, identifier:_; 63, call; 63, 64; 63, 65; 64, identifier:range; 65, argument_list; 65, 66; 66, identifier:size; 67, for_in_clause; 67, 68; 67, 69; 68, identifier:_; 69, call; 69, 70; 69, 71; 70, identifier:range; 71, argument_list; 71, 72; 72, identifier:size; 73, for_statement; 73, 74; 73, 75; 73, 80; 74, identifier:i; 75, call; 75, 76; 75, 77; 76, identifier:range; 77, argument_list; 77, 78; 77, 79; 78, integer:0; 79, identifier:size; 80, block; 80, 81; 81, for_statement; 81, 82; 81, 83; 81, 88; 82, identifier:j; 83, call; 83, 84; 83, 85; 84, identifier:range; 85, argument_list; 85, 86; 85, 87; 86, integer:0; 87, identifier:size; 88, block; 88, 89; 89, if_statement; 89, 90; 89, 95; 89, 122; 89, 175; 89, 229; 89, 287; 89, 344; 89, 382; 90, comparison_operator:==; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:cat; 93, identifier:i; 94, string:'framework'; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 102; 96, 112; 97, comparison_operator:==; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:cat; 100, identifier:j; 101, string:'framework'; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 110; 105, subscript; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:mediation_matrix; 108, identifier:i; 109, identifier:j; 110, unary_operator:-; 110, 111; 111, integer:1; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 121; 116, subscript; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:mediation_matrix; 119, identifier:i; 120, identifier:j; 121, integer:0; 122, elif_clause; 122, 123; 122, 128; 123, comparison_operator:==; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:cat; 126, identifier:i; 127, string:'corelib'; 128, block; 128, 129; 129, if_statement; 129, 130; 129, 155; 129, 165; 130, parenthesized_expression; 130, 131; 131, boolean_operator:or; 131, 132; 131, 152; 132, boolean_operator:or; 132, 133; 132, 140; 133, comparison_operator:in; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:cat; 136, identifier:j; 137, tuple; 137, 138; 137, 139; 138, string:'framework'; 139, string:'corelib'; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:ent; 144, identifier:i; 145, identifier:startswith; 146, argument_list; 146, 147; 147, binary_operator:+; 147, 148; 147, 151; 148, subscript; 148, 149; 148, 150; 149, identifier:packages; 150, identifier:j; 151, string:'.'; 152, comparison_operator:==; 152, 153; 152, 154; 153, identifier:i; 154, identifier:j; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 163; 158, subscript; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:mediation_matrix; 161, identifier:i; 162, identifier:j; 163, unary_operator:-; 163, 164; 164, integer:1; 165, else_clause; 165, 166; 166, block; 166, 167; 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:mediation_matrix; 172, identifier:i; 173, identifier:j; 174, integer:0; 175, elif_clause; 175, 176; 175, 181; 176, comparison_operator:==; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:cat; 179, identifier:i; 180, string:'applib'; 181, block; 181, 182; 182, if_statement; 182, 183; 182, 209; 182, 219; 183, parenthesized_expression; 183, 184; 184, boolean_operator:or; 184, 185; 184, 206; 185, boolean_operator:or; 185, 186; 185, 194; 186, comparison_operator:in; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:cat; 189, identifier:j; 190, tuple; 190, 191; 190, 192; 190, 193; 191, string:'framework'; 192, string:'corelib'; 193, string:'applib'; 194, call; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, subscript; 196, 197; 196, 198; 197, identifier:ent; 198, identifier:i; 199, identifier:startswith; 200, argument_list; 200, 201; 201, binary_operator:+; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:packages; 204, identifier:j; 205, string:'.'; 206, comparison_operator:==; 206, 207; 206, 208; 207, identifier:i; 208, identifier:j; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 217; 212, subscript; 212, 213; 212, 216; 213, subscript; 213, 214; 213, 215; 214, identifier:mediation_matrix; 215, identifier:i; 216, identifier:j; 217, unary_operator:-; 217, 218; 218, integer:1; 219, else_clause; 219, 220; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 228; 223, subscript; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:mediation_matrix; 226, identifier:i; 227, identifier:j; 228, integer:0; 229, elif_clause; 229, 230; 229, 235; 229, 236; 229, 237; 230, comparison_operator:==; 230, 231; 230, 234; 231, subscript; 231, 232; 231, 233; 232, identifier:cat; 233, identifier:i; 234, string:'appmodule'; 235, comment; 236, comment; 237, block; 237, 238; 238, if_statement; 238, 239; 238, 267; 238, 277; 239, parenthesized_expression; 239, 240; 240, boolean_operator:or; 240, 241; 240, 264; 241, boolean_operator:or; 241, 242; 241, 252; 242, comparison_operator:in; 242, 243; 242, 246; 243, subscript; 243, 244; 243, 245; 244, identifier:cat; 245, identifier:j; 246, tuple; 246, 247; 246, 248; 246, 249; 246, 250; 246, 251; 247, string:'framework'; 248, string:'corelib'; 249, string:'applib'; 250, string:'broker'; 251, string:'data'; 252, call; 252, 253; 252, 258; 253, attribute; 253, 254; 253, 257; 254, subscript; 254, 255; 254, 256; 255, identifier:ent; 256, identifier:i; 257, identifier:startswith; 258, argument_list; 258, 259; 259, binary_operator:+; 259, 260; 259, 263; 260, subscript; 260, 261; 260, 262; 261, identifier:packages; 262, identifier:j; 263, string:'.'; 264, comparison_operator:==; 264, 265; 264, 266; 265, identifier:i; 266, identifier:j; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 275; 270, subscript; 270, 271; 270, 274; 271, subscript; 271, 272; 271, 273; 272, identifier:mediation_matrix; 273, identifier:i; 274, identifier:j; 275, unary_operator:-; 275, 276; 276, integer:1; 277, else_clause; 277, 278; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 286; 281, subscript; 281, 282; 281, 285; 282, subscript; 282, 283; 282, 284; 283, identifier:mediation_matrix; 284, identifier:i; 285, identifier:j; 286, integer:0; 287, elif_clause; 287, 288; 287, 293; 287, 294; 287, 295; 287, 296; 288, comparison_operator:==; 288, 289; 288, 292; 289, subscript; 289, 290; 289, 291; 290, identifier:cat; 291, identifier:i; 292, string:'broker'; 293, comment; 294, comment; 295, comment; 296, block; 296, 297; 297, if_statement; 297, 298; 297, 324; 297, 334; 298, parenthesized_expression; 298, 299; 299, boolean_operator:or; 299, 300; 299, 321; 300, boolean_operator:or; 300, 301; 300, 309; 301, comparison_operator:in; 301, 302; 301, 305; 302, subscript; 302, 303; 302, 304; 303, identifier:cat; 304, identifier:j; 305, tuple; 305, 306; 305, 307; 305, 308; 306, string:'appmodule'; 307, string:'corelib'; 308, string:'framework'; 309, call; 309, 310; 309, 315; 310, attribute; 310, 311; 310, 314; 311, subscript; 311, 312; 311, 313; 312, identifier:ent; 313, identifier:i; 314, identifier:startswith; 315, argument_list; 315, 316; 316, binary_operator:+; 316, 317; 316, 320; 317, subscript; 317, 318; 317, 319; 318, identifier:packages; 319, identifier:j; 320, string:'.'; 321, comparison_operator:==; 321, 322; 321, 323; 322, identifier:i; 323, identifier:j; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 332; 327, subscript; 327, 328; 327, 331; 328, subscript; 328, 329; 328, 330; 329, identifier:mediation_matrix; 330, identifier:i; 331, identifier:j; 332, unary_operator:-; 332, 333; 333, integer:1; 334, else_clause; 334, 335; 335, block; 335, 336; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 343; 338, subscript; 338, 339; 338, 342; 339, subscript; 339, 340; 339, 341; 340, identifier:mediation_matrix; 341, identifier:i; 342, identifier:j; 343, integer:0; 344, elif_clause; 344, 345; 344, 350; 345, comparison_operator:==; 345, 346; 345, 349; 346, subscript; 346, 347; 346, 348; 347, identifier:cat; 348, identifier:i; 349, string:'data'; 350, block; 350, 351; 351, if_statement; 351, 352; 351, 362; 351, 372; 352, parenthesized_expression; 352, 353; 353, boolean_operator:or; 353, 354; 353, 359; 354, comparison_operator:==; 354, 355; 354, 358; 355, subscript; 355, 356; 355, 357; 356, identifier:cat; 357, identifier:j; 358, string:'framework'; 359, comparison_operator:==; 359, 360; 359, 361; 360, identifier:i; 361, identifier:j; 362, block; 362, 363; 363, expression_statement; 363, 364; 364, assignment; 364, 365; 364, 370; 365, subscript; 365, 366; 365, 369; 366, subscript; 366, 367; 366, 368; 367, identifier:mediation_matrix; 368, identifier:i; 369, identifier:j; 370, unary_operator:-; 370, 371; 371, integer:1; 372, else_clause; 372, 373; 373, block; 373, 374; 374, expression_statement; 374, 375; 375, assignment; 375, 376; 375, 381; 376, subscript; 376, 377; 376, 380; 377, subscript; 377, 378; 377, 379; 378, identifier:mediation_matrix; 379, identifier:i; 380, identifier:j; 381, integer:0; 382, else_clause; 382, 383; 382, 384; 383, comment; 384, block; 384, 385; 385, raise_statement; 385, 386; 386, call; 386, 387; 386, 388; 387, identifier:DesignStructureMatrixError; 388, argument_list; 388, 389; 389, binary_operator:%; 389, 390; 389, 391; 390, string:'Mediation matrix value NOT generated for %s:%s'; 391, tuple; 391, 392; 391, 393; 392, identifier:i; 393, identifier:j; 394, return_statement; 394, 395; 395, identifier:mediation_matrix | def generate_mediation_matrix(dsm):
"""
Generate the mediation matrix of the given matrix.
Rules for mediation matrix generation:
Set -1 for items NOT to be considered
Set 0 for items which MUST NOT be present
Set 1 for items which MUST be present
Each module has optional dependencies to itself.
- Framework has optional dependency to all framework items (-1),
and to nothing else.
- Core libraries have dependencies to framework.
Dependencies to other core libraries are tolerated.
- Application libraries have dependencies to framework.
Dependencies to other core or application libraries are tolerated.
No dependencies to application modules.
- Application modules have dependencies to framework and libraries.
Dependencies to other application modules
should be mediated over a broker.
Dependencies to data are tolerated.
- Data have no dependencies at all
(but framework/libraries would be tolerated).
Args:
dsm (:class:`DesignStructureMatrix`): the DSM to generate
the mediation matrix for.
"""
cat = dsm.categories
ent = dsm.entities
size = dsm.size[0]
if not cat:
cat = ['appmodule'] * size
packages = [e.split('.')[0] for e in ent]
# define and initialize the mediation matrix
mediation_matrix = [[0 for _ in range(size)]
for _ in range(size)]
for i in range(0, size):
for j in range(0, size):
if cat[i] == 'framework':
if cat[j] == 'framework':
mediation_matrix[i][j] = -1
else:
mediation_matrix[i][j] = 0
elif cat[i] == 'corelib':
if (cat[j] in ('framework', 'corelib') or
ent[i].startswith(packages[j] + '.') or
i == j):
mediation_matrix[i][j] = -1
else:
mediation_matrix[i][j] = 0
elif cat[i] == 'applib':
if (cat[j] in ('framework', 'corelib', 'applib') or
ent[i].startswith(packages[j] + '.') or
i == j):
mediation_matrix[i][j] = -1
else:
mediation_matrix[i][j] = 0
elif cat[i] == 'appmodule':
# we cannot force an app module to import things from
# the broker if the broker itself did not import anything
if (cat[j] in ('framework', 'corelib',
'applib', 'broker', 'data') or
ent[i].startswith(packages[j] + '.') or
i == j):
mediation_matrix[i][j] = -1
else:
mediation_matrix[i][j] = 0
elif cat[i] == 'broker':
# we cannot force the broker to import things from
# app modules if there is nothing to be imported.
# also broker should be authorized to use third apps
if (cat[j] in (
'appmodule', 'corelib', 'framework') or
ent[i].startswith(packages[j] + '.') or
i == j):
mediation_matrix[i][j] = -1
else:
mediation_matrix[i][j] = 0
elif cat[i] == 'data':
if (cat[j] == 'framework' or
i == j):
mediation_matrix[i][j] = -1
else:
mediation_matrix[i][j] = 0
else:
# mediation_matrix[i][j] = -2 # errors in the generation
raise DesignStructureMatrixError(
'Mediation matrix value NOT generated for %s:%s' % (
i, j))
return mediation_matrix |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:check; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:dsm; 6, default_parameter; 6, 7; 6, 8; 7, identifier:independence_factor; 8, integer:5; 9, dictionary_splat_pattern; 9, 10; 10, identifier:kwargs; 11, block; 11, 12; 11, 14; 11, 15; 11, 19; 11, 23; 11, 24; 11, 30; 11, 36; 11, 44; 11, 55; 11, 59; 11, 60; 11, 111; 11, 112; 11, 113; 11, 114; 11, 115; 11, 141; 11, 189; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:least_common_mechanism; 18, False; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:message; 22, string:''; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:data; 27, attribute; 27, 28; 27, 29; 28, identifier:dsm; 29, identifier:data; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:categories; 33, attribute; 33, 34; 33, 35; 34, identifier:dsm; 35, identifier:categories; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:dsm_size; 39, subscript; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:dsm; 42, identifier:size; 43, integer:0; 44, if_statement; 44, 45; 44, 47; 45, not_operator; 45, 46; 46, identifier:categories; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:categories; 51, binary_operator:*; 51, 52; 51, 54; 52, list:['appmodule']; 52, 53; 53, string:'appmodule'; 54, identifier:dsm_size; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:dependent_module_number; 58, list:[]; 59, comment; 60, for_statement; 60, 61; 60, 62; 60, 67; 61, identifier:j; 62, call; 62, 63; 62, 64; 63, identifier:range; 64, argument_list; 64, 65; 64, 66; 65, integer:0; 66, identifier:dsm_size; 67, block; 67, 68; 67, 75; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:dependent_module_number; 72, identifier:append; 73, argument_list; 73, 74; 74, integer:0; 75, for_statement; 75, 76; 75, 77; 75, 82; 76, identifier:i; 77, call; 77, 78; 77, 79; 78, identifier:range; 79, argument_list; 79, 80; 79, 81; 80, integer:0; 81, identifier:dsm_size; 82, block; 82, 83; 83, if_statement; 83, 84; 83, 104; 84, parenthesized_expression; 84, 85; 85, boolean_operator:and; 85, 86; 85, 97; 86, boolean_operator:and; 86, 87; 86, 92; 87, comparison_operator:!=; 87, 88; 87, 91; 88, subscript; 88, 89; 88, 90; 89, identifier:categories; 90, identifier:i; 91, string:'framework'; 92, comparison_operator:!=; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:categories; 95, identifier:j; 96, string:'framework'; 97, comparison_operator:>; 97, 98; 97, 103; 98, subscript; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:data; 101, identifier:i; 102, identifier:j; 103, integer:0; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, augmented_assignment:+=; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:dependent_module_number; 109, identifier:j; 110, integer:1; 111, comment; 112, comment; 113, comment; 114, comment; 115, for_statement; 115, 116; 115, 119; 115, 125; 116, pattern_list; 116, 117; 116, 118; 117, identifier:index; 118, identifier:item; 119, call; 119, 120; 119, 121; 120, identifier:enumerate; 121, argument_list; 121, 122; 122, attribute; 122, 123; 122, 124; 123, identifier:dsm; 124, identifier:categories; 125, block; 125, 126; 126, if_statement; 126, 127; 126, 134; 127, boolean_operator:or; 127, 128; 127, 131; 128, comparison_operator:==; 128, 129; 128, 130; 129, identifier:item; 130, string:'broker'; 131, comparison_operator:==; 131, 132; 131, 133; 132, identifier:item; 133, string:'applib'; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:dependent_module_number; 139, identifier:index; 140, integer:0; 141, if_statement; 141, 142; 141, 150; 141, 155; 142, comparison_operator:<=; 142, 143; 142, 147; 143, call; 143, 144; 143, 145; 144, identifier:max; 145, argument_list; 145, 146; 146, identifier:dependent_module_number; 147, binary_operator:/; 147, 148; 147, 149; 148, identifier:dsm_size; 149, identifier:independence_factor; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:least_common_mechanism; 154, True; 155, else_clause; 155, 156; 156, block; 156, 157; 156, 164; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:maximum; 160, call; 160, 161; 160, 162; 161, identifier:max; 162, argument_list; 162, 163; 163, identifier:dependent_module_number; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:message; 167, parenthesized_expression; 167, 168; 168, binary_operator:%; 168, 169; 168, 172; 169, concatenated_string; 169, 170; 169, 171; 170, string:'Dependencies to %s (%s) > matrix size (%s) / '; 171, string:'independence factor (%s) = %s'; 172, tuple; 172, 173; 172, 183; 172, 184; 172, 185; 172, 186; 173, subscript; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:dsm; 176, identifier:entities; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:dependent_module_number; 180, identifier:index; 181, argument_list; 181, 182; 182, identifier:maximum; 183, identifier:maximum; 184, identifier:dsm_size; 185, identifier:independence_factor; 186, binary_operator:/; 186, 187; 186, 188; 187, identifier:dsm_size; 188, identifier:independence_factor; 189, return_statement; 189, 190; 190, expression_list; 190, 191; 190, 192; 191, identifier:least_common_mechanism; 192, identifier:message | def check(self, dsm, independence_factor=5, **kwargs):
"""
Check least common mechanism.
Args:
dsm (:class:`DesignStructureMatrix`): the DSM to check.
independence_factor (int): if the maximum dependencies for one
module is inferior or equal to the DSM size divided by the
independence factor, then this criterion is verified.
Returns:
bool: True if least common mechanism, else False
"""
# leastCommonMechanismMatrix
least_common_mechanism = False
message = ''
# get the list of dependent modules for each module
data = dsm.data
categories = dsm.categories
dsm_size = dsm.size[0]
if not categories:
categories = ['appmodule'] * dsm_size
dependent_module_number = []
# evaluate Matrix(data)
for j in range(0, dsm_size):
dependent_module_number.append(0)
for i in range(0, dsm_size):
if (categories[i] != 'framework' and
categories[j] != 'framework' and
data[i][j] > 0):
dependent_module_number[j] += 1
# except for the broker if any and libs, check that threshold is not
# overlapped
# index of brokers
# and app_libs are set to 0
for index, item in enumerate(dsm.categories):
if item == 'broker' or item == 'applib':
dependent_module_number[index] = 0
if max(dependent_module_number) <= dsm_size / independence_factor:
least_common_mechanism = True
else:
maximum = max(dependent_module_number)
message = (
'Dependencies to %s (%s) > matrix size (%s) / '
'independence factor (%s) = %s' % (
dsm.entities[dependent_module_number.index(maximum)],
maximum, dsm_size, independence_factor,
dsm_size / independence_factor))
return least_common_mechanism, message |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:abstract; 3, parameters; 3, 4; 4, identifier:class_; 5, block; 5, 6; 5, 8; 5, 22; 5, 26; 5, 27; 5, 28; 5, 35; 5, 71; 5, 77; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 16; 9, not_operator; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:inspect; 13, identifier:isclass; 14, argument_list; 14, 15; 15, identifier:class_; 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:"@abstract can only be applied to classes"; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:abc_meta; 25, None; 26, comment; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:class_meta; 31, call; 31, 32; 31, 33; 32, identifier:type; 33, argument_list; 33, 34; 34, identifier:class_; 35, if_statement; 35, 36; 35, 41; 35, 42; 35, 43; 36, comparison_operator:not; 36, 37; 36, 38; 37, identifier:class_meta; 38, tuple; 38, 39; 38, 40; 39, identifier:_ABCMetaclass; 40, identifier:_ABCObjectMetaclass; 41, comment; 42, comment; 43, block; 43, 44; 44, if_statement; 44, 45; 44, 48; 44, 54; 44, 64; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:class_meta; 47, identifier:type; 48, block; 48, 49; 48, 53; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:abc_meta; 52, identifier:_ABCMetaclass; 53, comment; 54, elif_clause; 54, 55; 54, 58; 55, comparison_operator:is; 55, 56; 55, 57; 56, identifier:class_meta; 57, identifier:ObjectMetaclass; 58, block; 58, 59; 58, 63; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:abc_meta; 62, identifier:_ABCObjectMetaclass; 63, comment; 64, else_clause; 64, 65; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:ValueError; 69, argument_list; 69, 70; 70, string:"@abstract cannot be applied to classes with custom metaclass"; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:class_; 75, identifier:__abstract__; 76, True; 77, return_statement; 77, 78; 78, conditional_expression:if; 78, 79; 78, 86; 78, 87; 79, call; 79, 80; 79, 84; 80, call; 80, 81; 80, 82; 81, identifier:metaclass; 82, argument_list; 82, 83; 83, identifier:abc_meta; 84, argument_list; 84, 85; 85, identifier:class_; 86, identifier:abc_meta; 87, identifier:class_ | def abstract(class_):
"""Mark the class as _abstract_ base class, forbidding its instantiation.
.. note::
Unlike other modifiers, ``@abstract`` can be applied
to all Python classes, not just subclasses of :class:`Object`.
.. versionadded:: 0.0.3
"""
if not inspect.isclass(class_):
raise TypeError("@abstract can only be applied to classes")
abc_meta = None
# if the class is not already using a metaclass specific to ABC,
# we need to change that
class_meta = type(class_)
if class_meta not in (_ABCMetaclass, _ABCObjectMetaclass):
# decide what metaclass to use, depending on whether it's a subclass
# of our universal :class:`Object` or not
if class_meta is type:
abc_meta = _ABCMetaclass # like ABCMeta, but can never instantiate
elif class_meta is ObjectMetaclass:
abc_meta = _ABCObjectMetaclass # ABCMeta mixed with ObjectMetaclass
else:
raise ValueError(
"@abstract cannot be applied to classes with custom metaclass")
class_.__abstract__ = True
return metaclass(abc_meta)(class_) if abc_meta else class_ |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:final; 3, parameters; 3, 4; 4, identifier:arg; 5, block; 5, 6; 5, 8; 5, 43; 5, 56; 5, 62; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 15; 8, 31; 9, call; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:inspect; 12, identifier:isclass; 13, argument_list; 13, 14; 14, identifier:arg; 15, block; 15, 16; 16, if_statement; 16, 17; 16, 23; 17, not_operator; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:isinstance; 20, argument_list; 20, 21; 20, 22; 21, identifier:arg; 22, identifier:ObjectMetaclass; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:ValueError; 27, argument_list; 27, 28; 28, concatenated_string; 28, 29; 28, 30; 29, string:"@final can only be applied to a class "; 30, string:"that is a subclass of Object"; 31, elif_clause; 31, 32; 31, 37; 32, not_operator; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:is_method; 35, argument_list; 35, 36; 36, identifier:arg; 37, block; 37, 38; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:TypeError; 41, argument_list; 41, 42; 42, string:"@final can only be applied to classes or methods"; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:method; 46, conditional_expression:if; 46, 47; 46, 50; 46, 55; 47, attribute; 47, 48; 47, 49; 48, identifier:arg; 49, identifier:method; 50, call; 50, 51; 50, 52; 51, identifier:isinstance; 52, argument_list; 52, 53; 52, 54; 53, identifier:arg; 54, identifier:_WrappedMethod; 55, identifier:arg; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:method; 60, identifier:__final__; 61, True; 62, return_statement; 62, 63; 63, identifier:arg | def final(arg):
"""Mark a class or method as _final_.
Final classes are those that end the inheritance chain, i.e. forbid
further subclassing. A final class can thus be only instantiated,
not inherited from.
Similarly, methods marked as final in a superclass cannot be overridden
in any of the subclasses.
.. note::
Final method itself can also be (in fact, it usually is) an overridden
method from a superclass. In those cases, it's recommended to place
@\ :func:`final` modifier before @\ :func:`override` for clarity::
class Foo(Base):
@final
@override
def florb(self):
super(Foo, self).florb()
# ...
.. versionadded:: 0.0.3
Now applicable to methods in addition to classes
"""
if inspect.isclass(arg):
if not isinstance(arg, ObjectMetaclass):
raise ValueError("@final can only be applied to a class "
"that is a subclass of Object")
elif not is_method(arg):
raise TypeError("@final can only be applied to classes or methods")
method = arg.method if isinstance(arg, _WrappedMethod) else arg
method.__final__ = True
return arg |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:override; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:base; 6, identifier:ABSENT; 7, block; 7, 8; 7, 10; 7, 14; 7, 15; 7, 16; 7, 49; 7, 50; 7, 60; 7, 61; 7, 77; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:arg; 13, identifier:base; 14, comment; 15, comment; 16, if_statement; 16, 17; 16, 29; 17, boolean_operator:or; 17, 18; 17, 24; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:inspect; 21, identifier:isfunction; 22, argument_list; 22, 23; 23, identifier:arg; 24, call; 24, 25; 24, 26; 25, identifier:isinstance; 26, argument_list; 26, 27; 26, 28; 27, identifier:arg; 28, identifier:NonInstanceMethod; 29, block; 29, 30; 29, 37; 29, 44; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:_OverrideDecorator; 34, identifier:maybe_signal_classmethod; 35, argument_list; 35, 36; 36, identifier:arg; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:decorator; 40, call; 40, 41; 40, 42; 41, identifier:_OverrideDecorator; 42, argument_list; 42, 43; 43, None; 44, return_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:decorator; 47, argument_list; 47, 48; 48, identifier:arg; 49, comment; 50, if_statement; 50, 51; 50, 54; 51, comparison_operator:is; 51, 52; 51, 53; 52, identifier:arg; 53, identifier:ABSENT; 54, block; 54, 55; 55, return_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:_OverrideDecorator; 58, argument_list; 58, 59; 59, None; 60, comment; 61, if_statement; 61, 62; 61, 71; 62, boolean_operator:or; 62, 63; 62, 67; 63, call; 63, 64; 63, 65; 64, identifier:is_class; 65, argument_list; 65, 66; 66, identifier:arg; 67, call; 67, 68; 67, 69; 68, identifier:is_string; 69, argument_list; 69, 70; 70, identifier:arg; 71, block; 71, 72; 72, return_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:_OverrideDecorator; 75, argument_list; 75, 76; 76, identifier:arg; 77, raise_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:TypeError; 80, argument_list; 80, 81; 81, concatenated_string; 81, 82; 81, 83; 82, string:"explicit base class for @override "; 83, string:"must be either a string or a class object" | def override(base=ABSENT):
"""Mark a method as overriding a corresponding method from superclass.
:param base:
Optional base class from which this method is being overridden.
If provided, it can be a class itself, or its (qualified) name.
.. note::
When overriding a :class:`classmethod`, remember to place ``@override``
above the ``@classmethod`` decorator::
class Foo(Bar):
@override
@classmethod
def florb(cls):
pass
"""
arg = base # ``base`` is just for clean, user-facing argument name
# direct application of the modifier through ``@override``
if inspect.isfunction(arg) or isinstance(arg, NonInstanceMethod):
_OverrideDecorator.maybe_signal_classmethod(arg)
decorator = _OverrideDecorator(None)
return decorator(arg)
# indirect (but simple) application of the modifier through ``@override()``
if arg is ABSENT:
return _OverrideDecorator(None)
# full-blown application, with base class specified
if is_class(arg) or is_string(arg):
return _OverrideDecorator(arg)
raise TypeError("explicit base class for @override "
"must be either a string or a class object") |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_index; 3, parameters; 3, 4; 3, 6; 4, list_splat_pattern; 4, 5; 5, identifier:args; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 21; 8, 31; 8, 145; 8, 152; 8, 166; 8, 170; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:start; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:kwargs; 17, identifier:pop; 18, argument_list; 18, 19; 18, 20; 19, string:'start'; 20, integer:0; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:step; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:kwargs; 27, identifier:pop; 28, argument_list; 28, 29; 28, 30; 29, string:'step'; 30, integer:1; 31, if_statement; 31, 32; 31, 38; 31, 59; 32, comparison_operator:==; 32, 33; 32, 37; 33, call; 33, 34; 33, 35; 34, identifier:len; 35, argument_list; 35, 36; 36, identifier:args; 37, integer:2; 38, block; 38, 39; 38, 45; 38, 50; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, pattern_list; 41, 42; 41, 43; 42, identifier:elem; 43, identifier:list_; 44, identifier:args; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:ensure_sequence; 48, argument_list; 48, 49; 49, identifier:list_; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:predicate; 53, lambda; 53, 54; 53, 56; 54, lambda_parameters; 54, 55; 55, identifier:item; 56, comparison_operator:==; 56, 57; 56, 58; 57, identifier:item; 58, identifier:elem; 59, else_clause; 59, 60; 60, block; 60, 61; 60, 75; 60, 89; 60, 105; 60, 114; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:ensure_keyword_args; 64, argument_list; 64, 65; 64, 66; 64, 70; 65, identifier:kwargs; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:mandatory; 68, tuple; 68, 69; 69, string:'in_'; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:optional; 72, tuple; 72, 73; 72, 74; 73, string:'of'; 74, string:'where'; 75, if_statement; 75, 76; 75, 83; 76, boolean_operator:and; 76, 77; 76, 80; 77, comparison_operator:in; 77, 78; 77, 79; 78, string:'of'; 79, identifier:kwargs; 80, comparison_operator:in; 80, 81; 80, 82; 81, string:'where'; 82, identifier:kwargs; 83, block; 83, 84; 84, raise_statement; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:TypeError; 87, argument_list; 87, 88; 88, string:"either an item or predicate must be supplied, not both"; 89, if_statement; 89, 90; 89, 99; 90, not_operator; 90, 91; 91, parenthesized_expression; 91, 92; 92, boolean_operator:or; 92, 93; 92, 96; 93, comparison_operator:in; 93, 94; 93, 95; 94, string:'of'; 95, identifier:kwargs; 96, comparison_operator:in; 96, 97; 96, 98; 97, string:'where'; 98, identifier:kwargs; 99, block; 99, 100; 100, raise_statement; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:TypeError; 103, argument_list; 103, 104; 104, string:"an item or predicate must be supplied"; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:list_; 108, call; 108, 109; 108, 110; 109, identifier:ensure_sequence; 110, argument_list; 110, 111; 111, subscript; 111, 112; 111, 113; 112, identifier:kwargs; 113, string:'in_'; 114, if_statement; 114, 115; 114, 118; 114, 128; 115, comparison_operator:in; 115, 116; 115, 117; 116, string:'where'; 117, identifier:kwargs; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:predicate; 122, call; 122, 123; 122, 124; 123, identifier:ensure_callable; 124, argument_list; 124, 125; 125, subscript; 125, 126; 125, 127; 126, identifier:kwargs; 127, string:'where'; 128, else_clause; 128, 129; 129, block; 129, 130; 129, 136; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:elem; 133, subscript; 133, 134; 133, 135; 134, identifier:kwargs; 135, string:'of'; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:predicate; 139, lambda; 139, 140; 139, 142; 140, lambda_parameters; 140, 141; 141, identifier:item; 142, comparison_operator:==; 142, 143; 142, 144; 143, identifier:item; 144, identifier:elem; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:len_; 148, call; 148, 149; 148, 150; 149, identifier:len; 150, argument_list; 150, 151; 151, identifier:list_; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:start; 155, call; 155, 156; 155, 157; 156, identifier:max; 157, argument_list; 157, 158; 157, 159; 158, integer:0; 159, call; 159, 160; 159, 161; 160, identifier:min; 161, argument_list; 161, 162; 161, 165; 162, binary_operator:-; 162, 163; 162, 164; 163, identifier:len_; 164, integer:1; 165, identifier:start; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:i; 169, identifier:start; 170, while_statement; 170, 171; 170, 175; 170, 192; 171, comparison_operator:<=; 171, 172; 171, 173; 171, 174; 172, integer:0; 173, identifier:i; 174, identifier:len_; 175, block; 175, 176; 175, 188; 176, if_statement; 176, 177; 176, 183; 177, call; 177, 178; 177, 179; 178, identifier:predicate; 179, argument_list; 179, 180; 180, subscript; 180, 181; 180, 182; 181, identifier:list_; 182, identifier:i; 183, block; 183, 184; 184, return_statement; 184, 185; 185, expression_list; 185, 186; 185, 187; 186, identifier:list_; 187, identifier:i; 188, expression_statement; 188, 189; 189, augmented_assignment:+=; 189, 190; 189, 191; 190, identifier:i; 191, identifier:step; 192, else_clause; 192, 193; 193, block; 193, 194; 194, return_statement; 194, 195; 195, expression_list; 195, 196; 195, 197; 196, identifier:list_; 197, unary_operator:-; 197, 198; 198, integer:1 | def _index(*args, **kwargs):
"""Implementation of list searching.
:param of: Element to search for
:param where: Predicate to search for
:param in_: List to search in
:param start: Start index for the lookup
:param step: Counter step (i.e. in/decrement) for each iteration
:return: Pair of ``(list, index)``,
where ``list`` is the list we searched in
and ``index`` is the index of the first element found, or -1
"""
start = kwargs.pop('start', 0)
step = kwargs.pop('step', 1)
if len(args) == 2:
elem, list_ = args
ensure_sequence(list_)
predicate = lambda item: item == elem
else:
ensure_keyword_args(kwargs,
mandatory=('in_',), optional=('of', 'where'))
if 'of' in kwargs and 'where' in kwargs:
raise TypeError(
"either an item or predicate must be supplied, not both")
if not ('of' in kwargs or 'where' in kwargs):
raise TypeError("an item or predicate must be supplied")
list_ = ensure_sequence(kwargs['in_'])
if 'where' in kwargs:
predicate = ensure_callable(kwargs['where'])
else:
elem = kwargs['of']
predicate = lambda item: item == elem
len_ = len(list_)
start = max(0, min(len_ - 1, start))
i = start
while 0 <= i < len_:
if predicate(list_[i]):
return list_, i
i += step
else:
return list_, -1 |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:tally_poll; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 16; 5, 28; 5, 37; 5, 47; 5, 54; 5, 65; 5, 89; 5, 106; 5, 115; 5, 132; 5, 172; 5, 178; 5, 187; 5, 213; 5, 220; 5, 228; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 13; 9, not_operator; 9, 10; 10, attribute; 10, 11; 10, 12; 11, identifier:args; 12, identifier:msg; 13, block; 13, 14; 14, return_statement; 14, 15; 15, string:"Syntax: !vote tally <pollnum>"; 16, if_statement; 16, 17; 16, 25; 17, not_operator; 17, 18; 18, call; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:args; 22, identifier:msg; 23, identifier:isdigit; 24, argument_list; 25, block; 25, 26; 26, return_statement; 26, 27; 27, string:"Not A Valid Positive Integer."; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:pid; 31, call; 31, 32; 31, 33; 32, identifier:int; 33, argument_list; 33, 34; 34, attribute; 34, 35; 34, 36; 35, identifier:args; 36, identifier:msg; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:poll; 40, call; 40, 41; 40, 42; 41, identifier:get_open_poll; 42, argument_list; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:args; 45, identifier:session; 46, identifier:pid; 47, if_statement; 47, 48; 47, 51; 48, comparison_operator:is; 48, 49; 48, 50; 49, identifier:poll; 50, None; 51, block; 51, 52; 52, return_statement; 52, 53; 53, string:"That poll doesn't exist or was deleted. Use !poll list to see valid polls"; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:state; 57, conditional_expression:if; 57, 58; 57, 59; 57, 64; 58, string:"Active"; 59, comparison_operator:==; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:poll; 62, identifier:active; 63, integer:1; 64, string:"Closed"; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:votes; 68, call; 68, 69; 68, 88; 69, attribute; 69, 70; 69, 87; 70, call; 70, 71; 70, 81; 71, attribute; 71, 72; 71, 80; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:args; 76, identifier:session; 77, identifier:query; 78, argument_list; 78, 79; 79, identifier:Poll_responses; 80, identifier:filter; 81, argument_list; 81, 82; 82, comparison_operator:==; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:Poll_responses; 85, identifier:pid; 86, identifier:pid; 87, identifier:all; 88, argument_list; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:args; 93, identifier:send; 94, argument_list; 94, 95; 95, binary_operator:%; 95, 96; 95, 97; 96, string:"%s poll: %s, %d total votes"; 97, tuple; 97, 98; 97, 99; 97, 102; 98, identifier:state; 99, attribute; 99, 100; 99, 101; 100, identifier:poll; 101, identifier:question; 102, call; 102, 103; 102, 104; 103, identifier:len; 104, argument_list; 104, 105; 105, identifier:votes; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:votemap; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:collections; 112, identifier:defaultdict; 113, argument_list; 113, 114; 114, identifier:list; 115, for_statement; 115, 116; 115, 117; 115, 118; 116, identifier:v; 117, identifier:votes; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 128; 121, attribute; 121, 122; 121, 127; 122, subscript; 122, 123; 122, 124; 123, identifier:votemap; 124, attribute; 124, 125; 124, 126; 125, identifier:v; 126, identifier:response; 127, identifier:append; 128, argument_list; 128, 129; 129, attribute; 129, 130; 129, 131; 130, identifier:v; 131, identifier:voter; 132, for_statement; 132, 133; 132, 134; 132, 142; 133, identifier:x; 134, call; 134, 135; 134, 136; 135, identifier:sorted; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:votemap; 140, identifier:keys; 141, argument_list; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:args; 147, identifier:send; 148, argument_list; 148, 149; 148, 167; 149, binary_operator:%; 149, 150; 149, 151; 150, string:"%s: %d -- %s"; 151, tuple; 151, 152; 151, 153; 151, 159; 152, identifier:x; 153, call; 153, 154; 153, 155; 154, identifier:len; 155, argument_list; 155, 156; 156, subscript; 156, 157; 156, 158; 157, identifier:votemap; 158, identifier:x; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, string:", "; 162, identifier:join; 163, argument_list; 163, 164; 164, subscript; 164, 165; 164, 166; 165, identifier:votemap; 166, identifier:x; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:target; 169, attribute; 169, 170; 169, 171; 170, identifier:args; 171, identifier:nick; 172, if_statement; 172, 173; 172, 175; 173, not_operator; 173, 174; 174, identifier:votemap; 175, block; 175, 176; 176, return_statement; 176, 177; 177, string:""; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:ranking; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:collections; 184, identifier:defaultdict; 185, argument_list; 185, 186; 186, identifier:list; 187, for_statement; 187, 188; 187, 189; 187, 194; 188, identifier:x; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:votemap; 192, identifier:keys; 193, argument_list; 194, block; 194, 195; 194, 204; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:num; 198, call; 198, 199; 198, 200; 199, identifier:len; 200, argument_list; 200, 201; 201, subscript; 201, 202; 201, 203; 202, identifier:votemap; 203, identifier:x; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 211; 206, attribute; 206, 207; 206, 210; 207, subscript; 207, 208; 207, 209; 208, identifier:ranking; 209, identifier:num; 210, identifier:append; 211, argument_list; 211, 212; 212, identifier:x; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:high; 216, call; 216, 217; 216, 218; 217, identifier:max; 218, argument_list; 218, 219; 219, identifier:ranking; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:winners; 223, tuple; 223, 224; 223, 227; 224, subscript; 224, 225; 224, 226; 225, identifier:ranking; 226, identifier:high; 227, identifier:high; 228, if_statement; 228, 229; 228, 237; 228, 252; 229, comparison_operator:==; 229, 230; 229, 236; 230, call; 230, 231; 230, 232; 231, identifier:len; 232, argument_list; 232, 233; 233, subscript; 233, 234; 233, 235; 234, identifier:winners; 235, integer:0; 236, integer:1; 237, block; 237, 238; 237, 248; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:winners; 241, tuple; 241, 242; 241, 247; 242, subscript; 242, 243; 242, 246; 243, subscript; 243, 244; 243, 245; 244, identifier:winners; 245, integer:0; 246, integer:0; 247, identifier:high; 248, return_statement; 248, 249; 249, binary_operator:%; 249, 250; 249, 251; 250, string:"The winner is %s with %d votes."; 251, identifier:winners; 252, else_clause; 252, 253; 253, block; 253, 254; 253, 267; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:winners; 257, tuple; 257, 258; 257, 266; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, string:", "; 261, identifier:join; 262, argument_list; 262, 263; 263, subscript; 263, 264; 263, 265; 264, identifier:winners; 265, integer:0; 266, identifier:high; 267, return_statement; 267, 268; 268, binary_operator:%; 268, 269; 268, 270; 269, string:"Tie between %s with %d votes."; 270, identifier:winners | def tally_poll(args):
"""Shows the results of poll."""
if not args.msg:
return "Syntax: !vote tally <pollnum>"
if not args.msg.isdigit():
return "Not A Valid Positive Integer."
pid = int(args.msg)
poll = get_open_poll(args.session, pid)
if poll is None:
return "That poll doesn't exist or was deleted. Use !poll list to see valid polls"
state = "Active" if poll.active == 1 else "Closed"
votes = args.session.query(Poll_responses).filter(Poll_responses.pid == pid).all()
args.send("%s poll: %s, %d total votes" % (state, poll.question, len(votes)))
votemap = collections.defaultdict(list)
for v in votes:
votemap[v.response].append(v.voter)
for x in sorted(votemap.keys()):
args.send("%s: %d -- %s" % (x, len(votemap[x]), ", ".join(votemap[x])), target=args.nick)
if not votemap:
return ""
ranking = collections.defaultdict(list)
for x in votemap.keys():
num = len(votemap[x])
ranking[num].append(x)
high = max(ranking)
winners = (ranking[high], high)
if len(winners[0]) == 1:
winners = (winners[0][0], high)
return "The winner is %s with %d votes." % winners
else:
winners = (", ".join(winners[0]), high)
return "Tie between %s with %d votes." % winners |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_setup_pages; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, identifier:config; 6, block; 6, 7; 6, 9; 6, 15; 6, 21; 6, 27; 6, 33; 6, 40; 6, 44; 6, 48; 6, 82; 6, 92; 6, 104; 7, expression_statement; 7, 8; 8, comment; 9, import_from_statement; 9, 10; 9, 13; 10, dotted_name; 10, 11; 10, 12; 11, identifier:cms; 12, identifier:exceptions; 13, dotted_name; 13, 14; 14, identifier:NoHomeFound; 15, import_from_statement; 15, 16; 15, 19; 16, dotted_name; 16, 17; 16, 18; 17, identifier:cms; 18, identifier:models; 19, dotted_name; 19, 20; 20, identifier:Page; 21, import_from_statement; 21, 22; 21, 25; 22, dotted_name; 22, 23; 22, 24; 23, identifier:cms; 24, identifier:utils; 25, dotted_name; 25, 26; 26, identifier:get_language_list; 27, import_from_statement; 27, 28; 27, 31; 28, dotted_name; 28, 29; 28, 30; 29, identifier:django; 30, identifier:conf; 31, dotted_name; 31, 32; 32, identifier:settings; 33, import_from_statement; 33, 34; 33, 38; 34, dotted_name; 34, 35; 34, 36; 34, 37; 35, identifier:django; 36, identifier:utils; 37, identifier:translation; 38, dotted_name; 38, 39; 39, identifier:override; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:app_page; 43, None; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:get_url; 47, False; 48, if_statement; 48, 49; 48, 55; 49, call; 49, 50; 49, 51; 50, identifier:getattr; 51, argument_list; 51, 52; 51, 53; 51, 54; 52, identifier:settings; 53, string:'ALDRYN_SEARCH_CMS_PAGE'; 54, False; 55, block; 55, 56; 55, 62; 55, 70; 55, 76; 56, import_from_statement; 56, 57; 56, 60; 57, dotted_name; 57, 58; 57, 59; 58, identifier:aldryn_search; 59, identifier:search_indexes; 60, dotted_name; 60, 61; 61, identifier:TitleIndex; 62, function_definition; 62, 63; 62, 64; 62, 67; 63, function_name:fake_url; 64, parameters; 64, 65; 64, 66; 65, identifier:self; 66, identifier:obj; 67, block; 67, 68; 68, return_statement; 68, 69; 69, string:''; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:get_url; 73, attribute; 73, 74; 73, 75; 74, identifier:TitleIndex; 75, identifier:get_url; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:TitleIndex; 80, identifier:get_url; 81, identifier:fake_url; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:site; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:Site; 89, identifier:objects; 90, identifier:get_current; 91, argument_list; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:auto_sites; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:cls; 99, identifier:auto_setup; 100, identifier:get; 101, argument_list; 101, 102; 101, 103; 102, string:'sites'; 103, True; 104, if_statement; 104, 105; 104, 114; 105, boolean_operator:or; 105, 106; 105, 109; 106, comparison_operator:is; 106, 107; 106, 108; 107, identifier:auto_sites; 108, True; 109, comparison_operator:in; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:site; 112, identifier:pk; 113, identifier:auto_sites; 114, block; 114, 115; 114, 161; 114, 170; 115, if_statement; 115, 116; 115, 122; 116, call; 116, 117; 116, 118; 117, identifier:getattr; 118, argument_list; 118, 119; 118, 120; 118, 121; 119, identifier:cls; 120, string:'app_config'; 121, False; 122, block; 122, 123; 122, 135; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:configs; 126, call; 126, 127; 126, 134; 127, attribute; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:cls; 131, identifier:app_config; 132, identifier:objects; 133, identifier:all; 134, argument_list; 135, if_statement; 135, 136; 135, 142; 135, 151; 136, not_operator; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:configs; 140, identifier:exists; 141, argument_list; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:config; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:cls; 149, identifier:_create_config; 150, argument_list; 151, else_clause; 151, 152; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:config; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:configs; 159, identifier:first; 160, argument_list; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:langs; 164, call; 164, 165; 164, 166; 165, identifier:get_language_list; 166, argument_list; 166, 167; 167, attribute; 167, 168; 167, 169; 168, identifier:site; 169, identifier:pk; 170, if_statement; 170, 171; 170, 195; 171, not_operator; 171, 172; 172, call; 172, 173; 172, 194; 173, attribute; 173, 174; 173, 193; 174, call; 174, 175; 174, 187; 175, attribute; 175, 176; 175, 186; 176, call; 176, 177; 176, 182; 177, attribute; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:Page; 180, identifier:objects; 181, identifier:on_site; 182, argument_list; 182, 183; 183, attribute; 183, 184; 183, 185; 184, identifier:site; 185, identifier:pk; 186, identifier:filter; 187, argument_list; 187, 188; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:application_urls; 190, attribute; 190, 191; 190, 192; 191, identifier:cls; 192, identifier:__name__; 193, identifier:exists; 194, argument_list; 195, block; 195, 196; 195, 327; 196, for_statement; 196, 197; 196, 198; 196, 199; 197, identifier:lang; 198, identifier:langs; 199, block; 199, 200; 200, with_statement; 200, 201; 200, 207; 201, with_clause; 201, 202; 202, with_item; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:override; 205, argument_list; 205, 206; 206, identifier:lang; 207, block; 207, 208; 207, 249; 207, 275; 207, 283; 207, 304; 208, if_statement; 208, 209; 208, 210; 208, 232; 208, 243; 209, identifier:config; 210, block; 210, 211; 210, 226; 211, if_statement; 211, 212; 211, 217; 212, subscript; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:cls; 215, identifier:auto_setup; 216, string:'config_translated_fields'; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:cls; 222, identifier:_create_config_translation; 223, argument_list; 223, 224; 223, 225; 224, identifier:config; 225, identifier:lang; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:namespace; 229, attribute; 229, 230; 229, 231; 230, identifier:config; 231, identifier:namespace; 232, elif_clause; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:cls; 235, identifier:app_name; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:namespace; 240, attribute; 240, 241; 240, 242; 241, identifier:cls; 242, identifier:app_name; 243, else_clause; 243, 244; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:namespace; 248, None; 249, try_statement; 249, 250; 249, 268; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:home; 254, call; 254, 255; 254, 267; 255, attribute; 255, 256; 255, 266; 256, call; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:Page; 260, identifier:objects; 261, identifier:get_home; 262, argument_list; 262, 263; 263, attribute; 263, 264; 263, 265; 264, identifier:site; 265, identifier:pk; 266, identifier:get_draft_object; 267, argument_list; 268, except_clause; 268, 269; 268, 270; 269, identifier:NoHomeFound; 270, block; 270, 271; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:home; 274, None; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:set_home; 278, call; 278, 279; 278, 280; 279, identifier:hasattr; 280, argument_list; 280, 281; 280, 282; 281, identifier:Page; 282, string:'set_as_homepage'; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:home; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:cls; 289, identifier:_create_page; 290, argument_list; 290, 291; 290, 292; 290, 293; 290, 298; 290, 301; 291, identifier:home; 292, identifier:lang; 293, subscript; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:cls; 296, identifier:auto_setup; 297, string:'home title'; 298, keyword_argument; 298, 299; 298, 300; 299, identifier:site; 300, identifier:site; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:set_home; 303, identifier:set_home; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:app_page; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:cls; 310, identifier:_create_page; 311, argument_list; 311, 312; 311, 313; 311, 314; 311, 319; 311, 322; 311, 323; 311, 324; 312, identifier:app_page; 313, identifier:lang; 314, subscript; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:cls; 317, identifier:auto_setup; 318, string:'page title'; 319, attribute; 319, 320; 319, 321; 320, identifier:cls; 321, identifier:__name__; 322, identifier:home; 323, identifier:namespace; 324, keyword_argument; 324, 325; 324, 326; 325, identifier:site; 326, identifier:site; 327, if_statement; 327, 328; 327, 329; 328, identifier:get_url; 329, block; 329, 330; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:TitleIndex; 334, identifier:get_url; 335, identifier:get_url | def _setup_pages(cls, config):
"""
Create the page structure.
It created a home page (if not exists) and a sub-page, and attach the Apphook to the
sub-page.
Pages titles are provided by ``AutoCMSAppMixin.auto_setup``
:param setup_config: boolean to control whether creating the ApphookConfig instance
"""
from cms.exceptions import NoHomeFound
from cms.models import Page
from cms.utils import get_language_list
from django.conf import settings
from django.utils.translation import override
app_page = None
get_url = False
if getattr(settings, 'ALDRYN_SEARCH_CMS_PAGE', False):
from aldryn_search.search_indexes import TitleIndex
def fake_url(self, obj):
return ''
get_url = TitleIndex.get_url
TitleIndex.get_url = fake_url
site = Site.objects.get_current()
auto_sites = cls.auto_setup.get('sites', True)
if auto_sites is True or site.pk in auto_sites:
if getattr(cls, 'app_config', False):
configs = cls.app_config.objects.all()
if not configs.exists():
config = cls._create_config()
else:
config = configs.first()
langs = get_language_list(site.pk)
if not Page.objects.on_site(site.pk).filter(application_urls=cls.__name__).exists():
for lang in langs:
with override(lang):
if config:
if cls.auto_setup['config_translated_fields']:
cls._create_config_translation(config, lang)
namespace = config.namespace
elif cls.app_name:
namespace = cls.app_name
else:
namespace = None
try:
home = Page.objects.get_home(site.pk).get_draft_object()
except NoHomeFound:
home = None
set_home = hasattr(Page, 'set_as_homepage')
home = cls._create_page(
home, lang, cls.auto_setup['home title'], site=site, set_home=set_home
)
app_page = cls._create_page(
app_page, lang, cls.auto_setup['page title'], cls.__name__, home,
namespace, site=site
)
if get_url:
TitleIndex.get_url = get_url |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:pretty_description; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:description; 5, default_parameter; 5, 6; 5, 7; 6, identifier:wrap_at; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:indent; 10, integer:0; 11, block; 11, 12; 11, 14; 11, 47; 11, 53; 11, 73; 11, 77; 11, 103; 11, 113; 11, 125; 11, 141; 11, 145; 11, 241; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 22; 15, boolean_operator:or; 15, 16; 15, 19; 16, comparison_operator:is; 16, 17; 16, 18; 17, identifier:wrap_at; 18, None; 19, comparison_operator:<; 19, 20; 19, 21; 20, identifier:wrap_at; 21, integer:0; 22, block; 22, 23; 22, 32; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:width; 26, call; 26, 27; 26, 28; 27, identifier:console_width; 28, argument_list; 28, 29; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:default; 31, integer:79; 32, if_statement; 32, 33; 32, 36; 32, 41; 33, comparison_operator:is; 33, 34; 33, 35; 34, identifier:wrap_at; 35, None; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:wrap_at; 40, identifier:width; 41, else_clause; 41, 42; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, augmented_assignment:+=; 44, 45; 44, 46; 45, identifier:wrap_at; 46, identifier:width; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:indent; 50, binary_operator:*; 50, 51; 50, 52; 51, string:' '; 52, identifier:indent; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:text_wrapper; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:textwrap; 59, identifier:TextWrapper; 60, argument_list; 60, 61; 60, 64; 60, 67; 60, 70; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:width; 63, identifier:wrap_at; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:replace_whitespace; 66, False; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:initial_indent; 69, identifier:indent; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:subsequent_indent; 72, identifier:indent; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:new_desc; 76, list:[]; 77, for_statement; 77, 78; 77, 79; 77, 85; 78, identifier:line; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:description; 82, identifier:split; 83, argument_list; 83, 84; 84, string:'\n'; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:new_desc; 90, identifier:append; 91, argument_list; 91, 92; 92, call; 92, 93; 92, 102; 93, attribute; 93, 94; 93, 101; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:line; 97, identifier:replace; 98, argument_list; 98, 99; 98, 100; 99, string:'\n'; 100, string:''; 101, identifier:strip; 102, argument_list; 103, while_statement; 103, 104; 103, 108; 104, not_operator; 104, 105; 105, subscript; 105, 106; 105, 107; 106, identifier:new_desc; 107, integer:0; 108, block; 108, 109; 109, delete_statement; 109, 110; 110, subscript; 110, 111; 110, 112; 111, identifier:new_desc; 112, integer:0; 113, while_statement; 113, 114; 113, 119; 114, not_operator; 114, 115; 115, subscript; 115, 116; 115, 117; 116, identifier:new_desc; 117, unary_operator:-; 117, 118; 118, integer:1; 119, block; 119, 120; 120, delete_statement; 120, 121; 121, subscript; 121, 122; 121, 123; 122, identifier:new_desc; 123, unary_operator:-; 123, 124; 124, integer:1; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:separators; 128, list_comprehension; 128, 129; 128, 130; 128, 138; 129, identifier:i; 130, for_in_clause; 130, 131; 130, 134; 131, pattern_list; 131, 132; 131, 133; 132, identifier:i; 133, identifier:l; 134, call; 134, 135; 134, 136; 135, identifier:enumerate; 136, argument_list; 136, 137; 137, identifier:new_desc; 138, if_clause; 138, 139; 139, not_operator; 139, 140; 140, identifier:l; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:paragraphs; 144, list:[]; 145, if_statement; 145, 146; 145, 147; 146, identifier:separators; 147, block; 147, 148; 147, 158; 147, 170; 147, 208; 147, 221; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, pattern_list; 150, 151; 150, 152; 151, identifier:start; 152, identifier:end; 153, expression_list; 153, 154; 153, 155; 154, integer:0; 155, subscript; 155, 156; 155, 157; 156, identifier:separators; 157, integer:0; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:paragraphs; 162, identifier:append; 163, argument_list; 163, 164; 164, subscript; 164, 165; 164, 166; 165, identifier:new_desc; 166, slice; 166, 167; 166, 168; 166, 169; 167, identifier:start; 168, colon; 169, identifier:end; 170, for_statement; 170, 171; 170, 172; 170, 181; 171, identifier:i; 172, call; 172, 173; 172, 174; 173, identifier:range; 174, argument_list; 174, 175; 175, binary_operator:-; 175, 176; 175, 180; 176, call; 176, 177; 176, 178; 177, identifier:len; 178, argument_list; 178, 179; 179, identifier:separators; 180, integer:1; 181, block; 181, 182; 181, 188; 181, 196; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:start; 185, binary_operator:+; 185, 186; 185, 187; 186, identifier:end; 187, integer:1; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:end; 191, subscript; 191, 192; 191, 193; 192, identifier:separators; 193, binary_operator:+; 193, 194; 193, 195; 194, identifier:i; 195, integer:1; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:paragraphs; 200, identifier:append; 201, argument_list; 201, 202; 202, subscript; 202, 203; 202, 204; 203, identifier:new_desc; 204, slice; 204, 205; 204, 206; 204, 207; 205, identifier:start; 206, colon; 207, identifier:end; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:paragraphs; 212, identifier:append; 213, argument_list; 213, 214; 214, subscript; 214, 215; 214, 216; 215, identifier:new_desc; 216, slice; 216, 217; 216, 220; 217, binary_operator:+; 217, 218; 217, 219; 218, identifier:end; 219, integer:1; 220, colon; 221, return_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, string:'\n\n'; 225, identifier:join; 226, generator_expression; 226, 227; 226, 238; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:text_wrapper; 230, identifier:fill; 231, argument_list; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, string:' '; 235, identifier:join; 236, argument_list; 236, 237; 237, identifier:p; 238, for_in_clause; 238, 239; 238, 240; 239, identifier:p; 240, identifier:paragraphs; 241, return_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:text_wrapper; 245, identifier:fill; 246, argument_list; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, string:' '; 250, identifier:join; 251, argument_list; 251, 252; 252, identifier:new_desc | def pretty_description(description, wrap_at=None, indent=0):
"""
Return a pretty formatted string given some text.
Args:
description (str): string to format.
wrap_at (int): maximum length of a line.
indent (int): level of indentation.
Returns:
str: pretty formatted string.
"""
if wrap_at is None or wrap_at < 0:
width = console_width(default=79)
if wrap_at is None:
wrap_at = width
else:
wrap_at += width
indent = ' ' * indent
text_wrapper = textwrap.TextWrapper(
width=wrap_at, replace_whitespace=False,
initial_indent=indent, subsequent_indent=indent)
new_desc = []
for line in description.split('\n'):
new_desc.append(line.replace('\n', '').strip())
while not new_desc[0]:
del new_desc[0]
while not new_desc[-1]:
del new_desc[-1]
separators = [i for i, l in enumerate(new_desc) if not l]
paragraphs = []
if separators:
start, end = 0, separators[0]
paragraphs.append(new_desc[start:end])
for i in range(len(separators) - 1):
start = end + 1
end = separators[i + 1]
paragraphs.append(new_desc[start:end])
paragraphs.append(new_desc[end + 1:])
return '\n\n'.join(text_wrapper.fill(' '.join(p)) for p in paragraphs)
return text_wrapper.fill(' '.join(new_desc)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:search; 3, parameters; 3, 4; 3, 5; 3, 10; 3, 13; 3, 16; 4, identifier:term; 5, default_parameter; 5, 6; 5, 7; 6, identifier:category; 7, attribute; 7, 8; 7, 9; 8, identifier:Categories; 9, identifier:ALL; 10, default_parameter; 10, 11; 10, 12; 11, identifier:pages; 12, integer:1; 13, default_parameter; 13, 14; 13, 15; 14, identifier:sort; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:order; 18, None; 19, block; 19, 20; 19, 22; 19, 28; 19, 49; 20, expression_statement; 20, 21; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:s; 25, call; 25, 26; 25, 27; 26, identifier:Search; 27, argument_list; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:s; 32, identifier:search; 33, argument_list; 33, 34; 33, 37; 33, 40; 33, 43; 33, 46; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:term; 36, identifier:term; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:category; 39, identifier:category; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:pages; 42, identifier:pages; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:sort; 45, identifier:sort; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:order; 48, identifier:order; 49, return_statement; 49, 50; 50, identifier:s | def search(term, category=Categories.ALL, pages=1, sort=None, order=None):
"""Return a search result for term in category. Can also be
sorted and span multiple pages."""
s = Search()
s.search(term=term, category=category, pages=pages, sort=sort, order=order)
return s |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:popular; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:category; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:sortOption; 9, string:"title"; 10, block; 10, 11; 10, 13; 10, 19; 10, 27; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:s; 16, call; 16, 17; 16, 18; 17, identifier:Search; 18, argument_list; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:s; 23, identifier:popular; 24, argument_list; 24, 25; 24, 26; 25, identifier:category; 26, identifier:sortOption; 27, return_statement; 27, 28; 28, identifier:s | def popular(category=None, sortOption = "title"):
"""Return a search result containing torrents appearing
on the KAT home page. Can be categorized. Cannot be
sorted or contain multiple pages"""
s = Search()
s.popular(category, sortOption)
return s |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:recent; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 4, default_parameter; 4, 5; 4, 6; 5, identifier:category; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:pages; 9, integer:1; 10, default_parameter; 10, 11; 10, 12; 11, identifier:sort; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:order; 15, None; 16, block; 16, 17; 16, 19; 16, 25; 16, 35; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:s; 22, call; 22, 23; 22, 24; 23, identifier:Search; 24, argument_list; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:s; 29, identifier:recent; 30, argument_list; 30, 31; 30, 32; 30, 33; 30, 34; 31, identifier:category; 32, identifier:pages; 33, identifier:sort; 34, identifier:order; 35, return_statement; 35, 36; 36, identifier:s | def recent(category=None, pages=1, sort=None, order=None):
"""Return most recently added torrents. Can be sorted and categorized
and contain multiple pages."""
s = Search()
s.recent(category, pages, sort, order)
return s |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:pick_action_todo; 3, parameters; 4, block; 4, 5; 4, 7; 4, 70; 5, expression_statement; 5, 6; 6, comment; 7, for_statement; 7, 8; 7, 11; 7, 15; 7, 16; 8, pattern_list; 8, 9; 8, 10; 9, identifier:ndx; 10, identifier:todo; 11, call; 11, 12; 11, 13; 12, identifier:enumerate; 13, argument_list; 13, 14; 14, identifier:things_to_do; 15, comment; 16, block; 16, 17; 17, if_statement; 17, 18; 17, 24; 18, call; 18, 19; 18, 20; 19, identifier:roll_dice; 20, argument_list; 20, 21; 21, subscript; 21, 22; 21, 23; 22, identifier:todo; 23, string:"chance"; 24, block; 24, 25; 24, 36; 24, 53; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:cur_act; 28, subscript; 28, 29; 28, 30; 29, identifier:actions; 30, call; 30, 31; 30, 32; 31, identifier:get_action_by_name; 32, argument_list; 32, 33; 33, subscript; 33, 34; 33, 35; 34, identifier:todo; 35, string:"name"; 36, if_statement; 36, 37; 36, 50; 37, boolean_operator:and; 37, 38; 37, 43; 38, comparison_operator:==; 38, 39; 38, 42; 39, subscript; 39, 40; 39, 41; 40, identifier:todo; 41, string:"WHERE_COL"; 42, string:"energy"; 43, comparison_operator:>; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:my_char; 46, string:"energy"; 47, subscript; 47, 48; 47, 49; 48, identifier:todo; 49, string:"WHERE_VAL"; 50, block; 50, 51; 51, return_statement; 51, 52; 52, identifier:cur_act; 53, if_statement; 53, 54; 53, 67; 54, boolean_operator:and; 54, 55; 54, 60; 55, comparison_operator:==; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:todo; 58, string:"WHERE_COL"; 59, string:"gold"; 60, comparison_operator:>; 60, 61; 60, 64; 61, subscript; 61, 62; 61, 63; 62, identifier:my_char; 63, string:"gold"; 64, subscript; 64, 65; 64, 66; 65, identifier:todo; 66, string:"WHERE_VAL"; 67, block; 67, 68; 68, return_statement; 68, 69; 69, identifier:cur_act; 70, return_statement; 70, 71; 71, subscript; 71, 72; 71, 73; 72, identifier:actions; 73, integer:3 | def pick_action_todo():
"""
only for testing and AI - user will usually choose an action
Sort of works
"""
for ndx, todo in enumerate(things_to_do):
#print('todo = ', todo)
if roll_dice(todo["chance"]):
cur_act = actions[get_action_by_name(todo["name"])]
if todo["WHERE_COL"] == "energy" and my_char["energy"] > todo["WHERE_VAL"]:
return cur_act
if todo["WHERE_COL"] == "gold" and my_char["gold"] > todo["WHERE_VAL"]:
return cur_act
return actions[3] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:select; 3, parameters; 3, 4; 3, 5; 4, identifier:soup; 5, identifier:selector; 6, block; 6, 7; 6, 9; 6, 17; 6, 22; 6, 289; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:tokens; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:selector; 15, identifier:split; 16, argument_list; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:current_context; 20, list:[soup]; 20, 21; 21, identifier:soup; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:token; 24, identifier:tokens; 25, block; 25, 26; 25, 35; 25, 102; 25, 156; 25, 222; 25, 253; 25, 254; 25, 265; 25, 269; 25, 285; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:m; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:attribselect_re; 32, identifier:match; 33, argument_list; 33, 34; 34, identifier:token; 35, if_statement; 35, 36; 35, 37; 35, 38; 36, identifier:m; 37, comment; 38, block; 38, 39; 38, 51; 38, 59; 38, 68; 38, 72; 38, 97; 38, 101; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 46; 41, pattern_list; 41, 42; 41, 43; 41, 44; 41, 45; 42, identifier:tag; 43, identifier:attribute; 44, identifier:operator; 45, identifier:value; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:m; 49, identifier:groups; 50, argument_list; 51, if_statement; 51, 52; 51, 54; 52, not_operator; 52, 53; 53, identifier:tag; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:tag; 58, True; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:checker; 62, call; 62, 63; 62, 64; 63, identifier:attribute_checker; 64, argument_list; 64, 65; 64, 66; 64, 67; 65, identifier:operator; 66, identifier:attribute; 67, identifier:value; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:found; 71, list:[]; 72, for_statement; 72, 73; 72, 74; 72, 75; 73, identifier:context; 74, identifier:current_context; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:found; 80, identifier:extend; 81, argument_list; 81, 82; 82, list_comprehension; 82, 83; 82, 84; 82, 92; 83, identifier:el; 84, for_in_clause; 84, 85; 84, 86; 85, identifier:el; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:context; 89, identifier:findAll; 90, argument_list; 90, 91; 91, identifier:tag; 92, if_clause; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:checker; 95, argument_list; 95, 96; 96, identifier:el; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:current_context; 100, identifier:found; 101, continue_statement; 102, if_statement; 102, 103; 102, 106; 102, 107; 103, comparison_operator:in; 103, 104; 103, 105; 104, string:'#'; 105, identifier:token; 106, comment; 107, block; 107, 108; 107, 120; 107, 128; 107, 143; 107, 150; 107, 155; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, pattern_list; 110, 111; 110, 112; 111, identifier:tag; 112, identifier:id; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:token; 116, identifier:split; 117, argument_list; 117, 118; 117, 119; 118, string:'#'; 119, integer:1; 120, if_statement; 120, 121; 120, 123; 121, not_operator; 121, 122; 122, identifier:tag; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:tag; 127, True; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:el; 131, call; 131, 132; 131, 137; 132, attribute; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:current_context; 135, integer:0; 136, identifier:find; 137, argument_list; 137, 138; 137, 139; 138, identifier:tag; 139, dictionary; 139, 140; 140, pair; 140, 141; 140, 142; 141, string:'id'; 142, identifier:id; 143, if_statement; 143, 144; 143, 146; 144, not_operator; 144, 145; 145, identifier:el; 146, block; 146, 147; 146, 149; 147, return_statement; 147, 148; 148, list:[]; 149, comment; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:current_context; 153, list:[el]; 153, 154; 154, identifier:el; 155, continue_statement; 156, if_statement; 156, 157; 156, 160; 156, 161; 157, comparison_operator:in; 157, 158; 157, 159; 158, string:'.'; 159, identifier:token; 160, comment; 161, block; 161, 162; 161, 174; 161, 182; 161, 186; 161, 217; 161, 221; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, pattern_list; 164, 165; 164, 166; 165, identifier:tag; 166, identifier:klass; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:token; 170, identifier:split; 171, argument_list; 171, 172; 171, 173; 172, string:'.'; 173, integer:1; 174, if_statement; 174, 175; 174, 177; 175, not_operator; 175, 176; 176, identifier:tag; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:tag; 181, True; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:found; 185, list:[]; 186, for_statement; 186, 187; 186, 188; 186, 189; 187, identifier:context; 188, identifier:current_context; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:found; 194, identifier:extend; 195, argument_list; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:context; 199, identifier:findAll; 200, argument_list; 200, 201; 200, 202; 201, identifier:tag; 202, dictionary; 202, 203; 203, pair; 203, 204; 203, 205; 204, string:'class'; 205, lambda; 205, 206; 205, 208; 206, lambda_parameters; 206, 207; 207, identifier:attr; 208, boolean_operator:and; 208, 209; 208, 210; 209, identifier:attr; 210, comparison_operator:in; 210, 211; 210, 212; 211, identifier:klass; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:attr; 215, identifier:split; 216, argument_list; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:current_context; 220, identifier:found; 221, continue_statement; 222, if_statement; 222, 223; 222, 226; 222, 227; 223, comparison_operator:==; 223, 224; 223, 225; 224, identifier:token; 225, string:'*'; 226, comment; 227, block; 227, 228; 227, 232; 227, 248; 227, 252; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:found; 231, list:[]; 232, for_statement; 232, 233; 232, 234; 232, 235; 233, identifier:context; 234, identifier:current_context; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:found; 240, identifier:extend; 241, argument_list; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:context; 245, identifier:findAll; 246, argument_list; 246, 247; 247, True; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:current_context; 251, identifier:found; 252, continue_statement; 253, comment; 254, if_statement; 254, 255; 254, 262; 255, not_operator; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:tag_re; 259, identifier:match; 260, argument_list; 260, 261; 261, identifier:token; 262, block; 262, 263; 263, return_statement; 263, 264; 264, list:[]; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:found; 268, list:[]; 269, for_statement; 269, 270; 269, 271; 269, 272; 270, identifier:context; 271, identifier:current_context; 272, block; 272, 273; 273, expression_statement; 273, 274; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:found; 277, identifier:extend; 278, argument_list; 278, 279; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:context; 282, identifier:findAll; 283, argument_list; 283, 284; 284, identifier:token; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:current_context; 288, identifier:found; 289, return_statement; 289, 290; 290, identifier:current_context | def select(soup, selector):
"""
soup should be a BeautifulSoup instance; selector is a CSS selector
specifying the elements you want to retrieve.
"""
tokens = selector.split()
current_context = [soup]
for token in tokens:
m = attribselect_re.match(token)
if m:
# Attribute selector
tag, attribute, operator, value = m.groups()
if not tag:
tag = True
checker = attribute_checker(operator, attribute, value)
found = []
for context in current_context:
found.extend([el for el in context.findAll(tag) if checker(el)])
current_context = found
continue
if '#' in token:
# ID selector
tag, id = token.split('#', 1)
if not tag:
tag = True
el = current_context[0].find(tag, {'id': id})
if not el:
return [] # No match
current_context = [el]
continue
if '.' in token:
# Class selector
tag, klass = token.split('.', 1)
if not tag:
tag = True
found = []
for context in current_context:
found.extend(
context.findAll(tag,
{'class': lambda attr: attr and klass in attr.split()}
)
)
current_context = found
continue
if token == '*':
# Star selector
found = []
for context in current_context:
found.extend(context.findAll(True))
current_context = found
continue
# Here we should just have a regular tag
if not tag_re.match(token):
return []
found = []
for context in current_context:
found.extend(context.findAll(token))
current_context = found
return current_context |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:try_; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:block; 5, default_parameter; 5, 6; 5, 7; 6, identifier:except_; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:else_; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:finally_; 13, None; 14, block; 14, 15; 14, 17; 14, 22; 14, 38; 14, 49; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:ensure_callable; 20, argument_list; 20, 21; 21, identifier:block; 22, if_statement; 22, 23; 22, 30; 23, not_operator; 23, 24; 24, parenthesized_expression; 24, 25; 25, boolean_operator:or; 25, 26; 25, 29; 26, boolean_operator:or; 26, 27; 26, 28; 27, identifier:except_; 28, identifier:else_; 29, identifier:finally_; 30, block; 30, 31; 31, raise_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:TypeError; 34, argument_list; 34, 35; 35, concatenated_string; 35, 36; 35, 37; 36, string:"at least one of `except_`, `else_` or `finally_` "; 37, string:"functions must be provided"; 38, if_statement; 38, 39; 38, 43; 39, boolean_operator:and; 39, 40; 39, 41; 40, identifier:else_; 41, not_operator; 41, 42; 42, identifier:except_; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:TypeError; 47, argument_list; 47, 48; 48, string:"`else_` can only be provided along with `except_`"; 49, if_statement; 49, 50; 49, 51; 49, 232; 50, identifier:except_; 51, block; 51, 52; 51, 91; 51, 130; 52, if_statement; 52, 53; 52, 57; 52, 65; 53, call; 53, 54; 53, 55; 54, identifier:callable; 55, argument_list; 55, 56; 56, identifier:except_; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:except_; 61, list:[(Exception, except_)]; 61, 62; 62, tuple; 62, 63; 62, 64; 63, identifier:Exception; 64, identifier:except_; 65, else_clause; 65, 66; 66, block; 66, 67; 66, 72; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:ensure_iterable; 70, argument_list; 70, 71; 71, identifier:except_; 72, if_statement; 72, 73; 72, 77; 73, call; 73, 74; 73, 75; 74, identifier:is_mapping; 75, argument_list; 75, 76; 76, identifier:except_; 77, block; 77, 78; 77, 83; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:ensure_ordered_mapping; 81, argument_list; 81, 82; 82, identifier:except_; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:except_; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:except_; 89, identifier:items; 90, argument_list; 91, function_definition; 91, 92; 91, 93; 91, 94; 92, function_name:handle_exception; 93, parameters; 94, block; 94, 95; 94, 97; 94, 111; 94, 129; 95, expression_statement; 95, 96; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 102; 99, pattern_list; 99, 100; 99, 101; 100, identifier:exc_type; 101, identifier:exc_object; 102, subscript; 102, 103; 102, 108; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:sys; 106, identifier:exc_info; 107, argument_list; 108, slice; 108, 109; 108, 110; 109, colon; 110, integer:2; 111, for_statement; 111, 112; 111, 115; 111, 116; 112, pattern_list; 112, 113; 112, 114; 113, identifier:t; 114, identifier:handler; 115, identifier:except_; 116, block; 116, 117; 117, if_statement; 117, 118; 117, 123; 118, call; 118, 119; 118, 120; 119, identifier:issubclass; 120, argument_list; 120, 121; 120, 122; 121, identifier:exc_type; 122, identifier:t; 123, block; 123, 124; 124, return_statement; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:handler; 127, argument_list; 127, 128; 128, identifier:exc_object; 129, raise_statement; 130, if_statement; 130, 131; 130, 132; 130, 190; 131, identifier:else_; 132, block; 132, 133; 132, 138; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:ensure_callable; 136, argument_list; 136, 137; 137, identifier:else_; 138, if_statement; 138, 139; 138, 140; 138, 170; 139, identifier:finally_; 140, block; 140, 141; 140, 146; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:ensure_callable; 144, argument_list; 144, 145; 145, identifier:finally_; 146, try_statement; 146, 147; 146, 152; 146, 158; 146, 164; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:block; 151, argument_list; 152, except_clause; 152, 153; 153, block; 153, 154; 154, return_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:handle_exception; 157, argument_list; 158, else_clause; 158, 159; 159, block; 159, 160; 160, return_statement; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:else_; 163, argument_list; 164, finally_clause; 164, 165; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:finally_; 169, argument_list; 170, else_clause; 170, 171; 171, block; 171, 172; 172, try_statement; 172, 173; 172, 178; 172, 184; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 177; 176, identifier:block; 177, argument_list; 178, except_clause; 178, 179; 179, block; 179, 180; 180, return_statement; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:handle_exception; 183, argument_list; 184, else_clause; 184, 185; 185, block; 185, 186; 186, return_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:else_; 189, argument_list; 190, else_clause; 190, 191; 191, block; 191, 192; 192, if_statement; 192, 193; 192, 194; 192, 218; 193, identifier:finally_; 194, block; 194, 195; 194, 200; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 198; 197, identifier:ensure_callable; 198, argument_list; 198, 199; 199, identifier:finally_; 200, try_statement; 200, 201; 200, 206; 200, 212; 201, block; 201, 202; 202, return_statement; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:block; 205, argument_list; 206, except_clause; 206, 207; 207, block; 207, 208; 208, return_statement; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:handle_exception; 211, argument_list; 212, finally_clause; 212, 213; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:finally_; 217, argument_list; 218, else_clause; 218, 219; 219, block; 219, 220; 220, try_statement; 220, 221; 220, 226; 221, block; 221, 222; 222, return_statement; 222, 223; 223, call; 223, 224; 223, 225; 224, identifier:block; 225, argument_list; 226, except_clause; 226, 227; 227, block; 227, 228; 228, return_statement; 228, 229; 229, call; 229, 230; 229, 231; 230, identifier:handle_exception; 231, argument_list; 232, elif_clause; 232, 233; 232, 234; 233, identifier:finally_; 234, block; 234, 235; 234, 240; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 238; 237, identifier:ensure_callable; 238, argument_list; 238, 239; 239, identifier:finally_; 240, try_statement; 240, 241; 240, 246; 241, block; 241, 242; 242, return_statement; 242, 243; 243, call; 243, 244; 243, 245; 244, identifier:block; 245, argument_list; 246, finally_clause; 246, 247; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 251; 250, identifier:finally_; 251, argument_list | def try_(block, except_=None, else_=None, finally_=None):
"""Emulate a ``try`` block.
:param block: Function to be executed within the ``try`` statement
:param except_: Function to execute when an :class:`Exception` occurs.
It receives a single argument: the exception object.
Alternatively, a list of key-value pairs can be passed,
mapping exception types to their handler functions.
:param else_: Function to execute when ``block`` completes successfully.
Note that it requires ``except_`` to be provided as well
:param finally_: Function to execute at the end,
regardless of whether an exception occurred or not
:return: If no exception was raised while executing ``block``,
the result of ``else_`` (if provided) or ``block`` is returned.
Otherwise, the result of ``except_`` is returned.
Note that :func:`try_` can _still_ raise an exception if it occurs
while evaluating ``except_``, ``else_`` or ``finally_`` functions.
"""
ensure_callable(block)
if not (except_ or else_ or finally_):
raise TypeError("at least one of `except_`, `else_` or `finally_` "
"functions must be provided")
if else_ and not except_:
raise TypeError("`else_` can only be provided along with `except_`")
if except_:
if callable(except_):
except_ = [(Exception, except_)]
else:
ensure_iterable(except_)
if is_mapping(except_):
ensure_ordered_mapping(except_)
except_ = except_.items()
def handle_exception():
"""Dispatch current exception to proper handler in ``except_``."""
exc_type, exc_object = sys.exc_info()[:2]
for t, handler in except_:
if issubclass(exc_type, t):
return handler(exc_object)
raise
if else_:
ensure_callable(else_)
if finally_:
ensure_callable(finally_)
try:
block()
except:
return handle_exception()
else:
return else_()
finally:
finally_()
else:
try:
block()
except:
return handle_exception()
else:
return else_()
else:
if finally_:
ensure_callable(finally_)
try:
return block()
except:
return handle_exception()
finally:
finally_()
else:
try:
return block()
except:
return handle_exception()
elif finally_:
ensure_callable(finally_)
try:
return block()
finally:
finally_() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:replace_u_end_day; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:day; 5, identifier:year; 6, identifier:month; 7, block; 7, 8; 7, 10; 7, 19; 7, 26; 7, 38; 7, 61; 7, 78; 7, 114; 7, 115; 7, 134; 7, 135; 7, 160; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:day; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:day; 16, identifier:lstrip; 17, argument_list; 17, 18; 18, string:'-'; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:year; 22, call; 22, 23; 22, 24; 23, identifier:int; 24, argument_list; 24, 25; 25, identifier:year; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:month; 29, call; 29, 30; 29, 31; 30, identifier:int; 31, argument_list; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:month; 35, identifier:lstrip; 36, argument_list; 36, 37; 37, string:'-'; 38, if_statement; 38, 39; 38, 46; 38, 47; 39, boolean_operator:or; 39, 40; 39, 43; 40, comparison_operator:==; 40, 41; 40, 42; 41, identifier:day; 42, string:'uu'; 43, comparison_operator:==; 43, 44; 43, 45; 44, identifier:day; 45, string:'3u'; 46, comment; 47, block; 47, 48; 48, return_statement; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:str; 51, argument_list; 51, 52; 52, subscript; 52, 53; 52, 60; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:calendar; 56, identifier:monthrange; 57, argument_list; 57, 58; 57, 59; 58, identifier:year; 59, identifier:month; 60, integer:1; 61, if_statement; 61, 62; 61, 69; 62, boolean_operator:or; 62, 63; 62, 66; 63, comparison_operator:==; 63, 64; 63, 65; 64, identifier:day; 65, string:'0u'; 66, comparison_operator:==; 66, 67; 66, 68; 67, identifier:day; 68, string:'1u'; 69, block; 69, 70; 70, return_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:day; 74, identifier:replace; 75, argument_list; 75, 76; 75, 77; 76, string:'u'; 77, string:'9'; 78, if_statement; 78, 79; 78, 86; 79, boolean_operator:or; 79, 80; 79, 83; 80, comparison_operator:==; 80, 81; 80, 82; 81, identifier:day; 82, string:'2u'; 83, comparison_operator:==; 83, 84; 83, 85; 84, identifier:day; 85, string:'u9'; 86, block; 86, 87; 87, if_statement; 87, 88; 87, 98; 87, 101; 87, 109; 88, boolean_operator:or; 88, 89; 88, 92; 89, comparison_operator:!=; 89, 90; 89, 91; 90, identifier:month; 91, string:'02'; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:calendar; 95, identifier:isleap; 96, argument_list; 96, 97; 97, identifier:year; 98, block; 98, 99; 99, return_statement; 99, 100; 100, string:'29'; 101, elif_clause; 101, 102; 101, 105; 101, 106; 102, comparison_operator:==; 102, 103; 102, 104; 103, identifier:day; 104, string:'2u'; 105, comment; 106, block; 106, 107; 107, return_statement; 107, 108; 108, string:'28'; 109, else_clause; 109, 110; 109, 111; 110, comment; 111, block; 111, 112; 112, return_statement; 112, 113; 113, string:'19'; 114, comment; 115, if_statement; 115, 116; 115, 125; 116, comparison_operator:<; 116, 117; 116, 118; 116, 124; 117, integer:1; 118, call; 118, 119; 118, 120; 119, identifier:int; 120, argument_list; 120, 121; 121, subscript; 121, 122; 121, 123; 122, identifier:day; 123, integer:1; 124, integer:9; 125, block; 125, 126; 126, return_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:day; 130, identifier:replace; 131, argument_list; 131, 132; 131, 133; 132, string:'u'; 133, string:'2'; 134, comment; 135, if_statement; 135, 136; 135, 139; 136, comparison_operator:==; 136, 137; 136, 138; 137, identifier:day; 138, string:'u1'; 139, block; 139, 140; 140, if_statement; 140, 141; 140, 152; 140, 153; 140, 156; 141, comparison_operator:==; 141, 142; 141, 151; 142, subscript; 142, 143; 142, 150; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:calendar; 146, identifier:monthrange; 147, argument_list; 147, 148; 147, 149; 148, identifier:year; 149, identifier:month; 150, integer:1; 151, integer:31; 152, comment; 153, block; 153, 154; 154, return_statement; 154, 155; 155, string:'31'; 156, else_clause; 156, 157; 157, block; 157, 158; 158, return_statement; 158, 159; 159, string:'21'; 160, if_statement; 160, 161; 160, 164; 160, 180; 161, comparison_operator:==; 161, 162; 161, 163; 162, identifier:day; 163, string:'u0'; 164, block; 164, 165; 165, if_statement; 165, 166; 165, 177; 166, comparison_operator:>=; 166, 167; 166, 176; 167, subscript; 167, 168; 167, 175; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:calendar; 171, identifier:monthrange; 172, argument_list; 172, 173; 172, 174; 173, identifier:year; 174, identifier:month; 175, integer:1; 176, integer:30; 177, block; 177, 178; 178, return_statement; 178, 179; 179, string:'30'; 180, else_clause; 180, 181; 181, block; 181, 182; 182, return_statement; 182, 183; 183, string:'20' | def replace_u_end_day(day, year, month):
"""Find the latest legitimate day."""
day = day.lstrip('-')
year = int(year)
month = int(month.lstrip('-'))
if day == 'uu' or day == '3u':
# Use the last day of the month for a given year/month.
return str(calendar.monthrange(year, month)[1])
if day == '0u' or day == '1u':
return day.replace('u', '9')
if day == '2u' or day == 'u9':
if month != '02' or calendar.isleap(year):
return '29'
elif day == '2u':
# It is Feburary and not a leap year.
return '28'
else:
# It is February, not a leap year, day ends in 9.
return '19'
# 'u2' 'u3' 'u4' 'u5' 'u6' 'u7' 'u8'
if 1 < int(day[1]) < 9:
return day.replace('u', '2')
# 'u0' 'u1'
if day == 'u1':
if calendar.monthrange(year, month)[1] == 31:
# See if the month has a 31st.
return '31'
else:
return '21'
if day == 'u0':
if calendar.monthrange(year, month)[1] >= 30:
return '30'
else:
return '20' |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:zero_year_special_case; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:from_date; 5, identifier:to_date; 6, identifier:start; 7, identifier:end; 8, block; 8, 9; 8, 11; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 19; 11, 20; 11, 255; 11, 256; 11, 257; 11, 268; 11, 269; 12, boolean_operator:and; 12, 13; 12, 16; 13, comparison_operator:==; 13, 14; 13, 15; 14, identifier:start; 15, string:'pos'; 16, comparison_operator:==; 16, 17; 16, 18; 17, identifier:end; 18, string:'pos'; 19, comment; 20, block; 20, 21; 20, 39; 20, 40; 20, 58; 20, 59; 21, if_statement; 21, 22; 21, 36; 22, boolean_operator:and; 22, 23; 22, 29; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:from_date; 26, identifier:startswith; 27, argument_list; 27, 28; 28, string:'0000'; 29, not_operator; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:to_date; 33, identifier:startswith; 34, argument_list; 34, 35; 35, string:'0000'; 36, block; 36, 37; 37, return_statement; 37, 38; 38, True; 39, comment; 40, if_statement; 40, 41; 40, 55; 41, boolean_operator:and; 41, 42; 41, 49; 42, not_operator; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:from_date; 46, identifier:startswith; 47, argument_list; 47, 48; 48, string:'0000'; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:to_date; 52, identifier:startswith; 53, argument_list; 53, 54; 54, string:'0000'; 55, block; 55, 56; 56, return_statement; 56, 57; 57, False; 58, comment; 59, if_statement; 59, 60; 59, 73; 59, 74; 59, 75; 60, boolean_operator:and; 60, 61; 60, 67; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:from_date; 64, identifier:startswith; 65, argument_list; 65, 66; 66, string:'0000'; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:to_date; 70, identifier:startswith; 71, argument_list; 71, 72; 72, string:'0000'; 73, comment; 74, comment; 75, block; 75, 76; 75, 150; 75, 151; 75, 152; 75, 226; 75, 227; 75, 234; 75, 235; 76, if_statement; 76, 77; 76, 83; 76, 92; 76, 93; 76, 117; 76, 118; 77, comparison_operator:==; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:len; 80, argument_list; 80, 81; 81, identifier:from_date; 82, integer:4; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, pattern_list; 86, 87; 86, 88; 87, identifier:fm; 88, identifier:fd; 89, expression_list; 89, 90; 89, 91; 90, integer:1; 91, integer:1; 92, comment; 93, elif_clause; 93, 94; 93, 100; 94, comparison_operator:==; 94, 95; 94, 99; 95, call; 95, 96; 95, 97; 96, identifier:len; 97, argument_list; 97, 98; 98, identifier:from_date; 99, integer:7; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, pattern_list; 103, 104; 103, 105; 104, identifier:fm; 105, identifier:fd; 106, expression_list; 106, 107; 106, 116; 107, call; 107, 108; 107, 109; 108, identifier:int; 109, argument_list; 109, 110; 110, subscript; 110, 111; 110, 112; 111, identifier:from_date; 112, slice; 112, 113; 112, 114; 112, 115; 113, integer:5; 114, colon; 115, integer:7; 116, integer:1; 117, comment; 118, elif_clause; 118, 119; 118, 125; 119, comparison_operator:==; 119, 120; 119, 124; 120, call; 120, 121; 120, 122; 121, identifier:len; 122, argument_list; 122, 123; 123, identifier:from_date; 124, integer:10; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, pattern_list; 128, 129; 128, 130; 129, identifier:fm; 130, identifier:fd; 131, expression_list; 131, 132; 131, 141; 132, call; 132, 133; 132, 134; 133, identifier:int; 134, argument_list; 134, 135; 135, subscript; 135, 136; 135, 137; 136, identifier:from_date; 137, slice; 137, 138; 137, 139; 137, 140; 138, integer:5; 139, colon; 140, integer:7; 141, call; 141, 142; 141, 143; 142, identifier:int; 143, argument_list; 143, 144; 144, subscript; 144, 145; 144, 146; 145, identifier:from_date; 146, slice; 146, 147; 146, 148; 146, 149; 147, integer:8; 148, colon; 149, integer:10; 150, comment; 151, comment; 152, if_statement; 152, 153; 152, 159; 152, 168; 152, 169; 152, 193; 152, 194; 153, comparison_operator:==; 153, 154; 153, 158; 154, call; 154, 155; 154, 156; 155, identifier:len; 156, argument_list; 156, 157; 157, identifier:to_date; 158, integer:4; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 165; 162, pattern_list; 162, 163; 162, 164; 163, identifier:tm; 164, identifier:td; 165, expression_list; 165, 166; 165, 167; 166, integer:1; 167, integer:1; 168, comment; 169, elif_clause; 169, 170; 169, 176; 170, comparison_operator:==; 170, 171; 170, 175; 171, call; 171, 172; 171, 173; 172, identifier:len; 173, argument_list; 173, 174; 174, identifier:to_date; 175, integer:7; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, pattern_list; 179, 180; 179, 181; 180, identifier:tm; 181, identifier:td; 182, expression_list; 182, 183; 182, 192; 183, call; 183, 184; 183, 185; 184, identifier:int; 185, argument_list; 185, 186; 186, subscript; 186, 187; 186, 188; 187, identifier:to_date; 188, slice; 188, 189; 188, 190; 188, 191; 189, integer:5; 190, colon; 191, integer:7; 192, integer:1; 193, comment; 194, elif_clause; 194, 195; 194, 201; 195, comparison_operator:==; 195, 196; 195, 200; 196, call; 196, 197; 196, 198; 197, identifier:len; 198, argument_list; 198, 199; 199, identifier:to_date; 200, integer:10; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, pattern_list; 204, 205; 204, 206; 205, identifier:tm; 206, identifier:td; 207, expression_list; 207, 208; 207, 217; 208, call; 208, 209; 208, 210; 209, identifier:int; 210, argument_list; 210, 211; 211, subscript; 211, 212; 211, 213; 212, identifier:to_date; 213, slice; 213, 214; 213, 215; 213, 216; 214, integer:5; 215, colon; 216, integer:7; 217, call; 217, 218; 217, 219; 218, identifier:int; 219, argument_list; 219, 220; 220, subscript; 220, 221; 220, 222; 221, identifier:to_date; 222, slice; 222, 223; 222, 224; 222, 225; 223, integer:8; 224, colon; 225, integer:10; 226, comment; 227, if_statement; 227, 228; 227, 231; 228, comparison_operator:==; 228, 229; 228, 230; 229, identifier:from_date; 230, identifier:to_date; 231, block; 231, 232; 232, return_statement; 232, 233; 233, True; 234, comment; 235, if_statement; 235, 236; 235, 239; 235, 251; 236, comparison_operator:<=; 236, 237; 236, 238; 237, identifier:fm; 238, identifier:tm; 239, block; 239, 240; 240, if_statement; 240, 241; 240, 244; 240, 247; 241, comparison_operator:<=; 241, 242; 241, 243; 242, identifier:fd; 243, identifier:td; 244, block; 244, 245; 245, return_statement; 245, 246; 246, True; 247, else_clause; 247, 248; 248, block; 248, 249; 249, return_statement; 249, 250; 250, False; 251, else_clause; 251, 252; 252, block; 252, 253; 253, return_statement; 253, 254; 254, False; 255, comment; 256, comment; 257, elif_clause; 257, 258; 257, 265; 258, boolean_operator:and; 258, 259; 258, 262; 259, comparison_operator:==; 259, 260; 259, 261; 260, identifier:start; 261, string:'neg'; 262, comparison_operator:==; 262, 263; 262, 264; 263, identifier:end; 264, string:'neg'; 265, block; 265, 266; 266, return_statement; 266, 267; 267, False; 268, comment; 269, elif_clause; 269, 270; 269, 277; 270, boolean_operator:and; 270, 271; 270, 274; 271, comparison_operator:==; 271, 272; 271, 273; 272, identifier:start; 273, string:'neg'; 274, comparison_operator:==; 274, 275; 274, 276; 275, identifier:end; 276, string:'pos'; 277, block; 277, 278; 278, if_statement; 278, 279; 278, 285; 278, 288; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:from_date; 282, identifier:startswith; 283, argument_list; 283, 284; 284, string:"0000"; 285, block; 285, 286; 286, return_statement; 286, 287; 287, False; 288, else_clause; 288, 289; 289, block; 289, 290; 290, return_statement; 290, 291; 291, True | def zero_year_special_case(from_date, to_date, start, end):
"""strptime does not resolve a 0000 year, we must handle this."""
if start == 'pos' and end == 'pos':
# always interval from earlier to later
if from_date.startswith('0000') and not to_date.startswith('0000'):
return True
# always interval from later to earlier
if not from_date.startswith('0000') and to_date.startswith('0000'):
return False
# an interval from 0000-MM-DD/0000-MM-DD ??? PARSE !!!
if from_date.startswith('0000') and to_date.startswith('0000'):
# fill from date assuming first subsequent date object if missing
# missing m+d, assume jan 1
if len(from_date) == 4:
fm, fd = 1, 1
# missing d, assume the 1st
elif len(from_date) == 7:
fm, fd = int(from_date[5:7]), 1
# not missing any date objects
elif len(from_date) == 10:
fm, fd = int(from_date[5:7]), int(from_date[8:10])
# fill to date assuming first subsequent date object if missing
# missing m+d, assume jan 1
if len(to_date) == 4:
tm, td = 1, 1
# missing d, assume the 1st
elif len(to_date) == 7:
tm, td = int(to_date[5:7]), 1
# not missing any date objects
elif len(to_date) == 10:
tm, td = int(to_date[5:7]), int(to_date[8:10])
# equality check
if from_date == to_date:
return True
# compare the dates
if fm <= tm:
if fd <= td:
return True
else:
return False
else:
return False
# these cases are always one way or the other
# "-0000" is an invalid edtf
elif start == 'neg' and end == 'neg':
return False
# False unless start is not "0000"
elif start == 'neg' and end == 'pos':
if from_date.startswith("0000"):
return False
else:
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:is_valid_interval; 3, parameters; 3, 4; 4, identifier:edtf_candidate; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 17; 5, 18; 5, 26; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:from_date; 12, None; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:to_date; 16, None; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 23; 20, pattern_list; 20, 21; 20, 22; 21, identifier:end; 22, identifier:start; 23, expression_list; 23, 24; 23, 25; 24, string:'pos'; 25, string:'pos'; 26, if_statement; 26, 27; 26, 35; 26, 36; 26, 37; 26, 446; 27, comparison_operator:==; 27, 28; 27, 34; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:edtf_candidate; 31, identifier:count; 32, argument_list; 32, 33; 33, string:'/'; 34, integer:1; 35, comment; 36, comment; 37, block; 37, 38; 37, 46; 37, 57; 37, 66; 37, 67; 37, 93; 37, 94; 37, 120; 37, 121; 37, 132; 37, 133; 37, 164; 37, 165; 37, 191; 37, 217; 37, 218; 37, 244; 37, 270; 37, 271; 37, 319; 37, 365; 37, 366; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:edtf_candidate; 41, call; 41, 42; 41, 43; 42, identifier:replace_all; 43, argument_list; 43, 44; 43, 45; 44, identifier:edtf_candidate; 45, identifier:interval_replacements; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:edtf_candidate; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:re; 52, identifier:sub; 53, argument_list; 53, 54; 53, 55; 53, 56; 54, identifier:U_PATTERN; 55, identifier:replace_u; 56, identifier:edtf_candidate; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:parts; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:edtf_candidate; 63, identifier:split; 64, argument_list; 64, 65; 65, string:'/'; 66, comment; 67, if_statement; 67, 68; 67, 76; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:parts; 72, integer:0; 73, identifier:startswith; 74, argument_list; 74, 75; 75, string:"-"; 76, block; 76, 77; 76, 81; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:start; 80, string:'neg'; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:parts; 85, integer:0; 86, subscript; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:parts; 89, integer:0; 90, slice; 90, 91; 90, 92; 91, integer:1; 92, colon; 93, comment; 94, if_statement; 94, 95; 94, 103; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:parts; 99, integer:1; 100, identifier:startswith; 101, argument_list; 101, 102; 102, string:"-"; 103, block; 103, 104; 103, 108; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:end; 107, string:'neg'; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:parts; 112, integer:1; 113, subscript; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:parts; 116, integer:1; 117, slice; 117, 118; 117, 119; 118, integer:1; 119, colon; 120, comment; 121, if_statement; 121, 122; 121, 129; 122, boolean_operator:and; 122, 123; 122, 126; 123, comparison_operator:==; 123, 124; 123, 125; 124, identifier:start; 125, string:'pos'; 126, comparison_operator:==; 126, 127; 126, 128; 127, identifier:end; 128, string:'neg'; 129, block; 129, 130; 130, return_statement; 130, 131; 131, False; 132, comment; 133, if_statement; 133, 134; 133, 151; 134, boolean_operator:or; 134, 135; 134, 143; 135, call; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:parts; 139, integer:0; 140, identifier:startswith; 141, argument_list; 141, 142; 142, string:"0000"; 143, call; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:parts; 147, integer:1; 148, identifier:startswith; 149, argument_list; 149, 150; 150, string:"0000"; 151, block; 151, 152; 152, return_statement; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:zero_year_special_case; 155, argument_list; 155, 156; 155, 159; 155, 162; 155, 163; 156, subscript; 156, 157; 156, 158; 157, identifier:parts; 158, integer:0; 159, subscript; 159, 160; 159, 161; 160, identifier:parts; 161, integer:1; 162, identifier:start; 163, identifier:end; 164, comment; 165, if_statement; 165, 166; 165, 176; 166, comparison_operator:==; 166, 167; 166, 175; 167, call; 167, 168; 167, 173; 168, attribute; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:parts; 171, integer:0; 172, identifier:count; 173, argument_list; 173, 174; 174, string:"-"; 175, integer:2; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:from_date; 180, call; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:datetime; 184, identifier:datetime; 185, identifier:strptime; 186, argument_list; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:parts; 189, integer:0; 190, string:"%Y-%m-%d"; 191, if_statement; 191, 192; 191, 202; 192, comparison_operator:==; 192, 193; 192, 201; 193, call; 193, 194; 193, 199; 194, attribute; 194, 195; 194, 198; 195, subscript; 195, 196; 195, 197; 196, identifier:parts; 197, integer:1; 198, identifier:count; 199, argument_list; 199, 200; 200, string:"-"; 201, integer:2; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:to_date; 206, call; 206, 207; 206, 212; 207, attribute; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:datetime; 210, identifier:datetime; 211, identifier:strptime; 212, argument_list; 212, 213; 212, 216; 213, subscript; 213, 214; 213, 215; 214, identifier:parts; 215, integer:1; 216, string:"%Y-%m-%d"; 217, comment; 218, if_statement; 218, 219; 218, 229; 219, comparison_operator:==; 219, 220; 219, 228; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:parts; 224, integer:0; 225, identifier:count; 226, argument_list; 226, 227; 227, string:"-"; 228, integer:1; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:from_date; 233, call; 233, 234; 233, 239; 234, attribute; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:datetime; 237, identifier:datetime; 238, identifier:strptime; 239, argument_list; 239, 240; 239, 243; 240, subscript; 240, 241; 240, 242; 241, identifier:parts; 242, integer:0; 243, string:"%Y-%m"; 244, if_statement; 244, 245; 244, 255; 245, comparison_operator:==; 245, 246; 245, 254; 246, call; 246, 247; 246, 252; 247, attribute; 247, 248; 247, 251; 248, subscript; 248, 249; 248, 250; 249, identifier:parts; 250, integer:1; 251, identifier:count; 252, argument_list; 252, 253; 253, string:"-"; 254, integer:1; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:to_date; 259, call; 259, 260; 259, 265; 260, attribute; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:datetime; 263, identifier:datetime; 264, identifier:strptime; 265, argument_list; 265, 266; 265, 269; 266, subscript; 266, 267; 266, 268; 267, identifier:parts; 268, integer:1; 269, string:"%Y-%m"; 270, comment; 271, if_statement; 271, 272; 271, 282; 271, 283; 272, comparison_operator:==; 272, 273; 272, 281; 273, call; 273, 274; 273, 279; 274, attribute; 274, 275; 274, 278; 275, subscript; 275, 276; 275, 277; 276, identifier:parts; 277, integer:0; 278, identifier:count; 279, argument_list; 279, 280; 280, string:"-"; 281, integer:0; 282, comment; 283, block; 283, 284; 284, if_statement; 284, 285; 284, 290; 284, 303; 285, comparison_operator:==; 285, 286; 285, 289; 286, subscript; 286, 287; 286, 288; 287, identifier:parts; 288, integer:0; 289, string:'unknown'; 290, block; 290, 291; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:from_date; 294, call; 294, 295; 294, 300; 295, attribute; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:datetime; 298, identifier:datetime; 299, identifier:strptime; 300, argument_list; 300, 301; 300, 302; 301, string:"0001"; 302, string:"%Y"; 303, else_clause; 303, 304; 304, block; 304, 305; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:from_date; 308, call; 308, 309; 308, 314; 309, attribute; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:datetime; 312, identifier:datetime; 313, identifier:strptime; 314, argument_list; 314, 315; 314, 318; 315, subscript; 315, 316; 315, 317; 316, identifier:parts; 317, integer:0; 318, string:"%Y"; 319, if_statement; 319, 320; 319, 330; 319, 331; 320, comparison_operator:==; 320, 321; 320, 329; 321, call; 321, 322; 321, 327; 322, attribute; 322, 323; 322, 326; 323, subscript; 323, 324; 323, 325; 324, identifier:parts; 325, integer:1; 326, identifier:count; 327, argument_list; 327, 328; 328, string:"-"; 329, integer:0; 330, comment; 331, block; 331, 332; 332, if_statement; 332, 333; 332, 344; 332, 349; 333, boolean_operator:or; 333, 334; 333, 339; 334, comparison_operator:==; 334, 335; 334, 338; 335, subscript; 335, 336; 335, 337; 336, identifier:parts; 337, integer:1; 338, string:'open'; 339, comparison_operator:==; 339, 340; 339, 343; 340, subscript; 340, 341; 340, 342; 341, identifier:parts; 342, integer:1; 343, string:'unknown'; 344, block; 344, 345; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:to_date; 348, string:'open'; 349, else_clause; 349, 350; 350, block; 350, 351; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 354; 353, identifier:to_date; 354, call; 354, 355; 354, 360; 355, attribute; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:datetime; 358, identifier:datetime; 359, identifier:strptime; 360, argument_list; 360, 361; 360, 364; 361, subscript; 361, 362; 361, 363; 362, identifier:parts; 363, integer:1; 364, string:"%Y"; 365, comment; 366, if_statement; 366, 367; 366, 374; 366, 377; 366, 378; 366, 398; 366, 399; 366, 421; 366, 422; 366, 442; 367, boolean_operator:and; 367, 368; 367, 371; 368, comparison_operator:==; 368, 369; 368, 370; 369, identifier:start; 370, string:'neg'; 371, comparison_operator:==; 371, 372; 371, 373; 372, identifier:end; 373, string:'pos'; 374, block; 374, 375; 375, return_statement; 375, 376; 376, True; 377, comment; 378, elif_clause; 378, 379; 378, 386; 379, boolean_operator:and; 379, 380; 379, 383; 380, comparison_operator:==; 380, 381; 380, 382; 381, identifier:start; 382, string:'neg'; 383, comparison_operator:==; 383, 384; 383, 385; 384, identifier:end; 385, string:'neg'; 386, block; 386, 387; 387, if_statement; 387, 388; 387, 395; 388, boolean_operator:and; 388, 389; 388, 394; 389, boolean_operator:and; 389, 390; 389, 393; 390, comparison_operator:>=; 390, 391; 390, 392; 391, identifier:from_date; 392, identifier:to_date; 393, identifier:from_date; 394, identifier:to_date; 395, block; 395, 396; 396, return_statement; 396, 397; 397, True; 398, comment; 399, elif_clause; 399, 400; 399, 418; 400, parenthesized_expression; 400, 401; 401, boolean_operator:or; 401, 402; 401, 413; 402, boolean_operator:or; 402, 403; 402, 408; 403, comparison_operator:==; 403, 404; 403, 407; 404, subscript; 404, 405; 404, 406; 405, identifier:parts; 406, integer:1; 407, string:'unknown'; 408, comparison_operator:==; 408, 409; 408, 412; 409, subscript; 409, 410; 409, 411; 410, identifier:parts; 411, integer:1; 412, string:'open'; 413, comparison_operator:==; 413, 414; 413, 417; 414, subscript; 414, 415; 414, 416; 415, identifier:parts; 416, integer:0; 417, string:'unknown'; 418, block; 418, 419; 419, return_statement; 419, 420; 420, True; 421, comment; 422, elif_clause; 422, 423; 422, 430; 423, boolean_operator:and; 423, 424; 423, 427; 424, comparison_operator:==; 424, 425; 424, 426; 425, identifier:start; 426, string:'pos'; 427, comparison_operator:==; 427, 428; 427, 429; 428, identifier:end; 429, string:'pos'; 430, block; 430, 431; 431, if_statement; 431, 432; 431, 439; 432, boolean_operator:and; 432, 433; 432, 438; 433, boolean_operator:and; 433, 434; 433, 437; 434, comparison_operator:<=; 434, 435; 434, 436; 435, identifier:from_date; 436, identifier:to_date; 437, identifier:from_date; 438, identifier:to_date; 439, block; 439, 440; 440, return_statement; 440, 441; 441, True; 442, else_clause; 442, 443; 443, block; 443, 444; 444, return_statement; 444, 445; 445, False; 446, else_clause; 446, 447; 447, block; 447, 448; 448, return_statement; 448, 449; 449, False | def is_valid_interval(edtf_candidate):
"""Test to see if the edtf candidate is a valid interval"""
# resolve interval into from / to datetime objects
from_date = None
to_date = None
# initialize interval flags for special cases, assume positive
end, start = 'pos', 'pos'
if edtf_candidate.count('/') == 1:
# replace all 'problem' cases (unspecified, 0000 date, ?~, -, y)
# break the interval into two date strings
edtf_candidate = replace_all(edtf_candidate, interval_replacements)
edtf_candidate = re.sub(U_PATTERN, replace_u, edtf_candidate)
parts = edtf_candidate.split('/')
# set flag for negative start date
if parts[0].startswith("-"):
start = 'neg'
parts[0] = parts[0][1:]
# set flag for negative end date
if parts[1].startswith("-"):
end = 'neg'
parts[1] = parts[1][1:]
# if starts positive and ends negative, that's always False
if start == 'pos' and end == 'neg':
return False
# handle special case of 0000 year
if parts[0].startswith("0000") or parts[1].startswith("0000"):
return zero_year_special_case(parts[0], parts[1], start, end)
# 2 '-' characters means we are matching year-month-day
if parts[0].count("-") == 2:
from_date = datetime.datetime.strptime(parts[0], "%Y-%m-%d")
if parts[1].count("-") == 2:
to_date = datetime.datetime.strptime(parts[1], "%Y-%m-%d")
# 1 '-' character means we are match year-month
if parts[0].count("-") == 1:
from_date = datetime.datetime.strptime(parts[0], "%Y-%m")
if parts[1].count("-") == 1:
to_date = datetime.datetime.strptime(parts[1], "%Y-%m")
# zero '-' characters means we are matching a year
if parts[0].count("-") == 0:
# if from_date is unknown, we can assume the lowest possible date
if parts[0] == 'unknown':
from_date = datetime.datetime.strptime("0001", "%Y")
else:
from_date = datetime.datetime.strptime(parts[0], "%Y")
if parts[1].count("-") == 0:
# when the to_date is open and the from_date is valid, it's valid
if parts[1] == 'open' or parts[1] == 'unknown':
to_date = 'open'
else:
to_date = datetime.datetime.strptime(parts[1], "%Y")
# if it starts negative and ends positive, that's always True
if start == 'neg' and end == 'pos':
return True
# if start and end are negative, the from_date must be >= to_date
elif start == 'neg' and end == 'neg':
if from_date >= to_date and from_date and to_date:
return True
# if the to_date is unknown or open, it could be any date, therefore
elif (
parts[1] == 'unknown' or
parts[1] == 'open' or
parts[0] == 'unknown'
):
return True
# if start and end are positive, the from_date must be <= to_date
elif start == 'pos' and end == 'pos':
if from_date <= to_date and from_date and to_date:
return True
else:
return False
else:
return False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:template; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:page; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:layout; 9, None; 10, dictionary_splat_pattern; 10, 11; 11, identifier:kwargs; 12, block; 12, 13; 12, 15; 12, 19; 12, 238; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:pkey; 18, string:"_template_extends__"; 19, function_definition; 19, 20; 19, 21; 19, 23; 20, function_name:decorator; 21, parameters; 21, 22; 22, identifier:f; 23, block; 23, 24; 24, if_statement; 24, 25; 24, 31; 24, 149; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:inspect; 28, identifier:isclass; 29, argument_list; 29, 30; 30, identifier:f; 31, block; 31, 32; 31, 38; 31, 48; 31, 97; 31, 105; 31, 113; 31, 119; 31, 126; 31, 138; 31, 147; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:layout_; 35, boolean_operator:or; 35, 36; 35, 37; 36, identifier:layout; 37, identifier:page; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:extends; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:kwargs; 44, identifier:pop; 45, argument_list; 45, 46; 45, 47; 46, string:"extends"; 47, None; 48, if_statement; 48, 49; 48, 56; 49, boolean_operator:and; 49, 50; 49, 51; 50, identifier:extends; 51, call; 51, 52; 51, 53; 52, identifier:hasattr; 53, argument_list; 53, 54; 53, 55; 54, identifier:extends; 55, identifier:pkey; 56, block; 56, 57; 56, 69; 56, 83; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:items; 60, call; 60, 61; 60, 68; 61, attribute; 61, 62; 61, 67; 62, call; 62, 63; 62, 64; 63, identifier:getattr; 64, argument_list; 64, 65; 64, 66; 65, identifier:extends; 66, identifier:pkey; 67, identifier:items; 68, argument_list; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:in; 70, 71; 70, 72; 71, string:"layout"; 72, identifier:items; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:layout_; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:items; 80, identifier:pop; 81, argument_list; 81, 82; 82, string:"layout"; 83, for_statement; 83, 84; 83, 87; 83, 88; 84, pattern_list; 84, 85; 84, 86; 85, identifier:k; 86, identifier:v; 87, identifier:items; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:kwargs; 93, identifier:setdefault; 94, argument_list; 94, 95; 94, 96; 95, identifier:k; 96, identifier:v; 97, if_statement; 97, 98; 97, 100; 98, not_operator; 98, 99; 99, identifier:layout_; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:layout_; 104, string:"layout.html"; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:kwargs; 109, identifier:setdefault; 110, argument_list; 110, 111; 110, 112; 111, string:"brand_name"; 112, string:""; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:kwargs; 117, string:"layout"; 118, identifier:layout_; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:setattr; 122, argument_list; 122, 123; 122, 124; 122, 125; 123, identifier:f; 124, identifier:pkey; 125, identifier:kwargs; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:setattr; 129, argument_list; 129, 130; 129, 131; 129, 132; 130, identifier:f; 131, string:"base_layout"; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:kwargs; 135, identifier:get; 136, argument_list; 136, 137; 137, string:"layout"; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:f; 142, identifier:g; 143, argument_list; 143, 144; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:TEMPLATE_CONTEXT; 146, identifier:kwargs; 147, return_statement; 147, 148; 148, identifier:f; 149, else_clause; 149, 150; 150, block; 150, 151; 150, 236; 151, decorated_definition; 151, 152; 151, 159; 152, decorator; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:functools; 156, identifier:wraps; 157, argument_list; 157, 158; 158, identifier:f; 159, function_definition; 159, 160; 159, 161; 159, 166; 160, function_name:wrap; 161, parameters; 161, 162; 161, 164; 162, list_splat_pattern; 162, 163; 163, identifier:args2; 164, dictionary_splat_pattern; 164, 165; 165, identifier:kwargs2; 166, block; 166, 167; 166, 177; 166, 234; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:response; 170, call; 170, 171; 170, 172; 171, identifier:f; 172, argument_list; 172, 173; 172, 175; 173, list_splat; 173, 174; 174, identifier:args2; 175, dictionary_splat; 175, 176; 176, identifier:kwargs2; 177, if_statement; 177, 178; 177, 187; 178, boolean_operator:or; 178, 179; 178, 184; 179, call; 179, 180; 179, 181; 180, identifier:isinstance; 181, argument_list; 181, 182; 181, 183; 182, identifier:response; 183, identifier:dict; 184, comparison_operator:is; 184, 185; 184, 186; 185, identifier:response; 186, None; 187, block; 187, 188; 187, 194; 187, 205; 187, 216; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:response; 191, boolean_operator:or; 191, 192; 191, 193; 192, identifier:response; 193, dictionary; 194, if_statement; 194, 195; 194, 196; 195, identifier:page; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:response; 201, identifier:setdefault; 202, argument_list; 202, 203; 202, 204; 203, string:"template_"; 204, identifier:page; 205, if_statement; 205, 206; 205, 207; 206, identifier:layout; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:response; 212, identifier:setdefault; 213, argument_list; 213, 214; 213, 215; 214, string:"layout_"; 215, identifier:layout; 216, for_statement; 216, 217; 216, 220; 216, 225; 217, pattern_list; 217, 218; 217, 219; 218, identifier:k; 219, identifier:v; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:kwargs; 223, identifier:items; 224, argument_list; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:response; 230, identifier:setdefault; 231, argument_list; 231, 232; 231, 233; 232, identifier:k; 233, identifier:v; 234, return_statement; 234, 235; 235, identifier:response; 236, return_statement; 236, 237; 237, identifier:wrap; 238, return_statement; 238, 239; 239, identifier:decorator | def template(page=None, layout=None, **kwargs):
"""
Decorator to change the view template and layout.
It works on both View class and view methods
on class
only $layout is applied, everything else will be passed to the kwargs
Using as first argument, it will be the layout.
:first arg or $layout: The layout to use for that view
:param layout: The layout to use for that view
:param kwargs:
get pass to the TEMPLATE_CONTEXT
** on method that return a dict
page or layout are optional
:param page: The html page
:param layout: The layout to use for that view
:param kwargs:
get pass to the view as k/V
** on other methods that return other type, it doesn't apply
:return:
"""
pkey = "_template_extends__"
def decorator(f):
if inspect.isclass(f):
layout_ = layout or page
extends = kwargs.pop("extends", None)
if extends and hasattr(extends, pkey):
items = getattr(extends, pkey).items()
if "layout" in items:
layout_ = items.pop("layout")
for k, v in items:
kwargs.setdefault(k, v)
if not layout_:
layout_ = "layout.html"
kwargs.setdefault("brand_name", "")
kwargs["layout"] = layout_
setattr(f, pkey, kwargs)
setattr(f, "base_layout", kwargs.get("layout"))
f.g(TEMPLATE_CONTEXT=kwargs)
return f
else:
@functools.wraps(f)
def wrap(*args2, **kwargs2):
response = f(*args2, **kwargs2)
if isinstance(response, dict) or response is None:
response = response or {}
if page:
response.setdefault("template_", page)
if layout:
response.setdefault("layout_", layout)
for k, v in kwargs.items():
response.setdefault(k, v)
return response
return wrap
return decorator |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:getargspec; 3, parameters; 3, 4; 4, identifier:obj; 5, block; 5, 6; 5, 8; 5, 20; 5, 124; 5, 125; 5, 126; 5, 127; 5, 140; 5, 207; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 15; 10, pattern_list; 10, 11; 10, 12; 10, 13; 10, 14; 11, identifier:argnames; 12, identifier:varargs; 13, identifier:varkw; 14, identifier:_defaults; 15, expression_list; 15, 16; 15, 17; 15, 18; 15, 19; 16, None; 17, None; 18, None; 19, None; 20, if_statement; 20, 21; 20, 34; 20, 48; 20, 95; 20, 117; 21, boolean_operator:or; 21, 22; 21, 28; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:inspect; 25, identifier:isfunction; 26, argument_list; 26, 27; 27, identifier:obj; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:inspect; 31, identifier:ismethod; 32, argument_list; 32, 33; 33, identifier:obj; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 42; 37, pattern_list; 37, 38; 37, 39; 37, 40; 37, 41; 38, identifier:argnames; 39, identifier:varargs; 40, identifier:varkw; 41, identifier:_defaults; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:inspect; 45, identifier:getargspec; 46, argument_list; 46, 47; 47, identifier:obj; 48, elif_clause; 48, 49; 48, 55; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:inspect; 52, identifier:isclass; 53, argument_list; 53, 54; 54, identifier:obj; 55, block; 55, 56; 56, if_statement; 56, 57; 56, 65; 56, 78; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:inspect; 60, identifier:ismethoddescriptor; 61, argument_list; 61, 62; 62, attribute; 62, 63; 62, 64; 63, identifier:obj; 64, identifier:__init__; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 73; 68, pattern_list; 68, 69; 68, 70; 68, 71; 68, 72; 69, identifier:argnames; 70, identifier:varargs; 71, identifier:varkw; 72, identifier:_defaults; 73, expression_list; 73, 74; 73, 75; 73, 76; 73, 77; 74, list:[]; 75, False; 76, False; 77, None; 78, else_clause; 78, 79; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 87; 82, pattern_list; 82, 83; 82, 84; 82, 85; 82, 86; 83, identifier:argnames; 84, identifier:varargs; 85, identifier:varkw; 86, identifier:_defaults; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:inspect; 90, identifier:getargspec; 91, argument_list; 91, 92; 92, attribute; 92, 93; 92, 94; 93, identifier:obj; 94, identifier:__init__; 95, elif_clause; 95, 96; 95, 101; 96, call; 96, 97; 96, 98; 97, identifier:hasattr; 98, argument_list; 98, 99; 98, 100; 99, identifier:obj; 100, string:'__call__'; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 109; 104, pattern_list; 104, 105; 104, 106; 104, 107; 104, 108; 105, identifier:argnames; 106, identifier:varargs; 107, identifier:varkw; 108, identifier:_defaults; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:inspect; 112, identifier:getargspec; 113, argument_list; 113, 114; 114, attribute; 114, 115; 114, 116; 115, identifier:obj; 116, identifier:__call__; 117, else_clause; 117, 118; 118, block; 118, 119; 119, raise_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:TypeError; 122, argument_list; 122, 123; 123, string:"Object not callable?"; 124, comment; 125, comment; 126, comment; 127, if_statement; 127, 128; 127, 135; 128, boolean_operator:and; 128, 129; 128, 130; 129, identifier:argnames; 130, comparison_operator:==; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:argnames; 133, integer:0; 134, string:'self'; 135, block; 135, 136; 136, delete_statement; 136, 137; 137, subscript; 137, 138; 137, 139; 138, identifier:argnames; 139, integer:0; 140, if_statement; 140, 141; 140, 144; 140, 155; 141, comparison_operator:is; 141, 142; 141, 143; 142, identifier:_defaults; 143, None; 144, block; 144, 145; 144, 149; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:_defaults; 148, list:[]; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:defaults; 152, call; 152, 153; 152, 154; 153, identifier:dict; 154, argument_list; 155, else_clause; 155, 156; 155, 157; 156, comment; 157, block; 157, 158; 157, 164; 157, 171; 157, 177; 157, 183; 157, 200; 157, 206; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:defaults; 161, call; 161, 162; 161, 163; 162, identifier:dict; 163, argument_list; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:_defaults; 167, call; 167, 168; 167, 169; 168, identifier:list; 169, argument_list; 169, 170; 170, identifier:_defaults; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:_defaults; 175, identifier:reverse; 176, argument_list; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:argnames; 181, identifier:reverse; 182, argument_list; 183, for_statement; 183, 184; 183, 187; 183, 191; 184, pattern_list; 184, 185; 184, 186; 185, identifier:i; 186, identifier:default; 187, call; 187, 188; 187, 189; 188, identifier:enumerate; 189, argument_list; 189, 190; 190, identifier:_defaults; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 199; 194, subscript; 194, 195; 194, 196; 195, identifier:defaults; 196, subscript; 196, 197; 196, 198; 197, identifier:argnames; 198, identifier:i; 199, identifier:default; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:argnames; 204, identifier:reverse; 205, argument_list; 206, comment; 207, return_statement; 207, 208; 208, expression_list; 208, 209; 208, 210; 208, 211; 208, 215; 209, identifier:argnames; 210, identifier:defaults; 211, conditional_expression:if; 211, 212; 211, 213; 211, 214; 212, True; 213, identifier:varargs; 214, False; 215, conditional_expression:if; 215, 216; 215, 217; 215, 218; 216, True; 217, identifier:varkw; 218, False | def getargspec(obj):
"""An improved inspect.getargspec.
Has a slightly different return value from the default getargspec.
Returns a tuple of:
required, optional, args, kwargs
list, dict, bool, bool
Required is a list of required named arguments.
Optional is a dictionary mapping optional arguments to defaults.
Args and kwargs are True for the respective unlimited argument type.
"""
argnames, varargs, varkw, _defaults = None, None, None, None
if inspect.isfunction(obj) or inspect.ismethod(obj):
argnames, varargs, varkw, _defaults = inspect.getargspec(obj)
elif inspect.isclass(obj):
if inspect.ismethoddescriptor(obj.__init__):
argnames, varargs, varkw, _defaults = [], False, False, None
else:
argnames, varargs, varkw, _defaults = inspect.getargspec(obj.__init__)
elif hasattr(obj, '__call__'):
argnames, varargs, varkw, _defaults = inspect.getargspec(obj.__call__)
else:
raise TypeError("Object not callable?")
# Need test case to prove this is even possible.
# if (argnames, varargs, varkw, defaults) is (None, None, None, None):
# raise InspectionFailed()
if argnames and argnames[0] == 'self':
del argnames[0]
if _defaults is None:
_defaults = []
defaults = dict()
else:
# Create a mapping dictionary of defaults; this is slightly more useful.
defaults = dict()
_defaults = list(_defaults)
_defaults.reverse()
argnames.reverse()
for i, default in enumerate(_defaults):
defaults[argnames[i]] = default
argnames.reverse()
# del argnames[-len(_defaults):]
return argnames, defaults, True if varargs else False, True if varkw else False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_find_overlap; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:queries; 5, identifier:client; 6, identifier:find_method; 7, identifier:get_method; 8, identifier:overlap_function; 9, block; 9, 10; 9, 12; 9, 16; 9, 71; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:results; 15, list:[]; 16, for_statement; 16, 17; 16, 18; 16, 19; 17, identifier:query; 18, identifier:queries; 19, block; 19, 20; 19, 32; 19, 46; 19, 64; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:candidates; 23, await; 23, 24; 24, call; 24, 25; 24, 30; 25, call; 25, 26; 25, 27; 26, identifier:getattr; 27, argument_list; 27, 28; 27, 29; 28, identifier:client; 29, identifier:find_method; 30, argument_list; 30, 31; 31, identifier:query; 32, if_statement; 32, 33; 32, 35; 33, not_operator; 33, 34; 34, identifier:candidates; 35, block; 35, 36; 36, raise_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:ValueError; 39, argument_list; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, string:'no result found for {!r}'; 43, identifier:format; 44, argument_list; 44, 45; 45, identifier:query; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:result; 49, await; 49, 50; 50, call; 50, 51; 50, 56; 51, call; 51, 52; 51, 53; 52, identifier:getattr; 53, argument_list; 53, 54; 53, 55; 54, identifier:client; 55, identifier:get_method; 56, argument_list; 56, 57; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:id_; 59, attribute; 59, 60; 59, 63; 60, subscript; 60, 61; 60, 62; 61, identifier:candidates; 62, integer:0; 63, identifier:id_; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:results; 68, identifier:append; 69, argument_list; 69, 70; 70, identifier:result; 71, return_statement; 71, 72; 72, await; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:overlap_function; 75, argument_list; 75, 76; 75, 77; 76, identifier:results; 77, identifier:client | async def _find_overlap(queries, client, find_method, get_method,
overlap_function):
"""Generic find and overlap implementation.
Arguments
names (:py:class:`collections.abc.Sequence`): The queries of the
people to find overlaps for.
client (:py:class:`~.TMDbClient`): The TMDb client.
find_method (:py:class:`str`): The name of the client method to
use for finding candidates.
get_method (:py:class:`str`): The name of the client method to
use for getting detailed information on a candidate.
overlap_function (:py:class:`collections.abc.Callable`): The
function to call for the resulting overlap.
"""
results = []
for query in queries:
candidates = await getattr(client, find_method)(query)
if not candidates:
raise ValueError('no result found for {!r}'.format(query))
result = await getattr(client, get_method)(id_=candidates[0].id_)
results.append(result)
return await overlap_function(results, client) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:power; 3, parameters; 3, 4; 4, identifier:set_; 5, block; 5, 6; 5, 8; 5, 13; 5, 37; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 11; 10, identifier:ensure_countable; 11, argument_list; 11, 12; 12, identifier:set_; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:result; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:chain; 19, identifier:from_iterable; 20, generator_expression; 20, 21; 20, 26; 21, call; 21, 22; 21, 23; 22, identifier:combinations; 23, argument_list; 23, 24; 23, 25; 24, identifier:set_; 25, identifier:r; 26, for_in_clause; 26, 27; 26, 28; 27, identifier:r; 28, call; 28, 29; 28, 30; 29, identifier:xrange; 30, argument_list; 30, 31; 31, binary_operator:+; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:len; 34, argument_list; 34, 35; 35, identifier:set_; 36, integer:1; 37, return_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:_harmonize_subset_types; 40, argument_list; 40, 41; 40, 42; 41, identifier:set_; 42, identifier:result | def power(set_):
"""Returns all subsets of given set.
:return: Powerset of given set, i.e. iterable containing all its subsets,
sorted by ascending cardinality.
"""
ensure_countable(set_)
result = chain.from_iterable(combinations(set_, r)
for r in xrange(len(set_) + 1))
return _harmonize_subset_types(set_, result) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:generate_pws_in_order; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:n; 6, default_parameter; 6, 7; 6, 8; 7, identifier:filter_func; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:N_max; 11, float:1e6; 12, block; 12, 13; 12, 15; 12, 16; 12, 26; 12, 27; 12, 36; 12, 37; 12, 41; 12, 47; 12, 53; 12, 294; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:states; 19, list:[(-1.0, helper.START)]; 19, 20; 20, tuple; 20, 21; 20, 23; 21, unary_operator:-; 21, 22; 22, float:1.0; 23, attribute; 23, 24; 23, 25; 24, identifier:helper; 25, identifier:START; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:p_min; 30, binary_operator:/; 30, 31; 30, 32; 31, float:1e-9; 32, parenthesized_expression; 32, 33; 33, binary_operator:**; 33, 34; 33, 35; 34, identifier:n; 35, integer:2; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:ret; 40, list:[]; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:done; 44, call; 44, 45; 44, 46; 45, identifier:set; 46, argument_list; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:already_added_in_heap; 50, call; 50, 51; 50, 52; 51, identifier:set; 52, argument_list; 53, while_statement; 53, 54; 53, 67; 53, 68; 54, boolean_operator:and; 54, 55; 54, 61; 55, comparison_operator:<; 55, 56; 55, 60; 56, call; 56, 57; 56, 58; 57, identifier:len; 58, argument_list; 58, 59; 59, identifier:ret; 60, identifier:n; 61, comparison_operator:>; 61, 62; 61, 66; 62, call; 62, 63; 62, 64; 63, identifier:len; 64, argument_list; 64, 65; 65, identifier:states; 66, integer:0; 67, comment; 68, block; 68, 69; 68, 80; 68, 90; 68, 96; 68, 110; 68, 222; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, pattern_list; 71, 72; 71, 73; 72, identifier:p; 73, identifier:s; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:heapq; 77, identifier:heappop; 78, argument_list; 78, 79; 79, identifier:states; 80, if_statement; 80, 81; 80, 84; 81, comparison_operator:<; 81, 82; 81, 83; 82, identifier:p; 83, integer:0; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:p; 88, unary_operator:-; 88, 89; 89, identifier:p; 90, if_statement; 90, 91; 90, 94; 91, comparison_operator:in; 91, 92; 91, 93; 92, identifier:s; 93, identifier:done; 94, block; 94, 95; 95, continue_statement; 96, assert_statement; 96, 97; 96, 104; 97, comparison_operator:==; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:s; 100, integer:0; 101, attribute; 101, 102; 101, 103; 102, identifier:helper; 103, identifier:START; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, string:"Broken s: {!r}"; 107, identifier:format; 108, argument_list; 108, 109; 109, identifier:s; 110, if_statement; 110, 111; 110, 119; 110, 158; 111, comparison_operator:==; 111, 112; 111, 116; 112, subscript; 112, 113; 112, 114; 113, identifier:s; 114, unary_operator:-; 114, 115; 115, integer:1; 116, attribute; 116, 117; 116, 118; 117, identifier:helper; 118, identifier:END; 119, block; 119, 120; 119, 127; 119, 137; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:done; 124, identifier:add; 125, argument_list; 125, 126; 126, identifier:s; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:clean_s; 130, subscript; 130, 131; 130, 132; 131, identifier:s; 132, slice; 132, 133; 132, 134; 132, 135; 133, integer:1; 134, colon; 135, unary_operator:-; 135, 136; 136, integer:1; 137, if_statement; 137, 138; 137, 146; 138, boolean_operator:or; 138, 139; 138, 142; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:filter_func; 141, None; 142, call; 142, 143; 142, 144; 143, identifier:filter_func; 144, argument_list; 144, 145; 145, identifier:clean_s; 146, block; 146, 147; 146, 156; 146, 157; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:ret; 151, identifier:append; 152, argument_list; 152, 153; 153, tuple; 153, 154; 153, 155; 154, identifier:clean_s; 155, identifier:p; 156, comment; 157, comment; 158, else_clause; 158, 159; 159, block; 159, 160; 160, for_statement; 160, 161; 160, 164; 160, 174; 161, pattern_list; 161, 162; 161, 163; 162, identifier:c; 163, identifier:f; 164, call; 164, 165; 164, 173; 165, attribute; 165, 166; 165, 172; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:_get_next; 170, argument_list; 170, 171; 171, identifier:s; 172, identifier:items; 173, argument_list; 174, block; 174, 175; 174, 198; 174, 207; 175, if_statement; 175, 176; 175, 196; 176, parenthesized_expression; 176, 177; 177, boolean_operator:or; 177, 178; 177, 190; 178, boolean_operator:or; 178, 179; 178, 184; 179, comparison_operator:<; 179, 180; 179, 183; 180, binary_operator:*; 180, 181; 180, 182; 181, identifier:f; 182, identifier:p; 183, identifier:p_min; 184, comparison_operator:in; 184, 185; 184, 189; 185, parenthesized_expression; 185, 186; 186, binary_operator:+; 186, 187; 186, 188; 187, identifier:s; 188, identifier:c; 189, identifier:done; 190, comparison_operator:in; 190, 191; 190, 195; 191, parenthesized_expression; 191, 192; 192, binary_operator:+; 192, 193; 192, 194; 193, identifier:s; 194, identifier:c; 195, identifier:already_added_in_heap; 196, block; 196, 197; 197, continue_statement; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:already_added_in_heap; 202, identifier:add; 203, argument_list; 203, 204; 204, binary_operator:+; 204, 205; 204, 206; 205, identifier:s; 206, identifier:c; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:heapq; 211, identifier:heappush; 212, argument_list; 212, 213; 212, 214; 213, identifier:states; 214, tuple; 214, 215; 214, 219; 215, binary_operator:*; 215, 216; 215, 218; 216, unary_operator:-; 216, 217; 217, identifier:f; 218, identifier:p; 219, binary_operator:+; 219, 220; 219, 221; 220, identifier:s; 221, identifier:c; 222, if_statement; 222, 223; 222, 233; 223, comparison_operator:>; 223, 224; 223, 228; 224, call; 224, 225; 224, 226; 225, identifier:len; 226, argument_list; 226, 227; 227, identifier:states; 228, binary_operator:/; 228, 229; 228, 232; 229, binary_operator:*; 229, 230; 229, 231; 230, identifier:N_max; 231, integer:3; 232, integer:2; 233, block; 233, 234; 233, 253; 233, 272; 233, 289; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:print; 237, argument_list; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, string:"Heap size: {}. ret={}. (expected: {}) s={!r}"; 241, identifier:format; 242, argument_list; 242, 243; 242, 247; 242, 251; 242, 252; 243, call; 243, 244; 243, 245; 244, identifier:len; 245, argument_list; 245, 246; 246, identifier:states; 247, call; 247, 248; 247, 249; 248, identifier:len; 249, argument_list; 249, 250; 250, identifier:ret; 251, identifier:n; 252, identifier:s; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 256; 255, identifier:print; 256, argument_list; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, string:"The size of states={}. Still need={} pws. Truncating"; 260, identifier:format; 261, argument_list; 261, 262; 261, 266; 262, call; 262, 263; 262, 264; 263, identifier:len; 264, argument_list; 264, 265; 265, identifier:states; 266, binary_operator:-; 266, 267; 266, 268; 267, identifier:n; 268, call; 268, 269; 268, 270; 269, identifier:len; 270, argument_list; 270, 271; 271, identifier:ret; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 275; 274, identifier:states; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:heapq; 278, identifier:nsmallest; 279, argument_list; 279, 280; 279, 288; 280, call; 280, 281; 280, 282; 281, identifier:int; 282, argument_list; 282, 283; 283, binary_operator:/; 283, 284; 283, 287; 284, binary_operator:*; 284, 285; 284, 286; 285, identifier:N_max; 286, integer:3; 287, integer:4; 288, identifier:states; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 292; 291, identifier:print; 292, argument_list; 292, 293; 293, string:"Done"; 294, return_statement; 294, 295; 295, identifier:ret | def generate_pws_in_order(self, n, filter_func=None, N_max=1e6):
"""
Generates passwords in order between upto N_max
@N_max is the maximum size of the priority queue will be tolerated,
so if the size of the queue is bigger than 1.5 * N_max, it will shrink
the size to 0.75 * N_max
@n is the number of password to generate.
**This function is expensive, and shuold be called only if necessary.
Cache its call as much as possible**
# TODO: Need to recheck how to make sure this is working.
"""
# assert alpha < beta, 'alpha={} must be less than beta={}'.format(alpha, beta)
states = [(-1.0, helper.START)]
# get the topk first
p_min = 1e-9 / (n**2) # max 1 million entries in the heap
ret = []
done = set()
already_added_in_heap = set()
while len(ret) < n and len(states) > 0:
# while n > 0 and len(states) > 0:
p, s = heapq.heappop(states)
if p < 0:
p = -p
if s in done: continue
assert s[0] == helper.START, "Broken s: {!r}".format(s)
if s[-1] == helper.END:
done.add(s)
clean_s = s[1:-1]
if filter_func is None or filter_func(clean_s):
ret.append((clean_s, p))
# n -= 1
# yield (clean_s, p)
else:
for c, f in self._get_next(s).items():
if (f*p < p_min or (s+c) in done or
(s+c) in already_added_in_heap):
continue
already_added_in_heap.add(s+c)
heapq.heappush(states, (-f*p, s+c))
if len(states) > N_max * 3 / 2:
print("Heap size: {}. ret={}. (expected: {}) s={!r}"
.format(len(states), len(ret), n, s))
print("The size of states={}. Still need={} pws. Truncating"
.format(len(states), n - len(ret)))
states = heapq.nsmallest(int(N_max * 3/4), states)
print("Done")
return ret |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:expand_seed; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:start_seed; 6, identifier:num_iterations; 7, identifier:val; 8, block; 8, 9; 8, 11; 8, 26; 8, 36; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:grd; 17, identifier:set_tile; 18, argument_list; 18, 19; 18, 22; 18, 25; 19, subscript; 19, 20; 19, 21; 20, identifier:start_seed; 21, integer:0; 22, subscript; 22, 23; 22, 24; 23, identifier:start_seed; 24, integer:1; 25, identifier:val; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:cur_pos; 29, list:[start_seed[0], start_seed[1]]; 29, 30; 29, 33; 30, subscript; 30, 31; 30, 32; 31, identifier:start_seed; 32, integer:0; 33, subscript; 33, 34; 33, 35; 34, identifier:start_seed; 35, integer:1; 36, while_statement; 36, 37; 36, 40; 36, 41; 37, comparison_operator:>; 37, 38; 37, 39; 38, identifier:num_iterations; 39, integer:0; 40, comment; 41, block; 41, 42; 41, 46; 41, 148; 41, 162; 41, 176; 41, 191; 41, 206; 41, 221; 41, 236; 42, expression_statement; 42, 43; 43, augmented_assignment:-=; 43, 44; 43, 45; 44, identifier:num_iterations; 45, integer:1; 46, for_statement; 46, 47; 46, 48; 46, 69; 47, identifier:y; 48, call; 48, 49; 48, 50; 49, identifier:range; 50, argument_list; 50, 51; 50, 60; 51, binary_operator:-; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:cur_pos; 54, integer:0; 55, call; 55, 56; 55, 57; 56, identifier:randint; 57, argument_list; 57, 58; 57, 59; 58, integer:0; 59, integer:2; 60, binary_operator:+; 60, 61; 60, 64; 61, subscript; 61, 62; 61, 63; 62, identifier:cur_pos; 63, integer:0; 64, call; 64, 65; 64, 66; 65, identifier:randint; 66, argument_list; 66, 67; 66, 68; 67, integer:0; 68, integer:2; 69, block; 69, 70; 70, for_statement; 70, 71; 70, 72; 70, 93; 71, identifier:x; 72, call; 72, 73; 72, 74; 73, identifier:range; 74, argument_list; 74, 75; 74, 84; 75, binary_operator:-; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:cur_pos; 78, integer:1; 79, call; 79, 80; 79, 81; 80, identifier:randint; 81, argument_list; 81, 82; 81, 83; 82, integer:0; 83, integer:2; 84, binary_operator:+; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:cur_pos; 87, integer:1; 88, call; 88, 89; 88, 90; 89, identifier:randint; 90, argument_list; 90, 91; 90, 92; 91, integer:0; 92, integer:2; 93, block; 93, 94; 94, if_statement; 94, 95; 94, 118; 94, 119; 95, boolean_operator:and; 95, 96; 95, 111; 96, boolean_operator:and; 96, 97; 96, 108; 97, boolean_operator:and; 97, 98; 97, 105; 98, comparison_operator:<; 98, 99; 98, 100; 99, identifier:x; 100, attribute; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:grd; 104, identifier:grid_width; 105, comparison_operator:>=; 105, 106; 105, 107; 106, identifier:x; 107, integer:0; 108, comparison_operator:>=; 108, 109; 108, 110; 109, identifier:y; 110, integer:0; 111, comparison_operator:<; 111, 112; 111, 113; 112, identifier:y; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:grd; 117, identifier:grid_height; 118, comment; 119, block; 119, 120; 120, if_statement; 120, 121; 120, 132; 121, comparison_operator:!=; 121, 122; 121, 131; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:grd; 127, identifier:get_tile; 128, argument_list; 128, 129; 128, 130; 129, identifier:y; 130, identifier:x; 131, identifier:val; 132, block; 132, 133; 132, 144; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 140; 135, attribute; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:grd; 139, identifier:set_tile; 140, argument_list; 140, 141; 140, 142; 140, 143; 141, identifier:y; 142, identifier:x; 143, identifier:TERRAIN_LAND; 144, expression_statement; 144, 145; 145, augmented_assignment:-=; 145, 146; 145, 147; 146, identifier:num_iterations; 147, integer:1; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:new_x; 151, binary_operator:-; 151, 152; 151, 161; 152, binary_operator:+; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:cur_pos; 155, integer:0; 156, call; 156, 157; 156, 158; 157, identifier:randint; 158, argument_list; 158, 159; 158, 160; 159, integer:0; 160, integer:3; 161, integer:2; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:new_y; 165, binary_operator:-; 165, 166; 165, 175; 166, binary_operator:+; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:cur_pos; 169, integer:1; 170, call; 170, 171; 170, 172; 171, identifier:randint; 172, argument_list; 172, 173; 172, 174; 173, integer:0; 174, integer:3; 175, integer:2; 176, if_statement; 176, 177; 176, 186; 177, comparison_operator:>; 177, 178; 177, 179; 178, identifier:new_x; 179, binary_operator:-; 179, 180; 179, 185; 180, attribute; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:grd; 184, identifier:grid_width; 185, integer:1; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:new_x; 190, integer:0; 191, if_statement; 191, 192; 191, 201; 192, comparison_operator:>; 192, 193; 192, 194; 193, identifier:new_y; 194, binary_operator:-; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:grd; 199, identifier:grid_height; 200, integer:1; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:new_y; 205, integer:0; 206, if_statement; 206, 207; 206, 210; 207, comparison_operator:<; 207, 208; 207, 209; 208, identifier:new_x; 209, integer:0; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:new_x; 214, binary_operator:-; 214, 215; 214, 220; 215, attribute; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:grd; 219, identifier:grid_width; 220, integer:1; 221, if_statement; 221, 222; 221, 225; 222, comparison_operator:<; 222, 223; 222, 224; 223, identifier:new_y; 224, integer:0; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:new_y; 229, binary_operator:-; 229, 230; 229, 235; 230, attribute; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:grd; 234, identifier:grid_height; 235, integer:1; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:cur_pos; 239, list:[new_y, new_x]; 239, 240; 239, 241; 240, identifier:new_y; 241, identifier:new_x | def expand_seed(self, start_seed, num_iterations, val):
"""
takes a seed start point and grows out in random
directions setting cell points to val
"""
self.grd.set_tile(start_seed[0], start_seed[1], val)
cur_pos = [start_seed[0], start_seed[1]]
while num_iterations > 0: # don't use loop as it will hit boundaries often
num_iterations -= 1
for y in range(cur_pos[0]-randint(0,2), cur_pos[0] + randint(0,2)):
for x in range(cur_pos[1]-randint(0,2), cur_pos[1] + randint(0,2)):
if x < self.grd.grid_width and x >= 0 and y >= 0 and y < self.grd.grid_height:
#print(x,y,val)
if self.grd.get_tile(y,x) != val:
self.grd.set_tile(y, x, TERRAIN_LAND)
num_iterations -= 1
new_x = cur_pos[0] + randint(0,3)-2
new_y = cur_pos[1] + randint(0,3)-2
if new_x > self.grd.grid_width - 1:
new_x = 0
if new_y > self.grd.grid_height - 1:
new_y = 0
if new_x < 0:
new_x = self.grd.grid_width - 1
if new_y < 0:
new_y = self.grd.grid_height - 1
cur_pos = [new_y, new_x] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:denoise_grid; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:val; 6, default_parameter; 6, 7; 6, 8; 7, identifier:expand; 8, integer:1; 9, block; 9, 10; 9, 12; 9, 48; 9, 244; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:updated_grid; 15, list_comprehension; 15, 16; 15, 37; 15, 38; 16, list_comprehension; 16, 17; 16, 26; 16, 27; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:grd; 22, identifier:get_tile; 23, argument_list; 23, 24; 23, 25; 24, identifier:y; 25, identifier:x; 26, line_continuation:\; 27, for_in_clause; 27, 28; 27, 29; 28, identifier:x; 29, call; 29, 30; 29, 31; 30, identifier:range; 31, argument_list; 31, 32; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:grd; 36, identifier:grid_width; 37, line_continuation:\; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:y; 40, call; 40, 41; 40, 42; 41, identifier:range; 42, argument_list; 42, 43; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:grd; 47, identifier:grid_height; 48, for_statement; 48, 49; 48, 50; 48, 62; 49, identifier:row; 50, call; 50, 51; 50, 52; 51, identifier:range; 52, argument_list; 52, 53; 53, binary_operator:-; 53, 54; 53, 61; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:grd; 59, identifier:get_grid_height; 60, argument_list; 61, identifier:expand; 62, block; 62, 63; 63, for_statement; 63, 64; 63, 65; 63, 77; 64, identifier:col; 65, call; 65, 66; 65, 67; 66, identifier:range; 67, argument_list; 67, 68; 68, binary_operator:-; 68, 69; 68, 76; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:grd; 74, identifier:get_grid_width; 75, argument_list; 76, identifier:expand; 77, block; 77, 78; 77, 94; 77, 95; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 85; 80, subscript; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:updated_grid; 83, identifier:row; 84, identifier:col; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:grd; 90, identifier:get_tile; 91, argument_list; 91, 92; 91, 93; 92, identifier:row; 93, identifier:col; 94, comment; 95, if_statement; 95, 96; 95, 107; 96, comparison_operator:==; 96, 97; 96, 106; 97, call; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:grd; 102, identifier:get_tile; 103, argument_list; 103, 104; 103, 105; 104, identifier:row; 105, identifier:col; 106, identifier:val; 107, block; 107, 108; 108, for_statement; 108, 109; 108, 110; 108, 116; 109, identifier:y; 110, call; 110, 111; 110, 112; 111, identifier:range; 112, argument_list; 112, 113; 112, 115; 113, unary_operator:-; 113, 114; 114, identifier:expand; 115, identifier:expand; 116, block; 116, 117; 117, for_statement; 117, 118; 117, 119; 117, 125; 118, identifier:x; 119, call; 119, 120; 119, 121; 120, identifier:range; 121, argument_list; 121, 122; 121, 124; 122, unary_operator:-; 122, 123; 123, identifier:expand; 124, identifier:expand; 125, block; 125, 126; 125, 132; 125, 138; 125, 147; 125, 156; 125, 181; 125, 206; 125, 207; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:new_x; 129, binary_operator:+; 129, 130; 129, 131; 130, identifier:col; 131, identifier:x; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:new_y; 135, binary_operator:+; 135, 136; 135, 137; 136, identifier:row; 137, identifier:y; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:<; 139, 140; 139, 141; 140, identifier:new_x; 141, integer:0; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:new_x; 146, integer:0; 147, if_statement; 147, 148; 147, 151; 148, comparison_operator:<; 148, 149; 148, 150; 149, identifier:new_y; 150, integer:0; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:new_y; 155, integer:0; 156, if_statement; 156, 157; 156, 168; 157, comparison_operator:>; 157, 158; 157, 159; 158, identifier:new_x; 159, binary_operator:-; 159, 160; 159, 167; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:grd; 165, identifier:get_grid_width; 166, argument_list; 167, integer:1; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:new_x; 172, binary_operator:-; 172, 173; 172, 180; 173, call; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:grd; 178, identifier:get_grid_width; 179, argument_list; 180, integer:1; 181, if_statement; 181, 182; 181, 193; 182, comparison_operator:>; 182, 183; 182, 184; 183, identifier:new_y; 184, binary_operator:-; 184, 185; 184, 192; 185, call; 185, 186; 185, 191; 186, attribute; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:grd; 190, identifier:get_grid_height; 191, argument_list; 192, integer:1; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:new_y; 197, binary_operator:-; 197, 198; 197, 205; 198, call; 198, 199; 198, 204; 199, attribute; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:grd; 203, identifier:get_grid_height; 204, argument_list; 205, integer:1; 206, comment; 207, if_statement; 207, 208; 207, 211; 207, 234; 208, comparison_operator:>; 208, 209; 208, 210; 209, identifier:expand; 210, integer:0; 211, block; 211, 212; 212, if_statement; 212, 213; 212, 225; 213, comparison_operator:>; 213, 214; 213, 221; 214, call; 214, 215; 214, 216; 215, identifier:randint; 216, argument_list; 216, 217; 216, 218; 217, integer:1; 218, binary_operator:*; 218, 219; 218, 220; 219, identifier:expand; 220, integer:2; 221, parenthesized_expression; 221, 222; 222, binary_operator:+; 222, 223; 222, 224; 223, identifier:expand; 224, integer:1; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 233; 228, subscript; 228, 229; 228, 232; 229, subscript; 229, 230; 229, 231; 230, identifier:updated_grid; 231, identifier:new_y; 232, identifier:new_x; 233, identifier:val; 234, else_clause; 234, 235; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 243; 238, subscript; 238, 239; 238, 242; 239, subscript; 239, 240; 239, 241; 240, identifier:updated_grid; 241, identifier:new_y; 242, identifier:new_x; 243, identifier:val; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:self; 249, identifier:grd; 250, identifier:replace_grid; 251, argument_list; 251, 252; 252, identifier:updated_grid | def denoise_grid(self, val, expand=1):
"""
for every cell in the grid of 'val' fill all cells
around it to de noise the grid
"""
updated_grid = [[self.grd.get_tile(y,x) \
for x in range(self.grd.grid_width)] \
for y in range(self.grd.grid_height)]
for row in range(self.grd.get_grid_height() - expand):
for col in range(self.grd.get_grid_width() - expand):
updated_grid[row][col] = self.grd.get_tile(row,col) # set original point
if self.grd.get_tile(row,col) == val:
for y in range(-expand, expand):
for x in range(-expand, expand):
new_x = col+x
new_y = row+y
if new_x < 0: new_x = 0
if new_y < 0: new_y = 0
if new_x > self.grd.get_grid_width() - 1: new_x = self.grd.get_grid_width() - 1
if new_y > self.grd.get_grid_height() - 1: new_y = self.grd.get_grid_height() - 1
# randomly NOT denoise to make interesting edges
if expand > 0:
if randint(1,expand * 2) > (expand+1):
updated_grid[new_y][new_x] = val
else:
updated_grid[new_y][new_x] = val
self.grd.replace_grid(updated_grid) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:run; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:num_runs; 6, identifier:show_trails; 7, identifier:log_file_base; 8, block; 8, 9; 8, 11; 8, 16; 8, 35; 8, 60; 8, 80; 8, 86; 8, 87; 8, 134; 8, 272; 8, 273; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:print; 14, argument_list; 14, 15; 15, string:"--------------------------------------------------"; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:print; 19, argument_list; 19, 20; 19, 21; 19, 28; 20, string:"Starting Simulation - target = "; 21, attribute; 21, 22; 21, 27; 22, subscript; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:agent_list; 26, integer:0; 27, identifier:target_y; 28, attribute; 28, 29; 28, 34; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:agent_list; 33, integer:0; 34, identifier:target_x; 35, expression_statement; 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:world; 42, identifier:grd; 43, identifier:set_tile; 44, argument_list; 44, 45; 44, 52; 44, 59; 45, attribute; 45, 46; 45, 51; 46, subscript; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:agent_list; 50, integer:0; 51, identifier:target_y; 52, attribute; 52, 53; 52, 58; 53, subscript; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:agent_list; 57, integer:0; 58, identifier:target_x; 59, string:'T'; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:highlight_cell_surroundings; 65, argument_list; 65, 66; 65, 73; 66, attribute; 66, 67; 66, 72; 67, subscript; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:agent_list; 71, integer:0; 72, identifier:target_y; 73, attribute; 73, 74; 73, 79; 74, subscript; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:agent_list; 78, integer:0; 79, identifier:target_x; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:start_all_agents; 85, argument_list; 86, comment; 87, try_statement; 87, 88; 87, 124; 88, block; 88, 89; 89, with_statement; 89, 90; 89, 102; 90, with_clause; 90, 91; 91, with_item; 91, 92; 92, as_pattern; 92, 93; 92, 100; 93, call; 93, 94; 93, 95; 94, identifier:open; 95, argument_list; 95, 96; 95, 99; 96, binary_operator:+; 96, 97; 96, 98; 97, identifier:log_file_base; 98, string:'__agents.txt'; 99, string:"w"; 100, as_pattern_target; 100, 101; 101, identifier:f; 102, block; 102, 103; 102, 110; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:f; 107, identifier:write; 108, argument_list; 108, 109; 109, string:"Starting World = \n"; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:f; 114, identifier:write; 115, argument_list; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:str; 118, argument_list; 118, 119; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:world; 123, identifier:grd; 124, except_clause; 124, 125; 124, 126; 125, identifier:Exception; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:print; 130, argument_list; 130, 131; 131, binary_operator:+; 131, 132; 131, 133; 132, string:'Cant save log results to '; 133, identifier:log_file_base; 134, for_statement; 134, 135; 134, 136; 134, 141; 135, identifier:cur_run; 136, call; 136, 137; 136, 138; 137, identifier:range; 138, argument_list; 138, 139; 138, 140; 139, integer:0; 140, identifier:num_runs; 141, block; 141, 142; 141, 148; 141, 246; 141, 247; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 145; 144, identifier:print; 145, argument_list; 145, 146; 145, 147; 146, string:"WorldSimulation:run#"; 147, identifier:cur_run; 148, for_statement; 148, 149; 148, 152; 148, 158; 149, pattern_list; 149, 150; 149, 151; 150, identifier:num; 151, identifier:agt; 152, call; 152, 153; 152, 154; 153, identifier:enumerate; 154, argument_list; 154, 155; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:agent_list; 158, block; 158, 159; 158, 222; 158, 228; 158, 245; 159, if_statement; 159, 160; 159, 163; 160, comparison_operator:==; 160, 161; 160, 162; 161, identifier:show_trails; 162, string:'Y'; 163, block; 163, 164; 164, if_statement; 164, 165; 164, 182; 164, 200; 165, boolean_operator:or; 165, 166; 165, 174; 166, comparison_operator:==; 166, 167; 166, 173; 167, call; 167, 168; 167, 169; 168, identifier:len; 169, argument_list; 169, 170; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:agent_list; 173, integer:1; 174, comparison_operator:>; 174, 175; 174, 181; 175, call; 175, 176; 175, 177; 176, identifier:len; 177, argument_list; 177, 178; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:agent_list; 181, integer:9; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 192; 185, attribute; 185, 186; 185, 191; 186, attribute; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:world; 190, identifier:grd; 191, identifier:set_tile; 192, argument_list; 192, 193; 192, 196; 192, 199; 193, attribute; 193, 194; 193, 195; 194, identifier:agt; 195, identifier:current_y; 196, attribute; 196, 197; 196, 198; 197, identifier:agt; 198, identifier:current_x; 199, string:'o'; 200, else_clause; 200, 201; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 211; 204, attribute; 204, 205; 204, 210; 205, attribute; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:world; 209, identifier:grd; 210, identifier:set_tile; 211, argument_list; 211, 212; 211, 215; 211, 218; 212, attribute; 212, 213; 212, 214; 213, identifier:agt; 214, identifier:current_y; 215, attribute; 215, 216; 215, 217; 216, identifier:agt; 217, identifier:current_x; 218, call; 218, 219; 218, 220; 219, identifier:str; 220, argument_list; 220, 221; 221, identifier:num; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:agt; 226, identifier:do_your_job; 227, argument_list; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 237; 230, attribute; 230, 231; 230, 236; 231, attribute; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:self; 234, identifier:world; 235, identifier:grd; 236, identifier:set_tile; 237, argument_list; 237, 238; 237, 241; 237, 244; 238, attribute; 238, 239; 238, 240; 239, identifier:agt; 240, identifier:current_y; 241, attribute; 241, 242; 241, 243; 242, identifier:agt; 243, identifier:current_x; 244, string:'A'; 245, comment; 246, comment; 247, if_statement; 247, 248; 247, 251; 248, comparison_operator:!=; 248, 249; 248, 250; 249, identifier:log_file_base; 250, string:'N'; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 261; 254, attribute; 254, 255; 254, 260; 255, attribute; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:world; 259, identifier:grd; 260, identifier:save; 261, argument_list; 261, 262; 262, binary_operator:+; 262, 263; 262, 271; 263, binary_operator:+; 263, 264; 263, 267; 264, binary_operator:+; 264, 265; 264, 266; 265, identifier:log_file_base; 266, string:'_'; 267, call; 267, 268; 267, 269; 268, identifier:str; 269, argument_list; 269, 270; 270, identifier:cur_run; 271, string:'.log'; 272, comment; 273, with_statement; 273, 274; 273, 286; 274, with_clause; 274, 275; 275, with_item; 275, 276; 276, as_pattern; 276, 277; 276, 284; 277, call; 277, 278; 277, 279; 278, identifier:open; 279, argument_list; 279, 280; 279, 283; 280, binary_operator:+; 280, 281; 280, 282; 281, identifier:log_file_base; 282, string:'__agents.txt'; 283, string:"a"; 284, as_pattern_target; 284, 285; 285, identifier:f; 286, block; 286, 287; 286, 320; 286, 334; 286, 341; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:f; 291, identifier:write; 292, argument_list; 292, 293; 293, binary_operator:+; 293, 294; 293, 319; 294, binary_operator:+; 294, 295; 294, 309; 295, binary_operator:+; 295, 296; 295, 308; 296, binary_operator:+; 296, 297; 296, 298; 297, string:"\nWorld tgt= ["; 298, call; 298, 299; 298, 300; 299, identifier:str; 300, argument_list; 300, 301; 301, attribute; 301, 302; 301, 307; 302, subscript; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:self; 305, identifier:agent_list; 306, integer:0; 307, identifier:target_y; 308, string:","; 309, call; 309, 310; 309, 311; 310, identifier:str; 311, argument_list; 311, 312; 312, attribute; 312, 313; 312, 318; 313, subscript; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:self; 316, identifier:agent_list; 317, integer:0; 318, identifier:target_x; 319, string:"]\n"; 320, expression_statement; 320, 321; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:f; 324, identifier:write; 325, argument_list; 325, 326; 326, call; 326, 327; 326, 328; 327, identifier:str; 328, argument_list; 328, 329; 329, attribute; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:self; 332, identifier:world; 333, identifier:grd; 334, expression_statement; 334, 335; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:f; 338, identifier:write; 339, argument_list; 339, 340; 340, string:'\n\nAgent Name , starting, num Steps , num Climbs\n'; 341, for_statement; 341, 342; 341, 345; 341, 351; 342, pattern_list; 342, 343; 342, 344; 343, identifier:num; 344, identifier:agt; 345, call; 345, 346; 345, 347; 346, identifier:enumerate; 347, argument_list; 347, 348; 348, attribute; 348, 349; 348, 350; 349, identifier:self; 350, identifier:agent_list; 351, block; 351, 352; 351, 378; 351, 398; 351, 413; 352, expression_statement; 352, 353; 353, assignment; 353, 354; 353, 355; 354, identifier:res; 355, binary_operator:+; 355, 356; 355, 377; 356, binary_operator:+; 356, 357; 356, 371; 357, binary_operator:+; 357, 358; 357, 370; 358, binary_operator:+; 358, 359; 358, 364; 359, binary_operator:+; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:agt; 362, identifier:name; 363, string:' , ['; 364, call; 364, 365; 364, 366; 365, identifier:str; 366, argument_list; 366, 367; 367, attribute; 367, 368; 367, 369; 368, identifier:agt; 369, identifier:start_y; 370, string:', '; 371, call; 371, 372; 371, 373; 372, identifier:str; 373, argument_list; 373, 374; 374, attribute; 374, 375; 374, 376; 375, identifier:agt; 376, identifier:start_x; 377, string:'], '; 378, expression_statement; 378, 379; 379, augmented_assignment:+=; 379, 380; 379, 381; 380, identifier:res; 381, binary_operator:+; 381, 382; 381, 397; 382, binary_operator:+; 382, 383; 382, 391; 383, binary_operator:+; 383, 384; 383, 390; 384, call; 384, 385; 384, 386; 385, identifier:str; 386, argument_list; 386, 387; 387, attribute; 387, 388; 387, 389; 388, identifier:agt; 389, identifier:num_steps; 390, string:' , '; 391, call; 391, 392; 391, 393; 392, identifier:str; 393, argument_list; 393, 394; 394, attribute; 394, 395; 394, 396; 395, identifier:agt; 396, identifier:num_climbs; 397, string:' , '; 398, expression_statement; 398, 399; 399, augmented_assignment:+=; 399, 400; 399, 401; 400, identifier:res; 401, call; 401, 402; 401, 405; 402, attribute; 402, 403; 402, 404; 403, string:''; 404, identifier:join; 405, argument_list; 405, 406; 406, list_comprehension; 406, 407; 406, 408; 407, identifier:a; 408, for_in_clause; 408, 409; 408, 410; 409, identifier:a; 410, attribute; 410, 411; 410, 412; 411, identifier:agt; 412, identifier:results; 413, expression_statement; 413, 414; 414, call; 414, 415; 414, 418; 415, attribute; 415, 416; 415, 417; 416, identifier:f; 417, identifier:write; 418, argument_list; 418, 419; 419, binary_operator:+; 419, 420; 419, 421; 420, identifier:res; 421, string:'\n' | def run(self, num_runs, show_trails, log_file_base):
"""
Run each agent in the world for 'num_runs' iterations
Optionally saves grid results to file if base name is
passed to method.
"""
print("--------------------------------------------------")
print("Starting Simulation - target = ", self.agent_list[0].target_y, self.agent_list[0].target_x)
self.world.grd.set_tile(self.agent_list[0].target_y , self.agent_list[0].target_x , 'T')
self.highlight_cell_surroundings(self.agent_list[0].target_y, self.agent_list[0].target_x)
self.start_all_agents()
# save the agents results here
try:
with open (log_file_base + '__agents.txt', "w") as f:
f.write("Starting World = \n")
f.write(str(self.world.grd))
except Exception:
print('Cant save log results to ' + log_file_base)
for cur_run in range(0,num_runs):
print("WorldSimulation:run#", cur_run)
for num, agt in enumerate(self.agent_list):
if show_trails == 'Y':
if len(self.agent_list) == 1 or len(self.agent_list) > 9:
self.world.grd.set_tile(agt.current_y, agt.current_x, 'o')
else:
self.world.grd.set_tile(agt.current_y, agt.current_x, str(num))
agt.do_your_job()
self.world.grd.set_tile(agt.current_y, agt.current_x, 'A') # update the main world grid with agents changes
# save grid after each run if required
if log_file_base != 'N':
self.world.grd.save(log_file_base + '_' + str(cur_run) + '.log')
# save the agents results here
with open (log_file_base + '__agents.txt', "a") as f:
f.write("\nWorld tgt= [" + str(self.agent_list[0].target_y) + "," + str(self.agent_list[0].target_x) + "]\n")
f.write(str(self.world.grd))
f.write('\n\nAgent Name , starting, num Steps , num Climbs\n')
for num, agt in enumerate(self.agent_list):
res = agt.name + ' , [' + str(agt.start_y) + ', ' + str(agt.start_x) + '], '
res += str(agt.num_steps) + ' , ' + str(agt.num_climbs) + ' , '
res += ''.join([a for a in agt.results])
f.write(res + '\n') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:merge; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:arg; 5, list_splat_pattern; 5, 6; 6, identifier:rest; 7, dictionary_splat_pattern; 7, 8; 8, identifier:kwargs; 9, block; 9, 10; 9, 12; 9, 21; 9, 27; 9, 39; 9, 40; 9, 41; 9, 42; 9, 46; 9, 76; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:ensure_keyword_args; 15, argument_list; 15, 16; 15, 17; 16, identifier:kwargs; 17, keyword_argument; 17, 18; 17, 19; 18, identifier:optional; 19, tuple; 19, 20; 20, string:'default'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:has_default; 24, comparison_operator:in; 24, 25; 24, 26; 25, string:'default'; 26, identifier:kwargs; 27, if_statement; 27, 28; 27, 29; 28, identifier:has_default; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:default; 33, call; 33, 34; 33, 35; 34, identifier:ensure_callable; 35, argument_list; 35, 36; 36, subscript; 36, 37; 36, 38; 37, identifier:kwargs; 38, string:'default'; 39, comment; 40, comment; 41, comment; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:unary_result; 45, True; 46, if_statement; 46, 47; 46, 48; 46, 70; 47, identifier:rest; 48, block; 48, 49; 48, 66; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:fs; 52, binary_operator:+; 52, 53; 52, 58; 53, tuple; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:ensure_callable; 56, argument_list; 56, 57; 57, identifier:arg; 58, call; 58, 59; 58, 60; 59, identifier:tuple; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:imap; 63, argument_list; 63, 64; 63, 65; 64, identifier:ensure_callable; 65, identifier:rest; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:unary_result; 69, False; 70, else_clause; 70, 71; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:fs; 75, identifier:arg; 76, if_statement; 76, 77; 76, 81; 76, 135; 77, call; 77, 78; 77, 79; 78, identifier:is_mapping; 79, argument_list; 79, 80; 80, identifier:fs; 81, block; 81, 82; 82, if_statement; 82, 83; 82, 84; 82, 111; 83, identifier:has_default; 84, block; 84, 85; 85, return_statement; 85, 86; 86, lambda; 86, 87; 86, 89; 87, lambda_parameters; 87, 88; 88, identifier:arg_; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:fs; 92, identifier:__class__; 93, generator_expression; 93, 94; 93, 108; 94, tuple; 94, 95; 94, 96; 95, identifier:k; 96, call; 96, 97; 96, 104; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:fs; 100, identifier:get; 101, argument_list; 101, 102; 101, 103; 102, identifier:k; 103, identifier:default; 104, argument_list; 104, 105; 105, subscript; 105, 106; 105, 107; 106, identifier:arg_; 107, identifier:k; 108, for_in_clause; 108, 109; 108, 110; 109, identifier:k; 110, identifier:arg_; 111, else_clause; 111, 112; 112, block; 112, 113; 113, return_statement; 113, 114; 114, lambda; 114, 115; 114, 117; 115, lambda_parameters; 115, 116; 116, identifier:arg_; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:fs; 120, identifier:__class__; 121, generator_expression; 121, 122; 121, 132; 122, tuple; 122, 123; 122, 124; 123, identifier:k; 124, call; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:fs; 127, identifier:k; 128, argument_list; 128, 129; 129, subscript; 129, 130; 129, 131; 130, identifier:arg_; 131, identifier:k; 132, for_in_clause; 132, 133; 132, 134; 133, identifier:k; 134, identifier:arg_; 135, else_clause; 135, 136; 136, block; 136, 137; 136, 142; 136, 212; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:ensure_sequence; 140, argument_list; 140, 141; 141, identifier:fs; 142, if_statement; 142, 143; 142, 144; 142, 145; 142, 146; 142, 147; 142, 182; 143, identifier:has_default; 144, comment; 145, comment; 146, comment; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:func; 151, lambda; 151, 152; 151, 154; 152, lambda_parameters; 152, 153; 153, identifier:arg_; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:fs; 157, identifier:__class__; 158, generator_expression; 158, 159; 158, 174; 159, call; 159, 160; 159, 172; 160, parenthesized_expression; 160, 161; 161, conditional_expression:if; 161, 162; 161, 165; 161, 171; 162, subscript; 162, 163; 162, 164; 163, identifier:fs; 164, identifier:i; 165, comparison_operator:<; 165, 166; 165, 167; 166, identifier:i; 167, call; 167, 168; 167, 169; 168, identifier:len; 169, argument_list; 169, 170; 170, identifier:fs; 171, identifier:default; 172, argument_list; 172, 173; 173, identifier:x; 174, for_in_clause; 174, 175; 174, 178; 175, pattern_list; 175, 176; 175, 177; 176, identifier:i; 177, identifier:x; 178, call; 178, 179; 178, 180; 179, identifier:enumerate; 180, argument_list; 180, 181; 181, identifier:arg_; 182, else_clause; 182, 183; 182, 184; 182, 185; 182, 186; 183, comment; 184, comment; 185, comment; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:func; 190, lambda; 190, 191; 190, 193; 191, lambda_parameters; 191, 192; 192, identifier:arg_; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:fs; 196, identifier:__class__; 197, generator_expression; 197, 198; 197, 204; 198, call; 198, 199; 198, 202; 199, subscript; 199, 200; 199, 201; 200, identifier:fs; 201, identifier:i; 202, argument_list; 202, 203; 203, identifier:x; 204, for_in_clause; 204, 205; 204, 208; 205, pattern_list; 205, 206; 205, 207; 206, identifier:i; 207, identifier:x; 208, call; 208, 209; 208, 210; 209, identifier:enumerate; 210, argument_list; 210, 211; 211, identifier:arg_; 212, return_statement; 212, 213; 213, conditional_expression:if; 213, 214; 213, 215; 213, 216; 214, identifier:func; 215, identifier:unary_result; 216, lambda; 216, 217; 216, 220; 217, lambda_parameters; 217, 218; 218, list_splat_pattern; 218, 219; 219, identifier:args; 220, call; 220, 221; 220, 222; 221, identifier:func; 222, argument_list; 222, 223; 223, identifier:args | def merge(arg, *rest, **kwargs):
"""Merge a collection, with functions as items, into a single function
that takes a collection and maps its items through corresponding functions.
:param arg: A collection of functions, such as list, tuple, or dictionary
:param default: Optional default function to use for items
within merged function's arguments that do not have
corresponding functions in ``arg``
Example with two-element tuple::
>> dict_ = {'Alice': -5, 'Bob': 4}
>> func = merge((str.upper, abs))
>> dict(map(func, dict_.items()))
{'ALICE': 5, 'BOB': 4}
Example with a dictionary::
>> func = merge({'id': int, 'name': str.split})
>> data = [
{'id': '1', 'name': "John Doe"},
{'id': '2', 'name': "Anne Arbor"},
]
>> list(map(func, data))
[{'id': 1, 'name': ['John', 'Doe']},
{'id': 2, 'name': ['Anne', 'Arbor']}]
:return: Merged function
.. versionadded:: 0.0.2
"""
ensure_keyword_args(kwargs, optional=('default',))
has_default = 'default' in kwargs
if has_default:
default = ensure_callable(kwargs['default'])
# if more than one argument was given, they must all be functions;
# result will be a function that takes multiple arguments (rather than
# a single collection) and returns a tuple
unary_result = True
if rest:
fs = (ensure_callable(arg),) + tuple(imap(ensure_callable, rest))
unary_result = False
else:
fs = arg
if is_mapping(fs):
if has_default:
return lambda arg_: fs.__class__((k, fs.get(k, default)(arg_[k]))
for k in arg_)
else:
return lambda arg_: fs.__class__((k, fs[k](arg_[k]))
for k in arg_)
else:
ensure_sequence(fs)
if has_default:
# we cannot use ``izip_longest(fs, arg_, fillvalue=default)``,
# because we want to terminate the generator
# only when ``arg_`` is exhausted (not when just ``fs`` is)
func = lambda arg_: fs.__class__(
(fs[i] if i < len(fs) else default)(x)
for i, x in enumerate(arg_))
else:
# we cannot use ``izip(fs, arg_)`` because it would short-circuit
# if ``arg_`` is longer than ``fs``, rather than raising
# the required ``IndexError``
func = lambda arg_: fs.__class__(fs[i](x)
for i, x in enumerate(arg_))
return func if unary_result else lambda *args: func(args) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:cmd; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:send; 5, identifier:msg; 6, identifier:args; 7, block; 7, 8; 7, 10; 7, 29; 7, 35; 7, 46; 7, 54; 7, 64; 7, 74; 7, 89; 7, 117; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 22; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 20; 13, attribute; 13, 14; 13, 19; 14, subscript; 14, 15; 14, 18; 15, subscript; 15, 16; 15, 17; 16, identifier:args; 17, string:'config'; 18, string:'feature'; 19, identifier:getboolean; 20, argument_list; 20, 21; 21, string:'hooks'; 22, block; 22, 23; 22, 28; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:send; 26, argument_list; 26, 27; 27, string:"Hooks are disabled, and this command depends on hooks. Please contact the bot admin(s)."; 28, return_statement; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:session; 32, subscript; 32, 33; 32, 34; 33, identifier:args; 34, string:'db'; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:parser; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:arguments; 41, identifier:ArgParser; 42, argument_list; 42, 43; 43, subscript; 43, 44; 43, 45; 44, identifier:args; 45, string:'config'; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:group; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:parser; 52, identifier:add_mutually_exclusive_group; 53, argument_list; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:group; 58, identifier:add_argument; 59, argument_list; 59, 60; 59, 61; 60, string:'--high'; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:action; 63, string:'store_true'; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:group; 68, identifier:add_argument; 69, argument_list; 69, 70; 69, 71; 70, string:'--low'; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:action; 73, string:'store_true'; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:group; 78, identifier:add_argument; 79, argument_list; 79, 80; 79, 81; 79, 84; 80, string:'nick'; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:nargs; 83, string:'?'; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:action; 86, attribute; 86, 87; 86, 88; 87, identifier:arguments; 88, identifier:NickParser; 89, try_statement; 89, 90; 89, 100; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:cmdargs; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:parser; 97, identifier:parse_args; 98, argument_list; 98, 99; 99, identifier:msg; 100, except_clause; 100, 101; 100, 107; 101, as_pattern; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:arguments; 104, identifier:ArgumentException; 105, as_pattern_target; 105, 106; 106, identifier:e; 107, block; 107, 108; 107, 116; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:send; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:str; 114, argument_list; 114, 115; 115, identifier:e; 116, return_statement; 117, if_statement; 117, 118; 117, 121; 117, 173; 117, 225; 117, 361; 118, attribute; 118, 119; 118, 120; 119, identifier:cmdargs; 120, identifier:high; 121, block; 121, 122; 121, 151; 121, 156; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:data; 125, call; 125, 126; 125, 150; 126, attribute; 126, 127; 126, 149; 127, call; 127, 128; 127, 147; 128, attribute; 128, 129; 128, 146; 129, call; 129, 130; 129, 138; 130, attribute; 130, 131; 130, 137; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:session; 134, identifier:query; 135, argument_list; 135, 136; 136, identifier:Scores; 137, identifier:order_by; 138, argument_list; 138, 139; 139, call; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:Scores; 143, identifier:score; 144, identifier:desc; 145, argument_list; 146, identifier:limit; 147, argument_list; 147, 148; 148, integer:3; 149, identifier:all; 150, argument_list; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:send; 154, argument_list; 154, 155; 155, string:'High Scores:'; 156, for_statement; 156, 157; 156, 158; 156, 159; 157, identifier:x; 158, identifier:data; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:send; 163, argument_list; 163, 164; 164, binary_operator:%; 164, 165; 164, 166; 165, string:"%s: %s"; 166, tuple; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:x; 169, identifier:nick; 170, attribute; 170, 171; 170, 172; 171, identifier:x; 172, identifier:score; 173, elif_clause; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:cmdargs; 176, identifier:low; 177, block; 177, 178; 177, 203; 177, 208; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:data; 181, call; 181, 182; 181, 202; 182, attribute; 182, 183; 182, 201; 183, call; 183, 184; 183, 199; 184, attribute; 184, 185; 184, 198; 185, call; 185, 186; 185, 194; 186, attribute; 186, 187; 186, 193; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:session; 190, identifier:query; 191, argument_list; 191, 192; 192, identifier:Scores; 193, identifier:order_by; 194, argument_list; 194, 195; 195, attribute; 195, 196; 195, 197; 196, identifier:Scores; 197, identifier:score; 198, identifier:limit; 199, argument_list; 199, 200; 200, integer:3; 201, identifier:all; 202, argument_list; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:send; 206, argument_list; 206, 207; 207, string:'Low Scores:'; 208, for_statement; 208, 209; 208, 210; 208, 211; 209, identifier:x; 210, identifier:data; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 215; 214, identifier:send; 215, argument_list; 215, 216; 216, binary_operator:%; 216, 217; 216, 218; 217, string:"%s: %s"; 218, tuple; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:x; 221, identifier:nick; 222, attribute; 222, 223; 222, 224; 223, identifier:x; 224, identifier:score; 225, elif_clause; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:cmdargs; 228, identifier:nick; 229, block; 229, 230; 229, 240; 229, 251; 229, 273; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:name; 233, call; 233, 234; 233, 239; 234, attribute; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:cmdargs; 237, identifier:nick; 238, identifier:lower; 239, argument_list; 240, if_statement; 240, 241; 240, 244; 241, comparison_operator:==; 241, 242; 241, 243; 242, identifier:name; 243, string:'c'; 244, block; 244, 245; 244, 250; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 248; 247, identifier:send; 248, argument_list; 248, 249; 249, string:"We all know you love C better than anything else, so why rub it in?"; 250, return_statement; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:score; 254, call; 254, 255; 254, 272; 255, attribute; 255, 256; 255, 271; 256, call; 256, 257; 256, 265; 257, attribute; 257, 258; 257, 264; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:session; 261, identifier:query; 262, argument_list; 262, 263; 263, identifier:Scores; 264, identifier:filter; 265, argument_list; 265, 266; 266, comparison_operator:==; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:Scores; 269, identifier:nick; 270, identifier:name; 271, identifier:scalar; 272, argument_list; 273, if_statement; 273, 274; 273, 277; 273, 352; 274, comparison_operator:is; 274, 275; 274, 276; 275, identifier:score; 276, None; 277, block; 277, 278; 277, 292; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:plural; 281, conditional_expression:if; 281, 282; 281, 283; 281, 291; 282, string:''; 283, comparison_operator:==; 283, 284; 283, 290; 284, call; 284, 285; 284, 286; 285, identifier:abs; 286, argument_list; 286, 287; 287, attribute; 287, 288; 287, 289; 288, identifier:score; 289, identifier:score; 290, integer:1; 291, string:'s'; 292, if_statement; 292, 293; 292, 302; 292, 338; 293, comparison_operator:==; 293, 294; 293, 295; 294, identifier:name; 295, call; 295, 296; 295, 301; 296, attribute; 296, 297; 296, 300; 297, subscript; 297, 298; 297, 299; 298, identifier:args; 299, string:'botnick'; 300, identifier:lower; 301, argument_list; 302, block; 302, 303; 302, 321; 302, 332; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:emote; 306, conditional_expression:if; 306, 307; 306, 308; 306, 313; 307, string:':)'; 308, comparison_operator:>; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:score; 311, identifier:score; 312, integer:0; 313, conditional_expression:if; 313, 314; 313, 315; 313, 320; 314, string:':('; 315, comparison_operator:<; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:score; 318, identifier:score; 319, integer:0; 320, string:':|'; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:output; 324, binary_operator:%; 324, 325; 324, 326; 325, string:'has %s point%s! %s'; 326, tuple; 326, 327; 326, 330; 326, 331; 327, attribute; 327, 328; 327, 329; 328, identifier:score; 329, identifier:score; 330, identifier:plural; 331, identifier:emote; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 335; 334, identifier:send; 335, argument_list; 335, 336; 335, 337; 336, identifier:output; 337, string:'action'; 338, else_clause; 338, 339; 339, block; 339, 340; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 343; 342, identifier:send; 343, argument_list; 343, 344; 344, binary_operator:%; 344, 345; 344, 346; 345, string:"%s has %i point%s!"; 346, tuple; 346, 347; 346, 348; 346, 351; 347, identifier:name; 348, attribute; 348, 349; 348, 350; 349, identifier:score; 350, identifier:score; 351, identifier:plural; 352, else_clause; 352, 353; 353, block; 353, 354; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 357; 356, identifier:send; 357, argument_list; 357, 358; 358, binary_operator:%; 358, 359; 358, 360; 359, string:"Nobody cares about %s"; 360, identifier:name; 361, else_clause; 361, 362; 362, block; 362, 363; 363, if_statement; 363, 364; 363, 376; 363, 382; 364, comparison_operator:==; 364, 365; 364, 375; 365, call; 365, 366; 365, 374; 366, attribute; 366, 367; 366, 373; 367, call; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:session; 370, identifier:query; 371, argument_list; 371, 372; 372, identifier:Scores; 373, identifier:count; 374, argument_list; 375, integer:0; 376, block; 376, 377; 377, expression_statement; 377, 378; 378, call; 378, 379; 378, 380; 379, identifier:send; 380, argument_list; 380, 381; 381, string:"Nobody cares about anything =("; 382, else_clause; 382, 383; 383, block; 383, 384; 383, 406; 383, 420; 384, expression_statement; 384, 385; 385, assignment; 385, 386; 385, 387; 386, identifier:query; 387, call; 387, 388; 387, 405; 388, attribute; 388, 389; 388, 404; 389, call; 389, 390; 389, 398; 390, attribute; 390, 391; 390, 397; 391, call; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:session; 394, identifier:query; 395, argument_list; 395, 396; 396, identifier:Scores; 397, identifier:order_by; 398, argument_list; 398, 399; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:func; 402, identifier:random; 403, argument_list; 404, identifier:first; 405, argument_list; 406, expression_statement; 406, 407; 407, assignment; 407, 408; 407, 409; 408, identifier:plural; 409, conditional_expression:if; 409, 410; 409, 411; 409, 419; 410, string:''; 411, comparison_operator:==; 411, 412; 411, 418; 412, call; 412, 413; 412, 414; 413, identifier:abs; 414, argument_list; 414, 415; 415, attribute; 415, 416; 415, 417; 416, identifier:query; 417, identifier:score; 418, integer:1; 419, string:'s'; 420, expression_statement; 420, 421; 421, call; 421, 422; 421, 423; 422, identifier:send; 423, argument_list; 423, 424; 424, binary_operator:%; 424, 425; 424, 426; 425, string:"%s has %i point%s!"; 426, tuple; 426, 427; 426, 430; 426, 433; 427, attribute; 427, 428; 427, 429; 428, identifier:query; 429, identifier:nick; 430, attribute; 430, 431; 430, 432; 431, identifier:query; 432, identifier:score; 433, identifier:plural | def cmd(send, msg, args):
"""Gets scores.
Syntax: {command} <--high|--low|nick>
"""
if not args['config']['feature'].getboolean('hooks'):
send("Hooks are disabled, and this command depends on hooks. Please contact the bot admin(s).")
return
session = args['db']
parser = arguments.ArgParser(args['config'])
group = parser.add_mutually_exclusive_group()
group.add_argument('--high', action='store_true')
group.add_argument('--low', action='store_true')
group.add_argument('nick', nargs='?', action=arguments.NickParser)
try:
cmdargs = parser.parse_args(msg)
except arguments.ArgumentException as e:
send(str(e))
return
if cmdargs.high:
data = session.query(Scores).order_by(Scores.score.desc()).limit(3).all()
send('High Scores:')
for x in data:
send("%s: %s" % (x.nick, x.score))
elif cmdargs.low:
data = session.query(Scores).order_by(Scores.score).limit(3).all()
send('Low Scores:')
for x in data:
send("%s: %s" % (x.nick, x.score))
elif cmdargs.nick:
name = cmdargs.nick.lower()
if name == 'c':
send("We all know you love C better than anything else, so why rub it in?")
return
score = session.query(Scores).filter(Scores.nick == name).scalar()
if score is not None:
plural = '' if abs(score.score) == 1 else 's'
if name == args['botnick'].lower():
emote = ':)' if score.score > 0 else ':(' if score.score < 0 else ':|'
output = 'has %s point%s! %s' % (score.score, plural, emote)
send(output, 'action')
else:
send("%s has %i point%s!" % (name, score.score, plural))
else:
send("Nobody cares about %s" % name)
else:
if session.query(Scores).count() == 0:
send("Nobody cares about anything =(")
else:
query = session.query(Scores).order_by(func.random()).first()
plural = '' if abs(query.score) == 1 else 's'
send("%s has %i point%s!" % (query.nick, query.score, plural)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:buildPrices; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:roles; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:regex; 10, identifier:default_price_regex; 11, default_parameter; 11, 12; 11, 13; 12, identifier:default; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:additional; 16, dictionary; 17, block; 17, 18; 17, 20; 18, expression_statement; 18, 19; 19, string:''' Create a dictionary with price information. Multiple ways are
supported.
:rtype: :obj:`dict`: keys are role as str, values are the prices as
cent count'''; 20, if_statement; 20, 21; 20, 26; 20, 63; 20, 142; 20, 184; 21, call; 21, 22; 21, 23; 22, identifier:isinstance; 23, argument_list; 23, 24; 23, 25; 24, identifier:data; 25, identifier:dict; 26, block; 26, 27; 26, 48; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:data; 30, list_comprehension; 30, 31; 30, 41; 31, tuple; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:item; 34, integer:0; 35, call; 35, 36; 35, 37; 36, identifier:convertPrice; 37, argument_list; 37, 38; 38, subscript; 38, 39; 38, 40; 39, identifier:item; 40, integer:1; 41, for_in_clause; 41, 42; 41, 43; 42, identifier:item; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:data; 46, identifier:items; 47, argument_list; 48, return_statement; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:dict; 51, argument_list; 51, 52; 52, list_comprehension; 52, 53; 52, 54; 52, 57; 53, identifier:v; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:v; 56, identifier:data; 57, if_clause; 57, 58; 58, comparison_operator:is; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:v; 61, integer:1; 62, None; 63, elif_clause; 63, 64; 63, 79; 64, boolean_operator:and; 64, 65; 64, 73; 65, call; 65, 66; 65, 67; 66, identifier:isinstance; 67, argument_list; 67, 68; 67, 69; 68, identifier:data; 69, tuple; 69, 70; 69, 71; 69, 72; 70, identifier:str; 71, identifier:float; 72, identifier:int; 73, not_operator; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:isinstance; 76, argument_list; 76, 77; 76, 78; 77, identifier:data; 78, identifier:bool; 79, block; 79, 80; 79, 92; 79, 99; 79, 106; 79, 113; 79, 140; 80, if_statement; 80, 81; 80, 84; 81, comparison_operator:is; 81, 82; 81, 83; 82, identifier:default; 83, None; 84, block; 84, 85; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:ValueError; 88, argument_list; 88, 89; 89, concatenated_string; 89, 90; 89, 91; 90, string:'You have to call setAdditionalCharges '; 91, string:'before it is possible to pass a string as price'; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:basePrice; 95, call; 95, 96; 95, 97; 96, identifier:convertPrice; 97, argument_list; 97, 98; 98, identifier:data; 99, if_statement; 99, 100; 99, 103; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:basePrice; 102, None; 103, block; 103, 104; 104, return_statement; 104, 105; 105, dictionary; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:prices; 109, dictionary; 109, 110; 110, pair; 110, 111; 110, 112; 111, identifier:default; 112, identifier:basePrice; 113, for_statement; 113, 114; 113, 115; 113, 116; 114, identifier:role; 115, identifier:additional; 116, block; 116, 117; 116, 126; 116, 132; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:extraCharge; 120, call; 120, 121; 120, 122; 121, identifier:convertPrice; 122, argument_list; 122, 123; 123, subscript; 123, 124; 123, 125; 124, identifier:additional; 125, identifier:role; 126, if_statement; 126, 127; 126, 130; 127, comparison_operator:is; 127, 128; 127, 129; 128, identifier:extraCharge; 129, None; 130, block; 130, 131; 131, continue_statement; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:prices; 136, identifier:role; 137, binary_operator:+; 137, 138; 137, 139; 138, identifier:basePrice; 139, identifier:extraCharge; 140, return_statement; 140, 141; 141, identifier:prices; 142, elif_clause; 142, 143; 142, 144; 143, identifier:roles; 144, block; 144, 145; 144, 149; 144, 156; 144, 182; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:prices; 148, dictionary; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:priceRoles; 152, call; 152, 153; 152, 154; 153, identifier:iter; 154, argument_list; 154, 155; 155, identifier:roles; 156, for_statement; 156, 157; 156, 158; 156, 159; 157, identifier:priceData; 158, identifier:data; 159, block; 159, 160; 159, 167; 159, 173; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:price; 163, call; 163, 164; 163, 165; 164, identifier:convertPrice; 165, argument_list; 165, 166; 166, identifier:priceData; 167, if_statement; 167, 168; 167, 171; 168, comparison_operator:is; 168, 169; 168, 170; 169, identifier:price; 170, None; 171, block; 171, 172; 172, continue_statement; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 181; 175, subscript; 175, 176; 175, 177; 176, identifier:prices; 177, call; 177, 178; 177, 179; 178, identifier:next; 179, argument_list; 179, 180; 180, identifier:priceRoles; 181, identifier:price; 182, return_statement; 182, 183; 183, identifier:prices; 184, else_clause; 184, 185; 185, block; 185, 186; 186, raise_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:TypeError; 189, argument_list; 189, 190; 190, string:'This type is for prices not supported!' | def buildPrices(data, roles=None, regex=default_price_regex,
default=None, additional={}):
''' Create a dictionary with price information. Multiple ways are
supported.
:rtype: :obj:`dict`: keys are role as str, values are the prices as
cent count'''
if isinstance(data, dict):
data = [(item[0], convertPrice(item[1])) for item in data.items()]
return dict([v for v in data if v[1] is not None])
elif isinstance(data, (str, float, int)) and not isinstance(data, bool):
if default is None:
raise ValueError('You have to call setAdditionalCharges '
'before it is possible to pass a string as price')
basePrice = convertPrice(data)
if basePrice is None:
return {}
prices = {default: basePrice}
for role in additional:
extraCharge = convertPrice(additional[role])
if extraCharge is None:
continue
prices[role] = basePrice + extraCharge
return prices
elif roles:
prices = {}
priceRoles = iter(roles)
for priceData in data:
price = convertPrice(priceData)
if price is None:
continue
prices[next(priceRoles)] = price
return prices
else:
raise TypeError('This type is for prices not supported!') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:toTag; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:output; 6, block; 6, 7; 6, 9; 6, 10; 6, 19; 6, 42; 6, 65; 6, 88; 6, 111; 6, 134; 6, 156; 6, 179; 6, 180; 6, 209; 6, 210; 6, 313; 7, expression_statement; 7, 8; 8, string:''' This methods adds all data of this canteen as canteen xml tag
to the given xml Document.
:meth:`toXMLFeed` uses this method to create the XML Feed. So there is
normally no need to call it directly.
:param output: XML Document to which the data should be added
:type output: xml.dom.DOMImplementation.createDocument
'''; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:canteen; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:output; 16, identifier:createElement; 17, argument_list; 17, 18; 18, string:'canteen'; 19, if_statement; 19, 20; 19, 25; 20, comparison_operator:is; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_name; 24, None; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:canteen; 30, identifier:appendChild; 31, argument_list; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:_buildStringTag; 36, argument_list; 36, 37; 36, 38; 36, 41; 37, string:'name'; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:_name; 41, identifier:output; 42, if_statement; 42, 43; 42, 48; 43, comparison_operator:is; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:_address; 47, None; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:canteen; 53, identifier:appendChild; 54, argument_list; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:_buildStringTag; 59, argument_list; 59, 60; 59, 61; 59, 64; 60, string:'address'; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:_address; 64, identifier:output; 65, if_statement; 65, 66; 65, 71; 66, comparison_operator:is; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:_city; 70, None; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:canteen; 76, identifier:appendChild; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:_buildStringTag; 82, argument_list; 82, 83; 82, 84; 82, 87; 83, string:'city'; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:_city; 87, identifier:output; 88, if_statement; 88, 89; 88, 94; 89, comparison_operator:is; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:_phone; 93, None; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:canteen; 99, identifier:appendChild; 100, argument_list; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:_buildStringTag; 105, argument_list; 105, 106; 105, 107; 105, 110; 106, string:'phone'; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:_phone; 110, identifier:output; 111, if_statement; 111, 112; 111, 117; 112, comparison_operator:is; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:_email; 116, None; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:canteen; 122, identifier:appendChild; 123, argument_list; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:_buildStringTag; 128, argument_list; 128, 129; 128, 130; 128, 133; 129, string:'email'; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:_email; 133, identifier:output; 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:_location; 139, None; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:canteen; 145, identifier:appendChild; 146, argument_list; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:_buildLocationTag; 151, argument_list; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:_location; 155, identifier:output; 156, if_statement; 156, 157; 156, 162; 157, comparison_operator:is; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:_availability; 161, None; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:canteen; 167, identifier:appendChild; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:_buildStringTag; 173, argument_list; 173, 174; 173, 175; 173, 178; 174, string:'availability'; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:_availability; 178, identifier:output; 179, comment; 180, for_statement; 180, 181; 180, 182; 180, 196; 181, identifier:feed; 182, call; 182, 183; 182, 184; 183, identifier:sorted; 184, argument_list; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:feeds; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:key; 190, lambda; 190, 191; 190, 193; 191, lambda_parameters; 191, 192; 192, identifier:v; 193, attribute; 193, 194; 193, 195; 194, identifier:v; 195, identifier:priority; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:canteen; 201, identifier:appendChild; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:feed; 206, identifier:toTag; 207, argument_list; 207, 208; 208, identifier:output; 209, comment; 210, for_statement; 210, 211; 210, 212; 210, 222; 211, identifier:date; 212, call; 212, 213; 212, 214; 213, identifier:sorted; 214, argument_list; 214, 215; 215, call; 215, 216; 215, 221; 216, attribute; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:self; 219, identifier:_days; 220, identifier:keys; 221, argument_list; 222, block; 222, 223; 222, 232; 222, 243; 222, 277; 222, 278; 222, 306; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:day; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:output; 229, identifier:createElement; 230, argument_list; 230, 231; 231, string:'day'; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:day; 236, identifier:setAttribute; 237, argument_list; 237, 238; 237, 239; 238, string:'date'; 239, call; 239, 240; 239, 241; 240, identifier:str; 241, argument_list; 241, 242; 242, identifier:date; 243, if_statement; 243, 244; 243, 251; 243, 252; 244, comparison_operator:is; 244, 245; 244, 250; 245, subscript; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:_days; 249, identifier:date; 250, False; 251, comment; 252, block; 252, 253; 252, 262; 252, 269; 252, 276; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:closed; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:output; 259, identifier:createElement; 260, argument_list; 260, 261; 261, string:'closed'; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:day; 266, identifier:appendChild; 267, argument_list; 267, 268; 268, identifier:closed; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:canteen; 273, identifier:appendChild; 274, argument_list; 274, 275; 275, identifier:day; 276, continue_statement; 277, comment; 278, for_statement; 278, 279; 278, 280; 278, 285; 279, identifier:categoryname; 280, subscript; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:self; 283, identifier:_days; 284, identifier:date; 285, block; 285, 286; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:day; 290, identifier:appendChild; 291, argument_list; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:self; 295, identifier:_buildCategoryTag; 296, argument_list; 296, 297; 296, 298; 296, 305; 297, identifier:categoryname; 298, subscript; 298, 299; 298, 304; 299, subscript; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:self; 302, identifier:_days; 303, identifier:date; 304, identifier:categoryname; 305, identifier:output; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:canteen; 310, identifier:appendChild; 311, argument_list; 311, 312; 312, identifier:day; 313, return_statement; 313, 314; 314, identifier:canteen | def toTag(self, output):
''' This methods adds all data of this canteen as canteen xml tag
to the given xml Document.
:meth:`toXMLFeed` uses this method to create the XML Feed. So there is
normally no need to call it directly.
:param output: XML Document to which the data should be added
:type output: xml.dom.DOMImplementation.createDocument
'''
# create canteen tag, which represents our data
canteen = output.createElement('canteen')
if self._name is not None:
canteen.appendChild(self._buildStringTag('name', self._name, output))
if self._address is not None:
canteen.appendChild(self._buildStringTag('address', self._address, output))
if self._city is not None:
canteen.appendChild(self._buildStringTag('city', self._city, output))
if self._phone is not None:
canteen.appendChild(self._buildStringTag('phone', self._phone, output))
if self._email is not None:
canteen.appendChild(self._buildStringTag('email', self._email, output))
if self._location is not None:
canteen.appendChild(self._buildLocationTag(self._location, output))
if self._availability is not None:
canteen.appendChild(self._buildStringTag('availability', self._availability, output))
# iterate above all feeds:
for feed in sorted(self.feeds, key=lambda v: v.priority):
canteen.appendChild(feed.toTag(output))
# iterate above all days (sorted):
for date in sorted(self._days.keys()):
day = output.createElement('day')
day.setAttribute('date', str(date))
if self._days[date] is False: # canteen closed
closed = output.createElement('closed')
day.appendChild(closed)
canteen.appendChild(day)
continue
# canteen is open
for categoryname in self._days[date]:
day.appendChild(self._buildCategoryTag(
categoryname, self._days[date][categoryname], output))
canteen.appendChild(day)
return canteen |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 33; 2, function_name:AddAnalogShortIdMsecRecord; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 4, identifier:site_service; 5, identifier:tag; 6, identifier:time_value; 7, identifier:msec; 8, identifier:value; 9, default_parameter; 9, 10; 9, 11; 10, identifier:low_warn; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:high_warn; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:low_alarm; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:high_alarm; 20, False; 21, default_parameter; 21, 22; 21, 23; 22, identifier:oor_low; 23, False; 24, default_parameter; 24, 25; 24, 26; 25, identifier:oor_high; 26, False; 27, default_parameter; 27, 28; 27, 29; 28, identifier:unreliable; 29, False; 30, default_parameter; 30, 31; 30, 32; 31, identifier:manual; 32, False; 33, block; 33, 34; 33, 36; 33, 37; 33, 49; 33, 61; 33, 71; 33, 78; 33, 88; 33, 98; 33, 108; 33, 118; 33, 128; 33, 138; 33, 148; 33, 158; 33, 165; 33, 166; 33, 187; 34, expression_statement; 34, 35; 35, comment; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:szService; 40, call; 40, 41; 40, 42; 41, identifier:c_char_p; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:site_service; 46, identifier:encode; 47, argument_list; 47, 48; 48, string:'utf-8'; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:szPointId; 52, call; 52, 53; 52, 54; 53, identifier:c_char_p; 54, argument_list; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:tag; 58, identifier:encode; 59, argument_list; 59, 60; 60, string:'utf-8'; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:tTime; 64, call; 64, 65; 64, 66; 65, identifier:c_long; 66, argument_list; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:int; 69, argument_list; 69, 70; 70, identifier:time_value; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:dValue; 74, call; 74, 75; 74, 76; 75, identifier:c_double; 76, argument_list; 76, 77; 77, identifier:value; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:bLowWarning; 81, call; 81, 82; 81, 83; 82, identifier:c_int; 83, argument_list; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:int; 86, argument_list; 86, 87; 87, identifier:low_warn; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:bHighWarning; 91, call; 91, 92; 91, 93; 92, identifier:c_int; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:int; 96, argument_list; 96, 97; 97, identifier:high_warn; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:bLowAlarm; 101, call; 101, 102; 101, 103; 102, identifier:c_int; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:int; 106, argument_list; 106, 107; 107, identifier:low_alarm; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:bHighAlarm; 111, call; 111, 112; 111, 113; 112, identifier:c_int; 113, argument_list; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:int; 116, argument_list; 116, 117; 117, identifier:high_alarm; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:bOutOfRangeLow; 121, call; 121, 122; 121, 123; 122, identifier:c_int; 123, argument_list; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:int; 126, argument_list; 126, 127; 127, identifier:oor_low; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:bOutOfRangeHigh; 131, call; 131, 132; 131, 133; 132, identifier:c_int; 133, argument_list; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:int; 136, argument_list; 136, 137; 137, identifier:oor_high; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:bUnReliable; 141, call; 141, 142; 141, 143; 142, identifier:c_int; 143, argument_list; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:int; 146, argument_list; 146, 147; 147, identifier:unreliable; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:bManual; 151, call; 151, 152; 151, 153; 152, identifier:c_int; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:int; 156, argument_list; 156, 157; 157, identifier:manual; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:usMsec; 161, call; 161, 162; 161, 163; 162, identifier:c_ushort; 163, argument_list; 163, 164; 164, identifier:msec; 165, comment; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:nRet; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:dnaserv_dll; 172, identifier:DnaAddAnalogShortIdMsecRecord; 173, argument_list; 173, 174; 173, 175; 173, 176; 173, 177; 173, 178; 173, 179; 173, 180; 173, 181; 173, 182; 173, 183; 173, 184; 173, 185; 173, 186; 174, identifier:szService; 175, identifier:szPointId; 176, identifier:tTime; 177, identifier:dValue; 178, identifier:bLowWarning; 179, identifier:bHighWarning; 180, identifier:bLowAlarm; 181, identifier:bHighAlarm; 182, identifier:bOutOfRangeLow; 183, identifier:bOutOfRangeHigh; 184, identifier:bUnReliable; 185, identifier:bManual; 186, identifier:usMsec; 187, return_statement; 187, 188; 188, identifier:nRet | def AddAnalogShortIdMsecRecord(site_service, tag, time_value, msec, value,
low_warn=False, high_warn=False, low_alarm=False, high_alarm=False,
oor_low=False, oor_high=False, unreliable=False, manual=False):
"""
This function will add an analog value to the specified eDNA service and
tag, with many optional status definitions.
:param site_service: The site.service where data will be pushed
:param tag: The eDNA tag to push data. Tag only (e.g. ADE1CA01)
:param time_value: The time of the point, which MUST be in UTC Epoch
format. For example, "1483926416" not "2016/01/01 01:01:01".
:param msec: The additional milliseconds for the time_value
:param value: The value associated with the above time.
:param low_warn: TRUE if the point is in a low warning state
:param high_warn: TRUE if the point is in a high warning state
:param low_alarm: TRUE if the point is in a low alarm state
:param high_alarm: TRUE if the point is in a high alarm state
:param oor_low: TRUE if the point is out-of-range low
:param oor_high: TRUE if the point is out-of-range high
:param unreliable: TRUE if the point is unreliable
:param manual: TRUE if the point is manually set
:return: 0, if the data push is successful
"""
# Define all required variables in the correct ctypes format
szService = c_char_p(site_service.encode('utf-8'))
szPointId = c_char_p(tag.encode('utf-8'))
tTime = c_long(int(time_value))
dValue = c_double(value)
bLowWarning = c_int(int(low_warn))
bHighWarning = c_int(int(high_warn))
bLowAlarm = c_int(int(low_alarm))
bHighAlarm = c_int(int(high_alarm))
bOutOfRangeLow = c_int(int(oor_low))
bOutOfRangeHigh = c_int(int(oor_high))
bUnReliable = c_int(int(unreliable))
bManual = c_int(int(manual))
usMsec = c_ushort(msec)
# Try to push the data. Function will return 0 if successful.
nRet = dnaserv_dll.DnaAddAnalogShortIdMsecRecord(szService, szPointId,
tTime, dValue, bLowWarning, bHighWarning, bLowAlarm,
bHighAlarm, bOutOfRangeLow, bOutOfRangeHigh, bUnReliable,
bManual, usMsec)
return nRet |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:update_node_ids; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sub_job_num; 7, None; 8, block; 8, 9; 8, 11; 8, 12; 8, 30; 8, 34; 8, 38; 8, 72; 8, 73; 8, 88; 8, 89; 8, 94; 8, 111; 8, 128; 8, 140; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:dag_id; 15, conditional_expression:if; 15, 16; 15, 23; 15, 24; 16, binary_operator:%; 16, 17; 16, 18; 17, string:'%s.%s'; 18, tuple; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:cluster_id; 22, identifier:sub_job_num; 23, identifier:sub_job_num; 24, call; 24, 25; 24, 26; 25, identifier:str; 26, argument_list; 26, 27; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:cluster_id; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:job_delimiter; 33, string:'+++'; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:attr_delimiter; 37, string:';;;'; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:format; 41, list:[
'-format', '"%d' + attr_delimiter + '"', 'ClusterId',
'-format', '"%v' + attr_delimiter + '"', 'Cmd',
'-format', '"%v' + attr_delimiter + '"', 'Args', # Old way
'-format', '"%v' + job_delimiter + '"', 'Arguments' # New way
]; 41, 42; 41, 43; 41, 48; 41, 49; 41, 50; 41, 55; 41, 56; 41, 57; 41, 62; 41, 63; 41, 64; 41, 65; 41, 70; 41, 71; 42, string:'-format'; 43, binary_operator:+; 43, 44; 43, 47; 44, binary_operator:+; 44, 45; 44, 46; 45, string:'"%d'; 46, identifier:attr_delimiter; 47, string:'"'; 48, string:'ClusterId'; 49, string:'-format'; 50, binary_operator:+; 50, 51; 50, 54; 51, binary_operator:+; 51, 52; 51, 53; 52, string:'"%v'; 53, identifier:attr_delimiter; 54, string:'"'; 55, string:'Cmd'; 56, string:'-format'; 57, binary_operator:+; 57, 58; 57, 61; 58, binary_operator:+; 58, 59; 58, 60; 59, string:'"%v'; 60, identifier:attr_delimiter; 61, string:'"'; 62, string:'Args'; 63, comment; 64, string:'-format'; 65, binary_operator:+; 65, 66; 65, 69; 66, binary_operator:+; 66, 67; 66, 68; 67, string:'"%v'; 68, identifier:job_delimiter; 69, string:'"'; 70, string:'Arguments'; 71, comment; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:cmd; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, string:'condor_q -constraint DAGManJobID=={0} {1} && condor_history -constraint DAGManJobID=={0} {1}'; 79, identifier:format; 80, argument_list; 80, 81; 80, 82; 81, identifier:dag_id; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, string:' '; 85, identifier:join; 86, argument_list; 86, 87; 87, identifier:format; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:_args; 92, list:[cmd]; 92, 93; 93, identifier:cmd; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, pattern_list; 96, 97; 96, 98; 97, identifier:out; 98, identifier:err; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:_execute; 103, argument_list; 103, 104; 103, 105; 103, 108; 104, identifier:_args; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:shell; 107, True; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:run_in_job_dir; 110, False; 111, if_statement; 111, 112; 111, 113; 112, identifier:err; 113, block; 113, 114; 113, 123; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:log; 118, identifier:error; 119, argument_list; 119, 120; 119, 121; 119, 122; 120, string:'Error while associating ids for jobs dag %s: %s'; 121, identifier:dag_id; 122, identifier:err; 123, raise_statement; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:HTCondorError; 126, argument_list; 126, 127; 127, identifier:err; 128, if_statement; 128, 129; 128, 131; 129, not_operator; 129, 130; 130, identifier:out; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:log; 136, identifier:warning; 137, argument_list; 137, 138; 137, 139; 138, string:'Error while associating ids for jobs in dag %s: No jobs found for dag.'; 139, identifier:dag_id; 140, try_statement; 140, 141; 140, 142; 140, 296; 141, comment; 142, block; 142, 143; 142, 152; 142, 153; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:jobs_out; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:out; 149, identifier:split; 150, argument_list; 150, 151; 151, identifier:job_delimiter; 152, comment; 153, for_statement; 153, 154; 153, 155; 153, 158; 154, identifier:node; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:_node_set; 158, block; 158, 159; 158, 165; 158, 166; 158, 176; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:job; 162, attribute; 162, 163; 162, 164; 163, identifier:node; 164, identifier:job; 165, comment; 166, if_statement; 166, 167; 166, 174; 167, comparison_operator:!=; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:job; 170, identifier:cluster_id; 171, attribute; 171, 172; 171, 173; 172, identifier:job; 173, identifier:NULL_CLUSTER_ID; 174, block; 174, 175; 175, continue_statement; 176, for_statement; 176, 177; 176, 178; 176, 179; 177, identifier:job_out; 178, identifier:jobs_out; 179, block; 179, 180; 179, 189; 179, 190; 179, 203; 179, 204; 179, 246; 179, 252; 179, 267; 180, if_statement; 180, 181; 180, 187; 181, boolean_operator:or; 181, 182; 181, 184; 182, not_operator; 182, 183; 183, identifier:job_out; 184, comparison_operator:not; 184, 185; 184, 186; 185, identifier:attr_delimiter; 186, identifier:job_out; 187, block; 187, 188; 188, continue_statement; 189, comment; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 197; 192, pattern_list; 192, 193; 192, 194; 192, 195; 192, 196; 193, identifier:cluster_id; 194, identifier:cmd; 195, identifier:_args; 196, identifier:_arguments; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:job_out; 200, identifier:split; 201, argument_list; 201, 202; 202, identifier:attr_delimiter; 203, comment; 204, if_statement; 204, 205; 204, 212; 204, 221; 204, 222; 204, 235; 204, 236; 205, boolean_operator:and; 205, 206; 205, 209; 206, comparison_operator:==; 206, 207; 206, 208; 207, identifier:_args; 208, string:'undefined'; 209, comparison_operator:!=; 209, 210; 209, 211; 210, identifier:_arguments; 211, string:'undefined'; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:args; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:_arguments; 219, identifier:strip; 220, argument_list; 221, comment; 222, elif_clause; 222, 223; 222, 230; 223, boolean_operator:and; 223, 224; 223, 227; 224, comparison_operator:==; 224, 225; 224, 226; 225, identifier:_args; 226, string:'undefined'; 227, comparison_operator:==; 227, 228; 227, 229; 228, identifier:_arguments; 229, string:'undefined'; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:args; 234, None; 235, comment; 236, else_clause; 236, 237; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:args; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:_args; 244, identifier:strip; 245, argument_list; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:job_cmd; 249, attribute; 249, 250; 249, 251; 250, identifier:job; 251, identifier:executable; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:job_args; 255, conditional_expression:if; 255, 256; 255, 263; 255, 266; 256, call; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:job; 260, identifier:arguments; 261, identifier:strip; 262, argument_list; 263, attribute; 263, 264; 263, 265; 264, identifier:job; 265, identifier:arguments; 266, None; 267, if_statement; 267, 268; 267, 275; 268, boolean_operator:and; 268, 269; 268, 272; 269, comparison_operator:in; 269, 270; 269, 271; 270, identifier:job_cmd; 271, identifier:cmd; 272, comparison_operator:==; 272, 273; 272, 274; 273, identifier:job_args; 274, identifier:args; 275, block; 275, 276; 275, 286; 275, 295; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:log; 280, identifier:info; 281, argument_list; 281, 282; 281, 283; 281, 284; 281, 285; 282, string:'Linking cluster_id %s to job with command and arguments: %s %s'; 283, identifier:cluster_id; 284, identifier:job_cmd; 285, identifier:job_args; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:job; 290, identifier:_cluster_id; 291, call; 291, 292; 291, 293; 292, identifier:int; 293, argument_list; 293, 294; 294, identifier:cluster_id; 295, break_statement; 296, except_clause; 296, 297; 296, 301; 297, as_pattern; 297, 298; 297, 299; 298, identifier:ValueError; 299, as_pattern_target; 299, 300; 300, identifier:e; 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:warning; 307, argument_list; 307, 308; 308, call; 308, 309; 308, 310; 309, identifier:str; 310, argument_list; 310, 311; 311, identifier:e | def update_node_ids(self, sub_job_num=None):
"""
Associate Jobs with respective cluster ids.
"""
# Build condor_q and condor_history commands
dag_id = '%s.%s' % (self.cluster_id, sub_job_num) if sub_job_num else str(self.cluster_id)
job_delimiter = '+++'
attr_delimiter = ';;;'
format = [
'-format', '"%d' + attr_delimiter + '"', 'ClusterId',
'-format', '"%v' + attr_delimiter + '"', 'Cmd',
'-format', '"%v' + attr_delimiter + '"', 'Args', # Old way
'-format', '"%v' + job_delimiter + '"', 'Arguments' # New way
]
# Get ID, Executable, and Arguments for each job that is either started to be processed or finished in the workflow
cmd = 'condor_q -constraint DAGManJobID=={0} {1} && condor_history -constraint DAGManJobID=={0} {1}'.format(dag_id, ' '.join(format))
# 'condor_q -constraint DAGManJobID==1018 -format "%d\n" ClusterId -format "%s\n" CMD -format "%s\n" ARGS && condor_history -constraint DAGManJobID==1018 -format "%d\n" ClusterId -format "%s\n" CMD -format "%s\n" ARGS'
_args = [cmd]
out, err = self._execute(_args, shell=True, run_in_job_dir=False)
if err:
log.error('Error while associating ids for jobs dag %s: %s', dag_id, err)
raise HTCondorError(err)
if not out:
log.warning('Error while associating ids for jobs in dag %s: No jobs found for dag.', dag_id)
try:
# Split into one line per job
jobs_out = out.split(job_delimiter)
# Match node to cluster id using combination of cmd and arguments
for node in self._node_set:
job = node.job
# Skip jobs that already have cluster id defined
if job.cluster_id != job.NULL_CLUSTER_ID:
continue
for job_out in jobs_out:
if not job_out or attr_delimiter not in job_out:
continue
# Split line by attributes
cluster_id, cmd, _args, _arguments = job_out.split(attr_delimiter)
# If new form of arguments is used, _args will be 'undefined' and _arguments will not
if _args == 'undefined' and _arguments != 'undefined':
args = _arguments.strip()
# If both are undefined, then there are no arguments
elif _args == 'undefined' and _arguments == 'undefined':
args = None
# Otherwise, using old form and _arguments will be 'undefined' and _args will not.
else:
args = _args.strip()
job_cmd = job.executable
job_args = job.arguments.strip() if job.arguments else None
if job_cmd in cmd and job_args == args:
log.info('Linking cluster_id %s to job with command and arguments: %s %s', cluster_id,
job_cmd, job_args)
job._cluster_id = int(cluster_id)
break
except ValueError as e:
log.warning(str(e)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:concat_cols; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 14; 4, identifier:df1; 5, identifier:df2; 6, identifier:idx_col; 7, identifier:df1_cols; 8, identifier:df2_cols; 9, identifier:df1_suffix; 10, identifier:df2_suffix; 11, default_parameter; 11, 12; 11, 13; 12, identifier:wc_cols; 13, list:[]; 14, default_parameter; 14, 15; 14, 16; 15, identifier:suffix_all; 16, False; 17, block; 17, 18; 17, 20; 17, 29; 17, 38; 17, 83; 17, 109; 17, 110; 17, 111; 17, 112; 17, 189; 17, 197; 17, 205; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:df1; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:df1; 26, identifier:set_index; 27, argument_list; 27, 28; 28, identifier:idx_col; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:df2; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:df2; 35, identifier:set_index; 36, argument_list; 36, 37; 37, identifier:idx_col; 38, if_statement; 38, 39; 38, 46; 39, not_operator; 39, 40; 40, comparison_operator:==; 40, 41; 40, 45; 41, call; 41, 42; 41, 43; 42, identifier:len; 43, argument_list; 43, 44; 44, identifier:wc_cols; 45, integer:0; 46, block; 46, 47; 47, for_statement; 47, 48; 47, 49; 47, 50; 48, identifier:wc; 49, identifier:wc_cols; 50, block; 50, 51; 50, 67; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:df1_cols; 54, binary_operator:+; 54, 55; 54, 56; 55, identifier:df1_cols; 56, list_comprehension; 56, 57; 56, 58; 56, 63; 57, identifier:c; 58, for_in_clause; 58, 59; 58, 60; 59, identifier:c; 60, attribute; 60, 61; 60, 62; 61, identifier:df1; 62, identifier:columns; 63, if_clause; 63, 64; 64, comparison_operator:in; 64, 65; 64, 66; 65, identifier:wc; 66, identifier:c; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:df2_cols; 70, binary_operator:+; 70, 71; 70, 72; 71, identifier:df2_cols; 72, list_comprehension; 72, 73; 72, 74; 72, 79; 73, identifier:c; 74, for_in_clause; 74, 75; 74, 76; 75, identifier:c; 76, attribute; 76, 77; 76, 78; 77, identifier:df2; 78, identifier:columns; 79, if_clause; 79, 80; 80, comparison_operator:in; 80, 81; 80, 82; 81, identifier:wc; 82, identifier:c; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:combo; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:pd; 89, identifier:concat; 90, argument_list; 90, 91; 90, 106; 91, list:[df1.loc[:,df1_cols],df2.loc[:,df2_cols]]; 91, 92; 91, 99; 92, subscript; 92, 93; 92, 96; 92, 98; 93, attribute; 93, 94; 93, 95; 94, identifier:df1; 95, identifier:loc; 96, slice; 96, 97; 97, colon; 98, identifier:df1_cols; 99, subscript; 99, 100; 99, 103; 99, 105; 100, attribute; 100, 101; 100, 102; 101, identifier:df2; 102, identifier:loc; 103, slice; 103, 104; 104, colon; 105, identifier:df2_cols; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:axis; 108, integer:1; 109, comment; 110, comment; 111, comment; 112, if_statement; 112, 113; 112, 114; 112, 141; 113, identifier:suffix_all; 114, block; 114, 115; 114, 127; 114, 139; 114, 140; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:df1_cols; 118, list_comprehension; 118, 119; 118, 124; 119, binary_operator:%; 119, 120; 119, 121; 120, string:"%s%s"; 121, tuple; 121, 122; 121, 123; 122, identifier:c; 123, identifier:df1_suffix; 124, for_in_clause; 124, 125; 124, 126; 125, identifier:c; 126, identifier:df1_cols; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:df2_cols; 130, list_comprehension; 130, 131; 130, 136; 131, binary_operator:%; 131, 132; 131, 133; 132, string:"%s%s"; 133, tuple; 133, 134; 133, 135; 134, identifier:c; 135, identifier:df2_suffix; 136, for_in_clause; 136, 137; 136, 138; 137, identifier:c; 138, identifier:df2_cols; 139, comment; 140, comment; 141, else_clause; 141, 142; 142, block; 142, 143; 142, 155; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:common_cols; 146, list_comprehension; 146, 147; 146, 148; 146, 151; 147, identifier:col; 148, for_in_clause; 148, 149; 148, 150; 149, identifier:col; 150, identifier:df1_cols; 151, if_clause; 151, 152; 152, comparison_operator:in; 152, 153; 152, 154; 153, identifier:col; 154, identifier:df2_cols; 155, for_statement; 155, 156; 155, 157; 155, 158; 156, identifier:col; 157, identifier:common_cols; 158, block; 158, 159; 158, 174; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 169; 161, subscript; 161, 162; 161, 163; 162, identifier:df1_cols; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:df1_cols; 166, identifier:index; 167, argument_list; 167, 168; 168, identifier:col; 169, binary_operator:%; 169, 170; 169, 171; 170, string:"%s%s"; 171, tuple; 171, 172; 171, 173; 172, identifier:col; 173, identifier:df1_suffix; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 184; 176, subscript; 176, 177; 176, 178; 177, identifier:df2_cols; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:df2_cols; 181, identifier:index; 182, argument_list; 182, 183; 183, identifier:col; 184, binary_operator:%; 184, 185; 184, 186; 185, string:"%s%s"; 186, tuple; 186, 187; 186, 188; 187, identifier:col; 188, identifier:df2_suffix; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:combo; 193, identifier:columns; 194, binary_operator:+; 194, 195; 194, 196; 195, identifier:df1_cols; 196, identifier:df2_cols; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 204; 199, attribute; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:combo; 202, identifier:index; 203, identifier:name; 204, identifier:idx_col; 205, return_statement; 205, 206; 206, identifier:combo | def concat_cols(df1,df2,idx_col,df1_cols,df2_cols,
df1_suffix,df2_suffix,wc_cols=[],suffix_all=False):
"""
Concatenates two pandas tables
:param df1: dataframe 1
:param df2: dataframe 2
:param idx_col: column name which will be used as a common index
"""
df1=df1.set_index(idx_col)
df2=df2.set_index(idx_col)
if not len(wc_cols)==0:
for wc in wc_cols:
df1_cols=df1_cols+[c for c in df1.columns if wc in c]
df2_cols=df2_cols+[c for c in df2.columns if wc in c]
combo=pd.concat([df1.loc[:,df1_cols],df2.loc[:,df2_cols]],axis=1)
# find common columns and rename them
# print df1_cols
# print df2_cols
if suffix_all:
df1_cols=["%s%s" % (c,df1_suffix) for c in df1_cols]
df2_cols=["%s%s" % (c,df2_suffix) for c in df2_cols]
# df1_cols[df1_cols.index(col)]="%s%s" % (col,df1_suffix)
# df2_cols[df2_cols.index(col)]="%s%s" % (col,df2_suffix)
else:
common_cols=[col for col in df1_cols if col in df2_cols]
for col in common_cols:
df1_cols[df1_cols.index(col)]="%s%s" % (col,df1_suffix)
df2_cols[df2_cols.index(col)]="%s%s" % (col,df2_suffix)
combo.columns=df1_cols+df2_cols
combo.index.name=idx_col
return combo |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:to_json; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:indent; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort_keys; 10, True; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:json; 18, identifier:dumps; 19, argument_list; 19, 20; 19, 25; 19, 28; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:to_dict; 24, argument_list; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:indent; 27, identifier:indent; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:sort_keys; 30, identifier:sort_keys | def to_json(self, indent=None, sort_keys=True):
"""Return a JSON string representation of this instance
:param indent: specify an indent level or a string used to indent each
level
:param sort_keys: the output is sorted by key
"""
return json.dumps(self.to_dict(), indent=indent, sort_keys=sort_keys) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_validate_method_decoration; 3, parameters; 3, 4; 3, 5; 4, identifier:meta; 5, identifier:class_; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 21; 6, 44; 6, 45; 6, 46; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:super_mro; 14, subscript; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:class_; 17, identifier:__mro__; 18, slice; 18, 19; 18, 20; 19, integer:1; 20, colon; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:own_methods; 24, generator_expression; 24, 25; 24, 28; 24, 39; 25, tuple; 25, 26; 25, 27; 26, identifier:name; 27, identifier:member; 28, for_in_clause; 28, 29; 28, 32; 29, pattern_list; 29, 30; 29, 31; 30, identifier:name; 31, identifier:member; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:class_; 36, identifier:__dict__; 37, identifier:items; 38, argument_list; 39, if_clause; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:is_method; 42, argument_list; 42, 43; 43, identifier:member; 44, comment; 45, comment; 46, for_statement; 46, 47; 46, 50; 46, 51; 47, pattern_list; 47, 48; 47, 49; 48, identifier:name; 49, identifier:method; 50, identifier:own_methods; 51, block; 51, 52; 51, 80; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, pattern_list; 54, 55; 54, 56; 55, identifier:shadowed_method; 56, identifier:base_class; 57, call; 57, 58; 57, 59; 58, identifier:next; 59, argument_list; 59, 60; 59, 77; 60, generator_expression; 60, 61; 60, 68; 60, 71; 61, tuple; 61, 62; 61, 67; 62, call; 62, 63; 62, 64; 63, identifier:getattr; 64, argument_list; 64, 65; 64, 66; 65, identifier:base; 66, identifier:name; 67, identifier:base; 68, for_in_clause; 68, 69; 68, 70; 69, identifier:base; 70, identifier:super_mro; 71, if_clause; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:hasattr; 74, argument_list; 74, 75; 74, 76; 75, identifier:base; 76, identifier:name; 77, tuple; 77, 78; 77, 79; 78, None; 79, None; 80, if_statement; 80, 81; 80, 87; 80, 88; 80, 89; 80, 186; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:meta; 84, identifier:_is_override; 85, argument_list; 85, 86; 86, identifier:method; 87, comment; 88, comment; 89, block; 89, 90; 89, 108; 89, 130; 89, 131; 89, 132; 89, 141; 89, 177; 90, if_statement; 90, 91; 90, 93; 91, not_operator; 91, 92; 92, identifier:shadowed_method; 93, block; 93, 94; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:ClassError; 97, argument_list; 97, 98; 97, 105; 98, binary_operator:%; 98, 99; 98, 100; 99, string:"unnecessary @override on %s.%s"; 100, tuple; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:class_; 103, identifier:__name__; 104, identifier:name; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:class_; 107, identifier:class_; 108, if_statement; 108, 109; 108, 115; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:meta; 112, identifier:_is_final; 113, argument_list; 113, 114; 114, identifier:shadowed_method; 115, block; 115, 116; 116, raise_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:ClassError; 119, argument_list; 119, 120; 119, 127; 120, binary_operator:%; 120, 121; 120, 122; 121, string:"illegal @override on a @final method %s.%s"; 122, tuple; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:base_class; 125, identifier:__name__; 126, identifier:name; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:class_; 129, identifier:class_; 130, comment; 131, comment; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:override_base; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:meta; 138, identifier:_get_override_base; 139, argument_list; 139, 140; 140, identifier:method; 141, if_statement; 141, 142; 141, 147; 142, boolean_operator:and; 142, 143; 142, 144; 143, identifier:override_base; 144, comparison_operator:is; 144, 145; 144, 146; 145, identifier:base_class; 146, identifier:override_base; 147, block; 147, 148; 148, if_statement; 148, 149; 148, 153; 148, 167; 149, call; 149, 150; 149, 151; 150, identifier:is_class; 151, argument_list; 151, 152; 152, identifier:override_base; 153, block; 153, 154; 154, raise_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:ClassError; 157, argument_list; 157, 158; 158, binary_operator:%; 158, 159; 158, 160; 159, string:"incorrect override base: expected %s, got %s"; 160, tuple; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:base_class; 163, identifier:__name__; 164, attribute; 164, 165; 164, 166; 165, identifier:override_base; 166, identifier:__name__; 167, else_clause; 167, 168; 168, block; 168, 169; 169, raise_statement; 169, 170; 170, call; 170, 171; 170, 172; 171, identifier:ClassError; 172, argument_list; 172, 173; 173, binary_operator:%; 173, 174; 173, 175; 174, string:"invalid override base specified: %s"; 175, tuple; 175, 176; 176, identifier:override_base; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:setattr; 180, argument_list; 180, 181; 180, 182; 180, 183; 181, identifier:class_; 182, identifier:name; 183, attribute; 183, 184; 183, 185; 184, identifier:method; 185, identifier:method; 186, else_clause; 186, 187; 187, block; 187, 188; 188, if_statement; 188, 189; 188, 196; 189, boolean_operator:and; 189, 190; 189, 191; 190, identifier:shadowed_method; 191, comparison_operator:not; 191, 192; 191, 193; 192, identifier:name; 193, attribute; 193, 194; 193, 195; 194, identifier:meta; 195, identifier:OVERRIDE_EXEMPTIONS; 196, block; 196, 197; 196, 234; 197, if_statement; 197, 198; 197, 204; 197, 219; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:meta; 201, identifier:_is_final; 202, argument_list; 202, 203; 203, identifier:shadowed_method; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:msg; 208, binary_operator:%; 208, 209; 208, 210; 209, string:"%s.%s is hiding a @final method %s.%s"; 210, tuple; 210, 211; 210, 214; 210, 215; 210, 218; 211, attribute; 211, 212; 211, 213; 212, identifier:class_; 213, identifier:__name__; 214, identifier:name; 215, attribute; 215, 216; 215, 217; 216, identifier:base_class; 217, identifier:__name__; 218, identifier:name; 219, else_clause; 219, 220; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:msg; 224, parenthesized_expression; 224, 225; 225, binary_operator:%; 225, 226; 225, 229; 226, concatenated_string; 226, 227; 226, 228; 227, string:"overridden method %s.%s "; 228, string:"must be marked with @override"; 229, tuple; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:class_; 232, identifier:__name__; 233, identifier:name; 234, raise_statement; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:ClassError; 237, argument_list; 237, 238; 237, 239; 238, identifier:msg; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:class_; 241, identifier:class_ | def _validate_method_decoration(meta, class_):
"""Validate the usage of ``@override`` and ``@final`` modifiers
on methods of the given ``class_``.
"""
# TODO(xion): employ some code inspection tricks to serve ClassErrors
# as if they were thrown at the offending class's/method's definition
super_mro = class_.__mro__[1:]
own_methods = ((name, member)
for name, member in class_.__dict__.items()
if is_method(member))
# check that ``@override`` modifier is present where it should be
# and absent where it shouldn't (e.g. ``@final`` methods)
for name, method in own_methods:
shadowed_method, base_class = next(
((getattr(base, name), base)
for base in super_mro if hasattr(base, name)),
(None, None)
)
if meta._is_override(method):
# ``@override`` is legal only when the method actually shadows
# a method from a superclass, and that metod is not ``@final``
if not shadowed_method:
raise ClassError("unnecessary @override on %s.%s" % (
class_.__name__, name), class_=class_)
if meta._is_final(shadowed_method):
raise ClassError(
"illegal @override on a @final method %s.%s" % (
base_class.__name__, name), class_=class_)
# if @override had parameter supplied, verify if it was
# the same class as the base of shadowed method
override_base = meta._get_override_base(method)
if override_base and base_class is not override_base:
if is_class(override_base):
raise ClassError(
"incorrect override base: expected %s, got %s" % (
base_class.__name__, override_base.__name__))
else:
raise ClassError(
"invalid override base specified: %s" % (
override_base,))
setattr(class_, name, method.method)
else:
if shadowed_method and name not in meta.OVERRIDE_EXEMPTIONS:
if meta._is_final(shadowed_method):
msg = "%s.%s is hiding a @final method %s.%s" % (
class_.__name__, name, base_class.__name__, name)
else:
msg = ("overridden method %s.%s "
"must be marked with @override" % (
class_.__name__, name))
raise ClassError(msg, class_=class_) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_parse_status; 3, parameters; 3, 4; 4, identifier:bug_el; 5, block; 5, 6; 5, 8; 5, 14; 5, 15; 5, 43; 5, 60; 5, 77; 5, 100; 5, 112; 5, 124; 5, 136; 5, 148; 5, 171; 5, 194; 5, 217; 5, 242; 5, 267; 5, 288; 5, 305; 5, 306; 5, 307; 5, 308; 5, 309; 5, 310; 5, 311; 5, 312; 5, 313; 5, 314; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:bug; 11, call; 11, 12; 11, 13; 12, identifier:Bugreport; 13, argument_list; 14, comment; 15, for_statement; 15, 16; 15, 17; 15, 29; 16, identifier:field; 17, tuple; 17, 18; 17, 19; 17, 20; 17, 21; 17, 22; 17, 23; 17, 24; 17, 25; 17, 26; 17, 27; 17, 28; 18, string:'originator'; 19, string:'subject'; 20, string:'msgid'; 21, string:'package'; 22, string:'severity'; 23, string:'owner'; 24, string:'summary'; 25, string:'location'; 26, string:'source'; 27, string:'pending'; 28, string:'forwarded'; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:setattr; 33, argument_list; 33, 34; 33, 35; 33, 36; 34, identifier:bug; 35, identifier:field; 36, call; 36, 37; 36, 38; 37, identifier:_parse_string_el; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:bug_el; 41, argument_list; 41, 42; 42, identifier:field; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:bug; 47, identifier:date; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:datetime; 51, identifier:utcfromtimestamp; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:float; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:bug_el; 58, argument_list; 58, 59; 59, string:'date'; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:bug; 64, identifier:log_modified; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:datetime; 68, identifier:utcfromtimestamp; 69, argument_list; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:float; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:bug_el; 75, argument_list; 75, 76; 76, string:'log_modified'; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:bug; 81, identifier:tags; 82, list_comprehension; 82, 83; 82, 87; 83, call; 83, 84; 83, 85; 84, identifier:_uc; 85, argument_list; 85, 86; 86, identifier:tag; 87, for_in_clause; 87, 88; 87, 89; 88, identifier:tag; 89, call; 89, 90; 89, 99; 90, attribute; 90, 91; 90, 98; 91, call; 91, 92; 91, 93; 92, identifier:str; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:bug_el; 96, argument_list; 96, 97; 97, string:'tags'; 98, identifier:split; 99, argument_list; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:bug; 104, identifier:done; 105, call; 105, 106; 105, 107; 106, identifier:_parse_bool; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:bug_el; 110, argument_list; 110, 111; 111, string:'done'; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:bug; 116, identifier:archived; 117, call; 117, 118; 117, 119; 118, identifier:_parse_bool; 119, argument_list; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:bug_el; 122, argument_list; 122, 123; 123, string:'archived'; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:bug; 128, identifier:unarchived; 129, call; 129, 130; 129, 131; 130, identifier:_parse_bool; 131, argument_list; 131, 132; 132, call; 132, 133; 132, 134; 133, identifier:bug_el; 134, argument_list; 134, 135; 135, string:'unarchived'; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:bug; 140, identifier:bug_num; 141, call; 141, 142; 141, 143; 142, identifier:int; 143, argument_list; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:bug_el; 146, argument_list; 146, 147; 147, string:'bug_num'; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:bug; 152, identifier:mergedwith; 153, list_comprehension; 153, 154; 153, 158; 154, call; 154, 155; 154, 156; 155, identifier:int; 156, argument_list; 156, 157; 157, identifier:i; 158, for_in_clause; 158, 159; 158, 160; 159, identifier:i; 160, call; 160, 161; 160, 170; 161, attribute; 161, 162; 161, 169; 162, call; 162, 163; 162, 164; 163, identifier:str; 164, argument_list; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:bug_el; 167, argument_list; 167, 168; 168, string:'mergedwith'; 169, identifier:split; 170, argument_list; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:bug; 175, identifier:blockedby; 176, list_comprehension; 176, 177; 176, 181; 177, call; 177, 178; 177, 179; 178, identifier:int; 179, argument_list; 179, 180; 180, identifier:i; 181, for_in_clause; 181, 182; 181, 183; 182, identifier:i; 183, call; 183, 184; 183, 193; 184, attribute; 184, 185; 184, 192; 185, call; 185, 186; 185, 187; 186, identifier:str; 187, argument_list; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:bug_el; 190, argument_list; 190, 191; 191, string:'blockedby'; 192, identifier:split; 193, argument_list; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:bug; 198, identifier:blocks; 199, list_comprehension; 199, 200; 199, 204; 200, call; 200, 201; 200, 202; 201, identifier:int; 202, argument_list; 202, 203; 203, identifier:i; 204, for_in_clause; 204, 205; 204, 206; 205, identifier:i; 206, call; 206, 207; 206, 216; 207, attribute; 207, 208; 207, 215; 208, call; 208, 209; 208, 210; 209, identifier:str; 210, argument_list; 210, 211; 211, call; 211, 212; 211, 213; 212, identifier:bug_el; 213, argument_list; 213, 214; 214, string:'blocks'; 215, identifier:split; 216, argument_list; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:bug; 221, identifier:found_versions; 222, list_comprehension; 222, 223; 222, 230; 223, call; 223, 224; 223, 225; 224, identifier:_uc; 225, argument_list; 225, 226; 226, call; 226, 227; 226, 228; 227, identifier:str; 228, argument_list; 228, 229; 229, identifier:el; 230, for_in_clause; 230, 231; 230, 232; 231, identifier:el; 232, boolean_operator:or; 232, 233; 232, 241; 233, call; 233, 234; 233, 240; 234, attribute; 234, 235; 234, 239; 235, call; 235, 236; 235, 237; 236, identifier:bug_el; 237, argument_list; 237, 238; 238, string:'found_versions'; 239, identifier:children; 240, argument_list; 241, list:[]; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:bug; 246, identifier:fixed_versions; 247, list_comprehension; 247, 248; 247, 255; 248, call; 248, 249; 248, 250; 249, identifier:_uc; 250, argument_list; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:str; 253, argument_list; 253, 254; 254, identifier:el; 255, for_in_clause; 255, 256; 255, 257; 256, identifier:el; 257, boolean_operator:or; 257, 258; 257, 266; 258, call; 258, 259; 258, 265; 259, attribute; 259, 260; 259, 264; 260, call; 260, 261; 260, 262; 261, identifier:bug_el; 262, argument_list; 262, 263; 263, string:'fixed_versions'; 264, identifier:children; 265, argument_list; 266, list:[]; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:affects; 270, list_comprehension; 270, 271; 270, 272; 270, 286; 271, identifier:_f; 272, for_in_clause; 272, 273; 272, 274; 273, identifier:_f; 274, call; 274, 275; 274, 284; 275, attribute; 275, 276; 275, 283; 276, call; 276, 277; 276, 278; 277, identifier:str; 278, argument_list; 278, 279; 279, call; 279, 280; 279, 281; 280, identifier:bug_el; 281, argument_list; 281, 282; 282, string:'affects'; 283, identifier:split; 284, argument_list; 284, 285; 285, string:','; 286, if_clause; 286, 287; 287, identifier:_f; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:bug; 292, identifier:affects; 293, list_comprehension; 293, 294; 293, 302; 294, call; 294, 295; 294, 301; 295, attribute; 295, 296; 295, 300; 296, call; 296, 297; 296, 298; 297, identifier:_uc; 298, argument_list; 298, 299; 299, identifier:a; 300, identifier:strip; 301, argument_list; 302, for_in_clause; 302, 303; 302, 304; 303, identifier:a; 304, identifier:affects; 305, comment; 306, comment; 307, comment; 308, comment; 309, comment; 310, comment; 311, comment; 312, comment; 313, comment; 314, return_statement; 314, 315; 315, identifier:bug | def _parse_status(bug_el):
"""Return a bugreport object from a given status xml element"""
bug = Bugreport()
# plain fields
for field in ('originator', 'subject', 'msgid', 'package', 'severity',
'owner', 'summary', 'location', 'source', 'pending',
'forwarded'):
setattr(bug, field, _parse_string_el(bug_el(field)))
bug.date = datetime.utcfromtimestamp(float(bug_el('date')))
bug.log_modified = datetime.utcfromtimestamp(float(bug_el('log_modified')))
bug.tags = [_uc(tag) for tag in str(bug_el('tags')).split()]
bug.done = _parse_bool(bug_el('done'))
bug.archived = _parse_bool(bug_el('archived'))
bug.unarchived = _parse_bool(bug_el('unarchived'))
bug.bug_num = int(bug_el('bug_num'))
bug.mergedwith = [int(i) for i in str(bug_el('mergedwith')).split()]
bug.blockedby = [int(i) for i in str(bug_el('blockedby')).split()]
bug.blocks = [int(i) for i in str(bug_el('blocks')).split()]
bug.found_versions = [_uc(str(el)) for el in
bug_el('found_versions').children() or []]
bug.fixed_versions = [_uc(str(el)) for el in
bug_el('fixed_versions').children() or []]
affects = [_f for _f in str(bug_el('affects')).split(',') if _f]
bug.affects = [_uc(a).strip() for a in affects]
# Also available, but unused or broken
# bug.keywords = [_uc(keyword) for keyword in
# str(bug_el('keywords')).split()]
# bug.fixed = _parse_crappy_soap(tmp, "fixed")
# bug.found = _parse_crappy_soap(tmp, "found")
# bug.found_date = \
# [datetime.utcfromtimestamp(i) for i in tmp["found_date"]]
# bug.fixed_date = \
# [datetime.utcfromtimestamp(i) for i in tmp["fixed_date"]]
return bug |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:set; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:attr; 6, identifier:value; 7, block; 7, 8; 7, 10; 7, 64; 7, 75; 7, 102; 7, 125; 7, 143; 7, 168; 7, 239; 8, expression_statement; 8, 9; 9, comment; 10, function_definition; 10, 11; 10, 12; 10, 17; 11, function_name:escape_new_syntax; 12, parameters; 12, 13; 12, 14; 13, identifier:value; 14, default_parameter; 14, 15; 14, 16; 15, identifier:double_quote_escape; 16, string:'"'; 17, block; 17, 18; 17, 25; 17, 35; 17, 47; 17, 62; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:value; 21, call; 21, 22; 21, 23; 22, identifier:str; 23, argument_list; 23, 24; 24, identifier:value; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:value; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:value; 31, identifier:replace; 32, argument_list; 32, 33; 32, 34; 33, string:"'"; 34, string:"''"; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:value; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:value; 41, identifier:replace; 42, argument_list; 42, 43; 42, 44; 43, string:'"'; 44, binary_operator:%; 44, 45; 44, 46; 45, string:'%s"'; 46, identifier:double_quote_escape; 47, if_statement; 47, 48; 47, 55; 48, boolean_operator:or; 48, 49; 48, 52; 49, comparison_operator:in; 49, 50; 49, 51; 50, string:' '; 51, identifier:value; 52, comparison_operator:in; 52, 53; 52, 54; 53, string:'\t'; 54, identifier:value; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:value; 59, binary_operator:%; 59, 60; 59, 61; 60, string:"'%s'"; 61, identifier:value; 62, return_statement; 62, 63; 63, identifier:value; 64, function_definition; 64, 65; 64, 66; 64, 68; 65, function_name:escape_new_syntax_pre_post_script; 66, parameters; 66, 67; 67, identifier:value; 68, block; 68, 69; 69, return_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:escape_new_syntax; 72, argument_list; 72, 73; 72, 74; 73, identifier:value; 74, string:'\\'; 75, function_definition; 75, 76; 75, 77; 75, 79; 76, function_name:escape_remap; 77, parameters; 77, 78; 78, identifier:value; 79, block; 79, 80; 79, 90; 79, 100; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:value; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:value; 86, identifier:replace; 87, argument_list; 87, 88; 87, 89; 88, string:'='; 89, string:'\='; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:value; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:value; 96, identifier:replace; 97, argument_list; 97, 98; 97, 99; 98, string:';'; 99, string:'\;'; 100, return_statement; 100, 101; 101, identifier:value; 102, function_definition; 102, 103; 102, 104; 102, 107; 103, function_name:join_function_template; 104, parameters; 104, 105; 104, 106; 105, identifier:join_string; 106, identifier:escape_func; 107, block; 107, 108; 108, return_statement; 108, 109; 109, lambda; 109, 110; 109, 112; 110, lambda_parameters; 110, 111; 111, identifier:value; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:join_string; 115, identifier:join; 116, argument_list; 116, 117; 117, list_comprehension; 117, 118; 117, 122; 118, call; 118, 119; 118, 120; 119, identifier:escape_func; 120, argument_list; 120, 121; 121, identifier:i; 122, for_in_clause; 122, 123; 122, 124; 123, identifier:i; 124, identifier:value; 125, function_definition; 125, 126; 125, 127; 125, 130; 126, function_name:quote_join_function_template; 127, parameters; 127, 128; 127, 129; 128, identifier:join_string; 129, identifier:escape_func; 130, block; 130, 131; 131, return_statement; 131, 132; 132, lambda; 132, 133; 132, 135; 133, lambda_parameters; 133, 134; 134, identifier:value; 135, call; 135, 136; 135, 141; 136, call; 136, 137; 136, 138; 137, identifier:join_function_template; 138, argument_list; 138, 139; 138, 140; 139, identifier:join_string; 140, identifier:escape_func; 141, argument_list; 141, 142; 142, identifier:value; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:join_functions; 146, dictionary; 146, 147; 146, 154; 146, 161; 147, pair; 147, 148; 147, 149; 148, string:'rempas'; 149, call; 149, 150; 149, 151; 150, identifier:quote_join_function_template; 151, argument_list; 151, 152; 151, 153; 152, string:'; '; 153, identifier:escape_remap; 154, pair; 154, 155; 154, 156; 155, string:'arguments'; 156, call; 156, 157; 156, 158; 157, identifier:quote_join_function_template; 158, argument_list; 158, 159; 158, 160; 159, string:' '; 160, identifier:escape_new_syntax; 161, pair; 161, 162; 161, 163; 162, string:'Arguments'; 163, call; 163, 164; 163, 165; 164, identifier:quote_join_function_template; 165, argument_list; 165, 166; 165, 167; 166, string:' '; 167, identifier:escape_new_syntax_pre_post_script; 168, if_statement; 168, 169; 168, 172; 168, 177; 168, 186; 169, comparison_operator:is; 169, 170; 169, 171; 170, identifier:value; 171, False; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:value; 176, string:'false'; 177, elif_clause; 177, 178; 177, 181; 178, comparison_operator:is; 178, 179; 178, 180; 179, identifier:value; 180, True; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:value; 185, string:'true'; 186, elif_clause; 186, 187; 186, 198; 187, boolean_operator:or; 187, 188; 187, 193; 188, call; 188, 189; 188, 190; 189, identifier:isinstance; 190, argument_list; 190, 191; 190, 192; 191, identifier:value; 192, identifier:list; 193, call; 193, 194; 193, 195; 194, identifier:isinstance; 195, argument_list; 195, 196; 195, 197; 196, identifier:value; 197, identifier:tuple; 198, block; 198, 199; 198, 207; 198, 232; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:join_function; 202, call; 202, 203; 202, 204; 203, identifier:join_function_template; 204, argument_list; 204, 205; 204, 206; 205, string:', '; 206, identifier:str; 207, for_statement; 207, 208; 207, 209; 207, 217; 208, identifier:key; 209, call; 209, 210; 209, 211; 210, identifier:list; 211, argument_list; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:join_functions; 215, identifier:keys; 216, argument_list; 217, block; 217, 218; 218, if_statement; 218, 219; 218, 225; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:attr; 222, identifier:endswith; 223, argument_list; 223, 224; 224, identifier:key; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:join_function; 229, subscript; 229, 230; 229, 231; 230, identifier:join_functions; 231, identifier:key; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:value; 235, call; 235, 236; 235, 237; 236, identifier:join_function; 237, argument_list; 237, 238; 238, identifier:value; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 246; 241, subscript; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:attributes; 245, identifier:attr; 246, identifier:value | def set(self, attr, value):
"""Set the value of an attribute in the submit description file.
The value can be passed in as a Python type (i.e. a list, a tuple or a Python boolean).
The Python values will be reformatted into strings based on the standards described in
the HTCondor manual: http://research.cs.wisc.edu/htcondor/manual/current/condor_submit.html
Args:
attr (str): The name of the attribute to set.
value (str): The value to assign to 'attr'.
"""
def escape_new_syntax(value, double_quote_escape='"'):
value = str(value)
value = value.replace("'", "''")
value = value.replace('"', '%s"' % double_quote_escape)
if ' ' in value or '\t' in value:
value = "'%s'" % value
return value
def escape_new_syntax_pre_post_script(value):
return escape_new_syntax(value, '\\')
def escape_remap(value):
value = value.replace('=', '\=')
value = value.replace(';', '\;')
return value
def join_function_template(join_string, escape_func):
return lambda value: join_string.join([escape_func(i) for i in value])
def quote_join_function_template(join_string, escape_func):
return lambda value: join_function_template(join_string, escape_func)(value)
join_functions = {'rempas': quote_join_function_template('; ', escape_remap),
'arguments': quote_join_function_template(' ', escape_new_syntax),
'Arguments': quote_join_function_template(' ', escape_new_syntax_pre_post_script)
}
if value is False:
value = 'false'
elif value is True:
value = 'true'
elif isinstance(value, list) or isinstance(value, tuple):
join_function = join_function_template(', ', str)
for key in list(join_functions.keys()):
if attr.endswith(key):
join_function = join_functions[key]
value = join_function(value)
self.attributes[attr] = value |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_update_status; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sub_job_num; 7, None; 8, block; 8, 9; 8, 11; 8, 29; 8, 36; 8, 51; 8, 56; 8, 73; 8, 90; 8, 107; 8, 117; 8, 126; 8, 179; 8, 180; 8, 186; 8, 200; 8, 233; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:job_id; 14, conditional_expression:if; 14, 15; 14, 22; 14, 23; 15, binary_operator:%; 15, 16; 15, 17; 16, string:'%s.%s'; 17, tuple; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:cluster_id; 21, identifier:sub_job_num; 22, identifier:sub_job_num; 23, call; 23, 24; 23, 25; 24, identifier:str; 25, argument_list; 25, 26; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:cluster_id; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:format; 32, list:['-format', '"%d"', 'JobStatus']; 32, 33; 32, 34; 32, 35; 33, string:'-format'; 34, string:'"%d"'; 35, string:'JobStatus'; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:cmd; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, string:'condor_q {0} {1} && condor_history {0} {1}'; 42, identifier:format; 43, argument_list; 43, 44; 43, 45; 44, identifier:job_id; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, string:' '; 48, identifier:join; 49, argument_list; 49, 50; 50, identifier:format; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:args; 54, list:[cmd]; 54, 55; 55, identifier:cmd; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, pattern_list; 58, 59; 58, 60; 59, identifier:out; 60, identifier:err; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:_execute; 65, argument_list; 65, 66; 65, 67; 65, 70; 66, identifier:args; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:shell; 69, True; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:run_in_job_dir; 72, False; 73, if_statement; 73, 74; 73, 75; 74, identifier:err; 75, block; 75, 76; 75, 85; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:log; 80, identifier:error; 81, argument_list; 81, 82; 81, 83; 81, 84; 82, string:'Error while updating status for job %s: %s'; 83, identifier:job_id; 84, identifier:err; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:HTCondorError; 88, argument_list; 88, 89; 89, identifier:err; 90, if_statement; 90, 91; 90, 93; 91, not_operator; 91, 92; 92, identifier:out; 93, block; 93, 94; 93, 102; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:log; 98, identifier:error; 99, argument_list; 99, 100; 99, 101; 100, string:'Error while updating status for job %s: Job not found.'; 101, identifier:job_id; 102, raise_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:HTCondorError; 105, argument_list; 105, 106; 106, string:'Job not found.'; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:out; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:out; 113, identifier:replace; 114, argument_list; 114, 115; 114, 116; 115, string:'\"'; 116, string:''; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:log; 121, identifier:info; 122, argument_list; 122, 123; 122, 124; 122, 125; 123, string:'Job %s status: %s'; 124, identifier:job_id; 125, identifier:out; 126, if_statement; 126, 127; 126, 129; 127, not_operator; 127, 128; 128, identifier:sub_job_num; 129, block; 129, 130; 130, if_statement; 130, 131; 130, 139; 130, 150; 131, comparison_operator:>=; 131, 132; 131, 136; 132, call; 132, 133; 132, 134; 133, identifier:len; 134, argument_list; 134, 135; 135, identifier:out; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:num_jobs; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:out; 143, subscript; 143, 144; 143, 145; 144, identifier:out; 145, slice; 145, 146; 145, 147; 146, colon; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:num_jobs; 150, else_clause; 150, 151; 151, block; 151, 152; 151, 167; 151, 174; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:msg; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, string:'There are {0} sub-jobs, but {1} status(es).'; 158, identifier:format; 159, argument_list; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:num_jobs; 163, call; 163, 164; 163, 165; 164, identifier:len; 165, argument_list; 165, 166; 166, identifier:out; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:log; 171, identifier:error; 172, argument_list; 172, 173; 173, identifier:msg; 174, raise_statement; 174, 175; 175, call; 175, 176; 175, 177; 176, identifier:HTCondorError; 177, argument_list; 177, 178; 178, identifier:msg; 179, comment; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:status_dict; 183, call; 183, 184; 183, 185; 184, identifier:dict; 185, argument_list; 186, for_statement; 186, 187; 186, 188; 186, 193; 187, identifier:val; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:CONDOR_JOB_STATUSES; 191, identifier:values; 192, argument_list; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 199; 196, subscript; 196, 197; 196, 198; 197, identifier:status_dict; 198, identifier:val; 199, integer:0; 200, for_statement; 200, 201; 200, 202; 200, 203; 201, identifier:status_code_str; 202, identifier:out; 203, block; 203, 204; 203, 208; 203, 221; 203, 227; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:status_code; 207, integer:0; 208, try_statement; 208, 209; 208, 217; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:status_code; 213, call; 213, 214; 213, 215; 214, identifier:int; 215, argument_list; 215, 216; 216, identifier:status_code_str; 217, except_clause; 217, 218; 217, 219; 218, identifier:ValueError; 219, block; 219, 220; 220, pass_statement; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:key; 224, subscript; 224, 225; 224, 226; 225, identifier:CONDOR_JOB_STATUSES; 226, identifier:status_code; 227, expression_statement; 227, 228; 228, augmented_assignment:+=; 228, 229; 228, 232; 229, subscript; 229, 230; 229, 231; 230, identifier:status_dict; 231, identifier:key; 232, integer:1; 233, return_statement; 233, 234; 234, identifier:status_dict | def _update_status(self, sub_job_num=None):
"""Gets the job status.
Return:
str: The current status of the job
"""
job_id = '%s.%s' % (self.cluster_id, sub_job_num) if sub_job_num else str(self.cluster_id)
format = ['-format', '"%d"', 'JobStatus']
cmd = 'condor_q {0} {1} && condor_history {0} {1}'.format(job_id, ' '.join(format))
args = [cmd]
out, err = self._execute(args, shell=True, run_in_job_dir=False)
if err:
log.error('Error while updating status for job %s: %s', job_id, err)
raise HTCondorError(err)
if not out:
log.error('Error while updating status for job %s: Job not found.', job_id)
raise HTCondorError('Job not found.')
out = out.replace('\"', '')
log.info('Job %s status: %s', job_id, out)
if not sub_job_num:
if len(out) >= self.num_jobs:
out = out[:self.num_jobs]
else:
msg = 'There are {0} sub-jobs, but {1} status(es).'.format(self.num_jobs, len(out))
log.error(msg)
raise HTCondorError(msg)
#initialize status dictionary
status_dict = dict()
for val in CONDOR_JOB_STATUSES.values():
status_dict[val] = 0
for status_code_str in out:
status_code = 0
try:
status_code = int(status_code_str)
except ValueError:
pass
key = CONDOR_JOB_STATUSES[status_code]
status_dict[key] += 1
return status_dict |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:mangle; 3, parameters; 3, 4; 4, identifier:text; 5, block; 5, 6; 5, 8; 5, 17; 5, 18; 5, 25; 5, 26; 5, 32; 5, 38; 5, 43; 5, 47; 5, 51; 5, 55; 5, 56; 5, 65; 5, 319; 5, 320; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:text_bytes; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:text; 14, identifier:encode; 15, argument_list; 15, 16; 16, string:'utf-8'; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:buff; 21, call; 21, 22; 21, 23; 22, identifier:BytesIO; 23, argument_list; 23, 24; 24, identifier:text_bytes; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:mangled; 29, call; 29, 30; 29, 31; 30, identifier:BytesIO; 31, argument_list; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:last_tok; 35, attribute; 35, 36; 35, 37; 36, identifier:token; 37, identifier:INDENT; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:last_line; 41, unary_operator:-; 41, 42; 42, integer:1; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:last_col; 46, integer:0; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:last_line_text; 50, string:''; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:open_list_dicts; 54, integer:0; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:tokens; 59, call; 59, 60; 59, 61; 60, identifier:tokenizer; 61, argument_list; 61, 62; 62, attribute; 62, 63; 62, 64; 63, identifier:buff; 64, identifier:readline; 65, for_statement; 65, 66; 65, 76; 65, 77; 65, 78; 66, pattern_list; 66, 67; 66, 68; 66, 69; 66, 72; 66, 75; 67, identifier:t; 68, identifier:text; 69, tuple_pattern; 69, 70; 69, 71; 70, identifier:line_s; 71, identifier:col_s; 72, tuple_pattern; 72, 73; 72, 74; 73, identifier:line_e; 74, identifier:col_e; 75, identifier:line; 76, identifier:tokens; 77, comment; 78, block; 78, 79; 78, 117; 78, 118; 78, 126; 78, 127; 78, 139; 78, 151; 78, 152; 78, 180; 78, 181; 78, 182; 78, 183; 78, 184; 78, 302; 78, 303; 78, 307; 78, 311; 78, 315; 79, if_statement; 79, 80; 79, 88; 79, 89; 80, boolean_operator:and; 80, 81; 80, 84; 81, comparison_operator:>; 81, 82; 81, 83; 82, identifier:line_s; 83, identifier:last_line; 84, comparison_operator:!=; 84, 85; 84, 86; 85, identifier:last_line; 86, unary_operator:-; 86, 87; 87, integer:1; 88, comment; 89, block; 89, 90; 89, 94; 89, 95; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:last_col; 93, integer:0; 94, comment; 95, if_statement; 95, 96; 95, 108; 95, 109; 96, comparison_operator:==; 96, 97; 96, 107; 97, subscript; 97, 98; 97, 103; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:last_line_text; 101, identifier:rstrip; 102, argument_list; 103, slice; 103, 104; 103, 106; 104, unary_operator:-; 104, 105; 105, integer:1; 106, colon; 107, string:'\\'; 108, comment; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:mangled; 114, identifier:write; 115, argument_list; 115, 116; 116, string:b' \\\n'; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:striped; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:text; 124, identifier:strip; 125, argument_list; 126, comment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:list_dict_open; 130, list:[token.LSQB, token.LBRACE, '[', '{']; 130, 131; 130, 134; 130, 137; 130, 138; 131, attribute; 131, 132; 131, 133; 132, identifier:token; 133, identifier:LSQB; 134, attribute; 134, 135; 134, 136; 135, identifier:token; 136, identifier:LBRACE; 137, string:'['; 138, string:'{'; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:list_dict_close; 142, list:[token.RSQB, token.RBRACE, ']', '}']; 142, 143; 142, 146; 142, 149; 142, 150; 143, attribute; 143, 144; 143, 145; 144, identifier:token; 145, identifier:RSQB; 146, attribute; 146, 147; 146, 148; 147, identifier:token; 148, identifier:RBRACE; 149, string:']'; 150, string:'}'; 151, comment; 152, if_statement; 152, 153; 152, 160; 152, 161; 152, 166; 153, boolean_operator:or; 153, 154; 153, 157; 154, comparison_operator:in; 154, 155; 154, 156; 155, identifier:t; 156, identifier:list_dict_open; 157, comparison_operator:in; 157, 158; 157, 159; 158, identifier:striped; 159, identifier:list_dict_open; 160, comment; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, augmented_assignment:+=; 163, 164; 163, 165; 164, identifier:open_list_dicts; 165, integer:1; 166, elif_clause; 166, 167; 166, 174; 166, 175; 167, boolean_operator:or; 167, 168; 167, 171; 168, comparison_operator:in; 168, 169; 168, 170; 169, identifier:t; 170, identifier:list_dict_close; 171, comparison_operator:in; 171, 172; 171, 173; 172, identifier:striped; 173, identifier:list_dict_close; 174, comment; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, augmented_assignment:-=; 177, 178; 177, 179; 178, identifier:open_list_dicts; 179, integer:1; 180, comment; 181, comment; 182, comment; 183, comment; 184, if_statement; 184, 185; 184, 227; 184, 228; 184, 229; 184, 250; 184, 251; 184, 260; 185, boolean_operator:and; 185, 186; 185, 191; 186, comparison_operator:==; 186, 187; 186, 188; 187, identifier:t; 188, attribute; 188, 189; 188, 190; 189, identifier:token; 190, identifier:STRING; 191, parenthesized_expression; 191, 192; 192, boolean_operator:or; 192, 193; 192, 198; 193, comparison_operator:==; 193, 194; 193, 195; 194, identifier:last_tok; 195, attribute; 195, 196; 195, 197; 196, identifier:token; 197, identifier:INDENT; 198, parenthesized_expression; 198, 199; 199, boolean_operator:and; 199, 200; 199, 224; 200, parenthesized_expression; 200, 201; 201, boolean_operator:or; 201, 202; 201, 219; 202, boolean_operator:or; 202, 203; 202, 214; 203, boolean_operator:or; 203, 204; 203, 209; 204, comparison_operator:==; 204, 205; 204, 206; 205, identifier:last_tok; 206, attribute; 206, 207; 206, 208; 207, identifier:token; 208, identifier:NEWLINE; 209, comparison_operator:==; 209, 210; 209, 211; 210, identifier:last_tok; 211, attribute; 211, 212; 211, 213; 212, identifier:tokenize; 213, identifier:NL; 214, comparison_operator:==; 214, 215; 214, 216; 215, identifier:last_tok; 216, attribute; 216, 217; 216, 218; 217, identifier:token; 218, identifier:DEDENT; 219, comparison_operator:==; 219, 220; 219, 221; 220, identifier:last_tok; 221, attribute; 221, 222; 221, 223; 222, identifier:tokenize; 223, identifier:ENCODING; 224, comparison_operator:==; 224, 225; 224, 226; 225, identifier:open_list_dicts; 226, integer:0; 227, comment; 228, comment; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:mangled; 234, identifier:write; 235, argument_list; 235, 236; 236, binary_operator:*; 236, 237; 236, 238; 237, string:b'\n'; 238, parenthesized_expression; 238, 239; 239, binary_operator:-; 239, 240; 239, 249; 240, call; 240, 241; 240, 242; 241, identifier:len; 242, argument_list; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:text; 246, identifier:split; 247, argument_list; 247, 248; 248, string:'\n'; 249, integer:1; 250, comment; 251, elif_clause; 251, 252; 251, 257; 251, 258; 252, comparison_operator:==; 252, 253; 252, 254; 253, identifier:t; 254, attribute; 254, 255; 254, 256; 255, identifier:tokenize; 256, identifier:COMMENT; 257, comment; 258, block; 258, 259; 259, pass_statement; 260, else_clause; 260, 261; 260, 262; 261, comment; 262, block; 262, 263; 262, 280; 262, 281; 262, 282; 262, 283; 263, if_statement; 263, 264; 263, 267; 264, comparison_operator:>; 264, 265; 264, 266; 265, identifier:col_s; 266, identifier:last_col; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:mangled; 272, identifier:write; 273, argument_list; 273, 274; 274, binary_operator:*; 274, 275; 274, 276; 275, string:b' '; 276, parenthesized_expression; 276, 277; 277, binary_operator:-; 277, 278; 277, 279; 278, identifier:col_s; 279, identifier:last_col; 280, comment; 281, comment; 282, comment; 283, if_statement; 283, 284; 283, 289; 284, comparison_operator:!=; 284, 285; 284, 286; 285, identifier:t; 286, attribute; 286, 287; 286, 288; 287, identifier:tokenize; 288, identifier:ENCODING; 289, block; 289, 290; 290, expression_statement; 290, 291; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:mangled; 294, identifier:write; 295, argument_list; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:text; 299, identifier:encode; 300, argument_list; 300, 301; 301, string:'utf-8'; 302, comment; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:last_tok; 306, identifier:t; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:last_col; 310, identifier:col_e; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:last_line; 314, identifier:line_e; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:last_line_text; 318, identifier:line; 319, comment; 320, return_statement; 320, 321; 321, call; 321, 322; 321, 329; 322, attribute; 322, 323; 322, 328; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:mangled; 326, identifier:getvalue; 327, argument_list; 328, identifier:decode; 329, argument_list; 329, 330; 330, string:'utf-8' | def mangle(text):
"""
Takes a script and mangles it
TokenError is thrown when encountering bad syntax
"""
text_bytes = text.encode('utf-8')
# Wrap the input script as a byte stream
buff = BytesIO(text_bytes)
# Byte stream for the mangled script
mangled = BytesIO()
last_tok = token.INDENT
last_line = -1
last_col = 0
last_line_text = ''
open_list_dicts = 0
# Build tokens from the script
tokens = tokenizer(buff.readline)
for t, text, (line_s, col_s), (line_e, col_e), line in tokens:
# If this is a new line (except the very first)
if line_s > last_line and last_line != -1:
# Reset the column
last_col = 0
# If the last line ended in a '\' (continuation)
if last_line_text.rstrip()[-1:] == '\\':
# Recreate it
mangled.write(b' \\\n')
# We don't want to be calling the this multiple times
striped = text.strip()
# Tokens or characters for opening or closing a list/dict
list_dict_open = [token.LSQB, token.LBRACE, '[', '{']
list_dict_close = [token.RSQB, token.RBRACE, ']', '}']
# If this is a list or dict
if t in list_dict_open or striped in list_dict_open:
# Increase the dict / list level
open_list_dicts += 1
elif t in list_dict_close or striped in list_dict_close:
# Decrease the dict / list level
open_list_dicts -= 1
# Remove docstrings
# Docstrings are strings not used in an expression,
# unfortunatly it isn't as simple as "t is string and t
# not in expression"
if t == token.STRING and (last_tok == token.INDENT or (
(last_tok == token.NEWLINE or last_tok == tokenize.NL or
last_tok == token.DEDENT or last_tok == tokenize.ENCODING)
and open_list_dicts == 0)):
# Output number of lines corresponding those in
# the docstring comment
mangled.write(b'\n' * (len(text.split('\n')) - 1))
# Or is it a standard comment
elif t == tokenize.COMMENT:
# Plain comment, just don't write it
pass
else:
# Recreate indentation, ideally we should use tabs
if col_s > last_col:
mangled.write(b' ' * (col_s - last_col))
# On Python 3 the first token specifies the encoding
# but we already know it's utf-8 and writing it just
# gives us an invalid script
if t != tokenize.ENCODING:
mangled.write(text.encode('utf-8'))
# Store the previous state
last_tok = t
last_col = col_e
last_line = line_e
last_line_text = line
# Return a string
return mangled.getvalue().decode('utf-8') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 1, 34; 2, function_name:get_array_from_hist2D; 3, parameters; 3, 4; 3, 8; 3, 13; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:hist; 6, type; 6, 7; 7, identifier:Hist; 8, typed_default_parameter; 8, 9; 8, 10; 8, 12; 9, identifier:set_zero_to_NaN; 10, type; 10, 11; 11, identifier:bool; 12, True; 13, typed_default_parameter; 13, 14; 13, 15; 13, 17; 14, identifier:return_bin_edges; 15, type; 15, 16; 16, identifier:bool; 17, False; 18, type; 18, 19; 19, generic_type; 19, 20; 19, 21; 20, identifier:Tuple; 21, type_parameter; 21, 22; 21, 26; 21, 30; 22, type; 22, 23; 23, attribute; 23, 24; 23, 25; 24, identifier:np; 25, identifier:ndarray; 26, type; 26, 27; 27, attribute; 27, 28; 27, 29; 28, identifier:np; 29, identifier:ndarray; 30, type; 30, 31; 31, attribute; 31, 32; 31, 33; 32, identifier:np; 33, identifier:ndarray; 34, block; 34, 35; 34, 37; 34, 38; 34, 39; 34, 40; 34, 62; 34, 63; 34, 64; 34, 106; 34, 107; 34, 116; 34, 117; 34, 118; 34, 119; 34, 120; 34, 126; 34, 127; 34, 128; 34, 129; 34, 130; 34, 131; 34, 144; 34, 322; 34, 334; 35, expression_statement; 35, 36; 36, comment; 37, comment; 38, comment; 39, comment; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:shape; 43, tuple; 43, 44; 43, 53; 44, call; 44, 45; 44, 52; 45, attribute; 45, 46; 45, 51; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:hist; 49, identifier:GetYaxis; 50, argument_list; 51, identifier:GetNbins; 52, argument_list; 53, call; 53, 54; 53, 61; 54, attribute; 54, 55; 54, 60; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:hist; 58, identifier:GetXaxis; 59, argument_list; 60, identifier:GetNbins; 61, argument_list; 62, comment; 63, comment; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:hist_array; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:np; 70, identifier:array; 71, argument_list; 71, 72; 72, list_comprehension; 72, 73; 72, 79; 72, 90; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:hist; 76, identifier:GetBinContent; 77, argument_list; 77, 78; 78, identifier:x; 79, for_in_clause; 79, 80; 79, 81; 80, identifier:x; 81, call; 81, 82; 81, 83; 82, identifier:range; 83, argument_list; 83, 84; 83, 85; 84, integer:1; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:hist; 88, identifier:GetNcells; 89, argument_list; 90, if_clause; 90, 91; 91, boolean_operator:and; 91, 92; 91, 99; 92, not_operator; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:hist; 96, identifier:IsBinUnderflow; 97, argument_list; 97, 98; 98, identifier:x; 99, not_operator; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:hist; 103, identifier:IsBinOverflow; 104, argument_list; 104, 105; 105, identifier:x; 106, comment; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:hist_array; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:hist_array; 113, identifier:reshape; 114, argument_list; 114, 115; 115, identifier:shape; 116, comment; 117, comment; 118, comment; 119, comment; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:hist_array; 123, attribute; 123, 124; 123, 125; 124, identifier:hist_array; 125, identifier:T; 126, comment; 127, comment; 128, comment; 129, comment; 130, comment; 131, if_statement; 131, 132; 131, 133; 132, identifier:set_zero_to_NaN; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 141; 136, subscript; 136, 137; 136, 138; 137, identifier:hist_array; 138, comparison_operator:==; 138, 139; 138, 140; 139, identifier:hist_array; 140, integer:0; 141, attribute; 141, 142; 141, 143; 142, identifier:np; 143, identifier:nan; 144, if_statement; 144, 145; 144, 146; 144, 147; 144, 247; 145, identifier:return_bin_edges; 146, comment; 147, block; 147, 148; 147, 159; 147, 170; 147, 171; 147, 172; 147, 173; 147, 174; 147, 175; 147, 176; 147, 177; 147, 178; 147, 179; 147, 183; 147, 215; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:x_bin_edges; 151, call; 151, 152; 151, 153; 152, identifier:get_bin_edges_from_axis; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:hist; 157, identifier:GetXaxis; 158, argument_list; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:y_bin_edges; 162, call; 162, 163; 162, 164; 163, identifier:get_bin_edges_from_axis; 164, argument_list; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:hist; 168, identifier:GetYaxis; 169, argument_list; 170, comment; 171, comment; 172, comment; 173, comment; 174, comment; 175, comment; 176, comment; 177, comment; 178, comment; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:epsilon; 182, float:1e-9; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:x_range; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:np; 189, identifier:arange; 190, argument_list; 190, 191; 190, 197; 190, 205; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:np; 194, identifier:amin; 195, argument_list; 195, 196; 196, identifier:x_bin_edges; 197, binary_operator:+; 197, 198; 197, 204; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:np; 201, identifier:amax; 202, argument_list; 202, 203; 203, identifier:x_bin_edges; 204, identifier:epsilon; 205, call; 205, 206; 205, 213; 206, attribute; 206, 207; 206, 212; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:hist; 210, identifier:GetXaxis; 211, argument_list; 212, identifier:GetBinWidth; 213, argument_list; 213, 214; 214, integer:1; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:y_range; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:np; 221, identifier:arange; 222, argument_list; 222, 223; 222, 229; 222, 237; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:np; 226, identifier:amin; 227, argument_list; 227, 228; 228, identifier:y_bin_edges; 229, binary_operator:+; 229, 230; 229, 236; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:np; 233, identifier:amax; 234, argument_list; 234, 235; 235, identifier:y_bin_edges; 236, identifier:epsilon; 237, call; 237, 238; 237, 245; 238, attribute; 238, 239; 238, 244; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:hist; 242, identifier:GetYaxis; 243, argument_list; 244, identifier:GetBinWidth; 245, argument_list; 245, 246; 246, integer:1; 247, else_clause; 247, 248; 247, 249; 248, comment; 249, block; 249, 250; 249, 286; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:x_range; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:np; 256, identifier:array; 257, argument_list; 257, 258; 258, list_comprehension; 258, 259; 258, 269; 259, call; 259, 260; 259, 267; 260, attribute; 260, 261; 260, 266; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:hist; 264, identifier:GetXaxis; 265, argument_list; 266, identifier:GetBinCenter; 267, argument_list; 267, 268; 268, identifier:i; 269, for_in_clause; 269, 270; 269, 271; 270, identifier:i; 271, call; 271, 272; 271, 273; 272, identifier:range; 273, argument_list; 273, 274; 273, 275; 274, integer:1; 275, binary_operator:+; 275, 276; 275, 285; 276, call; 276, 277; 276, 284; 277, attribute; 277, 278; 277, 283; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:hist; 281, identifier:GetXaxis; 282, argument_list; 283, identifier:GetNbins; 284, argument_list; 285, integer:1; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:y_range; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:np; 292, identifier:array; 293, argument_list; 293, 294; 294, list_comprehension; 294, 295; 294, 305; 295, call; 295, 296; 295, 303; 296, attribute; 296, 297; 296, 302; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:hist; 300, identifier:GetYaxis; 301, argument_list; 302, identifier:GetBinCenter; 303, argument_list; 303, 304; 304, identifier:i; 305, for_in_clause; 305, 306; 305, 307; 306, identifier:i; 307, call; 307, 308; 307, 309; 308, identifier:range; 309, argument_list; 309, 310; 309, 311; 310, integer:1; 311, binary_operator:+; 311, 312; 311, 321; 312, call; 312, 313; 312, 320; 313, attribute; 313, 314; 313, 319; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:hist; 317, identifier:GetYaxis; 318, argument_list; 319, identifier:GetNbins; 320, argument_list; 321, integer:1; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 327; 324, pattern_list; 324, 325; 324, 326; 325, identifier:X; 326, identifier:Y; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:np; 330, identifier:meshgrid; 331, argument_list; 331, 332; 331, 333; 332, identifier:x_range; 333, identifier:y_range; 334, return_statement; 334, 335; 335, tuple; 335, 336; 335, 337; 335, 338; 336, identifier:X; 337, identifier:Y; 338, identifier:hist_array | def get_array_from_hist2D(hist: Hist, set_zero_to_NaN: bool = True, return_bin_edges: bool = False) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
""" Extract x, y, and bin values from a 2D ROOT histogram.
Converts the histogram into a numpy array, and suitably processes it for a surface plot
by removing 0s (which can cause problems when taking logs), and returning a set of (x, y) mesh
values utilziing either the bin edges or bin centers.
Note:
This is a different format than the 1D version!
Args:
hist (ROOT.TH2): Histogram to be converted.
set_zero_to_NaN: If true, set 0 in the array to NaN. Useful with matplotlib so that it will
ignore the values when plotting. See comments in this function for more details. Default: True.
return_bin_edges: Return x and y using bin edges instead of bin centers.
Returns:
Contains (x values, y values, numpy array of hist data) where (x, y) are values on a
grid (from np.meshgrid) using the selected bin values.
"""
# Process the hist into a suitable state
# NOTE: The shape specific can be somewhat confusing (ie. I would naviely expected to specify the x first.)
# This says that the ``GetYaxis().GetNbins()`` number of rows and ``GetXaxis().GetNbins()`` number of columns.
shape = (hist.GetYaxis().GetNbins(), hist.GetXaxis().GetNbins())
# To keep consistency with the root_numpy 2D hist format, we transpose the final result
# This format has x values as columns.
hist_array = np.array([hist.GetBinContent(x) for x in range(1, hist.GetNcells()) if not hist.IsBinUnderflow(x) and not hist.IsBinOverflow(x)])
# The hist_array was linear, so we need to shape it into our expected 2D values.
hist_array = hist_array.reshape(shape)
# Transpose the array to better match expectations
# In particular, by transposing the array, it means that ``thist_array[1][0]`` gives the 2nd x
# value (x_index = 1) and the 1st y value (y_index = 1). This is as we would expect. This is also
# the same convention as used by root_numpy
hist_array = hist_array.T
# Set all 0s to nan to get similar behavior to ROOT. In ROOT, it will basically ignore 0s. This is
# especially important for log plots. Matplotlib doesn't handle 0s as well, since it attempts to
# plot them and then will throw exceptions when the log is taken.
# By setting to nan, matplotlib basically ignores them similar to ROOT
# NOTE: This requires a few special functions later which ignore nan when calculating min and max.
if set_zero_to_NaN:
hist_array[hist_array == 0] = np.nan
if return_bin_edges:
# Bin edges
x_bin_edges = get_bin_edges_from_axis(hist.GetXaxis())
y_bin_edges = get_bin_edges_from_axis(hist.GetYaxis())
# NOTE: The addition of epsilon to the max is extremely important! Otherwise, the x and y
# ranges will be one bin short since ``arange`` is not inclusive. This could also be resolved
# by using ``linspace``, but I think this approach is perfectly fine.
# NOTE: This epsilon is smaller than the one in ``utils`` because we are sometimes dealing
# with small times (~ns). The other value is larger because (I seem to recall) that
# smaller values didn't always place nice with ROOT, but it is fine here, since we're
# working with numpy.
# NOTE: This should be identical to taking the min and max of the axis using
# ``TAxis.GetXmin()`` and ``TAxis.GetXmax()``, but I prefer this approach.
epsilon = 1e-9
x_range = np.arange(
np.amin(x_bin_edges),
np.amax(x_bin_edges) + epsilon,
hist.GetXaxis().GetBinWidth(1)
)
y_range = np.arange(
np.amin(y_bin_edges),
np.amax(y_bin_edges) + epsilon,
hist.GetYaxis().GetBinWidth(1)
)
else:
# We want an array of bin centers
x_range = np.array([hist.GetXaxis().GetBinCenter(i) for i in range(1, hist.GetXaxis().GetNbins() + 1)])
y_range = np.array([hist.GetYaxis().GetBinCenter(i) for i in range(1, hist.GetYaxis().GetNbins() + 1)])
X, Y = np.meshgrid(x_range, y_range)
return (X, Y, hist_array) |
Subsets and Splits