sequence
stringlengths 546
16.2k
| code
stringlengths 108
19.3k
|
---|---|
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:qteReplaceAppletInLayout; 3, parameters; 3, 4; 3, 5; 3, 11; 3, 18; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:newApplet; 7, type; 7, 8; 8, tuple; 8, 9; 8, 10; 9, identifier:QtmacsApplet; 10, identifier:str; 11, typed_default_parameter; 11, 12; 11, 13; 11, 17; 12, identifier:oldApplet; 13, type; 13, 14; 14, tuple; 14, 15; 14, 16; 15, identifier:QtmacsApplet; 16, identifier:str; 17, None; 18, typed_default_parameter; 18, 19; 18, 20; 18, 22; 19, identifier:windowObj; 20, type; 20, 21; 21, identifier:QtmacsWindow; 22, None; 23, block; 23, 24; 23, 26; 23, 27; 23, 28; 23, 29; 23, 30; 23, 52; 23, 53; 23, 54; 23, 55; 23, 56; 23, 78; 23, 79; 23, 114; 23, 115; 23, 136; 23, 137; 23, 138; 23, 139; 23, 140; 23, 141; 23, 160; 23, 161; 23, 183; 23, 184; 23, 185; 23, 191; 23, 192; 23, 193; 23, 207; 23, 208; 23, 220; 23, 251; 23, 252; 23, 261; 23, 262; 23, 263; 23, 264; 23, 265; 23, 273; 23, 281; 23, 288; 24, expression_statement; 24, 25; 25, comment; 26, comment; 27, comment; 28, comment; 29, comment; 30, if_statement; 30, 31; 30, 36; 30, 46; 31, call; 31, 32; 31, 33; 32, identifier:isinstance; 33, argument_list; 33, 34; 33, 35; 34, identifier:oldApplet; 35, identifier:str; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:oldAppObj; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:qteGetAppletHandle; 44, argument_list; 44, 45; 45, identifier:oldApplet; 46, else_clause; 46, 47; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:oldAppObj; 51, identifier:oldApplet; 52, comment; 53, comment; 54, comment; 55, comment; 56, if_statement; 56, 57; 56, 62; 56, 72; 57, call; 57, 58; 57, 59; 58, identifier:isinstance; 59, argument_list; 59, 60; 59, 61; 60, identifier:newApplet; 61, identifier:str; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:newAppObj; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:qteGetAppletHandle; 70, argument_list; 70, 71; 71, identifier:newApplet; 72, else_clause; 72, 73; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:newAppObj; 77, identifier:newApplet; 78, comment; 79, if_statement; 79, 80; 79, 83; 80, comparison_operator:is; 80, 81; 80, 82; 81, identifier:windowObj; 82, None; 83, block; 83, 84; 83, 92; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:windowObj; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:qteActiveWindow; 91, argument_list; 92, if_statement; 92, 93; 92, 96; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:windowObj; 95, None; 96, block; 96, 97; 96, 101; 96, 113; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:msg; 100, string:'Cannot determine the currently active window.'; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 108; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:qteLogger; 107, identifier:warning; 108, argument_list; 108, 109; 108, 110; 109, identifier:msg; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:stack_info; 112, True; 113, return_statement; 114, comment; 115, if_statement; 115, 116; 115, 125; 116, comparison_operator:==; 116, 117; 116, 124; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:windowObj; 121, identifier:qteAppletSplitter; 122, identifier:count; 123, argument_list; 124, integer:0; 125, block; 125, 126; 125, 135; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:windowObj; 131, identifier:qteAppletSplitter; 132, identifier:qteAddWidget; 133, argument_list; 133, 134; 134, identifier:newAppObj; 135, return_statement; 136, comment; 137, comment; 138, comment; 139, comment; 140, comment; 141, if_statement; 141, 142; 141, 145; 142, comparison_operator:is; 142, 143; 142, 144; 143, identifier:oldAppObj; 144, None; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:oldAppObj; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:qteNextApplet; 153, argument_list; 153, 154; 153, 157; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:numSkip; 156, integer:0; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:windowObj; 159, identifier:windowObj; 160, comment; 161, if_statement; 161, 162; 161, 165; 162, comparison_operator:is; 162, 163; 162, 164; 163, identifier:oldAppObj; 164, None; 165, block; 165, 166; 165, 170; 165, 182; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:msg; 169, string:'Applet to replace does not exist.'; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:qteLogger; 176, identifier:error; 177, argument_list; 177, 178; 177, 179; 178, identifier:msg; 179, keyword_argument; 179, 180; 179, 181; 180, identifier:stack_info; 181, True; 182, return_statement; 183, comment; 184, comment; 185, if_statement; 185, 186; 185, 189; 186, comparison_operator:is; 186, 187; 186, 188; 187, identifier:newAppObj; 188, identifier:oldAppObj; 189, block; 189, 190; 190, return_statement; 191, comment; 192, comment; 193, if_statement; 193, 194; 193, 205; 194, boolean_operator:and; 194, 195; 194, 200; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:oldAppObj; 198, identifier:qteIsVisible; 199, argument_list; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:newAppObj; 203, identifier:qteIsVisible; 204, argument_list; 205, block; 205, 206; 206, return_statement; 207, comment; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:split; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:_qteFindAppletInSplitter; 215, argument_list; 215, 216; 215, 217; 216, identifier:oldAppObj; 217, attribute; 217, 218; 217, 219; 218, identifier:windowObj; 219, identifier:qteAppletSplitter; 220, if_statement; 220, 221; 220, 224; 221, comparison_operator:is; 221, 222; 221, 223; 222, identifier:split; 223, None; 224, block; 224, 225; 224, 241; 224, 250; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:msg; 228, parenthesized_expression; 228, 229; 229, call; 229, 230; 229, 235; 230, attribute; 230, 231; 230, 234; 231, concatenated_string; 231, 232; 231, 233; 232, string:'Applet <b>{}</b> not replaced because it is not'; 233, string:'in the layout.'; 234, identifier:format; 235, argument_list; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:oldAppObj; 239, identifier:qteAppletID; 240, argument_list; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 248; 243, attribute; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:self; 246, identifier:qteLogger; 247, identifier:warning; 248, argument_list; 248, 249; 249, identifier:msg; 250, return_statement; 251, comment; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:oldAppIdx; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:split; 258, identifier:indexOf; 259, argument_list; 259, 260; 260, identifier:oldAppObj; 261, comment; 262, comment; 263, comment; 264, comment; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:sizes; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:split; 271, identifier:sizes; 272, argument_list; 273, expression_statement; 273, 274; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:split; 277, identifier:qteInsertWidget; 278, argument_list; 278, 279; 278, 280; 279, identifier:oldAppIdx; 280, identifier:newAppObj; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:oldAppObj; 285, identifier:hide; 286, argument_list; 286, 287; 287, True; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:split; 292, identifier:setSizes; 293, argument_list; 293, 294; 294, identifier:sizes | def qteReplaceAppletInLayout(self, newApplet: (QtmacsApplet, str),
oldApplet: (QtmacsApplet, str)=None,
windowObj: QtmacsWindow=None):
"""
Replace ``oldApplet`` with ``newApplet`` in the window layout.
If ``oldApplet`` is **None** then the currently active applet
will be replaced. If ``windowObj`` is **None** then the
currently active window is used.
The ``oldApplet`` and ``newApplet`` parameters can either be
instances of ``QtmacsApplet`` or strings denoting the applet
IDs. In the latter case the ``qteGetAppletHandle`` method is
used to fetch the respective applet instances.
|Args|
* ``newApplet`` (**QtmacsApplet**, **str**): applet to add.
* ``oldApplet`` (**QtmacsApplet**, **str**): applet to replace.
* ``windowObj`` (**QtmacsWindow**): the window in which to operate.
|Returns|
* **None**
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# If ``oldAppObj`` was specified by its ID (ie. a string) then
# fetch the associated ``QtmacsApplet`` instance. If
# ``oldAppObj`` is already an instance of ``QtmacsApplet``
# then use it directly.
if isinstance(oldApplet, str):
oldAppObj = self.qteGetAppletHandle(oldApplet)
else:
oldAppObj = oldApplet
# If ``newAppObj`` was specified by its ID (ie. a string) then
# fetch the associated ``QtmacsApplet`` instance. If
# ``newAppObj`` is already an instance of ``QtmacsApplet``
# then use it directly.
if isinstance(newApplet, str):
newAppObj = self.qteGetAppletHandle(newApplet)
else:
newAppObj = newApplet
# Use the currently active window if none was specified.
if windowObj is None:
windowObj = self.qteActiveWindow()
if windowObj is None:
msg = 'Cannot determine the currently active window.'
self.qteLogger.warning(msg, stack_info=True)
return
# If the main splitter contains no applet then just add newAppObj.
if windowObj.qteAppletSplitter.count() == 0:
windowObj.qteAppletSplitter.qteAddWidget(newAppObj)
return
# If no oldAppObj was specified use the currently active one
# instead. Do not use qteActiveApplet to determine it, though,
# because it may point to a mini buffer. If it is, then we
# need the last active Qtmacs applet. In either case, the
# qteNextApplet method will take care of these distinctions.
if oldAppObj is None:
oldAppObj = self.qteNextApplet(numSkip=0, windowObj=windowObj)
# Sanity check: the applet to replace must exist.
if oldAppObj is None:
msg = 'Applet to replace does not exist.'
self.qteLogger.error(msg, stack_info=True)
return
# Sanity check: do nothing if the old- and new applet are the
# same.
if newAppObj is oldAppObj:
return
# Sanity check: do nothing if both applets are already
# visible.
if oldAppObj.qteIsVisible() and newAppObj.qteIsVisible():
return
# Search for the splitter that contains 'oldAppObj'.
split = self._qteFindAppletInSplitter(
oldAppObj, windowObj.qteAppletSplitter)
if split is None:
msg = ('Applet <b>{}</b> not replaced because it is not'
'in the layout.'.format(oldAppObj.qteAppletID()))
self.qteLogger.warning(msg)
return
# Determine the position of oldAppObj inside the splitter.
oldAppIdx = split.indexOf(oldAppObj)
# Replace oldAppObj with newAppObj but maintain the widget sizes. To do
# so, first insert newAppObj into the splitter at the position of
# oldAppObj. Afterwards, remove oldAppObj by re-parenting it, make
# it invisible, and restore the widget sizes.
sizes = split.sizes()
split.qteInsertWidget(oldAppIdx, newAppObj)
oldAppObj.hide(True)
split.setSizes(sizes) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:qteRemoveAppletFromLayout; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:applet; 7, type; 7, 8; 8, tuple; 8, 9; 8, 10; 9, identifier:QtmacsApplet; 10, identifier:str; 11, block; 11, 12; 11, 14; 11, 15; 11, 16; 11, 17; 11, 18; 11, 40; 11, 41; 11, 65; 11, 71; 11, 72; 11, 73; 11, 74; 11, 75; 11, 76; 11, 156; 11, 157; 11, 158; 11, 159; 11, 160; 11, 161; 11, 162; 11, 171; 11, 172; 11, 179; 11, 180; 11, 181; 11, 215; 11, 216; 11, 217; 11, 226; 11, 227; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, comment; 16, comment; 17, comment; 18, if_statement; 18, 19; 18, 24; 18, 34; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:applet; 23, identifier:str; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:appletObj; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:qteGetAppletHandle; 32, argument_list; 32, 33; 33, identifier:applet; 34, else_clause; 34, 35; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:appletObj; 39, identifier:applet; 40, comment; 41, for_statement; 41, 42; 41, 43; 41, 46; 42, identifier:window; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:_qteWindowList; 46, block; 46, 47; 46, 59; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:split; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:_qteFindAppletInSplitter; 54, argument_list; 54, 55; 54, 56; 55, identifier:appletObj; 56, attribute; 56, 57; 56, 58; 57, identifier:window; 58, identifier:qteAppletSplitter; 59, if_statement; 59, 60; 59, 63; 60, comparison_operator:is; 60, 61; 60, 62; 61, identifier:split; 62, None; 63, block; 63, 64; 64, break_statement; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:split; 68, None; 69, block; 69, 70; 70, return_statement; 71, comment; 72, comment; 73, comment; 74, comment; 75, comment; 76, if_statement; 76, 77; 76, 92; 76, 93; 76, 94; 77, boolean_operator:and; 77, 78; 77, 84; 78, parenthesized_expression; 78, 79; 79, comparison_operator:is; 79, 80; 79, 81; 80, identifier:split; 81, attribute; 81, 82; 81, 83; 82, identifier:window; 83, identifier:qteAppletSplitter; 84, parenthesized_expression; 84, 85; 85, comparison_operator:==; 85, 86; 85, 91; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:split; 89, identifier:count; 90, argument_list; 91, integer:1; 92, comment; 93, comment; 94, block; 94, 95; 94, 107; 94, 108; 94, 109; 94, 110; 94, 111; 94, 122; 94, 147; 94, 148; 94, 155; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 105; 97, attribute; 97, 98; 97, 104; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:split; 101, identifier:widget; 102, argument_list; 102, 103; 103, integer:0; 104, identifier:hide; 105, argument_list; 105, 106; 106, True; 107, comment; 108, comment; 109, comment; 110, comment; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:nextApp; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:qteNextApplet; 118, argument_list; 118, 119; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:windowObj; 121, identifier:window; 122, if_statement; 122, 123; 122, 126; 123, comparison_operator:is; 123, 124; 123, 125; 124, identifier:nextApp; 125, None; 126, block; 126, 127; 126, 141; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:nextApp; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:qteNextApplet; 134, argument_list; 134, 135; 134, 138; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:skipInvisible; 137, False; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:skipVisible; 140, True; 141, if_statement; 141, 142; 141, 145; 142, comparison_operator:is; 142, 143; 142, 144; 143, identifier:nextApp; 144, None; 145, block; 145, 146; 146, return_statement; 147, comment; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:split; 152, identifier:qteAddWidget; 153, argument_list; 153, 154; 154, identifier:nextApp; 155, return_statement; 156, comment; 157, comment; 158, comment; 159, comment; 160, comment; 161, comment; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:appletIdx; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:split; 168, identifier:indexOf; 169, argument_list; 169, 170; 170, identifier:appletObj; 171, comment; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:appletObj; 176, identifier:hide; 177, argument_list; 177, 178; 178, True; 179, comment; 180, comment; 181, if_statement; 181, 182; 181, 189; 182, comparison_operator:!=; 182, 183; 182, 188; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:split; 186, identifier:count; 187, argument_list; 188, integer:1; 189, block; 189, 190; 189, 206; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:msg; 193, parenthesized_expression; 193, 194; 194, call; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, concatenated_string; 196, 197; 196, 198; 197, string:'Splitter has <b>{}</b> elements left instead of'; 198, string:' exactly one.'; 199, identifier:format; 200, argument_list; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:split; 204, identifier:count; 205, argument_list; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 213; 208, attribute; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:qteLogger; 212, identifier:warning; 213, argument_list; 213, 214; 214, identifier:msg; 215, comment; 216, comment; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:otherWidget; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:split; 223, identifier:widget; 224, argument_list; 224, 225; 225, integer:0; 226, comment; 227, if_statement; 227, 228; 227, 235; 227, 236; 227, 237; 227, 238; 227, 301; 228, comparison_operator:==; 228, 229; 228, 234; 229, attribute; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:otherWidget; 232, identifier:_qteAdmin; 233, identifier:widgetSignature; 234, string:'__QtmacsLayoutSplitter__'; 235, comment; 236, comment; 237, comment; 238, block; 238, 239; 238, 287; 238, 288; 238, 295; 239, for_statement; 239, 240; 239, 241; 239, 249; 239, 250; 239, 251; 239, 252; 239, 253; 240, identifier:ii; 241, call; 241, 242; 241, 243; 242, identifier:range; 243, argument_list; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:otherWidget; 247, identifier:count; 248, argument_list; 249, comment; 250, comment; 251, comment; 252, comment; 253, block; 253, 254; 253, 263; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:obj; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:otherWidget; 260, identifier:widget; 261, argument_list; 261, 262; 262, integer:0; 263, if_statement; 263, 264; 263, 267; 263, 275; 264, comparison_operator:==; 264, 265; 264, 266; 265, identifier:appletIdx; 266, integer:0; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:split; 272, identifier:qteAddWidget; 273, argument_list; 273, 274; 274, identifier:obj; 275, else_clause; 275, 276; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:split; 281, identifier:qteInsertWidget; 282, argument_list; 282, 283; 282, 286; 283, binary_operator:+; 283, 284; 283, 285; 284, integer:1; 285, identifier:ii; 286, identifier:obj; 287, comment; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:otherWidget; 292, identifier:setParent; 293, argument_list; 293, 294; 294, None; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:otherWidget; 299, identifier:close; 300, argument_list; 301, else_clause; 301, 302; 301, 303; 301, 304; 301, 305; 301, 306; 302, comment; 303, comment; 304, comment; 305, comment; 306, block; 306, 307; 307, if_statement; 307, 308; 307, 313; 308, comparison_operator:is; 308, 309; 308, 310; 309, identifier:split; 310, attribute; 310, 311; 310, 312; 311, identifier:window; 312, identifier:qteAppletSplitter; 313, block; 313, 314; 313, 325; 313, 332; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:otherWidget; 318, identifier:qteReparent; 319, argument_list; 319, 320; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:split; 323, identifier:parent; 324, argument_list; 325, expression_statement; 325, 326; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:split; 329, identifier:setParent; 330, argument_list; 330, 331; 331, None; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:split; 336, identifier:close; 337, argument_list | def qteRemoveAppletFromLayout(self, applet: (QtmacsApplet, str)):
"""
Remove ``applet`` from the window layout.
This method removes ``applet`` and implicitly deletes
obsolete (ie. half-full) splitters in the process. If
``applet`` is the only visible applet in the layout then it
will be replaced with the first invisible applet. If no
invisible applets are left then the method does nothing.
The ``applet`` parameter can either be an instance of
``QtmacsApplet`` or a string denoting an applet ID. In the
latter case the ``qteGetAppletHandle`` method is used to fetch
the respective applet instance.
If ``applet`` does not refer to an existing applet then
nothing happens.
|Args|
* ``applet`` (**QtmacsApplet**, **str**): the applet to remove
from the layout.
|Returns|
* **None**
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# If ``applet`` was specified by its ID (ie. a string) then
# fetch the associated ``QtmacsApplet`` instance. If
# ``applet`` is already an instance of ``QtmacsApplet`` then
# use it directly.
if isinstance(applet, str):
appletObj = self.qteGetAppletHandle(applet)
else:
appletObj = applet
# Return immediately if the applet does not exist in any splitter.
for window in self._qteWindowList:
split = self._qteFindAppletInSplitter(
appletObj, window.qteAppletSplitter)
if split is not None:
break
if split is None:
return
# If the applet lives in the main splitter and is the only
# widget there it must be replaced with another applet. This
# case needs to be handled separately from the other options
# because every other splitter will always contain exactly two
# items (ie. two applets, two splitters, or one of each).
if (split is window.qteAppletSplitter) and (split.count() == 1):
# Remove the existing applet object from the splitter and
# hide it.
split.widget(0).hide(True)
# Get the next available applet to focus on. Try to find a
# visible applet in the current window, and if none exists
# then pick the first invisible one. If there is neither
# a visible nor an invisible applet left then do nothing.
nextApp = self.qteNextApplet(windowObj=window)
if nextApp is None:
nextApp = self.qteNextApplet(skipInvisible=False,
skipVisible=True)
if nextApp is None:
return
# Ok, we found an applet to show.
split.qteAddWidget(nextApp)
return
# ------------------------------------------------------------
# If we got until here we know that the splitter (root or not)
# contains (at least) two elements. Note: if it contains more
# than two elements then there is a bug somewhere.
# ------------------------------------------------------------
# Find the index of the object inside the splitter.
appletIdx = split.indexOf(appletObj)
# Detach the applet from the splitter and make it invisible.
appletObj.hide(True)
# Verify that really only one additional element is left in
# the splitter. If not, then something is wrong.
if split.count() != 1:
msg = ('Splitter has <b>{}</b> elements left instead of'
' exactly one.'.format(split.count()))
self.qteLogger.warning(msg)
# Get a reference to the other widget in the splitter (either
# a QtmacsSplitter or a QtmacsApplet).
otherWidget = split.widget(0)
# Is the other widget another splitter?
if otherWidget._qteAdmin.widgetSignature == '__QtmacsLayoutSplitter__':
# Yes, ``otherWidget`` is a QtmacsSplitter object,
# therefore shift all its widgets over to the current
# splitter.
for ii in range(otherWidget.count()):
# Get the next widget from that splitter. Note that we
# always pick the widget at the 0'th position because
# the splitter will re-index the remaining widgets
# after each removal.
obj = otherWidget.widget(0)
if appletIdx == 0:
split.qteAddWidget(obj)
else:
split.qteInsertWidget(1 + ii, obj)
# Delete the child splitter.
otherWidget.setParent(None)
otherWidget.close()
else:
# No, ``otherWidget`` is a QtmacsApplet, therefore move it
# to the parent splitter and delete the current one,
# unless 'split' is the root splitter in which case
# nothing happens.
if split is not window.qteAppletSplitter:
otherWidget.qteReparent(split.parent())
split.setParent(None)
split.close() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:qteKillApplet; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:appletID; 7, type; 7, 8; 8, identifier:str; 9, block; 9, 10; 9, 12; 9, 13; 9, 27; 9, 55; 9, 56; 9, 71; 9, 72; 9, 78; 9, 79; 9, 87; 9, 88; 9, 89; 9, 107; 9, 108; 9, 109; 9, 135; 9, 136; 9, 137; 9, 138; 9, 151; 9, 152; 9, 166; 9, 175; 9, 176; 9, 177; 9, 178; 9, 179; 9, 180; 9, 181; 9, 187; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:ID_list; 16, list_comprehension; 16, 17; 16, 22; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:_; 20, identifier:qteAppletID; 21, argument_list; 22, for_in_clause; 22, 23; 22, 24; 23, identifier:_; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:_qteAppletList; 27, if_statement; 27, 28; 27, 31; 27, 32; 27, 34; 28, comparison_operator:not; 28, 29; 28, 30; 29, identifier:appletID; 30, identifier:ID_list; 31, comment; 32, block; 32, 33; 33, return_statement; 34, else_clause; 34, 35; 34, 36; 34, 37; 35, comment; 36, comment; 37, block; 37, 38; 37, 47; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:idx; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:ID_list; 44, identifier:index; 45, argument_list; 45, 46; 46, identifier:appletID; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:appObj; 50, subscript; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:_qteAppletList; 54, identifier:idx; 55, comment; 56, if_statement; 56, 57; 56, 63; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:qteIsMiniApplet; 61, argument_list; 61, 62; 62, identifier:appObj; 63, block; 63, 64; 63, 70; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:qteKillMiniApplet; 69, argument_list; 70, return_statement; 71, comment; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:appObj; 76, identifier:qteToBeKilled; 77, argument_list; 78, comment; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:window; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:appObj; 85, identifier:qteParentWindow; 86, argument_list; 87, comment; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:newApplet; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:qteNextApplet; 96, argument_list; 96, 97; 96, 101; 96, 104; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:numSkip; 99, unary_operator:-; 99, 100; 100, integer:1; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:skipInvisible; 103, False; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:skipVisible; 106, True; 107, comment; 108, comment; 109, if_statement; 109, 110; 109, 119; 109, 124; 110, boolean_operator:or; 110, 111; 110, 115; 111, parenthesized_expression; 111, 112; 112, comparison_operator:is; 112, 113; 112, 114; 113, identifier:newApplet; 114, None; 115, parenthesized_expression; 115, 116; 116, comparison_operator:is; 116, 117; 116, 118; 117, identifier:newApplet; 118, identifier:appObj; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:newApplet; 123, None; 124, else_clause; 124, 125; 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:qteReplaceAppletInLayout; 131, argument_list; 131, 132; 131, 133; 131, 134; 132, identifier:newApplet; 133, identifier:appObj; 134, identifier:window; 135, comment; 136, comment; 137, comment; 138, if_statement; 138, 139; 138, 144; 139, comparison_operator:is; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:_qteActiveApplet; 143, identifier:appObj; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:_qteActiveApplet; 150, identifier:newApplet; 151, comment; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:qteLogger; 158, identifier:debug; 159, argument_list; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, string:'Kill applet: <b>{}</b>'; 163, identifier:format; 164, argument_list; 164, 165; 165, identifier:appletID; 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:_qteAppletList; 172, identifier:remove; 173, argument_list; 173, 174; 174, identifier:appObj; 175, comment; 176, comment; 177, comment; 178, comment; 179, comment; 180, comment; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:appObj; 185, identifier:close; 186, argument_list; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:sip; 191, identifier:delete; 192, argument_list; 192, 193; 193, identifier:appObj | def qteKillApplet(self, appletID: str):
"""
Destroy the applet with ID ``appletID``.
This method removes ``appletID`` from Qtmacs permanently - no
questions asked. It is the responsibility of the (macro)
programmer to use it responsibly.
If the applet was visible then the method also takes care of
replacing with the next invisible applet, if one is available.
If ``appletID`` does not refer to a valid applet then nothing
happens.
|Args|
* ``appletID`` (**str**): name of applet to be destroyed.
|Returns|
* **None**
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# Compile list of all applet IDs.
ID_list = [_.qteAppletID() for _ in self._qteAppletList]
if appletID not in ID_list:
# Do nothing if the applet does not exist.
return
else:
# Get a reference to the actual applet object based on the
# name.
idx = ID_list.index(appletID)
appObj = self._qteAppletList[idx]
# Mini applets are killed with a special method.
if self.qteIsMiniApplet(appObj):
self.qteKillMiniApplet()
return
# Inform the applet that it is about to be killed.
appObj.qteToBeKilled()
# Determine the window of the applet.
window = appObj.qteParentWindow()
# Get the previous invisible applet (*may* come in handy a few
# lines below).
newApplet = self.qteNextApplet(numSkip=-1, skipInvisible=False,
skipVisible=True)
# If there is no invisible applet available, or the only available
# applet is the one to be killed, then set newApplet to None.
if (newApplet is None) or (newApplet is appObj):
newApplet = None
else:
self.qteReplaceAppletInLayout(newApplet, appObj, window)
# Ensure that _qteActiveApplet does not point to the applet
# to be killed as it will otherwise result in a dangling
# pointer.
if self._qteActiveApplet is appObj:
self._qteActiveApplet = newApplet
# Remove the applet object from the applet list.
self.qteLogger.debug('Kill applet: <b>{}</b>'.format(appletID))
self._qteAppletList.remove(appObj)
# Close the applet and schedule it for destruction. Explicitly
# call the sip.delete() method to ensure that all signals are
# *immediately* disconnected, as otherwise there is a good
# chance that Qtmacs segfaults if Python/Qt thinks the slots
# are still connected when really the object does not exist
# anymore.
appObj.close()
sip.delete(appObj) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:qteRunHook; 3, parameters; 3, 4; 3, 5; 3, 9; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:hookName; 7, type; 7, 8; 8, identifier:str; 9, typed_default_parameter; 9, 10; 9, 11; 9, 13; 10, identifier:msgObj; 11, type; 11, 12; 12, identifier:QtmacsMessage; 13, None; 14, block; 14, 15; 14, 17; 14, 18; 14, 24; 14, 25; 14, 31; 14, 32; 14, 43; 14, 44; 14, 51; 14, 52; 14, 53; 14, 54; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:reg; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_qteRegistryHooks; 24, comment; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:not; 26, 27; 26, 28; 27, identifier:hookName; 28, identifier:reg; 29, block; 29, 30; 30, return_statement; 31, comment; 32, if_statement; 32, 33; 32, 36; 33, comparison_operator:is; 33, 34; 33, 35; 34, identifier:msgObj; 35, None; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:msgObj; 40, call; 40, 41; 40, 42; 41, identifier:QtmacsMessage; 42, argument_list; 43, comment; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:msgObj; 48, identifier:setHookName; 49, argument_list; 49, 50; 50, identifier:hookName; 51, comment; 52, comment; 53, comment; 54, for_statement; 54, 55; 54, 56; 54, 59; 55, identifier:fun; 56, subscript; 56, 57; 56, 58; 57, identifier:reg; 58, identifier:hookName; 59, block; 59, 60; 60, try_statement; 60, 61; 60, 67; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:fun; 65, argument_list; 65, 66; 66, identifier:msgObj; 67, except_clause; 67, 68; 67, 72; 67, 73; 68, as_pattern; 68, 69; 68, 70; 69, identifier:Exception; 70, as_pattern_target; 70, 71; 71, identifier:err; 72, comment; 73, block; 73, 74; 73, 93; 73, 97; 73, 113; 73, 114; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:msg; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, string:'<b>{}</b>-hook function <b>{}</b>'; 80, identifier:format; 81, argument_list; 81, 82; 81, 83; 82, identifier:hookName; 83, subscript; 83, 84; 83, 88; 84, call; 84, 85; 84, 86; 85, identifier:str; 86, argument_list; 86, 87; 87, identifier:fun; 88, slice; 88, 89; 88, 90; 88, 91; 89, integer:1; 90, colon; 91, unary_operator:-; 91, 92; 92, integer:1; 93, expression_statement; 93, 94; 94, augmented_assignment:+=; 94, 95; 94, 96; 95, identifier:msg; 96, string:" did not execute properly."; 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:err; 102, identifier:QtmacsArgumentError; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, augmented_assignment:+=; 105, 106; 105, 107; 106, identifier:msg; 107, binary_operator:+; 107, 108; 107, 109; 108, string:'<br/>'; 109, call; 109, 110; 109, 111; 110, identifier:str; 111, argument_list; 111, 112; 112, identifier:err; 113, comment; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:qteLogger; 120, identifier:exception; 121, argument_list; 121, 122; 121, 123; 121, 126; 122, identifier:msg; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:exc_info; 125, True; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:stack_info; 128, True | def qteRunHook(self, hookName: str, msgObj: QtmacsMessage=None):
"""
Trigger the hook named ``hookName`` and pass on ``msgObj``.
This will call all slots associated with ``hookName`` but
without calling the event loop in between. Therefore, if
one slots changes the state of the GUI, every subsequent slot
may have difficulties determining the actual state of the GUI
using Qt accessor functions. It is thus usually a good idea
to either avoid manipulating the GUI directly, or call macros
because Qtmacs will always run the event loop in between any
two macros.
.. note: the slots are executed in the order in which they
were registered via ``qteConnectHook``, but there is no
guarantee that this is really so. However, it is guaranteed
that all slots will be triggered, even if some raise an error
during the execution.
|Args|
* ``hookName`` (**str**): the name of the hook to trigger.
* ``msgObj`` (**QtmacsMessage**): data passed to the function.
|Returns|
* **None**
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# Shorthand.
reg = self._qteRegistryHooks
# Do nothing if there are not recipients for the hook.
if hookName not in reg:
return
# Create an empty ``QtmacsMessage`` object if none was provided.
if msgObj is None:
msgObj = QtmacsMessage()
# Add information about the hook that will deliver ``msgObj``.
msgObj.setHookName(hookName)
# Try to call each slot. Intercept any errors but ensure that
# really all slots are called, irrespective of how many of them
# raise an error during execution.
for fun in reg[hookName]:
try:
fun(msgObj)
except Exception as err:
# Format the error message.
msg = '<b>{}</b>-hook function <b>{}</b>'.format(
hookName, str(fun)[1:-1])
msg += " did not execute properly."
if isinstance(err, QtmacsArgumentError):
msg += '<br/>' + str(err)
# Log the error.
self.qteLogger.exception(msg, exc_info=True, stack_info=True) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:qteMacroNameMangling; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:macroCls; 6, block; 6, 7; 6, 9; 6, 10; 6, 23; 6, 24; 6, 25; 6, 26; 6, 41; 6, 42; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:macroName; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:re; 16, identifier:sub; 17, argument_list; 17, 18; 17, 19; 17, 20; 18, string:r"([A-Z])"; 19, string:r'-\1'; 20, attribute; 20, 21; 20, 22; 21, identifier:macroCls; 22, identifier:__name__; 23, comment; 24, comment; 25, comment; 26, if_statement; 26, 27; 26, 32; 27, comparison_operator:==; 27, 28; 27, 31; 28, subscript; 28, 29; 28, 30; 29, identifier:macroName; 30, integer:0; 31, string:'-'; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:macroName; 36, subscript; 36, 37; 36, 38; 37, identifier:macroName; 38, slice; 38, 39; 38, 40; 39, integer:1; 40, colon; 41, comment; 42, return_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:macroName; 46, identifier:lower; 47, argument_list | def qteMacroNameMangling(self, macroCls):
"""
Convert the class name of a macro class to macro name.
The name mangling inserts a '-' character after every capital
letter and then lowers the entire string.
Example: if the class name of ``macroCls`` is 'ThisIsAMacro'
then this method will return 'this-is-a-macro', ie. every
capital letter (except the first) will be prefixed with a
hyphen and changed to lower case.
The method returns the name mangled macro name or **None**
if an error occurred.
|Args|
* ``macroCls`` (**QtmacsMacro**): ``QtmacsMacro``- or derived
class (not an instance!)
|Returns|
**str**: the name mangled string or **None** if an error occurred.
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# Replace camel bump as hyphenated lower case string.
macroName = re.sub(r"([A-Z])", r'-\1', macroCls.__name__)
# If the first character of the class name was a
# capital letter (likely) then the above substitution would have
# resulted in a leading hyphen. Remove it.
if macroName[0] == '-':
macroName = macroName[1:]
# Return the lower case string.
return macroName.lower() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:qteRegisterMacro; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 11; 4, identifier:self; 5, identifier:macroCls; 6, typed_default_parameter; 6, 7; 6, 8; 6, 10; 7, identifier:replaceMacro; 8, type; 8, 9; 9, identifier:bool; 10, False; 11, typed_default_parameter; 11, 12; 11, 13; 11, 15; 12, identifier:macroName; 13, type; 13, 14; 14, identifier:str; 15, None; 16, block; 16, 17; 16, 19; 16, 20; 16, 49; 16, 50; 16, 90; 16, 91; 16, 92; 16, 93; 16, 129; 16, 130; 16, 136; 16, 137; 16, 172; 16, 173; 16, 208; 16, 209; 16, 210; 16, 214; 16, 215; 16, 354; 16, 355; 16, 356; 16, 357; 17, expression_statement; 17, 18; 18, comment; 19, comment; 20, if_statement; 20, 21; 20, 27; 21, not_operator; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:issubclass; 24, argument_list; 24, 25; 24, 26; 25, identifier:macroCls; 26, identifier:QtmacsMacro; 27, block; 27, 28; 27, 43; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:args; 31, tuple; 31, 32; 31, 33; 31, 34; 32, string:'macroCls'; 33, string:'class QtmacsMacro'; 34, subscript; 34, 35; 34, 42; 35, subscript; 35, 36; 35, 41; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:inspect; 39, identifier:stack; 40, argument_list; 41, integer:0; 42, integer:3; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:QtmacsArgumentError; 46, argument_list; 46, 47; 47, list_splat; 47, 48; 48, identifier:args; 49, comment; 50, try_statement; 50, 51; 50, 58; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:macroObj; 55, call; 55, 56; 55, 57; 56, identifier:macroCls; 57, argument_list; 58, except_clause; 58, 59; 58, 60; 59, identifier:Exception; 60, block; 60, 61; 60, 65; 60, 76; 60, 88; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:msg; 64, string:'The macro <b>{}</b> has a faulty constructor.'; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:msg; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:msg; 71, identifier:format; 72, argument_list; 72, 73; 73, attribute; 73, 74; 73, 75; 74, identifier:macroCls; 75, identifier:__name__; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:qteLogger; 82, identifier:error; 83, argument_list; 83, 84; 83, 85; 84, identifier:msg; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:stack_info; 87, True; 88, return_statement; 88, 89; 89, None; 90, comment; 91, comment; 92, comment; 93, if_statement; 93, 94; 93, 97; 93, 98; 94, comparison_operator:is; 94, 95; 94, 96; 95, identifier:macroName; 96, None; 97, comment; 98, block; 98, 99; 99, if_statement; 99, 100; 99, 107; 99, 108; 99, 118; 100, comparison_operator:is; 100, 101; 100, 106; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:macroObj; 104, identifier:qteMacroName; 105, argument_list; 106, None; 107, comment; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:macroName; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:qteMacroNameMangling; 116, argument_list; 116, 117; 117, identifier:macroCls; 118, else_clause; 118, 119; 118, 120; 119, comment; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:macroName; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:macroObj; 127, identifier:qteMacroName; 128, argument_list; 129, comment; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:macroObj; 134, identifier:_qteMacroName; 135, identifier:macroName; 136, comment; 137, if_statement; 137, 138; 137, 148; 138, comparison_operator:==; 138, 139; 138, 147; 139, call; 139, 140; 139, 141; 140, identifier:len; 141, argument_list; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:macroObj; 145, identifier:qteAppletSignature; 146, argument_list; 147, integer:0; 148, block; 148, 149; 148, 158; 148, 170; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:msg; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, string:'Macro <b>{}</b> has no applet signatures.'; 155, identifier:format; 156, argument_list; 156, 157; 157, identifier:macroName; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 165; 160, attribute; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:qteLogger; 164, identifier:error; 165, argument_list; 165, 166; 165, 167; 166, identifier:msg; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:stack_info; 169, True; 170, return_statement; 170, 171; 171, None; 172, comment; 173, if_statement; 173, 174; 173, 184; 174, comparison_operator:==; 174, 175; 174, 183; 175, call; 175, 176; 175, 177; 176, identifier:len; 177, argument_list; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:macroObj; 181, identifier:qteWidgetSignature; 182, argument_list; 183, integer:0; 184, block; 184, 185; 184, 194; 184, 206; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:msg; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, string:'Macro <b>{}</b> has no widget signatures.'; 191, identifier:format; 192, argument_list; 192, 193; 193, identifier:macroName; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 201; 196, attribute; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:qteLogger; 200, identifier:error; 201, argument_list; 201, 202; 201, 203; 202, identifier:msg; 203, keyword_argument; 203, 204; 203, 205; 204, identifier:stack_info; 205, True; 206, return_statement; 206, 207; 207, None; 208, comment; 209, comment; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:anyRegistered; 213, False; 214, comment; 215, for_statement; 215, 216; 215, 217; 215, 222; 215, 223; 216, identifier:app_sig; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:macroObj; 220, identifier:qteAppletSignature; 221, argument_list; 222, comment; 223, block; 223, 224; 224, for_statement; 224, 225; 224, 226; 224, 231; 224, 232; 224, 233; 225, identifier:wid_sig; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:macroObj; 229, identifier:qteWidgetSignature; 230, argument_list; 231, comment; 232, comment; 233, block; 233, 234; 233, 241; 233, 242; 233, 243; 233, 244; 233, 322; 233, 323; 233, 331; 233, 341; 233, 350; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:macroNameInternal; 237, tuple; 237, 238; 237, 239; 237, 240; 238, identifier:macroName; 239, identifier:app_sig; 240, identifier:wid_sig; 241, comment; 242, comment; 243, comment; 244, if_statement; 244, 245; 244, 250; 245, comparison_operator:in; 245, 246; 245, 247; 246, identifier:macroNameInternal; 247, attribute; 247, 248; 247, 249; 248, identifier:self; 249, identifier:_qteRegistryMacros; 250, block; 250, 251; 251, if_statement; 251, 252; 251, 253; 251, 254; 251, 295; 252, identifier:replaceMacro; 253, comment; 254, block; 254, 255; 254, 266; 254, 270; 254, 280; 254, 289; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:tmp; 258, call; 258, 259; 258, 264; 259, attribute; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:self; 262, identifier:_qteRegistryMacros; 263, identifier:pop; 264, argument_list; 264, 265; 265, identifier:macroNameInternal; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:msg; 269, string:'Replacing existing macro <b>{}</b> with new {}.'; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:msg; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:msg; 276, identifier:format; 277, argument_list; 277, 278; 277, 279; 278, identifier:macroNameInternal; 279, identifier:macroObj; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 287; 282, attribute; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:self; 285, identifier:qteLogger; 286, identifier:info; 287, argument_list; 287, 288; 288, identifier:msg; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:tmp; 293, identifier:deleteLater; 294, argument_list; 295, else_clause; 295, 296; 296, block; 296, 297; 296, 301; 296, 310; 296, 319; 296, 320; 296, 321; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 300; 299, identifier:msg; 300, string:'Macro <b>{}</b> already exists (not replaced).'; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:msg; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:msg; 307, identifier:format; 308, argument_list; 308, 309; 309, identifier:macroNameInternal; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 317; 312, attribute; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:self; 315, identifier:qteLogger; 316, identifier:info; 317, argument_list; 317, 318; 318, identifier:msg; 319, comment; 320, comment; 321, continue_statement; 322, comment; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 330; 325, subscript; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:self; 328, identifier:_qteRegistryMacros; 329, identifier:macroNameInternal; 330, identifier:macroObj; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 334; 333, identifier:msg; 334, parenthesized_expression; 334, 335; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, string:'Macro <b>{}</b> successfully registered.'; 338, identifier:format; 339, argument_list; 339, 340; 340, identifier:macroNameInternal; 341, expression_statement; 341, 342; 342, call; 342, 343; 342, 348; 343, attribute; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:self; 346, identifier:qteLogger; 347, identifier:info; 348, argument_list; 348, 349; 349, identifier:msg; 350, expression_statement; 350, 351; 351, assignment; 351, 352; 351, 353; 352, identifier:anyRegistered; 353, True; 354, comment; 355, comment; 356, comment; 357, return_statement; 357, 358; 358, identifier:macroName | def qteRegisterMacro(self, macroCls, replaceMacro: bool=False,
macroName: str=None):
"""
Register a macro.
If ``macroName`` is **None** then its named is deduced from
its class name (see ``qteMacroNameMangling`` for details).
Multiple macros with the same name can co-exist as long as
their applet- and widget signatures, as reported by the
``qteAppletSignature`` and ``qteWidgetSignature`` methods,
differ. If ``macroCls`` has the same name and signatures as an
already registered macro then the ``replaceMacro`` flag
decides:
* **True**: the existing macro will be replaced for all
applet- and widget signatures specified by the new macro
``macroCls``.
* **False**: the ``macroCls`` will not be registered.
The method returns **None** if an error occurred (eg. the
macro constructor is faulty), or the macro name as a
string. If a macro was already registered and not replaced
(ie. ``replaceMacro``) then the macro name is returned
nonetheless.
.. note:: if an existing macro is replaced the old macro
is not deleted (it probably should be, though).
|Args|
* ``macroCls`` (**QtmacsMacro**): QtmacsMacro or derived
(not type checked!)
* ``replaceMacro`` (**bool**): whether or not to replace
an existing macro.
* ``macroName`` (**str**): the name under which the macro
should be registered.
|Returns|
**str**: the name of the just registered macro, or **None** if
that failed.
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# Check type of input arguments.
if not issubclass(macroCls, QtmacsMacro):
args = ('macroCls', 'class QtmacsMacro', inspect.stack()[0][3])
raise QtmacsArgumentError(*args)
# Try to instantiate the macro class.
try:
macroObj = macroCls()
except Exception:
msg = 'The macro <b>{}</b> has a faulty constructor.'
msg = msg.format(macroCls.__name__)
self.qteLogger.error(msg, stack_info=True)
return None
# The three options to determine the macro name, in order of
# precedence, are: passed to this function, specified in the
# macro constructor, name mangled.
if macroName is None:
# No macro name was passed to the function.
if macroObj.qteMacroName() is None:
# The macro has already named itself.
macroName = self.qteMacroNameMangling(macroCls)
else:
# The macro name is inferred from the class name.
macroName = macroObj.qteMacroName()
# Let the macro know under which name it is known inside Qtmacs.
macroObj._qteMacroName = macroName
# Ensure the macro has applet signatures.
if len(macroObj.qteAppletSignature()) == 0:
msg = 'Macro <b>{}</b> has no applet signatures.'.format(macroName)
self.qteLogger.error(msg, stack_info=True)
return None
# Ensure the macro has widget signatures.
if len(macroObj.qteWidgetSignature()) == 0:
msg = 'Macro <b>{}</b> has no widget signatures.'.format(macroName)
self.qteLogger.error(msg, stack_info=True)
return None
# Flag to indicate that at least one new macro type was
# registered.
anyRegistered = False
# Iterate over all applet signatures.
for app_sig in macroObj.qteAppletSignature():
# Iterate over all widget signatures.
for wid_sig in macroObj.qteWidgetSignature():
# Infer the macro name from the class name of the
# passed macro object.
macroNameInternal = (macroName, app_sig, wid_sig)
# If a macro with this name already exists then either
# replace it, or skip the registration process for the
# new one.
if macroNameInternal in self._qteRegistryMacros:
if replaceMacro:
# Remove existing macro.
tmp = self._qteRegistryMacros.pop(macroNameInternal)
msg = 'Replacing existing macro <b>{}</b> with new {}.'
msg = msg.format(macroNameInternal, macroObj)
self.qteLogger.info(msg)
tmp.deleteLater()
else:
msg = 'Macro <b>{}</b> already exists (not replaced).'
msg = msg.format(macroNameInternal)
self.qteLogger.info(msg)
# Macro was not registered for this widget
# signature.
continue
# Add macro object to the registry.
self._qteRegistryMacros[macroNameInternal] = macroObj
msg = ('Macro <b>{}</b> successfully registered.'
.format(macroNameInternal))
self.qteLogger.info(msg)
anyRegistered = True
# Return the name of the macro, irrespective of whether or not
# it is a newly created macro, or if the old macro was kept
# (in case of a name conflict).
return macroName |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:qteGetMacroObject; 3, parameters; 3, 4; 3, 5; 3, 9; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:macroName; 7, type; 7, 8; 8, identifier:str; 9, typed_parameter; 9, 10; 9, 11; 10, identifier:widgetObj; 11, type; 11, 12; 12, attribute; 12, 13; 12, 14; 13, identifier:QtGui; 14, identifier:QWidget; 15, block; 15, 16; 15, 18; 15, 19; 15, 20; 15, 21; 15, 22; 15, 23; 15, 24; 15, 25; 15, 26; 15, 106; 15, 107; 15, 108; 15, 124; 15, 125; 15, 126; 15, 127; 15, 143; 15, 183; 15, 184; 16, expression_statement; 16, 17; 17, comment; 18, comment; 19, comment; 20, comment; 21, comment; 22, comment; 23, comment; 24, comment; 25, comment; 26, if_statement; 26, 27; 26, 32; 26, 74; 27, call; 27, 28; 27, 29; 28, identifier:hasattr; 29, argument_list; 29, 30; 29, 31; 30, identifier:widgetObj; 31, string:'_qteAdmin'; 32, block; 32, 33; 32, 41; 32, 49; 32, 50; 32, 51; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:app_signature; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:widgetObj; 39, identifier:_qteAdmin; 40, identifier:appletSignature; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:wid_signature; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:widgetObj; 47, identifier:_qteAdmin; 48, identifier:widgetSignature; 49, comment; 50, comment; 51, if_statement; 51, 52; 51, 55; 52, comparison_operator:is; 52, 53; 52, 54; 53, identifier:app_signature; 54, None; 55, block; 55, 56; 55, 60; 55, 72; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:msg; 59, string:'Applet has no signature.'; 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:qteLogger; 66, identifier:error; 67, argument_list; 67, 68; 67, 69; 68, identifier:msg; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:stack_info; 71, True; 72, return_statement; 72, 73; 73, None; 74, else_clause; 74, 75; 75, block; 75, 76; 75, 80; 75, 87; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:wid_signature; 79, None; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:app; 83, call; 83, 84; 83, 85; 84, identifier:qteGetAppletFromWidget; 85, argument_list; 85, 86; 86, identifier:widgetObj; 87, if_statement; 87, 88; 87, 91; 87, 96; 88, comparison_operator:is; 88, 89; 88, 90; 89, identifier:app; 90, None; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:app_signature; 95, None; 96, else_clause; 96, 97; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:app_signature; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:app; 104, identifier:_qteAdmin; 105, identifier:appletSignature; 106, comment; 107, comment; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:name_match; 111, list_comprehension; 111, 112; 111, 113; 111, 118; 112, identifier:m; 113, for_in_clause; 113, 114; 113, 115; 114, identifier:m; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_qteRegistryMacros; 118, if_clause; 118, 119; 119, comparison_operator:==; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:m; 122, integer:0; 123, identifier:macroName; 124, comment; 125, comment; 126, comment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:app_sig_match; 130, list_comprehension; 130, 131; 130, 132; 130, 135; 131, identifier:_; 132, for_in_clause; 132, 133; 132, 134; 133, identifier:_; 134, identifier:name_match; 135, if_clause; 135, 136; 136, comparison_operator:in; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:_; 139, integer:1; 140, tuple; 140, 141; 140, 142; 141, identifier:app_signature; 142, string:'*'; 143, if_statement; 143, 144; 143, 147; 143, 162; 144, comparison_operator:is; 144, 145; 144, 146; 145, identifier:wid_signature; 146, None; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:wid_sig_match; 151, list_comprehension; 151, 152; 151, 153; 151, 156; 152, identifier:_; 153, for_in_clause; 153, 154; 153, 155; 154, identifier:_; 155, identifier:app_sig_match; 156, if_clause; 156, 157; 157, comparison_operator:==; 157, 158; 157, 161; 158, subscript; 158, 159; 158, 160; 159, identifier:_; 160, integer:2; 161, string:'*'; 162, else_clause; 162, 163; 162, 164; 162, 165; 162, 166; 163, comment; 164, comment; 165, comment; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:wid_sig_match; 170, list_comprehension; 170, 171; 170, 172; 170, 175; 171, identifier:_; 172, for_in_clause; 172, 173; 172, 174; 173, identifier:_; 174, identifier:app_sig_match; 175, if_clause; 175, 176; 176, comparison_operator:in; 176, 177; 176, 180; 177, subscript; 177, 178; 177, 179; 178, identifier:_; 179, integer:2; 180, tuple; 180, 181; 180, 182; 181, identifier:wid_signature; 182, string:'*'; 183, comment; 184, if_statement; 184, 185; 184, 191; 184, 192; 184, 193; 184, 196; 184, 218; 185, comparison_operator:==; 185, 186; 185, 190; 186, call; 186, 187; 186, 188; 187, identifier:len; 188, argument_list; 188, 189; 189, identifier:wid_sig_match; 190, integer:0; 191, comment; 192, comment; 193, block; 193, 194; 194, return_statement; 194, 195; 195, None; 196, elif_clause; 196, 197; 196, 203; 197, comparison_operator:==; 197, 198; 197, 202; 198, call; 198, 199; 198, 200; 199, identifier:len; 200, argument_list; 200, 201; 201, identifier:wid_sig_match; 202, integer:1; 203, block; 203, 204; 203, 210; 203, 211; 203, 212; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:match; 207, subscript; 207, 208; 207, 209; 208, identifier:wid_sig_match; 209, integer:0; 210, comment; 211, comment; 212, return_statement; 212, 213; 213, subscript; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:_qteRegistryMacros; 217, identifier:match; 218, else_clause; 218, 219; 218, 220; 218, 221; 218, 222; 218, 223; 218, 224; 218, 225; 219, comment; 220, comment; 221, comment; 222, comment; 223, comment; 224, comment; 225, block; 225, 226; 225, 248; 225, 268; 225, 269; 225, 283; 225, 303; 225, 304; 225, 318; 225, 338; 225, 339; 225, 340; 225, 341; 225, 363; 225, 383; 225, 384; 225, 388; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:tmp; 229, list_comprehension; 229, 230; 229, 231; 229, 234; 230, identifier:match; 231, for_in_clause; 231, 232; 231, 233; 232, identifier:match; 233, identifier:wid_sig_match; 234, if_clause; 234, 235; 235, boolean_operator:and; 235, 236; 235, 242; 236, parenthesized_expression; 236, 237; 237, comparison_operator:!=; 237, 238; 237, 241; 238, subscript; 238, 239; 238, 240; 239, identifier:match; 240, integer:1; 241, string:'*'; 242, parenthesized_expression; 242, 243; 243, comparison_operator:!=; 243, 244; 243, 247; 244, subscript; 244, 245; 244, 246; 245, identifier:match; 246, integer:2; 247, string:'*'; 248, if_statement; 248, 249; 248, 255; 249, comparison_operator:>; 249, 250; 249, 254; 250, call; 250, 251; 250, 252; 251, identifier:len; 252, argument_list; 252, 253; 253, identifier:tmp; 254, integer:0; 255, block; 255, 256; 255, 262; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:match; 259, subscript; 259, 260; 259, 261; 260, identifier:tmp; 261, integer:0; 262, return_statement; 262, 263; 263, subscript; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:self; 266, identifier:_qteRegistryMacros; 267, identifier:match; 268, comment; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:tmp; 272, list_comprehension; 272, 273; 272, 274; 272, 277; 273, identifier:match; 274, for_in_clause; 274, 275; 274, 276; 275, identifier:match; 276, identifier:wid_sig_match; 277, if_clause; 277, 278; 278, comparison_operator:!=; 278, 279; 278, 282; 279, subscript; 279, 280; 279, 281; 280, identifier:match; 281, integer:2; 282, string:'*'; 283, if_statement; 283, 284; 283, 290; 284, comparison_operator:>; 284, 285; 284, 289; 285, call; 285, 286; 285, 287; 286, identifier:len; 287, argument_list; 287, 288; 288, identifier:tmp; 289, integer:0; 290, block; 290, 291; 290, 297; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:match; 294, subscript; 294, 295; 294, 296; 295, identifier:tmp; 296, integer:0; 297, return_statement; 297, 298; 298, subscript; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:self; 301, identifier:_qteRegistryMacros; 302, identifier:match; 303, comment; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:tmp; 307, list_comprehension; 307, 308; 307, 309; 307, 312; 308, identifier:match; 309, for_in_clause; 309, 310; 309, 311; 310, identifier:match; 311, identifier:wid_sig_match; 312, if_clause; 312, 313; 313, comparison_operator:!=; 313, 314; 313, 317; 314, subscript; 314, 315; 314, 316; 315, identifier:match; 316, integer:1; 317, string:'*'; 318, if_statement; 318, 319; 318, 325; 319, comparison_operator:>; 319, 320; 319, 324; 320, call; 320, 321; 320, 322; 321, identifier:len; 322, argument_list; 322, 323; 323, identifier:tmp; 324, integer:0; 325, block; 325, 326; 325, 332; 326, expression_statement; 326, 327; 327, assignment; 327, 328; 327, 329; 328, identifier:match; 329, subscript; 329, 330; 329, 331; 330, identifier:tmp; 331, integer:0; 332, return_statement; 332, 333; 333, subscript; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:self; 336, identifier:_qteRegistryMacros; 337, identifier:match; 338, comment; 339, comment; 340, comment; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 344; 343, identifier:tmp; 344, list_comprehension; 344, 345; 344, 346; 344, 349; 345, identifier:match; 346, for_in_clause; 346, 347; 346, 348; 347, identifier:match; 348, identifier:wid_sig_match; 349, if_clause; 349, 350; 350, boolean_operator:and; 350, 351; 350, 357; 351, parenthesized_expression; 351, 352; 352, comparison_operator:==; 352, 353; 352, 356; 353, subscript; 353, 354; 353, 355; 354, identifier:match; 355, integer:1; 356, string:'*'; 357, parenthesized_expression; 357, 358; 358, comparison_operator:==; 358, 359; 358, 362; 359, subscript; 359, 360; 359, 361; 360, identifier:match; 361, integer:2; 362, string:'*'; 363, if_statement; 363, 364; 363, 370; 364, comparison_operator:>; 364, 365; 364, 369; 365, call; 365, 366; 365, 367; 366, identifier:len; 367, argument_list; 367, 368; 368, identifier:tmp; 369, integer:0; 370, block; 370, 371; 370, 377; 371, expression_statement; 371, 372; 372, assignment; 372, 373; 372, 374; 373, identifier:match; 374, subscript; 374, 375; 374, 376; 375, identifier:tmp; 376, integer:0; 377, return_statement; 377, 378; 378, subscript; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:self; 381, identifier:_qteRegistryMacros; 382, identifier:match; 383, comment; 384, expression_statement; 384, 385; 385, assignment; 385, 386; 385, 387; 386, identifier:msg; 387, string:'No compatible macro found - should be impossible.'; 388, expression_statement; 388, 389; 389, call; 389, 390; 389, 395; 390, attribute; 390, 391; 390, 394; 391, attribute; 391, 392; 391, 393; 392, identifier:self; 393, identifier:qteLogger; 394, identifier:error; 395, argument_list; 395, 396; 395, 397; 396, identifier:msg; 397, keyword_argument; 397, 398; 397, 399; 398, identifier:stack_info; 399, True | def qteGetMacroObject(self, macroName: str, widgetObj: QtGui.QWidget):
"""
Return macro that is name- and signature compatible with
``macroName`` and ``widgetObj``.
The method considers all macros with name ``macroName`` and
returns the one that matches 'best'. To determine this best
match, the applet-and widget signatures of the macro are
compared to those of ``widgetObj`` and picked in the following
order:
1. Applet- and widget signature of both match.
2. Widget signature matches, applet signature in macro is "*"
3. Applet signature matches, widget signature in macro is "*"
4. Macro reports "*" for both its applet- and widget signature.
If the macro does not fit any of these four criteria, then no
compatible macro is available and the method returns **None**.
|Args|
* ``macroName`` (**str**): name of macro.
* ``widgetObj`` (**QWidget**): widget for which a compatible
macro is sought.
|Returns|
* **QtmacsMacro**: best matching macro, or **None**.
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# Determine the applet- and widget signature. This is trivial
# if the widget was registered with Qtmacs because its
# '_qteAdmin' attribute will provide this information. If, on
# the other hand, the widget was not registered with Qtmacs
# then it has no signature, yet its parent applet must because
# every applet has one. The only exception when the applet
# signature is therefore when there are no applets to begin
# with, ie. the the window is empty.
if hasattr(widgetObj, '_qteAdmin'):
app_signature = widgetObj._qteAdmin.appletSignature
wid_signature = widgetObj._qteAdmin.widgetSignature
# Return immediately if the applet signature is None
# (should be impossible).
if app_signature is None:
msg = 'Applet has no signature.'
self.qteLogger.error(msg, stack_info=True)
return None
else:
wid_signature = None
app = qteGetAppletFromWidget(widgetObj)
if app is None:
app_signature = None
else:
app_signature = app._qteAdmin.appletSignature
# Find all macros with name 'macroName'. This will produce a list of
# tuples with entries (macroName, app_sig, wid_sig).
name_match = [m for m in self._qteRegistryMacros if m[0] == macroName]
# Find all macros with a compatible applet signature. This is
# produce another list of tuples with the same format as
# the name_match list (see above).
app_sig_match = [_ for _ in name_match if _[1] in (app_signature, '*')]
if wid_signature is None:
wid_sig_match = [_ for _ in app_sig_match if _[2] == '*']
else:
# Find all macros with a compatible widget signature. This is
# a list of tuples, each tuple consisting of (macroName,
# app_sig, wid_sig).
wid_sig_match = [_ for _ in app_sig_match
if _[2] in (wid_signature, '*')]
# Pick a macro.
if len(wid_sig_match) == 0:
# No macro is compatible with either the applet- or widget
# signature.
return None
elif len(wid_sig_match) == 1:
match = wid_sig_match[0]
# Exactly one macro is compatible with either the applet-
# or widget signature.
return self._qteRegistryMacros[match]
else:
# Found multiple matches. For any given macro 'name',
# applet signature 'app', and widget signature 'wid' there
# can be at most four macros in the list: *:*:name,
# wid:*:name, *:app:name, and wid:app:name.
# See if there is a macro for which both the applet and
# widget signature match.
tmp = [match for match in wid_sig_match if (match[1] != '*')
and (match[2] != '*')]
if len(tmp) > 0:
match = tmp[0]
return self._qteRegistryMacros[match]
# See if there is a macro with a matching widget signature.
tmp = [match for match in wid_sig_match if match[2] != '*']
if len(tmp) > 0:
match = tmp[0]
return self._qteRegistryMacros[match]
# See if there is a macro with a matching applet signature.
tmp = [match for match in wid_sig_match if match[1] != '*']
if len(tmp) > 0:
match = tmp[0]
return self._qteRegistryMacros[match]
# At this point only one possibility is left, namely a
# generic macro that is applicable to arbitrary applets
# and widgets, eg. NextApplet.
tmp = [match for match in wid_sig_match if (match[1] == '*')
and (match[2] == '*')]
if len(tmp) > 0:
match = tmp[0]
return self._qteRegistryMacros[match]
# This should be impossible.
msg = 'No compatible macro found - should be impossible.'
self.qteLogger.error(msg, stack_info=True) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:qteGetAllMacroNames; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_default_parameter; 5, 6; 5, 7; 5, 11; 6, identifier:widgetObj; 7, type; 7, 8; 8, attribute; 8, 9; 8, 10; 9, identifier:QtGui; 10, identifier:QWidget; 11, None; 12, block; 12, 13; 12, 15; 12, 16; 12, 17; 12, 18; 12, 31; 12, 41; 12, 51; 12, 52; 12, 53; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, comment; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:macro_list; 21, call; 21, 22; 21, 23; 22, identifier:tuple; 23, argument_list; 23, 24; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:_qteRegistryMacros; 29, identifier:keys; 30, argument_list; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:macro_list; 34, list_comprehension; 34, 35; 34, 38; 35, subscript; 35, 36; 35, 37; 36, identifier:_; 37, integer:0; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:_; 40, identifier:macro_list; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:macro_list; 44, call; 44, 45; 44, 46; 45, identifier:tuple; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:set; 49, argument_list; 49, 50; 50, identifier:macro_list; 51, comment; 52, comment; 53, if_statement; 53, 54; 53, 57; 53, 60; 54, comparison_operator:is; 54, 55; 54, 56; 55, identifier:widgetObj; 56, None; 57, block; 57, 58; 58, return_statement; 58, 59; 59, identifier:macro_list; 60, else_clause; 60, 61; 60, 62; 60, 63; 60, 64; 61, comment; 62, comment; 63, comment; 64, block; 64, 65; 64, 79; 64, 80; 64, 81; 64, 82; 64, 98; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:macro_list; 68, list_comprehension; 68, 69; 68, 76; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:qteGetMacroObject; 73, argument_list; 73, 74; 73, 75; 74, identifier:macroName; 75, identifier:widgetObj; 76, for_in_clause; 76, 77; 76, 78; 77, identifier:macroName; 78, identifier:macro_list; 79, comment; 80, comment; 81, comment; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:macro_list; 85, list_comprehension; 85, 86; 85, 91; 85, 94; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:_; 89, identifier:qteMacroName; 90, argument_list; 91, for_in_clause; 91, 92; 91, 93; 92, identifier:_; 93, identifier:macro_list; 94, if_clause; 94, 95; 95, comparison_operator:is; 95, 96; 95, 97; 96, identifier:_; 97, None; 98, return_statement; 98, 99; 99, identifier:macro_list | def qteGetAllMacroNames(self, widgetObj: QtGui.QWidget=None):
"""
Return all macro names known to Qtmacs as a list.
If ``widgetObj`` is **None** then the names of all registered
macros are returned as a tuple. Otherwise, only those macro
compatible with ``widgetObj`` are returned. See
``qteGetMacroObject`` for the definition of a compatible
macro.
|Args|
* ``widgetObj`` (**QWidget**): widget with which the macros
must be compatible.
|Returns|
* **tuple**: tuple of macro names.
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# The keys of qteRegistryMacros are (macroObj, app_sig,
# wid_sig) tuples. Get them, extract the macro names, and
# remove all duplicates.
macro_list = tuple(self._qteRegistryMacros.keys())
macro_list = [_[0] for _ in macro_list]
macro_list = tuple(set(macro_list))
# If no widget object was supplied then omit the signature
# check and return the macro list verbatim.
if widgetObj is None:
return macro_list
else:
# Use qteGetMacroObject to compile a list of macros that
# are compatible with widgetObj. This list contains
# (macroObj, macroName, app_sig, wid_sig) tuples.
macro_list = [self.qteGetMacroObject(macroName, widgetObj)
for macroName in macro_list]
# Remove all elements where macroObj=None. This is the
# case if no compatible macro with the specified name
# could be found for widgetObj.
macro_list = [_.qteMacroName() for _ in macro_list
if _ is not None]
return macro_list |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:qteBindKeyGlobal; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:keysequence; 6, typed_parameter; 6, 7; 6, 8; 7, identifier:macroName; 8, type; 8, 9; 9, identifier:str; 10, block; 10, 11; 10, 13; 10, 14; 10, 15; 10, 22; 10, 23; 10, 24; 10, 60; 10, 61; 10, 62; 10, 72; 10, 73; 10, 74; 10, 75; 10, 76; 10, 91; 11, expression_statement; 11, 12; 12, comment; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:keysequence; 18, call; 18, 19; 18, 20; 19, identifier:QtmacsKeysequence; 20, argument_list; 20, 21; 21, identifier:keysequence; 22, comment; 23, comment; 24, if_statement; 24, 25; 24, 32; 25, not_operator; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:qteIsMacroRegistered; 30, argument_list; 30, 31; 31, identifier:macroName; 32, block; 32, 33; 32, 37; 32, 46; 32, 58; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:msg; 36, string:'Cannot globally bind key to unknown macro <b>{}</b>.'; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:msg; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:msg; 43, identifier:format; 44, argument_list; 44, 45; 45, identifier:macroName; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:qteLogger; 52, identifier:error; 53, argument_list; 53, 54; 53, 55; 54, identifier:msg; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:stack_info; 57, True; 58, return_statement; 58, 59; 59, False; 60, comment; 61, comment; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:_qteGlobalKeyMap; 68, identifier:qteInsertKey; 69, argument_list; 69, 70; 69, 71; 70, identifier:keysequence; 71, identifier:macroName; 72, comment; 73, comment; 74, comment; 75, comment; 76, for_statement; 76, 77; 76, 78; 76, 81; 77, identifier:app; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:_qteAppletList; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:qteBindKeyApplet; 87, argument_list; 87, 88; 87, 89; 87, 90; 88, identifier:keysequence; 89, identifier:macroName; 90, identifier:app; 91, return_statement; 91, 92; 92, True | def qteBindKeyGlobal(self, keysequence, macroName: str):
"""
Associate ``macroName`` with ``keysequence`` in all current
applets.
This method will bind ``macroName`` to ``keysequence`` in the
global key map and **all** local key maps. This also applies
for all applets (and their constituent widgets) yet to be
instantiated because they will inherit a copy of the global
keymap.
.. note:: This binding is signature independent.
If the ``macroName`` was not registered the method returns
**False**.
The ``keysequence`` can be specified either as a string (eg
'<ctrl>+x <ctrl>+f'), or a list of tuples containing the
constants from the ``QtCore.Qt`` name space
(eg. [(ControlModifier, Key_X), (ControlModifier, Key_F)]), or
as a ``QtmacsKeysequence`` object.
|Args|
* ``keysequence`` (**str**, **list** of **tuples**,
**QtmacsKeysequence**): key sequence to activate ``macroName``
for specified ``widgetSignature``.
* ``macroName`` (**str**): name of macro to associate with
``keysequence``.
|Returns|
**bool**: **True** if the binding was successful.
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
* **QtmacsKeysequenceError** if the provided ``keysequence``
could not be parsed.
"""
# Convert the key sequence into a QtmacsKeysequence object, or
# raise an QtmacsOtherError if the conversion is impossible.
keysequence = QtmacsKeysequence(keysequence)
# Sanity check: the macro must have been registered
# beforehand.
if not self.qteIsMacroRegistered(macroName):
msg = 'Cannot globally bind key to unknown macro <b>{}</b>.'
msg = msg.format(macroName)
self.qteLogger.error(msg, stack_info=True)
return False
# Insert/overwrite the key sequence and associate it with the
# new macro.
self._qteGlobalKeyMap.qteInsertKey(keysequence, macroName)
# Now update the local key map of every applet. Note that
# globally bound macros apply to every applet (hence the loop
# below) and every widget therein (hence the "*" parameter for
# the widget signature).
for app in self._qteAppletList:
self.qteBindKeyApplet(keysequence, macroName, app)
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:qteRegisterApplet; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:cls; 6, typed_default_parameter; 6, 7; 6, 8; 6, 10; 7, identifier:replaceApplet; 8, type; 8, 9; 9, identifier:bool; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 44; 11, 45; 11, 46; 11, 52; 11, 53; 11, 102; 11, 103; 11, 104; 11, 105; 11, 106; 11, 112; 11, 113; 11, 121; 11, 135; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, if_statement; 15, 16; 15, 22; 16, not_operator; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:issubclass; 19, argument_list; 19, 20; 19, 21; 20, identifier:cls; 21, identifier:QtmacsApplet; 22, block; 22, 23; 22, 38; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:args; 26, tuple; 26, 27; 26, 28; 26, 29; 27, string:'cls'; 28, string:'class QtmacsApplet'; 29, subscript; 29, 30; 29, 37; 30, subscript; 30, 31; 30, 36; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:inspect; 34, identifier:stack; 35, argument_list; 36, integer:0; 37, integer:3; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:QtmacsArgumentError; 41, argument_list; 41, 42; 42, list_splat; 42, 43; 43, identifier:args; 44, comment; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:class_name; 49, attribute; 49, 50; 49, 51; 50, identifier:cls; 51, identifier:__name__; 52, comment; 53, if_statement; 53, 54; 53, 59; 54, comparison_operator:in; 54, 55; 54, 56; 55, identifier:class_name; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:_qteRegistryApplets; 59, block; 59, 60; 59, 69; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:msg; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, string:'The original applet <b>{}</b>'; 66, identifier:format; 67, argument_list; 67, 68; 68, identifier:class_name; 69, if_statement; 69, 70; 69, 71; 69, 85; 70, identifier:replaceApplet; 71, block; 71, 72; 71, 76; 72, expression_statement; 72, 73; 73, augmented_assignment:+=; 73, 74; 73, 75; 74, identifier:msg; 75, string:' was redefined.'; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:qteLogger; 82, identifier:warning; 83, argument_list; 83, 84; 84, identifier:msg; 85, else_clause; 85, 86; 86, block; 86, 87; 86, 91; 86, 100; 87, expression_statement; 87, 88; 88, augmented_assignment:+=; 88, 89; 88, 90; 89, identifier:msg; 90, string:' was not redefined.'; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:qteLogger; 97, identifier:warning; 98, argument_list; 98, 99; 99, identifier:msg; 100, return_statement; 100, 101; 101, identifier:class_name; 102, comment; 103, comment; 104, comment; 105, comment; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:cls; 110, identifier:__qteRegisterAppletInit__; 111, argument_list; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_qteRegistryApplets; 119, identifier:class_name; 120, identifier:cls; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:qteLogger; 127, identifier:info; 128, argument_list; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, string:'Applet <b>{}</b> now registered.'; 132, identifier:format; 133, argument_list; 133, 134; 134, identifier:class_name; 135, return_statement; 135, 136; 136, identifier:class_name | def qteRegisterApplet(self, cls, replaceApplet: bool=False):
"""
Register ``cls`` as an applet.
The name of the applet is the class name of ``cls``
itself. For instance, if the applet was defined and registered
as
class NewApplet17(QtmacsApplet):
...
app_name = qteRegisterApplet(NewApplet17)
then the applet will be known as *NewApplet17*, which is also
returned in ``app_name``.
If an applet with this name already exists then
``replaceApplet`` decides whether the registration will
overwrite the existing definition or ignore the registration
request altogether. In the first case, none of the already
instantiated applets will be affected, only newly created ones
will use the new definition.
.. note:: this method expects a *class*, not an instance.
|Args|
* ``cls`` (**class QtmacsApplet**): this must really be a class,
not an instance.
* ``replaceApplet`` (**bool**): if applet with same name exists,
then replace it.
|Returns|
* **str**: name under which the applet was registered with Qtmacs.
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# Check type of input arguments.
if not issubclass(cls, QtmacsApplet):
args = ('cls', 'class QtmacsApplet', inspect.stack()[0][3])
raise QtmacsArgumentError(*args)
# Extract the class name as string, because this is the name
# under which the applet will be known.
class_name = cls.__name__
# Issue a warning if an applet with this name already exists.
if class_name in self._qteRegistryApplets:
msg = 'The original applet <b>{}</b>'.format(class_name)
if replaceApplet:
msg += ' was redefined.'
self.qteLogger.warning(msg)
else:
msg += ' was not redefined.'
self.qteLogger.warning(msg)
return class_name
# Execute the classmethod __qteRegisterAppletInit__ to
# allow the applet to make global initialisations that do
# not depend on a particular instance, eg. the supported
# file types.
cls.__qteRegisterAppletInit__()
# Add the class (not instance!) to the applet registry.
self._qteRegistryApplets[class_name] = cls
self.qteLogger.info('Applet <b>{}</b> now registered.'
.format(class_name))
return class_name |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:start; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kwargs; 7, block; 7, 8; 7, 10; 7, 14; 7, 18; 7, 25; 7, 121; 7, 127; 7, 132; 7, 138; 7, 198; 7, 262; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:ts; 13, dictionary; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:ret; 17, dictionary; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:info_parts; 21, set; 21, 22; 21, 23; 21, 24; 22, string:'info'; 23, string:'get-value'; 24, string:'get_value'; 25, for_statement; 25, 26; 25, 27; 25, 30; 26, identifier:cmd; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:commands; 30, block; 30, 31; 30, 43; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:parser_cmd; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:parser_commands; 39, identifier:get; 40, argument_list; 40, 41; 40, 42; 41, identifier:cmd; 42, identifier:cmd; 43, if_statement; 43, 44; 43, 51; 44, boolean_operator:or; 44, 45; 44, 48; 45, comparison_operator:in; 45, 46; 45, 47; 46, identifier:parser_cmd; 47, identifier:kwargs; 48, comparison_operator:in; 48, 49; 48, 50; 49, identifier:cmd; 50, identifier:kwargs; 51, block; 51, 52; 51, 67; 51, 81; 51, 91; 51, 101; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:kws; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:kwargs; 58, identifier:get; 59, argument_list; 59, 60; 59, 61; 60, identifier:cmd; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:kwargs; 64, identifier:get; 65, argument_list; 65, 66; 66, identifier:parser_cmd; 67, if_statement; 67, 68; 67, 73; 68, call; 68, 69; 68, 70; 69, identifier:isinstance; 70, argument_list; 70, 71; 70, 72; 71, identifier:kws; 72, identifier:Namespace; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:kws; 77, call; 77, 78; 77, 79; 78, identifier:vars; 79, argument_list; 79, 80; 80, identifier:kws; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:func; 84, call; 84, 85; 84, 86; 85, identifier:getattr; 86, argument_list; 86, 87; 86, 88; 87, identifier:self; 88, boolean_operator:or; 88, 89; 88, 90; 89, identifier:cmd; 90, string:'main'; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:ret; 95, identifier:cmd; 96, call; 96, 97; 96, 98; 97, identifier:func; 98, argument_list; 98, 99; 99, dictionary_splat; 99, 100; 100, identifier:kws; 101, if_statement; 101, 102; 101, 105; 102, comparison_operator:not; 102, 103; 102, 104; 103, identifier:cmd; 104, identifier:info_parts; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:ts; 110, identifier:cmd; 111, call; 111, 112; 111, 113; 112, identifier:str; 113, argument_list; 113, 114; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:dt; 118, identifier:datetime; 119, identifier:now; 120, argument_list; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:exp; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:_experiment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:project_parts; 130, set; 130, 131; 131, string:'setup'; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:projectname; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:_projectname; 138, if_statement; 138, 139; 138, 158; 138, 187; 139, parenthesized_expression; 139, 140; 140, boolean_operator:and; 140, 141; 140, 151; 141, boolean_operator:and; 141, 142; 141, 145; 142, comparison_operator:is; 142, 143; 142, 144; 143, identifier:projectname; 144, None; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:project_parts; 148, identifier:intersection; 149, argument_list; 149, 150; 150, identifier:ts; 151, comparison_operator:in; 151, 152; 151, 153; 152, identifier:projectname; 153, attribute; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:config; 157, identifier:projects; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 172; 161, attribute; 161, 162; 161, 171; 162, subscript; 162, 163; 162, 170; 163, subscript; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:config; 168, identifier:projects; 169, identifier:projectname; 170, string:'timestamps'; 171, identifier:update; 172, argument_list; 172, 173; 173, dictionary_comprehension; 173, 174; 173, 179; 174, pair; 174, 175; 174, 176; 175, identifier:key; 176, subscript; 176, 177; 176, 178; 177, identifier:ts; 178, identifier:key; 179, for_in_clause; 179, 180; 179, 181; 180, identifier:key; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:project_parts; 184, identifier:intersection; 185, argument_list; 185, 186; 186, identifier:ts; 187, elif_clause; 187, 188; 187, 190; 187, 191; 188, not_operator; 188, 189; 189, identifier:ts; 190, comment; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:no_modification; 197, True; 198, if_statement; 198, 199; 198, 210; 199, boolean_operator:and; 199, 200; 199, 203; 200, comparison_operator:is; 200, 201; 200, 202; 201, identifier:exp; 202, None; 203, comparison_operator:in; 203, 204; 203, 205; 204, identifier:exp; 205, attribute; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:config; 209, identifier:experiments; 210, block; 210, 211; 210, 217; 210, 238; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:projectname; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:projectname; 217, try_statement; 217, 218; 217, 234; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:ts; 223, identifier:update; 224, argument_list; 224, 225; 225, subscript; 225, 226; 225, 233; 226, subscript; 226, 227; 226, 232; 227, attribute; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:config; 231, identifier:projects; 232, identifier:projectname; 233, string:'timestamps'; 234, except_clause; 234, 235; 234, 236; 235, identifier:KeyError; 236, block; 236, 237; 237, pass_statement; 238, if_statement; 238, 239; 238, 246; 239, not_operator; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:is_archived; 244, argument_list; 244, 245; 245, identifier:exp; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 260; 249, attribute; 249, 250; 249, 259; 250, subscript; 250, 251; 250, 258; 251, subscript; 251, 252; 251, 257; 252, attribute; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:config; 256, identifier:experiments; 257, identifier:exp; 258, string:'timestamps'; 259, identifier:update; 260, argument_list; 260, 261; 261, identifier:ts; 262, return_statement; 262, 263; 263, call; 263, 264; 263, 265; 264, identifier:Namespace; 265, argument_list; 265, 266; 266, dictionary_splat; 266, 267; 267, identifier:ret | def start(self, **kwargs):
"""
Start the commands of this organizer
Parameters
----------
``**kwargs``
Any keyword from the :attr:`commands` or :attr:`parser_commands`
attribute
Returns
-------
argparse.Namespace
The namespace with the commands as given in ``**kwargs`` and the
return values of the corresponding method"""
ts = {}
ret = {}
info_parts = {'info', 'get-value', 'get_value'}
for cmd in self.commands:
parser_cmd = self.parser_commands.get(cmd, cmd)
if parser_cmd in kwargs or cmd in kwargs:
kws = kwargs.get(cmd, kwargs.get(parser_cmd))
if isinstance(kws, Namespace):
kws = vars(kws)
func = getattr(self, cmd or 'main')
ret[cmd] = func(**kws)
if cmd not in info_parts:
ts[cmd] = str(dt.datetime.now())
exp = self._experiment
project_parts = {'setup'}
projectname = self._projectname
if (projectname is not None and project_parts.intersection(ts) and
projectname in self.config.projects):
self.config.projects[projectname]['timestamps'].update(
{key: ts[key] for key in project_parts.intersection(ts)})
elif not ts: # don't make modifications for info
self.no_modification = True
if exp is not None and exp in self.config.experiments:
projectname = self.projectname
try:
ts.update(self.config.projects[projectname]['timestamps'])
except KeyError:
pass
if not self.is_archived(exp):
self.config.experiments[exp]['timestamps'].update(ts)
return Namespace(**ret) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:app_main; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:experiment; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:last; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:new; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:verbose; 16, False; 17, default_parameter; 17, 18; 17, 19; 18, identifier:verbosity_level; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:no_modification; 22, False; 23, default_parameter; 23, 24; 23, 25; 24, identifier:match; 25, False; 26, block; 26, 27; 26, 29; 26, 96; 26, 153; 26, 191; 26, 225; 27, expression_statement; 27, 28; 28, comment; 29, if_statement; 29, 30; 29, 31; 30, identifier:match; 31, block; 31, 32; 31, 41; 31, 58; 31, 90; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:patt; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:re; 38, identifier:compile; 39, argument_list; 39, 40; 40, identifier:experiment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:matches; 44, call; 44, 45; 44, 46; 45, identifier:list; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:filter; 49, argument_list; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:patt; 52, identifier:search; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:config; 57, identifier:experiments; 58, if_statement; 58, 59; 58, 65; 58, 75; 59, comparison_operator:>; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:matches; 64, integer:1; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:ValueError; 69, argument_list; 69, 70; 70, binary_operator:%; 70, 71; 70, 72; 71, string:"Found multiple matches for %s: %s"; 72, tuple; 72, 73; 72, 74; 73, identifier:experiment; 74, identifier:matches; 75, elif_clause; 75, 76; 75, 82; 76, comparison_operator:==; 76, 77; 76, 81; 77, call; 77, 78; 77, 79; 78, identifier:len; 79, argument_list; 79, 80; 80, identifier:matches; 81, integer:0; 82, block; 82, 83; 83, raise_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:ValueError; 86, argument_list; 86, 87; 87, binary_operator:%; 87, 88; 87, 89; 88, string:"No experiment matches %s"; 89, identifier:experiment; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:experiment; 93, subscript; 93, 94; 93, 95; 94, identifier:matches; 95, integer:0; 96, if_statement; 96, 97; 96, 104; 96, 111; 96, 145; 97, boolean_operator:and; 97, 98; 97, 99; 98, identifier:last; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:config; 103, identifier:experiments; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:experiment; 110, None; 111, elif_clause; 111, 112; 111, 119; 112, boolean_operator:and; 112, 113; 112, 114; 113, identifier:new; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:config; 118, identifier:experiments; 119, block; 119, 120; 120, try_statement; 120, 121; 120, 135; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:experiment; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:utils; 130, identifier:get_next_name; 131, argument_list; 131, 132; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:experiment; 135, except_clause; 135, 136; 135, 137; 136, identifier:ValueError; 137, block; 137, 138; 138, raise_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:ValueError; 141, argument_list; 141, 142; 142, concatenated_string; 142, 143; 142, 144; 143, string:"Could not estimate an experiment id! Please use the "; 144, string:"experiment argument to provide an id."; 145, else_clause; 145, 146; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:_experiment; 152, identifier:experiment; 153, if_statement; 153, 154; 153, 155; 153, 162; 154, identifier:verbose; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:verbose; 159, attribute; 159, 160; 159, 161; 160, identifier:logging; 161, identifier:DEBUG; 162, elif_clause; 162, 163; 162, 164; 163, identifier:verbosity_level; 164, block; 164, 165; 165, if_statement; 165, 166; 165, 173; 165, 182; 166, comparison_operator:in; 166, 167; 166, 168; 167, identifier:verbosity_level; 168, list:['DEBUG', 'INFO', 'WARNING', 'ERROR']; 168, 169; 168, 170; 168, 171; 168, 172; 169, string:'DEBUG'; 170, string:'INFO'; 171, string:'WARNING'; 172, string:'ERROR'; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:verbose; 177, call; 177, 178; 177, 179; 178, identifier:getattr; 179, argument_list; 179, 180; 179, 181; 180, identifier:logging; 181, identifier:verbosity_level; 182, else_clause; 182, 183; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:verbose; 187, call; 187, 188; 187, 189; 188, identifier:int; 189, argument_list; 189, 190; 190, identifier:verbosity_level; 191, if_statement; 191, 192; 191, 193; 192, identifier:verbose; 193, block; 193, 194; 193, 216; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 214; 196, attribute; 196, 197; 196, 213; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:logging; 200, identifier:getLogger; 201, argument_list; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:utils; 205, identifier:get_toplevel_module; 206, argument_list; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:inspect; 210, identifier:getmodule; 211, argument_list; 211, 212; 212, identifier:self; 213, identifier:setLevel; 214, argument_list; 214, 215; 215, identifier:verbose; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:logger; 222, identifier:setLevel; 223, argument_list; 223, 224; 224, identifier:verbose; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:no_modification; 230, identifier:no_modification | def app_main(self, experiment=None, last=False, new=False,
verbose=False, verbosity_level=None, no_modification=False,
match=False):
"""
The main function for parsing global arguments
Parameters
----------
experiment: str
The id of the experiment to use
last: bool
If True, the last experiment is used
new: bool
If True, a new experiment is created
verbose: bool
Increase the verbosity level to DEBUG. See also `verbosity_level`
for a more specific determination of the verbosity
verbosity_level: str or int
The verbosity level to use. Either one of ``'DEBUG', 'INFO',
'WARNING', 'ERROR'`` or the corresponding integer (see pythons
logging module)
no_modification: bool
If True/set, no modifications in the configuration files will be
done
match: bool
If True/set, interprete `experiment` as a regular expression
(regex) und use the matching experiment"""
if match:
patt = re.compile(experiment)
matches = list(filter(patt.search, self.config.experiments))
if len(matches) > 1:
raise ValueError("Found multiple matches for %s: %s" % (
experiment, matches))
elif len(matches) == 0:
raise ValueError("No experiment matches %s" % experiment)
experiment = matches[0]
if last and self.config.experiments:
self.experiment = None
elif new and self.config.experiments:
try:
self.experiment = utils.get_next_name(self.experiment)
except ValueError:
raise ValueError(
"Could not estimate an experiment id! Please use the "
"experiment argument to provide an id.")
else:
self._experiment = experiment
if verbose:
verbose = logging.DEBUG
elif verbosity_level:
if verbosity_level in ['DEBUG', 'INFO', 'WARNING', 'ERROR']:
verbose = getattr(logging, verbosity_level)
else:
verbose = int(verbosity_level)
if verbose:
logging.getLogger(
utils.get_toplevel_module(inspect.getmodule(self))).setLevel(
verbose)
self.logger.setLevel(verbose)
self.no_modification = no_modification |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 28; 2, function_name:configure; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:global_config; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:project_config; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:ifile; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:forcing; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:serial; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:nprocs; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:update_from; 25, None; 26, dictionary_splat_pattern; 26, 27; 27, identifier:kwargs; 28, block; 28, 29; 28, 31; 28, 79; 28, 95; 28, 111; 28, 139; 28, 147; 29, expression_statement; 29, 30; 30, comment; 31, if_statement; 31, 32; 31, 33; 31, 42; 31, 65; 32, identifier:global_config; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:d; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:config; 41, identifier:global_config; 42, elif_clause; 42, 43; 42, 44; 43, identifier:project_config; 44, block; 44, 45; 44, 53; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:app_main; 50, argument_list; 50, 51; 51, dictionary_splat; 51, 52; 52, identifier:kwargs; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:d; 56, subscript; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:config; 61, identifier:projects; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:projectname; 65, else_clause; 65, 66; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:d; 70, subscript; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:config; 75, identifier:experiments; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:experiment; 79, if_statement; 79, 80; 79, 83; 80, comparison_operator:is; 80, 81; 80, 82; 81, identifier:ifile; 82, None; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:d; 88, string:'input'; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:osp; 92, identifier:abspath; 93, argument_list; 93, 94; 94, identifier:ifile; 95, if_statement; 95, 96; 95, 99; 96, comparison_operator:is; 96, 97; 96, 98; 97, identifier:forcing; 98, None; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:d; 104, string:'forcing'; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:osp; 108, identifier:abspath; 109, argument_list; 109, 110; 110, identifier:forcing; 111, if_statement; 111, 112; 111, 115; 112, comparison_operator:is; 112, 113; 112, 114; 113, identifier:update_from; 114, None; 115, block; 115, 116; 116, with_statement; 116, 117; 116, 126; 117, with_clause; 117, 118; 118, with_item; 118, 119; 119, as_pattern; 119, 120; 119, 124; 120, call; 120, 121; 120, 122; 121, identifier:open; 122, argument_list; 122, 123; 123, string:'update_from'; 124, as_pattern_target; 124, 125; 125, identifier:f; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:d; 131, identifier:update; 132, argument_list; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:yaml; 136, identifier:load; 137, argument_list; 137, 138; 138, identifier:f; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:global_config; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:config; 146, identifier:global_config; 147, if_statement; 147, 148; 147, 149; 147, 156; 148, identifier:serial; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:global_config; 154, string:'serial'; 155, True; 156, elif_clause; 156, 157; 156, 158; 157, identifier:nprocs; 158, block; 158, 159; 158, 171; 158, 177; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:nprocs; 162, conditional_expression:if; 162, 163; 162, 167; 162, 170; 163, call; 163, 164; 163, 165; 164, identifier:int; 165, argument_list; 165, 166; 166, identifier:nprocs; 167, comparison_operator:!=; 167, 168; 167, 169; 168, identifier:nprocs; 169, string:'all'; 170, identifier:nprocs; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:global_config; 175, string:'serial'; 176, False; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, subscript; 179, 180; 179, 181; 180, identifier:global_config; 181, string:'nprocs'; 182, identifier:nprocs | def configure(self, global_config=False, project_config=False, ifile=None,
forcing=None, serial=False, nprocs=None, update_from=None,
**kwargs):
"""
Configure the project and experiments
Parameters
----------
global_config: bool
If True/set, the configuration are applied globally (already
existing and configured experiments are not impacted)
project_config: bool
Apply the configuration on the entire project instance instead of
only the single experiment (already existing and configured
experiments are not impacted)
ifile: str
The input file for the project. Must be a netCDF file with
population data
forcing: str
The input file for the project containing variables with population
evolution information. Possible variables in the netCDF file are
*movement* containing the number of people to move and *change*
containing the population change (positive or negative)
serial: bool
Do the parameterization always serial (i.e. not in parallel on
multiple processors). Does automatically impact global settings
nprocs: int or 'all'
Maximum number of processes to when making the parameterization in
parallel. Does automatically impact global settings and disables
`serial`
update_from: str
Path to a yaml configuration file to update the specified
configuration with it
``**kwargs``
Other keywords for the :meth:`app_main` method"""
if global_config:
d = self.config.global_config
elif project_config:
self.app_main(**kwargs)
d = self.config.projects[self.projectname]
else:
d = self.config.experiments[self.experiment]
if ifile is not None:
d['input'] = osp.abspath(ifile)
if forcing is not None:
d['forcing'] = osp.abspath(forcing)
if update_from is not None:
with open('update_from') as f:
d.update(yaml.load(f))
global_config = self.config.global_config
if serial:
global_config['serial'] = True
elif nprocs:
nprocs = int(nprocs) if nprocs != 'all' else nprocs
global_config['serial'] = False
global_config['nprocs'] = nprocs |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:generate_basic_type_docs; 3, parameters; 3, 4; 3, 5; 4, identifier:fields; 5, identifier:existing_types; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 12; 6, 138; 6, 142; 6, 143; 6, 144; 6, 345; 6, 367; 6, 374; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:raml_built_in_types; 15, dictionary; 15, 16; 15, 22; 15, 28; 15, 38; 15, 44; 15, 54; 15, 60; 15, 70; 15, 82; 15, 88; 15, 94; 15, 104; 15, 110; 15, 116; 15, 122; 15, 128; 16, pair; 16, 17; 16, 18; 17, string:"any"; 18, dictionary; 18, 19; 19, pair; 19, 20; 19, 21; 20, string:"parent"; 21, None; 22, pair; 22, 23; 22, 24; 23, string:"time-only"; 24, dictionary; 24, 25; 25, pair; 25, 26; 25, 27; 26, string:"parent"; 27, string:"any"; 28, pair; 28, 29; 28, 30; 29, string:"datetime"; 30, dictionary; 30, 31; 30, 34; 31, pair; 31, 32; 31, 33; 32, string:"parent"; 33, string:"any"; 34, pair; 34, 35; 34, 36; 35, string:"pale_children"; 36, list:["timestamp"]; 36, 37; 37, string:"timestamp"; 38, pair; 38, 39; 38, 40; 39, string:"datetime-only"; 40, dictionary; 40, 41; 41, pair; 41, 42; 41, 43; 42, string:"parent"; 43, string:"any"; 44, pair; 44, 45; 44, 46; 45, string:"date-only"; 46, dictionary; 46, 47; 46, 50; 47, pair; 47, 48; 47, 49; 48, string:"parent"; 49, string:"any"; 50, pair; 50, 51; 50, 52; 51, string:"pale_children"; 52, list:["date"]; 52, 53; 53, string:"date"; 54, pair; 54, 55; 54, 56; 55, string:"number"; 56, dictionary; 56, 57; 57, pair; 57, 58; 57, 59; 58, string:"parent"; 59, string:"any"; 60, pair; 60, 61; 60, 62; 61, string:"boolean"; 62, dictionary; 62, 63; 62, 66; 63, pair; 63, 64; 63, 65; 64, string:"parent"; 65, string:"any"; 66, pair; 66, 67; 66, 68; 67, string:"pale_children"; 68, list:["boolean"]; 68, 69; 69, string:"boolean"; 70, pair; 70, 71; 70, 72; 71, string:"string"; 72, dictionary; 72, 73; 72, 76; 73, pair; 73, 74; 73, 75; 74, string:"parent"; 75, string:"any"; 76, pair; 76, 77; 76, 78; 77, string:"pale_children"; 78, list:["url", "string", "uri"]; 78, 79; 78, 80; 78, 81; 79, string:"url"; 80, string:"string"; 81, string:"uri"; 82, pair; 82, 83; 82, 84; 83, string:"null"; 84, dictionary; 84, 85; 85, pair; 85, 86; 85, 87; 86, string:"parent"; 87, string:"any"; 88, pair; 88, 89; 88, 90; 89, string:"file"; 90, dictionary; 90, 91; 91, pair; 91, 92; 91, 93; 92, string:"parent"; 93, string:"any"; 94, pair; 94, 95; 94, 96; 95, string:"array"; 96, dictionary; 96, 97; 96, 100; 97, pair; 97, 98; 97, 99; 98, string:"parent"; 99, string:"any"; 100, pair; 100, 101; 100, 102; 101, string:"pale_children"; 102, list:["list"]; 102, 103; 103, string:"list"; 104, pair; 104, 105; 104, 106; 105, string:"object"; 106, dictionary; 106, 107; 107, pair; 107, 108; 107, 109; 108, string:"parent"; 109, string:"any"; 110, pair; 110, 111; 110, 112; 111, string:"union"; 112, dictionary; 112, 113; 113, pair; 113, 114; 113, 115; 114, string:"parent"; 115, string:"any"; 116, pair; 116, 117; 116, 118; 117, string:"XSD Schema"; 118, dictionary; 118, 119; 119, pair; 119, 120; 119, 121; 120, string:"parent"; 121, string:"any"; 122, pair; 122, 123; 122, 124; 123, string:"JSON Schema"; 124, dictionary; 124, 125; 125, pair; 125, 126; 125, 127; 126, string:"parent"; 127, string:"any"; 128, pair; 128, 129; 128, 130; 129, string:"integer"; 130, dictionary; 130, 131; 130, 134; 131, pair; 131, 132; 131, 133; 132, string:"parent"; 133, string:"number"; 134, pair; 134, 135; 134, 136; 135, string:"pale_children"; 136, list:["integer"]; 136, 137; 137, string:"integer"; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:basic_types; 141, dictionary; 142, comment; 143, comment; 144, for_statement; 144, 145; 144, 146; 144, 147; 144, 148; 145, identifier:field; 146, identifier:fields; 147, comment; 148, block; 148, 149; 149, if_statement; 149, 150; 149, 155; 150, call; 150, 151; 150, 152; 151, identifier:hasattr; 152, argument_list; 152, 153; 152, 154; 153, identifier:field; 154, string:"value_type"; 155, block; 155, 156; 155, 168; 155, 169; 155, 170; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:type_name; 159, call; 159, 160; 159, 165; 160, attribute; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:field; 163, identifier:value_type; 164, identifier:replace; 165, argument_list; 165, 166; 165, 167; 166, string:" "; 167, string:"_"; 168, comment; 169, comment; 170, if_statement; 170, 171; 170, 184; 171, boolean_operator:and; 171, 172; 171, 180; 171, 181; 172, boolean_operator:and; 172, 173; 172, 176; 172, 177; 173, comparison_operator:not; 173, 174; 173, 175; 174, identifier:type_name; 175, identifier:raml_built_in_types; 176, line_continuation:\; 177, comparison_operator:not; 177, 178; 177, 179; 178, identifier:type_name; 179, identifier:basic_types; 180, line_continuation:\; 181, comparison_operator:not; 181, 182; 181, 183; 182, identifier:type_name; 183, identifier:existing_types; 184, block; 184, 185; 184, 191; 184, 192; 184, 216; 184, 217; 184, 218; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:basic_types; 189, identifier:type_name; 190, dictionary; 191, comment; 192, if_statement; 192, 193; 192, 198; 193, call; 193, 194; 193, 195; 194, identifier:hasattr; 195, argument_list; 195, 196; 195, 197; 196, identifier:field; 197, string:"__doc__"; 198, block; 198, 199; 198, 208; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:modified_description; 202, call; 202, 203; 202, 204; 203, identifier:clean_description; 204, argument_list; 204, 205; 205, attribute; 205, 206; 205, 207; 206, identifier:field; 207, identifier:__doc__; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 215; 210, subscript; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:basic_types; 213, identifier:type_name; 214, string:"description"; 215, identifier:modified_description; 216, comment; 217, comment; 218, for_statement; 218, 219; 218, 220; 218, 221; 219, identifier:raml_type; 220, identifier:raml_built_in_types; 221, block; 221, 222; 222, if_statement; 222, 223; 222, 228; 222, 247; 223, comparison_operator:in; 223, 224; 223, 225; 224, string:"pale_children"; 225, subscript; 225, 226; 225, 227; 226, identifier:raml_built_in_types; 227, identifier:raml_type; 228, block; 228, 229; 229, if_statement; 229, 230; 229, 237; 230, comparison_operator:in; 230, 231; 230, 232; 231, identifier:type_name; 232, subscript; 232, 233; 232, 236; 233, subscript; 233, 234; 233, 235; 234, identifier:raml_built_in_types; 235, identifier:raml_type; 236, string:"pale_children"; 237, block; 237, 238; 237, 246; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 245; 240, subscript; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:basic_types; 243, identifier:type_name; 244, string:"type"; 245, identifier:raml_type; 246, break_statement; 247, else_clause; 247, 248; 247, 249; 247, 250; 248, comment; 249, comment; 250, block; 250, 251; 251, if_statement; 251, 252; 251, 261; 251, 305; 251, 306; 252, boolean_operator:and; 252, 253; 252, 258; 253, call; 253, 254; 253, 255; 254, identifier:hasattr; 255, argument_list; 255, 256; 255, 257; 256, identifier:field; 257, string:"is_list"; 258, attribute; 258, 259; 258, 260; 259, identifier:field; 260, identifier:is_list; 261, block; 261, 262; 261, 270; 261, 271; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 269; 264, subscript; 264, 265; 264, 268; 265, subscript; 265, 266; 265, 267; 266, identifier:basic_types; 267, identifier:type_name; 268, string:"type"; 269, string:"array"; 270, comment; 271, if_statement; 271, 272; 271, 283; 271, 294; 271, 295; 272, boolean_operator:and; 272, 273; 272, 278; 273, call; 273, 274; 273, 275; 274, identifier:hasattr; 275, argument_list; 275, 276; 275, 277; 276, identifier:field; 277, string:"list_item_type"; 278, comparison_operator:!=; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:field; 281, identifier:list_item_type; 282, None; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 291; 286, subscript; 286, 287; 286, 290; 287, subscript; 287, 288; 287, 289; 288, identifier:basic_types; 289, identifier:type_name; 290, string:"items"; 291, attribute; 291, 292; 291, 293; 292, identifier:field; 293, identifier:list_item_type; 294, comment; 295, else_clause; 295, 296; 296, block; 296, 297; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 304; 299, subscript; 299, 300; 299, 303; 300, subscript; 300, 301; 300, 302; 301, identifier:basic_types; 302, identifier:type_name; 303, string:"items"; 304, string:"base"; 305, comment; 306, else_clause; 306, 307; 307, block; 307, 308; 307, 316; 307, 317; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:pale_parent_class; 311, subscript; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:field; 314, identifier:__mro__; 315, integer:1; 316, comment; 317, if_statement; 317, 318; 317, 323; 317, 332; 317, 333; 318, comparison_operator:==; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:pale_parent_class; 321, identifier:__name__; 322, string:"object"; 323, block; 323, 324; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 331; 326, subscript; 326, 327; 326, 330; 327, subscript; 327, 328; 327, 329; 328, identifier:basic_types; 329, identifier:type_name; 330, string:"type"; 331, string:"object"; 332, comment; 333, else_clause; 333, 334; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 342; 337, subscript; 337, 338; 337, 341; 338, subscript; 338, 339; 338, 340; 339, identifier:basic_types; 340, identifier:type_name; 341, string:"type"; 342, attribute; 342, 343; 342, 344; 343, identifier:pale_parent_class; 344, identifier:value_type; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:ordered_basic_types; 348, call; 348, 349; 348, 350; 349, identifier:OrderedDict; 350, argument_list; 350, 351; 351, call; 351, 352; 351, 353; 352, identifier:sorted; 353, argument_list; 353, 354; 353, 359; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:basic_types; 357, identifier:items; 358, argument_list; 359, keyword_argument; 359, 360; 359, 361; 360, identifier:key; 361, lambda; 361, 362; 361, 364; 362, lambda_parameters; 362, 363; 363, identifier:t; 364, subscript; 364, 365; 364, 366; 365, identifier:t; 366, integer:0; 367, expression_statement; 367, 368; 368, assignment; 368, 369; 368, 370; 369, identifier:basic_docs; 370, call; 370, 371; 370, 372; 371, identifier:generate_type_docs; 372, argument_list; 372, 373; 373, identifier:ordered_basic_types; 374, return_statement; 374, 375; 375, tuple; 375, 376; 375, 377; 376, identifier:basic_docs; 377, identifier:basic_types | def generate_basic_type_docs(fields, existing_types):
"""Map resource types to their RAML equivalents.
Expects fields to be a list of modules - each module would be something like pale.fields.
Expects existing_types to be a list of dict of existing types, which will take precedence
and prevent a new type with the same name from being added.
For more on RAML built-in-types, see:
https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md#built-in-types
"""
# These types are standard in RAML 1.0
# They should not be defined in the RAML file that we return
# We will inherit from them in the types we define
raml_built_in_types = {
"any": {
"parent": None,
},
"time-only": {
"parent": "any",
},
"datetime": {
"parent": "any",
"pale_children": ["timestamp"],
},
"datetime-only": {
"parent": "any",
},
"date-only": {
"parent": "any",
"pale_children": ["date"],
},
"number": {
"parent": "any",
},
"boolean": {
"parent": "any",
"pale_children": ["boolean"]
},
"string": {
"parent": "any",
"pale_children": ["url", "string", "uri"],
},
"null": {
"parent": "any",
},
"file": {
"parent": "any",
},
"array": {
"parent": "any",
"pale_children": ["list"],
},
"object": {
"parent": "any",
},
"union": {
"parent": "any",
},
"XSD Schema": {
"parent": "any",
},
"JSON Schema": {
"parent": "any",
},
"integer": {
"parent": "number",
"pale_children": ["integer"],
},
}
basic_types = {}
# Find all classes defined in a set of resources and build up an object with
# the relevant details of the basic types
for field in fields:
# if this is a Pale type, it will have a 'value_type' property,
if hasattr(field, "value_type"):
type_name = field.value_type.replace(" ", "_")
# add this type only if it is not in the built-in raml types and we have
# not added it yet
if type_name not in raml_built_in_types \
and type_name not in basic_types \
and type_name not in existing_types:
basic_types[type_name] = {}
# strip newlines and leading whitespaces from doc string, then add as description
if hasattr(field, "__doc__"):
modified_description = clean_description(field.__doc__)
basic_types[type_name]["description"] = modified_description
# if this type is listed as the child of a built-in raml type,
# use the raml type as its parent type
for raml_type in raml_built_in_types:
if "pale_children" in raml_built_in_types[raml_type]:
if type_name in raml_built_in_types[raml_type]["pale_children"]:
basic_types[type_name]["type"] = raml_type
break
else:
# if this is not the child of a built-in raml type
# and if this type is a list composed of other items:
if hasattr(field, "is_list") and field.is_list:
basic_types[type_name]["type"] = "array"
# and the type is defined, use the defined type
if hasattr(field, "list_item_type") and field.list_item_type != None:
basic_types[type_name]["items"] = field.list_item_type
# otherwise, use the base type
else:
basic_types[type_name]["items"] = "base"
# otherwise use the pale parent class as its type
else:
pale_parent_class = field.__mro__[1]
# if we are at the base class, inherit from the RAML "object" type
if pale_parent_class.__name__ == "object":
basic_types[type_name]["type"] = "object"
# otherwise, inherit from the named parent
else:
basic_types[type_name]["type"] = pale_parent_class.value_type
ordered_basic_types = OrderedDict(sorted(basic_types.items(), key=lambda t: t[0]))
basic_docs = generate_type_docs(ordered_basic_types)
return (basic_docs, basic_types) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:generate_doc_dict; 3, parameters; 3, 4; 3, 5; 4, identifier:module; 5, identifier:user; 6, block; 6, 7; 6, 9; 6, 18; 6, 30; 6, 37; 6, 53; 6, 57; 6, 105; 6, 112; 6, 128; 7, expression_statement; 7, 8; 8, comment; 9, import_from_statement; 9, 10; 9, 12; 9, 14; 9, 16; 10, dotted_name; 10, 11; 11, identifier:pale; 12, dotted_name; 12, 13; 13, identifier:extract_endpoints; 14, dotted_name; 14, 15; 15, identifier:extract_resources; 16, dotted_name; 16, 17; 17, identifier:is_pale_module; 18, if_statement; 18, 19; 18, 24; 19, not_operator; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:is_pale_module; 22, argument_list; 22, 23; 23, identifier:module; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:ValueError; 28, argument_list; 28, 29; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:module_endpoints; 33, call; 33, 34; 33, 35; 34, identifier:extract_endpoints; 35, argument_list; 35, 36; 36, identifier:module; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:ep_doc; 40, dictionary_comprehension; 40, 41; 40, 49; 41, pair; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:ep; 44, identifier:_route_name; 45, call; 45, 46; 45, 47; 46, identifier:document_endpoint; 47, argument_list; 47, 48; 48, identifier:ep; 49, for_in_clause; 49, 50; 49, 51; 49, 52; 50, identifier:ep; 51, line_continuation:\; 52, identifier:module_endpoints; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:ep_doc_filtered; 56, dictionary; 57, for_statement; 57, 58; 57, 59; 57, 60; 57, 61; 57, 62; 57, 63; 57, 64; 58, identifier:endpoint; 59, identifier:ep_doc; 60, comment; 61, comment; 62, comment; 63, comment; 64, block; 64, 65; 65, if_statement; 65, 66; 65, 96; 66, boolean_operator:or; 66, 67; 66, 85; 66, 86; 67, boolean_operator:and; 67, 68; 67, 82; 68, boolean_operator:and; 68, 69; 68, 79; 69, comparison_operator:!=; 69, 70; 69, 78; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:ep_doc; 74, identifier:endpoint; 75, identifier:get; 76, argument_list; 76, 77; 77, string:"requires_permission"; 78, None; 79, comparison_operator:!=; 79, 80; 79, 81; 80, identifier:user; 81, None; 82, attribute; 82, 83; 82, 84; 83, identifier:user; 84, identifier:is_admin; 85, line_continuation:\; 86, comparison_operator:==; 86, 87; 86, 95; 87, call; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:ep_doc; 91, identifier:endpoint; 92, identifier:get; 93, argument_list; 93, 94; 94, string:"requires_permission"; 95, None; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:ep_doc_filtered; 101, identifier:endpoint; 102, subscript; 102, 103; 102, 104; 103, identifier:ep_doc; 104, identifier:endpoint; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:module_resources; 108, call; 108, 109; 108, 110; 109, identifier:extract_resources; 110, argument_list; 110, 111; 111, identifier:module; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:res_doc; 115, dictionary_comprehension; 115, 116; 115, 124; 116, pair; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:r; 119, identifier:_value_type; 120, call; 120, 121; 120, 122; 121, identifier:document_resource; 122, argument_list; 122, 123; 123, identifier:r; 124, for_in_clause; 124, 125; 124, 126; 124, 127; 125, identifier:r; 126, line_continuation:\; 127, identifier:module_resources; 128, return_statement; 128, 129; 129, dictionary; 129, 130; 129, 133; 130, pair; 130, 131; 130, 132; 131, string:'endpoints'; 132, identifier:ep_doc_filtered; 133, pair; 133, 134; 133, 135; 134, string:'resources'; 135, identifier:res_doc | def generate_doc_dict(module, user):
"""Compile a Pale module's documentation into a python dictionary.
The returned dictionary is suitable to be rendered by a JSON formatter,
or passed to a template engine, or manipulated in some other way.
"""
from pale import extract_endpoints, extract_resources, is_pale_module
if not is_pale_module(module):
raise ValueError(
"""The passed in `module` (%s) is not a pale module. `paledoc`
only works on modules with a `_module_type` set to equal
`pale.ImplementationModule`.""")
module_endpoints = extract_endpoints(module)
ep_doc = { ep._route_name: document_endpoint(ep) for ep \
in module_endpoints }
ep_doc_filtered = {}
for endpoint in ep_doc:
# check if user has permission to view this endpoint
# this is currently an on/off switch: if any endpoint has a "@requires_permission"
# decorator, user.is_admin must be True for the user to see documentation
# @TODO - make this permission more granular if necessary
if ep_doc[endpoint].get("requires_permission") != None and user != None and user.is_admin or \
ep_doc[endpoint].get("requires_permission") == None:
ep_doc_filtered[endpoint] = ep_doc[endpoint]
module_resources = extract_resources(module)
res_doc = { r._value_type: document_resource(r) for r \
in module_resources }
return {'endpoints': ep_doc_filtered,
'resources': res_doc} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_driver; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:desired_capabilities; 7, None; 8, block; 8, 9; 8, 11; 8, 17; 8, 28; 8, 35; 8, 43; 8, 47; 8, 344; 8, 345; 8, 346; 8, 347; 8, 348; 8, 388; 8, 397; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:override_caps; 14, boolean_operator:or; 14, 15; 14, 16; 15, identifier:desired_capabilities; 16, dictionary; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 18, 21; 19, identifier:desired_capabilities; 20, line_continuation:\; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:config; 26, identifier:make_selenium_desired_capabilities; 27, argument_list; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:desired_capabilities; 32, identifier:update; 33, argument_list; 33, 34; 34, identifier:override_caps; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:browser_string; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:config; 42, identifier:browser; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:chromedriver_version; 46, None; 47, if_statement; 47, 48; 47, 51; 47, 103; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:remote; 51, block; 51, 52; 51, 63; 51, 64; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:driver; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:remote_service; 60, identifier:build_driver; 61, argument_list; 61, 62; 62, identifier:desired_capabilities; 63, comment; 64, if_statement; 64, 65; 64, 77; 65, boolean_operator:and; 65, 66; 65, 69; 65, 70; 66, comparison_operator:==; 66, 67; 66, 68; 67, identifier:browser_string; 68, string:"CHROME"; 69, line_continuation:\; 70, comparison_operator:==; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:remote_service; 75, identifier:name; 76, string:"saucelabs"; 77, block; 77, 78; 77, 89; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 79, 82; 80, identifier:chromedriver_version; 81, line_continuation:\; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:desired_capabilities; 85, identifier:get; 86, argument_list; 86, 87; 86, 88; 87, string:"chromedriver-version"; 88, None; 89, if_statement; 89, 90; 89, 93; 90, comparison_operator:is; 90, 91; 90, 92; 91, identifier:chromedriver_version; 92, None; 93, block; 93, 94; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:ValueError; 97, argument_list; 97, 98; 98, concatenated_string; 98, 99; 98, 100; 98, 101; 98, 102; 99, string:"when using Chrome, you must set a "; 100, string:"``chromedriver-version`` capability so that Selenic "; 101, string:"can detect which version of Chromedriver will "; 102, string:"be used."; 103, else_clause; 103, 104; 104, block; 104, 105; 104, 282; 104, 283; 104, 292; 104, 306; 104, 328; 105, if_statement; 105, 106; 105, 109; 105, 190; 105, 240; 105, 253; 105, 266; 106, comparison_operator:==; 106, 107; 106, 108; 107, identifier:browser_string; 108, string:"CHROME"; 109, block; 109, 110; 109, 118; 109, 157; 109, 168; 109, 183; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:chromedriver_path; 113, subscript; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:local_conf; 117, string:"CHROMEDRIVER_PATH"; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:driver; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:webdriver; 124, identifier:Chrome; 125, argument_list; 125, 126; 125, 127; 125, 137; 125, 140; 125, 147; 126, identifier:chromedriver_path; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:chrome_options; 129, call; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:local_conf; 134, identifier:get; 135, argument_list; 135, 136; 136, string:"CHROME_OPTIONS"; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:desired_capabilities; 139, identifier:desired_capabilities; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:service_log_path; 142, subscript; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:local_conf; 146, string:"SERVICE_LOG_PATH"; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:service_args; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:local_conf; 154, identifier:get; 155, argument_list; 155, 156; 156, string:"SERVICE_ARGS"; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:version_line; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:subprocess; 163, identifier:check_output; 164, argument_list; 164, 165; 165, list:[chromedriver_path, "--version"]; 165, 166; 165, 167; 166, identifier:chromedriver_path; 167, string:"--version"; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:version_str; 171, call; 171, 172; 171, 181; 172, attribute; 172, 173; 172, 180; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:re; 176, identifier:match; 177, argument_list; 177, 178; 177, 179; 178, string:ur"^ChromeDriver (\d+\.\d+)"; 179, identifier:version_line; 180, identifier:group; 181, argument_list; 181, 182; 182, integer:1; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:chromedriver_version; 186, call; 186, 187; 186, 188; 187, identifier:StrictVersion; 188, argument_list; 188, 189; 189, identifier:version_str; 190, elif_clause; 190, 191; 190, 194; 191, comparison_operator:==; 191, 192; 191, 193; 192, identifier:browser_string; 193, string:"FIREFOX"; 194, block; 194, 195; 194, 211; 194, 227; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:profile; 198, boolean_operator:or; 198, 199; 198, 207; 198, 208; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:local_conf; 204, identifier:get; 205, argument_list; 205, 206; 206, string:"FIREFOX_PROFILE"; 207, line_continuation:\; 208, call; 208, 209; 208, 210; 209, identifier:FirefoxProfile; 210, argument_list; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:binary; 214, boolean_operator:or; 214, 215; 214, 223; 214, 224; 215, call; 215, 216; 215, 221; 216, attribute; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:self; 219, identifier:local_conf; 220, identifier:get; 221, argument_list; 221, 222; 222, string:"FIREFOX_BINARY"; 223, line_continuation:\; 224, call; 224, 225; 224, 226; 225, identifier:FirefoxBinary; 226, argument_list; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:driver; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:webdriver; 233, identifier:Firefox; 234, argument_list; 234, 235; 234, 236; 234, 237; 235, identifier:profile; 236, identifier:binary; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:capabilities; 239, identifier:desired_capabilities; 240, elif_clause; 240, 241; 240, 244; 241, comparison_operator:==; 241, 242; 241, 243; 242, identifier:browser_string; 243, string:"INTERNETEXPLORER"; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:driver; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:webdriver; 251, identifier:Ie; 252, argument_list; 253, elif_clause; 253, 254; 253, 257; 254, comparison_operator:==; 254, 255; 254, 256; 255, identifier:browser_string; 256, string:"OPERA"; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:driver; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:webdriver; 264, identifier:Opera; 265, argument_list; 266, else_clause; 266, 267; 266, 268; 266, 269; 266, 270; 266, 271; 266, 272; 266, 273; 266, 274; 267, comment; 268, comment; 269, comment; 270, comment; 271, comment; 272, comment; 273, comment; 274, block; 274, 275; 275, raise_statement; 275, 276; 276, call; 276, 277; 276, 278; 277, identifier:ValueError; 278, argument_list; 278, 279; 279, binary_operator:+; 279, 280; 279, 281; 280, string:"can't start a local "; 281, identifier:browser_string; 282, comment; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:driver_caps; 286, call; 286, 287; 286, 288; 287, identifier:NormalizedCapabilities; 288, argument_list; 288, 289; 289, attribute; 289, 290; 289, 291; 290, identifier:driver; 291, identifier:desired_capabilities; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 293, 296; 294, identifier:browser_version; 295, line_continuation:\; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:re; 299, identifier:sub; 300, argument_list; 300, 301; 300, 302; 300, 303; 301, string:r"\..*$"; 302, string:""; 303, subscript; 303, 304; 303, 305; 304, identifier:driver_caps; 305, string:"browserVersion"; 306, if_statement; 306, 307; 306, 320; 307, comparison_operator:!=; 307, 308; 307, 315; 308, call; 308, 309; 308, 314; 309, attribute; 309, 310; 309, 313; 310, subscript; 310, 311; 310, 312; 311, identifier:driver_caps; 312, string:"platformName"; 313, identifier:upper; 314, argument_list; 315, attribute; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:self; 318, identifier:config; 319, identifier:platform; 320, block; 320, 321; 321, raise_statement; 321, 322; 322, call; 322, 323; 322, 324; 323, identifier:ValueError; 324, argument_list; 324, 325; 325, concatenated_string; 325, 326; 325, 327; 326, string:"the platform you want is not the one "; 327, string:"you are running selenic on"; 328, if_statement; 328, 329; 328, 336; 329, comparison_operator:!=; 329, 330; 329, 331; 330, identifier:browser_version; 331, attribute; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:self; 334, identifier:config; 335, identifier:version; 336, block; 336, 337; 337, raise_statement; 337, 338; 338, call; 338, 339; 338, 340; 339, identifier:ValueError; 340, argument_list; 340, 341; 341, concatenated_string; 341, 342; 341, 343; 342, string:"the version installed is not the one "; 343, string:"you wanted"; 344, comment; 345, comment; 346, comment; 347, comment; 348, if_statement; 348, 349; 348, 374; 348, 375; 349, boolean_operator:or; 349, 350; 349, 362; 349, 363; 350, parenthesized_expression; 350, 351; 351, boolean_operator:and; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:self; 354, identifier:remote_service; 355, comparison_operator:==; 355, 356; 355, 361; 356, attribute; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:self; 359, identifier:remote_service; 360, identifier:name; 361, string:"browserstack"; 362, line_continuation:\; 363, parenthesized_expression; 363, 364; 364, boolean_operator:and; 364, 365; 364, 368; 365, comparison_operator:is; 365, 366; 365, 367; 366, identifier:chromedriver_version; 367, None; 368, comparison_operator:>; 368, 369; 368, 370; 369, identifier:chromedriver_version; 370, call; 370, 371; 370, 372; 371, identifier:StrictVersion; 372, argument_list; 372, 373; 373, string:"2.13"; 374, comment; 375, block; 375, 376; 375, 380; 375, 381; 376, expression_statement; 376, 377; 377, call; 377, 378; 377, 379; 378, identifier:chromedriver_element_center_patch; 379, argument_list; 380, comment; 381, expression_statement; 381, 382; 382, call; 382, 383; 382, 384; 383, identifier:setattr; 384, argument_list; 384, 385; 384, 386; 384, 387; 385, identifier:driver; 386, identifier:CHROMEDRIVER_ELEMENT_CENTER_PATCH_FLAG; 387, True; 388, expression_statement; 388, 389; 389, assignment; 389, 390; 389, 391; 390, identifier:driver; 391, call; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:self; 394, identifier:patch; 395, argument_list; 395, 396; 396, identifier:driver; 397, return_statement; 397, 398; 398, identifier:driver | def get_driver(self, desired_capabilities=None):
"""
Creates a Selenium driver on the basis of the configuration file
upon which this object was created.
:param desired_capabilities: Capabilities that the caller
desires to override. This have priority over those
capabilities that are set by the configuration file passed
to the builder.
:type desired_capabilities: class:`dict`
:returns: A driver.
:raises ValueError: When it can't figure out how to create a
browser as specified by the BROWSER
configuration variable.
"""
override_caps = desired_capabilities or {}
desired_capabilities = \
self.config.make_selenium_desired_capabilities()
desired_capabilities.update(override_caps)
browser_string = self.config.browser
chromedriver_version = None
if self.remote:
driver = self.remote_service.build_driver(desired_capabilities)
# There is no equivalent for BrowserStack.
if browser_string == "CHROME" and \
self.remote_service.name == "saucelabs":
chromedriver_version = \
desired_capabilities.get("chromedriver-version", None)
if chromedriver_version is None:
raise ValueError(
"when using Chrome, you must set a "
"``chromedriver-version`` capability so that Selenic "
"can detect which version of Chromedriver will "
"be used.")
else:
if browser_string == "CHROME":
chromedriver_path = self.local_conf["CHROMEDRIVER_PATH"]
driver = webdriver.Chrome(
chromedriver_path,
chrome_options=self.local_conf.get("CHROME_OPTIONS"),
desired_capabilities=desired_capabilities,
service_log_path=self.local_conf["SERVICE_LOG_PATH"],
service_args=self.local_conf.get("SERVICE_ARGS"))
version_line = subprocess.check_output(
[chromedriver_path, "--version"])
version_str = re.match(ur"^ChromeDriver (\d+\.\d+)",
version_line).group(1)
chromedriver_version = StrictVersion(version_str)
elif browser_string == "FIREFOX":
profile = self.local_conf.get("FIREFOX_PROFILE") or \
FirefoxProfile()
binary = self.local_conf.get("FIREFOX_BINARY") or \
FirefoxBinary()
driver = webdriver.Firefox(profile, binary,
capabilities=desired_capabilities)
elif browser_string == "INTERNETEXPLORER":
driver = webdriver.Ie()
elif browser_string == "OPERA":
driver = webdriver.Opera()
else:
# SAFARI
# HTMLUNIT
# HTMLUNITWITHJS
# IPHONE
# IPAD
# ANDROID
# PHANTOMJS
raise ValueError("can't start a local " + browser_string)
# Check that what we get is what the config wanted...
driver_caps = NormalizedCapabilities(driver.desired_capabilities)
browser_version = \
re.sub(r"\..*$", "", driver_caps["browserVersion"])
if driver_caps["platformName"].upper() != self.config.platform:
raise ValueError("the platform you want is not the one "
"you are running selenic on")
if browser_version != self.config.version:
raise ValueError("the version installed is not the one "
"you wanted")
# On BrowserStack we cannot set the version of chromedriver or
# query it. So we make the reasonable assuption that the
# version of chromedriver is greater than 2.13. (There have
# been at least 7 releases after 2.13 at the time of writing.)
if (self.remote_service and
self.remote_service.name == "browserstack") or \
(chromedriver_version is not None and
chromedriver_version > StrictVersion("2.13")):
# We patch ActionChains.
chromedriver_element_center_patch()
# We need to mark the driver as needing the patch.
setattr(driver, CHROMEDRIVER_ELEMENT_CENTER_PATCH_FLAG, True)
driver = self.patch(driver)
return driver |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:load; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:typedef; 6, identifier:value; 7, dictionary_splat_pattern; 7, 8; 8, identifier:kwargs; 9, block; 9, 10; 9, 12; 10, expression_statement; 10, 11; 11, comment; 12, try_statement; 12, 13; 12, 22; 12, 35; 13, block; 13, 14; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:bound_type; 17, subscript; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:bound_types; 21, identifier:typedef; 22, except_clause; 22, 23; 22, 24; 23, identifier:KeyError; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:DeclareException; 28, argument_list; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, string:"Can't load unknown type {}"; 32, identifier:format; 33, argument_list; 33, 34; 34, identifier:typedef; 35, else_clause; 35, 36; 35, 37; 36, comment; 37, block; 37, 38; 38, return_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:bound_type; 42, string:"load"; 43, argument_list; 43, 44; 43, 45; 44, identifier:value; 45, dictionary_splat; 45, 46; 46, identifier:kwargs | def load(self, typedef, value, **kwargs):
"""
Return the result of the bound load method for a typedef
Looks up the load function that was bound to the engine for a typedef,
and return the result of passing the given `value` and any `context`
to that function.
Parameters
----------
typedef : :class:`~TypeDefinition`
The typedef whose bound load method should be used
value : object
The value to be passed into the bound load method
**kwargs : kwargs
Context for the value being loaded
Returns
-------
loaded_value : object
The return value of the load function for the input value
Raises
------
exc : :class:`KeyError`
If the input typedef is not bound to this engine
Example
-------
.. code-block:: python
class Account(TypeDefinition):
prefix = "::account"
def load(self, value, **context):
return value + Account.prefix
def dump(self, value, **context):
return value[:-len(Account.prefix)]
typedef = Account()
engine = TypeEngine("accounts")
engine.register(typedef)
engine.bind()
assert engine.dump(typedef, "Jill::account") == "Jill"
"""
try:
bound_type = self.bound_types[typedef]
except KeyError:
raise DeclareException(
"Can't load unknown type {}".format(typedef))
else:
# Don't need to try/catch since load/dump are bound together
return bound_type["load"](value, **kwargs) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:qteSetLexer; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:lexer; 6, block; 6, 7; 6, 9; 6, 33; 6, 34; 6, 40; 6, 63; 6, 64; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 24; 10, boolean_operator:and; 10, 11; 10, 15; 11, parenthesized_expression; 11, 12; 12, comparison_operator:is; 12, 13; 12, 14; 13, identifier:lexer; 14, None; 15, parenthesized_expression; 15, 16; 16, not_operator; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:issubclass; 19, argument_list; 19, 20; 19, 21; 20, identifier:lexer; 21, attribute; 21, 22; 21, 23; 22, identifier:Qsci; 23, identifier:QsciLexer; 24, block; 24, 25; 24, 32; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:QtmacsOtherError; 28, argument_list; 28, 29; 29, concatenated_string; 29, 30; 29, 31; 30, string:'lexer must be a class object and derived from'; 31, string:' <b>QsciLexer</b>'; 32, return_statement; 33, comment; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:qteLastLexer; 39, identifier:lexer; 40, if_statement; 40, 41; 40, 44; 40, 52; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:lexer; 43, None; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:setLexer; 50, argument_list; 50, 51; 51, None; 52, else_clause; 52, 53; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:setLexer; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:lexer; 62, argument_list; 63, comment; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:setMonospace; 69, argument_list | def qteSetLexer(self, lexer):
"""
Specify the lexer to use.
The only difference between this method and the native
``setLexer`` method is that expects ``lexer`` to be class, not
an instance. Another feature is that this method knows which
``lexer`` class was installed last, and this information can
be retrieved with ``qteLexer`` again.
|Args|
* ``lexer`` (**QsciLexer**): lexer class (*not* instance).
|Returns|
**None**
|Raises|
* **QtmacsOtherError** if lexer is not a class.
"""
if (lexer is not None) and (not issubclass(lexer, Qsci.QsciLexer)):
QtmacsOtherError('lexer must be a class object and derived from'
' <b>QsciLexer</b>')
return
# Install and backup the lexer class.
self.qteLastLexer = lexer
if lexer is None:
self.setLexer(None)
else:
self.setLexer(lexer())
# Make all fonts in the style mono space.
self.setMonospace() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:get_function_args; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:func; 5, default_parameter; 5, 6; 5, 7; 6, identifier:no_self; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:no_varargs; 10, False; 11, block; 11, 12; 11, 14; 11, 23; 11, 24; 11, 37; 11, 50; 11, 57; 11, 93; 11, 94; 11, 118; 11, 119; 11, 146; 11, 168; 11, 196; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:par_dict; 17, attribute; 17, 18; 17, 22; 18, call; 18, 19; 18, 20; 19, identifier:signature; 20, argument_list; 20, 21; 21, identifier:func; 22, identifier:parameters; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:pos; 27, lambda; 27, 28; 27, 30; 28, lambda_parameters; 28, 29; 29, identifier:x; 30, comparison_operator:==; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:x; 33, identifier:kind; 34, attribute; 34, 35; 34, 36; 35, identifier:Parameter; 36, identifier:VAR_POSITIONAL; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:kw; 40, lambda; 40, 41; 40, 43; 41, lambda_parameters; 41, 42; 42, identifier:x; 43, comparison_operator:==; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:x; 46, identifier:kind; 47, attribute; 47, 48; 47, 49; 48, identifier:Parameter; 49, identifier:VAR_KEYWORD; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:opts; 53, list:["", "*", "**"]; 53, 54; 53, 55; 53, 56; 54, string:""; 55, string:"*"; 56, string:"**"; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:args; 60, list_comprehension; 60, 61; 60, 84; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, string:"{prefix}{arg}"; 64, identifier:format; 65, argument_list; 65, 66; 65, 81; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:prefix; 68, subscript; 68, 69; 68, 70; 69, identifier:opts; 70, binary_operator:+; 70, 71; 70, 75; 71, call; 71, 72; 71, 73; 72, identifier:pos; 73, argument_list; 73, 74; 74, identifier:value; 75, binary_operator:*; 75, 76; 75, 77; 76, integer:2; 77, call; 77, 78; 77, 79; 78, identifier:kw; 79, argument_list; 79, 80; 80, identifier:value; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:arg; 83, identifier:par; 84, for_in_clause; 84, 85; 84, 88; 85, pattern_list; 85, 86; 85, 87; 86, identifier:par; 87, identifier:value; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:par_dict; 91, identifier:items; 92, argument_list; 93, comment; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:self_filtered_args; 97, parenthesized_expression; 97, 98; 98, conditional_expression:if; 98, 99; 98, 100; 98, 102; 99, identifier:args; 100, not_operator; 100, 101; 101, identifier:args; 102, parenthesized_expression; 102, 103; 103, subscript; 103, 104; 103, 105; 104, identifier:args; 105, slice; 105, 106; 105, 117; 106, conditional_expression:if; 106, 107; 106, 108; 106, 116; 107, integer:1; 108, boolean_operator:and; 108, 109; 108, 115; 109, parenthesized_expression; 109, 110; 110, comparison_operator:==; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:args; 113, integer:0; 114, string:"self"; 115, identifier:no_self; 116, integer:0; 117, colon; 118, comment; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:pos; 122, lambda; 122, 123; 122, 125; 123, lambda_parameters; 123, 124; 124, identifier:x; 125, boolean_operator:and; 125, 126; 125, 140; 126, boolean_operator:and; 126, 127; 126, 134; 127, parenthesized_expression; 127, 128; 128, comparison_operator:>; 128, 129; 128, 133; 129, call; 129, 130; 129, 131; 130, identifier:len; 131, argument_list; 131, 132; 132, identifier:x; 133, integer:1; 134, parenthesized_expression; 134, 135; 135, comparison_operator:==; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:x; 138, integer:0; 139, string:"*"; 140, parenthesized_expression; 140, 141; 141, comparison_operator:!=; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:x; 144, integer:1; 145, string:"*"; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:kw; 149, lambda; 149, 150; 149, 152; 150, lambda_parameters; 150, 151; 151, identifier:x; 152, boolean_operator:and; 152, 153; 152, 160; 153, parenthesized_expression; 153, 154; 154, comparison_operator:>; 154, 155; 154, 159; 155, call; 155, 156; 155, 157; 156, identifier:len; 157, argument_list; 157, 158; 158, identifier:x; 159, integer:2; 160, parenthesized_expression; 160, 161; 161, comparison_operator:==; 161, 162; 161, 167; 162, subscript; 162, 163; 162, 164; 163, identifier:x; 164, slice; 164, 165; 164, 166; 165, colon; 166, integer:2; 167, string:"**"; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:varargs_filtered_args; 171, list_comprehension; 171, 172; 171, 173; 171, 176; 172, identifier:arg; 173, for_in_clause; 173, 174; 173, 175; 174, identifier:arg; 175, identifier:self_filtered_args; 176, if_clause; 176, 177; 177, boolean_operator:or; 177, 178; 177, 181; 178, parenthesized_expression; 178, 179; 179, not_operator; 179, 180; 180, identifier:no_varargs; 181, call; 181, 182; 181, 183; 182, identifier:all; 183, argument_list; 183, 184; 184, list:[no_varargs, not pos(arg), not kw(arg)]; 184, 185; 184, 186; 184, 191; 185, identifier:no_varargs; 186, not_operator; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:pos; 189, argument_list; 189, 190; 190, identifier:arg; 191, not_operator; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:kw; 194, argument_list; 194, 195; 195, identifier:arg; 196, return_statement; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:tuple; 199, argument_list; 199, 200; 200, identifier:varargs_filtered_args | def get_function_args(func, no_self=False, no_varargs=False):
"""
Return tuple of the function argument names in the order of the function signature.
:param func: Function
:type func: function object
:param no_self: Flag that indicates whether the function argument *self*,
if present, is included in the output (False) or not (True)
:type no_self: boolean
:param no_varargs: Flag that indicates whether keyword arguments are
included in the output (True) or not (False)
:type no_varargs: boolean
:rtype: tuple
For example:
>>> import pexdoc.pinspect
>>> class MyClass(object):
... def __init__(self, value, **kwargs):
... pass
...
>>> pexdoc.pinspect.get_function_args(MyClass.__init__)
('self', 'value', '**kwargs')
>>> pexdoc.pinspect.get_function_args(
... MyClass.__init__, no_self=True
... )
('value', '**kwargs')
>>> pexdoc.pinspect.get_function_args(
... MyClass.__init__, no_self=True, no_varargs=True
... )
('value',)
>>> pexdoc.pinspect.get_function_args(
... MyClass.__init__, no_varargs=True
... )
('self', 'value')
"""
par_dict = signature(func).parameters
# Mark positional and/or keyword arguments (if any)
pos = lambda x: x.kind == Parameter.VAR_POSITIONAL
kw = lambda x: x.kind == Parameter.VAR_KEYWORD
opts = ["", "*", "**"]
args = [
"{prefix}{arg}".format(prefix=opts[pos(value) + 2 * kw(value)], arg=par)
for par, value in par_dict.items()
]
# Filter out 'self' from parameter list (optional)
self_filtered_args = (
args if not args else (args[1 if (args[0] == "self") and no_self else 0 :])
)
# Filter out positional or keyword arguments (optional)
pos = lambda x: (len(x) > 1) and (x[0] == "*") and (x[1] != "*")
kw = lambda x: (len(x) > 2) and (x[:2] == "**")
varargs_filtered_args = [
arg
for arg in self_filtered_args
if (not no_varargs) or all([no_varargs, not pos(arg), not kw(arg)])
]
return tuple(varargs_filtered_args) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_close_callable; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:node; 6, default_parameter; 6, 7; 6, 8; 7, identifier:force; 8, False; 9, block; 9, 10; 9, 12; 9, 13; 9, 14; 9, 15; 9, 27; 9, 35; 9, 36; 9, 37; 9, 38; 9, 39; 9, 40; 9, 41; 9, 42; 9, 43; 9, 44; 9, 48; 9, 94; 9, 95; 9, 104; 9, 109; 9, 110; 9, 111; 9, 112; 9, 113; 9, 114; 9, 115; 9, 116; 9, 117; 9, 118; 9, 119; 9, 120; 9, 121; 9, 122; 9, 123; 9, 127; 9, 274; 9, 275; 9, 276; 9, 277; 9, 278; 9, 279; 9, 285; 9, 294; 9, 310; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, comment; 14, comment; 15, try_statement; 15, 16; 15, 23; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:lineno; 20, attribute; 20, 21; 20, 22; 21, identifier:node; 22, identifier:lineno; 23, except_clause; 23, 24; 23, 25; 24, identifier:AttributeError; 25, block; 25, 26; 26, return_statement; 27, if_statement; 27, 28; 27, 33; 28, comparison_operator:<=; 28, 29; 28, 30; 29, identifier:lineno; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_processed_line; 33, block; 33, 34; 34, return_statement; 35, comment; 36, comment; 37, comment; 38, comment; 39, comment; 40, comment; 41, comment; 42, comment; 43, comment; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:name; 47, string:""; 48, try_statement; 48, 49; 48, 90; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:name; 53, parenthesized_expression; 53, 54; 54, conditional_expression:if; 54, 55; 54, 58; 54, 63; 55, attribute; 55, 56; 55, 57; 56, identifier:node; 57, identifier:name; 58, call; 58, 59; 58, 60; 59, identifier:hasattr; 60, argument_list; 60, 61; 60, 62; 61, identifier:node; 62, string:"name"; 63, parenthesized_expression; 63, 64; 64, conditional_expression:if; 64, 65; 64, 72; 64, 81; 65, attribute; 65, 66; 65, 71; 66, subscript; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:node; 69, identifier:targets; 70, integer:0; 71, identifier:id; 72, call; 72, 73; 72, 74; 73, identifier:hasattr; 74, argument_list; 74, 75; 74, 80; 75, subscript; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:node; 78, identifier:targets; 79, integer:0; 80, string:"id"; 81, attribute; 81, 82; 81, 89; 82, attribute; 82, 83; 82, 88; 83, subscript; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:node; 86, identifier:targets; 87, integer:0; 88, identifier:value; 89, identifier:id; 90, except_clause; 90, 91; 90, 92; 91, identifier:AttributeError; 92, block; 92, 93; 93, pass_statement; 94, comment; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:indent; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:_get_indent; 102, argument_list; 102, 103; 103, identifier:node; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:count; 107, unary_operator:-; 107, 108; 108, integer:1; 109, comment; 110, comment; 111, comment; 112, comment; 113, comment; 114, comment; 115, comment; 116, comment; 117, comment; 118, comment; 119, comment; 120, comment; 121, comment; 122, comment; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:dlist; 126, list:[]; 127, while_statement; 127, 128; 127, 137; 128, comparison_operator:>=; 128, 129; 128, 130; 129, identifier:count; 130, unary_operator:-; 130, 131; 131, call; 131, 132; 131, 133; 132, identifier:len; 133, argument_list; 133, 134; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:_indent_stack; 137, block; 137, 138; 137, 148; 137, 160; 137, 170; 137, 180; 137, 181; 137, 182; 137, 183; 137, 184; 137, 185; 137, 186; 137, 187; 137, 188; 137, 189; 137, 190; 137, 191; 137, 192; 137, 193; 137, 194; 137, 264; 137, 270; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:element_full_name; 141, subscript; 141, 142; 141, 147; 142, subscript; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:_indent_stack; 146, identifier:count; 147, string:"full_name"; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:edict; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_callables_db; 156, identifier:get; 157, argument_list; 157, 158; 157, 159; 158, identifier:element_full_name; 159, None; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:stack_indent; 163, subscript; 163, 164; 163, 169; 164, subscript; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:_indent_stack; 168, identifier:count; 169, string:"level"; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:open_callable; 173, boolean_operator:and; 173, 174; 173, 175; 174, identifier:element_full_name; 175, parenthesized_expression; 175, 176; 176, not_operator; 176, 177; 177, subscript; 177, 178; 177, 179; 178, identifier:edict; 179, string:"last_lineno"; 180, comment; 181, comment; 182, comment; 183, comment; 184, comment; 185, comment; 186, comment; 187, comment; 188, comment; 189, comment; 190, comment; 191, comment; 192, comment; 193, comment; 194, if_statement; 194, 195; 194, 234; 194, 235; 194, 236; 194, 237; 194, 238; 194, 239; 194, 240; 194, 241; 194, 242; 194, 243; 194, 244; 194, 245; 194, 246; 194, 247; 194, 248; 195, boolean_operator:and; 195, 196; 195, 197; 196, identifier:open_callable; 197, parenthesized_expression; 197, 198; 198, boolean_operator:or; 198, 199; 198, 205; 199, boolean_operator:or; 199, 200; 199, 201; 200, identifier:force; 201, parenthesized_expression; 201, 202; 202, comparison_operator:<; 202, 203; 202, 204; 203, identifier:indent; 204, identifier:stack_indent; 205, parenthesized_expression; 205, 206; 206, boolean_operator:and; 206, 207; 206, 211; 207, parenthesized_expression; 207, 208; 208, comparison_operator:==; 208, 209; 208, 210; 209, identifier:indent; 210, identifier:stack_indent; 211, parenthesized_expression; 211, 212; 212, boolean_operator:or; 212, 213; 212, 219; 213, parenthesized_expression; 213, 214; 214, comparison_operator:!=; 214, 215; 214, 218; 215, subscript; 215, 216; 215, 217; 216, identifier:edict; 217, string:"type"; 218, string:"prop"; 219, parenthesized_expression; 219, 220; 220, boolean_operator:and; 220, 221; 220, 227; 221, parenthesized_expression; 221, 222; 222, comparison_operator:==; 222, 223; 222, 226; 223, subscript; 223, 224; 223, 225; 224, identifier:edict; 225, string:"type"; 226, string:"prop"; 227, parenthesized_expression; 227, 228; 228, boolean_operator:and; 228, 229; 228, 230; 229, identifier:name; 230, parenthesized_expression; 230, 231; 231, comparison_operator:!=; 231, 232; 231, 233; 232, identifier:name; 233, identifier:element_full_name; 234, comment; 235, comment; 236, comment; 237, comment; 238, comment; 239, comment; 240, comment; 241, comment; 242, comment; 243, comment; 244, comment; 245, comment; 246, comment; 247, comment; 248, block; 248, 249; 248, 257; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 254; 251, subscript; 251, 252; 251, 253; 252, identifier:edict; 253, string:"last_lineno"; 254, binary_operator:-; 254, 255; 254, 256; 255, identifier:lineno; 256, integer:1; 257, expression_statement; 257, 258; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:dlist; 261, identifier:append; 262, argument_list; 262, 263; 263, identifier:count; 264, if_statement; 264, 265; 264, 268; 265, comparison_operator:>; 265, 266; 265, 267; 266, identifier:indent; 267, identifier:stack_indent; 268, block; 268, 269; 269, break_statement; 270, expression_statement; 270, 271; 271, augmented_assignment:-=; 271, 272; 271, 273; 272, identifier:count; 273, integer:1; 274, comment; 275, comment; 276, comment; 277, comment; 278, comment; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:stack; 282, attribute; 282, 283; 282, 284; 283, identifier:self; 284, identifier:_indent_stack; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:stack_length; 288, call; 288, 289; 288, 290; 289, identifier:len; 290, argument_list; 290, 291; 291, attribute; 291, 292; 291, 293; 292, identifier:self; 293, identifier:_indent_stack; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:dlist; 297, list_comprehension; 297, 298; 297, 299; 297, 302; 298, identifier:item; 299, for_in_clause; 299, 300; 299, 301; 300, identifier:item; 301, identifier:dlist; 302, if_clause; 302, 303; 303, comparison_operator:!=; 303, 304; 303, 309; 304, subscript; 304, 305; 304, 308; 305, subscript; 305, 306; 305, 307; 306, identifier:stack; 307, identifier:item; 308, string:"type"; 309, string:"module"; 310, for_statement; 310, 311; 310, 312; 310, 313; 311, identifier:item; 312, identifier:dlist; 313, block; 313, 314; 314, delete_statement; 314, 315; 315, subscript; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:self; 318, identifier:_indent_stack; 319, binary_operator:+; 319, 320; 319, 321; 320, identifier:stack_length; 321, identifier:item | def _close_callable(self, node, force=False):
"""Record last line number of callable."""
# Only nodes that have a line number can be considered for closing
# callables. Similarly, only nodes with lines greater than the one
# already processed can be considered for closing callables
try:
lineno = node.lineno
except AttributeError:
return
if lineno <= self._processed_line:
return
# [[[cog
# code = """
# print(pcolor('Close callable @ line = {0}'.format(lineno), 'green'))
# """
# cog.out(code)
# ]]]
# [[[end]]]
# Extract node name for property closing. Once a property is found,
# it can only be closed out by a node type that has a name
name = ""
try:
name = (
node.name
if hasattr(node, "name")
else (
node.targets[0].id
if hasattr(node.targets[0], "id")
else node.targets[0].value.id
)
)
except AttributeError:
pass
# Traverse backwards through call stack and close callables as needed
indent = self._get_indent(node)
count = -1
# [[[cog
# code = """
# print(
# pcolor(
# ' Name {0} @ {1}, indent = {2}'.format(
# name if name else 'None', lineno, indent
# ),
# 'yellow'
# )
# )
# """
# cog.out(code)
# ]]]
# [[[end]]]
dlist = []
while count >= -len(self._indent_stack):
element_full_name = self._indent_stack[count]["full_name"]
edict = self._callables_db.get(element_full_name, None)
stack_indent = self._indent_stack[count]["level"]
open_callable = element_full_name and (not edict["last_lineno"])
# [[[cog
# code = """
# print(
# pcolor(
# ' Name {0}, indent, {1}, stack_indent {2}'.format(
# element_full_name, indent, stack_indent
# ),
# 'yellow'
# )
# )
# """
# cog.out(code)
# ]]]
# [[[end]]]
if open_callable and (
force
or (indent < stack_indent)
or (
(indent == stack_indent)
and (
(edict["type"] != "prop")
or (
(edict["type"] == "prop")
and (name and (name != element_full_name))
)
)
)
):
# [[[cog
# code = """
# print(
# pcolor(
# ' Closing {0} @ {1}'.format(
# element_full_name, lineno-1
# ),
# 'yellow'
# )
# )
# """
# cog.out(code)
# ]]]
# [[[end]]]
edict["last_lineno"] = lineno - 1
dlist.append(count)
if indent > stack_indent:
break
count -= 1
# Callables have to be removed from stack when they are closed,
# otherwise if a callable is subsequently followed after a few
# lines by another callable at a further indentation level (like a for
# loop) the second callable would incorrectly appear within the scope
# of the first callable
stack = self._indent_stack
stack_length = len(self._indent_stack)
dlist = [item for item in dlist if stack[item]["type"] != "module"]
for item in dlist:
del self._indent_stack[stack_length + item] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:qteRemoveKey; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:keysequence; 7, type; 7, 8; 8, identifier:QtmacsKeysequence; 9, block; 9, 10; 9, 12; 9, 13; 9, 14; 9, 18; 9, 19; 9, 23; 9, 24; 9, 25; 9, 26; 9, 34; 9, 35; 9, 36; 9, 37; 9, 62; 9, 63; 9, 64; 9, 86; 9, 87; 9, 88; 9, 89; 9, 90; 9, 91; 9, 92; 9, 101; 9, 102; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:keyMap; 17, identifier:self; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:keyMapRef; 22, identifier:keyMap; 23, comment; 24, comment; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:keysequence; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:keysequence; 32, identifier:toQtKeylist; 33, argument_list; 34, comment; 35, comment; 36, comment; 37, for_statement; 37, 38; 37, 39; 37, 45; 37, 46; 37, 47; 37, 48; 38, identifier:key; 39, subscript; 39, 40; 39, 41; 40, identifier:keysequence; 41, slice; 41, 42; 41, 43; 42, colon; 43, unary_operator:-; 43, 44; 44, integer:1; 45, comment; 46, comment; 47, comment; 48, block; 48, 49; 48, 55; 48, 56; 49, if_statement; 49, 50; 49, 53; 50, comparison_operator:not; 50, 51; 50, 52; 51, identifier:key; 52, identifier:keyMap; 53, block; 53, 54; 54, return_statement; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:keyMap; 59, subscript; 59, 60; 59, 61; 60, identifier:keyMap; 61, identifier:key; 62, comment; 63, comment; 64, if_statement; 64, 65; 64, 71; 64, 73; 65, comparison_operator:not; 65, 66; 65, 70; 66, subscript; 66, 67; 66, 68; 67, identifier:keysequence; 68, unary_operator:-; 68, 69; 69, integer:1; 70, identifier:keyMap; 71, block; 71, 72; 72, return_statement; 73, else_clause; 73, 74; 73, 75; 74, comment; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:keyMap; 80, identifier:pop; 81, argument_list; 81, 82; 82, subscript; 82, 83; 82, 84; 83, identifier:keysequence; 84, unary_operator:-; 84, 85; 85, integer:1; 86, comment; 87, comment; 88, comment; 89, comment; 90, comment; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:keysequence; 95, subscript; 95, 96; 95, 97; 96, identifier:keysequence; 97, slice; 97, 98; 97, 99; 98, colon; 99, unary_operator:-; 99, 100; 100, integer:1; 101, comment; 102, while_statement; 102, 103; 102, 108; 102, 109; 102, 110; 103, parenthesized_expression; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:len; 106, argument_list; 106, 107; 107, identifier:keysequence; 108, comment; 109, comment; 110, block; 110, 111; 110, 115; 110, 130; 110, 131; 110, 132; 110, 133; 110, 134; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:keyMap; 114, identifier:keyMapRef; 115, for_statement; 115, 116; 115, 117; 115, 123; 116, identifier:key; 117, subscript; 117, 118; 117, 119; 118, identifier:keysequence; 119, slice; 119, 120; 119, 121; 120, colon; 121, unary_operator:-; 121, 122; 122, integer:1; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:keyMap; 127, subscript; 127, 128; 127, 129; 128, identifier:keyMap; 129, identifier:key; 130, comment; 131, comment; 132, comment; 133, comment; 134, if_statement; 134, 135; 134, 141; 134, 143; 135, call; 135, 136; 135, 137; 136, identifier:len; 137, argument_list; 137, 138; 138, subscript; 138, 139; 138, 140; 139, identifier:keyMap; 140, identifier:key; 141, block; 141, 142; 142, return_statement; 143, else_clause; 143, 144; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:keyMap; 149, identifier:pop; 150, argument_list; 150, 151; 151, identifier:key | def qteRemoveKey(self, keysequence: QtmacsKeysequence):
"""
Remove ``keysequence`` from this key map.
|Args|
* ``keysequence`` (**QtmacsKeysequence**): key sequence to
remove from this key map.
|Returns|
**None**
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# Get a dedicated reference to self to facilitate traversing
# through the key map.
keyMap = self
# Keep a reference to the root element in the key map.
keyMapRef = keyMap
# Get the key sequence as a list of tuples, where each tuple
# contains the the control modifier and the key code, and both
# are specified as Qt constants.
keysequence = keysequence.toQtKeylist()
# ------------------------------------------------------------
# Remove the leaf element from the tree.
# ------------------------------------------------------------
for key in keysequence[:-1]:
# Quit if the key does not exist. This can happen if the
# user tries to remove a key that has never been
# registered.
if key not in keyMap:
return
# Go one level down in the key-map tree.
keyMap = keyMap[key]
# The specified key sequence does not exist if the leaf
# element (ie. last entry in the key sequence) is missing.
if keysequence[-1] not in keyMap:
return
else:
# Remove the leaf.
keyMap.pop(keysequence[-1])
# ------------------------------------------------------------
# Prune the prefix path defined by ``keysequence`` and remove
# all empty dictionaries. Start at the leaf level.
# ------------------------------------------------------------
# Drop the last element in the key sequence, because it was
# removed in the above code fragment already.
keysequence = keysequence[:-1]
# Now successively remove the key sequence in reverse order.
while(len(keysequence)):
# Start at the root and move to the last branch level
# before the leaf level.
keyMap = keyMapRef
for key in keysequence[:-1]:
keyMap = keyMap[key]
# If the leaf is a non-empty dictionary then another key
# with the same prefix still exists. In this case do
# nothing. However, if the leaf is now empty it must be
# removed.
if len(keyMap[key]):
return
else:
keyMap.pop(key) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:find_labels; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:key; 6, default_parameter; 6, 7; 6, 8; 7, identifier:find_in_name; 8, True; 9, default_parameter; 9, 10; 9, 11; 10, identifier:find_in_units; 11, False; 12, block; 12, 13; 12, 15; 12, 125; 12, 162; 12, 184; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 22; 16, comparison_operator:is; 16, 17; 16, 21; 17, call; 17, 18; 17, 19; 18, identifier:type; 19, argument_list; 19, 20; 20, identifier:key; 21, identifier:str; 22, block; 22, 23; 22, 27; 22, 123; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:found_keys; 26, list:[]; 27, if_statement; 27, 28; 27, 34; 27, 86; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:key; 31, identifier:startswith; 32, argument_list; 32, 33; 33, string:'~'; 34, block; 34, 35; 35, for_statement; 35, 36; 35, 39; 35, 45; 36, pattern_list; 36, 37; 36, 38; 37, identifier:label_no; 38, identifier:label; 39, call; 39, 40; 39, 41; 40, identifier:enumerate; 41, argument_list; 41, 42; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:labels; 45, block; 45, 46; 45, 66; 46, if_statement; 46, 47; 46, 58; 47, boolean_operator:and; 47, 48; 47, 49; 48, identifier:find_in_name; 49, comparison_operator:in; 49, 50; 49, 55; 50, subscript; 50, 51; 50, 52; 51, identifier:key; 52, slice; 52, 53; 52, 54; 53, integer:1; 54, colon; 55, attribute; 55, 56; 55, 57; 56, identifier:label; 57, identifier:name; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:found_keys; 63, identifier:append; 64, argument_list; 64, 65; 65, identifier:label_no; 66, if_statement; 66, 67; 66, 78; 67, boolean_operator:and; 67, 68; 67, 69; 68, identifier:find_in_units; 69, comparison_operator:in; 69, 70; 69, 75; 70, subscript; 70, 71; 70, 72; 71, identifier:key; 72, slice; 72, 73; 72, 74; 73, integer:1; 74, colon; 75, attribute; 75, 76; 75, 77; 76, identifier:label; 77, identifier:units; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:found_keys; 83, identifier:append; 84, argument_list; 84, 85; 85, identifier:label_no; 86, else_clause; 86, 87; 87, block; 87, 88; 88, for_statement; 88, 89; 88, 92; 88, 98; 89, pattern_list; 89, 90; 89, 91; 90, identifier:label_no; 91, identifier:label; 92, call; 92, 93; 92, 94; 93, identifier:enumerate; 94, argument_list; 94, 95; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:labels; 98, block; 98, 99; 98, 111; 99, if_statement; 99, 100; 99, 107; 100, boolean_operator:and; 100, 101; 100, 102; 101, identifier:find_in_name; 102, comparison_operator:==; 102, 103; 102, 104; 103, identifier:key; 104, attribute; 104, 105; 104, 106; 105, identifier:label; 106, identifier:name; 107, block; 107, 108; 108, return_statement; 108, 109; 109, list:[label_no]; 109, 110; 110, identifier:label_no; 111, if_statement; 111, 112; 111, 119; 112, boolean_operator:and; 112, 113; 112, 114; 113, identifier:find_in_units; 114, comparison_operator:==; 114, 115; 114, 116; 115, identifier:key; 116, attribute; 116, 117; 116, 118; 117, identifier:label; 118, identifier:units; 119, block; 119, 120; 120, return_statement; 120, 121; 121, list:[label_no]; 121, 122; 122, identifier:label_no; 123, return_statement; 123, 124; 124, identifier:found_keys; 125, if_statement; 125, 126; 125, 131; 126, call; 126, 127; 126, 128; 127, identifier:hasattr; 128, argument_list; 128, 129; 128, 130; 129, identifier:key; 130, string:'__call__'; 131, block; 131, 132; 131, 136; 131, 160; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:found_keys; 135, list:[]; 136, for_statement; 136, 137; 136, 140; 136, 146; 137, pattern_list; 137, 138; 137, 139; 138, identifier:label_no; 139, identifier:label; 140, call; 140, 141; 140, 142; 141, identifier:enumerate; 142, argument_list; 142, 143; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:labels; 146, block; 146, 147; 147, if_statement; 147, 148; 147, 152; 148, call; 148, 149; 148, 150; 149, identifier:key; 150, argument_list; 150, 151; 151, identifier:label; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:found_keys; 157, identifier:append; 158, argument_list; 158, 159; 159, identifier:label_no; 160, return_statement; 160, 161; 161, identifier:found_keys; 162, if_statement; 162, 163; 162, 169; 163, comparison_operator:is; 163, 164; 163, 168; 164, call; 164, 165; 164, 166; 165, identifier:type; 166, argument_list; 166, 167; 167, identifier:key; 168, identifier:int; 169, block; 169, 170; 170, return_statement; 170, 171; 171, conditional_expression:if; 171, 172; 171, 174; 171, 183; 172, list:[key]; 172, 173; 173, identifier:key; 174, comparison_operator:<; 174, 175; 174, 176; 175, identifier:key; 176, subscript; 176, 177; 176, 182; 177, attribute; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:matrix; 181, identifier:shape; 182, integer:1; 183, list:[]; 184, return_statement; 184, 185; 185, list:[key]; 185, 186; 186, identifier:key | def find_labels(self,key,find_in_name=True,find_in_units=False):
"""
Takes a string or a function to find a set of label indizes
that match.
If the string starts with a '~', the label only has to contain the string.
"""
if type(key) is str:
found_keys = []
if key.startswith('~'):
for label_no,label in enumerate(self.labels):
if find_in_name and key[1:] in label.name:
found_keys.append(label_no)
if find_in_units and key[1:] in label.units:
found_keys.append(label_no)
else:
for label_no,label in enumerate(self.labels):
if find_in_name and key == label.name:
return [label_no]
if find_in_units and key == label.units:
return [label_no]
return found_keys
if hasattr(key, '__call__'):
found_keys = []
for label_no,label in enumerate(self.labels):
if key(label):
found_keys.append(label_no)
return found_keys
if type(key) is int:
return [key] if key < self.matrix.shape[1] else []
return [key] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:_get_constrained_labels; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:remove_dimensions; 7, False; 8, dictionary_splat_pattern; 8, 9; 9, identifier:kwargs; 10, block; 10, 11; 10, 13; 10, 17; 10, 263; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:new_labels; 16, list:[]; 17, for_statement; 17, 18; 17, 21; 17, 27; 18, pattern_list; 18, 19; 18, 20; 19, identifier:label_no; 20, identifier:label; 21, call; 21, 22; 21, 23; 22, identifier:enumerate; 23, argument_list; 23, 24; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:labels; 27, block; 27, 28; 27, 35; 27, 39; 27, 248; 27, 256; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:new_label; 31, call; 31, 32; 31, 33; 32, identifier:LabelDimension; 33, argument_list; 33, 34; 34, identifier:label; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:remove; 38, False; 39, for_statement; 39, 40; 39, 41; 39, 42; 40, identifier:k; 41, identifier:kwargs; 42, block; 42, 43; 42, 70; 42, 126; 42, 153; 42, 208; 42, 235; 43, if_statement; 43, 44; 43, 49; 44, comparison_operator:==; 44, 45; 44, 46; 45, identifier:k; 46, attribute; 46, 47; 46, 48; 47, identifier:label; 48, identifier:name; 49, block; 49, 50; 49, 58; 49, 66; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:new_label; 54, identifier:max; 55, subscript; 55, 56; 55, 57; 56, identifier:kwargs; 57, identifier:k; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:new_label; 62, identifier:min; 63, subscript; 63, 64; 63, 65; 64, identifier:kwargs; 65, identifier:k; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:remove; 69, True; 70, if_statement; 70, 71; 70, 78; 71, comparison_operator:==; 71, 72; 71, 73; 72, identifier:k; 73, binary_operator:+; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:label; 76, identifier:name; 77, string:'__lt'; 78, block; 78, 79; 78, 125; 79, if_statement; 79, 80; 79, 85; 79, 106; 80, comparison_operator:==; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:new_label; 83, identifier:units; 84, string:'1'; 85, block; 85, 86; 85, 105; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:new_label; 90, identifier:max; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:np; 94, identifier:min; 95, argument_list; 95, 96; 96, list:[new_label.max,kwargs[k]-1]; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:new_label; 99, identifier:max; 100, binary_operator:-; 100, 101; 100, 104; 101, subscript; 101, 102; 101, 103; 102, identifier:kwargs; 103, identifier:k; 104, integer:1; 105, comment; 106, else_clause; 106, 107; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:new_label; 112, identifier:max; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:np; 116, identifier:min; 117, argument_list; 117, 118; 118, list:[new_label.max,kwargs[k]]; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:new_label; 121, identifier:max; 122, subscript; 122, 123; 122, 124; 123, identifier:kwargs; 124, identifier:k; 125, comment; 126, if_statement; 126, 127; 126, 134; 127, comparison_operator:==; 127, 128; 127, 129; 128, identifier:k; 129, binary_operator:+; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:label; 132, identifier:name; 133, string:'__lte'; 134, block; 134, 135; 134, 152; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:new_label; 139, identifier:max; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:np; 143, identifier:min; 144, argument_list; 144, 145; 145, list:[new_label.max,kwargs[k]]; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:new_label; 148, identifier:max; 149, subscript; 149, 150; 149, 151; 150, identifier:kwargs; 151, identifier:k; 152, comment; 153, if_statement; 153, 154; 153, 161; 154, comparison_operator:==; 154, 155; 154, 156; 155, identifier:k; 156, binary_operator:+; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:label; 159, identifier:name; 160, string:'__gt'; 161, block; 161, 162; 161, 207; 162, if_statement; 162, 163; 162, 168; 162, 188; 163, comparison_operator:==; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:new_label; 166, identifier:units; 167, string:'1'; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:new_label; 173, identifier:min; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:np; 177, identifier:max; 178, argument_list; 178, 179; 179, list:[new_label.min,kwargs[k]+1]; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:new_label; 182, identifier:min; 183, binary_operator:+; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:kwargs; 186, identifier:k; 187, integer:1; 188, else_clause; 188, 189; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:new_label; 194, identifier:min; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:np; 198, identifier:max; 199, argument_list; 199, 200; 200, list:[new_label.min,kwargs[k]]; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:new_label; 203, identifier:min; 204, subscript; 204, 205; 204, 206; 205, identifier:kwargs; 206, identifier:k; 207, comment; 208, if_statement; 208, 209; 208, 216; 209, comparison_operator:==; 209, 210; 209, 211; 210, identifier:k; 211, binary_operator:+; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:label; 214, identifier:name; 215, string:'__gte'; 216, block; 216, 217; 216, 234; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:new_label; 221, identifier:min; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:np; 225, identifier:max; 226, argument_list; 226, 227; 227, list:[new_label.min,kwargs[k]]; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:new_label; 230, identifier:min; 231, subscript; 231, 232; 231, 233; 232, identifier:kwargs; 233, identifier:k; 234, comment; 235, if_statement; 235, 236; 235, 243; 236, comparison_operator:==; 236, 237; 236, 238; 237, identifier:k; 238, binary_operator:+; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:label; 241, identifier:name; 242, string:'__evals'; 243, block; 243, 244; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:remove; 247, True; 248, if_statement; 248, 249; 248, 250; 249, identifier:remove_dimensions; 250, block; 250, 251; 251, if_statement; 251, 252; 251, 253; 251, 254; 252, identifier:remove; 253, comment; 254, block; 254, 255; 255, continue_statement; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:new_labels; 260, identifier:append; 261, argument_list; 261, 262; 262, identifier:new_label; 263, return_statement; 263, 264; 264, identifier:new_labels | def _get_constrained_labels(self,remove_dimensions=False,**kwargs):
"""
returns labels which have updated minima and maxima,
depending on the kwargs supplied to this
"""
new_labels = []
for label_no,label in enumerate(self.labels):
new_label = LabelDimension(label)
remove = False
for k in kwargs:
if k == label.name:
new_label.max = kwargs[k]
new_label.min = kwargs[k]
remove = True
if k == label.name+'__lt':
if new_label.units == '1':
new_label.max = np.min([new_label.max,kwargs[k]-1]) # is this right?
else:
new_label.max = np.min([new_label.max,kwargs[k]])
#remove = True
if k == label.name+'__lte':
new_label.max = np.min([new_label.max,kwargs[k]])
#remove = True
if k == label.name+'__gt':
if new_label.units == '1':
new_label.min = np.max([new_label.min,kwargs[k]+1])
else:
new_label.min = np.max([new_label.min,kwargs[k]])
#remove = True
if k == label.name+'__gte':
new_label.min = np.max([new_label.min,kwargs[k]])
#remove = True
if k == label.name+'__evals':
remove = True
if remove_dimensions:
if remove:
# skipping removed labels
continue
new_labels.append(new_label)
return new_labels |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:populate_field_values; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:data; 6, block; 6, 7; 6, 9; 6, 38; 6, 79; 6, 225; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 16; 10, not_operator; 10, 11; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_meta; 15, identifier:case_sensitive_fields; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:data; 20, dictionary_comprehension; 20, 21; 20, 28; 21, pair; 21, 22; 21, 27; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:k; 25, identifier:lower; 26, argument_list; 27, identifier:v; 28, for_in_clause; 28, 29; 28, 32; 29, pattern_list; 29, 30; 29, 31; 30, identifier:k; 31, identifier:v; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:six; 35, identifier:iteritems; 36, argument_list; 36, 37; 37, identifier:data; 38, if_statement; 38, 39; 38, 44; 38, 45; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_meta; 43, identifier:match_fuzzy_keys; 44, comment; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:data; 49, dictionary_comprehension; 49, 50; 49, 69; 50, pair; 50, 51; 50, 68; 51, call; 51, 52; 51, 67; 52, attribute; 52, 53; 52, 66; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, string:''; 56, identifier:join; 57, generator_expression; 57, 58; 57, 59; 57, 62; 58, identifier:x; 59, for_in_clause; 59, 60; 59, 61; 60, identifier:x; 61, identifier:k; 62, if_clause; 62, 63; 63, comparison_operator:in; 63, 64; 63, 65; 64, identifier:x; 65, identifier:ALPHANUMERIC; 66, identifier:lower; 67, argument_list; 68, identifier:v; 69, for_in_clause; 69, 70; 69, 73; 70, pattern_list; 70, 71; 70, 72; 71, identifier:k; 72, identifier:v; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:six; 76, identifier:iteritems; 77, argument_list; 77, 78; 78, identifier:data; 79, for_statement; 79, 80; 79, 81; 79, 86; 80, identifier:field; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_meta; 85, identifier:fields; 86, block; 86, 87; 86, 106; 86, 110; 86, 137; 86, 216; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:name; 90, conditional_expression:if; 90, 91; 90, 94; 90, 99; 91, attribute; 91, 92; 91, 93; 92, identifier:field; 93, identifier:name; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_meta; 98, identifier:case_sensitive_fields; 99, call; 99, 100; 99, 105; 100, attribute; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:field; 103, identifier:name; 104, identifier:lower; 105, argument_list; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:value; 109, None; 110, if_statement; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:_meta; 115, identifier:match_fuzzy_keys; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:name; 120, call; 120, 121; 120, 136; 121, attribute; 121, 122; 121, 135; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, string:''; 125, identifier:join; 126, generator_expression; 126, 127; 126, 128; 126, 131; 127, identifier:x; 128, for_in_clause; 128, 129; 128, 130; 129, identifier:x; 130, identifier:name; 131, if_clause; 131, 132; 132, comparison_operator:in; 132, 133; 132, 134; 133, identifier:x; 134, identifier:ALPHANUMERIC; 135, identifier:lower; 136, argument_list; 137, if_statement; 137, 138; 137, 141; 137, 154; 137, 198; 138, comparison_operator:in; 138, 139; 138, 140; 139, identifier:name; 140, identifier:data; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:value; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:field; 148, identifier:to_python; 149, argument_list; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:data; 152, identifier:name; 153, identifier:self; 154, elif_clause; 154, 155; 154, 164; 155, boolean_operator:and; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:field; 158, identifier:required; 159, comparison_operator:is; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:field; 162, identifier:default; 163, None; 164, block; 164, 165; 164, 179; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:message; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, string:"Response from {0} is missing required field '{1}'"; 171, identifier:format; 172, argument_list; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:_url; 176, attribute; 176, 177; 176, 178; 177, identifier:field; 178, identifier:name; 179, if_statement; 179, 180; 179, 183; 179, 189; 180, attribute; 180, 181; 180, 182; 181, identifier:self; 182, identifier:_strict; 183, block; 183, 184; 184, raise_statement; 184, 185; 185, call; 185, 186; 185, 187; 186, identifier:MissingFieldException; 187, argument_list; 187, 188; 188, identifier:message; 189, else_clause; 189, 190; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:logger; 195, identifier:warn; 196, argument_list; 196, 197; 197, identifier:message; 198, elif_clause; 198, 199; 198, 204; 199, comparison_operator:is; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:field; 202, identifier:default; 203, None; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:value; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:copy; 211, identifier:copy; 212, argument_list; 212, 213; 213, attribute; 213, 214; 213, 215; 214, identifier:field; 215, identifier:default; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 219; 218, identifier:setattr; 219, argument_list; 219, 220; 219, 221; 219, 224; 220, identifier:self; 221, attribute; 221, 222; 221, 223; 222, identifier:field; 223, identifier:_attr_name; 224, identifier:value; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:_populated_field_values; 230, True | def populate_field_values(self, data):
"""Load resource data and populate field values"""
if not self._meta.case_sensitive_fields:
data = {k.lower(): v for k, v in six.iteritems(data)}
if self._meta.match_fuzzy_keys:
# String any non-alphanumeric chars from each key
data = {''.join(x for x in k if x in ALPHANUMERIC).lower(): v for k, v in six.iteritems(data)}
for field in self._meta.fields:
name = field.name if self._meta.case_sensitive_fields else field.name.lower()
value = None
if self._meta.match_fuzzy_keys:
name = ''.join(x for x in name if x in ALPHANUMERIC).lower()
if name in data:
value = field.to_python(data[name], self)
elif field.required and field.default is None:
message = "Response from {0} is missing required field '{1}'".format(self._url, field.name)
if self._strict:
raise MissingFieldException(message)
else:
logger.warn(message)
elif field.default is not None:
value = copy.copy(field.default)
setattr(self, field._attr_name, value)
self._populated_field_values = True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:build_contact; 3, parameters; 3, 4; 3, 5; 4, identifier:request; 5, default_parameter; 5, 6; 5, 7; 6, identifier:slug; 7, string:""; 8, block; 8, 9; 8, 11; 8, 21; 8, 31; 8, 37; 8, 43; 8, 62; 8, 63; 8, 136; 8, 451; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:controller; 14, call; 14, 15; 14, 16; 15, identifier:get_object_or_404; 16, argument_list; 16, 17; 16, 18; 17, identifier:ContactFormController; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:slug; 20, identifier:slug; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:site; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:Site; 28, identifier:objects; 29, identifier:get_current; 30, argument_list; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:UserModel; 34, call; 34, 35; 34, 36; 35, identifier:get_user_model; 36, argument_list; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:user; 40, attribute; 40, 41; 40, 42; 41, identifier:request; 42, identifier:user; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:form; 46, call; 46, 47; 46, 48; 47, identifier:ContactForm; 48, argument_list; 48, 49; 48, 54; 48, 59; 49, boolean_operator:or; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:request; 52, identifier:POST; 53, None; 54, boolean_operator:or; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:request; 57, identifier:FILES; 58, None; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:controller; 61, identifier:controller; 62, comment; 63, if_statement; 63, 64; 63, 67; 63, 68; 64, attribute; 64, 65; 64, 66; 65, identifier:user; 66, identifier:is_authenticated; 67, comment; 68, block; 68, 69; 68, 86; 68, 100; 68, 110; 68, 124; 69, try_statement; 69, 70; 69, 77; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:name; 74, attribute; 74, 75; 74, 76; 75, identifier:user; 76, identifier:display_name; 77, except_clause; 77, 78; 77, 79; 78, identifier:AttributeError; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:name; 83, attribute; 83, 84; 83, 85; 84, identifier:user; 85, identifier:username; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 99; 88, subscript; 88, 89; 88, 98; 89, attribute; 89, 90; 89, 97; 90, attribute; 90, 91; 90, 96; 91, subscript; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:form; 94, identifier:fields; 95, string:'sender_name'; 96, identifier:widget; 97, identifier:attrs; 98, string:'readonly'; 99, string:'true'; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 109; 102, attribute; 102, 103; 102, 108; 103, subscript; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:form; 106, identifier:fields; 107, string:'sender_name'; 108, identifier:initial; 109, identifier:name; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 123; 112, subscript; 112, 113; 112, 122; 113, attribute; 113, 114; 113, 121; 114, attribute; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:form; 118, identifier:fields; 119, string:'sender_email'; 120, identifier:widget; 121, identifier:attrs; 122, string:'readonly'; 123, string:'true'; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 133; 126, attribute; 126, 127; 126, 132; 127, subscript; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:form; 130, identifier:fields; 131, string:'sender_email'; 132, identifier:initial; 133, attribute; 133, 134; 133, 135; 134, identifier:user; 135, identifier:email; 136, if_statement; 136, 137; 136, 142; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:form; 140, identifier:is_valid; 141, argument_list; 142, block; 142, 143; 142, 191; 142, 441; 143, if_statement; 143, 144; 143, 147; 143, 148; 144, attribute; 144, 145; 144, 146; 145, identifier:controller; 146, identifier:store_in_db; 147, comment; 148, block; 148, 149; 148, 159; 148, 165; 148, 171; 148, 185; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:new_msg; 152, call; 152, 153; 152, 154; 153, identifier:Contact; 154, argument_list; 154, 155; 155, dictionary_splat; 155, 156; 156, attribute; 156, 157; 156, 158; 157, identifier:form; 158, identifier:cleaned_data; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:new_msg; 163, identifier:controller; 164, identifier:controller; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:new_msg; 169, identifier:site; 170, identifier:site; 171, if_statement; 171, 172; 171, 175; 171, 176; 172, attribute; 172, 173; 172, 174; 173, identifier:controller; 174, identifier:override_subject; 175, comment; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:new_msg; 181, identifier:subject; 182, attribute; 182, 183; 182, 184; 183, identifier:controller; 184, identifier:override_subject; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:new_msg; 189, identifier:save; 190, argument_list; 191, if_statement; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:controller; 194, identifier:send_emails; 195, block; 195, 196; 195, 202; 195, 240; 195, 254; 195, 279; 195, 321; 195, 363; 195, 381; 195, 403; 195, 435; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:form_data; 199, attribute; 199, 200; 199, 201; 200, identifier:form; 201, identifier:cleaned_data; 202, if_statement; 202, 203; 202, 206; 202, 213; 202, 224; 203, attribute; 203, 204; 203, 205; 204, identifier:controller; 205, identifier:override_subject; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:subject; 210, attribute; 210, 211; 210, 212; 211, identifier:controller; 212, identifier:override_subject; 213, elif_clause; 213, 214; 213, 217; 214, comparison_operator:in; 214, 215; 214, 216; 215, string:'subject'; 216, identifier:form_data; 217, block; 217, 218; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:subject; 221, subscript; 221, 222; 221, 223; 222, identifier:form_data; 223, string:'subject'; 224, else_clause; 224, 225; 225, block; 225, 226; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:subject; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, string:"{} message from {}"; 232, identifier:format; 233, argument_list; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:controller; 236, identifier:name; 237, subscript; 237, 238; 237, 239; 238, identifier:form_data; 239, string:'sender_name'; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:body; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, string:"{} \n\n {}"; 246, identifier:format; 247, argument_list; 247, 248; 247, 251; 248, subscript; 248, 249; 248, 250; 249, identifier:form_data; 250, string:'body'; 251, subscript; 251, 252; 251, 253; 252, identifier:form_data; 253, string:'sender_name'; 254, if_statement; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:controller; 257, identifier:request_contact_info; 258, block; 258, 259; 259, expression_statement; 259, 260; 260, augmented_assignment:+=; 260, 261; 260, 262; 261, identifier:body; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, string:"\nAddress: {} \nCity: {} \nState: {} \nPhone: {}"; 265, identifier:format; 266, argument_list; 266, 267; 266, 270; 266, 273; 266, 276; 267, subscript; 267, 268; 267, 269; 268, identifier:form_data; 269, string:'contact_address'; 270, subscript; 270, 271; 270, 272; 271, identifier:form_data; 272, string:'contact_city'; 273, subscript; 273, 274; 273, 275; 274, identifier:form_data; 275, string:'contact_state'; 276, subscript; 276, 277; 276, 278; 277, identifier:form_data; 278, string:'contact_phone'; 279, if_statement; 279, 280; 279, 285; 279, 286; 280, comparison_operator:==; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:controller; 283, identifier:email_options; 284, string:'2'; 285, comment; 286, block; 286, 287; 287, try_statement; 287, 288; 287, 309; 288, block; 288, 289; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:to; 292, list:[UserModel.objects.get(username=form.cleaned_data['to']).email]; 292, 293; 293, attribute; 293, 294; 293, 308; 294, call; 294, 295; 294, 300; 295, attribute; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:UserModel; 298, identifier:objects; 299, identifier:get; 300, argument_list; 300, 301; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:username; 303, subscript; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:form; 306, identifier:cleaned_data; 307, string:'to'; 308, identifier:email; 309, except_clause; 309, 310; 309, 311; 310, identifier:Exception; 311, block; 311, 312; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:to; 315, list:[form.cleaned_data['to']]; 315, 316; 316, subscript; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:form; 319, identifier:cleaned_data; 320, string:'to'; 321, if_statement; 321, 322; 321, 327; 322, comparison_operator:==; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:controller; 325, identifier:email_options; 326, string:'1'; 327, block; 327, 328; 327, 344; 328, expression_statement; 328, 329; 329, assignment; 329, 330; 329, 331; 330, identifier:to; 331, list_comprehension; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:r; 334, identifier:email; 335, for_in_clause; 335, 336; 335, 337; 336, identifier:r; 337, call; 337, 338; 337, 343; 338, attribute; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:controller; 341, identifier:recipients; 342, identifier:all; 343, argument_list; 344, for_statement; 344, 345; 344, 346; 344, 353; 345, identifier:r; 346, call; 346, 347; 346, 352; 347, attribute; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:controller; 350, identifier:other_recipients; 351, identifier:all; 352, argument_list; 353, block; 353, 354; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 359; 356, attribute; 356, 357; 356, 358; 357, identifier:to; 358, identifier:append; 359, argument_list; 359, 360; 360, attribute; 360, 361; 360, 362; 361, identifier:r; 362, identifier:email; 363, if_statement; 363, 364; 363, 369; 364, comparison_operator:in; 364, 365; 364, 366; 365, string:'send_a_copy'; 366, attribute; 366, 367; 366, 368; 367, identifier:form; 368, identifier:cleaned_data; 369, block; 369, 370; 370, expression_statement; 370, 371; 371, call; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:to; 374, identifier:append; 375, argument_list; 375, 376; 376, subscript; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:form; 379, identifier:cleaned_data; 380, string:'sender_email'; 381, expression_statement; 381, 382; 382, assignment; 382, 383; 382, 384; 383, identifier:mail; 384, call; 384, 385; 384, 386; 385, identifier:EmailMessage; 386, argument_list; 386, 387; 386, 390; 386, 393; 386, 400; 387, keyword_argument; 387, 388; 387, 389; 388, identifier:subject; 389, identifier:subject; 390, keyword_argument; 390, 391; 390, 392; 391, identifier:body; 392, identifier:body; 393, keyword_argument; 393, 394; 393, 395; 394, identifier:from_email; 395, subscript; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, identifier:form; 398, identifier:cleaned_data; 399, string:'sender_email'; 400, keyword_argument; 400, 401; 400, 402; 401, identifier:to; 402, identifier:to; 403, if_statement; 403, 404; 403, 409; 404, comparison_operator:in; 404, 405; 404, 406; 405, string:'photo'; 406, attribute; 406, 407; 406, 408; 407, identifier:request; 408, identifier:FILES; 409, block; 409, 410; 409, 418; 410, expression_statement; 410, 411; 411, assignment; 411, 412; 411, 413; 412, identifier:photo; 413, subscript; 413, 414; 413, 417; 414, attribute; 414, 415; 414, 416; 415, identifier:request; 416, identifier:FILES; 417, string:'photo'; 418, expression_statement; 418, 419; 419, call; 419, 420; 419, 423; 420, attribute; 420, 421; 420, 422; 421, identifier:mail; 422, identifier:attach; 423, argument_list; 423, 424; 423, 427; 423, 432; 424, attribute; 424, 425; 424, 426; 425, identifier:photo; 426, identifier:name; 427, call; 427, 428; 427, 431; 428, attribute; 428, 429; 428, 430; 429, identifier:photo; 430, identifier:read; 431, argument_list; 432, attribute; 432, 433; 432, 434; 433, identifier:photo; 434, identifier:content_type; 435, expression_statement; 435, 436; 436, call; 436, 437; 436, 440; 437, attribute; 437, 438; 437, 439; 438, identifier:mail; 439, identifier:send; 440, argument_list; 441, return_statement; 441, 442; 442, call; 442, 443; 442, 444; 443, identifier:render; 444, argument_list; 444, 445; 444, 446; 444, 447; 445, identifier:request; 446, string:'success_url'; 447, dictionary; 447, 448; 448, pair; 448, 449; 448, 450; 449, string:'controller'; 450, identifier:controller; 451, return_statement; 451, 452; 452, call; 452, 453; 452, 454; 453, identifier:render; 454, argument_list; 454, 455; 454, 456; 454, 457; 455, identifier:request; 456, string:'contact/form.html'; 457, dictionary; 457, 458; 457, 461; 457, 464; 458, pair; 458, 459; 458, 460; 459, string:'form'; 460, identifier:form; 461, pair; 461, 462; 461, 463; 462, string:'site'; 463, identifier:site; 464, pair; 464, 465; 464, 466; 465, string:'controller'; 466, identifier:controller | def build_contact(request, slug=""):
"""
Builds appropriate contact form based on options
set in the contact_form controller.
"""
controller = get_object_or_404(ContactFormController, slug=slug)
site = Site.objects.get_current()
UserModel = get_user_model()
user = request.user
form = ContactForm(request.POST or None, request.FILES or None, controller=controller)
# if we know, fill in the user name and email
if user.is_authenticated:
# first, resolve username for tango and non-tango sites
try:
name = user.display_name
except AttributeError:
name = user.username
form.fields['sender_name'].widget.attrs['readonly'] = 'true'
form.fields['sender_name'].initial = name
form.fields['sender_email'].widget.attrs['readonly'] = 'true'
form.fields['sender_email'].initial = user.email
if form.is_valid():
if controller.store_in_db:
# To do: sanitize submission.
new_msg = Contact(**form.cleaned_data)
new_msg.controller = controller
new_msg.site = site
if controller.override_subject: # we're overriding the subject
new_msg.subject = controller.override_subject
new_msg.save()
if controller.send_emails:
form_data = form.cleaned_data
if controller.override_subject:
subject = controller.override_subject
elif 'subject' in form_data:
subject = form_data['subject']
else:
subject = "{} message from {}".format(controller.name, form_data['sender_name'])
body = "{} \n\n {}".format(form_data['body'], form_data['sender_name'])
if controller.request_contact_info:
body += "\nAddress: {} \nCity: {} \nState: {} \nPhone: {}".format(
form_data['contact_address'],
form_data['contact_city'],
form_data['contact_state'],
form_data['contact_phone']
)
if controller.email_options == '2': # Create selectable list from recipients
try:
to = [UserModel.objects.get(username=form.cleaned_data['to']).email]
except Exception:
to = [form.cleaned_data['to']]
if controller.email_options == '1':
to = [r.email for r in controller.recipients.all()]
for r in controller.other_recipients.all():
to.append(r.email)
if 'send_a_copy' in form.cleaned_data:
to.append(form.cleaned_data['sender_email'])
mail = EmailMessage(
subject=subject,
body=body,
from_email=form.cleaned_data['sender_email'],
to=to
)
if 'photo' in request.FILES:
photo = request.FILES['photo']
mail.attach(photo.name, photo.read(), photo.content_type)
mail.send()
return render(request, 'success_url', {'controller': controller})
return render(request, 'contact/form.html', {
'form': form,
'site': site,
'controller': controller
}) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:stop_task; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:task_tag; 6, default_parameter; 6, 7; 6, 8; 7, identifier:stop_dependent; 8, True; 9, default_parameter; 9, 10; 9, 11; 10, identifier:stop_requirements; 11, False; 12, block; 12, 13; 12, 15; 12, 16; 12, 27; 12, 33; 12, 69; 12, 116; 12, 213; 12, 374; 12, 384; 12, 405; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:task; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:started_tasks; 23, argument_list; 23, 24; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:task_registry_id; 26, identifier:task_tag; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:task; 30, None; 31, block; 31, 32; 32, return_statement; 33, function_definition; 33, 34; 33, 35; 33, 37; 34, function_name:stop; 35, parameters; 35, 36; 36, identifier:task_to_stop; 37, block; 37, 38; 38, if_statement; 38, 39; 38, 44; 39, comparison_operator:in; 39, 40; 39, 41; 40, identifier:task_to_stop; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:__started; 44, block; 44, 45; 44, 60; 45, if_statement; 45, 46; 45, 53; 46, comparison_operator:is; 46, 47; 46, 52; 47, call; 47, 48; 47, 49; 48, identifier:isinstance; 49, argument_list; 49, 50; 49, 51; 50, identifier:task_to_stop; 51, identifier:WStoppableTask; 52, True; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:task_to_stop; 58, identifier:stop; 59, argument_list; 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:__started; 66, identifier:remove; 67, argument_list; 67, 68; 68, identifier:task_to_stop; 69, function_definition; 69, 70; 69, 71; 69, 73; 70, function_name:stop_dependency; 71, parameters; 71, 72; 72, identifier:task_to_stop; 73, block; 73, 74; 73, 78; 73, 102; 73, 111; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:deeper_dependencies; 77, list:[]; 78, for_statement; 78, 79; 78, 80; 78, 83; 79, identifier:dependent_task; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:__started; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 94; 85, comparison_operator:in; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:task_to_stop; 88, identifier:__registry_tag__; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:dependent_task; 92, identifier:__class__; 93, identifier:__dependency__; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:deeper_dependencies; 99, identifier:append; 100, argument_list; 100, 101; 101, identifier:dependent_task; 102, for_statement; 102, 103; 102, 104; 102, 105; 103, identifier:dependent_task; 104, identifier:deeper_dependencies; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:stop_dependency; 109, argument_list; 109, 110; 110, identifier:dependent_task; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:stop; 114, argument_list; 114, 115; 115, identifier:task_to_stop; 116, function_definition; 116, 117; 116, 118; 116, 123; 117, function_name:calculate_requirements; 118, parameters; 118, 119; 118, 120; 119, identifier:task_to_stop; 120, default_parameter; 120, 121; 120, 122; 121, identifier:cross_requirements; 122, False; 123, block; 123, 124; 123, 130; 123, 156; 123, 163; 123, 169; 123, 211; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:requirements; 127, call; 127, 128; 127, 129; 128, identifier:set; 129, argument_list; 130, for_statement; 130, 131; 130, 132; 130, 135; 131, identifier:dependent_task; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:__started; 135, block; 135, 136; 136, if_statement; 136, 137; 136, 148; 137, comparison_operator:in; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:dependent_task; 141, identifier:__class__; 142, identifier:__registry_tag__; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:task_to_stop; 146, identifier:__class__; 147, identifier:__dependency__; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:requirements; 153, identifier:add; 154, argument_list; 154, 155; 155, identifier:dependent_task; 156, if_statement; 156, 157; 156, 160; 157, comparison_operator:is; 157, 158; 157, 159; 158, identifier:cross_requirements; 159, True; 160, block; 160, 161; 161, return_statement; 161, 162; 162, identifier:requirements; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:result; 166, call; 166, 167; 166, 168; 167, identifier:set; 168, argument_list; 169, for_statement; 169, 170; 169, 171; 169, 172; 170, identifier:task_a; 171, identifier:requirements; 172, block; 172, 173; 172, 177; 172, 199; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:requirement_match; 176, False; 177, for_statement; 177, 178; 177, 179; 177, 180; 178, identifier:task_b; 179, identifier:requirements; 180, block; 180, 181; 181, if_statement; 181, 182; 181, 193; 182, comparison_operator:in; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:task_a; 186, identifier:__class__; 187, identifier:__registry_tag__; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:task_b; 191, identifier:__class__; 192, identifier:__dependency__; 193, block; 193, 194; 193, 198; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:requirement_match; 197, True; 198, break_statement; 199, if_statement; 199, 200; 199, 203; 200, comparison_operator:is; 200, 201; 200, 202; 201, identifier:requirement_match; 202, False; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:result; 208, identifier:add; 209, argument_list; 209, 210; 210, identifier:task_a; 211, return_statement; 211, 212; 212, identifier:result; 213, function_definition; 213, 214; 213, 215; 213, 225; 214, function_name:calculate_priorities; 215, parameters; 215, 216; 215, 217; 215, 219; 215, 222; 216, identifier:task_to_stop; 217, list_splat_pattern; 217, 218; 218, identifier:extra_tasks; 219, default_parameter; 219, 220; 219, 221; 220, identifier:current_result; 221, None; 222, default_parameter; 222, 223; 222, 224; 223, identifier:requirements_left; 224, None; 225, block; 225, 226; 225, 235; 225, 240; 225, 255; 225, 265; 225, 277; 225, 286; 225, 319; 225, 328; 225, 349; 225, 359; 226, if_statement; 226, 227; 226, 230; 227, comparison_operator:is; 227, 228; 227, 229; 228, identifier:current_result; 229, None; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:current_result; 234, list:[]; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:tasks_to_stop; 238, list:[task_to_stop]; 238, 239; 239, identifier:task_to_stop; 240, if_statement; 240, 241; 240, 247; 241, comparison_operator:>; 241, 242; 241, 246; 242, call; 242, 243; 242, 244; 243, identifier:len; 244, argument_list; 244, 245; 245, identifier:extra_tasks; 246, integer:0; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:tasks_to_stop; 252, identifier:extend; 253, argument_list; 253, 254; 254, identifier:extra_tasks; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:current_result; 259, identifier:append; 260, argument_list; 260, 261; 261, call; 261, 262; 261, 263; 262, identifier:list; 263, argument_list; 263, 264; 264, identifier:tasks_to_stop; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:all_requirements; 268, call; 268, 269; 268, 270; 269, identifier:calculate_requirements; 270, argument_list; 270, 271; 270, 274; 271, subscript; 271, 272; 271, 273; 272, identifier:tasks_to_stop; 273, integer:0; 274, keyword_argument; 274, 275; 274, 276; 275, identifier:cross_requirements; 276, True; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:nested_requirements; 280, call; 280, 281; 280, 282; 281, identifier:calculate_requirements; 282, argument_list; 282, 283; 283, subscript; 283, 284; 283, 285; 284, identifier:tasks_to_stop; 285, integer:0; 286, for_statement; 286, 287; 286, 288; 286, 293; 287, identifier:dependent_task; 288, subscript; 288, 289; 288, 290; 289, identifier:tasks_to_stop; 290, slice; 290, 291; 290, 292; 291, integer:1; 292, colon; 293, block; 293, 294; 293, 306; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:nested_requirements; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:nested_requirements; 300, identifier:union; 301, argument_list; 301, 302; 302, call; 302, 303; 302, 304; 303, identifier:calculate_requirements; 304, argument_list; 304, 305; 305, identifier:dependent_task; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:all_requirements; 310, identifier:update; 311, argument_list; 311, 312; 312, call; 312, 313; 312, 314; 313, identifier:calculate_requirements; 314, argument_list; 314, 315; 314, 316; 315, identifier:dependent_task; 316, keyword_argument; 316, 317; 316, 318; 317, identifier:cross_requirements; 318, True; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:all_requirements; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:all_requirements; 325, identifier:difference; 326, argument_list; 326, 327; 327, identifier:nested_requirements; 328, if_statement; 328, 329; 328, 332; 329, comparison_operator:is; 329, 330; 329, 331; 330, identifier:requirements_left; 331, None; 332, block; 332, 333; 332, 342; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:requirements_left; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:requirements_left; 339, identifier:difference; 340, argument_list; 340, 341; 341, identifier:all_requirements; 342, expression_statement; 342, 343; 343, call; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:nested_requirements; 346, identifier:update; 347, argument_list; 347, 348; 348, identifier:requirements_left; 349, if_statement; 349, 350; 349, 356; 350, comparison_operator:==; 350, 351; 350, 355; 351, call; 351, 352; 351, 353; 352, identifier:len; 353, argument_list; 353, 354; 354, identifier:nested_requirements; 355, integer:0; 356, block; 356, 357; 357, return_statement; 357, 358; 358, identifier:current_result; 359, return_statement; 359, 360; 360, call; 360, 361; 360, 362; 361, identifier:calculate_priorities; 362, argument_list; 362, 363; 362, 368; 362, 371; 363, list_splat; 363, 364; 364, call; 364, 365; 364, 366; 365, identifier:list; 366, argument_list; 366, 367; 367, identifier:nested_requirements; 368, keyword_argument; 368, 369; 368, 370; 369, identifier:current_result; 370, identifier:current_result; 371, keyword_argument; 371, 372; 371, 373; 372, identifier:requirements_left; 373, identifier:all_requirements; 374, if_statement; 374, 375; 374, 378; 375, comparison_operator:is; 375, 376; 375, 377; 376, identifier:stop_dependent; 377, True; 378, block; 378, 379; 379, expression_statement; 379, 380; 380, call; 380, 381; 380, 382; 381, identifier:stop_dependency; 382, argument_list; 382, 383; 383, identifier:task; 384, if_statement; 384, 385; 384, 388; 385, comparison_operator:is; 385, 386; 385, 387; 386, identifier:stop_requirements; 387, True; 388, block; 388, 389; 389, for_statement; 389, 390; 389, 391; 389, 395; 390, identifier:task_list; 391, call; 391, 392; 391, 393; 392, identifier:calculate_priorities; 393, argument_list; 393, 394; 394, identifier:task; 395, block; 395, 396; 396, for_statement; 396, 397; 396, 398; 396, 399; 397, identifier:single_task; 398, identifier:task_list; 399, block; 399, 400; 400, expression_statement; 400, 401; 401, call; 401, 402; 401, 403; 402, identifier:stop; 403, argument_list; 403, 404; 404, identifier:single_task; 405, if_statement; 405, 406; 405, 409; 405, 410; 406, comparison_operator:is; 406, 407; 406, 408; 407, identifier:stop_dependent; 408, True; 409, comment; 410, block; 410, 411; 411, expression_statement; 411, 412; 412, call; 412, 413; 412, 414; 413, identifier:stop; 414, argument_list; 414, 415; 415, identifier:task | def stop_task(self, task_tag, stop_dependent=True, stop_requirements=False):
""" Stop task with the given task tag. If task already stopped, then nothing happens.
:param task_tag: task to stop
:param stop_dependent: if True, then every task, that require the given task as dependency, will be \
stopped before.
:param stop_requirements: if True, then every task, that is required as dependency for the given task, \
will be stopped after.
:return: None
"""
# TODO: "coverage" requires more tests
task = self.started_tasks(task_registry_id=task_tag)
if task is None:
return
def stop(task_to_stop):
if task_to_stop in self.__started:
if isinstance(task_to_stop, WStoppableTask) is True:
task_to_stop.stop()
self.__started.remove(task_to_stop)
def stop_dependency(task_to_stop):
deeper_dependencies = []
for dependent_task in self.__started:
if task_to_stop.__registry_tag__ in dependent_task.__class__.__dependency__:
deeper_dependencies.append(dependent_task)
for dependent_task in deeper_dependencies:
stop_dependency(dependent_task)
stop(task_to_stop)
def calculate_requirements(task_to_stop, cross_requirements=False):
requirements = set()
for dependent_task in self.__started:
if dependent_task.__class__.__registry_tag__ in task_to_stop.__class__.__dependency__:
requirements.add(dependent_task)
if cross_requirements is True:
return requirements
result = set()
for task_a in requirements:
requirement_match = False
for task_b in requirements:
if task_a.__class__.__registry_tag__ in task_b.__class__.__dependency__:
requirement_match = True
break
if requirement_match is False:
result.add(task_a)
return result
def calculate_priorities(task_to_stop, *extra_tasks, current_result=None, requirements_left=None):
if current_result is None:
current_result = []
tasks_to_stop = [task_to_stop]
if len(extra_tasks) > 0:
tasks_to_stop.extend(extra_tasks)
current_result.append(list(tasks_to_stop))
all_requirements = calculate_requirements(tasks_to_stop[0], cross_requirements=True)
nested_requirements = calculate_requirements(tasks_to_stop[0])
for dependent_task in tasks_to_stop[1:]:
nested_requirements = nested_requirements.union(calculate_requirements(dependent_task))
all_requirements.update(calculate_requirements(dependent_task, cross_requirements=True))
all_requirements = all_requirements.difference(nested_requirements)
if requirements_left is not None:
requirements_left = requirements_left.difference(all_requirements)
nested_requirements.update(requirements_left)
if len(nested_requirements) == 0:
return current_result
return calculate_priorities(
*list(nested_requirements), current_result=current_result, requirements_left=all_requirements
)
if stop_dependent is True:
stop_dependency(task)
if stop_requirements is True:
for task_list in calculate_priorities(task):
for single_task in task_list:
stop(single_task)
if stop_dependent is not True: # check if we've already stopped this task
stop(task) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:diffs2persistence; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:rev_docs; 5, default_parameter; 5, 6; 5, 7; 6, identifier:window_size; 7, integer:50; 8, default_parameter; 8, 9; 8, 10; 9, identifier:revert_radius; 10, integer:15; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sunset; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:verbose; 16, False; 17, block; 17, 18; 17, 20; 17, 31; 17, 38; 17, 45; 17, 65; 17, 66; 17, 83; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:rev_docs; 23, call; 23, 24; 23, 29; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:mwxml; 27, identifier:utilities; 28, identifier:normalize; 29, argument_list; 29, 30; 30, identifier:rev_docs; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:window_size; 34, call; 34, 35; 34, 36; 35, identifier:int; 36, argument_list; 36, 37; 37, identifier:window_size; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:revert_radius; 41, call; 41, 42; 41, 43; 42, identifier:int; 43, argument_list; 43, 44; 44, identifier:revert_radius; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:sunset; 48, conditional_expression:if; 48, 49; 48, 53; 48, 56; 48, 57; 49, call; 49, 50; 49, 51; 50, identifier:Timestamp; 51, argument_list; 51, 52; 52, identifier:sunset; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:sunset; 55, None; 56, line_continuation:\; 57, call; 57, 58; 57, 59; 58, identifier:Timestamp; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:time; 63, identifier:time; 64, argument_list; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:page_docs; 69, call; 69, 70; 69, 71; 70, identifier:groupby; 71, argument_list; 71, 72; 71, 73; 72, identifier:rev_docs; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:key; 75, lambda; 75, 76; 75, 78; 76, lambda_parameters; 76, 77; 77, identifier:d; 78, subscript; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:d; 81, string:'page'; 82, string:'title'; 83, for_statement; 83, 84; 83, 87; 83, 88; 84, pattern_list; 84, 85; 84, 86; 85, identifier:page_title; 86, identifier:rev_docs; 87, identifier:page_docs; 88, block; 88, 89; 88, 103; 88, 104; 88, 111; 88, 112; 88, 121; 88, 122; 88, 131; 88, 307; 88, 364; 89, if_statement; 89, 90; 89, 91; 90, identifier:verbose; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:sys; 97, identifier:stderr; 98, identifier:write; 99, argument_list; 99, 100; 100, binary_operator:+; 100, 101; 100, 102; 101, identifier:page_title; 102, string:": "; 103, comment; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:rev_docs; 107, call; 107, 108; 107, 109; 108, identifier:peekable; 109, argument_list; 109, 110; 110, identifier:rev_docs; 111, comment; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:window; 115, call; 115, 116; 115, 117; 116, identifier:deque; 117, argument_list; 117, 118; 118, keyword_argument; 118, 119; 118, 120; 119, identifier:maxlen; 120, identifier:window_size; 121, comment; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:state; 125, call; 125, 126; 125, 127; 126, identifier:DiffState; 127, argument_list; 127, 128; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:revert_radius; 130, identifier:revert_radius; 131, while_statement; 131, 132; 131, 133; 132, identifier:rev_docs; 133, block; 133, 134; 133, 141; 133, 150; 133, 185; 133, 206; 133, 231; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:rev_doc; 137, call; 137, 138; 137, 139; 138, identifier:next; 139, argument_list; 139, 140; 140, identifier:rev_docs; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:next_doc; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:rev_docs; 147, identifier:peek; 148, argument_list; 148, 149; 149, None; 150, if_statement; 150, 151; 150, 154; 150, 172; 151, comparison_operator:is; 151, 152; 151, 153; 152, identifier:next_doc; 153, None; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:seconds_visible; 158, binary_operator:-; 158, 159; 158, 165; 158, 166; 159, call; 159, 160; 159, 161; 160, identifier:Timestamp; 161, argument_list; 161, 162; 162, subscript; 162, 163; 162, 164; 163, identifier:next_doc; 164, string:'timestamp'; 165, line_continuation:\; 166, call; 166, 167; 166, 168; 167, identifier:Timestamp; 168, argument_list; 168, 169; 169, subscript; 169, 170; 169, 171; 170, identifier:rev_doc; 171, string:'timestamp'; 172, else_clause; 172, 173; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:seconds_visible; 177, binary_operator:-; 177, 178; 177, 179; 178, identifier:sunset; 179, call; 179, 180; 179, 181; 180, identifier:Timestamp; 181, argument_list; 181, 182; 182, subscript; 182, 183; 182, 184; 183, identifier:rev_doc; 184, string:'timestamp'; 185, if_statement; 185, 186; 185, 189; 186, comparison_operator:<; 186, 187; 186, 188; 187, identifier:seconds_visible; 188, integer:0; 189, block; 189, 190; 189, 202; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:logger; 194, identifier:warn; 195, argument_list; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, string:"Seconds visible {0} is less than zero."; 199, identifier:format; 200, argument_list; 200, 201; 201, identifier:seconds_visible; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:seconds_visible; 205, integer:0; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 212; 207, 213; 208, pattern_list; 208, 209; 208, 210; 208, 211; 209, identifier:_; 210, identifier:tokens_added; 211, identifier:_; 212, line_continuation:\; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:state; 216, identifier:update_opdocs; 217, argument_list; 217, 218; 217, 221; 217, 226; 218, subscript; 218, 219; 218, 220; 219, identifier:rev_doc; 220, string:'sha1'; 221, subscript; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:rev_doc; 224, string:'diff'; 225, string:'ops'; 226, tuple; 226, 227; 226, 230; 227, subscript; 227, 228; 227, 229; 228, identifier:rev_doc; 229, string:'user'; 230, identifier:seconds_visible; 231, if_statement; 231, 232; 231, 238; 231, 239; 231, 296; 232, comparison_operator:==; 232, 233; 232, 237; 233, call; 233, 234; 233, 235; 234, identifier:len; 235, argument_list; 235, 236; 236, identifier:window; 237, identifier:window_size; 238, comment; 239, block; 239, 240; 239, 248; 239, 257; 239, 267; 239, 273; 239, 276; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 245; 242, pattern_list; 242, 243; 242, 244; 243, identifier:old_doc; 244, identifier:old_added; 245, subscript; 245, 246; 245, 247; 246, identifier:window; 247, integer:0; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:window; 252, identifier:append; 253, argument_list; 253, 254; 254, tuple; 254, 255; 254, 256; 255, identifier:rev_doc; 256, identifier:tokens_added; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:persistence; 260, call; 260, 261; 260, 262; 261, identifier:token_persistence; 262, argument_list; 262, 263; 262, 264; 262, 265; 262, 266; 263, identifier:old_doc; 264, identifier:old_added; 265, identifier:window; 266, None; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 272; 269, subscript; 269, 270; 269, 271; 270, identifier:old_doc; 271, string:'persistence'; 272, identifier:persistence; 273, expression_statement; 273, 274; 274, yield; 274, 275; 275, identifier:old_doc; 276, if_statement; 276, 277; 276, 278; 277, identifier:verbose; 278, block; 278, 279; 278, 288; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 286; 281, attribute; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:sys; 284, identifier:stderr; 285, identifier:write; 286, argument_list; 286, 287; 287, string:"."; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 295; 290, attribute; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:sys; 293, identifier:stderr; 294, identifier:flush; 295, argument_list; 296, else_clause; 296, 297; 297, block; 297, 298; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:window; 302, identifier:append; 303, argument_list; 303, 304; 304, tuple; 304, 305; 304, 306; 305, identifier:rev_doc; 306, identifier:tokens_added; 307, while_statement; 307, 308; 307, 314; 308, comparison_operator:>; 308, 309; 308, 313; 309, call; 309, 310; 309, 311; 310, identifier:len; 311, argument_list; 311, 312; 312, identifier:window; 313, integer:0; 314, block; 314, 315; 314, 325; 314, 335; 314, 341; 314, 344; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 320; 317, pattern_list; 317, 318; 317, 319; 318, identifier:old_doc; 319, identifier:old_added; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:window; 323, identifier:popleft; 324, argument_list; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:persistence; 328, call; 328, 329; 328, 330; 329, identifier:token_persistence; 330, argument_list; 330, 331; 330, 332; 330, 333; 330, 334; 331, identifier:old_doc; 332, identifier:old_added; 333, identifier:window; 334, identifier:sunset; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 340; 337, subscript; 337, 338; 337, 339; 338, identifier:old_doc; 339, string:'persistence'; 340, identifier:persistence; 341, expression_statement; 341, 342; 342, yield; 342, 343; 343, identifier:old_doc; 344, if_statement; 344, 345; 344, 346; 345, identifier:verbose; 346, block; 346, 347; 346, 356; 347, expression_statement; 347, 348; 348, call; 348, 349; 348, 354; 349, attribute; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:sys; 352, identifier:stderr; 353, identifier:write; 354, argument_list; 354, 355; 355, string:"_"; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 363; 358, attribute; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:sys; 361, identifier:stderr; 362, identifier:flush; 363, argument_list; 364, if_statement; 364, 365; 364, 366; 365, identifier:verbose; 366, block; 366, 367; 367, expression_statement; 367, 368; 368, call; 368, 369; 368, 374; 369, attribute; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:sys; 372, identifier:stderr; 373, identifier:write; 374, argument_list; 374, 375; 375, string:"\n" | def diffs2persistence(rev_docs, window_size=50, revert_radius=15, sunset=None,
verbose=False):
"""
Processes a sorted and page-partitioned sequence of revision documents into
and adds a 'persistence' field to them containing statistics about how each
token "added" in the revision persisted through future revisions.
:Parameters:
rev_docs : `iterable` ( `dict` )
JSON documents of revision data containing a 'diff' field as
generated by ``dump2diffs``. It's assumed that rev_docs are
partitioned by page and otherwise in chronological order.
window_size : `int`
The size of the window of revisions from which persistence data
will be generated.
revert_radius : `int`
The number of revisions back that a revert can reference.
sunset : :class:`mwtypes.Timestamp`
The date of the database dump we are generating from. This is
used to apply a 'time visible' statistic. If not set, now() will
be assumed.
keep_diff : `bool`
Do not drop the `diff` field from the revision document after
processing is complete.
verbose : `bool`
Prints out dots and stuff to stderr
:Returns:
A generator of rev_docs with a 'persistence' field containing
statistics about individual tokens.
"""
rev_docs = mwxml.utilities.normalize(rev_docs)
window_size = int(window_size)
revert_radius = int(revert_radius)
sunset = Timestamp(sunset) if sunset is not None \
else Timestamp(time.time())
# Group the docs by page
page_docs = groupby(rev_docs, key=lambda d: d['page']['title'])
for page_title, rev_docs in page_docs:
if verbose:
sys.stderr.write(page_title + ": ")
# We need a look-ahead to know how long this revision was visible
rev_docs = peekable(rev_docs)
# The window allows us to manage memory
window = deque(maxlen=window_size)
# The state does the actual processing work
state = DiffState(revert_radius=revert_radius)
while rev_docs:
rev_doc = next(rev_docs)
next_doc = rev_docs.peek(None)
if next_doc is not None:
seconds_visible = Timestamp(next_doc['timestamp']) - \
Timestamp(rev_doc['timestamp'])
else:
seconds_visible = sunset - Timestamp(rev_doc['timestamp'])
if seconds_visible < 0:
logger.warn("Seconds visible {0} is less than zero."
.format(seconds_visible))
seconds_visible = 0
_, tokens_added, _ = \
state.update_opdocs(rev_doc['sha1'], rev_doc['diff']['ops'],
(rev_doc['user'], seconds_visible))
if len(window) == window_size:
# Time to start writing some stats
old_doc, old_added = window[0]
window.append((rev_doc, tokens_added))
persistence = token_persistence(old_doc, old_added, window,
None)
old_doc['persistence'] = persistence
yield old_doc
if verbose:
sys.stderr.write(".")
sys.stderr.flush()
else:
window.append((rev_doc, tokens_added))
while len(window) > 0:
old_doc, old_added = window.popleft()
persistence = token_persistence(old_doc, old_added, window, sunset)
old_doc['persistence'] = persistence
yield old_doc
if verbose:
sys.stderr.write("_")
sys.stderr.flush()
if verbose:
sys.stderr.write("\n") |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:qteAddWidget; 3, parameters; 3, 4; 3, 5; 3, 11; 3, 16; 3, 21; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:widgetObj; 7, type; 7, 8; 8, attribute; 8, 9; 8, 10; 9, identifier:QtGui; 10, identifier:QWidget; 11, typed_default_parameter; 11, 12; 11, 13; 11, 15; 12, identifier:isFocusable; 13, type; 13, 14; 14, identifier:bool; 15, True; 16, typed_default_parameter; 16, 17; 16, 18; 16, 20; 17, identifier:widgetSignature; 18, type; 18, 19; 19, identifier:str; 20, None; 21, typed_default_parameter; 21, 22; 21, 23; 21, 25; 22, identifier:autoBind; 23, type; 23, 24; 24, identifier:bool; 25, True; 26, block; 26, 27; 26, 29; 26, 30; 26, 31; 26, 32; 26, 33; 26, 45; 26, 57; 26, 58; 26, 66; 26, 67; 26, 79; 26, 80; 26, 81; 26, 108; 26, 109; 26, 110; 26, 111; 26, 115; 26, 125; 26, 135; 26, 137; 26, 138; 26, 139; 26, 140; 26, 141; 26, 152; 26, 153; 26, 154; 26, 155; 26, 161; 26, 296; 27, expression_statement; 27, 28; 28, comment; 29, comment; 30, comment; 31, comment; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:widgetObj; 37, identifier:_qteAdmin; 38, call; 38, 39; 38, 40; 39, identifier:QtmacsAdminStructure; 40, argument_list; 40, 41; 40, 42; 41, identifier:self; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:isFocusable; 44, identifier:isFocusable; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:widgetObj; 50, identifier:_qteAdmin; 51, identifier:appletID; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:_qteAdmin; 56, identifier:appletID; 57, comment; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:widgetObj; 63, identifier:_qteAdmin; 64, identifier:isQtmacsApplet; 65, False; 66, comment; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:widgetObj; 72, identifier:_qteAdmin; 73, identifier:appletSignature; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:qteAppletSignature; 78, argument_list; 79, comment; 80, comment; 81, if_statement; 81, 82; 81, 85; 81, 98; 82, comparison_operator:is; 82, 83; 82, 84; 83, identifier:widgetSignature; 84, None; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:widgetObj; 91, identifier:_qteAdmin; 92, identifier:widgetSignature; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:widgetObj; 96, identifier:__class__; 97, identifier:__name__; 98, else_clause; 98, 99; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:widgetObj; 105, identifier:_qteAdmin; 106, identifier:widgetSignature; 107, identifier:widgetSignature; 108, comment; 109, comment; 110, comment; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:wo; 114, identifier:widgetObj; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:wo; 119, identifier:qteSignature; 120, attribute; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:wo; 123, identifier:_qteAdmin; 124, identifier:widgetSignature; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:wo; 129, identifier:qteSetKeyFilterPolicy; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:wo; 133, identifier:_qteAdmin; 134, identifier:qteSetKeyFilterPolicy; 135, delete_statement; 135, 136; 136, identifier:wo; 137, comment; 138, comment; 139, comment; 140, comment; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 150; 143, attribute; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:_qteAdmin; 148, identifier:widgetList; 149, identifier:append; 150, argument_list; 150, 151; 151, identifier:widgetObj; 152, comment; 153, comment; 154, comment; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:default_bind; 158, attribute; 158, 159; 158, 160; 159, identifier:qte_global; 160, identifier:default_widget_keybindings; 161, if_statement; 161, 162; 161, 170; 161, 171; 162, boolean_operator:and; 162, 163; 162, 164; 163, identifier:autoBind; 164, parenthesized_expression; 164, 165; 165, comparison_operator:in; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:widgetObj; 168, identifier:qteSignature; 169, identifier:default_bind; 170, comment; 171, block; 171, 172; 171, 180; 171, 181; 171, 182; 171, 219; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:module_name; 175, subscript; 175, 176; 175, 177; 176, identifier:default_bind; 177, attribute; 177, 178; 177, 179; 178, identifier:widgetObj; 179, identifier:qteSignature; 180, comment; 181, comment; 182, try_statement; 182, 183; 182, 193; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:mod; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:importlib; 190, identifier:import_module; 191, argument_list; 191, 192; 192, identifier:module_name; 193, except_clause; 193, 194; 193, 195; 194, identifier:ImportError; 195, block; 195, 196; 195, 206; 195, 218; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:msg; 199, parenthesized_expression; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, string:'Module <b>{}</b> could not be imported.'; 203, identifier:format; 204, argument_list; 204, 205; 205, identifier:module_name; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 213; 208, attribute; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:qteLogger; 212, identifier:exception; 213, argument_list; 213, 214; 213, 215; 214, identifier:msg; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:stack_info; 217, True; 218, return_statement; 219, if_statement; 219, 220; 219, 225; 219, 226; 219, 227; 219, 228; 219, 229; 219, 230; 219, 231; 219, 272; 220, call; 220, 221; 220, 222; 221, identifier:hasattr; 222, argument_list; 222, 223; 222, 224; 223, identifier:mod; 224, string:'install_macros_and_bindings'; 225, comment; 226, comment; 227, comment; 228, comment; 229, comment; 230, comment; 231, block; 231, 232; 232, try_statement; 232, 233; 232, 241; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:mod; 238, identifier:install_macros_and_bindings; 239, argument_list; 239, 240; 240, identifier:widgetObj; 241, except_clause; 241, 242; 241, 243; 242, identifier:Exception; 243, block; 243, 244; 243, 251; 243, 260; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:msg; 247, parenthesized_expression; 247, 248; 248, concatenated_string; 248, 249; 248, 250; 249, string:'<b>install_macros_and_bindings</b> function'; 250, string:' in <b>{}</b> did not execute properly.'; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:msg; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:msg; 257, identifier:format; 258, argument_list; 258, 259; 259, identifier:module_name; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 267; 262, attribute; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:self; 265, identifier:qteLogger; 266, identifier:error; 267, argument_list; 267, 268; 267, 269; 268, identifier:msg; 269, keyword_argument; 269, 270; 269, 271; 270, identifier:stack_info; 271, True; 272, else_clause; 272, 273; 273, block; 273, 274; 273, 287; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:msg; 277, parenthesized_expression; 277, 278; 278, call; 278, 279; 278, 285; 279, attribute; 279, 280; 279, 284; 280, concatenated_string; 280, 281; 280, 282; 280, 283; 281, string:'Module <b>{}</b> has no '; 282, string:'<b>install_macros_and_bindings</b>'; 283, string:' method'; 284, identifier:format; 285, argument_list; 285, 286; 286, identifier:module_name; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 294; 289, attribute; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:qteLogger; 293, identifier:error; 294, argument_list; 294, 295; 295, identifier:msg; 296, return_statement; 296, 297; 297, identifier:widgetObj | def qteAddWidget(self, widgetObj: QtGui.QWidget, isFocusable: bool=True,
widgetSignature: str=None, autoBind: bool=True):
"""
Augment the standard Qt ``widgetObj`` with Qtmacs specific fields.
Example: from a programmers perspective there is no difference
between::
wid = QtGui.QTextEdit(self)
and::
wid = self.qteAddWidget(QtGui.QTextEdit(self))
Both return a handle to a Qt widget (a ``QTextEdit`` in this
case). However, the ``qteAddWidget`` adds the following fields
to the object:
* ``_qteAdmin``: this is an instance of the ``QtmacsAdminStructure``
to tell Qtmacs how to treat the widget.
* ``qteSignature``: an attribute that returns the signature of the
widget and equals ``widgetSignature``. If no such signature was
specified it defaults to the Qt internal name as a string, eg.
for a push button this would be 'QPushButton'.
* ``qteSetKeyFilterPolicy``: this points directly to the equally
named method inside the _qteAdmin object. This is a convenience
shortcut to avoid using the _qteAdmin structure directly in
macro/applet code, because only Qtmacs itself should temper
with it.
|Args|
* ``widgetObj`` (**QWidget**): any widget from the QtGui library.
* ``isFocusable`` (**bool**): whether or not the widget can
receive the focus.
* ``widgetSignature`` (**str**): specify the widget signature
(defaults to class name)
* ``autoBind`` (**bool**): if **True** and ``widgetSignature``
is a recognisable name (eg. **QTextEdit**) then automatically
load the appropriate key-bindings for this widget.
|Returns|
* **QWidget**: handle to widget object (or **None** if it could
not be added).
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
"""
# Add a Qtmacs data structure to the widget to allow their
# event administration. Note that, in all likelihood, the
# widget is an arbitrary Qt widget (eg. QLineEdit,
# QPushButton, etc).
widgetObj._qteAdmin = QtmacsAdminStructure(
self, isFocusable=isFocusable)
widgetObj._qteAdmin.appletID = self._qteAdmin.appletID
# Specify that this widget is not a QtmacsApplet.
widgetObj._qteAdmin.isQtmacsApplet = False
# Remember the signature of the applet containing this widget.
widgetObj._qteAdmin.appletSignature = self.qteAppletSignature()
# Set the widget signature. If none was specified, use the
# class name (eg. QLineEdit).
if widgetSignature is None:
widgetObj._qteAdmin.widgetSignature = widgetObj.__class__.__name__
else:
widgetObj._qteAdmin.widgetSignature = widgetSignature
# For convenience, as it is otherwise difficult for the macro
# programmer to determine the widget signature used by Qtmacs.
# Note: the "wo" is only a shorthand to avoid too long lines.
wo = widgetObj
wo.qteSignature = wo._qteAdmin.widgetSignature
wo.qteSetKeyFilterPolicy = wo._qteAdmin.qteSetKeyFilterPolicy
del wo
# Add the widget to the widgetList of this QtmacsApplet.
# Important: this MUST happen before macros and key-bindings are loaded
# and bound automatically (see code below) because the method to
# bind the keys will verify that the widget exists in ``widgetList``.
self._qteAdmin.widgetList.append(widgetObj)
# If a widget has a default key-bindings file then the global
# dictionary ``default_widget_keybindings`` will contain its
# name.
default_bind = qte_global.default_widget_keybindings
if autoBind and (widgetObj.qteSignature in default_bind):
# Shorthand.
module_name = default_bind[widgetObj.qteSignature]
# Import the module with the default key-bindings for the
# current widget type.
try:
mod = importlib.import_module(module_name)
except ImportError:
msg = ('Module <b>{}</b> could not be imported.'.format(
module_name))
self.qteLogger.exception(msg, stack_info=True)
return
if hasattr(mod, 'install_macros_and_bindings'):
# By convention, the module has an
# install_macros_and_bindings method. If an error
# occurs intercept it, but do not abort the method
# since the error only relates to a failed attempt to
# apply default key-bindings, not to register the
# widget (the main purpose of this method).
try:
mod.install_macros_and_bindings(widgetObj)
except Exception:
msg = ('<b>install_macros_and_bindings</b> function'
' in <b>{}</b> did not execute properly.')
msg = msg.format(module_name)
self.qteLogger.error(msg, stack_info=True)
else:
msg = ('Module <b>{}</b> has no '
'<b>install_macros_and_bindings</b>'
' method'.format(module_name))
self.qteLogger.error(msg)
return widgetObj |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:qteSetWidgetFocusOrder; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:widList; 7, type; 7, 8; 8, identifier:tuple; 9, block; 9, 10; 9, 12; 9, 13; 9, 22; 9, 23; 9, 29; 9, 30; 9, 42; 9, 43; 9, 74; 9, 75; 9, 82; 9, 102; 9, 103; 9, 104; 9, 113; 9, 114; 9, 115; 9, 116; 9, 117; 9, 118; 9, 137; 9, 138; 9, 155; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 20; 14, comparison_operator:<; 14, 15; 14, 19; 15, call; 15, 16; 15, 17; 16, identifier:len; 17, argument_list; 17, 18; 18, identifier:widList; 19, integer:2; 20, block; 20, 21; 21, return_statement; 22, comment; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:qteAutoremoveDeletedWidgets; 28, argument_list; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:widList; 33, list_comprehension; 33, 34; 33, 35; 33, 38; 34, identifier:_; 35, for_in_clause; 35, 36; 35, 37; 36, identifier:_; 37, identifier:widList; 38, if_clause; 38, 39; 39, comparison_operator:is; 39, 40; 39, 41; 40, identifier:_; 41, None; 42, comment; 43, for_statement; 43, 44; 43, 45; 43, 46; 44, identifier:wid; 45, identifier:widList; 46, block; 46, 47; 47, if_statement; 47, 48; 47, 55; 48, comparison_operator:not; 48, 49; 48, 50; 49, identifier:wid; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:_qteAdmin; 54, identifier:widgetList; 55, block; 55, 56; 55, 60; 55, 64; 55, 73; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:msg; 59, string:'Cannot change focus order because some '; 60, expression_statement; 60, 61; 61, augmented_assignment:+=; 61, 62; 61, 63; 62, identifier:msg; 63, string:'widgets do not exist.'; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:qteLogger; 70, identifier:warning; 71, argument_list; 71, 72; 72, identifier:msg; 73, return_statement; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:newList; 78, list:[widList[0]]; 78, 79; 79, subscript; 79, 80; 79, 81; 80, identifier:widList; 81, integer:0; 82, for_statement; 82, 83; 82, 84; 82, 89; 83, identifier:wid; 84, subscript; 84, 85; 84, 86; 85, identifier:widList; 86, slice; 86, 87; 86, 88; 87, integer:1; 88, colon; 89, block; 89, 90; 90, if_statement; 90, 91; 90, 94; 91, comparison_operator:not; 91, 92; 91, 93; 92, identifier:wid; 93, identifier:newList; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:newList; 99, identifier:append; 100, argument_list; 100, 101; 101, identifier:wid; 102, comment; 103, comment; 104, if_statement; 104, 105; 104, 111; 105, comparison_operator:<; 105, 106; 105, 110; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, identifier:newList; 110, integer:2; 111, block; 111, 112; 112, return_statement; 113, comment; 114, comment; 115, comment; 116, comment; 117, comment; 118, for_statement; 118, 119; 118, 120; 118, 125; 119, identifier:wid; 120, subscript; 120, 121; 120, 122; 121, identifier:newList; 122, slice; 122, 123; 122, 124; 123, integer:1; 124, colon; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 135; 128, attribute; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:_qteAdmin; 133, identifier:widgetList; 134, identifier:remove; 135, argument_list; 135, 136; 136, identifier:wid; 137, comment; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:startIdx; 141, binary_operator:+; 141, 142; 141, 154; 142, call; 142, 143; 142, 150; 143, attribute; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:_qteAdmin; 148, identifier:widgetList; 149, identifier:index; 150, argument_list; 150, 151; 151, subscript; 151, 152; 151, 153; 152, identifier:newList; 153, integer:0; 154, integer:1; 155, for_statement; 155, 156; 155, 159; 155, 167; 156, pattern_list; 156, 157; 156, 158; 157, identifier:idx; 158, identifier:wid; 159, call; 159, 160; 159, 161; 160, identifier:enumerate; 161, argument_list; 161, 162; 162, subscript; 162, 163; 162, 164; 163, identifier:newList; 164, slice; 164, 165; 164, 166; 165, integer:1; 166, colon; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 177; 170, attribute; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:_qteAdmin; 175, identifier:widgetList; 176, identifier:insert; 177, argument_list; 177, 178; 177, 181; 178, binary_operator:+; 178, 179; 178, 180; 179, identifier:startIdx; 180, identifier:idx; 181, identifier:wid | def qteSetWidgetFocusOrder(self, widList: tuple):
"""
Change the focus order of the widgets in this applet.
This method re-arranges the internal (cyclic) widget list so
that all widgets specified in ``widList`` will be focused in
the given order.
|Args|
* ``widList`` (**tuple**): a tuple of widget objects.
|Returns|
* **None**
|Raises|
* **None**
"""
# A list with less than two entries cannot be re-ordered.
if len(widList) < 2:
return
# Housekeeping: remove non-existing widgets from the admin structure.
self.qteAutoremoveDeletedWidgets()
# Remove all **None** widgets.
widList = [_ for _ in widList if _ is not None]
# Ensure that all widgets exist in the current applet.
for wid in widList:
if wid not in self._qteAdmin.widgetList:
msg = 'Cannot change focus order because some '
msg += 'widgets do not exist.'
self.qteLogger.warning(msg)
return
# Remove all duplicates from the user supplied list.
newList = [widList[0]]
for wid in widList[1:]:
if wid not in newList:
newList.append(wid)
# If the duplicate free list has only one entry then there is
# nothing left to reorder.
if len(newList) < 2:
return
# The purpose of the code is the following: suppose
# _qteAdmin.widgetList = [0,1,2,3,4,5] and newList=[2,5,1].
# Then change _qteAdmin.widgetList to [0,1,2,5,1,3,4]. Step
# 1: remove all but the first widget in newList from
# _qteAdmin.widgetList.
for wid in newList[1:]:
self._qteAdmin.widgetList.remove(wid)
# 2: re-insert the removed elements as a sequence again.
startIdx = self._qteAdmin.widgetList.index(newList[0]) + 1
for idx, wid in enumerate(newList[1:]):
self._qteAdmin.widgetList.insert(startIdx + idx, wid) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 37; 2, function_name:qteNextWidget; 3, parameters; 3, 4; 3, 5; 3, 10; 3, 17; 3, 22; 3, 27; 3, 32; 4, identifier:self; 5, typed_default_parameter; 5, 6; 5, 7; 5, 9; 6, identifier:numSkip; 7, type; 7, 8; 8, identifier:int; 9, integer:1; 10, typed_default_parameter; 10, 11; 10, 12; 10, 16; 11, identifier:ofsWidget; 12, type; 12, 13; 13, attribute; 13, 14; 13, 15; 14, identifier:QtGui; 15, identifier:QWidget; 16, None; 17, typed_default_parameter; 17, 18; 17, 19; 17, 21; 18, identifier:skipVisible; 19, type; 19, 20; 20, identifier:bool; 21, False; 22, typed_default_parameter; 22, 23; 22, 24; 22, 26; 23, identifier:skipInvisible; 24, type; 24, 25; 25, identifier:bool; 26, True; 27, typed_default_parameter; 27, 28; 27, 29; 27, 31; 28, identifier:skipFocusable; 29, type; 29, 30; 30, identifier:bool; 31, False; 32, typed_default_parameter; 32, 33; 32, 34; 32, 36; 33, identifier:skipUnfocusable; 34, type; 34, 35; 35, identifier:bool; 36, True; 37, block; 37, 38; 37, 40; 37, 41; 37, 67; 37, 68; 37, 74; 37, 75; 37, 86; 37, 87; 37, 88; 37, 89; 37, 114; 37, 132; 37, 151; 37, 170; 37, 188; 37, 189; 37, 190; 37, 199; 37, 200; 37, 211; 37, 230; 37, 231; 37, 232; 37, 233; 37, 251; 37, 252; 37, 253; 37, 265; 37, 266; 38, expression_statement; 38, 39; 39, comment; 40, comment; 41, if_statement; 41, 42; 41, 53; 42, boolean_operator:and; 42, 43; 42, 49; 43, not_operator; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:hasattr; 46, argument_list; 46, 47; 46, 48; 47, identifier:ofsWidget; 48, string:'_qteAdmin'; 49, parenthesized_expression; 49, 50; 50, comparison_operator:is; 50, 51; 50, 52; 51, identifier:ofsWidget; 52, None; 53, block; 53, 54; 53, 58; 53, 62; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:msg; 57, string:'<ofsWidget> was probably not added with <qteAddWidget>'; 58, expression_statement; 58, 59; 59, augmented_assignment:+=; 59, 60; 59, 61; 60, identifier:msg; 61, string:' method because it lacks the <_qteAdmin> attribute.'; 62, raise_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:QtmacsOtherError; 65, argument_list; 65, 66; 66, identifier:msg; 67, comment; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:qteAutoremoveDeletedWidgets; 73, argument_list; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:widList; 78, call; 78, 79; 78, 80; 79, identifier:list; 80, argument_list; 80, 81; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_qteAdmin; 85, identifier:widgetList; 86, comment; 87, comment; 88, comment; 89, if_statement; 89, 90; 89, 95; 90, not_operator; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:len; 93, argument_list; 93, 94; 94, identifier:widList; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 105; 96, 110; 97, comparison_operator:is; 97, 98; 97, 104; 98, call; 98, 99; 98, 100; 99, identifier:qteGetAppletFromWidget; 100, argument_list; 100, 101; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:_qteActiveWidget; 104, identifier:self; 105, block; 105, 106; 106, return_statement; 106, 107; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:_qteActiveWidget; 110, else_clause; 110, 111; 111, block; 111, 112; 112, return_statement; 112, 113; 113, None; 114, if_statement; 114, 115; 114, 116; 114, 117; 115, identifier:skipInvisible; 116, comment; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:widList; 121, list_comprehension; 121, 122; 121, 123; 121, 126; 122, identifier:wid; 123, for_in_clause; 123, 124; 123, 125; 124, identifier:wid; 125, identifier:widList; 126, if_clause; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:wid; 130, identifier:isVisible; 131, argument_list; 132, if_statement; 132, 133; 132, 134; 132, 135; 133, identifier:skipVisible; 134, comment; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:widList; 139, list_comprehension; 139, 140; 139, 141; 139, 144; 140, identifier:wid; 141, for_in_clause; 141, 142; 141, 143; 142, identifier:wid; 143, identifier:widList; 144, if_clause; 144, 145; 145, not_operator; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:wid; 149, identifier:isVisible; 150, argument_list; 151, if_statement; 151, 152; 151, 153; 151, 154; 152, identifier:skipFocusable; 153, comment; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:widList; 158, list_comprehension; 158, 159; 158, 160; 158, 163; 159, identifier:wid; 160, for_in_clause; 160, 161; 160, 162; 161, identifier:wid; 162, identifier:widList; 163, if_clause; 163, 164; 164, not_operator; 164, 165; 165, attribute; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:wid; 168, identifier:_qteAdmin; 169, identifier:isFocusable; 170, if_statement; 170, 171; 170, 172; 170, 173; 171, identifier:skipUnfocusable; 172, comment; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:widList; 177, list_comprehension; 177, 178; 177, 179; 177, 182; 178, identifier:wid; 179, for_in_clause; 179, 180; 179, 181; 180, identifier:wid; 181, identifier:widList; 182, if_clause; 182, 183; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:wid; 186, identifier:_qteAdmin; 187, identifier:isFocusable; 188, comment; 189, comment; 190, if_statement; 190, 191; 190, 196; 191, not_operator; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:len; 194, argument_list; 194, 195; 195, identifier:widList; 196, block; 196, 197; 197, return_statement; 197, 198; 198, None; 199, comment; 200, if_statement; 200, 201; 200, 204; 201, comparison_operator:is; 201, 202; 201, 203; 202, identifier:ofsWidget; 203, None; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:ofsWidget; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:_qteActiveWidget; 211, if_statement; 211, 212; 211, 221; 212, boolean_operator:and; 212, 213; 212, 217; 213, parenthesized_expression; 213, 214; 214, comparison_operator:is; 214, 215; 214, 216; 215, identifier:ofsWidget; 216, None; 217, parenthesized_expression; 217, 218; 218, comparison_operator:==; 218, 219; 218, 220; 219, identifier:numSkip; 220, integer:0; 221, block; 221, 222; 222, if_statement; 222, 223; 222, 227; 223, call; 223, 224; 223, 225; 224, identifier:qteIsQtmacsWidget; 225, argument_list; 225, 226; 226, identifier:ofsWidget; 227, block; 227, 228; 228, return_statement; 228, 229; 229, identifier:ofsWidget; 230, comment; 231, comment; 232, comment; 233, try_statement; 233, 234; 233, 244; 234, block; 234, 235; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:ofsIdx; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:widList; 241, identifier:index; 242, argument_list; 242, 243; 243, identifier:ofsWidget; 244, except_clause; 244, 245; 244, 246; 245, identifier:ValueError; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:ofsIdx; 250, integer:0; 251, comment; 252, comment; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:ofsIdx; 256, binary_operator:%; 256, 257; 256, 261; 257, parenthesized_expression; 257, 258; 258, binary_operator:+; 258, 259; 258, 260; 259, identifier:ofsIdx; 260, identifier:numSkip; 261, call; 261, 262; 261, 263; 262, identifier:len; 263, argument_list; 263, 264; 264, identifier:widList; 265, comment; 266, return_statement; 266, 267; 267, subscript; 267, 268; 267, 269; 268, identifier:widList; 269, identifier:ofsIdx | def qteNextWidget(self, numSkip: int=1, ofsWidget: QtGui.QWidget=None,
skipVisible: bool=False, skipInvisible: bool=True,
skipFocusable: bool=False,
skipUnfocusable: bool=True):
"""
Return the next widget in cyclic order.
If ``ofsWidget`` is **None** then start counting at the
currently active widget and return the applet ``numSkip``
items away in cyclic order in the internal widget list. If
``numSkip`` is positive traverse the applet list forwards,
otherwise backwards. The methods supports the following
selection criteria:
* ``skipVisible``: only invisible widgets are considered.
* ``skipInvisible``: only visible widgets are considered.
* ``skipFocusable``: only unfocusable widgets are considered.
* ``skipUnfocusable``: only unfocusable widgets are considered.
|Args|
* ``numSkip`` (**int**): number of applets to skip.
* ``ofsWidget`` (**QWidget**): widget from where to start counting.
* ``skipVisible`` (**bool**): whether or not to skip currently
shown widgets.
* ``skipInvisible`` (**bool**): whether or not to skip currently
not shown widgets.
* ``skipFocusable`` (**bool**): whether or not to skip focusable
widgets.
* ``skipUnfocusable`` (**bool**): whether or not to skip unfocusable
widgets.
|Returns|
* **QWidget**: either the next widget that fits the criteria, or
**None** if no such widget exists.
|Raises|
* **QtmacsArgumentError** if at least one argument has an invalid type.
* **QtmacsOtherError** if ``ofsWidget`` was not added with
``qteAddWidget``.
"""
# Check type of input arguments.
if not hasattr(ofsWidget, '_qteAdmin') and (ofsWidget is not None):
msg = '<ofsWidget> was probably not added with <qteAddWidget>'
msg += ' method because it lacks the <_qteAdmin> attribute.'
raise QtmacsOtherError(msg)
# Housekeeping: remove non-existing widgets from the admin structure.
self.qteAutoremoveDeletedWidgets()
# Make a copy of the widget list.
widList = list(self._qteAdmin.widgetList)
# Return immediately if the widget list is empty. The actual
# return value is either self._qteActiveWidget (if it points
# to a child widget of the current applet), or None.
if not len(widList):
if qteGetAppletFromWidget(self._qteActiveWidget) is self:
return self._qteActiveWidget
else:
return None
if skipInvisible:
# Remove all invisible widgets.
widList = [wid for wid in widList if wid.isVisible()]
if skipVisible:
# Remove all visible widgets.
widList = [wid for wid in widList if not wid.isVisible()]
if skipFocusable:
# Remove all visible widgets.
widList = [wid for wid in widList if not wid._qteAdmin.isFocusable]
if skipUnfocusable:
# Remove all unfocusable widgets.
widList = [wid for wid in widList if wid._qteAdmin.isFocusable]
# Return immediately if the list is empty. This is typically
# the case at startup before any applet has been added.
if not len(widList):
return None
# If no offset widget was given then use the currently active one.
if ofsWidget is None:
ofsWidget = self._qteActiveWidget
if (ofsWidget is not None) and (numSkip == 0):
if qteIsQtmacsWidget(ofsWidget):
return ofsWidget
# Determine the index of the offset widget; assume it is zero
# if the widget does not exist, eg. if the currently active
# applet is not part of the pruned widList list.
try:
ofsIdx = widList.index(ofsWidget)
except ValueError:
ofsIdx = 0
# Compute the index of the next widget and wrap around the
# list if necessary.
ofsIdx = (ofsIdx + numSkip) % len(widList)
# Return the widget.
return widList[ofsIdx] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:qteMakeWidgetActive; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:widgetObj; 7, type; 7, 8; 8, attribute; 8, 9; 8, 10; 9, identifier:QtGui; 10, identifier:QWidget; 11, block; 11, 12; 11, 14; 11, 15; 11, 27; 11, 28; 11, 29; 11, 46; 11, 47; 11, 48; 11, 63; 11, 64; 11, 78; 11, 79; 11, 85; 11, 86; 11, 118; 11, 119; 11, 120; 11, 121; 11, 132; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:is; 16, 17; 16, 18; 17, identifier:widgetObj; 18, None; 19, block; 19, 20; 19, 26; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:_qteActiveWidget; 25, None; 26, return_statement; 27, comment; 28, comment; 29, if_statement; 29, 30; 29, 36; 30, comparison_operator:is; 30, 31; 30, 35; 31, call; 31, 32; 31, 33; 32, identifier:qteGetAppletFromWidget; 33, argument_list; 33, 34; 34, identifier:widgetObj; 35, identifier:self; 36, block; 36, 37; 36, 41; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:msg; 40, string:'The specified widget is not inside the current applet.'; 41, raise_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:QtmacsOtherError; 44, argument_list; 44, 45; 45, identifier:msg; 46, comment; 47, comment; 48, if_statement; 48, 49; 48, 55; 49, not_operator; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:hasattr; 52, argument_list; 52, 53; 52, 54; 53, identifier:widgetObj; 54, string:'_qteAdmin'; 55, block; 55, 56; 55, 62; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:_qteActiveWidget; 61, identifier:widgetObj; 62, return_statement; 63, comment; 64, if_statement; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:widgetObj; 68, identifier:_qteAdmin; 69, identifier:isQtmacsApplet; 70, block; 70, 71; 70, 77; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:_qteActiveWidget; 76, None; 77, return_statement; 78, comment; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:qteAutoremoveDeletedWidgets; 84, argument_list; 85, comment; 86, if_statement; 86, 87; 86, 94; 87, comparison_operator:not; 87, 88; 87, 89; 88, identifier:widgetObj; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:_qteAdmin; 93, identifier:widgetList; 94, block; 94, 95; 94, 99; 94, 111; 94, 117; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:msg; 98, string:'Widget is not registered for this applet.'; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:qteLogger; 105, identifier:error; 106, argument_list; 106, 107; 106, 108; 107, identifier:msg; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:stack_info; 110, True; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:_qteActiveWidget; 116, None; 117, return_statement; 118, comment; 119, comment; 120, comment; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:qteSetWidgetFocusOrder; 126, argument_list; 126, 127; 127, tuple; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:_qteActiveWidget; 131, identifier:widgetObj; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:_qteActiveWidget; 137, identifier:widgetObj | def qteMakeWidgetActive(self, widgetObj: QtGui.QWidget):
"""
Give keyboard focus to ``widgetObj``.
If ``widgetObj`` is **None** then the internal focus state
is reset, but the focus manger will automatically
activate the first available widget again.
|Args|
* ``widgetObj`` (**QWidget**): the widget to focus on.
|Returns|
* **None**
|Raises|
* **QtmacsOtherError** if ``widgetObj`` was not added with
``qteAddWidget``.
"""
# Void the active widget information.
if widgetObj is None:
self._qteActiveWidget = None
return
# Ensure that this applet is an ancestor of ``widgetObj``
# inside the Qt hierarchy.
if qteGetAppletFromWidget(widgetObj) is not self:
msg = 'The specified widget is not inside the current applet.'
raise QtmacsOtherError(msg)
# If widgetObj is not registered with Qtmacs then simply declare
# it active and return.
if not hasattr(widgetObj, '_qteAdmin'):
self._qteActiveWidget = widgetObj
return
# Do nothing if widgetObj refers to an applet.
if widgetObj._qteAdmin.isQtmacsApplet:
self._qteActiveWidget = None
return
# Housekeeping: remove non-existing widgets from the admin structure.
self.qteAutoremoveDeletedWidgets()
# Verify the widget is registered for this applet.
if widgetObj not in self._qteAdmin.widgetList:
msg = 'Widget is not registered for this applet.'
self.qteLogger.error(msg, stack_info=True)
self._qteActiveWidget = None
return
# The focus manager in QtmacsMain will hand the focus to
# whatever the _qteActiveWidget variable of the active applet
# points to.
self.qteSetWidgetFocusOrder((self._qteActiveWidget, widgetObj))
self._qteActiveWidget = widgetObj |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:make_result; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 4, identifier:self; 5, identifier:result_class; 6, default_parameter; 6, 7; 6, 8; 7, identifier:node; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:prev_node; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:remember; 14, True; 15, default_parameter; 15, 16; 15, 17; 16, identifier:key_chunks; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:notify; 20, True; 21, dictionary_splat_pattern; 21, 22; 22, identifier:kwargs; 23, block; 23, 24; 23, 26; 23, 46; 23, 52; 23, 71; 23, 77; 23, 102; 23, 113; 23, 133; 23, 140; 23, 176; 23, 208; 24, expression_statement; 24, 25; 25, comment; 26, function_definition; 26, 27; 26, 28; 26, 32; 27, function_name:canonicalize; 28, parameters; 28, 29; 28, 30; 29, identifier:node; 30, dictionary_splat_pattern; 30, 31; 31, identifier:kwargs; 32, block; 32, 33; 33, return_statement; 33, 34; 34, conditional_expression:if; 34, 35; 34, 36; 34, 39; 35, None; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:node; 38, None; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:node; 42, identifier:canonicalize; 43, argument_list; 43, 44; 44, dictionary_splat; 44, 45; 45, identifier:kwargs; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:index; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:index; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:result; 55, call; 55, 56; 55, 57; 56, identifier:result_class; 57, argument_list; 57, 58; 57, 64; 57, 70; 58, call; 58, 59; 58, 60; 59, identifier:canonicalize; 60, argument_list; 60, 61; 60, 62; 61, identifier:node; 62, dictionary_splat; 62, 63; 63, identifier:kwargs; 64, call; 64, 65; 64, 66; 65, identifier:canonicalize; 66, argument_list; 66, 67; 66, 68; 67, identifier:prev_node; 68, dictionary_splat; 68, 69; 69, identifier:kwargs; 70, identifier:index; 71, if_statement; 71, 72; 71, 74; 72, not_operator; 72, 73; 73, identifier:remember; 74, block; 74, 75; 75, return_statement; 75, 76; 76, identifier:result; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 84; 79, subscript; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:history; 83, identifier:index; 84, call; 84, 85; 84, 86; 85, identifier:result_class; 86, argument_list; 86, 87; 86, 94; 86, 101; 87, call; 87, 88; 87, 89; 88, identifier:canonicalize; 89, argument_list; 89, 90; 89, 91; 90, identifier:node; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:include_nodes; 93, False; 94, call; 94, 95; 94, 96; 95, identifier:canonicalize; 96, argument_list; 96, 97; 96, 98; 97, identifier:prev_node; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:include_nodes; 100, False; 101, identifier:index; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:key_chunks; 105, boolean_operator:or; 105, 106; 105, 107; 106, identifier:key_chunks; 107, call; 107, 108; 107, 109; 108, identifier:split_key; 109, argument_list; 109, 110; 110, attribute; 110, 111; 110, 112; 111, identifier:node; 112, identifier:key; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:asymptotic_key_chunks; 116, generator_expression; 116, 117; 116, 124; 117, subscript; 117, 118; 117, 119; 118, identifier:key_chunks; 119, slice; 119, 120; 119, 121; 120, colon; 121, binary_operator:+; 121, 122; 121, 123; 122, identifier:x; 123, integer:1; 124, for_in_clause; 124, 125; 124, 126; 125, identifier:x; 126, call; 126, 127; 126, 128; 127, identifier:xrange; 128, argument_list; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:len; 131, argument_list; 131, 132; 132, identifier:key_chunks; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:event_keys; 136, list:[(False, key_chunks)]; 136, 137; 137, tuple; 137, 138; 137, 139; 138, False; 139, identifier:key_chunks; 140, for_statement; 140, 141; 140, 142; 140, 143; 141, identifier:_key_chunks; 142, identifier:asymptotic_key_chunks; 143, block; 143, 144; 143, 150; 143, 167; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:exact; 147, comparison_operator:==; 147, 148; 147, 149; 148, identifier:_key_chunks; 149, identifier:key_chunks; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 163; 152, attribute; 152, 153; 152, 162; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:indices; 158, identifier:setdefault; 159, argument_list; 159, 160; 159, 161; 160, identifier:_key_chunks; 161, list:[]; 162, identifier:append; 163, argument_list; 163, 164; 164, tuple; 164, 165; 164, 166; 165, identifier:index; 166, identifier:exact; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:event_keys; 171, identifier:append; 172, argument_list; 172, 173; 173, tuple; 173, 174; 173, 175; 174, True; 175, identifier:_key_chunks; 176, if_statement; 176, 177; 176, 178; 177, identifier:notify; 178, block; 178, 179; 179, for_statement; 179, 180; 179, 181; 179, 182; 180, identifier:event_key; 181, identifier:event_keys; 182, block; 182, 183; 183, try_statement; 183, 184; 183, 196; 183, 200; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:event; 188, call; 188, 189; 188, 194; 189, attribute; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:events; 193, identifier:pop; 194, argument_list; 194, 195; 195, identifier:event_key; 196, except_clause; 196, 197; 196, 198; 197, identifier:KeyError; 198, block; 198, 199; 199, pass_statement; 200, else_clause; 200, 201; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:event; 206, identifier:set; 207, argument_list; 208, return_statement; 208, 209; 209, identifier:result | def make_result(self, result_class, node=None, prev_node=None,
remember=True, key_chunks=None, notify=True, **kwargs):
"""Makes an etcd result.
If `remember` is ``True``, it keeps the result in the history and
triggers events if waiting. `key_chunks` is the result of
:func:`split_key` of the `node.key`. It is not required if `remember`
is ``False``. Otherwise, it is optional but recommended to eliminate
waste if the key chunks are already supplied.
"""
def canonicalize(node, **kwargs):
return None if node is None else node.canonicalize(**kwargs)
index = self.index
result = result_class(canonicalize(node, **kwargs),
canonicalize(prev_node, **kwargs), index)
if not remember:
return result
self.history[index] = result_class(
canonicalize(node, include_nodes=False),
canonicalize(prev_node, include_nodes=False), index)
key_chunks = key_chunks or split_key(node.key)
asymptotic_key_chunks = (key_chunks[:x + 1]
for x in xrange(len(key_chunks)))
event_keys = [(False, key_chunks)]
for _key_chunks in asymptotic_key_chunks:
exact = _key_chunks == key_chunks
self.indices.setdefault(_key_chunks, []).append((index, exact))
event_keys.append((True, _key_chunks))
if notify:
for event_key in event_keys:
try:
event = self.events.pop(event_key)
except KeyError:
pass
else:
event.set()
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 6; 2, function_name:_get_class_frame_source; 3, parameters; 3, 4; 4, identifier:class_name; 5, comment; 6, block; 6, 7; 6, 9; 6, 185; 7, expression_statement; 7, 8; 8, comment; 9, for_statement; 9, 10; 9, 11; 9, 16; 10, identifier:frame_info; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:inspect; 14, identifier:stack; 15, argument_list; 16, block; 16, 17; 16, 57; 17, try_statement; 17, 18; 17, 53; 18, block; 18, 19; 19, with_statement; 19, 20; 19, 31; 20, with_clause; 20, 21; 21, with_item; 21, 22; 22, as_pattern; 22, 23; 22, 29; 23, call; 23, 24; 23, 25; 24, identifier:open; 25, argument_list; 25, 26; 26, subscript; 26, 27; 26, 28; 27, identifier:frame_info; 28, integer:1; 29, as_pattern_target; 29, 30; 30, identifier:fp; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:src; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, string:""; 38, identifier:join; 39, argument_list; 39, 40; 40, subscript; 40, 41; 40, 46; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:fp; 44, identifier:readlines; 45, argument_list; 46, slice; 46, 47; 46, 52; 47, binary_operator:-; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:frame_info; 50, integer:2; 51, integer:1; 52, colon; 53, except_clause; 53, 54; 53, 55; 54, identifier:IOError; 55, block; 55, 56; 56, continue_statement; 57, if_statement; 57, 58; 57, 70; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:re; 61, identifier:search; 62, argument_list; 62, 63; 62, 69; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, string:r"\bclass\b\s+\b{}\b"; 66, identifier:format; 67, argument_list; 67, 68; 68, identifier:class_name; 69, identifier:src; 70, block; 70, 71; 70, 82; 70, 91; 70, 95; 70, 99; 70, 103; 70, 107; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:reader; 74, attribute; 74, 75; 74, 81; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:six; 78, identifier:StringIO; 79, argument_list; 79, 80; 80, identifier:src; 81, identifier:readline; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:tokens; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:tokenize; 88, identifier:generate_tokens; 89, argument_list; 89, 90; 90, identifier:reader; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:source_tokens; 94, list:[]; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:indent_level; 98, integer:0; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:base_indent_level; 102, integer:0; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:has_base_level; 106, False; 107, for_statement; 107, 108; 107, 114; 107, 115; 107, 116; 108, pattern_list; 108, 109; 108, 110; 108, 111; 108, 112; 108, 113; 109, identifier:token; 110, identifier:value; 111, identifier:_; 112, identifier:_; 113, identifier:_; 114, identifier:tokens; 115, comment; 116, block; 116, 117; 116, 126; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:source_tokens; 121, identifier:append; 122, argument_list; 122, 123; 123, tuple; 123, 124; 123, 125; 124, identifier:token; 125, identifier:value; 126, if_statement; 126, 127; 126, 132; 126, 137; 126, 173; 127, comparison_operator:==; 127, 128; 127, 129; 128, identifier:token; 129, attribute; 129, 130; 129, 131; 130, identifier:tokenize; 131, identifier:INDENT; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, augmented_assignment:+=; 134, 135; 134, 136; 135, identifier:indent_level; 136, integer:1; 137, elif_clause; 137, 138; 137, 143; 138, comparison_operator:==; 138, 139; 138, 140; 139, identifier:token; 140, attribute; 140, 141; 140, 142; 141, identifier:tokenize; 142, identifier:DEDENT; 143, block; 143, 144; 143, 148; 144, expression_statement; 144, 145; 145, augmented_assignment:-=; 145, 146; 145, 147; 146, identifier:indent_level; 147, integer:1; 148, if_statement; 148, 149; 148, 154; 149, boolean_operator:and; 149, 150; 149, 151; 150, identifier:has_base_level; 151, comparison_operator:<=; 151, 152; 151, 153; 152, identifier:indent_level; 153, identifier:base_indent_level; 154, block; 154, 155; 155, return_statement; 155, 156; 156, tuple; 156, 157; 156, 163; 156, 168; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:tokenize; 160, identifier:untokenize; 161, argument_list; 161, 162; 162, identifier:source_tokens; 163, attribute; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:frame_info; 166, integer:0; 167, identifier:f_globals; 168, attribute; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:frame_info; 171, integer:0; 172, identifier:f_locals; 173, elif_clause; 173, 174; 173, 176; 174, not_operator; 174, 175; 175, identifier:has_base_level; 176, block; 176, 177; 176, 181; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:has_base_level; 180, True; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:base_indent_level; 184, identifier:indent_level; 185, raise_statement; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:TypeError; 188, argument_list; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, string:'Unable to retrieve source for class "{}"'; 192, identifier:format; 193, argument_list; 193, 194; 194, identifier:class_name | def _get_class_frame_source(class_name):
# type: (str) -> Optional[str]
"""Return the source code for a class by checking the frame stack.
This is necessary because it is not possible to get the source of a class
being created by a metaclass directly.
Args:
class_name: The class to look for on the stack.
Returns:
The source code for the requested class if the class was found and the
source was accessible.
"""
for frame_info in inspect.stack():
try:
with open(frame_info[1]) as fp:
src = "".join(fp.readlines()[frame_info[2] - 1 :])
except IOError:
continue
if re.search(r"\bclass\b\s+\b{}\b".format(class_name), src):
reader = six.StringIO(src).readline
tokens = tokenize.generate_tokens(reader)
source_tokens = []
indent_level = 0
base_indent_level = 0
has_base_level = False
for token, value, _, _, _ in tokens: # type: ignore
source_tokens.append((token, value))
if token == tokenize.INDENT:
indent_level += 1
elif token == tokenize.DEDENT:
indent_level -= 1
if has_base_level and indent_level <= base_indent_level:
return (
tokenize.untokenize(source_tokens),
frame_info[0].f_globals,
frame_info[0].f_locals,
)
elif not has_base_level:
has_base_level = True
base_indent_level = indent_level
raise TypeError(
'Unable to retrieve source for class "{}"'.format(class_name)
) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 6; 2, function_name:_create_typed_object_meta; 3, parameters; 3, 4; 4, identifier:get_fset; 5, comment; 6, block; 6, 7; 6, 9; 6, 54; 6, 302; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 15; 9, 16; 10, function_name:_get_fget; 11, parameters; 11, 12; 11, 13; 11, 14; 12, identifier:attr; 13, identifier:private_attr; 14, identifier:type_; 15, comment; 16, block; 16, 17; 16, 19; 16, 52; 17, expression_statement; 17, 18; 18, comment; 19, function_definition; 19, 20; 19, 21; 19, 23; 19, 24; 20, function_name:_fget; 21, parameters; 21, 22; 22, identifier:self; 23, comment; 24, block; 24, 25; 24, 27; 25, expression_statement; 25, 26; 26, comment; 27, try_statement; 27, 28; 27, 35; 28, block; 28, 29; 29, return_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:getattr; 32, argument_list; 32, 33; 32, 34; 33, identifier:self; 34, identifier:private_attr; 35, except_clause; 35, 36; 35, 37; 36, identifier:AttributeError; 37, block; 37, 38; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:AttributeError; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, string:"'{}' object has no attribute '{}'"; 45, identifier:format; 46, argument_list; 46, 47; 46, 51; 47, call; 47, 48; 47, 49; 48, identifier:_get_type_name; 49, argument_list; 49, 50; 50, identifier:type_; 51, identifier:attr; 52, return_statement; 52, 53; 53, identifier:_fget; 54, class_definition; 54, 55; 54, 56; 54, 58; 55, identifier:_AnnotatedObjectMeta; 56, argument_list; 56, 57; 57, identifier:type; 58, block; 58, 59; 58, 61; 59, expression_statement; 59, 60; 60, comment; 61, function_definition; 61, 62; 61, 63; 61, 75; 61, 76; 62, function_name:__new__; 63, parameters; 63, 64; 63, 65; 63, 66; 63, 67; 63, 68; 63, 69; 63, 70; 63, 71; 63, 72; 63, 74; 64, identifier:mcs; 65, comment; 66, identifier:name; 67, comment; 68, identifier:bases; 69, comment; 70, identifier:attrs; 71, comment; 72, dictionary_splat_pattern; 72, 73; 73, identifier:kwargs; 74, comment; 75, comment; 76, block; 76, 77; 76, 79; 76, 89; 76, 104; 76, 122; 76, 134; 76, 138; 76, 229; 76, 245; 76, 251; 76, 285; 77, expression_statement; 77, 78; 78, comment; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:annotations; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:attrs; 85, identifier:get; 86, argument_list; 86, 87; 86, 88; 87, string:"__annotations__"; 88, dictionary; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:use_comment_type_hints; 92, parenthesized_expression; 92, 93; 93, boolean_operator:and; 93, 94; 93, 96; 94, not_operator; 94, 95; 95, identifier:annotations; 96, comparison_operator:!=; 96, 97; 96, 103; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:attrs; 100, identifier:get; 101, argument_list; 101, 102; 102, string:"__module__"; 103, identifier:__name__; 104, if_statement; 104, 105; 104, 106; 105, identifier:use_comment_type_hints; 106, block; 106, 107; 106, 114; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:frame_source; 110, call; 110, 111; 110, 112; 111, identifier:_get_class_frame_source; 112, argument_list; 112, 113; 113, identifier:name; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:annotations; 117, call; 117, 118; 117, 119; 118, identifier:get_type_hints; 119, argument_list; 119, 120; 120, list_splat; 120, 121; 121, identifier:frame_source; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:names; 125, binary_operator:+; 125, 126; 125, 130; 126, call; 126, 127; 126, 128; 127, identifier:list; 128, argument_list; 128, 129; 129, identifier:attrs; 130, call; 130, 131; 130, 132; 131, identifier:list; 132, argument_list; 132, 133; 133, identifier:annotations; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:typed_attrs; 137, dictionary; 138, for_statement; 138, 139; 138, 140; 138, 141; 139, identifier:attr; 140, identifier:names; 141, block; 141, 142; 141, 153; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:typed_attrs; 146, identifier:attr; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:attrs; 150, identifier:get; 151, argument_list; 151, 152; 152, identifier:attr; 153, if_statement; 153, 154; 153, 161; 154, call; 154, 155; 154, 156; 155, identifier:_is_propertyable; 156, argument_list; 156, 157; 156, 158; 156, 159; 156, 160; 157, identifier:names; 158, identifier:attrs; 159, identifier:annotations; 160, identifier:attr; 161, block; 161, 162; 161, 171; 161, 184; 161, 209; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:private_attr; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, string:"__{}"; 168, identifier:format; 169, argument_list; 169, 170; 170, identifier:attr; 171, if_statement; 171, 172; 171, 175; 172, comparison_operator:in; 172, 173; 172, 174; 173, identifier:attr; 174, identifier:attrs; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 181; 178, subscript; 178, 179; 178, 180; 179, identifier:typed_attrs; 180, identifier:private_attr; 181, subscript; 181, 182; 181, 183; 182, identifier:attrs; 183, identifier:attr; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:type_; 187, parenthesized_expression; 187, 188; 188, conditional_expression:if; 188, 189; 188, 194; 188, 206; 189, subscript; 189, 190; 189, 191; 190, identifier:Optional; 191, subscript; 191, 192; 191, 193; 192, identifier:annotations; 193, identifier:attr; 194, boolean_operator:and; 194, 195; 194, 201; 195, boolean_operator:and; 195, 196; 195, 198; 196, not_operator; 196, 197; 197, identifier:use_comment_type_hints; 198, comparison_operator:in; 198, 199; 198, 200; 199, identifier:attr; 200, identifier:attrs; 201, comparison_operator:is; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:attrs; 204, identifier:attr; 205, None; 206, subscript; 206, 207; 206, 208; 207, identifier:annotations; 208, identifier:attr; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:typed_attrs; 213, identifier:attr; 214, call; 214, 215; 214, 216; 215, identifier:property; 216, argument_list; 216, 217; 216, 223; 217, call; 217, 218; 217, 219; 218, identifier:_get_fget; 219, argument_list; 219, 220; 219, 221; 219, 222; 220, identifier:attr; 221, identifier:private_attr; 222, identifier:type_; 223, call; 223, 224; 223, 225; 224, identifier:get_fset; 225, argument_list; 225, 226; 225, 227; 225, 228; 226, identifier:attr; 227, identifier:private_attr; 228, identifier:type_; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:properties; 232, list_comprehension; 232, 233; 232, 234; 232, 237; 233, identifier:attr; 234, for_in_clause; 234, 235; 234, 236; 235, identifier:attr; 236, identifier:annotations; 237, if_clause; 237, 238; 238, call; 238, 239; 238, 240; 239, identifier:_is_propertyable; 240, argument_list; 240, 241; 240, 242; 240, 243; 240, 244; 241, identifier:names; 242, identifier:attrs; 243, identifier:annotations; 244, identifier:attr; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 250; 247, subscript; 247, 248; 247, 249; 248, identifier:typed_attrs; 249, string:"_tp__typed_properties"; 250, identifier:properties; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 256; 253, subscript; 253, 254; 253, 255; 254, identifier:typed_attrs; 255, string:"_tp__required_typed_properties"; 256, list_comprehension; 256, 257; 256, 258; 256, 261; 257, identifier:attr; 258, for_in_clause; 258, 259; 258, 260; 259, identifier:attr; 260, identifier:properties; 261, if_clause; 261, 262; 262, boolean_operator:and; 262, 263; 262, 275; 263, parenthesized_expression; 263, 264; 264, boolean_operator:or; 264, 265; 264, 268; 265, comparison_operator:not; 265, 266; 265, 267; 266, identifier:attr; 267, identifier:attrs; 268, boolean_operator:and; 268, 269; 268, 274; 269, comparison_operator:is; 269, 270; 269, 273; 270, subscript; 270, 271; 270, 272; 271, identifier:attrs; 272, identifier:attr; 273, None; 274, identifier:use_comment_type_hints; 275, comparison_operator:not; 275, 276; 275, 277; 276, identifier:NoneType; 277, call; 277, 278; 277, 279; 278, identifier:getattr; 279, argument_list; 279, 280; 279, 283; 279, 284; 280, subscript; 280, 281; 280, 282; 281, identifier:annotations; 282, identifier:attr; 283, string:"__args__"; 284, tuple; 285, return_statement; 285, 286; 286, call; 286, 287; 286, 294; 287, attribute; 287, 288; 287, 293; 288, call; 288, 289; 288, 290; 289, identifier:super; 290, argument_list; 290, 291; 290, 292; 291, identifier:_AnnotatedObjectMeta; 292, identifier:mcs; 293, identifier:__new__; 294, argument_list; 294, 295; 294, 296; 294, 297; 294, 298; 294, 299; 294, 300; 295, comment; 296, identifier:mcs; 297, identifier:name; 298, identifier:bases; 299, identifier:typed_attrs; 300, dictionary_splat; 300, 301; 301, identifier:kwargs; 302, return_statement; 302, 303; 303, identifier:_AnnotatedObjectMeta | def _create_typed_object_meta(get_fset):
# type: (Callable[[str, str, Type[_T]], Callable[[_T], None]]) -> type
"""Create a metaclass for typed objects.
Args:
get_fset: A function that takes three parameters: the name of an
attribute, the name of the private attribute that holds the
property data, and a type. This function must an object method that
accepts a value.
Returns:
A metaclass that reads annotations from a class definition and creates
properties for annotated, public, non-constant, non-method attributes
that will guarantee the type of the stored value matches the
annotation.
"""
def _get_fget(attr, private_attr, type_):
# type: (str, str, Type[_T]) -> Callable[[], Any]
"""Create a property getter method for an attribute.
Args:
attr: The name of the attribute that will be retrieved.
private_attr: The name of the attribute that will store any data
related to the attribute.
type_: The annotated type defining what values can be stored in the
attribute.
Returns:
A function that takes self and retrieves the private attribute from
self.
"""
def _fget(self):
# type: (...) -> Any
"""Get attribute from self without revealing the private name."""
try:
return getattr(self, private_attr)
except AttributeError:
raise AttributeError(
"'{}' object has no attribute '{}'".format(
_get_type_name(type_), attr
)
)
return _fget
class _AnnotatedObjectMeta(type):
"""A metaclass that reads annotations from a class definition."""
def __new__(
mcs, # type: Type[_AnnotatedObjectMeta]
name, # type: str
bases, # type: List[type]
attrs, # type: Dict[str, Any]
**kwargs # type: Dict[str, Any]
):
# type: (...) -> type
"""Create class objs that replaces annotated attrs with properties.
Args:
mcs: The class object being created.
name: The name of the class to create.
bases: The list of all base classes for the new class.
attrs: The list of all attributes for the new class from the
definition.
Returns:
A new class instance with the expected base classes and
attributes, but with annotated, public, non-constant,
non-method attributes replaced by property objects that
validate against the annotated type.
"""
annotations = attrs.get("__annotations__", {})
use_comment_type_hints = (
not annotations and attrs.get("__module__") != __name__
)
if use_comment_type_hints:
frame_source = _get_class_frame_source(name)
annotations = get_type_hints(*frame_source)
names = list(attrs) + list(annotations)
typed_attrs = {}
for attr in names:
typed_attrs[attr] = attrs.get(attr)
if _is_propertyable(names, attrs, annotations, attr):
private_attr = "__{}".format(attr)
if attr in attrs:
typed_attrs[private_attr] = attrs[attr]
type_ = (
Optional[annotations[attr]]
if not use_comment_type_hints
and attr in attrs
and attrs[attr] is None
else annotations[attr]
)
typed_attrs[attr] = property(
_get_fget(attr, private_attr, type_),
get_fset(attr, private_attr, type_),
)
properties = [
attr
for attr in annotations
if _is_propertyable(names, attrs, annotations, attr)
]
typed_attrs["_tp__typed_properties"] = properties
typed_attrs["_tp__required_typed_properties"] = [
attr
for attr in properties
if (
attr not in attrs
or attrs[attr] is None
and use_comment_type_hints
)
and NoneType not in getattr(annotations[attr], "__args__", ())
]
return super(_AnnotatedObjectMeta, mcs).__new__( # type: ignore
mcs, name, bases, typed_attrs, **kwargs
)
return _AnnotatedObjectMeta |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_build_module_db; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 18; 5, 74; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:tdict; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:collections; 14, identifier:defaultdict; 15, argument_list; 15, 16; 16, lambda; 16, 17; 17, list:[]; 18, for_statement; 18, 19; 18, 22; 18, 31; 19, pattern_list; 19, 20; 19, 21; 20, identifier:callable_name; 21, identifier:callable_dict; 22, call; 22, 23; 22, 30; 23, attribute; 23, 24; 23, 29; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:_exh_obj; 28, identifier:callables_db; 29, identifier:items; 30, argument_list; 31, block; 31, 32; 31, 40; 31, 59; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, pattern_list; 34, 35; 34, 36; 35, identifier:fname; 36, identifier:line_no; 37, subscript; 37, 38; 37, 39; 38, identifier:callable_dict; 39, string:"code_id"; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:cname; 43, parenthesized_expression; 43, 44; 44, conditional_expression:if; 44, 45; 44, 53; 44, 58; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, string:"{cls_name}.__init__"; 48, identifier:format; 49, argument_list; 49, 50; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:cls_name; 52, identifier:callable_name; 53, comparison_operator:==; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:callable_dict; 56, string:"type"; 57, string:"class"; 58, identifier:callable_name; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:tdict; 64, identifier:fname; 65, identifier:append; 66, argument_list; 66, 67; 67, dictionary; 67, 68; 67, 71; 68, pair; 68, 69; 68, 70; 69, string:"name"; 70, identifier:cname; 71, pair; 71, 72; 71, 73; 72, string:"line"; 73, identifier:line_no; 74, for_statement; 74, 75; 74, 76; 74, 81; 75, identifier:fname; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:tdict; 79, identifier:keys; 80, argument_list; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 89; 84, subscript; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:_module_obj_db; 88, identifier:fname; 89, call; 89, 90; 89, 91; 90, identifier:sorted; 91, argument_list; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:tdict; 94, identifier:fname; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:key; 97, lambda; 97, 98; 97, 100; 98, lambda_parameters; 98, 99; 99, identifier:idict; 100, subscript; 100, 101; 100, 102; 101, identifier:idict; 102, string:"line" | def _build_module_db(self):
"""
Build database of module callables sorted by line number.
The database is a dictionary whose keys are module file names and
whose values are lists of dictionaries containing name and line
number of callables in that module
"""
tdict = collections.defaultdict(lambda: [])
for callable_name, callable_dict in self._exh_obj.callables_db.items():
fname, line_no = callable_dict["code_id"]
cname = (
"{cls_name}.__init__".format(cls_name=callable_name)
if callable_dict["type"] == "class"
else callable_name
)
tdict[fname].append({"name": cname, "line": line_no})
for fname in tdict.keys():
self._module_obj_db[fname] = sorted(
tdict[fname], key=lambda idict: idict["line"]
) |
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:cls; 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, 25; 9, 37; 9, 71; 9, 77; 9, 86; 9, 187; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:newkeys; 15, call; 15, 16; 15, 17; 16, identifier:bool; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:kwargs; 21, identifier:get; 22, argument_list; 22, 23; 22, 24; 23, string:'newkeys'; 24, False; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:ignore; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:kwargs; 31, identifier:get; 32, argument_list; 32, 33; 32, 34; 33, string:'ignore'; 34, call; 34, 35; 34, 36; 35, identifier:list; 36, argument_list; 37, if_statement; 37, 38; 37, 44; 37, 50; 38, comparison_operator:<; 38, 39; 38, 43; 39, call; 39, 40; 39, 41; 40, identifier:len; 41, argument_list; 41, 42; 42, identifier:args; 43, integer:1; 44, block; 44, 45; 45, raise_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:ValueError; 48, argument_list; 48, 49; 49, string:'no ents given to Ent.merge()'; 50, elif_clause; 50, 51; 50, 63; 51, not_operator; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:all; 54, generator_expression; 54, 55; 54, 60; 55, call; 55, 56; 55, 57; 56, identifier:isinstance; 57, argument_list; 57, 58; 57, 59; 58, identifier:s; 59, identifier:Ent; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:s; 62, identifier:args; 63, block; 63, 64; 64, raise_statement; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:ValueError; 67, argument_list; 67, 68; 68, concatenated_string; 68, 69; 68, 70; 69, string:'all positional arguments to Ent.merge() must '; 70, string:'be instances of Ent'; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:ent; 74, subscript; 74, 75; 74, 76; 75, identifier:args; 76, integer:0; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:data; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:cls; 83, identifier:load; 84, argument_list; 84, 85; 85, identifier:ent; 86, for_statement; 86, 87; 86, 88; 86, 93; 87, identifier:ent; 88, subscript; 88, 89; 88, 90; 89, identifier:args; 90, slice; 90, 91; 90, 92; 91, integer:1; 92, colon; 93, block; 93, 94; 94, for_statement; 94, 95; 94, 98; 94, 105; 95, pattern_list; 95, 96; 95, 97; 96, identifier:key; 97, identifier:value; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:ent; 102, identifier:__dict__; 103, identifier:items; 104, argument_list; 105, block; 105, 106; 105, 112; 106, if_statement; 106, 107; 106, 110; 107, comparison_operator:in; 107, 108; 107, 109; 108, identifier:key; 109, identifier:ignore; 110, block; 110, 111; 111, continue_statement; 112, if_statement; 112, 113; 112, 118; 112, 176; 113, comparison_operator:in; 113, 114; 113, 115; 114, identifier:key; 115, attribute; 115, 116; 115, 117; 116, identifier:data; 117, identifier:__dict__; 118, block; 118, 119; 118, 127; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:v1; 122, subscript; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:data; 125, identifier:__dict__; 126, identifier:key; 127, if_statement; 127, 128; 127, 137; 128, comparison_operator:==; 128, 129; 128, 133; 129, call; 129, 130; 129, 131; 130, identifier:type; 131, argument_list; 131, 132; 132, identifier:value; 133, call; 133, 134; 133, 135; 134, identifier:type; 135, argument_list; 135, 136; 136, identifier:v1; 137, block; 137, 138; 138, if_statement; 138, 139; 138, 144; 138, 161; 139, call; 139, 140; 139, 141; 140, identifier:isinstance; 141, argument_list; 141, 142; 141, 143; 142, identifier:v1; 143, identifier:Ent; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 152; 147, subscript; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:data; 150, identifier:__dict__; 151, identifier:key; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:cls; 155, identifier:merge; 156, argument_list; 156, 157; 156, 158; 156, 159; 157, identifier:v1; 158, identifier:value; 159, dictionary_splat; 159, 160; 160, identifier:kwargs; 161, else_clause; 161, 162; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 170; 165, subscript; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:data; 168, identifier:__dict__; 169, identifier:key; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:cls; 173, identifier:load; 174, argument_list; 174, 175; 175, identifier:value; 176, elif_clause; 176, 177; 176, 178; 177, identifier:newkeys; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 186; 181, subscript; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:data; 184, identifier:__dict__; 185, identifier:key; 186, identifier:value; 187, return_statement; 187, 188; 188, identifier:data | def merge(cls, *args, **kwargs):
"""Create a new Ent from one or more existing Ents. Keys in the
later Ent objects will overwrite the keys of the previous Ents.
Later keys of different type than in earlier Ents will be bravely
ignored.
The following keyword arguments are recognized:
newkeys: boolean value to determine whether keys from later Ents
should be included if they do not exist in earlier Ents.
ignore: list of strings of key names that should not be overridden by
later Ent keys.
"""
newkeys = bool(kwargs.get('newkeys', False))
ignore = kwargs.get('ignore', list())
if len(args) < 1:
raise ValueError('no ents given to Ent.merge()')
elif not all(isinstance(s, Ent) for s in args):
raise ValueError('all positional arguments to Ent.merge() must '
'be instances of Ent')
ent = args[0]
data = cls.load(ent)
for ent in args[1:]:
for key, value in ent.__dict__.items():
if key in ignore:
continue
if key in data.__dict__:
v1 = data.__dict__[key]
if type(value) == type(v1):
if isinstance(v1, Ent):
data.__dict__[key] = cls.merge(v1, value, **kwargs)
else:
data.__dict__[key] = cls.load(value)
elif newkeys:
data.__dict__[key] = value
return data |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:diff; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:cls; 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, 25; 9, 37; 9, 71; 9, 77; 9, 83; 9, 208; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:newkeys; 15, call; 15, 16; 15, 17; 16, identifier:bool; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:kwargs; 21, identifier:get; 22, argument_list; 22, 23; 22, 24; 23, string:'newkeys'; 24, False; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:ignore; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:kwargs; 31, identifier:get; 32, argument_list; 32, 33; 32, 34; 33, string:'ignore'; 34, call; 34, 35; 34, 36; 35, identifier:list; 36, argument_list; 37, if_statement; 37, 38; 37, 44; 37, 50; 38, comparison_operator:<; 38, 39; 38, 43; 39, call; 39, 40; 39, 41; 40, identifier:len; 41, argument_list; 41, 42; 42, identifier:args; 43, integer:2; 44, block; 44, 45; 45, raise_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:ValueError; 48, argument_list; 48, 49; 49, string:'less than two ents given to Ent.diff()'; 50, elif_clause; 50, 51; 50, 63; 51, not_operator; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:all; 54, generator_expression; 54, 55; 54, 60; 55, call; 55, 56; 55, 57; 56, identifier:isinstance; 57, argument_list; 57, 58; 57, 59; 58, identifier:s; 59, identifier:Ent; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:s; 62, identifier:args; 63, block; 63, 64; 64, raise_statement; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:ValueError; 67, argument_list; 67, 68; 68, concatenated_string; 68, 69; 68, 70; 69, string:'all positional arguments to Ent.diff() must '; 70, string:'be instances of Ent'; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:s1; 74, subscript; 74, 75; 74, 76; 75, identifier:args; 76, integer:0; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:differences; 80, call; 80, 81; 80, 82; 81, identifier:Ent; 82, argument_list; 83, for_statement; 83, 84; 83, 85; 83, 90; 84, identifier:s2; 85, subscript; 85, 86; 85, 87; 86, identifier:args; 87, slice; 87, 88; 87, 89; 88, integer:1; 89, colon; 90, block; 90, 91; 90, 204; 91, for_statement; 91, 92; 91, 95; 91, 102; 92, pattern_list; 92, 93; 92, 94; 93, identifier:key; 94, identifier:value; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:s2; 99, identifier:__dict__; 100, identifier:items; 101, argument_list; 102, block; 102, 103; 102, 109; 103, if_statement; 103, 104; 103, 107; 104, comparison_operator:in; 104, 105; 104, 106; 105, identifier:key; 106, identifier:ignore; 107, block; 107, 108; 108, continue_statement; 109, if_statement; 109, 110; 109, 115; 109, 188; 110, comparison_operator:in; 110, 111; 110, 112; 111, identifier:key; 112, attribute; 112, 113; 112, 114; 113, identifier:s1; 114, identifier:__dict__; 115, block; 115, 116; 115, 124; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:v1; 119, subscript; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:s1; 122, identifier:__dict__; 123, identifier:key; 124, if_statement; 124, 125; 124, 134; 125, comparison_operator:==; 125, 126; 125, 130; 126, call; 126, 127; 126, 128; 127, identifier:type; 128, argument_list; 128, 129; 129, identifier:value; 130, call; 130, 131; 130, 132; 131, identifier:type; 132, argument_list; 132, 133; 133, identifier:v1; 134, block; 134, 135; 135, if_statement; 135, 136; 135, 141; 135, 170; 136, call; 136, 137; 136, 138; 137, identifier:isinstance; 138, argument_list; 138, 139; 138, 140; 139, identifier:v1; 140, identifier:Ent; 141, block; 141, 142; 141, 154; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:delta; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:cls; 148, identifier:diff; 149, argument_list; 149, 150; 149, 151; 149, 152; 150, identifier:v1; 151, identifier:value; 152, dictionary_splat; 152, 153; 153, identifier:kwargs; 154, if_statement; 154, 155; 154, 161; 155, call; 155, 156; 155, 157; 156, identifier:len; 157, argument_list; 157, 158; 158, attribute; 158, 159; 158, 160; 159, identifier:delta; 160, identifier:__dict__; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 169; 164, subscript; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:differences; 167, identifier:__dict__; 168, identifier:key; 169, identifier:delta; 170, elif_clause; 170, 171; 170, 174; 171, comparison_operator:!=; 171, 172; 171, 173; 172, identifier:v1; 173, identifier:value; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 182; 177, subscript; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:differences; 180, identifier:__dict__; 181, identifier:key; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:cls; 185, identifier:load; 186, argument_list; 186, 187; 187, identifier:value; 188, elif_clause; 188, 189; 188, 190; 189, identifier:newkeys; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 198; 193, subscript; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:differences; 196, identifier:__dict__; 197, identifier:key; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:cls; 201, identifier:load; 202, argument_list; 202, 203; 203, identifier:value; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:s1; 207, identifier:s2; 208, return_statement; 208, 209; 209, identifier:differences | def diff(cls, *args, **kwargs):
"""Create a new Ent representing the differences in two or more
existing Ents. Keys in the later Ents with values that differ
from the earlier Ents will be present in the final Ent with the
latest value seen for that key. Later keys of different type than in
earlier Ents will be bravely ignored.
The following keywoard arguments are recognized:
newkeys: boolean value to determine whether keys from later Ents
should be included if they do not exist in earlier Ents.
ignore: list of strings of key names that will not be included.
"""
newkeys = bool(kwargs.get('newkeys', False))
ignore = kwargs.get('ignore', list())
if len(args) < 2:
raise ValueError('less than two ents given to Ent.diff()')
elif not all(isinstance(s, Ent) for s in args):
raise ValueError('all positional arguments to Ent.diff() must '
'be instances of Ent')
s1 = args[0]
differences = Ent()
for s2 in args[1:]:
for key, value in s2.__dict__.items():
if key in ignore:
continue
if key in s1.__dict__:
v1 = s1.__dict__[key]
if type(value) == type(v1):
if isinstance(v1, Ent):
delta = cls.diff(v1, value, **kwargs)
if len(delta.__dict__):
differences.__dict__[key] = delta
elif v1 != value:
differences.__dict__[key] = cls.load(value)
elif newkeys:
differences.__dict__[key] = cls.load(value)
s1 = s2
return differences |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_sorted_keys_items; 3, parameters; 3, 4; 4, identifier:dobj; 5, block; 5, 6; 5, 8; 5, 19; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:keys; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:dobj; 17, identifier:keys; 18, argument_list; 19, for_statement; 19, 20; 19, 21; 19, 22; 20, identifier:key; 21, identifier:keys; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, yield; 24, 25; 25, expression_list; 25, 26; 25, 27; 26, identifier:key; 27, subscript; 27, 28; 27, 29; 28, identifier:dobj; 29, identifier:key | def _sorted_keys_items(dobj):
"""Return dictionary items sorted by key."""
keys = sorted(dobj.keys())
for key in keys:
yield key, dobj[key] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:get_or_create_exh_obj; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:full_cname; 6, False; 7, default_parameter; 7, 8; 7, 9; 8, identifier:exclude; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:callables_fname; 12, None; 13, block; 13, 14; 13, 16; 13, 40; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 23; 17, not_operator; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:hasattr; 20, argument_list; 20, 21; 20, 22; 21, identifier:__builtin__; 22, string:"_EXH"; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:set_exh_obj; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:ExHandle; 30, argument_list; 30, 31; 30, 34; 30, 37; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:full_cname; 33, identifier:full_cname; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:exclude; 36, identifier:exclude; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:callables_fname; 39, identifier:callables_fname; 40, return_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:get_exh_obj; 43, argument_list | def get_or_create_exh_obj(full_cname=False, exclude=None, callables_fname=None):
r"""
Return global exception handler if set, otherwise create a new one and return it.
:param full_cname: Flag that indicates whether fully qualified
function/method/class property names are obtained for
functions/methods/class properties that use the
exception manager (True) or not (False).
There is a performance penalty if the flag is True as
the call stack needs to be traced. This argument is
only relevant if the global exception handler is not
set and a new one is created
:type full_cname: boolean
:param exclude: Module exclusion list. A particular callable in an
otherwise fully qualified name is omitted if it belongs
to a module in this list. If None all callables are
included
:type exclude: list of strings or None
:param callables_fname: File name that contains traced modules information.
File can be produced by either the
:py:meth:`pexdoc.pinspect.Callables.save` or
:py:meth:`pexdoc.ExHandle.save_callables`
methods
:type callables_fname: :ref:`FileNameExists` or None
:rtype: :py:class:`pexdoc.ExHandle`
:raises:
* OSError (File *[callables_fname]* could not be found
* RuntimeError (Argument \\`exclude\\` is not valid)
* RuntimeError (Argument \\`callables_fname\\` is not valid)
* RuntimeError (Argument \\`full_cname\\` is not valid)
"""
if not hasattr(__builtin__, "_EXH"):
set_exh_obj(
ExHandle(
full_cname=full_cname, exclude=exclude, callables_fname=callables_fname
)
)
return get_exh_obj() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_property_search; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:fobj; 6, block; 6, 7; 6, 9; 6, 10; 6, 22; 6, 33; 6, 44; 6, 45; 6, 68; 6, 79; 6, 94; 6, 113; 6, 114; 6, 118; 6, 211; 6, 212; 6, 221; 6, 234; 6, 293; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:scontext; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:fobj; 17, identifier:f_locals; 18, identifier:get; 19, argument_list; 19, 20; 19, 21; 20, string:"self"; 21, None; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:class_obj; 25, conditional_expression:if; 25, 26; 25, 29; 25, 32; 26, attribute; 26, 27; 26, 28; 27, identifier:scontext; 28, identifier:__class__; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:scontext; 31, None; 32, None; 33, if_statement; 33, 34; 33, 36; 34, not_operator; 34, 35; 35, identifier:class_obj; 36, block; 36, 37; 36, 42; 37, delete_statement; 37, 38; 38, expression_list; 38, 39; 38, 40; 38, 41; 39, identifier:fobj; 40, identifier:scontext; 41, identifier:class_obj; 42, return_statement; 42, 43; 43, None; 44, comment; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:class_props; 48, list_comprehension; 48, 49; 48, 52; 48, 62; 49, tuple; 49, 50; 49, 51; 50, identifier:member_name; 51, identifier:member_obj; 52, for_in_clause; 52, 53; 52, 56; 53, pattern_list; 53, 54; 53, 55; 54, identifier:member_name; 55, identifier:member_obj; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:inspect; 59, identifier:getmembers; 60, argument_list; 60, 61; 61, identifier:class_obj; 62, if_clause; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:isinstance; 65, argument_list; 65, 66; 65, 67; 66, identifier:member_obj; 67, identifier:property; 68, if_statement; 68, 69; 68, 71; 69, not_operator; 69, 70; 70, identifier:class_props; 71, block; 71, 72; 71, 77; 72, delete_statement; 72, 73; 73, expression_list; 73, 74; 73, 75; 73, 76; 74, identifier:fobj; 75, identifier:scontext; 76, identifier:class_obj; 77, return_statement; 77, 78; 78, None; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:class_file; 82, call; 82, 83; 82, 91; 83, attribute; 83, 84; 83, 90; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:inspect; 87, identifier:getfile; 88, argument_list; 88, 89; 89, identifier:class_obj; 90, identifier:replace; 91, argument_list; 91, 92; 91, 93; 92, string:".pyc"; 93, string:".py"; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:class_name; 97, call; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:_callables_obj; 102, identifier:get_callable_from_line; 103, argument_list; 103, 104; 103, 105; 104, identifier:class_file; 105, subscript; 105, 106; 105, 112; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:inspect; 109, identifier:getsourcelines; 110, argument_list; 110, 111; 111, identifier:class_obj; 112, integer:1; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:prop_actions_dicts; 117, dictionary; 118, for_statement; 118, 119; 118, 122; 118, 123; 119, pattern_list; 119, 120; 119, 121; 120, identifier:prop_name; 121, identifier:prop_obj; 122, identifier:class_props; 123, block; 123, 124; 123, 137; 123, 205; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:prop_dict; 127, dictionary; 127, 128; 127, 131; 127, 134; 128, pair; 128, 129; 128, 130; 129, string:"fdel"; 130, None; 131, pair; 131, 132; 131, 133; 132, string:"fget"; 133, None; 134, pair; 134, 135; 134, 136; 135, string:"fset"; 136, None; 137, for_statement; 137, 138; 137, 139; 137, 140; 138, identifier:action; 139, identifier:prop_dict; 140, block; 140, 141; 140, 149; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:action_obj; 144, call; 144, 145; 144, 146; 145, identifier:getattr; 146, argument_list; 146, 147; 146, 148; 147, identifier:prop_obj; 148, identifier:action; 149, if_statement; 149, 150; 149, 151; 149, 152; 149, 153; 149, 154; 149, 155; 150, identifier:action_obj; 151, comment; 152, comment; 153, comment; 154, comment; 155, block; 155, 156; 155, 169; 155, 185; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 161; 158, pattern_list; 158, 159; 158, 160; 159, identifier:prev_func_obj; 160, identifier:next_func_obj; 161, tuple; 161, 162; 161, 163; 162, identifier:action_obj; 163, call; 163, 164; 163, 165; 164, identifier:getattr; 165, argument_list; 165, 166; 165, 167; 165, 168; 166, identifier:action_obj; 167, string:"__wrapped__"; 168, None; 169, while_statement; 169, 170; 169, 171; 170, identifier:next_func_obj; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 177; 174, pattern_list; 174, 175; 174, 176; 175, identifier:prev_func_obj; 176, identifier:next_func_obj; 177, tuple; 177, 178; 177, 179; 178, identifier:next_func_obj; 179, call; 179, 180; 179, 181; 180, identifier:getattr; 181, argument_list; 181, 182; 181, 183; 181, 184; 182, identifier:next_func_obj; 183, string:"__wrapped__"; 184, None; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:prop_dict; 189, identifier:action; 190, list:[
id(_get_func_code(action_obj)),
id(_get_func_code(prev_func_obj)),
]; 190, 191; 190, 198; 191, call; 191, 192; 191, 193; 192, identifier:id; 193, argument_list; 193, 194; 194, call; 194, 195; 194, 196; 195, identifier:_get_func_code; 196, argument_list; 196, 197; 197, identifier:action_obj; 198, call; 198, 199; 198, 200; 199, identifier:id; 200, argument_list; 200, 201; 201, call; 201, 202; 201, 203; 202, identifier:_get_func_code; 203, argument_list; 203, 204; 204, identifier:prev_func_obj; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 210; 207, subscript; 207, 208; 207, 209; 208, identifier:prop_actions_dicts; 209, identifier:prop_name; 210, identifier:prop_dict; 211, comment; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:func_id; 215, call; 215, 216; 215, 217; 216, identifier:id; 217, argument_list; 217, 218; 218, attribute; 218, 219; 218, 220; 219, identifier:fobj; 220, identifier:f_code; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:desc_dict; 224, dictionary; 224, 225; 224, 228; 224, 231; 225, pair; 225, 226; 225, 227; 226, string:"fget"; 227, string:"getter"; 228, pair; 228, 229; 228, 230; 229, string:"fset"; 230, string:"setter"; 231, pair; 231, 232; 231, 233; 232, string:"fdel"; 233, string:"deleter"; 234, for_statement; 234, 235; 234, 238; 234, 243; 235, pattern_list; 235, 236; 235, 237; 236, identifier:prop_name; 237, identifier:prop_actions_dict; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:prop_actions_dicts; 241, identifier:items; 242, argument_list; 243, block; 243, 244; 244, for_statement; 244, 245; 244, 248; 244, 253; 245, pattern_list; 245, 246; 245, 247; 246, identifier:action_name; 247, identifier:action_id_list; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:prop_actions_dict; 251, identifier:items; 252, argument_list; 253, block; 253, 254; 254, if_statement; 254, 255; 254, 261; 255, boolean_operator:and; 255, 256; 255, 257; 256, identifier:action_id_list; 257, parenthesized_expression; 257, 258; 258, comparison_operator:in; 258, 259; 258, 260; 259, identifier:func_id; 260, identifier:action_id_list; 261, block; 261, 262; 261, 273; 261, 279; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:prop_name; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, string:"."; 268, identifier:join; 269, argument_list; 269, 270; 270, list:[class_name, prop_name]; 270, 271; 270, 272; 271, identifier:class_name; 272, identifier:prop_name; 273, delete_statement; 273, 274; 274, expression_list; 274, 275; 274, 276; 274, 277; 274, 278; 275, identifier:fobj; 276, identifier:scontext; 277, identifier:class_obj; 278, identifier:class_props; 279, return_statement; 279, 280; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, string:"{prop_name}({prop_action})"; 283, identifier:format; 284, argument_list; 284, 285; 284, 288; 285, keyword_argument; 285, 286; 285, 287; 286, identifier:prop_name; 287, identifier:prop_name; 288, keyword_argument; 288, 289; 288, 290; 289, identifier:prop_action; 290, subscript; 290, 291; 290, 292; 291, identifier:desc_dict; 292, identifier:action_name; 293, return_statement; 293, 294; 294, None | def _property_search(self, fobj):
"""Return full name if object is a class property, otherwise return None."""
# Get class object
scontext = fobj.f_locals.get("self", None)
class_obj = scontext.__class__ if scontext is not None else None
if not class_obj:
del fobj, scontext, class_obj
return None
# Get class properties objects
class_props = [
(member_name, member_obj)
for member_name, member_obj in inspect.getmembers(class_obj)
if isinstance(member_obj, property)
]
if not class_props:
del fobj, scontext, class_obj
return None
class_file = inspect.getfile(class_obj).replace(".pyc", ".py")
class_name = self._callables_obj.get_callable_from_line(
class_file, inspect.getsourcelines(class_obj)[1]
)
# Get properties actions
prop_actions_dicts = {}
for prop_name, prop_obj in class_props:
prop_dict = {"fdel": None, "fget": None, "fset": None}
for action in prop_dict:
action_obj = getattr(prop_obj, action)
if action_obj:
# Unwrap action object. Contracts match the wrapped
# code object while exceptions registered in the
# body of the function/method which has decorators
# match the unwrapped object
prev_func_obj, next_func_obj = (
action_obj,
getattr(action_obj, "__wrapped__", None),
)
while next_func_obj:
prev_func_obj, next_func_obj = (
next_func_obj,
getattr(next_func_obj, "__wrapped__", None),
)
prop_dict[action] = [
id(_get_func_code(action_obj)),
id(_get_func_code(prev_func_obj)),
]
prop_actions_dicts[prop_name] = prop_dict
# Create properties directory
func_id = id(fobj.f_code)
desc_dict = {"fget": "getter", "fset": "setter", "fdel": "deleter"}
for prop_name, prop_actions_dict in prop_actions_dicts.items():
for action_name, action_id_list in prop_actions_dict.items():
if action_id_list and (func_id in action_id_list):
prop_name = ".".join([class_name, prop_name])
del fobj, scontext, class_obj, class_props
return "{prop_name}({prop_action})".format(
prop_name=prop_name, prop_action=desc_dict[action_name]
)
return None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_execute; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:request; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 321; 8, 330; 8, 358; 8, 364; 8, 415; 9, expression_statement; 9, 10; 10, comment; 11, try_statement; 11, 12; 11, 121; 11, 181; 11, 218; 11, 244; 11, 270; 12, block; 12, 13; 12, 20; 12, 26; 12, 32; 12, 38; 12, 67; 12, 78; 12, 107; 12, 113; 12, 119; 12, 120; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:_create_context; 18, argument_list; 18, 19; 19, identifier:request; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:_authenticate; 25, argument_list; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:context; 29, call; 29, 30; 29, 31; 30, identifier:get_current_context; 31, argument_list; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:_parse_args; 37, argument_list; 38, if_statement; 38, 39; 38, 55; 39, boolean_operator:and; 39, 40; 39, 45; 39, 46; 40, call; 40, 41; 40, 42; 41, identifier:hasattr; 42, argument_list; 42, 43; 42, 44; 43, identifier:self; 44, string:'_before_handlers'; 45, line_continuation:\; 46, call; 46, 47; 46, 48; 47, identifier:isinstance; 48, argument_list; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:_before_handlers; 52, tuple; 52, 53; 52, 54; 53, identifier:list; 54, identifier:tuple; 55, block; 55, 56; 56, for_statement; 56, 57; 56, 58; 56, 61; 57, identifier:handler; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:_before_handlers; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:handler; 65, argument_list; 65, 66; 66, identifier:context; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:context; 71, identifier:handler_result; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:_handle; 76, argument_list; 76, 77; 77, identifier:context; 78, if_statement; 78, 79; 78, 95; 79, boolean_operator:and; 79, 80; 79, 85; 79, 86; 80, call; 80, 81; 80, 82; 81, identifier:hasattr; 82, argument_list; 82, 83; 82, 84; 83, identifier:self; 84, string:'_after_handlers'; 85, line_continuation:\; 86, call; 86, 87; 86, 88; 87, identifier:isinstance; 88, argument_list; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:_after_handlers; 92, tuple; 92, 93; 92, 94; 93, identifier:list; 94, identifier:tuple; 95, block; 95, 96; 96, for_statement; 96, 97; 96, 98; 96, 101; 97, identifier:handler; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:_after_handlers; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:handler; 105, argument_list; 105, 106; 106, identifier:context; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_render; 112, argument_list; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:response; 116, attribute; 116, 117; 116, 118; 117, identifier:context; 118, identifier:response; 119, comment; 120, comment; 121, except_clause; 121, 122; 121, 126; 122, as_pattern; 122, 123; 122, 124; 123, identifier:AuthenticationError; 124, as_pattern_target; 124, 125; 125, identifier:e; 126, block; 126, 127; 126, 152; 126, 161; 126, 173; 127, if_statement; 127, 128; 127, 139; 127, 146; 128, boolean_operator:and; 128, 129; 128, 134; 129, call; 129, 130; 129, 131; 130, identifier:hasattr; 131, argument_list; 131, 132; 131, 133; 132, identifier:e; 133, string:'message'; 134, comparison_operator:is; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:e; 137, identifier:message; 138, None; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:message; 143, attribute; 143, 144; 143, 145; 144, identifier:e; 145, identifier:message; 146, else_clause; 146, 147; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:message; 151, string:"You don't have permission to do that."; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:err; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:APIError; 158, identifier:Forbidden; 159, argument_list; 159, 160; 160, identifier:message; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:response; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:_response_class; 168, argument_list; 168, 169; 169, list_splat; 169, 170; 170, attribute; 170, 171; 170, 172; 171, identifier:err; 172, identifier:response; 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:response; 178, identifier:headers; 179, string:"Content-Type"; 180, string:'application/json'; 181, except_clause; 181, 182; 181, 186; 182, as_pattern; 182, 183; 182, 184; 183, identifier:ArgumentError; 184, as_pattern_target; 184, 185; 185, identifier:e; 186, block; 186, 187; 186, 198; 186, 210; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:err; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:APIError; 193, identifier:UnprocessableEntity; 194, argument_list; 194, 195; 195, attribute; 195, 196; 195, 197; 196, identifier:e; 197, identifier:message; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:response; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:_response_class; 205, argument_list; 205, 206; 206, list_splat; 206, 207; 207, attribute; 207, 208; 207, 209; 208, identifier:err; 209, identifier:response; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 217; 212, subscript; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:response; 215, identifier:headers; 216, string:"Content-Type"; 217, string:'application/json'; 218, except_clause; 218, 219; 218, 223; 219, as_pattern; 219, 220; 219, 221; 220, identifier:APIError; 221, as_pattern_target; 221, 222; 222, identifier:e; 223, block; 223, 224; 223, 236; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:response; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:self; 230, identifier:_response_class; 231, argument_list; 231, 232; 232, list_splat; 232, 233; 233, attribute; 233, 234; 233, 235; 234, identifier:e; 235, identifier:response; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 243; 238, subscript; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:response; 241, identifier:headers; 242, string:"Content-Type"; 243, string:'application/json'; 244, except_clause; 244, 245; 244, 249; 245, as_pattern; 245, 246; 245, 247; 246, identifier:PaleRaisedResponse; 247, as_pattern_target; 247, 248; 248, identifier:r; 249, block; 249, 250; 249, 262; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:response; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:self; 256, identifier:_response_class; 257, argument_list; 257, 258; 258, list_splat; 258, 259; 259, attribute; 259, 260; 259, 261; 260, identifier:r; 261, identifier:response; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 269; 264, subscript; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:response; 267, identifier:headers; 268, string:"Content-Type"; 269, string:'application/json'; 270, except_clause; 270, 271; 270, 275; 271, as_pattern; 271, 272; 271, 273; 272, identifier:Exception; 273, as_pattern_target; 273, 274; 274, identifier:e; 275, block; 275, 276; 275, 289; 275, 301; 275, 313; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:logging; 280, identifier:exception; 281, argument_list; 281, 282; 281, 283; 281, 288; 282, string:"Failed to handle Pale Endpoint %s: %r"; 283, attribute; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:__class__; 287, identifier:__name__; 288, identifier:e; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:err; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:APIError; 295, identifier:Exception; 296, argument_list; 296, 297; 297, call; 297, 298; 297, 299; 298, identifier:repr; 299, argument_list; 299, 300; 300, identifier:e; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:response; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:self; 307, identifier:_response_class; 308, argument_list; 308, 309; 309, list_splat; 309, 310; 310, attribute; 310, 311; 310, 312; 311, identifier:err; 312, identifier:response; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 320; 315, subscript; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:response; 318, identifier:headers; 319, string:"Content-Type"; 320, string:'application/json'; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:allow_cors; 324, call; 324, 325; 324, 326; 325, identifier:getattr; 326, argument_list; 326, 327; 326, 328; 326, 329; 327, identifier:self; 328, string:"_allow_cors"; 329, None; 330, if_statement; 330, 331; 330, 334; 330, 343; 331, comparison_operator:is; 331, 332; 331, 333; 332, identifier:allow_cors; 333, True; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 342; 337, subscript; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:response; 340, identifier:headers; 341, string:'Access-Control-Allow-Origin'; 342, string:'*'; 343, elif_clause; 343, 344; 343, 349; 344, call; 344, 345; 344, 346; 345, identifier:isinstance; 346, argument_list; 346, 347; 346, 348; 347, identifier:allow_cors; 348, identifier:basestring; 349, block; 349, 350; 350, expression_statement; 350, 351; 351, assignment; 351, 352; 351, 357; 352, subscript; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:response; 355, identifier:headers; 356, string:'Access-Control-Allow-Origin'; 357, identifier:allow_cors; 358, expression_statement; 358, 359; 359, assignment; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:context; 362, identifier:response; 363, identifier:response; 364, try_statement; 364, 365; 364, 396; 365, block; 365, 366; 366, if_statement; 366, 367; 366, 383; 367, boolean_operator:and; 367, 368; 367, 373; 367, 374; 368, call; 368, 369; 368, 370; 369, identifier:hasattr; 370, argument_list; 370, 371; 370, 372; 371, identifier:self; 372, string:'_after_response_handlers'; 373, line_continuation:\; 374, call; 374, 375; 374, 376; 375, identifier:isinstance; 376, argument_list; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:self; 379, identifier:_after_response_handlers; 380, tuple; 380, 381; 380, 382; 381, identifier:list; 382, identifier:tuple; 383, block; 383, 384; 384, for_statement; 384, 385; 384, 386; 384, 389; 385, identifier:handler; 386, attribute; 386, 387; 386, 388; 387, identifier:self; 388, identifier:_after_response_handlers; 389, block; 389, 390; 390, expression_statement; 390, 391; 391, call; 391, 392; 391, 393; 392, identifier:handler; 393, argument_list; 393, 394; 393, 395; 394, identifier:context; 395, identifier:response; 396, except_clause; 396, 397; 396, 401; 397, as_pattern; 397, 398; 397, 399; 398, identifier:Exception; 399, as_pattern_target; 399, 400; 400, identifier:e; 401, block; 401, 402; 401, 414; 402, expression_statement; 402, 403; 403, call; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:logging; 406, identifier:exception; 407, argument_list; 407, 408; 407, 409; 408, string:"Failed to process _after_response_handlers for Endpoint %s"; 409, attribute; 409, 410; 409, 413; 410, attribute; 410, 411; 410, 412; 411, identifier:self; 412, identifier:__class__; 413, identifier:__name__; 414, raise_statement; 415, return_statement; 415, 416; 416, identifier:response | def _execute(self, request, **kwargs):
"""The top-level execute function for the endpoint.
This method is intended to remain as-is, and not be overridden.
It gets called by your HTTP framework's route handler, and performs
the following actions to process the request:
``authenticate_request``
Validate the Bearer token, populate the ``current_user``, and make
sure that the token covers the scope needed to call the requested
method.
*
*
``parse arguments``
The argument parser is responsible for:
- First, coercing and patching any parameters that might require
it due to versioning (i.e. the caller is using an old API
version that supports `index` as a parameter for pagination,
but the current version uses the name `offset`)
- Second, iterating through the endpoint's supported arguments
and validating that the params passed in comply with the
endpoint's requirements
- Third, populating the `context.args` array with the validated
arguments
If any of the arguments are invalid, then the Argument parser will
raise an ArgumentError that bubbles up to the `try/catch` block of
the execute method.
*
*
``before handler``
The before_handlers are specified by the Endpoint definition, and
are intended to supporty DRY-ing up your codebase. Have a set of
Endpoints that all need to grab an object from the ORM based on the
same parameter? Make them inherit from an Endpoint subclass that
performs that task in a before_handler!
*
*
``handle``
The core logic of your API endpoint, as implemented by you in your
Endpoint subclass. The API Framework expects ``handle`` to return
a dictionary specifying the response object and the JSON key that
it should hang off of, or a tuple of a dictionary and an HTTP status
code.
*
*
``after_handler``
Like the before_handlers, the ``after_handlers`` happen after the
handle method, and allow the endpoint developer to re-use code for
post-processing data from an endpoint.
*
*
``render response``
Like the argument parser, the response renderer is responsible for
a few things:
- First, it converts the ORM objects into JSON-serializable
Python dictionaries using the Resource objects defined by the
API implementation,
- Second, it does any version parameter coersion, renaming and
reformatting the edge version of the response to match the
version requested by the API caller,
- and Third, it serializes the Python dictionary into the response
format requested by the API caller (right now, we only support
JSON responses, but it'd be reasonble to support something like
HTML or XML or whatever in the future).
The rendered JSON text is then returned as the response that should
be sent by your HTTP framework's routing handler.
*
*
``_after_response_handler``
The `_after_response_handlers` are specified by the Endpoint
definition, and enable manipulation of the response object before it
is returned to the client, but after the response is rendered.
Because these are instancemethods, they may share instance data
from `self` specified in the endpoint's `_handle` method.
``_finalize_content``
The `_finalize_content` method is overridden by the Endpoint and is called
after the response is rendered into a serializable result.
This method is called with two arguments, the context and the rendered content,
and expected to return updated rendered content.
For in-place modification of dicts, this method will still be expected
to return the given argument.
``_allow_cors``
This value is set to enable CORs for a given endpoint.
When set to a string it supplies an explicit value to
'Access-Control-Allow-Origin'.
Set to True, this will allow access from *all* domains;
Access-Control-Allow-Origin = "*"
"""
try:
self._create_context(request)
self._authenticate()
context = get_current_context()
self._parse_args()
if hasattr(self, '_before_handlers') and \
isinstance(self._before_handlers, (list, tuple)):
for handler in self._before_handlers:
handler(context)
context.handler_result = self._handle(context)
if hasattr(self, '_after_handlers') and \
isinstance(self._after_handlers, (list, tuple)):
for handler in self._after_handlers:
handler(context)
self._render()
response = context.response
# After calling ._render(), the response is ready to go, so we
# shouldn't need to handle any other exceptions beyond this point.
except AuthenticationError as e:
if hasattr(e, 'message') and e.message is not None:
message = e.message
else:
message = "You don't have permission to do that."
err = APIError.Forbidden(message)
response = self._response_class(*err.response)
response.headers["Content-Type"] = 'application/json'
except ArgumentError as e:
err = APIError.UnprocessableEntity(e.message)
response = self._response_class(*err.response)
response.headers["Content-Type"] = 'application/json'
except APIError as e:
response = self._response_class(*e.response)
response.headers["Content-Type"] = 'application/json'
except PaleRaisedResponse as r:
response = self._response_class(*r.response)
response.headers["Content-Type"] = 'application/json'
except Exception as e:
logging.exception("Failed to handle Pale Endpoint %s: %r", self.__class__.__name__,
e)
err = APIError.Exception(repr(e))
response = self._response_class(*err.response)
response.headers["Content-Type"] = 'application/json'
allow_cors = getattr(self, "_allow_cors", None)
if allow_cors is True:
response.headers['Access-Control-Allow-Origin'] = '*'
elif isinstance(allow_cors, basestring):
response.headers['Access-Control-Allow-Origin'] = allow_cors
context.response = response
try:
if hasattr(self, '_after_response_handlers') and \
isinstance(self._after_response_handlers, (list, tuple)):
for handler in self._after_response_handlers:
handler(context, response)
except Exception as e:
logging.exception(
"Failed to process _after_response_handlers for Endpoint %s",
self.__class__.__name__)
raise
return response |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:add_entity_errors; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:property_name; 6, default_parameter; 6, 7; 6, 8; 7, identifier:direct_errors; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:schema_errors; 11, None; 12, block; 12, 13; 12, 15; 12, 26; 12, 27; 12, 122; 12, 123; 12, 207; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 23; 16, boolean_operator:and; 16, 17; 16, 20; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:direct_errors; 19, None; 20, comparison_operator:is; 20, 21; 20, 22; 21, identifier:schema_errors; 22, None; 23, block; 23, 24; 24, return_statement; 24, 25; 25, identifier:self; 26, comment; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:direct_errors; 30, None; 31, block; 31, 32; 31, 49; 31, 68; 31, 81; 32, if_statement; 32, 33; 32, 38; 33, comparison_operator:not; 33, 34; 33, 35; 34, identifier:property_name; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:errors; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 46; 41, subscript; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:errors; 45, identifier:property_name; 46, call; 46, 47; 46, 48; 47, identifier:dict; 48, argument_list; 49, if_statement; 49, 50; 49, 57; 50, comparison_operator:not; 50, 51; 50, 52; 51, string:'direct'; 52, subscript; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:errors; 56, identifier:property_name; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 67; 60, subscript; 60, 61; 60, 66; 61, subscript; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:errors; 65, identifier:property_name; 66, string:'direct'; 67, list:[]; 68, if_statement; 68, 69; 68, 75; 69, comparison_operator:is; 69, 70; 69, 74; 70, call; 70, 71; 70, 72; 71, identifier:type; 72, argument_list; 72, 73; 73, identifier:direct_errors; 74, identifier:list; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:direct_errors; 79, list:[direct_errors]; 79, 80; 80, identifier:direct_errors; 81, for_statement; 81, 82; 81, 83; 81, 84; 82, identifier:error; 83, identifier:direct_errors; 84, block; 84, 85; 84, 109; 85, if_statement; 85, 86; 85, 92; 86, not_operator; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:isinstance; 89, argument_list; 89, 90; 89, 91; 90, identifier:error; 91, identifier:Error; 92, block; 92, 93; 92, 97; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:err; 96, string:'Error must be of type {}'; 97, raise_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:x; 101, identifier:InvalidErrorType; 102, argument_list; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:err; 106, identifier:format; 107, argument_list; 107, 108; 108, identifier:Error; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 120; 111, attribute; 111, 112; 111, 119; 112, subscript; 112, 113; 112, 118; 113, subscript; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:errors; 117, identifier:property_name; 118, string:'direct'; 119, identifier:append; 120, argument_list; 120, 121; 121, identifier:error; 122, comment; 123, if_statement; 123, 124; 123, 127; 124, comparison_operator:is; 124, 125; 124, 126; 125, identifier:schema_errors; 126, None; 127, block; 127, 128; 127, 141; 127, 147; 127, 164; 128, if_statement; 128, 129; 128, 134; 129, call; 129, 130; 129, 131; 130, identifier:isinstance; 131, argument_list; 131, 132; 131, 133; 132, identifier:schema_errors; 133, identifier:Result; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:schema_errors; 138, attribute; 138, 139; 138, 140; 139, identifier:schema_errors; 140, identifier:errors; 141, if_statement; 141, 142; 141, 144; 142, not_operator; 142, 143; 143, identifier:schema_errors; 144, block; 144, 145; 145, return_statement; 145, 146; 146, identifier:self; 147, if_statement; 147, 148; 147, 153; 148, comparison_operator:not; 148, 149; 148, 150; 149, identifier:property_name; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:errors; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 161; 156, subscript; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:errors; 160, identifier:property_name; 161, call; 161, 162; 161, 163; 162, identifier:dict; 163, argument_list; 164, if_statement; 164, 165; 164, 172; 164, 183; 165, comparison_operator:not; 165, 166; 165, 167; 166, string:'schema'; 167, subscript; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:errors; 171, identifier:property_name; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 182; 175, subscript; 175, 176; 175, 181; 176, subscript; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:errors; 180, identifier:property_name; 181, string:'schema'; 182, identifier:schema_errors; 183, else_clause; 183, 184; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 194; 187, subscript; 187, 188; 187, 193; 188, subscript; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:errors; 192, identifier:property_name; 193, string:'schema'; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:merge_errors; 198, argument_list; 198, 199; 198, 206; 199, subscript; 199, 200; 199, 205; 200, subscript; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:errors; 204, identifier:property_name; 205, string:'schema'; 206, identifier:schema_errors; 207, return_statement; 207, 208; 208, identifier:self | def add_entity_errors(
self,
property_name,
direct_errors=None,
schema_errors=None
):
"""
Attach nested entity errors
Accepts a list errors coming from validators attached directly,
or a dict of errors produced by a nested schema.
:param property_name: str, property name
:param direct_errors: list, errors from validators attached directly
:param schema_errors: dict, errors from nested schema
:return: shiftschema.result.Result
"""
if direct_errors is None and schema_errors is None:
return self
# direct errors
if direct_errors is not None:
if property_name not in self.errors:
self.errors[property_name] = dict()
if 'direct' not in self.errors[property_name]:
self.errors[property_name]['direct'] = []
if type(direct_errors) is not list:
direct_errors = [direct_errors]
for error in direct_errors:
if not isinstance(error, Error):
err = 'Error must be of type {}'
raise x.InvalidErrorType(err.format(Error))
self.errors[property_name]['direct'].append(error)
# schema errors
if schema_errors is not None:
if isinstance(schema_errors, Result):
schema_errors = schema_errors.errors
if not schema_errors:
return self
if property_name not in self.errors:
self.errors[property_name] = dict()
if 'schema' not in self.errors[property_name]:
self.errors[property_name]['schema'] = schema_errors
else:
self.errors[property_name]['schema'] = self.merge_errors(
self.errors[property_name]['schema'],
schema_errors
)
return self |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:add_collection_errors; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:property_name; 6, default_parameter; 6, 7; 6, 8; 7, identifier:direct_errors; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:collection_errors; 11, None; 12, block; 12, 13; 12, 15; 12, 26; 12, 27; 12, 122; 12, 123; 12, 268; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 23; 16, boolean_operator:and; 16, 17; 16, 20; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:direct_errors; 19, None; 20, comparison_operator:is; 20, 21; 20, 22; 21, identifier:collection_errors; 22, None; 23, block; 23, 24; 24, return_statement; 24, 25; 25, identifier:self; 26, comment; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:direct_errors; 30, None; 31, block; 31, 32; 31, 45; 31, 62; 31, 81; 32, if_statement; 32, 33; 32, 39; 33, comparison_operator:is; 33, 34; 33, 38; 34, call; 34, 35; 34, 36; 35, identifier:type; 36, argument_list; 36, 37; 37, identifier:direct_errors; 38, identifier:list; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:direct_errors; 43, list:[direct_errors]; 43, 44; 44, identifier:direct_errors; 45, if_statement; 45, 46; 45, 51; 46, comparison_operator:not; 46, 47; 46, 48; 47, identifier:property_name; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:errors; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 59; 54, subscript; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:errors; 58, identifier:property_name; 59, call; 59, 60; 59, 61; 60, identifier:dict; 61, argument_list; 62, if_statement; 62, 63; 62, 70; 63, comparison_operator:not; 63, 64; 63, 65; 64, string:'direct'; 65, subscript; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:errors; 69, identifier:property_name; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 80; 73, subscript; 73, 74; 73, 79; 74, subscript; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:errors; 78, identifier:property_name; 79, string:'direct'; 80, list:[]; 81, for_statement; 81, 82; 81, 83; 81, 84; 82, identifier:error; 83, identifier:direct_errors; 84, block; 84, 85; 84, 109; 85, if_statement; 85, 86; 85, 92; 86, not_operator; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:isinstance; 89, argument_list; 89, 90; 89, 91; 90, identifier:error; 91, identifier:Error; 92, block; 92, 93; 92, 97; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:err; 96, string:'Error must be of type {}'; 97, raise_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:x; 101, identifier:InvalidErrorType; 102, argument_list; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:err; 106, identifier:format; 107, argument_list; 107, 108; 108, identifier:Error; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 120; 111, attribute; 111, 112; 111, 119; 112, subscript; 112, 113; 112, 118; 113, subscript; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:errors; 117, identifier:property_name; 118, string:'direct'; 119, identifier:append; 120, argument_list; 120, 121; 121, identifier:error; 122, comment; 123, if_statement; 123, 124; 123, 125; 124, identifier:collection_errors; 125, block; 125, 126; 125, 133; 125, 151; 125, 157; 125, 174; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:enum; 129, call; 129, 130; 129, 131; 130, identifier:enumerate; 131, argument_list; 131, 132; 132, identifier:collection_errors; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:errors_dict; 136, dictionary_comprehension; 136, 137; 136, 140; 136, 145; 137, pair; 137, 138; 137, 139; 138, identifier:i; 139, identifier:e; 140, for_in_clause; 140, 141; 140, 144; 141, pattern_list; 141, 142; 141, 143; 142, identifier:i; 143, identifier:e; 144, identifier:enum; 145, if_clause; 145, 146; 146, not_operator; 146, 147; 147, call; 147, 148; 147, 149; 148, identifier:bool; 149, argument_list; 149, 150; 150, identifier:e; 151, if_statement; 151, 152; 151, 154; 152, not_operator; 152, 153; 153, identifier:errors_dict; 154, block; 154, 155; 155, return_statement; 155, 156; 156, identifier:self; 157, if_statement; 157, 158; 157, 163; 158, comparison_operator:not; 158, 159; 158, 160; 159, identifier:property_name; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:errors; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 171; 166, subscript; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:errors; 170, identifier:property_name; 171, call; 171, 172; 171, 173; 172, identifier:dict; 173, argument_list; 174, if_statement; 174, 175; 174, 182; 174, 193; 175, comparison_operator:not; 175, 176; 175, 177; 176, string:'collection'; 177, subscript; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:errors; 181, identifier:property_name; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 192; 185, subscript; 185, 186; 185, 191; 186, subscript; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:errors; 190, identifier:property_name; 191, string:'collection'; 192, identifier:errors_dict; 193, else_clause; 193, 194; 194, block; 194, 195; 194, 205; 194, 209; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:local; 198, subscript; 198, 199; 198, 204; 199, subscript; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:errors; 203, identifier:property_name; 204, string:'collection'; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:remote; 208, identifier:errors_dict; 209, for_statement; 209, 210; 209, 213; 209, 218; 210, pattern_list; 210, 211; 210, 212; 211, identifier:index; 212, identifier:result; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:remote; 216, identifier:items; 217, argument_list; 218, block; 218, 219; 219, if_statement; 219, 220; 219, 223; 219, 236; 220, comparison_operator:not; 220, 221; 220, 222; 221, identifier:index; 222, identifier:local; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 235; 226, subscript; 226, 227; 226, 234; 227, subscript; 227, 228; 227, 233; 228, subscript; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:errors; 232, identifier:property_name; 233, string:'collection'; 234, identifier:index; 235, identifier:result; 236, else_clause; 236, 237; 237, block; 237, 238; 237, 256; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:merged; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:merge_errors; 245, argument_list; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, subscript; 247, 248; 247, 249; 248, identifier:local; 249, identifier:index; 250, identifier:errors; 251, attribute; 251, 252; 251, 255; 252, subscript; 252, 253; 252, 254; 253, identifier:remote; 254, identifier:index; 255, identifier:errors; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 267; 258, subscript; 258, 259; 258, 266; 259, subscript; 259, 260; 259, 265; 260, subscript; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:errors; 264, identifier:property_name; 265, string:'collection'; 266, identifier:index; 267, identifier:merged; 268, return_statement; 268, 269; 269, identifier:self | def add_collection_errors(
self,
property_name,
direct_errors=None,
collection_errors=None
):
"""
Add collection errors
Accepts a list errors coming from validators attached directly,
or a list of schema results for each item in the collection.
:param property_name: str, property name
:param direct_errors: list, errors from validators attached directly
:param collection_errors: list of results for collection members
:return: shiftschema.result.Result
"""
if direct_errors is None and collection_errors is None:
return self
# direct errors
if direct_errors is not None:
if type(direct_errors) is not list:
direct_errors = [direct_errors]
if property_name not in self.errors:
self.errors[property_name] = dict()
if 'direct' not in self.errors[property_name]:
self.errors[property_name]['direct'] = []
for error in direct_errors:
if not isinstance(error, Error):
err = 'Error must be of type {}'
raise x.InvalidErrorType(err.format(Error))
self.errors[property_name]['direct'].append(error)
# collection errors
if collection_errors:
enum = enumerate(collection_errors)
errors_dict = {i: e for i, e in enum if not bool(e)}
if not errors_dict:
return self
if property_name not in self.errors:
self.errors[property_name] = dict()
if 'collection' not in self.errors[property_name]:
self.errors[property_name]['collection'] = errors_dict
else:
local = self.errors[property_name]['collection']
remote = errors_dict
for index, result in remote.items():
if index not in local:
self.errors[property_name]['collection'][index] = result
else:
merged = self.merge_errors(
local[index].errors,
remote[index].errors
)
self.errors[property_name]['collection'][index] = merged
return self |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:merge_errors; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:errors_local; 6, identifier:errors_remote; 7, block; 7, 8; 7, 10; 7, 312; 7, 313; 8, expression_statement; 8, 9; 9, comment; 10, for_statement; 10, 11; 10, 12; 10, 13; 10, 14; 11, identifier:prop; 12, identifier:errors_remote; 13, comment; 14, block; 14, 15; 14, 29; 14, 35; 14, 48; 14, 54; 14, 67; 14, 68; 14, 113; 14, 117; 14, 121; 14, 142; 14, 163; 14, 164; 14, 182; 14, 183; 14, 205; 14, 206; 14, 235; 14, 236; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:not; 16, 17; 16, 18; 17, identifier:prop; 18, identifier:errors_local; 19, block; 19, 20; 19, 28; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:errors_local; 24, identifier:prop; 25, subscript; 25, 26; 25, 27; 26, identifier:errors_remote; 27, identifier:prop; 28, continue_statement; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:local; 32, subscript; 32, 33; 32, 34; 33, identifier:errors_local; 34, identifier:prop; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:local; 38, conditional_expression:if; 38, 39; 38, 42; 38, 47; 39, attribute; 39, 40; 39, 41; 40, identifier:local; 41, identifier:errors; 42, call; 42, 43; 42, 44; 43, identifier:isinstance; 44, argument_list; 44, 45; 44, 46; 45, identifier:local; 46, identifier:Result; 47, identifier:local; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:remote; 51, subscript; 51, 52; 51, 53; 52, identifier:errors_remote; 53, identifier:prop; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:remote; 57, conditional_expression:if; 57, 58; 57, 61; 57, 66; 58, attribute; 58, 59; 58, 60; 59, identifier:remote; 60, identifier:errors; 61, call; 61, 62; 61, 63; 62, identifier:isinstance; 63, argument_list; 63, 64; 63, 65; 64, identifier:remote; 65, identifier:Result; 66, identifier:remote; 67, comment; 68, if_statement; 68, 69; 68, 78; 69, not_operator; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:isinstance; 72, argument_list; 72, 73; 72, 74; 73, identifier:local; 74, call; 74, 75; 74, 76; 75, identifier:type; 76, argument_list; 76, 77; 77, identifier:remote; 78, block; 78, 79; 78, 83; 78, 87; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:msg; 82, string:'Type mismatch on property [{}] when merging errors. '; 83, expression_statement; 83, 84; 84, augmented_assignment:+=; 84, 85; 84, 86; 85, identifier:msg; 86, string:'Unable to merge [{}] into [{}]'; 87, raise_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:x; 91, identifier:UnableToMergeResultsType; 92, argument_list; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:msg; 96, identifier:format; 97, argument_list; 97, 98; 97, 99; 97, 105; 98, identifier:prop; 99, call; 99, 100; 99, 101; 100, identifier:type; 101, argument_list; 101, 102; 102, subscript; 102, 103; 102, 104; 103, identifier:errors_remote; 104, identifier:prop; 105, call; 105, 106; 105, 107; 106, identifier:type; 107, argument_list; 107, 108; 108, subscript; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:errors; 112, identifier:prop; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:mismatch; 116, string:'Unable to merge nested entity errors with nested '; 117, expression_statement; 117, 118; 118, augmented_assignment:+=; 118, 119; 118, 120; 119, identifier:mismatch; 120, string:'collection errors on property [{}]'; 121, if_statement; 121, 122; 121, 129; 122, boolean_operator:and; 122, 123; 122, 126; 123, comparison_operator:in; 123, 124; 123, 125; 124, string:'schema'; 125, identifier:local; 126, comparison_operator:in; 126, 127; 126, 128; 127, string:'collection'; 128, identifier:remote; 129, block; 129, 130; 130, raise_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:x; 134, identifier:UnableToMergeResultsType; 135, argument_list; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:mismatch; 139, identifier:format; 140, argument_list; 140, 141; 141, identifier:prop; 142, if_statement; 142, 143; 142, 150; 143, boolean_operator:and; 143, 144; 143, 147; 144, comparison_operator:in; 144, 145; 144, 146; 145, string:'collection'; 146, identifier:local; 147, comparison_operator:in; 147, 148; 147, 149; 148, string:'schema'; 149, identifier:remote; 150, block; 150, 151; 151, raise_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:x; 155, identifier:UnableToMergeResultsType; 156, argument_list; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:mismatch; 160, identifier:format; 161, argument_list; 161, 162; 162, identifier:prop; 163, comment; 164, if_statement; 164, 165; 164, 171; 165, comparison_operator:is; 165, 166; 165, 170; 166, call; 166, 167; 166, 168; 167, identifier:type; 168, argument_list; 168, 169; 169, identifier:remote; 170, identifier:list; 171, block; 171, 172; 171, 181; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:errors_local; 177, identifier:prop; 178, identifier:extend; 179, argument_list; 179, 180; 180, identifier:remote; 181, continue_statement; 182, comment; 183, if_statement; 183, 184; 183, 191; 184, boolean_operator:and; 184, 185; 184, 188; 185, comparison_operator:in; 185, 186; 185, 187; 186, string:'direct'; 187, identifier:remote; 188, comparison_operator:in; 188, 189; 188, 190; 189, string:'direct'; 190, identifier:local; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 201; 194, attribute; 194, 195; 194, 200; 195, subscript; 195, 196; 195, 199; 196, subscript; 196, 197; 196, 198; 197, identifier:errors_local; 198, identifier:prop; 199, string:'direct'; 200, identifier:extend; 201, argument_list; 201, 202; 202, subscript; 202, 203; 202, 204; 203, identifier:remote; 204, string:'direct'; 205, comment; 206, if_statement; 206, 207; 206, 214; 207, boolean_operator:and; 207, 208; 207, 211; 208, comparison_operator:in; 208, 209; 208, 210; 209, string:'schema'; 210, identifier:remote; 211, comparison_operator:in; 211, 212; 211, 213; 212, string:'schema'; 213, identifier:local; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 222; 217, subscript; 217, 218; 217, 221; 218, subscript; 218, 219; 218, 220; 219, identifier:errors_local; 220, identifier:prop; 221, string:'schema'; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:self; 225, identifier:merge_errors; 226, argument_list; 226, 227; 226, 232; 227, subscript; 227, 228; 227, 231; 228, subscript; 228, 229; 228, 230; 229, identifier:errors_local; 230, identifier:prop; 231, string:'schema'; 232, subscript; 232, 233; 232, 234; 233, identifier:remote; 234, string:'schema'; 235, comment; 236, if_statement; 236, 237; 236, 244; 237, boolean_operator:and; 237, 238; 237, 241; 238, comparison_operator:in; 238, 239; 238, 240; 239, string:'collection'; 240, identifier:remote; 241, comparison_operator:in; 241, 242; 241, 243; 242, string:'collection'; 243, identifier:local; 244, block; 244, 245; 245, for_statement; 245, 246; 245, 249; 245, 256; 246, pattern_list; 246, 247; 246, 248; 247, identifier:index; 248, identifier:result; 249, call; 249, 250; 249, 255; 250, attribute; 250, 251; 250, 254; 251, subscript; 251, 252; 251, 253; 252, identifier:remote; 253, string:'collection'; 254, identifier:items; 255, argument_list; 256, block; 256, 257; 257, if_statement; 257, 258; 257, 263; 257, 274; 258, comparison_operator:not; 258, 259; 258, 260; 259, identifier:index; 260, subscript; 260, 261; 260, 262; 261, identifier:local; 262, string:'collection'; 263, block; 263, 264; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 273; 266, subscript; 266, 267; 266, 272; 267, subscript; 267, 268; 267, 271; 268, subscript; 268, 269; 268, 270; 269, identifier:errors_local; 270, identifier:prop; 271, string:'collection'; 272, identifier:index; 273, identifier:result; 274, else_clause; 274, 275; 275, block; 275, 276; 275, 302; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:merged; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:self; 282, identifier:merge_errors; 283, argument_list; 283, 284; 283, 293; 284, attribute; 284, 285; 284, 292; 285, subscript; 285, 286; 285, 291; 286, subscript; 286, 287; 286, 290; 287, subscript; 287, 288; 287, 289; 288, identifier:errors_local; 289, identifier:prop; 290, string:'collection'; 291, identifier:index; 292, identifier:errors; 293, attribute; 293, 294; 293, 301; 294, subscript; 294, 295; 294, 300; 295, subscript; 295, 296; 295, 299; 296, subscript; 296, 297; 296, 298; 297, identifier:errors_remote; 298, identifier:prop; 299, string:'collection'; 300, identifier:index; 301, identifier:errors; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 311; 304, subscript; 304, 305; 304, 310; 305, subscript; 305, 306; 305, 309; 306, subscript; 306, 307; 306, 308; 307, identifier:errors_local; 308, identifier:prop; 309, string:'collection'; 310, identifier:index; 311, identifier:merged; 312, comment; 313, return_statement; 313, 314; 314, identifier:errors_local | def merge_errors(self, errors_local, errors_remote):
"""
Merge errors
Recursively traverses error graph to merge remote errors into local
errors to return a new joined graph.
:param errors_local: dict, local errors, will be updated
:param errors_remote: dict, remote errors, provides updates
:return: dict
"""
for prop in errors_remote:
# create if doesn't exist
if prop not in errors_local:
errors_local[prop] = errors_remote[prop]
continue
local = errors_local[prop]
local = local.errors if isinstance(local, Result) else local
remote = errors_remote[prop]
remote = remote.errors if isinstance(remote, Result) else remote
# check compatibility
if not isinstance(local, type(remote)):
msg = 'Type mismatch on property [{}] when merging errors. '
msg += 'Unable to merge [{}] into [{}]'
raise x.UnableToMergeResultsType(msg.format(
prop,
type(errors_remote[prop]),
type(self.errors[prop])
))
mismatch = 'Unable to merge nested entity errors with nested '
mismatch += 'collection errors on property [{}]'
if 'schema' in local and 'collection' in remote:
raise x.UnableToMergeResultsType(mismatch.format(prop))
if 'collection' in local and 'schema' in remote:
raise x.UnableToMergeResultsType(mismatch.format(prop))
# merge simple & state
if type(remote) is list:
errors_local[prop].extend(remote)
continue
# merge direct errors on nested entities and collection
if 'direct' in remote and 'direct' in local:
errors_local[prop]['direct'].extend(remote['direct'])
# merge nested schema errors
if 'schema' in remote and 'schema' in local:
errors_local[prop]['schema'] = self.merge_errors(
errors_local[prop]['schema'],
remote['schema']
)
# merge nested collections errors
if 'collection' in remote and 'collection' in local:
for index, result in remote['collection'].items():
if index not in local['collection']:
errors_local[prop]['collection'][index] = result
else:
merged = self.merge_errors(
errors_local[prop]['collection'][index].errors,
errors_remote[prop]['collection'][index].errors,
)
errors_local[prop]['collection'][index] = merged
# and return
return errors_local |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:cursorPositionChangedEvent; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 17; 5, 25; 5, 33; 5, 34; 5, 35; 5, 36; 5, 47; 5, 54; 5, 65; 5, 66; 5, 67; 5, 68; 5, 69; 5, 96; 5, 97; 5, 105; 5, 106; 5, 107; 5, 108; 5, 109; 5, 120; 5, 121; 5, 122; 5, 187; 5, 188; 5, 206; 5, 207; 5, 208; 5, 217; 5, 225; 5, 231; 5, 239; 5, 240; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:qteWidget; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:sender; 16, argument_list; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:tc; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:qteWidget; 23, identifier:textCursor; 24, argument_list; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:origin; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:tc; 31, identifier:position; 32, argument_list; 33, comment; 34, comment; 35, comment; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:qteWidget; 41, identifier:cursorPositionChanged; 42, identifier:disconnect; 43, argument_list; 43, 44; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:cursorPositionChangedEvent; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:qteRemoveHighlighting; 52, argument_list; 52, 53; 53, identifier:qteWidget; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:qteWidget; 59, identifier:cursorPositionChanged; 60, identifier:connect; 61, argument_list; 61, 62; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:cursorPositionChangedEvent; 65, comment; 66, comment; 67, comment; 68, comment; 69, if_statement; 69, 70; 69, 80; 69, 82; 70, comparison_operator:>=; 70, 71; 70, 72; 71, identifier:origin; 72, call; 72, 73; 72, 74; 73, identifier:len; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:qteWidget; 78, identifier:toPlainText; 79, argument_list; 80, block; 80, 81; 81, return_statement; 82, else_clause; 82, 83; 82, 84; 82, 85; 83, comment; 84, comment; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:char; 89, subscript; 89, 90; 89, 95; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:qteWidget; 93, identifier:toPlainText; 94, argument_list; 95, identifier:origin; 96, comment; 97, if_statement; 97, 98; 97, 103; 98, comparison_operator:not; 98, 99; 98, 100; 99, identifier:char; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:charToHighlight; 103, block; 103, 104; 104, return_statement; 105, comment; 106, comment; 107, comment; 108, comment; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:qteWidget; 114, identifier:cursorPositionChanged; 115, identifier:disconnect; 116, argument_list; 116, 117; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:cursorPositionChangedEvent; 120, comment; 121, comment; 122, if_statement; 122, 123; 122, 130; 122, 159; 123, comparison_operator:==; 123, 124; 123, 125; 124, identifier:char; 125, subscript; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:charToHighlight; 129, integer:0; 130, block; 130, 131; 130, 135; 130, 136; 130, 137; 130, 138; 130, 139; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:start; 134, identifier:origin; 135, comment; 136, comment; 137, comment; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:stop; 142, call; 142, 143; 142, 150; 143, attribute; 143, 144; 143, 149; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:qteWidget; 147, identifier:toPlainText; 148, argument_list; 149, identifier:find; 150, argument_list; 150, 151; 150, 156; 151, subscript; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:charToHighlight; 155, integer:1; 156, binary_operator:+; 156, 157; 156, 158; 157, identifier:start; 158, integer:1; 159, else_clause; 159, 160; 159, 161; 159, 162; 160, comment; 161, comment; 162, block; 162, 163; 162, 167; 162, 168; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:stop; 166, identifier:origin; 167, comment; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:start; 171, call; 171, 172; 171, 179; 172, attribute; 172, 173; 172, 178; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:qteWidget; 176, identifier:toPlainText; 177, argument_list; 178, identifier:rfind; 179, argument_list; 179, 180; 179, 185; 179, 186; 180, subscript; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:charToHighlight; 184, integer:0; 185, integer:0; 186, identifier:stop; 187, comment; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:oldCharFormats; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:highlightCharacters; 195, argument_list; 195, 196; 195, 197; 195, 200; 195, 205; 196, identifier:qteWidget; 197, tuple; 197, 198; 197, 199; 198, identifier:start; 199, identifier:stop; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:QtCore; 203, identifier:Qt; 204, identifier:blue; 205, integer:100; 206, comment; 207, comment; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:data; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:qteMacroData; 215, argument_list; 215, 216; 216, identifier:qteWidget; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:data; 221, identifier:matchingPositions; 222, tuple; 222, 223; 222, 224; 223, identifier:start; 224, identifier:stop; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:data; 229, identifier:oldCharFormats; 230, identifier:oldCharFormats; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:qteSaveMacroData; 236, argument_list; 236, 237; 236, 238; 237, identifier:data; 238, identifier:qteWidget; 239, comment; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 247; 242, attribute; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:qteWidget; 245, identifier:cursorPositionChanged; 246, identifier:connect; 247, argument_list; 247, 248; 248, attribute; 248, 249; 248, 250; 249, identifier:self; 250, identifier:cursorPositionChangedEvent | def cursorPositionChangedEvent(self):
"""
Update the highlighting.
This is an overloaded version of the native Qt slot of
``QTextEdit``.
In this class, the purpose of this slot is to check if the
character to the right of the cursor needs highlighting,
assuming there is a second character to pair with it.
|Args|
* **None**
|Returns|
* **None**
|Raises|
* **None**
"""
# Determine the sender and cursor position.
qteWidget = self.sender()
tc = qteWidget.textCursor()
origin = tc.position()
# Remove all the highlighting. Since this will move the
# cursor, first disconnect this very routine to avoid an
# infinite recursion.
qteWidget.cursorPositionChanged.disconnect(
self.cursorPositionChangedEvent)
self.qteRemoveHighlighting(qteWidget)
qteWidget.cursorPositionChanged.connect(
self.cursorPositionChangedEvent)
# If we are beyond the last character (for instance because
# the cursor was explicitly moved to the end of the buffer)
# then there is no character to the right and will result in
# an error when trying to fetch it.
if origin >= len(qteWidget.toPlainText()):
return
else:
# It is save to retrieve the character to the right of the
# cursor.
char = qteWidget.toPlainText()[origin]
# Return if the character is not in the matching list.
if char not in self.charToHighlight:
return
# Disconnect the 'cursorPositionChanged' signal from this
# function because it will make changes to the cursor position
# and would therefore immediately trigger itself, resulting in
# an infinite recursion.
qteWidget.cursorPositionChanged.disconnect(
self.cursorPositionChangedEvent)
# If we got until here "char" must be one of the two
# characters to highlight.
if char == self.charToHighlight[0]:
start = origin
# Found the first character, so now look for the second
# one. If this second character does not exist the
# function returns '-1' which is safe because the
# ``self.highlightCharacter`` method can deal with this.
stop = qteWidget.toPlainText().find(self.charToHighlight[1],
start + 1)
else:
# Found the second character so the start index is indeed
# the stop index.
stop = origin
# Search for the preceeding first character.
start = qteWidget.toPlainText().rfind(self.charToHighlight[0],
0, stop)
# Highlight the characters.
oldCharFormats = self.highlightCharacters(qteWidget, (start, stop),
QtCore.Qt.blue, 100)
# Store the positions of the changed character in the
# macroData structure of this widget.
data = self.qteMacroData(qteWidget)
data.matchingPositions = (start, stop)
data.oldCharFormats = oldCharFormats
self.qteSaveMacroData(data, qteWidget)
# Reconnect the 'cursorPositionChanged' signal.
qteWidget.cursorPositionChanged.connect(
self.cursorPositionChangedEvent) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:scenarios; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:generate_seed; 7, False; 8, block; 8, 9; 8, 11; 8, 18; 8, 30; 8, 84; 8, 85; 8, 86; 8, 90; 8, 94; 8, 158; 8, 173; 8, 174; 8, 175; 8, 176; 8, 205; 8, 206; 8, 207; 8, 208; 8, 209; 8, 210; 8, 214; 8, 215; 8, 280; 8, 281; 8, 282; 8, 290; 8, 298; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:seed; 14, call; 14, 15; 14, 16; 15, identifier:prime_numbers; 16, argument_list; 16, 17; 17, integer:1000; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:sweeps_all; 21, call; 21, 22; 21, 29; 22, attribute; 22, 23; 22, 28; 23, subscript; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:experiment; 27, string:"sweeps"; 28, identifier:keys; 29, argument_list; 30, if_statement; 30, 31; 30, 36; 30, 74; 31, comparison_operator:in; 31, 32; 31, 33; 32, string:"combinations"; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:experiment; 36, block; 36, 37; 37, if_statement; 37, 38; 37, 47; 37, 48; 37, 63; 38, call; 38, 39; 38, 40; 39, identifier:isinstance; 40, argument_list; 40, 41; 40, 46; 41, subscript; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:experiment; 45, string:"combinations"; 46, identifier:list; 47, comment; 48, block; 48, 49; 48, 60; 48, 61; 48, 62; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:combinations_in_experiment; 52, dictionary; 52, 53; 53, pair; 53, 54; 53, 55; 54, string:" "; 55, subscript; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:experiment; 59, string:"combinations"; 60, comment; 61, comment; 62, comment; 63, else_clause; 63, 64; 63, 65; 64, comment; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:combinations_in_experiment; 69, subscript; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:experiment; 73, string:"combinations"; 74, else_clause; 74, 75; 74, 76; 75, comment; 76, block; 76, 77; 76, 83; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:combinations_in_experiment; 80, call; 80, 81; 80, 82; 81, identifier:dict; 82, argument_list; 83, comment; 84, comment; 85, comment; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:all_combinations_sweeps; 89, list:[]; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:all_combinations; 93, list:[]; 94, for_statement; 94, 95; 94, 98; 94, 103; 94, 104; 95, pattern_list; 95, 96; 95, 97; 96, identifier:key; 97, identifier:combinations_; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:combinations_in_experiment; 101, identifier:items; 102, argument_list; 103, comment; 104, block; 104, 105; 104, 137; 104, 149; 105, if_statement; 105, 106; 105, 108; 105, 109; 105, 119; 106, not_operator; 106, 107; 107, identifier:combinations_; 108, comment; 109, block; 109, 110; 109, 114; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:combinations_sweeps; 113, list:[]; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:combinations; 117, list:[[]]; 117, 118; 118, list:[]; 119, else_clause; 119, 120; 119, 121; 120, comment; 121, block; 121, 122; 121, 128; 121, 129; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:combinations_sweeps; 125, subscript; 125, 126; 125, 127; 126, identifier:combinations_; 127, integer:0; 128, comment; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:combinations; 132, subscript; 132, 133; 132, 134; 133, identifier:combinations_; 134, slice; 134, 135; 134, 136; 135, integer:1; 136, colon; 137, for_statement; 137, 138; 137, 139; 137, 140; 137, 141; 138, identifier:item; 139, identifier:combinations_sweeps; 140, comment; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:all_combinations_sweeps; 146, identifier:append; 147, argument_list; 147, 148; 148, identifier:item; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:all_combinations; 153, identifier:append; 154, argument_list; 154, 155; 155, tuple; 155, 156; 155, 157; 156, identifier:combinations_sweeps; 157, identifier:combinations; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:sweeps_fully_factorial; 161, call; 161, 162; 161, 163; 162, identifier:list; 163, argument_list; 163, 164; 164, binary_operator:-; 164, 165; 164, 169; 165, call; 165, 166; 165, 167; 166, identifier:set; 167, argument_list; 167, 168; 168, identifier:sweeps_all; 169, call; 169, 170; 169, 171; 170, identifier:set; 171, argument_list; 171, 172; 172, identifier:all_combinations_sweeps; 173, comment; 174, comment; 175, comment; 176, for_statement; 176, 177; 176, 178; 176, 179; 177, identifier:sweep; 178, identifier:sweeps_fully_factorial; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:all_combinations; 184, identifier:append; 185, argument_list; 185, 186; 186, tuple; 186, 187; 186, 189; 187, list:[sweep]; 187, 188; 188, identifier:sweep; 189, list_comprehension; 189, 190; 189, 192; 190, list:[x]; 190, 191; 191, identifier:x; 192, for_in_clause; 192, 193; 192, 194; 193, identifier:x; 194, call; 194, 195; 194, 204; 195, attribute; 195, 196; 195, 203; 196, subscript; 196, 197; 196, 202; 197, subscript; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:experiment; 201, string:"sweeps"; 202, identifier:sweep; 203, identifier:keys; 204, argument_list; 205, comment; 206, comment; 207, comment; 208, comment; 209, comment; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:red_iter; 213, integer:0; 214, comment; 215, while_statement; 215, 216; 215, 222; 216, comparison_operator:>; 216, 217; 216, 221; 217, call; 217, 218; 217, 219; 218, identifier:len; 219, argument_list; 219, 220; 220, identifier:all_combinations; 221, integer:1; 222, block; 222, 223; 222, 229; 222, 235; 222, 245; 222, 262; 222, 275; 222, 279; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:comb1; 226, subscript; 226, 227; 226, 228; 227, identifier:all_combinations; 228, integer:0; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:comb2; 232, subscript; 232, 233; 232, 234; 233, identifier:all_combinations; 234, integer:1; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:new_sweeps; 238, binary_operator:+; 238, 239; 238, 242; 239, subscript; 239, 240; 239, 241; 240, identifier:comb1; 241, integer:0; 242, subscript; 242, 243; 242, 244; 243, identifier:comb2; 244, integer:0; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:new_combinations; 248, list_comprehension; 248, 249; 248, 252; 248, 257; 249, binary_operator:+; 249, 250; 249, 251; 250, identifier:x; 251, identifier:y; 252, for_in_clause; 252, 253; 252, 254; 253, identifier:x; 254, subscript; 254, 255; 254, 256; 255, identifier:comb1; 256, integer:1; 257, for_in_clause; 257, 258; 257, 259; 258, identifier:y; 259, subscript; 259, 260; 259, 261; 260, identifier:comb2; 261, integer:1; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:all_combinations; 265, binary_operator:+; 265, 266; 265, 270; 266, list:[(new_sweeps, new_combinations)]; 266, 267; 267, tuple; 267, 268; 267, 269; 268, identifier:new_sweeps; 269, identifier:new_combinations; 270, subscript; 270, 271; 270, 272; 271, identifier:all_combinations; 272, slice; 272, 273; 272, 274; 273, integer:2; 274, colon; 275, expression_statement; 275, 276; 276, augmented_assignment:+=; 276, 277; 276, 278; 277, identifier:red_iter; 278, integer:1; 279, comment; 280, comment; 281, comment; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:sweep_names; 285, subscript; 285, 286; 285, 289; 286, subscript; 286, 287; 286, 288; 287, identifier:all_combinations; 288, integer:0; 289, integer:0; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:combinations; 293, subscript; 293, 294; 293, 297; 294, subscript; 294, 295; 294, 296; 295, identifier:all_combinations; 296, integer:0; 297, integer:1; 298, for_statement; 298, 299; 298, 300; 298, 301; 299, identifier:combination; 300, identifier:combinations; 301, block; 301, 302; 301, 320; 301, 333; 301, 372; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 305; 304, identifier:scenario; 305, call; 305, 306; 305, 307; 306, identifier:Scenario; 307, argument_list; 307, 308; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:self; 311, identifier:_apply_combination; 312, argument_list; 312, 313; 312, 318; 312, 319; 313, subscript; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:self; 316, identifier:experiment; 317, string:"base"; 318, identifier:sweep_names; 319, identifier:combination; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:scenario; 324, identifier:parameters; 325, call; 325, 326; 325, 327; 326, identifier:dict; 327, argument_list; 327, 328; 328, call; 328, 329; 328, 330; 329, identifier:zip; 330, argument_list; 330, 331; 330, 332; 331, identifier:sweep_names; 332, identifier:combination; 333, if_statement; 333, 334; 333, 335; 333, 336; 334, identifier:generate_seed; 335, comment; 336, block; 336, 337; 337, if_statement; 337, 338; 337, 343; 337, 364; 338, comparison_operator:in; 338, 339; 338, 340; 339, string:"@seed@"; 340, attribute; 340, 341; 340, 342; 341, identifier:scenario; 342, identifier:xml; 343, block; 343, 344; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:scenario; 348, identifier:xml; 349, call; 349, 350; 349, 355; 350, attribute; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, identifier:scenario; 353, identifier:xml; 354, identifier:replace; 355, argument_list; 355, 356; 355, 357; 356, string:"@seed@"; 357, call; 357, 358; 357, 359; 358, identifier:str; 359, argument_list; 359, 360; 360, call; 360, 361; 360, 362; 361, identifier:next; 362, argument_list; 362, 363; 363, identifier:seed; 364, else_clause; 364, 365; 365, block; 365, 366; 366, raise_statement; 366, 367; 367, parenthesized_expression; 367, 368; 368, call; 368, 369; 368, 370; 369, identifier:RuntimeError; 370, argument_list; 370, 371; 371, string:"@seed@ placeholder is not found"; 372, expression_statement; 372, 373; 373, yield; 373, 374; 374, identifier:scenario | def scenarios(self, generate_seed=False):
"""
Generator function. Spits out scenarios for this experiment
"""
seed = prime_numbers(1000)
sweeps_all = self.experiment["sweeps"].keys()
if "combinations" in self.experiment:
if isinstance(self.experiment["combinations"], list):
# For backward compatibility with experiments1-4s
combinations_in_experiment = {" ": self.experiment["combinations"]}
# if self.experiment["combinations"] == []:
# # Special notation for fully-factorial experiments
# combinations_in_experiment = {" ":[[],[]]}
else:
# Combinations must be a dictionary in this particular case
combinations_in_experiment = self.experiment["combinations"]
else:
# Support no combinations element:
combinations_in_experiment = dict() # empty dict
# 1) calculate combinations_sweeps (depends on ALL combinations_ items)
# Get the list of fully factorial sweeps
all_combinations_sweeps = []
all_combinations = []
for key, combinations_ in combinations_in_experiment.items():
# generate all permutations of all combinations
if not combinations_:
# Fully factorial experiment, shortcut for "combinations":[[],[]]
combinations_sweeps = []
combinations = [[]]
else:
# First item in combinations list is a list of sweeps
combinations_sweeps = combinations_[0]
# then - all combinations
combinations = combinations_[1:]
for item in combinations_sweeps:
# TODO: error if sweep is already in this list?
all_combinations_sweeps.append(item)
all_combinations.append((combinations_sweeps, combinations))
sweeps_fully_factorial = list(set(sweeps_all) - set(all_combinations_sweeps))
# print "fully fact: %s" % sweeps_fully_factorial
# 2) produce a list of all combinations of fully factorial sweeps
# First sets of "combinations": the fully-factorial sweeps
for sweep in sweeps_fully_factorial:
all_combinations.append(([sweep], [[x] for x in self.experiment["sweeps"][sweep].keys()]))
# 3) take the dot (inner) product of the list above (fully factorial arm combinations)
# with the first combinations list, that with the second combination list, ...
# step-by-step reduce the list of combinations to a single item
# (dot-product of each list of combinations)
# this could use a lot of memory...
red_iter = 0
# print "all combinations:", red_iter, all_combinations
while len(all_combinations) > 1:
comb1 = all_combinations[0]
comb2 = all_combinations[1]
new_sweeps = comb1[0] + comb2[0]
new_combinations = [x+y for x in comb1[1] for y in comb2[1]]
all_combinations = [(new_sweeps, new_combinations)] + all_combinations[2:]
red_iter += 1
# print "all combinations:", red_iter, all_combinations
# 4) write out the document for each in (3), which should specify one arm for each
# sweep with no repetition of combinations
sweep_names = all_combinations[0][0]
combinations = all_combinations[0][1]
for combination in combinations:
scenario = Scenario(self._apply_combination(self.experiment["base"], sweep_names, combination))
scenario.parameters = dict(zip(sweep_names, combination))
if generate_seed:
# Replace seed if requested by the user
if "@seed@" in scenario.xml:
scenario.xml = scenario.xml.replace("@seed@", str(next(seed)))
else:
raise(RuntimeError("@seed@ placeholder is not found"))
yield scenario |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:subdict_by_keys; 3, parameters; 3, 4; 3, 5; 4, identifier:dict_obj; 5, identifier:keys; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, return_statement; 9, 10; 10, dictionary_comprehension; 10, 11; 10, 16; 11, pair; 11, 12; 11, 13; 12, identifier:k; 13, subscript; 13, 14; 13, 15; 14, identifier:dict_obj; 15, identifier:k; 16, for_in_clause; 16, 17; 16, 18; 17, identifier:k; 18, call; 18, 19; 18, 25; 19, attribute; 19, 20; 19, 24; 20, call; 20, 21; 20, 22; 21, identifier:set; 22, argument_list; 22, 23; 23, identifier:keys; 24, identifier:intersection; 25, argument_list; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:dict_obj; 29, identifier:keys; 30, argument_list | def subdict_by_keys(dict_obj, keys):
"""Returns a sub-dict composed solely of the given keys.
Parameters
----------
dict_obj : dict
The dict to create a sub-dict from.
keys : list of str
The keys to keep in the sub-dict. Keys not present in the given dict
will be ignored.
Returns
-------
dict
A sub-dict of the given dict composed solely of the given keys.
Example:
--------
>>> dict_obj = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> subdict = subdict_by_keys(dict_obj, ['b', 'd', 'e'])
>>> print(sorted(subdict.items()))
[('b', 2), ('d', 4)]
"""
return {k: dict_obj[k] for k in set(keys).intersection(dict_obj.keys())} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:deep_merge_dict; 3, parameters; 3, 4; 3, 5; 4, identifier:base; 5, identifier:priority; 6, block; 6, 7; 6, 9; 6, 26; 6, 35; 6, 72; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 23; 10, boolean_operator:or; 10, 11; 10, 17; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 16; 15, identifier:base; 16, identifier:dict; 17, not_operator; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:isinstance; 20, argument_list; 20, 21; 20, 22; 21, identifier:priority; 22, identifier:dict; 23, block; 23, 24; 24, return_statement; 24, 25; 25, identifier:priority; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:result; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:copy; 32, identifier:deepcopy; 33, argument_list; 33, 34; 34, identifier:base; 35, for_statement; 35, 36; 35, 37; 35, 42; 36, identifier:key; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:priority; 40, identifier:keys; 41, argument_list; 42, block; 42, 43; 43, if_statement; 43, 44; 43, 47; 43, 62; 44, comparison_operator:in; 44, 45; 44, 46; 45, identifier:key; 46, identifier:base; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:result; 52, identifier:key; 53, call; 53, 54; 53, 55; 54, identifier:deep_merge_dict; 55, argument_list; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:base; 58, identifier:key; 59, subscript; 59, 60; 59, 61; 60, identifier:priority; 61, identifier:key; 62, else_clause; 62, 63; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:result; 68, identifier:key; 69, subscript; 69, 70; 69, 71; 70, identifier:priority; 71, identifier:key; 72, return_statement; 72, 73; 73, identifier:result | def deep_merge_dict(base, priority):
"""Recursively merges the two given dicts into a single dict.
Treating base as the the initial point of the resulting merged dict,
and considering the nested dictionaries as trees, they are merged os:
1. Every path to every leaf in priority would be represented in the result.
2. Subtrees of base are overwritten if a leaf is found in the
corresponding path in priority.
3. The invariant that all priority leaf nodes remain leafs is maintained.
Parameters
----------
base : dict
The first, lower-priority, dict to merge.
priority : dict
The second, higher-priority, dict to merge.
Returns
-------
dict
A recursive merge of the two given dicts.
Example:
--------
>>> base = {'a': 1, 'b': 2, 'c': {'d': 4}, 'e': 5}
>>> priority = {'a': {'g': 7}, 'c': 3, 'e': 5, 'f': 6}
>>> result = deep_merge_dict(base, priority)
>>> print(sorted(result.items()))
[('a', {'g': 7}), ('b', 2), ('c', 3), ('e', 5), ('f', 6)]
"""
if not isinstance(base, dict) or not isinstance(priority, dict):
return priority
result = copy.deepcopy(base)
for key in priority.keys():
if key in base:
result[key] = deep_merge_dict(base[key], priority[key])
else:
result[key] = priority[key]
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:norm_int_dict; 3, parameters; 3, 4; 4, identifier:int_dict; 5, block; 5, 6; 5, 8; 5, 16; 5, 27; 5, 41; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:norm_dict; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:int_dict; 14, identifier:copy; 15, argument_list; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:val_sum; 19, call; 19, 20; 19, 21; 20, identifier:sum; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:norm_dict; 25, identifier:values; 26, argument_list; 27, for_statement; 27, 28; 27, 29; 27, 30; 28, identifier:key; 29, identifier:norm_dict; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 36; 33, subscript; 33, 34; 33, 35; 34, identifier:norm_dict; 35, identifier:key; 36, binary_operator:/; 36, 37; 36, 40; 37, subscript; 37, 38; 37, 39; 38, identifier:norm_dict; 39, identifier:key; 40, identifier:val_sum; 41, return_statement; 41, 42; 42, identifier:norm_dict | def norm_int_dict(int_dict):
"""Normalizes values in the given dict with int values.
Parameters
----------
int_dict : list
A dict object mapping each key to an int value.
Returns
-------
dict
A dict where each key is mapped to its relative part in the sum of
all dict values.
Example
-------
>>> dict_obj = {'a': 3, 'b': 5, 'c': 2}
>>> result = norm_int_dict(dict_obj)
>>> print(sorted(result.items()))
[('a', 0.3), ('b', 0.5), ('c', 0.2)]
"""
norm_dict = int_dict.copy()
val_sum = sum(norm_dict.values())
for key in norm_dict:
norm_dict[key] = norm_dict[key] / val_sum
return norm_dict |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:sum_num_dicts; 3, parameters; 3, 4; 3, 5; 4, identifier:dicts; 5, default_parameter; 5, 6; 5, 7; 6, identifier:normalize; 7, False; 8, block; 8, 9; 8, 11; 8, 15; 8, 39; 8, 47; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:sum_dict; 14, dictionary; 15, for_statement; 15, 16; 15, 17; 15, 18; 16, identifier:dicti; 17, identifier:dicts; 18, block; 18, 19; 19, for_statement; 19, 20; 19, 21; 19, 22; 20, identifier:key; 21, identifier:dicti; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 28; 25, subscript; 25, 26; 25, 27; 26, identifier:sum_dict; 27, identifier:key; 28, binary_operator:+; 28, 29; 28, 36; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:sum_dict; 32, identifier:get; 33, argument_list; 33, 34; 33, 35; 34, identifier:key; 35, integer:0; 36, subscript; 36, 37; 36, 38; 37, identifier:dicti; 38, identifier:key; 39, if_statement; 39, 40; 39, 41; 40, identifier:normalize; 41, block; 41, 42; 42, return_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:norm_int_dict; 45, argument_list; 45, 46; 46, identifier:sum_dict; 47, return_statement; 47, 48; 48, identifier:sum_dict | def sum_num_dicts(dicts, normalize=False):
"""Sums the given dicts into a single dict mapping each key to the sum
of its mappings in all given dicts.
Parameters
----------
dicts : list
A list of dict objects mapping each key to an numeric value.
normalize : bool, default False
Indicated whether to normalize all values by value sum.
Returns
-------
dict
A dict where each key is mapped to the sum of its mappings in all
given dicts.
Example
-------
>>> dict1 = {'a': 3, 'b': 2}
>>> dict2 = {'a':7, 'c': 8}
>>> result = sum_num_dicts([dict1, dict2])
>>> print(sorted(result.items()))
[('a', 10), ('b', 2), ('c', 8)]
>>> result = sum_num_dicts([dict1, dict2], normalize=True)
>>> print(sorted(result.items()))
[('a', 0.5), ('b', 0.1), ('c', 0.4)]
"""
sum_dict = {}
for dicti in dicts:
for key in dicti:
sum_dict[key] = sum_dict.get(key, 0) + dicti[key]
if normalize:
return norm_int_dict(sum_dict)
return sum_dict |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:reverse_dict; 3, parameters; 3, 4; 4, identifier:dict_obj; 5, block; 5, 6; 5, 8; 5, 12; 5, 31; 5, 49; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:new_dict; 11, dictionary; 12, for_statement; 12, 13; 12, 14; 12, 15; 13, identifier:key; 14, identifier:dict_obj; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:add_to_dict_val_set; 19, argument_list; 19, 20; 19, 23; 19, 28; 20, keyword_argument; 20, 21; 20, 22; 21, identifier:dict_obj; 22, identifier:new_dict; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:key; 25, subscript; 25, 26; 25, 27; 26, identifier:dict_obj; 27, identifier:key; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:val; 30, identifier:key; 31, for_statement; 31, 32; 31, 33; 31, 34; 32, identifier:key; 33, identifier:new_dict; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, subscript; 37, 38; 37, 39; 38, identifier:new_dict; 39, identifier:key; 40, call; 40, 41; 40, 42; 41, identifier:sorted; 42, argument_list; 42, 43; 42, 46; 43, subscript; 43, 44; 43, 45; 44, identifier:new_dict; 45, identifier:key; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:reverse; 48, False; 49, return_statement; 49, 50; 50, identifier:new_dict | def reverse_dict(dict_obj):
"""Reverse a dict, so each value in it maps to a sorted list of its keys.
Parameters
----------
dict_obj : dict
A key-value dict.
Returns
-------
dict
A dict where each value maps to a sorted list of all the unique keys
that mapped to it.
Example
-------
>>> dicti = {'a': 1, 'b': 3, 'c': 1}
>>> reverse_dict(dicti)
{1: ['a', 'c'], 3: ['b']}
"""
new_dict = {}
for key in dict_obj:
add_to_dict_val_set(dict_obj=new_dict, key=dict_obj[key], val=key)
for key in new_dict:
new_dict[key] = sorted(new_dict[key], reverse=False)
return new_dict |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:flatten_dict; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:dict_obj; 5, default_parameter; 5, 6; 5, 7; 6, identifier:separator; 7, string:'.'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:flatten_lists; 10, False; 11, block; 11, 12; 11, 14; 11, 21; 11, 25; 11, 57; 11, 117; 11, 122; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:reducer; 17, call; 17, 18; 17, 19; 18, identifier:_get_key_reducer; 19, argument_list; 19, 20; 20, identifier:separator; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:flat; 24, dictionary; 25, function_definition; 25, 26; 25, 27; 25, 31; 26, function_name:_flatten_key_val; 27, parameters; 27, 28; 27, 29; 27, 30; 28, identifier:key; 29, identifier:val; 30, identifier:parent; 31, block; 31, 32; 31, 40; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:flat_key; 35, call; 35, 36; 35, 37; 36, identifier:reducer; 37, argument_list; 37, 38; 37, 39; 38, identifier:parent; 39, identifier:key; 40, try_statement; 40, 41; 40, 48; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:_flatten; 45, argument_list; 45, 46; 45, 47; 46, identifier:val; 47, identifier:flat_key; 48, except_clause; 48, 49; 48, 50; 49, identifier:TypeError; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:flat; 55, identifier:flat_key; 56, identifier:val; 57, function_definition; 57, 58; 57, 59; 57, 64; 58, function_name:_flatten; 59, parameters; 59, 60; 59, 61; 60, identifier:d; 61, default_parameter; 61, 62; 61, 63; 62, identifier:parent; 63, None; 64, block; 64, 65; 65, try_statement; 65, 66; 65, 84; 66, block; 66, 67; 67, for_statement; 67, 68; 67, 71; 67, 76; 68, pattern_list; 68, 69; 68, 70; 69, identifier:key; 70, identifier:val; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:d; 74, identifier:items; 75, argument_list; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:_flatten_key_val; 80, argument_list; 80, 81; 80, 82; 80, 83; 81, identifier:key; 82, identifier:val; 83, identifier:parent; 84, except_clause; 84, 85; 84, 86; 85, identifier:AttributeError; 86, block; 86, 87; 86, 98; 87, if_statement; 87, 88; 87, 95; 88, call; 88, 89; 88, 90; 89, identifier:isinstance; 90, argument_list; 90, 91; 90, 92; 91, identifier:d; 92, tuple; 92, 93; 92, 94; 93, identifier:str; 94, identifier:bytes; 95, block; 95, 96; 96, raise_statement; 96, 97; 97, identifier:TypeError; 98, for_statement; 98, 99; 98, 102; 98, 106; 99, pattern_list; 99, 100; 99, 101; 100, identifier:i; 101, identifier:value; 102, call; 102, 103; 102, 104; 103, identifier:enumerate; 104, argument_list; 104, 105; 105, identifier:d; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:_flatten_key_val; 110, argument_list; 110, 111; 110, 115; 110, 116; 111, call; 111, 112; 111, 113; 112, identifier:str; 113, argument_list; 113, 114; 114, identifier:i; 115, identifier:value; 116, identifier:parent; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:_flatten; 120, argument_list; 120, 121; 121, identifier:dict_obj; 122, return_statement; 122, 123; 123, identifier:flat | def flatten_dict(dict_obj, separator='.', flatten_lists=False):
"""Flattens the given dict into a single-level dict with flattend keys.
Parameters
----------
dict_obj : dict
A possibly nested dict.
separator : str, optional
The character to use as a separator between keys. Defaults to '.'.
flatten_lists : bool, optional
If True, list values are also flattened. False by default.
Returns
-------
dict
A shallow dict, where no value is a dict in itself, and keys are
concatenations of original key paths separated with the given
separator.
Example
-------
>>> dicti = {'a': 1, 'b': {'g': 4, 'o': 9}, 'x': [4, 'd']}
>>> flat = flatten_dict(dicti)
>>> sorted(flat.items())
[('a', 1), ('b.g', 4), ('b.o', 9), ('x.0', 4), ('x.1', 'd')]
"""
reducer = _get_key_reducer(separator)
flat = {}
def _flatten_key_val(key, val, parent):
flat_key = reducer(parent, key)
try:
_flatten(val, flat_key)
except TypeError:
flat[flat_key] = val
def _flatten(d, parent=None):
try:
for key, val in d.items():
_flatten_key_val(key, val, parent)
except AttributeError:
if isinstance(d, (str, bytes)):
raise TypeError
for i, value in enumerate(d):
_flatten_key_val(str(i), value, parent)
_flatten(dict_obj)
return flat |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:key_value_nested_generator; 3, parameters; 3, 4; 4, identifier:dict_obj; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 12; 8, 17; 9, pattern_list; 9, 10; 9, 11; 10, identifier:key; 11, identifier:value; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:dict_obj; 15, identifier:items; 16, argument_list; 17, block; 17, 18; 18, if_statement; 18, 19; 18, 24; 18, 39; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:value; 23, identifier:dict; 24, block; 24, 25; 25, for_statement; 25, 26; 25, 29; 25, 33; 26, pattern_list; 26, 27; 26, 28; 27, identifier:key; 28, identifier:value; 29, call; 29, 30; 29, 31; 30, identifier:key_value_nested_generator; 31, argument_list; 31, 32; 32, identifier:value; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, yield; 35, 36; 36, expression_list; 36, 37; 36, 38; 37, identifier:key; 38, identifier:value; 39, else_clause; 39, 40; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, yield; 42, 43; 43, expression_list; 43, 44; 43, 45; 44, identifier:key; 45, identifier:value | def key_value_nested_generator(dict_obj):
"""Recursively iterate over key-value pairs of nested dictionaries.
Parameters
----------
dict_obj : dict
The outer-most dict to iterate on.
Returns
-------
generator
A generator over key-value pairs in all nested dictionaries.
Example
-------
>>> dicti = {'a': 1, 'b': {'c': 3, 'd': 4}}
>>> print(sorted(list(key_value_nested_generator(dicti))))
[('a', 1), ('c', 3), ('d', 4)]
"""
for key, value in dict_obj.items():
if isinstance(value, dict):
for key, value in key_value_nested_generator(value):
yield key, value
else:
yield key, value |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:key_tuple_value_nested_generator; 3, parameters; 3, 4; 4, identifier:dict_obj; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 12; 8, 17; 9, pattern_list; 9, 10; 9, 11; 10, identifier:key; 11, identifier:value; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:dict_obj; 15, identifier:items; 16, argument_list; 17, block; 17, 18; 18, if_statement; 18, 19; 18, 24; 18, 45; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:value; 23, identifier:dict; 24, block; 24, 25; 25, for_statement; 25, 26; 25, 29; 25, 33; 26, pattern_list; 26, 27; 26, 28; 27, identifier:nested_key; 28, identifier:value; 29, call; 29, 30; 29, 31; 30, identifier:key_tuple_value_nested_generator; 31, argument_list; 31, 32; 32, identifier:value; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, yield; 35, 36; 36, expression_list; 36, 37; 36, 44; 37, binary_operator:+; 37, 38; 37, 43; 38, call; 38, 39; 38, 40; 39, identifier:tuple; 40, argument_list; 40, 41; 41, list:[key]; 41, 42; 42, identifier:key; 43, identifier:nested_key; 44, identifier:value; 45, else_clause; 45, 46; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, yield; 48, 49; 49, expression_list; 49, 50; 49, 55; 50, call; 50, 51; 50, 52; 51, identifier:tuple; 52, argument_list; 52, 53; 53, list:[key]; 53, 54; 54, identifier:key; 55, identifier:value | def key_tuple_value_nested_generator(dict_obj):
"""Recursively iterate over key-tuple-value pairs of nested dictionaries.
Parameters
----------
dict_obj : dict
The outer-most dict to iterate on.
Returns
-------
generator
A generator over key-tuple-value pairs in all nested dictionaries.
Example
-------
>>> dicti = {'a': 1, 'b': {'c': 3, 'd': 4}}
>>> print(sorted(list(key_tuple_value_nested_generator(dicti))))
[(('a',), 1), (('b', 'c'), 3), (('b', 'd'), 4)]
"""
for key, value in dict_obj.items():
if isinstance(value, dict):
for nested_key, value in key_tuple_value_nested_generator(value):
yield tuple([key]) + nested_key, value
else:
yield tuple([key]), value |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:release_lock; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:verbose; 7, identifier:VERBOSE; 8, default_parameter; 8, 9; 8, 10; 9, identifier:raiseError; 10, identifier:RAISE_ERROR; 11, block; 11, 12; 11, 14; 11, 244; 11, 245; 11, 337; 11, 338; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 26; 14, 35; 15, not_operator; 15, 16; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:os; 20, identifier:path; 21, identifier:isfile; 22, argument_list; 22, 23; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:__lockPath; 26, block; 26, 27; 26, 31; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:released; 30, True; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:code; 34, integer:0; 35, else_clause; 35, 36; 36, block; 36, 37; 37, try_statement; 37, 38; 37, 61; 37, 103; 38, block; 38, 39; 39, with_statement; 39, 40; 39, 52; 40, with_clause; 40, 41; 41, with_item; 41, 42; 42, as_pattern; 42, 43; 42, 50; 43, call; 43, 44; 43, 45; 44, identifier:open; 45, argument_list; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:__lockPath; 49, string:'rb'; 50, as_pattern_target; 50, 51; 51, identifier:fd; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:lock; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:fd; 59, identifier:readlines; 60, argument_list; 61, except_clause; 61, 62; 61, 66; 62, as_pattern; 62, 63; 62, 64; 63, identifier:Exception; 64, as_pattern_target; 64, 65; 65, identifier:err; 66, block; 66, 67; 66, 83; 66, 87; 66, 98; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:code; 70, call; 70, 71; 70, 72; 71, identifier:Exception; 72, argument_list; 72, 73; 73, binary_operator:%; 73, 74; 73, 75; 74, string:"Unable to read release lock file '%s' (%s)"; 75, tuple; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:__lockPath; 79, call; 79, 80; 79, 81; 80, identifier:str; 81, argument_list; 81, 82; 82, identifier:err; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:released; 86, False; 87, if_statement; 87, 88; 87, 89; 88, identifier:verbose; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:print; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:str; 96, argument_list; 96, 97; 97, identifier:code; 98, if_statement; 98, 99; 98, 100; 99, identifier:raiseError; 100, block; 100, 101; 101, raise_statement; 101, 102; 102, identifier:code; 103, else_clause; 103, 104; 104, block; 104, 105; 105, if_statement; 105, 106; 105, 111; 105, 120; 105, 234; 106, not_operator; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:len; 109, argument_list; 109, 110; 110, identifier:lock; 111, block; 111, 112; 111, 116; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:code; 115, integer:1; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:released; 119, True; 120, elif_clause; 120, 121; 120, 136; 121, comparison_operator:==; 121, 122; 121, 129; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:lock; 126, integer:0; 127, identifier:rstrip; 128, argument_list; 129, call; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:__lockPass; 134, identifier:encode; 135, argument_list; 136, block; 136, 137; 137, try_statement; 137, 138; 137, 182; 137, 224; 138, block; 138, 139; 139, with_statement; 139, 140; 139, 152; 139, 153; 140, with_clause; 140, 141; 141, with_item; 141, 142; 142, as_pattern; 142, 143; 142, 150; 143, call; 143, 144; 143, 145; 144, identifier:open; 145, argument_list; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:__lockPath; 149, string:'wb'; 150, as_pattern_target; 150, 151; 151, identifier:f; 152, comment; 153, block; 153, 154; 153, 165; 153, 171; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:f; 158, identifier:write; 159, argument_list; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, string:''; 163, identifier:encode; 164, argument_list; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:f; 169, identifier:flush; 170, argument_list; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:os; 175, identifier:fsync; 176, argument_list; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:f; 180, identifier:fileno; 181, argument_list; 182, except_clause; 182, 183; 182, 187; 183, as_pattern; 183, 184; 183, 185; 184, identifier:Exception; 185, as_pattern_target; 185, 186; 186, identifier:err; 187, block; 187, 188; 187, 192; 187, 208; 187, 219; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:released; 191, False; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:code; 195, call; 195, 196; 195, 197; 196, identifier:Exception; 197, argument_list; 197, 198; 198, binary_operator:%; 198, 199; 198, 200; 199, string:"Unable to write release lock file '%s' (%s)"; 200, tuple; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:__lockPath; 204, call; 204, 205; 204, 206; 205, identifier:str; 206, argument_list; 206, 207; 207, identifier:err; 208, if_statement; 208, 209; 208, 210; 209, identifier:verbose; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 214; 213, identifier:print; 214, argument_list; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:str; 217, argument_list; 217, 218; 218, identifier:code; 219, if_statement; 219, 220; 219, 221; 220, identifier:raiseError; 221, block; 221, 222; 222, raise_statement; 222, 223; 223, identifier:code; 224, else_clause; 224, 225; 225, block; 225, 226; 225, 230; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:released; 229, True; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:code; 233, integer:2; 234, else_clause; 234, 235; 235, block; 235, 236; 235, 240; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:code; 239, integer:4; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:released; 243, False; 244, comment; 245, if_statement; 245, 246; 245, 253; 246, boolean_operator:and; 246, 247; 246, 248; 247, identifier:released; 248, comparison_operator:is; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:__fd; 252, None; 253, block; 253, 254; 254, try_statement; 254, 255; 254, 293; 254, 331; 255, block; 255, 256; 256, if_statement; 256, 257; 256, 263; 257, not_operator; 257, 258; 258, attribute; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:__fd; 262, identifier:closed; 263, block; 263, 264; 263, 272; 263, 285; 264, expression_statement; 264, 265; 265, call; 265, 266; 265, 271; 266, attribute; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:__fd; 270, identifier:flush; 271, argument_list; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:os; 276, identifier:fsync; 277, argument_list; 277, 278; 278, call; 278, 279; 278, 284; 279, attribute; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:self; 282, identifier:__fd; 283, identifier:fileno; 284, argument_list; 285, expression_statement; 285, 286; 286, call; 286, 287; 286, 292; 287, attribute; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:self; 290, identifier:__fd; 291, identifier:close; 292, argument_list; 293, except_clause; 293, 294; 293, 298; 294, as_pattern; 294, 295; 294, 296; 295, identifier:Exception; 296, as_pattern_target; 296, 297; 297, identifier:err; 298, block; 298, 299; 298, 315; 298, 326; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:code; 302, call; 302, 303; 302, 304; 303, identifier:Exception; 304, argument_list; 304, 305; 305, binary_operator:%; 305, 306; 305, 307; 306, string:"Unable to close file descriptor of locked file '%s' (%s)"; 307, tuple; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:__filePath; 311, call; 311, 312; 311, 313; 312, identifier:str; 313, argument_list; 313, 314; 314, identifier:err; 315, if_statement; 315, 316; 315, 317; 316, identifier:verbose; 317, block; 317, 318; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 321; 320, identifier:print; 321, argument_list; 321, 322; 322, call; 322, 323; 322, 324; 323, identifier:str; 324, argument_list; 324, 325; 325, identifier:code; 326, if_statement; 326, 327; 326, 328; 327, identifier:raiseError; 328, block; 328, 329; 329, raise_statement; 329, 330; 330, identifier:code; 331, else_clause; 331, 332; 332, block; 332, 333; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:code; 336, integer:3; 337, comment; 338, return_statement; 338, 339; 339, expression_list; 339, 340; 339, 341; 340, identifier:released; 341, identifier:code | def release_lock(self, verbose=VERBOSE, raiseError=RAISE_ERROR):
"""
Release the lock when set and close file descriptor if opened.
:Parameters:
#. verbose (bool): Whether to be verbose about errors when encountered
#. raiseError (bool): Whether to raise error exception when encountered
:Returns:
#. result (boolean): Whether the lock is succesfully released.
#. code (integer, Exception): Integer code indicating the reason how the
lock was successfully or unsuccessfully released. When releasing the
lock generates an error, this will be caught and returned in a message
Exception code.
* 0: Lock is not found, therefore successfully released
* 1: Lock is found empty, therefore successfully released
* 2: Lock is found owned by this locker and successfully released
* 3: Lock is found owned by this locker and successfully released and locked file descriptor was successfully closed
* 4: Lock is found owned by another locker, this locker has no permission to release it. Therefore unsuccessfully released
* Exception: Lock was not successfully released because of an unexpected error.
The error is caught and returned in this Exception. In this case
result is False.
"""
if not os.path.isfile(self.__lockPath):
released = True
code = 0
else:
try:
with open(self.__lockPath, 'rb') as fd:
lock = fd.readlines()
except Exception as err:
code = Exception( "Unable to read release lock file '%s' (%s)"%(self.__lockPath,str(err)) )
released = False
if verbose: print(str(code))
if raiseError: raise code
else:
if not len(lock):
code = 1
released = True
elif lock[0].rstrip() == self.__lockPass.encode():
try:
with open(self.__lockPath, 'wb') as f:
#f.write( ''.encode('utf-8') )
f.write( ''.encode() )
f.flush()
os.fsync(f.fileno())
except Exception as err:
released = False
code = Exception( "Unable to write release lock file '%s' (%s)"%(self.__lockPath,str(err)) )
if verbose: print(str(code))
if raiseError: raise code
else:
released = True
code = 2
else:
code = 4
released = False
# close file descriptor if lock is released and descriptor is not None
if released and self.__fd is not None:
try:
if not self.__fd.closed:
self.__fd.flush()
os.fsync(self.__fd.fileno())
self.__fd.close()
except Exception as err:
code = Exception( "Unable to close file descriptor of locked file '%s' (%s)"%(self.__filePath,str(err)) )
if verbose: print(str(code))
if raiseError: raise code
else:
code = 3
# return
return released, code |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:guess_codec; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:file; 5, default_parameter; 5, 6; 5, 7; 6, identifier:errors; 7, string:"strict"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:require_char; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 22; 11, 23; 11, 30; 11, 38; 11, 44; 11, 45; 11, 227; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:gedcom_char_to_codec; 18, dictionary; 18, 19; 19, pair; 19, 20; 19, 21; 20, string:'ansel'; 21, string:'gedcom'; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:bom_codec; 26, call; 26, 27; 26, 28; 27, identifier:check_bom; 28, argument_list; 28, 29; 29, identifier:file; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:bom_size; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:file; 36, identifier:tell; 37, argument_list; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:codec; 41, boolean_operator:or; 41, 42; 41, 43; 42, identifier:bom_codec; 43, string:'gedcom'; 44, comment; 45, while_statement; 45, 46; 45, 47; 45, 48; 46, True; 47, comment; 48, block; 48, 49; 48, 57; 48, 66; 48, 67; 48, 68; 48, 69; 48, 70; 48, 83; 48, 91; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:line; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:file; 55, identifier:readline; 56, argument_list; 57, if_statement; 57, 58; 57, 60; 58, not_operator; 58, 59; 59, identifier:line; 60, block; 60, 61; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:IOError; 64, argument_list; 64, 65; 65, string:"Unexpected EOF while reading GEDCOM header"; 66, comment; 67, comment; 68, comment; 69, comment; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:line; 73, call; 73, 74; 73, 81; 74, attribute; 74, 75; 74, 80; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:line; 78, identifier:lstrip; 79, argument_list; 80, identifier:rstrip; 81, argument_list; 81, 82; 82, string:b"\r\n"; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:words; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:line; 89, identifier:split; 90, argument_list; 91, if_statement; 91, 92; 91, 110; 91, 111; 91, 123; 92, boolean_operator:and; 92, 93; 92, 105; 93, boolean_operator:and; 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:words; 99, integer:2; 100, comparison_operator:==; 100, 101; 100, 104; 101, subscript; 101, 102; 101, 103; 102, identifier:words; 103, integer:0; 104, string:b"0"; 105, comparison_operator:!=; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:words; 108, integer:1; 109, string:b"HEAD"; 110, comment; 111, block; 111, 112; 112, if_statement; 112, 113; 112, 114; 112, 120; 113, identifier:require_char; 114, block; 114, 115; 115, raise_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:CodecError; 118, argument_list; 118, 119; 119, string:"GEDCOM header does not have CHAR record"; 120, else_clause; 120, 121; 121, block; 121, 122; 122, break_statement; 123, elif_clause; 123, 124; 123, 142; 124, boolean_operator:and; 124, 125; 124, 137; 125, boolean_operator:and; 125, 126; 125, 132; 126, comparison_operator:>=; 126, 127; 126, 131; 127, call; 127, 128; 127, 129; 128, identifier:len; 129, argument_list; 129, 130; 130, identifier:words; 131, integer:3; 132, comparison_operator:==; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:words; 135, integer:0; 136, string:b"1"; 137, comparison_operator:==; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:words; 140, integer:1; 141, string:b"CHAR"; 142, block; 142, 143; 142, 199; 142, 226; 143, try_statement; 143, 144; 143, 186; 144, block; 144, 145; 144, 157; 144, 175; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:encoding; 148, call; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:words; 152, integer:2; 153, identifier:decode; 154, argument_list; 154, 155; 154, 156; 155, identifier:codec; 156, identifier:errors; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:encoding; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:gedcom_char_to_codec; 163, identifier:get; 164, argument_list; 164, 165; 164, 170; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:encoding; 168, identifier:lower; 169, argument_list; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:encoding; 173, identifier:lower; 174, argument_list; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:new_codec; 178, attribute; 178, 179; 178, 185; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:codecs; 182, identifier:lookup; 183, argument_list; 183, 184; 184, identifier:encoding; 185, identifier:name; 186, except_clause; 186, 187; 186, 188; 187, identifier:LookupError; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:CodecError; 192, argument_list; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, string:"Unknown codec name {0}"; 196, identifier:format; 197, argument_list; 197, 198; 198, identifier:encoding; 199, if_statement; 199, 200; 199, 203; 199, 208; 200, comparison_operator:is; 200, 201; 200, 202; 201, identifier:bom_codec; 202, None; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:codec; 207, identifier:new_codec; 208, elif_clause; 208, 209; 208, 212; 209, comparison_operator:!=; 209, 210; 209, 211; 210, identifier:new_codec; 211, identifier:bom_codec; 212, block; 212, 213; 213, raise_statement; 213, 214; 214, call; 214, 215; 214, 216; 215, identifier:CodecError; 216, argument_list; 216, 217; 217, call; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, concatenated_string; 219, 220; 219, 221; 220, string:"CHAR codec {0} is different from BOM "; 221, string:"codec {1}"; 222, identifier:format; 223, argument_list; 223, 224; 223, 225; 224, identifier:new_codec; 225, identifier:bom_codec; 226, break_statement; 227, return_statement; 227, 228; 228, expression_list; 228, 229; 228, 230; 229, identifier:codec; 230, identifier:bom_size | def guess_codec(file, errors="strict", require_char=False):
"""Look at file contents and guess its correct encoding.
File must be open in binary mode and positioned at offset 0. If BOM
record is present then it is assumed to be UTF-8 or UTF-16 encoded
file. GEDCOM header is searched for CHAR record and encoding name
is extracted from it, if BOM record is present then CHAR record
must match BOM-defined encoding.
:param file: File object, must be open in binary mode.
:param str errors: Controls error handling behavior during string
decoding, accepts same values as standard `codecs.decode` method.
:param bool require_char: If True then exception is thrown if CHAR
record is not found in a header, if False and CHAR is not in the
header then codec determined from BOM or "gedcom" is returned.
:returns: Tuple (codec_name, bom_size)
:raises: :py:class:`CodecError` when codec name in file is unknown or
when codec name in file contradicts codec determined from BOM.
:raises: :py:class:`UnicodeDecodeError` when codec fails to decode
input lines and `errors` is set to "strict" (default).
"""
# mapping of gedcom character set specifiers to Python encoding names
gedcom_char_to_codec = {
'ansel': 'gedcom',
}
# check BOM first
bom_codec = check_bom(file)
bom_size = file.tell()
codec = bom_codec or 'gedcom'
# scan header until CHAR or end of header
while True:
# this stops at '\n'
line = file.readline()
if not line:
raise IOError("Unexpected EOF while reading GEDCOM header")
# do not decode bytes to strings here, reason is that some
# stupid apps split CONC record at byte level (in middle of
# of multi-byte characters). This implies that we can only
# work with encodings that have ASCII as single-byte subset.
line = line.lstrip().rstrip(b"\r\n")
words = line.split()
if len(words) >= 2 and words[0] == b"0" and words[1] != b"HEAD":
# past header but have not seen CHAR
if require_char:
raise CodecError("GEDCOM header does not have CHAR record")
else:
break
elif len(words) >= 3 and words[0] == b"1" and words[1] == b"CHAR":
try:
encoding = words[2].decode(codec, errors)
encoding = gedcom_char_to_codec.get(encoding.lower(),
encoding.lower())
new_codec = codecs.lookup(encoding).name
except LookupError:
raise CodecError("Unknown codec name {0}".format(encoding))
if bom_codec is None:
codec = new_codec
elif new_codec != bom_codec:
raise CodecError("CHAR codec {0} is different from BOM "
"codec {1}".format(new_codec, bom_codec))
break
return codec, bom_size |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:read_record; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:offset; 6, block; 6, 7; 6, 9; 6, 17; 6, 21; 6, 22; 6, 26; 6, 173; 6, 226; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:_log; 13, identifier:debug; 14, argument_list; 14, 15; 14, 16; 15, string:"in read_record(%s)"; 16, identifier:offset; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:stack; 20, list:[]; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:reclevel; 25, None; 26, for_statement; 26, 27; 26, 28; 26, 34; 27, identifier:gline; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:gedcom_lines; 32, argument_list; 32, 33; 33, identifier:offset; 34, block; 34, 35; 34, 43; 34, 49; 34, 66; 34, 67; 34, 68; 34, 114; 34, 115; 34, 123; 34, 124; 34, 142; 34, 143; 34, 156; 34, 166; 34, 167; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:_log; 39, identifier:debug; 40, argument_list; 40, 41; 40, 42; 41, string:" read_record, gline: %s"; 42, identifier:gline; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:level; 46, attribute; 46, 47; 46, 48; 47, identifier:gline; 48, identifier:level; 49, if_statement; 49, 50; 49, 53; 49, 54; 49, 59; 50, comparison_operator:is; 50, 51; 50, 52; 51, identifier:reclevel; 52, None; 53, comment; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:reclevel; 58, identifier:level; 59, elif_clause; 59, 60; 59, 63; 59, 64; 60, comparison_operator:<=; 60, 61; 60, 62; 61, identifier:level; 62, identifier:reclevel; 63, comment; 64, block; 64, 65; 65, break_statement; 66, comment; 67, comment; 68, for_statement; 68, 69; 68, 70; 68, 78; 68, 79; 69, identifier:rec; 70, call; 70, 71; 70, 72; 71, identifier:reversed; 72, argument_list; 72, 73; 73, subscript; 73, 74; 73, 75; 74, identifier:stack; 75, slice; 75, 76; 75, 77; 76, identifier:level; 77, colon; 78, comment; 79, block; 79, 80; 80, if_statement; 80, 81; 80, 82; 81, identifier:rec; 82, block; 82, 83; 82, 108; 83, if_statement; 83, 84; 83, 89; 84, comparison_operator:is; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:rec; 87, identifier:value; 88, None; 89, block; 89, 90; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:rec; 94, identifier:value; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:rec; 99, identifier:value; 100, identifier:decode; 101, argument_list; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:_encoding; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:_errors; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:rec; 112, identifier:freeze; 113, argument_list; 114, comment; 115, delete_statement; 115, 116; 116, subscript; 116, 117; 116, 118; 117, identifier:stack; 118, slice; 118, 119; 118, 122; 119, binary_operator:+; 119, 120; 119, 121; 120, identifier:level; 121, integer:1; 122, colon; 123, comment; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:stack; 128, identifier:extend; 129, argument_list; 129, 130; 130, binary_operator:*; 130, 131; 130, 133; 131, list:[None]; 131, 132; 132, None; 133, parenthesized_expression; 133, 134; 134, binary_operator:-; 134, 135; 134, 138; 135, binary_operator:+; 135, 136; 135, 137; 136, identifier:level; 137, integer:1; 138, call; 138, 139; 138, 140; 139, identifier:len; 140, argument_list; 140, 141; 141, identifier:stack; 142, comment; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:parent; 146, conditional_expression:if; 146, 147; 146, 152; 146, 155; 147, subscript; 147, 148; 147, 149; 148, identifier:stack; 149, binary_operator:-; 149, 150; 149, 151; 150, identifier:level; 151, integer:1; 152, comparison_operator:>; 152, 153; 152, 154; 153, identifier:level; 154, integer:0; 155, None; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:rec; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:_make_record; 163, argument_list; 163, 164; 163, 165; 164, identifier:parent; 165, identifier:gline; 166, comment; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:stack; 171, identifier:level; 172, identifier:rec; 173, for_statement; 173, 174; 173, 175; 173, 183; 174, identifier:rec; 175, call; 175, 176; 175, 177; 176, identifier:reversed; 177, argument_list; 177, 178; 178, subscript; 178, 179; 178, 180; 179, identifier:stack; 180, slice; 180, 181; 180, 182; 181, identifier:reclevel; 182, colon; 183, block; 183, 184; 184, if_statement; 184, 185; 184, 186; 185, identifier:rec; 186, block; 186, 187; 186, 212; 186, 218; 187, if_statement; 187, 188; 187, 193; 188, comparison_operator:is; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:rec; 191, identifier:value; 192, None; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:rec; 198, identifier:value; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:rec; 203, identifier:value; 204, identifier:decode; 205, argument_list; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:_encoding; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:_errors; 212, expression_statement; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:rec; 216, identifier:freeze; 217, argument_list; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:_log; 222, identifier:debug; 223, argument_list; 223, 224; 223, 225; 224, string:" read_record, rec: %s"; 225, identifier:rec; 226, return_statement; 226, 227; 227, conditional_expression:if; 227, 228; 227, 231; 227, 232; 228, subscript; 228, 229; 228, 230; 229, identifier:stack; 230, identifier:reclevel; 231, identifier:stack; 232, None | def read_record(self, offset):
"""Read next complete record from a file starting at given position.
Reads the record at given position and all its sub-records. Stops
reading at EOF or next record with the same or higher (smaller) level
number. File position after return from this method is not specified,
re-position file if you want to read other records.
:param int offset: Position in file to start reading from.
:return: :py:class:`model.Record` instance or None if offset points
past EOF.
:raises: :py:exc:`ParserError` if `offsets` does not point to the
beginning of a record or for any parsing errors.
"""
_log.debug("in read_record(%s)", offset)
stack = [] # stores per-level current records
reclevel = None
for gline in self.gedcom_lines(offset):
_log.debug(" read_record, gline: %s", gline)
level = gline.level
if reclevel is None:
# this is the first record, remember its level
reclevel = level
elif level <= reclevel:
# stop at the record of the same or higher (smaller) level
break
# All previously seen records at this level and below can
# be finalized now
for rec in reversed(stack[level:]):
# decode bytes value into string
if rec:
if rec.value is not None:
rec.value = rec.value.decode(self._encoding,
self._errors)
rec.freeze()
# _log.debug(" read_record, rec: %s", rec)
del stack[level + 1:]
# extend stack to fit this level (and make parent levels if needed)
stack.extend([None] * (level + 1 - len(stack)))
# make Record out of it (it can be updated later)
parent = stack[level - 1] if level > 0 else None
rec = self._make_record(parent, gline)
# store as current record at this level
stack[level] = rec
for rec in reversed(stack[reclevel:]):
if rec:
if rec.value is not None:
rec.value = rec.value.decode(self._encoding, self._errors)
rec.freeze()
_log.debug(" read_record, rec: %s", rec)
return stack[reclevel] if stack else None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_make_record; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:parent; 6, identifier:gline; 7, block; 7, 8; 7, 10; 7, 72; 7, 73; 7, 79; 7, 104; 7, 146; 7, 147; 7, 159; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 20; 10, 21; 11, boolean_operator:and; 11, 12; 11, 13; 12, identifier:parent; 13, comparison_operator:in; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:gline; 16, identifier:tag; 17, tuple; 17, 18; 17, 19; 18, string:"CONT"; 19, string:"CONC"; 20, comment; 21, block; 21, 22; 21, 70; 22, if_statement; 22, 23; 22, 28; 22, 29; 23, comparison_operator:!=; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:parent; 26, identifier:tag; 27, string:"BLOB"; 28, comment; 29, block; 29, 30; 29, 36; 29, 52; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:value; 33, attribute; 33, 34; 33, 35; 34, identifier:gline; 35, identifier:value; 36, if_statement; 36, 37; 36, 42; 37, comparison_operator:==; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:gline; 40, identifier:tag; 41, string:"CONT"; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:value; 46, binary_operator:+; 46, 47; 46, 48; 47, string:b"\n"; 48, parenthesized_expression; 48, 49; 49, boolean_operator:or; 49, 50; 49, 51; 50, identifier:value; 51, string:b""; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:value; 55, None; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:parent; 61, identifier:value; 62, binary_operator:+; 62, 63; 62, 69; 63, parenthesized_expression; 63, 64; 64, boolean_operator:or; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:parent; 67, identifier:value; 68, string:b""; 69, identifier:value; 70, return_statement; 70, 71; 71, None; 72, comment; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:dialect; 76, attribute; 76, 77; 76, 78; 77, identifier:model; 78, identifier:DIALECT_DEFAULT; 79, if_statement; 79, 80; 79, 97; 80, boolean_operator:and; 80, 81; 80, 94; 81, not_operator; 81, 82; 82, parenthesized_expression; 82, 83; 83, boolean_operator:and; 83, 84; 83, 89; 84, comparison_operator:==; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:gline; 87, identifier:level; 88, integer:0; 89, comparison_operator:==; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:gline; 92, identifier:tag; 93, string:"HEAD"; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:_header; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:dialect; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:dialect; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:rec; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:model; 110, identifier:make_record; 111, argument_list; 111, 112; 111, 117; 111, 122; 111, 127; 111, 132; 111, 135; 111, 140; 111, 143; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:level; 114, attribute; 114, 115; 114, 116; 115, identifier:gline; 116, identifier:level; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:xref_id; 119, attribute; 119, 120; 119, 121; 120, identifier:gline; 121, identifier:xref_id; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:tag; 124, attribute; 124, 125; 124, 126; 125, identifier:gline; 126, identifier:tag; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:value; 129, attribute; 129, 130; 129, 131; 130, identifier:gline; 131, identifier:value; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:sub_records; 134, list:[]; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:offset; 137, attribute; 137, 138; 137, 139; 138, identifier:gline; 139, identifier:offset; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:dialect; 142, identifier:dialect; 143, keyword_argument; 143, 144; 143, 145; 144, identifier:parser; 145, identifier:self; 146, comment; 147, if_statement; 147, 148; 147, 149; 148, identifier:parent; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 157; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:parent; 155, identifier:sub_records; 156, identifier:append; 157, argument_list; 157, 158; 158, identifier:rec; 159, return_statement; 159, 160; 160, identifier:rec | def _make_record(self, parent, gline):
"""Process next record.
This method created new record from the line read from file if
needed and/or updates its parent record. If the parent record tag
is ``BLOB`` and new record tag is ``CONT`` then record is skipped
entirely and None is returned. Otherwise if new record tag is ``CONT``
or ``CONC`` its value is added to parent value. For all other tags
new record is made and it is added to parent sub_records attribute.
Parameters
----------
parent : `model.Record`
Parent record of the new record
gline : `gedcom_line`
Current parsed line
Returns
-------
`model.Record` or None
"""
if parent and gline.tag in ("CONT", "CONC"):
# concatenate, only for non-BLOBs
if parent.tag != "BLOB":
# have to be careful concatenating empty/None values
value = gline.value
if gline.tag == "CONT":
value = b"\n" + (value or b"")
if value is not None:
parent.value = (parent.value or b"") + value
return None
# avoid infinite cycle
dialect = model.DIALECT_DEFAULT
if not (gline.level == 0 and gline.tag == "HEAD") and self._header:
dialect = self.dialect
rec = model.make_record(level=gline.level, xref_id=gline.xref_id,
tag=gline.tag, value=gline.value,
sub_records=[], offset=gline.offset,
dialect=dialect, parser=self)
# add to parent's sub-records list
if parent:
parent.sub_records.append(rec)
return rec |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:parse; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:gvid; 6, default_parameter; 6, 7; 6, 8; 7, identifier:exception; 8, True; 9, block; 9, 10; 9, 12; 9, 27; 9, 36; 9, 59; 9, 147; 9, 186; 9, 197; 9, 214; 9, 222; 9, 228; 9, 249; 9, 268; 9, 278; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:==; 13, 14; 13, 15; 14, identifier:gvid; 15, string:'invalid'; 16, block; 16, 17; 17, return_statement; 17, 18; 18, call; 18, 19; 18, 25; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:cls; 22, identifier:get_class; 23, argument_list; 23, 24; 24, string:'null'; 25, argument_list; 25, 26; 26, integer:0; 27, if_statement; 27, 28; 27, 33; 28, not_operator; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:bool; 31, argument_list; 31, 32; 32, identifier:gvid; 33, block; 33, 34; 34, return_statement; 34, 35; 35, None; 36, if_statement; 36, 37; 36, 45; 37, not_operator; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:isinstance; 40, argument_list; 40, 41; 40, 42; 41, identifier:gvid; 42, attribute; 42, 43; 42, 44; 43, identifier:six; 44, identifier:string_types; 45, block; 45, 46; 46, raise_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:TypeError; 49, argument_list; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, string:"Can't parse; not a string. Got a '{}' "; 53, identifier:format; 54, argument_list; 54, 55; 55, call; 55, 56; 55, 57; 56, identifier:type; 57, argument_list; 57, 58; 58, identifier:gvid; 59, try_statement; 59, 60; 59, 112; 60, block; 60, 61; 61, if_statement; 61, 62; 61, 66; 61, 67; 61, 103; 62, not_operator; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:cls; 65, identifier:sl; 66, comment; 67, block; 67, 68; 67, 89; 68, if_statement; 68, 69; 68, 72; 68, 79; 69, attribute; 69, 70; 69, 71; 70, identifier:six; 71, identifier:PY3; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:fn; 76, attribute; 76, 77; 76, 78; 77, identifier:cls; 78, identifier:decode; 79, else_clause; 79, 80; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:fn; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:cls; 87, identifier:decode; 88, identifier:__func__; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:sl; 92, call; 92, 93; 92, 94; 93, identifier:fn; 94, argument_list; 94, 95; 95, subscript; 95, 96; 95, 97; 96, identifier:gvid; 97, slice; 97, 98; 97, 99; 97, 100; 98, integer:0; 99, colon; 100, attribute; 100, 101; 100, 102; 101, identifier:cls; 102, identifier:sl_width; 103, else_clause; 103, 104; 104, block; 104, 105; 104, 111; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:sl; 108, attribute; 108, 109; 108, 110; 109, identifier:cls; 110, identifier:sl; 111, comment; 112, except_clause; 112, 113; 112, 117; 113, as_pattern; 113, 114; 113, 115; 114, identifier:ValueError; 115, as_pattern_target; 115, 116; 116, identifier:e; 117, block; 117, 118; 118, if_statement; 118, 119; 118, 120; 118, 135; 119, identifier:exception; 120, block; 120, 121; 121, raise_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:ValueError; 124, argument_list; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, string:"Failed to parse gvid '{}': {}"; 128, identifier:format; 129, argument_list; 129, 130; 129, 131; 130, identifier:gvid; 131, call; 131, 132; 131, 133; 132, identifier:str; 133, argument_list; 133, 134; 134, identifier:e; 135, else_clause; 135, 136; 136, block; 136, 137; 137, return_statement; 137, 138; 138, call; 138, 139; 138, 145; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:cls; 142, identifier:get_class; 143, argument_list; 143, 144; 144, string:'null'; 145, argument_list; 145, 146; 146, integer:0; 147, try_statement; 147, 148; 147, 157; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:cls; 152, subscript; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:cls; 155, identifier:sl_map; 156, identifier:sl; 157, except_clause; 157, 158; 157, 159; 158, identifier:KeyError; 159, block; 159, 160; 160, if_statement; 160, 161; 160, 162; 160, 174; 161, identifier:exception; 162, block; 162, 163; 163, raise_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:ValueError; 166, argument_list; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, string:"Failed to parse gvid '{}': Unknown summary level '{}' "; 170, identifier:format; 171, argument_list; 171, 172; 171, 173; 172, identifier:gvid; 173, identifier:sl; 174, else_clause; 174, 175; 175, block; 175, 176; 176, return_statement; 176, 177; 177, call; 177, 178; 177, 184; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:cls; 181, identifier:get_class; 182, argument_list; 182, 183; 183, string:'null'; 184, argument_list; 184, 185; 185, integer:0; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:m; 189, call; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:cls; 193, identifier:regex; 194, identifier:match; 195, argument_list; 195, 196; 196, identifier:gvid; 197, if_statement; 197, 198; 197, 200; 198, not_operator; 198, 199; 199, identifier:m; 200, block; 200, 201; 201, raise_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:ValueError; 204, argument_list; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, string:"Failed to match '{}' to '{}' "; 208, identifier:format; 209, argument_list; 209, 210; 209, 211; 210, identifier:gvid; 211, attribute; 211, 212; 211, 213; 212, identifier:cls; 213, identifier:regex_str; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:d; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:m; 220, identifier:groupdict; 221, argument_list; 222, if_statement; 222, 223; 222, 225; 223, not_operator; 223, 224; 224, identifier:d; 225, block; 225, 226; 226, return_statement; 226, 227; 227, None; 228, if_statement; 228, 229; 228, 232; 228, 239; 229, attribute; 229, 230; 229, 231; 230, identifier:six; 231, identifier:PY3; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:fn; 236, attribute; 236, 237; 236, 238; 237, identifier:cls; 238, identifier:decode; 239, else_clause; 239, 240; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:fn; 244, attribute; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:cls; 247, identifier:decode; 248, identifier:__func__; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:d; 252, dictionary_comprehension; 252, 253; 252, 259; 253, pair; 253, 254; 253, 255; 254, identifier:k; 255, call; 255, 256; 255, 257; 256, identifier:fn; 257, argument_list; 257, 258; 258, identifier:v; 259, for_in_clause; 259, 260; 259, 263; 260, pattern_list; 260, 261; 260, 262; 261, identifier:k; 262, identifier:v; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:d; 266, identifier:items; 267, argument_list; 268, try_statement; 268, 269; 268, 274; 269, block; 269, 270; 270, delete_statement; 270, 271; 271, subscript; 271, 272; 271, 273; 272, identifier:d; 273, string:'sl'; 274, except_clause; 274, 275; 274, 276; 275, identifier:KeyError; 276, block; 276, 277; 277, pass_statement; 278, return_statement; 278, 279; 279, call; 279, 280; 279, 281; 280, identifier:cls; 281, argument_list; 281, 282; 282, dictionary_splat; 282, 283; 283, identifier:d | def parse(cls, gvid, exception=True):
"""
Parse a string value into the geoid of this class.
:param gvid: String value to parse.
:param exception: If true ( default) raise an eception on parse erorrs. If False, return a
'null' geoid.
:return:
"""
if gvid == 'invalid':
return cls.get_class('null')(0)
if not bool(gvid):
return None
if not isinstance(gvid, six.string_types):
raise TypeError("Can't parse; not a string. Got a '{}' ".format(type(gvid)))
try:
if not cls.sl:
# Civick and ACS include the SL, so can call from base type.
if six.PY3:
fn = cls.decode
else:
fn = cls.decode.__func__
sl = fn(gvid[0:cls.sl_width])
else:
sl = cls.sl # Otherwise must use derived class.
except ValueError as e:
if exception:
raise ValueError("Failed to parse gvid '{}': {}".format(gvid, str(e)))
else:
return cls.get_class('null')(0)
try:
cls = cls.sl_map[sl]
except KeyError:
if exception:
raise ValueError("Failed to parse gvid '{}': Unknown summary level '{}' ".format(gvid, sl))
else:
return cls.get_class('null')(0)
m = cls.regex.match(gvid)
if not m:
raise ValueError("Failed to match '{}' to '{}' ".format(gvid, cls.regex_str))
d = m.groupdict()
if not d:
return None
if six.PY3:
fn = cls.decode
else:
fn = cls.decode.__func__
d = {k: fn(v) for k, v in d.items()}
try:
del d['sl']
except KeyError:
pass
return cls(**d) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:filter_pastdate; 3, parameters; 3, 4; 3, 5; 4, identifier:string; 5, default_parameter; 5, 6; 5, 7; 6, identifier:default; 7, None; 8, block; 8, 9; 8, 11; 8, 21; 8, 31; 8, 32; 8, 57; 8, 77; 8, 121; 8, 178; 8, 184; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 18; 12, boolean_operator:and; 12, 13; 12, 15; 13, not_operator; 13, 14; 14, identifier:string; 15, comparison_operator:is; 15, 16; 15, 17; 16, identifier:default; 17, None; 18, block; 18, 19; 19, return_statement; 19, 20; 20, identifier:default; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:today; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:datetime; 28, identifier:date; 29, identifier:today; 30, argument_list; 31, comment; 32, try_statement; 32, 33; 32, 49; 33, block; 33, 34; 33, 48; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:parts; 37, call; 37, 38; 37, 39; 38, identifier:map; 39, argument_list; 39, 40; 39, 41; 40, identifier:int; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:re; 44, identifier:split; 45, argument_list; 45, 46; 45, 47; 46, string:'\D+'; 47, identifier:string; 48, comment; 49, except_clause; 49, 50; 49, 51; 50, identifier:ValueError; 51, block; 51, 52; 52, raise_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:InvalidInputError; 55, argument_list; 55, 56; 56, string:"invalid date; use format: DD [MM [YYYY]]"; 57, if_statement; 57, 58; 57, 71; 58, boolean_operator:or; 58, 59; 58, 65; 59, comparison_operator:<; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:parts; 64, integer:1; 65, comparison_operator:>; 65, 66; 65, 70; 66, call; 66, 67; 66, 68; 67, identifier:len; 68, argument_list; 68, 69; 69, identifier:parts; 70, integer:3; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:InvalidInputError; 75, argument_list; 75, 76; 76, string:"invalid date; use format: DD [MM [YYYY]]"; 77, if_statement; 77, 78; 77, 84; 77, 85; 78, comparison_operator:==; 78, 79; 78, 83; 79, call; 79, 80; 79, 81; 80, identifier:len; 81, argument_list; 81, 82; 82, identifier:parts; 83, integer:1; 84, comment; 85, block; 85, 86; 85, 108; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:parts; 90, identifier:append; 91, argument_list; 91, 92; 92, conditional_expression:if; 92, 93; 92, 98; 92, 105; 93, binary_operator:-; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:today; 96, identifier:month; 97, integer:1; 98, comparison_operator:>; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:parts; 101, integer:0; 102, attribute; 102, 103; 102, 104; 103, identifier:today; 104, identifier:day; 105, attribute; 105, 106; 105, 107; 106, identifier:today; 107, identifier:month; 108, if_statement; 108, 109; 108, 114; 109, comparison_operator:<; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:parts; 112, integer:1; 113, integer:1; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:parts; 119, integer:1; 120, integer:12; 121, if_statement; 121, 122; 121, 128; 121, 129; 122, comparison_operator:==; 122, 123; 122, 127; 123, call; 123, 124; 123, 125; 124, identifier:len; 125, argument_list; 125, 126; 126, identifier:parts; 127, integer:2; 128, comment; 129, block; 129, 130; 130, if_statement; 130, 131; 130, 155; 130, 167; 131, boolean_operator:or; 131, 132; 131, 139; 131, 140; 132, comparison_operator:>; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:parts; 135, integer:1; 136, attribute; 136, 137; 136, 138; 137, identifier:today; 138, identifier:month; 139, line_continuation:\; 140, boolean_operator:and; 140, 141; 140, 148; 141, comparison_operator:==; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:parts; 144, integer:1; 145, attribute; 145, 146; 145, 147; 146, identifier:today; 147, identifier:month; 148, comparison_operator:>; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:parts; 151, integer:0; 152, attribute; 152, 153; 152, 154; 153, identifier:today; 154, identifier:day; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:parts; 160, identifier:append; 161, argument_list; 161, 162; 162, binary_operator:-; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:today; 165, identifier:year; 166, integer:1; 167, else_clause; 167, 168; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:parts; 173, identifier:append; 174, argument_list; 174, 175; 175, attribute; 175, 176; 175, 177; 176, identifier:today; 177, identifier:year; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:parts; 182, identifier:reverse; 183, argument_list; 184, try_statement; 184, 185; 184, 208; 185, block; 185, 186; 185, 196; 185, 206; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:date; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:datetime; 192, identifier:date; 193, argument_list; 193, 194; 194, list_splat; 194, 195; 195, identifier:parts; 196, if_statement; 196, 197; 196, 200; 197, comparison_operator:>; 197, 198; 197, 199; 198, identifier:date; 199, identifier:today; 200, block; 200, 201; 201, raise_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:InvalidInputError; 204, argument_list; 204, 205; 205, string:"cannot choose a date in the future"; 206, return_statement; 206, 207; 207, identifier:date; 208, except_clause; 208, 209; 208, 210; 209, identifier:ValueError; 210, block; 210, 211; 210, 213; 211, print_statement; 211, 212; 212, identifier:parts; 213, raise_statement; 213, 214; 214, call; 214, 215; 214, 216; 215, identifier:InvalidInputError; 216, argument_list; 216, 217; 217, string:"invalid date; use format: DD [MM [YYYY]]" | def filter_pastdate(string, default=None):
"""Coerce to a date not beyond the current date
If only a day is given, assumes the current month if that day has
passed or is the current day, otherwise assumes the previous month.
If a day and month are given, but no year, assumes the current year
if the given date has passed (or is today), otherwise the previous
year.
"""
if not string and default is not None:
return default
today = datetime.date.today()
# split the string
try:
parts = map(int, re.split('\D+', string)) # split the string
except ValueError:
raise InvalidInputError("invalid date; use format: DD [MM [YYYY]]")
if len(parts) < 1 or len(parts) > 3:
raise InvalidInputError("invalid date; use format: DD [MM [YYYY]]")
if len(parts) == 1:
# no month or year given; append month
parts.append(today.month - 1 if parts[0] > today.day else today.month)
if parts[1] < 1:
parts[1] = 12
if len(parts) == 2:
# no year given; append year
if parts[1] > today.month \
or parts[1] == today.month and parts[0] > today.day:
parts.append(today.year - 1)
else:
parts.append(today.year)
parts.reverse()
try:
date = datetime.date(*parts)
if date > today:
raise InvalidInputError("cannot choose a date in the future")
return date
except ValueError:
print parts
raise InvalidInputError("invalid date; use format: DD [MM [YYYY]]") |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:get_rooms; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:lid; 6, default_parameter; 6, 7; 6, 8; 7, identifier:start; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:end; 11, None; 12, block; 12, 13; 12, 15; 12, 19; 12, 80; 12, 99; 12, 111; 12, 121; 12, 122; 12, 132; 12, 148; 12, 157; 12, 165; 12, 179; 12, 197; 12, 212; 12, 455; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:range_str; 18, string:"availability"; 19, if_statement; 19, 20; 19, 21; 19, 74; 20, identifier:start; 21, block; 21, 22; 21, 54; 21, 60; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:start_datetime; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:datetime; 29, identifier:datetime; 30, identifier:combine; 31, argument_list; 31, 32; 31, 45; 32, call; 32, 33; 32, 44; 33, attribute; 33, 34; 33, 43; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:datetime; 38, identifier:datetime; 39, identifier:strptime; 40, argument_list; 40, 41; 40, 42; 41, identifier:start; 42, string:"%Y-%m-%d"; 43, identifier:date; 44, argument_list; 45, call; 45, 46; 45, 53; 46, attribute; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:datetime; 50, identifier:datetime; 51, identifier:min; 52, identifier:time; 53, argument_list; 54, expression_statement; 54, 55; 55, augmented_assignment:+=; 55, 56; 55, 57; 56, identifier:range_str; 57, binary_operator:+; 57, 58; 57, 59; 58, string:"="; 59, identifier:start; 60, if_statement; 60, 61; 60, 67; 61, boolean_operator:and; 61, 62; 61, 63; 62, identifier:end; 63, not_operator; 63, 64; 64, comparison_operator:==; 64, 65; 64, 66; 65, identifier:start; 66, identifier:end; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, augmented_assignment:+=; 69, 70; 69, 71; 70, identifier:range_str; 71, binary_operator:+; 71, 72; 71, 73; 72, string:","; 73, identifier:end; 74, else_clause; 74, 75; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:start_datetime; 79, None; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:resp; 83, call; 83, 84; 83, 98; 84, attribute; 84, 85; 84, 97; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:_request; 89, argument_list; 89, 90; 89, 91; 90, string:"GET"; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, string:"/1.1/space/categories/{}"; 94, identifier:format; 95, argument_list; 95, 96; 96, identifier:lid; 97, identifier:json; 98, argument_list; 99, if_statement; 99, 100; 99, 103; 100, comparison_operator:in; 100, 101; 100, 102; 101, string:"error"; 102, identifier:resp; 103, block; 103, 104; 104, raise_statement; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:APIError; 107, argument_list; 107, 108; 108, subscript; 108, 109; 108, 110; 109, identifier:resp; 110, string:"error"; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:output; 114, dictionary; 114, 115; 114, 118; 115, pair; 115, 116; 115, 117; 116, string:"id"; 117, identifier:lid; 118, pair; 118, 119; 118, 120; 119, string:"categories"; 120, list:[]; 121, comment; 122, if_statement; 122, 123; 122, 129; 123, comparison_operator:<; 123, 124; 123, 128; 124, call; 124, 125; 124, 126; 125, identifier:len; 126, argument_list; 126, 127; 127, identifier:resp; 128, integer:1; 129, block; 129, 130; 130, return_statement; 130, 131; 131, identifier:output; 132, if_statement; 132, 133; 132, 138; 133, comparison_operator:in; 133, 134; 133, 135; 134, string:"error"; 135, subscript; 135, 136; 135, 137; 136, identifier:resp; 137, integer:0; 138, block; 138, 139; 139, raise_statement; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:APIError; 142, argument_list; 142, 143; 143, subscript; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:resp; 146, integer:0; 147, string:"error"; 148, if_statement; 148, 149; 148, 154; 149, comparison_operator:not; 149, 150; 149, 151; 150, string:"categories"; 151, subscript; 151, 152; 151, 153; 152, identifier:resp; 153, integer:0; 154, block; 154, 155; 155, return_statement; 155, 156; 156, identifier:output; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:categories; 160, subscript; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:resp; 163, integer:0; 164, string:"categories"; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:id_to_category; 168, dictionary_comprehension; 168, 169; 168, 176; 169, pair; 169, 170; 169, 173; 170, subscript; 170, 171; 170, 172; 171, identifier:i; 172, string:"cid"; 173, subscript; 173, 174; 173, 175; 174, identifier:i; 175, string:"name"; 176, for_in_clause; 176, 177; 176, 178; 177, identifier:i; 178, identifier:categories; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:categories; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, string:","; 185, identifier:join; 186, argument_list; 186, 187; 187, list_comprehension; 187, 188; 187, 194; 188, call; 188, 189; 188, 190; 189, identifier:str; 190, argument_list; 190, 191; 191, subscript; 191, 192; 191, 193; 192, identifier:x; 193, string:"cid"; 194, for_in_clause; 194, 195; 194, 196; 195, identifier:x; 196, identifier:categories; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:resp; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:_request; 204, argument_list; 204, 205; 204, 206; 205, string:"GET"; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, string:"/1.1/space/category/{}"; 209, identifier:format; 210, argument_list; 210, 211; 211, identifier:categories; 212, for_statement; 212, 213; 212, 214; 212, 219; 213, identifier:category; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:resp; 217, identifier:json; 218, argument_list; 219, block; 219, 220; 219, 239; 219, 240; 219, 251; 219, 257; 219, 273; 219, 289; 219, 441; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:cat_out; 223, dictionary; 223, 224; 223, 229; 223, 236; 224, pair; 224, 225; 224, 226; 225, string:"cid"; 226, subscript; 226, 227; 226, 228; 227, identifier:category; 228, string:"cid"; 229, pair; 229, 230; 229, 231; 230, string:"name"; 231, subscript; 231, 232; 231, 233; 232, identifier:id_to_category; 233, subscript; 233, 234; 233, 235; 234, identifier:category; 235, string:"cid"; 236, pair; 236, 237; 236, 238; 237, string:"rooms"; 238, list:[]; 239, comment; 240, if_statement; 240, 241; 240, 249; 241, call; 241, 242; 241, 247; 242, attribute; 242, 243; 242, 246; 243, subscript; 243, 244; 243, 245; 244, identifier:cat_out; 245, string:"name"; 246, identifier:endswith; 247, argument_list; 247, 248; 248, string:"Equipment"; 249, block; 249, 250; 250, continue_statement; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:items; 254, subscript; 254, 255; 254, 256; 255, identifier:category; 256, string:"items"; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:items; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, string:","; 263, identifier:join; 264, argument_list; 264, 265; 265, list_comprehension; 265, 266; 265, 270; 266, call; 266, 267; 266, 268; 267, identifier:str; 268, argument_list; 268, 269; 269, identifier:x; 270, for_in_clause; 270, 271; 270, 272; 271, identifier:x; 272, identifier:items; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:resp; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:self; 279, identifier:_request; 280, argument_list; 280, 281; 280, 282; 281, string:"GET"; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, string:"/1.1/space/item/{}?{}"; 285, identifier:format; 286, argument_list; 286, 287; 286, 288; 287, identifier:items; 288, identifier:range_str; 289, for_statement; 289, 290; 289, 291; 289, 296; 290, identifier:room; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:resp; 294, identifier:json; 295, argument_list; 296, block; 296, 297; 296, 305; 296, 306; 296, 336; 296, 337; 296, 370; 296, 371; 296, 380; 296, 381; 296, 382; 296, 432; 297, if_statement; 297, 298; 297, 303; 298, comparison_operator:in; 298, 299; 298, 302; 299, subscript; 299, 300; 299, 301; 300, identifier:room; 301, string:"id"; 302, identifier:ROOM_BLACKLIST; 303, block; 303, 304; 304, continue_statement; 305, comment; 306, if_statement; 306, 307; 306, 314; 307, boolean_operator:and; 307, 308; 307, 311; 308, comparison_operator:in; 308, 309; 308, 310; 309, string:"image"; 310, identifier:room; 311, subscript; 311, 312; 311, 313; 312, identifier:room; 313, string:"image"; 314, block; 314, 315; 315, if_statement; 315, 316; 315, 325; 316, not_operator; 316, 317; 317, call; 317, 318; 317, 323; 318, attribute; 318, 319; 318, 322; 319, subscript; 319, 320; 319, 321; 320, identifier:room; 321, string:"image"; 322, identifier:startswith; 323, argument_list; 323, 324; 324, string:"http"; 325, block; 325, 326; 326, expression_statement; 326, 327; 327, assignment; 327, 328; 327, 331; 328, subscript; 328, 329; 328, 330; 329, identifier:room; 330, string:"image"; 331, binary_operator:+; 331, 332; 331, 333; 332, string:"https:"; 333, subscript; 333, 334; 333, 335; 334, identifier:room; 335, string:"image"; 336, comment; 337, if_statement; 337, 338; 337, 341; 338, comparison_operator:in; 338, 339; 338, 340; 339, string:"description"; 340, identifier:room; 341, block; 341, 342; 341, 354; 342, expression_statement; 342, 343; 343, assignment; 343, 344; 343, 345; 344, identifier:description; 345, call; 345, 346; 345, 351; 346, attribute; 346, 347; 346, 350; 347, subscript; 347, 348; 347, 349; 348, identifier:room; 349, string:"description"; 350, identifier:replace; 351, argument_list; 351, 352; 351, 353; 352, string:u'\xa0'; 353, string:u' '; 354, expression_statement; 354, 355; 355, assignment; 355, 356; 355, 359; 356, subscript; 356, 357; 356, 358; 357, identifier:room; 358, string:"description"; 359, call; 359, 360; 359, 369; 360, attribute; 360, 361; 360, 368; 361, attribute; 361, 362; 361, 367; 362, call; 362, 363; 362, 364; 363, identifier:BeautifulSoup; 364, argument_list; 364, 365; 364, 366; 365, identifier:description; 366, string:"html.parser"; 367, identifier:text; 368, identifier:strip; 369, argument_list; 370, comment; 371, if_statement; 371, 372; 371, 375; 372, comparison_operator:in; 372, 373; 372, 374; 373, string:"formid"; 374, identifier:room; 375, block; 375, 376; 376, delete_statement; 376, 377; 377, subscript; 377, 378; 377, 379; 378, identifier:room; 379, string:"formid"; 380, comment; 381, comment; 382, if_statement; 382, 383; 382, 384; 383, identifier:start_datetime; 384, block; 384, 385; 384, 389; 384, 426; 385, expression_statement; 385, 386; 386, assignment; 386, 387; 386, 388; 387, identifier:out_times; 388, list:[]; 389, for_statement; 389, 390; 389, 391; 389, 394; 390, identifier:time; 391, subscript; 391, 392; 391, 393; 392, identifier:room; 393, string:"availability"; 394, block; 394, 395; 394, 414; 395, expression_statement; 395, 396; 396, assignment; 396, 397; 396, 398; 397, identifier:parsed_start; 398, call; 398, 399; 398, 404; 399, attribute; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:datetime; 402, identifier:datetime; 403, identifier:strptime; 404, argument_list; 404, 405; 404, 413; 405, subscript; 405, 406; 405, 409; 406, subscript; 406, 407; 406, 408; 407, identifier:time; 408, string:"from"; 409, slice; 409, 410; 409, 411; 410, colon; 411, unary_operator:-; 411, 412; 412, integer:6; 413, string:"%Y-%m-%dT%H:%M:%S"; 414, if_statement; 414, 415; 414, 418; 415, comparison_operator:>=; 415, 416; 415, 417; 416, identifier:parsed_start; 417, identifier:start_datetime; 418, block; 418, 419; 419, expression_statement; 419, 420; 420, call; 420, 421; 420, 424; 421, attribute; 421, 422; 421, 423; 422, identifier:out_times; 423, identifier:append; 424, argument_list; 424, 425; 425, identifier:time; 426, expression_statement; 426, 427; 427, assignment; 427, 428; 427, 431; 428, subscript; 428, 429; 428, 430; 429, identifier:room; 430, string:"availability"; 431, identifier:out_times; 432, expression_statement; 432, 433; 433, call; 433, 434; 433, 439; 434, attribute; 434, 435; 434, 438; 435, subscript; 435, 436; 435, 437; 436, identifier:cat_out; 437, string:"rooms"; 438, identifier:append; 439, argument_list; 439, 440; 440, identifier:room; 441, if_statement; 441, 442; 441, 445; 442, subscript; 442, 443; 442, 444; 443, identifier:cat_out; 444, string:"rooms"; 445, block; 445, 446; 446, expression_statement; 446, 447; 447, call; 447, 448; 447, 453; 448, attribute; 448, 449; 448, 452; 449, subscript; 449, 450; 449, 451; 450, identifier:output; 451, string:"categories"; 452, identifier:append; 453, argument_list; 453, 454; 454, identifier:cat_out; 455, return_statement; 455, 456; 456, identifier:output | def get_rooms(self, lid, start=None, end=None):
"""Returns a list of rooms and their availabilities, grouped by category.
:param lid: The ID of the location to retrieve rooms for.
:type lid: int
:param start: The start range for the availabilities to retrieve, in YYYY-MM-DD format.
:type start: str
:param end: The end range for the availabilities to retrieve, in YYYY-MM-DD format.
:type end: str
"""
range_str = "availability"
if start:
start_datetime = datetime.datetime.combine(datetime.datetime.strptime(start, "%Y-%m-%d").date(), datetime.datetime.min.time())
range_str += "=" + start
if end and not start == end:
range_str += "," + end
else:
start_datetime = None
resp = self._request("GET", "/1.1/space/categories/{}".format(lid)).json()
if "error" in resp:
raise APIError(resp["error"])
output = {"id": lid, "categories": []}
# if there aren't any rooms associated with this location, return
if len(resp) < 1:
return output
if "error" in resp[0]:
raise APIError(resp[0]["error"])
if "categories" not in resp[0]:
return output
categories = resp[0]["categories"]
id_to_category = {i["cid"]: i["name"] for i in categories}
categories = ",".join([str(x["cid"]) for x in categories])
resp = self._request("GET", "/1.1/space/category/{}".format(categories))
for category in resp.json():
cat_out = {"cid": category["cid"], "name": id_to_category[category["cid"]], "rooms": []}
# ignore equipment categories
if cat_out["name"].endswith("Equipment"):
continue
items = category["items"]
items = ",".join([str(x) for x in items])
resp = self._request("GET", "/1.1/space/item/{}?{}".format(items, range_str))
for room in resp.json():
if room["id"] in ROOM_BLACKLIST:
continue
# prepend protocol to urls
if "image" in room and room["image"]:
if not room["image"].startswith("http"):
room["image"] = "https:" + room["image"]
# convert html descriptions to text
if "description" in room:
description = room["description"].replace(u'\xa0', u' ')
room["description"] = BeautifulSoup(description, "html.parser").text.strip()
# remove extra fields
if "formid" in room:
del room["formid"]
# enforce date filter
# API returns dates outside of the range, fix this manually
if start_datetime:
out_times = []
for time in room["availability"]:
parsed_start = datetime.datetime.strptime(time["from"][:-6], "%Y-%m-%dT%H:%M:%S")
if parsed_start >= start_datetime:
out_times.append(time)
room["availability"] = out_times
cat_out["rooms"].append(room)
if cat_out["rooms"]:
output["categories"].append(cat_out)
return output |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 28; 2, function_name:reconstruct_ancestral_states; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 4, identifier:tree; 5, identifier:character; 6, identifier:states; 7, default_parameter; 7, 8; 7, 9; 8, identifier:prediction_method; 9, identifier:MPPA; 10, default_parameter; 10, 11; 10, 12; 11, identifier:model; 12, identifier:F81; 13, default_parameter; 13, 14; 13, 15; 14, identifier:params; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:avg_br_len; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:num_nodes; 21, None; 22, default_parameter; 22, 23; 22, 24; 23, identifier:num_tips; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:force_joint; 27, True; 28, block; 28, 29; 28, 31; 28, 63; 28, 79; 28, 97; 28, 108; 28, 207; 28, 223; 29, expression_statement; 29, 30; 30, comment; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 41; 33, attribute; 33, 34; 33, 40; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:logging; 37, identifier:getLogger; 38, argument_list; 38, 39; 39, string:'pastml'; 40, identifier:debug; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, string:'ACR settings for {}:\n\tMethod:\t{}{}.'; 45, identifier:format; 46, argument_list; 46, 47; 46, 48; 46, 49; 47, identifier:character; 48, identifier:prediction_method; 49, conditional_expression:if; 49, 50; 49, 56; 49, 62; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, string:'\n\tModel:\t{}'; 53, identifier:format; 54, argument_list; 54, 55; 55, identifier:model; 56, boolean_operator:and; 56, 57; 56, 58; 57, identifier:model; 58, call; 58, 59; 58, 60; 59, identifier:is_ml; 60, argument_list; 60, 61; 61, identifier:prediction_method; 62, string:''; 63, if_statement; 63, 64; 63, 67; 64, comparison_operator:==; 64, 65; 64, 66; 65, identifier:COPY; 66, identifier:prediction_method; 67, block; 67, 68; 68, return_statement; 68, 69; 69, dictionary; 69, 70; 69, 73; 69, 76; 70, pair; 70, 71; 70, 72; 71, identifier:CHARACTER; 72, identifier:character; 73, pair; 73, 74; 73, 75; 74, identifier:STATES; 75, identifier:states; 76, pair; 76, 77; 76, 78; 77, identifier:METHOD; 78, identifier:prediction_method; 79, if_statement; 79, 80; 79, 82; 80, not_operator; 80, 81; 81, identifier:num_nodes; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:num_nodes; 86, call; 86, 87; 86, 88; 87, identifier:sum; 88, generator_expression; 88, 89; 88, 90; 89, integer:1; 90, for_in_clause; 90, 91; 90, 92; 91, identifier:_; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:tree; 95, identifier:traverse; 96, argument_list; 97, if_statement; 97, 98; 97, 100; 98, not_operator; 98, 99; 99, identifier:num_tips; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:num_tips; 104, call; 104, 105; 104, 106; 105, identifier:len; 106, argument_list; 106, 107; 107, identifier:tree; 108, if_statement; 108, 109; 108, 113; 109, call; 109, 110; 109, 111; 110, identifier:is_ml; 111, argument_list; 111, 112; 112, identifier:prediction_method; 113, block; 113, 114; 113, 141; 113, 151; 113, 167; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:avg_br_len; 117, None; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:avg_br_len; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:np; 125, identifier:mean; 126, generator_expression; 126, 127; 126, 130; 126, 137; 127, attribute; 127, 128; 127, 129; 128, identifier:n; 129, identifier:dist; 130, for_in_clause; 130, 131; 130, 132; 131, identifier:n; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:tree; 135, identifier:traverse; 136, argument_list; 137, if_clause; 137, 138; 138, attribute; 138, 139; 138, 140; 139, identifier:n; 140, identifier:dist; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 147; 143, pattern_list; 143, 144; 143, 145; 143, 146; 144, identifier:freqs; 145, identifier:sf; 146, identifier:kappa; 147, expression_list; 147, 148; 147, 149; 147, 150; 148, None; 149, None; 150, None; 151, if_statement; 151, 152; 151, 155; 152, comparison_operator:is; 152, 153; 152, 154; 153, identifier:params; 154, None; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 162; 158, pattern_list; 158, 159; 158, 160; 158, 161; 159, identifier:freqs; 160, identifier:sf; 161, identifier:kappa; 162, call; 162, 163; 162, 164; 163, identifier:_parse_pastml_parameters; 164, argument_list; 164, 165; 164, 166; 165, identifier:params; 166, identifier:states; 167, return_statement; 167, 168; 168, call; 168, 169; 168, 170; 169, identifier:ml_acr; 170, argument_list; 170, 171; 170, 174; 170, 177; 170, 180; 170, 183; 170, 186; 170, 189; 170, 192; 170, 195; 170, 198; 170, 201; 170, 204; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:tree; 173, identifier:tree; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:character; 176, identifier:character; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:prediction_method; 179, identifier:prediction_method; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:model; 182, identifier:model; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:states; 185, identifier:states; 186, keyword_argument; 186, 187; 186, 188; 187, identifier:avg_br_len; 188, identifier:avg_br_len; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:num_nodes; 191, identifier:num_nodes; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:num_tips; 194, identifier:num_tips; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:freqs; 197, identifier:freqs; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:sf; 200, identifier:sf; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:kappa; 203, identifier:kappa; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:force_joint; 206, identifier:force_joint; 207, if_statement; 207, 208; 207, 212; 208, call; 208, 209; 208, 210; 209, identifier:is_parsimonious; 210, argument_list; 210, 211; 211, identifier:prediction_method; 212, block; 212, 213; 213, return_statement; 213, 214; 214, call; 214, 215; 214, 216; 215, identifier:parsimonious_acr; 216, argument_list; 216, 217; 216, 218; 216, 219; 216, 220; 216, 221; 216, 222; 217, identifier:tree; 218, identifier:character; 219, identifier:prediction_method; 220, identifier:states; 221, identifier:num_nodes; 222, identifier:num_tips; 223, raise_statement; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:ValueError; 226, argument_list; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, string:'Method {} is unknown, should be one of ML ({}), one of MP ({}) or {}'; 230, identifier:format; 231, argument_list; 231, 232; 231, 233; 231, 239; 231, 245; 232, identifier:prediction_method; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, string:', '; 236, identifier:join; 237, argument_list; 237, 238; 238, identifier:ML_METHODS; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, string:', '; 242, identifier:join; 243, argument_list; 243, 244; 244, identifier:MP_METHODS; 245, identifier:COPY | def reconstruct_ancestral_states(tree, character, states, prediction_method=MPPA, model=F81,
params=None, avg_br_len=None, num_nodes=None, num_tips=None,
force_joint=True):
"""
Reconstructs ancestral states for the given character on the given tree.
:param character: character whose ancestral states are to be reconstructed.
:type character: str
:param tree: tree whose ancestral state are to be reconstructed,
annotated with the feature specified as `character` containing node states when known.
:type tree: ete3.Tree
:param states: possible character states.
:type states: numpy.array
:param avg_br_len: (optional) average non-zero branch length for this tree. If not specified, will be calculated.
:type avg_br_len: float
:param model: (optional, default is F81) state evolution model to be used by PASTML.
:type model: str
:param prediction_method: (optional, default is MPPA) ancestral state prediction method to be used by PASTML.
:type prediction_method: str
:param num_nodes: (optional) total number of nodes in the given tree (including tips).
If not specified, will be calculated.
:type num_nodes: int
:param num_tips: (optional) total number of tips in the given tree.
If not specified, will be calculated.
:type num_tips: int
:param params: an optional way to fix some parameters,
must be in a form {param: value},
where param can be a state (then the value should specify its frequency between 0 and 1),
or "scaling factor" (then the value should be the scaling factor for three branches,
e.g. set to 1 to keep the original branches). Could also be in a form path_to_param_file.
Only makes sense for ML methods.
:type params: dict or str
:return: ACR result dictionary whose values depend on the prediction method.
:rtype: dict
"""
logging.getLogger('pastml').debug('ACR settings for {}:\n\tMethod:\t{}{}.'
.format(character, prediction_method,
'\n\tModel:\t{}'.format(model)
if model and is_ml(prediction_method) else ''))
if COPY == prediction_method:
return {CHARACTER: character, STATES: states, METHOD: prediction_method}
if not num_nodes:
num_nodes = sum(1 for _ in tree.traverse())
if not num_tips:
num_tips = len(tree)
if is_ml(prediction_method):
if avg_br_len is None:
avg_br_len = np.mean(n.dist for n in tree.traverse() if n.dist)
freqs, sf, kappa = None, None, None
if params is not None:
freqs, sf, kappa = _parse_pastml_parameters(params, states)
return ml_acr(tree=tree, character=character, prediction_method=prediction_method, model=model, states=states,
avg_br_len=avg_br_len, num_nodes=num_nodes, num_tips=num_tips, freqs=freqs, sf=sf, kappa=kappa,
force_joint=force_joint)
if is_parsimonious(prediction_method):
return parsimonious_acr(tree, character, prediction_method, states, num_nodes, num_tips)
raise ValueError('Method {} is unknown, should be one of ML ({}), one of MP ({}) or {}'
.format(prediction_method, ', '.join(ML_METHODS), ', '.join(MP_METHODS), COPY)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:acr; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:tree; 5, identifier:df; 6, default_parameter; 6, 7; 6, 8; 7, identifier:prediction_method; 8, identifier:MPPA; 9, default_parameter; 9, 10; 9, 11; 10, identifier:model; 11, identifier:F81; 12, default_parameter; 12, 13; 12, 14; 13, identifier:column2parameters; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:force_joint; 17, True; 18, block; 18, 19; 18, 21; 18, 61; 18, 69; 18, 74; 18, 84; 18, 94; 18, 106; 18, 113; 18, 136; 18, 148; 18, 160; 18, 288; 18, 343; 18, 347; 18, 374; 19, expression_statement; 19, 20; 20, comment; 21, for_statement; 21, 22; 21, 23; 21, 26; 22, identifier:c; 23, attribute; 23, 24; 23, 25; 24, identifier:df; 25, identifier:columns; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, subscript; 29, 30; 29, 31; 30, identifier:df; 31, identifier:c; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:df; 36, identifier:c; 37, identifier:apply; 38, argument_list; 38, 39; 39, lambda; 39, 40; 39, 42; 40, lambda_parameters; 40, 41; 41, identifier:_; 42, conditional_expression:if; 42, 43; 42, 44; 42, 50; 43, string:''; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:pd; 47, identifier:isna; 48, argument_list; 48, 49; 49, identifier:_; 50, call; 50, 51; 50, 60; 51, attribute; 51, 52; 51, 59; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:_; 55, identifier:encode; 56, argument_list; 56, 57; 56, 58; 57, string:'ASCII'; 58, string:'replace'; 59, identifier:decode; 60, argument_list; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:columns; 64, call; 64, 65; 64, 66; 65, identifier:preannotate_tree; 66, argument_list; 66, 67; 66, 68; 67, identifier:df; 68, identifier:tree; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:name_tree; 72, argument_list; 72, 73; 73, identifier:tree; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:collapse_zero_branches; 77, argument_list; 77, 78; 77, 79; 78, identifier:tree; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:features_to_be_merged; 81, attribute; 81, 82; 81, 83; 82, identifier:df; 83, identifier:columns; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 90; 86, pattern_list; 86, 87; 86, 88; 86, 89; 87, identifier:avg_br_len; 88, identifier:num_nodes; 89, identifier:num_tips; 90, call; 90, 91; 90, 92; 91, identifier:get_tree_stats; 92, argument_list; 92, 93; 93, identifier:tree; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 104; 96, attribute; 96, 97; 96, 103; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:logging; 100, identifier:getLogger; 101, argument_list; 101, 102; 102, string:'pastml'; 103, identifier:debug; 104, argument_list; 104, 105; 105, string:'\n=============ACR==============================='; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:column2parameters; 109, conditional_expression:if; 109, 110; 109, 111; 109, 112; 110, identifier:column2parameters; 111, identifier:column2parameters; 112, dictionary; 113, function_definition; 113, 114; 113, 115; 113, 117; 114, function_name:_work; 115, parameters; 115, 116; 116, identifier:args; 117, block; 117, 118; 118, return_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:reconstruct_ancestral_states; 121, argument_list; 121, 122; 121, 124; 121, 127; 121, 130; 121, 133; 122, list_splat; 122, 123; 123, identifier:args; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:avg_br_len; 126, identifier:avg_br_len; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:num_nodes; 129, identifier:num_nodes; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:num_tips; 132, identifier:num_tips; 133, keyword_argument; 133, 134; 133, 135; 134, identifier:force_joint; 135, identifier:force_joint; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:prediction_methods; 139, call; 139, 140; 139, 141; 140, identifier:value2list; 141, argument_list; 141, 142; 141, 146; 141, 147; 142, call; 142, 143; 142, 144; 143, identifier:len; 144, argument_list; 144, 145; 145, identifier:columns; 146, identifier:prediction_method; 147, identifier:MPPA; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:models; 151, call; 151, 152; 151, 153; 152, identifier:value2list; 153, argument_list; 153, 154; 153, 158; 153, 159; 154, call; 154, 155; 154, 156; 155, identifier:len; 156, argument_list; 156, 157; 157, identifier:columns; 158, identifier:model; 159, identifier:F81; 160, function_definition; 160, 161; 160, 162; 160, 166; 161, function_name:get_states; 162, parameters; 162, 163; 162, 164; 162, 165; 163, identifier:method; 164, identifier:model; 165, identifier:column; 166, block; 166, 167; 166, 192; 166, 212; 166, 221; 166, 258; 166, 265; 166, 286; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:df_states; 170, list_comprehension; 170, 171; 170, 172; 170, 181; 171, identifier:_; 172, for_in_clause; 172, 173; 172, 174; 173, identifier:_; 174, call; 174, 175; 174, 180; 175, attribute; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:df; 178, identifier:column; 179, identifier:unique; 180, argument_list; 181, if_clause; 181, 182; 182, boolean_operator:and; 182, 183; 182, 189; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:pd; 186, identifier:notnull; 187, argument_list; 187, 188; 188, identifier:_; 189, comparison_operator:!=; 189, 190; 189, 191; 190, identifier:_; 191, string:''; 192, if_statement; 192, 193; 192, 204; 193, boolean_operator:or; 193, 194; 193, 199; 194, not_operator; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:is_ml; 197, argument_list; 197, 198; 198, identifier:method; 199, comparison_operator:not; 199, 200; 199, 201; 200, identifier:model; 201, set; 201, 202; 201, 203; 202, identifier:HKY; 203, identifier:JTT; 204, block; 204, 205; 205, return_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:np; 209, identifier:sort; 210, argument_list; 210, 211; 211, identifier:df_states; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:states; 215, conditional_expression:if; 215, 216; 215, 217; 215, 220; 216, identifier:HKY_STATES; 217, comparison_operator:==; 217, 218; 217, 219; 218, identifier:HKY; 219, identifier:model; 220, identifier:JTT_STATES; 221, if_statement; 221, 222; 221, 232; 222, not_operator; 222, 223; 223, binary_operator:&; 223, 224; 223, 228; 224, call; 224, 225; 224, 226; 225, identifier:set; 226, argument_list; 226, 227; 227, identifier:df_states; 228, call; 228, 229; 228, 230; 229, identifier:set; 230, argument_list; 230, 231; 231, identifier:states; 232, block; 232, 233; 233, raise_statement; 233, 234; 234, call; 234, 235; 234, 236; 235, identifier:ValueError; 236, argument_list; 236, 237; 237, call; 237, 238; 237, 243; 238, attribute; 238, 239; 238, 242; 239, concatenated_string; 239, 240; 239, 241; 240, string:'The allowed states for model {} are {}, '; 241, string:'but your annotation file specifies {} as states in column {}.'; 242, identifier:format; 243, argument_list; 243, 244; 243, 245; 243, 251; 243, 257; 244, identifier:model; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, string:', '; 248, identifier:join; 249, argument_list; 249, 250; 250, identifier:states; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, string:', '; 254, identifier:join; 255, argument_list; 255, 256; 256, identifier:df_states; 257, identifier:column; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:state_set; 261, call; 261, 262; 261, 263; 262, identifier:set; 263, argument_list; 263, 264; 264, identifier:states; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 270; 267, subscript; 267, 268; 267, 269; 268, identifier:df; 269, identifier:column; 270, call; 270, 271; 270, 276; 271, attribute; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:df; 274, identifier:column; 275, identifier:apply; 276, argument_list; 276, 277; 277, lambda; 277, 278; 277, 280; 278, lambda_parameters; 278, 279; 279, identifier:_; 280, conditional_expression:if; 280, 281; 280, 282; 280, 285; 281, identifier:_; 282, comparison_operator:in; 282, 283; 282, 284; 283, identifier:_; 284, identifier:state_set; 285, string:''; 286, return_statement; 286, 287; 287, identifier:states; 288, with_statement; 288, 289; 288, 297; 289, with_clause; 289, 290; 290, with_item; 290, 291; 291, as_pattern; 291, 292; 291, 295; 292, call; 292, 293; 292, 294; 293, identifier:ThreadPool; 294, argument_list; 295, as_pattern_target; 295, 296; 296, identifier:pool; 297, block; 297, 298; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 299, 302; 300, identifier:acr_results; 301, line_continuation:\; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:pool; 305, identifier:map; 306, argument_list; 306, 307; 306, 310; 307, keyword_argument; 307, 308; 307, 309; 308, identifier:func; 309, identifier:_work; 310, keyword_argument; 310, 311; 310, 312; 311, identifier:iterable; 312, generator_expression; 312, 313; 312, 332; 313, tuple; 313, 314; 313, 315; 313, 316; 313, 322; 313, 323; 313, 324; 314, identifier:tree; 315, identifier:column; 316, call; 316, 317; 316, 318; 317, identifier:get_states; 318, argument_list; 318, 319; 318, 320; 318, 321; 319, identifier:method; 320, identifier:model; 321, identifier:column; 322, identifier:method; 323, identifier:model; 324, conditional_expression:if; 324, 325; 324, 328; 324, 331; 325, subscript; 325, 326; 325, 327; 326, identifier:column2parameters; 327, identifier:column; 328, comparison_operator:in; 328, 329; 328, 330; 329, identifier:column; 330, identifier:column2parameters; 331, None; 332, for_in_clause; 332, 333; 332, 337; 333, tuple_pattern; 333, 334; 333, 335; 333, 336; 334, identifier:column; 335, identifier:method; 336, identifier:model; 337, call; 337, 338; 337, 339; 338, identifier:zip; 339, argument_list; 339, 340; 339, 341; 339, 342; 340, identifier:columns; 341, identifier:prediction_methods; 342, identifier:models; 343, expression_statement; 343, 344; 344, assignment; 344, 345; 344, 346; 345, identifier:result; 346, list:[]; 347, for_statement; 347, 348; 347, 349; 347, 350; 348, identifier:acr_res; 349, identifier:acr_results; 350, block; 350, 351; 351, if_statement; 351, 352; 351, 357; 351, 365; 352, call; 352, 353; 352, 354; 353, identifier:isinstance; 354, argument_list; 354, 355; 354, 356; 355, identifier:acr_res; 356, identifier:list; 357, block; 357, 358; 358, expression_statement; 358, 359; 359, call; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:result; 362, identifier:extend; 363, argument_list; 363, 364; 364, identifier:acr_res; 365, else_clause; 365, 366; 366, block; 366, 367; 367, expression_statement; 367, 368; 368, call; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:result; 371, identifier:append; 372, argument_list; 372, 373; 373, identifier:acr_res; 374, return_statement; 374, 375; 375, identifier:result | def acr(tree, df, prediction_method=MPPA, model=F81, column2parameters=None, force_joint=True):
"""
Reconstructs ancestral states for the given tree and
all the characters specified as columns of the given annotation dataframe.
:param df: dataframe indexed with tree node names
and containing characters for which ACR should be performed as columns.
:type df: pandas.DataFrame
:param tree: tree whose ancestral state are to be reconstructed.
:type tree: ete3.Tree
:param model: (optional, default is F81) model(s) to be used by PASTML,
can be either one model to be used for all the characters,
or a list of different models (in the same order as the annotation dataframe columns)
:type model: str or list(str)
:param prediction_method: (optional, default is MPPA) ancestral state prediction method(s) to be used by PASTML,
can be either one method to be used for all the characters,
or a list of different methods (in the same order as the annotation dataframe columns)
:type prediction_method: str or list(str)
:param column2parameters: an optional way to fix some parameters,
must be in a form {column: {param: value}},
where param can be a character state (then the value should specify its frequency between 0 and 1),
or pastml.ml.SCALING_FACTOR (then the value should be the scaling factor for three branches,
e.g. set to 1 to keep the original branches). Could also be in a form {column: path_to_param_file}.
:type column2parameters: dict
:param force_joint: (optional, default is True) whether the JOINT state should be added to the MPPA prediction
even when not selected by the Brier score
:type force_joint: bool
:return: list of ACR result dictionaries, one per character.
:rtype: list(dict)
"""
for c in df.columns:
df[c] = df[c].apply(lambda _: '' if pd.isna(_) else _.encode('ASCII', 'replace').decode())
columns = preannotate_tree(df, tree)
name_tree(tree)
collapse_zero_branches(tree, features_to_be_merged=df.columns)
avg_br_len, num_nodes, num_tips = get_tree_stats(tree)
logging.getLogger('pastml').debug('\n=============ACR===============================')
column2parameters = column2parameters if column2parameters else {}
def _work(args):
return reconstruct_ancestral_states(*args, avg_br_len=avg_br_len, num_nodes=num_nodes, num_tips=num_tips,
force_joint=force_joint)
prediction_methods = value2list(len(columns), prediction_method, MPPA)
models = value2list(len(columns), model, F81)
def get_states(method, model, column):
df_states = [_ for _ in df[column].unique() if pd.notnull(_) and _ != '']
if not is_ml(method) or model not in {HKY, JTT}:
return np.sort(df_states)
states = HKY_STATES if HKY == model else JTT_STATES
if not set(df_states) & set(states):
raise ValueError('The allowed states for model {} are {}, '
'but your annotation file specifies {} as states in column {}.'
.format(model, ', '.join(states), ', '.join(df_states), column))
state_set = set(states)
df[column] = df[column].apply(lambda _: _ if _ in state_set else '')
return states
with ThreadPool() as pool:
acr_results = \
pool.map(func=_work, iterable=((tree, column, get_states(method, model, column), method, model,
column2parameters[column] if column in column2parameters else None)
for (column, method, model) in zip(columns, prediction_methods, models)))
result = []
for acr_res in acr_results:
if isinstance(acr_res, list):
result.extend(acr_res)
else:
result.append(acr_res)
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:export_bert; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:data; 5, identifier:electrodes; 6, identifier:filename; 7, block; 7, 8; 7, 10; 7, 11; 7, 49; 7, 57; 7, 69; 7, 76; 7, 77; 7, 85; 7, 93; 7, 107; 7, 121; 7, 122; 7, 123; 7, 136; 7, 154; 7, 172; 7, 189; 7, 196; 7, 227; 7, 239; 7, 246; 7, 247; 7, 257; 7, 268; 7, 277; 7, 283; 7, 300; 7, 307; 7, 359; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, if_statement; 11, 12; 11, 16; 12, call; 12, 13; 12, 14; 13, identifier:has_multiple_timesteps; 14, argument_list; 14, 15; 15, identifier:data; 16, block; 16, 17; 16, 44; 16, 45; 16, 46; 16, 47; 16, 48; 17, for_statement; 17, 18; 17, 21; 17, 28; 18, pattern_list; 18, 19; 18, 20; 19, identifier:i; 20, identifier:timestep; 21, call; 21, 22; 21, 23; 22, identifier:enumerate; 23, argument_list; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:split_timesteps; 26, argument_list; 26, 27; 27, identifier:data; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:export_bert; 32, argument_list; 32, 33; 32, 34; 32, 35; 33, identifier:timestep; 34, identifier:electrodes; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:filename; 38, identifier:replace; 39, argument_list; 39, 40; 39, 41; 40, string:"."; 41, binary_operator:%; 41, 42; 41, 43; 42, string:"_%.3d."; 43, identifier:i; 44, comment; 45, comment; 46, comment; 47, comment; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:f; 52, call; 52, 53; 52, 54; 53, identifier:open; 54, argument_list; 54, 55; 54, 56; 55, identifier:filename; 56, string:'w'; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:f; 61, identifier:write; 62, argument_list; 62, 63; 63, binary_operator:%; 63, 64; 63, 65; 64, string:"%d\n"; 65, call; 65, 66; 65, 67; 66, identifier:len; 67, argument_list; 67, 68; 68, identifier:electrodes; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:f; 73, identifier:write; 74, argument_list; 74, 75; 75, string:"# "; 76, comment; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:electrodes; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:electrodes; 83, identifier:copy; 84, argument_list; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:data; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:data; 91, identifier:copy; 92, argument_list; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:electrodes; 97, identifier:columns; 98, call; 98, 99; 98, 106; 99, attribute; 99, 100; 99, 105; 100, attribute; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:electrodes; 103, identifier:columns; 104, identifier:str; 105, identifier:lower; 106, argument_list; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:data; 111, identifier:columns; 112, call; 112, 113; 112, 120; 113, attribute; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:data; 117, identifier:columns; 118, identifier:str; 119, identifier:lower; 120, argument_list; 121, comment; 122, comment; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:cols_to_export; 126, list:["a", "b", "m", "n", "u", "i", "r", "rho_a", "error"]; 126, 127; 126, 128; 126, 129; 126, 130; 126, 131; 126, 132; 126, 133; 126, 134; 126, 135; 127, string:"a"; 128, string:"b"; 129, string:"m"; 130, string:"n"; 131, string:"u"; 132, string:"i"; 133, string:"r"; 134, string:"rho_a"; 135, string:"error"; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:data; 140, identifier:drop; 141, argument_list; 141, 142; 141, 150; 141, 151; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:data; 146, identifier:columns; 147, identifier:difference; 148, argument_list; 148, 149; 149, identifier:cols_to_export; 150, integer:1; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:inplace; 153, True; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:data; 158, identifier:rename; 159, argument_list; 159, 160; 159, 169; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:columns; 162, dictionary; 162, 163; 162, 166; 163, pair; 163, 164; 163, 165; 164, string:"rho_a"; 165, string:"rhoa"; 166, pair; 166, 167; 166, 168; 167, string:"error"; 168, string:"err"; 169, keyword_argument; 169, 170; 169, 171; 170, identifier:inplace; 171, True; 172, for_statement; 172, 173; 172, 174; 172, 179; 173, identifier:key; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:electrodes; 177, identifier:keys; 178, argument_list; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:f; 184, identifier:write; 185, argument_list; 185, 186; 186, binary_operator:%; 186, 187; 186, 188; 187, string:"%s "; 188, identifier:key; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:f; 193, identifier:write; 194, argument_list; 194, 195; 195, string:"\n"; 196, for_statement; 196, 197; 196, 198; 196, 206; 197, identifier:row; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:electrodes; 201, identifier:itertuples; 202, argument_list; 202, 203; 203, keyword_argument; 203, 204; 203, 205; 204, identifier:index; 205, False; 206, block; 206, 207; 206, 220; 207, for_statement; 207, 208; 207, 209; 207, 210; 208, identifier:val; 209, identifier:row; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:f; 215, identifier:write; 216, argument_list; 216, 217; 217, binary_operator:%; 217, 218; 217, 219; 218, string:"%5.3f "; 219, identifier:val; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:f; 224, identifier:write; 225, argument_list; 225, 226; 226, string:"\n"; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:f; 231, identifier:write; 232, argument_list; 232, 233; 233, binary_operator:%; 233, 234; 233, 235; 234, string:"%d\n"; 235, call; 235, 236; 235, 237; 236, identifier:len; 237, argument_list; 237, 238; 238, identifier:data; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:f; 243, identifier:write; 244, argument_list; 244, 245; 245, string:"# "; 246, comment; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:columns; 250, call; 250, 251; 250, 256; 251, attribute; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:data; 254, identifier:columns; 255, identifier:tolist; 256, argument_list; 257, for_statement; 257, 258; 257, 259; 257, 260; 258, identifier:c; 259, string:"abmn"; 260, block; 260, 261; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:columns; 265, identifier:remove; 266, argument_list; 266, 267; 267, identifier:c; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:columns; 271, binary_operator:+; 271, 272; 271, 276; 272, call; 272, 273; 272, 274; 273, identifier:list; 274, argument_list; 274, 275; 275, string:"abmn"; 276, identifier:columns; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:data; 280, subscript; 280, 281; 280, 282; 281, identifier:data; 282, identifier:columns; 283, for_statement; 283, 284; 283, 285; 283, 290; 284, identifier:key; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:data; 288, identifier:keys; 289, argument_list; 290, block; 290, 291; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:f; 295, identifier:write; 296, argument_list; 296, 297; 297, binary_operator:%; 297, 298; 297, 299; 298, string:"%s "; 299, identifier:key; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:f; 304, identifier:write; 305, argument_list; 305, 306; 306, string:"\n"; 307, for_statement; 307, 308; 307, 309; 307, 317; 308, identifier:row; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:data; 312, identifier:itertuples; 313, argument_list; 313, 314; 314, keyword_argument; 314, 315; 314, 316; 315, identifier:index; 316, False; 317, block; 317, 318; 317, 352; 318, for_statement; 318, 319; 318, 322; 318, 326; 319, pattern_list; 319, 320; 319, 321; 320, identifier:i; 321, identifier:val; 322, call; 322, 323; 322, 324; 323, identifier:enumerate; 324, argument_list; 324, 325; 325, identifier:row; 326, block; 326, 327; 327, if_statement; 327, 328; 327, 331; 327, 341; 328, comparison_operator:<; 328, 329; 328, 330; 329, identifier:i; 330, integer:4; 331, block; 331, 332; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:f; 336, identifier:write; 337, argument_list; 337, 338; 338, binary_operator:%; 338, 339; 338, 340; 339, string:"%d "; 340, identifier:val; 341, else_clause; 341, 342; 342, block; 342, 343; 343, expression_statement; 343, 344; 344, call; 344, 345; 344, 348; 345, attribute; 345, 346; 345, 347; 346, identifier:f; 347, identifier:write; 348, argument_list; 348, 349; 349, binary_operator:%; 349, 350; 349, 351; 350, string:"%E "; 351, identifier:val; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:f; 356, identifier:write; 357, argument_list; 357, 358; 358, string:"\n"; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:f; 363, identifier:close; 364, argument_list | def export_bert(data, electrodes, filename):
"""Export to unified data format used in pyGIMLi & BERT.
Parameters
----------
data : :py:class:`pandas.DataFrame`
DataFrame with at least a, b, m, n and r.
electrodes : :py:class:`pandas.DataFrame`
DataFrame with electrode positions.
filename : str
String of the output filename.
"""
# Check for multiple timesteps
if has_multiple_timesteps(data):
for i, timestep in enumerate(split_timesteps(data)):
export_bert(timestep, electrodes,
filename.replace(".", "_%.3d." % i))
# TODO: Make ABMN consistent
# index_full = ert.data.groupby(list("abmn")).groups.keys()
# g = ert.data.groupby('timestep')
# q = ert.data.pivot_table(values='r', index=list("abmn"), columns="timestep", dropna=True)
# ert.data.reset_index(list("abmn"))
f = open(filename, 'w')
f.write("%d\n" % len(electrodes))
f.write("# ")
# Make temporary copies for renaming
electrodes = electrodes.copy()
data = data.copy()
electrodes.columns = electrodes.columns.str.lower()
data.columns = data.columns.str.lower()
# Remove unnecessary columns and rename according to bert conventions
# https://gitlab.com/resistivity-net/bert#the-unified-data-format
cols_to_export = ["a", "b", "m", "n", "u", "i", "r", "rho_a", "error"]
data.drop(data.columns.difference(cols_to_export), 1, inplace=True)
data.rename(columns={"rho_a": "rhoa", "error": "err"}, inplace=True)
for key in electrodes.keys():
f.write("%s " % key)
f.write("\n")
for row in electrodes.itertuples(index=False):
for val in row:
f.write("%5.3f " % val)
f.write("\n")
f.write("%d\n" % len(data))
f.write("# ")
# Make sure that a, b, m, n are the first 4 columns
columns = data.columns.tolist()
for c in "abmn":
columns.remove(c)
columns = list("abmn") + columns
data = data[columns]
for key in data.keys():
f.write("%s " % key)
f.write("\n")
for row in data.itertuples(index=False):
for i, val in enumerate(row):
if i < 4:
f.write("%d " % val)
else:
f.write("%E " % val)
f.write("\n")
f.close() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_sort_dd_skips; 3, parameters; 3, 4; 3, 5; 4, identifier:configs; 5, identifier:dd_indices_all; 6, block; 6, 7; 6, 9; 6, 28; 6, 46; 6, 47; 6, 56; 6, 73; 6, 74; 6, 78; 6, 105; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:config_current_skips; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:np; 15, identifier:abs; 16, argument_list; 16, 17; 17, binary_operator:-; 17, 18; 17, 23; 18, subscript; 18, 19; 18, 20; 18, 22; 19, identifier:configs; 20, slice; 20, 21; 21, colon; 22, integer:1; 23, subscript; 23, 24; 23, 25; 23, 27; 24, identifier:configs; 25, slice; 25, 26; 26, colon; 27, integer:0; 28, if_statement; 28, 29; 28, 40; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:np; 32, identifier:all; 33, argument_list; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:np; 37, identifier:isnan; 38, argument_list; 38, 39; 39, identifier:config_current_skips; 40, block; 40, 41; 41, return_statement; 41, 42; 42, dictionary; 42, 43; 43, pair; 43, 44; 43, 45; 44, integer:0; 45, list:[]; 46, comment; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:available_skips_raw; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:np; 53, identifier:unique; 54, argument_list; 54, 55; 55, identifier:config_current_skips; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:available_skips; 59, call; 59, 60; 59, 71; 60, attribute; 60, 61; 60, 70; 61, subscript; 61, 62; 61, 63; 62, identifier:available_skips_raw; 63, unary_operator:~; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:np; 67, identifier:isnan; 68, argument_list; 68, 69; 69, identifier:available_skips_raw; 70, identifier:astype; 71, argument_list; 71, 72; 72, identifier:int; 73, comment; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:dd_configs_sorted; 77, dictionary; 78, for_statement; 78, 79; 78, 80; 78, 81; 79, identifier:skip; 80, identifier:available_skips; 81, block; 81, 82; 81, 95; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:indices; 85, subscript; 85, 86; 85, 94; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:np; 89, identifier:where; 90, argument_list; 90, 91; 91, comparison_operator:==; 91, 92; 91, 93; 92, identifier:config_current_skips; 93, identifier:skip; 94, integer:0; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 102; 97, subscript; 97, 98; 97, 99; 98, identifier:dd_configs_sorted; 99, binary_operator:-; 99, 100; 99, 101; 100, identifier:skip; 101, integer:1; 102, subscript; 102, 103; 102, 104; 103, identifier:dd_indices_all; 104, identifier:indices; 105, return_statement; 105, 106; 106, identifier:dd_configs_sorted | def _sort_dd_skips(configs, dd_indices_all):
"""Given a set of dipole-dipole configurations, sort them according to
their current skip.
Parameters
----------
configs: Nx4 numpy.ndarray
Dipole-Dipole configurations
Returns
-------
dd_configs_sorted: dict
dictionary with the skip as keys, and arrays/lists with indices to
these skips.
"""
config_current_skips = np.abs(configs[:, 1] - configs[:, 0])
if np.all(np.isnan(config_current_skips)):
return {0: []}
# determine skips
available_skips_raw = np.unique(config_current_skips)
available_skips = available_skips_raw[
~np.isnan(available_skips_raw)
].astype(int)
# now determine the configurations
dd_configs_sorted = {}
for skip in available_skips:
indices = np.where(config_current_skips == skip)[0]
dd_configs_sorted[skip - 1] = dd_indices_all[indices]
return dd_configs_sorted |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:get_label; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:parameter; 5, identifier:ptype; 6, default_parameter; 6, 7; 6, 8; 7, identifier:flavor; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:mpl; 11, None; 12, block; 12, 13; 12, 15; 12, 16; 12, 71; 12, 72; 12, 82; 12, 94; 12, 108; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, if_statement; 16, 17; 16, 20; 16, 38; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:flavor; 19, None; 20, block; 20, 21; 21, if_statement; 21, 22; 21, 27; 22, comparison_operator:not; 22, 23; 22, 24; 23, identifier:flavor; 24, tuple; 24, 25; 24, 26; 25, string:'latex'; 26, string:'mathml'; 27, block; 27, 28; 28, raise_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:Exception; 31, argument_list; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, string:'flavor not recognized: {}'; 35, identifier:format; 36, argument_list; 36, 37; 37, identifier:flavor; 38, else_clause; 38, 39; 39, block; 39, 40; 39, 50; 39, 58; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:mpl; 43, None; 44, block; 44, 45; 45, raise_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:Exception; 48, argument_list; 48, 49; 49, string:'either the flavor or mpl must be provided'; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:rendering; 53, subscript; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:mpl; 56, identifier:rcParams; 57, string:'text.usetex'; 58, if_statement; 58, 59; 58, 60; 58, 65; 59, identifier:rendering; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:flavor; 64, string:'latex'; 65, else_clause; 65, 66; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:flavor; 70, string:'mathml'; 71, comment; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:not; 73, 74; 73, 75; 74, identifier:parameter; 75, identifier:labels; 76, block; 76, 77; 77, raise_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:Exception; 80, argument_list; 80, 81; 81, string:'parameter not known'; 82, if_statement; 82, 83; 82, 88; 83, comparison_operator:not; 83, 84; 83, 85; 84, identifier:ptype; 85, subscript; 85, 86; 85, 87; 86, identifier:labels; 87, identifier:parameter; 88, block; 88, 89; 89, raise_statement; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:Exception; 92, argument_list; 92, 93; 93, string:'ptype not known'; 94, if_statement; 94, 95; 94, 102; 95, comparison_operator:not; 95, 96; 95, 97; 96, identifier:flavor; 97, subscript; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:labels; 100, identifier:parameter; 101, identifier:ptype; 102, block; 102, 103; 103, raise_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:Exception; 106, argument_list; 106, 107; 107, string:'flavor not known'; 108, return_statement; 108, 109; 109, subscript; 109, 110; 109, 115; 110, subscript; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:labels; 113, identifier:parameter; 114, identifier:ptype; 115, identifier:flavor | def get_label(parameter, ptype, flavor=None, mpl=None):
"""Return the label of a given SIP parameter
Parameters
----------
parameter: string
type of parameter, e.g. rmag|rpha|cre|cim
ptype: string
material|meas. Either return the material property (e.g. resistivity)
or the measurement parameter (e.g., impedance)
flavor: string, optional
if set, must be one of latex|mathml. Return a label for latex
processing, or for mathml processing
mpl: matplotlib, optional
if set, infer flavor from mpl.rcParams. Will not be used if flavor is
set
Returns
-------
label: string
the requested label
"""
# determine flavor
if flavor is not None:
if flavor not in ('latex', 'mathml'):
raise Exception('flavor not recognized: {}'.format(flavor))
else:
if mpl is None:
raise Exception('either the flavor or mpl must be provided')
rendering = mpl.rcParams['text.usetex']
if rendering:
flavor = 'latex'
else:
flavor = 'mathml'
# check if the requested label is present
if parameter not in labels:
raise Exception('parameter not known')
if ptype not in labels[parameter]:
raise Exception('ptype not known')
if flavor not in labels[parameter][ptype]:
raise Exception('flavor not known')
return labels[parameter][ptype][flavor] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:convert; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:input_format; 5, identifier:output_format; 6, identifier:data; 7, default_parameter; 7, 8; 7, 9; 8, identifier:one_spectrum; 9, False; 10, block; 10, 11; 10, 13; 10, 20; 10, 35; 10, 50; 10, 51; 10, 52; 10, 105; 10, 116; 10, 126; 10, 150; 10, 151; 10, 170; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:==; 14, 15; 14, 16; 15, identifier:input_format; 16, identifier:output_format; 17, block; 17, 18; 18, return_statement; 18, 19; 19, identifier:data; 20, if_statement; 20, 21; 20, 24; 21, comparison_operator:not; 21, 22; 21, 23; 22, identifier:input_format; 23, identifier:from_converters; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:KeyError; 28, argument_list; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, string:'Input format {0} not known!'; 32, identifier:format; 33, argument_list; 33, 34; 34, identifier:input_format; 35, if_statement; 35, 36; 35, 39; 36, comparison_operator:not; 36, 37; 36, 38; 37, identifier:output_format; 38, identifier:to_converters; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:KeyError; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, string:'Output format {0} not known!'; 47, identifier:format; 48, argument_list; 48, 49; 49, identifier:output_format; 50, comment; 51, comment; 52, if_statement; 52, 53; 52, 71; 52, 95; 53, boolean_operator:and; 53, 54; 53, 70; 54, boolean_operator:and; 54, 55; 54, 63; 55, comparison_operator:==; 55, 56; 55, 62; 56, call; 56, 57; 56, 58; 57, identifier:len; 58, argument_list; 58, 59; 59, attribute; 59, 60; 59, 61; 60, identifier:data; 61, identifier:shape; 62, integer:2; 63, comparison_operator:==; 63, 64; 63, 69; 64, subscript; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:data; 67, identifier:shape; 68, integer:0; 69, integer:2; 70, identifier:one_spectrum; 71, block; 71, 72; 71, 91; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:work_data; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:np; 78, identifier:hstack; 79, argument_list; 79, 80; 80, tuple; 80, 81; 80, 86; 81, subscript; 81, 82; 81, 83; 81, 84; 82, identifier:data; 83, integer:0; 84, slice; 84, 85; 85, colon; 86, subscript; 86, 87; 86, 88; 86, 89; 87, identifier:data; 88, integer:1; 89, slice; 89, 90; 90, colon; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:one_spec_2d; 94, True; 95, else_clause; 95, 96; 96, block; 96, 97; 96, 101; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:work_data; 100, identifier:data; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:one_spec_2d; 104, False; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 110; 107, pattern_list; 107, 108; 107, 109; 108, identifier:cre; 109, identifier:cim; 110, call; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:from_converters; 113, identifier:input_format; 114, argument_list; 114, 115; 115, identifier:work_data; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:converted_data; 119, call; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:to_converters; 122, identifier:output_format; 123, argument_list; 123, 124; 123, 125; 124, identifier:cre; 125, identifier:cim; 126, if_statement; 126, 127; 126, 128; 127, identifier:one_spec_2d; 128, block; 128, 129; 128, 139; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, pattern_list; 131, 132; 131, 133; 132, identifier:part1; 133, identifier:part2; 134, call; 134, 135; 134, 136; 135, identifier:split_data; 136, argument_list; 136, 137; 136, 138; 137, identifier:converted_data; 138, True; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:converted_data; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:np; 145, identifier:vstack; 146, argument_list; 146, 147; 147, tuple; 147, 148; 147, 149; 148, identifier:part1; 149, identifier:part2; 150, comment; 151, if_statement; 151, 152; 151, 160; 152, comparison_operator:==; 152, 153; 152, 159; 153, call; 153, 154; 153, 155; 154, identifier:len; 155, argument_list; 155, 156; 156, attribute; 156, 157; 156, 158; 157, identifier:data; 158, identifier:shape; 159, integer:1; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:converted_data; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:np; 167, identifier:squeeze; 168, argument_list; 168, 169; 169, identifier:converted_data; 170, return_statement; 170, 171; 171, identifier:converted_data | def convert(input_format, output_format, data, one_spectrum=False):
"""
Convert from the given format to the requested format
Parameters
----------
input_format : format of input data (parameter 'data')
output_format : format of output data
data : numpy array containing data in specified input format
one_spectrum : True|False, the input data comprises one spectrum. This
allows for an additional format of the data array.
Possible input/output formats:
------------------------------
'lnrmag_rpha'
'log10rmag_rpha'
'rmag_rpha'
'rre_rim'
'rre_rmim'
'cmag_cpha'
'cre_cim'
'cre_cmim'
'ccomplex'
'rcomplex'
Array format
------------
data is either 1D or 2D. A 1D array correspond to one spectrum, with double
the size of the frequencies (which are not needed for the conversion).
Thus, the first halt either comprises a magnitude data, and the second one
phase data, or the parts comprise real and imaginary parts.
For the 2D case there exist two possibilities:
First, if one_spectrum is False, then the first axis denotes the spectrum
number, and each spectrum is stored on the second axis as described for the
1D case.
Second, if one_spectrum is True, and the first axis has the size two, then
the axis denotes either magnitude (index 0) and phase (index 1), or real
(index 0) and imaginary (index 1) parts. The second axis has the same size
as there are frequencies.
Internally we always convert to real part and imaginary part of
conductivity, and then convert back to the output format.
Return values are of the same dimensions as input variables.
"""
if input_format == output_format:
return data
if input_format not in from_converters:
raise KeyError('Input format {0} not known!'.format(input_format))
if output_format not in to_converters:
raise KeyError('Output format {0} not known!'.format(output_format))
# internally we always work with the second axis of double the frequency
# size
if len(data.shape) == 2 and data.shape[0] == 2 and one_spectrum:
work_data = np.hstack((data[0, :], data[1, :]))
one_spec_2d = True
else:
work_data = data
one_spec_2d = False
cre, cim = from_converters[input_format](work_data)
converted_data = to_converters[output_format](cre, cim)
if one_spec_2d:
part1, part2 = split_data(converted_data, True)
converted_data = np.vstack((part1, part2))
# reshape to input size (this should only be necessary for 1D data)
if len(data.shape) == 1:
converted_data = np.squeeze(converted_data)
return converted_data |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:plot_ps_extra; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:dataobj; 5, identifier:key; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 32; 8, 61; 8, 107; 8, 116; 8, 129; 8, 138; 8, 152; 8, 153; 8, 159; 8, 167; 8, 190; 8, 199; 8, 203; 8, 290; 8, 325; 8, 331; 8, 337; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 19; 11, 24; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 16; 15, identifier:dataobj; 16, attribute; 16, 17; 16, 18; 17, identifier:pd; 18, identifier:DataFrame; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:df_raw; 23, identifier:dataobj; 24, else_clause; 24, 25; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:df_raw; 29, attribute; 29, 30; 29, 31; 30, identifier:dataobj; 31, identifier:data; 32, if_statement; 32, 33; 32, 40; 32, 55; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:kwargs; 36, identifier:get; 37, argument_list; 37, 38; 37, 39; 38, string:'subquery'; 39, False; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:df; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:df_raw; 47, identifier:query; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:kwargs; 52, identifier:get; 53, argument_list; 53, 54; 54, string:'subquery'; 55, else_clause; 55, 56; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:df; 60, identifier:df_raw; 61, function_definition; 61, 62; 61, 63; 61, 66; 62, function_name:fancyfy; 63, parameters; 63, 64; 63, 65; 64, identifier:axes; 65, identifier:N; 66, block; 66, 67; 66, 88; 67, for_statement; 67, 68; 67, 69; 67, 80; 68, identifier:ax; 69, attribute; 69, 70; 69, 79; 70, subscript; 70, 71; 70, 72; 70, 77; 71, identifier:axes; 72, slice; 72, 73; 72, 74; 72, 75; 73, integer:0; 74, colon; 75, unary_operator:-; 75, 76; 76, integer:1; 77, slice; 77, 78; 78, colon; 79, identifier:flat; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:ax; 85, identifier:set_xlabel; 86, argument_list; 86, 87; 87, string:''; 88, for_statement; 88, 89; 88, 90; 88, 99; 89, identifier:ax; 90, attribute; 90, 91; 90, 98; 91, subscript; 91, 92; 91, 93; 91, 95; 92, identifier:axes; 93, slice; 93, 94; 94, colon; 95, slice; 95, 96; 95, 97; 96, integer:1; 97, colon; 98, identifier:flat; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:ax; 104, identifier:set_ylabel; 105, argument_list; 105, 106; 106, string:''; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:g; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:df; 113, identifier:groupby; 114, argument_list; 114, 115; 115, string:'timestep'; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:N; 119, call; 119, 120; 119, 121; 120, identifier:len; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:g; 126, identifier:groups; 127, identifier:keys; 128, argument_list; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:nrx; 132, call; 132, 133; 132, 134; 133, identifier:min; 134, argument_list; 134, 135; 135, tuple; 135, 136; 135, 137; 136, identifier:N; 137, integer:5; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:nry; 141, call; 141, 142; 141, 143; 142, identifier:int; 143, argument_list; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:np; 147, identifier:ceil; 148, argument_list; 148, 149; 149, binary_operator:/; 149, 150; 149, 151; 150, identifier:N; 151, identifier:nrx; 152, comment; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:sizex; 156, binary_operator:*; 156, 157; 156, 158; 157, identifier:nrx; 158, integer:3; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:sizey; 162, binary_operator:-; 162, 163; 162, 166; 163, binary_operator:*; 163, 164; 163, 165; 164, identifier:nry; 165, integer:4; 166, integer:1; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, pattern_list; 169, 170; 169, 171; 170, identifier:fig; 171, identifier:axes; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:plt; 175, identifier:subplots; 176, argument_list; 176, 177; 176, 178; 176, 179; 176, 182; 176, 185; 177, identifier:nry; 178, identifier:nrx; 179, keyword_argument; 179, 180; 179, 181; 180, identifier:sharex; 181, True; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:sharey; 184, True; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:figsize; 187, tuple; 187, 188; 187, 189; 188, identifier:sizex; 189, identifier:sizey; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:axes; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:np; 196, identifier:atleast_2d; 197, argument_list; 197, 198; 198, identifier:axes; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:cbs; 202, list:[]; 203, for_statement; 203, 204; 203, 209; 203, 216; 204, pattern_list; 204, 205; 204, 206; 205, identifier:ax; 206, tuple_pattern; 206, 207; 206, 208; 207, identifier:name; 208, identifier:group; 209, call; 209, 210; 209, 211; 210, identifier:zip; 211, argument_list; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:axes; 214, identifier:flat; 215, identifier:g; 216, block; 216, 217; 216, 252; 216, 259; 216, 274; 216, 283; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 223; 219, pattern_list; 219, 220; 219, 221; 219, 222; 220, identifier:fig1; 221, identifier:axes1; 222, identifier:cb1; 223, call; 223, 224; 223, 225; 224, identifier:plot_pseudosection_type2; 225, argument_list; 225, 226; 225, 227; 225, 228; 225, 231; 225, 234; 225, 243; 226, identifier:group; 227, identifier:key; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:ax; 230, identifier:ax; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:log10; 233, False; 234, keyword_argument; 234, 235; 234, 236; 235, identifier:cbmin; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:kwargs; 239, identifier:get; 240, argument_list; 240, 241; 240, 242; 241, string:'cbmin'; 242, None; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:cbmax; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:kwargs; 248, identifier:get; 249, argument_list; 249, 250; 249, 251; 250, string:'cbmax'; 251, None; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:cbs; 256, identifier:append; 257, argument_list; 257, 258; 258, identifier:cb1; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:ax; 263, identifier:set_title; 264, argument_list; 264, 265; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, string:'timestep: {0}'; 268, identifier:format; 269, argument_list; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:int; 272, argument_list; 272, 273; 273, identifier:name; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 281; 276, attribute; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:ax; 279, identifier:xaxis; 280, identifier:set_ticks_position; 281, argument_list; 281, 282; 282, string:'bottom'; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:ax; 287, identifier:set_aspect; 288, argument_list; 288, 289; 289, string:'equal'; 290, for_statement; 290, 291; 290, 292; 290, 315; 291, identifier:cb; 292, attribute; 292, 293; 292, 314; 293, subscript; 293, 294; 293, 307; 293, 309; 294, call; 294, 295; 294, 303; 295, attribute; 295, 296; 295, 302; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:np; 299, identifier:array; 300, argument_list; 300, 301; 301, identifier:cbs; 302, identifier:reshape; 303, argument_list; 303, 304; 304, attribute; 304, 305; 304, 306; 305, identifier:axes; 306, identifier:shape; 307, slice; 307, 308; 308, colon; 309, slice; 309, 310; 309, 311; 309, 312; 310, integer:0; 311, colon; 312, unary_operator:-; 312, 313; 313, integer:1; 314, identifier:flat; 315, block; 315, 316; 316, expression_statement; 316, 317; 317, call; 317, 318; 317, 323; 318, attribute; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:cb; 321, identifier:ax; 322, identifier:set_visible; 323, argument_list; 323, 324; 324, False; 325, expression_statement; 325, 326; 326, call; 326, 327; 326, 328; 327, identifier:fancyfy; 328, argument_list; 328, 329; 328, 330; 329, identifier:axes; 330, identifier:N; 331, expression_statement; 331, 332; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:fig; 335, identifier:tight_layout; 336, argument_list; 337, return_statement; 337, 338; 338, identifier:fig | def plot_ps_extra(dataobj, key, **kwargs):
"""Create grouped pseudoplots for one or more time steps
Parameters
----------
dataobj: :class:`reda.containers.ERT`
An ERT container with loaded data
key: string
The column name to plot
subquery: string, optional
cbmin: float, optional
cbmax: float, optional
Examples
--------
>>> import reda.testing.containers
>>> ert = reda.testing.containers.ERTContainer_nr
>>> import reda.plotters.pseudoplots as PS
>>> fig = PS.plot_ps_extra(ert, key='r')
"""
if isinstance(dataobj, pd.DataFrame):
df_raw = dataobj
else:
df_raw = dataobj.data
if kwargs.get('subquery', False):
df = df_raw.query(kwargs.get('subquery'))
else:
df = df_raw
def fancyfy(axes, N):
for ax in axes[0:-1, :].flat:
ax.set_xlabel('')
for ax in axes[:, 1:].flat:
ax.set_ylabel('')
g = df.groupby('timestep')
N = len(g.groups.keys())
nrx = min((N, 5))
nry = int(np.ceil(N / nrx))
# the sizes are heuristics [inches]
sizex = nrx * 3
sizey = nry * 4 - 1
fig, axes = plt.subplots(
nry, nrx,
sharex=True,
sharey=True,
figsize=(sizex, sizey),
)
axes = np.atleast_2d(axes)
cbs = []
for ax, (name, group) in zip(axes.flat, g):
fig1, axes1, cb1 = plot_pseudosection_type2(
group,
key,
ax=ax,
log10=False,
cbmin=kwargs.get('cbmin', None),
cbmax=kwargs.get('cbmax', None),
)
cbs.append(cb1)
ax.set_title('timestep: {0}'.format(int(name)))
ax.xaxis.set_ticks_position('bottom')
ax.set_aspect('equal')
for cb in np.array(cbs).reshape(axes.shape)[:, 0:-1].flat:
cb.ax.set_visible(False)
fancyfy(axes, N)
fig.tight_layout()
return fig |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:plot_pseudodepths; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:configs; 5, identifier:nr_electrodes; 6, default_parameter; 6, 7; 6, 8; 7, identifier:spacing; 8, integer:1; 9, default_parameter; 9, 10; 9, 11; 10, identifier:grid; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:ctypes; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:dd_merge; 17, False; 18, dictionary_splat_pattern; 18, 19; 19, identifier:kwargs; 20, block; 20, 21; 20, 23; 20, 24; 20, 25; 20, 38; 20, 51; 20, 52; 20, 59; 20, 74; 20, 75; 20, 79; 20, 83; 20, 386; 21, expression_statement; 21, 22; 22, comment; 23, comment; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:pseudo_d_functions; 28, dictionary; 28, 29; 28, 32; 28, 35; 29, pair; 29, 30; 29, 31; 30, string:'dd'; 31, identifier:_pseudodepths_dd_simple; 32, pair; 32, 33; 32, 34; 33, string:'schlumberger'; 34, identifier:_pseudodepths_schlumberger; 35, pair; 35, 36; 35, 37; 36, string:'wenner'; 37, identifier:_pseudodepths_wenner; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:titles; 41, dictionary; 41, 42; 41, 45; 41, 48; 42, pair; 42, 43; 42, 44; 43, string:'dd'; 44, string:'dipole-dipole configurations'; 45, pair; 45, 46; 45, 47; 46, string:'schlumberger'; 47, string:'Schlumberger configurations'; 48, pair; 48, 49; 48, 50; 49, string:'wenner'; 50, string:'Wenner configurations'; 51, comment; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:only_types; 55, boolean_operator:or; 55, 56; 55, 57; 56, identifier:ctypes; 57, list:['dd', ]; 57, 58; 58, string:'dd'; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:results; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:fT; 65, identifier:filter; 66, argument_list; 66, 67; 66, 68; 67, identifier:configs; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:settings; 70, dictionary; 70, 71; 71, pair; 71, 72; 71, 73; 72, string:'only_types'; 73, identifier:only_types; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:figs; 78, list:[]; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:axes; 82, list:[]; 83, for_statement; 83, 84; 83, 85; 83, 93; 84, identifier:key; 85, call; 85, 86; 85, 87; 86, identifier:sorted; 87, argument_list; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:results; 91, identifier:keys; 92, argument_list; 93, block; 93, 94; 93, 100; 93, 106; 93, 112; 93, 113; 93, 114; 93, 115; 93, 116; 93, 117; 93, 193; 93, 197; 93, 198; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:print; 97, argument_list; 97, 98; 97, 99; 98, string:'plotting: '; 99, identifier:key; 100, if_statement; 100, 101; 100, 104; 101, comparison_operator:==; 101, 102; 101, 103; 102, identifier:key; 103, string:'not_sorted'; 104, block; 104, 105; 105, continue_statement; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:index_dict; 109, subscript; 109, 110; 109, 111; 110, identifier:results; 111, identifier:key; 112, comment; 113, comment; 114, comment; 115, comment; 116, comment; 117, if_statement; 117, 118; 117, 124; 117, 165; 118, boolean_operator:and; 118, 119; 118, 122; 119, comparison_operator:==; 119, 120; 119, 121; 120, identifier:key; 121, string:'dd'; 122, not_operator; 122, 123; 123, identifier:dd_merge; 124, block; 124, 125; 124, 129; 124, 133; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:plot_list; 128, list:[]; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:labels_add; 132, list:[]; 133, for_statement; 133, 134; 133, 135; 133, 143; 134, identifier:skip; 135, call; 135, 136; 135, 137; 136, identifier:sorted; 137, argument_list; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:index_dict; 141, identifier:keys; 142, argument_list; 143, block; 143, 144; 143, 153; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:plot_list; 148, identifier:append; 149, argument_list; 149, 150; 150, subscript; 150, 151; 150, 152; 151, identifier:index_dict; 152, identifier:skip; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:labels_add; 157, identifier:append; 158, argument_list; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, string:' - skip {0}'; 162, identifier:format; 163, argument_list; 163, 164; 164, identifier:skip; 165, else_clause; 165, 166; 165, 167; 166, comment; 167, block; 167, 168; 167, 182; 167, 188; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:plot_list; 171, list:[np.hstack(index_dict.values()), ]; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:np; 175, identifier:hstack; 176, argument_list; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:index_dict; 180, identifier:values; 181, argument_list; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:print; 185, argument_list; 185, 186; 185, 187; 186, string:'schlumberger'; 187, identifier:plot_list; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:labels_add; 191, list:['', ]; 191, 192; 192, string:''; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:grid; 196, None; 197, comment; 198, for_statement; 198, 199; 198, 202; 198, 207; 199, pattern_list; 199, 200; 199, 201; 200, identifier:indices; 201, identifier:label_add; 202, call; 202, 203; 202, 204; 203, identifier:zip; 204, argument_list; 204, 205; 204, 206; 205, identifier:plot_list; 206, identifier:labels_add; 207, block; 207, 208; 207, 217; 207, 223; 207, 236; 207, 255; 207, 269; 207, 270; 207, 334; 207, 345; 207, 352; 207, 359; 207, 366; 207, 372; 207, 379; 208, if_statement; 208, 209; 208, 215; 209, comparison_operator:==; 209, 210; 209, 214; 210, call; 210, 211; 210, 212; 211, identifier:len; 212, argument_list; 212, 213; 213, identifier:indices; 214, integer:0; 215, block; 215, 216; 216, continue_statement; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:ddc; 220, subscript; 220, 221; 220, 222; 221, identifier:configs; 222, identifier:indices; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 228; 225, pattern_list; 225, 226; 225, 227; 226, identifier:px; 227, identifier:pz; 228, call; 228, 229; 228, 232; 229, subscript; 229, 230; 229, 231; 230, identifier:pseudo_d_functions; 231, identifier:key; 232, argument_list; 232, 233; 232, 234; 232, 235; 233, identifier:ddc; 234, identifier:spacing; 235, identifier:grid; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 241; 238, pattern_list; 238, 239; 238, 240; 239, identifier:fig; 240, identifier:ax; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:plt; 244, identifier:subplots; 245, argument_list; 245, 246; 246, keyword_argument; 246, 247; 246, 248; 247, identifier:figsize; 248, tuple; 248, 249; 248, 252; 249, binary_operator:/; 249, 250; 249, 251; 250, integer:15; 251, float:2.54; 252, binary_operator:/; 252, 253; 252, 254; 253, integer:5; 254, float:2.54; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:ax; 259, identifier:scatter; 260, argument_list; 260, 261; 260, 262; 260, 263; 260, 266; 261, identifier:px; 262, identifier:pz; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:color; 265, string:'k'; 266, keyword_argument; 266, 267; 266, 268; 267, identifier:alpha; 268, float:0.5; 269, comment; 270, if_statement; 270, 271; 270, 274; 270, 305; 271, comparison_operator:is; 271, 272; 271, 273; 272, identifier:grid; 273, None; 274, block; 274, 275; 274, 283; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:electrodes; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:grid; 281, identifier:get_electrode_positions; 282, argument_list; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:ax; 287, identifier:scatter; 288, argument_list; 288, 289; 288, 294; 288, 299; 288, 302; 289, subscript; 289, 290; 289, 291; 289, 293; 290, identifier:electrodes; 291, slice; 291, 292; 292, colon; 293, integer:0; 294, subscript; 294, 295; 294, 296; 294, 298; 295, identifier:electrodes; 296, slice; 296, 297; 297, colon; 298, integer:1; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:color; 301, string:'b'; 302, keyword_argument; 302, 303; 302, 304; 303, identifier:label; 304, string:'electrodes'; 305, else_clause; 305, 306; 306, block; 306, 307; 307, expression_statement; 307, 308; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:ax; 311, identifier:scatter; 312, argument_list; 312, 313; 312, 322; 312, 328; 312, 331; 313, binary_operator:*; 313, 314; 313, 321; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:np; 317, identifier:arange; 318, argument_list; 318, 319; 318, 320; 319, integer:0; 320, identifier:nr_electrodes; 321, identifier:spacing; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:np; 325, identifier:zeros; 326, argument_list; 326, 327; 327, identifier:nr_electrodes; 328, keyword_argument; 328, 329; 328, 330; 329, identifier:color; 330, string:'b'; 331, keyword_argument; 331, 332; 331, 333; 332, identifier:label; 333, string:'electrodes'; 334, expression_statement; 334, 335; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:ax; 338, identifier:set_title; 339, argument_list; 339, 340; 340, binary_operator:+; 340, 341; 340, 344; 341, subscript; 341, 342; 341, 343; 342, identifier:titles; 343, identifier:key; 344, identifier:label_add; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:ax; 349, identifier:set_aspect; 350, argument_list; 350, 351; 351, string:'equal'; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:ax; 356, identifier:set_xlabel; 357, argument_list; 357, 358; 358, string:'x [m]'; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:ax; 363, identifier:set_ylabel; 364, argument_list; 364, 365; 365, string:'x [z]'; 366, expression_statement; 366, 367; 367, call; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:fig; 370, identifier:tight_layout; 371, argument_list; 372, expression_statement; 372, 373; 373, call; 373, 374; 373, 377; 374, attribute; 374, 375; 374, 376; 375, identifier:figs; 376, identifier:append; 377, argument_list; 377, 378; 378, identifier:fig; 379, expression_statement; 379, 380; 380, call; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:axes; 383, identifier:append; 384, argument_list; 384, 385; 385, identifier:ax; 386, if_statement; 386, 387; 386, 393; 386, 402; 387, comparison_operator:==; 387, 388; 387, 392; 388, call; 388, 389; 388, 390; 389, identifier:len; 390, argument_list; 390, 391; 391, identifier:figs; 392, integer:1; 393, block; 393, 394; 394, return_statement; 394, 395; 395, expression_list; 395, 396; 395, 399; 396, subscript; 396, 397; 396, 398; 397, identifier:figs; 398, integer:0; 399, subscript; 399, 400; 399, 401; 400, identifier:axes; 401, integer:0; 402, else_clause; 402, 403; 403, block; 403, 404; 404, return_statement; 404, 405; 405, expression_list; 405, 406; 405, 407; 406, identifier:figs; 407, identifier:axes | def plot_pseudodepths(configs, nr_electrodes, spacing=1, grid=None,
ctypes=None, dd_merge=False, **kwargs):
"""Plot pseudodepths for the measurements. If grid is given, then the
actual electrode positions are used, and the parameter 'spacing' is
ignored'
Parameters
----------
configs: :class:`numpy.ndarray`
Nx4 array containing the quadrupoles for different measurements
nr_electrodes: int
The overall number of electrodes of the dataset. This is used to plot
the surface electrodes
spacing: float, optional
assumed distance between electrodes. Default=1
grid: crtomo.grid.crt_grid instance, optional
grid instance. Used to infer real electrode positions
ctypes: list of strings, optional
a list of configuration types that will be plotted. All
configurations that can not be sorted into these types will not be
plotted! Possible types:
* dd
* schlumberger
dd_merge: bool, optional
if True, merge all skips. Otherwise, generate individual plots for
each skip
Returns
-------
figs: matplotlib.figure.Figure instance or list of Figure instances
if only one type was plotted, then the figure instance is returned.
Otherwise, return a list of figure instances.
axes: axes object or list of axes ojects
plot axes
Examples
--------
.. plot::
:include-source:
from reda.plotters.plots2d import plot_pseudodepths
# define a few measurements
import numpy as np
configs = np.array((
(1, 2, 4, 3),
(1, 2, 5, 4),
(1, 2, 6, 5),
(2, 3, 5, 4),
(2, 3, 6, 5),
(3, 4, 6, 5),
))
# plot
fig, axes = plot_pseudodepths(configs, nr_electrodes=6, spacing=1,
ctypes=['dd', ])
.. plot::
:include-source:
from reda.plotters.plots2d import plot_pseudodepths
# define a few measurements
import numpy as np
configs = np.array((
(4, 7, 5, 6),
(3, 8, 5, 6),
(2, 9, 5, 6),
(1, 10, 5, 6),
))
# plot
fig, axes = plot_pseudodepths(configs, nr_electrodes=10, spacing=1,
ctypes=['schlumberger', ])
"""
# for each configuration type we have different ways of computing
# pseudodepths
pseudo_d_functions = {
'dd': _pseudodepths_dd_simple,
'schlumberger': _pseudodepths_schlumberger,
'wenner': _pseudodepths_wenner,
}
titles = {
'dd': 'dipole-dipole configurations',
'schlumberger': 'Schlumberger configurations',
'wenner': 'Wenner configurations',
}
# sort the configurations into the various types of configurations
only_types = ctypes or ['dd', ]
results = fT.filter(configs, settings={'only_types': only_types, })
# loop through all measurement types
figs = []
axes = []
for key in sorted(results.keys()):
print('plotting: ', key)
if key == 'not_sorted':
continue
index_dict = results[key]
# it is possible that we want to generate multiple plots for one
# type of measurement, i.e., to separate skips of dipole-dipole
# measurements. Therefore we generate two lists:
# 1) list of list of indices to plot
# 2) corresponding labels
if key == 'dd' and not dd_merge:
plot_list = []
labels_add = []
for skip in sorted(index_dict.keys()):
plot_list.append(index_dict[skip])
labels_add.append(' - skip {0}'.format(skip))
else:
# merge all indices
plot_list = [np.hstack(index_dict.values()), ]
print('schlumberger', plot_list)
labels_add = ['', ]
grid = None
# generate plots
for indices, label_add in zip(plot_list, labels_add):
if len(indices) == 0:
continue
ddc = configs[indices]
px, pz = pseudo_d_functions[key](ddc, spacing, grid)
fig, ax = plt.subplots(figsize=(15 / 2.54, 5 / 2.54))
ax.scatter(px, pz, color='k', alpha=0.5)
# plot electrodes
if grid is not None:
electrodes = grid.get_electrode_positions()
ax.scatter(
electrodes[:, 0],
electrodes[:, 1],
color='b',
label='electrodes', )
else:
ax.scatter(
np.arange(0, nr_electrodes) * spacing,
np.zeros(nr_electrodes),
color='b',
label='electrodes', )
ax.set_title(titles[key] + label_add)
ax.set_aspect('equal')
ax.set_xlabel('x [m]')
ax.set_ylabel('x [z]')
fig.tight_layout()
figs.append(fig)
axes.append(ax)
if len(figs) == 1:
return figs[0], axes[0]
else:
return figs, axes |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:complete; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:uio; 6, default_parameter; 6, 7; 6, 8; 7, identifier:dropped; 8, False; 9, block; 9, 10; 9, 12; 9, 22; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 19; 12, 20; 13, boolean_operator:and; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:dropped; 17, not_operator; 17, 18; 18, identifier:dropped; 19, comment; 20, block; 20, 21; 21, return_statement; 22, for_statement; 22, 23; 22, 24; 22, 27; 23, identifier:end; 24, list:['src', 'dst']; 24, 25; 24, 26; 25, string:'src'; 26, string:'dst'; 27, block; 27, 28; 27, 37; 27, 48; 27, 55; 27, 66; 27, 154; 27, 155; 27, 180; 27, 181; 28, if_statement; 28, 29; 28, 34; 29, call; 29, 30; 29, 31; 30, identifier:getattr; 31, argument_list; 31, 32; 31, 33; 32, identifier:self; 33, identifier:end; 34, block; 34, 35; 34, 36; 35, continue_statement; 36, comment; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:uio; 41, identifier:show; 42, argument_list; 42, 43; 43, binary_operator:+; 43, 44; 43, 47; 44, binary_operator:+; 44, 45; 44, 46; 45, string:'\nEnter '; 46, identifier:end; 47, string:' for transaction:'; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:uio; 52, identifier:show; 53, argument_list; 53, 54; 54, string:''; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:uio; 59, identifier:show; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:summary; 65, argument_list; 66, try_statement; 66, 67; 66, 141; 67, block; 67, 68; 67, 72; 67, 78; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:endpoints; 71, list:[]; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:remaining; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:amount; 78, while_statement; 78, 79; 78, 80; 79, identifier:remaining; 80, block; 80, 81; 80, 91; 80, 109; 80, 120; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:account; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:uio; 87, identifier:text; 88, argument_list; 88, 89; 88, 90; 89, string:' Enter account'; 90, None; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:amount; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:uio; 97, identifier:decimal; 98, argument_list; 98, 99; 98, 100; 98, 103; 98, 106; 99, string:' Enter amount'; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:default; 102, identifier:remaining; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:lower; 105, integer:0; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:upper; 108, identifier:remaining; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:endpoints; 113, identifier:append; 114, argument_list; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:Endpoint; 117, argument_list; 117, 118; 117, 119; 118, identifier:account; 119, identifier:amount; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:remaining; 123, binary_operator:-; 123, 124; 123, 127; 123, 128; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:amount; 127, line_continuation:\; 128, call; 128, 129; 128, 130; 129, identifier:sum; 130, argument_list; 130, 131; 131, call; 131, 132; 131, 133; 132, identifier:map; 133, argument_list; 133, 134; 133, 140; 134, lambda; 134, 135; 134, 137; 135, lambda_parameters; 135, 136; 136, identifier:x; 137, attribute; 137, 138; 137, 139; 138, identifier:x; 139, identifier:amount; 140, identifier:endpoints; 141, except_clause; 141, 142; 141, 145; 141, 146; 142, attribute; 142, 143; 142, 144; 143, identifier:ui; 144, identifier:RejectWarning; 145, comment; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:sys; 151, identifier:exit; 152, argument_list; 152, 153; 153, string:"bye!"; 154, comment; 155, if_statement; 155, 156; 155, 159; 156, comparison_operator:==; 156, 157; 156, 158; 157, identifier:end; 158, string:'src'; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:endpoints; 163, call; 163, 164; 163, 165; 164, identifier:map; 165, argument_list; 165, 166; 165, 179; 166, lambda; 166, 167; 166, 169; 167, lambda_parameters; 167, 168; 168, identifier:x; 169, call; 169, 170; 169, 171; 170, identifier:Endpoint; 171, argument_list; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:x; 174, identifier:account; 175, unary_operator:-; 175, 176; 176, attribute; 176, 177; 176, 178; 177, identifier:x; 178, identifier:amount; 179, identifier:endpoints; 180, comment; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:setattr; 184, argument_list; 184, 185; 184, 186; 184, 187; 185, identifier:self; 186, identifier:end; 187, identifier:endpoints | def complete(self, uio, dropped=False):
"""Query for all missing information in the transaction"""
if self.dropped and not dropped:
# do nothing for dropped xn, unless specifically told to
return
for end in ['src', 'dst']:
if getattr(self, end):
continue # we have this information
uio.show('\nEnter ' + end + ' for transaction:')
uio.show('')
uio.show(self.summary())
try:
endpoints = []
remaining = self.amount
while remaining:
account = uio.text(' Enter account', None)
amount = uio.decimal(
' Enter amount',
default=remaining,
lower=0,
upper=remaining
)
endpoints.append(Endpoint(account, amount))
remaining = self.amount \
- sum(map(lambda x: x.amount, endpoints))
except ui.RejectWarning:
# bail out
sys.exit("bye!")
# flip amounts if it was a src outcome
if end == 'src':
endpoints = map(
lambda x: Endpoint(x.account, -x.amount),
endpoints
)
# set endpoints
setattr(self, end, endpoints) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 27; 2, function_name:gen_dipole_dipole; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 4, identifier:self; 5, identifier:skipc; 6, default_parameter; 6, 7; 6, 8; 7, identifier:skipv; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:stepc; 11, integer:1; 12, default_parameter; 12, 13; 12, 14; 13, identifier:stepv; 14, integer:1; 15, default_parameter; 15, 16; 15, 17; 16, identifier:nr_voltage_dipoles; 17, integer:10; 18, default_parameter; 18, 19; 18, 20; 19, identifier:before_current; 20, False; 21, default_parameter; 21, 22; 21, 23; 22, identifier:start_skip; 23, integer:0; 24, default_parameter; 24, 25; 24, 26; 25, identifier:N; 26, None; 27, block; 27, 28; 27, 30; 27, 57; 27, 58; 27, 67; 27, 71; 27, 72; 27, 226; 27, 235; 27, 236; 27, 266; 28, expression_statement; 28, 29; 29, comment; 30, if_statement; 30, 31; 30, 40; 30, 46; 31, boolean_operator:and; 31, 32; 31, 35; 32, comparison_operator:is; 32, 33; 32, 34; 33, identifier:N; 34, None; 35, comparison_operator:is; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:nr_electrodes; 39, None; 40, block; 40, 41; 41, raise_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:Exception; 44, argument_list; 44, 45; 45, string:'You must provide the number of electrodes'; 46, elif_clause; 46, 47; 46, 50; 47, comparison_operator:is; 47, 48; 47, 49; 48, identifier:N; 49, None; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:N; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:nr_electrodes; 57, comment; 58, if_statement; 58, 59; 58, 62; 59, comparison_operator:is; 59, 60; 59, 61; 60, identifier:skipv; 61, None; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:skipv; 66, identifier:skipc; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:configs; 70, list:[]; 71, comment; 72, for_statement; 72, 73; 72, 74; 72, 86; 73, identifier:a; 74, call; 74, 75; 74, 76; 75, identifier:range; 76, argument_list; 76, 77; 76, 78; 76, 85; 77, integer:0; 78, binary_operator:-; 78, 79; 78, 84; 79, binary_operator:-; 79, 80; 79, 83; 80, binary_operator:-; 80, 81; 80, 82; 81, identifier:N; 82, identifier:skipv; 83, identifier:skipc; 84, integer:3; 85, identifier:stepc; 86, block; 86, 87; 86, 95; 86, 99; 86, 100; 86, 164; 86, 165; 86, 169; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:b; 90, binary_operator:+; 90, 91; 90, 94; 91, binary_operator:+; 91, 92; 91, 93; 92, identifier:a; 93, identifier:skipc; 94, integer:1; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:nr; 98, integer:0; 99, comment; 100, if_statement; 100, 101; 100, 102; 101, identifier:before_current; 102, block; 102, 103; 103, for_statement; 103, 104; 103, 105; 103, 117; 104, identifier:n; 105, call; 105, 106; 105, 107; 106, identifier:range; 107, argument_list; 107, 108; 107, 113; 107, 115; 108, binary_operator:-; 108, 109; 108, 112; 109, binary_operator:-; 109, 110; 109, 111; 110, identifier:a; 111, identifier:start_skip; 112, integer:1; 113, unary_operator:-; 113, 114; 114, integer:1; 115, unary_operator:-; 115, 116; 116, identifier:stepv; 117, block; 117, 118; 117, 122; 117, 128; 117, 136; 117, 142; 117, 157; 118, expression_statement; 118, 119; 119, augmented_assignment:+=; 119, 120; 119, 121; 120, identifier:nr; 121, integer:1; 122, if_statement; 122, 123; 122, 126; 123, comparison_operator:>; 123, 124; 123, 125; 124, identifier:nr; 125, identifier:nr_voltage_dipoles; 126, block; 126, 127; 127, continue_statement; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:m; 131, binary_operator:-; 131, 132; 131, 135; 132, binary_operator:-; 132, 133; 132, 134; 133, identifier:n; 134, identifier:skipv; 135, integer:1; 136, if_statement; 136, 137; 136, 140; 137, comparison_operator:<; 137, 138; 137, 139; 138, identifier:m; 139, integer:0; 140, block; 140, 141; 141, continue_statement; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:quadpole; 145, binary_operator:+; 145, 146; 145, 156; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:np; 149, identifier:array; 150, argument_list; 150, 151; 151, tuple; 151, 152; 151, 153; 151, 154; 151, 155; 152, identifier:a; 153, identifier:b; 154, identifier:m; 155, identifier:n; 156, integer:1; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:configs; 161, identifier:append; 162, argument_list; 162, 163; 163, identifier:quadpole; 164, comment; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:nr; 168, integer:0; 169, for_statement; 169, 170; 169, 171; 169, 185; 170, identifier:m; 171, call; 171, 172; 171, 173; 172, identifier:range; 173, argument_list; 173, 174; 173, 179; 173, 184; 174, binary_operator:+; 174, 175; 174, 178; 175, binary_operator:+; 175, 176; 175, 177; 176, identifier:b; 177, identifier:start_skip; 178, integer:1; 179, binary_operator:-; 179, 180; 179, 183; 180, binary_operator:-; 180, 181; 180, 182; 181, identifier:N; 182, identifier:skipv; 183, integer:1; 184, identifier:stepv; 185, block; 185, 186; 185, 190; 185, 196; 185, 204; 185, 219; 186, expression_statement; 186, 187; 187, augmented_assignment:+=; 187, 188; 187, 189; 188, identifier:nr; 189, integer:1; 190, if_statement; 190, 191; 190, 194; 191, comparison_operator:>; 191, 192; 191, 193; 192, identifier:nr; 193, identifier:nr_voltage_dipoles; 194, block; 194, 195; 195, continue_statement; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:n; 199, binary_operator:+; 199, 200; 199, 203; 200, binary_operator:+; 200, 201; 200, 202; 201, identifier:m; 202, identifier:skipv; 203, integer:1; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:quadpole; 207, binary_operator:+; 207, 208; 207, 218; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:np; 211, identifier:array; 212, argument_list; 212, 213; 213, tuple; 213, 214; 213, 215; 213, 216; 213, 217; 214, identifier:a; 215, identifier:b; 216, identifier:m; 217, identifier:n; 218, integer:1; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:configs; 223, identifier:append; 224, argument_list; 224, 225; 225, identifier:quadpole; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:configs; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:np; 232, identifier:array; 233, argument_list; 233, 234; 234, identifier:configs; 235, comment; 236, if_statement; 236, 237; 236, 242; 236, 249; 237, comparison_operator:is; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:configs; 241, None; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:self; 247, identifier:configs; 248, identifier:configs; 249, else_clause; 249, 250; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:self; 255, identifier:configs; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:np; 259, identifier:vstack; 260, argument_list; 260, 261; 261, tuple; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:self; 264, identifier:configs; 265, identifier:configs; 266, return_statement; 266, 267; 267, identifier:configs | def gen_dipole_dipole(self,
skipc,
skipv=None,
stepc=1,
stepv=1,
nr_voltage_dipoles=10,
before_current=False,
start_skip=0,
N=None):
"""Generate dipole-dipole configurations
Parameters
----------
skipc: int
number of electrode positions that are skipped between electrodes
of a given dipole
skipv: int
steplength between subsequent voltage dipoles. A steplength of 0
will produce increments by one, i.e., 3-4, 4-5, 5-6 ...
stepc: int
steplength between subsequent current dipoles. A steplength of 0
will produce increments by one, i.e., 3-4, 4-5, 5-6 ...
stepv: int
steplength between subsequent voltage dipoles. A steplength of 0
will produce increments by one, i.e., 3-4, 4-5, 5-6 ...
nr_voltage_dipoles: int
the number of voltage dipoles to generate for each current
injection dipole
before_current: bool, optional
if set to True, also generate voltage dipoles in front of current
dipoles.
start_skip: int, optional
how many electrode to skip before/after the first/second current
electrode.
N: int, optional
number of electrodes, must be given if not already known by the
config instance
Examples
--------
>>> from reda.configs.configManager import ConfigManager
>>> config = ConfigManager(nr_of_electrodes=10)
>>> config.gen_dipole_dipole(skipc=2)
array([[ 1, 4, 5, 8],
[ 1, 4, 6, 9],
[ 1, 4, 7, 10],
[ 2, 5, 6, 9],
[ 2, 5, 7, 10],
[ 3, 6, 7, 10]])
"""
if N is None and self.nr_electrodes is None:
raise Exception('You must provide the number of electrodes')
elif N is None:
N = self.nr_electrodes
# by default, current voltage dipoles have the same size
if skipv is None:
skipv = skipc
configs = []
# current dipoles
for a in range(0, N - skipv - skipc - 3, stepc):
b = a + skipc + 1
nr = 0
# potential dipoles before current injection
if before_current:
for n in range(a - start_skip - 1, -1, -stepv):
nr += 1
if nr > nr_voltage_dipoles:
continue
m = n - skipv - 1
if m < 0:
continue
quadpole = np.array((a, b, m, n)) + 1
configs.append(quadpole)
# potential dipoles after current injection
nr = 0
for m in range(b + start_skip + 1, N - skipv - 1, stepv):
nr += 1
if nr > nr_voltage_dipoles:
continue
n = m + skipv + 1
quadpole = np.array((a, b, m, n)) + 1
configs.append(quadpole)
configs = np.array(configs)
# now add to the instance
if self.configs is None:
self.configs = configs
else:
self.configs = np.vstack((self.configs, configs))
return configs |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:split_into_normal_and_reciprocal; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:pad; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:return_indices; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 60; 11, 68; 11, 76; 11, 77; 11, 90; 11, 91; 11, 95; 11, 99; 11, 103; 11, 107; 11, 111; 11, 115; 11, 323; 11, 324; 11, 342; 11, 349; 11, 355; 11, 391; 11, 400; 11, 409; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:configs; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:np; 21, identifier:hstack; 22, argument_list; 22, 23; 23, tuple; 23, 24; 23, 42; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:np; 27, identifier:sort; 28, argument_list; 28, 29; 28, 39; 29, subscript; 29, 30; 29, 33; 29, 35; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:configs; 33, slice; 33, 34; 34, colon; 35, slice; 35, 36; 35, 37; 35, 38; 36, integer:0; 37, colon; 38, integer:2; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:axis; 41, integer:1; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:np; 45, identifier:sort; 46, argument_list; 46, 47; 46, 57; 47, subscript; 47, 48; 47, 51; 47, 53; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:configs; 51, slice; 51, 52; 52, colon; 53, slice; 53, 54; 53, 55; 53, 56; 54, integer:2; 55, colon; 56, integer:4; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:axis; 59, integer:1; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:ab_min; 63, subscript; 63, 64; 63, 65; 63, 67; 64, identifier:configs; 65, slice; 65, 66; 66, colon; 67, integer:0; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:mn_min; 71, subscript; 71, 72; 71, 73; 71, 75; 72, identifier:configs; 73, slice; 73, 74; 74, colon; 75, integer:2; 76, comment; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:indices_normal; 80, subscript; 80, 81; 80, 89; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:np; 84, identifier:where; 85, argument_list; 85, 86; 86, comparison_operator:<; 86, 87; 86, 88; 87, identifier:ab_min; 88, identifier:mn_min; 89, integer:0; 90, comment; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:indices_used; 94, list:[]; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:normal; 98, list:[]; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:normal_indices; 102, list:[]; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:reciprocal_indices; 106, list:[]; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:reciprocal; 110, list:[]; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:duplicates; 114, list:[]; 115, for_statement; 115, 116; 115, 117; 115, 118; 116, identifier:index; 117, identifier:indices_normal; 118, block; 118, 119; 118, 126; 118, 139; 118, 146; 118, 147; 118, 205; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:indices_used; 123, identifier:append; 124, argument_list; 124, 125; 125, identifier:index; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:normal; 130, identifier:append; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 136; 132, 137; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:configs; 136, identifier:index; 137, slice; 137, 138; 138, colon; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:normal_indices; 143, identifier:append; 144, argument_list; 144, 145; 145, identifier:index; 146, comment; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:index_rec; 150, subscript; 150, 151; 150, 204; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:np; 154, identifier:where; 155, argument_list; 155, 156; 155, 157; 156, comment; 157, binary_operator:&; 157, 158; 157, 193; 158, binary_operator:&; 158, 159; 158, 182; 159, binary_operator:&; 159, 160; 159, 171; 160, parenthesized_expression; 160, 161; 161, comparison_operator:==; 161, 162; 161, 167; 162, subscript; 162, 163; 162, 164; 162, 166; 163, identifier:configs; 164, slice; 164, 165; 165, colon; 166, integer:0; 167, subscript; 167, 168; 167, 169; 167, 170; 168, identifier:configs; 169, identifier:index; 170, integer:2; 171, parenthesized_expression; 171, 172; 172, comparison_operator:==; 172, 173; 172, 178; 173, subscript; 173, 174; 173, 175; 173, 177; 174, identifier:configs; 175, slice; 175, 176; 176, colon; 177, integer:1; 178, subscript; 178, 179; 178, 180; 178, 181; 179, identifier:configs; 180, identifier:index; 181, integer:3; 182, parenthesized_expression; 182, 183; 183, comparison_operator:==; 183, 184; 183, 189; 184, subscript; 184, 185; 184, 186; 184, 188; 185, identifier:configs; 186, slice; 186, 187; 187, colon; 188, integer:2; 189, subscript; 189, 190; 189, 191; 189, 192; 190, identifier:configs; 191, identifier:index; 192, integer:0; 193, parenthesized_expression; 193, 194; 194, comparison_operator:==; 194, 195; 194, 200; 195, subscript; 195, 196; 195, 197; 195, 199; 196, identifier:configs; 197, slice; 197, 198; 198, colon; 199, integer:3; 200, subscript; 200, 201; 200, 202; 200, 203; 201, identifier:configs; 202, identifier:index; 203, integer:1; 204, integer:0; 205, if_statement; 205, 206; 205, 214; 205, 231; 205, 272; 206, boolean_operator:and; 206, 207; 206, 213; 207, comparison_operator:==; 207, 208; 207, 212; 208, call; 208, 209; 208, 210; 209, identifier:len; 210, argument_list; 210, 211; 211, identifier:index_rec; 212, integer:0; 213, identifier:pad; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:reciprocal; 219, identifier:append; 220, argument_list; 220, 221; 221, binary_operator:*; 221, 222; 221, 228; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:np; 225, identifier:ones; 226, argument_list; 226, 227; 227, integer:4; 228, attribute; 228, 229; 228, 230; 229, identifier:np; 230, identifier:nan; 231, elif_clause; 231, 232; 231, 238; 232, comparison_operator:==; 232, 233; 232, 237; 233, call; 233, 234; 233, 235; 234, identifier:len; 235, argument_list; 235, 236; 236, identifier:index_rec; 237, integer:1; 238, block; 238, 239; 238, 254; 238, 263; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:reciprocal; 243, identifier:append; 244, argument_list; 244, 245; 245, subscript; 245, 246; 245, 249; 245, 252; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:configs; 249, subscript; 249, 250; 249, 251; 250, identifier:index_rec; 251, integer:0; 252, slice; 252, 253; 253, colon; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:indices_used; 258, identifier:append; 259, argument_list; 259, 260; 260, subscript; 260, 261; 260, 262; 261, identifier:index_rec; 262, integer:0; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:reciprocal_indices; 267, identifier:append; 268, argument_list; 268, 269; 269, subscript; 269, 270; 269, 271; 270, identifier:index_rec; 271, integer:0; 272, elif_clause; 272, 273; 272, 279; 272, 280; 273, call; 273, 274; 273, 275; 274, identifier:len; 275, argument_list; 275, 276; 276, comparison_operator:>; 276, 277; 276, 278; 277, identifier:index_rec; 278, integer:1; 279, comment; 280, block; 280, 281; 280, 296; 280, 305; 280, 316; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:reciprocal; 285, identifier:append; 286, argument_list; 286, 287; 287, subscript; 287, 288; 287, 291; 287, 294; 288, attribute; 288, 289; 288, 290; 289, identifier:self; 290, identifier:configs; 291, subscript; 291, 292; 291, 293; 292, identifier:index_rec; 293, integer:0; 294, slice; 294, 295; 295, colon; 296, expression_statement; 296, 297; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:reciprocal_indices; 300, identifier:append; 301, argument_list; 301, 302; 302, subscript; 302, 303; 302, 304; 303, identifier:index_rec; 304, integer:0; 305, expression_statement; 305, 306; 306, augmented_assignment:+=; 306, 307; 306, 308; 307, identifier:duplicates; 308, call; 308, 309; 308, 310; 309, identifier:list; 310, argument_list; 310, 311; 311, subscript; 311, 312; 311, 313; 312, identifier:index_rec; 313, slice; 313, 314; 313, 315; 314, integer:1; 315, colon; 316, expression_statement; 316, 317; 317, augmented_assignment:+=; 317, 318; 317, 319; 318, identifier:indices_used; 319, call; 319, 320; 319, 321; 320, identifier:list; 321, argument_list; 321, 322; 322, identifier:index_rec; 323, comment; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 327; 326, identifier:set_all_indices; 327, call; 327, 328; 327, 329; 328, identifier:set; 329, argument_list; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:list; 332, argument_list; 332, 333; 333, call; 333, 334; 333, 335; 334, identifier:range; 335, argument_list; 335, 336; 335, 337; 336, integer:0; 337, subscript; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:configs; 340, identifier:shape; 341, integer:0; 342, expression_statement; 342, 343; 343, assignment; 343, 344; 343, 345; 344, identifier:set_used_indices; 345, call; 345, 346; 345, 347; 346, identifier:set; 347, argument_list; 347, 348; 348, identifier:indices_used; 349, expression_statement; 349, 350; 350, assignment; 350, 351; 350, 352; 351, identifier:reciprocal_only_indices; 352, binary_operator:-; 352, 353; 352, 354; 353, identifier:set_all_indices; 354, identifier:set_used_indices; 355, for_statement; 355, 356; 355, 357; 355, 358; 356, identifier:index; 357, identifier:reciprocal_only_indices; 358, block; 358, 359; 358, 378; 359, if_statement; 359, 360; 359, 361; 360, identifier:pad; 361, block; 361, 362; 362, expression_statement; 362, 363; 363, call; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:normal; 366, identifier:append; 367, argument_list; 367, 368; 368, binary_operator:*; 368, 369; 368, 375; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:np; 372, identifier:ones; 373, argument_list; 373, 374; 374, integer:4; 375, attribute; 375, 376; 375, 377; 376, identifier:np; 377, identifier:nan; 378, expression_statement; 378, 379; 379, call; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:reciprocal; 382, identifier:append; 383, argument_list; 383, 384; 384, subscript; 384, 385; 384, 388; 384, 389; 385, attribute; 385, 386; 385, 387; 386, identifier:self; 387, identifier:configs; 388, identifier:index; 389, slice; 389, 390; 390, colon; 391, expression_statement; 391, 392; 392, assignment; 392, 393; 392, 394; 393, identifier:normals; 394, call; 394, 395; 394, 398; 395, attribute; 395, 396; 395, 397; 396, identifier:np; 397, identifier:array; 398, argument_list; 398, 399; 399, identifier:normal; 400, expression_statement; 400, 401; 401, assignment; 401, 402; 401, 403; 402, identifier:reciprocals; 403, call; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:np; 406, identifier:array; 407, argument_list; 407, 408; 408, identifier:reciprocal; 409, if_statement; 409, 410; 409, 411; 409, 418; 410, identifier:return_indices; 411, block; 411, 412; 412, return_statement; 412, 413; 413, expression_list; 413, 414; 413, 415; 413, 416; 413, 417; 414, identifier:normals; 415, identifier:reciprocals; 416, identifier:normal_indices; 417, identifier:reciprocal_indices; 418, else_clause; 418, 419; 419, block; 419, 420; 420, return_statement; 420, 421; 421, expression_list; 421, 422; 421, 423; 422, identifier:normals; 423, identifier:reciprocals | def split_into_normal_and_reciprocal(self, pad=False,
return_indices=False):
"""Split the stored configurations into normal and reciprocal
measurements
** *Rule 1: the normal configuration contains the smallest electrode
number of the four involved electrodes in the current dipole* **
Parameters
----------
pad: bool, optional
if True, add numpy.nan values to the reciprocals for non-existent
measuremnts
return_indices: bool, optional
if True, also return the indices of normal and reciprocal
measurments. This can be used to extract corresponding
measurements.
Returns
-------
normal: numpy.ndarray
Nnx4 array. If pad is True, then Nn == N (total number of
unique measurements). Otherwise Nn is the number of normal
measurements.
reciprocal: numpy.ndarray
Nrx4 array. If pad is True, then Nr == N (total number of
unique measurements). Otherwise Nr is the number of reciprocal
measurements.
nor_indices: numpy.ndarray, optional
Nnx1 array containing the indices of normal measurements. Only
returned if return_indices is True.
rec_indices: numpy.ndarray, optional
Nrx1 array containing the indices of normal measurements. Only
returned if return_indices is True.
"""
# for simplicity, we create an array where AB and MN are sorted
configs = np.hstack((np.sort(self.configs[:, 0:2], axis=1),
np.sort(self.configs[:, 2:4], axis=1)))
ab_min = configs[:, 0]
mn_min = configs[:, 2]
# rule 1
indices_normal = np.where(ab_min < mn_min)[0]
# now look for reciprocals
indices_used = []
normal = []
normal_indices = []
reciprocal_indices = []
reciprocal = []
duplicates = []
for index in indices_normal:
indices_used.append(index)
normal.append(self.configs[index, :])
normal_indices.append(index)
# look for reciprocal configuration
index_rec = np.where(
# A == M, B == N, M == A, N == B
(configs[:, 0] == configs[index, 2]) &
(configs[:, 1] == configs[index, 3]) &
(configs[:, 2] == configs[index, 0]) &
(configs[:, 3] == configs[index, 1]))[0]
if len(index_rec) == 0 and pad:
reciprocal.append(np.ones(4) * np.nan)
elif len(index_rec) == 1:
reciprocal.append(self.configs[index_rec[0], :])
indices_used.append(index_rec[0])
reciprocal_indices.append(index_rec[0])
elif len(index_rec > 1):
# take the first one
reciprocal.append(self.configs[index_rec[0], :])
reciprocal_indices.append(index_rec[0])
duplicates += list(index_rec[1:])
indices_used += list(index_rec)
# now determine all reciprocal-only parameters
set_all_indices = set(list(range(0, configs.shape[0])))
set_used_indices = set(indices_used)
reciprocal_only_indices = set_all_indices - set_used_indices
for index in reciprocal_only_indices:
if pad:
normal.append(np.ones(4) * np.nan)
reciprocal.append(self.configs[index, :])
normals = np.array(normal)
reciprocals = np.array(reciprocal)
if return_indices:
return normals, reciprocals, normal_indices, reciprocal_indices
else:
return normals, reciprocals |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:gen_reciprocals; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:append; 7, False; 8, block; 8, 9; 8, 11; 8, 12; 8, 30; 8, 56; 8, 82; 8, 83; 8, 112; 8, 118; 8, 136; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:reciprocals; 15, subscript; 15, 16; 15, 23; 15, 25; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:configs; 21, identifier:copy; 22, argument_list; 23, slice; 23, 24; 24, colon; 25, slice; 25, 26; 25, 27; 25, 28; 26, colon; 27, colon; 28, unary_operator:-; 28, 29; 29, integer:1; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 40; 32, subscript; 32, 33; 32, 34; 32, 36; 33, identifier:reciprocals; 34, slice; 34, 35; 35, colon; 36, slice; 36, 37; 36, 38; 36, 39; 37, integer:0; 38, colon; 39, integer:2; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:np; 43, identifier:sort; 44, argument_list; 44, 45; 44, 53; 45, subscript; 45, 46; 45, 47; 45, 49; 46, identifier:reciprocals; 47, slice; 47, 48; 48, colon; 49, slice; 49, 50; 49, 51; 49, 52; 50, integer:0; 51, colon; 52, integer:2; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:axis; 55, integer:1; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 66; 58, subscript; 58, 59; 58, 60; 58, 62; 59, identifier:reciprocals; 60, slice; 60, 61; 61, colon; 62, slice; 62, 63; 62, 64; 62, 65; 63, integer:2; 64, colon; 65, integer:4; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:np; 69, identifier:sort; 70, argument_list; 70, 71; 70, 79; 71, subscript; 71, 72; 71, 73; 71, 75; 72, identifier:reciprocals; 73, slice; 73, 74; 74, colon; 75, slice; 75, 76; 75, 77; 75, 78; 76, integer:2; 77, colon; 78, integer:4; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:axis; 81, integer:1; 82, comment; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:ind; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:np; 89, identifier:lexsort; 90, argument_list; 90, 91; 91, tuple; 91, 92; 91, 97; 91, 102; 91, 107; 92, subscript; 92, 93; 92, 94; 92, 96; 93, identifier:reciprocals; 94, slice; 94, 95; 95, colon; 96, integer:3; 97, subscript; 97, 98; 97, 99; 97, 101; 98, identifier:reciprocals; 99, slice; 99, 100; 100, colon; 101, integer:2; 102, subscript; 102, 103; 102, 104; 102, 106; 103, identifier:reciprocals; 104, slice; 104, 105; 105, colon; 106, integer:1; 107, subscript; 107, 108; 107, 109; 107, 111; 108, identifier:reciprocals; 109, slice; 109, 110; 110, colon; 111, integer:0; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:reciprocals; 115, subscript; 115, 116; 115, 117; 116, identifier:reciprocals; 117, identifier:ind; 118, if_statement; 118, 119; 118, 120; 119, identifier:append; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:configs; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:np; 129, identifier:vstack; 130, argument_list; 130, 131; 131, tuple; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:configs; 135, identifier:reciprocals; 136, return_statement; 136, 137; 137, identifier:reciprocals | def gen_reciprocals(self, append=False):
""" Generate reciprocal configurations, sort by AB, and optionally
append to configurations.
Parameters
----------
append : bool
Append reciprocals to configs (the default is False).
Examples
--------
>>> cfgs = ConfigManager(nr_of_electrodes=5)
>>> nor = cfgs.gen_dipole_dipole(skipc=0)
>>> rec = cfgs.gen_reciprocals(append=True)
>>> print(cfgs.configs)
[[1 2 3 4]
[1 2 4 5]
[2 3 4 5]
[3 4 1 2]
[4 5 1 2]
[4 5 2 3]]
"""
# Switch AB and MN
reciprocals = self.configs.copy()[:, ::-1]
reciprocals[:, 0:2] = np.sort(reciprocals[:, 0:2], axis=1)
reciprocals[:, 2:4] = np.sort(reciprocals[:, 2:4], axis=1)
# # Sort by current dipoles
ind = np.lexsort((reciprocals[:, 3], reciprocals[:, 2],
reciprocals[:, 1], reciprocals[:, 0]))
reciprocals = reciprocals[ind]
if append:
self.configs = np.vstack((self.configs, reciprocals))
return reciprocals |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:gen_configs_permutate; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:injections_raw; 6, default_parameter; 6, 7; 6, 8; 7, identifier:only_same_dipole_length; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:ignore_crossed_dipoles; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:silent; 14, False; 15, block; 15, 16; 15, 18; 15, 32; 15, 40; 15, 44; 15, 227; 15, 228; 15, 232; 15, 282; 15, 289; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:injections; 21, call; 21, 22; 21, 30; 22, attribute; 22, 23; 22, 29; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:np; 26, identifier:atleast_2d; 27, argument_list; 27, 28; 28, identifier:injections_raw; 29, identifier:astype; 30, argument_list; 30, 31; 31, identifier:int; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:N; 35, subscript; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:injections; 38, identifier:shape; 39, integer:0; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:measurements; 43, list:[]; 44, for_statement; 44, 45; 44, 46; 44, 51; 45, identifier:injection; 46, call; 46, 47; 46, 48; 47, identifier:range; 48, argument_list; 48, 49; 48, 50; 49, integer:0; 50, identifier:N; 51, block; 51, 52; 51, 71; 51, 72; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:dipole_length; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:np; 58, identifier:abs; 59, argument_list; 59, 60; 60, binary_operator:-; 60, 61; 60, 66; 61, subscript; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:injections; 64, identifier:injection; 65, integer:1; 66, subscript; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:injections; 69, identifier:injection; 70, integer:0; 71, comment; 72, for_statement; 72, 73; 72, 74; 72, 88; 73, identifier:i; 74, binary_operator:-; 74, 75; 74, 83; 75, call; 75, 76; 75, 77; 76, identifier:set; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:range; 80, argument_list; 80, 81; 80, 82; 81, integer:0; 82, identifier:N; 83, call; 83, 84; 83, 85; 84, identifier:set; 85, argument_list; 85, 86; 86, list:[injection]; 86, 87; 87, identifier:injection; 88, block; 88, 89; 88, 112; 88, 121; 88, 144; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:test_dipole_length; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:np; 95, identifier:abs; 96, argument_list; 96, 97; 97, binary_operator:-; 97, 98; 97, 105; 98, subscript; 98, 99; 98, 104; 99, subscript; 99, 100; 99, 101; 99, 102; 100, identifier:injections; 101, identifier:i; 102, slice; 102, 103; 103, colon; 104, integer:1; 105, subscript; 105, 106; 105, 111; 106, subscript; 106, 107; 106, 108; 106, 109; 107, identifier:injections; 108, identifier:i; 109, slice; 109, 110; 110, colon; 111, integer:0; 112, if_statement; 112, 113; 112, 119; 113, parenthesized_expression; 113, 114; 114, boolean_operator:and; 114, 115; 114, 116; 115, identifier:only_same_dipole_length; 116, comparison_operator:!=; 116, 117; 116, 118; 117, identifier:test_dipole_length; 118, identifier:dipole_length; 119, block; 119, 120; 120, continue_statement; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:quadpole; 124, call; 124, 125; 124, 143; 125, attribute; 125, 126; 125, 142; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:np; 129, identifier:array; 130, argument_list; 130, 131; 131, list:[injections[injection, :], injections[i, :]]; 131, 132; 131, 137; 132, subscript; 132, 133; 132, 134; 132, 135; 133, identifier:injections; 134, identifier:injection; 135, slice; 135, 136; 136, colon; 137, subscript; 137, 138; 137, 139; 137, 140; 138, identifier:injections; 139, identifier:i; 140, slice; 140, 141; 141, colon; 142, identifier:flatten; 143, argument_list; 144, if_statement; 144, 145; 144, 148; 144, 149; 144, 150; 144, 151; 144, 217; 145, comparison_operator:is; 145, 146; 145, 147; 146, identifier:ignore_crossed_dipoles; 147, True; 148, comment; 149, comment; 150, comment; 151, block; 151, 152; 152, if_statement; 152, 153; 152, 169; 152, 180; 152, 208; 153, parenthesized_expression; 153, 154; 154, boolean_operator:and; 154, 155; 154, 162; 155, comparison_operator:>; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:quadpole; 158, integer:2; 159, subscript; 159, 160; 159, 161; 160, identifier:quadpole; 161, integer:0; 162, comparison_operator:<; 162, 163; 162, 166; 163, subscript; 163, 164; 163, 165; 164, identifier:quadpole; 165, integer:2; 166, subscript; 166, 167; 166, 168; 167, identifier:quadpole; 168, integer:1; 169, block; 169, 170; 170, if_statement; 170, 171; 170, 173; 171, not_operator; 171, 172; 172, identifier:silent; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 177; 176, identifier:print; 177, argument_list; 177, 178; 177, 179; 178, string:'A - ignoring'; 179, identifier:quadpole; 180, elif_clause; 180, 181; 180, 197; 181, parenthesized_expression; 181, 182; 182, boolean_operator:and; 182, 183; 182, 190; 183, comparison_operator:>; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:quadpole; 186, integer:3; 187, subscript; 187, 188; 187, 189; 188, identifier:quadpole; 189, integer:0; 190, comparison_operator:<; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:quadpole; 193, integer:3; 194, subscript; 194, 195; 194, 196; 195, identifier:quadpole; 196, integer:1; 197, block; 197, 198; 198, if_statement; 198, 199; 198, 201; 199, not_operator; 199, 200; 200, identifier:silent; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:print; 205, argument_list; 205, 206; 205, 207; 206, string:'B - ignoring'; 207, identifier:quadpole; 208, else_clause; 208, 209; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:measurements; 214, identifier:append; 215, argument_list; 215, 216; 216, identifier:quadpole; 217, else_clause; 217, 218; 217, 219; 218, comment; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:measurements; 224, identifier:append; 225, argument_list; 225, 226; 226, identifier:quadpole; 227, comment; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:filtered; 231, list:[]; 232, for_statement; 232, 233; 232, 234; 232, 235; 233, identifier:quadpole; 234, identifier:measurements; 235, block; 235, 236; 236, if_statement; 236, 237; 236, 261; 236, 273; 237, parenthesized_expression; 237, 238; 238, not_operator; 238, 239; 239, call; 239, 240; 239, 251; 240, attribute; 240, 241; 240, 250; 241, call; 241, 242; 241, 243; 242, identifier:set; 243, argument_list; 243, 244; 244, subscript; 244, 245; 244, 246; 245, identifier:quadpole; 246, slice; 246, 247; 246, 248; 246, 249; 247, integer:0; 248, colon; 249, integer:2; 250, identifier:isdisjoint; 251, argument_list; 251, 252; 252, call; 252, 253; 252, 254; 253, identifier:set; 254, argument_list; 254, 255; 255, subscript; 255, 256; 255, 257; 256, identifier:quadpole; 257, slice; 257, 258; 257, 259; 257, 260; 258, integer:2; 259, colon; 260, integer:4; 261, block; 261, 262; 262, if_statement; 262, 263; 262, 265; 263, not_operator; 263, 264; 264, identifier:silent; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 269; 268, identifier:print; 269, argument_list; 269, 270; 269, 271; 269, 272; 270, string:'Ignoring quadrupole because of '; 271, string:'repeated electrode use:'; 272, identifier:quadpole; 273, else_clause; 273, 274; 274, block; 274, 275; 275, expression_statement; 275, 276; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:filtered; 279, identifier:append; 280, argument_list; 280, 281; 281, identifier:quadpole; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:add_to_configs; 287, argument_list; 287, 288; 288, identifier:filtered; 289, return_statement; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:np; 293, identifier:array; 294, argument_list; 294, 295; 295, identifier:filtered | def gen_configs_permutate(self,
injections_raw,
only_same_dipole_length=False,
ignore_crossed_dipoles=False,
silent=False):
""" Create measurement configurations out of a pool of current
injections. Use only the provided dipoles for potential dipole
selection. This means that we have always reciprocal measurements.
Remove quadpoles where electrodes are used both as current and voltage
dipoles.
Parameters
----------
injections_raw : Nx2 array
current injections
only_same_dipole_length : bool, optional
if True, only generate permutations for the same dipole length
ignore_crossed_dipoles : bool, optional
If True, potential dipoles will be ignored that lie between current
dipoles, e.g. 1-4 3-5. In this case it is possible to not have
full normal-reciprocal coverage.
silent: bool, optional
if True, do not print information on ignored configs (default:
False)
Returns
-------
configs : Nx4 array
quadrupoles generated out of the current injections
"""
injections = np.atleast_2d(injections_raw).astype(int)
N = injections.shape[0]
measurements = []
for injection in range(0, N):
dipole_length = np.abs(injections[injection][1] -
injections[injection][0])
# select all dipole EXCEPT for the injection dipole
for i in set(range(0, N)) - set([injection]):
test_dipole_length = np.abs(injections[i, :][1] -
injections[i, :][0])
if (only_same_dipole_length
and test_dipole_length != dipole_length):
continue
quadpole = np.array(
[injections[injection, :], injections[i, :]]).flatten()
if ignore_crossed_dipoles is True:
# check if we need to ignore this dipole
# Note: this could be wrong if electrode number are not
# ascending!
if (quadpole[2] > quadpole[0]
and quadpole[2] < quadpole[1]):
if not silent:
print('A - ignoring', quadpole)
elif (quadpole[3] > quadpole[0]
and quadpole[3] < quadpole[1]):
if not silent:
print('B - ignoring', quadpole)
else:
measurements.append(quadpole)
else:
# add very quadpole
measurements.append(quadpole)
# check and remove double use of electrodes
filtered = []
for quadpole in measurements:
if (not set(quadpole[0:2]).isdisjoint(set(quadpole[2:4]))):
if not silent:
print('Ignoring quadrupole because of ',
'repeated electrode use:', quadpole)
else:
filtered.append(quadpole)
self.add_to_configs(filtered)
return np.array(filtered) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:to_pg_scheme; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:container; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:positions; 10, None; 11, block; 11, 12; 11, 14; 11, 28; 11, 44; 11, 58; 11, 80; 11, 88; 11, 91; 11, 92; 11, 100; 11, 101; 11, 123; 11, 124; 11, 137; 11, 164; 11, 165; 11, 182; 11, 183; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 22; 15, boolean_operator:and; 15, 16; 15, 19; 16, comparison_operator:is; 16, 17; 16, 18; 17, identifier:container; 18, None; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:positions; 21, None; 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:'electrode positions are required for BERT export'; 28, if_statement; 28, 29; 28, 38; 29, boolean_operator:and; 29, 30; 29, 33; 30, comparison_operator:is; 30, 31; 30, 32; 31, identifier:container; 32, None; 33, comparison_operator:is; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:container; 36, identifier:electrodes; 37, None; 38, block; 38, 39; 39, raise_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:Exception; 42, argument_list; 42, 43; 43, string:'container does not contain electrode positions'; 44, if_statement; 44, 45; 44, 52; 45, boolean_operator:and; 45, 46; 45, 49; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:container; 48, None; 49, comparison_operator:is; 49, 50; 49, 51; 50, identifier:positions; 51, None; 52, block; 52, 53; 53, raise_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:Exception; 56, argument_list; 56, 57; 57, string:'only one of container OR positions must be provided'; 58, if_statement; 58, 59; 58, 62; 58, 71; 59, comparison_operator:is; 59, 60; 59, 61; 60, identifier:container; 61, None; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:elec_positions; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:container; 69, identifier:electrodes; 70, identifier:values; 71, elif_clause; 71, 72; 71, 75; 72, comparison_operator:is; 72, 73; 72, 74; 73, identifier:positions; 74, None; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:elec_positions; 79, identifier:positions; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:opt_import; 83, argument_list; 83, 84; 83, 85; 84, string:"pybert"; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:requiredFor; 87, string:""; 88, import_statement; 88, 89; 89, dotted_name; 89, 90; 90, identifier:pybert; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:data; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:pybert; 98, identifier:DataContainerERT; 99, argument_list; 100, comment; 101, for_statement; 101, 102; 101, 108; 101, 112; 102, pattern_list; 102, 103; 102, 104; 103, identifier:nr; 104, tuple_pattern; 104, 105; 104, 106; 104, 107; 105, identifier:x; 106, identifier:y; 107, identifier:z; 108, call; 108, 109; 108, 110; 109, identifier:enumerate; 110, argument_list; 110, 111; 111, identifier:elec_positions; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:data; 117, identifier:createSensor; 118, argument_list; 118, 119; 119, tuple; 119, 120; 119, 121; 119, 122; 120, identifier:x; 121, identifier:y; 122, identifier:z; 123, comment; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:data; 128, identifier:resize; 129, argument_list; 129, 130; 130, subscript; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:configs; 135, identifier:shape; 136, integer:0; 137, for_statement; 137, 138; 137, 141; 137, 145; 138, pattern_list; 138, 139; 138, 140; 139, identifier:index; 140, identifier:token; 141, call; 141, 142; 141, 143; 142, identifier:enumerate; 143, argument_list; 143, 144; 144, string:"abmn"; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:data; 150, identifier:set; 151, argument_list; 151, 152; 151, 153; 152, identifier:token; 153, call; 153, 154; 153, 163; 154, attribute; 154, 155; 154, 162; 155, subscript; 155, 156; 155, 159; 155, 161; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:configs; 159, slice; 159, 160; 160, colon; 161, identifier:index; 162, identifier:tolist; 163, argument_list; 164, comment; 165, for_statement; 165, 166; 165, 167; 165, 168; 166, identifier:token; 167, string:"abmn"; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:data; 173, identifier:set; 174, argument_list; 174, 175; 174, 176; 175, identifier:token; 176, binary_operator:-; 176, 177; 176, 181; 177, call; 177, 178; 177, 179; 178, identifier:data; 179, argument_list; 179, 180; 180, identifier:token; 181, integer:1; 182, comment; 183, return_statement; 183, 184; 184, identifier:data | def to_pg_scheme(self, container=None, positions=None):
"""Convert the configuration to a pygimli measurement scheme
Parameters
----------
container: reda.containers.ERT.ERT
an ERT data container (we take the electrode positions from here)
positions = None
Returns
-------
data: pybert.DataContainerERT
Examples
--------
import numpy as np
from reda.configs.configManager import ConfigManager
configs = ConfigManager(nr_of_electrodes=48)
new_configs = configs.gen_dipole_dipole(skipc=2)
x = np.arange(0, 48, 1)
z = np.ones(48) * -1
y = np.zeros(48)
xyz = np.vstack((x, y, z)).T
scheme = configs.to_pg_scheme(positions=xyz)
print(scheme)
"""
if container is None and positions is None:
raise Exception('electrode positions are required for BERT export')
if container is not None and container.electrodes is None:
raise Exception('container does not contain electrode positions')
if container is not None and positions is not None:
raise Exception(
'only one of container OR positions must be provided')
if container is not None:
elec_positions = container.electrodes.values
elif positions is not None:
elec_positions = positions
opt_import("pybert", requiredFor="")
import pybert
# Initialize BERT DataContainer
data = pybert.DataContainerERT()
# Define electrodes (48 electrodes spaced by 0.5 m)
for nr, (x, y, z) in enumerate(elec_positions):
data.createSensor((x, y, z))
# Define number of measurements
data.resize(self.configs.shape[0])
for index, token in enumerate("abmn"):
data.set(token, self.configs[:, index].tolist())
# account for zero indexing
for token in "abmn":
data.set(token, data(token) - 1)
# np.vstack([data.get(x).array() for x in ("abmn")]).T
return data |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:try_sending; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:msg; 6, identifier:timeout_secs; 7, identifier:max_attempts; 8, block; 8, 9; 8, 11; 8, 22; 8, 33; 8, 37; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:is; 12, 13; 12, 14; 13, identifier:timeout_secs; 14, None; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:timeout_secs; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:timeout; 22, if_statement; 22, 23; 22, 26; 23, comparison_operator:is; 23, 24; 23, 25; 24, identifier:max_attempts; 25, None; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:max_attempts; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:retry_count; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:attempts; 36, integer:0; 37, while_statement; 37, 38; 37, 41; 38, comparison_operator:<; 38, 39; 38, 40; 39, identifier:attempts; 40, identifier:max_attempts; 41, block; 41, 42; 41, 52; 41, 60; 41, 72; 41, 76; 41, 92; 42, if_statement; 42, 43; 42, 50; 43, comparison_operator:not; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:msg; 46, identifier:seq_num; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:message; 50, block; 50, 51; 51, return_statement; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:event; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:aio; 58, identifier:Event; 59, argument_list; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 71; 62, subscript; 62, 63; 62, 70; 63, subscript; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:message; 67, attribute; 67, 68; 67, 69; 68, identifier:msg; 69, identifier:seq_num; 70, integer:1; 71, identifier:event; 72, expression_statement; 72, 73; 73, augmented_assignment:+=; 73, 74; 73, 75; 74, identifier:attempts; 75, integer:1; 76, if_statement; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:transport; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:transport; 87, identifier:sendto; 88, argument_list; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:msg; 91, identifier:packed_message; 92, try_statement; 92, 93; 92, 110; 93, block; 93, 94; 93, 109; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:myresult; 97, await; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:aio; 101, identifier:wait_for; 102, argument_list; 102, 103; 102, 108; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:event; 106, identifier:wait; 107, argument_list; 108, identifier:timeout_secs; 109, break_statement; 110, except_clause; 110, 111; 110, 115; 111, as_pattern; 111, 112; 111, 113; 112, identifier:Exception; 113, as_pattern_target; 113, 114; 114, identifier:inst; 115, block; 115, 116; 116, if_statement; 116, 117; 116, 120; 117, comparison_operator:>=; 117, 118; 117, 119; 118, identifier:attempts; 119, identifier:max_attempts; 120, block; 120, 121; 120, 160; 120, 161; 121, if_statement; 121, 122; 121, 129; 122, comparison_operator:in; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:msg; 125, identifier:seq_num; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:message; 129, block; 129, 130; 129, 142; 129, 151; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:callb; 133, subscript; 133, 134; 133, 141; 134, subscript; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:message; 138, attribute; 138, 139; 138, 140; 139, identifier:msg; 140, identifier:seq_num; 141, integer:2; 142, if_statement; 142, 143; 142, 144; 143, identifier:callb; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 148; 147, identifier:callb; 148, argument_list; 148, 149; 148, 150; 149, identifier:self; 150, None; 151, delete_statement; 151, 152; 152, parenthesized_expression; 152, 153; 153, subscript; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:message; 157, attribute; 157, 158; 157, 159; 158, identifier:msg; 159, identifier:seq_num; 160, comment; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:self; 165, identifier:unregister; 166, argument_list | async def try_sending(self,msg,timeout_secs, max_attempts):
"""Coroutine used to send message to the device when a response or ack is needed.
This coroutine will try to send up to max_attempts time the message, waiting timeout_secs
for an answer. If no answer is received, it will consider that the device is no longer
accessible and will unregister it.
:param msg: The message to send
:type msg: aiolifx.Message
:param timeout_secs: Number of seconds to wait for a response or ack
:type timeout_secs: int
:param max_attempts: .
:type max_attempts: int
:returns: a coroutine to be scheduled
:rtype: coroutine
"""
if timeout_secs is None:
timeout_secs = self.timeout
if max_attempts is None:
max_attempts = self.retry_count
attempts = 0
while attempts < max_attempts:
if msg.seq_num not in self.message: return
event = aio.Event()
self.message[msg.seq_num][1]= event
attempts += 1
if self.transport:
self.transport.sendto(msg.packed_message)
try:
myresult = await aio.wait_for(event.wait(),timeout_secs)
break
except Exception as inst:
if attempts >= max_attempts:
if msg.seq_num in self.message:
callb = self.message[msg.seq_num][2]
if callb:
callb(self, None)
del(self.message[msg.seq_num])
#It's dead Jim
self.unregister() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:apply_correction_factors; 3, parameters; 3, 4; 3, 5; 4, identifier:df; 5, identifier:correction_file; 6, block; 6, 7; 6, 9; 6, 160; 6, 186; 6, 212; 6, 224; 6, 232; 6, 245; 6, 421; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 17; 9, 36; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 14; 13, identifier:correction_file; 14, tuple; 14, 15; 14, 16; 15, identifier:list; 16, identifier:tuple; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:corr_data_raw; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:np; 24, identifier:vstack; 25, argument_list; 25, 26; 26, list_comprehension; 26, 27; 26, 33; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:np; 30, identifier:loadtxt; 31, argument_list; 31, 32; 32, identifier:x; 33, for_in_clause; 33, 34; 33, 35; 34, identifier:x; 35, identifier:correction_file; 36, else_clause; 36, 37; 37, block; 37, 38; 37, 47; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:corr_data_raw; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:np; 44, identifier:loadtxt; 45, argument_list; 45, 46; 46, identifier:correction_file; 47, if_statement; 47, 48; 47, 55; 47, 140; 47, 153; 48, comparison_operator:==; 48, 49; 48, 54; 49, subscript; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:corr_data_raw; 52, identifier:shape; 53, integer:1; 54, integer:3; 55, block; 55, 56; 55, 72; 55, 88; 55, 104; 55, 120; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:A; 59, call; 59, 60; 59, 70; 60, attribute; 60, 61; 60, 69; 61, parenthesized_expression; 61, 62; 62, binary_operator:/; 62, 63; 62, 68; 63, subscript; 63, 64; 63, 65; 63, 67; 64, identifier:corr_data_raw; 65, slice; 65, 66; 66, colon; 67, integer:0; 68, float:1e4; 69, identifier:astype; 70, argument_list; 70, 71; 71, identifier:int; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:B; 75, call; 75, 76; 75, 86; 76, attribute; 76, 77; 76, 85; 77, parenthesized_expression; 77, 78; 78, binary_operator:%; 78, 79; 78, 84; 79, subscript; 79, 80; 79, 81; 79, 83; 80, identifier:corr_data_raw; 81, slice; 81, 82; 82, colon; 83, integer:0; 84, float:1e4; 85, identifier:astype; 86, argument_list; 86, 87; 87, identifier:int; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:M; 91, call; 91, 92; 91, 102; 92, attribute; 92, 93; 92, 101; 93, parenthesized_expression; 93, 94; 94, binary_operator:/; 94, 95; 94, 100; 95, subscript; 95, 96; 95, 97; 95, 99; 96, identifier:corr_data_raw; 97, slice; 97, 98; 98, colon; 99, integer:1; 100, float:1e4; 101, identifier:astype; 102, argument_list; 102, 103; 103, identifier:int; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:N; 107, call; 107, 108; 107, 118; 108, attribute; 108, 109; 108, 117; 109, parenthesized_expression; 109, 110; 110, binary_operator:%; 110, 111; 110, 116; 111, subscript; 111, 112; 111, 113; 111, 115; 112, identifier:corr_data_raw; 113, slice; 113, 114; 114, colon; 115, integer:1; 116, float:1e4; 117, identifier:astype; 118, argument_list; 118, 119; 119, identifier:int; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:corr_data; 123, attribute; 123, 124; 123, 139; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:np; 127, identifier:vstack; 128, argument_list; 128, 129; 129, tuple; 129, 130; 129, 131; 129, 132; 129, 133; 129, 134; 130, identifier:A; 131, identifier:B; 132, identifier:M; 133, identifier:N; 134, subscript; 134, 135; 134, 136; 134, 138; 135, identifier:corr_data_raw; 136, slice; 136, 137; 137, colon; 138, integer:2; 139, identifier:T; 140, elif_clause; 140, 141; 140, 148; 141, comparison_operator:==; 141, 142; 141, 147; 142, subscript; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:corr_data_raw; 145, identifier:shape; 146, integer:1; 147, integer:5; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:corr_data; 152, identifier:corr_data_raw; 153, else_clause; 153, 154; 154, block; 154, 155; 155, raise_statement; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:Exception; 158, argument_list; 158, 159; 159, string:'error'; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 170; 162, subscript; 162, 163; 162, 164; 162, 166; 163, identifier:corr_data; 164, slice; 164, 165; 165, colon; 166, slice; 166, 167; 166, 168; 166, 169; 167, integer:0; 168, colon; 169, integer:2; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:np; 173, identifier:sort; 174, argument_list; 174, 175; 174, 183; 175, subscript; 175, 176; 175, 177; 175, 179; 176, identifier:corr_data; 177, slice; 177, 178; 178, colon; 179, slice; 179, 180; 179, 181; 179, 182; 180, integer:0; 181, colon; 182, integer:2; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:axis; 185, integer:1; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 196; 188, subscript; 188, 189; 188, 190; 188, 192; 189, identifier:corr_data; 190, slice; 190, 191; 191, colon; 192, slice; 192, 193; 192, 194; 192, 195; 193, integer:2; 194, colon; 195, integer:4; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:np; 199, identifier:sort; 200, argument_list; 200, 201; 200, 209; 201, subscript; 201, 202; 201, 203; 201, 205; 202, identifier:corr_data; 203, slice; 203, 204; 204, colon; 205, slice; 205, 206; 205, 207; 205, 208; 206, integer:2; 207, colon; 208, integer:4; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:axis; 211, integer:1; 212, if_statement; 212, 213; 212, 218; 213, comparison_operator:not; 213, 214; 213, 215; 214, string:'frequency'; 215, attribute; 215, 216; 215, 217; 216, identifier:df; 217, identifier:columns; 218, block; 218, 219; 219, raise_statement; 219, 220; 220, call; 220, 221; 220, 222; 221, identifier:Exception; 222, argument_list; 222, 223; 223, string:'No frequency data found. Are you sure this is a seit data set?'; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:df; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:df; 230, identifier:reset_index; 231, argument_list; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:gf; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:df; 238, identifier:groupby; 239, argument_list; 239, 240; 240, list:['a', 'b', 'm', 'n']; 240, 241; 240, 242; 240, 243; 240, 244; 241, string:'a'; 242, string:'b'; 243, string:'m'; 244, string:'n'; 245, for_statement; 245, 246; 245, 249; 245, 256; 245, 257; 245, 258; 246, pattern_list; 246, 247; 246, 248; 247, identifier:key; 248, identifier:item; 249, call; 249, 250; 249, 255; 250, attribute; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:gf; 253, identifier:indices; 254, identifier:items; 255, argument_list; 256, comment; 257, comment; 258, block; 258, 259; 258, 290; 258, 291; 258, 344; 258, 345; 258, 372; 258, 379; 258, 380; 258, 381; 258, 382; 258, 383; 258, 384; 258, 385; 258, 386; 258, 387; 258, 388; 258, 412; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:item_norm; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:np; 265, identifier:hstack; 266, argument_list; 266, 267; 267, tuple; 267, 268; 267, 279; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:np; 271, identifier:sort; 272, argument_list; 272, 273; 273, subscript; 273, 274; 273, 275; 274, identifier:key; 275, slice; 275, 276; 275, 277; 275, 278; 276, integer:0; 277, colon; 278, integer:2; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:np; 282, identifier:sort; 283, argument_list; 283, 284; 284, subscript; 284, 285; 284, 286; 285, identifier:key; 286, slice; 286, 287; 286, 288; 286, 289; 287, integer:2; 288, colon; 289, integer:4; 290, comment; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:index; 294, subscript; 294, 295; 294, 343; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:np; 298, identifier:where; 299, argument_list; 299, 300; 300, binary_operator:&; 300, 301; 300, 333; 301, binary_operator:&; 301, 302; 301, 323; 302, binary_operator:&; 302, 303; 302, 313; 303, parenthesized_expression; 303, 304; 304, comparison_operator:==; 304, 305; 304, 310; 305, subscript; 305, 306; 305, 307; 305, 309; 306, identifier:corr_data; 307, slice; 307, 308; 308, colon; 309, integer:0; 310, subscript; 310, 311; 310, 312; 311, identifier:item_norm; 312, integer:0; 313, parenthesized_expression; 313, 314; 314, comparison_operator:==; 314, 315; 314, 320; 315, subscript; 315, 316; 315, 317; 315, 319; 316, identifier:corr_data; 317, slice; 317, 318; 318, colon; 319, integer:1; 320, subscript; 320, 321; 320, 322; 321, identifier:item_norm; 322, integer:1; 323, parenthesized_expression; 323, 324; 324, comparison_operator:==; 324, 325; 324, 330; 325, subscript; 325, 326; 325, 327; 325, 329; 326, identifier:corr_data; 327, slice; 327, 328; 328, colon; 329, integer:2; 330, subscript; 330, 331; 330, 332; 331, identifier:item_norm; 332, integer:2; 333, parenthesized_expression; 333, 334; 334, comparison_operator:==; 334, 335; 334, 340; 335, subscript; 335, 336; 335, 337; 335, 339; 336, identifier:corr_data; 337, slice; 337, 338; 338, colon; 339, integer:3; 340, subscript; 340, 341; 340, 342; 341, identifier:item_norm; 342, integer:3; 343, integer:0; 344, comment; 345, if_statement; 345, 346; 345, 352; 346, comparison_operator:==; 346, 347; 346, 351; 347, call; 347, 348; 347, 349; 348, identifier:len; 349, argument_list; 349, 350; 350, identifier:index; 351, integer:0; 352, block; 352, 353; 352, 358; 352, 361; 352, 367; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 356; 355, identifier:print; 356, argument_list; 356, 357; 357, identifier:key; 358, import_statement; 358, 359; 359, dotted_name; 359, 360; 360, identifier:IPython; 361, expression_statement; 361, 362; 362, call; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:IPython; 365, identifier:embed; 366, argument_list; 367, raise_statement; 367, 368; 368, call; 368, 369; 368, 370; 369, identifier:Exception; 370, argument_list; 370, 371; 371, string:'No correction factor found for this configuration'; 372, expression_statement; 372, 373; 373, assignment; 373, 374; 373, 375; 374, identifier:factor; 375, subscript; 375, 376; 375, 377; 375, 378; 376, identifier:corr_data; 377, identifier:index; 378, integer:4; 379, comment; 380, comment; 381, comment; 382, comment; 383, comment; 384, comment; 385, comment; 386, comment; 387, comment; 388, for_statement; 388, 389; 388, 390; 388, 395; 389, identifier:col; 390, tuple; 390, 391; 390, 392; 390, 393; 390, 394; 391, string:'r'; 392, string:'Zt'; 393, string:'Vmn'; 394, string:'rho_a'; 395, block; 395, 396; 396, if_statement; 396, 397; 396, 402; 397, comparison_operator:in; 397, 398; 397, 399; 398, identifier:col; 399, attribute; 399, 400; 399, 401; 400, identifier:df; 401, identifier:columns; 402, block; 402, 403; 403, expression_statement; 403, 404; 404, augmented_assignment:*=; 404, 405; 404, 411; 405, subscript; 405, 406; 405, 409; 405, 410; 406, attribute; 406, 407; 406, 408; 407, identifier:df; 408, identifier:ix; 409, identifier:item; 410, identifier:col; 411, identifier:factor; 412, expression_statement; 412, 413; 413, assignment; 413, 414; 413, 420; 414, subscript; 414, 415; 414, 418; 414, 419; 415, attribute; 415, 416; 415, 417; 416, identifier:df; 417, identifier:ix; 418, identifier:item; 419, string:'corr_fac'; 420, identifier:factor; 421, return_statement; 421, 422; 422, expression_list; 422, 423; 422, 424; 423, identifier:df; 424, identifier:corr_data | def apply_correction_factors(df, correction_file):
"""Apply correction factors for a pseudo-2D measurement setup. See Weigand
and Kemna, 2017, Biogeosciences, for detailed information.
"""
if isinstance(correction_file, (list, tuple)):
corr_data_raw = np.vstack(
[np.loadtxt(x) for x in correction_file]
)
else:
corr_data_raw = np.loadtxt(correction_file)
if corr_data_raw.shape[1] == 3:
A = (corr_data_raw[:, 0] / 1e4).astype(int)
B = (corr_data_raw[:, 0] % 1e4).astype(int)
M = (corr_data_raw[:, 1] / 1e4).astype(int)
N = (corr_data_raw[:, 1] % 1e4).astype(int)
corr_data = np.vstack((A, B, M, N, corr_data_raw[:, 2])).T
elif corr_data_raw.shape[1] == 5:
corr_data = corr_data_raw
else:
raise Exception('error')
corr_data[:, 0:2] = np.sort(corr_data[:, 0:2], axis=1)
corr_data[:, 2:4] = np.sort(corr_data[:, 2:4], axis=1)
if 'frequency' not in df.columns:
raise Exception(
'No frequency data found. Are you sure this is a seit data set?'
)
df = df.reset_index()
gf = df.groupby(['a', 'b', 'm', 'n'])
for key, item in gf.indices.items():
# print('key', key)
# print(item)
item_norm = np.hstack((np.sort(key[0:2]), np.sort(key[2:4])))
# print(item_norm)
index = np.where(
(corr_data[:, 0] == item_norm[0]) &
(corr_data[:, 1] == item_norm[1]) &
(corr_data[:, 2] == item_norm[2]) &
(corr_data[:, 3] == item_norm[3])
)[0]
# print(index, corr_data[index])
if len(index) == 0:
print(key)
import IPython
IPython.embed()
raise Exception(
'No correction factor found for this configuration'
)
factor = corr_data[index, 4]
# if key == (1, 4, 2, 3):
# print(key)
# print(factor)
# print(df['R'])
# print(df['k'])
# import IPython
# IPython.embed()
# exit()
# apply correction factor
for col in ('r', 'Zt', 'Vmn', 'rho_a'):
if col in df.columns:
df.ix[item, col] *= factor
df.ix[item, 'corr_fac'] = factor
return df, corr_data |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:alter_zero_tip_allowed_states; 3, parameters; 3, 4; 3, 5; 4, identifier:tree; 5, identifier:feature; 6, block; 6, 7; 6, 9; 6, 16; 6, 24; 6, 64; 6, 65; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:zero_parent2tips; 12, call; 12, 13; 12, 14; 13, identifier:defaultdict; 14, argument_list; 14, 15; 15, identifier:list; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:allowed_state_feature; 19, call; 19, 20; 19, 21; 20, identifier:get_personalized_feature_name; 21, argument_list; 21, 22; 21, 23; 22, identifier:feature; 23, identifier:ALLOWED_STATES; 24, for_statement; 24, 25; 24, 26; 24, 27; 25, identifier:tip; 26, identifier:tree; 27, block; 27, 28; 28, if_statement; 28, 29; 28, 34; 29, comparison_operator:==; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:tip; 32, identifier:dist; 33, integer:0; 34, block; 34, 35; 34, 44; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:state; 38, call; 38, 39; 38, 40; 39, identifier:getattr; 40, argument_list; 40, 41; 40, 42; 40, 43; 41, identifier:tip; 42, identifier:feature; 43, None; 44, if_statement; 44, 45; 44, 52; 45, boolean_operator:and; 45, 46; 45, 49; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:state; 48, None; 49, comparison_operator:!=; 49, 50; 49, 51; 50, identifier:state; 51, string:''; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 62; 55, attribute; 55, 56; 55, 61; 56, subscript; 56, 57; 56, 58; 57, identifier:zero_parent2tips; 58, attribute; 58, 59; 58, 60; 59, identifier:tip; 60, identifier:up; 61, identifier:append; 62, argument_list; 62, 63; 63, identifier:tip; 64, comment; 65, for_statement; 65, 66; 65, 69; 65, 74; 65, 75; 66, pattern_list; 66, 67; 66, 68; 67, identifier:parent; 68, identifier:zero_tips; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:zero_parent2tips; 72, identifier:items; 73, argument_list; 74, comment; 75, block; 75, 76; 75, 80; 75, 111; 75, 124; 75, 125; 75, 129; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:counts; 79, None; 80, for_statement; 80, 81; 80, 82; 80, 83; 81, identifier:tip; 82, identifier:zero_tips; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 88; 84, 101; 85, comparison_operator:is; 85, 86; 85, 87; 86, identifier:counts; 87, None; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:counts; 92, call; 92, 93; 92, 100; 93, attribute; 93, 94; 93, 99; 94, call; 94, 95; 94, 96; 95, identifier:getattr; 96, argument_list; 96, 97; 96, 98; 97, identifier:tip; 98, identifier:allowed_state_feature; 99, identifier:copy; 100, argument_list; 101, else_clause; 101, 102; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, augmented_assignment:+=; 104, 105; 104, 106; 105, identifier:counts; 106, call; 106, 107; 106, 108; 107, identifier:getattr; 108, argument_list; 108, 109; 108, 110; 109, identifier:tip; 110, identifier:allowed_state_feature; 111, if_statement; 111, 112; 111, 122; 112, comparison_operator:==; 112, 113; 112, 118; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:counts; 116, identifier:max; 117, argument_list; 118, call; 118, 119; 118, 120; 119, identifier:len; 120, argument_list; 120, 121; 121, identifier:zero_tips; 122, block; 122, 123; 123, continue_statement; 124, comment; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:allowed_states; 128, None; 129, for_statement; 129, 130; 129, 131; 129, 132; 130, identifier:tip; 131, identifier:zero_tips; 132, block; 132, 133; 132, 171; 133, if_statement; 133, 134; 133, 137; 133, 150; 134, comparison_operator:is; 134, 135; 134, 136; 135, identifier:allowed_states; 136, None; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:allowed_states; 141, call; 141, 142; 141, 149; 142, attribute; 142, 143; 142, 148; 143, call; 143, 144; 143, 145; 144, identifier:getattr; 145, argument_list; 145, 146; 145, 147; 146, identifier:tip; 147, identifier:allowed_state_feature; 148, identifier:copy; 149, argument_list; 150, else_clause; 150, 151; 151, block; 151, 152; 151, 160; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:tip_allowed_states; 155, call; 155, 156; 155, 157; 156, identifier:getattr; 157, argument_list; 157, 158; 157, 159; 158, identifier:tip; 159, identifier:allowed_state_feature; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 170; 162, subscript; 162, 163; 162, 164; 163, identifier:allowed_states; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:np; 167, identifier:nonzero; 168, argument_list; 168, 169; 169, identifier:tip_allowed_states; 170, integer:1; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:tip; 175, identifier:add_feature; 176, argument_list; 176, 177; 176, 178; 177, identifier:allowed_state_feature; 178, identifier:allowed_states | def alter_zero_tip_allowed_states(tree, feature):
"""
Alters the bottom-up likelihood arrays for zero-distance tips
to make sure they do not contradict with other zero-distance tip siblings.
:param tree: ete3.Tree, the tree of interest
:param feature: str, character for which the likelihood is altered
:return: void, modifies the get_personalised_feature_name(feature, BU_LH) feature to zero-distance tips.
"""
zero_parent2tips = defaultdict(list)
allowed_state_feature = get_personalized_feature_name(feature, ALLOWED_STATES)
for tip in tree:
if tip.dist == 0:
state = getattr(tip, feature, None)
if state is not None and state != '':
zero_parent2tips[tip.up].append(tip)
# adjust zero tips to contain all the zero tip options as states
for parent, zero_tips in zero_parent2tips.items():
# If there is a common state do nothing
counts = None
for tip in zero_tips:
if counts is None:
counts = getattr(tip, allowed_state_feature).copy()
else:
counts += getattr(tip, allowed_state_feature)
if counts.max() == len(zero_tips):
continue
# Otherwise set all tip states to state union
allowed_states = None
for tip in zero_tips:
if allowed_states is None:
allowed_states = getattr(tip, allowed_state_feature).copy()
else:
tip_allowed_states = getattr(tip, allowed_state_feature)
allowed_states[np.nonzero(tip_allowed_states)] = 1
tip.add_feature(allowed_state_feature, allowed_states) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:choose_ancestral_states_mppa; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:tree; 5, identifier:feature; 6, identifier:states; 7, default_parameter; 7, 8; 7, 9; 8, identifier:force_joint; 9, True; 10, block; 10, 11; 10, 13; 10, 21; 10, 29; 10, 37; 10, 44; 10, 54; 10, 58; 10, 62; 10, 66; 10, 67; 10, 68; 10, 69; 10, 70; 10, 71; 10, 333; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:lh_feature; 16, call; 16, 17; 16, 18; 17, identifier:get_personalized_feature_name; 18, argument_list; 18, 19; 18, 20; 19, identifier:feature; 20, identifier:LH; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:allowed_state_feature; 24, call; 24, 25; 24, 26; 25, identifier:get_personalized_feature_name; 26, argument_list; 26, 27; 26, 28; 27, identifier:feature; 28, identifier:ALLOWED_STATES; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:joint_state_feature; 32, call; 32, 33; 32, 34; 33, identifier:get_personalized_feature_name; 34, argument_list; 34, 35; 34, 36; 35, identifier:feature; 36, identifier:JOINT_STATE; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:n; 40, call; 40, 41; 40, 42; 41, identifier:len; 42, argument_list; 42, 43; 43, identifier:states; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 49; 46, pattern_list; 46, 47; 46, 48; 47, identifier:_; 48, identifier:state2array; 49, call; 49, 50; 49, 51; 50, identifier:get_state2allowed_states; 51, argument_list; 51, 52; 51, 53; 52, identifier:states; 53, False; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:num_scenarios; 57, integer:1; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:unresolved_nodes; 61, integer:0; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:num_states; 65, integer:0; 66, comment; 67, comment; 68, comment; 69, comment; 70, comment; 71, for_statement; 71, 72; 71, 73; 71, 78; 72, identifier:node; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:tree; 76, identifier:traverse; 77, argument_list; 78, block; 78, 79; 78, 87; 78, 97; 78, 148; 78, 152; 78, 158; 78, 217; 78, 221; 78, 225; 78, 283; 78, 325; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:marginal_likelihoods; 82, call; 82, 83; 82, 84; 83, identifier:getattr; 84, argument_list; 84, 85; 84, 86; 85, identifier:node; 86, identifier:lh_feature; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:marginal_probs; 90, binary_operator:/; 90, 91; 90, 92; 91, identifier:marginal_likelihoods; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:marginal_likelihoods; 95, identifier:sum; 96, argument_list; 97, if_statement; 97, 98; 97, 99; 97, 137; 98, identifier:force_joint; 99, block; 99, 100; 99, 108; 99, 114; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:joint_index; 103, call; 103, 104; 103, 105; 104, identifier:getattr; 105, argument_list; 105, 106; 105, 107; 106, identifier:node; 107, identifier:joint_state_feature; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:joint_prob; 111, subscript; 111, 112; 111, 113; 112, identifier:marginal_probs; 113, identifier:joint_index; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:marginal_probs; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:np; 120, identifier:hstack; 121, argument_list; 121, 122; 122, tuple; 122, 123; 122, 135; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:np; 126, identifier:sort; 127, argument_list; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:np; 131, identifier:delete; 132, argument_list; 132, 133; 132, 134; 133, identifier:marginal_probs; 134, identifier:joint_index; 135, list:[joint_prob]; 135, 136; 136, identifier:joint_prob; 137, else_clause; 137, 138; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:marginal_probs; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:np; 145, identifier:sort; 146, argument_list; 146, 147; 147, identifier:marginal_probs; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:best_k; 151, identifier:n; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:best_correstion; 155, attribute; 155, 156; 155, 157; 156, identifier:np; 157, identifier:inf; 158, for_statement; 158, 159; 158, 160; 158, 167; 159, identifier:k; 160, call; 160, 161; 160, 162; 161, identifier:range; 162, argument_list; 162, 163; 162, 164; 163, integer:1; 164, binary_operator:+; 164, 165; 164, 166; 165, identifier:n; 166, integer:1; 167, block; 167, 168; 167, 195; 167, 204; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:correction; 171, binary_operator:-; 171, 172; 171, 194; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:np; 175, identifier:hstack; 176, argument_list; 176, 177; 177, tuple; 177, 178; 177, 186; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:np; 181, identifier:zeros; 182, argument_list; 182, 183; 183, binary_operator:-; 183, 184; 183, 185; 184, identifier:n; 185, identifier:k; 186, binary_operator:/; 186, 187; 186, 193; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:np; 190, identifier:ones; 191, argument_list; 191, 192; 192, identifier:k; 193, identifier:k; 194, identifier:marginal_probs; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:correction; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:correction; 201, identifier:dot; 202, argument_list; 202, 203; 203, identifier:correction; 204, if_statement; 204, 205; 204, 208; 205, comparison_operator:<; 205, 206; 205, 207; 206, identifier:correction; 207, identifier:best_correstion; 208, block; 208, 209; 208, 213; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:best_correstion; 212, identifier:correction; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:best_k; 216, identifier:k; 217, expression_statement; 217, 218; 218, augmented_assignment:*=; 218, 219; 218, 220; 219, identifier:num_scenarios; 220, identifier:best_k; 221, expression_statement; 221, 222; 222, augmented_assignment:+=; 222, 223; 222, 224; 223, identifier:num_states; 224, identifier:best_k; 225, if_statement; 225, 226; 225, 227; 225, 258; 226, identifier:force_joint; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:indices_selected; 231, subscript; 231, 232; 231, 255; 232, call; 232, 233; 232, 234; 233, identifier:sorted; 234, argument_list; 234, 235; 234, 239; 235, call; 235, 236; 235, 237; 236, identifier:range; 237, argument_list; 237, 238; 238, identifier:n; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:key; 241, lambda; 241, 242; 241, 244; 242, lambda_parameters; 242, 243; 243, identifier:_; 244, tuple; 244, 245; 244, 251; 245, conditional_expression:if; 245, 246; 245, 247; 245, 250; 246, integer:0; 247, comparison_operator:==; 247, 248; 247, 249; 248, identifier:n; 249, identifier:joint_index; 250, integer:1; 251, unary_operator:-; 251, 252; 252, subscript; 252, 253; 252, 254; 253, identifier:marginal_likelihoods; 254, identifier:_; 255, slice; 255, 256; 255, 257; 256, colon; 257, identifier:best_k; 258, else_clause; 258, 259; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:indices_selected; 263, subscript; 263, 264; 263, 280; 264, call; 264, 265; 264, 266; 265, identifier:sorted; 266, argument_list; 266, 267; 266, 271; 267, call; 267, 268; 267, 269; 268, identifier:range; 269, argument_list; 269, 270; 270, identifier:n; 271, keyword_argument; 271, 272; 271, 273; 272, identifier:key; 273, lambda; 273, 274; 273, 276; 274, lambda_parameters; 274, 275; 275, identifier:_; 276, unary_operator:-; 276, 277; 277, subscript; 277, 278; 277, 279; 278, identifier:marginal_likelihoods; 279, identifier:_; 280, slice; 280, 281; 280, 282; 281, colon; 282, identifier:best_k; 283, if_statement; 283, 284; 283, 287; 283, 296; 284, comparison_operator:==; 284, 285; 284, 286; 285, identifier:best_k; 286, integer:1; 287, block; 287, 288; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:allowed_states; 291, subscript; 291, 292; 291, 293; 292, identifier:state2array; 293, subscript; 293, 294; 293, 295; 294, identifier:indices_selected; 295, integer:0; 296, else_clause; 296, 297; 297, block; 297, 298; 297, 315; 297, 321; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:allowed_states; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:np; 304, identifier:zeros; 305, argument_list; 305, 306; 305, 310; 306, call; 306, 307; 306, 308; 307, identifier:len; 308, argument_list; 308, 309; 309, identifier:states; 310, keyword_argument; 310, 311; 310, 312; 311, identifier:dtype; 312, attribute; 312, 313; 312, 314; 313, identifier:np; 314, identifier:int; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 320; 317, subscript; 317, 318; 317, 319; 318, identifier:allowed_states; 319, identifier:indices_selected; 320, integer:1; 321, expression_statement; 321, 322; 322, augmented_assignment:+=; 322, 323; 322, 324; 323, identifier:unresolved_nodes; 324, integer:1; 325, expression_statement; 325, 326; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:node; 329, identifier:add_feature; 330, argument_list; 330, 331; 330, 332; 331, identifier:allowed_state_feature; 332, identifier:allowed_states; 333, return_statement; 333, 334; 334, expression_list; 334, 335; 334, 336; 334, 337; 335, identifier:num_scenarios; 336, identifier:unresolved_nodes; 337, identifier:num_states | def choose_ancestral_states_mppa(tree, feature, states, force_joint=True):
"""
Chooses node ancestral states based on their marginal probabilities using MPPA method.
:param force_joint: make sure that Joint state is chosen even if it has a low probability.
:type force_joint: bool
:param tree: tree of interest
:type tree: ete3.Tree
:param feature: character for which the ancestral states are to be chosen
:type feature: str
:param states: possible character states in order corresponding to the probabilities array
:type states: numpy.array
:return: number of ancestral scenarios selected,
calculated by multiplying the number of selected states for all nodes.
Also modified the get_personalized_feature_name(feature, ALLOWED_STATES) feature of each node
to only contain the selected states.
:rtype: int
"""
lh_feature = get_personalized_feature_name(feature, LH)
allowed_state_feature = get_personalized_feature_name(feature, ALLOWED_STATES)
joint_state_feature = get_personalized_feature_name(feature, JOINT_STATE)
n = len(states)
_, state2array = get_state2allowed_states(states, False)
num_scenarios = 1
unresolved_nodes = 0
num_states = 0
# If force_joint == True,
# we make sure that the joint state is always chosen,
# for this we sort the marginal probabilities array as [lowest_non_joint_mp, ..., highest_non_joint_mp, joint_mp]
# select k in 1:n such as the correction between choosing 0, 0, ..., 1/k, ..., 1/k and our sorted array is min
# and return the corresponding states
for node in tree.traverse():
marginal_likelihoods = getattr(node, lh_feature)
marginal_probs = marginal_likelihoods / marginal_likelihoods.sum()
if force_joint:
joint_index = getattr(node, joint_state_feature)
joint_prob = marginal_probs[joint_index]
marginal_probs = np.hstack((np.sort(np.delete(marginal_probs, joint_index)), [joint_prob]))
else:
marginal_probs = np.sort(marginal_probs)
best_k = n
best_correstion = np.inf
for k in range(1, n + 1):
correction = np.hstack((np.zeros(n - k), np.ones(k) / k)) - marginal_probs
correction = correction.dot(correction)
if correction < best_correstion:
best_correstion = correction
best_k = k
num_scenarios *= best_k
num_states += best_k
if force_joint:
indices_selected = sorted(range(n),
key=lambda _: (0 if n == joint_index else 1, -marginal_likelihoods[_]))[:best_k]
else:
indices_selected = sorted(range(n), key=lambda _: -marginal_likelihoods[_])[:best_k]
if best_k == 1:
allowed_states = state2array[indices_selected[0]]
else:
allowed_states = np.zeros(len(states), dtype=np.int)
allowed_states[indices_selected] = 1
unresolved_nodes += 1
node.add_feature(allowed_state_feature, allowed_states)
return num_scenarios, unresolved_nodes, num_states |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse; 3, parameters; 3, 4; 4, identifier:text; 5, block; 5, 6; 5, 8; 5, 9; 5, 21; 5, 22; 5, 26; 5, 27; 5, 31; 5, 32; 5, 233; 5, 234; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:fixed_text; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:validate; 15, identifier:fix_string_case; 16, argument_list; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:utf; 19, argument_list; 19, 20; 20, identifier:text; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:output; 25, list:[]; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:cur_end; 30, integer:0; 31, comment; 32, for_statement; 32, 33; 32, 36; 32, 40; 32, 41; 33, pattern_list; 33, 34; 33, 35; 34, identifier:cur; 35, identifier:i; 36, call; 36, 37; 36, 38; 37, identifier:enumerate; 38, argument_list; 38, 39; 39, identifier:fixed_text; 40, comment; 41, block; 41, 42; 41, 64; 41, 65; 41, 72; 41, 73; 41, 74; 41, 75; 42, try_statement; 42, 43; 42, 51; 42, 58; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:i; 48, identifier:encode; 49, argument_list; 49, 50; 50, string:'utf-8'; 51, except_clause; 51, 52; 51, 53; 52, identifier:UnicodeDecodeError; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:uni_pass; 57, False; 58, else_clause; 58, 59; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:uni_pass; 63, True; 64, comment; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:match; 68, dictionary; 68, 69; 69, pair; 69, 70; 69, 71; 70, string:'matched'; 71, False; 72, comment; 73, comment; 74, comment; 75, if_statement; 75, 76; 75, 78; 75, 92; 76, not_operator; 76, 77; 77, identifier:uni_pass; 78, block; 78, 79; 78, 85; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:cur_end; 82, binary_operator:+; 82, 83; 82, 84; 83, identifier:cur; 84, integer:1; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:output; 89, identifier:append; 90, argument_list; 90, 91; 91, identifier:i; 92, elif_clause; 92, 93; 92, 98; 92, 99; 93, boolean_operator:and; 93, 94; 93, 97; 94, comparison_operator:>=; 94, 95; 94, 96; 95, identifier:cur; 96, identifier:cur_end; 97, identifier:uni_pass; 98, comment; 99, block; 99, 100; 99, 108; 99, 109; 99, 213; 99, 214; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:match; 103, call; 103, 104; 103, 105; 104, identifier:match_non_rule_patterns; 105, argument_list; 105, 106; 105, 107; 106, identifier:fixed_text; 107, identifier:cur; 108, comment; 109, if_statement; 109, 110; 109, 113; 109, 134; 110, subscript; 110, 111; 110, 112; 111, identifier:match; 112, string:"matched"; 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:output; 118, identifier:append; 119, argument_list; 119, 120; 120, subscript; 120, 121; 120, 122; 121, identifier:match; 122, string:"replaced"; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:cur_end; 126, binary_operator:+; 126, 127; 126, 128; 127, identifier:cur; 128, call; 128, 129; 128, 130; 129, identifier:len; 130, argument_list; 130, 131; 131, subscript; 131, 132; 131, 133; 132, identifier:match; 133, string:"found"; 134, else_clause; 134, 135; 134, 136; 135, comment; 136, block; 136, 137; 136, 145; 136, 146; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:match; 140, call; 140, 141; 140, 142; 141, identifier:match_rule_patterns; 142, argument_list; 142, 143; 142, 144; 143, identifier:fixed_text; 144, identifier:cur; 145, comment; 146, if_statement; 146, 147; 146, 150; 146, 151; 147, subscript; 147, 148; 147, 149; 148, identifier:match; 149, string:"matched"; 150, comment; 151, block; 151, 152; 151, 163; 151, 164; 151, 184; 151, 185; 151, 186; 151, 187; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:cur_end; 155, binary_operator:+; 155, 156; 155, 157; 156, identifier:cur; 157, call; 157, 158; 157, 159; 158, identifier:len; 159, argument_list; 159, 160; 160, subscript; 160, 161; 160, 162; 161, identifier:match; 162, string:"found"; 163, comment; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:replaced; 167, call; 167, 168; 167, 169; 168, identifier:process_rules; 169, argument_list; 169, 170; 169, 175; 169, 178; 169, 181; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:rules; 172, subscript; 172, 173; 172, 174; 173, identifier:match; 174, string:"rules"; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:fixed_text; 177, identifier:fixed_text; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:cur; 180, identifier:cur; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:cur_end; 183, identifier:cur_end; 184, comment; 185, comment; 186, comment; 187, if_statement; 187, 188; 187, 191; 187, 192; 187, 200; 188, comparison_operator:is; 188, 189; 188, 190; 189, identifier:replaced; 190, None; 191, comment; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:output; 197, identifier:append; 198, argument_list; 198, 199; 199, identifier:replaced; 200, else_clause; 200, 201; 200, 202; 200, 203; 201, comment; 202, comment; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:output; 208, identifier:append; 209, argument_list; 209, 210; 210, subscript; 210, 211; 210, 212; 211, identifier:match; 212, string:"replaced"; 213, comment; 214, if_statement; 214, 215; 214, 219; 215, not_operator; 215, 216; 216, subscript; 216, 217; 216, 218; 217, identifier:match; 218, string:"matched"; 219, block; 219, 220; 219, 226; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:cur_end; 223, binary_operator:+; 223, 224; 223, 225; 224, identifier:cur; 225, integer:1; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:output; 230, identifier:append; 231, argument_list; 231, 232; 232, identifier:i; 233, comment; 234, return_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, string:''; 238, identifier:join; 239, argument_list; 239, 240; 240, identifier:output | def parse(text):
"""Parses input text, matches and replaces using avrodict
If a valid replacement is found, returns the replaced string. If
no replacement is found, returns the input text.
Usage:
::
from pyavrophonetic import avro
avro.parse("ami banglay gan gai")
"""
# Sanitize text case to meet phonetic comparison standards
fixed_text = validate.fix_string_case(utf(text))
# prepare output list
output = []
# cursor end point
cur_end = 0
# iterate through input text
for cur, i in enumerate(fixed_text):
# Trap characters with unicode encoding errors
try:
i.encode('utf-8')
except UnicodeDecodeError:
uni_pass = False
else:
uni_pass = True
# Default value for match
match = {'matched': False}
# Check cur is greater than or equals cur_end. If cursor is in
# a position that has alread been processed/replaced, we don't
# process anything at all
if not uni_pass:
cur_end = cur + 1
output.append(i)
elif cur >= cur_end and uni_pass:
# Try looking in non rule patterns with current string portion
match = match_non_rule_patterns(fixed_text, cur)
# Check if non rule patterns have matched
if match["matched"]:
output.append(match["replaced"])
cur_end = cur + len(match["found"])
else:
# if non rule patterns have not matched, try rule patterns
match = match_rule_patterns(fixed_text, cur)
# Check if rule patterns have matched
if match["matched"]:
# Update cur_end as cursor + length of match found
cur_end = cur + len(match["found"])
# Process its rules
replaced = process_rules(rules = match["rules"],
fixed_text = fixed_text,
cur = cur, cur_end = cur_end)
# If any rules match, output replacement from the
# rule, else output it's default top-level/default
# replacement
if replaced is not None:
# Rule has matched
output.append(replaced)
else:
# No rules have matched
# output common match
output.append(match["replaced"])
# If none matched, append present cursor value
if not match["matched"]:
cur_end = cur + 1
output.append(i)
# End looping through input text and produce output
return ''.join(output) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:process_match; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:match; 5, identifier:fixed_text; 6, identifier:cur; 7, identifier:cur_end; 8, block; 8, 9; 8, 11; 8, 12; 8, 13; 8, 17; 8, 18; 8, 38; 8, 39; 8, 75; 8, 76; 8, 77; 8, 302; 8, 303; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:replace; 16, True; 17, comment; 18, if_statement; 18, 19; 18, 24; 18, 31; 19, comparison_operator:==; 19, 20; 19, 23; 20, subscript; 20, 21; 20, 22; 21, identifier:match; 22, string:'type'; 23, string:'prefix'; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:chk; 28, binary_operator:-; 28, 29; 28, 30; 29, identifier:cur; 30, integer:1; 31, else_clause; 31, 32; 31, 33; 32, comment; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:chk; 37, identifier:cur_end; 38, comment; 39, if_statement; 39, 40; 39, 48; 39, 63; 40, call; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, subscript; 42, 43; 42, 44; 43, identifier:match; 44, string:'scope'; 45, identifier:startswith; 46, argument_list; 46, 47; 47, string:'!'; 48, block; 48, 49; 48, 59; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:scope; 52, subscript; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:match; 55, string:'scope'; 56, slice; 56, 57; 56, 58; 57, integer:1; 58, colon; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:negative; 62, True; 63, else_clause; 63, 64; 64, block; 64, 65; 64, 71; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:scope; 68, subscript; 68, 69; 68, 70; 69, identifier:match; 70, string:'scope'; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:negative; 74, False; 75, comment; 76, comment; 77, if_statement; 77, 78; 77, 81; 77, 82; 77, 127; 77, 128; 77, 129; 77, 130; 77, 131; 77, 181; 77, 182; 77, 183; 77, 184; 77, 185; 77, 235; 77, 236; 78, comparison_operator:==; 78, 79; 78, 80; 79, identifier:scope; 80, string:'punctuation'; 81, comment; 82, block; 82, 83; 83, if_statement; 83, 84; 83, 122; 84, parenthesized_expression; 84, 85; 85, not_operator; 85, 86; 86, binary_operator:^; 86, 87; 86, 121; 87, parenthesized_expression; 87, 88; 88, boolean_operator:or; 88, 89; 88, 113; 89, boolean_operator:or; 89, 90; 89, 100; 90, parenthesized_expression; 90, 91; 91, boolean_operator:and; 91, 92; 91, 95; 92, comparison_operator:<; 92, 93; 92, 94; 93, identifier:chk; 94, integer:0; 95, comparison_operator:==; 95, 96; 95, 99; 96, subscript; 96, 97; 96, 98; 97, identifier:match; 98, string:'type'; 99, string:'prefix'; 100, parenthesized_expression; 100, 101; 101, boolean_operator:and; 101, 102; 101, 108; 102, comparison_operator:>=; 102, 103; 102, 104; 103, identifier:chk; 104, call; 104, 105; 104, 106; 105, identifier:len; 106, argument_list; 106, 107; 107, identifier:fixed_text; 108, comparison_operator:==; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:match; 111, string:'type'; 112, string:'suffix'; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:validate; 116, identifier:is_punctuation; 117, argument_list; 117, 118; 118, subscript; 118, 119; 118, 120; 119, identifier:fixed_text; 120, identifier:chk; 121, identifier:negative; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:replace; 126, False; 127, comment; 128, comment; 129, comment; 130, comment; 131, elif_clause; 131, 132; 131, 135; 132, comparison_operator:==; 132, 133; 132, 134; 133, identifier:scope; 134, string:'vowel'; 135, block; 135, 136; 136, if_statement; 136, 137; 136, 176; 137, parenthesized_expression; 137, 138; 138, not_operator; 138, 139; 139, binary_operator:^; 139, 140; 139, 175; 140, parenthesized_expression; 140, 141; 141, boolean_operator:and; 141, 142; 141, 167; 142, parenthesized_expression; 142, 143; 143, boolean_operator:or; 143, 144; 143, 154; 144, parenthesized_expression; 144, 145; 145, boolean_operator:and; 145, 146; 145, 149; 146, comparison_operator:>=; 146, 147; 146, 148; 147, identifier:chk; 148, integer:0; 149, comparison_operator:==; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:match; 152, string:'type'; 153, string:'prefix'; 154, parenthesized_expression; 154, 155; 155, boolean_operator:and; 155, 156; 155, 162; 156, comparison_operator:<; 156, 157; 156, 158; 157, identifier:chk; 158, call; 158, 159; 158, 160; 159, identifier:len; 160, argument_list; 160, 161; 161, identifier:fixed_text; 162, comparison_operator:==; 162, 163; 162, 166; 163, subscript; 163, 164; 163, 165; 164, identifier:match; 165, string:'type'; 166, string:'suffix'; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:validate; 170, identifier:is_vowel; 171, argument_list; 171, 172; 172, subscript; 172, 173; 172, 174; 173, identifier:fixed_text; 174, identifier:chk; 175, identifier:negative; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:replace; 180, False; 181, comment; 182, comment; 183, comment; 184, comment; 185, elif_clause; 185, 186; 185, 189; 186, comparison_operator:==; 186, 187; 186, 188; 187, identifier:scope; 188, string:'consonant'; 189, block; 189, 190; 190, if_statement; 190, 191; 190, 230; 191, parenthesized_expression; 191, 192; 192, not_operator; 192, 193; 193, binary_operator:^; 193, 194; 193, 229; 194, parenthesized_expression; 194, 195; 195, boolean_operator:and; 195, 196; 195, 221; 196, parenthesized_expression; 196, 197; 197, boolean_operator:or; 197, 198; 197, 208; 198, parenthesized_expression; 198, 199; 199, boolean_operator:and; 199, 200; 199, 203; 200, comparison_operator:>=; 200, 201; 200, 202; 201, identifier:chk; 202, integer:0; 203, comparison_operator:==; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:match; 206, string:'type'; 207, string:'prefix'; 208, parenthesized_expression; 208, 209; 209, boolean_operator:and; 209, 210; 209, 216; 210, comparison_operator:<; 210, 211; 210, 212; 211, identifier:chk; 212, call; 212, 213; 212, 214; 213, identifier:len; 214, argument_list; 214, 215; 215, identifier:fixed_text; 216, comparison_operator:==; 216, 217; 216, 220; 217, subscript; 217, 218; 217, 219; 218, identifier:match; 219, string:'type'; 220, string:'suffix'; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:validate; 224, identifier:is_consonant; 225, argument_list; 225, 226; 226, subscript; 226, 227; 226, 228; 227, identifier:fixed_text; 228, identifier:chk; 229, identifier:negative; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:replace; 234, False; 235, comment; 236, elif_clause; 236, 237; 236, 240; 236, 241; 237, comparison_operator:==; 237, 238; 237, 239; 238, identifier:scope; 239, string:'exact'; 240, comment; 241, block; 241, 242; 241, 282; 241, 283; 242, if_statement; 242, 243; 242, 248; 242, 264; 243, comparison_operator:==; 243, 244; 243, 247; 244, subscript; 244, 245; 244, 246; 245, identifier:match; 246, string:'type'; 247, string:'prefix'; 248, block; 248, 249; 248, 260; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:exact_start; 252, binary_operator:-; 252, 253; 252, 254; 253, identifier:cur; 254, call; 254, 255; 254, 256; 255, identifier:len; 256, argument_list; 256, 257; 257, subscript; 257, 258; 257, 259; 258, identifier:match; 259, string:'value'; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:exact_end; 263, identifier:cur; 264, else_clause; 264, 265; 264, 266; 265, comment; 266, block; 266, 267; 266, 271; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:exact_start; 270, identifier:cur_end; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:exact_end; 274, binary_operator:+; 274, 275; 274, 276; 275, identifier:cur_end; 276, call; 276, 277; 276, 278; 277, identifier:len; 278, argument_list; 278, 279; 279, subscript; 279, 280; 279, 281; 280, identifier:match; 281, string:'value'; 282, comment; 283, if_statement; 283, 284; 283, 297; 284, not_operator; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:validate; 288, identifier:is_exact; 289, argument_list; 289, 290; 289, 293; 289, 294; 289, 295; 289, 296; 290, subscript; 290, 291; 290, 292; 291, identifier:match; 292, string:'value'; 293, identifier:fixed_text; 294, identifier:exact_start; 295, identifier:exact_end; 296, identifier:negative; 297, block; 297, 298; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:replace; 301, False; 302, comment; 303, return_statement; 303, 304; 304, identifier:replace | def process_match(match, fixed_text, cur, cur_end):
"""Processes a single match in rules"""
# Set our tools
# -- Initial/default value for replace
replace = True
# -- Set check cursor depending on match['type']
if match['type'] == 'prefix':
chk = cur - 1
else:
# suffix
chk = cur_end
# -- Set scope based on whether scope is negative
if match['scope'].startswith('!'):
scope = match['scope'][1:]
negative = True
else:
scope = match['scope']
negative = False
# Let the matching begin
# -- Punctuations
if scope == 'punctuation':
# Conditions: XORd with negative
if (not ((chk < 0 and match['type'] == 'prefix') or
(chk >= len(fixed_text) and match['type'] == 'suffix') or
validate.is_punctuation(fixed_text[chk]))
^ negative):
replace = False
# -- Vowels -- Checks: 1. Cursor should not be at first character
# -- if prefix or last character if suffix, 2. Character at chk
# -- should be a vowel. 3. 'negative' will invert the value of 1
# -- AND 2
elif scope == 'vowel':
if (not (((chk >= 0 and match['type'] == 'prefix') or
(chk < len(fixed_text) and match['type'] == 'suffix'))
and validate.is_vowel(fixed_text[chk]))
^ negative):
replace = False
# -- Consonants -- Checks: 1. Cursor should not be at first
# -- character if prefix or last character if suffix, 2. Character
# -- at chk should be a consonant. 3. 'negative' will invert the
# -- value of 1 AND 2
elif scope == 'consonant':
if (not (((chk >= 0 and match['type'] == 'prefix') or
(chk < len(fixed_text) and match['type'] == 'suffix'))
and validate.is_consonant(fixed_text[chk]))
^ negative):
replace = False
# -- Exacts
elif scope == 'exact':
# Prepare cursor for exact search
if match['type'] == 'prefix':
exact_start = cur - len(match['value'])
exact_end = cur
else:
# suffix
exact_start = cur_end
exact_end = cur_end + len(match['value'])
# Validate exact find.
if not validate.is_exact(match['value'], fixed_text, exact_start,
exact_end, negative):
replace = False
# Return replace, which will be true if none of the checks above match
return replace |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:scatter_norrec; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filename; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:individual; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 25; 11, 29; 11, 30; 11, 34; 11, 68; 11, 79; 11, 95; 11, 147; 11, 298; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:std_diff_labels; 18, dictionary; 18, 19; 18, 22; 19, pair; 19, 20; 19, 21; 20, string:'r'; 21, string:'rdiff'; 22, pair; 22, 23; 22, 24; 23, string:'rpha'; 24, string:'rphadiff'; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:diff_labels; 28, identifier:std_diff_labels; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:labels_to_use; 33, dictionary; 34, for_statement; 34, 35; 34, 38; 34, 43; 34, 44; 35, pattern_list; 35, 36; 35, 37; 36, identifier:key; 37, identifier:item; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:diff_labels; 41, identifier:items; 42, argument_list; 43, comment; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 61; 46, boolean_operator:and; 46, 47; 46, 54; 47, comparison_operator:in; 47, 48; 47, 49; 48, identifier:key; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:data; 53, identifier:columns; 54, comparison_operator:in; 54, 55; 54, 56; 55, identifier:item; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:data; 60, identifier:columns; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:labels_to_use; 66, identifier:key; 67, identifier:item; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:g_freq; 71, call; 71, 72; 71, 77; 72, attribute; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:data; 76, identifier:groupby; 77, argument_list; 77, 78; 78, string:'frequency'; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:frequencies; 82, call; 82, 83; 82, 84; 83, identifier:list; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:sorted; 87, argument_list; 87, 88; 88, call; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:g_freq; 92, identifier:groups; 93, identifier:keys; 94, argument_list; 95, if_statement; 95, 96; 95, 97; 95, 106; 96, identifier:individual; 97, block; 97, 98; 97, 102; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:figures; 101, dictionary; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:axes_all; 105, dictionary; 106, else_clause; 106, 107; 107, block; 107, 108; 107, 119; 107, 126; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:Nx; 111, call; 111, 112; 111, 113; 112, identifier:len; 113, argument_list; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:labels_to_use; 117, identifier:keys; 118, argument_list; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:Ny; 122, call; 122, 123; 122, 124; 123, identifier:len; 124, argument_list; 124, 125; 125, identifier:frequencies; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, pattern_list; 128, 129; 128, 130; 129, identifier:fig; 130, identifier:axes; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:plt; 134, identifier:subplots; 135, argument_list; 135, 136; 135, 137; 135, 138; 136, identifier:Ny; 137, identifier:Nx; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:figsize; 140, tuple; 140, 141; 140, 144; 141, binary_operator:*; 141, 142; 141, 143; 142, identifier:Nx; 143, float:2.5; 144, binary_operator:*; 144, 145; 144, 146; 145, identifier:Ny; 146, float:2.5; 147, for_statement; 147, 148; 147, 153; 147, 157; 148, pattern_list; 148, 149; 148, 150; 149, identifier:row; 150, tuple_pattern; 150, 151; 150, 152; 151, identifier:name; 152, identifier:item; 153, call; 153, 154; 153, 155; 154, identifier:enumerate; 155, argument_list; 155, 156; 156, identifier:g_freq; 157, block; 157, 158; 157, 192; 157, 193; 157, 277; 158, if_statement; 158, 159; 158, 160; 158, 182; 159, identifier:individual; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 166; 163, pattern_list; 163, 164; 163, 165; 164, identifier:fig; 165, identifier:axes_row; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:plt; 169, identifier:subplots; 170, argument_list; 170, 171; 170, 172; 170, 173; 171, integer:1; 172, integer:2; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:figsize; 175, tuple; 175, 176; 175, 179; 176, binary_operator:/; 176, 177; 176, 178; 177, integer:16; 178, float:2.54; 179, binary_operator:/; 179, 180; 179, 181; 180, integer:6; 181, float:2.54; 182, else_clause; 182, 183; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:axes_row; 187, subscript; 187, 188; 187, 189; 187, 190; 188, identifier:axes; 189, identifier:row; 190, slice; 190, 191; 191, colon; 192, comment; 193, for_statement; 193, 194; 193, 199; 193, 210; 194, pattern_list; 194, 195; 194, 196; 195, identifier:col_nr; 196, tuple_pattern; 196, 197; 196, 198; 197, identifier:key; 198, identifier:diff_column; 199, call; 199, 200; 199, 201; 200, identifier:enumerate; 201, argument_list; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:sorted; 204, argument_list; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:labels_to_use; 208, identifier:items; 209, argument_list; 210, block; 210, 211; 210, 230; 210, 236; 210, 248; 210, 255; 210, 262; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:indices; 214, subscript; 214, 215; 214, 229; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:np; 218, identifier:where; 219, argument_list; 219, 220; 220, unary_operator:~; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:np; 224, identifier:isnan; 225, argument_list; 225, 226; 226, subscript; 226, 227; 226, 228; 227, identifier:item; 228, identifier:diff_column; 229, integer:0; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:ax; 233, subscript; 233, 234; 233, 235; 234, identifier:axes_row; 235, identifier:col_nr; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:ax; 240, identifier:scatter; 241, argument_list; 241, 242; 241, 245; 242, subscript; 242, 243; 242, 244; 243, identifier:item; 244, identifier:key; 245, subscript; 245, 246; 245, 247; 246, identifier:item; 247, identifier:diff_column; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:ax; 252, identifier:set_xlabel; 253, argument_list; 253, 254; 254, identifier:key; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:ax; 259, identifier:set_ylabel; 260, argument_list; 260, 261; 261, identifier:diff_column; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:ax; 266, identifier:set_title; 267, argument_list; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, string:'N: {}'; 271, identifier:format; 272, argument_list; 272, 273; 273, call; 273, 274; 273, 275; 274, identifier:len; 275, argument_list; 275, 276; 276, identifier:indices; 277, if_statement; 277, 278; 277, 279; 278, identifier:individual; 279, block; 279, 280; 279, 286; 279, 292; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:fig; 284, identifier:tight_layout; 285, argument_list; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 291; 288, subscript; 288, 289; 288, 290; 289, identifier:figures; 290, identifier:name; 291, identifier:fig; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 297; 294, subscript; 294, 295; 294, 296; 295, identifier:axes_all; 296, identifier:name; 297, identifier:axes_row; 298, if_statement; 298, 299; 298, 300; 298, 305; 299, identifier:individual; 300, block; 300, 301; 301, return_statement; 301, 302; 302, expression_list; 302, 303; 302, 304; 303, identifier:figures; 304, identifier:axes_all; 305, else_clause; 305, 306; 306, block; 306, 307; 306, 313; 307, expression_statement; 307, 308; 308, call; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:fig; 311, identifier:tight_layout; 312, argument_list; 313, return_statement; 313, 314; 314, expression_list; 314, 315; 314, 316; 315, identifier:fig; 316, identifier:axes | def scatter_norrec(self, filename=None, individual=False):
"""Create a scatter plot for all diff pairs
Parameters
----------
filename : string, optional
if given, save plot to file
individual : bool, optional
if set to True, return one figure for each row
Returns
-------
fig : matplotlib.Figure or list of :py:class:`matplotlib.Figure.Figure`
objects the figure object
axes : list of matplotlib.axes
the individual axes
"""
# if not otherwise specified, use these column pairs:
std_diff_labels = {
'r': 'rdiff',
'rpha': 'rphadiff',
}
diff_labels = std_diff_labels
# check which columns are present in the data
labels_to_use = {}
for key, item in diff_labels.items():
# only use if BOTH columns are present
if key in self.data.columns and item in self.data.columns:
labels_to_use[key] = item
g_freq = self.data.groupby('frequency')
frequencies = list(sorted(g_freq.groups.keys()))
if individual:
figures = {}
axes_all = {}
else:
Nx = len(labels_to_use.keys())
Ny = len(frequencies)
fig, axes = plt.subplots(
Ny, Nx,
figsize=(Nx * 2.5, Ny * 2.5)
)
for row, (name, item) in enumerate(g_freq):
if individual:
fig, axes_row = plt.subplots(
1, 2, figsize=(16 / 2.54, 6 / 2.54))
else:
axes_row = axes[row, :]
# loop over the various columns
for col_nr, (key, diff_column) in enumerate(
sorted(labels_to_use.items())):
indices = np.where(~np.isnan(item[diff_column]))[0]
ax = axes_row[col_nr]
ax.scatter(
item[key],
item[diff_column],
)
ax.set_xlabel(key)
ax.set_ylabel(diff_column)
ax.set_title('N: {}'.format(len(indices)))
if individual:
fig.tight_layout()
figures[name] = fig
axes_all[name] = axes_row
if individual:
return figures, axes_all
else:
fig.tight_layout()
return fig, axes |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_spectrum; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:nr_id; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:abmn; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:plot_filename; 13, None; 14, block; 14, 15; 14, 17; 14, 25; 14, 26; 14, 77; 14, 78; 14, 99; 14, 120; 14, 121; 14, 125; 14, 129; 14, 163; 14, 197; 14, 248; 15, expression_statement; 15, 16; 16, comment; 17, assert_statement; 17, 18; 18, boolean_operator:or; 18, 19; 18, 22; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:nr_id; 21, None; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:abmn; 24, None; 25, comment; 26, if_statement; 26, 27; 26, 30; 27, comparison_operator:is; 27, 28; 27, 29; 28, identifier:abmn; 29, None; 30, block; 30, 31; 30, 53; 30, 66; 30, 67; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:subdata; 34, call; 34, 35; 34, 51; 35, attribute; 35, 36; 35, 50; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:data; 41, identifier:query; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, string:'a == {} and b == {} and m == {} and n == {}'; 46, identifier:format; 47, argument_list; 47, 48; 48, list_splat; 48, 49; 49, identifier:abmn; 50, identifier:sort_values; 51, argument_list; 51, 52; 52, string:'frequency'; 53, if_statement; 53, 54; 53, 61; 54, comparison_operator:==; 54, 55; 54, 60; 55, subscript; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:subdata; 58, identifier:shape; 59, integer:0; 60, integer:0; 61, block; 61, 62; 62, return_statement; 62, 63; 63, expression_list; 63, 64; 63, 65; 64, None; 65, None; 66, comment; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:nr_id; 70, subscript; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:subdata; 74, string:'id'; 75, identifier:iloc; 76, integer:0; 77, comment; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:subdata_nor; 81, call; 81, 82; 81, 97; 82, attribute; 82, 83; 82, 96; 83, call; 83, 84; 83, 89; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:data; 88, identifier:query; 89, argument_list; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, string:'id == {} and norrec=="nor"'; 93, identifier:format; 94, argument_list; 94, 95; 95, identifier:nr_id; 96, identifier:sort_values; 97, argument_list; 97, 98; 98, string:'frequency'; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:subdata_rec; 102, call; 102, 103; 102, 118; 103, attribute; 103, 104; 103, 117; 104, call; 104, 105; 104, 110; 105, attribute; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:data; 109, identifier:query; 110, argument_list; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, string:'id == {} and norrec=="rec"'; 114, identifier:format; 115, argument_list; 115, 116; 116, identifier:nr_id; 117, identifier:sort_values; 118, argument_list; 118, 119; 119, string:'frequency'; 120, comment; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:spectrum_nor; 124, None; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:spectrum_rec; 128, None; 129, if_statement; 129, 130; 129, 137; 130, comparison_operator:>; 130, 131; 130, 136; 131, subscript; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:subdata_nor; 134, identifier:shape; 135, integer:0; 136, integer:0; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:spectrum_nor; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:eis_plot; 144, identifier:sip_response; 145, argument_list; 145, 146; 145, 153; 145, 158; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:frequencies; 148, attribute; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:subdata_nor; 151, string:'frequency'; 152, identifier:values; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:rmag; 155, subscript; 155, 156; 155, 157; 156, identifier:subdata_nor; 157, string:'r'; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:rpha; 160, subscript; 160, 161; 160, 162; 161, identifier:subdata_nor; 162, string:'rpha'; 163, if_statement; 163, 164; 163, 171; 164, comparison_operator:>; 164, 165; 164, 170; 165, subscript; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:subdata_rec; 168, identifier:shape; 169, integer:0; 170, integer:0; 171, block; 171, 172; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:spectrum_rec; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:eis_plot; 178, identifier:sip_response; 179, argument_list; 179, 180; 179, 187; 179, 192; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:frequencies; 182, attribute; 182, 183; 182, 186; 183, subscript; 183, 184; 183, 185; 184, identifier:subdata_rec; 185, string:'frequency'; 186, identifier:values; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:rmag; 189, subscript; 189, 190; 189, 191; 190, identifier:subdata_rec; 191, string:'r'; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:rpha; 194, subscript; 194, 195; 194, 196; 195, identifier:subdata_rec; 196, string:'rpha'; 197, if_statement; 197, 198; 197, 201; 198, comparison_operator:is; 198, 199; 198, 200; 199, identifier:plot_filename; 200, None; 201, block; 201, 202; 202, if_statement; 202, 203; 202, 206; 203, comparison_operator:is; 203, 204; 203, 205; 204, identifier:spectrum_nor; 205, None; 206, block; 206, 207; 206, 243; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:fig; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:spectrum_nor; 213, identifier:plot; 214, argument_list; 214, 215; 214, 216; 214, 219; 214, 222; 215, identifier:plot_filename; 216, keyword_argument; 216, 217; 216, 218; 217, identifier:reciprocal; 218, identifier:spectrum_rec; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:return_fig; 221, True; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:title; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, string:'a: {} b: {} m: {}: n: {}'; 227, identifier:format; 228, argument_list; 228, 229; 229, list_splat; 229, 230; 230, subscript; 230, 231; 230, 240; 230, 241; 231, attribute; 231, 232; 231, 239; 232, subscript; 232, 233; 232, 234; 233, identifier:subdata_nor; 234, list:['a', 'b', 'm', 'n']; 234, 235; 234, 236; 234, 237; 234, 238; 235, string:'a'; 236, string:'b'; 237, string:'m'; 238, string:'n'; 239, identifier:values; 240, integer:0; 241, slice; 241, 242; 242, colon; 243, return_statement; 243, 244; 244, expression_list; 244, 245; 244, 246; 244, 247; 245, identifier:spectrum_nor; 246, identifier:spectrum_rec; 247, identifier:fig; 248, return_statement; 248, 249; 249, expression_list; 249, 250; 249, 251; 250, identifier:spectrum_nor; 251, identifier:spectrum_rec | def get_spectrum(self, nr_id=None, abmn=None, plot_filename=None):
"""Return a spectrum and its reciprocal counter part, if present in the
dataset. Optimally, refer to the spectrum by its normal-reciprocal id.
Returns
-------
spectrum_nor : :py:class:`reda.eis.plots.sip_response`
Normal spectrum. None if no normal spectrum is available
spectrum_rec : :py:class:`reda.eis.plots.sip_response` or None
Reciprocal spectrum. None if no reciprocal spectrum is available
fig : :py:class:`matplotlib.Figure.Figure` , optional
Figure object (only if plot_filename is set)
"""
assert nr_id is None or abmn is None
# determine nr_id for given abmn tuple
if abmn is not None:
subdata = self.data.query(
'a == {} and b == {} and m == {} and n == {}'.format(*abmn)
).sort_values('frequency')
if subdata.shape[0] == 0:
return None, None
# determine the norrec-id of this spectrum
nr_id = subdata['id'].iloc[0]
# get spectra
subdata_nor = self.data.query(
'id == {} and norrec=="nor"'.format(nr_id)
).sort_values('frequency')
subdata_rec = self.data.query(
'id == {} and norrec=="rec"'.format(nr_id)
).sort_values('frequency')
# create spectrum objects
spectrum_nor = None
spectrum_rec = None
if subdata_nor.shape[0] > 0:
spectrum_nor = eis_plot.sip_response(
frequencies=subdata_nor['frequency'].values,
rmag=subdata_nor['r'],
rpha=subdata_nor['rpha'],
)
if subdata_rec.shape[0] > 0:
spectrum_rec = eis_plot.sip_response(
frequencies=subdata_rec['frequency'].values,
rmag=subdata_rec['r'],
rpha=subdata_rec['rpha'],
)
if plot_filename is not None:
if spectrum_nor is not None:
fig = spectrum_nor.plot(
plot_filename,
reciprocal=spectrum_rec,
return_fig=True,
title='a: {} b: {} m: {}: n: {}'.format(
*subdata_nor[['a', 'b', 'm', 'n']].values[0, :]
)
)
return spectrum_nor, spectrum_rec, fig
return spectrum_nor, spectrum_rec |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:import_bin; 3, parameters; 3, 4; 3, 5; 4, identifier:filename; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kwargs; 7, block; 7, 8; 7, 10; 7, 19; 7, 29; 7, 60; 7, 194; 7, 221; 7, 236; 7, 237; 7, 249; 7, 257; 7, 265; 7, 273; 7, 281; 7, 289; 7, 297; 7, 305; 7, 313; 7, 314; 7, 324; 7, 354; 7, 355; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 15; 12, pattern_list; 12, 13; 12, 14; 13, identifier:metadata; 14, identifier:data_raw; 15, call; 15, 16; 15, 17; 16, identifier:_import_bin; 17, argument_list; 17, 18; 18, identifier:filename; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:skip_rows; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:kwargs; 25, identifier:get; 26, argument_list; 26, 27; 26, 28; 27, string:'skip_rows'; 28, integer:0; 29, if_statement; 29, 30; 29, 33; 30, comparison_operator:>; 30, 31; 30, 32; 31, identifier:skip_rows; 32, integer:0; 33, block; 33, 34; 33, 52; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:data_raw; 38, identifier:drop; 39, argument_list; 39, 40; 39, 49; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:data_raw; 43, identifier:index; 44, call; 44, 45; 44, 46; 45, identifier:range; 46, argument_list; 46, 47; 46, 48; 47, integer:0; 48, identifier:skip_rows; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:inplace; 51, True; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:data_raw; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:data_raw; 58, identifier:reset_index; 59, argument_list; 60, if_statement; 60, 61; 60, 68; 60, 69; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:kwargs; 64, identifier:get; 65, argument_list; 65, 66; 65, 67; 66, string:'check_meas_nums'; 67, True; 68, comment; 69, block; 69, 70; 69, 88; 69, 89; 69, 116; 69, 117; 69, 118; 69, 132; 69, 145; 70, if_statement; 70, 71; 70, 80; 71, comparison_operator:!=; 71, 72; 71, 79; 72, subscript; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:data_raw; 76, string:'measurement_num'; 77, identifier:iloc; 78, integer:0; 79, integer:0; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:print; 84, argument_list; 84, 85; 85, binary_operator:+; 85, 86; 85, 87; 86, string:'WARNING: Measurement numbers do not start with 0 '; 87, string:'(did you download ALL data?)'; 88, comment; 89, if_statement; 89, 90; 89, 106; 90, not_operator; 90, 91; 91, comparison_operator:==; 91, 92; 91, 105; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:np; 95, identifier:all; 96, argument_list; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:np; 100, identifier:diff; 101, argument_list; 101, 102; 102, subscript; 102, 103; 102, 104; 103, identifier:data_raw; 104, string:'measurement_num'; 105, integer:1; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:print; 110, argument_list; 110, 111; 111, concatenated_string; 111, 112; 111, 113; 111, 114; 111, 115; 112, string:'WARNING '; 113, string:'Measurement numbers are not consecutive. '; 114, string:'Perhaps the first measurement belongs to another measurement?'; 115, string:' Use the skip_rows parameter to skip those measurements'; 116, comment; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:diff; 121, subscript; 121, 122; 121, 129; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:data_raw; 126, string:'measurement_num'; 127, identifier:diff; 128, argument_list; 129, slice; 129, 130; 129, 131; 130, integer:1; 131, colon; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:jump; 135, subscript; 135, 136; 135, 144; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:np; 139, identifier:where; 140, argument_list; 140, 141; 141, comparison_operator:!=; 141, 142; 141, 143; 142, identifier:diff; 143, integer:1; 144, integer:0; 145, if_statement; 145, 146; 145, 152; 146, comparison_operator:>; 146, 147; 146, 151; 147, call; 147, 148; 147, 149; 148, identifier:len; 149, argument_list; 149, 150; 150, identifier:jump; 151, integer:0; 152, block; 152, 153; 152, 158; 152, 163; 152, 172; 152, 177; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:print; 156, argument_list; 156, 157; 157, string:'WARNING: One or more jumps in measurement numbers detected'; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:print; 161, argument_list; 161, 162; 162, string:'The jump indices are:'; 163, for_statement; 163, 164; 163, 165; 163, 166; 164, identifier:jump_nr; 165, identifier:jump; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 170; 169, identifier:print; 170, argument_list; 170, 171; 171, identifier:jump_nr; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:print; 175, argument_list; 175, 176; 176, string:'Removing data points subsequent to the first jump'; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:data_raw; 180, subscript; 180, 181; 180, 184; 180, 192; 181, attribute; 181, 182; 181, 183; 182, identifier:data_raw; 183, identifier:iloc; 184, slice; 184, 185; 184, 186; 184, 187; 185, integer:0; 186, colon; 187, binary_operator:+; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:jump; 190, integer:0; 191, integer:1; 192, slice; 192, 193; 193, colon; 194, if_statement; 194, 195; 194, 202; 194, 203; 195, comparison_operator:==; 195, 196; 195, 201; 196, subscript; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:data_raw; 199, identifier:shape; 200, integer:0; 201, integer:0; 202, comment; 203, block; 203, 204; 204, return_statement; 204, 205; 205, expression_list; 205, 206; 205, 219; 205, 220; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:pd; 209, identifier:DataFrame; 210, argument_list; 210, 211; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:columns; 213, list:['a', 'b', 'm', 'n', 'r']; 213, 214; 213, 215; 213, 216; 213, 217; 213, 218; 214, string:'a'; 215, string:'b'; 216, string:'m'; 217, string:'n'; 218, string:'r'; 219, None; 220, None; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:data; 224, call; 224, 225; 224, 226; 225, identifier:_convert_coords_to_abmn_X; 226, argument_list; 226, 227; 226, 234; 227, subscript; 227, 228; 227, 229; 228, identifier:data_raw; 229, list:['x_a', 'x_b', 'x_m', 'x_n']; 229, 230; 229, 231; 229, 232; 229, 233; 230, string:'x_a'; 231, string:'x_b'; 232, string:'x_m'; 233, string:'x_n'; 234, dictionary_splat; 234, 235; 235, identifier:kwargs; 236, comment; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 242; 239, subscript; 239, 240; 239, 241; 240, identifier:data; 241, string:'r'; 242, binary_operator:/; 242, 243; 242, 246; 243, subscript; 243, 244; 243, 245; 244, identifier:data_raw; 245, string:'vp'; 246, subscript; 246, 247; 246, 248; 247, identifier:data_raw; 248, string:'Iab'; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 254; 251, subscript; 251, 252; 251, 253; 252, identifier:data; 253, string:'Vmn'; 254, subscript; 254, 255; 254, 256; 255, identifier:data_raw; 256, string:'vp'; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 262; 259, subscript; 259, 260; 259, 261; 260, identifier:data; 261, string:'vab'; 262, subscript; 262, 263; 262, 264; 263, identifier:data_raw; 264, string:'vab'; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 270; 267, subscript; 267, 268; 267, 269; 268, identifier:data; 269, string:'Iab'; 270, subscript; 270, 271; 270, 272; 271, identifier:data_raw; 272, string:'Iab'; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 278; 275, subscript; 275, 276; 275, 277; 276, identifier:data; 277, string:'mdelay'; 278, subscript; 278, 279; 278, 280; 279, identifier:data_raw; 280, string:'mdelay'; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 286; 283, subscript; 283, 284; 283, 285; 284, identifier:data; 285, string:'Tm'; 286, subscript; 286, 287; 286, 288; 287, identifier:data_raw; 288, string:'Tm'; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 294; 291, subscript; 291, 292; 291, 293; 292, identifier:data; 293, string:'Mx'; 294, subscript; 294, 295; 294, 296; 295, identifier:data_raw; 296, string:'Mx'; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 302; 299, subscript; 299, 300; 299, 301; 300, identifier:data; 301, string:'chargeability'; 302, subscript; 302, 303; 302, 304; 303, identifier:data_raw; 304, string:'m'; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 310; 307, subscript; 307, 308; 307, 309; 308, identifier:data; 309, string:'q'; 310, subscript; 310, 311; 310, 312; 311, identifier:data_raw; 312, string:'q'; 313, comment; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:rec_max; 317, call; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:kwargs; 320, identifier:get; 321, argument_list; 321, 322; 321, 323; 322, string:'reciprocals'; 323, None; 324, if_statement; 324, 325; 324, 328; 325, comparison_operator:is; 325, 326; 325, 327; 326, identifier:rec_max; 327, None; 328, block; 328, 329; 328, 334; 329, expression_statement; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:print; 332, argument_list; 332, 333; 333, string:'renumbering electrode numbers'; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 343; 336, subscript; 336, 337; 336, 338; 337, identifier:data; 338, list:['a', 'b', 'm', 'n']; 338, 339; 338, 340; 338, 341; 338, 342; 339, string:'a'; 340, string:'b'; 341, string:'m'; 342, string:'n'; 343, binary_operator:-; 343, 344; 343, 347; 344, binary_operator:+; 344, 345; 344, 346; 345, identifier:rec_max; 346, integer:1; 347, subscript; 347, 348; 347, 349; 348, identifier:data; 349, list:['a', 'b', 'm', 'n']; 349, 350; 349, 351; 349, 352; 349, 353; 350, string:'a'; 351, string:'b'; 352, string:'m'; 353, string:'n'; 354, comment; 355, return_statement; 355, 356; 356, expression_list; 356, 357; 356, 358; 356, 359; 357, identifier:data; 358, None; 359, None | def import_bin(filename, **kwargs):
"""Read a .bin file generated by the IRIS Instruments Syscal Pro System and
return a curated dataframe for further processing. This dataframe contains
only information currently deemed important. Use the function
reda.importers.iris_syscal_pro_binary._import_bin to extract ALL
information from a given .bin file.
Parameters
----------
filename : string
path to input filename
x0 : float, optional
position of first electrode. If not given, then use the smallest
x-position in the data as the first electrode.
spacing : float
electrode spacing. This is important if not all electrodes are used in
a given measurement setup. If not given, then the smallest distance
between electrodes is assumed to be the electrode spacing. Naturally,
this requires measurements (or injections) with subsequent electrodes.
reciprocals : int, optional
if provided, then assume that this is a reciprocal measurements where
only the electrode cables were switched. The provided number N is
treated as the maximum electrode number, and denotations are renamed
according to the equation :math:`X_n = N - (X_a - 1)`
check_meas_nums : bool
if True, then check that the measurement numbers are consecutive. Don't
return data after a jump to smaller measurement numbers (this usually
indicates that more data points were downloaded than are part of a
specific measurement. Default: True
skip_rows : int
Ignore this number of rows at the beginning, e.g., because they were
inadvertently imported from an earlier measurement. Default: 0
Returns
-------
data : :py:class:`pandas.DataFrame`
Contains the measurement data
electrodes : :py:class:`pandas.DataFrame`
Contains electrode positions (None at the moment)
topography : None
No topography information is contained in the text files, so we always
return None
"""
metadata, data_raw = _import_bin(filename)
skip_rows = kwargs.get('skip_rows', 0)
if skip_rows > 0:
data_raw.drop(data_raw.index[range(0, skip_rows)], inplace=True)
data_raw = data_raw.reset_index()
if kwargs.get('check_meas_nums', True):
# check that first number is 0
if data_raw['measurement_num'].iloc[0] != 0:
print('WARNING: Measurement numbers do not start with 0 ' +
'(did you download ALL data?)')
# check that all measurement numbers increase by one
if not np.all(np.diff(data_raw['measurement_num'])) == 1:
print(
'WARNING '
'Measurement numbers are not consecutive. '
'Perhaps the first measurement belongs to another measurement?'
' Use the skip_rows parameter to skip those measurements'
)
# now check if there is a jump in measurement numbers somewhere
# ignore first entry as this will always be nan
diff = data_raw['measurement_num'].diff()[1:]
jump = np.where(diff != 1)[0]
if len(jump) > 0:
print('WARNING: One or more jumps in measurement numbers detected')
print('The jump indices are:')
for jump_nr in jump:
print(jump_nr)
print('Removing data points subsequent to the first jump')
data_raw = data_raw.iloc[0:jump[0] + 1, :]
if data_raw.shape[0] == 0:
# no data present, return a bare DataFrame
return pd.DataFrame(columns=['a', 'b', 'm', 'n', 'r']), None, None
data = _convert_coords_to_abmn_X(
data_raw[['x_a', 'x_b', 'x_m', 'x_n']],
**kwargs
)
# [mV] / [mA]
data['r'] = data_raw['vp'] / data_raw['Iab']
data['Vmn'] = data_raw['vp']
data['vab'] = data_raw['vab']
data['Iab'] = data_raw['Iab']
data['mdelay'] = data_raw['mdelay']
data['Tm'] = data_raw['Tm']
data['Mx'] = data_raw['Mx']
data['chargeability'] = data_raw['m']
data['q'] = data_raw['q']
# rename electrode denotations
rec_max = kwargs.get('reciprocals', None)
if rec_max is not None:
print('renumbering electrode numbers')
data[['a', 'b', 'm', 'n']] = rec_max + 1 - data[['a', 'b', 'm', 'n']]
# print(data)
return data, None, None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 43; 2, function_name:datoms; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 3, 32; 3, 35; 3, 38; 3, 41; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:index; 7, string:'aevt'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:e; 10, string:''; 11, default_parameter; 11, 12; 11, 13; 12, identifier:a; 13, string:''; 14, default_parameter; 14, 15; 14, 16; 15, identifier:v; 16, string:''; 17, default_parameter; 17, 18; 17, 19; 18, identifier:limit; 19, integer:0; 20, default_parameter; 20, 21; 20, 22; 21, identifier:offset; 22, integer:0; 23, default_parameter; 23, 24; 23, 25; 24, identifier:chunk; 25, integer:100; 26, default_parameter; 26, 27; 26, 28; 27, identifier:start; 28, string:''; 29, default_parameter; 29, 30; 29, 31; 30, identifier:end; 31, string:''; 32, default_parameter; 32, 33; 32, 34; 33, identifier:since; 34, string:''; 35, default_parameter; 35, 36; 35, 37; 36, identifier:as_of; 37, string:''; 38, default_parameter; 38, 39; 38, 40; 39, identifier:history; 40, string:''; 41, dictionary_splat_pattern; 41, 42; 42, identifier:kwargs; 43, block; 43, 44; 43, 46; 43, 55; 43, 129; 43, 137; 43, 141; 44, expression_statement; 44, 45; 45, comment; 46, assert_statement; 46, 47; 46, 54; 47, comparison_operator:in; 47, 48; 47, 49; 48, identifier:index; 49, list:['aevt','eavt','avet','vaet']; 49, 50; 49, 51; 49, 52; 49, 53; 50, string:'aevt'; 51, string:'eavt'; 52, string:'avet'; 53, string:'vaet'; 54, string:"non-existant index"; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:data; 58, dictionary; 58, 59; 58, 62; 58, 73; 58, 82; 58, 91; 58, 96; 58, 99; 58, 102; 58, 105; 58, 111; 58, 120; 59, pair; 59, 60; 59, 61; 60, string:'index'; 61, identifier:index; 62, pair; 62, 63; 62, 64; 63, string:'a'; 64, conditional_expression:if; 64, 65; 64, 71; 64, 72; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, string:':{0}'; 68, identifier:format; 69, argument_list; 69, 70; 70, identifier:a; 71, identifier:a; 72, string:''; 73, pair; 73, 74; 73, 75; 74, string:'v'; 75, conditional_expression:if; 75, 76; 75, 80; 75, 81; 76, call; 76, 77; 76, 78; 77, identifier:dump_edn_val; 78, argument_list; 78, 79; 79, identifier:v; 80, identifier:v; 81, string:''; 82, pair; 82, 83; 82, 84; 83, string:'e'; 84, conditional_expression:if; 84, 85; 84, 89; 84, 90; 85, call; 85, 86; 85, 87; 86, identifier:int; 87, argument_list; 87, 88; 88, identifier:e; 89, identifier:e; 90, string:''; 91, pair; 91, 92; 91, 93; 92, string:'offset'; 93, boolean_operator:or; 93, 94; 93, 95; 94, identifier:offset; 95, integer:0; 96, pair; 96, 97; 96, 98; 97, string:'start'; 98, identifier:start; 99, pair; 99, 100; 99, 101; 100, string:'end'; 101, identifier:end; 102, pair; 102, 103; 102, 104; 103, string:'limit'; 104, identifier:limit; 105, pair; 105, 106; 105, 107; 106, string:'history'; 107, conditional_expression:if; 107, 108; 107, 109; 107, 110; 108, string:'true'; 109, identifier:history; 110, string:''; 111, pair; 111, 112; 111, 113; 112, string:'as-of'; 113, conditional_expression:if; 113, 114; 113, 118; 113, 119; 114, call; 114, 115; 114, 116; 115, identifier:int; 116, argument_list; 116, 117; 117, identifier:as_of; 118, identifier:as_of; 119, string:''; 120, pair; 120, 121; 120, 122; 121, string:'since'; 122, conditional_expression:if; 122, 123; 122, 127; 122, 128; 123, call; 123, 124; 123, 125; 124, identifier:int; 125, argument_list; 125, 126; 126, identifier:since; 127, identifier:since; 128, string:''; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:data; 133, string:'limit'; 134, binary_operator:+; 134, 135; 134, 136; 135, identifier:offset; 136, identifier:chunk; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:rs; 140, True; 141, while_statement; 141, 142; 141, 153; 142, boolean_operator:and; 142, 143; 142, 144; 143, identifier:rs; 144, parenthesized_expression; 144, 145; 145, comparison_operator:<; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:data; 148, string:'offset'; 149, parenthesized_expression; 149, 150; 150, boolean_operator:or; 150, 151; 150, 152; 151, identifier:limit; 152, integer:1000000000; 153, block; 153, 154; 153, 164; 153, 184; 153, 195; 153, 207; 153, 227; 153, 234; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:ta; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:datetime; 161, identifier:datetime; 162, identifier:now; 163, argument_list; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:rs; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:rest; 171, argument_list; 171, 172; 171, 173; 171, 178; 171, 181; 172, string:'GET'; 173, binary_operator:+; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:uri_db; 177, string:'-/datoms'; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:data; 180, identifier:data; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:parse; 183, True; 184, if_statement; 184, 185; 184, 190; 185, not_operator; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:len; 188, argument_list; 188, 189; 189, identifier:rs; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:rs; 194, False; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:tb; 198, binary_operator:-; 198, 199; 198, 206; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:datetime; 203, identifier:datetime; 204, identifier:now; 205, argument_list; 206, identifier:ta; 207, print_statement; 207, 208; 208, call; 208, 209; 208, 210; 209, identifier:cl; 210, argument_list; 210, 211; 210, 226; 211, binary_operator:%; 211, 212; 211, 213; 212, string:'<<< fetched %i datoms at offset %i in %sms'; 213, tuple; 213, 214; 213, 218; 213, 221; 214, call; 214, 215; 214, 216; 215, identifier:len; 216, argument_list; 216, 217; 217, identifier:rs; 218, subscript; 218, 219; 218, 220; 219, identifier:data; 220, string:'offset'; 221, binary_operator:/; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:tb; 224, identifier:microseconds; 225, float:1000.0; 226, string:'cyan'; 227, for_statement; 227, 228; 227, 229; 227, 230; 228, identifier:r; 229, identifier:rs; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, yield; 232, 233; 233, identifier:r; 234, expression_statement; 234, 235; 235, augmented_assignment:+=; 235, 236; 235, 239; 236, subscript; 236, 237; 236, 238; 237, identifier:data; 238, string:'offset'; 239, identifier:chunk | def datoms(self, index='aevt', e='', a='', v='',
limit=0, offset=0, chunk=100,
start='', end='', since='', as_of='', history='', **kwargs):
""" Returns a lazy generator that will only fetch groups of datoms
at the chunk size specified.
http://docs.datomic.com/clojure/index.html#datomic.api/datoms
"""
assert index in ['aevt','eavt','avet','vaet'], "non-existant index"
data = {'index': index,
'a': ':{0}'.format(a) if a else '',
'v': dump_edn_val(v) if v else '',
'e': int(e) if e else '',
'offset': offset or 0,
'start': start,
'end': end,
'limit': limit,
'history': 'true' if history else '',
'as-of': int(as_of) if as_of else '',
'since': int(since) if since else '',
}
data['limit'] = offset + chunk
rs = True
while rs and (data['offset'] < (limit or 1000000000)):
ta = datetime.datetime.now()
rs = self.rest('GET', self.uri_db + '-/datoms', data=data, parse=True)
if not len(rs):
rs = False
tb = datetime.datetime.now() - ta
print cl('<<< fetched %i datoms at offset %i in %sms' % (
len(rs), data['offset'], tb.microseconds/1000.0), 'cyan')
for r in rs: yield r
data['offset'] += chunk |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:add; 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, 19; 9, 32; 9, 149; 9, 151; 9, 174; 9, 176; 9, 205; 9, 207; 9, 369; 9, 371; 10, expression_statement; 10, 11; 11, comment; 12, assert_statement; 12, 13; 12, 18; 13, comparison_operator:is; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:resp; 17, None; 18, string:"Transaction already committed"; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 25; 21, pattern_list; 21, 22; 21, 23; 21, 24; 22, identifier:entity; 23, identifier:av_pairs; 24, identifier:args; 25, expression_list; 25, 26; 25, 27; 25, 28; 26, None; 27, list:[]; 28, call; 28, 29; 28, 30; 29, identifier:list; 30, argument_list; 30, 31; 31, identifier:args; 32, if_statement; 32, 33; 32, 37; 33, call; 33, 34; 33, 35; 34, identifier:len; 35, argument_list; 35, 36; 36, identifier:args; 37, block; 37, 38; 37, 124; 37, 126; 38, if_statement; 38, 39; 38, 48; 38, 63; 39, call; 39, 40; 39, 41; 40, identifier:isinstance; 41, argument_list; 41, 42; 41, 45; 42, subscript; 42, 43; 42, 44; 43, identifier:args; 44, integer:0; 45, tuple; 45, 46; 45, 47; 46, identifier:int; 47, identifier:long; 48, block; 48, 49; 48, 51; 49, expression_statement; 49, 50; 50, string:" first arg is an entity or tempid"; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:entity; 54, call; 54, 55; 54, 56; 55, identifier:E; 56, argument_list; 56, 57; 56, 60; 57, subscript; 57, 58; 57, 59; 58, identifier:args; 59, integer:0; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:tx; 62, identifier:self; 63, elif_clause; 63, 64; 63, 71; 64, call; 64, 65; 64, 66; 65, identifier:isinstance; 66, argument_list; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:args; 69, integer:0; 70, identifier:E; 71, block; 71, 72; 71, 74; 72, expression_statement; 72, 73; 73, string:" dont resuse entity from another tx"; 74, if_statement; 74, 75; 74, 82; 74, 89; 75, comparison_operator:is; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:args; 79, integer:0; 80, identifier:_tx; 81, identifier:self; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:entity; 86, subscript; 86, 87; 86, 88; 87, identifier:args; 88, integer:0; 89, else_clause; 89, 90; 90, block; 90, 91; 90, 118; 91, if_statement; 91, 92; 91, 100; 92, comparison_operator:>; 92, 93; 92, 99; 93, call; 93, 94; 93, 95; 94, identifier:int; 95, argument_list; 95, 96; 96, subscript; 96, 97; 96, 98; 97, identifier:args; 98, integer:0; 99, integer:0; 100, block; 100, 101; 100, 103; 101, expression_statement; 101, 102; 102, string:" use the entity id on a new obj"; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:entity; 106, call; 106, 107; 106, 108; 107, identifier:E; 108, argument_list; 108, 109; 108, 115; 109, call; 109, 110; 109, 111; 110, identifier:int; 111, argument_list; 111, 112; 112, subscript; 112, 113; 112, 114; 113, identifier:args; 114, integer:0; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:tx; 117, identifier:self; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:args; 122, integer:0; 123, None; 124, expression_statement; 124, 125; 125, string:" drop the first arg"; 126, if_statement; 126, 127; 126, 139; 127, boolean_operator:or; 127, 128; 127, 131; 128, comparison_operator:is; 128, 129; 128, 130; 129, identifier:entity; 130, None; 131, comparison_operator:in; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:args; 134, integer:0; 135, tuple; 135, 136; 135, 137; 135, 138; 136, None; 137, False; 138, integer:0; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:v; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:args; 146, identifier:pop; 147, argument_list; 147, 148; 148, integer:0; 149, expression_statement; 149, 150; 150, string:" auto generate a temp id?"; 151, if_statement; 151, 152; 151, 155; 152, comparison_operator:is; 152, 153; 152, 154; 153, identifier:entity; 154, None; 155, block; 155, 156; 155, 168; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:entity; 159, call; 159, 160; 159, 161; 160, identifier:E; 161, argument_list; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:ctmpid; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:tx; 167, identifier:self; 168, expression_statement; 168, 169; 169, augmented_assignment:-=; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:ctmpid; 173, integer:1; 174, expression_statement; 174, 175; 175, string:" a,v from kwargs"; 176, if_statement; 176, 177; 176, 185; 177, boolean_operator:and; 177, 178; 177, 184; 178, comparison_operator:==; 178, 179; 178, 183; 179, call; 179, 180; 179, 181; 180, identifier:len; 181, argument_list; 181, 182; 182, identifier:args; 183, integer:0; 184, identifier:kwargs; 185, block; 185, 186; 186, for_statement; 186, 187; 186, 190; 186, 195; 187, pattern_list; 187, 188; 187, 189; 188, identifier:a; 189, identifier:v; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:kwargs; 193, identifier:iteritems; 194, argument_list; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:addeav; 201, argument_list; 201, 202; 201, 203; 201, 204; 202, identifier:entity; 203, identifier:a; 204, identifier:v; 205, expression_statement; 205, 206; 206, string:" a,v from args "; 207, if_statement; 207, 208; 207, 212; 208, call; 208, 209; 208, 210; 209, identifier:len; 210, argument_list; 210, 211; 211, identifier:args; 212, block; 212, 213; 212, 225; 213, assert_statement; 213, 214; 213, 222; 214, comparison_operator:==; 214, 215; 214, 221; 215, binary_operator:%; 215, 216; 215, 220; 216, call; 216, 217; 216, 218; 217, identifier:len; 218, argument_list; 218, 219; 219, identifier:args; 220, integer:2; 221, integer:0; 222, binary_operator:%; 222, 223; 222, 224; 223, string:"imbalanced a,v in args: "; 224, identifier:args; 225, for_statement; 225, 226; 225, 229; 225, 233; 226, pattern_list; 226, 227; 226, 228; 227, identifier:first; 228, identifier:second; 229, call; 229, 230; 229, 231; 230, identifier:pairwise; 231, argument_list; 231, 232; 232, identifier:args; 233, block; 233, 234; 233, 249; 234, if_statement; 234, 235; 234, 242; 235, not_operator; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:first; 239, identifier:startswith; 240, argument_list; 240, 241; 241, string:':'; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:first; 246, binary_operator:+; 246, 247; 246, 248; 247, string:':'; 248, identifier:first; 249, if_statement; 249, 250; 249, 257; 249, 292; 249, 325; 249, 359; 250, not_operator; 250, 251; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:first; 254, identifier:endswith; 255, argument_list; 255, 256; 256, string:'/'; 257, block; 257, 258; 257, 260; 257, 291; 258, expression_statement; 258, 259; 259, string:" longhand used: blah/blah "; 260, if_statement; 260, 261; 260, 266; 260, 280; 261, call; 261, 262; 261, 263; 262, identifier:isinstance; 263, argument_list; 263, 264; 263, 265; 264, identifier:second; 265, identifier:list; 266, block; 266, 267; 267, for_statement; 267, 268; 267, 269; 267, 270; 268, identifier:v; 269, identifier:second; 270, block; 270, 271; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:addeav; 276, argument_list; 276, 277; 276, 278; 276, 279; 277, identifier:entity; 278, identifier:first; 279, identifier:v; 280, else_clause; 280, 281; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:addeav; 287, argument_list; 287, 288; 287, 289; 287, 290; 288, identifier:entity; 289, identifier:first; 290, identifier:second; 291, continue_statement; 292, elif_clause; 292, 293; 292, 298; 293, call; 293, 294; 293, 295; 294, identifier:isinstance; 295, argument_list; 295, 296; 295, 297; 296, identifier:second; 297, identifier:dict; 298, block; 298, 299; 298, 301; 299, expression_statement; 299, 300; 300, string:" shorthand used: blah/, dict "; 301, for_statement; 301, 302; 301, 305; 301, 310; 302, pattern_list; 302, 303; 302, 304; 303, identifier:a; 304, identifier:v; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:second; 308, identifier:iteritems; 309, argument_list; 310, block; 310, 311; 310, 324; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:self; 315, identifier:addeav; 316, argument_list; 316, 317; 316, 318; 316, 323; 317, identifier:entity; 318, binary_operator:%; 318, 319; 318, 320; 319, string:"%s%s"; 320, tuple; 320, 321; 320, 322; 321, identifier:first; 322, identifier:a; 323, identifier:v; 324, continue_statement; 325, elif_clause; 325, 326; 325, 333; 326, call; 326, 327; 326, 328; 327, identifier:isinstance; 328, argument_list; 328, 329; 328, 330; 329, identifier:second; 330, tuple; 330, 331; 330, 332; 331, identifier:list; 332, identifier:tuple; 333, block; 333, 334; 333, 336; 334, expression_statement; 334, 335; 335, string:" shorthand used: blah/, list|tuple "; 336, for_statement; 336, 337; 336, 340; 336, 344; 337, pattern_list; 337, 338; 337, 339; 338, identifier:a; 339, identifier:v; 340, call; 340, 341; 340, 342; 341, identifier:pairwise; 342, argument_list; 342, 343; 343, identifier:second; 344, block; 344, 345; 344, 358; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:self; 349, identifier:addeav; 350, argument_list; 350, 351; 350, 352; 350, 357; 351, identifier:entity; 352, binary_operator:%; 352, 353; 352, 354; 353, string:"%s%s"; 354, tuple; 354, 355; 354, 356; 355, identifier:first; 356, identifier:a; 357, identifier:v; 358, continue_statement; 359, else_clause; 359, 360; 360, block; 360, 361; 361, raise_statement; 361, 362; 362, expression_list; 362, 363; 362, 364; 363, identifier:Exception; 364, binary_operator:%; 364, 365; 364, 366; 365, string:"invalid pair: %s : %s"; 366, tuple; 366, 367; 366, 368; 367, identifier:first; 368, identifier:second; 369, expression_statement; 369, 370; 370, string:"pass back the entity so it can be resolved after tx()"; 371, return_statement; 371, 372; 372, identifier:entity | def add(self, *args, **kwargs):
""" Accumulate datums for the transaction
Start a transaction on an existing db connection
>>> tx = TX(db)
Get get an entity object with a tempid
>>> ref = add()
>>> ref = add(0)
>>> ref = add(None)
>>> ref = add(False)
Entity id passed as first argument (int|long)
>>> tx.add(1, 'thing/name', 'value')
Shorthand form for multiple attributes sharing a root namespace
>>> tx.add(':thing/', {'name':'value', 'tag':'value'})
Attributes with a value of None are ignored
>>> tx.add(':thing/ignored', None)
Add multiple datums for an attribute with carinality:many
>>> tx.add(':thing/color', ['red','white','blue'])
"""
assert self.resp is None, "Transaction already committed"
entity, av_pairs, args = None, [], list(args)
if len(args):
if isinstance(args[0], (int, long)):
" first arg is an entity or tempid"
entity = E(args[0], tx=self)
elif isinstance(args[0], E):
" dont resuse entity from another tx"
if args[0]._tx is self:
entity = args[0]
else:
if int(args[0]) > 0:
" use the entity id on a new obj"
entity = E(int(args[0]), tx=self)
args[0] = None
" drop the first arg"
if entity is not None or args[0] in (None, False, 0):
v = args.pop(0)
" auto generate a temp id?"
if entity is None:
entity = E(self.ctmpid, tx=self)
self.ctmpid -= 1
" a,v from kwargs"
if len(args) == 0 and kwargs:
for a,v in kwargs.iteritems():
self.addeav(entity, a, v)
" a,v from args "
if len(args):
assert len(args) % 2 == 0, "imbalanced a,v in args: " % args
for first, second in pairwise(args):
if not first.startswith(':'):
first = ':' + first
if not first.endswith('/'):
" longhand used: blah/blah "
if isinstance(second, list):
for v in second:
self.addeav(entity, first, v)
else:
self.addeav(entity, first, second)
continue
elif isinstance(second, dict):
" shorthand used: blah/, dict "
for a,v in second.iteritems():
self.addeav(entity, "%s%s" % (first, a), v)
continue
elif isinstance(second, (list, tuple)):
" shorthand used: blah/, list|tuple "
for a,v in pairwise(second):
self.addeav(entity, "%s%s" % (first, a), v)
continue
else:
raise Exception, "invalid pair: %s : %s" % (first,second)
"pass back the entity so it can be resolved after tx()"
return entity |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:parsimonious_acr; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:tree; 5, identifier:character; 6, identifier:prediction_method; 7, identifier:states; 8, identifier:num_nodes; 9, identifier:num_tips; 10, block; 10, 11; 10, 13; 10, 20; 10, 26; 10, 30; 10, 43; 10, 52; 10, 175; 10, 261; 10, 358; 10, 373; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:initialise_parsimonious_states; 16, argument_list; 16, 17; 16, 18; 16, 19; 17, identifier:tree; 18, identifier:character; 19, identifier:states; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:uppass; 23, argument_list; 23, 24; 23, 25; 24, identifier:tree; 25, identifier:character; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:results; 29, list:[]; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:result; 33, dictionary; 33, 34; 33, 37; 33, 40; 34, pair; 34, 35; 34, 36; 35, identifier:STATES; 36, identifier:states; 37, pair; 37, 38; 37, 39; 38, identifier:NUM_NODES; 39, identifier:num_nodes; 40, pair; 40, 41; 40, 42; 41, identifier:NUM_TIPS; 42, identifier:num_tips; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:logger; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:logging; 49, identifier:getLogger; 50, argument_list; 50, 51; 51, string:'pastml'; 52, function_definition; 52, 53; 52, 54; 52, 57; 53, function_name:process_result; 54, parameters; 54, 55; 54, 56; 55, identifier:method; 56, identifier:feature; 57, block; 57, 58; 57, 71; 57, 79; 57, 98; 57, 104; 57, 116; 57, 156; 57, 162; 57, 168; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:out_feature; 61, conditional_expression:if; 61, 62; 61, 67; 61, 70; 62, call; 62, 63; 62, 64; 63, identifier:get_personalized_feature_name; 64, argument_list; 64, 65; 64, 66; 65, identifier:character; 66, identifier:method; 67, comparison_operator:!=; 67, 68; 67, 69; 68, identifier:prediction_method; 69, identifier:method; 70, identifier:character; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:res; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:result; 77, identifier:copy; 78, argument_list; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 91; 80, 92; 81, pattern_list; 81, 82; 81, 85; 81, 88; 82, subscript; 82, 83; 82, 84; 83, identifier:res; 84, identifier:NUM_SCENARIOS; 85, subscript; 85, 86; 85, 87; 86, identifier:res; 87, identifier:NUM_UNRESOLVED_NODES; 88, subscript; 88, 89; 88, 90; 89, identifier:res; 90, identifier:NUM_STATES_PER_NODE; 91, line_continuation:\; 92, call; 92, 93; 92, 94; 93, identifier:choose_parsimonious_states; 94, argument_list; 94, 95; 94, 96; 94, 97; 95, identifier:tree; 96, identifier:feature; 97, identifier:out_feature; 98, expression_statement; 98, 99; 99, augmented_assignment:/=; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:res; 102, identifier:NUM_STATES_PER_NODE; 103, identifier:num_nodes; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:res; 108, identifier:PERC_UNRESOLVED; 109, binary_operator:/; 109, 110; 109, 115; 110, binary_operator:*; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:res; 113, identifier:NUM_UNRESOLVED_NODES; 114, integer:100; 115, identifier:num_nodes; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:logger; 120, identifier:debug; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, concatenated_string; 124, 125; 124, 126; 125, string:'{} node{} unresolved ({:.2f}%) for {} by {}, '; 126, string:'i.e. {:.4f} state{} per node in average.'; 127, identifier:format; 128, argument_list; 128, 129; 128, 132; 128, 140; 128, 143; 128, 144; 128, 145; 128, 148; 129, subscript; 129, 130; 129, 131; 130, identifier:res; 131, identifier:NUM_UNRESOLVED_NODES; 132, conditional_expression:if; 132, 133; 132, 134; 132, 139; 133, string:'s are'; 134, comparison_operator:!=; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:res; 137, identifier:NUM_UNRESOLVED_NODES; 138, integer:1; 139, string:' is'; 140, subscript; 140, 141; 140, 142; 141, identifier:res; 142, identifier:PERC_UNRESOLVED; 143, identifier:character; 144, identifier:method; 145, subscript; 145, 146; 145, 147; 146, identifier:res; 147, identifier:NUM_STATES_PER_NODE; 148, conditional_expression:if; 148, 149; 148, 150; 148, 155; 149, string:'s'; 150, comparison_operator:>; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:res; 153, identifier:NUM_STATES_PER_NODE; 154, integer:1; 155, string:''; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 161; 158, subscript; 158, 159; 158, 160; 159, identifier:res; 160, identifier:CHARACTER; 161, identifier:out_feature; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:res; 166, identifier:METHOD; 167, identifier:method; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:results; 172, identifier:append; 173, argument_list; 173, 174; 174, identifier:res; 175, if_statement; 175, 176; 175, 181; 176, comparison_operator:in; 176, 177; 176, 178; 177, identifier:prediction_method; 178, set; 178, 179; 178, 180; 179, identifier:ACCTRAN; 180, identifier:MP; 181, block; 181, 182; 181, 190; 181, 203; 181, 210; 181, 220; 181, 226; 181, 234; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:feature; 185, call; 185, 186; 185, 187; 186, identifier:get_personalized_feature_name; 187, argument_list; 187, 188; 187, 189; 188, identifier:character; 189, identifier:PARS_STATES; 190, if_statement; 190, 191; 190, 194; 191, comparison_operator:==; 191, 192; 191, 193; 192, identifier:prediction_method; 193, identifier:MP; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:feature; 198, call; 198, 199; 198, 200; 199, identifier:get_personalized_feature_name; 200, argument_list; 200, 201; 200, 202; 201, identifier:feature; 202, identifier:ACCTRAN; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:acctran; 206, argument_list; 206, 207; 206, 208; 206, 209; 207, identifier:tree; 208, identifier:character; 209, identifier:feature; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, subscript; 212, 213; 212, 214; 213, identifier:result; 214, identifier:STEPS; 215, call; 215, 216; 215, 217; 216, identifier:get_num_parsimonious_steps; 217, argument_list; 217, 218; 217, 219; 218, identifier:tree; 219, identifier:feature; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 223; 222, identifier:process_result; 223, argument_list; 223, 224; 223, 225; 224, identifier:ACCTRAN; 225, identifier:feature; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:bu_feature; 229, call; 229, 230; 229, 231; 230, identifier:get_personalized_feature_name; 231, argument_list; 231, 232; 231, 233; 232, identifier:character; 233, identifier:BU_PARS_STATES; 234, for_statement; 234, 235; 234, 236; 234, 241; 235, identifier:node; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:tree; 239, identifier:traverse; 240, argument_list; 241, block; 241, 242; 241, 254; 242, if_statement; 242, 243; 242, 246; 243, comparison_operator:==; 243, 244; 243, 245; 244, identifier:prediction_method; 245, identifier:ACCTRAN; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:node; 251, identifier:del_feature; 252, argument_list; 252, 253; 253, identifier:bu_feature; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:node; 258, identifier:del_feature; 259, argument_list; 259, 260; 260, identifier:feature; 261, if_statement; 261, 262; 261, 265; 262, comparison_operator:!=; 262, 263; 262, 264; 263, identifier:prediction_method; 264, identifier:ACCTRAN; 265, block; 265, 266; 265, 273; 265, 281; 265, 296; 265, 309; 265, 343; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 269; 268, identifier:downpass; 269, argument_list; 269, 270; 269, 271; 269, 272; 270, identifier:tree; 271, identifier:character; 272, identifier:states; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:feature; 276, call; 276, 277; 276, 278; 277, identifier:get_personalized_feature_name; 278, argument_list; 278, 279; 278, 280; 279, identifier:character; 280, identifier:PARS_STATES; 281, if_statement; 281, 282; 281, 285; 282, comparison_operator:==; 282, 283; 282, 284; 283, identifier:prediction_method; 284, identifier:DOWNPASS; 285, block; 285, 286; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 291; 288, subscript; 288, 289; 288, 290; 289, identifier:result; 290, identifier:STEPS; 291, call; 291, 292; 291, 293; 292, identifier:get_num_parsimonious_steps; 293, argument_list; 293, 294; 293, 295; 294, identifier:tree; 295, identifier:feature; 296, if_statement; 296, 297; 296, 302; 297, comparison_operator:in; 297, 298; 297, 299; 298, identifier:prediction_method; 299, set; 299, 300; 299, 301; 300, identifier:DOWNPASS; 301, identifier:MP; 302, block; 302, 303; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 306; 305, identifier:process_result; 306, argument_list; 306, 307; 306, 308; 307, identifier:DOWNPASS; 308, identifier:feature; 309, if_statement; 309, 310; 309, 315; 310, comparison_operator:in; 310, 311; 310, 312; 311, identifier:prediction_method; 312, set; 312, 313; 312, 314; 313, identifier:DELTRAN; 314, identifier:MP; 315, block; 315, 316; 315, 322; 315, 337; 316, expression_statement; 316, 317; 317, call; 317, 318; 317, 319; 318, identifier:deltran; 319, argument_list; 319, 320; 319, 321; 320, identifier:tree; 321, identifier:character; 322, if_statement; 322, 323; 322, 326; 323, comparison_operator:==; 323, 324; 323, 325; 324, identifier:prediction_method; 325, identifier:DELTRAN; 326, block; 326, 327; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 332; 329, subscript; 329, 330; 329, 331; 330, identifier:result; 331, identifier:STEPS; 332, call; 332, 333; 332, 334; 333, identifier:get_num_parsimonious_steps; 334, argument_list; 334, 335; 334, 336; 335, identifier:tree; 336, identifier:feature; 337, expression_statement; 337, 338; 338, call; 338, 339; 338, 340; 339, identifier:process_result; 340, argument_list; 340, 341; 340, 342; 341, identifier:DELTRAN; 342, identifier:feature; 343, for_statement; 343, 344; 343, 345; 343, 350; 344, identifier:node; 345, call; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:tree; 348, identifier:traverse; 349, argument_list; 350, block; 350, 351; 351, expression_statement; 351, 352; 352, call; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:node; 355, identifier:del_feature; 356, argument_list; 356, 357; 357, identifier:feature; 358, expression_statement; 358, 359; 359, call; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:logger; 362, identifier:debug; 363, argument_list; 363, 364; 364, call; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, string:"Parsimonious reconstruction for {} requires {} state changes."; 367, identifier:format; 368, argument_list; 368, 369; 368, 370; 369, identifier:character; 370, subscript; 370, 371; 370, 372; 371, identifier:result; 372, identifier:STEPS; 373, return_statement; 373, 374; 374, identifier:results | def parsimonious_acr(tree, character, prediction_method, states, num_nodes, num_tips):
"""
Calculates parsimonious states on the tree and stores them in the corresponding feature.
:param states: numpy array of possible states
:param prediction_method: str, ACCTRAN (accelerated transformation), DELTRAN (delayed transformation) or DOWNPASS
:param tree: ete3.Tree, the tree of interest
:param character: str, character for which the parsimonious states are reconstructed
:return: dict, mapping between reconstruction parameters and values
"""
initialise_parsimonious_states(tree, character, states)
uppass(tree, character)
results = []
result = {STATES: states, NUM_NODES: num_nodes, NUM_TIPS: num_tips}
logger = logging.getLogger('pastml')
def process_result(method, feature):
out_feature = get_personalized_feature_name(character, method) if prediction_method != method else character
res = result.copy()
res[NUM_SCENARIOS], res[NUM_UNRESOLVED_NODES], res[NUM_STATES_PER_NODE] \
= choose_parsimonious_states(tree, feature, out_feature)
res[NUM_STATES_PER_NODE] /= num_nodes
res[PERC_UNRESOLVED] = res[NUM_UNRESOLVED_NODES] * 100 / num_nodes
logger.debug('{} node{} unresolved ({:.2f}%) for {} by {}, '
'i.e. {:.4f} state{} per node in average.'
.format(res[NUM_UNRESOLVED_NODES], 's are' if res[NUM_UNRESOLVED_NODES] != 1 else ' is',
res[PERC_UNRESOLVED], character, method,
res[NUM_STATES_PER_NODE], 's' if res[NUM_STATES_PER_NODE] > 1 else ''))
res[CHARACTER] = out_feature
res[METHOD] = method
results.append(res)
if prediction_method in {ACCTRAN, MP}:
feature = get_personalized_feature_name(character, PARS_STATES)
if prediction_method == MP:
feature = get_personalized_feature_name(feature, ACCTRAN)
acctran(tree, character, feature)
result[STEPS] = get_num_parsimonious_steps(tree, feature)
process_result(ACCTRAN, feature)
bu_feature = get_personalized_feature_name(character, BU_PARS_STATES)
for node in tree.traverse():
if prediction_method == ACCTRAN:
node.del_feature(bu_feature)
node.del_feature(feature)
if prediction_method != ACCTRAN:
downpass(tree, character, states)
feature = get_personalized_feature_name(character, PARS_STATES)
if prediction_method == DOWNPASS:
result[STEPS] = get_num_parsimonious_steps(tree, feature)
if prediction_method in {DOWNPASS, MP}:
process_result(DOWNPASS, feature)
if prediction_method in {DELTRAN, MP}:
deltran(tree, character)
if prediction_method == DELTRAN:
result[STEPS] = get_num_parsimonious_steps(tree, feature)
process_result(DELTRAN, feature)
for node in tree.traverse():
node.del_feature(feature)
logger.debug("Parsimonious reconstruction for {} requires {} state changes."
.format(character, result[STEPS]))
return results |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:load_seit_data; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:directory; 5, default_parameter; 5, 6; 5, 7; 6, identifier:frequency_file; 7, string:'frequencies.dat'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:data_prefix; 10, string:'volt_'; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kwargs; 13, block; 13, 14; 13, 16; 13, 31; 13, 49; 13, 50; 13, 65; 13, 66; 13, 70; 13, 100; 13, 109; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:frequencies; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:np; 22, identifier:loadtxt; 23, argument_list; 23, 24; 24, binary_operator:+; 24, 25; 24, 30; 25, binary_operator:+; 25, 26; 25, 27; 26, identifier:directory; 27, attribute; 27, 28; 27, 29; 28, identifier:os; 29, identifier:sep; 30, identifier:frequency_file; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:data_files; 34, call; 34, 35; 34, 36; 35, identifier:sorted; 36, argument_list; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:glob; 39, argument_list; 39, 40; 40, binary_operator:+; 40, 41; 40, 48; 41, binary_operator:+; 41, 42; 41, 47; 42, binary_operator:+; 42, 43; 42, 44; 43, identifier:directory; 44, attribute; 44, 45; 44, 46; 45, identifier:os; 46, identifier:sep; 47, identifier:data_prefix; 48, string:'*'; 49, comment; 50, if_statement; 50, 51; 50, 59; 51, comparison_operator:!=; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:frequencies; 54, identifier:size; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, identifier:data_files; 59, block; 59, 60; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:Exception; 63, argument_list; 63, 64; 64, string:'number of frequencies does not match number of data files'; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:data_list; 69, list:[]; 70, for_statement; 70, 71; 70, 74; 70, 79; 71, pattern_list; 71, 72; 71, 73; 72, identifier:frequency; 73, identifier:filename; 74, call; 74, 75; 74, 76; 75, identifier:zip; 76, argument_list; 76, 77; 76, 78; 77, identifier:frequencies; 78, identifier:data_files; 79, block; 79, 80; 79, 87; 79, 93; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:subdata; 83, call; 83, 84; 83, 85; 84, identifier:load_mod_file; 85, argument_list; 85, 86; 86, identifier:filename; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:subdata; 91, string:'frequency'; 92, identifier:frequency; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:data_list; 97, identifier:append; 98, argument_list; 98, 99; 99, identifier:subdata; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:df; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:pd; 106, identifier:concat; 107, argument_list; 107, 108; 108, identifier:data_list; 109, return_statement; 109, 110; 110, expression_list; 110, 111; 110, 112; 110, 113; 111, identifier:df; 112, None; 113, None | def load_seit_data(directory, frequency_file='frequencies.dat',
data_prefix='volt_', **kwargs):
"""Load sEIT data from data directory. This function loads data previously
exported from reda using reda.exporters.crtomo.write_files_to_directory
Parameters
----------
directory : string
input directory
frequency_file : string, optional
file (located in directory) that contains the frequencies
data_prefix: string, optional
for each frequency a corresponding data file must be present in the
input directory. Frequencies and files are matched by sorting the
frequencies AND the filenames, retrieved using glob and the
data_prefix
Returns
-------
df : pandas.DataFrame
A DataFrame suitable for the sEIT container
electrodes : None
No electrode data is imported
topography : None
No topography data is imported
"""
frequencies = np.loadtxt(directory + os.sep + frequency_file)
data_files = sorted(glob(directory + os.sep + data_prefix + '*'))
# check that the number of frequencies matches the number of data files
if frequencies.size != len(data_files):
raise Exception(
'number of frequencies does not match number of data files')
# load data
data_list = []
for frequency, filename in zip(frequencies, data_files):
subdata = load_mod_file(filename)
subdata['frequency'] = frequency
data_list.append(subdata)
df = pd.concat(data_list)
return df, None, None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parse_date; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:date; 6, block; 6, 7; 6, 9; 6, 32; 6, 70; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 15; 10, comparison_operator:is; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:date_format; 14, None; 15, block; 15, 16; 16, return_statement; 16, 17; 17, call; 17, 18; 17, 31; 18, attribute; 18, 19; 18, 30; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:datetime; 23, identifier:datetime; 24, identifier:strptime; 25, argument_list; 25, 26; 25, 27; 26, identifier:date; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:date_format; 30, identifier:date; 31, argument_list; 32, if_statement; 32, 33; 32, 40; 32, 41; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:re; 36, identifier:match; 37, argument_list; 37, 38; 37, 39; 38, string:'\d{8}$'; 39, identifier:date; 40, comment; 41, block; 41, 42; 42, return_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:datetime; 46, identifier:date; 47, argument_list; 47, 48; 48, list_splat; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:map; 51, argument_list; 51, 52; 51, 53; 52, identifier:int; 53, tuple; 53, 54; 53, 59; 53, 65; 54, subscript; 54, 55; 54, 56; 55, identifier:date; 56, slice; 56, 57; 56, 58; 57, colon; 58, integer:4; 59, subscript; 59, 60; 59, 61; 60, identifier:date; 61, slice; 61, 62; 61, 63; 61, 64; 62, integer:4; 63, colon; 64, integer:6; 65, subscript; 65, 66; 65, 67; 66, identifier:date; 67, slice; 67, 68; 67, 69; 68, integer:6; 69, colon; 70, try_statement; 70, 71; 70, 72; 70, 141; 70, 142; 71, comment; 72, block; 72, 73; 72, 83; 72, 84; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:parts; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:date_delim; 79, identifier:split; 80, argument_list; 80, 81; 80, 82; 81, identifier:date; 82, integer:2; 83, comment; 84, if_statement; 84, 85; 84, 91; 85, comparison_operator:==; 85, 86; 85, 90; 86, call; 86, 87; 86, 88; 87, identifier:len; 88, argument_list; 88, 89; 89, identifier:parts; 90, integer:3; 91, block; 91, 92; 92, if_statement; 92, 93; 92, 101; 92, 102; 92, 115; 93, comparison_operator:==; 93, 94; 93, 100; 94, call; 94, 95; 94, 96; 95, identifier:len; 96, argument_list; 96, 97; 97, subscript; 97, 98; 97, 99; 98, identifier:parts; 99, integer:0; 100, integer:4; 101, comment; 102, block; 102, 103; 103, return_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:datetime; 107, identifier:date; 108, argument_list; 108, 109; 109, list_splat; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:map; 112, argument_list; 112, 113; 112, 114; 113, identifier:int; 114, identifier:parts; 115, elif_clause; 115, 116; 115, 124; 115, 125; 116, comparison_operator:==; 116, 117; 116, 123; 117, call; 117, 118; 117, 119; 118, identifier:len; 119, argument_list; 119, 120; 120, subscript; 120, 121; 120, 122; 121, identifier:parts; 122, integer:2; 123, integer:4; 124, comment; 125, block; 125, 126; 126, return_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:datetime; 130, identifier:date; 131, argument_list; 131, 132; 132, list_splat; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:map; 135, argument_list; 135, 136; 135, 137; 136, identifier:int; 137, call; 137, 138; 137, 139; 138, identifier:reversed; 139, argument_list; 139, 140; 140, identifier:parts; 141, comment; 142, except_clause; 142, 143; 142, 144; 142, 145; 143, identifier:TypeError; 144, identifier:ValueError; 145, block; 145, 146; 146, raise_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:reader; 150, identifier:DataError; 151, argument_list; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, string:'Bad date format: "{}"'; 155, identifier:format; 156, argument_list; 156, 157; 157, identifier:date | def parse_date(self, date):
"""Parse the date and return a datetime object
The heuristic for determining the date is:
- if ``date_format`` is set, parse using strptime
- if one field of 8 digits, YYYYMMDD
- split by '-' or '/'
- (TODO: substitute string months with their numbers)
- if (2, 2, 4), DD-MM-YYYY (not the peculiar US order)
- if (4, 2, 2), YYYY-MM-DD
- ka-boom!
The issue of reliably discerning between DD-MM-YYYY (sane) vs.
MM-DD-YYYY (absurd, but Big In America), without being told what's
being used, is intractable.
Return a datetime.date object.
"""
if self.date_format is not None:
return datetime.datetime.strptime(date, self.date_format).date()
if re.match('\d{8}$', date):
# assume YYYYMMDD
return datetime.date(*map(int, (date[:4], date[4:6], date[6:])))
try:
# split by '-' or '/'
parts = date_delim.split(date, 2) # maxsplit=2
if len(parts) == 3:
if len(parts[0]) == 4:
# YYYY, MM, DD
return datetime.date(*map(int, parts))
elif len(parts[2]) == 4:
# DD, MM, YYYY
return datetime.date(*map(int, reversed(parts)))
# fail
except TypeError, ValueError:
raise reader.DataError('Bad date format: "{}"'.format(date)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:commit; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:if_match; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:wait; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:timeout; 13, None; 14, block; 14, 15; 14, 17; 14, 39; 14, 54; 14, 62; 14, 93; 14, 104; 14, 119; 14, 123; 14, 183; 14, 184; 14, 185; 14, 186; 14, 187; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 22; 18, not_operator; 18, 19; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_changes; 22, block; 22, 23; 22, 38; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:LOG; 27, identifier:debug; 28, argument_list; 28, 29; 28, 30; 28, 35; 29, string:"No changes available for %s: %s"; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:__class__; 34, identifier:__name__; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:resource_id; 38, return_statement; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:LOG; 43, identifier:debug; 44, argument_list; 44, 45; 44, 46; 44, 51; 45, string:"Apply all the changes on the current %s: %s"; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:__class__; 50, identifier:__name__; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:resource_id; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:client; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:_get_client; 61, argument_list; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:endpoint; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:_endpoint; 70, identifier:format; 71, argument_list; 71, 72; 71, 79; 71, 86; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:resource_id; 74, boolean_operator:or; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:resource_id; 78, string:""; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:parent_id; 81, boolean_operator:or; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:parent_id; 85, string:""; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:grandparent_id; 88, boolean_operator:or; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:grandparent_id; 92, string:""; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:request_body; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:dump; 100, argument_list; 100, 101; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:include_read_only; 103, False; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:response; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:client; 110, identifier:update_resource; 111, argument_list; 111, 112; 111, 113; 111, 116; 112, identifier:endpoint; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:data; 115, identifier:request_body; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:if_match; 118, identifier:if_match; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:elapsed_time; 122, integer:0; 123, while_statement; 123, 124; 123, 125; 123, 174; 124, identifier:wait; 125, block; 125, 126; 125, 132; 125, 133; 125, 141; 125, 149; 125, 163; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:refresh; 131, argument_list; 132, comment; 133, if_statement; 133, 134; 133, 139; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:is_ready; 138, argument_list; 139, block; 139, 140; 140, break_statement; 141, expression_statement; 141, 142; 142, augmented_assignment:+=; 142, 143; 142, 144; 143, identifier:elapsed_time; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:CONFIG; 147, identifier:HNV; 148, identifier:retry_interval; 149, if_statement; 149, 150; 149, 155; 150, boolean_operator:and; 150, 151; 150, 152; 151, identifier:timeout; 152, comparison_operator:>; 152, 153; 152, 154; 153, identifier:elapsed_time; 154, identifier:timeout; 155, block; 155, 156; 156, raise_statement; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:exception; 160, identifier:TimeOut; 161, argument_list; 161, 162; 162, string:"The request timed out."; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:time; 167, identifier:sleep; 168, argument_list; 168, 169; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:CONFIG; 172, identifier:HNV; 173, identifier:retry_interval; 174, else_clause; 174, 175; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:_reset_model; 181, argument_list; 181, 182; 182, identifier:response; 183, comment; 184, comment; 185, comment; 186, comment; 187, return_statement; 187, 188; 188, identifier:self | def commit(self, if_match=None, wait=True, timeout=None):
"""Apply all the changes on the current model.
:param wait: Whether to wait until the operation is completed
:param timeout: The maximum amount of time required for this
operation to be completed.
If optional :param wait: is True and timeout is None (the default),
block if necessary until the resource is available. If timeout is a
positive number, it blocks at most timeout seconds and raises the
`TimeOut` exception if no item was available within that time.
Otherwise (block is false), return a resource if one is immediately
available, else raise the `NotFound` exception (timeout is ignored
in that case).
"""
if not self._changes:
LOG.debug("No changes available for %s: %s",
self.__class__.__name__, self.resource_id)
return
LOG.debug("Apply all the changes on the current %s: %s",
self.__class__.__name__, self.resource_id)
client = self._get_client()
endpoint = self._endpoint.format(
resource_id=self.resource_id or "",
parent_id=self.parent_id or "",
grandparent_id=self.grandparent_id or "")
request_body = self.dump(include_read_only=False)
response = client.update_resource(endpoint, data=request_body,
if_match=if_match)
elapsed_time = 0
while wait:
self.refresh() # Update the representation of the current model
if self.is_ready():
break
elapsed_time += CONFIG.HNV.retry_interval
if timeout and elapsed_time > timeout:
raise exception.TimeOut("The request timed out.")
time.sleep(CONFIG.HNV.retry_interval)
else:
self._reset_model(response)
# NOTE(alexcoman): In order to keep backwards compatibility the
# `method: commit` will return a reference to itself.
# An example for that can be the following use case:
# label = client.Model().commit()
return self |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:plot_histograms; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:ertobj; 5, identifier:keys; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 12; 8, 33; 8, 47; 8, 59; 8, 63; 8, 73; 8, 127; 8, 418; 8, 427; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, if_statement; 12, 13; 12, 20; 12, 25; 13, call; 13, 14; 13, 15; 14, identifier:isinstance; 15, argument_list; 15, 16; 15, 17; 16, identifier:ertobj; 17, attribute; 17, 18; 17, 19; 18, identifier:pd; 19, identifier:DataFrame; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:df; 24, identifier:ertobj; 25, else_clause; 25, 26; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:df; 30, attribute; 30, 31; 30, 32; 31, identifier:ertobj; 32, identifier:data; 33, if_statement; 33, 34; 33, 41; 34, comparison_operator:==; 34, 35; 34, 40; 35, subscript; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:df; 38, identifier:shape; 39, integer:0; 40, integer:0; 41, block; 41, 42; 42, raise_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:Exception; 45, argument_list; 45, 46; 46, string:'No data present, cannot plot'; 47, if_statement; 47, 48; 47, 53; 48, call; 48, 49; 48, 50; 49, identifier:isinstance; 50, argument_list; 50, 51; 50, 52; 51, identifier:keys; 52, identifier:str; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:keys; 57, list:[keys, ]; 57, 58; 58, identifier:keys; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:figures; 62, dictionary; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:merge_figs; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:kwargs; 69, identifier:get; 70, argument_list; 70, 71; 70, 72; 71, string:'merge'; 72, True; 73, if_statement; 73, 74; 73, 75; 74, identifier:merge_figs; 75, block; 75, 76; 75, 80; 75, 87; 75, 93; 75, 101; 75, 118; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:nr_x; 79, integer:2; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:nr_y; 83, call; 83, 84; 83, 85; 84, identifier:len; 85, argument_list; 85, 86; 86, identifier:keys; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:size_x; 90, binary_operator:/; 90, 91; 90, 92; 91, integer:15; 92, float:2.54; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:size_y; 96, binary_operator:/; 96, 97; 96, 100; 97, binary_operator:*; 97, 98; 97, 99; 98, integer:5; 99, identifier:nr_y; 100, float:2.54; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, pattern_list; 103, 104; 103, 105; 104, identifier:fig; 105, identifier:axes_all; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:plt; 109, identifier:subplots; 110, argument_list; 110, 111; 110, 112; 110, 113; 111, identifier:nr_y; 112, identifier:nr_x; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:figsize; 115, tuple; 115, 116; 115, 117; 116, identifier:size_x; 117, identifier:size_y; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:axes_all; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:np; 124, identifier:atleast_2d; 125, argument_list; 125, 126; 126, identifier:axes_all; 127, for_statement; 127, 128; 127, 131; 127, 135; 128, pattern_list; 128, 129; 128, 130; 129, identifier:row_nr; 130, identifier:key; 131, call; 131, 132; 131, 133; 132, identifier:enumerate; 133, argument_list; 133, 134; 134, identifier:keys; 135, block; 135, 136; 135, 146; 135, 154; 135, 166; 135, 177; 135, 190; 135, 202; 135, 213; 135, 249; 135, 255; 135, 268; 135, 280; 135, 287; 135, 303; 135, 318; 135, 333; 135, 402; 135, 408; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:print; 139, argument_list; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, string:'Generating histogram plot for key: {0}'; 143, identifier:format; 144, argument_list; 144, 145; 145, identifier:key; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:subdata_raw; 149, attribute; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:df; 152, identifier:key; 153, identifier:values; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:subdata; 157, subscript; 157, 158; 157, 159; 158, identifier:subdata_raw; 159, unary_operator:~; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:np; 163, identifier:isnan; 164, argument_list; 164, 165; 165, identifier:subdata_raw; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:subdata; 169, subscript; 169, 170; 169, 171; 170, identifier:subdata; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:np; 174, identifier:isfinite; 175, argument_list; 175, 176; 176, identifier:subdata; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:subdata_log10_with_nan; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:np; 183, identifier:log10; 184, argument_list; 184, 185; 185, subscript; 185, 186; 185, 187; 186, identifier:subdata; 187, comparison_operator:>; 187, 188; 187, 189; 188, identifier:subdata; 189, integer:0; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:subdata_log10; 193, subscript; 193, 194; 193, 195; 194, identifier:subdata_log10_with_nan; 195, unary_operator:~; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:np; 199, identifier:isnan; 200, argument_list; 200, 201; 201, identifier:subdata_log10_with_nan; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:subdata_log10; 205, subscript; 205, 206; 205, 207; 206, identifier:subdata_log10; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:np; 210, identifier:isfinite; 211, argument_list; 211, 212; 212, identifier:subdata_log10; 213, if_statement; 213, 214; 213, 215; 213, 226; 214, identifier:merge_figs; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:axes; 219, call; 219, 220; 219, 225; 220, attribute; 220, 221; 220, 224; 221, subscript; 221, 222; 221, 223; 222, identifier:axes_all; 223, identifier:row_nr; 224, identifier:squeeze; 225, argument_list; 226, else_clause; 226, 227; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 233; 230, pattern_list; 230, 231; 230, 232; 231, identifier:fig; 232, identifier:axes; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:plt; 236, identifier:subplots; 237, argument_list; 237, 238; 237, 239; 237, 240; 238, integer:1; 239, integer:2; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:figsize; 242, tuple; 242, 243; 242, 246; 243, binary_operator:/; 243, 244; 243, 245; 244, integer:10; 245, float:2.54; 246, binary_operator:/; 246, 247; 246, 248; 247, integer:5; 248, float:2.54; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:ax; 252, subscript; 252, 253; 252, 254; 253, identifier:axes; 254, integer:0; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:ax; 259, identifier:hist; 260, argument_list; 260, 261; 260, 262; 261, identifier:subdata; 262, call; 262, 263; 262, 264; 263, identifier:_get_nr_bins; 264, argument_list; 264, 265; 265, attribute; 265, 266; 265, 267; 266, identifier:subdata; 267, identifier:size; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:ax; 272, identifier:set_xlabel; 273, argument_list; 273, 274; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:units; 277, identifier:get_label; 278, argument_list; 278, 279; 279, identifier:key; 280, expression_statement; 280, 281; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:ax; 284, identifier:set_ylabel; 285, argument_list; 285, 286; 286, string:'count'; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 294; 289, attribute; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:ax; 292, identifier:xaxis; 293, identifier:set_major_locator; 294, argument_list; 294, 295; 295, call; 295, 296; 295, 301; 296, attribute; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:mpl; 299, identifier:ticker; 300, identifier:MaxNLocator; 301, argument_list; 301, 302; 302, integer:5; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:ax; 307, identifier:tick_params; 308, argument_list; 308, 309; 308, 312; 308, 315; 309, keyword_argument; 309, 310; 309, 311; 310, identifier:axis; 311, string:'both'; 312, keyword_argument; 312, 313; 312, 314; 313, identifier:which; 314, string:'major'; 315, keyword_argument; 315, 316; 315, 317; 316, identifier:labelsize; 317, integer:6; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:ax; 322, identifier:tick_params; 323, argument_list; 323, 324; 323, 327; 323, 330; 324, keyword_argument; 324, 325; 324, 326; 325, identifier:axis; 326, string:'both'; 327, keyword_argument; 327, 328; 327, 329; 328, identifier:which; 329, string:'minor'; 330, keyword_argument; 330, 331; 330, 332; 331, identifier:labelsize; 332, integer:6; 333, if_statement; 333, 334; 333, 339; 333, 398; 334, comparison_operator:>; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:subdata_log10; 337, identifier:size; 338, integer:0; 339, block; 339, 340; 339, 346; 339, 359; 339, 375; 339, 382; 340, expression_statement; 340, 341; 341, assignment; 341, 342; 341, 343; 342, identifier:ax; 343, subscript; 343, 344; 343, 345; 344, identifier:axes; 345, integer:1; 346, expression_statement; 346, 347; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:ax; 350, identifier:hist; 351, argument_list; 351, 352; 351, 353; 352, identifier:subdata_log10; 353, call; 353, 354; 353, 355; 354, identifier:_get_nr_bins; 355, argument_list; 355, 356; 356, attribute; 356, 357; 356, 358; 357, identifier:subdata; 358, identifier:size; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:ax; 363, identifier:set_xlabel; 364, argument_list; 364, 365; 365, binary_operator:+; 365, 366; 365, 374; 366, binary_operator:+; 366, 367; 366, 368; 367, string:r'$log_{10}($'; 368, call; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:units; 371, identifier:get_label; 372, argument_list; 372, 373; 373, identifier:key; 374, string:')'; 375, expression_statement; 375, 376; 376, call; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:ax; 379, identifier:set_ylabel; 380, argument_list; 380, 381; 381, string:'count'; 382, expression_statement; 382, 383; 383, call; 383, 384; 383, 389; 384, attribute; 384, 385; 384, 388; 385, attribute; 385, 386; 385, 387; 386, identifier:ax; 387, identifier:xaxis; 388, identifier:set_major_locator; 389, argument_list; 389, 390; 390, call; 390, 391; 390, 396; 391, attribute; 391, 392; 391, 395; 392, attribute; 392, 393; 392, 394; 393, identifier:mpl; 394, identifier:ticker; 395, identifier:MaxNLocator; 396, argument_list; 396, 397; 397, integer:5; 398, else_clause; 398, 399; 399, block; 399, 400; 399, 401; 400, pass_statement; 401, comment; 402, expression_statement; 402, 403; 403, call; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:fig; 406, identifier:tight_layout; 407, argument_list; 408, if_statement; 408, 409; 408, 411; 409, not_operator; 409, 410; 410, identifier:merge_figs; 411, block; 411, 412; 412, expression_statement; 412, 413; 413, assignment; 413, 414; 413, 417; 414, subscript; 414, 415; 414, 416; 415, identifier:figures; 416, identifier:key; 417, identifier:fig; 418, if_statement; 418, 419; 418, 420; 419, identifier:merge_figs; 420, block; 420, 421; 421, expression_statement; 421, 422; 422, assignment; 422, 423; 422, 426; 423, subscript; 423, 424; 423, 425; 424, identifier:figures; 425, string:'all'; 426, identifier:fig; 427, return_statement; 427, 428; 428, identifier:figures | def plot_histograms(ertobj, keys, **kwargs):
"""Generate histograms for one or more keys in the given container.
Parameters
----------
ertobj : container instance or :class:`pandas.DataFrame`
data object which contains the data.
keys : str or list of strings
which keys (column names) to plot
merge : bool, optional
if True, then generate only one figure with all key-plots as columns
(default True)
log10plot : bool, optional
default: True
extra_dims : list, optional
Examples
--------
>>> from reda.plotters import plot_histograms
>>> from reda.testing import ERTContainer
>>> figs_dict = plot_histograms(ERTContainer, "r", merge=False)
Generating histogram plot for key: r
Returns
-------
figures : dict
dictionary with the generated histogram figures
"""
# you can either provide a DataFrame or an ERT object
if isinstance(ertobj, pd.DataFrame):
df = ertobj
else:
df = ertobj.data
if df.shape[0] == 0:
raise Exception('No data present, cannot plot')
if isinstance(keys, str):
keys = [keys, ]
figures = {}
merge_figs = kwargs.get('merge', True)
if merge_figs:
nr_x = 2
nr_y = len(keys)
size_x = 15 / 2.54
size_y = 5 * nr_y / 2.54
fig, axes_all = plt.subplots(nr_y, nr_x, figsize=(size_x, size_y))
axes_all = np.atleast_2d(axes_all)
for row_nr, key in enumerate(keys):
print('Generating histogram plot for key: {0}'.format(key))
subdata_raw = df[key].values
subdata = subdata_raw[~np.isnan(subdata_raw)]
subdata = subdata[np.isfinite(subdata)]
subdata_log10_with_nan = np.log10(subdata[subdata > 0])
subdata_log10 = subdata_log10_with_nan[~np.isnan(
subdata_log10_with_nan)
]
subdata_log10 = subdata_log10[np.isfinite(subdata_log10)]
if merge_figs:
axes = axes_all[row_nr].squeeze()
else:
fig, axes = plt.subplots(1, 2, figsize=(10 / 2.54, 5 / 2.54))
ax = axes[0]
ax.hist(
subdata,
_get_nr_bins(subdata.size),
)
ax.set_xlabel(
units.get_label(key)
)
ax.set_ylabel('count')
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(5))
ax.tick_params(axis='both', which='major', labelsize=6)
ax.tick_params(axis='both', which='minor', labelsize=6)
if subdata_log10.size > 0:
ax = axes[1]
ax.hist(
subdata_log10,
_get_nr_bins(subdata.size),
)
ax.set_xlabel(r'$log_{10}($' + units.get_label(key) + ')')
ax.set_ylabel('count')
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(5))
else:
pass
# del(axes[1])
fig.tight_layout()
if not merge_figs:
figures[key] = fig
if merge_figs:
figures['all'] = fig
return figures |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:plot_histograms_extra_dims; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:dataobj; 5, identifier:keys; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 32; 8, 61; 8, 65; 8, 102; 8, 106; 8, 113; 8, 123; 8, 147; 8, 148; 8, 158; 8, 166; 8, 174; 8, 188; 8, 196; 8, 204; 8, 227; 8, 236; 8, 240; 8, 430; 8, 431; 8, 450; 8, 470; 8, 476; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 19; 11, 24; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 16; 15, identifier:dataobj; 16, attribute; 16, 17; 16, 18; 17, identifier:pd; 18, identifier:DataFrame; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:df_raw; 23, identifier:dataobj; 24, else_clause; 24, 25; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:df_raw; 29, attribute; 29, 30; 29, 31; 30, identifier:dataobj; 31, identifier:data; 32, if_statement; 32, 33; 32, 40; 32, 55; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:kwargs; 36, identifier:get; 37, argument_list; 37, 38; 37, 39; 38, string:'subquery'; 39, False; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:df; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:df_raw; 47, identifier:query; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:kwargs; 52, identifier:get; 53, argument_list; 53, 54; 54, string:'subquery'; 55, else_clause; 55, 56; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:df; 60, identifier:df_raw; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:split_timestamps; 64, True; 65, if_statement; 65, 66; 65, 67; 65, 90; 66, identifier:split_timestamps; 67, block; 67, 68; 67, 77; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:group_timestamps; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:df; 74, identifier:groupby; 75, argument_list; 75, 76; 76, string:'timestep'; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:N_ts; 80, call; 80, 81; 80, 82; 81, identifier:len; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 89; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:group_timestamps; 87, identifier:groups; 88, identifier:keys; 89, argument_list; 90, else_clause; 90, 91; 91, block; 91, 92; 91, 98; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:group_timestamps; 95, tuple; 95, 96; 95, 97; 96, string:'all'; 97, identifier:df; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:N_ts; 101, integer:1; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:columns; 105, identifier:keys; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:N_c; 109, call; 109, 110; 109, 111; 110, identifier:len; 111, argument_list; 111, 112; 112, identifier:columns; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:plot_log10; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:kwargs; 119, identifier:get; 120, argument_list; 120, 121; 120, 122; 121, string:'log10plot'; 122, False; 123, if_statement; 123, 124; 123, 125; 123, 136; 124, identifier:plot_log10; 125, block; 125, 126; 125, 132; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:transformers; 129, list:['lin', 'log10']; 129, 130; 129, 131; 130, string:'lin'; 131, string:'log10'; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:N_log10; 135, integer:2; 136, else_clause; 136, 137; 137, block; 137, 138; 137, 143; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:transformers; 141, list:['lin', ]; 141, 142; 142, string:'lin'; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:N_log10; 146, integer:1; 147, comment; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:Nx_max; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:kwargs; 154, identifier:get; 155, argument_list; 155, 156; 155, 157; 156, string:'Nx'; 157, integer:4; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:N; 161, binary_operator:*; 161, 162; 161, 165; 162, binary_operator:*; 162, 163; 162, 164; 163, identifier:N_ts; 164, identifier:N_c; 165, identifier:N_log10; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:Nx; 169, call; 169, 170; 169, 171; 170, identifier:min; 171, argument_list; 171, 172; 171, 173; 172, identifier:Nx_max; 173, identifier:N; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:Ny; 177, call; 177, 178; 177, 179; 178, identifier:int; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:np; 183, identifier:ceil; 184, argument_list; 184, 185; 185, binary_operator:/; 185, 186; 185, 187; 186, identifier:N; 187, identifier:Nx; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:size_x; 191, binary_operator:/; 191, 192; 191, 195; 192, binary_operator:*; 192, 193; 192, 194; 193, integer:5; 194, identifier:Nx; 195, float:2.54; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:size_y; 199, binary_operator:/; 199, 200; 199, 203; 200, binary_operator:*; 200, 201; 200, 202; 201, integer:5; 202, identifier:Ny; 203, float:2.54; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 209; 206, pattern_list; 206, 207; 206, 208; 207, identifier:fig; 208, identifier:axes; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:plt; 212, identifier:subplots; 213, argument_list; 213, 214; 213, 215; 213, 216; 213, 221; 213, 224; 214, identifier:Ny; 215, identifier:Nx; 216, keyword_argument; 216, 217; 216, 218; 217, identifier:figsize; 218, tuple; 218, 219; 218, 220; 219, identifier:size_x; 220, identifier:size_y; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:sharex; 223, True; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:sharey; 226, True; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:axes; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:np; 233, identifier:atleast_2d; 234, argument_list; 234, 235; 235, identifier:axes; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:index; 239, integer:0; 240, for_statement; 240, 241; 240, 244; 240, 245; 241, pattern_list; 241, 242; 241, 243; 242, identifier:ts_name; 243, identifier:tgroup; 244, identifier:group_timestamps; 245, block; 245, 246; 246, for_statement; 246, 247; 246, 248; 246, 249; 247, identifier:column; 248, identifier:columns; 249, block; 249, 250; 250, for_statement; 250, 251; 250, 252; 250, 253; 250, 254; 251, identifier:transformer; 252, identifier:transformers; 253, comment; 254, block; 254, 255; 254, 263; 254, 275; 254, 286; 254, 331; 254, 339; 254, 352; 254, 364; 254, 371; 254, 387; 254, 402; 254, 417; 254, 426; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:subdata_raw; 258, attribute; 258, 259; 258, 262; 259, subscript; 259, 260; 259, 261; 260, identifier:tgroup; 261, identifier:column; 262, identifier:values; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:subdata; 266, subscript; 266, 267; 266, 268; 267, identifier:subdata_raw; 268, unary_operator:~; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:np; 272, identifier:isnan; 273, argument_list; 273, 274; 274, identifier:subdata_raw; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:subdata; 278, subscript; 278, 279; 278, 280; 279, identifier:subdata; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:np; 283, identifier:isfinite; 284, argument_list; 284, 285; 285, identifier:subdata; 286, if_statement; 286, 287; 286, 290; 287, comparison_operator:==; 287, 288; 287, 289; 288, identifier:transformer; 289, string:'log10'; 290, block; 290, 291; 290, 304; 290, 316; 290, 327; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:subdata_log10_with_nan; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:np; 297, identifier:log10; 298, argument_list; 298, 299; 299, subscript; 299, 300; 299, 301; 300, identifier:subdata; 301, comparison_operator:>; 301, 302; 301, 303; 302, identifier:subdata; 303, integer:0; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:subdata_log10; 307, subscript; 307, 308; 307, 309; 308, identifier:subdata_log10_with_nan; 309, unary_operator:~; 309, 310; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:np; 313, identifier:isnan; 314, argument_list; 314, 315; 315, identifier:subdata_log10_with_nan; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:subdata_log10; 319, subscript; 319, 320; 319, 321; 320, identifier:subdata_log10; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:np; 324, identifier:isfinite; 325, argument_list; 325, 326; 326, identifier:subdata_log10; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:subdata; 330, identifier:subdata_log10; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 334; 333, identifier:ax; 334, subscript; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:axes; 337, identifier:flat; 338, identifier:index; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:ax; 343, identifier:hist; 344, argument_list; 344, 345; 344, 346; 345, identifier:subdata; 346, call; 346, 347; 346, 348; 347, identifier:_get_nr_bins; 348, argument_list; 348, 349; 349, attribute; 349, 350; 349, 351; 350, identifier:subdata; 351, identifier:size; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:ax; 356, identifier:set_xlabel; 357, argument_list; 357, 358; 358, call; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:units; 361, identifier:get_label; 362, argument_list; 362, 363; 363, identifier:column; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:ax; 368, identifier:set_ylabel; 369, argument_list; 369, 370; 370, string:'count'; 371, expression_statement; 371, 372; 372, call; 372, 373; 372, 378; 373, attribute; 373, 374; 373, 377; 374, attribute; 374, 375; 374, 376; 375, identifier:ax; 376, identifier:xaxis; 377, identifier:set_major_locator; 378, argument_list; 378, 379; 379, call; 379, 380; 379, 385; 380, attribute; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:mpl; 383, identifier:ticker; 384, identifier:MaxNLocator; 385, argument_list; 385, 386; 386, integer:3; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:ax; 391, identifier:tick_params; 392, argument_list; 392, 393; 392, 396; 392, 399; 393, keyword_argument; 393, 394; 393, 395; 394, identifier:axis; 395, string:'both'; 396, keyword_argument; 396, 397; 396, 398; 397, identifier:which; 398, string:'major'; 399, keyword_argument; 399, 400; 399, 401; 400, identifier:labelsize; 401, integer:6; 402, expression_statement; 402, 403; 403, call; 403, 404; 403, 407; 404, attribute; 404, 405; 404, 406; 405, identifier:ax; 406, identifier:tick_params; 407, argument_list; 407, 408; 407, 411; 407, 414; 408, keyword_argument; 408, 409; 408, 410; 409, identifier:axis; 410, string:'both'; 411, keyword_argument; 411, 412; 411, 413; 412, identifier:which; 413, string:'minor'; 414, keyword_argument; 414, 415; 414, 416; 415, identifier:labelsize; 416, integer:6; 417, expression_statement; 417, 418; 418, call; 418, 419; 418, 422; 419, attribute; 419, 420; 419, 421; 420, identifier:ax; 421, identifier:set_title; 422, argument_list; 422, 423; 423, binary_operator:%; 423, 424; 423, 425; 424, string:"timestep: %d"; 425, identifier:ts_name; 426, expression_statement; 426, 427; 427, augmented_assignment:+=; 427, 428; 427, 429; 428, identifier:index; 429, integer:1; 430, comment; 431, for_statement; 431, 432; 431, 433; 431, 442; 432, identifier:ax; 433, attribute; 433, 434; 433, 441; 434, subscript; 434, 435; 434, 436; 434, 438; 435, identifier:axes; 436, slice; 436, 437; 437, colon; 438, slice; 438, 439; 438, 440; 439, integer:1; 440, colon; 441, identifier:flat; 442, block; 442, 443; 443, expression_statement; 443, 444; 444, call; 444, 445; 444, 448; 445, attribute; 445, 446; 445, 447; 446, identifier:ax; 447, identifier:set_ylabel; 448, argument_list; 448, 449; 449, string:''; 450, for_statement; 450, 451; 450, 452; 450, 462; 451, identifier:ax; 452, attribute; 452, 453; 452, 461; 453, subscript; 453, 454; 453, 455; 453, 459; 454, identifier:axes; 455, slice; 455, 456; 455, 457; 456, colon; 457, unary_operator:-; 457, 458; 458, integer:1; 459, slice; 459, 460; 460, colon; 461, identifier:flat; 462, block; 462, 463; 463, expression_statement; 463, 464; 464, call; 464, 465; 464, 468; 465, attribute; 465, 466; 465, 467; 466, identifier:ax; 467, identifier:set_xlabel; 468, argument_list; 468, 469; 469, string:''; 470, expression_statement; 470, 471; 471, call; 471, 472; 471, 475; 472, attribute; 472, 473; 472, 474; 473, identifier:fig; 474, identifier:tight_layout; 475, argument_list; 476, return_statement; 476, 477; 477, identifier:fig | def plot_histograms_extra_dims(dataobj, keys, **kwargs):
"""Produce histograms grouped by the extra dimensions.
Extra dimensions are:
* timesteps
* frequency
Parameters
----------
dataobj : :py:class:`pandas.DataFrame` or reda container
The data container/data frame which holds the data
keys: list|tuple|iterable
The keys (columns) of the dataobj to plot
subquery : string, optional
?
log10plot: bool
if True, generate linear and log10 versions of the histogram
Nx : int, optional
?
Returns
-------
Examples
--------
>>> import reda.testing.containers
>>> ert = reda.testing.containers.ERTContainer_nr
>>> import reda.plotters.histograms as RH
>>> fig = RH.plot_histograms_extra_dims(ert, ['r', ])
>>> import reda.testing.containers
>>> ert = reda.testing.containers.ERTContainer_nr
>>> import reda.plotters.histograms as RH
>>> fig = RH.plot_histograms_extra_dims(ert, ['r', 'a'])
"""
if isinstance(dataobj, pd.DataFrame):
df_raw = dataobj
else:
df_raw = dataobj.data
if kwargs.get('subquery', False):
df = df_raw.query(kwargs.get('subquery'))
else:
df = df_raw
split_timestamps = True
if split_timestamps:
group_timestamps = df.groupby('timestep')
N_ts = len(group_timestamps.groups.keys())
else:
group_timestamps = ('all', df)
N_ts = 1
columns = keys
N_c = len(columns)
plot_log10 = kwargs.get('log10plot', False)
if plot_log10:
transformers = ['lin', 'log10']
N_log10 = 2
else:
transformers = ['lin', ]
N_log10 = 1
# determine layout of plots
Nx_max = kwargs.get('Nx', 4)
N = N_ts * N_c * N_log10
Nx = min(Nx_max, N)
Ny = int(np.ceil(N / Nx))
size_x = 5 * Nx / 2.54
size_y = 5 * Ny / 2.54
fig, axes = plt.subplots(Ny, Nx, figsize=(size_x, size_y), sharex=True,
sharey=True)
axes = np.atleast_2d(axes)
index = 0
for ts_name, tgroup in group_timestamps:
for column in columns:
for transformer in transformers:
# print('{0}-{1}-{2}'.format(ts_name, column, transformer))
subdata_raw = tgroup[column].values
subdata = subdata_raw[~np.isnan(subdata_raw)]
subdata = subdata[np.isfinite(subdata)]
if transformer == 'log10':
subdata_log10_with_nan = np.log10(subdata[subdata > 0])
subdata_log10 = subdata_log10_with_nan[~np.isnan(
subdata_log10_with_nan)
]
subdata_log10 = subdata_log10[np.isfinite(subdata_log10)]
subdata = subdata_log10
ax = axes.flat[index]
ax.hist(
subdata,
_get_nr_bins(subdata.size),
)
ax.set_xlabel(
units.get_label(column)
)
ax.set_ylabel('count')
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(3))
ax.tick_params(axis='both', which='major', labelsize=6)
ax.tick_params(axis='both', which='minor', labelsize=6)
ax.set_title("timestep: %d" % ts_name)
index += 1
# remove some labels
for ax in axes[:, 1:].flat:
ax.set_ylabel('')
for ax in axes[:-1, :].flat:
ax.set_xlabel('')
fig.tight_layout()
return fig |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:request; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 4, identifier:self; 5, identifier:method; 6, identifier:url; 7, default_parameter; 7, 8; 7, 9; 8, identifier:params; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:headers; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:data; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:json; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:token_refresh_attempts; 21, integer:2; 22, dictionary_splat_pattern; 22, 23; 23, identifier:kwargs; 24, block; 24, 25; 24, 27; 24, 56; 24, 62; 24, 71; 24, 81; 24, 90; 24, 99; 24, 124; 24, 134; 24, 148; 24, 177; 25, expression_statement; 25, 26; 26, comment; 27, if_statement; 27, 28; 27, 34; 28, call; 28, 29; 28, 30; 29, identifier:all; 30, argument_list; 30, 31; 31, list:[data, json]; 31, 32; 31, 33; 32, identifier:data; 33, identifier:json; 34, block; 34, 35; 34, 42; 34, 49; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:msg; 38, parenthesized_expression; 38, 39; 39, concatenated_string; 39, 40; 39, 41; 40, string:'"data" and "json" request parameters can not be used '; 41, string:'at the same time'; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:logging; 46, identifier:warn; 47, argument_list; 47, 48; 48, identifier:msg; 49, raise_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:exceptions; 53, identifier:GCPHTTPError; 54, argument_list; 54, 55; 55, identifier:msg; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:req_headers; 59, boolean_operator:or; 59, 60; 59, 61; 60, identifier:headers; 61, dictionary; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:req_headers; 66, identifier:update; 67, argument_list; 67, 68; 68, attribute; 68, 69; 68, 70; 69, identifier:_utils; 70, identifier:DEFAULT_REQUEST_HEADERS; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:req_kwargs; 74, dictionary; 74, 75; 74, 78; 75, pair; 75, 76; 75, 77; 76, string:'params'; 77, identifier:params; 78, pair; 78, 79; 78, 80; 79, string:'headers'; 80, identifier:req_headers; 81, if_statement; 81, 82; 81, 83; 82, identifier:data; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:req_kwargs; 88, string:'data'; 89, identifier:data; 90, if_statement; 90, 91; 90, 92; 91, identifier:json; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:req_kwargs; 97, string:'json'; 98, identifier:json; 99, if_statement; 99, 100; 99, 101; 100, identifier:token_refresh_attempts; 101, block; 101, 102; 102, if_statement; 102, 103; 102, 110; 103, not_operator; 103, 104; 104, await; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:valid_token_set; 109, argument_list; 110, block; 110, 111; 110, 120; 111, expression_statement; 111, 112; 112, await; 112, 113; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_auth_client; 118, identifier:refresh_token; 119, argument_list; 120, expression_statement; 120, 121; 121, augmented_assignment:-=; 121, 122; 121, 123; 122, identifier:token_refresh_attempts; 123, integer:1; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:req_headers; 128, identifier:update; 129, argument_list; 129, 130; 130, dictionary; 130, 131; 131, pair; 131, 132; 131, 133; 132, string:'Authorization'; 133, string:f'Bearer {self._auth_client.token}'; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:request_id; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:kwargs; 140, identifier:get; 141, argument_list; 141, 142; 141, 143; 142, string:'request_id'; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:uuid; 146, identifier:uuid4; 147, argument_list; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:logging; 152, identifier:debug; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:_utils; 158, identifier:REQ_LOG_FMT; 159, identifier:format; 160, argument_list; 160, 161; 160, 164; 160, 171; 160, 174; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:request_id; 163, identifier:request_id; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:method; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:method; 169, identifier:upper; 170, argument_list; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:url; 173, identifier:url; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:kwargs; 176, identifier:req_kwargs; 177, try_statement; 177, 178; 177, 308; 177, 342; 177, 353; 178, block; 178, 179; 179, with_statement; 179, 180; 179, 196; 180, with_clause; 180, 181; 181, with_item; 181, 182; 182, as_pattern; 182, 183; 182, 194; 183, call; 183, 184; 183, 189; 184, attribute; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:_session; 188, identifier:request; 189, argument_list; 189, 190; 189, 191; 189, 192; 190, identifier:method; 191, identifier:url; 192, dictionary_splat; 192, 193; 193, identifier:req_kwargs; 194, as_pattern_target; 194, 195; 195, identifier:resp; 196, block; 196, 197; 196, 226; 196, 241; 196, 295; 196, 301; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:log_kw; 200, dictionary; 200, 201; 200, 204; 200, 211; 200, 216; 200, 221; 201, pair; 201, 202; 201, 203; 202, string:'request_id'; 203, identifier:request_id; 204, pair; 204, 205; 204, 206; 205, string:'method'; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:method; 209, identifier:upper; 210, argument_list; 211, pair; 211, 212; 211, 213; 212, string:'url'; 213, attribute; 213, 214; 213, 215; 214, identifier:resp; 215, identifier:url; 216, pair; 216, 217; 216, 218; 217, string:'status'; 218, attribute; 218, 219; 218, 220; 219, identifier:resp; 220, identifier:status; 221, pair; 221, 222; 221, 223; 222, string:'reason'; 223, attribute; 223, 224; 223, 225; 224, identifier:resp; 225, identifier:reason; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:logging; 230, identifier:debug; 231, argument_list; 231, 232; 232, call; 232, 233; 232, 238; 233, attribute; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:_utils; 236, identifier:RESP_LOG_FMT; 237, identifier:format; 238, argument_list; 238, 239; 239, dictionary_splat; 239, 240; 240, identifier:log_kw; 241, if_statement; 241, 242; 241, 247; 242, comparison_operator:in; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:resp; 245, identifier:status; 246, identifier:REFRESH_STATUS_CODES; 247, block; 247, 248; 247, 257; 247, 286; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:logging; 252, identifier:warning; 253, argument_list; 253, 254; 254, concatenated_string; 254, 255; 254, 256; 255, string:f'[{request_id}] HTTP Status Code {resp.status}'; 256, string:f' returned requesting {resp.url}: {resp.reason}'; 257, if_statement; 257, 258; 257, 259; 258, identifier:token_refresh_attempts; 259, block; 259, 260; 259, 269; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:logging; 264, identifier:info; 265, argument_list; 265, 266; 266, concatenated_string; 266, 267; 266, 268; 267, string:f'[{request_id}] Attempting request to {resp.url} '; 268, string:'again.'; 269, return_statement; 269, 270; 270, await; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:request; 275, argument_list; 275, 276; 275, 277; 275, 278; 275, 281; 275, 284; 276, identifier:method; 277, identifier:url; 278, keyword_argument; 278, 279; 278, 280; 279, identifier:token_refresh_attempts; 280, identifier:token_refresh_attempts; 281, keyword_argument; 281, 282; 281, 283; 282, identifier:request_id; 283, identifier:request_id; 284, dictionary_splat; 284, 285; 285, identifier:req_kwargs; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:logging; 290, identifier:warning; 291, argument_list; 291, 292; 292, concatenated_string; 292, 293; 292, 294; 293, string:f'[{request_id}] Max attempts refreshing auth token '; 294, string:f'exhausted while requesting {resp.url}'; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:resp; 299, identifier:raise_for_status; 300, argument_list; 301, return_statement; 301, 302; 302, await; 302, 303; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:resp; 306, identifier:text; 307, argument_list; 308, except_clause; 308, 309; 308, 315; 308, 316; 308, 317; 309, as_pattern; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:aiohttp; 312, identifier:ClientResponseError; 313, as_pattern_target; 313, 314; 314, identifier:e; 315, comment; 316, comment; 317, block; 317, 318; 317, 322; 317, 332; 318, expression_statement; 318, 319; 319, assignment; 319, 320; 319, 321; 320, identifier:msg; 321, string:f'[{request_id}] HTTP error response from {resp.url}: {e}'; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:logging; 326, identifier:error; 327, argument_list; 327, 328; 327, 329; 328, identifier:msg; 329, keyword_argument; 329, 330; 329, 331; 330, identifier:exc_info; 331, identifier:e; 332, raise_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:exceptions; 336, identifier:GCPHTTPResponseError; 337, argument_list; 337, 338; 337, 339; 338, identifier:msg; 339, attribute; 339, 340; 339, 341; 340, identifier:resp; 341, identifier:status; 342, except_clause; 342, 343; 342, 349; 342, 350; 343, as_pattern; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:exceptions; 346, identifier:GCPHTTPResponseError; 347, as_pattern_target; 347, 348; 348, identifier:e; 349, comment; 350, block; 350, 351; 351, raise_statement; 351, 352; 352, identifier:e; 353, except_clause; 353, 354; 353, 358; 354, as_pattern; 354, 355; 354, 356; 355, identifier:Exception; 356, as_pattern_target; 356, 357; 357, identifier:e; 358, block; 358, 359; 358, 363; 358, 373; 359, expression_statement; 359, 360; 360, assignment; 360, 361; 360, 362; 361, identifier:msg; 362, string:f'[{request_id}] Request call failed: {e}'; 363, expression_statement; 363, 364; 364, call; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:logging; 367, identifier:error; 368, argument_list; 368, 369; 368, 370; 369, identifier:msg; 370, keyword_argument; 370, 371; 370, 372; 371, identifier:exc_info; 372, identifier:e; 373, raise_statement; 373, 374; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:exceptions; 377, identifier:GCPHTTPError; 378, argument_list; 378, 379; 379, identifier:msg | async def request(self, method, url, params=None, headers=None,
data=None, json=None, token_refresh_attempts=2,
**kwargs):
"""Make an asynchronous HTTP request.
Args:
method (str): HTTP method to use for the request.
url (str): URL to be requested.
params (dict): (optional) Query parameters for the request.
Defaults to ``None``.
headers (dict): (optional) HTTP headers to send with the
request. Headers pass through to the request will
include :attr:`DEFAULT_REQUEST_HEADERS`.
data (obj): (optional) A dictionary, bytes, or file-like
object to send in the body of the request.
json (obj): (optional) Any json compatible python
object.
NOTE: json and body parameters cannot be used at the same time.
token_refresh_attempts (int): (optional) Number of attempts a token
refresh should be performed.
Returns:
(str) HTTP response body.
Raises:
:exc:`.GCPHTTPError`: if any exception occurred,
specifically a :exc:`.GCPHTTPResponseError`, if the
exception is associated with a response status code.
"""
if all([data, json]):
msg = ('"data" and "json" request parameters can not be used '
'at the same time')
logging.warn(msg)
raise exceptions.GCPHTTPError(msg)
req_headers = headers or {}
req_headers.update(_utils.DEFAULT_REQUEST_HEADERS)
req_kwargs = {
'params': params,
'headers': req_headers,
}
if data:
req_kwargs['data'] = data
if json:
req_kwargs['json'] = json
if token_refresh_attempts:
if not await self.valid_token_set():
await self._auth_client.refresh_token()
token_refresh_attempts -= 1
req_headers.update(
{'Authorization': f'Bearer {self._auth_client.token}'}
)
request_id = kwargs.get('request_id', uuid.uuid4())
logging.debug(_utils.REQ_LOG_FMT.format(
request_id=request_id,
method=method.upper(),
url=url,
kwargs=req_kwargs))
try:
async with self._session.request(method, url, **req_kwargs) as resp:
log_kw = {
'request_id': request_id,
'method': method.upper(),
'url': resp.url,
'status': resp.status,
'reason': resp.reason
}
logging.debug(_utils.RESP_LOG_FMT.format(**log_kw))
if resp.status in REFRESH_STATUS_CODES:
logging.warning(
f'[{request_id}] HTTP Status Code {resp.status}'
f' returned requesting {resp.url}: {resp.reason}')
if token_refresh_attempts:
logging.info(
f'[{request_id}] Attempting request to {resp.url} '
'again.')
return await self.request(
method, url,
token_refresh_attempts=token_refresh_attempts,
request_id=request_id,
**req_kwargs)
logging.warning(
f'[{request_id}] Max attempts refreshing auth token '
f'exhausted while requesting {resp.url}')
resp.raise_for_status()
return await resp.text()
except aiohttp.ClientResponseError as e:
# bad HTTP status; avoid leaky abstractions and wrap HTTP errors
# with our own
msg = f'[{request_id}] HTTP error response from {resp.url}: {e}'
logging.error(msg, exc_info=e)
raise exceptions.GCPHTTPResponseError(msg, resp.status)
except exceptions.GCPHTTPResponseError as e:
# from recursive call
raise e
except Exception as e:
msg = f'[{request_id}] Request call failed: {e}'
logging.error(msg, exc_info=e)
raise exceptions.GCPHTTPError(msg) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:unflatten; 3, parameters; 3, 4; 3, 5; 4, identifier:processed; 5, identifier:merge_rules; 6, block; 6, 7; 6, 9; 6, 15; 6, 193; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:unflattened; 12, call; 12, 13; 12, 14; 13, identifier:OrderedDict; 14, argument_list; 15, for_statement; 15, 16; 15, 17; 15, 18; 16, identifier:key; 17, identifier:processed; 18, block; 18, 19; 18, 23; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:current_node; 22, identifier:unflattened; 23, for_statement; 23, 24; 23, 27; 23, 32; 23, 33; 23, 34; 24, pattern_list; 24, 25; 24, 26; 25, identifier:end; 26, identifier:part; 27, call; 27, 28; 27, 29; 28, identifier:enumerate; 29, argument_list; 29, 30; 29, 31; 30, identifier:key; 31, integer:1; 32, comment; 33, comment; 34, block; 34, 35; 34, 112; 34, 113; 34, 122; 34, 123; 34, 124; 34, 134; 34, 135; 34, 160; 34, 161; 34, 183; 34, 189; 35, if_statement; 35, 36; 35, 41; 35, 42; 36, call; 36, 37; 36, 38; 37, identifier:isinstance; 38, argument_list; 38, 39; 38, 40; 39, identifier:part; 40, identifier:IdValue; 41, comment; 42, block; 42, 43; 42, 111; 43, for_statement; 43, 44; 43, 45; 43, 46; 43, 67; 43, 68; 44, identifier:node; 45, identifier:current_node; 46, block; 46, 47; 47, if_statement; 47, 48; 47, 61; 48, boolean_operator:and; 48, 49; 48, 54; 49, call; 49, 50; 49, 51; 50, identifier:isinstance; 51, argument_list; 51, 52; 51, 53; 52, identifier:node; 53, identifier:IdDict; 54, comparison_operator:==; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:node; 57, identifier:identifier; 58, attribute; 58, 59; 58, 60; 59, identifier:part; 60, identifier:identifier; 61, block; 61, 62; 61, 66; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:current_node; 65, identifier:node; 66, break_statement; 67, comment; 68, else_clause; 68, 69; 69, block; 69, 70; 69, 76; 69, 84; 69, 85; 69, 100; 69, 107; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:new_node; 73, call; 73, 74; 73, 75; 74, identifier:IdDict; 75, argument_list; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:new_node; 80, identifier:identifier; 81, attribute; 81, 82; 81, 83; 82, identifier:part; 83, identifier:identifier; 84, comment; 85, if_statement; 85, 86; 85, 91; 86, comparison_operator:is; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:part; 89, identifier:original_value; 90, None; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:new_node; 96, string:'id'; 97, attribute; 97, 98; 97, 99; 98, identifier:part; 99, identifier:original_value; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:current_node; 104, identifier:append; 105, argument_list; 105, 106; 106, identifier:new_node; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:current_node; 110, identifier:new_node; 111, continue_statement; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:node; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:current_node; 119, identifier:get; 120, argument_list; 120, 121; 121, identifier:part; 122, comment; 123, comment; 124, if_statement; 124, 125; 124, 128; 125, comparison_operator:is; 125, 126; 125, 127; 126, identifier:node; 127, None; 128, block; 128, 129; 128, 133; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:current_node; 132, identifier:node; 133, continue_statement; 134, comment; 135, if_statement; 135, 136; 135, 142; 135, 143; 136, comparison_operator:==; 136, 137; 136, 141; 137, call; 137, 138; 137, 139; 138, identifier:len; 139, argument_list; 139, 140; 140, identifier:key; 141, identifier:end; 142, comment; 143, block; 143, 144; 143, 159; 144, if_statement; 144, 145; 144, 150; 145, comparison_operator:is; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:processed; 148, identifier:key; 149, None; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:current_node; 155, identifier:part; 156, subscript; 156, 157; 156, 158; 157, identifier:processed; 158, identifier:key; 159, continue_statement; 160, comment; 161, if_statement; 161, 162; 161, 169; 161, 174; 161, 175; 162, call; 162, 163; 162, 164; 163, identifier:isinstance; 164, argument_list; 164, 165; 164, 168; 165, subscript; 165, 166; 165, 167; 166, identifier:key; 167, identifier:end; 168, identifier:IdValue; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:new_node; 173, list:[]; 174, comment; 175, else_clause; 175, 176; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:new_node; 180, call; 180, 181; 180, 182; 181, identifier:OrderedDict; 182, argument_list; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:current_node; 187, identifier:part; 188, identifier:new_node; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:current_node; 192, identifier:new_node; 193, return_statement; 193, 194; 194, identifier:unflattened | def unflatten(processed, merge_rules):
"""
Unflattens a processed object into a JSON object.
"""
unflattened = OrderedDict()
for key in processed:
current_node = unflattened
for end, part in enumerate(key, 1):
# If this is a path to an item of an array.
# See http://standard.open-contracting.org/1.1-dev/en/schema/merging/#identifier-merge
if isinstance(part, IdValue):
# If the `id` of an object in the array matches, change into it.
for node in current_node:
if isinstance(node, IdDict) and node.identifier == part.identifier:
current_node = node
break
# Otherwise, append a new object, and change into it.
else:
new_node = IdDict()
new_node.identifier = part.identifier
# If the original object had an `id` value, set it.
if part.original_value is not None:
new_node['id'] = part.original_value
current_node.append(new_node)
current_node = new_node
continue
# Otherwise, this is a path to a property of an object.
node = current_node.get(part)
# If this is a path to a node we visited before, change into it. If it's an `id` field, it's already been
# set to its original value.
if node is not None:
current_node = node
continue
# If this is a full path, copy the data.
if len(key) == end:
# Omit null'ed fields.
if processed[key] is not None:
current_node[part] = processed[key]
continue
# If the path is to a new array, start a new array, and change into it.
if isinstance(key[end], IdValue):
new_node = []
# If the path is to a new object, start a new object, and change into it.
else:
new_node = OrderedDict()
current_node[part] = new_node
current_node = new_node
return unflattened |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:translate; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 110; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:translations; 11, list:[]; 12, for_statement; 12, 13; 12, 14; 12, 17; 12, 18; 12, 19; 13, identifier:lang; 14, attribute; 14, 15; 14, 16; 15, identifier:settings; 16, identifier:LANGUAGES; 17, comment; 18, comment; 19, block; 19, 20; 19, 32; 19, 33; 19, 62; 20, if_statement; 20, 21; 20, 30; 21, comparison_operator:==; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:lang; 24, integer:0; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:_get_default_language; 29, argument_list; 30, block; 30, 31; 31, continue_statement; 32, comment; 33, if_statement; 33, 34; 33, 39; 34, comparison_operator:is; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:translatable_slug; 38, None; 39, block; 39, 40; 40, if_statement; 40, 41; 40, 48; 41, comparison_operator:not; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:translatable_slug; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:translatable_fields; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:translatable_fields; 54, binary_operator:+; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:translatable_fields; 58, tuple; 58, 59; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:translatable_slug; 62, for_statement; 62, 63; 62, 64; 62, 67; 63, identifier:field; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:translatable_fields; 67, block; 67, 68; 67, 103; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, pattern_list; 70, 71; 70, 72; 71, identifier:trans; 72, identifier:created; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:Translation; 77, identifier:objects; 78, identifier:get_or_create; 79, argument_list; 79, 80; 79, 85; 79, 95; 79, 98; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:object_id; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:id; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:content_type; 87, call; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:ContentType; 91, identifier:objects; 92, identifier:get_for_model; 93, argument_list; 93, 94; 94, identifier:self; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:field; 97, identifier:field; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:lang; 100, subscript; 100, 101; 100, 102; 101, identifier:lang; 102, integer:0; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:translations; 107, identifier:append; 108, argument_list; 108, 109; 109, identifier:trans; 110, return_statement; 110, 111; 111, identifier:translations | def translate(self):
"""
Create all translations objects for this Translatable instance.
@rtype: list of Translation objects
@return: Returns a list of translations objects
"""
translations = []
for lang in settings.LANGUAGES:
# do not create an translations for default language.
# we will use the original model for this
if lang[0] == self._get_default_language():
continue
# create translations for all fields of each language
if self.translatable_slug is not None:
if self.translatable_slug not in self.translatable_fields:
self.translatable_fields = self.translatable_fields + (self.translatable_slug,)
for field in self.translatable_fields:
trans, created = Translation.objects.get_or_create(
object_id=self.id,
content_type=ContentType.objects.get_for_model(self),
field=field,
lang=lang[0],
)
translations.append(trans)
return translations |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:set_translation; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:lang; 6, identifier:field; 7, identifier:text; 8, block; 8, 9; 8, 11; 8, 12; 8, 16; 8, 35; 8, 36; 8, 49; 8, 55; 8, 61; 8, 62; 8, 91; 8, 134; 8, 135; 8, 145; 8, 153; 8, 154; 8, 166; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:auto_slug_obj; 15, None; 16, if_statement; 16, 17; 16, 24; 17, comparison_operator:==; 17, 18; 17, 19; 18, identifier:lang; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:_get_default_language; 23, argument_list; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:CanNotTranslate; 28, argument_list; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:_; 31, argument_list; 31, 32; 32, concatenated_string; 32, 33; 32, 34; 33, string:'You are not supposed to translate the default language. '; 34, string:'Use the model fields for translations in default language'; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:trans_obj; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:get_translation_obj; 43, argument_list; 43, 44; 43, 45; 43, 46; 44, identifier:lang; 45, identifier:field; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:create; 48, True; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:trans_obj; 53, identifier:translation; 54, identifier:text; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:trans_obj; 59, identifier:save; 60, argument_list; 61, comment; 62, if_statement; 62, 63; 62, 64; 63, identifier:INSTALLED_AUTOSLUG; 64, block; 64, 65; 65, if_statement; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:translatable_slug; 69, block; 69, 70; 70, try_statement; 70, 71; 70, 87; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:auto_slug_obj; 75, attribute; 75, 76; 75, 86; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:_meta; 81, identifier:get_field; 82, argument_list; 82, 83; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:translatable_slug; 86, identifier:populate_from; 87, except_clause; 87, 88; 87, 89; 88, identifier:AttributeError; 89, block; 89, 90; 90, pass_statement; 91, if_statement; 91, 92; 91, 93; 92, identifier:auto_slug_obj; 93, block; 93, 94; 93, 109; 93, 119; 93, 128; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:tobj; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:get_translation_obj; 101, argument_list; 101, 102; 101, 103; 101, 106; 102, identifier:lang; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:translatable_slug; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:create; 108, True; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:translation; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:get_translation; 116, argument_list; 116, 117; 116, 118; 117, identifier:lang; 118, identifier:auto_slug_obj; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:tobj; 123, identifier:translation; 124, call; 124, 125; 124, 126; 125, identifier:slugify; 126, argument_list; 126, 127; 127, identifier:translation; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:tobj; 132, identifier:save; 133, argument_list; 134, comment; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:key; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:_get_translation_cache_key; 142, argument_list; 142, 143; 142, 144; 143, identifier:lang; 144, identifier:field; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:cache; 149, identifier:set; 150, argument_list; 150, 151; 150, 152; 151, identifier:key; 152, identifier:text; 153, comment; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:cache; 158, identifier:delete; 159, argument_list; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:_get_translations_cache_key; 164, argument_list; 164, 165; 165, identifier:lang; 166, return_statement; 166, 167; 167, identifier:trans_obj | def set_translation(self, lang, field, text):
"""
Store a translation string in the specified field for a Translatable
istance
@type lang: string
@param lang: a string with the name of the language
@type field: string
@param field: a string with the name that we try to get
@type text: string
@param text: a string to be stored as translation of the field
"""
# Do not allow user to set a translations in the default language
auto_slug_obj = None
if lang == self._get_default_language():
raise CanNotTranslate(
_('You are not supposed to translate the default language. '
'Use the model fields for translations in default language')
)
# Get translation, if it does not exits create one
trans_obj = self.get_translation_obj(lang, field, create=True)
trans_obj.translation = text
trans_obj.save()
# check if the field has an autoslugfield and create the translation
if INSTALLED_AUTOSLUG:
if self.translatable_slug:
try:
auto_slug_obj = self._meta.get_field(self.translatable_slug).populate_from
except AttributeError:
pass
if auto_slug_obj:
tobj = self.get_translation_obj(lang, self.translatable_slug, create=True)
translation = self.get_translation(lang, auto_slug_obj)
tobj.translation = slugify(translation)
tobj.save()
# Update cache for this specif translations
key = self._get_translation_cache_key(lang, field)
cache.set(key, text)
# remove cache for translations dict
cache.delete(self._get_translations_cache_key(lang))
return trans_obj |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:jsonify; 3, parameters; 3, 4; 3, 5; 3, 9; 4, identifier:o; 5, default_parameter; 5, 6; 5, 7; 6, identifier:max_depth; 7, unary_operator:-; 7, 8; 8, integer:1; 9, default_parameter; 9, 10; 9, 11; 10, identifier:parse_enums; 11, identifier:PARSE_KEEP; 12, block; 12, 13; 12, 15; 12, 22; 12, 26; 12, 186; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:==; 16, 17; 16, 18; 17, identifier:max_depth; 18, integer:0; 19, block; 19, 20; 20, return_statement; 20, 21; 21, identifier:o; 22, expression_statement; 22, 23; 23, augmented_assignment:-=; 23, 24; 23, 25; 24, identifier:max_depth; 25, integer:1; 26, if_statement; 26, 27; 26, 32; 26, 125; 26, 147; 26, 169; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 31; 30, identifier:o; 31, identifier:dict; 32, block; 32, 33; 32, 44; 32, 106; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:keyattrs; 36, call; 36, 37; 36, 38; 37, identifier:getattr; 38, argument_list; 38, 39; 38, 42; 38, 43; 39, attribute; 39, 40; 39, 41; 40, identifier:o; 41, identifier:__class__; 42, string:'_altnames'; 43, dictionary; 44, function_definition; 44, 45; 44, 46; 44, 49; 45, function_name:_getter; 46, parameters; 46, 47; 46, 48; 47, identifier:key; 48, identifier:value; 49, block; 49, 50; 49, 60; 49, 69; 49, 79; 49, 93; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:key; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:keyattrs; 56, identifier:get; 57, argument_list; 57, 58; 57, 59; 58, identifier:key; 59, identifier:key; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:other; 63, call; 63, 64; 63, 65; 64, identifier:getattr; 65, argument_list; 65, 66; 65, 67; 65, 68; 66, identifier:o; 67, identifier:key; 68, identifier:value; 69, if_statement; 69, 70; 69, 74; 70, call; 70, 71; 70, 72; 71, identifier:callable; 72, argument_list; 72, 73; 73, identifier:other; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:other; 78, identifier:value; 79, if_statement; 79, 80; 79, 85; 79, 86; 80, call; 80, 81; 80, 82; 81, identifier:isinstance; 82, argument_list; 82, 83; 82, 84; 83, identifier:key; 84, identifier:Enum; 85, comment; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:key; 90, attribute; 90, 91; 90, 92; 91, identifier:key; 92, identifier:name; 93, return_statement; 93, 94; 94, expression_list; 94, 95; 94, 96; 95, identifier:key; 96, call; 96, 97; 96, 98; 97, identifier:jsonify; 98, argument_list; 98, 99; 98, 100; 98, 103; 99, identifier:other; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:max_depth; 102, identifier:max_depth; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:parse_enums; 105, identifier:parse_enums; 106, return_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:dict; 109, generator_expression; 109, 110; 109, 115; 110, call; 110, 111; 110, 112; 111, identifier:_getter; 112, argument_list; 112, 113; 112, 114; 113, identifier:key; 114, identifier:value; 115, for_in_clause; 115, 116; 115, 119; 116, pattern_list; 116, 117; 116, 118; 117, identifier:key; 118, identifier:value; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:six; 122, identifier:iteritems; 123, argument_list; 123, 124; 124, identifier:o; 125, elif_clause; 125, 126; 125, 131; 126, call; 126, 127; 126, 128; 127, identifier:isinstance; 128, argument_list; 128, 129; 128, 130; 129, identifier:o; 130, identifier:list; 131, block; 131, 132; 132, return_statement; 132, 133; 133, list_comprehension; 133, 134; 133, 144; 134, call; 134, 135; 134, 136; 135, identifier:jsonify; 136, argument_list; 136, 137; 136, 138; 136, 141; 137, identifier:x; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:max_depth; 140, identifier:max_depth; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:parse_enums; 143, identifier:parse_enums; 144, for_in_clause; 144, 145; 144, 146; 145, identifier:x; 146, identifier:o; 147, elif_clause; 147, 148; 147, 153; 148, call; 148, 149; 148, 150; 149, identifier:isinstance; 150, argument_list; 150, 151; 150, 152; 151, identifier:o; 152, identifier:tuple; 153, block; 153, 154; 154, return_statement; 154, 155; 155, generator_expression; 155, 156; 155, 166; 156, call; 156, 157; 156, 158; 157, identifier:jsonify; 158, argument_list; 158, 159; 158, 160; 158, 163; 159, identifier:x; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:max_depth; 162, identifier:max_depth; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:parse_enums; 165, identifier:parse_enums; 166, for_in_clause; 166, 167; 166, 168; 167, identifier:x; 168, identifier:o; 169, elif_clause; 169, 170; 169, 175; 170, call; 170, 171; 170, 172; 171, identifier:isinstance; 172, argument_list; 172, 173; 172, 174; 173, identifier:o; 174, identifier:Enum; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:o; 179, call; 179, 180; 179, 181; 180, identifier:_parse_enum; 181, argument_list; 181, 182; 181, 183; 182, identifier:o; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:parse_enums; 185, identifier:parse_enums; 186, return_statement; 186, 187; 187, identifier:o | def jsonify(o, max_depth=-1, parse_enums=PARSE_KEEP):
"""
Walks through object o, and attempts to get the property instead of the key, if available.
This means that for our VDev objects we can easily get a dict of all the 'parsed' values.
"""
if max_depth == 0:
return o
max_depth -= 1
if isinstance(o, dict):
keyattrs = getattr(o.__class__, '_altnames', {})
def _getter(key, value):
key = keyattrs.get(key, key)
other = getattr(o, key, value)
if callable(other):
other = value
if isinstance(key, Enum): # Make sure we use a name as the key... if we don't it might mess some things up.
key = key.name
return key, jsonify(other, max_depth=max_depth, parse_enums=parse_enums)
return dict(_getter(key, value) for key, value in six.iteritems(o))
elif isinstance(o, list):
return [jsonify(x, max_depth=max_depth, parse_enums=parse_enums) for x in o]
elif isinstance(o, tuple):
return (jsonify(x, max_depth=max_depth, parse_enums=parse_enums) for x in o)
elif isinstance(o, Enum):
o = _parse_enum(o, parse_enums=parse_enums)
return o |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:b58encode; 3, parameters; 3, 4; 3, 5; 4, identifier:val; 5, default_parameter; 5, 6; 5, 7; 6, identifier:charset; 7, identifier:DEFAULT_CHARSET; 8, block; 8, 9; 8, 11; 8, 65; 8, 87; 8, 103; 8, 110; 8, 123; 8, 130; 8, 139; 8, 146; 8, 154; 8, 176; 8, 186; 8, 198; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 24; 12, function_name:_b58encode_int; 13, parameters; 13, 14; 13, 15; 14, identifier:int_; 15, default_parameter; 15, 16; 15, 17; 16, identifier:default; 17, call; 17, 18; 17, 19; 18, identifier:bytes; 19, argument_list; 19, 20; 20, list:[charset[0]]; 20, 21; 21, subscript; 21, 22; 21, 23; 22, identifier:charset; 23, integer:0; 24, block; 24, 25; 24, 33; 24, 37; 24, 63; 25, if_statement; 25, 26; 25, 30; 26, boolean_operator:and; 26, 27; 26, 29; 27, not_operator; 27, 28; 28, identifier:int_; 29, identifier:default; 30, block; 30, 31; 31, return_statement; 31, 32; 32, identifier:default; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:output; 36, string:b''; 37, while_statement; 37, 38; 37, 39; 38, identifier:int_; 39, block; 39, 40; 39, 50; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 45; 42, pattern_list; 42, 43; 42, 44; 43, identifier:int_; 44, identifier:idx; 45, call; 45, 46; 45, 47; 46, identifier:divmod; 47, argument_list; 47, 48; 47, 49; 48, identifier:int_; 49, identifier:base; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:output; 53, binary_operator:+; 53, 54; 53, 62; 54, subscript; 54, 55; 54, 56; 55, identifier:charset; 56, slice; 56, 57; 56, 58; 56, 59; 57, identifier:idx; 58, colon; 59, binary_operator:+; 59, 60; 59, 61; 60, identifier:idx; 61, integer:1; 62, identifier:output; 63, return_statement; 63, 64; 64, identifier:output; 65, if_statement; 65, 66; 65, 72; 66, not_operator; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:isinstance; 69, argument_list; 69, 70; 69, 71; 70, identifier:val; 71, identifier:bytes; 72, block; 72, 73; 73, raise_statement; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:TypeError; 76, argument_list; 76, 77; 77, binary_operator:%; 77, 78; 77, 81; 78, concatenated_string; 78, 79; 78, 80; 79, string:"a bytes-like object is required, not '%s', "; 80, string:"use .encode('ascii') to encode unicode strings"; 81, attribute; 81, 82; 81, 86; 82, call; 82, 83; 82, 84; 83, identifier:type; 84, argument_list; 84, 85; 85, identifier:val; 86, identifier:__name__; 87, if_statement; 87, 88; 87, 93; 88, call; 88, 89; 88, 90; 89, identifier:isinstance; 90, argument_list; 90, 91; 90, 92; 91, identifier:charset; 92, identifier:str; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:charset; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:charset; 100, identifier:encode; 101, argument_list; 101, 102; 102, string:'ascii'; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:base; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, identifier:charset; 110, if_statement; 110, 111; 110, 115; 111, not_operator; 111, 112; 112, comparison_operator:==; 112, 113; 112, 114; 113, identifier:base; 114, integer:58; 115, block; 115, 116; 116, raise_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:ValueError; 119, argument_list; 119, 120; 120, binary_operator:%; 120, 121; 120, 122; 121, string:'charset base must be 58, not %s'; 122, identifier:base; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:pad_len; 126, call; 126, 127; 126, 128; 127, identifier:len; 128, argument_list; 128, 129; 129, identifier:val; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:val; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:val; 136, identifier:lstrip; 137, argument_list; 137, 138; 138, string:b'\0'; 139, expression_statement; 139, 140; 140, augmented_assignment:-=; 140, 141; 140, 142; 141, identifier:pad_len; 142, call; 142, 143; 142, 144; 143, identifier:len; 144, argument_list; 144, 145; 145, identifier:val; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, pattern_list; 148, 149; 148, 150; 149, identifier:p; 150, identifier:acc; 151, expression_list; 151, 152; 151, 153; 152, integer:1; 153, integer:0; 154, for_statement; 154, 155; 154, 156; 154, 163; 155, identifier:char; 156, call; 156, 157; 156, 158; 157, identifier:deque; 158, argument_list; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:reversed; 161, argument_list; 161, 162; 162, identifier:val; 163, block; 163, 164; 163, 170; 164, expression_statement; 164, 165; 165, augmented_assignment:+=; 165, 166; 165, 167; 166, identifier:acc; 167, binary_operator:*; 167, 168; 167, 169; 168, identifier:p; 169, identifier:char; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:p; 173, binary_operator:<<; 173, 174; 173, 175; 174, identifier:p; 175, integer:8; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:result; 179, call; 179, 180; 179, 181; 180, identifier:_b58encode_int; 181, argument_list; 181, 182; 181, 183; 182, identifier:acc; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:default; 185, False; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:prefix; 189, binary_operator:*; 189, 190; 189, 197; 190, call; 190, 191; 190, 192; 191, identifier:bytes; 192, argument_list; 192, 193; 193, list:[charset[0]]; 193, 194; 194, subscript; 194, 195; 194, 196; 195, identifier:charset; 196, integer:0; 197, identifier:pad_len; 198, return_statement; 198, 199; 199, binary_operator:+; 199, 200; 199, 201; 200, identifier:prefix; 201, identifier:result | def b58encode(val, charset=DEFAULT_CHARSET):
"""Encode input to base58check encoding.
:param bytes val: The value to base58check encode.
:param bytes charset: (optional) The character set to use for encoding.
:return: the encoded bytestring.
:rtype: bytes
:raises: TypeError: if `val` is not bytes.
Usage::
>>> import base58check
>>> base58check.b58encode(b'1BoatSLRHtKNngkdXEeobR76b53LETtpyT')
b'\x00v\x80\xad\xec\x8e\xab\xca\xba\xc6v\xbe\x9e\x83\x85J\xde\x0b\xd2,\xdb\x0b\xb9`\xde'
"""
def _b58encode_int(int_, default=bytes([charset[0]])):
if not int_ and default:
return default
output = b''
while int_:
int_, idx = divmod(int_, base)
output = charset[idx:idx+1] + output
return output
if not isinstance(val, bytes):
raise TypeError(
"a bytes-like object is required, not '%s', "
"use .encode('ascii') to encode unicode strings" %
type(val).__name__)
if isinstance(charset, str):
charset = charset.encode('ascii')
base = len(charset)
if not base == 58:
raise ValueError('charset base must be 58, not %s' % base)
pad_len = len(val)
val = val.lstrip(b'\0')
pad_len -= len(val)
p, acc = 1, 0
for char in deque(reversed(val)):
acc += p * char
p = p << 8
result = _b58encode_int(acc, default=False)
prefix = bytes([charset[0]]) * pad_len
return prefix + result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:m2m_callback; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, identifier:sender; 5, identifier:instance; 6, identifier:action; 7, identifier:reverse; 8, identifier:model; 9, identifier:pk_set; 10, identifier:using; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kwargs; 13, block; 13, 14; 13, 16; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 27; 17, boolean_operator:and; 17, 18; 17, 22; 18, call; 18, 19; 18, 20; 19, identifier:validate_instance; 20, argument_list; 20, 21; 21, identifier:instance; 22, subscript; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:settings; 25, identifier:AUTOMATED_LOGGING; 26, string:'to_database'; 27, block; 27, 28; 28, if_statement; 28, 29; 28, 34; 29, comparison_operator:in; 29, 30; 29, 31; 30, identifier:action; 31, list:["post_add", 'post_remove']; 31, 32; 31, 33; 32, string:"post_add"; 33, string:'post_remove'; 34, block; 34, 35; 34, 52; 34, 129; 34, 213; 34, 219; 34, 225; 34, 353; 34, 359; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:modification; 38, list_comprehension; 38, 39; 38, 49; 39, call; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:model; 43, identifier:objects; 44, identifier:get; 45, argument_list; 45, 46; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:pk; 48, identifier:x; 49, for_in_clause; 49, 50; 49, 51; 50, identifier:x; 51, identifier:pk_set; 52, if_statement; 52, 53; 52, 66; 52, 73; 53, boolean_operator:and; 53, 54; 53, 63; 54, comparison_operator:in; 54, 55; 54, 56; 55, string:'al_chl'; 56, call; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:instance; 60, identifier:__dict__; 61, identifier:keys; 62, argument_list; 63, attribute; 63, 64; 63, 65; 64, identifier:instance; 65, identifier:al_chl; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:changelog; 70, attribute; 70, 71; 70, 72; 71, identifier:instance; 72, identifier:al_chl; 73, else_clause; 73, 74; 74, block; 74, 75; 74, 81; 74, 89; 74, 100; 74, 115; 74, 123; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:changelog; 78, call; 78, 79; 78, 80; 79, identifier:ModelChangelog; 80, argument_list; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:changelog; 85, identifier:information; 86, call; 86, 87; 86, 88; 87, identifier:ModelObject; 88, argument_list; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:changelog; 94, identifier:information; 95, identifier:value; 96, call; 96, 97; 96, 98; 97, identifier:repr; 98, argument_list; 98, 99; 99, identifier:instance; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:changelog; 105, identifier:information; 106, identifier:type; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:ContentType; 111, identifier:objects; 112, identifier:get_for_model; 113, argument_list; 113, 114; 114, identifier:instance; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:changelog; 120, identifier:information; 121, identifier:save; 122, argument_list; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:changelog; 127, identifier:save; 128, argument_list; 129, for_statement; 129, 130; 129, 131; 129, 132; 130, identifier:f; 131, identifier:modification; 132, block; 132, 133; 132, 139; 132, 148; 132, 182; 132, 188; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:obj; 136, call; 136, 137; 136, 138; 137, identifier:ModelObject; 138, argument_list; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:obj; 143, identifier:value; 144, call; 144, 145; 144, 146; 145, identifier:repr; 146, argument_list; 146, 147; 147, identifier:f; 148, try_statement; 148, 149; 148, 163; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:obj; 154, identifier:type; 155, call; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:ContentType; 159, identifier:objects; 160, identifier:get_for_model; 161, argument_list; 161, 162; 162, identifier:f; 163, except_clause; 163, 164; 163, 165; 164, identifier:Exception; 165, block; 165, 166; 165, 175; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:logger; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:logging; 172, identifier:getLogger; 173, argument_list; 173, 174; 174, identifier:__name__; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:logger; 179, identifier:debug; 180, argument_list; 180, 181; 181, string:'Could not determin the type of the modification.'; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:obj; 186, identifier:save; 187, argument_list; 188, if_statement; 188, 189; 188, 192; 188, 202; 189, comparison_operator:==; 189, 190; 189, 191; 190, identifier:action; 191, string:'post_add'; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:changelog; 198, identifier:inserted; 199, identifier:add; 200, argument_list; 200, 201; 201, identifier:obj; 202, else_clause; 202, 203; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 211; 206, attribute; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:changelog; 209, identifier:removed; 210, identifier:add; 211, argument_list; 211, 212; 212, identifier:obj; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:changelog; 217, identifier:save; 218, argument_list; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:instance; 223, identifier:al_chl; 224, identifier:changelog; 225, if_statement; 225, 226; 225, 235; 225, 242; 226, comparison_operator:in; 226, 227; 226, 228; 227, string:'al_evt'; 228, call; 228, 229; 228, 234; 229, attribute; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:instance; 232, identifier:__dict__; 233, identifier:keys; 234, argument_list; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:target; 239, attribute; 239, 240; 239, 241; 240, identifier:instance; 241, identifier:al_evt; 242, else_clause; 242, 243; 243, block; 243, 244; 243, 250; 243, 258; 243, 269; 243, 275; 243, 288; 243, 305; 243, 313; 243, 324; 243, 339; 243, 347; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:target; 247, call; 247, 248; 247, 249; 248, identifier:Model; 249, argument_list; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:target; 254, identifier:user; 255, call; 255, 256; 255, 257; 256, identifier:get_current_user; 257, argument_list; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:target; 262, identifier:action; 263, conditional_expression:if; 263, 264; 263, 265; 263, 268; 264, integer:2; 265, comparison_operator:==; 265, 266; 265, 267; 266, identifier:action; 267, string:'post_add'; 268, integer:2; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:target; 273, identifier:save; 274, argument_list; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:ct; 278, attribute; 278, 279; 278, 287; 279, call; 279, 280; 279, 285; 280, attribute; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:ContentType; 283, identifier:objects; 284, identifier:get_for_model; 285, argument_list; 285, 286; 286, identifier:instance; 287, identifier:app_label; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:target; 292, identifier:application; 293, subscript; 293, 294; 293, 304; 294, call; 294, 295; 294, 300; 295, attribute; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:Application; 298, identifier:objects; 299, identifier:get_or_create; 300, argument_list; 300, 301; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:name; 303, identifier:ct; 304, integer:0; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:target; 309, identifier:information; 310, call; 310, 311; 310, 312; 311, identifier:ModelObject; 312, argument_list; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 320; 315, attribute; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:target; 318, identifier:information; 319, identifier:value; 320, call; 320, 321; 320, 322; 321, identifier:repr; 322, argument_list; 322, 323; 323, identifier:instance; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 331; 326, attribute; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:target; 329, identifier:information; 330, identifier:type; 331, call; 331, 332; 331, 337; 332, attribute; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:ContentType; 335, identifier:objects; 336, identifier:get_for_model; 337, argument_list; 337, 338; 338, identifier:instance; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 346; 341, attribute; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:target; 344, identifier:information; 345, identifier:save; 346, argument_list; 347, expression_statement; 347, 348; 348, assignment; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:instance; 351, identifier:al_evt; 352, identifier:target; 353, expression_statement; 353, 354; 354, assignment; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:target; 357, identifier:modification; 358, identifier:changelog; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:target; 363, identifier:save; 364, argument_list | def m2m_callback(sender, instance, action, reverse, model, pk_set, using, **kwargs):
"""
Many 2 Many relationship signall receivver.
Detect Many 2 Many relationship changes and append these to existing model or create if needed.
These get not recorded from the pre_save or post_save method and must therefor be received from
another method. This method to be precise.
"""
if validate_instance(instance) and settings.AUTOMATED_LOGGING['to_database']:
if action in ["post_add", 'post_remove']:
modification = [model.objects.get(pk=x) for x in pk_set]
if 'al_chl' in instance.__dict__.keys() and instance.al_chl:
changelog = instance.al_chl
else:
changelog = ModelChangelog()
changelog.information = ModelObject()
changelog.information.value = repr(instance)
changelog.information.type = ContentType.objects.get_for_model(instance)
changelog.information.save()
changelog.save()
for f in modification:
obj = ModelObject()
obj.value = repr(f)
try:
obj.type = ContentType.objects.get_for_model(f)
except Exception:
logger = logging.getLogger(__name__)
logger.debug('Could not determin the type of the modification.')
obj.save()
if action == 'post_add':
changelog.inserted.add(obj)
else:
changelog.removed.add(obj)
changelog.save()
instance.al_chl = changelog
if 'al_evt' in instance.__dict__.keys():
target = instance.al_evt
else:
target = Model()
target.user = get_current_user()
target.action = 2 if action == 'post_add' else 2
target.save()
ct = ContentType.objects.get_for_model(instance).app_label
target.application = Application.objects.get_or_create(name=ct)[0]
target.information = ModelObject()
target.information.value = repr(instance)
target.information.type = ContentType.objects.get_for_model(instance)
target.information.save()
instance.al_evt = target
target.modification = changelog
target.save() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:normalize_allele_name; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:raw_allele; 5, default_parameter; 5, 6; 5, 7; 6, identifier:omit_dra1; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:infer_class2_pair; 10, True; 11, block; 11, 12; 11, 14; 11, 21; 11, 30; 11, 40; 11, 48; 11, 53; 11, 54; 11, 82; 11, 136; 11, 145; 11, 151; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:cache_key; 17, tuple; 17, 18; 17, 19; 17, 20; 18, identifier:raw_allele; 19, identifier:omit_dra1; 20, identifier:infer_class2_pair; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:in; 22, 23; 22, 24; 23, identifier:cache_key; 24, identifier:_normalized_allele_cache; 25, block; 25, 26; 26, return_statement; 26, 27; 27, subscript; 27, 28; 27, 29; 28, identifier:_normalized_allele_cache; 29, identifier:cache_key; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:parsed_alleles; 33, call; 33, 34; 33, 35; 34, identifier:parse_classi_or_classii_allele_name; 35, argument_list; 35, 36; 35, 37; 36, identifier:raw_allele; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:infer_pair; 39, identifier:infer_class2_pair; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:species; 43, attribute; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:parsed_alleles; 46, integer:0; 47, identifier:species; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:normalized_list; 51, list:[species]; 51, 52; 52, identifier:species; 53, comment; 54, if_statement; 54, 55; 54, 63; 55, boolean_operator:and; 55, 56; 55, 57; 56, identifier:omit_dra1; 57, comparison_operator:==; 57, 58; 57, 62; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, identifier:parsed_alleles; 62, integer:2; 63, block; 63, 64; 63, 70; 63, 71; 63, 72; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, pattern_list; 66, 67; 66, 68; 67, identifier:alpha; 68, identifier:beta; 69, identifier:parsed_alleles; 70, comment; 71, comment; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:==; 73, 74; 73, 75; 74, identifier:alpha; 75, identifier:_DRA1_0101; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:parsed_alleles; 80, list:[beta]; 80, 81; 81, identifier:beta; 82, for_statement; 82, 83; 82, 84; 82, 85; 83, identifier:parsed_allele; 84, identifier:parsed_alleles; 85, block; 85, 86; 86, if_statement; 86, 87; 86, 95; 86, 114; 87, comparison_operator:>; 87, 88; 87, 94; 88, call; 88, 89; 88, 90; 89, identifier:len; 90, argument_list; 90, 91; 91, attribute; 91, 92; 91, 93; 92, identifier:parsed_allele; 93, identifier:allele_family; 94, integer:0; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:normalized_list; 100, identifier:append; 101, argument_list; 101, 102; 102, binary_operator:%; 102, 103; 102, 104; 103, string:"%s*%s:%s"; 104, tuple; 104, 105; 104, 108; 104, 111; 105, attribute; 105, 106; 105, 107; 106, identifier:parsed_allele; 107, identifier:gene; 108, attribute; 108, 109; 108, 110; 109, identifier:parsed_allele; 110, identifier:allele_family; 111, attribute; 111, 112; 111, 113; 112, identifier:parsed_allele; 113, identifier:allele_code; 114, else_clause; 114, 115; 114, 116; 114, 117; 114, 118; 114, 119; 114, 120; 115, comment; 116, comment; 117, comment; 118, comment; 119, comment; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:normalized_list; 125, identifier:append; 126, argument_list; 126, 127; 127, binary_operator:%; 127, 128; 127, 129; 128, string:"%s%s"; 129, tuple; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:parsed_allele; 132, identifier:gene; 133, attribute; 133, 134; 133, 135; 134, identifier:parsed_allele; 135, identifier:allele_code; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:normalized; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, string:"-"; 142, identifier:join; 143, argument_list; 143, 144; 144, identifier:normalized_list; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:_normalized_allele_cache; 149, identifier:cache_key; 150, identifier:normalized; 151, return_statement; 151, 152; 152, identifier:normalized | def normalize_allele_name(raw_allele, omit_dra1=False, infer_class2_pair=True):
"""MHC alleles are named with a frustratingly loose system. It's not uncommon
to see dozens of different forms for the same allele.
Note: this function works with both class I and class II allele names (including
alpha/beta pairs).
For example, these all refer to the same MHC sequence:
- HLA-A*02:01
- HLA-A02:01
- HLA-A:02:01
- HLA-A0201
- HLA-A00201
Additionally, for human alleles, the species prefix is often omitted:
- A*02:01
- A*00201
- A*0201
- A02:01
- A:02:01
- A:002:01
- A0201
- A00201
We might also encounter "6 digit" and "8 digit" MHC types (which specify
variants that don't affect amino acid sequence), for our purposes these
should be truncated to their "4-digit" forms:
- A*02:01:01
- A*02:01:01:01
There are also suffixes which we're going to ignore:
- HLA-A*02:01:01G
And lastly, for human alleles, there are serotypes which we'll treat
as approximately equal to a 4-digit type.
- HLA-A2
- A2
These should all be normalized to:
HLA-A*02:01
"""
cache_key = (raw_allele, omit_dra1, infer_class2_pair)
if cache_key in _normalized_allele_cache:
return _normalized_allele_cache[cache_key]
parsed_alleles = parse_classi_or_classii_allele_name(
raw_allele, infer_pair=infer_class2_pair)
species = parsed_alleles[0].species
normalized_list = [species]
# Optionally omit the alpha allele, e.g. for IEDB predictors.
if omit_dra1 and len(parsed_alleles) == 2:
alpha, beta = parsed_alleles
# by convention the alpha allelle is omitted since it's assumed
# to be DRA1*01:01
if alpha == _DRA1_0101:
parsed_alleles = [beta]
for parsed_allele in parsed_alleles:
if len(parsed_allele.allele_family) > 0:
normalized_list.append("%s*%s:%s" % (
parsed_allele.gene,
parsed_allele.allele_family,
parsed_allele.allele_code))
else:
# mice don't have allele families
# e.g. H-2-Kd
# species = H-2
# gene = K
# allele = d
normalized_list.append("%s%s" % (
parsed_allele.gene,
parsed_allele.allele_code))
normalized = "-".join(normalized_list)
_normalized_allele_cache[cache_key] = normalized
return normalized |
Subsets and Splits