sequence
stringlengths 546
16.2k
| code
stringlengths 108
19.3k
|
---|---|
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:Debounce; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:threshold; 6, integer:100; 7, block; 7, 8; 7, 10; 7, 14; 7, 18; 7, 178; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, augmented_assignment:*=; 11, 12; 11, 13; 12, identifier:threshold; 13, integer:1000; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:max_tick; 17, integer:0xFFFFFFFF; 18, class_definition; 18, 19; 18, 20; 18, 22; 19, identifier:_decorated; 20, argument_list; 20, 21; 21, identifier:object; 22, block; 22, 23; 22, 47; 22, 153; 23, function_definition; 23, 24; 23, 25; 23, 28; 24, function_name:__init__; 25, parameters; 25, 26; 25, 27; 26, identifier:self; 27, identifier:pigpio_cb; 28, block; 28, 29; 28, 35; 28, 41; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:_fn; 34, identifier:pigpio_cb; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:last; 40, integer:0; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:is_method; 46, False; 47, function_definition; 47, 48; 47, 49; 47, 55; 48, function_name:__call__; 49, parameters; 49, 50; 49, 51; 49, 53; 50, identifier:self; 51, list_splat_pattern; 51, 52; 52, identifier:args; 53, dictionary_splat_pattern; 53, 54; 54, identifier:kwargs; 55, block; 55, 56; 55, 75; 55, 102; 56, if_statement; 56, 57; 56, 60; 56, 67; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:is_method; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:tick; 64, subscript; 64, 65; 64, 66; 65, identifier:args; 66, integer:3; 67, else_clause; 67, 68; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:tick; 72, subscript; 72, 73; 72, 74; 73, identifier:args; 74, integer:2; 75, if_statement; 75, 76; 75, 81; 75, 92; 76, comparison_operator:>; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:last; 80, identifier:tick; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:delay; 85, binary_operator:+; 85, 86; 85, 91; 86, binary_operator:-; 86, 87; 86, 88; 87, identifier:max_tick; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:last; 91, identifier:tick; 92, else_clause; 92, 93; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:delay; 97, binary_operator:-; 97, 98; 97, 99; 98, identifier:tick; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:last; 102, if_statement; 102, 103; 102, 106; 102, 137; 103, comparison_operator:>; 103, 104; 103, 105; 104, identifier:delay; 105, identifier:threshold; 106, block; 106, 107; 106, 117; 106, 131; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_fn; 112, argument_list; 112, 113; 112, 115; 113, list_splat; 113, 114; 114, identifier:args; 115, dictionary_splat; 115, 116; 116, identifier:kwargs; 117, expression_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:print; 120, argument_list; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, string:'call passed by debouncer {} {} {}'; 124, identifier:format; 125, argument_list; 125, 126; 125, 127; 125, 130; 126, identifier:tick; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:last; 130, identifier:threshold; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:last; 136, identifier:tick; 137, else_clause; 137, 138; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:print; 142, argument_list; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, string:'call filtered out by debouncer {} {} {}'; 146, identifier:format; 147, argument_list; 147, 148; 147, 149; 147, 152; 148, identifier:tick; 149, attribute; 149, 150; 149, 151; 150, identifier:self; 151, identifier:last; 152, identifier:threshold; 153, function_definition; 153, 154; 153, 155; 153, 161; 153, 162; 153, 163; 154, function_name:__get__; 155, parameters; 155, 156; 155, 157; 155, 158; 156, identifier:self; 157, identifier:instance; 158, default_parameter; 158, 159; 158, 160; 159, identifier:type; 160, None; 161, comment; 162, comment; 163, block; 163, 164; 163, 170; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:is_method; 169, True; 170, return_statement; 170, 171; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:functools; 174, identifier:partial; 175, argument_list; 175, 176; 175, 177; 176, identifier:self; 177, identifier:instance; 178, return_statement; 178, 179; 179, identifier:_decorated | def Debounce(threshold=100):
"""
Simple debouncing decorator for apigpio callbacks.
Example:
`@Debouncer()
def my_cb(gpio, level, tick)
print('gpio cb: {} {} {}'.format(gpio, level, tick))
`
The threshold can be given to the decorator as an argument (in millisec).
This decorator can be used both on function and object's methods.
Warning: as the debouncer uses the tick from pigpio, which wraps around
after approximately 1 hour 12 minutes, you could theoretically miss one
call if your callback is called twice with that interval.
"""
threshold *= 1000
max_tick = 0xFFFFFFFF
class _decorated(object):
def __init__(self, pigpio_cb):
self._fn = pigpio_cb
self.last = 0
self.is_method = False
def __call__(self, *args, **kwargs):
if self.is_method:
tick = args[3]
else:
tick = args[2]
if self.last > tick:
delay = max_tick-self.last + tick
else:
delay = tick - self.last
if delay > threshold:
self._fn(*args, **kwargs)
print('call passed by debouncer {} {} {}'
.format(tick, self.last, threshold))
self.last = tick
else:
print('call filtered out by debouncer {} {} {}'
.format(tick, self.last, threshold))
def __get__(self, instance, type=None):
# with is called when an instance of `_decorated` is used as a class
# attribute, which is the case when decorating a method in a class
self.is_method = True
return functools.partial(self, instance)
return _decorated |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:fit; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:self; 5, identifier:target_type; 6, identifier:target; 7, default_parameter; 7, 8; 7, 9; 8, identifier:adjust_thickness; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:adjust_site_atten; 12, False; 13, default_parameter; 13, 14; 13, 15; 14, identifier:adjust_source_vel; 15, False; 16, block; 16, 17; 16, 19; 16, 27; 16, 34; 16, 35; 16, 43; 16, 51; 16, 57; 16, 58; 16, 62; 16, 78; 16, 97; 16, 98; 16, 129; 16, 130; 16, 156; 16, 177; 16, 291; 16, 305; 16, 316; 16, 333; 16, 368; 16, 369; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:density; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:profile; 26, identifier:density; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:nl; 30, call; 30, 31; 30, 32; 31, identifier:len; 32, argument_list; 32, 33; 33, identifier:density; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:slowness; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:profile; 42, identifier:slowness; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:thickness; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:profile; 50, identifier:thickness; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:site_atten; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_site_atten; 57, comment; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:initial; 61, identifier:slowness; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:bounds; 65, binary_operator:/; 65, 66; 65, 67; 66, integer:1; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:np; 70, identifier:tile; 71, argument_list; 71, 72; 71, 75; 72, tuple; 72, 73; 72, 74; 73, integer:4000; 74, integer:100; 75, tuple; 75, 76; 75, 77; 76, identifier:nl; 77, integer:1; 78, if_statement; 78, 79; 78, 81; 79, not_operator; 79, 80; 80, identifier:adjust_source_vel; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 88; 84, subscript; 84, 85; 84, 86; 85, identifier:bounds; 86, unary_operator:-; 86, 87; 87, integer:1; 88, tuple; 88, 89; 88, 93; 89, subscript; 89, 90; 89, 91; 90, identifier:initial; 91, unary_operator:-; 91, 92; 92, integer:1; 93, subscript; 93, 94; 93, 95; 94, identifier:initial; 95, unary_operator:-; 95, 96; 96, integer:1; 97, comment; 98, if_statement; 98, 99; 98, 100; 99, identifier:adjust_thickness; 100, block; 100, 101; 100, 120; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:bounds; 104, subscript; 104, 105; 104, 108; 104, 109; 105, attribute; 105, 106; 105, 107; 106, identifier:np; 107, identifier:r_; 108, identifier:bounds; 109, list_comprehension; 109, 110; 109, 117; 110, list:[t / 2, 2 * t]; 110, 111; 110, 114; 111, binary_operator:/; 111, 112; 111, 113; 112, identifier:t; 113, integer:2; 114, binary_operator:*; 114, 115; 114, 116; 115, integer:2; 116, identifier:t; 117, for_in_clause; 117, 118; 117, 119; 118, identifier:t; 119, identifier:thickness; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:initial; 123, subscript; 123, 124; 123, 127; 123, 128; 124, attribute; 124, 125; 124, 126; 125, identifier:np; 126, identifier:r_; 127, identifier:initial; 128, identifier:thickness; 129, comment; 130, if_statement; 130, 131; 130, 132; 131, identifier:adjust_site_atten; 132, block; 132, 133; 132, 145; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:bounds; 136, subscript; 136, 137; 136, 140; 136, 141; 137, attribute; 137, 138; 137, 139; 138, identifier:np; 139, identifier:r_; 140, identifier:bounds; 141, list:[[0.0001, 0.200]]; 141, 142; 142, list:[0.0001, 0.200]; 142, 143; 142, 144; 143, float:0.0001; 144, float:0.200; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:initial; 148, subscript; 148, 149; 148, 152; 148, 153; 149, attribute; 149, 150; 149, 151; 150, identifier:np; 151, identifier:r_; 152, identifier:initial; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:site_atten; 156, function_definition; 156, 157; 156, 158; 156, 161; 157, function_name:calc_rmse; 158, parameters; 158, 159; 158, 160; 159, identifier:this; 160, identifier:that; 161, block; 161, 162; 162, return_statement; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:np; 166, identifier:mean; 167, argument_list; 167, 168; 168, binary_operator:**; 168, 169; 168, 176; 169, parenthesized_expression; 169, 170; 170, binary_operator:/; 170, 171; 170, 175; 171, parenthesized_expression; 171, 172; 172, binary_operator:-; 172, 173; 172, 174; 173, identifier:this; 174, identifier:that; 175, identifier:that; 176, integer:2; 177, function_definition; 177, 178; 177, 179; 177, 181; 178, function_name:err; 179, parameters; 179, 180; 180, identifier:x; 181, block; 181, 182; 181, 191; 181, 212; 181, 224; 181, 237; 181, 246; 181, 256; 181, 257; 181, 265; 181, 276; 181, 289; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:_slowness; 185, subscript; 185, 186; 185, 187; 186, identifier:x; 187, slice; 187, 188; 187, 189; 187, 190; 188, integer:0; 189, colon; 190, identifier:nl; 191, if_statement; 191, 192; 191, 193; 191, 206; 192, identifier:adjust_thickness; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:_thickness; 197, subscript; 197, 198; 197, 199; 198, identifier:x; 199, slice; 199, 200; 199, 201; 199, 202; 200, identifier:nl; 201, colon; 202, parenthesized_expression; 202, 203; 203, binary_operator:*; 203, 204; 203, 205; 204, integer:2; 205, identifier:nl; 206, else_clause; 206, 207; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:_thickness; 211, identifier:thickness; 212, if_statement; 212, 213; 212, 214; 213, identifier:adjust_site_atten; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:self; 219, identifier:_site_atten; 220, subscript; 220, 221; 220, 222; 221, identifier:x; 222, unary_operator:-; 222, 223; 223, integer:1; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 229; 226, pattern_list; 226, 227; 226, 228; 227, identifier:crustal_amp; 228, identifier:site_term; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:_calc_amp; 233, argument_list; 233, 234; 233, 235; 233, 236; 234, identifier:density; 235, identifier:_thickness; 236, identifier:_slowness; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:calc; 240, conditional_expression:if; 240, 241; 240, 242; 240, 245; 241, identifier:crustal_amp; 242, comparison_operator:==; 242, 243; 242, 244; 243, identifier:target_type; 244, string:'crustal_amp'; 245, identifier:site_term; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:err; 249, binary_operator:*; 249, 250; 249, 251; 250, integer:10; 251, call; 251, 252; 251, 253; 252, identifier:calc_rmse; 253, argument_list; 253, 254; 253, 255; 254, identifier:target; 255, identifier:calc; 256, comment; 257, expression_statement; 257, 258; 258, augmented_assignment:+=; 258, 259; 258, 260; 259, identifier:err; 260, call; 260, 261; 260, 262; 261, identifier:calc_rmse; 262, argument_list; 262, 263; 262, 264; 263, identifier:slowness; 264, identifier:_slowness; 265, if_statement; 265, 266; 265, 267; 266, identifier:adjust_thickness; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, augmented_assignment:+=; 269, 270; 269, 271; 270, identifier:err; 271, call; 271, 272; 271, 273; 272, identifier:calc_rmse; 273, argument_list; 273, 274; 273, 275; 274, identifier:thickness; 275, identifier:_thickness; 276, if_statement; 276, 277; 276, 278; 277, identifier:adjust_site_atten; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, augmented_assignment:+=; 280, 281; 280, 282; 281, identifier:err; 282, call; 282, 283; 282, 284; 283, identifier:calc_rmse; 284, argument_list; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:_site_atten; 288, identifier:site_atten; 289, return_statement; 289, 290; 290, identifier:err; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:res; 294, call; 294, 295; 294, 296; 295, identifier:minimize; 296, argument_list; 296, 297; 296, 298; 296, 299; 296, 302; 297, identifier:err; 298, identifier:initial; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:method; 301, string:'L-BFGS-B'; 302, keyword_argument; 302, 303; 302, 304; 303, identifier:bounds; 304, identifier:bounds; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:slowness; 308, subscript; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:res; 311, identifier:x; 312, slice; 312, 313; 312, 314; 312, 315; 313, integer:0; 314, colon; 315, identifier:nl; 316, if_statement; 316, 317; 316, 318; 317, identifier:adjust_thickness; 318, block; 318, 319; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:thickness; 322, subscript; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:res; 325, identifier:x; 326, slice; 326, 327; 326, 328; 326, 329; 327, identifier:nl; 328, colon; 329, parenthesized_expression; 329, 330; 330, binary_operator:*; 330, 331; 330, 332; 331, integer:2; 332, identifier:nl; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:profile; 336, call; 336, 337; 336, 338; 337, identifier:Profile; 338, argument_list; 338, 339; 338, 363; 339, list_comprehension; 339, 340; 339, 350; 340, call; 340, 341; 340, 342; 341, identifier:Layer; 342, argument_list; 342, 343; 342, 346; 342, 347; 343, attribute; 343, 344; 343, 345; 344, identifier:l; 345, identifier:soil_type; 346, identifier:t; 347, binary_operator:/; 347, 348; 347, 349; 348, integer:1; 349, identifier:s; 350, for_in_clause; 350, 351; 350, 355; 351, pattern_list; 351, 352; 351, 353; 351, 354; 352, identifier:l; 353, identifier:t; 354, identifier:s; 355, call; 355, 356; 355, 357; 356, identifier:zip; 357, argument_list; 357, 358; 357, 361; 357, 362; 358, attribute; 358, 359; 358, 360; 359, identifier:self; 360, identifier:profile; 361, identifier:thickness; 362, identifier:slowness; 363, attribute; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:self; 366, identifier:profile; 367, identifier:wt_depth; 368, comment; 369, return_statement; 369, 370; 370, tuple; 370, 371; 370, 374; 370, 375; 371, attribute; 371, 372; 371, 373; 372, identifier:self; 373, identifier:motion; 374, identifier:profile; 375, attribute; 375, 376; 375, 377; 376, identifier:self; 377, identifier:loc_input | def fit(self,
target_type,
target,
adjust_thickness=False,
adjust_site_atten=False,
adjust_source_vel=False):
"""
Fit to a target crustal amplification or site term.
The fitting process adjusts the velocity, site attenuation, and layer
thickness (if enabled) to fit a target values. The frequency range is
specified by the input motion.
Parameters
----------
target_type: str
Options are 'crustal_amp' to only fit to the crustal amplification,
or 'site_term' to fit both the velocity and the site attenuation
parameter.
target: `array_like`
Target values.
adjust_thickness: bool (optional)
If the thickness of the layers is adjusted as well, default: False.
adjust_site_atten: bool (optional)
If the site attenuation is adjusted as well, default: False.
adjust_source_vel: bool (optional)
If the source velocity should be adjusted, default: False.
Returns
-------
profile: `pyrsa.site.Profile`
profile optimized to fit a target amplification.
"""
density = self.profile.density
nl = len(density)
# Slowness bounds
slowness = self.profile.slowness
thickness = self.profile.thickness
site_atten = self._site_atten
# Slowness
initial = slowness
bounds = 1 / np.tile((4000, 100), (nl, 1))
if not adjust_source_vel:
bounds[-1] = (initial[-1], initial[-1])
# Thickness bounds
if adjust_thickness:
bounds = np.r_[bounds, [[t / 2, 2 * t] for t in thickness]]
initial = np.r_[initial, thickness]
# Site attenuation bounds
if adjust_site_atten:
bounds = np.r_[bounds, [[0.0001, 0.200]]]
initial = np.r_[initial, self.site_atten]
def calc_rmse(this, that):
return np.mean(((this - that) / that) ** 2)
def err(x):
_slowness = x[0:nl]
if adjust_thickness:
_thickness = x[nl:(2 * nl)]
else:
_thickness = thickness
if adjust_site_atten:
self._site_atten = x[-1]
crustal_amp, site_term = self._calc_amp(density, _thickness,
_slowness)
calc = crustal_amp if target_type == 'crustal_amp' else site_term
err = 10 * calc_rmse(target, calc)
# Prefer the original values so add the difference to the error
err += calc_rmse(slowness, _slowness)
if adjust_thickness:
err += calc_rmse(thickness, _thickness)
if adjust_site_atten:
err += calc_rmse(self._site_atten, site_atten)
return err
res = minimize(err, initial, method='L-BFGS-B', bounds=bounds)
slowness = res.x[0:nl]
if adjust_thickness:
thickness = res.x[nl:(2 * nl)]
profile = Profile([
Layer(l.soil_type, t, 1 / s)
for l, t, s in zip(self.profile, thickness, slowness)
], self.profile.wt_depth)
# Update the calculated amplificaiton
return (self.motion, profile, self.loc_input) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:build_recipe_input; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:ob; 6, identifier:dal; 7, block; 7, 8; 7, 10; 7, 14; 7, 15; 7, 16; 7, 17; 7, 18; 7, 19; 7, 23; 7, 27; 7, 167; 7, 168; 7, 177; 7, 183; 7, 223; 7, 272; 7, 342; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:result; 13, dictionary; 14, comment; 15, comment; 16, comment; 17, comment; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:ob_query_skip; 22, False; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:ob_query_field; 26, string:'obresult'; 27, if_statement; 27, 28; 27, 33; 27, 161; 28, call; 28, 29; 28, 30; 29, identifier:isinstance; 30, argument_list; 30, 31; 30, 32; 31, identifier:ob; 32, identifier:ObservingBlock; 33, block; 33, 34; 33, 41; 33, 42; 34, import_statement; 34, 35; 35, aliased_import; 35, 36; 35, 40; 36, dotted_name; 36, 37; 36, 38; 36, 39; 37, identifier:numina; 38, identifier:types; 39, identifier:obsresult; 40, identifier:obtype; 41, comment; 42, for_statement; 42, 43; 42, 46; 42, 55; 42, 154; 43, pattern_list; 43, 44; 43, 45; 44, identifier:key; 45, identifier:req; 46, call; 46, 47; 46, 54; 47, attribute; 47, 48; 47, 53; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:requirements; 52, argument_list; 53, identifier:items; 54, argument_list; 55, block; 55, 56; 56, if_statement; 56, 57; 56, 66; 57, call; 57, 58; 57, 59; 58, identifier:isinstance; 59, argument_list; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:req; 62, identifier:type; 63, attribute; 63, 64; 63, 65; 64, identifier:obtype; 65, identifier:ObservationResultType; 66, block; 66, 67; 66, 71; 66, 75; 66, 86; 66, 87; 66, 93; 66, 101; 66, 114; 66, 122; 66, 153; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:ob_query_field; 70, identifier:key; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:ob_query_skip; 74, True; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:query_option; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:query_options; 83, identifier:get; 84, argument_list; 84, 85; 85, identifier:key; 86, comment; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:new_or; 90, call; 90, 91; 90, 92; 91, identifier:ObservationResult; 92, argument_list; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:new_or; 97, identifier:__dict__; 98, attribute; 98, 99; 98, 100; 99, identifier:ob; 100, identifier:__dict__; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:obsres; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:req; 107, identifier:query; 108, argument_list; 108, 109; 108, 110; 108, 111; 109, identifier:dal; 110, identifier:new_or; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:options; 113, identifier:query_option; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:tagger; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:mode; 121, identifier:tagger; 122, if_statement; 122, 123; 122, 126; 122, 145; 123, comparison_operator:is; 123, 124; 123, 125; 124, identifier:tagger; 125, None; 126, block; 126, 127; 126, 136; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:logger; 133, identifier:debug; 134, argument_list; 134, 135; 135, string:'Use mode tagger to fill tags in OB'; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:obsres; 140, identifier:tags; 141, call; 141, 142; 141, 143; 142, identifier:tagger; 143, argument_list; 143, 144; 144, identifier:obsres; 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:obsres; 151, identifier:tags; 152, None; 153, break_statement; 154, else_clause; 154, 155; 154, 156; 155, comment; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:obsres; 160, identifier:ob; 161, else_clause; 161, 162; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:obsres; 166, identifier:ob; 167, comment; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 175; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:logger; 174, identifier:debug; 175, argument_list; 175, 176; 176, string:'getting query fields per REQ'; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:qfields; 180, call; 180, 181; 180, 182; 181, identifier:set; 182, argument_list; 183, for_statement; 183, 184; 183, 187; 183, 196; 184, pattern_list; 184, 185; 184, 186; 185, identifier:key; 186, identifier:req; 187, call; 187, 188; 187, 195; 188, attribute; 188, 189; 188, 194; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:requirements; 193, argument_list; 194, identifier:items; 195, argument_list; 196, block; 196, 197; 196, 205; 196, 216; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:tag_n; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:req; 203, identifier:tag_names; 204, argument_list; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 212; 207, attribute; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:logger; 211, identifier:debug; 212, argument_list; 212, 213; 212, 214; 212, 215; 213, string:"%s has these query fields %s"; 214, identifier:key; 215, identifier:tag_n; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:qfields; 220, identifier:update; 221, argument_list; 221, 222; 222, identifier:tag_n; 223, if_statement; 223, 224; 223, 229; 224, comparison_operator:is; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:obsres; 227, identifier:tags; 228, None; 229, block; 229, 230; 229, 239; 229, 249; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:logger; 236, identifier:debug; 237, argument_list; 237, 238; 238, string:'running recipe tagger'; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 246; 241, attribute; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:logger; 245, identifier:debug; 246, argument_list; 246, 247; 246, 248; 247, string:'with query fields %s'; 248, identifier:qfields; 249, if_statement; 249, 250; 249, 251; 249, 264; 250, identifier:qfields; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:obsres; 256, identifier:tags; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:obsres_extractor; 261, argument_list; 261, 262; 261, 263; 262, identifier:obsres; 263, identifier:qfields; 264, else_clause; 264, 265; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:obsres; 270, identifier:tags; 271, dictionary; 272, for_statement; 272, 273; 272, 276; 272, 285; 273, pattern_list; 273, 274; 273, 275; 274, identifier:key; 275, identifier:req; 276, call; 276, 277; 276, 284; 277, attribute; 277, 278; 277, 283; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:self; 281, identifier:requirements; 282, argument_list; 283, identifier:items; 284, argument_list; 285, block; 285, 286; 286, try_statement; 286, 287; 286, 329; 287, block; 287, 288; 287, 299; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:query_option; 291, call; 291, 292; 291, 297; 292, attribute; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:self; 295, identifier:query_options; 296, identifier:get; 297, argument_list; 297, 298; 298, identifier:key; 299, if_statement; 299, 300; 299, 305; 299, 312; 300, boolean_operator:and; 300, 301; 300, 304; 301, comparison_operator:==; 301, 302; 301, 303; 302, identifier:key; 303, identifier:ob_query_field; 304, identifier:ob_query_skip; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 311; 308, subscript; 308, 309; 308, 310; 309, identifier:result; 310, identifier:key; 311, identifier:obsres; 312, else_clause; 312, 313; 313, block; 313, 314; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 319; 316, subscript; 316, 317; 316, 318; 317, identifier:result; 318, identifier:key; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:req; 322, identifier:query; 323, argument_list; 323, 324; 323, 325; 323, 326; 324, identifier:dal; 325, identifier:obsres; 326, keyword_argument; 326, 327; 326, 328; 327, identifier:options; 328, identifier:query_option; 329, except_clause; 329, 330; 329, 334; 330, as_pattern; 330, 331; 330, 332; 331, identifier:NoResultFound; 332, as_pattern_target; 332, 333; 333, identifier:notfound; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:req; 339, identifier:on_query_not_found; 340, argument_list; 340, 341; 341, identifier:notfound; 342, return_statement; 342, 343; 343, call; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:self; 346, identifier:create_input; 347, argument_list; 347, 348; 348, dictionary_splat; 348, 349; 349, identifier:result | def build_recipe_input(self, ob, dal):
"""Build a RecipeInput object."""
result = {}
# We have to decide if the ob input
# is a plain description (ObservingBlock)
# or if it contains the nested results (Obsres)
#
# it has to contain the tags corresponding to the observing modes...
ob_query_skip = False
ob_query_field = 'obresult'
if isinstance(ob, ObservingBlock):
import numina.types.obsresult as obtype
# We have to build an Obsres
for key, req in self.requirements().items():
if isinstance(req.type, obtype.ObservationResultType):
ob_query_field = key
ob_query_skip = True
query_option = self.query_options.get(key)
# print('req for ob is named', key, query_option)
new_or = ObservationResult()
new_or.__dict__ = ob.__dict__
obsres = req.query(dal, new_or, options=query_option)
tagger = self.mode.tagger
if tagger is not None:
self.logger.debug('Use mode tagger to fill tags in OB')
obsres.tags = tagger(obsres)
else:
obsres.tags = None
break
else:
# nothing to do
obsres = ob
else:
obsres = ob
# Get tags_names per REQ
self.logger.debug('getting query fields per REQ')
qfields = set()
for key, req in self.requirements().items():
tag_n = req.tag_names()
self.logger.debug("%s has these query fields %s", key, tag_n)
qfields.update(tag_n)
if obsres.tags is None:
self.logger.debug('running recipe tagger')
self.logger.debug('with query fields %s', qfields)
if qfields:
obsres.tags = self.obsres_extractor(obsres, qfields)
else:
obsres.tags = {}
for key, req in self.requirements().items():
try:
query_option = self.query_options.get(key)
if key == ob_query_field and ob_query_skip:
result[key] = obsres
else:
result[key] = req.query(dal, obsres, options=query_option)
except NoResultFound as notfound:
req.on_query_not_found(notfound)
return self.create_input(**result) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:subsets_of_fileinfo_from_txt; 3, parameters; 3, 4; 4, identifier:filename; 5, block; 5, 6; 5, 8; 5, 9; 5, 29; 5, 30; 5, 53; 5, 54; 5, 58; 5, 62; 5, 66; 5, 70; 5, 74; 5, 78; 5, 82; 5, 297; 5, 306; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 19; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:os; 15, identifier:path; 16, identifier:isfile; 17, argument_list; 17, 18; 18, identifier:filename; 19, block; 19, 20; 20, raise_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:ValueError; 23, argument_list; 23, 24; 24, binary_operator:+; 24, 25; 24, 28; 25, binary_operator:+; 25, 26; 25, 27; 26, string:"File "; 27, identifier:filename; 28, string:" not found!"; 29, comment; 30, with_statement; 30, 31; 30, 40; 31, with_clause; 31, 32; 32, with_item; 32, 33; 33, as_pattern; 33, 34; 33, 38; 34, call; 34, 35; 34, 36; 35, identifier:open; 36, argument_list; 36, 37; 37, identifier:filename; 38, as_pattern_target; 38, 39; 39, identifier:f; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:file_content; 44, call; 44, 45; 44, 52; 45, attribute; 45, 46; 45, 51; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:f; 49, identifier:read; 50, argument_list; 51, identifier:splitlines; 52, argument_list; 53, comment; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:dict_of_subsets_of_fileinfo; 57, dictionary; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:label; 61, None; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:sublist_of_fileinfo; 65, list:[]; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:idict; 69, integer:0; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:ifiles; 73, integer:0; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:nfiles; 77, integer:0; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:sublist_finished; 81, True; 82, for_statement; 82, 83; 82, 84; 82, 85; 83, identifier:line; 84, identifier:file_content; 85, block; 85, 86; 86, if_statement; 86, 87; 86, 93; 87, comparison_operator:>; 87, 88; 87, 92; 88, call; 88, 89; 88, 90; 89, identifier:len; 90, argument_list; 90, 91; 91, identifier:line; 92, integer:0; 93, block; 93, 94; 94, if_statement; 94, 95; 94, 100; 95, comparison_operator:!=; 95, 96; 95, 99; 96, subscript; 96, 97; 96, 98; 97, identifier:line; 98, integer:0; 99, string:'#'; 100, block; 100, 101; 101, if_statement; 101, 102; 101, 105; 101, 163; 102, comparison_operator:is; 102, 103; 102, 104; 103, identifier:label; 104, None; 105, block; 105, 106; 106, if_statement; 106, 107; 106, 112; 106, 156; 107, comparison_operator:==; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:line; 110, integer:0; 111, string:"@"; 112, block; 112, 113; 112, 130; 112, 144; 112, 148; 112, 152; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:nfiles; 116, call; 116, 117; 116, 118; 117, identifier:int; 118, argument_list; 118, 119; 119, subscript; 119, 120; 119, 129; 120, call; 120, 121; 120, 128; 121, attribute; 121, 122; 121, 127; 122, subscript; 122, 123; 122, 124; 123, identifier:line; 124, slice; 124, 125; 124, 126; 125, integer:1; 126, colon; 127, identifier:split; 128, argument_list; 129, integer:0; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:label; 133, subscript; 133, 134; 133, 143; 134, call; 134, 135; 134, 142; 135, attribute; 135, 136; 135, 141; 136, subscript; 136, 137; 136, 138; 137, identifier:line; 138, slice; 138, 139; 138, 140; 139, integer:1; 140, colon; 141, identifier:split; 142, argument_list; 143, integer:1; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:sublist_of_fileinfo; 147, list:[]; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:ifiles; 151, integer:0; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:sublist_finished; 155, False; 156, else_clause; 156, 157; 157, block; 157, 158; 158, raise_statement; 158, 159; 159, call; 159, 160; 159, 161; 160, identifier:ValueError; 161, argument_list; 161, 162; 162, string:"Expected @ symbol not found!"; 163, else_clause; 163, 164; 164, block; 164, 165; 164, 177; 164, 185; 164, 191; 164, 213; 164, 233; 164, 244; 164, 248; 165, if_statement; 165, 166; 165, 171; 166, comparison_operator:==; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:line; 169, integer:0; 170, string:"@"; 171, block; 171, 172; 172, raise_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:ValueError; 175, argument_list; 175, 176; 176, string:"Unexpected @ symbol found!"; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:tmplist; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:line; 183, identifier:split; 184, argument_list; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:tmpfile; 188, subscript; 188, 189; 188, 190; 189, identifier:tmplist; 190, integer:0; 191, if_statement; 191, 192; 191, 198; 191, 207; 192, comparison_operator:>; 192, 193; 192, 197; 193, call; 193, 194; 193, 195; 194, identifier:len; 195, argument_list; 195, 196; 196, identifier:tmplist; 197, integer:1; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:tmpinfo; 202, subscript; 202, 203; 202, 204; 203, identifier:tmplist; 204, slice; 204, 205; 204, 206; 205, integer:1; 206, colon; 207, else_clause; 207, 208; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:tmpinfo; 212, None; 213, if_statement; 213, 214; 213, 223; 214, not_operator; 214, 215; 215, call; 215, 216; 215, 221; 216, attribute; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:os; 219, identifier:path; 220, identifier:isfile; 221, argument_list; 221, 222; 222, identifier:tmpfile; 223, block; 223, 224; 224, raise_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:ValueError; 227, argument_list; 227, 228; 228, binary_operator:+; 228, 229; 228, 232; 229, binary_operator:+; 229, 230; 229, 231; 230, string:"File "; 231, identifier:tmpfile; 232, string:" not found!"; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:sublist_of_fileinfo; 237, identifier:append; 238, argument_list; 238, 239; 239, call; 239, 240; 239, 241; 240, identifier:FileInfo; 241, argument_list; 241, 242; 241, 243; 242, identifier:tmpfile; 243, identifier:tmpinfo; 244, expression_statement; 244, 245; 245, augmented_assignment:+=; 245, 246; 245, 247; 246, identifier:ifiles; 247, integer:1; 248, if_statement; 248, 249; 248, 252; 249, comparison_operator:==; 249, 250; 249, 251; 250, identifier:ifiles; 251, identifier:nfiles; 252, block; 252, 253; 252, 259; 252, 265; 252, 271; 252, 277; 252, 281; 252, 285; 252, 289; 252, 293; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 258; 255, subscript; 255, 256; 255, 257; 256, identifier:dict_of_subsets_of_fileinfo; 257, identifier:idict; 258, dictionary; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:tmpdict; 262, subscript; 262, 263; 262, 264; 263, identifier:dict_of_subsets_of_fileinfo; 264, identifier:idict; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 270; 267, subscript; 267, 268; 267, 269; 268, identifier:tmpdict; 269, string:'label'; 270, identifier:label; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 276; 273, subscript; 273, 274; 273, 275; 274, identifier:tmpdict; 275, string:'list_of_fileinfo'; 276, identifier:sublist_of_fileinfo; 277, expression_statement; 277, 278; 278, augmented_assignment:+=; 278, 279; 278, 280; 279, identifier:idict; 280, integer:1; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:label; 284, None; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:sublist_of_fileinfo; 288, list:[]; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:ifiles; 292, integer:0; 293, expression_statement; 293, 294; 294, assignment; 294, 295; 294, 296; 295, identifier:sublist_finished; 296, True; 297, if_statement; 297, 298; 297, 300; 298, not_operator; 298, 299; 299, identifier:sublist_finished; 300, block; 300, 301; 301, raise_statement; 301, 302; 302, call; 302, 303; 302, 304; 303, identifier:ValueError; 304, argument_list; 304, 305; 305, string:"Unexpected end of sublist of files."; 306, return_statement; 306, 307; 307, identifier:dict_of_subsets_of_fileinfo | def subsets_of_fileinfo_from_txt(filename):
"""Returns a dictionary with subsets of FileInfo instances from a TXT file.
Each subset of files must be preceded by a line:
@ <number> <label>
where <number> indicates the number of files in that subset,
and <label> is a label for that subset. Any additional text
beyond <label> in the same line is ignored.
Note that blank lines or lines starting by the hash symbol are
also ignored. The name of the files comprising each subset will be
obtained from the first contiguous character string in every
line (thus, the rest of the line will be discarded).
Parameters
----------
filename : string
Name of a TXT file containing a list of FITS files grouped
in different subsets by the @ symbol.
Returns
-------
dict_of_subsets_of_fileinfo : dictionary
Dictionary containing as many entries as different subsets
of files available. Each value of the dictionary is a
dictionary with a label (sequential number starting at zero)
and the list of FileInfo instances within subset.
"""
# check for input file
if not os.path.isfile(filename):
raise ValueError("File " + filename + " not found!")
# read input file
with open(filename) as f:
file_content = f.read().splitlines()
# obtain the different subsets of files
dict_of_subsets_of_fileinfo = {}
label = None
sublist_of_fileinfo = []
idict = 0
ifiles = 0
nfiles = 0
sublist_finished = True
for line in file_content:
if len(line) > 0:
if line[0] != '#':
if label is None:
if line[0] == "@":
nfiles = int(line[1:].split()[0])
label = line[1:].split()[1]
sublist_of_fileinfo = []
ifiles = 0
sublist_finished = False
else:
raise ValueError("Expected @ symbol not found!")
else:
if line[0] == "@":
raise ValueError("Unexpected @ symbol found!")
tmplist = line.split()
tmpfile = tmplist[0]
if len(tmplist) > 1:
tmpinfo = tmplist[1:]
else:
tmpinfo = None
if not os.path.isfile(tmpfile):
raise ValueError("File " + tmpfile + " not found!")
sublist_of_fileinfo.append(FileInfo(tmpfile, tmpinfo))
ifiles += 1
if ifiles == nfiles:
dict_of_subsets_of_fileinfo[idict] = {}
tmpdict = dict_of_subsets_of_fileinfo[idict]
tmpdict['label'] = label
tmpdict['list_of_fileinfo'] = sublist_of_fileinfo
idict += 1
label = None
sublist_of_fileinfo = []
ifiles = 0
sublist_finished = True
if not sublist_finished:
raise ValueError("Unexpected end of sublist of files.")
return dict_of_subsets_of_fileinfo |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:verify_refresh; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:request; 6, block; 6, 7; 6, 9; 6, 20; 6, 29; 6, 39; 6, 70; 6, 79; 6, 91; 6, 126; 6, 182; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 14; 10, not_operator; 10, 11; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:allow_refresh; 14, block; 14, 15; 15, raise_statement; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:InvalidTokenError; 18, argument_list; 18, 19; 19, string:'Token refresh is disabled'; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:token; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:get_jwt; 27, argument_list; 27, 28; 28, identifier:request; 29, if_statement; 29, 30; 29, 33; 30, comparison_operator:is; 30, 31; 30, 32; 31, identifier:token; 32, None; 33, block; 33, 34; 34, raise_statement; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:InvalidTokenError; 37, argument_list; 37, 38; 38, string:'Token not found'; 39, try_statement; 39, 40; 39, 53; 39, 54; 39, 62; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:claims_set; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:decode_jwt; 48, argument_list; 48, 49; 48, 50; 49, identifier:token; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:verify_expiration_on_refresh; 53, comment; 54, except_clause; 54, 55; 54, 56; 55, identifier:DecodeError; 56, block; 56, 57; 57, raise_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:DecodeError; 60, argument_list; 60, 61; 61, string:'Token could not be decoded'; 62, except_clause; 62, 63; 62, 64; 63, identifier:ExpiredSignatureError; 64, block; 64, 65; 65, raise_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:ExpiredSignatureError; 68, argument_list; 68, 69; 69, string:'Token has expired'; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:userid; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:get_userid; 77, argument_list; 77, 78; 78, identifier:claims_set; 79, if_statement; 79, 80; 79, 83; 80, comparison_operator:is; 80, 81; 80, 82; 81, identifier:userid; 82, None; 83, block; 83, 84; 84, raise_statement; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:MissingRequiredClaimError; 87, argument_list; 87, 88; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:userid_claim; 91, if_statement; 91, 92; 91, 97; 92, comparison_operator:is; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:refresh_nonce_handler; 96, None; 97, block; 97, 98; 97, 108; 98, if_statement; 98, 99; 98, 102; 99, comparison_operator:not; 99, 100; 99, 101; 100, string:'nonce'; 101, identifier:claims_set; 102, block; 102, 103; 103, raise_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:MissingRequiredClaimError; 106, argument_list; 106, 107; 107, string:'nonce'; 108, if_statement; 108, 109; 108, 120; 109, comparison_operator:!=; 109, 110; 109, 117; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:refresh_nonce_handler; 114, argument_list; 114, 115; 114, 116; 115, identifier:request; 116, identifier:userid; 117, subscript; 117, 118; 117, 119; 118, identifier:claims_set; 119, string:'nonce'; 120, block; 120, 121; 121, raise_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:InvalidTokenError; 124, argument_list; 124, 125; 125, string:'Refresh nonce is not valid'; 126, if_statement; 126, 127; 126, 132; 127, comparison_operator:is; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:refresh_delta; 131, None; 132, block; 132, 133; 132, 143; 132, 158; 132, 167; 133, if_statement; 133, 134; 133, 137; 134, comparison_operator:not; 134, 135; 134, 136; 135, string:'refresh_until'; 136, identifier:claims_set; 137, block; 137, 138; 138, raise_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:MissingRequiredClaimError; 141, argument_list; 141, 142; 142, string:'refresh_until'; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:now; 146, call; 146, 147; 146, 148; 147, identifier:timegm; 148, argument_list; 148, 149; 149, call; 149, 150; 149, 157; 150, attribute; 150, 151; 150, 156; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:datetime; 154, identifier:utcnow; 155, argument_list; 156, identifier:utctimetuple; 157, argument_list; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:refresh_until; 161, call; 161, 162; 161, 163; 162, identifier:int; 163, argument_list; 163, 164; 164, subscript; 164, 165; 164, 166; 165, identifier:claims_set; 166, string:'refresh_until'; 167, if_statement; 167, 168; 167, 176; 168, comparison_operator:<; 168, 169; 168, 170; 169, identifier:refresh_until; 170, parenthesized_expression; 170, 171; 171, binary_operator:-; 171, 172; 171, 173; 172, identifier:now; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:leeway; 176, block; 176, 177; 177, raise_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:ExpiredSignatureError; 180, argument_list; 180, 181; 181, string:'Refresh nonce has expired'; 182, return_statement; 182, 183; 183, identifier:userid | def verify_refresh(self, request):
"""
Verify if the request to refresh the token is valid.
If valid it returns the userid which can be used to create
an updated identity with ``remember_identity``.
Otherwise it raises an exception based on InvalidTokenError.
:param request: current request object
:type request: :class:`morepath.Request`
:returns: userid
:raises: InvalidTokenError, ExpiredSignatureError, DecodeError,
MissingRequiredClaimError
"""
if not self.allow_refresh:
raise InvalidTokenError('Token refresh is disabled')
token = self.get_jwt(request)
if token is None:
raise InvalidTokenError('Token not found')
try:
claims_set = self.decode_jwt(
token, self.verify_expiration_on_refresh
)
# reraise the exceptions to change the error messages
except DecodeError:
raise DecodeError('Token could not be decoded')
except ExpiredSignatureError:
raise ExpiredSignatureError('Token has expired')
userid = self.get_userid(claims_set)
if userid is None:
raise MissingRequiredClaimError(self.userid_claim)
if self.refresh_nonce_handler is not None:
if 'nonce' not in claims_set:
raise MissingRequiredClaimError('nonce')
if self.refresh_nonce_handler(request,
userid) != claims_set['nonce']:
raise InvalidTokenError('Refresh nonce is not valid')
if self.refresh_delta is not None:
if 'refresh_until' not in claims_set:
raise MissingRequiredClaimError('refresh_until')
now = timegm(datetime.utcnow().utctimetuple())
refresh_until = int(claims_set['refresh_until'])
if refresh_until < (now - self.leeway):
raise ExpiredSignatureError('Refresh nonce has expired')
return userid |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:get_default_terminal; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:environ; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:fallback; 9, identifier:_UNSPECIFIED; 10, block; 10, 11; 10, 13; 10, 24; 10, 25; 10, 34; 10, 35; 10, 39; 10, 48; 10, 81; 10, 82; 10, 95; 10, 96; 10, 102; 10, 103; 10, 110; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:environ; 16, None; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:environ; 21, attribute; 21, 22; 21, 23; 22, identifier:os; 23, identifier:environ; 24, comment; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:in; 26, 27; 26, 28; 27, string:"PIAS_OPT_TERMINAL"; 28, identifier:environ; 29, block; 29, 30; 30, return_statement; 30, 31; 31, subscript; 31, 32; 31, 33; 32, identifier:environ; 33, string:"PIAS_OPT_TERMINAL"; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:terminals; 38, list:[]; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:colorterm; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:environ; 45, identifier:get; 46, argument_list; 46, 47; 47, string:"COLORTERM"; 48, for_statement; 48, 49; 48, 50; 48, 55; 49, identifier:filename; 50, tuple; 50, 51; 50, 52; 50, 53; 50, 54; 51, identifier:colorterm; 52, string:"gnome-terminal"; 53, string:"konsole"; 54, string:"xterm"; 55, block; 55, 56; 56, if_statement; 56, 57; 56, 60; 57, comparison_operator:is; 57, 58; 57, 59; 58, identifier:filename; 59, None; 60, block; 60, 61; 60, 69; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:filepath; 64, call; 64, 65; 64, 66; 65, identifier:find_executable; 66, argument_list; 66, 67; 66, 68; 67, identifier:filename; 68, identifier:environ; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:is; 70, 71; 70, 72; 71, identifier:filepath; 72, None; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:terminals; 78, identifier:append; 79, argument_list; 79, 80; 80, identifier:filepath; 81, comment; 82, for_statement; 82, 83; 82, 84; 82, 87; 83, identifier:ancestor; 84, call; 84, 85; 84, 86; 85, identifier:get_ancestor_processes; 86, argument_list; 87, block; 87, 88; 88, if_statement; 88, 89; 88, 92; 89, comparison_operator:in; 89, 90; 89, 91; 90, identifier:ancestor; 91, identifier:terminals; 92, block; 92, 93; 93, return_statement; 93, 94; 94, identifier:ancestor; 95, comment; 96, for_statement; 96, 97; 96, 98; 96, 99; 97, identifier:term; 98, identifier:terminals; 99, block; 99, 100; 100, return_statement; 100, 101; 101, identifier:term; 102, comment; 103, if_statement; 103, 104; 103, 107; 104, comparison_operator:is; 104, 105; 104, 106; 105, identifier:fallback; 106, identifier:_UNSPECIFIED; 107, block; 107, 108; 108, return_statement; 108, 109; 109, identifier:fallback; 110, raise_statement; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:ValueError; 113, argument_list; 113, 114; 114, string:"Could not find a terminal" | def get_default_terminal(environ=None, fallback=_UNSPECIFIED):
"""Get the user's default terminal program."""
if environ is None:
environ = os.environ
# If the option is specified in the environment, respect it.
if "PIAS_OPT_TERMINAL" in environ:
return environ["PIAS_OPT_TERMINAL"]
# Find all candiate terminal programs.
terminals = []
colorterm = environ.get("COLORTERM")
for filename in (colorterm, "gnome-terminal", "konsole", "xterm"):
if filename is not None:
filepath = find_executable(filename, environ)
if filepath is not None:
terminals.append(filepath)
# If one of them is an ancestor process, use that.
for ancestor in get_ancestor_processes():
if ancestor in terminals:
return ancestor
# Otherwise use the first option that we found.
for term in terminals:
return term
# Use an explicit fallback option if given.
if fallback is not _UNSPECIFIED:
return fallback
raise ValueError("Could not find a terminal") |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:bitset; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 11; 3, 14; 4, identifier:name; 5, identifier:members; 6, default_parameter; 6, 7; 6, 8; 7, identifier:base; 8, attribute; 8, 9; 8, 10; 9, identifier:bases; 10, identifier:BitSet; 11, default_parameter; 11, 12; 11, 13; 12, identifier:list; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:tuple; 16, False; 17, block; 17, 18; 17, 20; 17, 31; 17, 53; 17, 68; 17, 90; 17, 109; 17, 127; 17, 145; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 23; 21, not_operator; 21, 22; 22, identifier:name; 23, block; 23, 24; 24, raise_statement; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:ValueError; 27, argument_list; 27, 28; 28, binary_operator:%; 28, 29; 28, 30; 29, string:'empty bitset name: %r'; 30, identifier:name; 31, if_statement; 31, 32; 31, 45; 32, boolean_operator:or; 32, 33; 32, 39; 33, not_operator; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:hasattr; 36, argument_list; 36, 37; 36, 38; 37, identifier:members; 38, string:'__getitem__'; 39, not_operator; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:hasattr; 42, argument_list; 42, 43; 42, 44; 43, identifier:members; 44, string:'__len__'; 45, block; 45, 46; 46, raise_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:ValueError; 49, argument_list; 49, 50; 50, binary_operator:%; 50, 51; 50, 52; 51, string:'non-sequence bitset members: %r'; 52, identifier:members; 53, if_statement; 53, 54; 53, 59; 54, not_operator; 54, 55; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, identifier:members; 59, block; 59, 60; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:ValueError; 63, argument_list; 63, 64; 64, binary_operator:%; 64, 65; 64, 66; 65, string:'less than one bitset member: %r'; 66, tuple; 66, 67; 67, identifier:members; 68, if_statement; 68, 69; 68, 81; 69, comparison_operator:!=; 69, 70; 69, 77; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:set; 75, argument_list; 75, 76; 76, identifier:members; 77, call; 77, 78; 77, 79; 78, identifier:len; 79, argument_list; 79, 80; 80, identifier:members; 81, block; 81, 82; 82, raise_statement; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:ValueError; 85, argument_list; 85, 86; 86, binary_operator:%; 86, 87; 86, 88; 87, string:'bitset members contains duplicates: %r'; 88, tuple; 88, 89; 89, identifier:members; 90, if_statement; 90, 91; 90, 101; 91, not_operator; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:issubclass; 94, argument_list; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:base; 97, identifier:__class__; 98, attribute; 98, 99; 98, 100; 99, identifier:meta; 100, identifier:MemberBitsMeta; 101, block; 101, 102; 102, raise_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:ValueError; 105, argument_list; 105, 106; 106, binary_operator:%; 106, 107; 106, 108; 107, string:'base does not subclass bitset.bases: %r'; 108, identifier:base; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:list; 112, call; 112, 113; 112, 124; 113, attribute; 113, 114; 113, 123; 114, dictionary; 114, 115; 114, 118; 115, pair; 115, 116; 115, 117; 116, False; 117, None; 118, pair; 118, 119; 118, 120; 119, True; 120, attribute; 120, 121; 120, 122; 121, identifier:series; 122, identifier:List; 123, identifier:get; 124, argument_list; 124, 125; 124, 126; 125, identifier:list; 126, identifier:list; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:tuple; 130, call; 130, 131; 130, 142; 131, attribute; 131, 132; 131, 141; 132, dictionary; 132, 133; 132, 136; 133, pair; 133, 134; 133, 135; 134, False; 135, None; 136, pair; 136, 137; 136, 138; 137, True; 138, attribute; 138, 139; 138, 140; 139, identifier:series; 140, identifier:Tuple; 141, identifier:get; 142, argument_list; 142, 143; 142, 144; 143, identifier:tuple; 144, identifier:tuple; 145, return_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:base; 149, identifier:_make_subclass; 150, argument_list; 150, 151; 150, 152; 150, 153; 150, 156; 151, identifier:name; 152, identifier:members; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:listcls; 155, identifier:list; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:tuplecls; 158, identifier:tuple | def bitset(name, members, base=bases.BitSet, list=False, tuple=False):
"""Return a new bitset class with given name and members.
Args:
name: Name of the class to be created.
members: Hashable sequence of allowed bitset members.
base: Base class to derive the returned class from.
list (bool): Include a custom class for bitset lists.
tuple (bool): Include a custom class for bitset tuples.
Example:
>>> Letters = bitset('Letters', 'abcdef', list=True, tuple=True)
>>> Letters # doctest: +ELLIPSIS
<class bitsets.meta.bitset('Letters', 'abcdef', 0x..., BitSet, List, Tuple)>
>>> Letters('deadbeef')
Letters(['a', 'b', 'd', 'e', 'f'])
"""
if not name:
raise ValueError('empty bitset name: %r' % name)
if not hasattr(members, '__getitem__') or not hasattr(members, '__len__'):
raise ValueError('non-sequence bitset members: %r' % members)
if not len(members):
raise ValueError('less than one bitset member: %r' % (members,))
if len(set(members)) != len(members):
raise ValueError('bitset members contains duplicates: %r' % (members,))
if not issubclass(base.__class__, meta.MemberBitsMeta):
raise ValueError('base does not subclass bitset.bases: %r' % base)
list = {False: None, True: series.List}.get(list, list)
tuple = {False: None, True: series.Tuple}.get(tuple, tuple)
return base._make_subclass(name, members, listcls=list, tuplecls=tuple) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_split_refextract_authors_str; 3, parameters; 3, 4; 4, identifier:authors_str; 5, block; 5, 6; 5, 8; 5, 27; 5, 31; 5, 35; 5, 150; 5, 151; 5, 161; 5, 162; 5, 163; 5, 164; 5, 165; 5, 166; 5, 194; 5, 214; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:author_seq; 11, generator_expression; 11, 12; 11, 17; 11, 25; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:x; 15, identifier:strip; 16, argument_list; 17, for_in_clause; 17, 18; 17, 19; 18, identifier:x; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:RE_SPLIT_AUTH; 22, identifier:split; 23, argument_list; 23, 24; 24, identifier:authors_str; 25, if_clause; 25, 26; 26, identifier:x; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:res; 30, list:[]; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:current; 34, string:''; 35, for_statement; 35, 36; 35, 37; 35, 38; 36, identifier:author; 37, identifier:author_seq; 38, block; 38, 39; 38, 64; 38, 65; 38, 79; 38, 80; 38, 94; 38, 95; 38, 109; 38, 110; 39, if_statement; 39, 40; 39, 48; 40, not_operator; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 45; 44, identifier:author; 45, attribute; 45, 46; 45, 47; 46, identifier:six; 47, identifier:text_type; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:author; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:six; 55, identifier:text_type; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:author; 60, identifier:decode; 61, argument_list; 61, 62; 61, 63; 62, string:'utf8'; 63, string:'ignore'; 64, comment; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:author; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:re; 71, identifier:sub; 72, argument_list; 72, 73; 72, 74; 72, 75; 72, 76; 73, string:r'\(|\)'; 74, string:''; 75, identifier:author; 76, attribute; 76, 77; 76, 78; 77, identifier:re; 78, identifier:U; 79, comment; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:author; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:re; 86, identifier:sub; 87, argument_list; 87, 88; 87, 89; 87, 90; 87, 91; 88, string:r'^[\W\d]+'; 89, string:''; 90, identifier:author; 91, attribute; 91, 92; 91, 93; 92, identifier:re; 93, identifier:U; 94, comment; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:author; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:re; 101, identifier:sub; 102, argument_list; 102, 103; 102, 104; 102, 105; 102, 106; 103, string:r'[^.\w]+$'; 104, string:''; 105, identifier:author; 106, attribute; 106, 107; 106, 108; 107, identifier:re; 108, identifier:U; 109, comment; 110, if_statement; 110, 111; 110, 117; 110, 134; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:RE_INITIALS_ONLY; 114, identifier:match; 115, argument_list; 115, 116; 116, identifier:author; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, augmented_assignment:+=; 119, 120; 119, 121; 120, identifier:current; 121, binary_operator:+; 121, 122; 121, 123; 122, string:', '; 123, call; 123, 124; 123, 131; 124, attribute; 124, 125; 124, 130; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:author; 128, identifier:strip; 129, argument_list; 130, identifier:replace; 131, argument_list; 131, 132; 131, 133; 132, string:'. '; 133, string:'.'; 134, else_clause; 134, 135; 135, block; 135, 136; 135, 146; 136, if_statement; 136, 137; 136, 138; 137, identifier:current; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:res; 143, identifier:append; 144, argument_list; 144, 145; 145, identifier:current; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:current; 149, identifier:author; 150, comment; 151, if_statement; 151, 152; 151, 153; 152, identifier:current; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:res; 158, identifier:append; 159, argument_list; 159, 160; 160, identifier:current; 161, comment; 162, comment; 163, comment; 164, comment; 165, comment; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:filters; 169, list:[
lambda a: a == 'ed',
lambda a: a.startswith(','),
lambda a: len(a) == 1
]; 169, 170; 169, 176; 169, 185; 170, lambda; 170, 171; 170, 173; 171, lambda_parameters; 171, 172; 172, identifier:a; 173, comparison_operator:==; 173, 174; 173, 175; 174, identifier:a; 175, string:'ed'; 176, lambda; 176, 177; 176, 179; 177, lambda_parameters; 177, 178; 178, identifier:a; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:a; 182, identifier:startswith; 183, argument_list; 183, 184; 184, string:','; 185, lambda; 185, 186; 185, 188; 186, lambda_parameters; 186, 187; 187, identifier:a; 188, comparison_operator:==; 188, 189; 188, 193; 189, call; 189, 190; 189, 191; 190, identifier:len; 191, argument_list; 191, 192; 192, identifier:a; 193, integer:1; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:res; 197, list_comprehension; 197, 198; 197, 199; 197, 202; 198, identifier:r; 199, for_in_clause; 199, 200; 199, 201; 200, identifier:r; 201, identifier:res; 202, if_clause; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:all; 205, generator_expression; 205, 206; 205, 211; 206, not_operator; 206, 207; 207, call; 207, 208; 207, 209; 208, identifier:f; 209, argument_list; 209, 210; 210, identifier:r; 211, for_in_clause; 211, 212; 211, 213; 212, identifier:f; 213, identifier:filters; 214, return_statement; 214, 215; 215, identifier:res | def _split_refextract_authors_str(authors_str):
"""Extract author names out of refextract authors output."""
author_seq = (x.strip() for x in RE_SPLIT_AUTH.split(authors_str) if x)
res = []
current = ''
for author in author_seq:
if not isinstance(author, six.text_type):
author = six.text_type(author.decode('utf8', 'ignore'))
# First clean the token.
author = re.sub(r'\(|\)', '', author, re.U)
# Names usually start with characters.
author = re.sub(r'^[\W\d]+', '', author, re.U)
# Names should end with characters or dot.
author = re.sub(r'[^.\w]+$', '', author, re.U)
# If we have initials join them with the previous token.
if RE_INITIALS_ONLY.match(author):
current += ', ' + author.strip().replace('. ', '.')
else:
if current:
res.append(current)
current = author
# Add last element.
if current:
res.append(current)
# Manual filterings that we don't want to add in regular expressions since
# it would make them more complex.
# * ed might sneak in
# * many legacy refs look like 'X. and Somebody E.'
# * might miss lowercase initials
filters = [
lambda a: a == 'ed',
lambda a: a.startswith(','),
lambda a: len(a) == 1
]
res = [r for r in res if all(not f(r) for f in filters)]
return res |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 30; 2, function_name:query; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 4, identifier:pattern_path; 5, identifier:dict_; 6, default_parameter; 6, 7; 6, 8; 7, identifier:max_length; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:strip; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:case_sensitive; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:unique; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:deduplicate; 20, False; 21, default_parameter; 21, 22; 21, 23; 22, identifier:string_transformations; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:hyperlink; 26, False; 27, default_parameter; 27, 28; 27, 29; 28, identifier:return_multiple_columns; 29, False; 30, block; 30, 31; 30, 33; 30, 42; 30, 59; 30, 77; 30, 89; 30, 90; 30, 91; 30, 95; 30, 102; 30, 103; 30, 104; 30, 105; 30, 111; 30, 131; 31, expression_statement; 31, 32; 32, comment; 33, if_statement; 33, 34; 33, 37; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:string_transformations; 36, None; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:string_transformations; 41, list:[]; 42, if_statement; 42, 43; 42, 44; 43, identifier:max_length; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:string_transformations; 49, identifier:append; 50, argument_list; 50, 51; 51, lambda; 51, 52; 51, 54; 52, lambda_parameters; 52, 53; 53, identifier:x; 54, subscript; 54, 55; 54, 56; 55, identifier:x; 56, slice; 56, 57; 56, 58; 57, colon; 58, identifier:max_length; 59, if_statement; 59, 60; 59, 61; 60, identifier:hyperlink; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:string_transformations; 66, identifier:append; 67, argument_list; 67, 68; 68, lambda; 68, 69; 68, 71; 69, lambda_parameters; 69, 70; 70, identifier:x; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, string:'=HYPERLINK("{0}")'; 74, identifier:format; 75, argument_list; 75, 76; 76, identifier:x; 77, if_statement; 77, 78; 77, 83; 78, call; 78, 79; 78, 80; 79, identifier:isinstance; 80, argument_list; 80, 81; 80, 82; 81, identifier:pattern_path; 82, identifier:basestring; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:pattern_path; 87, list:[pattern_path]; 87, 88; 88, identifier:pattern_path; 89, comment; 90, comment; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:original_pattern_path; 94, identifier:pattern_path; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:pattern_path; 98, subscript; 98, 99; 98, 100; 99, identifier:pattern_path; 100, slice; 100, 101; 101, colon; 102, comment; 103, comment; 104, comment; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:pattern_path; 109, identifier:reverse; 110, argument_list; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:result; 114, call; 114, 115; 114, 116; 115, identifier:_process_object; 116, argument_list; 116, 117; 116, 118; 116, 119; 116, 122; 116, 125; 116, 128; 117, identifier:pattern_path; 118, identifier:dict_; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:string_transformations; 121, identifier:string_transformations; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:strip; 124, identifier:strip; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:case_sensitive; 127, identifier:case_sensitive; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:return_multiple_columns; 130, identifier:return_multiple_columns; 131, if_statement; 131, 132; 131, 134; 131, 138; 131, 150; 131, 163; 132, not_operator; 132, 133; 133, identifier:result; 134, block; 134, 135; 134, 137; 135, return_statement; 135, 136; 136, None; 137, comment; 138, elif_clause; 138, 139; 138, 144; 139, call; 139, 140; 139, 141; 140, identifier:isinstance; 141, argument_list; 141, 142; 141, 143; 142, identifier:result; 143, identifier:dict; 144, block; 144, 145; 145, return_statement; 145, 146; 146, call; 146, 147; 146, 148; 147, identifier:_flatten; 148, argument_list; 148, 149; 149, identifier:result; 150, elif_clause; 150, 151; 150, 157; 151, comparison_operator:==; 151, 152; 151, 156; 152, call; 152, 153; 152, 154; 153, identifier:len; 154, argument_list; 154, 155; 155, identifier:result; 156, integer:1; 157, block; 157, 158; 157, 162; 158, return_statement; 158, 159; 159, subscript; 159, 160; 159, 161; 160, identifier:result; 161, integer:0; 162, comment; 163, else_clause; 163, 164; 164, block; 164, 165; 164, 193; 164, 221; 165, if_statement; 165, 166; 165, 167; 166, identifier:unique; 167, block; 167, 168; 167, 177; 167, 188; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:msg; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, string:"pattern_path: {0}\n\n"; 174, identifier:format; 175, argument_list; 175, 176; 176, identifier:original_pattern_path; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:msg; 180, binary_operator:+; 180, 181; 180, 182; 181, identifier:msg; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:pprint; 185, identifier:pformat; 186, argument_list; 186, 187; 187, identifier:dict_; 188, raise_statement; 188, 189; 189, call; 189, 190; 189, 191; 190, identifier:UniqueError; 191, argument_list; 191, 192; 192, identifier:msg; 193, if_statement; 193, 194; 193, 195; 193, 196; 194, identifier:deduplicate; 195, comment; 196, block; 196, 197; 196, 201; 196, 217; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:new_result; 200, list:[]; 201, for_statement; 201, 202; 201, 203; 201, 204; 202, identifier:item; 203, identifier:result; 204, block; 204, 205; 205, if_statement; 205, 206; 205, 209; 206, comparison_operator:not; 206, 207; 206, 208; 207, identifier:item; 208, identifier:new_result; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:new_result; 214, identifier:append; 215, argument_list; 215, 216; 216, identifier:item; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:result; 220, identifier:new_result; 221, return_statement; 221, 222; 222, identifier:result | def query(pattern_path, dict_, max_length=None, strip=False,
case_sensitive=False, unique=False, deduplicate=False,
string_transformations=None, hyperlink=False,
return_multiple_columns=False):
"""Query the given dict with the given pattern path and return the result.
The ``pattern_path`` is a either a single regular expression string or a
list of regex strings that will be matched against the keys of the dict and
its subdicts to find the value(s) in the dict to return.
The returned result is either a single value (None, "foo", 42, False...)
or (if the pattern path matched multiple values in the dict) a list of
values.
If the dict contains sub-lists or sub-dicts values from these will be
flattened into a simple flat list to be returned.
"""
if string_transformations is None:
string_transformations = []
if max_length:
string_transformations.append(lambda x: x[:max_length])
if hyperlink:
string_transformations.append(
lambda x: '=HYPERLINK("{0}")'.format(x))
if isinstance(pattern_path, basestring):
pattern_path = [pattern_path]
# Copy the pattern_path because we're going to modify it which can be
# unexpected and confusing to user code.
original_pattern_path = pattern_path
pattern_path = pattern_path[:]
# We're going to be popping strings off the end of the pattern path
# (because Python lists don't come with a convenient pop-from-front method)
# so we need the list in reverse order.
pattern_path.reverse()
result = _process_object(pattern_path, dict_,
string_transformations=string_transformations,
strip=strip, case_sensitive=case_sensitive,
return_multiple_columns=return_multiple_columns)
if not result:
return None # Empty lists finally get turned into None.
elif isinstance(result, dict):
return _flatten(result)
elif len(result) == 1:
return result[0] # One-item lists just get turned into the item.
else:
if unique:
msg = "pattern_path: {0}\n\n".format(original_pattern_path)
msg = msg + pprint.pformat(dict_)
raise UniqueError(msg)
if deduplicate:
# Deduplicate the list while maintaining order.
new_result = []
for item in result:
if item not in new_result:
new_result.append(item)
result = new_result
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:get_reactions; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 4, default_parameter; 4, 5; 4, 6; 5, identifier:columns; 6, string:'all'; 7, default_parameter; 7, 8; 7, 9; 8, identifier:n_results; 9, integer:20; 10, default_parameter; 10, 11; 10, 12; 11, identifier:write_db; 12, False; 13, dictionary_splat_pattern; 13, 14; 14, identifier:kwargs; 15, block; 15, 16; 15, 18; 15, 31; 15, 35; 15, 116; 15, 120; 15, 135; 15, 156; 15, 162; 15, 167; 15, 171; 15, 388; 15, 485; 15, 490; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 24; 19, boolean_operator:or; 19, 20; 19, 21; 20, identifier:write_db; 21, comparison_operator:==; 21, 22; 21, 23; 22, identifier:columns; 23, string:'all'; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:columns; 28, subscript; 28, 29; 28, 30; 29, identifier:all_columns; 30, string:'reactions'; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:queries; 34, dictionary; 35, for_statement; 35, 36; 35, 39; 35, 44; 36, pattern_list; 36, 37; 36, 38; 37, identifier:key; 38, identifier:value; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:kwargs; 42, identifier:items; 43, argument_list; 44, block; 44, 45; 44, 52; 44, 76; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:key; 48, call; 48, 49; 48, 50; 49, identifier:map_column_names; 50, argument_list; 50, 51; 51, identifier:key; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:==; 53, 54; 53, 55; 54, identifier:key; 55, string:'distinct'; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 64; 58, comparison_operator:in; 58, 59; 58, 60; 59, identifier:value; 60, list:[True, 'True', 'true']; 60, 61; 60, 62; 60, 63; 61, True; 62, string:'True'; 63, string:'true'; 64, block; 64, 65; 64, 75; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:queries; 69, identifier:update; 70, argument_list; 70, 71; 71, dictionary; 71, 72; 72, pair; 72, 73; 72, 74; 73, identifier:key; 74, True; 75, continue_statement; 76, if_statement; 76, 77; 76, 88; 76, 99; 77, boolean_operator:or; 77, 78; 77, 83; 78, call; 78, 79; 78, 80; 79, identifier:isinstance; 80, argument_list; 80, 81; 80, 82; 81, identifier:value; 82, identifier:int; 83, call; 83, 84; 83, 85; 84, identifier:isinstance; 85, argument_list; 85, 86; 85, 87; 86, identifier:value; 87, identifier:float; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:queries; 93, identifier:update; 94, argument_list; 94, 95; 95, dictionary; 95, 96; 96, pair; 96, 97; 96, 98; 97, identifier:key; 98, identifier:value; 99, else_clause; 99, 100; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:queries; 105, identifier:update; 106, argument_list; 106, 107; 107, dictionary; 107, 108; 108, pair; 108, 109; 108, 110; 109, identifier:key; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, string:'{0}'; 113, identifier:format; 114, argument_list; 114, 115; 115, identifier:value; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:subtables; 119, list:[]; 120, if_statement; 120, 121; 120, 122; 120, 129; 121, identifier:write_db; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:subtables; 126, list:['reactionSystems', 'publication']; 126, 127; 126, 128; 127, string:'reactionSystems'; 128, string:'publication'; 129, else_clause; 129, 130; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:subtables; 134, list:[]; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:data; 138, call; 138, 139; 138, 140; 139, identifier:query; 140, argument_list; 140, 141; 140, 144; 140, 147; 140, 150; 140, 153; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:table; 143, string:'reactions'; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:subtables; 146, identifier:subtables; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:columns; 149, identifier:columns; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:n_results; 152, identifier:n_results; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:queries; 155, identifier:queries; 156, if_statement; 156, 157; 156, 159; 157, not_operator; 157, 158; 158, identifier:write_db; 159, block; 159, 160; 160, return_statement; 160, 161; 161, identifier:data; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:print; 165, argument_list; 165, 166; 166, string:'Writing result to Reactions.db'; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:unique_ids; 170, list:[]; 171, for_statement; 171, 172; 171, 173; 171, 178; 172, identifier:row; 173, subscript; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:data; 176, string:'reactions'; 177, string:'edges'; 178, block; 178, 179; 179, with_statement; 179, 180; 179, 189; 180, with_clause; 180, 181; 181, with_item; 181, 182; 182, as_pattern; 182, 183; 182, 187; 183, call; 183, 184; 183, 185; 184, identifier:CathubSQLite; 185, argument_list; 185, 186; 186, string:'Reactions.db'; 187, as_pattern_target; 187, 188; 188, identifier:db; 189, block; 189, 190; 189, 196; 189, 200; 189, 238; 189, 242; 189, 246; 189, 280; 189, 302; 189, 308; 189, 314; 189, 315; 189, 319; 189, 325; 189, 342; 189, 349; 189, 350; 189, 364; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:row; 193, subscript; 193, 194; 193, 195; 194, identifier:row; 195, string:'node'; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:key_values; 199, dictionary; 200, for_statement; 200, 201; 200, 202; 200, 205; 201, identifier:key; 202, subscript; 202, 203; 202, 204; 203, identifier:all_columns; 204, string:'reactions'; 205, block; 205, 206; 205, 212; 205, 213; 205, 214; 205, 229; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:v; 209, subscript; 209, 210; 209, 211; 210, identifier:row; 211, identifier:key; 212, comment; 213, comment; 214, try_statement; 214, 215; 214, 225; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:v; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:json; 222, identifier:loads; 223, argument_list; 223, 224; 224, identifier:v; 225, except_clause; 225, 226; 225, 227; 226, identifier:BaseException; 227, block; 227, 228; 228, pass_statement; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 237; 231, subscript; 231, 232; 231, 233; 232, identifier:key_values; 233, call; 233, 234; 233, 235; 234, identifier:convert; 235, argument_list; 235, 236; 236, identifier:key; 237, identifier:v; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:ase_ids; 241, dictionary; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:energy_corrections; 245, dictionary; 246, for_statement; 246, 247; 246, 248; 246, 251; 247, identifier:row_rs; 248, subscript; 248, 249; 248, 250; 249, identifier:row; 250, string:'reactionSystems'; 251, block; 251, 252; 251, 260; 251, 270; 252, if_statement; 252, 253; 252, 258; 253, comparison_operator:==; 253, 254; 253, 257; 254, subscript; 254, 255; 254, 256; 255, identifier:row_rs; 256, string:'name'; 257, string:'N/A'; 258, block; 258, 259; 259, continue_statement; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 267; 262, subscript; 262, 263; 262, 264; 263, identifier:ase_ids; 264, subscript; 264, 265; 264, 266; 265, identifier:row_rs; 266, string:'name'; 267, subscript; 267, 268; 267, 269; 268, identifier:row_rs; 269, string:'aseId'; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 277; 272, subscript; 272, 273; 272, 274; 273, identifier:energy_corrections; 274, subscript; 274, 275; 274, 276; 275, identifier:row_rs; 276, string:'name'; 277, subscript; 277, 278; 277, 279; 278, identifier:row_rs; 279, string:'energyCorrection'; 280, if_statement; 280, 281; 280, 283; 280, 292; 281, not_operator; 281, 282; 282, identifier:ase_ids; 283, block; 283, 284; 283, 288; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:ase_ids; 287, None; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:energy_corrections; 291, None; 292, else_clause; 292, 293; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, augmented_assignment:+=; 295, 296; 295, 297; 296, identifier:unique_ids; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:ase_ids; 300, identifier:values; 301, argument_list; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 307; 304, subscript; 304, 305; 304, 306; 305, identifier:key_values; 306, string:'ase_ids'; 307, identifier:ase_ids; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 313; 310, subscript; 310, 311; 310, 312; 311, identifier:key_values; 312, string:'energy_corrections'; 313, identifier:ase_ids; 314, comment; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:pub_key_values; 318, dictionary; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:row_p; 322, subscript; 322, 323; 322, 324; 323, identifier:row; 324, string:'publication'; 325, for_statement; 325, 326; 325, 327; 325, 330; 326, identifier:key; 327, subscript; 327, 328; 327, 329; 328, identifier:all_columns; 329, string:'publications'; 330, block; 330, 331; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 339; 333, subscript; 333, 334; 333, 335; 334, identifier:pub_key_values; 335, call; 335, 336; 335, 337; 336, identifier:convert; 337, argument_list; 337, 338; 338, identifier:key; 339, subscript; 339, 340; 339, 341; 340, identifier:row_p; 341, identifier:key; 342, expression_statement; 342, 343; 343, call; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:db; 346, identifier:write_publication; 347, argument_list; 347, 348; 348, identifier:pub_key_values; 349, comment; 350, expression_statement; 350, 351; 351, assignment; 351, 352; 351, 353; 352, identifier:id; 353, call; 353, 354; 353, 357; 354, attribute; 354, 355; 354, 356; 355, identifier:db; 356, identifier:check; 357, argument_list; 357, 358; 357, 361; 358, subscript; 358, 359; 358, 360; 359, identifier:key_values; 360, string:'chemical_composition'; 361, subscript; 361, 362; 361, 363; 362, identifier:key_values; 363, string:'reaction_energy'; 364, if_statement; 364, 365; 364, 368; 364, 378; 365, comparison_operator:is; 365, 366; 365, 367; 366, identifier:id; 367, None; 368, block; 368, 369; 369, expression_statement; 369, 370; 370, assignment; 370, 371; 370, 372; 371, identifier:id; 372, call; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, identifier:db; 375, identifier:write; 376, argument_list; 376, 377; 377, identifier:key_values; 378, else_clause; 378, 379; 379, block; 379, 380; 380, expression_statement; 380, 381; 381, call; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, identifier:db; 384, identifier:update; 385, argument_list; 385, 386; 385, 387; 386, identifier:id; 387, identifier:key_values; 388, if_statement; 388, 389; 388, 392; 388, 393; 389, comparison_operator:is; 389, 390; 389, 391; 390, identifier:ase_ids; 391, None; 392, comment; 393, block; 393, 394; 394, with_statement; 394, 395; 394, 408; 395, with_clause; 395, 396; 396, with_item; 396, 397; 397, as_pattern; 397, 398; 397, 406; 398, call; 398, 399; 398, 404; 399, attribute; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:ase; 402, identifier:db; 403, identifier:connect; 404, argument_list; 404, 405; 405, string:'Reactions.db'; 406, as_pattern_target; 406, 407; 407, identifier:ase_db; 408, block; 408, 409; 408, 415; 408, 423; 408, 430; 408, 438; 408, 448; 408, 460; 409, expression_statement; 409, 410; 410, assignment; 410, 411; 410, 412; 411, identifier:con; 412, attribute; 412, 413; 412, 414; 413, identifier:ase_db; 414, identifier:connection; 415, expression_statement; 415, 416; 416, assignment; 416, 417; 416, 418; 417, identifier:cur; 418, call; 418, 419; 418, 422; 419, attribute; 419, 420; 419, 421; 420, identifier:con; 421, identifier:cursor; 422, argument_list; 423, expression_statement; 423, 424; 424, call; 424, 425; 424, 428; 425, attribute; 425, 426; 425, 427; 426, identifier:cur; 427, identifier:execute; 428, argument_list; 428, 429; 429, string:'SELECT unique_id from systems;'; 430, expression_statement; 430, 431; 431, assignment; 431, 432; 431, 433; 432, identifier:unique_ids0; 433, call; 433, 434; 433, 437; 434, attribute; 434, 435; 434, 436; 435, identifier:cur; 436, identifier:fetchall; 437, argument_list; 438, expression_statement; 438, 439; 439, assignment; 439, 440; 439, 441; 440, identifier:unique_ids0; 441, list_comprehension; 441, 442; 441, 445; 442, subscript; 442, 443; 442, 444; 443, identifier:un; 444, integer:0; 445, for_in_clause; 445, 446; 445, 447; 446, identifier:un; 447, identifier:unique_ids0; 448, expression_statement; 448, 449; 449, assignment; 449, 450; 449, 451; 450, identifier:unique_ids; 451, list_comprehension; 451, 452; 451, 453; 451, 456; 452, identifier:un; 453, for_in_clause; 453, 454; 453, 455; 454, identifier:un; 455, identifier:unique_ids; 456, if_clause; 456, 457; 457, comparison_operator:not; 457, 458; 457, 459; 458, identifier:un; 459, identifier:unique_ids0; 460, for_statement; 460, 461; 460, 462; 460, 469; 460, 470; 461, identifier:unique_id; 462, call; 462, 463; 462, 464; 463, identifier:list; 464, argument_list; 464, 465; 465, call; 465, 466; 465, 467; 466, identifier:set; 467, argument_list; 467, 468; 468, identifier:unique_ids; 469, comment; 470, block; 470, 471; 470, 478; 471, expression_statement; 471, 472; 472, assignment; 472, 473; 472, 474; 473, identifier:atomsrow; 474, call; 474, 475; 474, 476; 475, identifier:get_atomsrow_by_id; 476, argument_list; 476, 477; 477, identifier:unique_id; 478, expression_statement; 478, 479; 479, call; 479, 480; 479, 483; 480, attribute; 480, 481; 480, 482; 481, identifier:ase_db; 482, identifier:write; 483, argument_list; 483, 484; 484, identifier:atomsrow; 485, expression_statement; 485, 486; 486, call; 486, 487; 486, 488; 487, identifier:print; 488, argument_list; 488, 489; 489, string:'Writing complete!'; 490, return_statement; 490, 491; 491, identifier:data | def get_reactions(columns='all', n_results=20, write_db=False, **kwargs):
"""
Get reactions from server
Give key value strings as arguments
"""
if write_db or columns == 'all':
columns = all_columns['reactions']
queries = {}
for key, value in kwargs.items():
key = map_column_names(key)
if key == 'distinct':
if value in [True, 'True', 'true']:
queries.update({key: True})
continue
if isinstance(value, int) or isinstance(value, float):
queries.update({key: value})
else:
queries.update({key: '{0}'.format(value)})
subtables = []
if write_db:
subtables = ['reactionSystems', 'publication']
else:
subtables = []
data = query(table='reactions', subtables=subtables,
columns=columns,
n_results=n_results, queries=queries)
if not write_db:
return data
print('Writing result to Reactions.db')
unique_ids = []
for row in data['reactions']['edges']:
with CathubSQLite('Reactions.db') as db:
row = row['node']
key_values = {}
for key in all_columns['reactions']:
v = row[key]
# if isinstance(v, unicode):
# v = v.encode('utf-8')
try:
v = json.loads(v)
except BaseException:
pass
key_values[convert(key)] = v
ase_ids = {}
energy_corrections = {}
for row_rs in row['reactionSystems']:
if row_rs['name'] == 'N/A':
continue
ase_ids[row_rs['name']] = row_rs['aseId']
energy_corrections[row_rs['name']] = row_rs['energyCorrection']
if not ase_ids:
ase_ids = None
energy_corrections = None
else:
unique_ids += ase_ids.values()
key_values['ase_ids'] = ase_ids
key_values['energy_corrections'] = ase_ids
# publications
pub_key_values = {}
row_p = row['publication']
for key in all_columns['publications']:
pub_key_values[convert(key)] = row_p[key]
db.write_publication(pub_key_values)
# reactions and reaction_systems
id = db.check(key_values['chemical_composition'],
key_values['reaction_energy'])
if id is None:
id = db.write(key_values)
else:
db.update(id, key_values)
if ase_ids is not None:
# Ase structures
with ase.db.connect('Reactions.db') as ase_db:
con = ase_db.connection
cur = con.cursor()
cur.execute('SELECT unique_id from systems;')
unique_ids0 = cur.fetchall()
unique_ids0 = [un[0] for un in unique_ids0]
unique_ids = [un for un in unique_ids if un not in unique_ids0]
for unique_id in list(set(unique_ids)):
# if ase_db.count('unique_id={}'.format(unique_id)) == 0:
atomsrow = get_atomsrow_by_id(unique_id)
ase_db.write(atomsrow)
print('Writing complete!')
return data |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:extract_atoms; 3, parameters; 3, 4; 4, identifier:molecule; 5, block; 5, 6; 5, 8; 5, 15; 5, 26; 5, 30; 5, 109; 5, 158; 5, 235; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 9, comparison_operator:==; 9, 10; 9, 11; 10, identifier:molecule; 11, string:''; 12, block; 12, 13; 13, return_statement; 13, 14; 14, identifier:molecule; 15, try_statement; 15, 16; 15, 22; 16, block; 16, 17; 17, return_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:float; 20, argument_list; 20, 21; 21, identifier:molecule; 22, except_clause; 22, 23; 22, 24; 23, identifier:BaseException; 24, block; 24, 25; 25, pass_statement; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:atoms; 29, string:''; 30, if_statement; 30, 31; 30, 39; 30, 99; 31, not_operator; 31, 32; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:molecule; 36, integer:0; 37, identifier:isalpha; 38, argument_list; 39, block; 39, 40; 39, 44; 39, 58; 39, 69; 39, 91; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:i; 43, integer:0; 44, while_statement; 44, 45; 44, 53; 45, not_operator; 45, 46; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:molecule; 50, identifier:i; 51, identifier:isalpha; 52, argument_list; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, augmented_assignment:+=; 55, 56; 55, 57; 56, identifier:i; 57, integer:1; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:prefactor; 61, call; 61, 62; 61, 63; 62, identifier:float; 63, argument_list; 63, 64; 64, subscript; 64, 65; 64, 66; 65, identifier:molecule; 66, slice; 66, 67; 66, 68; 67, colon; 68, identifier:i; 69, if_statement; 69, 70; 69, 73; 69, 85; 70, comparison_operator:<; 70, 71; 70, 72; 71, identifier:prefactor; 72, integer:0; 73, block; 73, 74; 73, 81; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:prefactor; 77, call; 77, 78; 77, 79; 78, identifier:abs; 79, argument_list; 79, 80; 80, identifier:prefactor; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:sign; 84, string:'-'; 85, else_clause; 85, 86; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:sign; 90, string:''; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:molecule; 94, subscript; 94, 95; 94, 96; 95, identifier:molecule; 96, slice; 96, 97; 96, 98; 97, identifier:i; 98, colon; 99, else_clause; 99, 100; 100, block; 100, 101; 100, 105; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:prefactor; 104, integer:1; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:sign; 108, string:''; 109, for_statement; 109, 110; 109, 111; 109, 118; 110, identifier:k; 111, call; 111, 112; 111, 113; 112, identifier:range; 113, argument_list; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:len; 116, argument_list; 116, 117; 117, identifier:molecule; 118, block; 118, 119; 119, if_statement; 119, 120; 119, 127; 119, 150; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:molecule; 124, identifier:k; 125, identifier:isdigit; 126, argument_list; 127, block; 127, 128; 128, for_statement; 128, 129; 128, 130; 128, 141; 129, identifier:j; 130, call; 130, 131; 130, 132; 131, identifier:range; 132, argument_list; 132, 133; 133, binary_operator:-; 133, 134; 133, 140; 134, call; 134, 135; 134, 136; 135, identifier:int; 136, argument_list; 136, 137; 137, subscript; 137, 138; 137, 139; 138, identifier:molecule; 139, identifier:k; 140, integer:1; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, augmented_assignment:+=; 143, 144; 143, 145; 144, identifier:atoms; 145, subscript; 145, 146; 145, 147; 146, identifier:molecule; 147, binary_operator:-; 147, 148; 147, 149; 148, identifier:k; 149, integer:1; 150, else_clause; 150, 151; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, augmented_assignment:+=; 153, 154; 153, 155; 154, identifier:atoms; 155, subscript; 155, 156; 155, 157; 156, identifier:molecule; 157, identifier:k; 158, if_statement; 158, 159; 158, 164; 158, 172; 159, comparison_operator:==; 159, 160; 159, 163; 160, binary_operator:%; 160, 161; 160, 162; 161, identifier:prefactor; 162, integer:1; 163, integer:0; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, augmented_assignment:*=; 166, 167; 166, 168; 167, identifier:atoms; 168, call; 168, 169; 168, 170; 169, identifier:int; 170, argument_list; 170, 171; 171, identifier:prefactor; 172, elif_clause; 172, 173; 172, 178; 173, comparison_operator:==; 173, 174; 173, 177; 174, binary_operator:%; 174, 175; 174, 176; 175, identifier:prefactor; 176, integer:1; 177, float:0.5; 178, block; 178, 179; 178, 186; 178, 193; 178, 197; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:atoms_sort; 182, call; 182, 183; 182, 184; 183, identifier:sorted; 184, argument_list; 184, 185; 185, identifier:atoms; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:N; 189, call; 189, 190; 189, 191; 190, identifier:len; 191, argument_list; 191, 192; 192, identifier:atoms; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:atoms; 196, string:''; 197, for_statement; 197, 198; 197, 199; 197, 203; 198, identifier:n; 199, call; 199, 200; 199, 201; 200, identifier:range; 201, argument_list; 201, 202; 202, identifier:N; 203, block; 203, 204; 203, 222; 204, for_statement; 204, 205; 204, 206; 204, 215; 205, identifier:m; 206, call; 206, 207; 206, 208; 207, identifier:range; 208, argument_list; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:int; 211, argument_list; 211, 212; 212, binary_operator:-; 212, 213; 212, 214; 213, identifier:prefactor; 214, float:0.5; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, augmented_assignment:+=; 217, 218; 217, 219; 218, identifier:atoms; 219, subscript; 219, 220; 219, 221; 220, identifier:atoms_sort; 221, identifier:n; 222, if_statement; 222, 223; 222, 228; 223, comparison_operator:==; 223, 224; 223, 227; 224, binary_operator:%; 224, 225; 224, 226; 225, identifier:n; 226, integer:2; 227, integer:0; 228, block; 228, 229; 229, expression_statement; 229, 230; 230, augmented_assignment:+=; 230, 231; 230, 232; 231, identifier:atoms; 232, subscript; 232, 233; 232, 234; 233, identifier:atoms_sort; 234, identifier:n; 235, return_statement; 235, 236; 236, binary_operator:+; 236, 237; 236, 238; 237, identifier:sign; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, string:''; 241, identifier:join; 242, argument_list; 242, 243; 243, call; 243, 244; 243, 245; 244, identifier:sorted; 245, argument_list; 245, 246; 246, identifier:atoms | def extract_atoms(molecule):
"""Return a string with all atoms in molecule"""
if molecule == '':
return molecule
try:
return float(molecule)
except BaseException:
pass
atoms = ''
if not molecule[0].isalpha():
i = 0
while not molecule[i].isalpha():
i += 1
prefactor = float(molecule[:i])
if prefactor < 0:
prefactor = abs(prefactor)
sign = '-'
else:
sign = ''
molecule = molecule[i:]
else:
prefactor = 1
sign = ''
for k in range(len(molecule)):
if molecule[k].isdigit():
for j in range(int(molecule[k]) - 1):
atoms += molecule[k - 1]
else:
atoms += molecule[k]
if prefactor % 1 == 0:
atoms *= int(prefactor)
elif prefactor % 1 == 0.5:
atoms_sort = sorted(atoms)
N = len(atoms)
atoms = ''
for n in range(N):
for m in range(int(prefactor - 0.5)):
atoms += atoms_sort[n]
if n % 2 == 0:
atoms += atoms_sort[n]
return sign + ''.join(sorted(atoms)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:check_reaction; 3, parameters; 3, 4; 3, 5; 4, identifier:reactants; 5, identifier:products; 6, block; 6, 7; 6, 9; 6, 34; 6, 59; 6, 70; 6, 81; 6, 88; 6, 95; 6, 99; 6, 103; 6, 138; 6, 162; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:reactant_list; 12, list_comprehension; 12, 13; 12, 31; 13, call; 13, 14; 13, 29; 14, attribute; 14, 15; 14, 28; 15, call; 15, 16; 15, 26; 16, attribute; 16, 17; 16, 25; 17, subscript; 17, 18; 17, 24; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:reactant; 21, identifier:split; 22, argument_list; 22, 23; 23, string:'@'; 24, integer:0; 25, identifier:strip; 26, argument_list; 26, 27; 27, string:'star'; 28, identifier:strip; 29, argument_list; 29, 30; 30, string:'gas'; 31, for_in_clause; 31, 32; 31, 33; 32, identifier:reactant; 33, identifier:reactants; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:product_list; 37, list_comprehension; 37, 38; 37, 56; 38, call; 38, 39; 38, 54; 39, attribute; 39, 40; 39, 53; 40, call; 40, 41; 40, 51; 41, attribute; 41, 42; 41, 50; 42, subscript; 42, 43; 42, 49; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:product; 46, identifier:split; 47, argument_list; 47, 48; 48, string:'@'; 49, integer:0; 50, identifier:strip; 51, argument_list; 51, 52; 52, string:'star'; 53, identifier:strip; 54, argument_list; 54, 55; 55, string:'gas'; 56, for_in_clause; 56, 57; 56, 58; 57, identifier:product; 58, identifier:products; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:reactant_atoms; 62, list_comprehension; 62, 63; 62, 67; 63, call; 63, 64; 63, 65; 64, identifier:extract_atoms; 65, argument_list; 65, 66; 66, identifier:reactant; 67, for_in_clause; 67, 68; 67, 69; 68, identifier:reactant; 69, identifier:reactant_list; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:product_atoms; 73, list_comprehension; 73, 74; 73, 78; 74, call; 74, 75; 74, 76; 75, identifier:extract_atoms; 76, argument_list; 76, 77; 77, identifier:product; 78, for_in_clause; 78, 79; 78, 80; 79, identifier:product; 80, identifier:product_list; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:reactants; 84, call; 84, 85; 84, 86; 85, identifier:add_atoms; 86, argument_list; 86, 87; 87, identifier:reactant_atoms; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:products; 91, call; 91, 92; 91, 93; 92, identifier:add_atoms; 93, argument_list; 93, 94; 94, identifier:product_atoms; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:r_stars; 98, integer:0; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:p_stars; 102, integer:0; 103, for_statement; 103, 104; 103, 107; 103, 111; 104, pattern_list; 104, 105; 104, 106; 105, identifier:i; 106, identifier:a; 107, call; 107, 108; 107, 109; 108, identifier:enumerate; 109, argument_list; 109, 110; 110, identifier:reactant_atoms; 111, block; 111, 112; 112, if_statement; 112, 113; 112, 122; 112, 127; 113, boolean_operator:or; 113, 114; 113, 117; 114, comparison_operator:==; 114, 115; 114, 116; 115, identifier:a; 116, string:''; 117, comparison_operator:in; 117, 118; 117, 119; 118, string:'star'; 119, subscript; 119, 120; 119, 121; 120, identifier:reactant_list; 121, identifier:i; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, augmented_assignment:+=; 124, 125; 124, 126; 125, identifier:r_stars; 126, integer:1; 127, elif_clause; 127, 128; 127, 133; 128, call; 128, 129; 128, 130; 129, identifier:isinstance; 130, argument_list; 130, 131; 130, 132; 131, identifier:a; 132, identifier:float; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, augmented_assignment:+=; 135, 136; 135, 137; 136, identifier:r_stars; 137, identifier:a; 138, for_statement; 138, 139; 138, 140; 138, 141; 139, identifier:a; 140, identifier:product_atoms; 141, block; 141, 142; 142, if_statement; 142, 143; 142, 146; 142, 151; 143, comparison_operator:==; 143, 144; 143, 145; 144, identifier:a; 145, string:''; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, augmented_assignment:+=; 148, 149; 148, 150; 149, identifier:p_stars; 150, integer:1; 151, elif_clause; 151, 152; 151, 157; 152, call; 152, 153; 152, 154; 153, identifier:isinstance; 154, argument_list; 154, 155; 154, 156; 155, identifier:a; 156, identifier:float; 157, block; 157, 158; 158, expression_statement; 158, 159; 159, augmented_assignment:+=; 159, 160; 159, 161; 160, identifier:p_stars; 161, identifier:a; 162, assert_statement; 162, 163; 163, comparison_operator:==; 163, 164; 163, 173; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, string:''; 167, identifier:join; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:sorted; 171, argument_list; 171, 172; 172, identifier:reactants; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, string:''; 176, identifier:join; 177, argument_list; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:sorted; 180, argument_list; 180, 181; 181, identifier:products | def check_reaction(reactants, products):
"""Check the stoichiometry and format of chemical reaction used for
folder structure.
list of reactants -> list of products
"""
reactant_list = [reactant.split('@')[0].strip(
'star').strip('gas') for reactant in reactants]
product_list = [product.split('@')[0].strip(
'star').strip('gas') for product in products]
reactant_atoms = [extract_atoms(reactant) for reactant in reactant_list]
product_atoms = [extract_atoms(product) for product in product_list]
reactants = add_atoms(reactant_atoms)
products = add_atoms(product_atoms)
r_stars = 0
p_stars = 0
for i, a in enumerate(reactant_atoms):
if a == '' or 'star' in reactant_list[i]:
r_stars += 1
elif isinstance(a, float):
r_stars += a
for a in product_atoms:
if a == '':
p_stars += 1
elif isinstance(a, float):
p_stars += a
assert ''.join(sorted(reactants)) == ''.join(sorted(products)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:fowler_array; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:fowlerdata; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ti; 7, float:0.0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ts; 10, float:0.0; 11, default_parameter; 11, 12; 11, 13; 12, identifier:gain; 13, float:1.0; 14, default_parameter; 14, 15; 14, 16; 15, identifier:ron; 16, float:1.0; 17, default_parameter; 17, 18; 17, 19; 18, identifier:badpixels; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:dtype; 22, string:'float64'; 23, default_parameter; 23, 24; 23, 25; 24, identifier:saturation; 25, integer:65631; 26, default_parameter; 26, 27; 26, 28; 27, identifier:blank; 28, integer:0; 29, default_parameter; 29, 30; 29, 31; 30, identifier:normalize; 31, False; 32, block; 32, 33; 32, 35; 32, 42; 32, 52; 32, 62; 32, 72; 32, 82; 32, 92; 32, 101; 32, 113; 32, 123; 32, 139; 32, 140; 32, 151; 32, 163; 32, 164; 32, 176; 32, 177; 32, 186; 32, 200; 32, 245; 32, 257; 32, 266; 32, 278; 32, 286; 32, 304; 33, expression_statement; 33, 34; 34, comment; 35, import_statement; 35, 36; 36, aliased_import; 36, 37; 36, 41; 37, dotted_name; 37, 38; 37, 39; 37, 40; 38, identifier:numina; 39, identifier:array; 40, identifier:_nirproc; 41, identifier:_nirproc; 42, if_statement; 42, 43; 42, 46; 43, comparison_operator:<=; 43, 44; 43, 45; 44, identifier:gain; 45, integer:0; 46, block; 46, 47; 47, raise_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:ValueError; 50, argument_list; 50, 51; 51, string:"invalid parameter, gain <= 0.0"; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:<=; 53, 54; 53, 55; 54, identifier:ron; 55, integer:0; 56, block; 56, 57; 57, raise_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:ValueError; 60, argument_list; 60, 61; 61, string:"invalid parameter, ron < 0.0"; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:<; 63, 64; 63, 65; 64, identifier:ti; 65, integer:0; 66, block; 66, 67; 67, raise_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:ValueError; 70, argument_list; 70, 71; 71, string:"invalid parameter, ti < 0.0"; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:<; 73, 74; 73, 75; 74, identifier:ts; 75, integer:0; 76, block; 76, 77; 77, raise_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:ValueError; 80, argument_list; 80, 81; 81, string:"invalid parameter, ts < 0.0"; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:<=; 83, 84; 83, 85; 84, identifier:saturation; 85, integer:0; 86, block; 86, 87; 87, raise_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:ValueError; 90, argument_list; 90, 91; 91, string:"invalid parameter, saturation <= 0"; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:fowlerdata; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:numpy; 98, identifier:asarray; 99, argument_list; 99, 100; 100, identifier:fowlerdata; 101, if_statement; 101, 102; 101, 107; 102, comparison_operator:!=; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:fowlerdata; 105, identifier:ndim; 106, integer:3; 107, block; 107, 108; 108, raise_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:ValueError; 111, argument_list; 111, 112; 112, string:'fowlerdata must be 3D'; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:npairs; 116, binary_operator://; 116, 117; 116, 122; 117, subscript; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:fowlerdata; 120, identifier:shape; 121, integer:0; 122, integer:2; 123, if_statement; 123, 124; 123, 133; 124, comparison_operator:!=; 124, 125; 124, 128; 125, binary_operator:*; 125, 126; 125, 127; 126, integer:2; 127, identifier:npairs; 128, subscript; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:fowlerdata; 131, identifier:shape; 132, integer:0; 133, block; 133, 134; 134, raise_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:ValueError; 137, argument_list; 137, 138; 138, string:'axis-0 in fowlerdata must be even'; 139, comment; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:ndtype; 143, call; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:fowlerdata; 147, identifier:dtype; 148, identifier:newbyteorder; 149, argument_list; 149, 150; 150, string:'='; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:fowlerdata; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:numpy; 157, identifier:asarray; 158, argument_list; 158, 159; 158, 160; 159, identifier:fowlerdata; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:dtype; 162, identifier:ndtype; 163, comment; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:fdtype; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:numpy; 170, identifier:result_type; 171, argument_list; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:fowlerdata; 174, identifier:dtype; 175, identifier:dtype; 176, comment; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:mdtype; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:numpy; 183, identifier:dtype; 184, argument_list; 184, 185; 185, string:'uint8'; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:fshape; 189, tuple; 189, 190; 189, 195; 190, subscript; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:fowlerdata; 193, identifier:shape; 194, integer:1; 195, subscript; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:fowlerdata; 198, identifier:shape; 199, integer:2; 200, if_statement; 200, 201; 200, 204; 200, 217; 201, comparison_operator:is; 201, 202; 201, 203; 202, identifier:badpixels; 203, None; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:badpixels; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:numpy; 211, identifier:zeros; 212, argument_list; 212, 213; 212, 214; 213, identifier:fshape; 214, keyword_argument; 214, 215; 214, 216; 215, identifier:dtype; 216, identifier:mdtype; 217, else_clause; 217, 218; 218, block; 218, 219; 218, 233; 219, if_statement; 219, 220; 219, 225; 220, comparison_operator:!=; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:badpixels; 223, identifier:shape; 224, identifier:fshape; 225, block; 225, 226; 226, raise_statement; 226, 227; 227, call; 227, 228; 227, 229; 228, identifier:ValueError; 229, argument_list; 229, 230; 230, concatenated_string; 230, 231; 230, 232; 231, string:'shape of badpixels is not '; 232, string:'compatible with shape of fowlerdata'; 233, if_statement; 233, 234; 233, 239; 234, comparison_operator:!=; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:badpixels; 237, identifier:dtype; 238, identifier:mdtype; 239, block; 239, 240; 240, raise_statement; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:ValueError; 243, argument_list; 243, 244; 244, string:'dtype of badpixels must be uint8'; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:result; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:numpy; 251, identifier:empty; 252, argument_list; 252, 253; 252, 254; 253, identifier:fshape; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:dtype; 256, identifier:fdtype; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:var; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:numpy; 263, identifier:empty_like; 264, argument_list; 264, 265; 265, identifier:result; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:npix; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:numpy; 272, identifier:empty; 273, argument_list; 273, 274; 273, 275; 274, identifier:fshape; 275, keyword_argument; 275, 276; 275, 277; 276, identifier:dtype; 277, identifier:mdtype; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:mask; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:badpixels; 284, identifier:copy; 285, argument_list; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:_nirproc; 290, identifier:_process_fowler_intl; 291, argument_list; 291, 292; 291, 293; 291, 294; 291, 295; 291, 296; 291, 297; 291, 298; 291, 299; 291, 300; 291, 301; 291, 302; 291, 303; 292, identifier:fowlerdata; 293, identifier:ti; 294, identifier:ts; 295, identifier:gain; 296, identifier:ron; 297, identifier:badpixels; 298, identifier:saturation; 299, identifier:blank; 300, identifier:result; 301, identifier:var; 302, identifier:npix; 303, identifier:mask; 304, return_statement; 304, 305; 305, expression_list; 305, 306; 305, 307; 305, 308; 305, 309; 306, identifier:result; 307, identifier:var; 308, identifier:npix; 309, identifier:mask | def fowler_array(fowlerdata, ti=0.0, ts=0.0, gain=1.0, ron=1.0,
badpixels=None, dtype='float64',
saturation=65631, blank=0, normalize=False):
"""Loop over the first axis applying Fowler processing.
*fowlerdata* is assumed to be a 3D numpy.ndarray containing the
result of a nIR observation in Fowler mode (Fowler and Gatley 1991).
The shape of the array must be of the form 2N_p x M x N, with N_p being
the number of pairs in Fowler mode.
The output signal is just the mean value of the differences between the
last N_p values (S_i) and the first N_p values (R-i).
.. math::
S_F = \\frac{1}{N_p}\\sum\\limits_{i=0}^{N_p-1} S_i - R_i
If the source has a radiance F, then the measured signal is equivalent
to:
.. math::
S_F = F T_I - F T_S (N_p -1) = F T_E
being T_I the integration time (*ti*), the time since the first
productive read to the last productive read for a given pixel and T_S the
time between samples (*ts*). T_E is the time between correlated reads
:math:`T_E = T_I - T_S (N_p - 1)`.
The variance of the signnal is the sum of two terms, one for the readout
noise:
.. math::
\\mathrm{var}(S_{F1}) =\\frac{2\sigma_R^2}{N_p}
and other for the photon noise:
.. math::
\\mathrm{var}(S_{F2}) = F T_E - F T_S \\frac{1}{3}(N_p-\\frac{1}{N_p})
= F T_I - F T_S (\\frac{4}{3} N_p -1 - \\frac{1}{3N_p})
:param fowlerdata: Convertible to a 3D numpy.ndarray with first axis even
:param ti: Integration time.
:param ts: Time between samples.
:param gain: Detector gain.
:param ron: Detector readout noise in counts.
:param badpixels: An optional MxN mask of dtype 'uint8'.
:param dtype: The dtype of the float outputs.
:param saturation: The saturation level of the detector.
:param blank: Invalid values in output are substituted by *blank*.
:returns: A tuple of (signal, variance of the signal, numper of pixels used
and badpixel mask.
:raises: ValueError
"""
import numina.array._nirproc as _nirproc
if gain <= 0:
raise ValueError("invalid parameter, gain <= 0.0")
if ron <= 0:
raise ValueError("invalid parameter, ron < 0.0")
if ti < 0:
raise ValueError("invalid parameter, ti < 0.0")
if ts < 0:
raise ValueError("invalid parameter, ts < 0.0")
if saturation <= 0:
raise ValueError("invalid parameter, saturation <= 0")
fowlerdata = numpy.asarray(fowlerdata)
if fowlerdata.ndim != 3:
raise ValueError('fowlerdata must be 3D')
npairs = fowlerdata.shape[0] // 2
if 2 * npairs != fowlerdata.shape[0]:
raise ValueError('axis-0 in fowlerdata must be even')
# change byteorder
ndtype = fowlerdata.dtype.newbyteorder('=')
fowlerdata = numpy.asarray(fowlerdata, dtype=ndtype)
# type of the output
fdtype = numpy.result_type(fowlerdata.dtype, dtype)
# Type of the mask
mdtype = numpy.dtype('uint8')
fshape = (fowlerdata.shape[1], fowlerdata.shape[2])
if badpixels is None:
badpixels = numpy.zeros(fshape, dtype=mdtype)
else:
if badpixels.shape != fshape:
raise ValueError('shape of badpixels is not '
'compatible with shape of fowlerdata')
if badpixels.dtype != mdtype:
raise ValueError('dtype of badpixels must be uint8')
result = numpy.empty(fshape, dtype=fdtype)
var = numpy.empty_like(result)
npix = numpy.empty(fshape, dtype=mdtype)
mask = badpixels.copy()
_nirproc._process_fowler_intl(
fowlerdata, ti, ts, gain, ron,
badpixels, saturation, blank,
result, var, npix, mask
)
return result, var, npix, mask |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:reactions; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:columns; 5, identifier:n_results; 6, identifier:write_db; 7, identifier:queries; 8, block; 8, 9; 8, 11; 8, 99; 8, 111; 8, 130; 8, 134; 8, 138; 8, 142; 8, 164; 8, 177; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 18; 12, not_operator; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:isinstance; 15, argument_list; 15, 16; 15, 17; 16, identifier:queries; 17, identifier:dict; 18, block; 18, 19; 18, 23; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:query_dict; 22, dictionary; 23, for_statement; 23, 24; 23, 25; 23, 26; 24, identifier:q; 25, identifier:queries; 26, block; 26, 27; 26, 38; 26, 61; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, pattern_list; 29, 30; 29, 31; 30, identifier:key; 31, identifier:value; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:q; 35, identifier:split; 36, argument_list; 36, 37; 37, string:'='; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:==; 39, 40; 39, 41; 40, identifier:key; 41, string:'distinct'; 42, block; 42, 43; 43, if_statement; 43, 44; 43, 49; 44, comparison_operator:in; 44, 45; 44, 46; 45, identifier:value; 46, list:['True', 'true']; 46, 47; 46, 48; 47, string:'True'; 48, string:'true'; 49, block; 49, 50; 49, 60; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:query_dict; 54, identifier:update; 55, argument_list; 55, 56; 56, dictionary; 56, 57; 57, pair; 57, 58; 57, 59; 58, identifier:key; 59, True; 60, continue_statement; 61, try_statement; 61, 62; 61, 80; 62, block; 62, 63; 62, 70; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:value; 66, call; 66, 67; 66, 68; 67, identifier:int; 68, argument_list; 68, 69; 69, identifier:value; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:query_dict; 74, identifier:update; 75, argument_list; 75, 76; 76, dictionary; 76, 77; 77, pair; 77, 78; 77, 79; 78, identifier:key; 79, identifier:value; 80, except_clause; 80, 81; 80, 82; 81, identifier:BaseException; 82, block; 82, 83; 82, 98; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:query_dict; 87, identifier:update; 88, argument_list; 88, 89; 89, dictionary; 89, 90; 90, pair; 90, 91; 90, 92; 91, identifier:key; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, string:'{0}'; 95, identifier:format; 96, argument_list; 96, 97; 97, identifier:value; 98, comment; 99, if_statement; 99, 100; 99, 105; 100, boolean_operator:and; 100, 101; 100, 102; 101, identifier:write_db; 102, comparison_operator:>; 102, 103; 102, 104; 103, identifier:n_results; 104, integer:1000; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:print; 109, argument_list; 109, 110; 110, comment; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:data; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:query; 117, identifier:get_reactions; 118, argument_list; 118, 119; 118, 122; 118, 125; 118, 128; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:columns; 121, identifier:columns; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:n_results; 124, identifier:n_results; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:write_db; 127, identifier:write_db; 128, dictionary_splat; 128, 129; 129, identifier:query_dict; 130, if_statement; 130, 131; 130, 132; 131, identifier:write_db; 132, block; 132, 133; 133, return_statement; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:table; 137, list:[]; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:headers; 141, list:[]; 142, for_statement; 142, 143; 142, 144; 142, 149; 143, identifier:row; 144, subscript; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:data; 147, string:'reactions'; 148, string:'edges'; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, augmented_assignment:+=; 151, 152; 151, 153; 152, identifier:table; 153, list:[list(row['node'].values())]; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:list; 156, argument_list; 156, 157; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:row; 161, string:'node'; 162, identifier:values; 163, argument_list; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:headers; 167, call; 167, 168; 167, 169; 168, identifier:list; 169, argument_list; 169, 170; 170, call; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, subscript; 172, 173; 172, 174; 173, identifier:row; 174, string:'node'; 175, identifier:keys; 176, argument_list; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:print; 180, argument_list; 180, 181; 181, binary_operator:+; 181, 182; 181, 187; 182, call; 182, 183; 182, 184; 183, identifier:tabulate; 184, argument_list; 184, 185; 184, 186; 185, identifier:table; 186, identifier:headers; 187, string:'\n' | def reactions(columns, n_results, write_db, queries):
"""Search for reactions"""
if not isinstance(queries, dict):
query_dict = {}
for q in queries:
key, value = q.split('=')
if key == 'distinct':
if value in ['True', 'true']:
query_dict.update({key: True})
continue
try:
value = int(value)
query_dict.update({key: value})
except BaseException:
query_dict.update({key: '{0}'.format(value)})
# Keep {0} in string.format for python2.6 compatibility
if write_db and n_results > 1000:
print("""Warning: You're attempting to write more than a 1000 rows
with geometries. This could take some time""")
data = query.get_reactions(columns=columns,
n_results=n_results,
write_db=write_db,
**query_dict)
if write_db:
return
table = []
headers = []
for row in data['reactions']['edges']:
table += [list(row['node'].values())]
headers = list(row['node'].keys())
print(tabulate(table, headers) + '\n') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:publications; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:columns; 5, identifier:n_results; 6, identifier:queries; 7, block; 7, 8; 7, 10; 7, 97; 7, 112; 7, 132; 7, 136; 7, 140; 7, 250; 7, 263; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 17; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 16; 15, identifier:queries; 16, identifier:dict; 17, block; 17, 18; 17, 22; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:query_dict; 21, dictionary; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:q; 24, identifier:queries; 25, block; 25, 26; 25, 37; 25, 60; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 31; 28, pattern_list; 28, 29; 28, 30; 29, identifier:key; 30, identifier:value; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:q; 34, identifier:split; 35, argument_list; 35, 36; 36, string:'='; 37, if_statement; 37, 38; 37, 41; 38, comparison_operator:==; 38, 39; 38, 40; 39, identifier:key; 40, string:'distinct'; 41, block; 41, 42; 42, if_statement; 42, 43; 42, 48; 43, comparison_operator:in; 43, 44; 43, 45; 44, identifier:value; 45, list:['True', 'true']; 45, 46; 45, 47; 46, string:'True'; 47, string:'true'; 48, block; 48, 49; 48, 59; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:query_dict; 53, identifier:update; 54, argument_list; 54, 55; 55, dictionary; 55, 56; 56, pair; 56, 57; 56, 58; 57, identifier:key; 58, True; 59, continue_statement; 60, try_statement; 60, 61; 60, 79; 61, block; 61, 62; 61, 69; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:value; 65, call; 65, 66; 65, 67; 66, identifier:int; 67, argument_list; 67, 68; 68, identifier:value; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:query_dict; 73, identifier:update; 74, argument_list; 74, 75; 75, dictionary; 75, 76; 76, pair; 76, 77; 76, 78; 77, identifier:key; 78, identifier:value; 79, except_clause; 79, 80; 79, 81; 80, identifier:BaseException; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:query_dict; 86, identifier:update; 87, argument_list; 87, 88; 88, dictionary; 88, 89; 89, pair; 89, 90; 89, 91; 90, identifier:key; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, string:'{0}'; 94, identifier:format; 95, argument_list; 95, 96; 96, identifier:value; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:not; 98, 99; 98, 100; 99, string:'sort'; 100, identifier:query_dict; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:query_dict; 106, identifier:update; 107, argument_list; 107, 108; 108, dictionary; 108, 109; 109, pair; 109, 110; 109, 111; 110, string:'order'; 111, string:'-year'; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:data; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:query; 118, identifier:query; 119, argument_list; 119, 120; 119, 123; 119, 126; 119, 129; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:table; 122, string:'publications'; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:columns; 125, identifier:columns; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:n_results; 128, identifier:n_results; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:queries; 131, identifier:query_dict; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:table; 135, list:[]; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:headers; 139, list:[]; 140, for_statement; 140, 141; 140, 142; 140, 147; 141, identifier:row; 142, subscript; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:data; 145, string:'publications'; 146, string:'edges'; 147, block; 147, 148; 147, 161; 147, 245; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:value; 151, call; 151, 152; 151, 153; 152, identifier:list; 153, argument_list; 153, 154; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:row; 158, string:'node'; 159, identifier:values; 160, argument_list; 161, for_statement; 161, 162; 161, 165; 161, 169; 162, pattern_list; 162, 163; 162, 164; 163, identifier:n; 164, identifier:v; 165, call; 165, 166; 165, 167; 166, identifier:enumerate; 167, argument_list; 167, 168; 168, identifier:value; 169, block; 169, 170; 170, if_statement; 170, 171; 170, 183; 171, boolean_operator:and; 171, 172; 171, 177; 172, call; 172, 173; 172, 174; 173, identifier:isinstance; 174, argument_list; 174, 175; 174, 176; 175, identifier:v; 176, identifier:str; 177, comparison_operator:>; 177, 178; 177, 182; 178, call; 178, 179; 178, 180; 179, identifier:len; 180, argument_list; 180, 181; 181, identifier:v; 182, integer:20; 183, block; 183, 184; 183, 193; 183, 197; 183, 201; 183, 235; 183, 239; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:splited; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:v; 190, identifier:split; 191, argument_list; 191, 192; 192, string:' '; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:size; 196, integer:0; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:sentence; 200, string:''; 201, for_statement; 201, 202; 201, 203; 201, 204; 202, identifier:word; 203, identifier:splited; 204, block; 204, 205; 205, if_statement; 205, 206; 205, 209; 205, 223; 206, comparison_operator:<; 206, 207; 206, 208; 207, identifier:size; 208, integer:20; 209, block; 209, 210; 209, 217; 210, expression_statement; 210, 211; 211, augmented_assignment:+=; 211, 212; 211, 213; 212, identifier:size; 213, call; 213, 214; 213, 215; 214, identifier:len; 215, argument_list; 215, 216; 216, identifier:word; 217, expression_statement; 217, 218; 218, augmented_assignment:+=; 218, 219; 218, 220; 219, identifier:sentence; 220, binary_operator:+; 220, 221; 220, 222; 221, string:' '; 222, identifier:word; 223, else_clause; 223, 224; 224, block; 224, 225; 224, 231; 225, expression_statement; 225, 226; 226, augmented_assignment:+=; 226, 227; 226, 228; 227, identifier:sentence; 228, binary_operator:+; 228, 229; 228, 230; 229, string:'\n'; 230, identifier:word; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:size; 234, integer:0; 235, expression_statement; 235, 236; 236, augmented_assignment:+=; 236, 237; 236, 238; 237, identifier:sentence; 238, string:'\n'; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:value; 243, identifier:n; 244, identifier:sentence; 245, expression_statement; 245, 246; 246, augmented_assignment:+=; 246, 247; 246, 248; 247, identifier:table; 248, list:[value]; 248, 249; 249, identifier:value; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:headers; 253, call; 253, 254; 253, 255; 254, identifier:list; 255, argument_list; 255, 256; 256, call; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, subscript; 258, 259; 258, 260; 259, identifier:row; 260, string:'node'; 261, identifier:keys; 262, argument_list; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:print; 266, argument_list; 266, 267; 267, binary_operator:+; 267, 268; 267, 276; 268, call; 268, 269; 268, 270; 269, identifier:tabulate; 270, argument_list; 270, 271; 270, 272; 270, 273; 271, identifier:table; 272, identifier:headers; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:tablefmt; 275, string:"grid"; 276, string:'\n' | def publications(columns, n_results, queries):
"""Search for publications"""
if not isinstance(queries, dict):
query_dict = {}
for q in queries:
key, value = q.split('=')
if key == 'distinct':
if value in ['True', 'true']:
query_dict.update({key: True})
continue
try:
value = int(value)
query_dict.update({key: value})
except BaseException:
query_dict.update({key: '{0}'.format(value)})
if 'sort' not in query_dict:
query_dict.update({'order': '-year'})
data = query.query(table='publications',
columns=columns,
n_results=n_results,
queries=query_dict)
table = []
headers = []
for row in data['publications']['edges']:
value = list(row['node'].values())
for n, v in enumerate(value):
if isinstance(v, str) and len(v) > 20:
splited = v.split(' ')
size = 0
sentence = ''
for word in splited:
if size < 20:
size += len(word)
sentence += ' ' + word
else:
sentence += '\n' + word
size = 0
sentence += '\n'
value[n] = sentence
table += [value]
headers = list(row['node'].keys())
print(tabulate(table, headers, tablefmt="grid") + '\n') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:make_folders; 3, parameters; 3, 4; 3, 5; 4, identifier:template; 5, identifier:custom_base; 6, block; 6, 7; 6, 9; 6, 26; 6, 36; 6, 56; 6, 64; 6, 70; 6, 131; 6, 253; 6, 337; 6, 348; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 14; 10, function_name:dict_representer; 11, parameters; 11, 12; 11, 13; 12, identifier:dumper; 13, identifier:data; 14, block; 14, 15; 15, return_statement; 15, 16; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:dumper; 19, identifier:represent_dict; 20, argument_list; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:data; 24, identifier:items; 25, argument_list; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:Dumper; 30, identifier:add_representer; 31, argument_list; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:collections; 34, identifier:OrderedDict; 35, identifier:dict_representer; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:custom_base; 39, None; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:custom_base; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:os; 48, identifier:path; 49, identifier:abspath; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:os; 54, identifier:path; 55, identifier:curdir; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:template; 59, binary_operator:+; 59, 60; 59, 63; 60, binary_operator:+; 60, 61; 60, 62; 61, identifier:custom_base; 62, string:'/'; 63, identifier:template; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:template_data; 67, attribute; 67, 68; 67, 69; 68, identifier:ase_tools; 69, identifier:REACTION_TEMPLATE; 70, if_statement; 70, 71; 70, 80; 71, not_operator; 71, 72; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:os; 76, identifier:path; 77, identifier:exists; 78, argument_list; 78, 79; 79, identifier:template; 80, block; 80, 81; 81, with_statement; 81, 82; 81, 92; 82, with_clause; 82, 83; 83, with_item; 83, 84; 84, as_pattern; 84, 85; 84, 90; 85, call; 85, 86; 85, 87; 86, identifier:open; 87, argument_list; 87, 88; 87, 89; 88, identifier:template; 89, string:'w'; 90, as_pattern_target; 90, 91; 91, identifier:outfile; 92, block; 92, 93; 92, 113; 92, 130; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:outfile; 97, identifier:write; 98, argument_list; 98, 99; 99, binary_operator:+; 99, 100; 99, 112; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:yaml; 103, identifier:dump; 104, argument_list; 104, 105; 104, 106; 104, 109; 105, identifier:template_data; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:indent; 108, integer:4; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:Dumper; 111, identifier:Dumper; 112, string:'\n'; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:print; 116, argument_list; 116, 117; 117, binary_operator:+; 117, 118; 117, 129; 118, binary_operator:+; 118, 119; 118, 128; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, string:"Created template file: {template}\n"; 122, identifier:format; 123, argument_list; 123, 124; 124, dictionary_splat; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:locals; 127, argument_list; 128, string:' Please edit it and run the script again to create your folderstructure.\n'; 129, string:' Run cathub make_folders --help for instructions'; 130, return_statement; 131, with_statement; 131, 132; 131, 141; 132, with_clause; 132, 133; 133, with_item; 133, 134; 134, as_pattern; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:open; 137, argument_list; 137, 138; 138, identifier:template; 139, as_pattern_target; 139, 140; 140, identifier:infile; 141, block; 141, 142; 141, 151; 141, 157; 141, 163; 141, 169; 141, 175; 141, 181; 141, 187; 141, 193; 141, 199; 141, 205; 141, 211; 141, 217; 141, 223; 141, 229; 141, 235; 141, 241; 141, 247; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:template_data; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:yaml; 148, identifier:load; 149, argument_list; 149, 150; 150, identifier:infile; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:title; 154, subscript; 154, 155; 154, 156; 155, identifier:template_data; 156, string:'title'; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:authors; 160, subscript; 160, 161; 160, 162; 161, identifier:template_data; 162, string:'authors'; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:journal; 166, subscript; 166, 167; 166, 168; 167, identifier:template_data; 168, string:'journal'; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:volume; 172, subscript; 172, 173; 172, 174; 173, identifier:template_data; 174, string:'volume'; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:number; 178, subscript; 178, 179; 178, 180; 179, identifier:template_data; 180, string:'number'; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:pages; 184, subscript; 184, 185; 184, 186; 185, identifier:template_data; 186, string:'pages'; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:year; 190, subscript; 190, 191; 190, 192; 191, identifier:template_data; 192, string:'year'; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:email; 196, subscript; 196, 197; 196, 198; 197, identifier:template_data; 198, string:'email'; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:publisher; 202, subscript; 202, 203; 202, 204; 203, identifier:template_data; 204, string:'publisher'; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:doi; 208, subscript; 208, 209; 208, 210; 209, identifier:template_data; 210, string:'doi'; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:dft_code; 214, subscript; 214, 215; 214, 216; 215, identifier:template_data; 216, string:'DFT_code'; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:dft_functionals; 220, subscript; 220, 221; 220, 222; 221, identifier:template_data; 222, string:'DFT_functionals'; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:reactions; 226, subscript; 226, 227; 226, 228; 227, identifier:template_data; 228, string:'reactions'; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:crystal_structures; 232, subscript; 232, 233; 232, 234; 233, identifier:template_data; 234, string:'crystal_structures'; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:bulk_compositions; 238, subscript; 238, 239; 238, 240; 239, identifier:template_data; 240, string:'bulk_compositions'; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:facets; 244, subscript; 244, 245; 244, 246; 245, identifier:template_data; 246, string:'facets'; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:energy_corrections; 250, subscript; 250, 251; 250, 252; 251, identifier:template_data; 252, string:'energy_corrections'; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:make_folders_template; 257, identifier:main; 258, argument_list; 258, 259; 258, 262; 258, 277; 258, 280; 258, 283; 258, 286; 258, 289; 258, 292; 258, 295; 258, 298; 258, 301; 258, 304; 258, 307; 258, 322; 258, 325; 258, 328; 258, 331; 258, 334; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:title; 261, identifier:title; 262, keyword_argument; 262, 263; 262, 264; 263, identifier:authors; 264, conditional_expression:if; 264, 265; 264, 269; 264, 276; 265, call; 265, 266; 265, 267; 266, identifier:eval; 267, argument_list; 267, 268; 268, identifier:authors; 269, call; 269, 270; 269, 271; 270, identifier:isinstance; 271, argument_list; 271, 272; 271, 273; 272, identifier:authors; 273, attribute; 273, 274; 273, 275; 274, identifier:six; 275, identifier:string_types; 276, identifier:authors; 277, keyword_argument; 277, 278; 277, 279; 278, identifier:journal; 279, identifier:journal; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:volume; 282, identifier:volume; 283, keyword_argument; 283, 284; 283, 285; 284, identifier:number; 285, identifier:number; 286, keyword_argument; 286, 287; 286, 288; 287, identifier:pages; 288, identifier:pages; 289, keyword_argument; 289, 290; 289, 291; 290, identifier:year; 291, identifier:year; 292, keyword_argument; 292, 293; 292, 294; 293, identifier:email; 294, identifier:email; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:publisher; 297, identifier:publisher; 298, keyword_argument; 298, 299; 298, 300; 299, identifier:doi; 300, identifier:doi; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:DFT_code; 303, identifier:dft_code; 304, keyword_argument; 304, 305; 304, 306; 305, identifier:DFT_functionals; 306, identifier:dft_functionals; 307, keyword_argument; 307, 308; 307, 309; 308, identifier:reactions; 309, conditional_expression:if; 309, 310; 309, 314; 309, 321; 310, call; 310, 311; 310, 312; 311, identifier:eval; 312, argument_list; 312, 313; 313, identifier:reactions; 314, call; 314, 315; 314, 316; 315, identifier:isinstance; 316, argument_list; 316, 317; 316, 318; 317, identifier:reactions; 318, attribute; 318, 319; 318, 320; 319, identifier:six; 320, identifier:string_types; 321, identifier:reactions; 322, keyword_argument; 322, 323; 322, 324; 323, identifier:custom_base; 324, identifier:custom_base; 325, keyword_argument; 325, 326; 325, 327; 326, identifier:bulk_compositions; 327, identifier:bulk_compositions; 328, keyword_argument; 328, 329; 328, 330; 329, identifier:crystal_structures; 330, identifier:crystal_structures; 331, keyword_argument; 331, 332; 331, 333; 332, identifier:facets; 333, identifier:facets; 334, keyword_argument; 334, 335; 334, 336; 335, identifier:energy_corrections; 336, identifier:energy_corrections; 337, expression_statement; 337, 338; 338, assignment; 338, 339; 338, 340; 339, identifier:pub_id; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:tools; 343, identifier:get_pub_id; 344, argument_list; 344, 345; 344, 346; 344, 347; 345, identifier:title; 346, identifier:authors; 347, identifier:year; 348, expression_statement; 348, 349; 349, call; 349, 350; 349, 351; 350, identifier:print; 351, argument_list; 351, 352; 352, call; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, string:"Now dump your DFT output files into the folder, and run 'cathub folder2db {pub_id}'"; 355, identifier:format; 356, argument_list; 356, 357; 357, dictionary_splat; 357, 358; 358, call; 358, 359; 358, 360; 359, identifier:locals; 360, argument_list | def make_folders(template, custom_base):
"""Create a basic folder tree for dumping DFT calculcations for reaction energies.
Dear all
Use this command make the right structure for your folders
for submitting data for Catalysis Hub's Surface Reactions.
Start by creating a template file by calling:
$ cathub make_folders <template_name>
Then open the template and modify it to so that it contains information
about your data. You will need to enter publication/dataset information,
and specify the types of surfaces, facets and reactions.
The 'reactions' entry should include two lists for each reaction;
'reactants' and 'products', corresponding to left- and right hand side of
each chemical equation respectively.
Remember to balance the equation by including a prefactor or minus sign
in the name when relevant. For example:
reactions:
- reactants: ['CCH3star@ontop']
products: ['Cstar@hollow', 'CH3star@ontop']
- reactants: ['CH4gas', '-0.5H2gas', 'star']
products: ['CH3star']
Please include the phase of the species as an extension:
'gas' for gas phase (i.e. CH4 -> CH4gas)
'star' for empty slab or adsorbed phase. (i.e. OH -> OHstar)
The site of adsorbed species is also included as an extension:
'@site' (i.e. OHstar in bridge-> OHstar@bridge)
Energy corrections to gas phase molecules can be included as:
energy_corrections: {H2: 0.1, CH4: -0.15}
Then, save the template and call:
$ cathub make_folders <template_name>
And folders will be created automatically.
You can create several templates and call make_folders again
if you, for example, are using different functionals or are
doing different reactions on different surfaces.
After creating your folders, add your output files from the
electronic structure calculations at the positions.
Accepted file formats include everything that can be read by ASE
and contains the total potential energy of the calculation, such
as .traj or .OUTCAR files.
After dumping your files, run `cathub folder2db <your folder>`
to collect the data.
"""
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
Dumper.add_representer(collections.OrderedDict, dict_representer)
if custom_base is None:
custom_base = os.path.abspath(os.path.curdir)
template = custom_base + '/' + template
template_data = ase_tools.REACTION_TEMPLATE
if not os.path.exists(template):
with open(template, 'w') as outfile:
outfile.write(
yaml.dump(
template_data,
indent=4,
Dumper=Dumper) +
'\n')
print("Created template file: {template}\n".format(**locals()) +
' Please edit it and run the script again to create your folderstructure.\n' +
' Run cathub make_folders --help for instructions')
return
with open(template) as infile:
template_data = yaml.load(infile)
title = template_data['title']
authors = template_data['authors']
journal = template_data['journal']
volume = template_data['volume']
number = template_data['number']
pages = template_data['pages']
year = template_data['year']
email = template_data['email']
publisher = template_data['publisher']
doi = template_data['doi']
dft_code = template_data['DFT_code']
dft_functionals = template_data['DFT_functionals']
reactions = template_data['reactions']
crystal_structures = template_data['crystal_structures']
bulk_compositions = template_data['bulk_compositions']
facets = template_data['facets']
energy_corrections = template_data['energy_corrections']
make_folders_template.main(
title=title,
authors=eval(authors) if isinstance(
authors, six.string_types) else authors,
journal=journal,
volume=volume,
number=number,
pages=pages,
year=year,
email=email,
publisher=publisher,
doi=doi,
DFT_code=dft_code,
DFT_functionals=dft_functionals,
reactions=eval(reactions) if isinstance(
reactions, six.string_types) else reactions,
custom_base=custom_base,
bulk_compositions=bulk_compositions,
crystal_structures=crystal_structures,
facets=facets,
energy_corrections=energy_corrections
)
pub_id = tools.get_pub_id(title, authors, year)
print(
"Now dump your DFT output files into the folder, and run 'cathub folder2db {pub_id}'".format(**locals())) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:opensignals_hierarchy; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:root; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:update; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:clone; 12, False; 13, block; 13, 14; 13, 16; 13, 29; 13, 40; 13, 41; 13, 47; 13, 65; 13, 66; 13, 160; 13, 161; 13, 167; 13, 185; 13, 215; 13, 438; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:root; 19, None; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:root; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:os; 27, identifier:getcwd; 28, argument_list; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:categories; 32, call; 32, 33; 32, 34; 33, identifier:list; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:NOTEBOOK_KEYS; 38, identifier:keys; 39, argument_list; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:current_dir; 44, binary_operator:+; 44, 45; 44, 46; 45, identifier:root; 46, string:"\\opensignalstools_environment"; 47, if_statement; 47, 48; 47, 57; 48, not_operator; 48, 49; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:os; 53, identifier:path; 54, identifier:isdir; 55, argument_list; 55, 56; 56, identifier:current_dir; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:os; 62, identifier:makedirs; 63, argument_list; 63, 64; 64, identifier:current_dir; 65, comment; 66, for_statement; 66, 67; 66, 68; 66, 72; 67, identifier:var; 68, list:["images", "styles", "signal_samples"]; 68, 69; 68, 70; 68, 71; 69, string:"images"; 70, string:"styles"; 71, string:"signal_samples"; 72, block; 72, 73; 73, if_statement; 73, 74; 73, 87; 73, 116; 74, not_operator; 74, 75; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:os; 79, identifier:path; 80, identifier:isdir; 81, argument_list; 81, 82; 82, binary_operator:+; 82, 83; 82, 86; 83, binary_operator:+; 83, 84; 83, 85; 84, identifier:root; 85, string:"\\opensignalstools_environment\\"; 86, identifier:var; 87, block; 87, 88; 87, 100; 87, 108; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:src; 91, binary_operator:+; 91, 92; 91, 99; 92, binary_operator:+; 92, 93; 92, 98; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:os; 96, identifier:getcwd; 97, argument_list; 98, string:"\\"; 99, identifier:var; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:destination; 103, binary_operator:+; 103, 104; 103, 107; 104, binary_operator:+; 104, 105; 104, 106; 105, identifier:current_dir; 106, string:"\\"; 107, identifier:var; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:shutil; 112, identifier:copytree; 113, argument_list; 113, 114; 113, 115; 114, identifier:src; 115, identifier:destination; 116, elif_clause; 116, 117; 116, 120; 117, comparison_operator:is; 117, 118; 117, 119; 118, identifier:update; 119, True; 120, block; 120, 121; 120, 132; 120, 144; 120, 152; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:shutil; 125, identifier:rmtree; 126, argument_list; 126, 127; 127, binary_operator:+; 127, 128; 127, 131; 128, binary_operator:+; 128, 129; 128, 130; 129, identifier:root; 130, string:"\\opensignalstools_environment\\"; 131, identifier:var; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:src; 135, binary_operator:+; 135, 136; 135, 143; 136, binary_operator:+; 136, 137; 136, 142; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:os; 140, identifier:getcwd; 141, argument_list; 142, string:"\\"; 143, identifier:var; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:destination; 147, binary_operator:+; 147, 148; 147, 151; 148, binary_operator:+; 148, 149; 148, 150; 149, identifier:current_dir; 150, string:"\\"; 151, identifier:var; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:shutil; 156, identifier:copytree; 157, argument_list; 157, 158; 157, 159; 158, identifier:src; 159, identifier:destination; 160, comment; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:current_dir; 164, binary_operator:+; 164, 165; 164, 166; 165, identifier:root; 166, string:"\\opensignalstools_environment\\Categories"; 167, if_statement; 167, 168; 167, 177; 168, not_operator; 168, 169; 169, call; 169, 170; 169, 175; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:os; 173, identifier:path; 174, identifier:isdir; 175, argument_list; 175, 176; 176, identifier:current_dir; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:os; 182, identifier:makedirs; 183, argument_list; 183, 184; 184, identifier:current_dir; 185, for_statement; 185, 186; 185, 187; 185, 188; 186, identifier:category; 187, identifier:categories; 188, block; 188, 189; 189, if_statement; 189, 190; 189, 203; 190, not_operator; 190, 191; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:os; 195, identifier:path; 196, identifier:isdir; 197, argument_list; 197, 198; 198, binary_operator:+; 198, 199; 198, 202; 199, binary_operator:+; 199, 200; 199, 201; 200, identifier:current_dir; 201, string:"\\"; 202, identifier:category; 203, block; 203, 204; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:os; 208, identifier:makedirs; 209, argument_list; 209, 210; 210, binary_operator:+; 210, 211; 210, 214; 211, binary_operator:+; 211, 212; 211, 213; 212, identifier:current_dir; 213, string:"\\"; 214, identifier:category; 215, if_statement; 215, 216; 215, 219; 215, 220; 215, 221; 216, comparison_operator:is; 216, 217; 216, 218; 217, identifier:clone; 218, True; 219, comment; 220, comment; 221, block; 221, 222; 221, 228; 221, 237; 221, 321; 221, 322; 221, 329; 221, 342; 221, 353; 221, 366; 221, 377; 221, 390; 221, 401; 221, 414; 221, 425; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:dir_path; 225, binary_operator:+; 225, 226; 225, 227; 226, identifier:root; 227, string:"\\notebook_code"; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:list_of_code_dirs; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:os; 234, identifier:listdir; 235, argument_list; 235, 236; 236, identifier:dir_path; 237, for_statement; 237, 238; 237, 239; 237, 240; 238, identifier:folder; 239, identifier:list_of_code_dirs; 240, block; 240, 241; 240, 249; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:folder_path; 244, binary_operator:+; 244, 245; 244, 248; 245, binary_operator:+; 245, 246; 245, 247; 246, identifier:root; 247, string:"\\notebook_code\\"; 248, identifier:folder; 249, if_statement; 249, 250; 249, 257; 250, boolean_operator:and; 250, 251; 250, 254; 251, comparison_operator:!=; 251, 252; 251, 253; 252, identifier:folder; 253, string:"MainFiles"; 254, comparison_operator:!=; 254, 255; 254, 256; 255, identifier:folder; 256, string:"__pycache__"; 257, block; 257, 258; 257, 267; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:list_of_code_files; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:os; 264, identifier:listdir; 265, argument_list; 265, 266; 266, identifier:folder_path; 267, for_statement; 267, 268; 267, 269; 267, 270; 268, identifier:file; 269, identifier:list_of_code_files; 270, block; 270, 271; 271, if_statement; 271, 272; 271, 275; 272, comparison_operator:!=; 272, 273; 272, 274; 273, identifier:file; 274, string:"__pycache__"; 275, block; 275, 276; 275, 292; 275, 303; 275, 312; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:spec; 279, call; 279, 280; 279, 285; 280, attribute; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:importlib; 283, identifier:util; 284, identifier:spec_from_file_location; 285, argument_list; 285, 286; 285, 287; 286, identifier:file; 287, binary_operator:+; 287, 288; 287, 291; 288, binary_operator:+; 288, 289; 288, 290; 289, identifier:folder_path; 290, string:"\\"; 291, identifier:file; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:foo; 295, call; 295, 296; 295, 301; 296, attribute; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:importlib; 299, identifier:util; 300, identifier:module_from_spec; 301, argument_list; 301, 302; 302, identifier:spec; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 310; 305, attribute; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:spec; 308, identifier:loader; 309, identifier:exec_module; 310, argument_list; 310, 311; 311, identifier:foo; 312, expression_statement; 312, 313; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:foo; 316, identifier:run; 317, argument_list; 317, 318; 318, binary_operator:+; 318, 319; 318, 320; 319, identifier:root; 320, string:"\\opensignalstools_environment"; 321, comment; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 325; 324, identifier:main_page; 325, call; 325, 326; 325, 327; 326, identifier:notebook; 327, argument_list; 327, 328; 328, string:"Main_Files_By_Category"; 329, expression_statement; 329, 330; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:main_page; 333, identifier:write_to_file; 334, argument_list; 334, 335; 334, 338; 334, 339; 335, binary_operator:+; 335, 336; 335, 337; 336, identifier:root; 337, string:"\\opensignalstools_environment"; 338, string:"opensignalstools"; 339, keyword_argument; 339, 340; 339, 341; 340, identifier:footer; 341, False; 342, expression_statement; 342, 343; 343, assignment; 343, 344; 343, 345; 344, identifier:by_difficulty; 345, call; 345, 346; 345, 347; 346, identifier:notebook; 347, argument_list; 347, 348; 347, 349; 347, 350; 348, string:"Main_Files_By_Difficulty"; 349, string:"Notebooks Grouped by Difficulty"; 350, keyword_argument; 350, 351; 350, 352; 351, identifier:notebook_description; 352, identifier:DESCRIPTION_GROUP_BY; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:by_difficulty; 357, identifier:write_to_file; 358, argument_list; 358, 359; 358, 362; 358, 363; 359, binary_operator:+; 359, 360; 359, 361; 360, identifier:root; 361, string:"\\opensignalstools_environment"; 362, string:"by_diff"; 363, keyword_argument; 363, 364; 363, 365; 364, identifier:footer; 365, False; 366, expression_statement; 366, 367; 367, assignment; 367, 368; 367, 369; 368, identifier:by_tags; 369, call; 369, 370; 369, 371; 370, identifier:notebook; 371, argument_list; 371, 372; 371, 373; 371, 374; 372, string:"Main_Files_By_Tag"; 373, string:"Notebooks Grouped by Tag Values"; 374, keyword_argument; 374, 375; 374, 376; 375, identifier:notebook_description; 376, identifier:DESCRIPTION_GROUP_BY; 377, expression_statement; 377, 378; 378, call; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:by_tags; 381, identifier:write_to_file; 382, argument_list; 382, 383; 382, 386; 382, 387; 383, binary_operator:+; 383, 384; 383, 385; 384, identifier:root; 385, string:"\\opensignalstools_environment"; 386, string:"by_tag"; 387, keyword_argument; 387, 388; 387, 389; 388, identifier:footer; 389, False; 390, expression_statement; 390, 391; 391, assignment; 391, 392; 391, 393; 392, identifier:by_signal_type; 393, call; 393, 394; 393, 395; 394, identifier:notebook; 395, argument_list; 395, 396; 395, 397; 395, 398; 396, string:"Main_Files_By_Signal_Type"; 397, string:"Notebooks Grouped by Signal Type"; 398, keyword_argument; 398, 399; 398, 400; 399, identifier:notebook_description; 400, identifier:DESCRIPTION_GROUP_BY; 401, expression_statement; 401, 402; 402, call; 402, 403; 402, 406; 403, attribute; 403, 404; 403, 405; 404, identifier:by_signal_type; 405, identifier:write_to_file; 406, argument_list; 406, 407; 406, 410; 406, 411; 407, binary_operator:+; 407, 408; 407, 409; 408, identifier:root; 409, string:"\\opensignalstools_environment"; 410, string:"by_signal_type"; 411, keyword_argument; 411, 412; 411, 413; 412, identifier:footer; 413, False; 414, expression_statement; 414, 415; 415, assignment; 415, 416; 415, 417; 416, identifier:signal_samples; 417, call; 417, 418; 417, 419; 418, identifier:notebook; 419, argument_list; 419, 420; 419, 421; 419, 422; 420, string:"Main_Files_Signal_Samples"; 421, string:"Signal Samples Library"; 422, keyword_argument; 422, 423; 422, 424; 423, identifier:notebook_description; 424, identifier:DESCRIPTION_SIGNAL_SAMPLES; 425, expression_statement; 425, 426; 426, call; 426, 427; 426, 430; 427, attribute; 427, 428; 427, 429; 428, identifier:signal_samples; 429, identifier:write_to_file; 430, argument_list; 430, 431; 430, 434; 430, 435; 431, binary_operator:+; 431, 432; 431, 433; 432, identifier:root; 433, string:"\\opensignalstools_environment"; 434, string:"signal_samples"; 435, keyword_argument; 435, 436; 435, 437; 436, identifier:footer; 437, False; 438, return_statement; 438, 439; 439, binary_operator:+; 439, 440; 439, 441; 440, identifier:root; 441, string:"\\opensignalstools_environment" | def opensignals_hierarchy(root=None, update=False, clone=False):
"""
Function that generates the OpenSignalsTools Notebooks File Hierarchy programatically.
----------
Parameters
----------
root : None or str
The file path where the OpenSignalsTools Environment will be stored.
update : bool
If True the old files will be replaced by the new ones.
clone : bool
If True then all the available Notebooks will be stored in the users computer.
If False only the folder hierarchy of OpenSignalsTools will be generated, giving to the
user a blank template for creating his own Notebook Environment.
Returns
-------
out : str
The root file path of OpenSignalsTools Environment is returned.
"""
if root is None:
root = os.getcwd()
categories = list(NOTEBOOK_KEYS.keys())
# ============================ Creation of the main directory ==================================
current_dir = root + "\\opensignalstools_environment"
if not os.path.isdir(current_dir):
os.makedirs(current_dir)
# ================== Copy of 'images' 'styles' and 'signal_samples' folders ====================
for var in ["images", "styles", "signal_samples"]:
if not os.path.isdir(root + "\\opensignalstools_environment\\" + var):
src = os.getcwd() + "\\" + var
destination = current_dir + "\\" + var
shutil.copytree(src, destination)
elif update is True:
shutil.rmtree(root + "\\opensignalstools_environment\\" + var)
src = os.getcwd() + "\\" + var
destination = current_dir + "\\" + var
shutil.copytree(src, destination)
# =========================== Generation of 'Categories' folder ================================
current_dir = root + "\\opensignalstools_environment\\Categories"
if not os.path.isdir(current_dir):
os.makedirs(current_dir)
for category in categories:
if not os.path.isdir(current_dir + "\\" + category):
os.makedirs(current_dir + "\\" + category)
if clone is True:
# Fill each folder inside "Categories" directory with the respective notebooks.
# Each notebook will be created by a specific function.
dir_path = root + "\\notebook_code"
list_of_code_dirs = os.listdir(dir_path)
for folder in list_of_code_dirs:
folder_path = root + "\\notebook_code\\" + folder
if folder != "MainFiles" and folder != "__pycache__":
list_of_code_files = os.listdir(folder_path)
for file in list_of_code_files:
if file != "__pycache__":
spec = importlib.util.spec_from_file_location(file, folder_path +
"\\" + file)
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
foo.run(root + "\\opensignalstools_environment")
# Generation of opensignalstools environment main files.
main_page = notebook("Main_Files_By_Category")
main_page.write_to_file(root + "\\opensignalstools_environment", "opensignalstools",
footer=False)
by_difficulty = notebook("Main_Files_By_Difficulty", "Notebooks Grouped by Difficulty", notebook_description=DESCRIPTION_GROUP_BY)
by_difficulty.write_to_file(root + "\\opensignalstools_environment", "by_diff",
footer=False)
by_tags = notebook("Main_Files_By_Tag", "Notebooks Grouped by Tag Values",
notebook_description=DESCRIPTION_GROUP_BY)
by_tags.write_to_file(root + "\\opensignalstools_environment", "by_tag",
footer=False)
by_signal_type = notebook("Main_Files_By_Signal_Type", "Notebooks Grouped by Signal Type",
notebook_description=DESCRIPTION_GROUP_BY)
by_signal_type.write_to_file(root + "\\opensignalstools_environment",
"by_signal_type", footer=False)
signal_samples = notebook("Main_Files_Signal_Samples", "Signal Samples Library",
notebook_description=DESCRIPTION_SIGNAL_SAMPLES)
signal_samples.write_to_file(root + "\\opensignalstools_environment",
"signal_samples", footer=False)
return root + "\\opensignalstools_environment" |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_generate_notebook_by_tag_body; 3, parameters; 3, 4; 3, 5; 4, identifier:notebook_object; 5, identifier:dict_by_tag; 6, block; 6, 7; 6, 9; 6, 20; 6, 26; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:tag_keys; 12, call; 12, 13; 12, 14; 13, identifier:list; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:dict_by_tag; 18, identifier:keys; 19, argument_list; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:tag_keys; 24, identifier:sort; 25, argument_list; 26, for_statement; 26, 27; 26, 28; 26, 29; 27, identifier:tag; 28, identifier:tag_keys; 29, block; 29, 30; 30, if_statement; 30, 31; 30, 38; 31, comparison_operator:not; 31, 32; 31, 37; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:tag; 35, identifier:lower; 36, argument_list; 37, identifier:SIGNAL_TYPE_LIST; 38, block; 38, 39; 38, 45; 38, 55; 38, 156; 38, 160; 38, 161; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:markdown_cell; 42, attribute; 42, 43; 42, 44; 43, identifier:group_tag_code; 44, identifier:TAG_TABLE_HEADER; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:markdown_cell; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:markdown_cell; 51, identifier:replace; 52, argument_list; 52, 53; 52, 54; 53, string:"Tag i"; 54, identifier:tag; 55, for_statement; 55, 56; 55, 57; 55, 60; 56, identifier:notebook_file; 57, subscript; 57, 58; 57, 59; 58, identifier:dict_by_tag; 59, identifier:tag; 60, block; 60, 61; 60, 70; 60, 77; 60, 91; 60, 105; 60, 129; 60, 138; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:split_path; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:notebook_file; 67, identifier:split; 68, argument_list; 68, 69; 69, string:"\\"; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:notebook_type; 73, subscript; 73, 74; 73, 75; 74, identifier:split_path; 75, unary_operator:-; 75, 76; 76, integer:2; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:notebook_name; 80, subscript; 80, 81; 80, 90; 81, call; 81, 82; 81, 88; 82, attribute; 82, 83; 82, 87; 83, subscript; 83, 84; 83, 85; 84, identifier:split_path; 85, unary_operator:-; 85, 86; 86, integer:1; 87, identifier:split; 88, argument_list; 88, 89; 89, string:"&"; 90, integer:0; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:notebook_title; 94, subscript; 94, 95; 94, 104; 95, call; 95, 96; 95, 102; 96, attribute; 96, 97; 96, 101; 97, subscript; 97, 98; 97, 99; 98, identifier:split_path; 99, unary_operator:-; 99, 100; 100, integer:1; 101, identifier:split; 102, argument_list; 102, 103; 103, string:"&"; 104, integer:1; 105, expression_statement; 105, 106; 106, augmented_assignment:+=; 106, 107; 106, 108; 107, identifier:markdown_cell; 108, binary_operator:+; 108, 109; 108, 128; 109, binary_operator:+; 109, 110; 109, 123; 110, binary_operator:+; 110, 111; 110, 120; 111, binary_operator:+; 111, 112; 111, 113; 111, 114; 112, string:"\t<tr>\n\t\t<td width='20%' class='header_image_color_"; 113, line_continuation:\; 114, call; 114, 115; 114, 116; 115, identifier:str; 116, argument_list; 116, 117; 117, subscript; 117, 118; 117, 119; 118, identifier:NOTEBOOK_KEYS; 119, identifier:notebook_type; 120, concatenated_string; 120, 121; 120, 122; 121, string:"'><img "; 122, string:"src='../../images/icons/"; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:notebook_type; 126, identifier:title; 127, argument_list; 128, string:".png' width='15%'>\n\t\t</td>"; 129, expression_statement; 129, 130; 130, augmented_assignment:+=; 130, 131; 130, 132; 131, identifier:markdown_cell; 132, binary_operator:+; 132, 133; 132, 137; 133, binary_operator:+; 133, 134; 133, 135; 133, 136; 134, string:"\n\t\t<td width='60%' class='center_cell open_cell_light'>"; 135, line_continuation:\; 136, identifier:notebook_title; 137, string:"\n\t\t</td>"; 138, expression_statement; 138, 139; 139, augmented_assignment:+=; 139, 140; 139, 141; 140, identifier:markdown_cell; 141, binary_operator:+; 141, 142; 141, 155; 142, binary_operator:+; 142, 143; 142, 154; 143, binary_operator:+; 143, 144; 143, 153; 144, binary_operator:+; 144, 145; 144, 148; 145, concatenated_string; 145, 146; 145, 147; 146, string:"\n\t\t<td width='20%' class='center_cell'>\n\t\t\t<a href='"; 147, string:"../"; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:notebook_type; 151, identifier:title; 152, argument_list; 153, string:"/"; 154, identifier:notebook_name; 155, string:"'><div class='file_icon'></div></a>\n\t\t</td>\n\t</tr>"; 156, expression_statement; 156, 157; 157, augmented_assignment:+=; 157, 158; 157, 159; 158, identifier:markdown_cell; 159, string:"</table>"; 160, comment; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:notebook_object; 166, string:"cells"; 167, identifier:append; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 175; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:nb; 173, identifier:v4; 174, identifier:new_markdown_cell; 175, argument_list; 175, 176; 176, identifier:markdown_cell | def _generate_notebook_by_tag_body(notebook_object, dict_by_tag):
"""
Internal function that is used for generation of the page where notebooks are organized by
tag values.
----------
Parameters
----------
notebook_object : notebook object
Object of "notebook" class where the body will be created.
dict_by_tag : dict
Dictionary where each key is a tag and the respective value will be a list containing the
Notebooks (title and filename) that include this tag.
"""
tag_keys = list(dict_by_tag.keys())
tag_keys.sort()
for tag in tag_keys:
if tag.lower() not in SIGNAL_TYPE_LIST:
markdown_cell = group_tag_code.TAG_TABLE_HEADER
markdown_cell = markdown_cell.replace("Tag i", tag)
for notebook_file in dict_by_tag[tag]:
split_path = notebook_file.split("\\")
notebook_type = split_path[-2]
notebook_name = split_path[-1].split("&")[0]
notebook_title = split_path[-1].split("&")[1]
markdown_cell += "\t<tr>\n\t\t<td width='20%' class='header_image_color_" + \
str(NOTEBOOK_KEYS[notebook_type]) + "'><img " \
"src='../../images/icons/" + notebook_type.title() +\
".png' width='15%'>\n\t\t</td>"
markdown_cell += "\n\t\t<td width='60%' class='center_cell open_cell_light'>" + \
notebook_title + "\n\t\t</td>"
markdown_cell += "\n\t\t<td width='20%' class='center_cell'>\n\t\t\t<a href='" \
"../" + notebook_type.title() + "/" + notebook_name + \
"'><div class='file_icon'></div></a>\n\t\t</td>\n\t</tr>"
markdown_cell += "</table>"
# ==================== Insertion of HTML table in a new Notebook cell ======================
notebook_object["cells"].append(nb.v4.new_markdown_cell(markdown_cell)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_preprocess_params; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, identifier:kwargs; 6, block; 6, 7; 6, 9; 6, 10; 6, 255; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, for_statement; 10, 11; 10, 14; 10, 19; 11, pattern_list; 11, 12; 11, 13; 12, identifier:attr; 13, identifier:val; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:kwargs; 17, identifier:items; 18, argument_list; 19, block; 19, 20; 19, 37; 19, 53; 20, if_statement; 20, 21; 20, 31; 21, boolean_operator:and; 21, 22; 21, 28; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:cls; 25, identifier:is_the_primary_key; 26, argument_list; 26, 27; 27, identifier:attr; 28, attribute; 28, 29; 28, 30; 29, identifier:cls; 30, identifier:_prevent_primary_key_initialization_; 31, block; 31, 32; 31, 36; 32, delete_statement; 32, 33; 33, subscript; 33, 34; 33, 35; 34, identifier:kwargs; 35, identifier:attr; 36, continue_statement; 37, if_statement; 37, 38; 37, 41; 37, 42; 37, 43; 37, 44; 37, 45; 38, comparison_operator:==; 38, 39; 38, 40; 39, identifier:val; 40, string:""; 41, comment; 42, comment; 43, comment; 44, comment; 45, block; 45, 46; 45, 52; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:kwargs; 50, identifier:attr; 51, None; 52, continue_statement; 53, if_statement; 53, 54; 53, 68; 54, boolean_operator:and; 54, 55; 54, 63; 55, comparison_operator:in; 55, 56; 55, 57; 56, identifier:attr; 57, attribute; 57, 58; 57, 62; 58, call; 58, 59; 58, 60; 59, identifier:class_mapper; 60, argument_list; 60, 61; 61, identifier:cls; 62, identifier:relationships; 63, comparison_operator:not; 63, 64; 63, 65; 64, identifier:attr; 65, attribute; 65, 66; 65, 67; 66, identifier:cls; 67, identifier:_no_overwrite_; 68, block; 68, 69; 68, 80; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:rel; 72, subscript; 72, 73; 72, 79; 73, attribute; 73, 74; 73, 78; 74, call; 74, 75; 74, 76; 75, identifier:class_mapper; 76, argument_list; 76, 77; 77, identifier:cls; 78, identifier:relationships; 79, identifier:attr; 80, if_statement; 80, 81; 80, 84; 80, 215; 81, attribute; 81, 82; 81, 83; 82, identifier:rel; 83, identifier:uselist; 84, block; 84, 85; 85, if_statement; 85, 86; 85, 91; 85, 135; 86, call; 86, 87; 86, 88; 87, identifier:isinstance; 88, argument_list; 88, 89; 88, 90; 89, identifier:val; 90, identifier:list; 91, block; 91, 92; 92, if_statement; 92, 93; 92, 104; 93, call; 93, 94; 93, 95; 94, identifier:all; 95, generator_expression; 95, 96; 95, 101; 96, call; 96, 97; 96, 98; 97, identifier:isinstance; 98, argument_list; 98, 99; 98, 100; 99, identifier:v; 100, identifier:dict; 101, for_in_clause; 101, 102; 101, 103; 102, identifier:v; 103, identifier:val; 104, block; 104, 105; 104, 114; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:rel_cls; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:cls; 111, identifier:mapped_rel_class; 112, argument_list; 112, 113; 113, identifier:attr; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:kwargs; 118, identifier:attr; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:rel_cls; 122, identifier:update_or_new_all; 123, argument_list; 123, 124; 123, 127; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:list_of_kwargs; 126, identifier:val; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:keys; 129, list:[rel_cls.primary_key_name()]; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:rel_cls; 133, identifier:primary_key_name; 134, argument_list; 135, elif_clause; 135, 136; 135, 141; 136, call; 136, 137; 136, 138; 137, identifier:isinstance; 138, argument_list; 138, 139; 138, 140; 139, identifier:val; 140, identifier:dict; 141, block; 141, 142; 141, 151; 141, 163; 141, 184; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:rel_cls; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:cls; 148, identifier:mapped_rel_class; 149, argument_list; 149, 150; 150, identifier:attr; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:mapping_col; 154, attribute; 154, 155; 154, 162; 155, attribute; 155, 156; 155, 161; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:rel; 159, identifier:collection_class; 160, argument_list; 161, identifier:keyfunc; 162, identifier:name; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:list_of_kwargs; 166, list_comprehension; 166, 167; 166, 175; 167, call; 167, 168; 167, 169; 168, identifier:merge; 169, argument_list; 169, 170; 169, 171; 170, identifier:v; 171, dictionary; 171, 172; 172, pair; 172, 173; 172, 174; 173, identifier:mapping_col; 174, identifier:k; 175, for_in_clause; 175, 176; 175, 179; 176, pattern_list; 176, 177; 176, 178; 177, identifier:k; 178, identifier:v; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:val; 182, identifier:items; 183, argument_list; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 189; 186, subscript; 186, 187; 186, 188; 187, identifier:kwargs; 188, identifier:attr; 189, dictionary_comprehension; 189, 190; 189, 197; 190, pair; 190, 191; 190, 196; 191, call; 191, 192; 191, 193; 192, identifier:getattr; 193, argument_list; 193, 194; 193, 195; 194, identifier:obj; 195, identifier:mapping_col; 196, identifier:obj; 197, for_in_clause; 197, 198; 197, 199; 198, identifier:obj; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:rel_cls; 202, identifier:update_or_new_all; 203, argument_list; 203, 204; 203, 207; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:list_of_kwargs; 206, identifier:list_of_kwargs; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:keys; 209, list:[rel_cls.primary_key_name()]; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:rel_cls; 213, identifier:primary_key_name; 214, argument_list; 215, elif_clause; 215, 216; 215, 221; 216, call; 216, 217; 216, 218; 217, identifier:isinstance; 218, argument_list; 218, 219; 218, 220; 219, identifier:val; 220, identifier:dict; 221, block; 221, 222; 221, 231; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:rel_cls; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:cls; 228, identifier:mapped_rel_class; 229, argument_list; 229, 230; 230, identifier:attr; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 236; 233, subscript; 233, 234; 233, 235; 234, identifier:kwargs; 235, identifier:attr; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:rel_cls; 239, identifier:update_or_new; 240, argument_list; 240, 241; 241, dictionary_splat; 241, 242; 242, call; 242, 243; 242, 244; 243, identifier:merge; 244, argument_list; 244, 245; 244, 246; 245, identifier:val; 246, dictionary; 246, 247; 247, pair; 247, 248; 247, 249; 248, string:'keys'; 249, list:[rel_cls.primary_key_name()]; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:rel_cls; 253, identifier:primary_key_name; 254, argument_list; 255, return_statement; 255, 256; 256, identifier:kwargs | def _preprocess_params(cls, kwargs):
"""Returns a preprocessed dictionary of parameters.
Use this to filter the kwargs passed to `new`, `create`,
`build` methods.
Args:
**kwargs: a dictionary of parameters
"""
# kwargs.pop('csrf_token', None)
for attr, val in kwargs.items():
if cls.is_the_primary_key(attr) and cls._prevent_primary_key_initialization_:
del kwargs[attr]
continue
if val == "":
# Making an assumption that there is no good usecase
# for setting an empty string. This will help prevent
# cases where empty string is sent because of client
# not clearing form fields to null
kwargs[attr] = None
continue
if attr in class_mapper(cls).relationships and attr not in cls._no_overwrite_:
rel = class_mapper(cls).relationships[attr]
if rel.uselist:
if isinstance(val, list):
if all(isinstance(v, dict) for v in val):
rel_cls = cls.mapped_rel_class(attr)
kwargs[attr] = rel_cls.update_or_new_all(
list_of_kwargs=val, keys=[rel_cls.primary_key_name()])
elif isinstance(val, dict):
rel_cls = cls.mapped_rel_class(attr)
mapping_col = rel.collection_class().keyfunc.name
list_of_kwargs = [merge(v, {mapping_col: k}) for k, v in val.items()]
kwargs[attr] = {getattr(obj, mapping_col): obj for obj in rel_cls.update_or_new_all(
list_of_kwargs=list_of_kwargs, keys=[rel_cls.primary_key_name()])}
elif isinstance(val, dict):
rel_cls = cls.mapped_rel_class(attr)
kwargs[attr] = rel_cls.update_or_new(
**merge(val, {'keys': [rel_cls.primary_key_name()]}))
return kwargs |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:add_all; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:cls; 5, identifier:models; 6, default_parameter; 6, 7; 6, 8; 7, identifier:commit; 8, True; 9, default_parameter; 9, 10; 9, 11; 10, identifier:check_type; 11, False; 12, block; 12, 13; 12, 15; 12, 39; 12, 72; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 17; 16, identifier:check_type; 17, block; 17, 18; 18, for_statement; 18, 19; 18, 20; 18, 21; 19, identifier:model; 20, identifier:models; 21, block; 21, 22; 22, if_statement; 22, 23; 22, 29; 23, not_operator; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:isinstance; 26, argument_list; 26, 27; 26, 28; 27, identifier:model; 28, identifier:cls; 29, block; 29, 30; 30, raise_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:ValueError; 33, argument_list; 33, 34; 34, call; 34, 35; 34, 36; 35, string:'%s is not of type %s'; 36, argument_list; 36, 37; 36, 38; 37, identifier:model; 38, identifier:cls; 39, if_statement; 39, 40; 39, 43; 39, 61; 40, comparison_operator:in; 40, 41; 40, 42; 41, None; 42, identifier:models; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 51; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:cls; 49, identifier:session; 50, identifier:add_all; 51, argument_list; 51, 52; 52, list_comprehension; 52, 53; 52, 54; 52, 57; 53, identifier:m; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:m; 56, identifier:models; 57, if_clause; 57, 58; 58, comparison_operator:is; 58, 59; 58, 60; 59, identifier:m; 60, None; 61, else_clause; 61, 62; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:cls; 68, identifier:session; 69, identifier:add_all; 70, argument_list; 70, 71; 71, identifier:models; 72, try_statement; 72, 73; 72, 87; 73, block; 73, 74; 73, 85; 74, if_statement; 74, 75; 74, 76; 75, identifier:commit; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:cls; 82, identifier:session; 83, identifier:commit; 84, argument_list; 85, return_statement; 85, 86; 86, identifier:models; 87, except_clause; 87, 88; 88, block; 88, 89; 88, 97; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:cls; 94, identifier:session; 95, identifier:rollback; 96, argument_list; 97, raise_statement | def add_all(cls, models, commit=True, check_type=False):
"""Batch method for adding a list of model instances
to the db in one get_or_404.
Args:
models (list): A list of the instances to add.
commit (bool, optional): Defaults to True. If False, the
transaction won't get committed.
check_type (bool, optional) : If True, each instance
is type checked and exception is thrown if it is
not an instance of the model. By default, False.
Returns:
list: A list of `Model` instances
"""
if check_type:
for model in models:
if not isinstance(model, cls):
raise ValueError('%s is not of type %s' (model, cls))
if None in models:
cls.session.add_all([m for m in models if m is not None])
else:
cls.session.add_all(models)
try:
if commit:
cls.session.commit()
return models
except:
cls.session.rollback()
raise |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:peakdelta; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:v; 5, identifier:delta; 6, default_parameter; 6, 7; 6, 8; 7, identifier:x; 8, None; 9, block; 9, 10; 9, 12; 9, 16; 9, 20; 9, 35; 9, 42; 9, 60; 9, 74; 9, 86; 9, 95; 9, 103; 9, 107; 9, 218; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:maxtab; 15, list:[]; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:mintab; 19, list:[]; 20, if_statement; 20, 21; 20, 24; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:x; 23, None; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:x; 28, call; 28, 29; 28, 30; 29, identifier:arange; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:len; 33, argument_list; 33, 34; 34, identifier:v; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:v; 38, call; 38, 39; 38, 40; 39, identifier:asarray; 40, argument_list; 40, 41; 41, identifier:v; 42, if_statement; 42, 43; 42, 52; 43, comparison_operator:!=; 43, 44; 43, 48; 44, call; 44, 45; 44, 46; 45, identifier:len; 46, argument_list; 46, 47; 47, identifier:v; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, identifier:x; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:sys; 57, identifier:exit; 58, argument_list; 58, 59; 59, string:'Input vectors v and x must have same length'; 60, if_statement; 60, 61; 60, 66; 61, not_operator; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:isscalar; 64, argument_list; 64, 65; 65, identifier:delta; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:sys; 71, identifier:exit; 72, argument_list; 72, 73; 73, string:'Input argument delta must be a scalar'; 74, if_statement; 74, 75; 74, 78; 75, comparison_operator:<=; 75, 76; 75, 77; 76, identifier:delta; 77, integer:0; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:sys; 83, identifier:exit; 84, argument_list; 84, 85; 85, string:'Input argument delta must be positive'; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, pattern_list; 88, 89; 88, 90; 89, identifier:mn; 90, identifier:mx; 91, expression_list; 91, 92; 91, 93; 92, identifier:Inf; 93, unary_operator:-; 93, 94; 94, identifier:Inf; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, pattern_list; 97, 98; 97, 99; 98, identifier:mnpos; 99, identifier:mxpos; 100, expression_list; 100, 101; 100, 102; 101, identifier:NaN; 102, identifier:NaN; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:lookformax; 106, True; 107, for_statement; 107, 108; 107, 109; 107, 116; 108, identifier:i; 109, call; 109, 110; 109, 111; 110, identifier:arange; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, identifier:v; 116, block; 116, 117; 116, 123; 116, 138; 116, 153; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:this; 120, subscript; 120, 121; 120, 122; 121, identifier:v; 122, identifier:i; 123, if_statement; 123, 124; 123, 127; 124, comparison_operator:>; 124, 125; 124, 126; 125, identifier:this; 126, identifier:mx; 127, block; 127, 128; 127, 132; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:mx; 131, identifier:this; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:mxpos; 135, subscript; 135, 136; 135, 137; 136, identifier:x; 137, identifier:i; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:<; 139, 140; 139, 141; 140, identifier:this; 141, identifier:mn; 142, block; 142, 143; 142, 147; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:mn; 146, identifier:this; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:mnpos; 150, subscript; 150, 151; 150, 152; 151, identifier:x; 152, identifier:i; 153, if_statement; 153, 154; 153, 155; 153, 186; 154, identifier:lookformax; 155, block; 155, 156; 156, if_statement; 156, 157; 156, 162; 157, comparison_operator:<; 157, 158; 157, 159; 158, identifier:this; 159, binary_operator:-; 159, 160; 159, 161; 160, identifier:mx; 161, identifier:delta; 162, block; 162, 163; 162, 172; 162, 176; 162, 182; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:maxtab; 167, identifier:append; 168, argument_list; 168, 169; 169, tuple; 169, 170; 169, 171; 170, identifier:mxpos; 171, identifier:mx; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:mn; 175, identifier:this; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:mnpos; 179, subscript; 179, 180; 179, 181; 180, identifier:x; 181, identifier:i; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:lookformax; 185, False; 186, else_clause; 186, 187; 187, block; 187, 188; 188, if_statement; 188, 189; 188, 194; 189, comparison_operator:>; 189, 190; 189, 191; 190, identifier:this; 191, binary_operator:+; 191, 192; 191, 193; 192, identifier:mn; 193, identifier:delta; 194, block; 194, 195; 194, 204; 194, 208; 194, 214; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:mintab; 199, identifier:append; 200, argument_list; 200, 201; 201, tuple; 201, 202; 201, 203; 202, identifier:mnpos; 203, identifier:mn; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:mx; 207, identifier:this; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:mxpos; 211, subscript; 211, 212; 211, 213; 212, identifier:x; 213, identifier:i; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:lookformax; 217, True; 218, return_statement; 218, 219; 219, expression_list; 219, 220; 219, 224; 220, call; 220, 221; 220, 222; 221, identifier:array; 222, argument_list; 222, 223; 223, identifier:maxtab; 224, call; 224, 225; 224, 226; 225, identifier:array; 226, argument_list; 226, 227; 227, identifier:mintab | def peakdelta(v, delta, x=None):
"""
Returns two arrays
function [maxtab, mintab]=peakdelta(v, delta, x)
%PEAKDET Detect peaks in a vector
% [MAXTAB, MINTAB] = peakdelta(V, DELTA) finds the local
% maxima and minima ("peaks") in the vector V.
% MAXTAB and MINTAB consists of two columns. Column 1
% contains indices in V, and column 2 the found values.
%
% With [MAXTAB, MINTAB] = peakdelta(V, DELTA, X) the indices
% in MAXTAB and MINTAB are replaced with the corresponding
% X-values.
%
% A point is considered a maximum peak if it has the maximal
% value, and was preceded (to the left) by a value lower by
% DELTA.
% Eli Billauer, 3.4.05 (Explicitly not copyrighted).
% This function is released to the public domain; Any use is allowed.
"""
maxtab = []
mintab = []
if x is None:
x = arange(len(v))
v = asarray(v)
if len(v) != len(x):
sys.exit('Input vectors v and x must have same length')
if not isscalar(delta):
sys.exit('Input argument delta must be a scalar')
if delta <= 0:
sys.exit('Input argument delta must be positive')
mn, mx = Inf, -Inf
mnpos, mxpos = NaN, NaN
lookformax = True
for i in arange(len(v)):
this = v[i]
if this > mx:
mx = this
mxpos = x[i]
if this < mn:
mn = this
mnpos = x[i]
if lookformax:
if this < mx - delta:
maxtab.append((mxpos, mx))
mn = this
mnpos = x[i]
lookformax = False
else:
if this > mn + delta:
mintab.append((mnpos, mn))
mx = this
mxpos = x[i]
lookformax = True
return array(maxtab), array(mintab) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_datetime; 3, parameters; 3, 4; 4, identifier:value; 5, block; 5, 6; 5, 8; 5, 16; 5, 74; 5, 87; 5, 96; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 8, 13; 9, comparison_operator:is; 9, 10; 9, 11; 10, identifier:value; 11, None; 12, comment; 13, block; 13, 14; 14, return_statement; 14, 15; 15, None; 16, function_definition; 16, 17; 16, 18; 16, 20; 17, function_name:_get_fixed_timezone; 18, parameters; 18, 19; 19, identifier:offset; 20, block; 20, 21; 20, 23; 20, 38; 20, 47; 20, 60; 20, 66; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 29; 24, call; 24, 25; 24, 26; 25, identifier:isinstance; 26, argument_list; 26, 27; 26, 28; 27, identifier:offset; 28, identifier:timedelta; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:offset; 33, binary_operator://; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:offset; 36, identifier:seconds; 37, integer:60; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:sign; 41, conditional_expression:if; 41, 42; 41, 43; 41, 46; 42, string:'-'; 43, comparison_operator:<; 43, 44; 43, 45; 44, identifier:offset; 45, integer:0; 46, string:'+'; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:hhmm; 50, binary_operator:%; 50, 51; 50, 52; 51, string:'%02d%02d'; 52, call; 52, 53; 52, 54; 53, identifier:divmod; 54, argument_list; 54, 55; 54, 59; 55, call; 55, 56; 55, 57; 56, identifier:abs; 57, argument_list; 57, 58; 58, identifier:offset; 59, integer:60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:name; 63, binary_operator:+; 63, 64; 63, 65; 64, identifier:sign; 65, identifier:hhmm; 66, return_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:pytz; 70, identifier:FixedOffset; 71, argument_list; 71, 72; 71, 73; 72, identifier:offset; 73, identifier:name; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:DATETIME_RE; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:re; 80, identifier:compile; 81, argument_list; 81, 82; 82, concatenated_string; 82, 83; 82, 84; 82, 85; 82, 86; 83, string:r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'; 84, string:r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})'; 85, string:r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'; 86, string:r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$'; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:match; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:DATETIME_RE; 93, identifier:match; 94, argument_list; 94, 95; 95, identifier:value; 96, if_statement; 96, 97; 96, 98; 97, identifier:match; 98, block; 98, 99; 98, 107; 98, 126; 98, 135; 98, 206; 98, 230; 98, 236; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:kw; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:match; 105, identifier:groupdict; 106, argument_list; 107, if_statement; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:kw; 110, string:'microsecond'; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:kw; 116, string:'microsecond'; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, subscript; 119, 120; 119, 121; 120, identifier:kw; 121, string:'microsecond'; 122, identifier:ljust; 123, argument_list; 123, 124; 123, 125; 124, integer:6; 125, string:'0'; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:tzinfo; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:kw; 132, identifier:pop; 133, argument_list; 133, 134; 134, string:'tzinfo'; 135, if_statement; 135, 136; 135, 139; 135, 146; 136, comparison_operator:==; 136, 137; 136, 138; 137, identifier:tzinfo; 138, string:'Z'; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:tzinfo; 143, attribute; 143, 144; 143, 145; 144, identifier:pytz; 145, identifier:UTC; 146, elif_clause; 146, 147; 146, 150; 147, comparison_operator:is; 147, 148; 147, 149; 148, identifier:tzinfo; 149, None; 150, block; 150, 151; 150, 171; 150, 187; 150, 199; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:offset_mins; 154, conditional_expression:if; 154, 155; 154, 164; 154, 170; 155, call; 155, 156; 155, 157; 156, identifier:int; 157, argument_list; 157, 158; 158, subscript; 158, 159; 158, 160; 159, identifier:tzinfo; 160, slice; 160, 161; 160, 163; 161, unary_operator:-; 161, 162; 162, integer:2; 163, colon; 164, comparison_operator:>; 164, 165; 164, 169; 165, call; 165, 166; 165, 167; 166, identifier:len; 167, argument_list; 167, 168; 168, identifier:tzinfo; 169, integer:3; 170, integer:0; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:offset; 174, binary_operator:+; 174, 175; 174, 186; 175, binary_operator:*; 175, 176; 175, 177; 176, integer:60; 177, call; 177, 178; 177, 179; 178, identifier:int; 179, argument_list; 179, 180; 180, subscript; 180, 181; 180, 182; 181, identifier:tzinfo; 182, slice; 182, 183; 182, 184; 182, 185; 183, integer:1; 184, colon; 185, integer:3; 186, identifier:offset_mins; 187, if_statement; 187, 188; 187, 193; 188, comparison_operator:==; 188, 189; 188, 192; 189, subscript; 189, 190; 189, 191; 190, identifier:tzinfo; 191, integer:0; 192, string:'-'; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:offset; 197, unary_operator:-; 197, 198; 198, identifier:offset; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:tzinfo; 202, call; 202, 203; 202, 204; 203, identifier:_get_fixed_timezone; 204, argument_list; 204, 205; 205, identifier:offset; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:kw; 209, dictionary_comprehension; 209, 210; 209, 216; 209, 226; 210, pair; 210, 211; 210, 212; 211, identifier:k; 212, call; 212, 213; 212, 214; 213, identifier:int; 214, argument_list; 214, 215; 215, identifier:v; 216, for_in_clause; 216, 217; 216, 220; 217, pattern_list; 217, 218; 217, 219; 218, identifier:k; 219, identifier:v; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:six; 223, identifier:iteritems; 224, argument_list; 224, 225; 225, identifier:kw; 226, if_clause; 226, 227; 227, comparison_operator:is; 227, 228; 227, 229; 228, identifier:v; 229, None; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:kw; 234, string:'tzinfo'; 235, identifier:tzinfo; 236, return_statement; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:datetime; 239, argument_list; 239, 240; 240, dictionary_splat; 240, 241; 241, identifier:kw | def parse_datetime(value):
"""
Convert datetime string to datetime object.
Helper function to convert a datetime string found in json responses to a datetime object with timezone information.
The server is storing all datetime strings as UTC (ZULU time). This function supports time zone offsets. When
the input contains one, the output uses a timezone with a fixed offset from UTC.
Inspired on the Django project. From `django.utils.dateparse.parse_datetime`. The code is copyrighted and
licences with an MIT license in the following fashion::
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
..versionadded 2.5:
:param value: datetime string
:type value: str or None
:return: datetime of the value is well formatted. Otherwise (including if value is None) returns None
:rtype: datetime or None
:raises ValueError: if the value is well formatted but not a valid datetime
"""
if value is None:
# do not process the value
return None
def _get_fixed_timezone(offset):
"""Return a tzinfo instance with a fixed offset from UTC."""
if isinstance(offset, timedelta):
offset = offset.seconds // 60
sign = '-' if offset < 0 else '+'
hhmm = '%02d%02d' % divmod(abs(offset), 60)
name = sign + hhmm
return pytz.FixedOffset(offset, name)
DATETIME_RE = re.compile(
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'
r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$'
)
match = DATETIME_RE.match(value)
if match:
kw = match.groupdict()
if kw['microsecond']:
kw['microsecond'] = kw['microsecond'].ljust(6, '0')
tzinfo = kw.pop('tzinfo')
if tzinfo == 'Z':
tzinfo = pytz.UTC
elif tzinfo is not None:
offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
offset = 60 * int(tzinfo[1:3]) + offset_mins
if tzinfo[0] == '-':
offset = -offset
tzinfo = _get_fixed_timezone(offset)
kw = {k: int(v) for k, v in six.iteritems(kw) if v is not None}
kw['tzinfo'] = tzinfo
return datetime(**kw) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:add_property_grid_widget; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:part_instance; 6, default_parameter; 6, 7; 6, 8; 7, identifier:max_height; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:custom_title; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:show_headers; 14, True; 15, default_parameter; 15, 16; 15, 17; 16, identifier:show_columns; 17, None; 18, block; 18, 19; 18, 21; 18, 25; 18, 26; 18, 85; 18, 95; 18, 96; 18, 106; 18, 112; 18, 135; 18, 136; 18, 179; 18, 180; 18, 189; 18, 232; 18, 238; 18, 244; 18, 245; 18, 286; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:height; 24, identifier:max_height; 25, comment; 26, if_statement; 26, 27; 26, 32; 26, 39; 26, 68; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 31; 30, identifier:part_instance; 31, identifier:Part; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:part_instance_id; 36, attribute; 36, 37; 36, 38; 37, identifier:part_instance; 38, identifier:id; 39, elif_clause; 39, 40; 39, 50; 40, boolean_operator:and; 40, 41; 40, 46; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 45; 44, identifier:part_instance; 45, identifier:text_type; 46, call; 46, 47; 46, 48; 47, identifier:is_uuid; 48, argument_list; 48, 49; 49, identifier:part_instance; 50, block; 50, 51; 50, 55; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:part_instance_id; 54, identifier:part_instance; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:part_instance; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:_client; 63, identifier:part; 64, argument_list; 64, 65; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:id; 67, identifier:part_instance_id; 68, else_clause; 68, 69; 69, block; 69, 70; 70, raise_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:IllegalArgumentError; 73, argument_list; 73, 74; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, concatenated_string; 76, 77; 76, 78; 77, string:"When using the add_property_grid_widget, part_instance must be a "; 78, string:"Part or Part id. Type is: {}"; 79, identifier:format; 80, argument_list; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:type; 83, argument_list; 83, 84; 84, identifier:part_instance; 85, if_statement; 85, 86; 85, 88; 86, not_operator; 86, 87; 87, identifier:show_columns; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:show_columns; 92, call; 92, 93; 92, 94; 93, identifier:list; 94, argument_list; 95, comment; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:possible_columns; 99, list:[ShowColumnTypes.DESCRIPTION, ShowColumnTypes.UNIT]; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:ShowColumnTypes; 102, identifier:DESCRIPTION; 103, attribute; 103, 104; 103, 105; 104, identifier:ShowColumnTypes; 105, identifier:UNIT; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:display_columns; 109, call; 109, 110; 109, 111; 110, identifier:dict; 111, argument_list; 112, for_statement; 112, 113; 112, 114; 112, 115; 113, identifier:possible_column; 114, identifier:possible_columns; 115, block; 115, 116; 116, if_statement; 116, 117; 116, 120; 116, 127; 117, comparison_operator:in; 117, 118; 117, 119; 118, identifier:possible_column; 119, identifier:show_columns; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, subscript; 123, 124; 123, 125; 124, identifier:display_columns; 125, identifier:possible_column; 126, True; 127, else_clause; 127, 128; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:display_columns; 133, identifier:possible_column; 134, False; 135, comment; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:config; 139, dictionary; 139, 140; 139, 145; 139, 150; 139, 166; 139, 170; 140, pair; 140, 141; 140, 142; 141, string:"xtype"; 142, attribute; 142, 143; 142, 144; 143, identifier:ComponentXType; 144, identifier:PROPERTYGRID; 145, pair; 145, 146; 145, 147; 146, string:"category"; 147, attribute; 147, 148; 147, 149; 148, identifier:Category; 149, identifier:INSTANCE; 150, pair; 150, 151; 150, 152; 151, string:"filter"; 152, dictionary; 152, 153; 152, 163; 153, pair; 153, 154; 153, 155; 154, string:"activity_id"; 155, call; 155, 156; 155, 157; 156, identifier:str; 157, argument_list; 157, 158; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:activity; 162, identifier:id; 163, pair; 163, 164; 163, 165; 164, string:"part"; 165, identifier:part_instance_id; 166, pair; 166, 167; 166, 168; 167, string:"hideHeaders"; 168, not_operator; 168, 169; 169, identifier:show_headers; 170, pair; 170, 171; 170, 172; 171, string:"viewModel"; 172, dictionary; 172, 173; 173, pair; 173, 174; 173, 175; 174, string:"data"; 175, dictionary; 175, 176; 176, pair; 176, 177; 176, 178; 177, string:"displayColumns"; 178, identifier:display_columns; 179, comment; 180, if_statement; 180, 181; 180, 182; 181, identifier:height; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:config; 187, string:'height'; 188, identifier:height; 189, if_statement; 189, 190; 189, 193; 189, 204; 189, 219; 190, comparison_operator:is; 190, 191; 190, 192; 191, identifier:custom_title; 192, False; 193, block; 193, 194; 193, 198; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:show_title_value; 197, string:"Default"; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:title; 201, attribute; 201, 202; 201, 203; 202, identifier:part_instance; 203, identifier:name; 204, elif_clause; 204, 205; 204, 208; 205, comparison_operator:is; 205, 206; 205, 207; 206, identifier:custom_title; 207, None; 208, block; 208, 209; 208, 213; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:show_title_value; 212, string:"No title"; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:title; 216, call; 216, 217; 216, 218; 217, identifier:str; 218, argument_list; 219, else_clause; 219, 220; 220, block; 220, 221; 220, 225; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:show_title_value; 224, string:"Custom title"; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:title; 228, call; 228, 229; 228, 230; 229, identifier:str; 230, argument_list; 230, 231; 231, identifier:custom_title; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:config; 236, string:"title"; 237, identifier:title; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 243; 240, subscript; 240, 241; 240, 242; 241, identifier:config; 242, string:"showTitleValue"; 243, identifier:show_title_value; 244, comment; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:meta; 248, dictionary; 248, 249; 248, 259; 248, 265; 248, 268; 248, 271; 248, 274; 248, 277; 248, 283; 249, pair; 249, 250; 249, 251; 250, string:"activityId"; 251, call; 251, 252; 251, 253; 252, identifier:str; 253, argument_list; 253, 254; 254, attribute; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:activity; 258, identifier:id; 259, pair; 259, 260; 259, 261; 260, string:"customHeight"; 261, conditional_expression:if; 261, 262; 261, 263; 261, 264; 262, identifier:height; 263, identifier:height; 264, None; 265, pair; 265, 266; 265, 267; 266, string:"customTitle"; 267, identifier:title; 268, pair; 268, 269; 268, 270; 269, string:"partInstanceId"; 270, identifier:part_instance_id; 271, pair; 271, 272; 271, 273; 272, string:"showColumns"; 273, identifier:show_columns; 274, pair; 274, 275; 274, 276; 275, string:"showHeaders"; 276, identifier:show_headers; 277, pair; 277, 278; 277, 279; 278, string:"showHeightValue"; 279, conditional_expression:if; 279, 280; 279, 281; 279, 282; 280, string:"Set height"; 281, identifier:height; 282, string:"Automatic height"; 283, pair; 283, 284; 283, 285; 284, string:"showTitleValue"; 285, identifier:show_title_value; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:self; 290, identifier:_add_widget; 291, argument_list; 291, 292; 292, call; 292, 293; 292, 294; 293, identifier:dict; 294, argument_list; 294, 295; 294, 298; 294, 301; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:config; 297, identifier:config; 298, keyword_argument; 298, 299; 298, 300; 299, identifier:meta; 300, identifier:meta; 301, keyword_argument; 301, 302; 301, 303; 302, identifier:name; 303, attribute; 303, 304; 303, 305; 304, identifier:WidgetNames; 305, identifier:PROPERTYGRIDWIDGET | def add_property_grid_widget(self, part_instance, max_height=None, custom_title=False, show_headers=True,
show_columns=None):
"""
Add a KE-chain Property Grid widget to the customization.
The widget will be saved to KE-chain.
:param part_instance: The part instance on which the property grid will be based
:type part_instance: :class:`Part` or UUID
:param max_height: The max height of the property grid in pixels
:type max_height: int or None
:param custom_title: A custom title for the property grid::
* False (default): Part instance name
* String value: Custom title
* None: No title
:type custom_title: bool or basestring or None
:param show_headers: Show or hide the headers in the grid (default True)
:type show_headers: bool
:param show_columns: Columns to be hidden or shown (default to 'unit' and 'description')
:type show_columns: list
:raises IllegalArgumentError: When unknown or illegal arguments are passed.
"""
height = max_height
# Check whether the parent_part_instance is uuid type or class `Part`
if isinstance(part_instance, Part):
part_instance_id = part_instance.id
elif isinstance(part_instance, text_type) and is_uuid(part_instance):
part_instance_id = part_instance
part_instance = self._client.part(id=part_instance_id)
else:
raise IllegalArgumentError("When using the add_property_grid_widget, part_instance must be a "
"Part or Part id. Type is: {}".format(type(part_instance)))
if not show_columns:
show_columns = list()
# Set the display_columns for the config
possible_columns = [ShowColumnTypes.DESCRIPTION, ShowColumnTypes.UNIT]
display_columns = dict()
for possible_column in possible_columns:
if possible_column in show_columns:
display_columns[possible_column] = True
else:
display_columns[possible_column] = False
# Declare property grid config
config = {
"xtype": ComponentXType.PROPERTYGRID,
"category": Category.INSTANCE,
"filter": {
"activity_id": str(self.activity.id),
"part": part_instance_id
},
"hideHeaders": not show_headers,
"viewModel": {
"data": {
"displayColumns": display_columns
}
},
}
# Add max height and custom title
if height:
config['height'] = height
if custom_title is False:
show_title_value = "Default"
title = part_instance.name
elif custom_title is None:
show_title_value = "No title"
title = str()
else:
show_title_value = "Custom title"
title = str(custom_title)
config["title"] = title
config["showTitleValue"] = show_title_value
# Declare the meta info for the property grid
meta = {
"activityId": str(self.activity.id),
"customHeight": height if height else None,
"customTitle": title,
"partInstanceId": part_instance_id,
"showColumns": show_columns,
"showHeaders": show_headers,
"showHeightValue": "Set height" if height else "Automatic height",
"showTitleValue": show_title_value
}
self._add_widget(dict(config=config, meta=meta, name=WidgetNames.PROPERTYGRIDWIDGET)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_generate_data_for_format; 3, parameters; 3, 4; 4, identifier:fmt; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 14; 5, 15; 5, 16; 5, 20; 5, 230; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:data; 12, dictionary; 13, comment; 14, comment; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:free_size_start; 19, False; 20, for_statement; 20, 21; 20, 26; 20, 32; 21, pattern_list; 21, 22; 21, 23; 21, 24; 21, 25; 22, identifier:literal_text; 23, identifier:field_name; 24, identifier:format_spec; 25, identifier:conversion; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:formatter; 29, identifier:parse; 30, argument_list; 30, 31; 31, identifier:fmt; 32, block; 32, 33; 32, 40; 32, 49; 32, 50; 32, 51; 32, 74; 32, 75; 33, if_statement; 33, 34; 33, 35; 34, identifier:literal_text; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:free_size_start; 39, False; 40, if_statement; 40, 41; 40, 43; 41, not_operator; 41, 42; 42, identifier:field_name; 43, block; 43, 44; 43, 48; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:free_size_start; 47, False; 48, continue_statement; 49, comment; 50, comment; 51, if_statement; 51, 52; 51, 62; 52, boolean_operator:or; 52, 53; 52, 59; 53, boolean_operator:or; 53, 54; 53, 56; 54, not_operator; 54, 55; 55, identifier:format_spec; 56, comparison_operator:==; 56, 57; 56, 58; 57, identifier:format_spec; 58, string:"s"; 59, comparison_operator:==; 59, 60; 59, 61; 60, identifier:format_spec; 61, string:"d"; 62, block; 62, 63; 63, if_statement; 63, 64; 63, 65; 63, 68; 64, identifier:free_size_start; 65, block; 65, 66; 66, return_statement; 66, 67; 67, None; 68, else_clause; 68, 69; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:free_size_start; 73, True; 74, comment; 75, if_statement; 75, 76; 75, 81; 75, 82; 75, 125; 75, 170; 76, boolean_operator:and; 76, 77; 76, 78; 77, identifier:format_spec; 78, comparison_operator:in; 78, 79; 78, 80; 79, string:'%'; 80, identifier:format_spec; 81, comment; 82, block; 82, 83; 82, 93; 82, 94; 82, 119; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:t; 86, call; 86, 87; 86, 92; 87, attribute; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:dt; 90, identifier:datetime; 91, identifier:now; 92, argument_list; 93, comment; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:t; 97, subscript; 97, 98; 97, 118; 98, call; 98, 99; 98, 100; 99, identifier:parse; 100, argument_list; 100, 101; 100, 106; 101, binary_operator:+; 101, 102; 101, 105; 102, binary_operator:+; 102, 103; 102, 104; 103, string:"{t:"; 104, identifier:format_spec; 105, string:"}"; 106, call; 106, 107; 106, 108; 107, identifier:compose; 108, argument_list; 108, 109; 108, 114; 109, binary_operator:+; 109, 110; 109, 113; 110, binary_operator:+; 110, 111; 110, 112; 111, string:"{t:"; 112, identifier:format_spec; 113, string:"}"; 114, dictionary; 114, 115; 115, pair; 115, 116; 115, 117; 116, string:'t'; 117, identifier:t; 118, string:'t'; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:data; 123, identifier:field_name; 124, identifier:t; 125, elif_clause; 125, 126; 125, 131; 125, 132; 126, boolean_operator:and; 126, 127; 126, 128; 127, identifier:format_spec; 128, comparison_operator:in; 128, 129; 128, 130; 129, string:'d'; 130, identifier:format_spec; 131, comment; 132, block; 132, 133; 132, 153; 133, if_statement; 133, 134; 133, 140; 133, 148; 134, not_operator; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:format_spec; 138, identifier:isalpha; 139, argument_list; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:n; 144, call; 144, 145; 144, 146; 145, identifier:_get_number_from_fmt; 146, argument_list; 146, 147; 147, identifier:format_spec; 148, else_clause; 148, 149; 148, 150; 149, comment; 150, block; 150, 151; 151, return_statement; 151, 152; 152, None; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:data; 157, identifier:field_name; 158, binary_operator:%; 158, 159; 158, 166; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:random; 162, identifier:randint; 163, argument_list; 163, 164; 163, 165; 164, integer:0; 165, integer:99999999999999999; 166, parenthesized_expression; 166, 167; 167, binary_operator:**; 167, 168; 167, 169; 168, integer:10; 169, identifier:n; 170, else_clause; 170, 171; 170, 172; 171, comment; 172, block; 172, 173; 172, 202; 172, 206; 172, 224; 173, if_statement; 173, 174; 173, 177; 173, 182; 173, 196; 174, comparison_operator:is; 174, 175; 174, 176; 175, identifier:format_spec; 176, None; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:n; 181, integer:4; 182, elif_clause; 182, 183; 182, 188; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:format_spec; 186, identifier:isalnum; 187, argument_list; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:n; 192, call; 192, 193; 192, 194; 193, identifier:_get_number_from_fmt; 194, argument_list; 194, 195; 195, identifier:format_spec; 196, else_clause; 196, 197; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:n; 201, integer:4; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:randstri; 205, string:''; 206, for_statement; 206, 207; 206, 208; 206, 212; 207, identifier:x; 208, call; 208, 209; 208, 210; 209, identifier:range; 210, argument_list; 210, 211; 211, identifier:n; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, augmented_assignment:+=; 214, 215; 214, 216; 215, identifier:randstri; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:random; 219, identifier:choice; 220, argument_list; 220, 221; 221, attribute; 221, 222; 221, 223; 222, identifier:string; 223, identifier:ascii_letters; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:data; 228, identifier:field_name; 229, identifier:randstri; 230, return_statement; 230, 231; 231, identifier:data | def _generate_data_for_format(fmt):
"""Generate a fake data dictionary to fill in the provided format string."""
# finally try some data, create some random data for the fmt.
data = {}
# keep track of how many "free_size" (wildcard) parameters we have
# if we get two in a row then we know the pattern is invalid, meaning
# we'll never be able to match the second wildcard field
free_size_start = False
for literal_text, field_name, format_spec, conversion in formatter.parse(fmt):
if literal_text:
free_size_start = False
if not field_name:
free_size_start = False
continue
# encapsulating free size keys,
# e.g. {:s}{:s} or {:s}{:4s}{:d}
if not format_spec or format_spec == "s" or format_spec == "d":
if free_size_start:
return None
else:
free_size_start = True
# make some data for this key and format
if format_spec and '%' in format_spec:
# some datetime
t = dt.datetime.now()
# run once through format to limit precision
t = parse(
"{t:" + format_spec + "}", compose("{t:" + format_spec + "}", {'t': t}))['t']
data[field_name] = t
elif format_spec and 'd' in format_spec:
# random number (with n sign. figures)
if not format_spec.isalpha():
n = _get_number_from_fmt(format_spec)
else:
# clearly bad
return None
data[field_name] = random.randint(0, 99999999999999999) % (10 ** n)
else:
# string type
if format_spec is None:
n = 4
elif format_spec.isalnum():
n = _get_number_from_fmt(format_spec)
else:
n = 4
randstri = ''
for x in range(n):
randstri += random.choice(string.ascii_letters)
data[field_name] = randstri
return data |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:relocate_model; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:part; 5, identifier:target_parent; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:include_children; 11, True; 12, block; 12, 13; 12, 15; 12, 48; 12, 49; 12, 64; 12, 65; 12, 73; 12, 89; 12, 104; 12, 105; 12, 119; 12, 120; 12, 126; 12, 142; 12, 368; 12, 369; 12, 404; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 30; 16, comparison_operator:in; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:target_parent; 19, identifier:id; 20, call; 20, 21; 20, 22; 21, identifier:get_illegal_targets; 22, argument_list; 22, 23; 22, 24; 23, identifier:part; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:include; 26, set; 26, 27; 27, attribute; 27, 28; 27, 29; 28, identifier:part; 29, identifier:id; 30, block; 30, 31; 31, raise_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:IllegalArgumentError; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, concatenated_string; 37, 38; 37, 39; 38, string:'cannot relocate part "{}" under target parent "{}", because the target is part of '; 39, string:'its descendants'; 40, identifier:format; 41, argument_list; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:part; 44, identifier:name; 45, attribute; 45, 46; 45, 47; 46, identifier:target_parent; 47, identifier:name; 48, comment; 49, if_statement; 49, 50; 49, 52; 50, not_operator; 50, 51; 51, identifier:name; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:name; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, string:"CLONE - {}"; 59, identifier:format; 60, argument_list; 60, 61; 61, attribute; 61, 62; 61, 63; 62, identifier:part; 63, identifier:name; 64, comment; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:part_desc; 68, subscript; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:part; 71, identifier:_json_data; 72, string:'description'; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:moved_part_model; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:target_parent; 79, identifier:add_model; 80, argument_list; 80, 81; 80, 84; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:name; 83, identifier:name; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:multiplicity; 86, attribute; 86, 87; 86, 88; 87, identifier:part; 88, identifier:multiplicity; 89, if_statement; 89, 90; 89, 91; 90, identifier:part_desc; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:moved_part_model; 96, identifier:edit; 97, argument_list; 97, 98; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:description; 100, call; 100, 101; 100, 102; 101, identifier:str; 102, argument_list; 102, 103; 103, identifier:part_desc; 104, comment; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 112; 107, attribute; 107, 108; 107, 111; 108, call; 108, 109; 108, 110; 109, identifier:get_mapping_dictionary; 110, argument_list; 111, identifier:update; 112, argument_list; 112, 113; 113, dictionary; 113, 114; 114, pair; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:part; 117, identifier:id; 118, identifier:moved_part_model; 119, comment; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:list_of_properties_sorted_by_order; 123, attribute; 123, 124; 123, 125; 124, identifier:part; 125, identifier:properties; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:list_of_properties_sorted_by_order; 130, identifier:sort; 131, argument_list; 131, 132; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:key; 134, lambda; 134, 135; 134, 137; 135, lambda_parameters; 135, 136; 136, identifier:x; 137, subscript; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:x; 140, identifier:_json_data; 141, string:'order'; 142, for_statement; 142, 143; 142, 144; 142, 145; 143, identifier:prop; 144, identifier:list_of_properties_sorted_by_order; 145, block; 145, 146; 145, 157; 145, 168; 145, 179; 145, 190; 145, 191; 145, 357; 145, 358; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:prop_type; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:prop; 153, identifier:_json_data; 154, identifier:get; 155, argument_list; 155, 156; 156, string:'property_type'; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:desc; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:prop; 164, identifier:_json_data; 165, identifier:get; 166, argument_list; 166, 167; 167, string:'description'; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:unit; 171, call; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:prop; 175, identifier:_json_data; 176, identifier:get; 177, argument_list; 177, 178; 178, string:'unit'; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:options; 182, call; 182, 183; 182, 188; 183, attribute; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:prop; 186, identifier:_json_data; 187, identifier:get; 188, argument_list; 188, 189; 189, string:'options'; 190, comment; 191, if_statement; 191, 192; 191, 197; 191, 232; 191, 233; 191, 324; 191, 325; 192, comparison_operator:==; 192, 193; 192, 194; 193, identifier:prop_type; 194, attribute; 194, 195; 194, 196; 195, identifier:PropertyType; 196, identifier:REFERENCES_VALUE; 197, block; 197, 198; 197, 210; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:referenced_part_ids; 201, list_comprehension; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:referenced_part; 204, identifier:id; 205, for_in_clause; 205, 206; 205, 207; 206, identifier:referenced_part; 207, attribute; 207, 208; 207, 209; 208, identifier:prop; 209, identifier:value; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:moved_prop; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:moved_part_model; 216, identifier:add_property; 217, argument_list; 217, 218; 217, 223; 217, 226; 217, 229; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:name; 220, attribute; 220, 221; 220, 222; 221, identifier:prop; 222, identifier:name; 223, keyword_argument; 223, 224; 223, 225; 224, identifier:description; 225, identifier:desc; 226, keyword_argument; 226, 227; 226, 228; 227, identifier:property_type; 228, identifier:prop_type; 229, keyword_argument; 229, 230; 229, 231; 230, identifier:default_value; 231, identifier:referenced_part_ids; 232, comment; 233, elif_clause; 233, 234; 233, 239; 234, comparison_operator:==; 234, 235; 234, 236; 235, identifier:prop_type; 236, attribute; 236, 237; 236, 238; 237, identifier:PropertyType; 238, identifier:ATTACHMENT_VALUE; 239, block; 239, 240; 239, 259; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:moved_prop; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:moved_part_model; 246, identifier:add_property; 247, argument_list; 247, 248; 247, 253; 247, 256; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:name; 250, attribute; 250, 251; 250, 252; 251, identifier:prop; 252, identifier:name; 253, keyword_argument; 253, 254; 253, 255; 254, identifier:description; 255, identifier:desc; 256, keyword_argument; 256, 257; 256, 258; 257, identifier:property_type; 258, identifier:prop_type; 259, if_statement; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:prop; 262, identifier:value; 263, block; 263, 264; 263, 280; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:attachment_name; 267, subscript; 267, 268; 267, 278; 268, call; 268, 269; 268, 276; 269, attribute; 269, 270; 269, 275; 270, subscript; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:prop; 273, identifier:_json_data; 274, string:'value'; 275, identifier:split; 276, argument_list; 276, 277; 277, string:'/'; 278, unary_operator:-; 278, 279; 279, integer:1; 280, with_statement; 280, 281; 280, 289; 281, with_clause; 281, 282; 282, with_item; 282, 283; 283, as_pattern; 283, 284; 283, 287; 284, call; 284, 285; 284, 286; 285, identifier:temp_chdir; 286, argument_list; 287, as_pattern_target; 287, 288; 288, identifier:target_dir; 289, block; 289, 290; 289, 308; 289, 317; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:full_path; 293, call; 293, 294; 293, 299; 294, attribute; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:os; 297, identifier:path; 298, identifier:join; 299, argument_list; 299, 300; 299, 307; 300, boolean_operator:or; 300, 301; 300, 302; 301, identifier:target_dir; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:os; 305, identifier:getcwd; 306, argument_list; 307, identifier:attachment_name; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:prop; 312, identifier:save_as; 313, argument_list; 313, 314; 314, keyword_argument; 314, 315; 314, 316; 315, identifier:filename; 316, identifier:full_path; 317, expression_statement; 317, 318; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:moved_prop; 321, identifier:upload; 322, argument_list; 322, 323; 323, identifier:full_path; 324, comment; 325, else_clause; 325, 326; 326, block; 326, 327; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:moved_prop; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:moved_part_model; 333, identifier:add_property; 334, argument_list; 334, 335; 334, 340; 334, 343; 334, 346; 334, 351; 334, 354; 335, keyword_argument; 335, 336; 335, 337; 336, identifier:name; 337, attribute; 337, 338; 337, 339; 338, identifier:prop; 339, identifier:name; 340, keyword_argument; 340, 341; 340, 342; 341, identifier:description; 342, identifier:desc; 343, keyword_argument; 343, 344; 343, 345; 344, identifier:property_type; 345, identifier:prop_type; 346, keyword_argument; 346, 347; 346, 348; 347, identifier:default_value; 348, attribute; 348, 349; 348, 350; 349, identifier:prop; 350, identifier:value; 351, keyword_argument; 351, 352; 351, 353; 352, identifier:unit; 353, identifier:unit; 354, keyword_argument; 354, 355; 354, 356; 355, identifier:options; 356, identifier:options; 357, comment; 358, expression_statement; 358, 359; 359, assignment; 359, 360; 359, 367; 360, subscript; 360, 361; 360, 364; 361, call; 361, 362; 361, 363; 362, identifier:get_mapping_dictionary; 363, argument_list; 364, attribute; 364, 365; 364, 366; 365, identifier:prop; 366, identifier:id; 367, identifier:moved_prop; 368, comment; 369, if_statement; 369, 370; 369, 371; 369, 372; 370, identifier:include_children; 371, comment; 372, block; 372, 373; 372, 379; 372, 380; 373, expression_statement; 373, 374; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:part; 377, identifier:populate_descendants; 378, argument_list; 379, comment; 380, for_statement; 380, 381; 380, 382; 380, 385; 381, identifier:sub_part; 382, attribute; 382, 383; 382, 384; 383, identifier:part; 384, identifier:_cached_children; 385, block; 385, 386; 386, expression_statement; 386, 387; 387, call; 387, 388; 387, 389; 388, identifier:relocate_model; 389, argument_list; 389, 390; 389, 393; 389, 396; 389, 401; 390, keyword_argument; 390, 391; 390, 392; 391, identifier:part; 392, identifier:sub_part; 393, keyword_argument; 393, 394; 393, 395; 394, identifier:target_parent; 395, identifier:moved_part_model; 396, keyword_argument; 396, 397; 396, 398; 397, identifier:name; 398, attribute; 398, 399; 398, 400; 399, identifier:sub_part; 400, identifier:name; 401, keyword_argument; 401, 402; 401, 403; 402, identifier:include_children; 403, identifier:include_children; 404, return_statement; 404, 405; 405, identifier:moved_part_model | def relocate_model(part, target_parent, name=None, include_children=True):
"""
Move the `Part` model to target parent.
.. versionadded:: 2.3
:param part: `Part` object to be moved
:type part: :class:`Part`
:param target_parent: `Part` object under which the desired `Part` is moved
:type target_parent: :class:`Part`
:param name: how the moved top-level `Part` should be called
:type name: basestring
:param include_children: True to move also the descendants of `Part`. If False, the children will be lost.
:type include_children: bool
:return: moved :class: Part model.
:raises IllegalArgumentError: if target_parent is descendant of part
"""
if target_parent.id in get_illegal_targets(part, include={part.id}):
raise IllegalArgumentError('cannot relocate part "{}" under target parent "{}", because the target is part of '
'its descendants'.format(part.name, target_parent.name))
# First, if the user doesn't provide the name, then just use the default "Clone - ..." name
if not name:
name = "CLONE - {}".format(part.name)
# The description cannot be added when creating a model, so edit the model after creation.
part_desc = part._json_data['description']
moved_part_model = target_parent.add_model(name=name, multiplicity=part.multiplicity)
if part_desc:
moved_part_model.edit(description=str(part_desc))
# Map the current part model id with newly created part model Object
get_mapping_dictionary().update({part.id: moved_part_model})
# Loop through properties and retrieve their type, description and unit
list_of_properties_sorted_by_order = part.properties
list_of_properties_sorted_by_order.sort(key=lambda x: x._json_data['order'])
for prop in list_of_properties_sorted_by_order:
prop_type = prop._json_data.get('property_type')
desc = prop._json_data.get('description')
unit = prop._json_data.get('unit')
options = prop._json_data.get('options')
# On "Part references" properties, the models referenced also need to be added
if prop_type == PropertyType.REFERENCES_VALUE:
referenced_part_ids = [referenced_part.id for referenced_part in prop.value]
moved_prop = moved_part_model.add_property(name=prop.name, description=desc, property_type=prop_type,
default_value=referenced_part_ids)
# On "Attachment" properties, attachments needs to be downloaded and re-uploaded to the new property.
elif prop_type == PropertyType.ATTACHMENT_VALUE:
moved_prop = moved_part_model.add_property(name=prop.name, description=desc, property_type=prop_type)
if prop.value:
attachment_name = prop._json_data['value'].split('/')[-1]
with temp_chdir() as target_dir:
full_path = os.path.join(target_dir or os.getcwd(), attachment_name)
prop.save_as(filename=full_path)
moved_prop.upload(full_path)
# Other properties are quite straightforward
else:
moved_prop = moved_part_model.add_property(name=prop.name, description=desc, property_type=prop_type,
default_value=prop.value, unit=unit, options=options)
# Map the current property model id with newly created property model Object
get_mapping_dictionary()[prop.id] = moved_prop
# Now copy the sub-tree of the part
if include_children:
# Populate the part so multiple children retrieval is not needed
part.populate_descendants()
# For each part, recursively run this function
for sub_part in part._cached_children:
relocate_model(part=sub_part, target_parent=moved_part_model, name=sub_part.name,
include_children=include_children)
return moved_part_model |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:relocate_instance; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:part; 5, identifier:target_parent; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:include_children; 11, True; 12, block; 12, 13; 12, 15; 12, 16; 12, 31; 12, 32; 12, 40; 12, 48; 12, 49; 12, 67; 12, 68; 12, 69; 12, 78; 12, 79; 12, 100; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, if_statement; 16, 17; 16, 19; 17, not_operator; 17, 18; 18, identifier:name; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:name; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, string:"CLONE - {}"; 26, identifier:format; 27, argument_list; 27, 28; 28, attribute; 28, 29; 28, 30; 29, identifier:part; 30, identifier:name; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:part_model; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:part; 38, identifier:model; 39, argument_list; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:target_parent_model; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:target_parent; 46, identifier:model; 47, argument_list; 48, comment; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:relocate_model; 52, argument_list; 52, 53; 52, 56; 52, 59; 52, 64; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:part; 55, identifier:part_model; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:target_parent; 58, identifier:target_parent_model; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:name; 61, attribute; 61, 62; 61, 63; 62, identifier:part_model; 63, identifier:name; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:include_children; 66, identifier:include_children; 67, comment; 68, comment; 69, if_statement; 69, 70; 69, 71; 70, identifier:include_children; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:part; 76, identifier:populate_descendants; 77, argument_list; 78, comment; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:moved_instance; 82, call; 82, 83; 82, 84; 83, identifier:move_part_instance; 84, argument_list; 84, 85; 84, 88; 84, 91; 84, 94; 84, 97; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:part_instance; 87, identifier:part; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:target_parent; 90, identifier:target_parent; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:part_model; 93, identifier:part_model; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:name; 96, identifier:name; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:include_children; 99, identifier:include_children; 100, return_statement; 100, 101; 101, identifier:moved_instance | def relocate_instance(part, target_parent, name=None, include_children=True):
"""
Move the `Part` instance to target parent.
.. versionadded:: 2.3
:param part: `Part` object to be moved
:type part: :class:`Part`
:param target_parent: `Part` object under which the desired `Part` is moved
:type target_parent: :class:`Part`
:param name: how the moved top-level `Part` should be called
:type name: basestring
:param include_children: True to move also the descendants of `Part`. If False, the children will be lost.
:type include_children: bool
:return: moved :class: `Part` instance
"""
# First, if the user doesn't provide the name, then just use the default "Clone - ..." name
if not name:
name = "CLONE - {}".format(part.name)
# Initially the model of the part needs to be recreated under the model of the target_parent. Retrieve them.
part_model = part.model()
target_parent_model = target_parent.model()
# Call the move_part() function for those models.
relocate_model(part=part_model, target_parent=target_parent_model, name=part_model.name,
include_children=include_children)
# Populate the descendants of the Part (category=Instance), in order to avoid to retrieve children for every
# level and save time. Only need it the children should be included.
if include_children:
part.populate_descendants()
# This function will move the part instance under the target_parent instance, and its children if required.
moved_instance = move_part_instance(part_instance=part, target_parent=target_parent, part_model=part_model,
name=name, include_children=include_children)
return moved_instance |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:move_part_instance; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:part_instance; 5, identifier:target_parent; 6, identifier:part_model; 7, default_parameter; 7, 8; 7, 9; 8, identifier:name; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:include_children; 12, True; 13, block; 13, 14; 13, 16; 13, 17; 13, 27; 13, 28; 13, 38; 13, 39; 13, 234; 13, 235; 13, 236; 13, 270; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, if_statement; 17, 18; 17, 20; 18, not_operator; 18, 19; 19, identifier:name; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:name; 24, attribute; 24, 25; 24, 26; 25, identifier:part_instance; 26, identifier:name; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:moved_model; 31, subscript; 31, 32; 31, 35; 32, call; 32, 33; 32, 34; 33, identifier:get_mapping_dictionary; 34, argument_list; 35, attribute; 35, 36; 35, 37; 36, identifier:part_model; 37, identifier:id; 38, comment; 39, if_statement; 39, 40; 39, 47; 39, 48; 39, 49; 39, 85; 39, 193; 40, comparison_operator:==; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:moved_model; 43, identifier:multiplicity; 44, attribute; 44, 45; 44, 46; 45, identifier:Multiplicity; 46, identifier:ONE; 47, comment; 48, comment; 49, block; 49, 50; 49, 65; 49, 71; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:moved_instance; 53, subscript; 53, 54; 53, 64; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:moved_model; 57, identifier:instances; 58, argument_list; 58, 59; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:parent_id; 61, attribute; 61, 62; 61, 63; 62, identifier:target_parent; 63, identifier:id; 64, integer:0; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:map_property_instances; 68, argument_list; 68, 69; 68, 70; 69, identifier:part_instance; 70, identifier:moved_instance; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:moved_instance; 74, call; 74, 75; 74, 76; 75, identifier:update_part_with_properties; 76, argument_list; 76, 77; 76, 78; 76, 79; 77, identifier:part_instance; 78, identifier:moved_instance; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:name; 81, call; 81, 82; 81, 83; 82, identifier:str; 83, argument_list; 83, 84; 84, identifier:name; 85, elif_clause; 85, 86; 85, 93; 85, 94; 85, 95; 85, 96; 86, comparison_operator:==; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:moved_model; 89, identifier:multiplicity; 90, attribute; 90, 91; 90, 92; 91, identifier:Multiplicity; 92, identifier:ONE_MANY; 93, comment; 94, comment; 95, comment; 96, block; 96, 97; 97, if_statement; 97, 98; 97, 105; 97, 152; 98, comparison_operator:not; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:target_parent; 101, identifier:id; 102, call; 102, 103; 102, 104; 103, identifier:get_edited_one_many; 104, argument_list; 105, block; 105, 106; 105, 121; 105, 127; 105, 141; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:moved_instance; 109, subscript; 109, 110; 109, 120; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:moved_model; 113, identifier:instances; 114, argument_list; 114, 115; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:parent_id; 117, attribute; 117, 118; 117, 119; 118, identifier:target_parent; 119, identifier:id; 120, integer:0; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:map_property_instances; 124, argument_list; 124, 125; 124, 126; 125, identifier:part_instance; 126, identifier:moved_instance; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:moved_instance; 130, call; 130, 131; 130, 132; 131, identifier:update_part_with_properties; 132, argument_list; 132, 133; 132, 134; 132, 135; 133, identifier:part_instance; 134, identifier:moved_instance; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:name; 137, call; 137, 138; 137, 139; 138, identifier:str; 139, argument_list; 139, 140; 140, identifier:name; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, call; 144, 145; 144, 146; 145, identifier:get_edited_one_many; 146, argument_list; 147, identifier:append; 148, argument_list; 148, 149; 149, attribute; 149, 150; 149, 151; 150, identifier:target_parent; 151, identifier:id; 152, else_clause; 152, 153; 153, block; 153, 154; 153, 173; 153, 179; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:moved_instance; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:target_parent; 160, identifier:add; 161, argument_list; 161, 162; 161, 167; 161, 170; 162, keyword_argument; 162, 163; 162, 164; 163, identifier:name; 164, attribute; 164, 165; 164, 166; 165, identifier:part_instance; 166, identifier:name; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:model; 169, identifier:moved_model; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:suppress_kevents; 172, True; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 176; 175, identifier:map_property_instances; 176, argument_list; 176, 177; 176, 178; 177, identifier:part_instance; 178, identifier:moved_instance; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:moved_instance; 182, call; 182, 183; 182, 184; 183, identifier:update_part_with_properties; 184, argument_list; 184, 185; 184, 186; 184, 187; 185, identifier:part_instance; 186, identifier:moved_instance; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:name; 189, call; 189, 190; 189, 191; 190, identifier:str; 191, argument_list; 191, 192; 192, identifier:name; 193, else_clause; 193, 194; 193, 195; 193, 196; 194, comment; 195, comment; 196, block; 196, 197; 196, 214; 196, 220; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:moved_instance; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:target_parent; 203, identifier:add; 204, argument_list; 204, 205; 204, 208; 204, 211; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:name; 207, identifier:name; 208, keyword_argument; 208, 209; 208, 210; 209, identifier:model; 210, identifier:moved_model; 211, keyword_argument; 211, 212; 211, 213; 212, identifier:suppress_kevents; 213, True; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 217; 216, identifier:map_property_instances; 217, argument_list; 217, 218; 217, 219; 218, identifier:part_instance; 219, identifier:moved_instance; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:moved_instance; 223, call; 223, 224; 223, 225; 224, identifier:update_part_with_properties; 225, argument_list; 225, 226; 225, 227; 225, 228; 226, identifier:part_instance; 227, identifier:moved_instance; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:name; 230, call; 230, 231; 230, 232; 231, identifier:str; 232, argument_list; 232, 233; 233, identifier:name; 234, comment; 235, comment; 236, if_statement; 236, 237; 236, 238; 237, identifier:include_children; 238, block; 238, 239; 239, for_statement; 239, 240; 239, 241; 239, 244; 240, identifier:sub_instance; 241, attribute; 241, 242; 241, 243; 242, identifier:part_instance; 243, identifier:_cached_children; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 248; 247, identifier:move_part_instance; 248, argument_list; 248, 249; 248, 252; 248, 255; 248, 262; 248, 267; 249, keyword_argument; 249, 250; 249, 251; 250, identifier:part_instance; 251, identifier:sub_instance; 252, keyword_argument; 252, 253; 252, 254; 253, identifier:target_parent; 254, identifier:moved_instance; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:part_model; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:sub_instance; 260, identifier:model; 261, argument_list; 262, keyword_argument; 262, 263; 262, 264; 263, identifier:name; 264, attribute; 264, 265; 264, 266; 265, identifier:sub_instance; 266, identifier:name; 267, keyword_argument; 267, 268; 267, 269; 268, identifier:include_children; 269, True; 270, return_statement; 270, 271; 271, identifier:moved_instance | def move_part_instance(part_instance, target_parent, part_model, name=None, include_children=True):
"""
Move the `Part` instance to target parent and updates the properties based on the original part instance.
.. versionadded:: 2.3
:param part_instance: `Part` object to be moved
:type part_instance: :class:`Part`
:param part_model: `Part` object representing the model of part_instance
:type part_model: :class: `Part`
:param target_parent: `Part` object under which the desired `Part` is moved
:type target_parent: :class:`Part`
:param name: how the moved top-level `Part` should be called
:type name: basestring
:param include_children: True to move also the descendants of `Part`. If False, the children will be lost.
:type include_children: bool
:return: moved :class: `Part` instance
"""
# If no specific name has been required, then call in as Clone of the part_instance.
if not name:
name = part_instance.name
# Retrieve the model of the future part to be created
moved_model = get_mapping_dictionary()[part_model.id]
# Now act based on multiplicity
if moved_model.multiplicity == Multiplicity.ONE:
# If multiplicity is 'Exactly 1', that means the instance was automatically created with the model, so just
# retrieve it, map the original instance with the moved one and update the name and property values.
moved_instance = moved_model.instances(parent_id=target_parent.id)[0]
map_property_instances(part_instance, moved_instance)
moved_instance = update_part_with_properties(part_instance, moved_instance, name=str(name))
elif moved_model.multiplicity == Multiplicity.ONE_MANY:
# If multiplicity is '1 or more', that means one instance has automatically been created with the model, so
# retrieve it, map the original instance with the moved one and update the name and property values. Store
# the model in a list, in case there are multiple instance those need to be recreated.
if target_parent.id not in get_edited_one_many():
moved_instance = moved_model.instances(parent_id=target_parent.id)[0]
map_property_instances(part_instance, moved_instance)
moved_instance = update_part_with_properties(part_instance, moved_instance, name=str(name))
get_edited_one_many().append(target_parent.id)
else:
moved_instance = target_parent.add(name=part_instance.name, model=moved_model, suppress_kevents=True)
map_property_instances(part_instance, moved_instance)
moved_instance = update_part_with_properties(part_instance, moved_instance, name=str(name))
else:
# If multiplicity is '0 or more' or '0 or 1', it means no instance has been created automatically with the
# model, so then everything must be created and then updated.
moved_instance = target_parent.add(name=name, model=moved_model, suppress_kevents=True)
map_property_instances(part_instance, moved_instance)
moved_instance = update_part_with_properties(part_instance, moved_instance, name=str(name))
# If include_children is True, then recursively call this function for every descendant. Keep the name of the
# original sub-instance.
if include_children:
for sub_instance in part_instance._cached_children:
move_part_instance(part_instance=sub_instance, target_parent=moved_instance,
part_model=sub_instance.model(),
name=sub_instance.name, include_children=True)
return moved_instance |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:update_part_with_properties; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:part_instance; 5, identifier:moved_instance; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name; 8, None; 9, block; 9, 10; 9, 12; 9, 13; 9, 14; 9, 20; 9, 187; 9, 188; 9, 209; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:properties_id_dict; 17, call; 17, 18; 17, 19; 18, identifier:dict; 19, argument_list; 20, for_statement; 20, 21; 20, 22; 20, 25; 20, 26; 21, identifier:prop_instance; 22, attribute; 22, 23; 22, 24; 23, identifier:part_instance; 24, identifier:properties; 25, comment; 26, block; 26, 27; 27, if_statement; 27, 28; 27, 37; 27, 121; 27, 122; 27, 123; 27, 165; 28, comparison_operator:==; 28, 29; 28, 34; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:prop_instance; 32, identifier:_json_data; 33, string:'property_type'; 34, attribute; 34, 35; 34, 36; 35, identifier:PropertyType; 36, identifier:ATTACHMENT_VALUE; 37, block; 37, 38; 37, 48; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:moved_prop; 41, subscript; 41, 42; 41, 45; 42, call; 42, 43; 42, 44; 43, identifier:get_mapping_dictionary; 44, argument_list; 45, attribute; 45, 46; 45, 47; 46, identifier:prop_instance; 47, identifier:id; 48, if_statement; 48, 49; 48, 52; 48, 113; 49, attribute; 49, 50; 49, 51; 50, identifier:prop_instance; 51, identifier:value; 52, block; 52, 53; 52, 69; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:attachment_name; 56, subscript; 56, 57; 56, 67; 57, call; 57, 58; 57, 65; 58, attribute; 58, 59; 58, 64; 59, subscript; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:prop_instance; 62, identifier:_json_data; 63, string:'value'; 64, identifier:split; 65, argument_list; 65, 66; 66, string:'/'; 67, unary_operator:-; 67, 68; 68, integer:1; 69, with_statement; 69, 70; 69, 78; 70, with_clause; 70, 71; 71, with_item; 71, 72; 72, as_pattern; 72, 73; 72, 76; 73, call; 73, 74; 73, 75; 74, identifier:temp_chdir; 75, argument_list; 76, as_pattern_target; 76, 77; 77, identifier:target_dir; 78, block; 78, 79; 78, 97; 78, 106; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:full_path; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:os; 86, identifier:path; 87, identifier:join; 88, argument_list; 88, 89; 88, 96; 89, boolean_operator:or; 89, 90; 89, 91; 90, identifier:target_dir; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:os; 94, identifier:getcwd; 95, argument_list; 96, identifier:attachment_name; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:prop_instance; 101, identifier:save_as; 102, argument_list; 102, 103; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:filename; 105, identifier:full_path; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:moved_prop; 110, identifier:upload; 111, argument_list; 111, 112; 112, identifier:full_path; 113, else_clause; 113, 114; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:moved_prop; 119, identifier:clear; 120, argument_list; 121, comment; 122, comment; 123, elif_clause; 123, 124; 123, 133; 124, comparison_operator:==; 124, 125; 124, 130; 125, subscript; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:prop_instance; 128, identifier:_json_data; 129, string:'property_type'; 130, attribute; 130, 131; 130, 132; 131, identifier:PropertyType; 132, identifier:REFERENCES_VALUE; 133, block; 133, 134; 134, if_statement; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:prop_instance; 137, identifier:value; 138, block; 138, 139; 138, 149; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:moved_prop_instance; 142, subscript; 142, 143; 142, 146; 143, call; 143, 144; 143, 145; 144, identifier:get_mapping_dictionary; 145, argument_list; 146, attribute; 146, 147; 146, 148; 147, identifier:prop_instance; 148, identifier:id; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 156; 151, subscript; 151, 152; 151, 153; 152, identifier:properties_id_dict; 153, attribute; 153, 154; 153, 155; 154, identifier:moved_prop_instance; 155, identifier:id; 156, list_comprehension; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:ref_part; 159, identifier:id; 160, for_in_clause; 160, 161; 160, 162; 161, identifier:ref_part; 162, attribute; 162, 163; 162, 164; 163, identifier:prop_instance; 164, identifier:value; 165, else_clause; 165, 166; 166, block; 166, 167; 166, 177; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:moved_prop_instance; 170, subscript; 170, 171; 170, 174; 171, call; 171, 172; 171, 173; 172, identifier:get_mapping_dictionary; 173, argument_list; 174, attribute; 174, 175; 174, 176; 175, identifier:prop_instance; 176, identifier:id; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 184; 179, subscript; 179, 180; 179, 181; 180, identifier:properties_id_dict; 181, attribute; 181, 182; 181, 183; 182, identifier:moved_prop_instance; 183, identifier:id; 184, attribute; 184, 185; 184, 186; 185, identifier:prop_instance; 186, identifier:value; 187, comment; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:moved_instance; 192, identifier:update; 193, argument_list; 193, 194; 193, 200; 193, 203; 193, 206; 194, keyword_argument; 194, 195; 194, 196; 195, identifier:name; 196, call; 196, 197; 196, 198; 197, identifier:str; 198, argument_list; 198, 199; 199, identifier:name; 200, keyword_argument; 200, 201; 200, 202; 201, identifier:update_dict; 202, identifier:properties_id_dict; 203, keyword_argument; 203, 204; 203, 205; 204, identifier:bulk; 205, True; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:suppress_kevents; 208, True; 209, return_statement; 209, 210; 210, identifier:moved_instance | def update_part_with_properties(part_instance, moved_instance, name=None):
"""
Update the newly created part and its properties based on the original one.
:param part_instance: `Part` object to be copied
:type part_instance: :class:`Part`
:param moved_instance: `Part` object copied
:type moved_instance: :class:`Part`
:param name: Name of the updated part
:type name: basestring
:return: moved :class: `Part` instance
"""
# Instantiate and empty dictionary later used to map {property.id: property.value} in order to update the part
# in one go
properties_id_dict = dict()
for prop_instance in part_instance.properties:
# Do different magic if there is an attachment property and it has a value
if prop_instance._json_data['property_type'] == PropertyType.ATTACHMENT_VALUE:
moved_prop = get_mapping_dictionary()[prop_instance.id]
if prop_instance.value:
attachment_name = prop_instance._json_data['value'].split('/')[-1]
with temp_chdir() as target_dir:
full_path = os.path.join(target_dir or os.getcwd(), attachment_name)
prop_instance.save_as(filename=full_path)
moved_prop.upload(full_path)
else:
moved_prop.clear()
# For a reference value property, add the id's of the part referenced {property.id: [part1.id, part2.id, ...]},
# if there is part referenced at all.
elif prop_instance._json_data['property_type'] == PropertyType.REFERENCES_VALUE:
if prop_instance.value:
moved_prop_instance = get_mapping_dictionary()[prop_instance.id]
properties_id_dict[moved_prop_instance.id] = [ref_part.id for ref_part in prop_instance.value]
else:
moved_prop_instance = get_mapping_dictionary()[prop_instance.id]
properties_id_dict[moved_prop_instance.id] = prop_instance.value
# Update the name and property values in one go.
moved_instance.update(name=str(name), update_dict=properties_id_dict, bulk=True, suppress_kevents=True)
return moved_instance |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:_checkup; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:peaks; 5, identifier:ecg_integrated; 6, identifier:sample_rate; 7, identifier:rr_buffer; 8, identifier:spk1; 9, identifier:npk1; 10, identifier:threshold; 11, block; 11, 12; 11, 14; 11, 24; 11, 28; 11, 181; 11, 190; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:peaks_amp; 17, list_comprehension; 17, 18; 17, 21; 18, subscript; 18, 19; 18, 20; 19, identifier:ecg_integrated; 20, identifier:peak; 21, for_in_clause; 21, 22; 21, 23; 22, identifier:peak; 23, identifier:peaks; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:definitive_peaks; 27, list:[]; 28, for_statement; 28, 29; 28, 32; 28, 36; 29, pattern_list; 29, 30; 29, 31; 30, identifier:i; 31, identifier:peak; 32, call; 32, 33; 32, 34; 33, identifier:enumerate; 34, argument_list; 34, 35; 35, identifier:peaks; 36, block; 36, 37; 36, 43; 36, 44; 36, 45; 36, 173; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:amp; 40, subscript; 40, 41; 40, 42; 41, identifier:peaks_amp; 42, identifier:i; 43, comment; 44, comment; 45, if_statement; 45, 46; 45, 49; 45, 64; 45, 65; 45, 66; 45, 67; 45, 68; 45, 69; 45, 162; 45, 163; 46, comparison_operator:>; 46, 47; 46, 48; 47, identifier:amp; 48, identifier:threshold; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 56; 52, pattern_list; 52, 53; 52, 54; 52, 55; 53, identifier:definitive_peaks; 54, identifier:spk1; 55, identifier:rr_buffer; 56, call; 56, 57; 56, 58; 57, identifier:_acceptpeak; 58, argument_list; 58, 59; 58, 60; 58, 61; 58, 62; 58, 63; 59, identifier:peak; 60, identifier:amp; 61, identifier:definitive_peaks; 62, identifier:spk1; 63, identifier:rr_buffer; 64, comment; 65, comment; 66, comment; 67, comment; 68, comment; 69, elif_clause; 69, 70; 69, 89; 70, boolean_operator:and; 70, 71; 70, 81; 71, boolean_operator:and; 71, 72; 71, 77; 72, comparison_operator:>; 72, 73; 72, 74; 73, identifier:amp; 74, binary_operator:/; 74, 75; 74, 76; 75, identifier:threshold; 76, integer:2; 77, call; 77, 78; 77, 79; 78, identifier:list; 79, argument_list; 79, 80; 80, identifier:definitive_peaks; 81, comparison_operator:>; 81, 82; 81, 86; 82, call; 82, 83; 82, 84; 83, identifier:len; 84, argument_list; 84, 85; 85, identifier:peaks; 86, binary_operator:+; 86, 87; 86, 88; 87, identifier:i; 88, integer:1; 89, block; 89, 90; 89, 99; 89, 114; 89, 127; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:mean_rr; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:numpy; 96, identifier:mean; 97, argument_list; 97, 98; 98, identifier:rr_buffer; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:last_qrs_ms; 102, binary_operator:*; 102, 103; 102, 110; 103, parenthesized_expression; 103, 104; 104, binary_operator:-; 104, 105; 104, 106; 105, identifier:peak; 106, subscript; 106, 107; 106, 108; 107, identifier:definitive_peaks; 108, unary_operator:-; 108, 109; 109, integer:1; 110, parenthesized_expression; 110, 111; 111, binary_operator:/; 111, 112; 111, 113; 112, integer:1000; 113, identifier:sample_rate; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:last_qrs_to_next_peak; 117, binary_operator:-; 117, 118; 117, 123; 118, subscript; 118, 119; 118, 120; 119, identifier:peaks; 120, binary_operator:+; 120, 121; 120, 122; 121, identifier:i; 122, integer:1; 123, subscript; 123, 124; 123, 125; 124, identifier:definitive_peaks; 125, unary_operator:-; 125, 126; 126, integer:1; 127, if_statement; 127, 128; 127, 137; 127, 152; 128, boolean_operator:and; 128, 129; 128, 132; 129, comparison_operator:>; 129, 130; 129, 131; 130, identifier:last_qrs_ms; 131, integer:360; 132, comparison_operator:>; 132, 133; 132, 134; 133, identifier:last_qrs_to_next_peak; 134, binary_operator:*; 134, 135; 134, 136; 135, float:1.5; 136, identifier:mean_rr; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 144; 140, pattern_list; 140, 141; 140, 142; 140, 143; 141, identifier:definitive_peaks; 142, identifier:spk1; 143, identifier:rr_buffer; 144, call; 144, 145; 144, 146; 145, identifier:_acceptpeak; 146, argument_list; 146, 147; 146, 148; 146, 149; 146, 150; 146, 151; 147, identifier:peak; 148, identifier:amp; 149, identifier:definitive_peaks; 150, identifier:spk1; 151, identifier:rr_buffer; 152, else_clause; 152, 153; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:npk1; 157, call; 157, 158; 157, 159; 158, identifier:_noisepeak; 159, argument_list; 159, 160; 159, 161; 160, identifier:amp; 161, identifier:npk1; 162, comment; 163, else_clause; 163, 164; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:npk1; 168, call; 168, 169; 168, 170; 169, identifier:_noisepeak; 170, argument_list; 170, 171; 170, 172; 171, identifier:amp; 172, identifier:npk1; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:threshold; 176, call; 176, 177; 176, 178; 177, identifier:_buffer_update; 178, argument_list; 178, 179; 178, 180; 179, identifier:npk1; 180, identifier:spk1; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:definitive_peaks; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:numpy; 187, identifier:array; 188, argument_list; 188, 189; 189, identifier:definitive_peaks; 190, return_statement; 190, 191; 191, identifier:definitive_peaks | def _checkup(peaks, ecg_integrated, sample_rate, rr_buffer, spk1, npk1, threshold):
"""
Check each peak according to thresholds
----------
Parameters
----------
peaks : list
List of local maximums that pass the first stage of conditions needed to be considered as
an R peak.
ecg_integrated : ndarray
Array that contains the samples of the integrated signal.
sample_rate : int
Sampling rate at which the acquisition took place.
rr_buffer : list
Data structure that stores the duration of the last eight RR intervals.
spk1 : float
Actual value of SPK1 parameter defined in Pan-Tompkins real-time R peak detection algorithm
(named signal peak).
npk1 : int
Actual value of NPK1 parameter defined in Pan-Tompkins real-time R peak detection algorithm
(named noise peak).
threshold : float
Initial value of the adaptive threshold level (relevant parameter for the application of
specific criteria during the identification of R peaks).
Returns
-------
out : list
List with the position of the peaks considered as R peak by the algorithm.
"""
peaks_amp = [ecg_integrated[peak] for peak in peaks]
definitive_peaks = []
for i, peak in enumerate(peaks):
amp = peaks_amp[i]
# accept if larger than threshold and slope in raw signal
# is +-30% of previous slopes
if amp > threshold:
definitive_peaks, spk1, rr_buffer = _acceptpeak(peak, amp, definitive_peaks, spk1,
rr_buffer)
# accept as qrs if higher than half threshold,
# but is 360 ms after last qrs and next peak
# is more than 1.5 rr intervals away
# just abandon it if there is no peak before
# or after
elif amp > threshold / 2 and list(definitive_peaks) and len(peaks) > i + 1:
mean_rr = numpy.mean(rr_buffer)
last_qrs_ms = (peak - definitive_peaks[-1]) * (1000 / sample_rate)
last_qrs_to_next_peak = peaks[i+1] - definitive_peaks[-1]
if last_qrs_ms > 360 and last_qrs_to_next_peak > 1.5 * mean_rr:
definitive_peaks, spk1, rr_buffer = _acceptpeak(peak, amp, definitive_peaks, spk1,
rr_buffer)
else:
npk1 = _noisepeak(amp, npk1)
# if not either of these it is noise
else:
npk1 = _noisepeak(amp, npk1)
threshold = _buffer_update(npk1, spk1)
definitive_peaks = numpy.array(definitive_peaks)
return definitive_peaks |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:create_napp; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, default_parameter; 5, 6; 5, 7; 6, identifier:meta_package; 7, False; 8, block; 8, 9; 8, 11; 8, 17; 8, 29; 8, 33; 8, 37; 8, 42; 8, 47; 8, 52; 8, 57; 8, 62; 8, 67; 8, 72; 8, 77; 8, 82; 8, 87; 8, 103; 8, 119; 8, 128; 8, 138; 8, 151; 8, 152; 8, 162; 8, 163; 8, 190; 8, 205; 8, 206; 8, 215; 8, 222; 8, 229; 8, 246; 8, 300; 8, 314; 8, 318; 8, 326; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:templates_path; 14, binary_operator:/; 14, 15; 14, 16; 15, identifier:SKEL_PATH; 16, string:'napp-structure/username/napp'; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:ui_templates_path; 20, call; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:os; 24, identifier:path; 25, identifier:join; 26, argument_list; 26, 27; 26, 28; 27, identifier:templates_path; 28, string:'ui'; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:username; 32, None; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:napp_name; 36, None; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:print; 40, argument_list; 40, 41; 41, string:'--------------------------------------------------------------'; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:print; 45, argument_list; 45, 46; 46, string:'Welcome to the bootstrap process of your NApp.'; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:print; 50, argument_list; 50, 51; 51, string:'--------------------------------------------------------------'; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:print; 55, argument_list; 55, 56; 56, string:'In order to answer both the username and the napp name,'; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:print; 60, argument_list; 60, 61; 61, string:'You must follow this naming rules:'; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:print; 65, argument_list; 65, 66; 66, string:' - name starts with a letter'; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:print; 70, argument_list; 70, 71; 71, string:' - name contains only letters, numbers or underscores'; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:print; 75, argument_list; 75, 76; 76, string:' - at least three characters'; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:print; 80, argument_list; 80, 81; 81, string:'--------------------------------------------------------------'; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:print; 85, argument_list; 85, 86; 86, string:''; 87, while_statement; 87, 88; 87, 95; 88, not_operator; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:cls; 92, identifier:valid_name; 93, argument_list; 93, 94; 94, identifier:username; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:username; 99, call; 99, 100; 99, 101; 100, identifier:input; 101, argument_list; 101, 102; 102, string:'Please, insert your NApps Server username: '; 103, while_statement; 103, 104; 103, 111; 104, not_operator; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:cls; 108, identifier:valid_name; 109, argument_list; 109, 110; 110, identifier:napp_name; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:napp_name; 115, call; 115, 116; 115, 117; 116, identifier:input; 117, argument_list; 117, 118; 118, string:'Please, insert your NApp name: '; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:description; 122, call; 122, 123; 122, 124; 123, identifier:input; 124, argument_list; 124, 125; 125, concatenated_string; 125, 126; 125, 127; 126, string:'Please, insert a brief description for your'; 127, string:'NApp [optional]: '; 128, if_statement; 128, 129; 128, 131; 128, 132; 129, not_operator; 129, 130; 130, identifier:description; 131, comment; 132, block; 132, 133; 132, 137; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:description; 136, string:'# TODO: <<<< Insert your NApp description here >>>>'; 137, comment; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:context; 141, dictionary; 141, 142; 141, 145; 141, 148; 142, pair; 142, 143; 142, 144; 143, string:'username'; 144, identifier:username; 145, pair; 145, 146; 145, 147; 146, string:'napp'; 147, identifier:napp_name; 148, pair; 148, 149; 148, 150; 149, string:'description'; 150, identifier:description; 151, comment; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:os; 156, identifier:makedirs; 157, argument_list; 157, 158; 157, 159; 158, identifier:username; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:exist_ok; 161, True; 162, comment; 163, with_statement; 163, 164; 163, 182; 164, with_clause; 164, 165; 165, with_item; 165, 166; 166, as_pattern; 166, 167; 166, 180; 167, call; 167, 168; 167, 169; 168, identifier:open; 169, argument_list; 169, 170; 169, 179; 170, call; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:os; 174, identifier:path; 175, identifier:join; 176, argument_list; 176, 177; 176, 178; 177, identifier:username; 178, string:'__init__.py'; 179, string:'w'; 180, as_pattern_target; 180, 181; 181, identifier:init_file; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:init_file; 187, identifier:write; 188, argument_list; 188, 189; 189, comment; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:os; 194, identifier:makedirs; 195, argument_list; 195, 196; 196, call; 196, 197; 196, 202; 197, attribute; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:os; 200, identifier:path; 201, identifier:join; 202, argument_list; 202, 203; 202, 204; 203, identifier:username; 204, identifier:napp_name; 205, comment; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:templates; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:os; 212, identifier:listdir; 213, argument_list; 213, 214; 214, identifier:templates_path; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:templates; 219, identifier:remove; 220, argument_list; 220, 221; 221, string:'ui'; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:templates; 226, identifier:remove; 227, argument_list; 227, 228; 228, string:'openapi.yml.template'; 229, if_statement; 229, 230; 229, 231; 230, identifier:meta_package; 231, block; 231, 232; 231, 239; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:templates; 236, identifier:remove; 237, argument_list; 237, 238; 238, string:'main.py.template'; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:templates; 243, identifier:remove; 244, argument_list; 244, 245; 245, string:'settings.py.template'; 246, for_statement; 246, 247; 246, 248; 246, 249; 247, identifier:tmp; 248, identifier:templates; 249, block; 249, 250; 249, 270; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:fname; 253, call; 253, 254; 253, 259; 254, attribute; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:os; 257, identifier:path; 258, identifier:join; 259, argument_list; 259, 260; 259, 261; 259, 262; 260, identifier:username; 261, identifier:napp_name; 262, subscript; 262, 263; 262, 269; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:tmp; 266, identifier:rsplit; 267, argument_list; 267, 268; 268, string:'.template'; 269, integer:0; 270, with_statement; 270, 271; 270, 281; 271, with_clause; 271, 272; 272, with_item; 272, 273; 273, as_pattern; 273, 274; 273, 279; 274, call; 274, 275; 274, 276; 275, identifier:open; 276, argument_list; 276, 277; 276, 278; 277, identifier:fname; 278, string:'w'; 279, as_pattern_target; 279, 280; 280, identifier:file; 281, block; 281, 282; 281, 293; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:content; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:cls; 288, identifier:render_template; 289, argument_list; 289, 290; 289, 291; 289, 292; 290, identifier:templates_path; 291, identifier:tmp; 292, identifier:context; 293, expression_statement; 293, 294; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:file; 297, identifier:write; 298, argument_list; 298, 299; 299, identifier:content; 300, if_statement; 300, 301; 300, 303; 301, not_operator; 301, 302; 302, identifier:meta_package; 303, block; 303, 304; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:NAppsManager; 308, identifier:create_ui_structure; 309, argument_list; 309, 310; 309, 311; 309, 312; 309, 313; 310, identifier:username; 311, identifier:napp_name; 312, identifier:ui_templates_path; 313, identifier:context; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 317; 316, identifier:print; 317, argument_list; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 321; 320, identifier:print; 321, argument_list; 321, 322; 322, concatenated_string; 322, 323; 322, 324; 322, 325; 323, string:f'Congratulations! Your NApp has been bootstrapped!\nNow you '; 324, string:'can go to the directory {username}/{napp_name} and begin to '; 325, string:'code your NApp.'; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 329; 328, identifier:print; 329, argument_list; 329, 330; 330, string:'Have fun!' | def create_napp(cls, meta_package=False):
"""Bootstrap a basic NApp structure for you to develop your NApp.
This will create, on the current folder, a clean structure of a NAPP,
filling some contents on this structure.
"""
templates_path = SKEL_PATH / 'napp-structure/username/napp'
ui_templates_path = os.path.join(templates_path, 'ui')
username = None
napp_name = None
print('--------------------------------------------------------------')
print('Welcome to the bootstrap process of your NApp.')
print('--------------------------------------------------------------')
print('In order to answer both the username and the napp name,')
print('You must follow this naming rules:')
print(' - name starts with a letter')
print(' - name contains only letters, numbers or underscores')
print(' - at least three characters')
print('--------------------------------------------------------------')
print('')
while not cls.valid_name(username):
username = input('Please, insert your NApps Server username: ')
while not cls.valid_name(napp_name):
napp_name = input('Please, insert your NApp name: ')
description = input('Please, insert a brief description for your'
'NApp [optional]: ')
if not description:
# pylint: disable=fixme
description = '# TODO: <<<< Insert your NApp description here >>>>'
# pylint: enable=fixme
context = {'username': username, 'napp': napp_name,
'description': description}
#: Creating the directory structure (username/napp_name)
os.makedirs(username, exist_ok=True)
#: Creating ``__init__.py`` files
with open(os.path.join(username, '__init__.py'), 'w') as init_file:
init_file.write(f'"""Napps for the user {username}.""""')
os.makedirs(os.path.join(username, napp_name))
#: Creating the other files based on the templates
templates = os.listdir(templates_path)
templates.remove('ui')
templates.remove('openapi.yml.template')
if meta_package:
templates.remove('main.py.template')
templates.remove('settings.py.template')
for tmp in templates:
fname = os.path.join(username, napp_name,
tmp.rsplit('.template')[0])
with open(fname, 'w') as file:
content = cls.render_template(templates_path, tmp, context)
file.write(content)
if not meta_package:
NAppsManager.create_ui_structure(username, napp_name,
ui_templates_path, context)
print()
print(f'Congratulations! Your NApp has been bootstrapped!\nNow you '
'can go to the directory {username}/{napp_name} and begin to '
'code your NApp.')
print('Have fun!') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:match_app_version; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:app; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:label; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:version; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:default; 16, False; 17, block; 17, 18; 17, 20; 17, 96; 17, 105; 17, 116; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 32; 20, 89; 21, boolean_operator:or; 21, 22; 21, 24; 22, not_operator; 22, 23; 23, identifier:app; 24, boolean_operator:and; 24, 25; 24, 27; 25, not_operator; 25, 26; 26, identifier:label; 27, not_operator; 27, 28; 28, parenthesized_expression; 28, 29; 29, boolean_operator:and; 29, 30; 29, 31; 30, identifier:app; 31, identifier:label; 32, block; 32, 33; 32, 61; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:target_app; 36, list_comprehension; 36, 37; 36, 38; 36, 43; 37, identifier:a; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:a; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:app_versions; 43, if_clause; 43, 44; 44, boolean_operator:or; 44, 45; 44, 53; 45, comparison_operator:==; 45, 46; 45, 52; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:a; 49, identifier:get; 50, argument_list; 50, 51; 51, string:'app'; 52, identifier:app; 53, comparison_operator:==; 53, 54; 53, 60; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:a; 57, identifier:get; 58, argument_list; 58, 59; 59, string:'label'; 60, identifier:label; 61, if_statement; 61, 62; 61, 71; 61, 77; 62, boolean_operator:and; 62, 63; 62, 65; 63, not_operator; 63, 64; 64, identifier:target_app; 65, not_operator; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:isinstance; 68, argument_list; 68, 69; 68, 70; 69, identifier:default; 70, identifier:bool; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:NotFoundError; 75, argument_list; 75, 76; 76, string:"Could not find the app or label provided"; 77, elif_clause; 77, 78; 77, 86; 78, boolean_operator:and; 78, 79; 78, 81; 79, not_operator; 79, 80; 80, identifier:target_app; 81, call; 81, 82; 81, 83; 82, identifier:isinstance; 83, argument_list; 83, 84; 83, 85; 84, identifier:default; 85, identifier:bool; 86, block; 86, 87; 87, return_statement; 87, 88; 88, identifier:default; 89, else_clause; 89, 90; 90, block; 90, 91; 91, raise_statement; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:IllegalArgumentError; 94, argument_list; 94, 95; 95, string:"Please provide either app or label"; 96, if_statement; 96, 97; 96, 99; 97, not_operator; 97, 98; 98, identifier:version; 99, block; 99, 100; 100, raise_statement; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:IllegalArgumentError; 103, argument_list; 103, 104; 104, string:"Please provide semantic version string including operand eg: `>=1.0.0`"; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:app_version; 108, call; 108, 109; 108, 114; 109, attribute; 109, 110; 109, 113; 110, subscript; 110, 111; 110, 112; 111, identifier:target_app; 112, integer:0; 113, identifier:get; 114, argument_list; 114, 115; 115, string:'version'; 116, if_statement; 116, 117; 116, 122; 116, 134; 117, boolean_operator:and; 117, 118; 117, 121; 118, boolean_operator:and; 118, 119; 118, 120; 119, identifier:target_app; 120, identifier:app_version; 121, identifier:version; 122, block; 122, 123; 122, 126; 123, import_statement; 123, 124; 124, dotted_name; 124, 125; 125, identifier:semver; 126, return_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:semver; 130, identifier:match; 131, argument_list; 131, 132; 131, 133; 132, identifier:app_version; 133, identifier:version; 134, elif_clause; 134, 135; 134, 137; 135, not_operator; 135, 136; 136, identifier:app_version; 137, block; 137, 138; 138, if_statement; 138, 139; 138, 144; 138, 147; 139, call; 139, 140; 139, 141; 140, identifier:isinstance; 141, argument_list; 141, 142; 141, 143; 142, identifier:default; 143, identifier:bool; 144, block; 144, 145; 145, return_statement; 145, 146; 146, identifier:default; 147, else_clause; 147, 148; 148, block; 148, 149; 149, raise_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:NotFoundError; 152, argument_list; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, string:"No version found on the app '{}'"; 156, identifier:format; 157, argument_list; 157, 158; 158, call; 158, 159; 158, 164; 159, attribute; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:target_app; 162, integer:0; 163, identifier:get; 164, argument_list; 164, 165; 165, string:'app' | def match_app_version(self, app=None, label=None, version=None, default=False):
"""Match app version against a semantic version string.
Checks if a KE-chain app matches a version comparison. Uses the `semver` matcher to check.
`match("2.0.0", ">=1.0.0")` => `True`
`match("1.0.0", ">1.0.0")` => `False`
Examples
--------
>>> client.match_app_version(label='wim', version=">=1.99")
>>> True
>>> client.match_app_version(app='kechain2.core.pim', version=">=1.0.0")
>>> True
:param app: (optional) appname eg. 'kechain.core.wim'
:type app: basestring or None
:param label: (optional) app label (last part of the app name) eb 'wim'
:type label: basestring or None
:param version: semantic version string to match appname version against eg '2.0.0' or '>=2.0.0'
:type version: basestring
:param default: (optional) boolean to return if the version of the app is not set but the app found.
Set to None to return a NotFoundError when a version if not found in the app.
:type default: bool or None
:return: True if the version of the app matches against the match_version, otherwise False
:raises IllegalArgumentError: if no app nor a label is provided
:raises NotFoundError: if the app is not found
:raises ValueError: if the version provided is not parseable by semver,
should contain (<operand><major>.<minor>.<patch) where <operand> is '>,<,>=,<=,=='
"""
if not app or not label and not (app and label):
target_app = [a for a in self.app_versions if a.get('app') == app or a.get('label') == label]
if not target_app and not isinstance(default, bool):
raise NotFoundError("Could not find the app or label provided")
elif not target_app and isinstance(default, bool):
return default
else:
raise IllegalArgumentError("Please provide either app or label")
if not version:
raise IllegalArgumentError("Please provide semantic version string including operand eg: `>=1.0.0`")
app_version = target_app[0].get('version')
if target_app and app_version and version:
import semver
return semver.match(app_version, version)
elif not app_version:
if isinstance(default, bool):
return default
else:
raise NotFoundError("No version found on the app '{}'".format(target_app[0].get('app'))) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 1, 17; 2, function_name:activities; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:name; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:pk; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:scope; 13, None; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kwargs; 16, comment; 17, block; 17, 18; 17, 20; 17, 33; 17, 34; 17, 35; 17, 60; 17, 71; 17, 89; 17, 114; 17, 122; 17, 123; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:request_params; 23, dictionary; 23, 24; 23, 27; 23, 30; 24, pair; 24, 25; 24, 26; 25, string:'id'; 26, identifier:pk; 27, pair; 27, 28; 27, 29; 28, string:'name'; 29, identifier:name; 30, pair; 30, 31; 30, 32; 31, string:'scope'; 32, identifier:scope; 33, comment; 34, comment; 35, if_statement; 35, 36; 35, 50; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:match_app_version; 40, argument_list; 40, 41; 40, 44; 40, 47; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:label; 43, string:'wim'; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:version; 46, string:'>=2.0.0'; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:default; 49, False; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:request_params; 55, identifier:update; 56, argument_list; 56, 57; 57, subscript; 57, 58; 57, 59; 58, identifier:API_EXTRA_PARAMS; 59, string:'activity'; 60, if_statement; 60, 61; 60, 62; 61, identifier:kwargs; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:request_params; 67, identifier:update; 68, argument_list; 68, 69; 69, dictionary_splat; 69, 70; 70, identifier:kwargs; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:response; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:_request; 78, argument_list; 78, 79; 78, 80; 78, 86; 79, string:'GET'; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_build_url; 84, argument_list; 84, 85; 85, string:'activities'; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:params; 88, identifier:request_params; 89, if_statement; 89, 90; 89, 99; 89, 100; 90, comparison_operator:!=; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:response; 93, identifier:status_code; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:requests; 97, identifier:codes; 98, identifier:ok; 99, comment; 100, block; 100, 101; 101, raise_statement; 101, 102; 102, call; 102, 103; 102, 104; 103, identifier:NotFoundError; 104, argument_list; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, string:"Could not retrieve activities. Server responded with {}"; 108, identifier:format; 109, argument_list; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:str; 112, argument_list; 112, 113; 113, identifier:response; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:data; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:response; 120, identifier:json; 121, argument_list; 122, comment; 123, if_statement; 123, 124; 123, 138; 123, 139; 123, 154; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:match_app_version; 128, argument_list; 128, 129; 128, 132; 128, 135; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:label; 131, string:'wim'; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:version; 134, string:'<2.0.0'; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:default; 137, True; 138, comment; 139, block; 139, 140; 140, return_statement; 140, 141; 141, list_comprehension; 141, 142; 141, 149; 142, call; 142, 143; 142, 144; 143, identifier:Activity; 144, argument_list; 144, 145; 144, 146; 145, identifier:a; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:client; 148, identifier:self; 149, for_in_clause; 149, 150; 149, 151; 150, identifier:a; 151, subscript; 151, 152; 151, 153; 152, identifier:data; 153, string:'results'; 154, else_clause; 154, 155; 154, 156; 155, comment; 156, block; 156, 157; 157, return_statement; 157, 158; 158, list_comprehension; 158, 159; 158, 166; 159, call; 159, 160; 159, 161; 160, identifier:Activity2; 161, argument_list; 161, 162; 161, 163; 162, identifier:a; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:client; 165, identifier:self; 166, for_in_clause; 166, 167; 166, 168; 167, identifier:a; 168, subscript; 168, 169; 168, 170; 169, identifier:data; 170, string:'results' | def activities(self, name=None, pk=None, scope=None, **kwargs):
# type: (Optional[str], Optional[str], Optional[str], **Any) -> List[Activity]
"""Search for activities with optional name, pk and scope filter.
If additional `keyword=value` arguments are provided, these are added to the request parameters. Please
refer to the documentation of the KE-chain API for additional query parameters.
:param pk: id (primary key) of the activity to retrieve
:type pk: basestring or None
:param name: filter the activities by name
:type name: basestring or None
:param scope: filter by scope id
:type scope: basestring or None
:return: list of :class:`models.Activity`
:raises NotFoundError: If no `Activities` are found
"""
request_params = {
'id': pk,
'name': name,
'scope': scope
}
# update the fields query params
# for 'kechain.core.wim >= 2.0.0' add additional API params
if self.match_app_version(label='wim', version='>=2.0.0', default=False):
request_params.update(API_EXTRA_PARAMS['activity'])
if kwargs:
request_params.update(**kwargs)
response = self._request('GET', self._build_url('activities'), params=request_params)
if response.status_code != requests.codes.ok: # pragma: no cover
raise NotFoundError("Could not retrieve activities. Server responded with {}".format(str(response)))
data = response.json()
# for 'kechain.core.wim >= 2.0.0' we return Activity2, otherwise Activity1
if self.match_app_version(label='wim', version='<2.0.0', default=True):
# WIM1
return [Activity(a, client=self) for a in data['results']]
else:
# WIM2
return [Activity2(a, client=self) for a in data['results']] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 45; 1, 46; 2, function_name:parts; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 9; 3, 12; 3, 13; 3, 16; 3, 17; 3, 22; 3, 23; 3, 26; 3, 27; 3, 30; 3, 31; 3, 34; 3, 35; 3, 38; 3, 39; 3, 42; 3, 43; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:name; 7, None; 8, comment; 9, default_parameter; 9, 10; 9, 11; 10, identifier:pk; 11, None; 12, comment; 13, default_parameter; 13, 14; 13, 15; 14, identifier:model; 15, None; 16, comment; 17, default_parameter; 17, 18; 17, 19; 18, identifier:category; 19, attribute; 19, 20; 19, 21; 20, identifier:Category; 21, identifier:INSTANCE; 22, comment; 23, default_parameter; 23, 24; 23, 25; 24, identifier:bucket; 25, None; 26, comment; 27, default_parameter; 27, 28; 27, 29; 28, identifier:parent; 29, None; 30, comment; 31, default_parameter; 31, 32; 31, 33; 32, identifier:activity; 33, None; 34, comment; 35, default_parameter; 35, 36; 35, 37; 36, identifier:limit; 37, None; 38, comment; 39, default_parameter; 39, 40; 39, 41; 40, identifier:batch; 41, integer:100; 42, comment; 43, dictionary_splat_pattern; 43, 44; 44, identifier:kwargs; 45, comment; 46, block; 46, 47; 46, 49; 46, 50; 46, 61; 46, 94; 46, 105; 46, 123; 46, 140; 46, 148; 46, 154; 46, 210; 47, expression_statement; 47, 48; 48, comment; 49, comment; 50, if_statement; 50, 51; 50, 56; 51, boolean_operator:and; 51, 52; 51, 53; 52, identifier:limit; 53, comparison_operator:<; 53, 54; 53, 55; 54, identifier:limit; 55, identifier:batch; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:batch; 60, identifier:limit; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:request_params; 64, dictionary; 64, 65; 64, 68; 64, 71; 64, 79; 64, 82; 64, 85; 64, 88; 64, 91; 65, pair; 65, 66; 65, 67; 66, string:'id'; 67, identifier:pk; 68, pair; 68, 69; 68, 70; 69, string:'name'; 70, identifier:name; 71, pair; 71, 72; 71, 73; 72, string:'model'; 73, conditional_expression:if; 73, 74; 73, 77; 73, 78; 74, attribute; 74, 75; 74, 76; 75, identifier:model; 76, identifier:id; 77, identifier:model; 78, None; 79, pair; 79, 80; 79, 81; 80, string:'category'; 81, identifier:category; 82, pair; 82, 83; 82, 84; 83, string:'bucket'; 84, identifier:bucket; 85, pair; 85, 86; 85, 87; 86, string:'parent'; 87, identifier:parent; 88, pair; 88, 89; 88, 90; 89, string:'activity_id'; 90, identifier:activity; 91, pair; 91, 92; 91, 93; 92, string:'limit'; 93, identifier:batch; 94, if_statement; 94, 95; 94, 96; 95, identifier:kwargs; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:request_params; 101, identifier:update; 102, argument_list; 102, 103; 103, dictionary_splat; 103, 104; 104, identifier:kwargs; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:response; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_request; 112, argument_list; 112, 113; 112, 114; 112, 120; 113, string:'GET'; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_build_url; 118, argument_list; 118, 119; 119, string:'parts'; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:params; 122, identifier:request_params; 123, if_statement; 123, 124; 123, 133; 123, 134; 124, comparison_operator:!=; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:response; 127, identifier:status_code; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:requests; 131, identifier:codes; 132, identifier:ok; 133, comment; 134, block; 134, 135; 135, raise_statement; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:NotFoundError; 138, argument_list; 138, 139; 139, string:"Could not retrieve parts"; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:data; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:response; 146, identifier:json; 147, argument_list; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:part_results; 151, subscript; 151, 152; 151, 153; 152, identifier:data; 153, string:'results'; 154, if_statement; 154, 155; 154, 163; 155, boolean_operator:and; 155, 156; 155, 157; 156, identifier:batch; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:data; 160, identifier:get; 161, argument_list; 161, 162; 162, string:'next'; 163, block; 163, 164; 164, while_statement; 164, 165; 164, 168; 164, 169; 165, subscript; 165, 166; 165, 167; 166, identifier:data; 167, string:'next'; 168, comment; 169, block; 169, 170; 169, 181; 169, 193; 169, 201; 170, if_statement; 170, 171; 170, 179; 171, boolean_operator:and; 171, 172; 171, 173; 172, identifier:limit; 173, comparison_operator:>=; 173, 174; 173, 178; 174, call; 174, 175; 174, 176; 175, identifier:len; 176, argument_list; 176, 177; 177, identifier:part_results; 178, identifier:limit; 179, block; 179, 180; 180, break_statement; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:response; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:_request; 188, argument_list; 188, 189; 188, 190; 189, string:'GET'; 190, subscript; 190, 191; 190, 192; 191, identifier:data; 192, string:'next'; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:data; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:response; 199, identifier:json; 200, argument_list; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:part_results; 205, identifier:extend; 206, argument_list; 206, 207; 207, subscript; 207, 208; 207, 209; 208, identifier:data; 209, string:'results'; 210, return_statement; 210, 211; 211, call; 211, 212; 211, 213; 212, identifier:PartSet; 213, argument_list; 213, 214; 214, generator_expression; 214, 215; 214, 222; 215, call; 215, 216; 215, 217; 216, identifier:Part; 217, argument_list; 217, 218; 217, 219; 218, identifier:p; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:client; 221, identifier:self; 222, for_in_clause; 222, 223; 222, 224; 223, identifier:p; 224, identifier:part_results | def parts(self,
name=None, # type: Optional[str]
pk=None, # type: Optional[str]
model=None, # type: Optional[Part]
category=Category.INSTANCE, # type: Optional[str]
bucket=None, # type: Optional[str]
parent=None, # type: Optional[str]
activity=None, # type: Optional[str]
limit=None, # type: Optional[int]
batch=100, # type: int
**kwargs):
# type: (...) -> PartSet
"""Retrieve multiple KE-chain parts.
If no parameters are provided, all parts are retrieved.
If additional `keyword=value` arguments are provided, these are added to the request parameters. Please
refer to the documentation of the KE-chain API for additional query parameters.
:param name: filter on name
:type name: basestring or None
:param pk: filter on primary key
:type pk: basestring or None
:param model: filter on model_id
:type model: basestring or None
:param category: filter on category (INSTANCE, MODEL, None)
:type category: basestring or None
:param bucket: filter on bucket_id
:type bucket: basestring or None
:param parent: filter on the parent_id, returns all childrent of the parent_id
:type parent: basestring or None
:param activity: filter on activity_id
:type activity: basestring or None
:param limit: limit the return to # items (default unlimited, so return all results)
:type limit: int or None
:param batch: limit the batch size to # items (defaults to 100 items per batch)
:type batch: int or None
:param kwargs: additional `keyword=value` arguments for the api
:type kwargs: dict or None
:return: :class:`models.PartSet` which is an iterator of :class:`models.Part`
:raises NotFoundError: If no `Part` is found
Examples
--------
Return all parts (defaults to instances) with exact name 'Gears'.
>>> client = Client(url='https://default.localhost:9443', verify=False)
>>> client.login('admin','pass')
>>> client.parts(name='Gears') # doctest:Ellipsis
...
Return all parts with category is MODEL or category is INSTANCE.
>>> client.parts(name='Gears', category=None) # doctest:Ellipsis
...
Return a maximum of 5 parts
>>> client.parts(limit=5) # doctest:Ellipsis
...
"""
# if limit is provided and the batchsize is bigger than the limit, ensure that the batch size is maximised
if limit and limit < batch:
batch = limit
request_params = {
'id': pk,
'name': name,
'model': model.id if model else None,
'category': category,
'bucket': bucket,
'parent': parent,
'activity_id': activity,
'limit': batch
}
if kwargs:
request_params.update(**kwargs)
response = self._request('GET', self._build_url('parts'), params=request_params)
if response.status_code != requests.codes.ok: # pragma: no cover
raise NotFoundError("Could not retrieve parts")
data = response.json()
part_results = data['results']
if batch and data.get('next'):
while data['next']:
# respect the limit if set to > 0
if limit and len(part_results) >= limit:
break
response = self._request('GET', data['next'])
data = response.json()
part_results.extend(data['results'])
return PartSet((Part(p, client=self) for p in part_results)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:create_property; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 15; 3, 18; 3, 21; 4, identifier:self; 5, identifier:model; 6, identifier:name; 7, default_parameter; 7, 8; 7, 9; 8, identifier:description; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:property_type; 12, attribute; 12, 13; 12, 14; 13, identifier:PropertyType; 14, identifier:CHAR_VALUE; 15, default_parameter; 15, 16; 15, 17; 16, identifier:default_value; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:unit; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:options; 23, None; 24, block; 24, 25; 24, 27; 24, 41; 24, 73; 24, 94; 24, 95; 24, 96; 24, 124; 24, 161; 24, 162; 24, 163; 24, 164; 24, 165; 24, 183; 24, 199; 24, 219; 24, 228; 25, expression_statement; 25, 26; 26, comment; 27, if_statement; 27, 28; 27, 35; 28, comparison_operator:!=; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:model; 31, identifier:category; 32, attribute; 32, 33; 32, 34; 33, identifier:Category; 34, identifier:MODEL; 35, block; 35, 36; 36, raise_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:IllegalArgumentError; 39, argument_list; 39, 40; 40, string:"The model should be of category MODEL"; 41, if_statement; 41, 42; 41, 49; 42, not_operator; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:property_type; 46, identifier:endswith; 47, argument_list; 47, 48; 48, string:'_VALUE'; 49, block; 49, 50; 49, 60; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:warnings; 54, identifier:warn; 55, argument_list; 55, 56; 55, 59; 56, concatenated_string; 56, 57; 56, 58; 57, string:"Please use the `PropertyType` enumeration to ensure providing correct "; 58, string:"values to the backend."; 59, identifier:UserWarning; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:property_type; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, string:'{}_VALUE'; 66, identifier:format; 67, argument_list; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:property_type; 71, identifier:upper; 72, argument_list; 73, if_statement; 73, 74; 73, 81; 74, comparison_operator:not; 74, 75; 74, 76; 75, identifier:property_type; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:PropertyType; 79, identifier:values; 80, argument_list; 81, block; 81, 82; 82, raise_statement; 82, 83; 83, call; 83, 84; 83, 85; 84, identifier:IllegalArgumentError; 85, argument_list; 85, 86; 86, call; 86, 87; 86, 92; 87, attribute; 87, 88; 87, 91; 88, concatenated_string; 88, 89; 88, 90; 89, string:"Please provide a valid propertytype, please use one of `enums.PropertyType`. "; 90, string:"Got: '{}'"; 91, identifier:format; 92, argument_list; 92, 93; 93, identifier:property_type; 94, comment; 95, comment; 96, if_statement; 96, 97; 96, 117; 97, boolean_operator:and; 97, 98; 97, 116; 98, boolean_operator:and; 98, 99; 98, 108; 98, 109; 99, comparison_operator:in; 99, 100; 99, 101; 100, identifier:property_type; 101, tuple; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:PropertyType; 104, identifier:REFERENCE_VALUE; 105, attribute; 105, 106; 105, 107; 106, identifier:PropertyType; 107, identifier:REFERENCES_VALUE; 108, line_continuation:\; 109, call; 109, 110; 109, 111; 110, identifier:isinstance; 111, argument_list; 111, 112; 111, 113; 112, identifier:default_value; 113, tuple; 113, 114; 113, 115; 114, identifier:list; 115, identifier:tuple; 116, identifier:default_value; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:default_value; 121, subscript; 121, 122; 121, 123; 122, identifier:default_value; 123, integer:0; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:data; 127, dictionary; 127, 128; 127, 131; 127, 136; 127, 141; 127, 148; 127, 151; 127, 156; 128, pair; 128, 129; 128, 130; 129, string:"name"; 130, identifier:name; 131, pair; 131, 132; 131, 133; 132, string:"part"; 133, attribute; 133, 134; 133, 135; 134, identifier:model; 135, identifier:id; 136, pair; 136, 137; 136, 138; 137, string:"description"; 138, boolean_operator:or; 138, 139; 138, 140; 139, identifier:description; 140, string:''; 141, pair; 141, 142; 141, 143; 142, string:"property_type"; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:property_type; 146, identifier:upper; 147, argument_list; 148, pair; 148, 149; 148, 150; 149, string:"value"; 150, identifier:default_value; 151, pair; 151, 152; 151, 153; 152, string:"unit"; 153, boolean_operator:or; 153, 154; 153, 155; 154, identifier:unit; 155, string:''; 156, pair; 156, 157; 156, 158; 157, string:"options"; 158, boolean_operator:or; 158, 159; 158, 160; 159, identifier:options; 160, dictionary; 161, comment; 162, comment; 163, comment; 164, comment; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:response; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:_request; 172, argument_list; 172, 173; 172, 174; 172, 180; 173, string:'POST'; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:_build_url; 178, argument_list; 178, 179; 179, string:'properties'; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:json; 182, identifier:data; 183, if_statement; 183, 184; 183, 193; 184, comparison_operator:!=; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:response; 187, identifier:status_code; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:requests; 191, identifier:codes; 192, identifier:created; 193, block; 193, 194; 194, raise_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:APIError; 197, argument_list; 197, 198; 198, string:"Could not create property"; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:prop; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:Property; 205, identifier:create; 206, argument_list; 206, 207; 206, 216; 207, subscript; 207, 208; 207, 215; 208, subscript; 208, 209; 208, 214; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:response; 212, identifier:json; 213, argument_list; 214, string:'results'; 215, integer:0; 216, keyword_argument; 216, 217; 216, 218; 217, identifier:client; 218, identifier:self; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 226; 221, attribute; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:model; 224, identifier:properties; 225, identifier:append; 226, argument_list; 226, 227; 227, identifier:prop; 228, return_statement; 228, 229; 229, identifier:prop | def create_property(self, model, name, description=None, property_type=PropertyType.CHAR_VALUE, default_value=None,
unit=None, options=None):
"""Create a new property model under a given model.
Use the :class:`enums.PropertyType` to select which property type to create to ensure that you
provide the correct values to the KE-chain backend. The default is a `PropertyType.CHAR_VALUE` which is a
single line text in KE-chain.
:param model: parent model
:type model: :class:`models.Part`
:param name: property model name
:type name: basestring
:param description: property model description (optional)
:type description: basestring or None
:param property_type: choose one of the :class:`enums.PropertyType`, defaults to `PropertyType.CHAR_VALUE`.
:type property_type: basestring or None
:param default_value: (optional) default value used for part instances when creating a model.
:type default_value: any
:param unit: (optional) unit of the property
:type unit: basestring or None
:param options: (optional) property options (eg. validators or 'single selectlist choices')
:type options: basestring or None
:return: a :class:`models.Property` with category `MODEL`
:raises IllegalArgumentError: When the provided arguments are incorrect
:raises APIError: if the `Property` model could not be created
"""
if model.category != Category.MODEL:
raise IllegalArgumentError("The model should be of category MODEL")
if not property_type.endswith('_VALUE'):
warnings.warn("Please use the `PropertyType` enumeration to ensure providing correct "
"values to the backend.", UserWarning)
property_type = '{}_VALUE'.format(property_type.upper())
if property_type not in PropertyType.values():
raise IllegalArgumentError("Please provide a valid propertytype, please use one of `enums.PropertyType`. "
"Got: '{}'".format(property_type))
# because the references value only accepts a single 'model_id' in the default value, we need to convert this
# to a single value from the list of values.
if property_type in (PropertyType.REFERENCE_VALUE, PropertyType.REFERENCES_VALUE) and \
isinstance(default_value, (list, tuple)) and default_value:
default_value = default_value[0]
data = {
"name": name,
"part": model.id,
"description": description or '',
"property_type": property_type.upper(),
"value": default_value,
"unit": unit or '',
"options": options or {}
}
# # We add options after the fact only if they are available, otherwise the options will be set to null in the
# # request and that can't be handled by KE-chain.
# if options:
# data['options'] = options
response = self._request('POST', self._build_url('properties'),
json=data)
if response.status_code != requests.codes.created:
raise APIError("Could not create property")
prop = Property.create(response.json()['results'][0], client=self)
model.properties.append(prop)
return prop |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:set_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:request; 6, block; 6, 7; 6, 9; 6, 10; 6, 26; 6, 59; 6, 60; 6, 80; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:sort_request; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:request; 17, identifier:GET; 18, identifier:get; 19, argument_list; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:sort_parameter; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:default_sort; 26, if_statement; 26, 27; 26, 33; 26, 49; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:sort_request; 30, identifier:startswith; 31, argument_list; 31, 32; 32, string:'-'; 33, block; 33, 34; 33, 38; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:sort_order; 37, string:'-'; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:sort_field; 41, subscript; 41, 42; 41, 48; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:sort_request; 45, identifier:split; 46, argument_list; 46, 47; 47, string:'-'; 48, integer:1; 49, else_clause; 49, 50; 50, block; 50, 51; 50, 55; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:sort_order; 54, string:''; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:sort_field; 58, identifier:sort_request; 59, comment; 60, if_statement; 60, 61; 60, 67; 61, not_operator; 61, 62; 62, comparison_operator:in; 62, 63; 62, 64; 63, identifier:sort_field; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:_allowed_sort_fields; 67, block; 67, 68; 67, 74; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:sort_order; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:default_sort_order; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:sort_field; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:default_sort_field; 80, return_statement; 80, 81; 81, tuple; 81, 82; 81, 83; 82, identifier:sort_order; 83, identifier:sort_field | def set_sort(self, request):
"""
Take the sort parameter from the get parameters and split it into
the field and the prefix
"""
# Look for 'sort' in get request. If not available use default.
sort_request = request.GET.get(self.sort_parameter, self.default_sort)
if sort_request.startswith('-'):
sort_order = '-'
sort_field = sort_request.split('-')[1]
else:
sort_order = ''
sort_field = sort_request
# Invalid sort requests fail silently
if not sort_field in self._allowed_sort_fields:
sort_order = self.default_sort_order
sort_field = self.default_sort_field
return (sort_order, sort_field) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_next_sort_string; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:field; 6, block; 6, 7; 6, 9; 6, 10; 6, 46; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, if_statement; 10, 11; 10, 16; 10, 27; 11, comparison_operator:==; 11, 12; 11, 13; 12, identifier:field; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:sort_field; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:next_sort; 20, binary_operator:+; 20, 21; 20, 26; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:toggle_sort_order; 25, argument_list; 26, identifier:field; 27, else_clause; 27, 28; 28, block; 28, 29; 28, 40; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 30, 33; 31, identifier:default_order_for_field; 32, line_continuation:\; 33, subscript; 33, 34; 33, 39; 34, subscript; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_allowed_sort_fields; 38, identifier:field; 39, string:'default_direction'; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:next_sort; 43, binary_operator:+; 43, 44; 43, 45; 44, identifier:default_order_for_field; 45, identifier:field; 46, return_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:get_sort_string; 51, argument_list; 51, 52; 52, identifier:next_sort | def get_next_sort_string(self, field):
"""
If we're already sorted by the field then the sort query
returned reverses the sort order.
"""
# self.sort_field is the currect sort field
if field == self.sort_field:
next_sort = self.toggle_sort_order() + field
else:
default_order_for_field = \
self._allowed_sort_fields[field]['default_direction']
next_sort = default_order_for_field + field
return self.get_sort_string(next_sort) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_sort_indicator; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:field; 6, block; 6, 7; 6, 9; 6, 13; 6, 35; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:indicator; 12, string:''; 13, if_statement; 13, 14; 13, 19; 14, comparison_operator:==; 14, 15; 14, 16; 15, identifier:field; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:sort_field; 19, block; 19, 20; 19, 24; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:indicator; 23, string:'sort-asc'; 24, if_statement; 24, 25; 24, 30; 25, comparison_operator:==; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:sort_order; 29, string:'-'; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:indicator; 34, string:'sort-desc'; 35, return_statement; 35, 36; 36, identifier:indicator | def get_sort_indicator(self, field):
"""
Returns a sort class for the active sort only. That is, if field is not
sort_field, then nothing will be returned becaues the sort is not
active.
"""
indicator = ''
if field == self.sort_field:
indicator = 'sort-asc'
if self.sort_order == '-':
indicator = 'sort-desc'
return indicator |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:get_basic_sort_link; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:request; 6, identifier:field; 7, block; 7, 8; 7, 10; 7, 18; 7, 27; 7, 66; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:query_string; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:get_querystring; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:sort_string; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:get_next_sort_string; 25, argument_list; 25, 26; 26, identifier:field; 27, if_statement; 27, 28; 27, 29; 27, 49; 28, identifier:sort_string; 29, block; 29, 30; 29, 40; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:sort_link; 33, binary_operator:+; 33, 34; 33, 39; 34, binary_operator:+; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:request; 37, identifier:path; 38, string:'?'; 39, identifier:sort_string; 40, if_statement; 40, 41; 40, 42; 41, identifier:query_string; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, augmented_assignment:+=; 44, 45; 44, 46; 45, identifier:sort_link; 46, binary_operator:+; 46, 47; 46, 48; 47, string:'&'; 48, identifier:query_string; 49, else_clause; 49, 50; 50, block; 50, 51; 50, 57; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:sort_link; 54, attribute; 54, 55; 54, 56; 55, identifier:request; 56, identifier:path; 57, if_statement; 57, 58; 57, 59; 58, identifier:query_string; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, augmented_assignment:+=; 61, 62; 61, 63; 62, identifier:sort_link; 63, binary_operator:+; 63, 64; 63, 65; 64, string:'?'; 65, identifier:query_string; 66, return_statement; 66, 67; 67, identifier:sort_link | def get_basic_sort_link(self, request, field):
"""
Thanks to del_query_parameters and get_querystring, we build the link
with preserving interesting get parameters and removing the others
"""
query_string = self.get_querystring()
sort_string = self.get_next_sort_string(field)
if sort_string:
sort_link = request.path + '?' + sort_string
if query_string:
sort_link += '&' + query_string
else:
sort_link = request.path
if query_string:
sort_link += '?' + query_string
return sort_link |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:download_as_pdf; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 16; 3, 21; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:target_dir; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:pdf_filename; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:paper_size; 13, attribute; 13, 14; 13, 15; 14, identifier:PaperSize; 15, identifier:A4; 16, default_parameter; 16, 17; 16, 18; 17, identifier:paper_orientation; 18, attribute; 18, 19; 18, 20; 19, identifier:PaperOrientation; 20, identifier:PORTRAIT; 21, default_parameter; 21, 22; 21, 23; 22, identifier:include_appendices; 23, False; 24, block; 24, 25; 24, 27; 24, 39; 24, 52; 24, 70; 24, 83; 24, 99; 24, 114; 24, 138; 24, 139; 24, 255; 25, expression_statement; 25, 26; 26, comment; 27, if_statement; 27, 28; 27, 30; 28, not_operator; 28, 29; 29, identifier:pdf_filename; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:pdf_filename; 34, binary_operator:+; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:name; 38, string:'.pdf'; 39, if_statement; 39, 40; 39, 47; 40, not_operator; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:pdf_filename; 44, identifier:endswith; 45, argument_list; 45, 46; 46, string:'.pdf'; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, augmented_assignment:+=; 49, 50; 49, 51; 50, identifier:pdf_filename; 51, string:'.pdf'; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:full_path; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:os; 59, identifier:path; 60, identifier:join; 61, argument_list; 61, 62; 61, 69; 62, boolean_operator:or; 62, 63; 62, 64; 63, identifier:target_dir; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:os; 67, identifier:getcwd; 68, argument_list; 69, identifier:pdf_filename; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:request_params; 73, dictionary; 73, 74; 73, 77; 73, 80; 74, pair; 74, 75; 74, 76; 75, string:'papersize'; 76, identifier:paper_size; 77, pair; 77, 78; 77, 79; 78, string:'orientation'; 79, identifier:paper_orientation; 80, pair; 80, 81; 80, 82; 81, string:'appendices'; 82, identifier:include_appendices; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:url; 86, call; 86, 87; 86, 92; 87, attribute; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:_client; 91, identifier:_build_url; 92, argument_list; 92, 93; 92, 94; 93, string:'activity_export'; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:activity_id; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:id; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:response; 102, call; 102, 103; 102, 108; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:_client; 107, identifier:_request; 108, argument_list; 108, 109; 108, 110; 108, 111; 109, string:'GET'; 110, identifier:url; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:params; 113, identifier:request_params; 114, if_statement; 114, 115; 114, 124; 114, 125; 115, comparison_operator:!=; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:response; 118, identifier:status_code; 119, attribute; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:requests; 122, identifier:codes; 123, identifier:ok; 124, comment; 125, block; 125, 126; 126, raise_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:APIError; 129, argument_list; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, string:"Could not download PDF of activity {}"; 133, identifier:format; 134, argument_list; 134, 135; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:name; 138, comment; 139, if_statement; 139, 140; 139, 141; 140, identifier:include_appendices; 141, block; 141, 142; 141, 150; 141, 151; 141, 165; 141, 169; 141, 240; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:data; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:response; 148, identifier:json; 149, argument_list; 150, comment; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:url; 154, call; 154, 155; 154, 156; 155, identifier:urljoin; 156, argument_list; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:_client; 161, identifier:api_root; 162, subscript; 162, 163; 162, 164; 163, identifier:data; 164, string:'download_url'; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:count; 168, integer:0; 169, while_statement; 169, 170; 169, 173; 170, comparison_operator:<=; 170, 171; 170, 172; 171, identifier:count; 172, identifier:ASYNC_TIMEOUT_LIMIT; 173, block; 173, 174; 173, 188; 173, 229; 173, 233; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:response; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:_client; 182, identifier:_request; 183, argument_list; 183, 184; 183, 185; 184, string:'GET'; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:url; 187, identifier:url; 188, if_statement; 188, 189; 188, 198; 188, 199; 189, comparison_operator:==; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:response; 192, identifier:status_code; 193, attribute; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:requests; 196, identifier:codes; 197, identifier:ok; 198, comment; 199, block; 199, 200; 199, 228; 200, with_statement; 200, 201; 200, 211; 201, with_clause; 201, 202; 202, with_item; 202, 203; 203, as_pattern; 203, 204; 203, 209; 204, call; 204, 205; 204, 206; 205, identifier:open; 206, argument_list; 206, 207; 206, 208; 207, identifier:full_path; 208, string:'wb'; 209, as_pattern_target; 209, 210; 210, identifier:f; 211, block; 211, 212; 212, for_statement; 212, 213; 212, 214; 212, 220; 213, identifier:chunk; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:response; 217, identifier:iter_content; 218, argument_list; 218, 219; 219, integer:1024; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:f; 225, identifier:write; 226, argument_list; 226, 227; 227, identifier:chunk; 228, return_statement; 229, expression_statement; 229, 230; 230, augmented_assignment:+=; 230, 231; 230, 232; 231, identifier:count; 232, identifier:ASYNC_REFRESH_INTERVAL; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:time; 237, identifier:sleep; 238, argument_list; 238, 239; 239, identifier:ASYNC_REFRESH_INTERVAL; 240, raise_statement; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:APIError; 243, argument_list; 243, 244; 244, call; 244, 245; 244, 250; 245, attribute; 245, 246; 245, 249; 246, concatenated_string; 246, 247; 246, 248; 247, string:"Could not download PDF of activity {} within the time-out limit of {} "; 248, string:"seconds"; 249, identifier:format; 250, argument_list; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:self; 253, identifier:name; 254, identifier:ASYNC_TIMEOUT_LIMIT; 255, with_statement; 255, 256; 255, 266; 256, with_clause; 256, 257; 257, with_item; 257, 258; 258, as_pattern; 258, 259; 258, 264; 259, call; 259, 260; 259, 261; 260, identifier:open; 261, argument_list; 261, 262; 261, 263; 262, identifier:full_path; 263, string:'wb'; 264, as_pattern_target; 264, 265; 265, identifier:f; 266, block; 266, 267; 267, for_statement; 267, 268; 267, 269; 267, 275; 268, identifier:chunk; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:response; 272, identifier:iter_content; 273, argument_list; 273, 274; 274, integer:1024; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:f; 280, identifier:write; 281, argument_list; 281, 282; 282, identifier:chunk | def download_as_pdf(self, target_dir=None, pdf_filename=None, paper_size=PaperSize.A4,
paper_orientation=PaperOrientation.PORTRAIT, include_appendices=False):
"""
Retrieve the PDF of the Activity.
.. versionadded:: 2.1
:param target_dir: (optional) directory path name where the store the log.txt to.
:type target_dir: basestring or None
:param pdf_filename: (optional) log filename to write the log to, defaults to `log.txt`.
:type pdf_filename: basestring or None
:param paper_size: The size of the paper to which the PDF is downloaded:
- a4paper (default): A4 paper size
- a3paper: A3 paper size
- a2paper: A2 paper size
- a1paper: A1 paper size
- a0paper: A0 paper size
:type paper_size: basestring (see :class:`enums.PaperSize`)
:param paper_orientation: The orientation of the paper to which the PDF is downloaded:
- portrait (default): portrait orientation
- landscape: landscape orientation
:type paper_size: basestring (see :class:`enums.PaperOrientation`)
:param include_appendices: True if the PDF should contain appendices, False (default) if otherwise.
:type include_appendices: bool
:raises APIError: if the pdf file could not be found.
:raises OSError: if the file could not be written.
"""
if not pdf_filename:
pdf_filename = self.name + '.pdf'
if not pdf_filename.endswith('.pdf'):
pdf_filename += '.pdf'
full_path = os.path.join(target_dir or os.getcwd(), pdf_filename)
request_params = {
'papersize': paper_size,
'orientation': paper_orientation,
'appendices': include_appendices
}
url = self._client._build_url('activity_export', activity_id=self.id)
response = self._client._request('GET', url, params=request_params)
if response.status_code != requests.codes.ok: # pragma: no cover
raise APIError("Could not download PDF of activity {}".format(self.name))
# If appendices are included, the request becomes asynchronous
if include_appendices:
data = response.json()
# Download the pdf async
url = urljoin(self._client.api_root, data['download_url'])
count = 0
while count <= ASYNC_TIMEOUT_LIMIT:
response = self._client._request('GET', url=url)
if response.status_code == requests.codes.ok: # pragma: no cover
with open(full_path, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
return
count += ASYNC_REFRESH_INTERVAL
time.sleep(ASYNC_REFRESH_INTERVAL)
raise APIError("Could not download PDF of activity {} within the time-out limit of {} "
"seconds".format(self.name, ASYNC_TIMEOUT_LIMIT))
with open(full_path, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 30; 2, function_name:get_project; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 4, default_parameter; 4, 5; 4, 6; 5, identifier:url; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:username; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:password; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:token; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:scope; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:scope_id; 21, None; 22, default_parameter; 22, 23; 22, 24; 23, identifier:env_filename; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:status; 27, attribute; 27, 28; 27, 29; 28, identifier:ScopeStatus; 29, identifier:ACTIVE; 30, block; 30, 31; 30, 33; 30, 128; 30, 266; 31, expression_statement; 31, 32; 32, comment; 33, if_statement; 33, 34; 33, 45; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:env; 37, identifier:bool; 38, argument_list; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:kecenv; 41, identifier:KECHAIN_FORCE_ENV_USE; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:default; 44, False; 45, block; 45, 46; 45, 62; 45, 100; 46, if_statement; 46, 47; 46, 56; 47, not_operator; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:os; 51, identifier:getenv; 52, argument_list; 52, 53; 53, attribute; 53, 54; 53, 55; 54, identifier:kecenv; 55, identifier:KECHAIN_URL; 56, block; 56, 57; 57, raise_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:ClientError; 60, argument_list; 60, 61; 61, string:"Error: KECHAIN_URL should be provided as environment variable (use of env vars is enforced)"; 62, if_statement; 62, 63; 62, 92; 63, not_operator; 63, 64; 64, parenthesized_expression; 64, 65; 65, boolean_operator:or; 65, 66; 65, 74; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:os; 69, identifier:getenv; 70, argument_list; 70, 71; 71, attribute; 71, 72; 71, 73; 72, identifier:kecenv; 73, identifier:KECHAIN_TOKEN; 74, parenthesized_expression; 74, 75; 75, boolean_operator:and; 75, 76; 75, 84; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:os; 79, identifier:getenv; 80, argument_list; 80, 81; 81, attribute; 81, 82; 81, 83; 82, identifier:kecenv; 83, identifier:KECHAIN_PASSWORD; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:os; 87, identifier:getenv; 88, argument_list; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:kecenv; 91, identifier:KECHAIN_PASSWORD; 92, block; 92, 93; 93, raise_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:ClientError; 96, argument_list; 96, 97; 97, concatenated_string; 97, 98; 97, 99; 98, string:"Error: KECHAIN_TOKEN or KECHAIN_USERNAME and KECHAIN_PASSWORD should be provided as "; 99, string:"environment variable(s) (use of env vars is enforced)"; 100, if_statement; 100, 101; 100, 120; 101, not_operator; 101, 102; 102, parenthesized_expression; 102, 103; 103, boolean_operator:or; 103, 104; 103, 112; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:os; 107, identifier:getenv; 108, argument_list; 108, 109; 109, attribute; 109, 110; 109, 111; 110, identifier:kecenv; 111, identifier:KECHAIN_SCOPE; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:os; 115, identifier:getenv; 116, argument_list; 116, 117; 117, attribute; 117, 118; 117, 119; 118, identifier:kecenv; 119, identifier:KECHAIN_SCOPE_ID; 120, block; 120, 121; 121, raise_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:ClientError; 124, argument_list; 124, 125; 125, concatenated_string; 125, 126; 125, 127; 126, string:"Error: KECHAIN_SCOPE or KECHAIN_SCOPE_ID should be provided as environment variable "; 127, string:"(use of env vars is enforced)"; 128, if_statement; 128, 129; 128, 153; 128, 201; 128, 257; 129, boolean_operator:or; 129, 130; 129, 141; 129, 142; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:env; 133, identifier:bool; 134, argument_list; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:kecenv; 137, identifier:KECHAIN_FORCE_ENV_USE; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:default; 140, False; 141, line_continuation:\; 142, not_operator; 142, 143; 143, call; 143, 144; 143, 145; 144, identifier:any; 145, argument_list; 145, 146; 146, tuple; 146, 147; 146, 148; 146, 149; 146, 150; 146, 151; 146, 152; 147, identifier:url; 148, identifier:username; 149, identifier:password; 150, identifier:token; 151, identifier:scope; 152, identifier:scope_id; 153, block; 153, 154; 153, 165; 153, 177; 153, 189; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:client; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:Client; 160, identifier:from_env; 161, argument_list; 161, 162; 162, keyword_argument; 162, 163; 162, 164; 163, identifier:env_filename; 164, identifier:env_filename; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:scope_id; 168, call; 168, 169; 168, 170; 169, identifier:env; 170, argument_list; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:kecenv; 173, identifier:KECHAIN_SCOPE_ID; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:default; 176, None; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:scope; 180, call; 180, 181; 180, 182; 181, identifier:env; 182, argument_list; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:kecenv; 185, identifier:KECHAIN_SCOPE; 186, keyword_argument; 186, 187; 186, 188; 187, identifier:default; 188, None; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:status; 192, call; 192, 193; 192, 194; 193, identifier:env; 194, argument_list; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:kecenv; 197, identifier:KECHAIN_SCOPE_STATUS; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:default; 200, None; 201, elif_clause; 201, 202; 201, 232; 202, boolean_operator:and; 202, 203; 202, 219; 202, 220; 203, parenthesized_expression; 203, 204; 204, boolean_operator:and; 204, 205; 204, 215; 205, boolean_operator:and; 205, 206; 205, 207; 206, identifier:url; 207, parenthesized_expression; 207, 208; 208, boolean_operator:or; 208, 209; 208, 213; 209, parenthesized_expression; 209, 210; 210, boolean_operator:and; 210, 211; 210, 212; 211, identifier:username; 212, identifier:password; 213, parenthesized_expression; 213, 214; 214, identifier:token; 215, parenthesized_expression; 215, 216; 216, boolean_operator:or; 216, 217; 216, 218; 217, identifier:scope; 218, identifier:scope_id; 219, line_continuation:\; 220, not_operator; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:env; 224, identifier:bool; 225, argument_list; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:kecenv; 228, identifier:KECHAIN_FORCE_ENV_USE; 229, keyword_argument; 229, 230; 229, 231; 230, identifier:default; 231, False; 232, block; 232, 233; 232, 242; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:client; 236, call; 236, 237; 236, 238; 237, identifier:Client; 238, argument_list; 238, 239; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:url; 241, identifier:url; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:client; 246, identifier:login; 247, argument_list; 247, 248; 247, 251; 247, 254; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:username; 250, identifier:username; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:password; 253, identifier:password; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:token; 256, identifier:token; 257, else_clause; 257, 258; 258, block; 258, 259; 259, raise_statement; 259, 260; 260, call; 260, 261; 260, 262; 261, identifier:ClientError; 262, argument_list; 262, 263; 263, concatenated_string; 263, 264; 263, 265; 264, string:"Error: insufficient arguments to connect to KE-chain. "; 265, string:"See documentation of `pykechain.get_project()`"; 266, if_statement; 266, 267; 266, 268; 266, 281; 267, identifier:scope_id; 268, block; 268, 269; 269, return_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:client; 273, identifier:scope; 274, argument_list; 274, 275; 274, 278; 275, keyword_argument; 275, 276; 275, 277; 276, identifier:pk; 277, identifier:scope_id; 278, keyword_argument; 278, 279; 278, 280; 279, identifier:status; 280, identifier:status; 281, else_clause; 281, 282; 282, block; 282, 283; 283, return_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:client; 287, identifier:scope; 288, argument_list; 288, 289; 288, 292; 289, keyword_argument; 289, 290; 289, 291; 290, identifier:name; 291, identifier:scope; 292, keyword_argument; 292, 293; 292, 294; 293, identifier:status; 294, identifier:status | def get_project(url=None, username=None, password=None, token=None, scope=None, scope_id=None,
env_filename=None, status=ScopeStatus.ACTIVE):
"""
Retrieve and return the KE-chain project to be used throughout an app.
This helper is made to bootstrap a pykechain enabled python script or an jupyter notebook with the correct
project (technically this is a `pykechain.models.Scope` model).
When no parameters are passed in this function, it will try to retrieve `url`, `token`, `scope` (or `scope_id`)
from the environment variables or a neatly placed '.env' file.
when the environment variable KECHAIN_FORCE_ENV_USE is set to true, (or ok, on, 1, yes) then the use of
environmentvariables for the retrieval of the scope are enforced. The following environment variables can be set::
KECHAIN_URL - full url of KE-chain where to connect to eg: 'https://<some>.ke-chain.com'
KECHAIN_TOKEN - authentication token for the KE-chain user provided from KE-chain user account control
KECHAIN_USERNAME - the username for the credentials
KECHAIN_PASSWORD - the password for the credentials
KECHAIN_SCOPE - the name of the project / scope. Should be unique, otherwise use scope_id
KECHAIN_SCOPE_ID - the UUID of the project / scope.
KECHAIN_FORCE_ENV_USE - set to 'true', '1', 'ok', or 'yes' to always use the environment variables.
KECHAIN_SCOPE_STATUS - the status of the Scope to retrieve, defaults to None to retrieve all scopes
.. versionadded:: 1.12
:param url: (optional) url of KE-chain
:type url: basestring or None
:param username: (optional) username for authentication (together with password, if not token)
:type username: basestring or None
:param password: (optional) password for username/password authentication (together with username, if not token)
:type password: basestring or None
:param token: (optional) token for authentication (if not username/password)
:type token: basestring or None
:param scope: (optional) name of the scope to retrieve from KE-chain.
:type scope: basestring or None
:param scope_id: (optional) UUID of the scope to retrieve and return from KE-chain
:type scope_id: basestring or None
:param env_filename: (optional) name of the environment filename to bootstrap the Client
:type env_filename: basestring or None
:param status: (optional) status of the scope to retrieve, defaults to :attr:`enums.Scopestatus.ACTIVE`
:type status: basestring or None
:return: pykechain.models.Scope
:raises NotFoundError: If the scope could not be found
:raises ClientError: If the client connection to KE-chain was unsuccessful
:raises APIError: If other Errors occur to retrieve the scope
Example
-------
An example with parameters provided
>>> from pykechain import get_project
>>> project = get_project(url='http://localhost:8000',
... username='foo', password='bar', scope='1st!')
>>> print(project.name)
1st
An example with a .env file on disk::
# This is an .env file on disk.
KECHAIN_TOKEN=bd9377793f7e74a29dbb11fce969
KECHAIN_URL=http://localhost:8080
KECHAIN_SCOPE_ID=c9f0-228e-4d3a-9dc0-ec5a75d7
>>> project = get_project(env_filename='/path/to/.env')
>>> project.id
c9f0-228e-4d3a-9dc0-ec5a75d7
An example for get_project that will extract all from the environment variables
>>> env_vars = os.environ
>>> env_vars.get('KECHAIN_TOKEN')
bd9377793f7e74a29dbb11fce969
>>> env_vars.get('KECHAIN_URL')
http://localhost:8080
>>> env_vars.get('KECHAIN_SCOPE')
Bike Project
>>> project = get_project()
>>> project.name
Bike Project
"""
if env.bool(kecenv.KECHAIN_FORCE_ENV_USE, default=False):
if not os.getenv(kecenv.KECHAIN_URL):
raise ClientError(
"Error: KECHAIN_URL should be provided as environment variable (use of env vars is enforced)")
if not (os.getenv(kecenv.KECHAIN_TOKEN) or
(os.getenv(kecenv.KECHAIN_PASSWORD) and os.getenv(kecenv.KECHAIN_PASSWORD))):
raise ClientError("Error: KECHAIN_TOKEN or KECHAIN_USERNAME and KECHAIN_PASSWORD should be provided as "
"environment variable(s) (use of env vars is enforced)")
if not (os.getenv(kecenv.KECHAIN_SCOPE) or os.getenv(kecenv.KECHAIN_SCOPE_ID)):
raise ClientError("Error: KECHAIN_SCOPE or KECHAIN_SCOPE_ID should be provided as environment variable "
"(use of env vars is enforced)")
if env.bool(kecenv.KECHAIN_FORCE_ENV_USE, default=False) or \
not any((url, username, password, token, scope, scope_id)):
client = Client.from_env(env_filename=env_filename)
scope_id = env(kecenv.KECHAIN_SCOPE_ID, default=None)
scope = env(kecenv.KECHAIN_SCOPE, default=None)
status = env(kecenv.KECHAIN_SCOPE_STATUS, default=None)
elif (url and ((username and password) or (token)) and (scope or scope_id)) and \
not env.bool(kecenv.KECHAIN_FORCE_ENV_USE, default=False):
client = Client(url=url)
client.login(username=username, password=password, token=token)
else:
raise ClientError("Error: insufficient arguments to connect to KE-chain. "
"See documentation of `pykechain.get_project()`")
if scope_id:
return client.scope(pk=scope_id, status=status)
else:
return client.scope(name=scope, status=status) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:options; 3, parameters; 3, 4; 4, identifier:cls; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, call; 9, 10; 9, 11; 10, identifier:sorted; 11, generator_expression; 11, 12; 11, 15; 11, 26; 12, tuple; 12, 13; 12, 14; 13, identifier:value; 14, identifier:name; 15, for_in_clause; 15, 16; 15, 19; 16, tuple_pattern; 16, 17; 16, 18; 17, identifier:name; 18, identifier:value; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:cls; 23, identifier:__dict__; 24, identifier:items; 25, argument_list; 26, if_clause; 26, 27; 27, not_operator; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:name; 31, identifier:startswith; 32, argument_list; 32, 33; 33, string:'__' | def options(cls):
"""Provide a sorted list of options."""
return sorted((value, name) for (name, value) in cls.__dict__.items() if not name.startswith('__')) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:edit; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:name; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:description; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:version; 13, None; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kwargs; 16, block; 16, 17; 16, 19; 16, 28; 16, 54; 16, 80; 16, 106; 16, 118; 16, 145; 16, 167; 16, 176; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:update_dict; 22, dictionary; 22, 23; 23, pair; 23, 24; 23, 25; 24, string:'id'; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:id; 28, if_statement; 28, 29; 28, 30; 29, identifier:name; 30, block; 30, 31; 30, 44; 31, if_statement; 31, 32; 31, 38; 32, not_operator; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:isinstance; 35, argument_list; 35, 36; 35, 37; 36, identifier:name; 37, identifier:str; 38, block; 38, 39; 39, raise_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:IllegalArgumentError; 42, argument_list; 42, 43; 43, string:"name should be provided as a string"; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:update_dict; 48, identifier:update; 49, argument_list; 49, 50; 50, dictionary; 50, 51; 51, pair; 51, 52; 51, 53; 52, string:'name'; 53, identifier:name; 54, if_statement; 54, 55; 54, 56; 55, identifier:description; 56, block; 56, 57; 56, 70; 57, if_statement; 57, 58; 57, 64; 58, not_operator; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:isinstance; 61, argument_list; 61, 62; 61, 63; 62, identifier:description; 63, identifier:str; 64, block; 64, 65; 65, raise_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:IllegalArgumentError; 68, argument_list; 68, 69; 69, string:"description should be provided as a string"; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:update_dict; 74, identifier:update; 75, argument_list; 75, 76; 76, dictionary; 76, 77; 77, pair; 77, 78; 77, 79; 78, string:'description'; 79, identifier:description; 80, if_statement; 80, 81; 80, 82; 81, identifier:version; 82, block; 82, 83; 82, 96; 83, if_statement; 83, 84; 83, 90; 84, not_operator; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:isinstance; 87, argument_list; 87, 88; 87, 89; 88, identifier:version; 89, identifier:str; 90, block; 90, 91; 91, raise_statement; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:IllegalArgumentError; 94, argument_list; 94, 95; 95, string:"description should be provided as a string"; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:update_dict; 100, identifier:update; 101, argument_list; 101, 102; 102, dictionary; 102, 103; 103, pair; 103, 104; 103, 105; 104, string:'script_version'; 105, identifier:version; 106, if_statement; 106, 107; 106, 108; 106, 109; 107, identifier:kwargs; 108, comment; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:update_dict; 114, identifier:update; 115, argument_list; 115, 116; 116, dictionary_splat; 116, 117; 117, identifier:kwargs; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:response; 121, call; 121, 122; 121, 127; 122, attribute; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:_client; 126, identifier:_request; 127, argument_list; 127, 128; 127, 129; 127, 142; 128, string:'PUT'; 129, call; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:_client; 134, identifier:_build_url; 135, argument_list; 135, 136; 135, 137; 136, string:'service'; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:service_id; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:id; 142, keyword_argument; 142, 143; 142, 144; 143, identifier:json; 144, identifier:update_dict; 145, if_statement; 145, 146; 145, 155; 145, 156; 146, comparison_operator:!=; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:response; 149, identifier:status_code; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:requests; 153, identifier:codes; 154, identifier:ok; 155, comment; 156, block; 156, 157; 157, raise_statement; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:APIError; 160, argument_list; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, string:"Could not update Service ({})"; 164, identifier:format; 165, argument_list; 165, 166; 166, identifier:response; 167, if_statement; 167, 168; 167, 169; 168, identifier:name; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:name; 175, identifier:name; 176, if_statement; 176, 177; 176, 178; 177, identifier:version; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:version; 184, identifier:version | def edit(self, name=None, description=None, version=None, **kwargs):
"""
Edit Service details.
.. versionadded:: 1.13
:param name: (optional) name of the service to change.
:type name: basestring or None
:param description: (optional) description of the service.
:type description: basestring or None
:param version: (optional) version number of the service.
:type version: basestring or None
:param kwargs: (optional) additional keyword arguments to change.
:type kwargs: dict or None
:raises IllegalArgumentError: when you provide an illegal argument.
:raises APIError: if the service could not be updated.
"""
update_dict = {'id': self.id}
if name:
if not isinstance(name, str):
raise IllegalArgumentError("name should be provided as a string")
update_dict.update({'name': name})
if description:
if not isinstance(description, str):
raise IllegalArgumentError("description should be provided as a string")
update_dict.update({'description': description})
if version:
if not isinstance(version, str):
raise IllegalArgumentError("description should be provided as a string")
update_dict.update({'script_version': version})
if kwargs: # pragma: no cover
update_dict.update(**kwargs)
response = self._client._request('PUT',
self._client._build_url('service', service_id=self.id), json=update_dict)
if response.status_code != requests.codes.ok: # pragma: no cover
raise APIError("Could not update Service ({})".format(response))
if name:
self.name = name
if version:
self.version = version |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:children; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kwargs; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 13; 10, 14; 10, 50; 11, not_operator; 11, 12; 12, identifier:kwargs; 13, comment; 14, block; 14, 15; 14, 46; 15, if_statement; 15, 16; 15, 20; 16, not_operator; 16, 17; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_cached_children; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:_cached_children; 26, call; 26, 27; 26, 28; 27, identifier:list; 28, argument_list; 28, 29; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:_client; 34, identifier:parts; 35, argument_list; 35, 36; 35, 41; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:parent; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:id; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:category; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:category; 46, return_statement; 46, 47; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:_cached_children; 50, else_clause; 50, 51; 50, 52; 51, comment; 52, block; 52, 53; 53, return_statement; 53, 54; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:_client; 59, identifier:parts; 60, argument_list; 60, 61; 60, 66; 60, 71; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:parent; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:id; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:category; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:category; 71, dictionary_splat; 71, 72; 72, identifier:kwargs | def children(self, **kwargs):
"""Retrieve the children of this `Part` as `Partset`.
When you call the :func:`Part.children()` method without any additional filtering options for the children,
the children are cached to help speed up subsequent calls to retrieve the children. The cached children are
returned as a list and not as a `Partset`.
When you *do provide* additional keyword arguments (kwargs) that act as a specific children filter, the
cached children are _not_ used and a separate API call is made to retrieve only those children.
:param kwargs: Additional search arguments to search for, check :class:`pykechain.Client.parts`
for additional info
:type kwargs: dict
:return: a set of `Parts` as a :class:`PartSet`. Will be empty if no children. Will be a `List` if the
children are retrieved from the cached children.
:raises APIError: When an error occurs.
Example
-------
A normal call, which caches all children of the bike. If you call `bike.children` twice only 1 API call is made.
>>> bike = project.part('Bike')
>>> direct_descendants_of_bike = bike.children()
An example with providing additional part search parameters 'name__icontains'. Children are retrieved from the
API, not the bike's internal (already cached in previous example) cache.
>>> bike = project.part('Bike')
>>> wheel_children_of_bike = bike.children(name__icontains='wheel')
"""
if not kwargs:
# no kwargs provided is the default, we aim to cache it.
if not self._cached_children:
self._cached_children = list(self._client.parts(parent=self.id, category=self.category))
return self._cached_children
else:
# if kwargs are provided, we assume no use of cache as specific filtering on the children is performed.
return self._client.parts(parent=self.id, category=self.category, **kwargs) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:update; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:name; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:update_dict; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:bulk; 13, True; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kwargs; 16, block; 16, 17; 16, 19; 16, 20; 16, 24; 16, 30; 16, 67; 17, expression_statement; 17, 18; 18, comment; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:action; 23, string:'bulk_update_properties'; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:request_body; 27, call; 27, 28; 27, 29; 28, identifier:dict; 29, argument_list; 30, for_statement; 30, 31; 30, 34; 30, 39; 31, pattern_list; 31, 32; 31, 33; 32, identifier:prop_name_or_id; 33, identifier:property_value; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:update_dict; 37, identifier:items; 38, argument_list; 39, block; 39, 40; 40, if_statement; 40, 41; 40, 45; 40, 52; 41, call; 41, 42; 41, 43; 42, identifier:is_uuid; 43, argument_list; 43, 44; 44, identifier:prop_name_or_id; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:request_body; 50, identifier:prop_name_or_id; 51, identifier:property_value; 52, else_clause; 52, 53; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 66; 56, subscript; 56, 57; 56, 58; 57, identifier:request_body; 58, attribute; 58, 59; 58, 65; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:property; 63, argument_list; 63, 64; 64, identifier:prop_name_or_id; 65, identifier:id; 66, identifier:property_value; 67, if_statement; 67, 68; 67, 80; 67, 175; 68, boolean_operator:and; 68, 69; 68, 70; 69, identifier:bulk; 70, comparison_operator:>; 70, 71; 70, 79; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:update_dict; 77, identifier:keys; 78, argument_list; 79, integer:1; 80, block; 80, 81; 80, 97; 80, 147; 81, if_statement; 81, 82; 81, 83; 82, identifier:name; 83, block; 83, 84; 84, if_statement; 84, 85; 84, 91; 85, not_operator; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:isinstance; 88, argument_list; 88, 89; 88, 90; 89, identifier:name; 90, identifier:str; 91, block; 91, 92; 92, raise_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:IllegalArgumentError; 95, argument_list; 95, 96; 96, string:"Name of the part should be provided as a string"; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:r; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:_client; 105, identifier:_request; 106, argument_list; 106, 107; 106, 108; 106, 121; 106, 139; 107, string:'PUT'; 108, call; 108, 109; 108, 114; 109, attribute; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:_client; 113, identifier:_build_url; 114, argument_list; 114, 115; 114, 116; 115, string:'part'; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:part_id; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:id; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:data; 123, call; 123, 124; 123, 125; 124, identifier:dict; 125, argument_list; 125, 126; 125, 129; 125, 137; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:name; 128, identifier:name; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:properties; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:json; 134, identifier:dumps; 135, argument_list; 135, 136; 136, identifier:request_body; 137, dictionary_splat; 137, 138; 138, identifier:kwargs; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:params; 141, call; 141, 142; 141, 143; 142, identifier:dict; 143, argument_list; 143, 144; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:select_action; 146, identifier:action; 147, if_statement; 147, 148; 147, 157; 147, 158; 148, comparison_operator:!=; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:r; 151, identifier:status_code; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:requests; 155, identifier:codes; 156, identifier:ok; 157, comment; 158, block; 158, 159; 159, raise_statement; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:APIError; 162, argument_list; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, string:'{}: {}'; 166, identifier:format; 167, argument_list; 167, 168; 167, 172; 168, call; 168, 169; 168, 170; 169, identifier:str; 170, argument_list; 170, 171; 171, identifier:r; 172, attribute; 172, 173; 172, 174; 173, identifier:r; 174, identifier:content; 175, else_clause; 175, 176; 176, block; 176, 177; 177, for_statement; 177, 178; 177, 181; 177, 186; 178, pattern_list; 178, 179; 178, 180; 179, identifier:property_name; 180, identifier:property_value; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:update_dict; 184, identifier:items; 185, argument_list; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 197; 189, attribute; 189, 190; 189, 196; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:property; 194, argument_list; 194, 195; 195, identifier:property_name; 196, identifier:value; 197, identifier:property_value | def update(self, name=None, update_dict=None, bulk=True, **kwargs):
"""
Edit part name and property values in one go.
In order to prevent the backend from updating the frontend you may add `suppress_kevents=True` as
additional keyword=value argument to this method. This will improve performance of the backend
against a trade-off that someone looking at the frontend won't notice any changes unless the page
is refreshed.
:param name: new part name (defined as a string)
:type name: basestring or None
:param update_dict: dictionary with keys being property names (str) or property ids (uuid)
and values being property values
:type update_dict: dict
:param bulk: True to use the bulk_update_properties API endpoint for KE-chain versions later then 2.1.0b
:type bulk: boolean or None
:param kwargs: (optional) additional keyword arguments that will be passed inside the update request
:type kwargs: dict or None
:return: the updated :class:`Part`
:raises NotFoundError: when the property name is not a valid property of this part
:raises IllegalArgumentError: when the type or value of an argument provided is incorrect
:raises APIError: in case an Error occurs
Example
-------
>>> bike = client.scope('Bike Project').part('Bike')
>>> bike.update(name='Good name', update_dict={'Gears': 11, 'Total Height': 56.3}, bulk=True)
"""
# dict(name=name, properties=json.dumps(update_dict))) with property ids:value
action = 'bulk_update_properties'
request_body = dict()
for prop_name_or_id, property_value in update_dict.items():
if is_uuid(prop_name_or_id):
request_body[prop_name_or_id] = property_value
else:
request_body[self.property(prop_name_or_id).id] = property_value
if bulk and len(update_dict.keys()) > 1:
if name:
if not isinstance(name, str):
raise IllegalArgumentError("Name of the part should be provided as a string")
r = self._client._request('PUT', self._client._build_url('part', part_id=self.id),
data=dict(name=name, properties=json.dumps(request_body), **kwargs),
params=dict(select_action=action))
if r.status_code != requests.codes.ok: # pragma: no cover
raise APIError('{}: {}'.format(str(r), r.content))
else:
for property_name, property_value in update_dict.items():
self.property(property_name).value = property_value |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:add_with_properties; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:model; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:update_dict; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:bulk; 14, True; 15, dictionary_splat_pattern; 15, 16; 16, identifier:kwargs; 17, block; 17, 18; 17, 20; 17, 34; 17, 42; 17, 46; 17, 52; 17, 89; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 28; 21, comparison_operator:!=; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:category; 25, attribute; 25, 26; 25, 27; 26, identifier:Category; 27, identifier:INSTANCE; 28, block; 28, 29; 29, raise_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:APIError; 32, argument_list; 32, 33; 33, string:"Part should be of category INSTANCE"; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:name; 37, boolean_operator:or; 37, 38; 37, 39; 38, identifier:name; 39, attribute; 39, 40; 39, 41; 40, identifier:model; 41, identifier:name; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:action; 45, string:'new_instance_with_properties'; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:properties_update_dict; 49, call; 49, 50; 49, 51; 50, identifier:dict; 51, argument_list; 52, for_statement; 52, 53; 52, 56; 52, 61; 53, pattern_list; 53, 54; 53, 55; 54, identifier:prop_name_or_id; 55, identifier:property_value; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:update_dict; 59, identifier:items; 60, argument_list; 61, block; 61, 62; 62, if_statement; 62, 63; 62, 67; 62, 74; 63, call; 63, 64; 63, 65; 64, identifier:is_uuid; 65, argument_list; 65, 66; 66, identifier:prop_name_or_id; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:properties_update_dict; 72, identifier:prop_name_or_id; 73, identifier:property_value; 74, else_clause; 74, 75; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 88; 78, subscript; 78, 79; 78, 80; 79, identifier:properties_update_dict; 80, attribute; 80, 81; 80, 87; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:model; 84, identifier:property; 85, argument_list; 85, 86; 86, identifier:prop_name_or_id; 87, identifier:id; 88, identifier:property_value; 89, if_statement; 89, 90; 89, 91; 89, 193; 90, identifier:bulk; 91, block; 91, 92; 91, 147; 91, 175; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:r; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:_client; 100, identifier:_request; 101, argument_list; 101, 102; 101, 103; 101, 111; 101, 139; 102, string:'POST'; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:_client; 108, identifier:_build_url; 109, argument_list; 109, 110; 110, string:'parts'; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:data; 113, call; 113, 114; 113, 115; 114, identifier:dict; 115, argument_list; 115, 116; 115, 119; 115, 124; 115, 129; 115, 137; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:name; 118, identifier:name; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:model; 121, attribute; 121, 122; 121, 123; 122, identifier:model; 123, identifier:id; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:parent; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:id; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:properties; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:json; 134, identifier:dumps; 135, argument_list; 135, 136; 136, identifier:properties_update_dict; 137, dictionary_splat; 137, 138; 138, identifier:kwargs; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:params; 141, call; 141, 142; 141, 143; 142, identifier:dict; 143, argument_list; 143, 144; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:select_action; 146, identifier:action; 147, if_statement; 147, 148; 147, 157; 147, 158; 148, comparison_operator:!=; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:r; 151, identifier:status_code; 152, attribute; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:requests; 155, identifier:codes; 156, identifier:created; 157, comment; 158, block; 158, 159; 159, raise_statement; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:APIError; 162, argument_list; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, string:'{}: {}'; 166, identifier:format; 167, argument_list; 167, 168; 167, 172; 168, call; 168, 169; 168, 170; 169, identifier:str; 170, argument_list; 170, 171; 171, identifier:r; 172, attribute; 172, 173; 172, 174; 173, identifier:r; 174, identifier:content; 175, return_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:Part; 178, argument_list; 178, 179; 178, 188; 179, subscript; 179, 180; 179, 187; 180, subscript; 180, 181; 180, 186; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:r; 184, identifier:json; 185, argument_list; 186, string:'results'; 187, integer:0; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:client; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:_client; 193, else_clause; 193, 194; 193, 195; 194, comment; 195, block; 195, 196; 195, 208; 195, 209; 195, 221; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:new_part; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:add; 203, argument_list; 203, 204; 203, 205; 204, identifier:model; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:name; 207, identifier:name; 208, comment; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:new_part; 213, identifier:update; 214, argument_list; 214, 215; 214, 218; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:update_dict; 217, identifier:update_dict; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:bulk; 220, identifier:bulk; 221, return_statement; 221, 222; 222, identifier:new_part | def add_with_properties(self, model, name=None, update_dict=None, bulk=True, **kwargs):
"""
Add a part and update its properties in one go.
In order to prevent the backend from updating the frontend you may add `suppress_kevents=True` as
additional keyword=value argument to this method. This will improve performance of the backend
against a trade-off that someone looking at the frontend won't notice any changes unless the page
is refreshed.
:param model: model of the part which to add a new instance, should follow the model tree in KE-chain
:type model: :class:`Part`
:param name: (optional) name provided for the new instance as string otherwise use the name of the model
:type name: basestring or None
:param update_dict: dictionary with keys being property names (str) or property_id (from the property models)
and values being property values
:type update_dict: dict or None
:param bulk: True to use the bulk_update_properties API endpoint for KE-chain versions later then 2.1.0b
:type bulk: boolean or None
:param kwargs: (optional) additional keyword arguments that will be passed inside the update request
:type kwargs: dict or None
:return: the newly created :class:`Part`
:raises NotFoundError: when the property name is not a valid property of this part
:raises APIError: in case an Error occurs
Examples
--------
>>> bike = client.scope('Bike Project').part('Bike')
>>> wheel_model = client.scope('Bike Project').model('Wheel')
>>> bike.add_with_properties(wheel_model, 'Wooden Wheel', {'Spokes': 11, 'Material': 'Wood'})
"""
if self.category != Category.INSTANCE:
raise APIError("Part should be of category INSTANCE")
name = name or model.name
action = 'new_instance_with_properties'
properties_update_dict = dict()
for prop_name_or_id, property_value in update_dict.items():
if is_uuid(prop_name_or_id):
properties_update_dict[prop_name_or_id] = property_value
else:
properties_update_dict[model.property(prop_name_or_id).id] = property_value
if bulk:
r = self._client._request('POST', self._client._build_url('parts'),
data=dict(
name=name,
model=model.id,
parent=self.id,
properties=json.dumps(properties_update_dict),
**kwargs
),
params=dict(select_action=action))
if r.status_code != requests.codes.created: # pragma: no cover
raise APIError('{}: {}'.format(str(r), r.content))
return Part(r.json()['results'][0], client=self._client)
else: # do the old way
new_part = self.add(model, name=name) # type: Part
new_part.update(update_dict=update_dict, bulk=bulk)
return new_part |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:move; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:target_parent; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:include_children; 11, True; 12, default_parameter; 12, 13; 12, 14; 13, identifier:include_instances; 14, True; 15, block; 15, 16; 15, 18; 15, 28; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 21; 19, not_operator; 19, 20; 20, identifier:name; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:name; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:name; 28, if_statement; 28, 29; 28, 44; 28, 131; 28, 193; 29, boolean_operator:and; 29, 30; 29, 37; 30, comparison_operator:==; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:category; 34, attribute; 34, 35; 34, 36; 35, identifier:Category; 36, identifier:MODEL; 37, comparison_operator:==; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:target_parent; 40, identifier:category; 41, attribute; 41, 42; 41, 43; 42, identifier:Category; 43, identifier:MODEL; 44, block; 44, 45; 44, 63; 44, 123; 44, 129; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:moved_model; 48, call; 48, 49; 48, 50; 49, identifier:relocate_model; 50, argument_list; 50, 51; 50, 54; 50, 57; 50, 60; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:part; 53, identifier:self; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:target_parent; 56, identifier:target_parent; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:name; 59, identifier:name; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:include_children; 62, identifier:include_children; 63, if_statement; 63, 64; 63, 65; 64, identifier:include_instances; 65, block; 65, 66; 65, 77; 65, 88; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:retrieve_instances_to_copied; 69, call; 69, 70; 69, 71; 70, identifier:list; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:instances; 76, argument_list; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:retrieve_parent_instances; 80, call; 80, 81; 80, 82; 81, identifier:list; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:target_parent; 86, identifier:instances; 87, argument_list; 88, for_statement; 88, 89; 88, 90; 88, 91; 89, identifier:parent_instance; 90, identifier:retrieve_parent_instances; 91, block; 91, 92; 92, for_statement; 92, 93; 92, 94; 92, 95; 93, identifier:instance; 94, identifier:retrieve_instances_to_copied; 95, block; 95, 96; 95, 102; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:instance; 100, identifier:populate_descendants; 101, argument_list; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:move_part_instance; 105, argument_list; 105, 106; 105, 109; 105, 112; 105, 115; 105, 120; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:part_instance; 108, identifier:instance; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:target_parent; 111, identifier:parent_instance; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:part_model; 114, identifier:self; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:name; 117, attribute; 117, 118; 117, 119; 118, identifier:instance; 119, identifier:name; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:include_children; 122, identifier:include_children; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:delete; 128, argument_list; 129, return_statement; 129, 130; 130, identifier:moved_model; 131, elif_clause; 131, 132; 131, 147; 132, boolean_operator:and; 132, 133; 132, 140; 133, comparison_operator:==; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:category; 137, attribute; 137, 138; 137, 139; 138, identifier:Category; 139, identifier:INSTANCE; 140, comparison_operator:==; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:target_parent; 143, identifier:category; 144, attribute; 144, 145; 144, 146; 145, identifier:Category; 146, identifier:INSTANCE; 147, block; 147, 148; 147, 166; 147, 191; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:moved_instance; 151, call; 151, 152; 151, 153; 152, identifier:relocate_instance; 153, argument_list; 153, 154; 153, 157; 153, 160; 153, 163; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:part; 156, identifier:self; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:target_parent; 159, identifier:target_parent; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:name; 162, identifier:name; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:include_children; 165, identifier:include_children; 166, try_statement; 166, 167; 166, 174; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:delete; 173, argument_list; 174, except_clause; 174, 175; 174, 176; 175, identifier:APIError; 176, block; 176, 177; 176, 185; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:model_of_instance; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:model; 184, argument_list; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:model_of_instance; 189, identifier:delete; 190, argument_list; 191, return_statement; 191, 192; 192, identifier:moved_instance; 193, else_clause; 193, 194; 194, block; 194, 195; 195, raise_statement; 195, 196; 196, call; 196, 197; 196, 198; 197, identifier:IllegalArgumentError; 198, argument_list; 198, 199; 199, string:'part "{}" and target parent "{}" must have the same category' | def move(self, target_parent, name=None, include_children=True, include_instances=True):
"""
Move the `Part` to target parent, both of them the same category.
.. versionadded:: 2.3
:param target_parent: `Part` object under which the desired `Part` is moved
:type target_parent: :class:`Part`
:param name: how the moved top-level `Part` should be called
:type name: basestring
:param include_children: True to move also the descendants of `Part`. If False, the children will be lost.
:type include_children: bool
:param include_instances: True to move also the instances of `Part` to ALL the instances of target_parent.
:type include_instances: bool
:returns: moved :class:`Part` model.
:raises IllegalArgumentError: if part and target_parent have different `Category`
:raises IllegalArgumentError: if target_parent is descendant of part
Example
-------
>>> model_to_move = client.model(name='Model to be moved')
>>> bike = client.model('Bike')
>>> model_to_move.move(target_parent=bike, name='Moved model',
>>> include_children=True,
>>> include_instances=True)
"""
if not name:
name = self.name
if self.category == Category.MODEL and target_parent.category == Category.MODEL:
moved_model = relocate_model(part=self, target_parent=target_parent, name=name,
include_children=include_children)
if include_instances:
retrieve_instances_to_copied = list(self.instances())
retrieve_parent_instances = list(target_parent.instances())
for parent_instance in retrieve_parent_instances:
for instance in retrieve_instances_to_copied:
instance.populate_descendants()
move_part_instance(part_instance=instance, target_parent=parent_instance,
part_model=self, name=instance.name, include_children=include_children)
self.delete()
return moved_model
elif self.category == Category.INSTANCE and target_parent.category == Category.INSTANCE:
moved_instance = relocate_instance(part=self, target_parent=target_parent, name=name,
include_children=include_children)
try:
self.delete()
except APIError:
model_of_instance = self.model()
model_of_instance.delete()
return moved_instance
else:
raise IllegalArgumentError('part "{}" and target parent "{}" must have the same category') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 69; 2, function_name:plot; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 3, 36; 3, 39; 3, 42; 3, 45; 3, 48; 3, 51; 3, 54; 3, 57; 3, 60; 3, 63; 3, 66; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:workflow; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:view; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:depth; 13, unary_operator:-; 13, 14; 14, integer:1; 15, default_parameter; 15, 16; 15, 17; 16, identifier:name; 17, identifier:NONE; 18, default_parameter; 18, 19; 18, 20; 19, identifier:comment; 20, identifier:NONE; 21, default_parameter; 21, 22; 21, 23; 22, identifier:format; 23, identifier:NONE; 24, default_parameter; 24, 25; 24, 26; 25, identifier:engine; 26, identifier:NONE; 27, default_parameter; 27, 28; 27, 29; 28, identifier:encoding; 29, identifier:NONE; 30, default_parameter; 30, 31; 30, 32; 31, identifier:graph_attr; 32, identifier:NONE; 33, default_parameter; 33, 34; 33, 35; 34, identifier:node_attr; 35, identifier:NONE; 36, default_parameter; 36, 37; 36, 38; 37, identifier:edge_attr; 38, identifier:NONE; 39, default_parameter; 39, 40; 39, 41; 40, identifier:body; 41, identifier:NONE; 42, default_parameter; 42, 43; 42, 44; 43, identifier:node_styles; 44, identifier:NONE; 45, default_parameter; 45, 46; 45, 47; 46, identifier:node_data; 47, identifier:NONE; 48, default_parameter; 48, 49; 48, 50; 49, identifier:node_function; 50, identifier:NONE; 51, default_parameter; 51, 52; 51, 53; 52, identifier:edge_data; 53, identifier:NONE; 54, default_parameter; 54, 55; 54, 56; 55, identifier:max_lines; 56, identifier:NONE; 57, default_parameter; 57, 58; 57, 59; 58, identifier:max_width; 59, identifier:NONE; 60, default_parameter; 60, 61; 60, 62; 61, identifier:directory; 62, None; 63, default_parameter; 63, 64; 63, 65; 64, identifier:sites; 65, None; 66, default_parameter; 66, 67; 66, 68; 67, identifier:index; 68, False; 69, block; 69, 70; 69, 72; 69, 103; 69, 148; 69, 168; 69, 175; 69, 182; 69, 205; 69, 211; 69, 226; 69, 282; 70, expression_statement; 70, 71; 71, comment; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:d; 75, dictionary; 75, 76; 75, 79; 75, 82; 75, 85; 75, 88; 75, 91; 75, 94; 75, 97; 75, 100; 76, pair; 76, 77; 76, 78; 77, string:'name'; 78, identifier:name; 79, pair; 79, 80; 79, 81; 80, string:'comment'; 81, identifier:comment; 82, pair; 82, 83; 82, 84; 83, string:'format'; 84, identifier:format; 85, pair; 85, 86; 85, 87; 86, string:'engine'; 87, identifier:engine; 88, pair; 88, 89; 88, 90; 89, string:'encoding'; 90, identifier:encoding; 91, pair; 91, 92; 91, 93; 92, string:'graph_attr'; 93, identifier:graph_attr; 94, pair; 94, 95; 94, 96; 95, string:'node_attr'; 96, identifier:node_attr; 97, pair; 97, 98; 97, 99; 98, string:'edge_attr'; 99, identifier:edge_attr; 100, pair; 100, 101; 100, 102; 101, string:'body'; 102, identifier:body; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:options; 106, dictionary; 106, 107; 106, 128; 106, 131; 106, 134; 106, 137; 106, 140; 106, 143; 106, 144; 106, 147; 107, pair; 107, 108; 107, 109; 108, string:'digraph'; 109, boolean_operator:or; 109, 110; 109, 127; 110, dictionary_comprehension; 110, 111; 110, 114; 110, 123; 111, pair; 111, 112; 111, 113; 112, identifier:k; 113, identifier:v; 114, for_in_clause; 114, 115; 114, 118; 115, pattern_list; 115, 116; 115, 117; 116, identifier:k; 117, identifier:v; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:d; 121, identifier:items; 122, argument_list; 123, if_clause; 123, 124; 124, comparison_operator:is; 124, 125; 124, 126; 125, identifier:v; 126, identifier:NONE; 127, identifier:NONE; 128, pair; 128, 129; 128, 130; 129, string:'node_styles'; 130, identifier:node_styles; 131, pair; 131, 132; 131, 133; 132, string:'node_data'; 133, identifier:node_data; 134, pair; 134, 135; 134, 136; 135, string:'node_function'; 136, identifier:node_function; 137, pair; 137, 138; 137, 139; 138, string:'edge_data'; 139, identifier:edge_data; 140, pair; 140, 141; 140, 142; 141, string:'max_lines'; 142, identifier:max_lines; 143, comment; 144, pair; 144, 145; 144, 146; 145, string:'max_width'; 146, identifier:max_width; 147, comment; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:options; 151, dictionary_comprehension; 151, 152; 151, 155; 151, 164; 152, pair; 152, 153; 152, 154; 153, identifier:k; 154, identifier:v; 155, for_in_clause; 155, 156; 155, 159; 156, pattern_list; 156, 157; 156, 158; 157, identifier:k; 158, identifier:v; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:options; 162, identifier:items; 163, argument_list; 164, if_clause; 164, 165; 165, comparison_operator:is; 165, 166; 165, 167; 166, identifier:v; 167, identifier:NONE; 168, import_from_statement; 168, 169; 168, 173; 169, relative_import; 169, 170; 169, 171; 170, import_prefix; 171, dotted_name; 171, 172; 172, identifier:drw; 173, dotted_name; 173, 174; 174, identifier:SiteMap; 175, import_from_statement; 175, 176; 175, 180; 176, relative_import; 176, 177; 176, 178; 177, import_prefix; 178, dotted_name; 178, 179; 179, identifier:sol; 180, dotted_name; 180, 181; 181, identifier:Solution; 182, if_statement; 182, 183; 182, 192; 182, 197; 183, boolean_operator:and; 183, 184; 183, 187; 184, comparison_operator:is; 184, 185; 184, 186; 185, identifier:workflow; 186, None; 187, call; 187, 188; 187, 189; 188, identifier:isinstance; 189, argument_list; 189, 190; 189, 191; 190, identifier:self; 191, identifier:Solution; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:workflow; 196, True; 197, else_clause; 197, 198; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:workflow; 202, boolean_operator:or; 202, 203; 202, 204; 203, identifier:workflow; 204, False; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:sitemap; 208, call; 208, 209; 208, 210; 209, identifier:SiteMap; 210, argument_list; 211, expression_statement; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:sitemap; 215, identifier:add_items; 216, argument_list; 216, 217; 216, 218; 216, 221; 216, 224; 217, identifier:self; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:workflow; 220, identifier:workflow; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:depth; 223, identifier:depth; 224, dictionary_splat; 224, 225; 225, identifier:options; 226, if_statement; 226, 227; 226, 228; 227, identifier:view; 228, block; 228, 229; 228, 232; 228, 242; 229, import_statement; 229, 230; 230, dotted_name; 230, 231; 231, identifier:tempfile; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:directory; 235, boolean_operator:or; 235, 236; 235, 237; 236, identifier:directory; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:tempfile; 240, identifier:mkdtemp; 241, argument_list; 242, if_statement; 242, 243; 242, 246; 242, 262; 243, comparison_operator:is; 243, 244; 243, 245; 244, identifier:sites; 245, None; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:sitemap; 251, identifier:render; 252, argument_list; 252, 253; 252, 256; 252, 259; 253, keyword_argument; 253, 254; 253, 255; 254, identifier:directory; 255, identifier:directory; 256, keyword_argument; 256, 257; 256, 258; 257, identifier:view; 258, True; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:index; 261, identifier:index; 262, else_clause; 262, 263; 263, block; 263, 264; 264, expression_statement; 264, 265; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:sites; 268, identifier:add; 269, argument_list; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:sitemap; 273, identifier:site; 274, argument_list; 274, 275; 274, 276; 274, 279; 275, identifier:directory; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:view; 278, True; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:index; 281, identifier:index; 282, return_statement; 282, 283; 283, identifier:sitemap | def plot(self, workflow=None, view=True, depth=-1, name=NONE, comment=NONE,
format=NONE, engine=NONE, encoding=NONE, graph_attr=NONE,
node_attr=NONE, edge_attr=NONE, body=NONE, node_styles=NONE,
node_data=NONE, node_function=NONE, edge_data=NONE, max_lines=NONE,
max_width=NONE, directory=None, sites=None, index=False):
"""
Plots the Dispatcher with a graph in the DOT language with Graphviz.
:param workflow:
If True the latest solution will be plotted, otherwise the dmap.
:type workflow: bool, optional
:param view:
Open the rendered directed graph in the DOT language with the sys
default opener.
:type view: bool, optional
:param edge_data:
Edge attributes to view.
:type edge_data: tuple[str], optional
:param node_data:
Data node attributes to view.
:type node_data: tuple[str], optional
:param node_function:
Function node attributes to view.
:type node_function: tuple[str], optional
:param node_styles:
Default node styles according to graphviz node attributes.
:type node_styles: dict[str|Token, dict[str, str]]
:param depth:
Depth of sub-dispatch plots. If negative all levels are plotted.
:type depth: int, optional
:param name:
Graph name used in the source code.
:type name: str
:param comment:
Comment added to the first line of the source.
:type comment: str
:param directory:
(Sub)directory for source saving and rendering.
:type directory: str, optional
:param format:
Rendering output format ('pdf', 'png', ...).
:type format: str, optional
:param engine:
Layout command used ('dot', 'neato', ...).
:type engine: str, optional
:param encoding:
Encoding for saving the source.
:type encoding: str, optional
:param graph_attr:
Dict of (attribute, value) pairs for the graph.
:type graph_attr: dict, optional
:param node_attr:
Dict of (attribute, value) pairs set for all nodes.
:type node_attr: dict, optional
:param edge_attr:
Dict of (attribute, value) pairs set for all edges.
:type edge_attr: dict, optional
:param body:
Dict of (attribute, value) pairs to add to the graph body.
:type body: dict, optional
:param directory:
Where is the generated Flask app root located?
:type directory: str, optional
:param sites:
A set of :class:`~schedula.utils.drw.Site` to maintain alive the
backend server.
:type sites: set[~schedula.utils.drw.Site], optional
:param index:
Add the site index as first page?
:type index: bool, optional
:param max_lines:
Maximum number of lines for rendering node attributes.
:type max_lines: int, optional
:param max_width:
Maximum number of characters in a line to render node attributes.
:type max_width: int, optional
:param view:
Open the main page of the site?
:type view: bool, optional
:return:
A SiteMap.
:rtype: schedula.utils.drw.SiteMap
Example:
.. dispatcher:: dsp
:opt: graph_attr={'ratio': '1'}
:code:
>>> from schedula import Dispatcher
>>> dsp = Dispatcher(name='Dispatcher')
>>> def fun(a):
... return a + 1, a - 1
>>> dsp.add_function('fun', fun, ['a'], ['b', 'c'])
'fun'
>>> dsp.plot(view=False, graph_attr={'ratio': '1'})
SiteMap([(Dispatcher, SiteMap())])
"""
d = {
'name': name, 'comment': comment, 'format': format,
'engine': engine, 'encoding': encoding, 'graph_attr': graph_attr,
'node_attr': node_attr, 'edge_attr': edge_attr, 'body': body,
}
options = {
'digraph': {k: v for k, v in d.items() if v is not NONE} or NONE,
'node_styles': node_styles,
'node_data': node_data,
'node_function': node_function,
'edge_data': edge_data,
'max_lines': max_lines, # 5
'max_width': max_width, # 200
}
options = {k: v for k, v in options.items() if v is not NONE}
from .drw import SiteMap
from .sol import Solution
if workflow is None and isinstance(self, Solution):
workflow = True
else:
workflow = workflow or False
sitemap = SiteMap()
sitemap.add_items(self, workflow=workflow, depth=depth, **options)
if view:
import tempfile
directory = directory or tempfile.mkdtemp()
if sites is None:
sitemap.render(directory=directory, view=True, index=index)
else:
sites.add(sitemap.site(directory, view=True, index=index))
return sitemap |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:lookup_url; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:url; 6, identifier:klass; 7, default_parameter; 7, 8; 7, 9; 8, identifier:module; 9, None; 10, block; 10, 11; 10, 13; 10, 23; 10, 30; 11, expression_statement; 11, 12; 12, string:'''
Gets a proxy reference to the actor indicated by the URL in the
parameters. It can be a local reference or a remote direction to
another host.
This method can be called remotely synchronously.
:param srt. url: address that identifies an actor.
:param class klass: the class of the actor.
:param srt. module: if the actor class is not in the calling module,
you need to specify the module where it is here. Also, the *klass*
parameter change to be a string.
:return: :class:`~.Proxy` of the actor requested.
:raises: :class:`NotFoundError`, if the URL specified do not
correspond to any actor in the host.
:raises: :class:`HostDownError` if the host is down.
:raises: :class:`HostError` if there is an error looking for
the actor in another server.
'''; 13, if_statement; 13, 14; 13, 18; 14, not_operator; 14, 15; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:alive; 18, block; 18, 19; 19, raise_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:HostDownError; 22, argument_list; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:aurl; 26, call; 26, 27; 26, 28; 27, identifier:urlparse; 28, argument_list; 28, 29; 29, identifier:url; 30, if_statement; 30, 31; 30, 37; 30, 65; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:is_local; 35, argument_list; 35, 36; 36, identifier:aurl; 37, block; 37, 38; 38, if_statement; 38, 39; 38, 48; 38, 54; 39, comparison_operator:not; 39, 40; 39, 41; 40, identifier:url; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:actors; 46, identifier:keys; 47, argument_list; 48, block; 48, 49; 49, raise_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:NotFoundError; 52, argument_list; 52, 53; 53, identifier:url; 54, else_clause; 54, 55; 55, block; 55, 56; 56, return_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:Proxy; 59, argument_list; 59, 60; 60, subscript; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:actors; 64, identifier:url; 65, else_clause; 65, 66; 66, block; 66, 67; 67, try_statement; 67, 68; 67, 181; 67, 185; 68, block; 68, 69; 68, 79; 68, 163; 68, 176; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:dispatcher; 72, subscript; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:actors; 76, attribute; 76, 77; 76, 78; 77, identifier:aurl; 78, identifier:scheme; 79, if_statement; 79, 80; 79, 83; 79, 137; 79, 154; 80, comparison_operator:is; 80, 81; 80, 82; 81, identifier:module; 82, None; 83, block; 83, 84; 84, try_statement; 84, 85; 84, 111; 85, block; 85, 86; 85, 103; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:module_; 89, call; 89, 90; 89, 91; 90, identifier:__import__; 91, argument_list; 91, 92; 91, 93; 91, 96; 91, 99; 91, 101; 92, identifier:module; 93, call; 93, 94; 93, 95; 94, identifier:globals; 95, argument_list; 96, call; 96, 97; 96, 98; 97, identifier:locals; 98, argument_list; 99, list:[klass]; 99, 100; 100, identifier:klass; 101, unary_operator:-; 101, 102; 102, integer:1; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:klass_; 106, call; 106, 107; 106, 108; 107, identifier:getattr; 108, argument_list; 108, 109; 108, 110; 109, identifier:module_; 110, identifier:klass; 111, except_clause; 111, 112; 111, 113; 111, 114; 112, identifier:Exception; 113, identifier:e; 114, block; 114, 115; 115, raise_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:HostError; 118, argument_list; 118, 119; 119, binary_operator:+; 119, 120; 119, 133; 120, binary_operator:+; 120, 121; 120, 132; 121, binary_operator:+; 121, 122; 121, 131; 122, binary_operator:+; 122, 123; 122, 130; 123, binary_operator:+; 123, 124; 123, 129; 124, binary_operator:+; 124, 125; 124, 128; 125, binary_operator:+; 125, 126; 125, 127; 126, string:"At lookup_url: "; 127, string:"Import failed for module "; 128, identifier:module; 129, string:", class "; 130, identifier:klass; 131, string:". Check this values for the lookup."; 132, string:" ERROR: "; 133, call; 133, 134; 133, 135; 134, identifier:str; 135, argument_list; 135, 136; 136, identifier:e; 137, elif_clause; 137, 138; 137, 149; 138, call; 138, 139; 138, 140; 139, identifier:isinstance; 140, argument_list; 140, 141; 140, 142; 141, identifier:klass; 142, tuple; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:types; 145, identifier:TypeType; 146, attribute; 146, 147; 146, 148; 147, identifier:types; 148, identifier:ClassType; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:klass_; 153, identifier:klass; 154, else_clause; 154, 155; 155, block; 155, 156; 156, raise_statement; 156, 157; 157, call; 157, 158; 157, 159; 158, identifier:HostError; 159, argument_list; 159, 160; 160, binary_operator:+; 160, 161; 160, 162; 161, string:"The class specified to look up is"; 162, string:" not a class."; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:remote_actor; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:actor; 169, identifier:ActorRef; 170, argument_list; 170, 171; 170, 172; 170, 173; 171, identifier:url; 172, identifier:klass_; 173, attribute; 173, 174; 173, 175; 174, identifier:dispatcher; 175, identifier:channel; 176, return_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:Proxy; 179, argument_list; 179, 180; 180, identifier:remote_actor; 181, except_clause; 181, 182; 181, 183; 182, identifier:HostError; 183, block; 183, 184; 184, raise_statement; 185, except_clause; 185, 186; 185, 187; 185, 188; 186, identifier:Exception; 187, identifier:e; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:HostError; 192, argument_list; 192, 193; 193, binary_operator:+; 193, 194; 193, 199; 194, binary_operator:+; 194, 195; 194, 198; 195, binary_operator:+; 195, 196; 195, 197; 196, string:"ERROR looking for the actor on another "; 197, string:"server. Hosts must "; 198, string:"be in http to work properly. "; 199, call; 199, 200; 199, 201; 200, identifier:str; 201, argument_list; 201, 202; 202, identifier:e | def lookup_url(self, url, klass, module=None):
'''
Gets a proxy reference to the actor indicated by the URL in the
parameters. It can be a local reference or a remote direction to
another host.
This method can be called remotely synchronously.
:param srt. url: address that identifies an actor.
:param class klass: the class of the actor.
:param srt. module: if the actor class is not in the calling module,
you need to specify the module where it is here. Also, the *klass*
parameter change to be a string.
:return: :class:`~.Proxy` of the actor requested.
:raises: :class:`NotFoundError`, if the URL specified do not
correspond to any actor in the host.
:raises: :class:`HostDownError` if the host is down.
:raises: :class:`HostError` if there is an error looking for
the actor in another server.
'''
if not self.alive:
raise HostDownError()
aurl = urlparse(url)
if self.is_local(aurl):
if url not in self.actors.keys():
raise NotFoundError(url)
else:
return Proxy(self.actors[url])
else:
try:
dispatcher = self.actors[aurl.scheme]
if module is not None:
try:
module_ = __import__(module, globals(), locals(),
[klass], -1)
klass_ = getattr(module_, klass)
except Exception, e:
raise HostError("At lookup_url: " +
"Import failed for module " + module +
", class " + klass +
". Check this values for the lookup." +
" ERROR: " + str(e))
elif isinstance(klass, (types.TypeType, types.ClassType)):
klass_ = klass
else:
raise HostError("The class specified to look up is" +
" not a class.")
remote_actor = actor.ActorRef(url, klass_, dispatcher.channel)
return Proxy(remote_actor)
except HostError:
raise
except Exception, e:
raise HostError("ERROR looking for the actor on another " +
"server. Hosts must " +
"be in http to work properly. " + str(e)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:prepare_rapid_streamflow; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:path_to_rapid_qout; 6, identifier:connection_list_file; 7, block; 7, 8; 7, 10; 7, 23; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:ihg_filename; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, string:'{0}.ihg'; 16, identifier:format; 17, argument_list; 17, 18; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:project_manager; 22, identifier:name; 23, with_statement; 23, 24; 23, 34; 23, 35; 24, with_clause; 24, 25; 25, with_item; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:tmp_chdir; 28, argument_list; 28, 29; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:project_manager; 33, identifier:project_directory; 34, comment; 35, block; 35, 36; 35, 40; 35, 207; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:time_index_range; 39, list:[]; 40, with_statement; 40, 41; 40, 55; 41, with_clause; 41, 42; 42, with_item; 42, 43; 43, as_pattern; 43, 44; 43, 53; 44, call; 44, 45; 44, 46; 45, identifier:RAPIDDataset; 46, argument_list; 46, 47; 46, 48; 47, identifier:path_to_rapid_qout; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:out_tzinfo; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:tz; 53, as_pattern_target; 53, 54; 54, identifier:qout_nc; 55, block; 55, 56; 55, 74; 55, 138; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:time_index_range; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:qout_nc; 62, identifier:get_time_index_range; 63, argument_list; 63, 64; 63, 69; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:date_search_start; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:simulation_start; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:date_search_end; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:simulation_end; 74, if_statement; 74, 75; 74, 81; 75, comparison_operator:>; 75, 76; 75, 80; 76, call; 76, 77; 76, 78; 77, identifier:len; 78, argument_list; 78, 79; 79, identifier:time_index_range; 80, integer:0; 81, block; 81, 82; 81, 96; 81, 97; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:time_array; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:qout_nc; 88, identifier:get_time_array; 89, argument_list; 89, 90; 89, 93; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:return_datetime; 92, True; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:time_index_array; 95, identifier:time_index_range; 96, comment; 97, if_statement; 97, 98; 97, 103; 98, comparison_operator:is; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:simulation_start; 102, None; 103, block; 103, 104; 104, if_statement; 104, 105; 104, 112; 105, comparison_operator:==; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:simulation_start; 109, subscript; 109, 110; 109, 111; 110, identifier:time_array; 111, integer:0; 112, block; 112, 113; 112, 122; 112, 130; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:log; 117, identifier:warning; 118, argument_list; 118, 119; 119, concatenated_string; 119, 120; 119, 121; 120, string:"First timestep of streamflow skipped "; 121, string:"in order for GSSHA to capture the streamflow."; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:time_index_range; 125, subscript; 125, 126; 125, 127; 126, identifier:time_index_range; 127, slice; 127, 128; 127, 129; 128, integer:1; 129, colon; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:time_array; 133, subscript; 133, 134; 133, 135; 134, identifier:time_array; 135, slice; 135, 136; 135, 137; 136, integer:1; 137, colon; 138, if_statement; 138, 139; 138, 145; 138, 198; 139, comparison_operator:>; 139, 140; 139, 144; 140, call; 140, 141; 140, 142; 141, identifier:len; 142, argument_list; 142, 143; 143, identifier:time_index_range; 144, integer:0; 145, block; 145, 146; 145, 152; 145, 166; 145, 182; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:start_datetime; 149, subscript; 149, 150; 149, 151; 150, identifier:time_array; 151, integer:0; 152, if_statement; 152, 153; 152, 158; 153, comparison_operator:is; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:simulation_start; 157, None; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:_update_simulation_start; 164, argument_list; 164, 165; 165, identifier:start_datetime; 166, if_statement; 166, 167; 166, 172; 167, comparison_operator:is; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:simulation_end; 171, None; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:simulation_end; 178, subscript; 178, 179; 178, 180; 179, identifier:time_array; 180, unary_operator:-; 180, 181; 181, integer:1; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:qout_nc; 186, identifier:write_flows_to_gssha_time_series_ihg; 187, argument_list; 187, 188; 187, 189; 187, 190; 187, 193; 188, identifier:ihg_filename; 189, identifier:connection_list_file; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:date_search_start; 192, identifier:start_datetime; 193, keyword_argument; 193, 194; 193, 195; 194, identifier:date_search_end; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:simulation_end; 198, else_clause; 198, 199; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:log; 204, identifier:warning; 205, argument_list; 205, 206; 206, string:"No streamflow values found in time range ..."; 207, if_statement; 207, 208; 207, 214; 207, 215; 207, 267; 208, comparison_operator:>; 208, 209; 208, 213; 209, call; 209, 210; 209, 211; 210, identifier:len; 211, argument_list; 211, 212; 212, identifier:time_index_range; 213, integer:0; 214, comment; 215, block; 215, 216; 215, 222; 215, 237; 215, 246; 215, 247; 215, 260; 215, 261; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:_update_simulation_start_cards; 221, argument_list; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:self; 226, identifier:_update_card; 227, argument_list; 227, 228; 227, 229; 228, string:"END_TIME"; 229, call; 229, 230; 229, 235; 230, attribute; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:simulation_end; 234, identifier:strftime; 235, argument_list; 235, 236; 236, string:"%Y %m %d %H %M"; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:self; 241, identifier:_update_card; 242, argument_list; 242, 243; 242, 244; 242, 245; 243, string:"CHAN_POINT_INPUT"; 244, identifier:ihg_filename; 245, True; 246, comment; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:set_simulation_duration; 252, argument_list; 252, 253; 253, binary_operator:-; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:self; 256, identifier:simulation_end; 257, attribute; 257, 258; 257, 259; 258, identifier:self; 259, identifier:simulation_start; 260, comment; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:self; 265, identifier:_update_gmt; 266, argument_list; 267, else_clause; 267, 268; 267, 269; 268, comment; 269, block; 269, 270; 269, 277; 270, expression_statement; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:os; 274, identifier:remove; 275, argument_list; 275, 276; 276, identifier:ihg_filename; 277, expression_statement; 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:project_manager; 283, identifier:deleteCard; 284, argument_list; 284, 285; 284, 286; 285, string:'CHAN_POINT_INPUT'; 286, attribute; 286, 287; 286, 288; 287, identifier:self; 288, identifier:db_session | def prepare_rapid_streamflow(self, path_to_rapid_qout, connection_list_file):
"""
Prepares RAPID streamflow for GSSHA simulation
"""
ihg_filename = '{0}.ihg'.format(self.project_manager.name)
with tmp_chdir(self.project_manager.project_directory):
# write out IHG file
time_index_range = []
with RAPIDDataset(path_to_rapid_qout, out_tzinfo=self.tz) as qout_nc:
time_index_range = qout_nc.get_time_index_range(date_search_start=self.simulation_start,
date_search_end=self.simulation_end)
if len(time_index_range) > 0:
time_array = qout_nc.get_time_array(return_datetime=True,
time_index_array=time_index_range)
# GSSHA STARTS INGESTING STREAMFLOW AT SECOND TIME STEP
if self.simulation_start is not None:
if self.simulation_start == time_array[0]:
log.warning("First timestep of streamflow skipped "
"in order for GSSHA to capture the streamflow.")
time_index_range = time_index_range[1:]
time_array = time_array[1:]
if len(time_index_range) > 0:
start_datetime = time_array[0]
if self.simulation_start is None:
self._update_simulation_start(start_datetime)
if self.simulation_end is None:
self.simulation_end = time_array[-1]
qout_nc.write_flows_to_gssha_time_series_ihg(ihg_filename,
connection_list_file,
date_search_start=start_datetime,
date_search_end=self.simulation_end,
)
else:
log.warning("No streamflow values found in time range ...")
if len(time_index_range) > 0:
# update cards
self._update_simulation_start_cards()
self._update_card("END_TIME", self.simulation_end.strftime("%Y %m %d %H %M"))
self._update_card("CHAN_POINT_INPUT", ihg_filename, True)
# update duration
self.set_simulation_duration(self.simulation_end-self.simulation_start)
# UPDATE GMT CARD
self._update_gmt()
else:
# cleanup
os.remove(ihg_filename)
self.project_manager.deleteCard('CHAN_POINT_INPUT', self.db_session) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:stencil; 3, parameters; 3, 4; 4, dictionary_splat_pattern; 4, 5; 5, identifier:kwargs; 6, block; 6, 7; 6, 9; 6, 18; 6, 27; 6, 36; 6, 75; 6, 76; 6, 90; 6, 91; 6, 111; 6, 134; 6, 149; 6, 160; 6, 348; 6, 358; 6, 377; 6, 386; 6, 404; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:alnfile; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:kwargs; 15, identifier:get; 16, argument_list; 16, 17; 17, string:'alnfile'; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:gtypefile; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:kwargs; 24, identifier:get; 25, argument_list; 25, 26; 26, string:'gtypefile'; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:grpfile; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:kwargs; 33, identifier:get; 34, argument_list; 34, 35; 35, string:'grpfile'; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:grpfile; 39, None; 40, block; 40, 41; 40, 53; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:grpfile2chk; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:os; 48, identifier:path; 49, identifier:join; 50, argument_list; 50, 51; 50, 52; 51, identifier:DATA_DIR; 52, string:'ref.gene2transcripts.tsv'; 53, if_statement; 53, 54; 53, 62; 53, 67; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:os; 58, identifier:path; 59, identifier:exists; 60, argument_list; 60, 61; 61, identifier:grpfile2chk; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:grpfile; 66, identifier:grpfile2chk; 67, else_clause; 67, 68; 68, block; 68, 69; 69, print_statement; 69, 70; 69, 74; 70, chevron; 70, 71; 71, attribute; 71, 72; 71, 73; 72, identifier:sys; 73, identifier:stderr; 74, string:'[gbrs::stencil] A group file is *not* given. Genotype will be stenciled as is.'; 75, comment; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:alnmat; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:emase; 82, identifier:AlignmentPropertyMatrix; 83, argument_list; 83, 84; 83, 87; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:h5file; 86, identifier:alnfile; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:grpfile; 89, identifier:grpfile; 90, comment; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:hid; 94, call; 94, 95; 94, 96; 95, identifier:dict; 96, argument_list; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:zip; 99, argument_list; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:alnmat; 102, identifier:hname; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:np; 106, identifier:arange; 107, argument_list; 107, 108; 108, attribute; 108, 109; 108, 110; 109, identifier:alnmat; 110, identifier:num_haplotypes; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:gid; 114, call; 114, 115; 114, 116; 115, identifier:dict; 116, argument_list; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:zip; 119, argument_list; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:alnmat; 122, identifier:gname; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:np; 126, identifier:arange; 127, argument_list; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:len; 130, argument_list; 130, 131; 131, attribute; 131, 132; 131, 133; 132, identifier:alnmat; 133, identifier:gname; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:gtmask; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:np; 140, identifier:zeros; 141, argument_list; 141, 142; 142, tuple; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:alnmat; 145, identifier:num_haplotypes; 146, attribute; 146, 147; 146, 148; 147, identifier:alnmat; 148, identifier:num_loci; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:gtcall_g; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:dict; 155, identifier:fromkeys; 156, argument_list; 156, 157; 157, attribute; 157, 158; 157, 159; 158, identifier:alnmat; 159, identifier:gname; 160, with_statement; 160, 161; 160, 170; 161, with_clause; 161, 162; 162, with_item; 162, 163; 163, as_pattern; 163, 164; 163, 168; 164, call; 164, 165; 164, 166; 165, identifier:open; 166, argument_list; 166, 167; 167, identifier:gtypefile; 168, as_pattern_target; 168, 169; 169, identifier:fh; 170, block; 170, 171; 171, if_statement; 171, 172; 171, 175; 171, 280; 172, comparison_operator:is; 172, 173; 172, 174; 173, identifier:grpfile; 174, None; 175, block; 175, 176; 175, 187; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:gtcall_t; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:dict; 182, identifier:fromkeys; 183, argument_list; 183, 184; 184, attribute; 184, 185; 184, 186; 185, identifier:alnmat; 186, identifier:lname; 187, for_statement; 187, 188; 187, 189; 187, 194; 188, identifier:curline; 189, call; 189, 190; 189, 191; 190, identifier:dropwhile; 191, argument_list; 191, 192; 191, 193; 192, identifier:is_comment; 193, identifier:fh; 194, block; 194, 195; 194, 208; 194, 218; 194, 224; 194, 239; 194, 254; 194, 266; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:item; 198, call; 198, 199; 198, 206; 199, attribute; 199, 200; 199, 205; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:curline; 203, identifier:rstrip; 204, argument_list; 205, identifier:split; 206, argument_list; 206, 207; 207, string:"\t"; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 213; 210, pattern_list; 210, 211; 210, 212; 211, identifier:g; 212, identifier:gt; 213, subscript; 213, 214; 213, 215; 214, identifier:item; 215, slice; 215, 216; 215, 217; 216, colon; 217, integer:2; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 223; 220, subscript; 220, 221; 220, 222; 221, identifier:gtcall_g; 222, identifier:g; 223, identifier:gt; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:hid2set; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:np; 230, identifier:array; 231, argument_list; 231, 232; 232, list_comprehension; 232, 233; 232, 236; 233, subscript; 233, 234; 233, 235; 234, identifier:hid; 235, identifier:c; 236, for_in_clause; 236, 237; 236, 238; 237, identifier:c; 238, identifier:gt; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:tid2set; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:np; 245, identifier:array; 246, argument_list; 246, 247; 247, subscript; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:alnmat; 250, identifier:groups; 251, subscript; 251, 252; 251, 253; 252, identifier:gid; 253, identifier:g; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 265; 256, subscript; 256, 257; 256, 258; 257, identifier:gtmask; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:np; 261, identifier:meshgrid; 262, argument_list; 262, 263; 262, 264; 263, identifier:hid2set; 264, identifier:tid2set; 265, float:1.0; 266, for_statement; 266, 267; 266, 268; 266, 269; 267, identifier:t; 268, identifier:tid2set; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 279; 272, subscript; 272, 273; 272, 274; 273, identifier:gtcall_t; 274, subscript; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:alnmat; 277, identifier:lname; 278, identifier:t; 279, identifier:gt; 280, else_clause; 280, 281; 281, block; 281, 282; 282, for_statement; 282, 283; 282, 284; 282, 289; 283, identifier:curline; 284, call; 284, 285; 284, 286; 285, identifier:dropwhile; 286, argument_list; 286, 287; 286, 288; 287, identifier:is_comment; 288, identifier:fh; 289, block; 289, 290; 289, 303; 289, 313; 289, 319; 289, 334; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:item; 293, call; 293, 294; 293, 301; 294, attribute; 294, 295; 294, 300; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:curline; 298, identifier:rstrip; 299, argument_list; 300, identifier:split; 301, argument_list; 301, 302; 302, string:"\t"; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 308; 305, pattern_list; 305, 306; 305, 307; 306, identifier:g; 307, identifier:gt; 308, subscript; 308, 309; 308, 310; 309, identifier:item; 310, slice; 310, 311; 310, 312; 311, colon; 312, integer:2; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 318; 315, subscript; 315, 316; 315, 317; 316, identifier:gtcall_g; 317, identifier:g; 318, identifier:gt; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:hid2set; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:np; 325, identifier:array; 326, argument_list; 326, 327; 327, list_comprehension; 327, 328; 327, 331; 328, subscript; 328, 329; 328, 330; 329, identifier:hid; 330, identifier:c; 331, for_in_clause; 331, 332; 331, 333; 332, identifier:c; 333, identifier:gt; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 347; 336, subscript; 336, 337; 336, 338; 337, identifier:gtmask; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:np; 341, identifier:meshgrid; 342, argument_list; 342, 343; 342, 344; 343, identifier:hid2set; 344, subscript; 344, 345; 344, 346; 345, identifier:gid; 346, identifier:g; 347, float:1.0; 348, expression_statement; 348, 349; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:alnmat; 352, identifier:multiply; 353, argument_list; 353, 354; 353, 355; 354, identifier:gtmask; 355, keyword_argument; 355, 356; 355, 357; 356, identifier:axis; 357, integer:2; 358, for_statement; 358, 359; 358, 360; 358, 366; 359, identifier:h; 360, call; 360, 361; 360, 362; 361, identifier:xrange; 362, argument_list; 362, 363; 363, attribute; 363, 364; 363, 365; 364, identifier:alnmat; 365, identifier:num_haplotypes; 366, block; 366, 367; 367, expression_statement; 367, 368; 368, call; 368, 369; 368, 376; 369, attribute; 369, 370; 369, 375; 370, subscript; 370, 371; 370, 374; 371, attribute; 371, 372; 371, 373; 372, identifier:alnmat; 373, identifier:data; 374, identifier:h; 375, identifier:eliminate_zeros; 376, argument_list; 377, expression_statement; 377, 378; 378, assignment; 378, 379; 378, 380; 379, identifier:outfile; 380, call; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:kwargs; 383, identifier:get; 384, argument_list; 384, 385; 385, string:'outfile'; 386, if_statement; 386, 387; 386, 390; 387, comparison_operator:is; 387, 388; 387, 389; 388, identifier:outfile; 389, None; 390, block; 390, 391; 391, expression_statement; 391, 392; 392, assignment; 392, 393; 392, 394; 393, identifier:outfile; 394, binary_operator:+; 394, 395; 394, 396; 395, string:'gbrs.stenciled.'; 396, call; 396, 397; 396, 402; 397, attribute; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, identifier:os; 400, identifier:path; 401, identifier:basename; 402, argument_list; 402, 403; 403, identifier:alnfile; 404, expression_statement; 404, 405; 405, call; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:alnmat; 408, identifier:save; 409, argument_list; 409, 410; 410, keyword_argument; 410, 411; 410, 412; 411, identifier:h5file; 412, identifier:outfile | def stencil(**kwargs):
"""
Applying genotype calls to multi-way alignment incidence matrix
:param alnfile: alignment incidence file (h5),
:param gtypefile: genotype calls by GBRS (tsv),
:param grpfile: gene ID to isoform ID mapping info (tsv)
:return: genotyped version of alignment incidence file (h5)
"""
alnfile = kwargs.get('alnfile')
gtypefile = kwargs.get('gtypefile')
grpfile = kwargs.get('grpfile')
if grpfile is None:
grpfile2chk = os.path.join(DATA_DIR, 'ref.gene2transcripts.tsv')
if os.path.exists(grpfile2chk):
grpfile = grpfile2chk
else:
print >> sys.stderr, '[gbrs::stencil] A group file is *not* given. Genotype will be stenciled as is.'
# Load alignment incidence matrix ('alnfile' is assumed to be in multiway transcriptome)
alnmat = emase.AlignmentPropertyMatrix(h5file=alnfile, grpfile=grpfile)
# Load genotype calls
hid = dict(zip(alnmat.hname, np.arange(alnmat.num_haplotypes)))
gid = dict(zip(alnmat.gname, np.arange(len(alnmat.gname))))
gtmask = np.zeros((alnmat.num_haplotypes, alnmat.num_loci))
gtcall_g = dict.fromkeys(alnmat.gname)
with open(gtypefile) as fh:
if grpfile is not None:
gtcall_t = dict.fromkeys(alnmat.lname)
for curline in dropwhile(is_comment, fh):
item = curline.rstrip().split("\t")
g, gt = item[:2]
gtcall_g[g] = gt
hid2set = np.array([hid[c] for c in gt])
tid2set = np.array(alnmat.groups[gid[g]])
gtmask[np.meshgrid(hid2set, tid2set)] = 1.0
for t in tid2set:
gtcall_t[alnmat.lname[t]] = gt
else:
for curline in dropwhile(is_comment, fh):
item = curline.rstrip().split("\t")
g, gt = item[:2]
gtcall_g[g] = gt
hid2set = np.array([hid[c] for c in gt])
gtmask[np.meshgrid(hid2set, gid[g])] = 1.0
alnmat.multiply(gtmask, axis=2)
for h in xrange(alnmat.num_haplotypes):
alnmat.data[h].eliminate_zeros()
outfile = kwargs.get('outfile')
if outfile is None:
outfile = 'gbrs.stenciled.' + os.path.basename(alnfile)
alnmat.save(h5file=outfile) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:split_tracks; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:lat; 5, identifier:lon; 6, list_splat_pattern; 6, 7; 7, identifier:args; 8, block; 8, 9; 8, 11; 8, 15; 8, 29; 8, 40; 8, 316; 8, 353; 9, expression_statement; 9, 10; 10, string:'''assumes eastward motion'''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:tracks; 14, list:[]; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 20; 17, pattern_list; 17, 18; 17, 19; 18, identifier:lt; 19, identifier:ln; 20, expression_list; 20, 21; 20, 25; 21, list:[lat[0]]; 21, 22; 22, subscript; 22, 23; 22, 24; 23, identifier:lat; 24, integer:0; 25, list:[lon[0]]; 25, 26; 26, subscript; 26, 27; 26, 28; 27, identifier:lon; 28, integer:0; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:zz; 32, list_comprehension; 32, 33; 32, 37; 33, list:[z[0]]; 33, 34; 34, subscript; 34, 35; 34, 36; 35, identifier:z; 36, integer:0; 37, for_in_clause; 37, 38; 37, 39; 38, identifier:z; 39, identifier:args; 40, for_statement; 40, 41; 40, 42; 40, 50; 41, identifier:i; 42, call; 42, 43; 42, 44; 43, identifier:range; 44, argument_list; 44, 45; 44, 46; 45, integer:1; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, identifier:lon; 50, block; 50, 51; 50, 60; 50, 79; 50, 94; 50, 112; 50, 130; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:lt; 55, identifier:append; 56, argument_list; 56, 57; 57, subscript; 57, 58; 57, 59; 58, identifier:lat; 59, identifier:i; 60, for_statement; 60, 61; 60, 64; 60, 69; 61, pattern_list; 61, 62; 61, 63; 62, identifier:z; 63, identifier:a; 64, call; 64, 65; 64, 66; 65, identifier:zip; 66, argument_list; 66, 67; 66, 68; 67, identifier:zz; 68, identifier:args; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:z; 74, identifier:append; 75, argument_list; 75, 76; 76, subscript; 76, 77; 76, 78; 77, identifier:a; 78, identifier:i; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:d1; 82, call; 82, 83; 82, 84; 83, identifier:abs; 84, argument_list; 84, 85; 85, binary_operator:-; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:lon; 88, identifier:i; 89, subscript; 89, 90; 89, 91; 90, identifier:lon; 91, binary_operator:-; 91, 92; 91, 93; 92, identifier:i; 93, integer:1; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:d2; 97, call; 97, 98; 97, 99; 98, identifier:abs; 99, argument_list; 99, 100; 100, binary_operator:-; 100, 101; 100, 109; 101, parenthesized_expression; 101, 102; 102, binary_operator:+; 102, 103; 102, 108; 103, subscript; 103, 104; 103, 105; 104, identifier:lon; 105, binary_operator:-; 105, 106; 105, 107; 106, identifier:i; 107, integer:1; 108, integer:360; 109, subscript; 109, 110; 109, 111; 110, identifier:lon; 111, identifier:i; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:d3; 115, call; 115, 116; 115, 117; 116, identifier:abs; 117, argument_list; 117, 118; 118, binary_operator:-; 118, 119; 118, 124; 119, subscript; 119, 120; 119, 121; 120, identifier:lon; 121, binary_operator:-; 121, 122; 121, 123; 122, identifier:i; 123, integer:1; 124, parenthesized_expression; 124, 125; 125, binary_operator:+; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:lon; 128, identifier:i; 129, integer:360; 130, if_statement; 130, 131; 130, 134; 130, 216; 130, 305; 131, comparison_operator:<; 131, 132; 131, 133; 132, identifier:d2; 133, identifier:d1; 134, block; 134, 135; 134, 146; 134, 177; 134, 189; 134, 203; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:ln; 139, identifier:append; 140, argument_list; 140, 141; 141, binary_operator:-; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:lon; 144, identifier:i; 145, integer:360; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:tracks; 150, identifier:append; 151, argument_list; 151, 152; 152, binary_operator:+; 152, 153; 152, 166; 152, 167; 153, list:[np.array(lt),np.array(ln)]; 153, 154; 153, 160; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:np; 157, identifier:array; 158, argument_list; 158, 159; 159, identifier:lt; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:np; 163, identifier:array; 164, argument_list; 164, 165; 165, identifier:ln; 166, line_continuation:\; 167, list_comprehension; 167, 168; 167, 174; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:np; 171, identifier:array; 172, argument_list; 172, 173; 173, identifier:z; 174, for_in_clause; 174, 175; 174, 176; 175, identifier:z; 176, identifier:zz; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:lt; 180, list:[lat[i-1],lat[i]]; 180, 181; 180, 186; 181, subscript; 181, 182; 181, 183; 182, identifier:lat; 183, binary_operator:-; 183, 184; 183, 185; 184, identifier:i; 185, integer:1; 186, subscript; 186, 187; 186, 188; 187, identifier:lat; 188, identifier:i; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:ln; 192, list:[lon[i-1]+360,lon[i]]; 192, 193; 192, 200; 193, binary_operator:+; 193, 194; 193, 199; 194, subscript; 194, 195; 194, 196; 195, identifier:lon; 196, binary_operator:-; 196, 197; 196, 198; 197, identifier:i; 198, integer:1; 199, integer:360; 200, subscript; 200, 201; 200, 202; 201, identifier:lon; 202, identifier:i; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:zz; 206, list_comprehension; 206, 207; 206, 213; 207, list:[z[i-1]]; 207, 208; 208, subscript; 208, 209; 208, 210; 209, identifier:z; 210, binary_operator:-; 210, 211; 210, 212; 211, identifier:i; 212, integer:1; 213, for_in_clause; 213, 214; 213, 215; 214, identifier:z; 215, identifier:args; 216, elif_clause; 216, 217; 216, 220; 217, comparison_operator:<; 217, 218; 217, 219; 218, identifier:d3; 219, identifier:d1; 220, block; 220, 221; 220, 232; 220, 263; 220, 275; 220, 289; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:ln; 225, identifier:append; 226, argument_list; 226, 227; 227, binary_operator:+; 227, 228; 227, 231; 228, subscript; 228, 229; 228, 230; 229, identifier:lon; 230, identifier:i; 231, integer:360; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:tracks; 236, identifier:append; 237, argument_list; 237, 238; 238, binary_operator:+; 238, 239; 238, 252; 238, 253; 239, list:[np.array(lt),np.array(ln)]; 239, 240; 239, 246; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:np; 243, identifier:array; 244, argument_list; 244, 245; 245, identifier:lt; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:np; 249, identifier:array; 250, argument_list; 250, 251; 251, identifier:ln; 252, line_continuation:\; 253, list_comprehension; 253, 254; 253, 260; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:np; 257, identifier:array; 258, argument_list; 258, 259; 259, identifier:z; 260, for_in_clause; 260, 261; 260, 262; 261, identifier:z; 262, identifier:zz; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:lt; 266, list:[lat[i-1],lat[i]]; 266, 267; 266, 272; 267, subscript; 267, 268; 267, 269; 268, identifier:lat; 269, binary_operator:-; 269, 270; 269, 271; 270, identifier:i; 271, integer:1; 272, subscript; 272, 273; 272, 274; 273, identifier:lat; 274, identifier:i; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:ln; 278, list:[lon[i-1]-360,lon[i]]; 278, 279; 278, 286; 279, binary_operator:-; 279, 280; 279, 285; 280, subscript; 280, 281; 280, 282; 281, identifier:lon; 282, binary_operator:-; 282, 283; 282, 284; 283, identifier:i; 284, integer:1; 285, integer:360; 286, subscript; 286, 287; 286, 288; 287, identifier:lon; 288, identifier:i; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:zz; 292, list_comprehension; 292, 293; 292, 302; 293, list:[z[i-1],z[i]]; 293, 294; 293, 299; 294, subscript; 294, 295; 294, 296; 295, identifier:z; 296, binary_operator:-; 296, 297; 296, 298; 297, identifier:i; 298, integer:1; 299, subscript; 299, 300; 299, 301; 300, identifier:z; 301, identifier:i; 302, for_in_clause; 302, 303; 302, 304; 303, identifier:z; 304, identifier:args; 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:ln; 311, identifier:append; 312, argument_list; 312, 313; 313, subscript; 313, 314; 313, 315; 314, identifier:lon; 315, identifier:i; 316, if_statement; 316, 317; 316, 321; 317, call; 317, 318; 317, 319; 318, identifier:len; 319, argument_list; 319, 320; 320, identifier:lt; 321, block; 321, 322; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 327; 324, attribute; 324, 325; 324, 326; 325, identifier:tracks; 326, identifier:append; 327, argument_list; 327, 328; 328, binary_operator:+; 328, 329; 328, 342; 328, 343; 329, list:[np.array(lt),np.array(ln)]; 329, 330; 329, 336; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:np; 333, identifier:array; 334, argument_list; 334, 335; 335, identifier:lt; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:np; 339, identifier:array; 340, argument_list; 340, 341; 341, identifier:ln; 342, line_continuation:\; 343, list_comprehension; 343, 344; 343, 350; 344, call; 344, 345; 344, 348; 345, attribute; 345, 346; 345, 347; 346, identifier:np; 347, identifier:array; 348, argument_list; 348, 349; 349, identifier:z; 350, for_in_clause; 350, 351; 350, 352; 351, identifier:z; 352, identifier:zz; 353, return_statement; 353, 354; 354, identifier:tracks | def split_tracks(lat,lon,*args):
'''assumes eastward motion'''
tracks = []
lt,ln = [lat[0]],[lon[0]]
zz = [[z[0]] for z in args]
for i in range(1,len(lon)):
lt.append(lat[i])
for z,a in zip(zz,args):
z.append(a[i])
d1 = abs(lon[i] - lon[i-1])
d2 = abs((lon[i-1] + 360) - lon[i])
d3 = abs(lon[i-1] - (lon[i] + 360))
if d2 < d1:
ln.append(lon[i]-360)
tracks.append([np.array(lt),np.array(ln)] \
+ [np.array(z) for z in zz])
lt = [lat[i-1],lat[i]]
ln = [lon[i-1]+360,lon[i]]
zz = [[z[i-1]] for z in args]
elif d3 < d1:
ln.append(lon[i]+360)
tracks.append([np.array(lt),np.array(ln)] \
+ [np.array(z) for z in zz])
lt = [lat[i-1],lat[i]]
ln = [lon[i-1]-360,lon[i]]
zz = [[z[i-1],z[i]] for z in args]
else:
ln.append(lon[i])
if len(lt):
tracks.append([np.array(lt),np.array(ln)] \
+ [np.array(z) for z in zz])
return tracks |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:find_loaders; 3, parameters; 3, 4; 3, 5; 4, identifier:scheme; 5, default_parameter; 5, 6; 5, 7; 6, identifier:protocols; 7, None; 8, block; 8, 9; 8, 11; 8, 12; 8, 17; 8, 33; 8, 41; 8, 42; 8, 52; 8, 104; 8, 105; 8, 132; 8, 142; 8, 162; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:matching_groups; 15, list:['plaster.loader_factory']; 15, 16; 16, string:'plaster.loader_factory'; 17, if_statement; 17, 18; 17, 19; 18, identifier:protocols; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, augmented_assignment:+=; 21, 22; 21, 23; 22, identifier:matching_groups; 23, list_comprehension; 23, 24; 23, 30; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, string:'plaster.{0}_loader_factory'; 27, identifier:format; 28, argument_list; 28, 29; 29, identifier:proto; 30, for_in_clause; 30, 31; 30, 32; 31, identifier:proto; 32, identifier:protocols; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:scheme; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:scheme; 39, identifier:lower; 40, argument_list; 41, comment; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:parts; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:scheme; 48, identifier:split; 49, argument_list; 49, 50; 49, 51; 50, string:'+'; 51, integer:1; 52, if_statement; 52, 53; 52, 59; 53, comparison_operator:==; 53, 54; 53, 58; 54, call; 54, 55; 54, 56; 55, identifier:len; 56, argument_list; 56, 57; 57, identifier:parts; 58, integer:2; 59, block; 59, 60; 60, try_statement; 60, 61; 60, 73; 60, 79; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:distro; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:pkg_resources; 68, identifier:get_distribution; 69, argument_list; 69, 70; 70, subscript; 70, 71; 70, 72; 71, identifier:parts; 72, integer:0; 73, except_clause; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:pkg_resources; 76, identifier:DistributionNotFound; 77, block; 77, 78; 78, pass_statement; 79, else_clause; 79, 80; 80, block; 80, 81; 80, 92; 80, 93; 80, 94; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:ep; 84, call; 84, 85; 84, 86; 85, identifier:_find_ep_in_dist; 86, argument_list; 86, 87; 86, 88; 86, 91; 87, identifier:distro; 88, subscript; 88, 89; 88, 90; 89, identifier:parts; 90, integer:1; 91, identifier:matching_groups; 92, comment; 93, comment; 94, if_statement; 94, 95; 94, 96; 95, identifier:ep; 96, block; 96, 97; 97, return_statement; 97, 98; 98, list:[EntryPointLoaderInfo(ep, protocols)]; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:EntryPointLoaderInfo; 101, argument_list; 101, 102; 101, 103; 102, identifier:ep; 103, identifier:protocols; 104, comment; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:possible_entry_points; 108, list_comprehension; 108, 109; 108, 110; 108, 118; 109, identifier:ep; 110, for_in_clause; 110, 111; 110, 112; 111, identifier:ep; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:pkg_resources; 115, identifier:iter_entry_points; 116, argument_list; 116, 117; 117, string:'plaster.loader_factory'; 118, if_clause; 118, 119; 119, boolean_operator:or; 119, 120; 119, 123; 120, comparison_operator:is; 120, 121; 120, 122; 121, identifier:scheme; 122, None; 123, comparison_operator:==; 123, 124; 123, 125; 124, identifier:scheme; 125, call; 125, 126; 125, 131; 126, attribute; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:ep; 129, identifier:name; 130, identifier:lower; 131, argument_list; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:distros; 135, set_comprehension; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:ep; 138, identifier:dist; 139, for_in_clause; 139, 140; 139, 141; 140, identifier:ep; 141, identifier:possible_entry_points; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:matched_entry_points; 145, call; 145, 146; 145, 147; 146, identifier:list; 147, argument_list; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:filter; 150, argument_list; 150, 151; 150, 152; 151, None; 152, list_comprehension; 152, 153; 152, 159; 153, call; 153, 154; 153, 155; 154, identifier:_find_ep_in_dist; 155, argument_list; 155, 156; 155, 157; 155, 158; 156, identifier:distro; 157, identifier:scheme; 158, identifier:matching_groups; 159, for_in_clause; 159, 160; 159, 161; 160, identifier:distro; 161, identifier:distros; 162, return_statement; 162, 163; 163, list_comprehension; 163, 164; 163, 171; 164, call; 164, 165; 164, 166; 165, identifier:EntryPointLoaderInfo; 166, argument_list; 166, 167; 166, 168; 167, identifier:ep; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:protocols; 170, identifier:protocols; 171, for_in_clause; 171, 172; 171, 173; 172, identifier:ep; 173, identifier:matched_entry_points | def find_loaders(scheme, protocols=None):
"""
Find all loaders that match the requested scheme and protocols.
:param scheme: Any valid scheme. Examples would be something like ``ini``
or ``ini+pastedeploy``.
:param protocols: Zero or more :term:`loader protocol` identifiers that
the loader must implement. If ``None`` then only generic loaders will
be returned.
:returns: A list containing zero or more :class:`plaster.ILoaderInfo`
objects.
"""
# build a list of all required entry points
matching_groups = ['plaster.loader_factory']
if protocols:
matching_groups += [
'plaster.{0}_loader_factory'.format(proto)
for proto in protocols
]
scheme = scheme.lower()
# if a distribution is specified then it overrides the default search
parts = scheme.split('+', 1)
if len(parts) == 2:
try:
distro = pkg_resources.get_distribution(parts[0])
except pkg_resources.DistributionNotFound:
pass
else:
ep = _find_ep_in_dist(distro, parts[1], matching_groups)
# if we got one or more loaders from a specific distribution
# then they override everything else so we'll just return them
if ep:
return [EntryPointLoaderInfo(ep, protocols)]
# find any distributions supporting the default loader protocol
possible_entry_points = [
ep
for ep in pkg_resources.iter_entry_points('plaster.loader_factory')
if scheme is None or scheme == ep.name.lower()
]
distros = {ep.dist for ep in possible_entry_points}
matched_entry_points = list(filter(None, [
_find_ep_in_dist(distro, scheme, matching_groups)
for distro in distros
]))
return [
EntryPointLoaderInfo(ep, protocols=protocols)
for ep in matched_entry_points
] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:combine_dicts; 3, parameters; 3, 4; 3, 6; 3, 9; 4, list_splat_pattern; 4, 5; 5, identifier:dicts; 6, default_parameter; 6, 7; 6, 8; 7, identifier:copy; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:base; 11, None; 12, block; 12, 13; 12, 15; 12, 66; 12, 67; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 26; 15, 27; 15, 38; 16, boolean_operator:and; 16, 17; 16, 23; 17, comparison_operator:==; 17, 18; 17, 22; 18, call; 18, 19; 18, 20; 19, identifier:len; 20, argument_list; 20, 21; 21, identifier:dicts; 22, integer:1; 23, comparison_operator:is; 23, 24; 23, 25; 24, identifier:base; 25, None; 26, comment; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:cd; 31, call; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, subscript; 33, 34; 33, 35; 34, identifier:dicts; 35, integer:0; 36, identifier:copy; 37, argument_list; 38, else_clause; 38, 39; 39, block; 39, 40; 39, 49; 39, 50; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:cd; 43, conditional_expression:if; 43, 44; 43, 45; 43, 48; 44, dictionary; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:base; 47, None; 48, identifier:base; 49, comment; 50, for_statement; 50, 51; 50, 52; 50, 53; 50, 54; 51, identifier:d; 52, identifier:dicts; 53, comment; 54, block; 54, 55; 55, if_statement; 55, 56; 55, 57; 55, 58; 56, identifier:d; 57, comment; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:cd; 63, identifier:update; 64, argument_list; 64, 65; 65, identifier:d; 66, comment; 67, return_statement; 67, 68; 68, conditional_expression:if; 68, 69; 68, 87; 68, 88; 69, dictionary_comprehension; 69, 70; 69, 78; 70, pair; 70, 71; 70, 72; 71, identifier:k; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:_copy; 75, identifier:deepcopy; 76, argument_list; 76, 77; 77, identifier:v; 78, for_in_clause; 78, 79; 78, 82; 79, pattern_list; 79, 80; 79, 81; 80, identifier:k; 81, identifier:v; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:cd; 85, identifier:items; 86, argument_list; 87, identifier:copy; 88, identifier:cd | def combine_dicts(*dicts, copy=False, base=None):
"""
Combines multiple dicts in one.
:param dicts:
A sequence of dicts.
:type dicts: dict
:param copy:
If True, it returns a deepcopy of input values.
:type copy: bool, optional
:param base:
Base dict where combine multiple dicts in one.
:type base: dict, optional
:return:
A unique dict.
:rtype: dict
Example::
>>> sorted(combine_dicts({'a': 3, 'c': 3}, {'a': 1, 'b': 2}).items())
[('a', 1), ('b', 2), ('c', 3)]
"""
if len(dicts) == 1 and base is None: # Only one input dict.
cd = dicts[0].copy()
else:
cd = {} if base is None else base # Initialize empty dict.
for d in dicts: # Combine dicts.
if d:
# noinspection PyTypeChecker
cd.update(d)
# Return combined dict.
return {k: _copy.deepcopy(v) for k, v in cd.items()} if copy else cd |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:kk_dict; 3, parameters; 3, 4; 3, 6; 4, list_splat_pattern; 4, 5; 5, identifier:kk; 6, dictionary_splat_pattern; 6, 7; 7, identifier:adict; 8, block; 8, 9; 8, 11; 8, 64; 9, expression_statement; 9, 10; 10, comment; 11, for_statement; 11, 12; 11, 13; 11, 14; 12, identifier:k; 13, identifier:kk; 14, block; 14, 15; 15, if_statement; 15, 16; 15, 21; 15, 46; 15, 56; 16, call; 16, 17; 16, 18; 17, identifier:isinstance; 18, argument_list; 18, 19; 18, 20; 19, identifier:k; 20, identifier:dict; 21, block; 21, 22; 21, 39; 22, if_statement; 22, 23; 22, 33; 23, not_operator; 23, 24; 24, call; 24, 25; 24, 31; 25, attribute; 25, 26; 25, 30; 26, call; 26, 27; 26, 28; 27, identifier:set; 28, argument_list; 28, 29; 29, identifier:k; 30, identifier:isdisjoint; 31, argument_list; 31, 32; 32, identifier:adict; 33, block; 33, 34; 34, raise_statement; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:ValueError; 37, argument_list; 37, 38; 38, string:'keyword argument repeated'; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:adict; 43, identifier:update; 44, argument_list; 44, 45; 45, identifier:k; 46, elif_clause; 46, 47; 46, 50; 47, comparison_operator:in; 47, 48; 47, 49; 48, identifier:k; 49, identifier:adict; 50, block; 50, 51; 51, raise_statement; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:ValueError; 54, argument_list; 54, 55; 55, string:'keyword argument repeated'; 56, else_clause; 56, 57; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 63; 60, subscript; 60, 61; 60, 62; 61, identifier:adict; 62, identifier:k; 63, identifier:k; 64, return_statement; 64, 65; 65, identifier:adict | def kk_dict(*kk, **adict):
"""
Merges and defines dictionaries with values identical to keys.
:param kk:
A sequence of keys and/or dictionaries.
:type kk: object | dict, optional
:param adict:
A dictionary.
:type adict: dict, optional
:return:
Merged dictionary.
:rtype: dict
Example::
>>> sorted(kk_dict('a', 'b', 'c').items())
[('a', 'a'), ('b', 'b'), ('c', 'c')]
>>> sorted(kk_dict('a', 'b', **{'a-c': 'c'}).items())
[('a', 'a'), ('a-c', 'c'), ('b', 'b')]
>>> sorted(kk_dict('a', {'b': 'c'}, 'c').items())
[('a', 'a'), ('b', 'c'), ('c', 'c')]
>>> sorted(kk_dict('a', 'b', **{'b': 'c'}).items())
Traceback (most recent call last):
...
ValueError: keyword argument repeated
>>> sorted(kk_dict('a', {'b': 'd'}, **{'b': 'c'}).items())
Traceback (most recent call last):
...
ValueError: keyword argument repeated
"""
for k in kk:
if isinstance(k, dict):
if not set(k).isdisjoint(adict):
raise ValueError('keyword argument repeated')
adict.update(k)
elif k in adict:
raise ValueError('keyword argument repeated')
else:
adict[k] = k
return adict |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:map_dict; 3, parameters; 3, 4; 3, 5; 3, 7; 3, 10; 4, identifier:key_map; 5, list_splat_pattern; 5, 6; 6, identifier:dicts; 7, default_parameter; 7, 8; 7, 9; 8, identifier:copy; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:base; 12, None; 13, block; 13, 14; 13, 16; 13, 28; 13, 29; 13, 35; 13, 36; 13, 37; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:it; 19, call; 19, 20; 19, 27; 20, attribute; 20, 21; 20, 26; 21, call; 21, 22; 21, 23; 22, identifier:combine_dicts; 23, argument_list; 23, 24; 24, list_splat; 24, 25; 25, identifier:dicts; 26, identifier:items; 27, argument_list; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:get; 32, attribute; 32, 33; 32, 34; 33, identifier:key_map; 34, identifier:get; 35, comment; 36, comment; 37, return_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:combine_dicts; 40, argument_list; 40, 41; 40, 54; 40, 57; 41, dictionary_comprehension; 41, 42; 41, 49; 42, pair; 42, 43; 42, 48; 43, call; 43, 44; 43, 45; 44, identifier:get; 45, argument_list; 45, 46; 45, 47; 46, identifier:k; 47, identifier:k; 48, identifier:v; 49, for_in_clause; 49, 50; 49, 53; 50, pattern_list; 50, 51; 50, 52; 51, identifier:k; 52, identifier:v; 53, identifier:it; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:copy; 56, identifier:copy; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:base; 59, identifier:base | def map_dict(key_map, *dicts, copy=False, base=None):
"""
Returns a dict with new key values.
:param key_map:
A dictionary that maps the dict keys ({old key: new key}
:type key_map: dict
:param dicts:
A sequence of dicts.
:type dicts: dict
:param copy:
If True, it returns a deepcopy of input values.
:type copy: bool, optional
:param base:
Base dict where combine multiple dicts in one.
:type base: dict, optional
:return:
A unique dict with new key values.
:rtype: dict
Example::
>>> d = map_dict({'a': 'c', 'b': 'd'}, {'a': 1, 'b': 1}, {'b': 2})
>>> sorted(d.items())
[('c', 1), ('d', 2)]
"""
it = combine_dicts(*dicts).items() # Combine dicts.
get = key_map.get # Namespace shortcut.
# Return mapped dict.
return combine_dicts({get(k, k): v for k, v in it}, copy=copy, base=base) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:map_list; 3, parameters; 3, 4; 3, 5; 3, 7; 3, 10; 4, identifier:key_map; 5, list_splat_pattern; 5, 6; 6, identifier:inputs; 7, default_parameter; 7, 8; 7, 9; 8, identifier:copy; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:base; 12, None; 13, block; 13, 14; 13, 16; 13, 25; 13, 26; 13, 80; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:d; 19, conditional_expression:if; 19, 20; 19, 21; 19, 24; 20, dictionary; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:base; 23, None; 24, identifier:base; 25, comment; 26, for_statement; 26, 27; 26, 30; 26, 35; 27, pattern_list; 27, 28; 27, 29; 28, identifier:m; 29, identifier:v; 30, call; 30, 31; 30, 32; 31, identifier:zip; 32, argument_list; 32, 33; 32, 34; 33, identifier:key_map; 34, identifier:inputs; 35, block; 35, 36; 36, if_statement; 36, 37; 36, 42; 36, 53; 36, 71; 37, call; 37, 38; 37, 39; 38, identifier:isinstance; 39, argument_list; 39, 40; 39, 41; 40, identifier:m; 41, identifier:dict; 42, block; 42, 43; 42, 52; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:map_dict; 46, argument_list; 46, 47; 46, 48; 46, 49; 47, identifier:m; 48, identifier:v; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:base; 51, identifier:d; 52, comment; 53, elif_clause; 53, 54; 53, 59; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 58; 57, identifier:m; 58, identifier:list; 59, block; 59, 60; 59, 70; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:map_list; 63, argument_list; 63, 64; 63, 65; 63, 67; 64, identifier:m; 65, list_splat; 65, 66; 66, identifier:v; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:base; 69, identifier:d; 70, comment; 71, else_clause; 71, 72; 72, block; 72, 73; 72, 79; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:d; 77, identifier:m; 78, identifier:v; 79, comment; 80, return_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:combine_dicts; 83, argument_list; 83, 84; 83, 87; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:copy; 86, identifier:copy; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:base; 89, identifier:d | def map_list(key_map, *inputs, copy=False, base=None):
"""
Returns a new dict.
:param key_map:
A list that maps the dict keys ({old key: new key}
:type key_map: list[str | dict | list]
:param inputs:
A sequence of data.
:type inputs: iterable | dict | int | float | list | tuple
:param copy:
If True, it returns a deepcopy of input values.
:type copy: bool, optional
:param base:
Base dict where combine multiple dicts in one.
:type base: dict, optional
:return:
A unique dict with new values.
:rtype: dict
Example::
>>> key_map = [
... 'a',
... {'a': 'c'},
... [
... 'a',
... {'a': 'd'}
... ]
... ]
>>> inputs = (
... 2,
... {'a': 3, 'b': 2},
... [
... 1,
... {'a': 4}
... ]
... )
>>> d = map_list(key_map, *inputs)
>>> sorted(d.items())
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
"""
d = {} if base is None else base # Initialize empty dict.
for m, v in zip(key_map, inputs):
if isinstance(m, dict):
map_dict(m, v, base=d) # Apply a map dict.
elif isinstance(m, list):
map_list(m, *v, base=d) # Apply a map list.
else:
d[m] = v # Apply map.
return combine_dicts(copy=copy, base=d) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:selector; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:keys; 5, identifier:dictionary; 6, default_parameter; 6, 7; 6, 8; 7, identifier:copy; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:output_type; 11, string:'dict'; 12, default_parameter; 12, 13; 12, 14; 13, identifier:allow_miss; 14, False; 15, block; 15, 16; 15, 18; 15, 41; 15, 97; 15, 98; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 21; 18, 22; 18, 30; 19, not_operator; 19, 20; 20, identifier:allow_miss; 21, comment; 22, block; 22, 23; 23, function_definition; 23, 24; 23, 25; 23, 27; 24, function_name:check; 25, parameters; 25, 26; 26, identifier:key; 27, block; 27, 28; 28, return_statement; 28, 29; 29, True; 30, else_clause; 30, 31; 31, block; 31, 32; 32, function_definition; 32, 33; 32, 34; 32, 36; 33, function_name:check; 34, parameters; 34, 35; 35, identifier:key; 36, block; 36, 37; 37, return_statement; 37, 38; 38, comparison_operator:in; 38, 39; 38, 40; 39, identifier:key; 40, identifier:dictionary; 41, if_statement; 41, 42; 41, 45; 41, 46; 41, 72; 42, comparison_operator:==; 42, 43; 42, 44; 43, identifier:output_type; 44, string:'list'; 45, comment; 46, block; 46, 47; 46, 62; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:res; 50, list_comprehension; 50, 51; 50, 54; 50, 57; 51, subscript; 51, 52; 51, 53; 52, identifier:dictionary; 53, identifier:k; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:k; 56, identifier:keys; 57, if_clause; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:check; 60, argument_list; 60, 61; 61, identifier:k; 62, return_statement; 62, 63; 63, conditional_expression:if; 63, 64; 63, 70; 63, 71; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:_copy; 67, identifier:deepcopy; 68, argument_list; 68, 69; 69, identifier:res; 70, identifier:copy; 71, identifier:res; 72, elif_clause; 72, 73; 72, 76; 73, comparison_operator:==; 73, 74; 73, 75; 74, identifier:output_type; 75, string:'values'; 76, block; 76, 77; 77, return_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:bypass; 80, argument_list; 80, 81; 80, 94; 81, list_splat; 81, 82; 82, list_comprehension; 82, 83; 82, 86; 82, 89; 83, subscript; 83, 84; 83, 85; 84, identifier:dictionary; 85, identifier:k; 86, for_in_clause; 86, 87; 86, 88; 87, identifier:k; 88, identifier:keys; 89, if_clause; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:check; 92, argument_list; 92, 93; 93, identifier:k; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:copy; 96, identifier:copy; 97, comment; 98, return_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:bypass; 101, argument_list; 101, 102; 101, 116; 102, dictionary_comprehension; 102, 103; 102, 108; 102, 111; 103, pair; 103, 104; 103, 105; 104, identifier:k; 105, subscript; 105, 106; 105, 107; 106, identifier:dictionary; 107, identifier:k; 108, for_in_clause; 108, 109; 108, 110; 109, identifier:k; 110, identifier:keys; 111, if_clause; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:check; 114, argument_list; 114, 115; 115, identifier:k; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:copy; 118, identifier:copy | def selector(keys, dictionary, copy=False, output_type='dict',
allow_miss=False):
"""
Selects the chosen dictionary keys from the given dictionary.
:param keys:
Keys to select.
:type keys: list, tuple, set
:param dictionary:
A dictionary.
:type dictionary: dict
:param copy:
If True the output contains deep-copies of the values.
:type copy: bool
:param output_type:
Type of function output:
+ 'list': a list with all values listed in `keys`.
+ 'dict': a dictionary with any outputs listed in `keys`.
+ 'values': if output length == 1 return a single value otherwise a
tuple with all values listed in `keys`.
:type output_type: str, optional
:param allow_miss:
If True it does not raise when some key is missing in the dictionary.
:type allow_miss: bool
:return:
A dictionary with chosen dictionary keys if present in the sequence of
dictionaries. These are combined with :func:`combine_dicts`.
:rtype: dict
Example::
>>> from functools import partial
>>> fun = partial(selector, ['a', 'b'])
>>> sorted(fun({'a': 1, 'b': 2, 'c': 3}).items())
[('a', 1), ('b', 2)]
"""
if not allow_miss:
# noinspection PyUnusedLocal
def check(key):
return True
else:
def check(key):
return key in dictionary
if output_type == 'list': # Select as list.
res = [dictionary[k] for k in keys if check(k)]
return _copy.deepcopy(res) if copy else res
elif output_type == 'values':
return bypass(*[dictionary[k] for k in keys if check(k)], copy=copy)
# Select as dict.
return bypass({k: dictionary[k] for k in keys if check(k)}, copy=copy) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:difference; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:others; 7, block; 7, 8; 7, 10; 7, 18; 7, 24; 7, 30; 7, 92; 7, 98; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:result; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:__copy__; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:_elements; 21, attribute; 21, 22; 21, 23; 22, identifier:result; 23, identifier:_elements; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:_total; 27, attribute; 27, 28; 27, 29; 28, identifier:result; 29, identifier:_total; 30, for_statement; 30, 31; 30, 32; 30, 39; 31, identifier:other; 32, call; 32, 33; 32, 34; 33, identifier:map; 34, argument_list; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_as_multiset; 38, identifier:others; 39, block; 39, 40; 40, for_statement; 40, 41; 40, 44; 40, 49; 41, pattern_list; 41, 42; 41, 43; 42, identifier:element; 43, identifier:multiplicity; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:other; 47, identifier:items; 48, argument_list; 49, block; 49, 50; 50, if_statement; 50, 51; 50, 54; 51, comparison_operator:in; 51, 52; 51, 53; 52, identifier:element; 53, identifier:_elements; 54, block; 54, 55; 54, 61; 54, 67; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:old_multiplicity; 58, subscript; 58, 59; 58, 60; 59, identifier:_elements; 60, identifier:element; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:new_multiplicity; 64, binary_operator:-; 64, 65; 64, 66; 65, identifier:old_multiplicity; 66, identifier:multiplicity; 67, if_statement; 67, 68; 67, 71; 67, 82; 68, comparison_operator:>; 68, 69; 68, 70; 69, identifier:new_multiplicity; 70, integer:0; 71, block; 71, 72; 71, 78; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:_elements; 76, identifier:element; 77, identifier:new_multiplicity; 78, expression_statement; 78, 79; 79, augmented_assignment:-=; 79, 80; 79, 81; 80, identifier:_total; 81, identifier:multiplicity; 82, else_clause; 82, 83; 83, block; 83, 84; 83, 88; 84, delete_statement; 84, 85; 85, subscript; 85, 86; 85, 87; 86, identifier:_elements; 87, identifier:element; 88, expression_statement; 88, 89; 89, augmented_assignment:-=; 89, 90; 89, 91; 90, identifier:_total; 91, identifier:old_multiplicity; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:result; 96, identifier:_total; 97, identifier:_total; 98, return_statement; 98, 99; 99, identifier:result | def difference(self, *others):
r"""Return a new multiset with all elements from the others removed.
>>> ms = Multiset('aab')
>>> sorted(ms.difference('bc'))
['a', 'a']
You can also use the ``-`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.
>>> ms = Multiset('aabbbc')
>>> sorted(ms - Multiset('abd'))
['a', 'b', 'b', 'c']
For a variant of the operation which modifies the multiset in place see
:meth:`difference_update`.
Args:
others: The other sets to remove from the multiset. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].
Returns:
The resulting difference multiset.
"""
result = self.__copy__()
_elements = result._elements
_total = result._total
for other in map(self._as_multiset, others):
for element, multiplicity in other.items():
if element in _elements:
old_multiplicity = _elements[element]
new_multiplicity = old_multiplicity - multiplicity
if new_multiplicity > 0:
_elements[element] = new_multiplicity
_total -= multiplicity
else:
del _elements[element]
_total -= old_multiplicity
result._total = _total
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:union; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:others; 7, block; 7, 8; 7, 10; 7, 18; 7, 24; 7, 30; 7, 77; 7, 83; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:result; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:__copy__; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:_elements; 21, attribute; 21, 22; 21, 23; 22, identifier:result; 23, identifier:_elements; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:_total; 27, attribute; 27, 28; 27, 29; 28, identifier:result; 29, identifier:_total; 30, for_statement; 30, 31; 30, 32; 30, 39; 31, identifier:other; 32, call; 32, 33; 32, 34; 33, identifier:map; 34, argument_list; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_as_mapping; 38, identifier:others; 39, block; 39, 40; 40, for_statement; 40, 41; 40, 44; 40, 49; 41, pattern_list; 41, 42; 41, 43; 42, identifier:element; 43, identifier:multiplicity; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:other; 47, identifier:items; 48, argument_list; 49, block; 49, 50; 49, 60; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:old_multiplicity; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:_elements; 56, identifier:get; 57, argument_list; 57, 58; 57, 59; 58, identifier:element; 59, integer:0; 60, if_statement; 60, 61; 60, 64; 61, comparison_operator:>; 61, 62; 61, 63; 62, identifier:multiplicity; 63, identifier:old_multiplicity; 64, block; 64, 65; 64, 71; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:_elements; 69, identifier:element; 70, identifier:multiplicity; 71, expression_statement; 71, 72; 72, augmented_assignment:+=; 72, 73; 72, 74; 73, identifier:_total; 74, binary_operator:-; 74, 75; 74, 76; 75, identifier:multiplicity; 76, identifier:old_multiplicity; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:result; 81, identifier:_total; 82, identifier:_total; 83, return_statement; 83, 84; 84, identifier:result | def union(self, *others):
r"""Return a new multiset with all elements from the multiset and the others with maximal multiplicities.
>>> ms = Multiset('aab')
>>> sorted(ms.union('bc'))
['a', 'a', 'b', 'c']
You can also use the ``|`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.
>>> ms = Multiset('aab')
>>> sorted(ms | Multiset('aaa'))
['a', 'a', 'a', 'b']
For a variant of the operation which modifies the multiset in place see
:meth:`union_update`.
Args:
*others: The other sets to union the multiset with. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].
Returns:
The multiset resulting from the union.
"""
result = self.__copy__()
_elements = result._elements
_total = result._total
for other in map(self._as_mapping, others):
for element, multiplicity in other.items():
old_multiplicity = _elements.get(element, 0)
if multiplicity > old_multiplicity:
_elements[element] = multiplicity
_total += multiplicity - old_multiplicity
result._total = _total
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:intersection; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:others; 7, block; 7, 8; 7, 10; 7, 18; 7, 24; 7, 30; 7, 95; 7, 101; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:result; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:__copy__; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:_elements; 21, attribute; 21, 22; 21, 23; 22, identifier:result; 23, identifier:_elements; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:_total; 27, attribute; 27, 28; 27, 29; 28, identifier:result; 29, identifier:_total; 30, for_statement; 30, 31; 30, 32; 30, 39; 31, identifier:other; 32, call; 32, 33; 32, 34; 33, identifier:map; 34, argument_list; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_as_mapping; 38, identifier:others; 39, block; 39, 40; 40, for_statement; 40, 41; 40, 44; 40, 52; 41, pattern_list; 41, 42; 41, 43; 42, identifier:element; 43, identifier:multiplicity; 44, call; 44, 45; 44, 46; 45, identifier:list; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:_elements; 50, identifier:items; 51, argument_list; 52, block; 52, 53; 52, 63; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:new_multiplicity; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:other; 59, identifier:get; 60, argument_list; 60, 61; 60, 62; 61, identifier:element; 62, integer:0; 63, if_statement; 63, 64; 63, 67; 64, comparison_operator:<; 64, 65; 64, 66; 65, identifier:new_multiplicity; 66, identifier:multiplicity; 67, block; 67, 68; 68, if_statement; 68, 69; 68, 72; 68, 85; 69, comparison_operator:>; 69, 70; 69, 71; 70, identifier:new_multiplicity; 71, integer:0; 72, block; 72, 73; 72, 79; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:_elements; 77, identifier:element; 78, identifier:new_multiplicity; 79, expression_statement; 79, 80; 80, augmented_assignment:-=; 80, 81; 80, 82; 81, identifier:_total; 82, binary_operator:-; 82, 83; 82, 84; 83, identifier:multiplicity; 84, identifier:new_multiplicity; 85, else_clause; 85, 86; 86, block; 86, 87; 86, 91; 87, delete_statement; 87, 88; 88, subscript; 88, 89; 88, 90; 89, identifier:_elements; 90, identifier:element; 91, expression_statement; 91, 92; 92, augmented_assignment:-=; 92, 93; 92, 94; 93, identifier:_total; 94, identifier:multiplicity; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:result; 99, identifier:_total; 100, identifier:_total; 101, return_statement; 101, 102; 102, identifier:result | def intersection(self, *others):
r"""Return a new multiset with elements common to the multiset and all others.
>>> ms = Multiset('aab')
>>> sorted(ms.intersection('abc'))
['a', 'b']
You can also use the ``&`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.
>>> ms = Multiset('aab')
>>> sorted(ms & Multiset('aaac'))
['a', 'a']
For a variant of the operation which modifies the multiset in place see
:meth:`intersection_update`.
Args:
others: The other sets intersect with the multiset. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].
Returns:
The multiset resulting from the intersection of the sets.
"""
result = self.__copy__()
_elements = result._elements
_total = result._total
for other in map(self._as_mapping, others):
for element, multiplicity in list(_elements.items()):
new_multiplicity = other.get(element, 0)
if new_multiplicity < multiplicity:
if new_multiplicity > 0:
_elements[element] = new_multiplicity
_total -= multiplicity - new_multiplicity
else:
del _elements[element]
_total -= multiplicity
result._total = _total
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:symmetric_difference; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:other; 6, block; 6, 7; 6, 9; 6, 18; 6, 26; 6, 30; 6, 36; 6, 42; 6, 48; 6, 68; 6, 121; 6, 127; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:other; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_as_multiset; 16, argument_list; 16, 17; 17, identifier:other; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:result; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:__class__; 25, argument_list; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:_total; 29, integer:0; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:_elements; 33, attribute; 33, 34; 33, 35; 34, identifier:result; 35, identifier:_elements; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:self_elements; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:_elements; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:other_elements; 45, attribute; 45, 46; 45, 47; 46, identifier:other; 47, identifier:_elements; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:dist_elements; 51, binary_operator:|; 51, 52; 51, 60; 52, call; 52, 53; 52, 54; 53, identifier:set; 54, argument_list; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self_elements; 58, identifier:keys; 59, argument_list; 60, call; 60, 61; 60, 62; 61, identifier:set; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:other_elements; 66, identifier:keys; 67, argument_list; 68, for_statement; 68, 69; 68, 70; 68, 71; 69, identifier:element; 70, identifier:dist_elements; 71, block; 71, 72; 71, 82; 71, 92; 71, 106; 71, 110; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:multiplicity; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self_elements; 78, identifier:get; 79, argument_list; 79, 80; 79, 81; 80, identifier:element; 81, integer:0; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:other_multiplicity; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:other_elements; 88, identifier:get; 89, argument_list; 89, 90; 89, 91; 90, identifier:element; 91, integer:0; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:new_multiplicity; 95, parenthesized_expression; 95, 96; 96, conditional_expression:if; 96, 97; 96, 100; 96, 103; 97, binary_operator:-; 97, 98; 97, 99; 98, identifier:multiplicity; 99, identifier:other_multiplicity; 100, comparison_operator:>; 100, 101; 100, 102; 101, identifier:multiplicity; 102, identifier:other_multiplicity; 103, binary_operator:-; 103, 104; 103, 105; 104, identifier:other_multiplicity; 105, identifier:multiplicity; 106, expression_statement; 106, 107; 107, augmented_assignment:+=; 107, 108; 107, 109; 108, identifier:_total; 109, identifier:new_multiplicity; 110, if_statement; 110, 111; 110, 114; 111, comparison_operator:>; 111, 112; 111, 113; 112, identifier:new_multiplicity; 113, integer:0; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:_elements; 119, identifier:element; 120, identifier:new_multiplicity; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:result; 125, identifier:_total; 126, identifier:_total; 127, return_statement; 127, 128; 128, identifier:result | def symmetric_difference(self, other):
r"""Return a new set with elements in either the set or other but not both.
>>> ms = Multiset('aab')
>>> sorted(ms.symmetric_difference('abc'))
['a', 'c']
You can also use the ``^`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.
>>> ms = Multiset('aab')
>>> sorted(ms ^ Multiset('aaac'))
['a', 'b', 'c']
For a variant of the operation which modifies the multiset in place see
:meth:`symmetric_difference_update`.
Args:
other: The other set to take the symmetric difference with. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].
Returns:
The resulting symmetric difference multiset.
"""
other = self._as_multiset(other)
result = self.__class__()
_total = 0
_elements = result._elements
self_elements = self._elements
other_elements = other._elements
dist_elements = set(self_elements.keys()) | set(other_elements.keys())
for element in dist_elements:
multiplicity = self_elements.get(element, 0)
other_multiplicity = other_elements.get(element, 0)
new_multiplicity = (multiplicity - other_multiplicity
if multiplicity > other_multiplicity else other_multiplicity - multiplicity)
_total += new_multiplicity
if new_multiplicity > 0:
_elements[element] = new_multiplicity
result._total = _total
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:times; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:factor; 6, block; 6, 7; 6, 9; 6, 20; 6, 30; 6, 38; 6, 44; 6, 54; 6, 60; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 10, comparison_operator:==; 10, 11; 10, 12; 11, identifier:factor; 12, integer:0; 13, block; 13, 14; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:__class__; 19, argument_list; 20, if_statement; 20, 21; 20, 24; 21, comparison_operator:<; 21, 22; 21, 23; 22, identifier:factor; 23, integer:0; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:ValueError; 28, argument_list; 28, 29; 29, string:'The factor must no be negative.'; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:result; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:__copy__; 37, argument_list; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:_elements; 41, attribute; 41, 42; 41, 43; 42, identifier:result; 43, identifier:_elements; 44, for_statement; 44, 45; 44, 46; 44, 47; 45, identifier:element; 46, identifier:_elements; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, augmented_assignment:*=; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:_elements; 52, identifier:element; 53, identifier:factor; 54, expression_statement; 54, 55; 55, augmented_assignment:*=; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:result; 58, identifier:_total; 59, identifier:factor; 60, return_statement; 60, 61; 61, identifier:result | def times(self, factor):
"""Return a new set with each element's multiplicity multiplied with the given scalar factor.
>>> ms = Multiset('aab')
>>> sorted(ms.times(2))
['a', 'a', 'a', 'a', 'b', 'b']
You can also use the ``*`` operator for the same effect:
>>> sorted(ms * 3)
['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b']
For a variant of the operation which modifies the multiset in place see
:meth:`times_update`.
Args:
factor: The factor to multiply each multiplicity with.
"""
if factor == 0:
return self.__class__()
if factor < 0:
raise ValueError('The factor must no be negative.')
result = self.__copy__()
_elements = result._elements
for element in _elements:
_elements[element] *= factor
result._total *= factor
return result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:union_update; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:others; 7, block; 7, 8; 7, 10; 7, 16; 7, 22; 7, 69; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:_elements; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_elements; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:_total; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_total; 22, for_statement; 22, 23; 22, 24; 22, 31; 23, identifier:other; 24, call; 24, 25; 24, 26; 25, identifier:map; 26, argument_list; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_as_mapping; 30, identifier:others; 31, block; 31, 32; 32, for_statement; 32, 33; 32, 36; 32, 41; 33, pattern_list; 33, 34; 33, 35; 34, identifier:element; 35, identifier:multiplicity; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:other; 39, identifier:items; 40, argument_list; 41, block; 41, 42; 41, 52; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:old_multiplicity; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:_elements; 48, identifier:get; 49, argument_list; 49, 50; 49, 51; 50, identifier:element; 51, integer:0; 52, if_statement; 52, 53; 52, 56; 53, comparison_operator:>; 53, 54; 53, 55; 54, identifier:multiplicity; 55, identifier:old_multiplicity; 56, block; 56, 57; 56, 63; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:_elements; 61, identifier:element; 62, identifier:multiplicity; 63, expression_statement; 63, 64; 64, augmented_assignment:+=; 64, 65; 64, 66; 65, identifier:_total; 66, binary_operator:-; 66, 67; 66, 68; 67, identifier:multiplicity; 68, identifier:old_multiplicity; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:_total; 74, identifier:_total | def union_update(self, *others):
r"""Update the multiset, adding elements from all others using the maximum multiplicity.
>>> ms = Multiset('aab')
>>> ms.union_update('bc')
>>> sorted(ms)
['a', 'a', 'b', 'c']
You can also use the ``|=`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.
>>> ms = Multiset('aab')
>>> ms |= Multiset('bccd')
>>> sorted(ms)
['a', 'a', 'b', 'c', 'c', 'd']
For a variant of the operation which does not modify the multiset, but returns a new
multiset instead see :meth:`union`.
Args:
others: The other sets to union this multiset with. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].
"""
_elements = self._elements
_total = self._total
for other in map(self._as_mapping, others):
for element, multiplicity in other.items():
old_multiplicity = _elements.get(element, 0)
if multiplicity > old_multiplicity:
_elements[element] = multiplicity
_total += multiplicity - old_multiplicity
self._total = _total |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:intersection_update; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:others; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, for_statement; 10, 11; 10, 12; 10, 19; 11, identifier:other; 12, call; 12, 13; 12, 14; 13, identifier:map; 14, argument_list; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:_as_mapping; 18, identifier:others; 19, block; 19, 20; 20, for_statement; 20, 21; 20, 24; 20, 32; 21, pattern_list; 21, 22; 21, 23; 22, identifier:element; 23, identifier:current_count; 24, call; 24, 25; 24, 26; 25, identifier:list; 26, argument_list; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:items; 31, argument_list; 32, block; 32, 33; 32, 43; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:multiplicity; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:other; 39, identifier:get; 40, argument_list; 40, 41; 40, 42; 41, identifier:element; 42, integer:0; 43, if_statement; 43, 44; 43, 47; 44, comparison_operator:<; 44, 45; 44, 46; 45, identifier:multiplicity; 46, identifier:current_count; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:self; 52, identifier:element; 53, identifier:multiplicity | def intersection_update(self, *others):
r"""Update the multiset, keeping only elements found in it and all others.
>>> ms = Multiset('aab')
>>> ms.intersection_update('bc')
>>> sorted(ms)
['b']
You can also use the ``&=`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.
>>> ms = Multiset('aabc')
>>> ms &= Multiset('abbd')
>>> sorted(ms)
['a', 'b']
For a variant of the operation which does not modify the multiset, but returns a new
multiset instead see :meth:`intersection`.
Args:
others: The other sets to intersect this multiset with. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].
"""
for other in map(self._as_mapping, others):
for element, current_count in list(self.items()):
multiplicity = other.get(element, 0)
if multiplicity < current_count:
self[element] = multiplicity |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:difference_update; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:others; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, for_statement; 10, 11; 10, 12; 10, 19; 11, identifier:other; 12, call; 12, 13; 12, 14; 13, identifier:map; 14, argument_list; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:_as_multiset; 18, identifier:others; 19, block; 19, 20; 20, for_statement; 20, 21; 20, 24; 20, 29; 21, pattern_list; 21, 22; 21, 23; 22, identifier:element; 23, identifier:multiplicity; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:other; 27, identifier:items; 28, argument_list; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:discard; 35, argument_list; 35, 36; 35, 37; 36, identifier:element; 37, identifier:multiplicity | def difference_update(self, *others):
r"""Remove all elements contained the others from this multiset.
>>> ms = Multiset('aab')
>>> ms.difference_update('abc')
>>> sorted(ms)
['a']
You can also use the ``-=`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.
>>> ms = Multiset('aabbbc')
>>> ms -= Multiset('abd')
>>> sorted(ms)
['a', 'b', 'b', 'c']
For a variant of the operation which does not modify the multiset, but returns a new
multiset instead see :meth:`difference`.
Args:
others: The other sets to remove from this multiset. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].
"""
for other in map(self._as_multiset, others):
for element, multiplicity in other.items():
self.discard(element, multiplicity) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:symmetric_difference_update; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:other; 6, block; 6, 7; 6, 9; 6, 18; 6, 38; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:other; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_as_multiset; 16, argument_list; 16, 17; 17, identifier:other; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:elements; 21, binary_operator:|; 21, 22; 21, 30; 22, call; 22, 23; 22, 24; 23, identifier:set; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:distinct_elements; 29, argument_list; 30, call; 30, 31; 30, 32; 31, identifier:set; 32, argument_list; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:other; 36, identifier:distinct_elements; 37, argument_list; 38, for_statement; 38, 39; 38, 40; 38, 41; 39, identifier:element; 40, identifier:elements; 41, block; 41, 42; 41, 48; 41, 54; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:multiplicity; 45, subscript; 45, 46; 45, 47; 46, identifier:self; 47, identifier:element; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:other_count; 51, subscript; 51, 52; 51, 53; 52, identifier:other; 53, identifier:element; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:self; 58, identifier:element; 59, parenthesized_expression; 59, 60; 60, conditional_expression:if; 60, 61; 60, 64; 60, 67; 61, binary_operator:-; 61, 62; 61, 63; 62, identifier:multiplicity; 63, identifier:other_count; 64, comparison_operator:>; 64, 65; 64, 66; 65, identifier:multiplicity; 66, identifier:other_count; 67, binary_operator:-; 67, 68; 67, 69; 68, identifier:other_count; 69, identifier:multiplicity | def symmetric_difference_update(self, other):
r"""Update the multiset to contain only elements in either this multiset or the other but not both.
>>> ms = Multiset('aab')
>>> ms.symmetric_difference_update('abc')
>>> sorted(ms)
['a', 'c']
You can also use the ``^=`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.
>>> ms = Multiset('aabbbc')
>>> ms ^= Multiset('abd')
>>> sorted(ms)
['a', 'b', 'b', 'c', 'd']
For a variant of the operation which does not modify the multiset, but returns a new
multiset instead see :meth:`symmetric_difference`.
Args:
other: The other set to take the symmetric difference with. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].
"""
other = self._as_multiset(other)
elements = set(self.distinct_elements()) | set(other.distinct_elements())
for element in elements:
multiplicity = self[element]
other_count = other[element]
self[element] = (multiplicity - other_count if multiplicity > other_count else other_count - multiplicity) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:times_update; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:factor; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 9, 19; 9, 30; 10, comparison_operator:<; 10, 11; 10, 12; 11, identifier:factor; 12, integer:0; 13, block; 13, 14; 14, raise_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:ValueError; 17, argument_list; 17, 18; 18, string:"The factor must not be negative."; 19, elif_clause; 19, 20; 19, 23; 20, comparison_operator:==; 20, 21; 20, 22; 21, identifier:factor; 22, integer:0; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:clear; 29, argument_list; 30, else_clause; 30, 31; 31, block; 31, 32; 31, 38; 31, 48; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:_elements; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_elements; 38, for_statement; 38, 39; 38, 40; 38, 41; 39, identifier:element; 40, identifier:_elements; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, augmented_assignment:*=; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:_elements; 46, identifier:element; 47, identifier:factor; 48, expression_statement; 48, 49; 49, augmented_assignment:*=; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_total; 53, identifier:factor | def times_update(self, factor):
"""Update each this multiset by multiplying each element's multiplicity with the given scalar factor.
>>> ms = Multiset('aab')
>>> ms.times_update(2)
>>> sorted(ms)
['a', 'a', 'a', 'a', 'b', 'b']
You can also use the ``*=`` operator for the same effect:
>>> ms = Multiset('ac')
>>> ms *= 3
>>> sorted(ms)
['a', 'a', 'a', 'c', 'c', 'c']
For a variant of the operation which does not modify the multiset, but returns a new
multiset instead see :meth:`times`.
Args:
factor: The factor to multiply each multiplicity with.
"""
if factor < 0:
raise ValueError("The factor must not be negative.")
elif factor == 0:
self.clear()
else:
_elements = self._elements
for element in _elements:
_elements[element] *= factor
self._total *= factor |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:add; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:element; 6, default_parameter; 6, 7; 6, 8; 7, identifier:multiplicity; 8, integer:1; 9, block; 9, 10; 9, 12; 9, 22; 9, 30; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 16; 13, comparison_operator:<; 13, 14; 13, 15; 14, identifier:multiplicity; 15, integer:1; 16, block; 16, 17; 17, raise_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:ValueError; 20, argument_list; 20, 21; 21, string:"Multiplicity must be positive"; 22, expression_statement; 22, 23; 23, augmented_assignment:+=; 23, 24; 23, 29; 24, subscript; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:_elements; 28, identifier:element; 29, identifier:multiplicity; 30, expression_statement; 30, 31; 31, augmented_assignment:+=; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_total; 35, identifier:multiplicity | def add(self, element, multiplicity=1):
"""Adds an element to the multiset.
>>> ms = Multiset()
>>> ms.add('a')
>>> sorted(ms)
['a']
An optional multiplicity can be specified to define how many of the element are added:
>>> ms.add('b', 2)
>>> sorted(ms)
['a', 'b', 'b']
This extends the :meth:`MutableSet.add` signature to allow specifying the multiplicity.
Args:
element:
The element to add to the multiset.
multiplicity:
The multiplicity i.e. count of elements to add.
"""
if multiplicity < 1:
raise ValueError("Multiplicity must be positive")
self._elements[element] += multiplicity
self._total += multiplicity |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:remove; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:element; 6, default_parameter; 6, 7; 6, 8; 7, identifier:multiplicity; 8, None; 9, block; 9, 10; 9, 12; 9, 18; 9, 25; 9, 35; 9, 81; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:_elements; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:_elements; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:not; 19, 20; 19, 21; 20, identifier:element; 21, identifier:_elements; 22, block; 22, 23; 23, raise_statement; 23, 24; 24, identifier:KeyError; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:old_multiplicity; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:_elements; 31, identifier:get; 32, argument_list; 32, 33; 32, 34; 33, identifier:element; 34, integer:0; 35, if_statement; 35, 36; 35, 43; 35, 54; 35, 64; 36, boolean_operator:or; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:multiplicity; 39, None; 40, comparison_operator:>=; 40, 41; 40, 42; 41, identifier:multiplicity; 42, identifier:old_multiplicity; 43, block; 43, 44; 43, 48; 44, delete_statement; 44, 45; 45, subscript; 45, 46; 45, 47; 46, identifier:_elements; 47, identifier:element; 48, expression_statement; 48, 49; 49, augmented_assignment:-=; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_total; 53, identifier:old_multiplicity; 54, elif_clause; 54, 55; 54, 58; 55, comparison_operator:<; 55, 56; 55, 57; 56, identifier:multiplicity; 57, integer:0; 58, block; 58, 59; 59, raise_statement; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:ValueError; 62, argument_list; 62, 63; 63, string:"Multiplicity must be not be negative"; 64, elif_clause; 64, 65; 64, 68; 65, comparison_operator:>; 65, 66; 65, 67; 66, identifier:multiplicity; 67, integer:0; 68, block; 68, 69; 68, 75; 69, expression_statement; 69, 70; 70, augmented_assignment:-=; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:_elements; 73, identifier:element; 74, identifier:multiplicity; 75, expression_statement; 75, 76; 76, augmented_assignment:-=; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:_total; 80, identifier:multiplicity; 81, return_statement; 81, 82; 82, identifier:old_multiplicity | def remove(self, element, multiplicity=None):
"""Removes an element from the multiset.
If no multiplicity is specified, the element is completely removed from the multiset:
>>> ms = Multiset('aabbbc')
>>> ms.remove('a')
2
>>> sorted(ms)
['b', 'b', 'b', 'c']
If the multiplicity is given, it is subtracted from the element's multiplicity in the multiset:
>>> ms.remove('b', 2)
3
>>> sorted(ms)
['b', 'c']
It is not an error to remove more elements than are in the set:
>>> ms.remove('b', 2)
1
>>> sorted(ms)
['c']
This extends the :meth:`MutableSet.remove` signature to allow specifying the multiplicity.
Args:
element:
The element to remove from the multiset.
multiplicity:
An optional multiplicity i.e. count of elements to remove.
Returns:
The multiplicity of the element in the multiset before
the removal.
Raises:
KeyError: if the element is not contained in the set. Use :meth:`discard` if
you do not want an exception to be raised.
"""
_elements = self._elements
if element not in _elements:
raise KeyError
old_multiplicity = _elements.get(element, 0)
if multiplicity is None or multiplicity >= old_multiplicity:
del _elements[element]
self._total -= old_multiplicity
elif multiplicity < 0:
raise ValueError("Multiplicity must be not be negative")
elif multiplicity > 0:
_elements[element] -= multiplicity
self._total -= multiplicity
return old_multiplicity |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:discard; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:element; 6, default_parameter; 6, 7; 6, 8; 7, identifier:multiplicity; 8, None; 9, block; 9, 10; 9, 12; 9, 18; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:_elements; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:_elements; 18, if_statement; 18, 19; 18, 22; 18, 77; 19, comparison_operator:in; 19, 20; 19, 21; 20, identifier:element; 21, identifier:_elements; 22, block; 22, 23; 22, 29; 22, 75; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:old_multiplicity; 26, subscript; 26, 27; 26, 28; 27, identifier:_elements; 28, identifier:element; 29, if_statement; 29, 30; 29, 37; 29, 48; 29, 58; 30, boolean_operator:or; 30, 31; 30, 34; 31, comparison_operator:is; 31, 32; 31, 33; 32, identifier:multiplicity; 33, None; 34, comparison_operator:>=; 34, 35; 34, 36; 35, identifier:multiplicity; 36, identifier:old_multiplicity; 37, block; 37, 38; 37, 42; 38, delete_statement; 38, 39; 39, subscript; 39, 40; 39, 41; 40, identifier:_elements; 41, identifier:element; 42, expression_statement; 42, 43; 43, augmented_assignment:-=; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:_total; 47, identifier:old_multiplicity; 48, elif_clause; 48, 49; 48, 52; 49, comparison_operator:<; 49, 50; 49, 51; 50, identifier:multiplicity; 51, integer:0; 52, block; 52, 53; 53, raise_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:ValueError; 56, argument_list; 56, 57; 57, string:"Multiplicity must not be negative"; 58, elif_clause; 58, 59; 58, 62; 59, comparison_operator:>; 59, 60; 59, 61; 60, identifier:multiplicity; 61, integer:0; 62, block; 62, 63; 62, 69; 63, expression_statement; 63, 64; 64, augmented_assignment:-=; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:_elements; 67, identifier:element; 68, identifier:multiplicity; 69, expression_statement; 69, 70; 70, augmented_assignment:-=; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:_total; 74, identifier:multiplicity; 75, return_statement; 75, 76; 76, identifier:old_multiplicity; 77, else_clause; 77, 78; 78, block; 78, 79; 79, return_statement; 79, 80; 80, integer:0 | def discard(self, element, multiplicity=None):
"""Removes the `element` from the multiset.
If multiplicity is ``None``, all occurrences of the element are removed:
>>> ms = Multiset('aab')
>>> ms.discard('a')
2
>>> sorted(ms)
['b']
Otherwise, the multiplicity is subtracted from the one in the multiset and the
old multiplicity is removed:
>>> ms = Multiset('aab')
>>> ms.discard('a', 1)
2
>>> sorted(ms)
['a', 'b']
In contrast to :meth:`remove`, this does not raise an error if the
element is not in the multiset:
>>> ms = Multiset('a')
>>> ms.discard('b')
0
>>> sorted(ms)
['a']
It is also not an error to remove more elements than are in the set:
>>> ms.remove('a', 2)
1
>>> sorted(ms)
[]
Args:
element:
The element to remove from the multiset.
multiplicity:
An optional multiplicity i.e. count of elements to remove.
Returns:
The multiplicity of the element in the multiset before
the removal.
"""
_elements = self._elements
if element in _elements:
old_multiplicity = _elements[element]
if multiplicity is None or multiplicity >= old_multiplicity:
del _elements[element]
self._total -= old_multiplicity
elif multiplicity < 0:
raise ValueError("Multiplicity must not be negative")
elif multiplicity > 0:
_elements[element] -= multiplicity
self._total -= multiplicity
return old_multiplicity
else:
return 0 |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:async_thread; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 10; 4, identifier:sol; 5, identifier:args; 6, identifier:node_attr; 7, identifier:node_id; 8, list_splat_pattern; 8, 9; 9, identifier:a; 10, dictionary_splat_pattern; 10, 11; 11, identifier:kw; 12, block; 12, 13; 12, 15; 12, 34; 12, 51; 12, 55; 12, 81; 12, 87; 12, 101; 12, 120; 12, 209; 12, 219; 12, 232; 12, 245; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:executor; 18, call; 18, 19; 18, 20; 19, identifier:_get_executor; 20, argument_list; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:_executor_name; 23, argument_list; 23, 24; 23, 31; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:kw; 27, identifier:get; 28, argument_list; 28, 29; 28, 30; 29, string:'executor'; 30, False; 31, attribute; 31, 32; 31, 33; 32, identifier:sol; 33, identifier:dsp; 34, if_statement; 34, 35; 34, 37; 35, not_operator; 35, 36; 36, identifier:executor; 37, block; 37, 38; 38, return_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:sol; 42, identifier:_evaluate_node; 43, argument_list; 43, 44; 43, 45; 43, 46; 43, 47; 43, 49; 44, identifier:args; 45, identifier:node_attr; 46, identifier:node_id; 47, list_splat; 47, 48; 48, identifier:a; 49, dictionary_splat; 49, 50; 50, identifier:kw; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:futures; 54, identifier:args; 55, if_statement; 55, 56; 55, 70; 56, boolean_operator:and; 56, 57; 56, 62; 57, comparison_operator:==; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:node_attr; 60, string:'type'; 61, string:'data'; 62, parenthesized_expression; 62, 63; 63, boolean_operator:or; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:node_attr; 66, string:'wait_inputs'; 67, comparison_operator:in; 67, 68; 67, 69; 68, string:'function'; 69, identifier:node_attr; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:futures; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:args; 78, integer:0; 79, identifier:values; 80, argument_list; 81, import_from_statement; 81, 82; 81, 85; 82, dotted_name; 82, 83; 82, 84; 83, identifier:concurrent; 84, identifier:futures; 85, dotted_name; 85, 86; 86, identifier:Future; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:futures; 90, set_comprehension; 90, 91; 90, 92; 90, 95; 91, identifier:v; 92, for_in_clause; 92, 93; 92, 94; 93, identifier:v; 94, identifier:futures; 95, if_clause; 95, 96; 96, call; 96, 97; 96, 98; 97, identifier:isinstance; 98, argument_list; 98, 99; 98, 100; 99, identifier:v; 100, identifier:Future; 101, function_definition; 101, 102; 101, 103; 101, 104; 102, function_name:_submit; 103, parameters; 104, block; 104, 105; 105, return_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:executor; 109, identifier:thread; 110, argument_list; 110, 111; 110, 112; 110, 113; 110, 114; 110, 115; 110, 116; 110, 118; 111, identifier:_async_eval; 112, identifier:sol; 113, identifier:args; 114, identifier:node_attr; 115, identifier:node_id; 116, list_splat; 116, 117; 117, identifier:a; 118, dictionary_splat; 118, 119; 119, identifier:kw; 120, if_statement; 120, 121; 120, 122; 120, 123; 120, 201; 121, identifier:futures; 122, comment; 123, block; 123, 124; 123, 130; 123, 161; 123, 187; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:result; 127, call; 127, 128; 127, 129; 128, identifier:Future; 129, argument_list; 130, function_definition; 130, 131; 130, 132; 130, 134; 131, function_name:_set_res; 132, parameters; 132, 133; 133, identifier:fut; 134, block; 134, 135; 135, try_statement; 135, 136; 135, 148; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:result; 141, identifier:set_result; 142, argument_list; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:fut; 146, identifier:result; 147, argument_list; 148, except_clause; 148, 149; 148, 153; 149, as_pattern; 149, 150; 149, 151; 150, identifier:BaseException; 151, as_pattern_target; 151, 152; 152, identifier:ex; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:result; 158, identifier:set_exception; 159, argument_list; 159, 160; 160, identifier:ex; 161, function_definition; 161, 162; 161, 163; 161, 167; 162, function_name:_submit_task; 163, parameters; 163, 164; 164, default_parameter; 164, 165; 164, 166; 165, identifier:fut; 166, None; 167, block; 167, 168; 167, 175; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:futures; 172, identifier:discard; 173, argument_list; 173, 174; 174, identifier:fut; 175, expression_statement; 175, 176; 176, boolean_operator:and; 176, 177; 176, 179; 177, not_operator; 177, 178; 178, identifier:futures; 179, call; 179, 180; 179, 185; 180, attribute; 180, 181; 180, 184; 181, call; 181, 182; 181, 183; 182, identifier:_submit; 183, argument_list; 184, identifier:add_done_callback; 185, argument_list; 185, 186; 186, identifier:_set_res; 187, for_statement; 187, 188; 187, 189; 187, 193; 188, identifier:f; 189, call; 189, 190; 189, 191; 190, identifier:list; 191, argument_list; 191, 192; 192, identifier:futures; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:f; 198, identifier:add_done_callback; 199, argument_list; 199, 200; 200, identifier:_submit_task; 201, else_clause; 201, 202; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:result; 206, call; 206, 207; 206, 208; 207, identifier:_submit; 208, argument_list; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:timeout; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:node_attr; 215, identifier:get; 216, argument_list; 216, 217; 216, 218; 217, string:'await_result'; 218, False; 219, if_statement; 219, 220; 219, 223; 220, comparison_operator:is; 220, 221; 220, 222; 221, identifier:timeout; 222, False; 223, block; 223, 224; 224, return_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:_await_result; 227, argument_list; 227, 228; 227, 229; 227, 230; 227, 231; 228, identifier:result; 229, identifier:timeout; 230, identifier:sol; 231, identifier:node_id; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:n; 235, call; 235, 236; 235, 237; 236, identifier:len; 237, argument_list; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:node_attr; 241, identifier:get; 242, argument_list; 242, 243; 242, 244; 243, string:'outputs'; 244, list:[]; 245, return_statement; 245, 246; 246, conditional_expression:if; 246, 247; 246, 256; 246, 259; 247, call; 247, 248; 247, 249; 248, identifier:AsyncList; 249, argument_list; 249, 250; 249, 253; 250, keyword_argument; 250, 251; 250, 252; 251, identifier:future; 252, identifier:result; 253, keyword_argument; 253, 254; 253, 255; 254, identifier:n; 255, identifier:n; 256, comparison_operator:>; 256, 257; 256, 258; 257, identifier:n; 258, integer:1; 259, identifier:result | def async_thread(sol, args, node_attr, node_id, *a, **kw):
"""
Execute `sol._evaluate_node` in an asynchronous thread.
:param sol:
Solution to be updated.
:type sol: schedula.utils.sol.Solution
:param args:
Arguments to be passed to node calls.
:type args: tuple
:param node_attr:
Dictionary of node attributes.
:type node_attr: dict
:param node_id:
Data or function node id.
:type node_id: str
:param a:
Extra args to invoke `sol._evaluate_node`.
:type a: tuple
:param kw:
Extra kwargs to invoke `sol._evaluate_node`.
:type kw: dict
:return:
Function result.
:rtype: concurrent.futures.Future | AsyncList
"""
executor = _get_executor(_executor_name(kw.get('executor', False), sol.dsp))
if not executor:
return sol._evaluate_node(args, node_attr, node_id, *a, **kw)
futures = args
if node_attr['type'] == 'data' and (
node_attr['wait_inputs'] or 'function' in node_attr):
futures = args[0].values()
from concurrent.futures import Future
futures = {v for v in futures if isinstance(v, Future)}
def _submit():
return executor.thread(
_async_eval, sol, args, node_attr, node_id, *a, **kw
)
if futures: # Chain results.
result = Future()
def _set_res(fut):
try:
result.set_result(fut.result())
except BaseException as ex:
result.set_exception(ex)
def _submit_task(fut=None):
futures.discard(fut)
not futures and _submit().add_done_callback(_set_res)
for f in list(futures):
f.add_done_callback(_submit_task)
else:
result = _submit()
timeout = node_attr.get('await_result', False)
if timeout is not False:
return _await_result(result, timeout, sol, node_id)
n = len(node_attr.get('outputs', []))
return AsyncList(future=result, n=n) if n > 1 else result |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:pivot; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:table; 5, identifier:left; 6, identifier:top; 7, identifier:value; 8, block; 8, 9; 8, 11; 8, 15; 8, 19; 8, 23; 8, 119; 8, 121; 8, 161; 8, 168; 8, 175; 8, 179; 8, 181; 8, 238; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:rs; 14, dictionary; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:ysort; 18, list:[]; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:xsort; 22, list:[]; 23, for_statement; 23, 24; 23, 25; 23, 26; 24, identifier:row; 25, identifier:table; 26, block; 26, 27; 26, 40; 26, 41; 26, 53; 26, 66; 26, 67; 26, 79; 26, 94; 26, 109; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:yaxis; 30, call; 30, 31; 30, 32; 31, identifier:tuple; 32, argument_list; 32, 33; 33, list_comprehension; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:row; 36, identifier:c; 37, for_in_clause; 37, 38; 37, 39; 38, identifier:c; 39, identifier:left; 40, comment; 41, if_statement; 41, 42; 41, 45; 42, comparison_operator:not; 42, 43; 42, 44; 43, identifier:yaxis; 44, identifier:ysort; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:ysort; 50, identifier:append; 51, argument_list; 51, 52; 52, identifier:yaxis; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:xaxis; 56, call; 56, 57; 56, 58; 57, identifier:tuple; 58, argument_list; 58, 59; 59, list_comprehension; 59, 60; 59, 63; 60, subscript; 60, 61; 60, 62; 61, identifier:row; 62, identifier:c; 63, for_in_clause; 63, 64; 63, 65; 64, identifier:c; 65, identifier:top; 66, comment; 67, if_statement; 67, 68; 67, 71; 68, comparison_operator:not; 68, 69; 68, 70; 69, identifier:xaxis; 70, identifier:xsort; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:xsort; 76, identifier:append; 77, argument_list; 77, 78; 78, identifier:xaxis; 79, try_statement; 79, 80; 79, 85; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, subscript; 82, 83; 82, 84; 83, identifier:rs; 84, identifier:yaxis; 85, except_clause; 85, 86; 85, 87; 86, identifier:KeyError; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:rs; 92, identifier:yaxis; 93, dictionary; 94, if_statement; 94, 95; 94, 100; 95, comparison_operator:not; 95, 96; 95, 97; 96, identifier:xaxis; 97, subscript; 97, 98; 97, 99; 98, identifier:rs; 99, identifier:yaxis; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 108; 103, subscript; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:rs; 106, identifier:yaxis; 107, identifier:xaxis; 108, integer:0; 109, expression_statement; 109, 110; 110, augmented_assignment:+=; 110, 111; 110, 116; 111, subscript; 111, 112; 111, 115; 112, subscript; 112, 113; 112, 114; 113, identifier:rs; 114, identifier:yaxis; 115, identifier:xaxis; 116, subscript; 116, 117; 116, 118; 117, identifier:row; 118, identifier:value; 119, expression_statement; 119, 120; 120, comment; 121, for_statement; 121, 122; 121, 123; 121, 124; 122, identifier:key; 123, identifier:rs; 124, block; 124, 125; 125, if_statement; 125, 126; 125, 137; 126, binary_operator:-; 126, 127; 126, 133; 127, call; 127, 128; 127, 129; 128, identifier:len; 129, argument_list; 129, 130; 130, subscript; 130, 131; 130, 132; 131, identifier:rs; 132, identifier:key; 133, call; 133, 134; 133, 135; 134, identifier:len; 135, argument_list; 135, 136; 136, identifier:xsort; 137, block; 137, 138; 138, for_statement; 138, 139; 138, 140; 138, 141; 139, identifier:var; 140, identifier:xsort; 141, block; 141, 142; 142, if_statement; 142, 143; 142, 152; 143, comparison_operator:not; 143, 144; 143, 145; 144, identifier:var; 145, call; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:rs; 149, identifier:key; 150, identifier:keys; 151, argument_list; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 160; 155, subscript; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:rs; 158, identifier:key; 159, identifier:var; 160, string:''; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:headings; 164, call; 164, 165; 164, 166; 165, identifier:list; 166, argument_list; 166, 167; 167, identifier:left; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:headings; 172, identifier:extend; 173, argument_list; 173, 174; 174, identifier:xsort; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:t; 178, list:[]; 179, expression_statement; 179, 180; 180, comment; 181, for_statement; 181, 182; 181, 183; 181, 184; 182, identifier:left; 183, identifier:ysort; 184, block; 184, 185; 184, 192; 184, 205; 184, 217; 184, 224; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:row; 188, call; 188, 189; 188, 190; 189, identifier:list; 190, argument_list; 190, 191; 191, identifier:left; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:sortedkeys; 195, call; 195, 196; 195, 197; 196, identifier:sorted; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 204; 199, attribute; 199, 200; 199, 203; 200, subscript; 200, 201; 200, 202; 201, identifier:rs; 202, identifier:left; 203, identifier:keys; 204, argument_list; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:sortedvalues; 208, call; 208, 209; 208, 210; 209, identifier:map; 210, argument_list; 210, 211; 210, 216; 211, attribute; 211, 212; 211, 215; 212, subscript; 212, 213; 212, 214; 213, identifier:rs; 214, identifier:left; 215, identifier:get; 216, identifier:sortedkeys; 217, expression_statement; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:row; 221, identifier:extend; 222, argument_list; 222, 223; 223, identifier:sortedvalues; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:t; 228, identifier:append; 229, argument_list; 229, 230; 230, call; 230, 231; 230, 232; 231, identifier:dict; 232, argument_list; 232, 233; 233, call; 233, 234; 233, 235; 234, identifier:zip; 235, argument_list; 235, 236; 235, 237; 236, identifier:headings; 237, identifier:row; 238, return_statement; 238, 239; 239, identifier:t | def pivot(table, left, top, value):
"""
Creates a cross-tab or pivot table from a normalised input table. Use this
function to 'denormalize' a table of normalized records.
* The table argument can be a list of dictionaries or a Table object.
(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/334621)
* The left argument is a tuple of headings which are displayed down the
left side of the new table.
* The top argument is a tuple of headings which are displayed across the
top of the new table.
Tuples are used so that multiple element headings and columns can be used.
E.g. To transform the list (listOfDicts):
Name, Year, Value
-----------------------
'Simon', 2004, 32
'Simon', 2005, 128
'Russel', 2004, 64
'Eric', 2004, 52
'Russel', 2005, 32
into the new list:
'Name', 2004, 2005
------------------------
'Simon', 32, 128
'Russel', 64, 32
'Eric', 52, NA
you would call pivot with the arguments:
newList = pivot(listOfDicts, ('Name',), ('Year',), 'Value')
"""
rs = {}
ysort = []
xsort = []
for row in table:
yaxis = tuple([row[c] for c in left]) # e.g. yaxis = ('Simon',)
if yaxis not in ysort: ysort.append(yaxis)
xaxis = tuple([row[c] for c in top]) # e.g. xaxis = ('2004',)
if xaxis not in xsort: xsort.append(xaxis)
try:
rs[yaxis]
except KeyError:
rs[yaxis] = {}
if xaxis not in rs[yaxis]:
rs[yaxis][xaxis] = 0
rs[yaxis][xaxis] += row[value]
"""
In the following loop we take care of missing data,
e.g 'Eric' has a value in 2004 but not in 2005
"""
for key in rs:
if len(rs[key]) - len(xsort):
for var in xsort:
if var not in rs[key].keys():
rs[key][var] = ''
headings = list(left)
headings.extend(xsort)
t = []
"""
The lists 'sortedkeys' and 'sortedvalues' make sure that
even if the field 'top' is unordered, data will be transposed correctly.
E.g. in the example above the table rows are not ordered by the year
"""
for left in ysort:
row = list(left)
sortedkeys = sorted(rs[left].keys())
sortedvalues = map(rs[left].get, sortedkeys)
row.extend(sortedvalues)
t.append(dict(zip(headings,row)))
return t |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:download_hrrr_for_gssha; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 13; 3, 16; 3, 19; 4, identifier:main_directory; 5, identifier:forecast_start_date_string; 6, comment; 7, identifier:forecast_start_hour_string; 8, comment; 9, default_parameter; 9, 10; 9, 11; 10, identifier:leftlon; 11, unary_operator:-; 11, 12; 12, integer:180; 13, default_parameter; 13, 14; 13, 15; 14, identifier:rightlon; 15, integer:180; 16, default_parameter; 16, 17; 16, 18; 17, identifier:toplat; 18, integer:90; 19, default_parameter; 19, 20; 19, 21; 20, identifier:bottomlat; 21, unary_operator:-; 21, 22; 22, integer:90; 23, block; 23, 24; 23, 26; 23, 36; 23, 47; 23, 70; 23, 74; 23, 273; 24, expression_statement; 24, 25; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:out_directory; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:path; 32, identifier:join; 33, argument_list; 33, 34; 33, 35; 34, identifier:main_directory; 35, identifier:forecast_start_date_string; 36, try_statement; 36, 37; 36, 43; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:mkdir; 41, argument_list; 41, 42; 42, identifier:out_directory; 43, except_clause; 43, 44; 43, 45; 44, identifier:OSError; 45, block; 45, 46; 46, pass_statement; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:forecast_timestep_hour_string_array; 50, list:['00', '01', '02', '03', '04',
'05', '06', '07', '08', '09',
'10', '11', '12', '13', '14',
'15', '16', '17', '18']; 50, 51; 50, 52; 50, 53; 50, 54; 50, 55; 50, 56; 50, 57; 50, 58; 50, 59; 50, 60; 50, 61; 50, 62; 50, 63; 50, 64; 50, 65; 50, 66; 50, 67; 50, 68; 50, 69; 51, string:'00'; 52, string:'01'; 53, string:'02'; 54, string:'03'; 55, string:'04'; 56, string:'05'; 57, string:'06'; 58, string:'07'; 59, string:'08'; 60, string:'09'; 61, string:'10'; 62, string:'11'; 63, string:'12'; 64, string:'13'; 65, string:'14'; 66, string:'15'; 67, string:'16'; 68, string:'17'; 69, string:'18'; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:downloaded_file_list; 73, list:[]; 74, for_statement; 74, 75; 74, 76; 74, 77; 75, identifier:forecast_timestep_hour_string; 76, identifier:forecast_timestep_hour_string_array; 77, block; 77, 78; 77, 88; 77, 166; 77, 181; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:file_name; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, string:'hrrr.t{0}z.wrfsfcf{1}.grib2'; 84, identifier:format; 85, argument_list; 85, 86; 85, 87; 86, identifier:forecast_start_hour_string; 87, identifier:forecast_timestep_hour_string; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:payload; 91, dictionary; 91, 92; 91, 95; 91, 98; 91, 101; 91, 104; 91, 107; 91, 110; 91, 113; 91, 116; 91, 119; 91, 122; 91, 125; 91, 128; 91, 131; 91, 134; 91, 140; 91, 146; 91, 152; 91, 158; 92, pair; 92, 93; 92, 94; 93, string:'file'; 94, identifier:file_name; 95, pair; 95, 96; 95, 97; 96, string:'lev_10_m_above_ground'; 97, string:'on'; 98, pair; 98, 99; 98, 100; 99, string:'lev_2_m_above_ground'; 100, string:'on'; 101, pair; 101, 102; 101, 103; 102, string:'lev_entire_atmosphere'; 103, string:'on'; 104, pair; 104, 105; 104, 106; 105, string:'lev_surface'; 106, string:'on'; 107, pair; 107, 108; 107, 109; 108, string:'var_DSWRF'; 109, string:'on'; 110, pair; 110, 111; 110, 112; 111, string:'var_PRATE'; 112, string:'on'; 113, pair; 113, 114; 113, 115; 114, string:'var_PRES'; 115, string:'on'; 116, pair; 116, 117; 116, 118; 117, string:'var_RH'; 118, string:'on'; 119, pair; 119, 120; 119, 121; 120, string:'var_TMP'; 121, string:'on'; 122, pair; 122, 123; 122, 124; 123, string:'var_UGRD'; 124, string:'on'; 125, pair; 125, 126; 125, 127; 126, string:'var_VGRD'; 127, string:'on'; 128, pair; 128, 129; 128, 130; 129, string:'var_TCDC'; 130, string:'on'; 131, pair; 131, 132; 131, 133; 132, string:'subregion'; 133, string:''; 134, pair; 134, 135; 134, 136; 135, string:'leftlon'; 136, call; 136, 137; 136, 138; 137, identifier:str; 138, argument_list; 138, 139; 139, identifier:leftlon; 140, pair; 140, 141; 140, 142; 141, string:'rightlon'; 142, call; 142, 143; 142, 144; 143, identifier:str; 144, argument_list; 144, 145; 145, identifier:rightlon; 146, pair; 146, 147; 146, 148; 147, string:'toplat'; 148, call; 148, 149; 148, 150; 149, identifier:str; 150, argument_list; 150, 151; 151, identifier:toplat; 152, pair; 152, 153; 152, 154; 153, string:'bottomlat'; 154, call; 154, 155; 154, 156; 155, identifier:str; 156, argument_list; 156, 157; 157, identifier:bottomlat; 158, pair; 158, 159; 158, 160; 159, string:'dir'; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, string:'/hrrr.{0}'; 163, identifier:format; 164, argument_list; 164, 165; 165, identifier:forecast_start_date_string; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:r; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:requests; 172, identifier:get; 173, argument_list; 173, 174; 173, 175; 173, 178; 174, string:'http://nomads.ncep.noaa.gov/cgi-bin/filter_hrrr_2d.pl'; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:params; 177, identifier:payload; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:stream; 180, True; 181, if_statement; 181, 182; 181, 191; 181, 239; 182, comparison_operator:==; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:r; 185, identifier:status_code; 186, attribute; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:requests; 189, identifier:codes; 190, identifier:ok; 191, block; 191, 192; 191, 202; 191, 209; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:out_file; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:path; 198, identifier:join; 199, argument_list; 199, 200; 199, 201; 200, identifier:out_directory; 201, identifier:file_name; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:downloaded_file_list; 206, identifier:append; 207, argument_list; 207, 208; 208, identifier:out_file; 209, with_statement; 209, 210; 209, 220; 210, with_clause; 210, 211; 211, with_item; 211, 212; 212, as_pattern; 212, 213; 212, 218; 213, call; 213, 214; 213, 215; 214, identifier:open; 215, argument_list; 215, 216; 215, 217; 216, identifier:out_file; 217, string:'wb'; 218, as_pattern_target; 218, 219; 219, identifier:fd; 220, block; 220, 221; 221, for_statement; 221, 222; 221, 223; 221, 231; 222, identifier:chunk; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:r; 226, identifier:iter_content; 227, argument_list; 227, 228; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:chunk_size; 230, integer:1024; 231, block; 231, 232; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:fd; 236, identifier:write; 237, argument_list; 237, 238; 238, identifier:chunk; 239, else_clause; 239, 240; 240, block; 240, 241; 240, 253; 240, 268; 240, 272; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:log; 245, identifier:error; 246, argument_list; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, string:"Problem downloading {0}"; 250, identifier:format; 251, argument_list; 251, 252; 252, identifier:file_name; 253, for_statement; 253, 254; 253, 255; 253, 256; 254, identifier:filename; 255, identifier:downloaded_file_list; 256, block; 256, 257; 257, try_statement; 257, 258; 257, 264; 258, block; 258, 259; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 262; 261, identifier:remove; 262, argument_list; 262, 263; 263, identifier:filename; 264, except_clause; 264, 265; 264, 266; 265, identifier:OSError; 266, block; 266, 267; 267, pass_statement; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:downloaded_file_list; 271, list:[]; 272, break_statement; 273, return_statement; 273, 274; 274, identifier:downloaded_file_list | def download_hrrr_for_gssha(main_directory,
forecast_start_date_string, #EX. '20160913'
forecast_start_hour_string, #EX. '00' to '23'
leftlon=-180, rightlon=180,
toplat=90,bottomlat=-90):
"""
Function to download HRRR data for GSSHA
URL:
http://nomads.ncep.noaa.gov/cgi-bin/filter_hrrr_2d.pl
Args:
main_directory(str): Location of the output for the forecast data.
forecast_start_date_string(str): String for day of forecast. Ex. '20160913'
forecast_start_hour_string(str): String for hour of forecast start. Ex. '02'
leftlon(Optional[double,int]): Left bound for longitude. Default is -180.
rightlon(Optional[double,int]): Right bound for longitude. Default is 180.
toplat(Optional[double,int]): Top bound for latitude. Default is 90.
bottomlat(Optional[double,int]): Bottom bound for latitude. Default is -90.
Returns:
downloaded_file_list(list): List of paths to downloaded files.
Example::
from gsshapy.grid.hrrr_to_gssha import download_hrrr_for_gssha
hrrr_folder = '/HRRR'
leftlon = -95
rightlon = -75
toplat = 35
bottomlat = 30
downloaded_file_list = download_hrrr_for_gssha(hrrr_folder,'20160914','01',
leftlon,rightlon,toplat,bottomlat)
"""
out_directory = path.join(main_directory, forecast_start_date_string)
try:
mkdir(out_directory)
except OSError:
pass
forecast_timestep_hour_string_array = ['00', '01', '02', '03', '04',
'05', '06', '07', '08', '09',
'10', '11', '12', '13', '14',
'15', '16', '17', '18']
downloaded_file_list = []
for forecast_timestep_hour_string in forecast_timestep_hour_string_array:
file_name = 'hrrr.t{0}z.wrfsfcf{1}.grib2'.format(forecast_start_hour_string, forecast_timestep_hour_string)
payload = {
'file': file_name,
'lev_10_m_above_ground': 'on',
'lev_2_m_above_ground': 'on',
'lev_entire_atmosphere': 'on',
'lev_surface': 'on',
'var_DSWRF': 'on',
'var_PRATE': 'on',
'var_PRES': 'on',
'var_RH': 'on',
'var_TMP': 'on',
'var_UGRD': 'on',
'var_VGRD': 'on',
'var_TCDC': 'on',
'subregion': '',
'leftlon': str(leftlon),
'rightlon': str(rightlon),
'toplat': str(toplat),
'bottomlat': str(bottomlat),
'dir': '/hrrr.{0}'.format(forecast_start_date_string),
}
r = requests.get('http://nomads.ncep.noaa.gov/cgi-bin/filter_hrrr_2d.pl', params=payload, stream=True)
if r.status_code == requests.codes.ok:
out_file = path.join(out_directory, file_name)
downloaded_file_list.append(out_file)
with open(out_file, 'wb') as fd:
for chunk in r.iter_content(chunk_size=1024):
fd.write(chunk)
else:
log.error("Problem downloading {0}".format(file_name))
for filename in downloaded_file_list:
try:
remove(filename)
except OSError:
pass
downloaded_file_list = []
break
return downloaded_file_list |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_convert_data_to_hourly; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:gssha_data_var; 6, block; 6, 7; 6, 9; 6, 32; 6, 41; 6, 45; 6, 396; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:time_step_hours; 12, binary_operator:/; 12, 13; 12, 25; 13, subscript; 13, 14; 13, 24; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:np; 17, identifier:diff; 18, argument_list; 18, 19; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:data; 23, identifier:time; 24, integer:0; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:np; 28, identifier:timedelta64; 29, argument_list; 29, 30; 29, 31; 30, integer:1; 31, string:'h'; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:calc_function; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:_get_calc_function; 39, argument_list; 39, 40; 40, identifier:gssha_data_var; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:resampled_data; 44, None; 45, if_statement; 45, 46; 45, 49; 45, 70; 46, comparison_operator:<; 46, 47; 46, 48; 47, identifier:time_step_hours; 48, integer:1; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:resampled_data; 53, call; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:data; 58, identifier:resample; 59, argument_list; 59, 60; 59, 61; 59, 64; 59, 67; 60, string:'1H'; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:dim; 63, string:'time'; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:how; 66, identifier:calc_function; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:keep_attrs; 69, True; 70, elif_clause; 70, 71; 70, 74; 71, comparison_operator:>; 71, 72; 71, 73; 72, identifier:time_step_hours; 73, integer:1; 74, block; 74, 75; 74, 92; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:resampled_data; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:data; 83, identifier:resample; 84, argument_list; 84, 85; 84, 86; 84, 89; 85, string:'1H'; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:dim; 88, string:'time'; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:keep_attrs; 91, True; 92, for_statement; 92, 93; 92, 94; 92, 104; 93, identifier:time_idx; 94, call; 94, 95; 94, 96; 95, identifier:range; 96, argument_list; 96, 97; 97, subscript; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:data; 102, identifier:dims; 103, string:'time'; 104, block; 104, 105; 105, if_statement; 105, 106; 105, 117; 105, 118; 105, 283; 106, comparison_operator:<; 106, 107; 106, 110; 107, binary_operator:+; 107, 108; 107, 109; 108, identifier:time_idx; 109, integer:1; 110, subscript; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:data; 115, identifier:dims; 116, string:'time'; 117, comment; 118, block; 118, 119; 118, 131; 118, 145; 118, 159; 118, 176; 118, 192; 118, 216; 118, 246; 118, 257; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:start_time; 122, attribute; 122, 123; 122, 130; 123, subscript; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:data; 128, identifier:time; 129, identifier:time_idx; 130, identifier:values; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:end_time; 134, attribute; 134, 135; 134, 144; 135, subscript; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:data; 140, identifier:time; 141, binary_operator:+; 141, 142; 141, 143; 142, identifier:time_idx; 143, integer:1; 144, identifier:values; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:slope_timeslice; 148, call; 148, 149; 148, 150; 149, identifier:slice; 150, argument_list; 150, 151; 150, 155; 151, call; 151, 152; 151, 153; 152, identifier:str; 153, argument_list; 153, 154; 154, identifier:start_time; 155, call; 155, 156; 155, 157; 156, identifier:str; 157, argument_list; 157, 158; 158, identifier:end_time; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:slice_size; 162, binary_operator:-; 162, 163; 162, 175; 163, subscript; 163, 164; 163, 174; 164, attribute; 164, 165; 164, 173; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:resampled_data; 168, identifier:sel; 169, argument_list; 169, 170; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:time; 172, identifier:slope_timeslice; 173, identifier:dims; 174, string:'time'; 175, integer:1; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:first_timestep; 179, subscript; 179, 180; 179, 191; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:resampled_data; 183, identifier:sel; 184, argument_list; 184, 185; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:time; 187, call; 187, 188; 187, 189; 188, identifier:str; 189, argument_list; 189, 190; 190, identifier:start_time; 191, identifier:gssha_data_var; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:slope; 195, binary_operator:/; 195, 196; 195, 212; 196, parenthesized_expression; 196, 197; 197, binary_operator:-; 197, 198; 197, 211; 198, subscript; 198, 199; 198, 210; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:resampled_data; 202, identifier:sel; 203, argument_list; 203, 204; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:time; 206, call; 206, 207; 206, 208; 207, identifier:str; 208, argument_list; 208, 209; 209, identifier:end_time; 210, identifier:gssha_data_var; 211, identifier:first_timestep; 212, call; 212, 213; 212, 214; 213, identifier:float; 214, argument_list; 214, 215; 215, identifier:slice_size; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:data_timeslice; 219, call; 219, 220; 219, 221; 220, identifier:slice; 221, argument_list; 221, 222; 221, 234; 222, call; 222, 223; 222, 224; 223, identifier:str; 224, argument_list; 224, 225; 225, binary_operator:+; 225, 226; 225, 227; 226, identifier:start_time; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:np; 230, identifier:timedelta64; 231, argument_list; 231, 232; 231, 233; 232, integer:1; 233, string:'m'; 234, call; 234, 235; 234, 236; 235, identifier:str; 236, argument_list; 236, 237; 237, binary_operator:-; 237, 238; 237, 239; 238, identifier:end_time; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:np; 242, identifier:timedelta64; 243, argument_list; 243, 244; 243, 245; 244, integer:1; 245, string:'m'; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:data_subset; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:resampled_data; 252, identifier:sel; 253, argument_list; 253, 254; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:time; 256, identifier:data_timeslice; 257, for_statement; 257, 258; 257, 259; 257, 267; 258, identifier:xidx; 259, call; 259, 260; 259, 261; 260, identifier:range; 261, argument_list; 261, 262; 262, subscript; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:data_subset; 265, identifier:dims; 266, string:'time'; 267, block; 267, 268; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 275; 270, subscript; 270, 271; 270, 274; 271, subscript; 271, 272; 271, 273; 272, identifier:data_subset; 273, identifier:gssha_data_var; 274, identifier:xidx; 275, binary_operator:+; 275, 276; 275, 277; 276, identifier:first_timestep; 277, binary_operator:*; 277, 278; 277, 279; 278, identifier:slope; 279, parenthesized_expression; 279, 280; 280, binary_operator:+; 280, 281; 280, 282; 281, identifier:xidx; 282, integer:1; 283, else_clause; 283, 284; 283, 285; 284, comment; 285, block; 285, 286; 285, 298; 285, 309; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:start_time; 289, attribute; 289, 290; 289, 297; 290, subscript; 290, 291; 290, 296; 291, attribute; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:self; 294, identifier:data; 295, identifier:time; 296, identifier:time_idx; 297, identifier:values; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:end_time; 301, attribute; 301, 302; 301, 308; 302, subscript; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:resampled_data; 305, identifier:time; 306, unary_operator:-; 306, 307; 307, integer:1; 308, identifier:values; 309, if_statement; 309, 310; 309, 313; 310, comparison_operator:>; 310, 311; 310, 312; 311, identifier:end_time; 312, identifier:start_time; 313, block; 313, 314; 313, 330; 313, 344; 313, 355; 313, 359; 313, 372; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:first_timestep; 317, subscript; 317, 318; 317, 329; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:resampled_data; 321, identifier:sel; 322, argument_list; 322, 323; 323, keyword_argument; 323, 324; 323, 325; 324, identifier:time; 325, call; 325, 326; 325, 327; 326, identifier:str; 327, argument_list; 327, 328; 328, identifier:start_time; 329, identifier:gssha_data_var; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:data_timeslice; 333, call; 333, 334; 333, 335; 334, identifier:slice; 335, argument_list; 335, 336; 335, 340; 336, call; 336, 337; 336, 338; 337, identifier:str; 338, argument_list; 338, 339; 339, identifier:start_time; 340, call; 340, 341; 340, 342; 341, identifier:str; 342, argument_list; 342, 343; 343, identifier:end_time; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 347; 346, identifier:data_subset; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:resampled_data; 350, identifier:sel; 351, argument_list; 351, 352; 352, keyword_argument; 352, 353; 352, 354; 353, identifier:time; 354, identifier:data_timeslice; 355, expression_statement; 355, 356; 356, assignment; 356, 357; 356, 358; 357, identifier:slice_size; 358, integer:1; 359, if_statement; 359, 360; 359, 363; 360, comparison_operator:==; 360, 361; 360, 362; 361, identifier:calc_function; 362, string:"mean"; 363, block; 363, 364; 364, expression_statement; 364, 365; 365, assignment; 365, 366; 365, 367; 366, identifier:slice_size; 367, subscript; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:data_subset; 370, identifier:dims; 371, string:'time'; 372, for_statement; 372, 373; 372, 374; 372, 382; 373, identifier:xidx; 374, call; 374, 375; 374, 376; 375, identifier:range; 376, argument_list; 376, 377; 377, subscript; 377, 378; 377, 381; 378, attribute; 378, 379; 378, 380; 379, identifier:data_subset; 380, identifier:dims; 381, string:'time'; 382, block; 382, 383; 383, expression_statement; 383, 384; 384, assignment; 384, 385; 384, 390; 385, subscript; 385, 386; 385, 389; 386, subscript; 386, 387; 386, 388; 387, identifier:data_subset; 388, identifier:gssha_data_var; 389, identifier:xidx; 390, binary_operator:/; 390, 391; 390, 392; 391, identifier:first_timestep; 392, call; 392, 393; 392, 394; 393, identifier:float; 394, argument_list; 394, 395; 395, identifier:slice_size; 396, if_statement; 396, 397; 396, 400; 396, 401; 397, comparison_operator:is; 397, 398; 397, 399; 398, identifier:resampled_data; 399, None; 400, comment; 401, block; 401, 402; 401, 441; 401, 480; 402, if_statement; 402, 403; 402, 414; 403, comparison_operator:not; 403, 404; 403, 411; 404, attribute; 404, 405; 404, 410; 405, attribute; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:self; 408, identifier:data; 409, identifier:lsm; 410, identifier:x_var; 411, attribute; 411, 412; 411, 413; 412, identifier:resampled_data; 413, identifier:coords; 414, block; 414, 415; 415, expression_statement; 415, 416; 416, assignment; 416, 417; 416, 428; 417, subscript; 417, 418; 417, 421; 418, attribute; 418, 419; 418, 420; 419, identifier:resampled_data; 420, identifier:coords; 421, attribute; 421, 422; 421, 427; 422, attribute; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:self; 425, identifier:data; 426, identifier:lsm; 427, identifier:x_var; 428, subscript; 428, 429; 428, 434; 429, attribute; 429, 430; 429, 433; 430, attribute; 430, 431; 430, 432; 431, identifier:self; 432, identifier:data; 433, identifier:coords; 434, attribute; 434, 435; 434, 440; 435, attribute; 435, 436; 435, 439; 436, attribute; 436, 437; 436, 438; 437, identifier:self; 438, identifier:data; 439, identifier:lsm; 440, identifier:x_var; 441, if_statement; 441, 442; 441, 453; 442, comparison_operator:not; 442, 443; 442, 450; 443, attribute; 443, 444; 443, 449; 444, attribute; 444, 445; 444, 448; 445, attribute; 445, 446; 445, 447; 446, identifier:self; 447, identifier:data; 448, identifier:lsm; 449, identifier:y_var; 450, attribute; 450, 451; 450, 452; 451, identifier:resampled_data; 452, identifier:coords; 453, block; 453, 454; 454, expression_statement; 454, 455; 455, assignment; 455, 456; 455, 467; 456, subscript; 456, 457; 456, 460; 457, attribute; 457, 458; 457, 459; 458, identifier:resampled_data; 459, identifier:coords; 460, attribute; 460, 461; 460, 466; 461, attribute; 461, 462; 461, 465; 462, attribute; 462, 463; 462, 464; 463, identifier:self; 464, identifier:data; 465, identifier:lsm; 466, identifier:y_var; 467, subscript; 467, 468; 467, 473; 468, attribute; 468, 469; 468, 472; 469, attribute; 469, 470; 469, 471; 470, identifier:self; 471, identifier:data; 472, identifier:coords; 473, attribute; 473, 474; 473, 479; 474, attribute; 474, 475; 474, 478; 475, attribute; 475, 476; 475, 477; 476, identifier:self; 477, identifier:data; 478, identifier:lsm; 479, identifier:y_var; 480, expression_statement; 480, 481; 481, assignment; 481, 482; 481, 485; 482, attribute; 482, 483; 482, 484; 483, identifier:self; 484, identifier:data; 485, identifier:resampled_data | def _convert_data_to_hourly(self, gssha_data_var):
"""
This function converts the data to hourly data
and then puts it into the data_np_array
USED WHEN GENERATING HMET DATA ONLY
"""
time_step_hours = np.diff(self.data.time)[0]/np.timedelta64(1, 'h')
calc_function = self._get_calc_function(gssha_data_var)
resampled_data = None
if time_step_hours < 1:
resampled_data = self.data.resample('1H', dim='time',
how=calc_function,
keep_attrs=True)
elif time_step_hours > 1:
resampled_data = self.data.resample('1H', dim='time',
keep_attrs=True)
for time_idx in range(self.data.dims['time']):
if time_idx+1 < self.data.dims['time']:
# interpolate between time steps
start_time = self.data.time[time_idx].values
end_time = self.data.time[time_idx+1].values
slope_timeslice = slice(str(start_time), str(end_time))
slice_size = resampled_data.sel(time=slope_timeslice).dims['time'] - 1
first_timestep = resampled_data.sel(time=str(start_time))[gssha_data_var]
slope = (resampled_data.sel(time=str(end_time))[gssha_data_var]
- first_timestep)/float(slice_size)
data_timeslice = slice(str(start_time+np.timedelta64(1, 'm')),
str(end_time-np.timedelta64(1, 'm')))
data_subset = resampled_data.sel(time=data_timeslice)
for xidx in range(data_subset.dims['time']):
data_subset[gssha_data_var][xidx] = first_timestep + slope * (xidx+1)
else:
# just continue to repeat the timestep
start_time = self.data.time[time_idx].values
end_time = resampled_data.time[-1].values
if end_time > start_time:
first_timestep = resampled_data.sel(time=str(start_time))[gssha_data_var]
data_timeslice = slice(str(start_time), str(end_time))
data_subset = resampled_data.sel(time=data_timeslice)
slice_size = 1
if calc_function == "mean":
slice_size = data_subset.dims['time']
for xidx in range(data_subset.dims['time']):
data_subset[gssha_data_var][xidx] = first_timestep/float(slice_size)
if resampled_data is not None:
# make sure coordinates copied
if self.data.lsm.x_var not in resampled_data.coords:
resampled_data.coords[self.data.lsm.x_var] = self.data.coords[self.data.lsm.x_var]
if self.data.lsm.y_var not in resampled_data.coords:
resampled_data.coords[self.data.lsm.y_var] = self.data.coords[self.data.lsm.y_var]
self.data = resampled_data |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:get_sub_dsp; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:nodes_bunch; 6, default_parameter; 6, 7; 6, 8; 7, identifier:edges_bunch; 8, None; 9, block; 9, 10; 9, 12; 9, 13; 9, 30; 9, 31; 9, 53; 9, 54; 9, 68; 9, 82; 9, 90; 9, 91; 9, 131; 9, 132; 9, 149; 9, 150; 9, 189; 9, 194; 9, 195; 9, 212; 9, 213; 9, 231; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:nodes_bunch; 16, list_comprehension; 16, 17; 16, 27; 17, subscript; 17, 18; 17, 26; 18, subscript; 18, 19; 18, 25; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:get_node; 23, argument_list; 23, 24; 24, identifier:u; 25, integer:1; 26, integer:0; 27, for_in_clause; 27, 28; 27, 29; 28, identifier:u; 29, identifier:nodes_bunch; 30, comment; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:sub_dsp; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:copy_structure; 38, argument_list; 38, 39; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:dmap; 41, call; 41, 42; 41, 52; 42, attribute; 42, 43; 42, 51; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:dmap; 48, identifier:subgraph; 49, argument_list; 49, 50; 50, identifier:nodes_bunch; 51, identifier:copy; 52, argument_list; 53, comment; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, pattern_list; 56, 57; 56, 58; 57, identifier:nodes; 58, identifier:dmap_out_degree; 59, expression_list; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:sub_dsp; 62, identifier:nodes; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:sub_dsp; 66, identifier:dmap; 67, identifier:out_degree; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, pattern_list; 70, 71; 70, 72; 71, identifier:dmap_dv; 72, identifier:dmap_rm_edge; 73, expression_list; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:default_values; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:sub_dsp; 80, identifier:dmap; 81, identifier:remove_edge; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:dmap_rm_node; 85, attribute; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:sub_dsp; 88, identifier:dmap; 89, identifier:remove_node; 90, comment; 91, for_statement; 91, 92; 91, 93; 91, 94; 92, identifier:u; 93, identifier:nodes_bunch; 94, block; 94, 95; 94, 107; 94, 108; 94, 109; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:n; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:nodes; 102, identifier:u; 103, identifier:get; 104, argument_list; 104, 105; 104, 106; 105, string:'inputs'; 106, None; 107, comment; 108, comment; 109, if_statement; 109, 110; 109, 124; 110, boolean_operator:and; 110, 111; 110, 114; 111, comparison_operator:is; 111, 112; 111, 113; 112, identifier:n; 113, None; 114, not_operator; 114, 115; 115, call; 115, 116; 115, 122; 116, attribute; 116, 117; 116, 121; 117, call; 117, 118; 117, 119; 118, identifier:set; 119, argument_list; 119, 120; 120, identifier:n; 121, identifier:issubset; 122, argument_list; 122, 123; 123, identifier:nodes_bunch; 124, block; 124, 125; 124, 130; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:dmap_rm_node; 128, argument_list; 128, 129; 129, identifier:u; 130, comment; 131, comment; 132, if_statement; 132, 133; 132, 136; 133, comparison_operator:is; 133, 134; 133, 135; 134, identifier:edges_bunch; 135, None; 136, block; 136, 137; 137, for_statement; 137, 138; 137, 139; 137, 140; 137, 141; 138, identifier:e; 139, identifier:edges_bunch; 140, comment; 141, block; 141, 142; 141, 148; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 145; 144, identifier:dmap_rm_edge; 145, argument_list; 145, 146; 146, list_splat; 146, 147; 147, identifier:e; 148, comment; 149, comment; 150, for_statement; 150, 151; 150, 152; 150, 173; 150, 174; 151, identifier:u; 152, list_comprehension; 152, 153; 152, 154; 152, 167; 153, identifier:u; 154, for_in_clause; 154, 155; 154, 158; 155, pattern_list; 155, 156; 155, 157; 156, identifier:u; 157, identifier:n; 158, call; 158, 159; 158, 166; 159, attribute; 159, 160; 159, 165; 160, attribute; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:sub_dsp; 163, identifier:dmap; 164, identifier:nodes; 165, identifier:items; 166, argument_list; 167, if_clause; 167, 168; 168, comparison_operator:==; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:n; 171, string:'type'; 172, string:'function'; 173, comment; 174, block; 174, 175; 175, if_statement; 175, 176; 175, 181; 175, 182; 176, not_operator; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:dmap_out_degree; 179, argument_list; 179, 180; 180, identifier:u; 181, comment; 182, block; 182, 183; 182, 188; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:dmap_rm_node; 186, argument_list; 186, 187; 187, identifier:u; 188, comment; 189, import_from_statement; 189, 190; 189, 192; 190, dotted_name; 190, 191; 191, identifier:networkx; 192, dotted_name; 192, 193; 193, identifier:isolates; 194, comment; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 202; 197, attribute; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:sub_dsp; 200, identifier:dmap; 201, identifier:remove_nodes_from; 202, argument_list; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:list; 205, argument_list; 205, 206; 206, call; 206, 207; 206, 208; 207, identifier:isolates; 208, argument_list; 208, 209; 209, attribute; 209, 210; 209, 211; 210, identifier:sub_dsp; 211, identifier:dmap; 212, comment; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:sub_dsp; 217, identifier:default_values; 218, dictionary_comprehension; 218, 219; 218, 224; 218, 227; 219, pair; 219, 220; 219, 221; 220, identifier:k; 221, subscript; 221, 222; 221, 223; 222, identifier:dmap_dv; 223, identifier:k; 224, for_in_clause; 224, 225; 224, 226; 225, identifier:k; 226, identifier:dmap_dv; 227, if_clause; 227, 228; 228, comparison_operator:in; 228, 229; 228, 230; 229, identifier:k; 230, identifier:nodes; 231, return_statement; 231, 232; 232, identifier:sub_dsp | def get_sub_dsp(self, nodes_bunch, edges_bunch=None):
"""
Returns the sub-dispatcher induced by given node and edge bunches.
The induced sub-dispatcher contains the available nodes in nodes_bunch
and edges between those nodes, excluding those that are in edges_bunch.
The available nodes are non isolated nodes and function nodes that have
all inputs and at least one output.
:param nodes_bunch:
A container of node ids which will be iterated through once.
:type nodes_bunch: list[str], iterable
:param edges_bunch:
A container of edge ids that will be removed.
:type edges_bunch: list[(str, str)], iterable, optional
:return:
A dispatcher.
:rtype: Dispatcher
.. seealso:: :func:`get_sub_dsp_from_workflow`
.. note::
The sub-dispatcher edge or node attributes just point to the
original dispatcher. So changes to the node or edge structure
will not be reflected in the original dispatcher map while changes
to the attributes will.
**--------------------------------------------------------------------**
**Example**:
A dispatcher with a two functions `fun1` and `fun2`:
.. dispatcher:: dsp
:opt: graph_attr={'ratio': '1'}
>>> dsp = Dispatcher(name='Dispatcher')
>>> dsp.add_function(function_id='fun1', inputs=['a', 'b'],
... outputs=['c', 'd'])
'fun1'
>>> dsp.add_function(function_id='fun2', inputs=['a', 'd'],
... outputs=['c', 'e'])
'fun2'
Get the sub-dispatcher induced by given nodes bunch::
>>> sub_dsp = dsp.get_sub_dsp(['a', 'c', 'd', 'e', 'fun2'])
.. dispatcher:: sub_dsp
:opt: graph_attr={'ratio': '1'}
>>> sub_dsp.name = 'Sub-Dispatcher'
"""
# Get real paths.
nodes_bunch = [self.get_node(u)[1][0] for u in nodes_bunch]
# Define an empty dispatcher.
sub_dsp = self.copy_structure(
dmap=self.dmap.subgraph(nodes_bunch).copy()
)
# Namespace shortcuts for speed.
nodes, dmap_out_degree = sub_dsp.nodes, sub_dsp.dmap.out_degree
dmap_dv, dmap_rm_edge = self.default_values, sub_dsp.dmap.remove_edge
dmap_rm_node = sub_dsp.dmap.remove_node
# Remove function nodes that has not whole inputs available.
for u in nodes_bunch:
n = nodes[u].get('inputs', None) # Function inputs.
# No all inputs
if n is not None and not set(n).issubset(nodes_bunch):
dmap_rm_node(u) # Remove function node.
# Remove edges that are not in edges_bunch.
if edges_bunch is not None:
for e in edges_bunch: # Iterate sub-graph edges.
dmap_rm_edge(*e) # Remove edge.
# Remove function node with no outputs.
for u in [u for u, n in sub_dsp.dmap.nodes.items()
if n['type'] == 'function']:
# noinspection PyCallingNonCallable
if not dmap_out_degree(u): # No outputs.
dmap_rm_node(u) # Remove function node.
from networkx import isolates
# Remove isolate nodes from sub-graph.
sub_dsp.dmap.remove_nodes_from(list(isolates(sub_dsp.dmap)))
# Set default values.
sub_dsp.default_values = {k: dmap_dv[k] for k in dmap_dv if k in nodes}
return sub_dsp |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:blue; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:memo; 7, None; 8, block; 8, 9; 8, 11; 8, 20; 8, 29; 8, 37; 8, 47; 8, 77; 8, 83; 8, 92; 8, 108; 8, 146; 8, 303; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:memo; 14, conditional_expression:if; 14, 15; 14, 16; 14, 19; 15, dictionary; 16, comparison_operator:is; 16, 17; 16, 18; 17, identifier:memo; 18, None; 19, identifier:memo; 20, if_statement; 20, 21; 20, 24; 21, comparison_operator:in; 21, 22; 21, 23; 22, identifier:self; 23, identifier:memo; 24, block; 24, 25; 25, return_statement; 25, 26; 26, subscript; 26, 27; 26, 28; 27, identifier:memo; 28, identifier:self; 29, import_from_statement; 29, 30; 29, 35; 30, relative_import; 30, 31; 30, 32; 31, import_prefix; 32, dotted_name; 32, 33; 32, 34; 33, identifier:utils; 34, identifier:dsp; 35, dotted_name; 35, 36; 36, identifier:map_list; 37, import_from_statement; 37, 38; 37, 43; 37, 45; 38, relative_import; 38, 39; 38, 40; 39, import_prefix; 40, dotted_name; 40, 41; 40, 42; 41, identifier:utils; 42, identifier:blue; 43, dotted_name; 43, 44; 44, identifier:BlueDispatcher; 45, dotted_name; 45, 46; 46, identifier:_parent_blue; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:memo; 51, identifier:self; 52, assignment; 52, 53; 52, 54; 53, identifier:blue; 54, call; 54, 55; 54, 56; 55, identifier:BlueDispatcher; 56, argument_list; 56, 57; 56, 62; 56, 67; 56, 72; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:executor; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:executor; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:name; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:name; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:raises; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:raises; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:description; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:__doc__; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:dfl; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:default_values; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:key_map_data; 86, list:['data_id', {'value': 'default_value'}]; 86, 87; 86, 88; 87, string:'data_id'; 88, dictionary; 88, 89; 89, pair; 89, 90; 89, 91; 90, string:'value'; 91, string:'default_value'; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 97; 94, pattern_list; 94, 95; 94, 96; 95, identifier:pred; 96, identifier:succ; 97, expression_list; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:dmap; 102, identifier:pred; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:dmap; 107, identifier:succ; 108, function_definition; 108, 109; 108, 110; 108, 114; 109, function_name:_set_weight; 110, parameters; 110, 111; 110, 112; 110, 113; 111, identifier:n; 112, identifier:r; 113, identifier:d; 114, block; 114, 115; 114, 137; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:d; 118, dictionary_comprehension; 118, 119; 118, 124; 118, 133; 119, pair; 119, 120; 119, 121; 120, identifier:i; 121, subscript; 121, 122; 121, 123; 122, identifier:j; 123, string:'weight'; 124, for_in_clause; 124, 125; 124, 128; 125, pattern_list; 125, 126; 125, 127; 126, identifier:i; 127, identifier:j; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:d; 131, identifier:items; 132, argument_list; 133, if_clause; 133, 134; 134, comparison_operator:in; 134, 135; 134, 136; 135, string:'weight'; 136, identifier:j; 137, if_statement; 137, 138; 137, 139; 138, identifier:d; 139, block; 139, 140; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:r; 144, identifier:n; 145, identifier:d; 146, for_statement; 146, 147; 146, 150; 146, 170; 147, pattern_list; 147, 148; 147, 149; 148, identifier:k; 149, identifier:v; 150, call; 150, 151; 150, 152; 151, identifier:sorted; 152, argument_list; 152, 153; 152, 160; 153, call; 153, 154; 153, 159; 154, attribute; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:nodes; 158, identifier:items; 159, argument_list; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:key; 162, lambda; 162, 163; 162, 165; 163, lambda_parameters; 163, 164; 164, identifier:x; 165, subscript; 165, 166; 165, 169; 166, subscript; 166, 167; 166, 168; 167, identifier:x; 168, integer:1; 169, string:'index'; 170, block; 170, 171; 170, 179; 170, 188; 170, 192; 170, 292; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:v; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:v; 177, identifier:copy; 178, argument_list; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:t; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:v; 185, identifier:pop; 186, argument_list; 186, 187; 187, string:'type'; 188, delete_statement; 188, 189; 189, subscript; 189, 190; 189, 191; 190, identifier:v; 191, string:'index'; 192, if_statement; 192, 193; 192, 196; 192, 220; 193, comparison_operator:==; 193, 194; 193, 195; 194, identifier:t; 195, string:'data'; 196, block; 196, 197; 196, 201; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:method; 200, string:'add_data'; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:combine_dicts; 204, argument_list; 204, 205; 204, 217; 205, call; 205, 206; 205, 207; 206, identifier:map_list; 207, argument_list; 207, 208; 207, 209; 207, 210; 208, identifier:key_map_data; 209, identifier:k; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:dfl; 213, identifier:get; 214, argument_list; 214, 215; 214, 216; 215, identifier:k; 216, dictionary; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:base; 219, identifier:v; 220, elif_clause; 220, 221; 220, 226; 221, comparison_operator:in; 221, 222; 221, 223; 222, identifier:t; 223, tuple; 223, 224; 223, 225; 224, string:'function'; 225, string:'dispatcher'; 226, block; 226, 227; 226, 233; 226, 242; 226, 250; 226, 254; 226, 263; 226, 272; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:method; 230, binary_operator:%; 230, 231; 230, 232; 231, string:'add_%s'; 232, identifier:t; 233, if_statement; 233, 234; 233, 237; 234, comparison_operator:==; 234, 235; 234, 236; 235, identifier:t; 236, string:'dispatcher'; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:t; 241, string:'dsp'; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 249; 244, subscript; 244, 245; 244, 246; 245, identifier:v; 246, binary_operator:%; 246, 247; 246, 248; 247, string:'%s_id'; 248, identifier:t; 249, identifier:k; 250, delete_statement; 250, 251; 251, subscript; 251, 252; 251, 253; 252, identifier:v; 253, string:'wait_inputs'; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 257; 256, identifier:_set_weight; 257, argument_list; 257, 258; 257, 259; 257, 260; 258, string:'inp_weight'; 259, identifier:v; 260, subscript; 260, 261; 260, 262; 261, identifier:pred; 262, identifier:k; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:_set_weight; 266, argument_list; 266, 267; 266, 268; 266, 269; 267, string:'out_weight'; 268, identifier:v; 269, subscript; 269, 270; 269, 271; 270, identifier:succ; 271, identifier:k; 272, if_statement; 272, 273; 272, 276; 273, comparison_operator:in; 273, 274; 273, 275; 274, string:'function'; 275, identifier:v; 276, block; 276, 277; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 282; 279, subscript; 279, 280; 279, 281; 280, identifier:v; 281, identifier:t; 282, call; 282, 283; 282, 284; 283, identifier:_parent_blue; 284, argument_list; 284, 285; 284, 291; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:v; 288, identifier:pop; 289, argument_list; 289, 290; 290, string:'function'; 291, identifier:memo; 292, expression_statement; 292, 293; 293, call; 293, 294; 293, 299; 294, attribute; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:blue; 297, identifier:deferred; 298, identifier:append; 299, argument_list; 299, 300; 300, tuple; 300, 301; 300, 302; 301, identifier:method; 302, identifier:v; 303, return_statement; 303, 304; 304, identifier:blue | def blue(self, memo=None):
"""
Constructs a BlueDispatcher out of the current object.
:param memo:
A dictionary to cache Blueprints.
:type memo: dict[T,schedula.utils.blue.Blueprint]
:return:
A BlueDispatcher of the current object.
:rtype: schedula.utils.blue.BlueDispatcher
"""
memo = {} if memo is None else memo
if self in memo:
return memo[self]
from .utils.dsp import map_list
from .utils.blue import BlueDispatcher, _parent_blue
memo[self] = blue = BlueDispatcher(
executor=self.executor, name=self.name, raises=self.raises,
description=self.__doc__
)
dfl = self.default_values
key_map_data = ['data_id', {'value': 'default_value'}]
pred, succ = self.dmap.pred, self.dmap.succ
def _set_weight(n, r, d):
d = {i: j['weight'] for i, j in d.items() if 'weight' in j}
if d:
r[n] = d
for k, v in sorted(self.nodes.items(), key=lambda x: x[1]['index']):
v = v.copy()
t = v.pop('type')
del v['index']
if t == 'data':
method = 'add_data'
combine_dicts(map_list(key_map_data, k, dfl.get(k, {})), base=v)
elif t in ('function', 'dispatcher'):
method = 'add_%s' % t
if t == 'dispatcher':
t = 'dsp'
v['%s_id' % t] = k
del v['wait_inputs']
_set_weight('inp_weight', v, pred[k])
_set_weight('out_weight', v, succ[k])
if 'function' in v:
v[t] = _parent_blue(v.pop('function'), memo)
blue.deferred.append((method, v))
return blue |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:findOutlet; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:shapefile_path; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 12; 6, 17; 6, 26; 6, 35; 6, 43; 6, 51; 6, 58; 6, 68; 6, 77; 6, 85; 6, 92; 6, 103; 6, 104; 6, 112; 6, 123; 6, 131; 6, 151; 6, 157; 6, 161; 6, 360; 6, 370; 6, 376; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:check_watershed_boundary_geometry; 15, argument_list; 15, 16; 16, identifier:shapefile_path; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:shapefile; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:ogr; 23, identifier:Open; 24, argument_list; 24, 25; 25, identifier:shapefile_path; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:source_layer; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:shapefile; 32, identifier:GetLayer; 33, argument_list; 33, 34; 34, integer:0; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:source_lyr_proj; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:source_layer; 41, identifier:GetSpatialRef; 42, argument_list; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:osr_geographic_proj; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:osr; 49, identifier:SpatialReference; 50, argument_list; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:osr_geographic_proj; 55, identifier:ImportFromEPSG; 56, argument_list; 56, 57; 57, integer:4326; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:proj_transform; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:osr; 64, identifier:CoordinateTransformation; 65, argument_list; 65, 66; 65, 67; 66, identifier:source_lyr_proj; 67, identifier:osr_geographic_proj; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:boundary_feature; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:source_layer; 74, identifier:GetFeature; 75, argument_list; 75, 76; 76, integer:0; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:feat_geom; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:boundary_feature; 83, identifier:GetGeometryRef; 84, argument_list; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:feat_geom; 89, identifier:Transform; 90, argument_list; 90, 91; 91, identifier:proj_transform; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:polygon; 95, call; 95, 96; 95, 97; 96, identifier:shapely_loads; 97, argument_list; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:feat_geom; 101, identifier:ExportToWkb; 102, argument_list; 103, comment; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:mask_grid; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:getGrid; 111, argument_list; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:elevation_grid; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:getGrid; 119, argument_list; 119, 120; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:use_mask; 122, False; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:elevation_array; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:elevation_grid; 129, identifier:np_array; 130, argument_list; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:ma_elevation_array; 134, call; 134, 135; 134, 140; 135, attribute; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:np; 138, identifier:ma; 139, identifier:array; 140, argument_list; 140, 141; 140, 142; 141, identifier:elevation_array; 142, keyword_argument; 142, 143; 142, 144; 143, identifier:mask; 144, comparison_operator:==; 144, 145; 144, 150; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:mask_grid; 148, identifier:np_array; 149, argument_list; 150, integer:0; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:min_elevation; 154, attribute; 154, 155; 154, 156; 155, identifier:sys; 156, identifier:maxsize; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:outlet_pt; 160, None; 161, for_statement; 161, 162; 161, 163; 161, 171; 162, identifier:coord; 163, call; 163, 164; 163, 165; 164, identifier:list; 165, argument_list; 165, 166; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:polygon; 169, identifier:exterior; 170, identifier:coords; 171, block; 171, 172; 171, 191; 171, 198; 171, 345; 172, try_statement; 172, 173; 172, 186; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 179; 176, pattern_list; 176, 177; 176, 178; 177, identifier:col; 178, identifier:row; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:mask_grid; 182, identifier:lonlat2pixel; 183, argument_list; 183, 184; 184, list_splat; 184, 185; 185, identifier:coord; 186, except_clause; 186, 187; 186, 188; 186, 189; 187, identifier:IndexError; 188, comment; 189, block; 189, 190; 190, continue_statement; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:elevation_value; 194, subscript; 194, 195; 194, 196; 194, 197; 195, identifier:ma_elevation_array; 196, identifier:row; 197, identifier:col; 198, if_statement; 198, 199; 198, 206; 198, 207; 198, 208; 199, comparison_operator:is; 199, 200; 199, 201; 200, identifier:elevation_value; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:np; 204, identifier:ma; 205, identifier:masked; 206, comment; 207, comment; 208, block; 208, 209; 208, 216; 208, 222; 208, 226; 208, 230; 208, 234; 208, 325; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:actual_value; 212, subscript; 212, 213; 212, 214; 212, 215; 213, identifier:elevation_array; 214, identifier:row; 215, identifier:col; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:max_diff; 219, attribute; 219, 220; 219, 221; 220, identifier:sys; 221, identifier:maxsize; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:nrow; 225, None; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:ncol; 229, None; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:nval; 233, None; 234, for_statement; 234, 235; 234, 236; 234, 255; 235, identifier:row_ix; 236, call; 236, 237; 236, 238; 237, identifier:range; 238, argument_list; 238, 239; 238, 246; 239, call; 239, 240; 239, 241; 240, identifier:max; 241, argument_list; 241, 242; 241, 245; 242, binary_operator:-; 242, 243; 242, 244; 243, identifier:row; 244, integer:5; 245, integer:0; 246, call; 246, 247; 246, 248; 247, identifier:min; 248, argument_list; 248, 249; 248, 252; 249, binary_operator:+; 249, 250; 249, 251; 250, identifier:row; 251, integer:5; 252, attribute; 252, 253; 252, 254; 253, identifier:mask_grid; 254, identifier:y_size; 255, block; 255, 256; 256, for_statement; 256, 257; 256, 258; 256, 277; 257, identifier:col_ix; 258, call; 258, 259; 258, 260; 259, identifier:range; 260, argument_list; 260, 261; 260, 268; 261, call; 261, 262; 261, 263; 262, identifier:max; 263, argument_list; 263, 264; 263, 267; 264, binary_operator:-; 264, 265; 264, 266; 265, identifier:col; 266, integer:5; 267, integer:0; 268, call; 268, 269; 268, 270; 269, identifier:min; 270, argument_list; 270, 271; 270, 274; 271, binary_operator:+; 271, 272; 271, 273; 272, identifier:col; 273, integer:5; 274, attribute; 274, 275; 274, 276; 275, identifier:mask_grid; 276, identifier:x_size; 277, block; 277, 278; 277, 285; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 281; 280, identifier:val; 281, subscript; 281, 282; 281, 283; 281, 284; 282, identifier:ma_elevation_array; 283, identifier:row_ix; 284, identifier:col_ix; 285, if_statement; 285, 286; 285, 294; 286, not_operator; 286, 287; 287, comparison_operator:is; 287, 288; 287, 289; 288, identifier:val; 289, attribute; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:np; 292, identifier:ma; 293, identifier:masked; 294, block; 294, 295; 294, 304; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:val_diff; 298, call; 298, 299; 298, 300; 299, identifier:abs; 300, argument_list; 300, 301; 301, binary_operator:-; 301, 302; 301, 303; 302, identifier:val; 303, identifier:actual_value; 304, if_statement; 304, 305; 304, 308; 305, comparison_operator:<; 305, 306; 305, 307; 306, identifier:val_diff; 307, identifier:max_diff; 308, block; 308, 309; 308, 313; 308, 317; 308, 321; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 312; 311, identifier:max_diff; 312, identifier:val_diff; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:nval; 316, identifier:val; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:nrow; 320, identifier:row_ix; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:ncol; 324, identifier:col_ix; 325, if_statement; 325, 326; 325, 332; 326, comparison_operator:not; 326, 327; 326, 328; 327, None; 328, tuple; 328, 329; 328, 330; 328, 331; 329, identifier:nrow; 330, identifier:ncol; 331, identifier:nval; 332, block; 332, 333; 332, 337; 332, 341; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 336; 335, identifier:row; 336, identifier:nrow; 337, expression_statement; 337, 338; 338, assignment; 338, 339; 338, 340; 339, identifier:col; 340, identifier:ncol; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 344; 343, identifier:elevation_value; 344, identifier:nval; 345, if_statement; 345, 346; 345, 349; 346, comparison_operator:<; 346, 347; 346, 348; 347, identifier:elevation_value; 348, identifier:min_elevation; 349, block; 349, 350; 349, 354; 350, expression_statement; 350, 351; 351, assignment; 351, 352; 351, 353; 352, identifier:min_elevation; 353, identifier:elevation_value; 354, expression_statement; 354, 355; 355, assignment; 355, 356; 355, 357; 356, identifier:outlet_pt; 357, tuple; 357, 358; 357, 359; 358, identifier:col; 359, identifier:row; 360, if_statement; 360, 361; 360, 364; 361, comparison_operator:is; 361, 362; 361, 363; 362, identifier:outlet_pt; 363, None; 364, block; 364, 365; 365, raise_statement; 365, 366; 366, call; 366, 367; 366, 368; 367, identifier:IndexError; 368, argument_list; 368, 369; 369, string:'No valid outlet points found on boundary ...'; 370, expression_statement; 370, 371; 371, assignment; 371, 372; 371, 375; 372, pattern_list; 372, 373; 372, 374; 373, identifier:outcol; 374, identifier:outrow; 375, identifier:outlet_pt; 376, expression_statement; 376, 377; 377, call; 377, 378; 377, 381; 378, attribute; 378, 379; 378, 380; 379, identifier:self; 380, identifier:setOutlet; 381, argument_list; 381, 382; 381, 387; 382, keyword_argument; 382, 383; 382, 384; 383, identifier:col; 384, binary_operator:+; 384, 385; 384, 386; 385, identifier:outcol; 386, integer:1; 387, keyword_argument; 387, 388; 387, 389; 388, identifier:row; 389, binary_operator:+; 389, 390; 389, 391; 390, identifier:outrow; 391, integer:1 | def findOutlet(self, shapefile_path):
"""
Calculate outlet location
"""
# determine outlet from shapefile
# by getting outlet from first point in polygon
# make sure the boundary geometry is valid
check_watershed_boundary_geometry(shapefile_path)
shapefile = ogr.Open(shapefile_path)
source_layer = shapefile.GetLayer(0)
source_lyr_proj = source_layer.GetSpatialRef()
osr_geographic_proj = osr.SpatialReference()
osr_geographic_proj.ImportFromEPSG(4326)
proj_transform = osr.CoordinateTransformation(source_lyr_proj,
osr_geographic_proj)
boundary_feature = source_layer.GetFeature(0)
feat_geom = boundary_feature.GetGeometryRef()
feat_geom.Transform(proj_transform)
polygon = shapely_loads(feat_geom.ExportToWkb())
# make lowest point on boundary outlet
mask_grid = self.getGrid()
elevation_grid = self.getGrid(use_mask=False)
elevation_array = elevation_grid.np_array()
ma_elevation_array = np.ma.array(elevation_array,
mask=mask_grid.np_array()==0)
min_elevation = sys.maxsize
outlet_pt = None
for coord in list(polygon.exterior.coords):
try:
col, row = mask_grid.lonlat2pixel(*coord)
except IndexError:
# out of bounds
continue
elevation_value = ma_elevation_array[row, col]
if elevation_value is np.ma.masked:
# search for closest value in mask to this point
# elevation within 5 pixels in any direction
actual_value = elevation_array[row, col]
max_diff = sys.maxsize
nrow = None
ncol = None
nval = None
for row_ix in range(max(row-5, 0), min(row+5, mask_grid.y_size)):
for col_ix in range(max(col-5, 0), min(col+5, mask_grid.x_size)):
val = ma_elevation_array[row_ix, col_ix]
if not val is np.ma.masked:
val_diff = abs(val-actual_value)
if val_diff < max_diff:
max_diff = val_diff
nval = val
nrow = row_ix
ncol = col_ix
if None not in (nrow, ncol, nval):
row = nrow
col = ncol
elevation_value = nval
if elevation_value < min_elevation:
min_elevation = elevation_value
outlet_pt = (col, row)
if outlet_pt is None:
raise IndexError('No valid outlet points found on boundary ...')
outcol, outrow = outlet_pt
self.setOutlet(col=outcol+1, row=outrow+1) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_write; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:session; 6, identifier:openFile; 7, identifier:replaceParamFile; 8, block; 8, 9; 8, 11; 8, 12; 8, 18; 8, 19; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:events; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:precipEvents; 18, comment; 19, for_statement; 19, 20; 19, 21; 19, 22; 20, identifier:event; 21, identifier:events; 22, block; 22, 23; 22, 41; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:openFile; 27, identifier:write; 28, argument_list; 28, 29; 29, binary_operator:%; 29, 30; 29, 31; 30, string:'EVENT "%s"\nNRGAG %s\nNRPDS %s\n'; 31, tuple; 31, 32; 31, 35; 31, 38; 32, attribute; 32, 33; 32, 34; 33, identifier:event; 34, identifier:description; 35, attribute; 35, 36; 35, 37; 36, identifier:event; 37, identifier:nrGag; 38, attribute; 38, 39; 38, 40; 39, identifier:event; 40, identifier:nrPds; 41, if_statement; 41, 42; 41, 47; 42, comparison_operator:>; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:event; 45, identifier:nrGag; 46, integer:0; 47, block; 47, 48; 47, 54; 47, 58; 47, 59; 47, 60; 47, 93; 47, 94; 47, 95; 47, 110; 47, 111; 47, 112; 47, 113; 47, 145; 47, 167; 47, 168; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:values; 51, attribute; 51, 52; 51, 53; 52, identifier:event; 53, identifier:values; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:valList; 57, list:[]; 58, comment; 59, comment; 60, for_statement; 60, 61; 60, 62; 60, 63; 61, identifier:value; 62, identifier:values; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:valList; 68, identifier:append; 69, argument_list; 69, 70; 70, dictionary; 70, 71; 70, 76; 70, 81; 70, 88; 71, pair; 71, 72; 71, 73; 72, string:'ValueType'; 73, attribute; 73, 74; 73, 75; 74, identifier:value; 75, identifier:valueType; 76, pair; 76, 77; 76, 78; 77, string:'DateTime'; 78, attribute; 78, 79; 78, 80; 79, identifier:value; 80, identifier:dateTime; 81, pair; 81, 82; 81, 83; 82, string:'Gage'; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:value; 86, identifier:gage; 87, identifier:id; 88, pair; 88, 89; 88, 90; 89, string:'Value'; 90, attribute; 90, 91; 90, 92; 91, identifier:value; 92, identifier:value; 93, comment; 94, comment; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:pivotedValues; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:pivot; 101, identifier:pivot; 102, argument_list; 102, 103; 102, 104; 102, 107; 102, 109; 103, identifier:valList; 104, tuple; 104, 105; 104, 106; 105, string:'DateTime'; 106, string:'ValueType'; 107, tuple; 107, 108; 108, string:'Gage'; 109, string:'Value'; 110, comment; 111, comment; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:gages; 116, call; 116, 117; 116, 144; 117, attribute; 117, 118; 117, 142; 117, 143; 118, call; 118, 119; 118, 138; 119, attribute; 119, 120; 119, 136; 119, 137; 120, call; 120, 121; 120, 130; 121, attribute; 121, 122; 121, 128; 121, 129; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:session; 125, identifier:query; 126, argument_list; 126, 127; 127, identifier:PrecipGage; 128, line_continuation:\; 129, identifier:filter; 130, argument_list; 130, 131; 131, comparison_operator:==; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:PrecipGage; 134, identifier:event; 135, identifier:event; 136, line_continuation:\; 137, identifier:order_by; 138, argument_list; 138, 139; 139, attribute; 139, 140; 139, 141; 140, identifier:PrecipGage; 141, identifier:id; 142, line_continuation:\; 143, identifier:all; 144, argument_list; 145, for_statement; 145, 146; 145, 147; 145, 148; 146, identifier:gage; 147, identifier:gages; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:openFile; 153, identifier:write; 154, argument_list; 154, 155; 155, binary_operator:%; 155, 156; 155, 157; 156, string:'COORD %s %s "%s"\n'; 157, tuple; 157, 158; 157, 161; 157, 164; 158, attribute; 158, 159; 158, 160; 159, identifier:gage; 160, identifier:x; 161, attribute; 161, 162; 161, 163; 162, identifier:gage; 163, identifier:y; 164, attribute; 164, 165; 164, 166; 165, identifier:gage; 166, identifier:description; 167, comment; 168, for_statement; 168, 169; 168, 170; 168, 171; 168, 172; 169, identifier:row; 170, identifier:pivotedValues; 171, comment; 172, block; 172, 173; 172, 177; 172, 178; 172, 179; 172, 198; 172, 199; 172, 222; 172, 223; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:valString; 176, string:''; 177, comment; 178, comment; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:keys; 182, call; 182, 183; 182, 184; 183, identifier:sorted; 184, argument_list; 184, 185; 185, list_comprehension; 185, 186; 185, 187; 185, 190; 186, identifier:key; 187, for_in_clause; 187, 188; 187, 189; 188, identifier:key; 189, identifier:row; 190, if_clause; 190, 191; 191, boolean_operator:and; 191, 192; 191, 195; 192, comparison_operator:!=; 192, 193; 192, 194; 193, identifier:key; 194, string:'DateTime'; 195, comparison_operator:!=; 195, 196; 195, 197; 196, identifier:key; 197, string:'ValueType'; 198, comment; 199, for_statement; 199, 200; 199, 201; 199, 202; 200, identifier:key; 201, identifier:keys; 202, block; 202, 203; 203, if_statement; 203, 204; 203, 211; 204, boolean_operator:and; 204, 205; 204, 208; 205, comparison_operator:!=; 205, 206; 205, 207; 206, identifier:key; 207, string:'DateTime'; 208, comparison_operator:!=; 208, 209; 208, 210; 209, identifier:key; 210, string:'ValueType'; 211, block; 211, 212; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:valString; 215, binary_operator:%; 215, 216; 215, 217; 216, string:'%s %.3f'; 217, tuple; 217, 218; 217, 219; 218, identifier:valString; 219, subscript; 219, 220; 219, 221; 220, identifier:row; 221, identifier:key; 222, comment; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:openFile; 227, identifier:write; 228, argument_list; 228, 229; 229, binary_operator:%; 229, 230; 229, 231; 230, string:'%s %.4d %.2d %.2d %.2d %.2d%s\n'; 231, tuple; 231, 232; 231, 235; 231, 240; 231, 245; 231, 250; 231, 255; 231, 260; 232, subscript; 232, 233; 232, 234; 233, identifier:row; 234, string:'ValueType'; 235, attribute; 235, 236; 235, 239; 236, subscript; 236, 237; 236, 238; 237, identifier:row; 238, string:'DateTime'; 239, identifier:year; 240, attribute; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:row; 243, string:'DateTime'; 244, identifier:month; 245, attribute; 245, 246; 245, 249; 246, subscript; 246, 247; 246, 248; 247, identifier:row; 248, string:'DateTime'; 249, identifier:day; 250, attribute; 250, 251; 250, 254; 251, subscript; 251, 252; 251, 253; 252, identifier:row; 253, string:'DateTime'; 254, identifier:hour; 255, attribute; 255, 256; 255, 259; 256, subscript; 256, 257; 256, 258; 257, identifier:row; 258, string:'DateTime'; 259, identifier:minute; 260, identifier:valString | def _write(self, session, openFile, replaceParamFile):
"""
Precipitation File Write to File Method
"""
# Retrieve the events associated with this PrecipFile
events = self.precipEvents
# Write each event to file
for event in events:
openFile.write('EVENT "%s"\nNRGAG %s\nNRPDS %s\n' % (event.description, event.nrGag, event.nrPds))
if event.nrGag > 0:
values = event.values
valList = []
# Convert PrecipValue objects into a list of dictionaries, valList,
# so that it is compatible with the pivot function.
for value in values:
valList.append({'ValueType': value.valueType,
'DateTime': value.dateTime,
'Gage': value.gage.id,
'Value': value.value})
# Pivot using the function found at:
# code.activestate.com/recipes/334695
pivotedValues = pivot.pivot(valList, ('DateTime', 'ValueType'), ('Gage',), 'Value')
## TODO: Create custom pivot function that can work with sqlalchemy
## objects explicitly without the costly conversion.
# Create an empty set for obtaining a list of unique gages
gages = session.query(PrecipGage). \
filter(PrecipGage.event == event). \
order_by(PrecipGage.id). \
all()
for gage in gages:
openFile.write('COORD %s %s "%s"\n' % (gage.x, gage.y, gage.description))
# Write the value rows out to file
for row in pivotedValues:
# Extract the PrecipValues
valString = ''
# Retreive a list of sorted keys. This assumes the values are
# read into the database in order
keys = sorted([key for key in row if key != 'DateTime' and key != 'ValueType'])
# String all of the values together into valString
for key in keys:
if key != 'DateTime' and key != 'ValueType':
valString = '%s %.3f' % (valString, row[key])
# Write value line to file with appropriate formatting
openFile.write('%s %.4d %.2d %.2d %.2d %.2d%s\n' % (
row['ValueType'],
row['DateTime'].year,
row['DateTime'].month,
row['DateTime'].day,
row['DateTime'].hour,
row['DateTime'].minute,
valString)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:valueWritePreprocessor; 3, parameters; 3, 4; 3, 5; 4, identifier:valueString; 5, default_parameter; 5, 6; 5, 7; 6, identifier:replaceParamsFile; 7, None; 8, block; 8, 9; 8, 11; 8, 28; 8, 29; 8, 33; 8, 34; 8, 96; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 18; 12, comparison_operator:is; 12, 13; 12, 17; 13, call; 13, 14; 13, 15; 14, identifier:type; 15, argument_list; 15, 16; 16, identifier:valueString; 17, identifier:bool; 18, block; 18, 19; 18, 26; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:log; 23, identifier:warning; 24, argument_list; 24, 25; 25, string:"Only numerical variable types can be handled by the valueReadPreprocessor function."; 26, return_statement; 26, 27; 27, identifier:valueString; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:variableString; 32, identifier:valueString; 33, comment; 34, if_statement; 34, 35; 34, 38; 34, 39; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:replaceParamsFile; 37, None; 38, comment; 39, block; 39, 40; 40, if_statement; 40, 41; 40, 44; 40, 49; 41, comparison_operator:==; 41, 42; 41, 43; 42, identifier:variableString; 43, identifier:REPLACE_NO_VALUE; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:variableString; 48, string:'[NO_VARIABLE]'; 49, else_clause; 49, 50; 50, block; 50, 51; 51, try_statement; 51, 52; 51, 93; 52, block; 52, 53; 52, 60; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:number; 56, call; 56, 57; 56, 58; 57, identifier:int; 58, argument_list; 58, 59; 59, identifier:valueString; 60, if_statement; 60, 61; 60, 64; 61, comparison_operator:<; 61, 62; 61, 63; 62, identifier:number; 63, integer:0; 64, block; 64, 65; 64, 72; 64, 73; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:parameterID; 68, binary_operator:*; 68, 69; 68, 70; 69, identifier:number; 70, unary_operator:-; 70, 71; 71, integer:1; 72, comment; 73, for_statement; 73, 74; 73, 75; 73, 78; 74, identifier:targetParam; 75, attribute; 75, 76; 75, 77; 76, identifier:replaceParamsFile; 77, identifier:targetParameters; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 85; 80, comparison_operator:==; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:targetParam; 83, identifier:id; 84, identifier:parameterID; 85, block; 85, 86; 85, 92; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:variableString; 89, attribute; 89, 90; 89, 91; 90, identifier:targetParam; 91, identifier:targetVariable; 92, break_statement; 93, except_clause; 93, 94; 94, block; 94, 95; 95, pass_statement; 96, return_statement; 96, 97; 97, identifier:variableString | def valueWritePreprocessor(valueString, replaceParamsFile=None):
"""
Look up variable name in replace param file for the negative id given and return it.
Args:
valueString (str): String representing the value to be preprocessed.
replaceParamsFile (gsshapy.orm.ReplaceParamFile, optional): Instance of the replace param file. Required if
replacement variables are included in the project.
Returns:
str: Processed value as a string
"""
if type(valueString) is bool:
log.warning("Only numerical variable types can be handled by the valueReadPreprocessor function.")
return valueString
# Default
variableString = valueString
# Check for replacement variables
if replaceParamsFile is not None:
# Set Default
if variableString == REPLACE_NO_VALUE:
variableString = '[NO_VARIABLE]'
else:
try:
number = int(valueString)
if number < 0:
parameterID = number * -1
# Find the matching parameter
for targetParam in replaceParamsFile.targetParameters:
if targetParam.id == parameterID:
variableString = targetParam.targetVariable
break
except:
pass
return variableString |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:_createGsshaPyObjects; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, identifier:self; 5, identifier:mapTables; 6, identifier:indexMaps; 7, identifier:replaceParamFile; 8, identifier:directory; 9, identifier:session; 10, identifier:spatial; 11, identifier:spatialReferenceID; 12, block; 12, 13; 12, 15; 13, expression_statement; 13, 14; 14, comment; 15, for_statement; 15, 16; 15, 17; 15, 18; 15, 19; 16, identifier:mt; 17, identifier:mapTables; 18, comment; 19, block; 19, 20; 20, try_statement; 20, 21; 20, 22; 20, 23; 20, 24; 20, 315; 21, comment; 22, comment; 23, comment; 24, block; 24, 25; 24, 38; 24, 93; 24, 94; 24, 100; 24, 101; 24, 102; 24, 103; 24, 118; 24, 119; 25, if_statement; 25, 26; 25, 31; 26, comparison_operator:is; 26, 27; 26, 30; 27, subscript; 27, 28; 27, 29; 28, identifier:mt; 29, string:'indexMapName'; 30, None; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, subscript; 33, 34; 33, 35; 34, identifier:indexMaps; 35, subscript; 35, 36; 35, 37; 36, identifier:mt; 37, string:'indexMapName'; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:mapTable; 41, call; 41, 42; 41, 43; 42, identifier:MapTable; 43, argument_list; 43, 44; 43, 49; 43, 56; 43, 63; 43, 73; 43, 83; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:name; 46, subscript; 46, 47; 46, 48; 47, identifier:mt; 48, string:'name'; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:numIDs; 51, subscript; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:mt; 54, string:'numVars'; 55, string:'NUM_IDS'; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:maxNumCells; 58, subscript; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:mt; 61, string:'numVars'; 62, string:'MAX_NUMBER_CELLS'; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:numSed; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:mt; 69, string:'numVars'; 70, identifier:get; 71, argument_list; 71, 72; 72, string:'NUM_SED'; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:numContam; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:mt; 79, string:'numVars'; 80, identifier:get; 81, argument_list; 81, 82; 82, string:'NUM_CONTAM'; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:maxSoilID; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:mt; 89, string:'numVars'; 90, identifier:get; 91, argument_list; 91, 92; 92, string:'MAX_SOIL_ID'; 93, comment; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:mapTable; 98, identifier:mapTableFile; 99, identifier:self; 100, comment; 101, comment; 102, comment; 103, if_statement; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:mt; 106, string:'indexMapName'; 107, block; 107, 108; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:mapTable; 112, identifier:indexMap; 113, subscript; 113, 114; 113, 115; 114, identifier:indexMaps; 115, subscript; 115, 116; 115, 117; 116, identifier:mt; 117, string:'indexMapName'; 118, comment; 119, if_statement; 119, 120; 119, 125; 119, 231; 119, 232; 119, 287; 119, 288; 120, comparison_operator:==; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:mt; 123, string:'name'; 124, string:'CONTAMINANT_TRANSPORT'; 125, block; 125, 126; 126, for_statement; 126, 127; 126, 128; 126, 131; 126, 132; 127, identifier:contam; 128, subscript; 128, 129; 128, 130; 129, identifier:mt; 130, string:'contaminants'; 131, comment; 132, block; 132, 133; 132, 144; 132, 145; 132, 188; 132, 189; 132, 197; 132, 203; 132, 219; 132, 220; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:outputBaseFilename; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:_preprocessContaminantOutFilePath; 140, argument_list; 140, 141; 141, subscript; 141, 142; 141, 143; 142, identifier:contam; 143, string:'outPath'; 144, comment; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:contaminant; 148, call; 148, 149; 148, 150; 149, identifier:MTContaminant; 150, argument_list; 150, 151; 150, 156; 150, 159; 150, 170; 150, 181; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:name; 153, subscript; 153, 154; 153, 155; 154, identifier:contam; 155, string:'name'; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:outputFilename; 158, identifier:outputBaseFilename; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:precipConc; 161, call; 161, 162; 161, 163; 162, identifier:vrp; 163, argument_list; 163, 164; 163, 169; 164, subscript; 164, 165; 164, 168; 165, subscript; 165, 166; 165, 167; 166, identifier:contam; 167, string:'contamVars'; 168, string:'PRECIP_CONC'; 169, identifier:replaceParamFile; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:partition; 172, call; 172, 173; 172, 174; 173, identifier:vrp; 174, argument_list; 174, 175; 174, 180; 175, subscript; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:contam; 178, string:'contamVars'; 179, string:'PARTITION'; 180, identifier:replaceParamFile; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:numIDs; 183, subscript; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:contam; 186, string:'contamVars'; 187, string:'NUM_IDS'; 188, comment; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:indexMap; 192, subscript; 192, 193; 192, 194; 193, identifier:indexMaps; 194, subscript; 194, 195; 194, 196; 195, identifier:contam; 196, string:'indexMapName'; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:contaminant; 201, identifier:indexMap; 202, identifier:indexMap; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:_createValueObjects; 208, argument_list; 208, 209; 208, 212; 208, 215; 208, 216; 208, 217; 208, 218; 209, subscript; 209, 210; 209, 211; 210, identifier:contam; 211, string:'valueList'; 212, subscript; 212, 213; 212, 214; 213, identifier:contam; 214, string:'varList'; 215, identifier:mapTable; 216, identifier:indexMap; 217, identifier:contaminant; 218, identifier:replaceParamFile; 219, comment; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:self; 224, identifier:_readContaminantOutputFiles; 225, argument_list; 225, 226; 225, 227; 225, 228; 225, 229; 225, 230; 226, identifier:directory; 227, identifier:outputBaseFilename; 228, identifier:session; 229, identifier:spatial; 230, identifier:spatialReferenceID; 231, comment; 232, elif_clause; 232, 233; 232, 238; 233, comparison_operator:==; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:mt; 236, string:'name'; 237, string:'SEDIMENTS'; 238, block; 238, 239; 239, for_statement; 239, 240; 239, 241; 239, 244; 239, 245; 240, identifier:line; 241, subscript; 241, 242; 241, 243; 242, identifier:mt; 243, string:'valueList'; 244, comment; 245, block; 245, 246; 245, 280; 245, 281; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:sediment; 249, call; 249, 250; 249, 251; 250, identifier:MTSediment; 251, argument_list; 251, 252; 251, 257; 251, 266; 251, 275; 252, keyword_argument; 252, 253; 252, 254; 253, identifier:description; 254, subscript; 254, 255; 254, 256; 255, identifier:line; 256, integer:0; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:specificGravity; 259, call; 259, 260; 259, 261; 260, identifier:vrp; 261, argument_list; 261, 262; 261, 265; 262, subscript; 262, 263; 262, 264; 263, identifier:line; 264, integer:1; 265, identifier:replaceParamFile; 266, keyword_argument; 266, 267; 266, 268; 267, identifier:particleDiameter; 268, call; 268, 269; 268, 270; 269, identifier:vrp; 270, argument_list; 270, 271; 270, 274; 271, subscript; 271, 272; 271, 273; 272, identifier:line; 273, integer:2; 274, identifier:replaceParamFile; 275, keyword_argument; 275, 276; 275, 277; 276, identifier:outputFilename; 277, subscript; 277, 278; 277, 279; 278, identifier:line; 279, integer:3; 280, comment; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:sediment; 285, identifier:mapTable; 286, identifier:mapTable; 287, comment; 288, else_clause; 288, 289; 289, block; 289, 290; 289, 298; 289, 299; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:indexMap; 293, subscript; 293, 294; 293, 295; 294, identifier:indexMaps; 295, subscript; 295, 296; 295, 297; 296, identifier:mt; 297, string:'indexMapName'; 298, comment; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:self; 303, identifier:_createValueObjects; 304, argument_list; 304, 305; 304, 308; 304, 311; 304, 312; 304, 313; 304, 314; 305, subscript; 305, 306; 305, 307; 306, identifier:mt; 307, string:'valueList'; 308, subscript; 308, 309; 308, 310; 309, identifier:mt; 310, string:'varList'; 311, identifier:mapTable; 312, identifier:indexMap; 313, None; 314, identifier:replaceParamFile; 315, except_clause; 315, 316; 315, 317; 316, identifier:KeyError; 317, block; 317, 318; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:log; 322, identifier:info; 323, argument_list; 323, 324; 324, binary_operator:%; 324, 325; 324, 329; 325, parenthesized_expression; 325, 326; 326, concatenated_string; 326, 327; 326, 328; 327, string:'Index Map "%s" for Mapping Table "%s" not found in list of index maps in the mapping '; 328, string:'table file. The Mapping Table was not read into the database.'; 329, tuple; 329, 330; 329, 333; 330, subscript; 330, 331; 330, 332; 331, identifier:mt; 332, string:'indexMapName'; 333, subscript; 333, 334; 333, 335; 334, identifier:mt; 335, string:'name' | def _createGsshaPyObjects(self, mapTables, indexMaps, replaceParamFile, directory, session, spatial, spatialReferenceID):
"""
Create GSSHAPY Mapping Table ORM Objects Method
"""
for mt in mapTables:
# Create GSSHAPY MapTable object
try:
# Make sure the index map name listed with the map table is in the list of
# index maps read from the top of the mapping table file (Note that the index maps for the sediment
# and contaminant tables will have names of None, so we skip these cases.
if mt['indexMapName'] is not None:
indexMaps[mt['indexMapName']]
mapTable = MapTable(name=mt['name'],
numIDs=mt['numVars']['NUM_IDS'],
maxNumCells=mt['numVars']['MAX_NUMBER_CELLS'],
numSed=mt['numVars'].get('NUM_SED'),
numContam=mt['numVars'].get('NUM_CONTAM'),
maxSoilID=mt['numVars'].get('MAX_SOIL_ID'))
# Associate MapTable with this MapTableFile and IndexMaps
mapTable.mapTableFile = self
## NOTE: Index maps are associated wth contaminants for CONTAMINANT_TRANSPORT map
## tables. The SEDIMENTS map table are associated with index maps via the
## SOIL_EROSION_PROPS map table.
if mt['indexMapName']:
mapTable.indexMap = indexMaps[mt['indexMapName']]
# CONTAMINANT_TRANSPORT map table handler
if mt['name'] == 'CONTAMINANT_TRANSPORT':
for contam in mt['contaminants']:
# Preprocess the contaminant output paths to be relative
outputBaseFilename = self._preprocessContaminantOutFilePath(contam['outPath'])
# Initialize GSSHAPY MTContaminant object
contaminant = MTContaminant(name=contam['name'],
outputFilename=outputBaseFilename,
precipConc=vrp(contam['contamVars']['PRECIP_CONC'], replaceParamFile),
partition=vrp(contam['contamVars']['PARTITION'], replaceParamFile),
numIDs=contam['contamVars']['NUM_IDS'])
# Associate MTContaminant with appropriate IndexMap
indexMap = indexMaps[contam['indexMapName']]
contaminant.indexMap = indexMap
self._createValueObjects(contam['valueList'], contam['varList'], mapTable, indexMap,
contaminant, replaceParamFile)
# Read any output files if they are present
self._readContaminantOutputFiles(directory, outputBaseFilename, session, spatial, spatialReferenceID)
# SEDIMENTS map table handler
elif mt['name'] == 'SEDIMENTS':
for line in mt['valueList']:
# Create GSSHAPY MTSediment object
sediment = MTSediment(description=line[0],
specificGravity=vrp(line[1], replaceParamFile),
particleDiameter=vrp(line[2], replaceParamFile),
outputFilename=line[3])
# Associate the MTSediment with the MapTable
sediment.mapTable = mapTable
# All other map table handler
else:
indexMap = indexMaps[mt['indexMapName']]
# Create MTValue and MTIndex objects
self._createValueObjects(mt['valueList'], mt['varList'], mapTable, indexMap, None, replaceParamFile)
except KeyError:
log.info(('Index Map "%s" for Mapping Table "%s" not found in list of index maps in the mapping '
'table file. The Mapping Table was not read into the database.') % (
mt['indexMapName'], mt['name'])) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_valuePivot; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:session; 6, identifier:mapTable; 7, identifier:contaminant; 8, identifier:replaceParaFile; 9, block; 9, 10; 9, 12; 9, 13; 9, 63; 9, 64; 9, 69; 9, 85; 9, 86; 9, 87; 9, 88; 9, 89; 9, 93; 9, 97; 9, 366; 9, 367; 9, 368; 9, 369; 9, 370; 9, 374; 9, 375; 9, 431; 9, 432; 9, 448; 9, 449; 9, 457; 9, 458; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:indexes; 16, call; 16, 17; 16, 62; 17, attribute; 17, 18; 17, 60; 17, 61; 18, call; 18, 19; 18, 56; 19, attribute; 19, 20; 19, 54; 19, 55; 20, call; 20, 21; 20, 48; 21, attribute; 21, 22; 21, 46; 21, 47; 22, call; 22, 23; 22, 40; 23, attribute; 23, 24; 23, 38; 23, 39; 24, call; 24, 25; 24, 34; 25, attribute; 25, 26; 25, 32; 25, 33; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:session; 29, identifier:query; 30, argument_list; 30, 31; 31, identifier:MTIndex; 32, line_continuation:\; 33, identifier:join; 34, argument_list; 34, 35; 35, attribute; 35, 36; 35, 37; 36, identifier:MTValue; 37, identifier:index; 38, line_continuation:\; 39, identifier:filter; 40, argument_list; 40, 41; 41, comparison_operator:==; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:MTValue; 44, identifier:mapTable; 45, identifier:mapTable; 46, line_continuation:\; 47, identifier:filter; 48, argument_list; 48, 49; 49, comparison_operator:==; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:MTValue; 52, identifier:contaminant; 53, identifier:contaminant; 54, line_continuation:\; 55, identifier:order_by; 56, argument_list; 56, 57; 57, attribute; 57, 58; 57, 59; 58, identifier:MTIndex; 59, identifier:index; 60, line_continuation:\; 61, identifier:all; 62, argument_list; 63, comment; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:layer_indices; 67, list:[0]; 67, 68; 68, integer:0; 69, if_statement; 69, 70; 69, 77; 70, comparison_operator:in; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:mapTable; 73, identifier:name; 74, tuple; 74, 75; 74, 76; 75, string:'MULTI_LAYER_SOIL'; 76, string:'RICHARDS_EQN_INFILTRATION_BROOKS'; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:layer_indices; 81, call; 81, 82; 81, 83; 82, identifier:range; 83, argument_list; 83, 84; 84, integer:3; 85, comment; 86, comment; 87, comment; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:lines; 92, list:[]; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:values; 96, dictionary; 97, for_statement; 97, 98; 97, 99; 97, 100; 98, identifier:idx; 99, identifier:indexes; 100, block; 100, 101; 101, for_statement; 101, 102; 101, 103; 101, 104; 101, 105; 102, identifier:layer_index; 103, identifier:layer_indices; 104, comment; 105, block; 105, 106; 105, 168; 105, 169; 105, 170; 105, 171; 105, 172; 105, 173; 105, 174; 105, 175; 105, 179; 105, 180; 105, 231; 105, 232; 105, 250; 105, 265; 105, 280; 105, 281; 105, 358; 105, 359; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:values; 109, call; 109, 110; 109, 167; 110, attribute; 110, 111; 110, 165; 110, 166; 111, call; 111, 112; 111, 161; 112, attribute; 112, 113; 112, 159; 112, 160; 113, call; 113, 114; 113, 153; 114, attribute; 114, 115; 114, 151; 114, 152; 115, call; 115, 116; 115, 145; 116, attribute; 116, 117; 116, 143; 116, 144; 117, call; 117, 118; 117, 137; 118, attribute; 118, 119; 118, 135; 118, 136; 119, call; 119, 120; 119, 129; 120, attribute; 120, 121; 120, 127; 120, 128; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:session; 124, identifier:query; 125, argument_list; 125, 126; 126, identifier:MTValue; 127, line_continuation:\; 128, identifier:filter; 129, argument_list; 129, 130; 130, comparison_operator:==; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:MTValue; 133, identifier:mapTable; 134, identifier:mapTable; 135, line_continuation:\; 136, identifier:filter; 137, argument_list; 137, 138; 138, comparison_operator:==; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:MTValue; 141, identifier:contaminant; 142, identifier:contaminant; 143, line_continuation:\; 144, identifier:filter; 145, argument_list; 145, 146; 146, comparison_operator:==; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:MTValue; 149, identifier:index; 150, identifier:idx; 151, line_continuation:\; 152, identifier:filter; 153, argument_list; 153, 154; 154, comparison_operator:==; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:MTValue; 157, identifier:layer_id; 158, identifier:layer_index; 159, line_continuation:\; 160, identifier:order_by; 161, argument_list; 161, 162; 162, attribute; 162, 163; 162, 164; 163, identifier:MTValue; 164, identifier:id; 165, line_continuation:\; 166, identifier:all; 167, argument_list; 168, comment; 169, comment; 170, comment; 171, comment; 172, comment; 173, comment; 174, comment; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:valString; 178, string:''; 179, comment; 180, for_statement; 180, 181; 180, 182; 180, 183; 181, identifier:val; 182, identifier:values; 183, block; 183, 184; 183, 193; 183, 194; 183, 204; 183, 220; 184, if_statement; 184, 185; 184, 191; 185, comparison_operator:<=; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:val; 188, identifier:value; 189, unary_operator:-; 189, 190; 190, integer:9999; 191, block; 191, 192; 192, continue_statement; 193, comment; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:processedValue; 197, call; 197, 198; 197, 199; 198, identifier:vwp; 199, argument_list; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:val; 202, identifier:value; 203, identifier:replaceParaFile; 204, try_statement; 204, 205; 204, 212; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:numString; 209, binary_operator:%; 209, 210; 209, 211; 210, string:'%.6f'; 211, identifier:processedValue; 212, except_clause; 212, 213; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:numString; 217, binary_operator:%; 217, 218; 217, 219; 218, string:'%s'; 219, identifier:processedValue; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:valString; 223, binary_operator:%; 223, 224; 223, 225; 224, string:'%s%s%s'; 225, tuple; 225, 226; 225, 227; 225, 228; 226, identifier:valString; 227, identifier:numString; 228, binary_operator:*; 228, 229; 228, 230; 229, string:' '; 230, integer:3; 231, comment; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:spacing1; 235, call; 235, 236; 235, 237; 236, identifier:max; 237, argument_list; 237, 238; 237, 239; 238, integer:1; 239, binary_operator:-; 239, 240; 239, 241; 240, integer:6; 241, call; 241, 242; 241, 243; 242, identifier:len; 243, argument_list; 243, 244; 244, call; 244, 245; 244, 246; 245, identifier:str; 246, argument_list; 246, 247; 247, attribute; 247, 248; 247, 249; 248, identifier:idx; 249, identifier:index; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:spacing2; 253, call; 253, 254; 253, 255; 254, identifier:max; 255, argument_list; 255, 256; 255, 257; 256, integer:1; 257, binary_operator:-; 257, 258; 257, 259; 258, integer:40; 259, call; 259, 260; 259, 261; 260, identifier:len; 261, argument_list; 261, 262; 262, attribute; 262, 263; 262, 264; 263, identifier:idx; 264, identifier:description1; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:spacing3; 268, call; 268, 269; 268, 270; 269, identifier:max; 270, argument_list; 270, 271; 270, 272; 271, integer:1; 272, binary_operator:-; 272, 273; 272, 274; 273, integer:40; 274, call; 274, 275; 274, 276; 275, identifier:len; 276, argument_list; 276, 277; 277, attribute; 277, 278; 277, 279; 278, identifier:idx; 279, identifier:description2; 280, comment; 281, if_statement; 281, 282; 281, 285; 281, 311; 282, comparison_operator:==; 282, 283; 282, 284; 283, identifier:layer_index; 284, integer:0; 285, block; 285, 286; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:line; 289, binary_operator:%; 289, 290; 289, 291; 290, string:'%s%s%s%s%s%s%s\n'; 291, tuple; 291, 292; 291, 295; 291, 298; 291, 301; 291, 304; 291, 307; 291, 310; 292, attribute; 292, 293; 292, 294; 293, identifier:idx; 294, identifier:index; 295, binary_operator:*; 295, 296; 295, 297; 296, string:' '; 297, identifier:spacing1; 298, attribute; 298, 299; 298, 300; 299, identifier:idx; 300, identifier:description1; 301, binary_operator:*; 301, 302; 301, 303; 302, string:' '; 303, identifier:spacing2; 304, attribute; 304, 305; 304, 306; 305, identifier:idx; 306, identifier:description2; 307, binary_operator:*; 307, 308; 307, 309; 308, string:' '; 309, identifier:spacing3; 310, identifier:valString; 311, else_clause; 311, 312; 312, block; 312, 313; 312, 346; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:num_prepend_spaces; 316, binary_operator:+; 316, 317; 316, 345; 317, binary_operator:+; 317, 318; 317, 339; 318, binary_operator:+; 318, 319; 318, 337; 318, 338; 319, binary_operator:+; 319, 320; 319, 331; 320, binary_operator:+; 320, 321; 320, 330; 321, call; 321, 322; 321, 323; 322, identifier:len; 323, argument_list; 323, 324; 324, call; 324, 325; 324, 326; 325, identifier:str; 326, argument_list; 326, 327; 327, attribute; 327, 328; 327, 329; 328, identifier:idx; 329, identifier:index; 330, identifier:spacing1; 331, call; 331, 332; 331, 333; 332, identifier:len; 333, argument_list; 333, 334; 334, attribute; 334, 335; 334, 336; 335, identifier:idx; 336, identifier:description1; 337, line_continuation:\; 338, identifier:spacing2; 339, call; 339, 340; 339, 341; 340, identifier:len; 341, argument_list; 341, 342; 342, attribute; 342, 343; 342, 344; 343, identifier:idx; 344, identifier:description2; 345, identifier:spacing3; 346, expression_statement; 346, 347; 347, assignment; 347, 348; 347, 349; 348, identifier:line; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, string:'{0}{1}\n'; 352, identifier:format; 353, argument_list; 353, 354; 353, 357; 354, binary_operator:*; 354, 355; 354, 356; 355, string:' '; 356, identifier:num_prepend_spaces; 357, identifier:valString; 358, comment; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:lines; 363, identifier:append; 364, argument_list; 364, 365; 365, identifier:line; 366, comment; 367, comment; 368, comment; 369, comment; 370, expression_statement; 370, 371; 371, assignment; 371, 372; 371, 373; 372, identifier:varString; 373, string:''; 374, comment; 375, for_statement; 375, 376; 375, 379; 375, 383; 376, pattern_list; 376, 377; 376, 378; 377, identifier:idx; 378, identifier:val; 379, call; 379, 380; 379, 381; 380, identifier:enumerate; 381, argument_list; 381, 382; 382, identifier:values; 383, block; 383, 384; 384, if_statement; 384, 385; 384, 390; 384, 391; 384, 416; 385, comparison_operator:==; 385, 386; 385, 389; 386, attribute; 386, 387; 386, 388; 387, identifier:val; 388, identifier:variable; 389, string:'XSEDIMENT'; 390, comment; 391, block; 391, 392; 392, if_statement; 392, 393; 392, 401; 393, comparison_operator:>=; 393, 394; 393, 395; 394, identifier:idx; 395, binary_operator:-; 395, 396; 395, 400; 396, call; 396, 397; 396, 398; 397, identifier:len; 398, argument_list; 398, 399; 399, identifier:values; 400, integer:1; 401, block; 401, 402; 402, expression_statement; 402, 403; 403, assignment; 403, 404; 403, 405; 404, identifier:varString; 405, binary_operator:%; 405, 406; 405, 407; 406, string:'%s%s%s%s'; 407, tuple; 407, 408; 407, 409; 407, 412; 407, 413; 408, identifier:varString; 409, attribute; 409, 410; 409, 411; 410, identifier:mapTable; 411, identifier:numSed; 412, string:' SEDIMENTS....'; 413, binary_operator:*; 413, 414; 413, 415; 414, string:' '; 415, integer:2; 416, else_clause; 416, 417; 417, block; 417, 418; 418, expression_statement; 418, 419; 419, assignment; 419, 420; 419, 421; 420, identifier:varString; 421, binary_operator:%; 421, 422; 421, 423; 422, string:'%s%s%s'; 423, tuple; 423, 424; 423, 425; 423, 428; 424, identifier:varString; 425, attribute; 425, 426; 425, 427; 426, identifier:val; 427, identifier:variable; 428, binary_operator:*; 428, 429; 428, 430; 429, string:' '; 430, integer:2; 431, comment; 432, expression_statement; 432, 433; 433, assignment; 433, 434; 433, 435; 434, identifier:header; 435, binary_operator:%; 435, 436; 435, 437; 436, string:'ID%sDESCRIPTION1%sDESCRIPTION2%s%s\n'; 437, tuple; 437, 438; 437, 441; 437, 444; 437, 447; 438, binary_operator:*; 438, 439; 438, 440; 439, string:' '; 440, integer:4; 441, binary_operator:*; 441, 442; 441, 443; 442, string:' '; 443, integer:28; 444, binary_operator:*; 444, 445; 444, 446; 445, string:' '; 446, integer:28; 447, identifier:varString; 448, comment; 449, expression_statement; 449, 450; 450, call; 450, 451; 450, 454; 451, attribute; 451, 452; 451, 453; 452, identifier:lines; 453, identifier:insert; 454, argument_list; 454, 455; 454, 456; 455, integer:0; 456, identifier:header; 457, comment; 458, return_statement; 458, 459; 459, identifier:lines | def _valuePivot(self, session, mapTable, contaminant, replaceParaFile):
"""
This function retrieves the values of a mapping table from the database and pivots them into the format that is
required by the mapping table file. This function returns a list of strings that can be printed to the file
directly.
"""
# Retrieve the indices for the current mapping table and mapping table file
indexes = session.query(MTIndex). \
join(MTValue.index). \
filter(MTValue.mapTable == mapTable). \
filter(MTValue.contaminant == contaminant). \
order_by(MTIndex.index). \
all()
# determine number of layers
layer_indices = [0]
if mapTable.name in ('MULTI_LAYER_SOIL', 'RICHARDS_EQN_INFILTRATION_BROOKS'):
layer_indices = range(3)
# ----------------------------------------
# Construct each line in the mapping table
#-----------------------------------------
# All lines will be compiled into this list
lines = []
values = {}
for idx in indexes:
for layer_index in layer_indices:
# Retrieve values for the current index
values = session.query(MTValue). \
filter(MTValue.mapTable == mapTable). \
filter(MTValue.contaminant == contaminant). \
filter(MTValue.index == idx). \
filter(MTValue.layer_id == layer_index). \
order_by(MTValue.id). \
all()
# NOTE: The second order_by modifier in the query above handles the special ordering of XSEDIMENT columns
# in soil erosion properties table (i.e. these columns must be in the same order as the sediments in the
# sediments table. Accomplished by using the sedimentID field). Similarly, the contaminant filter is only
# used in the case of the contaminant transport table. Values that don't belong to a contaminant will have
# a contaminant attribute equal to None. Compare usage of this function by _writeMapTable and
# _writeContaminant.
#Value string
valString = ''
# Define valString
for val in values:
if val.value <= -9999:
continue
# Format value with trailing zeros up to 6 digits
processedValue = vwp(val.value, replaceParaFile)
try:
numString = '%.6f' % processedValue
except:
numString = '%s' % processedValue
valString = '%s%s%s' % (valString, numString, ' ' * 3)
# Determine spacing for aesthetics (so each column lines up)
spacing1 = max(1, 6 - len(str(idx.index)))
spacing2 = max(1, 40 - len(idx.description1))
spacing3 = max(1, 40 - len(idx.description2))
# Compile each mapping table line
if layer_index == 0:
line = '%s%s%s%s%s%s%s\n' % (
idx.index, ' ' * spacing1, idx.description1, ' ' * spacing2, idx.description2, ' ' * spacing3, valString)
else:
num_prepend_spaces = len(str(idx.index)) + spacing1 + len(idx.description1) \
+ spacing2 + len(idx.description2) + spacing3
line = '{0}{1}\n'.format(' ' * num_prepend_spaces, valString)
# Compile each lines into a list
lines.append(line)
#-----------------------------
# Define the value header line
#-----------------------------
# Define varString for the header line
varString = ''
# Compile list of variables (from MTValue object list) into a single string of variables
for idx, val in enumerate(values):
if val.variable == 'XSEDIMENT': # Special case for XSEDIMENT variable
if idx >= len(values) - 1:
varString = '%s%s%s%s' % (varString, mapTable.numSed, ' SEDIMENTS....', ' ' * 2)
else:
varString = '%s%s%s' % (varString, val.variable, ' ' * 2)
# Compile the mapping table header
header = 'ID%sDESCRIPTION1%sDESCRIPTION2%s%s\n' % (' ' * 4, ' ' * 28, ' ' * 28, varString)
# Prepend the header line to the list of lines
lines.insert(0, header)
# Return the list of lines
return lines |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:addRoughnessMapFromLandUse; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 4, identifier:self; 5, identifier:name; 6, identifier:session; 7, identifier:land_use_grid; 8, default_parameter; 8, 9; 8, 10; 9, identifier:land_use_to_roughness_table; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:land_use_grid_id; 13, None; 14, block; 14, 15; 14, 17; 14, 30; 14, 31; 14, 140; 14, 141; 14, 152; 14, 153; 14, 175; 14, 188; 14, 189; 14, 202; 14, 203; 14, 226; 14, 227; 14, 235; 14, 236; 14, 262; 14, 263; 14, 272; 14, 278; 14, 284; 14, 285; 14, 291; 14, 292; 14, 344; 14, 345; 14, 356; 14, 372; 14, 385; 14, 386; 14, 412; 14, 413; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:LAND_USE_GRID_TABLES; 20, dictionary; 20, 21; 20, 24; 20, 27; 21, pair; 21, 22; 21, 23; 22, string:'nga'; 23, string:'land_cover_nga.txt'; 24, pair; 24, 25; 24, 26; 25, string:'glcf'; 26, string:'land_cover_glcf_modis.txt'; 27, pair; 27, 28; 27, 29; 28, string:'nlcd'; 29, string:'land_cover_nlcd.txt'; 30, comment; 31, if_statement; 31, 32; 31, 39; 31, 44; 32, call; 32, 33; 32, 34; 33, identifier:isinstance; 34, argument_list; 34, 35; 34, 36; 35, identifier:land_use_to_roughness_table; 36, attribute; 36, 37; 36, 38; 37, identifier:pd; 38, identifier:DataFrame; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:df; 43, identifier:land_use_to_roughness_table; 44, else_clause; 44, 45; 45, block; 45, 46; 45, 92; 45, 93; 45, 104; 46, if_statement; 46, 47; 46, 50; 47, comparison_operator:is; 47, 48; 47, 49; 48, identifier:land_use_to_roughness_table; 49, None; 50, block; 50, 51; 50, 61; 51, if_statement; 51, 52; 51, 55; 52, comparison_operator:is; 52, 53; 52, 54; 53, identifier:land_use_grid_id; 54, None; 55, block; 55, 56; 56, raise_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:ValueError; 59, argument_list; 59, 60; 60, string:"Must have land_use_to_roughness_table or land_use_grid_id set ..."; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:land_use_to_roughness_table; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:os; 68, identifier:path; 69, identifier:join; 70, argument_list; 70, 71; 70, 86; 70, 87; 70, 88; 70, 89; 71, call; 71, 72; 71, 77; 72, attribute; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:os; 75, identifier:path; 76, identifier:dirname; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:os; 82, identifier:path; 83, identifier:realpath; 84, argument_list; 84, 85; 85, identifier:__file__; 86, string:'..'; 87, string:'grid'; 88, string:'land_cover'; 89, subscript; 89, 90; 89, 91; 90, identifier:LAND_USE_GRID_TABLES; 91, identifier:land_use_grid_id; 92, comment; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:land_use_to_roughness_table; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:os; 100, identifier:path; 101, identifier:abspath; 102, argument_list; 102, 103; 103, identifier:land_use_to_roughness_table; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:df; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:pd; 110, identifier:read_table; 111, argument_list; 111, 112; 111, 113; 111, 116; 111, 119; 111, 122; 111, 128; 112, identifier:land_use_to_roughness_table; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:delim_whitespace; 115, True; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:header; 118, None; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:skiprows; 121, integer:1; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:names; 124, tuple; 124, 125; 124, 126; 124, 127; 125, string:'id'; 126, string:'description'; 127, string:'roughness'; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:dtype; 130, dictionary; 130, 131; 130, 134; 130, 137; 131, pair; 131, 132; 131, 133; 132, string:'id'; 133, string:'int'; 134, pair; 134, 135; 134, 136; 135, string:'description'; 136, string:'str'; 137, pair; 137, 138; 137, 139; 138, string:'roughness'; 139, string:'float'; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:land_use_grid; 144, call; 144, 145; 144, 150; 145, attribute; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:os; 148, identifier:path; 149, identifier:abspath; 150, argument_list; 150, 151; 151, identifier:land_use_grid; 152, comment; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:land_use_resampled; 156, call; 156, 157; 156, 158; 157, identifier:resample_grid; 158, argument_list; 158, 159; 158, 160; 158, 167; 158, 172; 159, identifier:land_use_grid; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:projectFile; 165, identifier:getGrid; 166, argument_list; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:resample_method; 169, attribute; 169, 170; 169, 171; 170, identifier:gdalconst; 171, identifier:GRA_NearestNeighbour; 172, keyword_argument; 172, 173; 172, 174; 173, identifier:as_gdal_grid; 174, True; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:unique_land_use_ids; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:np; 181, identifier:unique; 182, argument_list; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:land_use_resampled; 186, identifier:np_array; 187, argument_list; 188, comment; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:df; 192, subscript; 192, 193; 192, 194; 193, identifier:df; 194, call; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:df; 198, identifier:id; 199, identifier:isin; 200, argument_list; 200, 201; 201, identifier:unique_land_use_ids; 202, comment; 203, for_statement; 203, 204; 203, 205; 203, 206; 204, identifier:land_use_id; 205, identifier:unique_land_use_ids; 206, block; 206, 207; 207, if_statement; 207, 208; 207, 215; 208, comparison_operator:not; 208, 209; 208, 210; 209, identifier:land_use_id; 210, attribute; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:df; 213, identifier:id; 214, identifier:values; 215, block; 215, 216; 216, raise_statement; 216, 217; 217, call; 217, 218; 217, 219; 218, identifier:IndexError; 219, argument_list; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, string:"Land use ID {0} not found in table."; 223, identifier:format; 224, argument_list; 224, 225; 225, identifier:land_use_id; 226, comment; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:deleteMapTable; 232, argument_list; 232, 233; 232, 234; 233, string:"ROUGHNESS"; 234, identifier:session; 235, comment; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:mapTable; 239, call; 239, 240; 239, 241; 240, identifier:MapTable; 241, argument_list; 241, 242; 241, 245; 241, 253; 241, 256; 241, 259; 242, keyword_argument; 242, 243; 242, 244; 243, identifier:name; 244, string:"ROUGHNESS"; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:numIDs; 247, call; 247, 248; 247, 249; 248, identifier:len; 249, argument_list; 249, 250; 250, attribute; 250, 251; 250, 252; 251, identifier:df; 252, identifier:index; 253, keyword_argument; 253, 254; 253, 255; 254, identifier:maxNumCells; 255, integer:0; 256, keyword_argument; 256, 257; 256, 258; 257, identifier:numSed; 258, integer:0; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:numContam; 261, integer:0; 262, comment; 263, expression_statement; 263, 264; 264, assignment; 264, 265; 264, 266; 265, identifier:indexMap; 266, call; 266, 267; 266, 268; 267, identifier:IndexMap; 268, argument_list; 268, 269; 269, keyword_argument; 269, 270; 269, 271; 270, identifier:name; 271, identifier:name; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:indexMap; 276, identifier:mapTableFile; 277, identifier:self; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:mapTable; 282, identifier:indexMap; 283, identifier:indexMap; 284, comment; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:mapTable; 289, identifier:mapTableFile; 290, identifier:self; 291, comment; 292, for_statement; 292, 293; 292, 294; 292, 299; 293, identifier:row; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:df; 297, identifier:itertuples; 298, argument_list; 299, block; 299, 300; 299, 316; 299, 322; 299, 332; 299, 338; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:idx; 303, call; 303, 304; 303, 305; 304, identifier:MTIndex; 305, argument_list; 305, 306; 305, 312; 305, 315; 306, call; 306, 307; 306, 308; 307, identifier:str; 308, argument_list; 308, 309; 309, attribute; 309, 310; 309, 311; 310, identifier:row; 311, identifier:id; 312, attribute; 312, 313; 312, 314; 313, identifier:row; 314, identifier:description; 315, string:''; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:idx; 320, identifier:indexMap; 321, identifier:indexMap; 322, expression_statement; 322, 323; 323, assignment; 323, 324; 323, 325; 324, identifier:val; 325, call; 325, 326; 325, 327; 326, identifier:MTValue; 327, argument_list; 327, 328; 327, 329; 328, string:'ROUGH'; 329, attribute; 329, 330; 329, 331; 330, identifier:row; 331, identifier:roughness; 332, expression_statement; 332, 333; 333, assignment; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:val; 336, identifier:index; 337, identifier:idx; 338, expression_statement; 338, 339; 339, assignment; 339, 340; 339, 343; 340, attribute; 340, 341; 340, 342; 341, identifier:val; 342, identifier:mapTable; 343, identifier:mapTable; 344, comment; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:manningn_card; 348, call; 348, 349; 348, 354; 349, attribute; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:self; 352, identifier:projectFile; 353, identifier:getCard; 354, argument_list; 354, 355; 355, string:'MANNING_N'; 356, if_statement; 356, 357; 356, 358; 357, identifier:manningn_card; 358, block; 358, 359; 358, 366; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:session; 363, identifier:delete; 364, argument_list; 364, 365; 365, identifier:manningn_card; 366, expression_statement; 366, 367; 367, call; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:session; 370, identifier:commit; 371, argument_list; 372, expression_statement; 372, 373; 373, assignment; 373, 374; 373, 379; 374, attribute; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:mapTable; 377, identifier:indexMap; 378, identifier:filename; 379, call; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, string:'{0}.idx'; 382, identifier:format; 383, argument_list; 383, 384; 384, identifier:name; 385, comment; 386, with_statement; 386, 387; 386, 397; 387, with_clause; 387, 388; 388, with_item; 388, 389; 389, call; 389, 390; 389, 391; 390, identifier:tmp_chdir; 391, argument_list; 391, 392; 392, attribute; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:self; 395, identifier:projectFile; 396, identifier:project_directory; 397, block; 397, 398; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:land_use_resampled; 402, identifier:to_grass_ascii; 403, argument_list; 403, 404; 403, 409; 404, attribute; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, identifier:mapTable; 407, identifier:indexMap; 408, identifier:filename; 409, keyword_argument; 409, 410; 409, 411; 410, identifier:print_nodata; 411, False; 412, comment; 413, if_statement; 413, 414; 413, 423; 414, not_operator; 414, 415; 415, call; 415, 416; 415, 421; 416, attribute; 416, 417; 416, 420; 417, attribute; 417, 418; 417, 419; 418, identifier:self; 419, identifier:projectFile; 420, identifier:getCard; 421, argument_list; 421, 422; 422, string:'MAPPING_TABLE'; 423, block; 423, 424; 424, expression_statement; 424, 425; 425, call; 425, 426; 425, 431; 426, attribute; 426, 427; 426, 430; 427, attribute; 427, 428; 427, 429; 428, identifier:self; 429, identifier:projectFile; 430, identifier:setCard; 431, argument_list; 431, 432; 431, 433; 431, 443; 432, string:'MAPPING_TABLE'; 433, call; 433, 434; 433, 437; 434, attribute; 434, 435; 434, 436; 435, string:'{0}.cmt'; 436, identifier:format; 437, argument_list; 437, 438; 438, attribute; 438, 439; 438, 442; 439, attribute; 439, 440; 439, 441; 440, identifier:self; 441, identifier:projectFile; 442, identifier:name; 443, keyword_argument; 443, 444; 443, 445; 444, identifier:add_quotes; 445, True | def addRoughnessMapFromLandUse(self, name,
session,
land_use_grid,
land_use_to_roughness_table=None,
land_use_grid_id=None,
):
"""
Adds a roughness map from land use file
Example::
from gsshapy.orm import ProjectFile
from gsshapy.lib import db_tools as dbt
from os import path, chdir
gssha_directory = '/gsshapy/tests/grid_standard/gssha_project'
land_use_grid = 'LC_5min_global_2012.tif'
land_use_to_roughness_table = ''/gsshapy/gridtogssha/land_cover/land_cover_glcf_modis.txt'
# Create Test DB
sqlalchemy_url, sql_engine = dbt.init_sqlite_memory()
# Create DB Sessions
db_session = dbt.create_session(sqlalchemy_url, sql_engine)
# Instantiate GSSHAPY object for reading to database
project_manager = ProjectFile()
# Call read method
project_manager.readInput(directory=gssha_directory,
projectFileName='grid_standard.prj',
session=db_session)
project_manager.mapTableFile.addRoughnessMapFromLandUse("roughness",
db_session,
land_use_to_roughness_table,
land_use_grid,
)
# WRITE OUT UPDATED GSSHA PROJECT FILE
project_manager.writeInput(session=db_session,
directory=gssha_directory,
name='grid_standard')
"""
LAND_USE_GRID_TABLES = {
'nga' : 'land_cover_nga.txt',
'glcf' : 'land_cover_glcf_modis.txt',
'nlcd' : 'land_cover_nlcd.txt',
}
# read in table
if isinstance(land_use_to_roughness_table, pd.DataFrame):
df = land_use_to_roughness_table
else:
if land_use_to_roughness_table is None:
if land_use_grid_id is None:
raise ValueError("Must have land_use_to_roughness_table or land_use_grid_id set ...")
land_use_to_roughness_table = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'..', 'grid',
'land_cover',
LAND_USE_GRID_TABLES[land_use_grid_id])
# make sure paths are absolute as the working directory changes
land_use_to_roughness_table = os.path.abspath(land_use_to_roughness_table)
df = pd.read_table(land_use_to_roughness_table, delim_whitespace=True,
header=None, skiprows=1,
names=('id', 'description', 'roughness'),
dtype={'id':'int', 'description':'str', 'roughness':'float'},
)
# make sure paths are absolute as the working directory changes
land_use_grid = os.path.abspath(land_use_grid)
# resample land use grid to gssha grid
land_use_resampled = resample_grid(land_use_grid,
self.projectFile.getGrid(),
resample_method=gdalconst.GRA_NearestNeighbour,
as_gdal_grid=True)
unique_land_use_ids = np.unique(land_use_resampled.np_array())
# only add ids in index map subset
df = df[df.id.isin(unique_land_use_ids)]
# make sure all needed land use IDs exist
for land_use_id in unique_land_use_ids:
if land_use_id not in df.id.values:
raise IndexError("Land use ID {0} not found in table.".format(land_use_id))
# delete duplicate/old tables with same name if they exist
self.deleteMapTable("ROUGHNESS", session)
# get num ids
mapTable = MapTable(name="ROUGHNESS",
numIDs=len(df.index),
maxNumCells=0,
numSed=0,
numContam=0)
# Create GSSHAPY IndexMap object from result object
indexMap = IndexMap(name=name)
indexMap.mapTableFile = self
mapTable.indexMap = indexMap
# Associate MapTable with this MapTableFile and IndexMaps
mapTable.mapTableFile = self
# add values to table
for row in df.itertuples():
idx = MTIndex(str(row.id), row.description, '')
idx.indexMap = indexMap
val = MTValue('ROUGH', row.roughness)
val.index = idx
val.mapTable = mapTable
# remove MANNING_N card becasue it is mutually exclusive
manningn_card = self.projectFile.getCard('MANNING_N')
if manningn_card:
session.delete(manningn_card)
session.commit()
mapTable.indexMap.filename = '{0}.idx'.format(name)
# write file
with tmp_chdir(self.projectFile.project_directory):
land_use_resampled.to_grass_ascii(mapTable.indexMap.filename,
print_nodata=False)
# update project card
if not self.projectFile.getCard('MAPPING_TABLE'):
self.projectFile.setCard('MAPPING_TABLE',
'{0}.cmt'.format(self.projectFile.name),
add_quotes=True) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:result; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:timeout; 7, None; 8, block; 8, 9; 8, 11; 8, 21; 8, 31; 8, 104; 8, 186; 8, 198; 8, 264; 8, 274; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 17; 13, pattern_list; 13, 14; 13, 15; 13, 16; 14, identifier:it; 15, identifier:exceptions; 16, identifier:future_lists; 17, expression_list; 17, 18; 17, 19; 17, 20; 18, list:[]; 19, list:[]; 20, list:[]; 21, import_from_statement; 21, 22; 21, 25; 21, 27; 22, dotted_name; 22, 23; 22, 24; 23, identifier:concurrent; 24, identifier:futures; 25, dotted_name; 25, 26; 26, identifier:Future; 27, aliased_import; 27, 28; 27, 30; 28, dotted_name; 28, 29; 29, identifier:wait; 30, identifier:wait_fut; 31, function_definition; 31, 32; 31, 33; 31, 37; 32, function_name:update; 33, parameters; 33, 34; 33, 35; 33, 36; 34, identifier:fut; 35, identifier:data; 36, identifier:key; 37, block; 37, 38; 38, if_statement; 38, 39; 38, 44; 38, 55; 39, call; 39, 40; 39, 41; 40, identifier:isinstance; 41, argument_list; 41, 42; 41, 43; 42, identifier:fut; 43, identifier:Future; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:it; 49, identifier:append; 50, argument_list; 50, 51; 51, tuple; 51, 52; 51, 53; 51, 54; 52, identifier:fut; 53, identifier:data; 54, identifier:key; 55, elif_clause; 55, 56; 55, 65; 56, boolean_operator:and; 56, 57; 56, 62; 57, call; 57, 58; 57, 59; 58, identifier:isinstance; 59, argument_list; 59, 60; 59, 61; 60, identifier:fut; 61, identifier:AsyncList; 62, comparison_operator:not; 62, 63; 62, 64; 63, identifier:fut; 64, identifier:future_lists; 65, block; 65, 66; 65, 73; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:future_lists; 70, identifier:append; 71, argument_list; 71, 72; 72, identifier:fut; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:it; 77, identifier:extend; 78, argument_list; 78, 79; 79, subscript; 79, 80; 79, 99; 80, list_comprehension; 80, 81; 80, 85; 80, 93; 81, tuple; 81, 82; 81, 83; 81, 84; 82, identifier:j; 83, identifier:fut; 84, identifier:i; 85, for_in_clause; 85, 86; 85, 89; 86, pattern_list; 86, 87; 86, 88; 87, identifier:i; 88, identifier:j; 89, call; 89, 90; 89, 91; 90, identifier:enumerate; 91, argument_list; 91, 92; 92, identifier:fut; 93, if_clause; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:isinstance; 96, argument_list; 96, 97; 96, 98; 97, identifier:j; 98, identifier:Future; 99, slice; 99, 100; 99, 101; 99, 102; 100, colon; 101, colon; 102, unary_operator:-; 102, 103; 103, integer:1; 104, for_statement; 104, 105; 104, 106; 104, 113; 105, identifier:s; 106, call; 106, 107; 106, 112; 107, attribute; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:sub_sol; 111, identifier:values; 112, argument_list; 113, block; 113, 114; 113, 134; 113, 160; 114, for_statement; 114, 115; 114, 118; 114, 126; 115, pattern_list; 115, 116; 115, 117; 116, identifier:k; 117, identifier:v; 118, call; 118, 119; 118, 120; 119, identifier:list; 120, argument_list; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:s; 124, identifier:items; 125, argument_list; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:update; 130, argument_list; 130, 131; 130, 132; 130, 133; 131, identifier:v; 132, identifier:s; 133, identifier:k; 134, for_statement; 134, 135; 134, 136; 134, 145; 135, identifier:d; 136, call; 136, 137; 136, 144; 137, attribute; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:s; 141, identifier:workflow; 142, identifier:nodes; 143, identifier:values; 144, argument_list; 145, block; 145, 146; 146, if_statement; 146, 147; 146, 150; 147, comparison_operator:in; 147, 148; 147, 149; 148, string:'results'; 149, identifier:d; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:update; 154, argument_list; 154, 155; 154, 158; 154, 159; 155, subscript; 155, 156; 155, 157; 156, identifier:d; 157, string:'results'; 158, identifier:d; 159, string:'results'; 160, for_statement; 160, 161; 160, 162; 160, 171; 161, identifier:d; 162, call; 162, 163; 162, 170; 163, attribute; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:s; 167, identifier:workflow; 168, identifier:edges; 169, identifier:values; 170, argument_list; 171, block; 171, 172; 172, if_statement; 172, 173; 172, 176; 173, comparison_operator:in; 173, 174; 173, 175; 174, string:'value'; 175, identifier:d; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:update; 180, argument_list; 180, 181; 180, 184; 180, 185; 181, subscript; 181, 182; 181, 183; 182, identifier:d; 183, string:'value'; 184, identifier:d; 185, string:'value'; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 189; 188, identifier:wait_fut; 189, argument_list; 189, 190; 189, 197; 190, set_comprehension; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:v; 193, integer:0; 194, for_in_clause; 194, 195; 194, 196; 195, identifier:v; 196, identifier:it; 197, identifier:timeout; 198, for_statement; 198, 199; 198, 203; 198, 204; 199, pattern_list; 199, 200; 199, 201; 199, 202; 200, identifier:f; 201, identifier:d; 202, identifier:k; 203, identifier:it; 204, block; 204, 205; 205, try_statement; 205, 206; 205, 217; 205, 240; 206, block; 206, 207; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 212; 209, subscript; 209, 210; 209, 211; 210, identifier:d; 211, identifier:k; 212, call; 212, 213; 212, 214; 213, identifier:await_result; 214, argument_list; 214, 215; 214, 216; 215, identifier:f; 216, integer:0; 217, except_clause; 217, 218; 217, 222; 218, as_pattern; 218, 219; 218, 220; 219, identifier:SkipNode; 220, as_pattern_target; 220, 221; 221, identifier:e; 222, block; 222, 223; 222, 236; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:exceptions; 227, identifier:append; 228, argument_list; 228, 229; 229, tuple; 229, 230; 229, 231; 229, 232; 229, 233; 230, identifier:f; 231, identifier:d; 232, identifier:k; 233, attribute; 233, 234; 233, 235; 234, identifier:e; 235, identifier:ex; 236, delete_statement; 236, 237; 237, subscript; 237, 238; 237, 239; 238, identifier:d; 239, identifier:k; 240, except_clause; 240, 241; 240, 248; 241, as_pattern; 241, 242; 241, 246; 242, tuple; 242, 243; 242, 244; 242, 245; 243, identifier:Exception; 244, identifier:ExecutorShutdown; 245, identifier:DispatcherAbort; 246, as_pattern_target; 246, 247; 247, identifier:ex; 248, block; 248, 249; 248, 260; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:exceptions; 253, identifier:append; 254, argument_list; 254, 255; 255, tuple; 255, 256; 255, 257; 255, 258; 255, 259; 256, identifier:f; 257, identifier:d; 258, identifier:k; 259, identifier:ex; 260, delete_statement; 260, 261; 261, subscript; 261, 262; 261, 263; 262, identifier:d; 263, identifier:k; 264, if_statement; 264, 265; 264, 266; 265, identifier:exceptions; 266, block; 266, 267; 267, raise_statement; 267, 268; 268, subscript; 268, 269; 268, 272; 269, subscript; 269, 270; 269, 271; 270, identifier:exceptions; 271, integer:0; 272, unary_operator:-; 272, 273; 273, integer:1; 274, return_statement; 274, 275; 275, identifier:self | def result(self, timeout=None):
"""
Set all asynchronous results.
:param timeout:
The number of seconds to wait for the result if the futures aren't
done. If None, then there is no limit on the wait time.
:type timeout: float
:return:
Update Solution.
:rtype: Solution
"""
it, exceptions, future_lists = [], [], []
from concurrent.futures import Future, wait as wait_fut
def update(fut, data, key):
if isinstance(fut, Future):
it.append((fut, data, key))
elif isinstance(fut, AsyncList) and fut not in future_lists:
future_lists.append(fut)
it.extend([(j, fut, i)
for i, j in enumerate(fut)
if isinstance(j, Future)][::-1])
for s in self.sub_sol.values():
for k, v in list(s.items()):
update(v, s, k)
for d in s.workflow.nodes.values():
if 'results' in d:
update(d['results'], d, 'results')
for d in s.workflow.edges.values():
if 'value' in d:
update(d['value'], d, 'value')
wait_fut({v[0] for v in it}, timeout)
for f, d, k in it:
try:
d[k] = await_result(f, 0)
except SkipNode as e:
exceptions.append((f, d, k, e.ex))
del d[k]
except (Exception, ExecutorShutdown, DispatcherAbort) as ex:
exceptions.append((f, d, k, ex))
del d[k]
if exceptions:
raise exceptions[0][-1]
return self |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_set_data_node_output; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 4, identifier:self; 5, identifier:node_id; 6, identifier:node_attr; 7, identifier:no_call; 8, default_parameter; 8, 9; 8, 10; 9, identifier:next_nds; 10, None; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kw; 13, block; 13, 14; 13, 16; 13, 17; 13, 29; 13, 163; 13, 305; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:est; 21, identifier:wait_in; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:_get_node_estimations; 26, argument_list; 26, 27; 26, 28; 27, identifier:node_attr; 28, identifier:node_id; 29, if_statement; 29, 30; 29, 32; 29, 149; 30, not_operator; 30, 31; 31, identifier:no_call; 32, block; 32, 33; 32, 58; 32, 81; 32, 108; 32, 129; 32, 141; 32, 148; 33, if_statement; 33, 34; 33, 37; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:node_id; 36, identifier:PLOT; 37, block; 37, 38; 37, 46; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:est; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:est; 44, identifier:copy; 45, argument_list; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, subscript; 48, 49; 48, 50; 49, identifier:est; 50, identifier:PLOT; 51, dictionary; 51, 52; 52, pair; 52, 53; 52, 54; 53, string:'value'; 54, dictionary; 54, 55; 55, pair; 55, 56; 55, 57; 56, string:'obj'; 57, identifier:self; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 63; 60, pattern_list; 60, 61; 60, 62; 61, identifier:sf; 62, identifier:args; 63, expression_list; 63, 64; 63, 65; 64, False; 65, tuple; 65, 66; 66, dictionary_comprehension; 66, 67; 66, 72; 67, pair; 67, 68; 67, 69; 68, identifier:k; 69, subscript; 69, 70; 69, 71; 70, identifier:v; 71, string:'value'; 72, for_in_clause; 72, 73; 72, 76; 73, pattern_list; 73, 74; 73, 75; 74, identifier:k; 75, identifier:v; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:est; 79, identifier:items; 80, argument_list; 81, if_statement; 81, 82; 81, 89; 81, 90; 82, not_operator; 82, 83; 83, parenthesized_expression; 83, 84; 84, boolean_operator:or; 84, 85; 84, 86; 85, identifier:wait_in; 86, comparison_operator:in; 86, 87; 86, 88; 87, string:'function'; 88, identifier:node_attr; 89, comment; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 96; 93, pattern_list; 93, 94; 93, 95; 94, identifier:sf; 95, identifier:args; 96, expression_list; 96, 97; 96, 98; 97, True; 98, call; 98, 99; 98, 100; 99, identifier:tuple; 100, argument_list; 100, 101; 101, call; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:args; 105, integer:0; 106, identifier:values; 107, argument_list; 108, try_statement; 108, 109; 108, 110; 108, 124; 109, comment; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:value; 114, call; 114, 115; 114, 116; 115, identifier:async_thread; 116, argument_list; 116, 117; 116, 118; 116, 119; 116, 120; 116, 121; 116, 122; 117, identifier:self; 118, identifier:args; 119, identifier:node_attr; 120, identifier:node_id; 121, identifier:sf; 122, dictionary_splat; 122, 123; 123, identifier:kw; 124, except_clause; 124, 125; 124, 126; 125, identifier:SkipNode; 126, block; 126, 127; 127, return_statement; 127, 128; 128, False; 129, if_statement; 129, 130; 129, 133; 129, 134; 130, comparison_operator:is; 130, 131; 130, 132; 131, identifier:value; 132, identifier:NONE; 133, comment; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:self; 139, identifier:node_id; 140, identifier:value; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:value; 144, dictionary; 144, 145; 145, pair; 145, 146; 145, 147; 146, string:'value'; 147, identifier:value; 148, comment; 149, else_clause; 149, 150; 150, block; 150, 151; 150, 157; 150, 158; 150, 162; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:self; 155, identifier:node_id; 156, identifier:NONE; 157, comment; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:value; 161, dictionary; 162, comment; 163, if_statement; 163, 164; 163, 165; 163, 166; 163, 186; 164, identifier:next_nds; 165, comment; 166, block; 166, 167; 166, 173; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:wf_add_edge; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:_wf_add_edge; 173, for_statement; 173, 174; 173, 175; 173, 176; 173, 177; 174, identifier:u; 175, identifier:next_nds; 176, comment; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 181; 180, identifier:wf_add_edge; 181, argument_list; 181, 182; 181, 183; 181, 184; 182, identifier:node_id; 183, identifier:u; 184, dictionary_splat; 184, 185; 185, identifier:value; 186, else_clause; 186, 187; 186, 188; 187, comment; 188, block; 188, 189; 188, 207; 188, 255; 188, 256; 188, 273; 188, 274; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 195; 191, pattern_list; 191, 192; 191, 193; 191, 194; 192, identifier:n; 193, identifier:has; 194, identifier:sub_sol; 195, expression_list; 195, 196; 195, 199; 195, 204; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:nodes; 199, attribute; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:workflow; 203, identifier:has_edge; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:sub_sol; 207, function_definition; 207, 208; 207, 209; 207, 211; 208, function_name:no_visited_in_sub_dsp; 209, parameters; 209, 210; 210, identifier:i; 211, block; 211, 212; 211, 218; 211, 253; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:node; 215, subscript; 215, 216; 215, 217; 216, identifier:n; 217, identifier:i; 218, if_statement; 218, 219; 218, 230; 219, boolean_operator:and; 219, 220; 219, 225; 220, comparison_operator:==; 220, 221; 220, 224; 221, subscript; 221, 222; 221, 223; 222, identifier:node; 223, string:'type'; 224, string:'dispatcher'; 225, call; 225, 226; 225, 227; 226, identifier:has; 227, argument_list; 227, 228; 227, 229; 228, identifier:i; 229, identifier:node_id; 230, block; 230, 231; 230, 245; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:visited; 234, attribute; 234, 235; 234, 244; 235, subscript; 235, 236; 235, 237; 236, identifier:sub_sol; 237, binary_operator:+; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:index; 241, subscript; 241, 242; 241, 243; 242, identifier:node; 243, string:'index'; 244, identifier:_visited; 245, return_statement; 245, 246; 246, comparison_operator:not; 246, 247; 246, 252; 247, subscript; 247, 248; 247, 251; 248, subscript; 248, 249; 248, 250; 249, identifier:node; 250, string:'inputs'; 251, identifier:node_id; 252, identifier:visited; 253, return_statement; 253, 254; 254, True; 255, comment; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:succ_fun; 259, list_comprehension; 259, 260; 259, 261; 259, 268; 260, identifier:u; 261, for_in_clause; 261, 262; 261, 263; 262, identifier:u; 263, subscript; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:self; 266, identifier:_succ; 267, identifier:node_id; 268, if_clause; 268, 269; 269, call; 269, 270; 269, 271; 270, identifier:no_visited_in_sub_dsp; 271, argument_list; 271, 272; 272, identifier:u; 273, comment; 274, if_statement; 274, 275; 274, 284; 274, 285; 275, boolean_operator:and; 275, 276; 275, 277; 276, identifier:succ_fun; 277, comparison_operator:not; 277, 278; 277, 281; 278, subscript; 278, 279; 278, 280; 279, identifier:succ_fun; 280, integer:0; 281, attribute; 281, 282; 281, 283; 282, identifier:self; 283, identifier:_visited; 284, comment; 285, block; 285, 286; 285, 292; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:wf_add_edge; 289, attribute; 289, 290; 289, 291; 290, identifier:self; 291, identifier:_wf_add_edge; 292, for_statement; 292, 293; 292, 294; 292, 295; 292, 296; 293, identifier:u; 294, identifier:succ_fun; 295, comment; 296, block; 296, 297; 297, expression_statement; 297, 298; 298, call; 298, 299; 298, 300; 299, identifier:wf_add_edge; 300, argument_list; 300, 301; 300, 302; 300, 303; 301, identifier:node_id; 302, identifier:u; 303, dictionary_splat; 303, 304; 304, identifier:value; 305, return_statement; 305, 306; 306, True | def _set_data_node_output(self, node_id, node_attr, no_call, next_nds=None,
**kw):
"""
Set the data node output from node estimations.
:param node_id:
Data node id.
:type node_id: str
:param node_attr:
Dictionary of node attributes.
:type node_attr: dict[str, T]
:param no_call:
If True data node estimations are not used.
:type no_call: bool
:return:
If the output have been evaluated correctly.
:rtype: bool
"""
# Get data node estimations.
est, wait_in = self._get_node_estimations(node_attr, node_id)
if not no_call:
if node_id is PLOT:
est = est.copy()
est[PLOT] = {'value': {'obj': self}}
sf, args = False, ({k: v['value'] for k, v in est.items()},)
if not (wait_in or 'function' in node_attr):
# Data node that has just one estimation value.
sf, args = True, tuple(args[0].values())
try:
# Final estimation of the node and node status.
value = async_thread(self, args, node_attr, node_id, sf, **kw)
except SkipNode:
return False
if value is not NONE: # Set data output.
self[node_id] = value
value = {'value': value} # Output value.
else:
self[node_id] = NONE # Set data output.
value = {} # Output value.
if next_nds:
# namespace shortcuts for speed.
wf_add_edge = self._wf_add_edge
for u in next_nds: # Set workflow.
wf_add_edge(node_id, u, **value)
else:
# namespace shortcuts for speed.
n, has, sub_sol = self.nodes, self.workflow.has_edge, self.sub_sol
def no_visited_in_sub_dsp(i):
node = n[i]
if node['type'] == 'dispatcher' and has(i, node_id):
visited = sub_sol[self.index + node['index']]._visited
return node['inputs'][node_id] not in visited
return True
# List of functions.
succ_fun = [u for u in self._succ[node_id]
if no_visited_in_sub_dsp(u)]
# Check if it has functions as outputs and wildcard condition.
if succ_fun and succ_fun[0] not in self._visited:
# namespace shortcuts for speed.
wf_add_edge = self._wf_add_edge
for u in succ_fun: # Set workflow.
wf_add_edge(node_id, u, **value)
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:_set_function_node_output; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 4, identifier:self; 5, identifier:node_id; 6, identifier:node_attr; 7, identifier:no_call; 8, default_parameter; 8, 9; 8, 10; 9, identifier:next_nds; 10, None; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kw; 13, block; 13, 14; 13, 16; 13, 17; 13, 29; 13, 30; 13, 48; 13, 65; 13, 71; 13, 72; 13, 88; 13, 96; 13, 97; 13, 111; 13, 152; 13, 153; 13, 190; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:o_nds; 21, identifier:dist; 22, expression_list; 22, 23; 22, 26; 23, subscript; 23, 24; 23, 25; 24, identifier:node_attr; 25, string:'outputs'; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:dist; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:output_nodes; 33, boolean_operator:or; 33, 34; 33, 35; 34, identifier:next_nds; 35, call; 35, 36; 35, 46; 36, attribute; 36, 37; 36, 45; 37, call; 37, 38; 37, 39; 38, identifier:set; 39, argument_list; 39, 40; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:_succ; 44, identifier:node_id; 45, identifier:difference; 46, argument_list; 46, 47; 47, identifier:dist; 48, if_statement; 48, 49; 48, 51; 48, 52; 49, not_operator; 49, 50; 50, identifier:output_nodes; 51, comment; 52, block; 52, 53; 52, 62; 52, 63; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:workflow; 59, identifier:remove_node; 60, argument_list; 60, 61; 61, identifier:node_id; 62, comment; 63, return_statement; 63, 64; 64, False; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:wf_add_edge; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:_wf_add_edge; 71, comment; 72, if_statement; 72, 73; 72, 74; 73, identifier:no_call; 74, block; 74, 75; 74, 86; 75, for_statement; 75, 76; 75, 77; 75, 78; 75, 79; 76, identifier:u; 77, identifier:output_nodes; 78, comment; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:wf_add_edge; 83, argument_list; 83, 84; 83, 85; 84, identifier:node_id; 85, identifier:u; 86, return_statement; 86, 87; 87, True; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:args; 91, subscript; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:_wf_pred; 95, identifier:node_id; 96, comment; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:args; 100, list_comprehension; 100, 101; 100, 106; 101, subscript; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:args; 104, identifier:k; 105, string:'value'; 106, for_in_clause; 106, 107; 106, 108; 107, identifier:k; 108, subscript; 108, 109; 108, 110; 109, identifier:node_attr; 110, string:'inputs'; 111, try_statement; 111, 112; 111, 147; 112, block; 112, 113; 112, 122; 112, 134; 112, 135; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:_check_function_domain; 118, argument_list; 118, 119; 118, 120; 118, 121; 119, identifier:args; 120, identifier:node_attr; 121, identifier:node_id; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:res; 125, call; 125, 126; 125, 127; 126, identifier:async_thread; 127, argument_list; 127, 128; 127, 129; 127, 130; 127, 131; 127, 132; 128, identifier:self; 129, identifier:args; 130, identifier:node_attr; 131, identifier:node_id; 132, dictionary_splat; 132, 133; 133, identifier:kw; 134, comment; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 146; 137, subscript; 137, 138; 137, 145; 138, subscript; 138, 139; 138, 144; 139, attribute; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:workflow; 143, identifier:node; 144, identifier:node_id; 145, string:'results'; 146, identifier:res; 147, except_clause; 147, 148; 147, 149; 148, identifier:SkipNode; 149, block; 149, 150; 150, return_statement; 150, 151; 151, False; 152, comment; 153, for_statement; 153, 154; 153, 157; 153, 171; 154, pattern_list; 154, 155; 154, 156; 155, identifier:k; 156, identifier:v; 157, call; 157, 158; 157, 159; 158, identifier:zip; 159, argument_list; 159, 160; 159, 161; 160, identifier:o_nds; 161, conditional_expression:if; 161, 162; 161, 163; 161, 169; 162, identifier:res; 163, comparison_operator:>; 163, 164; 163, 168; 164, call; 164, 165; 164, 166; 165, identifier:len; 166, argument_list; 166, 167; 167, identifier:o_nds; 168, integer:1; 169, list:[res]; 169, 170; 170, identifier:res; 171, block; 171, 172; 172, if_statement; 172, 173; 172, 180; 173, boolean_operator:and; 173, 174; 173, 177; 174, comparison_operator:in; 174, 175; 174, 176; 175, identifier:k; 176, identifier:output_nodes; 177, comparison_operator:is; 177, 178; 177, 179; 178, identifier:v; 179, identifier:NONE; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:wf_add_edge; 184, argument_list; 184, 185; 184, 186; 184, 187; 185, identifier:node_id; 186, identifier:k; 187, keyword_argument; 187, 188; 187, 189; 188, identifier:value; 189, identifier:v; 190, return_statement; 190, 191; 191, True | def _set_function_node_output(self, node_id, node_attr, no_call,
next_nds=None, **kw):
"""
Set the function node output from node inputs.
:param node_id:
Function node id.
:type node_id: str
:param node_attr:
Dictionary of node attributes.
:type node_attr: dict[str, T]
:param no_call:
If True data node estimation function is not used.
:type no_call: bool
:return:
If the output have been evaluated correctly.
:rtype: bool
"""
# Namespace shortcuts for speed.
o_nds, dist = node_attr['outputs'], self.dist
# List of nodes that can still be estimated by the function node.
output_nodes = next_nds or set(self._succ[node_id]).difference(dist)
if not output_nodes: # This function is not needed.
self.workflow.remove_node(node_id) # Remove function node.
return False
wf_add_edge = self._wf_add_edge # Namespace shortcuts for speed.
if no_call:
for u in output_nodes: # Set workflow out.
wf_add_edge(node_id, u)
return True
args = self._wf_pred[node_id] # List of the function's arguments.
args = [args[k]['value'] for k in node_attr['inputs']]
try:
self._check_function_domain(args, node_attr, node_id)
res = async_thread(self, args, node_attr, node_id, **kw)
# noinspection PyUnresolvedReferences
self.workflow.node[node_id]['results'] = res
except SkipNode:
return False
# Set workflow.
for k, v in zip(o_nds, res if len(o_nds) > 1 else [res]):
if k in output_nodes and v is not NONE:
wf_add_edge(node_id, k, value=v)
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:_add_initial_value; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 4, identifier:self; 5, identifier:data_id; 6, identifier:value; 7, default_parameter; 7, 8; 7, 9; 8, identifier:initial_dist; 9, float:0.0; 10, default_parameter; 10, 11; 10, 12; 11, identifier:fringe; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:check_cutoff; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:no_call; 18, None; 19, block; 19, 20; 19, 22; 19, 23; 19, 39; 19, 51; 19, 63; 19, 69; 19, 80; 19, 91; 19, 99; 19, 107; 19, 115; 19, 116; 19, 124; 19, 125; 19, 133; 19, 134; 19, 290; 19, 296; 19, 297; 19, 355; 20, expression_statement; 20, 21; 21, comment; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 29; 25, pattern_list; 25, 26; 25, 27; 25, 28; 26, identifier:nodes; 27, identifier:seen; 28, identifier:edge_weight; 29, expression_list; 29, 30; 29, 33; 29, 36; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:nodes; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:seen; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:_edge_length; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, pattern_list; 41, 42; 41, 43; 42, identifier:wf_remove_edge; 43, identifier:check_wait_in; 44, expression_list; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:_wf_remove_edge; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:check_wait_in; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 56; 53, pattern_list; 53, 54; 53, 55; 54, identifier:wf_add_edge; 55, identifier:dsp_in; 56, expression_list; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:_wf_add_edge; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:_set_sub_dsp_node_input; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:update_view; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:_update_meeting; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:is; 70, 71; 70, 72; 71, identifier:fringe; 72, None; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:fringe; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:fringe; 80, if_statement; 80, 81; 80, 84; 81, comparison_operator:is; 81, 82; 81, 83; 82, identifier:no_call; 83, None; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:no_call; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:no_call; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:check_cutoff; 94, boolean_operator:or; 94, 95; 94, 96; 95, identifier:check_cutoff; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:check_cutoff; 99, if_statement; 99, 100; 99, 103; 99, 104; 100, comparison_operator:not; 100, 101; 100, 102; 101, identifier:data_id; 102, identifier:nodes; 103, comment; 104, block; 104, 105; 105, return_statement; 105, 106; 106, False; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:wait_in; 110, subscript; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:nodes; 113, identifier:data_id; 114, string:'wait_inputs'; 115, comment; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:index; 119, subscript; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:nodes; 122, identifier:data_id; 123, string:'index'; 124, comment; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:wf_add_edge; 128, argument_list; 128, 129; 128, 130; 128, 131; 129, identifier:START; 130, identifier:data_id; 131, dictionary_splat; 131, 132; 132, identifier:value; 133, comment; 134, if_statement; 134, 135; 134, 140; 134, 141; 135, comparison_operator:in; 135, 136; 135, 137; 136, identifier:data_id; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:_wildcards; 140, comment; 141, block; 141, 142; 141, 151; 141, 152; 141, 161; 141, 162; 141, 288; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:_visited; 148, identifier:add; 149, argument_list; 149, 150; 150, identifier:data_id; 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:workflow; 158, identifier:add_node; 159, argument_list; 159, 160; 160, identifier:data_id; 161, comment; 162, for_statement; 162, 163; 162, 166; 162, 175; 162, 176; 163, pattern_list; 163, 164; 163, 165; 164, identifier:w; 165, identifier:edge_data; 166, call; 166, 167; 166, 174; 167, attribute; 167, 168; 167, 173; 168, subscript; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:dmap; 172, identifier:data_id; 173, identifier:items; 174, argument_list; 175, comment; 176, block; 176, 177; 176, 185; 176, 186; 176, 192; 176, 193; 176, 194; 176, 204; 176, 210; 176, 211; 176, 212; 176, 253; 176, 259; 176, 260; 176, 273; 176, 274; 176, 287; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:wf_add_edge; 180, argument_list; 180, 181; 180, 182; 180, 183; 181, identifier:data_id; 182, identifier:w; 183, dictionary_splat; 183, 184; 184, identifier:value; 185, comment; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:node; 189, subscript; 189, 190; 189, 191; 190, identifier:nodes; 191, identifier:w; 192, comment; 193, comment; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:vw_dist; 197, binary_operator:+; 197, 198; 197, 199; 198, identifier:initial_dist; 199, call; 199, 200; 199, 201; 200, identifier:edge_weight; 201, argument_list; 201, 202; 201, 203; 202, identifier:edge_data; 203, identifier:node; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 207; 206, identifier:update_view; 207, argument_list; 207, 208; 207, 209; 208, identifier:w; 209, identifier:vw_dist; 210, comment; 211, comment; 212, if_statement; 212, 213; 212, 217; 212, 227; 212, 244; 213, call; 213, 214; 213, 215; 214, identifier:check_cutoff; 215, argument_list; 215, 216; 216, identifier:vw_dist; 217, block; 217, 218; 217, 224; 217, 225; 217, 226; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:wf_remove_edge; 221, argument_list; 221, 222; 221, 223; 222, identifier:data_id; 223, identifier:w; 224, comment; 225, continue_statement; 226, comment; 227, elif_clause; 227, 228; 227, 233; 228, comparison_operator:==; 228, 229; 228, 232; 229, subscript; 229, 230; 229, 231; 230, identifier:node; 231, string:'type'; 232, string:'dispatcher'; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:dsp_in; 237, argument_list; 237, 238; 237, 239; 237, 240; 237, 241; 237, 242; 237, 243; 238, identifier:data_id; 239, identifier:w; 240, identifier:fringe; 241, identifier:check_cutoff; 242, identifier:no_call; 243, identifier:vw_dist; 244, elif_clause; 244, 245; 244, 250; 245, call; 245, 246; 245, 247; 246, identifier:check_wait_in; 247, argument_list; 247, 248; 247, 249; 248, True; 249, identifier:w; 250, block; 250, 251; 250, 252; 251, continue_statement; 252, comment; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 258; 255, subscript; 255, 256; 255, 257; 256, identifier:seen; 257, identifier:w; 258, identifier:vw_dist; 259, comment; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:vd; 263, tuple; 263, 264; 263, 265; 263, 266; 264, True; 265, identifier:w; 266, binary_operator:+; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:index; 270, subscript; 270, 271; 270, 272; 271, identifier:node; 272, string:'index'; 273, comment; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:heapq; 278, identifier:heappush; 279, argument_list; 279, 280; 279, 281; 280, identifier:fringe; 281, tuple; 281, 282; 281, 283; 281, 284; 282, identifier:vw_dist; 283, identifier:vd; 284, tuple; 284, 285; 284, 286; 285, identifier:w; 286, identifier:self; 287, comment; 288, return_statement; 288, 289; 289, True; 290, expression_statement; 290, 291; 291, call; 291, 292; 291, 293; 292, identifier:update_view; 293, argument_list; 293, 294; 293, 295; 294, identifier:data_id; 295, identifier:initial_dist; 296, comment; 297, if_statement; 297, 298; 297, 302; 297, 303; 297, 311; 298, call; 298, 299; 298, 300; 299, identifier:check_cutoff; 300, argument_list; 300, 301; 301, identifier:initial_dist; 302, comment; 303, block; 303, 304; 303, 310; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 307; 306, identifier:wf_remove_edge; 307, argument_list; 307, 308; 307, 309; 308, identifier:START; 309, identifier:data_id; 310, comment; 311, elif_clause; 311, 312; 311, 318; 311, 319; 312, not_operator; 312, 313; 313, call; 313, 314; 313, 315; 314, identifier:check_wait_in; 315, argument_list; 315, 316; 315, 317; 316, identifier:wait_in; 317, identifier:data_id; 318, comment; 319, block; 319, 320; 319, 326; 319, 327; 319, 338; 319, 339; 319, 340; 319, 353; 320, expression_statement; 320, 321; 321, assignment; 321, 322; 321, 325; 322, subscript; 322, 323; 322, 324; 323, identifier:seen; 324, identifier:data_id; 325, identifier:initial_dist; 326, comment; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:vd; 330, tuple; 330, 331; 330, 332; 330, 333; 331, identifier:wait_in; 332, identifier:data_id; 333, binary_operator:+; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:self; 336, identifier:index; 337, identifier:index; 338, comment; 339, comment; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:heapq; 344, identifier:heappush; 345, argument_list; 345, 346; 345, 347; 346, identifier:fringe; 347, tuple; 347, 348; 347, 349; 347, 350; 348, identifier:initial_dist; 349, identifier:vd; 350, tuple; 350, 351; 350, 352; 351, identifier:data_id; 352, identifier:self; 353, return_statement; 353, 354; 354, True; 355, return_statement; 355, 356; 356, False | def _add_initial_value(self, data_id, value, initial_dist=0.0,
fringe=None, check_cutoff=None, no_call=None):
"""
Add initial values updating workflow, seen, and fringe.
:param fringe:
Heapq of closest available nodes.
:type fringe: list[(float | int, bool, (str, Dispatcher)]
:param check_cutoff:
Check the cutoff limit.
:type check_cutoff: (int | float) -> bool
:param no_call:
If True data node estimation function is not used.
:type no_call: bool
:param data_id:
Data node id.
:type data_id: str
:param value:
Data node value e.g., {'value': val}.
:type value: dict[str, T]
:param initial_dist:
Data node initial distance in the ArciDispatch algorithm.
:type initial_dist: float, int, optional
:return:
True if the data has been visited, otherwise false.
:rtype: bool
"""
# Namespace shortcuts for speed.
nodes, seen, edge_weight = self.nodes, self.seen, self._edge_length
wf_remove_edge, check_wait_in = self._wf_remove_edge, self.check_wait_in
wf_add_edge, dsp_in = self._wf_add_edge, self._set_sub_dsp_node_input
update_view = self._update_meeting
if fringe is None:
fringe = self.fringe
if no_call is None:
no_call = self.no_call
check_cutoff = check_cutoff or self.check_cutoff
if data_id not in nodes: # Data node is not in the dmap.
return False
wait_in = nodes[data_id]['wait_inputs'] # Store wait inputs flag.
index = nodes[data_id]['index'] # Store node index.
wf_add_edge(START, data_id, **value) # Add edge.
if data_id in self._wildcards: # Check if the data node has wildcard.
self._visited.add(data_id) # Update visited nodes.
self.workflow.add_node(data_id) # Add node to workflow.
for w, edge_data in self.dmap[data_id].items(): # See func node.
wf_add_edge(data_id, w, **value) # Set workflow.
node = nodes[w] # Node attributes.
# Evaluate distance.
vw_dist = initial_dist + edge_weight(edge_data, node)
update_view(w, vw_dist) # Update view distance.
# Check the cutoff limit and if all inputs are satisfied.
if check_cutoff(vw_dist):
wf_remove_edge(data_id, w) # Remove workflow edge.
continue # Pass the node.
elif node['type'] == 'dispatcher':
dsp_in(data_id, w, fringe, check_cutoff, no_call, vw_dist)
elif check_wait_in(True, w):
continue # Pass the node.
seen[w] = vw_dist # Update distance.
vd = (True, w, self.index + node['index']) # Virtual distance.
heapq.heappush(fringe, (vw_dist, vd, (w, self))) # Add 2 heapq.
return True
update_view(data_id, initial_dist) # Update view distance.
if check_cutoff(initial_dist): # Check the cutoff limit.
wf_remove_edge(START, data_id) # Remove workflow edge.
elif not check_wait_in(wait_in, data_id): # Check inputs.
seen[data_id] = initial_dist # Update distance.
vd = (wait_in, data_id, self.index + index) # Virtual distance.
# Add node to heapq.
heapq.heappush(fringe, (initial_dist, vd, (data_id, self)))
return True
return False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:_see_node; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:node_id; 6, identifier:fringe; 7, identifier:dist; 8, default_parameter; 8, 9; 8, 10; 9, identifier:w_wait_in; 10, integer:0; 11, block; 11, 12; 11, 14; 11, 15; 11, 27; 11, 37; 11, 38; 11, 46; 11, 47; 11, 48; 11, 147; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 20; 17, pattern_list; 17, 18; 17, 19; 18, identifier:seen; 19, identifier:dists; 20, expression_list; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:seen; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:dist; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:wait_in; 30, subscript; 30, 31; 30, 36; 31, subscript; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:nodes; 35, identifier:node_id; 36, string:'wait_inputs'; 37, comment; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_update_meeting; 43, argument_list; 43, 44; 43, 45; 44, identifier:node_id; 45, identifier:dist; 46, comment; 47, comment; 48, if_statement; 48, 49; 48, 56; 48, 59; 48, 83; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:check_wait_in; 53, argument_list; 53, 54; 53, 55; 54, identifier:wait_in; 55, identifier:node_id; 56, block; 56, 57; 56, 58; 57, pass_statement; 58, comment; 59, elif_clause; 59, 60; 59, 63; 59, 64; 60, comparison_operator:in; 60, 61; 60, 62; 61, identifier:node_id; 62, identifier:dists; 63, comment; 64, block; 64, 65; 65, if_statement; 65, 66; 65, 71; 65, 72; 66, comparison_operator:<; 66, 67; 66, 68; 67, identifier:dist; 68, subscript; 68, 69; 68, 70; 69, identifier:dists; 70, identifier:node_id; 71, comment; 72, block; 72, 73; 73, raise_statement; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:DispatcherError; 76, argument_list; 76, 77; 76, 80; 77, concatenated_string; 77, 78; 77, 79; 78, string:'Contradictory paths found: '; 79, string:'negative weights?'; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:sol; 82, identifier:self; 83, elif_clause; 83, 84; 83, 93; 83, 94; 84, boolean_operator:or; 84, 85; 84, 88; 85, comparison_operator:not; 85, 86; 85, 87; 86, identifier:node_id; 87, identifier:seen; 88, comparison_operator:<; 88, 89; 88, 90; 89, identifier:dist; 90, subscript; 90, 91; 90, 92; 91, identifier:seen; 92, identifier:node_id; 93, comment; 94, block; 94, 95; 94, 101; 94, 102; 94, 112; 94, 113; 94, 114; 94, 130; 94, 131; 94, 144; 94, 146; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:seen; 99, identifier:node_id; 100, identifier:dist; 101, comment; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:index; 105, subscript; 105, 106; 105, 111; 106, subscript; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:nodes; 110, identifier:node_id; 111, string:'index'; 112, comment; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:vd; 117, tuple; 117, 118; 117, 124; 117, 125; 118, binary_operator:+; 118, 119; 118, 120; 119, identifier:w_wait_in; 120, call; 120, 121; 120, 122; 121, identifier:int; 122, argument_list; 122, 123; 123, identifier:wait_in; 124, identifier:node_id; 125, binary_operator:+; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:index; 129, identifier:index; 130, comment; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:heapq; 135, identifier:heappush; 136, argument_list; 136, 137; 136, 138; 137, identifier:fringe; 138, tuple; 138, 139; 138, 140; 138, 141; 139, identifier:dist; 140, identifier:vd; 141, tuple; 141, 142; 141, 143; 142, identifier:node_id; 143, identifier:self; 144, return_statement; 144, 145; 145, True; 146, comment; 147, return_statement; 147, 148; 148, False | def _see_node(self, node_id, fringe, dist, w_wait_in=0):
"""
See a node, updating seen and fringe.
:param node_id:
Node id to see.
:type node_id: str
:param fringe:
Heapq of closest available nodes.
:type fringe: list[(float | int, bool, (str, Dispatcher)]
:param dist:
Distance from the starting node.
:type dist: float, int
:param w_wait_in:
Additional weight for sorting correctly the nodes in the fringe.
:type w_wait_in: int, float
:return:
True if the node is visible, otherwise False.
:rtype: bool
"""
# Namespace shortcuts.
seen, dists = self.seen, self.dist
wait_in = self.nodes[node_id]['wait_inputs'] # Wait inputs flag.
self._update_meeting(node_id, dist) # Update view distance.
# Check if inputs are satisfied.
if self.check_wait_in(wait_in, node_id):
pass # Pass the node
elif node_id in dists: # The node w already estimated.
if dist < dists[node_id]: # Error for negative paths.
raise DispatcherError('Contradictory paths found: '
'negative weights?', sol=self)
elif node_id not in seen or dist < seen[node_id]: # Check min dist.
seen[node_id] = dist # Update dist.
index = self.nodes[node_id]['index'] # Node index.
# Virtual distance.
vd = (w_wait_in + int(wait_in), node_id, self.index + index)
# Add to heapq.
heapq.heappush(fringe, (dist, vd, (node_id, self)))
return True # The node is visible.
return False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:tick; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:events; 6, block; 6, 7; 6, 9; 6, 15; 6, 73; 6, 276; 6, 280; 6, 405; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:add_new_threads; 14, argument_list; 15, if_statement; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:drag_sprite; 19, block; 19, 20; 19, 32; 19, 40; 19, 50; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 25; 22, tuple_pattern; 22, 23; 22, 24; 23, identifier:mx; 24, identifier:my; 25, call; 25, 26; 25, 31; 26, attribute; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:screen; 30, identifier:get_mouse_pos; 31, argument_list; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, tuple_pattern; 34, 35; 34, 36; 35, identifier:ox; 36, identifier:oy; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:drag_offset; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:new_position; 43, tuple; 43, 44; 43, 47; 44, binary_operator:+; 44, 45; 44, 46; 45, identifier:mx; 46, identifier:ox; 47, binary_operator:+; 47, 48; 47, 49; 48, identifier:my; 49, identifier:oy; 50, if_statement; 50, 51; 50, 58; 51, comparison_operator:!=; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:drag_sprite; 56, identifier:position; 57, identifier:new_position; 58, block; 58, 59; 58, 65; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:has_dragged; 64, True; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:drag_sprite; 71, identifier:position; 72, identifier:new_position; 73, for_statement; 73, 74; 73, 75; 73, 76; 74, identifier:event; 75, identifier:events; 76, block; 76, 77; 77, if_statement; 77, 78; 77, 83; 77, 110; 77, 242; 78, comparison_operator:==; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:event; 81, identifier:kind; 82, string:"key_pressed"; 83, block; 83, 84; 83, 100; 84, assert_statement; 84, 85; 85, comparison_operator:in; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:event; 88, identifier:value; 89, call; 89, 90; 89, 99; 90, attribute; 90, 91; 90, 98; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:kurt; 94, identifier:Insert; 95, argument_list; 95, 96; 95, 97; 96, None; 97, string:"key"; 98, identifier:options; 99, argument_list; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:trigger_hats; 105, argument_list; 105, 106; 105, 107; 106, string:"whenKeyPressed"; 107, attribute; 107, 108; 107, 109; 108, identifier:event; 109, identifier:value; 110, elif_clause; 110, 111; 110, 116; 111, comparison_operator:==; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:event; 114, identifier:kind; 115, string:"mouse_down"; 116, block; 116, 117; 116, 127; 116, 178; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:mouse_pos; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:screen; 125, identifier:get_mouse_pos; 126, argument_list; 127, for_statement; 127, 128; 127, 129; 127, 137; 127, 168; 128, identifier:sprite; 129, call; 129, 130; 129, 131; 130, identifier:reversed; 131, argument_list; 131, 132; 132, attribute; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:project; 136, identifier:sprites; 137, block; 137, 138; 137, 145; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:rect; 141, call; 141, 142; 141, 143; 142, identifier:bounds; 143, argument_list; 143, 144; 144, identifier:sprite; 145, if_statement; 145, 146; 145, 152; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:rect; 149, identifier:collide_point; 150, argument_list; 150, 151; 151, identifier:mouse_pos; 152, block; 152, 153; 153, if_statement; 153, 154; 153, 162; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:screen; 159, identifier:touching_mouse; 160, argument_list; 160, 161; 161, identifier:sprite; 162, block; 162, 163; 162, 167; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:scriptable; 166, identifier:sprite; 167, break_statement; 168, else_clause; 168, 169; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:scriptable; 173, attribute; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:project; 177, identifier:stage; 178, if_statement; 178, 179; 178, 182; 178, 232; 179, attribute; 179, 180; 179, 181; 180, identifier:scriptable; 181, identifier:is_draggable; 182, block; 182, 183; 182, 195; 182, 203; 182, 215; 182, 221; 182, 227; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, tuple_pattern; 185, 186; 185, 187; 186, identifier:mx; 187, identifier:my; 188, call; 188, 189; 188, 194; 189, attribute; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:screen; 193, identifier:get_mouse_pos; 194, argument_list; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 200; 197, tuple_pattern; 197, 198; 197, 199; 198, identifier:x; 199, identifier:y; 200, attribute; 200, 201; 200, 202; 201, identifier:scriptable; 202, identifier:position; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:drag_offset; 208, tuple; 208, 209; 208, 212; 209, binary_operator:-; 209, 210; 209, 211; 210, identifier:x; 211, identifier:mx; 212, binary_operator:-; 212, 213; 212, 214; 213, identifier:y; 214, identifier:my; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:self; 219, identifier:drag_sprite; 220, identifier:scriptable; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:self; 225, identifier:has_dragged; 226, False; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:go_to_front; 230, argument_list; 230, 231; 231, identifier:scriptable; 232, else_clause; 232, 233; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:trigger_scriptable_hats; 239, argument_list; 239, 240; 239, 241; 240, identifier:scriptable; 241, string:"whenClicked"; 242, elif_clause; 242, 243; 242, 248; 243, comparison_operator:==; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:event; 246, identifier:kind; 247, string:"mouse_up"; 248, block; 248, 249; 249, if_statement; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:self; 252, identifier:drag_sprite; 253, block; 253, 254; 253, 270; 254, if_statement; 254, 255; 254, 259; 255, not_operator; 255, 256; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:has_dragged; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:self; 264, identifier:trigger_scriptable_hats; 265, argument_list; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:self; 268, identifier:drag_sprite; 269, string:"whenClicked"; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:drag_sprite; 275, None; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:remove_threads; 279, list:[]; 280, while_statement; 280, 281; 280, 282; 281, integer:1; 282, block; 282, 283; 283, for_statement; 283, 284; 283, 287; 283, 294; 283, 402; 284, tuple_pattern; 284, 285; 284, 286; 285, identifier:script; 286, identifier:thread; 287, call; 287, 288; 287, 293; 288, attribute; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:self; 291, identifier:threads; 292, identifier:items; 293, argument_list; 294, block; 294, 295; 294, 299; 294, 398; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:modified; 298, False; 299, for_statement; 299, 300; 299, 301; 299, 306; 300, identifier:event; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:thread; 304, identifier:tick; 305, argument_list; 306, block; 306, 307; 307, if_statement; 307, 308; 307, 313; 307, 392; 308, comparison_operator:==; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:event; 311, identifier:kind; 312, string:"stop"; 313, block; 313, 314; 314, if_statement; 314, 315; 314, 320; 314, 328; 314, 373; 315, comparison_operator:==; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:event; 318, identifier:value; 319, string:"all"; 320, block; 320, 321; 320, 327; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:self; 325, identifier:stop; 326, argument_list; 327, return_statement; 328, elif_clause; 328, 329; 328, 334; 329, comparison_operator:==; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:event; 332, identifier:value; 333, string:"other scripts in sprite"; 334, block; 334, 335; 334, 368; 334, 372; 335, for_statement; 335, 336; 335, 339; 335, 346; 336, tuple_pattern; 336, 337; 336, 338; 337, identifier:script; 338, identifier:other; 339, call; 339, 340; 339, 345; 340, attribute; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:self; 343, identifier:threads; 344, identifier:items; 345, argument_list; 346, block; 346, 347; 347, if_statement; 347, 348; 347, 355; 348, comparison_operator:==; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:other; 351, identifier:scriptable; 352, attribute; 352, 353; 352, 354; 353, identifier:thread; 354, identifier:scriptable; 355, block; 355, 356; 355, 362; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:other; 360, identifier:finish; 361, argument_list; 362, delete_statement; 362, 363; 363, subscript; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:self; 366, identifier:threads; 367, identifier:script; 368, expression_statement; 368, 369; 369, assignment; 369, 370; 369, 371; 370, identifier:modified; 371, True; 372, break_statement; 373, else_clause; 373, 374; 374, block; 374, 375; 374, 381; 374, 387; 374, 391; 375, expression_statement; 375, 376; 376, call; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:thread; 379, identifier:finish; 380, argument_list; 381, delete_statement; 381, 382; 382, subscript; 382, 383; 382, 386; 383, attribute; 383, 384; 383, 385; 384, identifier:self; 385, identifier:threads; 386, identifier:script; 387, expression_statement; 387, 388; 388, assignment; 388, 389; 388, 390; 389, identifier:modified; 390, True; 391, break_statement; 392, else_clause; 392, 393; 392, 394; 393, comment; 394, block; 394, 395; 395, expression_statement; 395, 396; 396, yield; 396, 397; 397, identifier:event; 398, if_statement; 398, 399; 398, 400; 399, identifier:modified; 400, block; 400, 401; 401, break_statement; 402, else_clause; 402, 403; 403, block; 403, 404; 404, break_statement; 405, expression_statement; 405, 406; 406, call; 406, 407; 406, 410; 407, attribute; 407, 408; 407, 409; 408, identifier:self; 409, identifier:add_new_threads; 410, argument_list | def tick(self, events):
"""Execute one frame of the interpreter.
Don't call more than 40 times per second.
"""
self.add_new_threads()
if self.drag_sprite:
(mx, my) = self.screen.get_mouse_pos()
(ox, oy) = self.drag_offset
new_position = (mx + ox, my + oy)
if self.drag_sprite.position != new_position:
self.has_dragged = True
self.drag_sprite.position = new_position
for event in events:
if event.kind == "key_pressed":
assert event.value in kurt.Insert(None, "key").options()
self.trigger_hats("whenKeyPressed", event.value)
elif event.kind == "mouse_down":
mouse_pos = self.screen.get_mouse_pos()
for sprite in reversed(self.project.sprites):
rect = bounds(sprite)
if rect.collide_point(mouse_pos):
if self.screen.touching_mouse(sprite):
scriptable = sprite
break
else:
scriptable = self.project.stage
if scriptable.is_draggable:
(mx, my) = self.screen.get_mouse_pos()
(x, y) = scriptable.position
self.drag_offset = (x - mx, y - my)
self.drag_sprite = scriptable
self.has_dragged = False
go_to_front(scriptable)
else:
self.trigger_scriptable_hats(scriptable, "whenClicked")
elif event.kind == "mouse_up":
if self.drag_sprite:
if not self.has_dragged:
self.trigger_scriptable_hats(self.drag_sprite,
"whenClicked")
self.drag_sprite = None
remove_threads = []
while 1:
for (script, thread) in self.threads.items():
modified = False
for event in thread.tick():
if event.kind == "stop":
if event.value == "all":
self.stop()
return
elif event.value == "other scripts in sprite":
for (script, other) in self.threads.items():
if other.scriptable == thread.scriptable:
other.finish()
del self.threads[script]
modified = True
break
else:
thread.finish()
del self.threads[script]
modified = True
break
else: # Pass to Screen
yield event
if modified:
break
else:
break
self.add_new_threads() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:evaluate; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:s; 6, identifier:value; 7, default_parameter; 7, 8; 7, 9; 8, identifier:insert; 9, None; 10, block; 10, 11; 10, 13; 10, 22; 10, 31; 10, 210; 10, 416; 11, expression_statement; 11, 12; 12, comment; 13, assert_statement; 13, 14; 14, not_operator; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:isinstance; 17, argument_list; 17, 18; 17, 19; 18, identifier:value; 19, attribute; 19, 20; 19, 21; 20, identifier:kurt; 21, identifier:Script; 22, if_statement; 22, 23; 22, 28; 23, boolean_operator:and; 23, 24; 23, 25; 24, identifier:insert; 25, attribute; 25, 26; 25, 27; 26, identifier:insert; 27, identifier:unevaluated; 28, block; 28, 29; 29, return_statement; 29, 30; 30, identifier:value; 31, if_statement; 31, 32; 31, 39; 32, call; 32, 33; 32, 34; 33, identifier:isinstance; 34, argument_list; 34, 35; 34, 36; 35, identifier:value; 36, attribute; 36, 37; 36, 38; 37, identifier:kurt; 38, identifier:Block; 39, block; 39, 40; 39, 51; 39, 105; 39, 115; 39, 145; 39, 154; 39, 186; 39, 201; 40, if_statement; 40, 41; 40, 48; 41, comparison_operator:==; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:value; 45, identifier:type; 46, identifier:shape; 47, string:"hat"; 48, block; 48, 49; 49, return_statement; 49, 50; 50, list:[]; 51, if_statement; 51, 52; 51, 59; 52, comparison_operator:not; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:value; 55, identifier:type; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:COMMANDS; 59, block; 59, 60; 60, if_statement; 60, 61; 60, 69; 60, 94; 61, call; 61, 62; 61, 63; 62, identifier:getattr; 63, argument_list; 63, 64; 63, 67; 63, 68; 64, attribute; 64, 65; 64, 66; 65, identifier:value; 66, identifier:type; 67, string:'_workaround'; 68, None; 69, block; 69, 70; 69, 81; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:value; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:value; 77, identifier:type; 78, identifier:_workaround; 79, argument_list; 79, 80; 80, identifier:value; 81, if_statement; 81, 82; 81, 84; 82, not_operator; 82, 83; 83, identifier:value; 84, block; 84, 85; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:kurt; 89, identifier:BlockNotSupported; 90, argument_list; 90, 91; 91, attribute; 91, 92; 91, 93; 92, identifier:value; 93, identifier:type; 94, else_clause; 94, 95; 95, block; 95, 96; 96, raise_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:kurt; 100, identifier:BlockNotSupported; 101, argument_list; 101, 102; 102, attribute; 102, 103; 102, 104; 103, identifier:value; 104, identifier:type; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:f; 108, subscript; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:COMMANDS; 112, attribute; 112, 113; 112, 114; 113, identifier:value; 114, identifier:type; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:args; 118, list_comprehension; 118, 119; 118, 127; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:evaluate; 123, argument_list; 123, 124; 123, 125; 123, 126; 124, identifier:s; 125, identifier:arg; 126, identifier:arg_insert; 127, for_in_clause; 127, 128; 127, 131; 128, tuple_pattern; 128, 129; 128, 130; 129, identifier:arg; 130, identifier:arg_insert; 131, call; 131, 132; 131, 133; 132, identifier:zip; 133, argument_list; 133, 134; 133, 140; 134, call; 134, 135; 134, 136; 135, identifier:list; 136, argument_list; 136, 137; 137, attribute; 137, 138; 137, 139; 138, identifier:value; 139, identifier:args; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:value; 143, identifier:type; 144, identifier:inserts; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:value; 148, call; 148, 149; 148, 150; 149, identifier:f; 150, argument_list; 150, 151; 150, 152; 151, identifier:s; 152, list_splat; 152, 153; 153, identifier:args; 154, function_definition; 154, 155; 154, 156; 154, 158; 155, function_name:flatten_generators; 156, parameters; 156, 157; 157, identifier:gen; 158, block; 158, 159; 159, for_statement; 159, 160; 159, 161; 159, 162; 160, identifier:item; 161, identifier:gen; 162, block; 162, 163; 163, if_statement; 163, 164; 163, 170; 163, 181; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:inspect; 167, identifier:isgenerator; 168, argument_list; 168, 169; 169, identifier:item; 170, block; 170, 171; 171, for_statement; 171, 172; 171, 173; 171, 177; 172, identifier:x; 173, call; 173, 174; 173, 175; 174, identifier:flatten_generators; 175, argument_list; 175, 176; 176, identifier:item; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, yield; 179, 180; 180, identifier:x; 181, else_clause; 181, 182; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, yield; 184, 185; 185, identifier:item; 186, if_statement; 186, 187; 186, 193; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:inspect; 190, identifier:isgenerator; 191, argument_list; 191, 192; 192, identifier:value; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:value; 197, call; 197, 198; 197, 199; 198, identifier:flatten_generators; 199, argument_list; 199, 200; 200, identifier:value; 201, if_statement; 201, 202; 201, 205; 202, comparison_operator:is; 202, 203; 202, 204; 203, identifier:value; 204, None; 205, block; 205, 206; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:value; 209, list:[]; 210, if_statement; 210, 211; 210, 212; 211, identifier:insert; 212, block; 212, 213; 212, 262; 212, 283; 213, if_statement; 213, 214; 213, 219; 214, call; 214, 215; 214, 216; 215, identifier:isinstance; 216, argument_list; 216, 217; 216, 218; 217, identifier:value; 218, identifier:basestring; 219, block; 219, 220; 219, 227; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:value; 223, call; 223, 224; 223, 225; 224, identifier:unicode; 225, argument_list; 225, 226; 226, identifier:value; 227, if_statement; 227, 228; 227, 236; 228, comparison_operator:in; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:insert; 231, identifier:shape; 232, tuple; 232, 233; 232, 234; 232, 235; 233, string:"number"; 234, string:"number-menu"; 235, string:"string"; 236, block; 236, 237; 237, try_statement; 237, 238; 237, 246; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:value; 242, call; 242, 243; 242, 244; 243, identifier:float; 244, argument_list; 244, 245; 245, identifier:value; 246, except_clause; 246, 247; 246, 250; 247, tuple; 247, 248; 247, 249; 248, identifier:TypeError; 249, identifier:ValueError; 250, block; 250, 251; 251, if_statement; 251, 252; 251, 257; 252, comparison_operator:==; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:insert; 255, identifier:shape; 256, string:"number"; 257, block; 257, 258; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:value; 261, integer:0; 262, if_statement; 262, 263; 262, 275; 263, boolean_operator:and; 263, 264; 263, 269; 264, call; 264, 265; 264, 266; 265, identifier:isinstance; 266, argument_list; 266, 267; 266, 268; 267, identifier:value; 268, identifier:float; 269, comparison_operator:==; 269, 270; 269, 271; 270, identifier:value; 271, call; 271, 272; 271, 273; 272, identifier:int; 273, argument_list; 273, 274; 274, identifier:value; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:value; 279, call; 279, 280; 279, 281; 280, identifier:int; 281, argument_list; 281, 282; 282, identifier:value; 283, if_statement; 283, 284; 283, 294; 283, 323; 283, 357; 283, 391; 284, comparison_operator:in; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:insert; 287, identifier:kind; 288, tuple; 288, 289; 288, 290; 288, 291; 288, 292; 288, 293; 289, string:"spriteOrStage"; 290, string:"spriteOrMouse"; 291, string:"stageOrThis"; 292, string:"spriteOnly"; 293, string:"touching"; 294, block; 294, 295; 295, if_statement; 295, 296; 295, 301; 296, comparison_operator:not; 296, 297; 296, 298; 297, identifier:value; 298, tuple; 298, 299; 298, 300; 299, string:"mouse-pointer"; 300, string:"edge"; 301, block; 301, 302; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 305; 304, identifier:value; 305, parenthesized_expression; 305, 306; 306, conditional_expression:if; 306, 307; 306, 312; 306, 315; 307, attribute; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:project; 311, identifier:stage; 312, comparison_operator:==; 312, 313; 312, 314; 313, identifier:value; 314, string:"Stage"; 315, call; 315, 316; 315, 321; 316, attribute; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:self; 319, identifier:project; 320, identifier:get_sprite; 321, argument_list; 321, 322; 322, identifier:value; 323, elif_clause; 323, 324; 323, 329; 324, comparison_operator:==; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:insert; 327, identifier:kind; 328, string:"var"; 329, block; 329, 330; 330, if_statement; 330, 331; 330, 336; 330, 345; 331, comparison_operator:in; 331, 332; 331, 333; 332, identifier:value; 333, attribute; 333, 334; 333, 335; 334, identifier:s; 335, identifier:variables; 336, block; 336, 337; 337, expression_statement; 337, 338; 338, assignment; 338, 339; 338, 340; 339, identifier:value; 340, subscript; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:s; 343, identifier:variables; 344, identifier:value; 345, else_clause; 345, 346; 346, block; 346, 347; 347, expression_statement; 347, 348; 348, assignment; 348, 349; 348, 350; 349, identifier:value; 350, subscript; 350, 351; 350, 356; 351, attribute; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:s; 354, identifier:project; 355, identifier:variables; 356, identifier:value; 357, elif_clause; 357, 358; 357, 363; 358, comparison_operator:==; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:insert; 361, identifier:kind; 362, string:"list"; 363, block; 363, 364; 364, if_statement; 364, 365; 364, 370; 364, 379; 365, comparison_operator:in; 365, 366; 365, 367; 366, identifier:value; 367, attribute; 367, 368; 367, 369; 368, identifier:s; 369, identifier:lists; 370, block; 370, 371; 371, expression_statement; 371, 372; 372, assignment; 372, 373; 372, 374; 373, identifier:value; 374, subscript; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:s; 377, identifier:lists; 378, identifier:value; 379, else_clause; 379, 380; 380, block; 380, 381; 381, expression_statement; 381, 382; 382, assignment; 382, 383; 382, 384; 383, identifier:value; 384, subscript; 384, 385; 384, 390; 385, attribute; 385, 386; 385, 389; 386, attribute; 386, 387; 386, 388; 387, identifier:s; 388, identifier:project; 389, identifier:lists; 390, identifier:value; 391, elif_clause; 391, 392; 391, 397; 392, comparison_operator:==; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:insert; 395, identifier:kind; 396, string:"sound"; 397, block; 397, 398; 398, for_statement; 398, 399; 398, 400; 398, 403; 399, identifier:sound; 400, attribute; 400, 401; 400, 402; 401, identifier:s; 402, identifier:sounds; 403, block; 403, 404; 404, if_statement; 404, 405; 404, 410; 405, comparison_operator:==; 405, 406; 405, 409; 406, attribute; 406, 407; 406, 408; 407, identifier:sound; 408, identifier:name; 409, identifier:value; 410, block; 410, 411; 410, 415; 411, expression_statement; 411, 412; 412, assignment; 412, 413; 412, 414; 413, identifier:value; 414, identifier:sound; 415, break_statement; 416, return_statement; 416, 417; 417, identifier:value | def evaluate(self, s, value, insert=None):
"""Expression evaluator.
* For expressions, returns the value of the expression.
* For Blocks, returns a generator (or the empty list []).
"""
assert not isinstance(value, kurt.Script)
if insert and insert.unevaluated:
return value
if isinstance(value, kurt.Block):
if value.type.shape == "hat":
return []
if value.type not in self.COMMANDS:
if getattr(value.type, '_workaround', None):
value = value.type._workaround(value)
if not value:
raise kurt.BlockNotSupported(value.type)
else:
raise kurt.BlockNotSupported(value.type)
f = self.COMMANDS[value.type]
args = [self.evaluate(s, arg, arg_insert)
for (arg, arg_insert)
in zip(list(value.args), value.type.inserts)]
value = f(s, *args)
def flatten_generators(gen):
for item in gen:
if inspect.isgenerator(item):
for x in flatten_generators(item):
yield x
else:
yield item
if inspect.isgenerator(value):
value = flatten_generators(value)
if value is None:
value = []
if insert:
if isinstance(value, basestring):
value = unicode(value)
if insert.shape in ("number", "number-menu", "string"):
try:
value = float(value)
except (TypeError, ValueError):
if insert.shape == "number":
value = 0
if isinstance(value, float) and value == int(value):
value = int(value)
if insert.kind in ("spriteOrStage", "spriteOrMouse", "stageOrThis",
"spriteOnly", "touching"):
if value not in ("mouse-pointer", "edge"):
value = (self.project.stage if value == "Stage"
else self.project.get_sprite(value))
elif insert.kind == "var":
if value in s.variables:
value = s.variables[value]
else:
value = s.project.variables[value]
elif insert.kind == "list":
if value in s.lists:
value = s.lists[value]
else:
value = s.project.lists[value]
elif insert.kind == "sound":
for sound in s.sounds:
if sound.name == value:
value = sound
break
return value |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_write; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:session; 6, identifier:openFile; 7, identifier:maskMap; 8, block; 8, 9; 8, 11; 8, 12; 8, 16; 8, 17; 8, 24; 8, 84; 8, 98; 8, 112; 8, 126; 8, 140; 8, 141; 8, 145; 8, 208; 8, 209; 8, 281; 8, 282; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:FIRST_VALUE_INDEX; 15, integer:12; 16, comment; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:openFile; 21, identifier:write; 22, argument_list; 22, 23; 23, string:'DATASET\r\n'; 24, if_statement; 24, 25; 24, 32; 24, 54; 25, comparison_operator:==; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:type; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:SCALAR_TYPE; 32, block; 32, 33; 32, 47; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:openFile; 37, identifier:write; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, string:'OBJTYPE {0}\r\n'; 42, identifier:format; 43, argument_list; 43, 44; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:objectType; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:openFile; 51, identifier:write; 52, argument_list; 52, 53; 53, string:'BEGSCL\r\n'; 54, elif_clause; 54, 55; 54, 62; 55, comparison_operator:==; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:type; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:VECTOR_TYPE; 62, block; 62, 63; 62, 77; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:openFile; 67, identifier:write; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, string:'VECTYPE {0}\r\n'; 72, identifier:format; 73, argument_list; 73, 74; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:vectorType; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:openFile; 81, identifier:write; 82, argument_list; 82, 83; 83, string:'BEGVEC\r\n'; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:openFile; 88, identifier:write; 89, argument_list; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, string:'OBJID {0}\r\n'; 93, identifier:format; 94, argument_list; 94, 95; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:objectID; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:openFile; 102, identifier:write; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, string:'ND {0}\r\n'; 107, identifier:format; 108, argument_list; 108, 109; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:numberData; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:openFile; 116, identifier:write; 117, argument_list; 117, 118; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, string:'NC {0}\r\n'; 121, identifier:format; 122, argument_list; 122, 123; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:numberCells; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:openFile; 130, identifier:write; 131, argument_list; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, string:'NAME {0}\r\n'; 135, identifier:format; 136, argument_list; 136, 137; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:name; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:statusString; 144, string:''; 145, if_statement; 145, 146; 145, 151; 145, 152; 146, call; 146, 147; 146, 148; 147, identifier:isinstance; 148, argument_list; 148, 149; 148, 150; 149, identifier:maskMap; 150, identifier:RasterMapFile; 151, comment; 152, block; 152, 153; 152, 162; 152, 188; 152, 189; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:statusGrassRasterString; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:maskMap; 159, identifier:getAsGrassAsciiGrid; 160, argument_list; 160, 161; 161, identifier:session; 162, if_statement; 162, 163; 162, 166; 162, 167; 162, 176; 163, comparison_operator:is; 163, 164; 163, 165; 164, identifier:statusGrassRasterString; 165, None; 166, comment; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:statusValues; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:statusGrassRasterString; 174, identifier:split; 175, argument_list; 176, else_clause; 176, 177; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:statusValues; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:maskMap; 185, identifier:rasterText; 186, identifier:split; 187, argument_list; 188, comment; 189, for_statement; 189, 190; 189, 191; 189, 199; 190, identifier:i; 191, call; 191, 192; 191, 193; 192, identifier:range; 193, argument_list; 193, 194; 193, 195; 194, identifier:FIRST_VALUE_INDEX; 195, call; 195, 196; 195, 197; 196, identifier:len; 197, argument_list; 197, 198; 198, identifier:statusValues; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, augmented_assignment:+=; 201, 202; 201, 203; 202, identifier:statusString; 203, binary_operator:+; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:statusValues; 206, identifier:i; 207, string:'\r\n'; 208, comment; 209, for_statement; 209, 210; 209, 211; 209, 214; 209, 215; 210, identifier:timeStepRaster; 211, attribute; 211, 212; 211, 213; 212, identifier:self; 213, identifier:rasters; 214, comment; 215, block; 215, 216; 215, 233; 215, 234; 215, 248; 215, 249; 215, 258; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:openFile; 220, identifier:write; 221, argument_list; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, string:'TS {0} {1}\r\n'; 225, identifier:format; 226, argument_list; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:timeStepRaster; 229, identifier:iStatus; 230, attribute; 230, 231; 230, 232; 231, identifier:timeStepRaster; 232, identifier:timestamp; 233, comment; 234, if_statement; 234, 235; 234, 240; 235, comparison_operator:==; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, identifier:timeStepRaster; 238, identifier:iStatus; 239, integer:1; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:openFile; 245, identifier:write; 246, argument_list; 246, 247; 247, identifier:statusString; 248, comment; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:valueString; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:timeStepRaster; 255, identifier:getAsWmsDatasetString; 256, argument_list; 256, 257; 257, identifier:session; 258, if_statement; 258, 259; 258, 262; 258, 270; 259, comparison_operator:is; 259, 260; 259, 261; 260, identifier:valueString; 261, None; 262, block; 262, 263; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:openFile; 267, identifier:write; 268, argument_list; 268, 269; 269, identifier:valueString; 270, else_clause; 270, 271; 271, block; 271, 272; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:openFile; 276, identifier:write; 277, argument_list; 277, 278; 278, attribute; 278, 279; 278, 280; 279, identifier:timeStepRaster; 280, identifier:rasterText; 281, comment; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:openFile; 286, identifier:write; 287, argument_list; 287, 288; 288, string:'ENDDS\r\n' | def _write(self, session, openFile, maskMap):
"""
WMS Dataset File Write to File Method
"""
# Magic numbers
FIRST_VALUE_INDEX = 12
# Write the header
openFile.write('DATASET\r\n')
if self.type == self.SCALAR_TYPE:
openFile.write('OBJTYPE {0}\r\n'.format(self.objectType))
openFile.write('BEGSCL\r\n')
elif self.type == self.VECTOR_TYPE:
openFile.write('VECTYPE {0}\r\n'.format(self.vectorType))
openFile.write('BEGVEC\r\n')
openFile.write('OBJID {0}\r\n'.format(self.objectID))
openFile.write('ND {0}\r\n'.format(self.numberData))
openFile.write('NC {0}\r\n'.format(self.numberCells))
openFile.write('NAME {0}\r\n'.format(self.name))
# Retrieve the mask map to use as the status rasters
statusString = ''
if isinstance(maskMap, RasterMapFile):
# Convert Mask Map to GRASS ASCII Raster
statusGrassRasterString = maskMap.getAsGrassAsciiGrid(session)
if statusGrassRasterString is not None:
# Split by lines
statusValues = statusGrassRasterString.split()
else:
statusValues = maskMap.rasterText.split()
# Assemble into a string in the WMS Dataset format
for i in range(FIRST_VALUE_INDEX, len(statusValues)):
statusString += statusValues[i] + '\r\n'
# Write time steps
for timeStepRaster in self.rasters:
# Write time step header
openFile.write('TS {0} {1}\r\n'.format(timeStepRaster.iStatus, timeStepRaster.timestamp))
# Write status raster (mask map) if applicable
if timeStepRaster.iStatus == 1:
openFile.write(statusString)
# Write value raster
valueString = timeStepRaster.getAsWmsDatasetString(session)
if valueString is not None:
openFile.write(valueString)
else:
openFile.write(timeStepRaster.rasterText)
# Write ending tag for the dataset
openFile.write('ENDDS\r\n') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:get_batch; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:sentences; 5, identifier:token_dict; 6, default_parameter; 6, 7; 6, 8; 7, identifier:ignore_case; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:unk_index; 11, integer:1; 12, default_parameter; 12, 13; 12, 14; 13, identifier:eos_index; 14, integer:2; 15, block; 15, 16; 15, 18; 15, 25; 15, 36; 15, 50; 15, 64; 15, 78; 15, 191; 15, 209; 15, 227; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:batch_size; 21, call; 21, 22; 21, 23; 22, identifier:len; 23, argument_list; 23, 24; 24, identifier:sentences; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:max_sentence_len; 28, call; 28, 29; 28, 30; 29, identifier:max; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:map; 33, argument_list; 33, 34; 33, 35; 34, identifier:len; 35, identifier:sentences; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:inputs; 39, list_comprehension; 39, 40; 39, 44; 40, binary_operator:*; 40, 41; 40, 43; 41, list:[0]; 41, 42; 42, integer:0; 43, identifier:max_sentence_len; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:_; 46, call; 46, 47; 46, 48; 47, identifier:range; 48, argument_list; 48, 49; 49, identifier:batch_size; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:outputs_forward; 53, list_comprehension; 53, 54; 53, 58; 54, binary_operator:*; 54, 55; 54, 57; 55, list:[0]; 55, 56; 56, integer:0; 57, identifier:max_sentence_len; 58, for_in_clause; 58, 59; 58, 60; 59, identifier:_; 60, call; 60, 61; 60, 62; 61, identifier:range; 62, argument_list; 62, 63; 63, identifier:batch_size; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:outputs_backward; 67, list_comprehension; 67, 68; 67, 72; 68, binary_operator:*; 68, 69; 68, 71; 69, list:[0]; 69, 70; 70, integer:0; 71, identifier:max_sentence_len; 72, for_in_clause; 72, 73; 72, 74; 73, identifier:_; 74, call; 74, 75; 74, 76; 75, identifier:range; 76, argument_list; 76, 77; 77, identifier:batch_size; 78, for_statement; 78, 79; 78, 82; 78, 86; 79, pattern_list; 79, 80; 79, 81; 80, identifier:i; 81, identifier:sentence; 82, call; 82, 83; 82, 84; 83, identifier:enumerate; 84, argument_list; 84, 85; 85, identifier:sentences; 86, block; 86, 87; 86, 100; 86, 108; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 99; 89, subscript; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:outputs_forward; 92, identifier:i; 93, binary_operator:-; 93, 94; 93, 98; 94, call; 94, 95; 94, 96; 95, identifier:len; 96, argument_list; 96, 97; 97, identifier:sentence; 98, integer:1; 99, identifier:eos_index; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 107; 102, subscript; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:outputs_backward; 105, identifier:i; 106, integer:0; 107, identifier:eos_index; 108, for_statement; 108, 109; 108, 112; 108, 116; 109, pattern_list; 109, 110; 109, 111; 110, identifier:j; 111, identifier:token; 112, call; 112, 113; 112, 114; 113, identifier:enumerate; 114, argument_list; 114, 115; 115, identifier:sentence; 116, block; 116, 117; 116, 146; 116, 154; 116, 171; 117, if_statement; 117, 118; 117, 119; 117, 134; 118, identifier:ignore_case; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:index; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:token_dict; 126, identifier:get; 127, argument_list; 127, 128; 127, 133; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:token; 131, identifier:lower; 132, argument_list; 133, identifier:unk_index; 134, else_clause; 134, 135; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:index; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:token_dict; 142, identifier:get; 143, argument_list; 143, 144; 143, 145; 144, identifier:token; 145, identifier:unk_index; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 153; 148, subscript; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:inputs; 151, identifier:i; 152, identifier:j; 153, identifier:index; 154, if_statement; 154, 155; 154, 160; 155, comparison_operator:>=; 155, 156; 155, 159; 156, binary_operator:-; 156, 157; 156, 158; 157, identifier:j; 158, integer:1; 159, integer:0; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 170; 163, subscript; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:outputs_forward; 166, identifier:i; 167, binary_operator:-; 167, 168; 167, 169; 168, identifier:j; 169, integer:1; 170, identifier:index; 171, if_statement; 171, 172; 171, 180; 172, comparison_operator:<; 172, 173; 172, 176; 173, binary_operator:+; 173, 174; 173, 175; 174, identifier:j; 175, integer:1; 176, call; 176, 177; 176, 178; 177, identifier:len; 178, argument_list; 178, 179; 179, identifier:sentence; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 190; 183, subscript; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:outputs_backward; 186, identifier:i; 187, binary_operator:+; 187, 188; 187, 189; 188, identifier:j; 189, integer:1; 190, identifier:index; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:outputs_forward; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:np; 197, identifier:expand_dims; 198, argument_list; 198, 199; 198, 205; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:np; 202, identifier:asarray; 203, argument_list; 203, 204; 204, identifier:outputs_forward; 205, keyword_argument; 205, 206; 205, 207; 206, identifier:axis; 207, unary_operator:-; 207, 208; 208, integer:1; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:outputs_backward; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:np; 215, identifier:expand_dims; 216, argument_list; 216, 217; 216, 223; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:np; 220, identifier:asarray; 221, argument_list; 221, 222; 222, identifier:outputs_backward; 223, keyword_argument; 223, 224; 223, 225; 224, identifier:axis; 225, unary_operator:-; 225, 226; 226, integer:1; 227, return_statement; 227, 228; 228, expression_list; 228, 229; 228, 235; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:np; 232, identifier:asarray; 233, argument_list; 233, 234; 234, identifier:inputs; 235, list:[outputs_forward, outputs_backward]; 235, 236; 235, 237; 236, identifier:outputs_forward; 237, identifier:outputs_backward | def get_batch(sentences,
token_dict,
ignore_case=False,
unk_index=1,
eos_index=2):
"""Get a batch of inputs and outputs from given sentences.
:param sentences: A list of list of tokens.
:param token_dict: The dict that maps a token to an integer. `<UNK>` and `<EOS>` should be preserved.
:param ignore_case: Whether ignoring the case of the token.
:param unk_index: The index for unknown token.
:param eos_index: The index for ending of sentence.
:return inputs, outputs: The inputs and outputs of the batch.
"""
batch_size = len(sentences)
max_sentence_len = max(map(len, sentences))
inputs = [[0] * max_sentence_len for _ in range(batch_size)]
outputs_forward = [[0] * max_sentence_len for _ in range(batch_size)]
outputs_backward = [[0] * max_sentence_len for _ in range(batch_size)]
for i, sentence in enumerate(sentences):
outputs_forward[i][len(sentence) - 1] = eos_index
outputs_backward[i][0] = eos_index
for j, token in enumerate(sentence):
if ignore_case:
index = token_dict.get(token.lower(), unk_index)
else:
index = token_dict.get(token, unk_index)
inputs[i][j] = index
if j - 1 >= 0:
outputs_forward[i][j - 1] = index
if j + 1 < len(sentence):
outputs_backward[i][j + 1] = index
outputs_forward = np.expand_dims(np.asarray(outputs_forward), axis=-1)
outputs_backward = np.expand_dims(np.asarray(outputs_backward), axis=-1)
return np.asarray(inputs), [outputs_forward, outputs_backward] |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:get_gene_info; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:ensembl_ids; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:hgnc_symbols; 9, None; 10, block; 10, 11; 10, 13; 10, 26; 10, 39; 10, 43; 10, 47; 10, 137; 10, 214; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:uniq_ensembl_ids; 16, call; 16, 17; 16, 18; 17, identifier:set; 18, generator_expression; 18, 19; 18, 20; 19, identifier:ensembl_id; 20, for_in_clause; 20, 21; 20, 22; 21, identifier:ensembl_id; 22, parenthesized_expression; 22, 23; 23, boolean_operator:or; 23, 24; 23, 25; 24, identifier:ensembl_ids; 25, list:[]; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:uniq_hgnc_symbols; 29, call; 29, 30; 29, 31; 30, identifier:set; 31, generator_expression; 31, 32; 31, 33; 32, identifier:hgnc_symbol; 33, for_in_clause; 33, 34; 33, 35; 34, identifier:hgnc_symbol; 35, parenthesized_expression; 35, 36; 36, boolean_operator:or; 36, 37; 36, 38; 37, identifier:hgnc_symbols; 38, list:[]; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:genes; 42, list:[]; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:gene_data; 46, list:[]; 47, if_statement; 47, 48; 47, 49; 47, 70; 48, identifier:uniq_ensembl_ids; 49, block; 49, 50; 50, for_statement; 50, 51; 50, 52; 50, 53; 51, identifier:ensembl_id; 52, identifier:uniq_ensembl_ids; 53, block; 53, 54; 54, for_statement; 54, 55; 54, 56; 54, 62; 55, identifier:res; 56, call; 56, 57; 56, 58; 57, identifier:query_gene; 58, argument_list; 58, 59; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:ensembl_id; 61, identifier:ensembl_id; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:gene_data; 67, identifier:append; 68, argument_list; 68, 69; 69, identifier:res; 70, elif_clause; 70, 71; 70, 72; 71, identifier:uniq_hgnc_symbols; 72, block; 72, 73; 73, for_statement; 73, 74; 73, 75; 73, 76; 74, identifier:hgnc_symbol; 75, identifier:uniq_hgnc_symbols; 76, block; 76, 77; 76, 86; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:query_res; 80, call; 80, 81; 80, 82; 81, identifier:query_gene; 82, argument_list; 82, 83; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:hgnc_symbol; 85, identifier:hgnc_symbol; 86, if_statement; 86, 87; 86, 88; 86, 100; 87, identifier:query_res; 88, block; 88, 89; 89, for_statement; 89, 90; 89, 91; 89, 92; 90, identifier:res; 91, identifier:query_res; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:gene_data; 97, identifier:append; 98, argument_list; 98, 99; 99, identifier:res; 100, else_clause; 100, 101; 100, 102; 101, comment; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:gene_data; 107, identifier:append; 108, argument_list; 108, 109; 109, dictionary; 109, 110; 109, 113; 109, 116; 109, 119; 109, 122; 109, 125; 109, 128; 109, 131; 109, 134; 110, pair; 110, 111; 110, 112; 111, string:'hgnc_symbol'; 112, identifier:hgnc_symbol; 113, pair; 113, 114; 113, 115; 114, string:'hgnc_id'; 115, None; 116, pair; 116, 117; 116, 118; 117, string:'ensembl_id'; 118, None; 119, pair; 119, 120; 119, 121; 120, string:'description'; 121, None; 122, pair; 122, 123; 122, 124; 123, string:'chrom'; 124, string:'unknown'; 125, pair; 125, 126; 125, 127; 126, string:'start'; 127, integer:0; 128, pair; 128, 129; 128, 130; 129, string:'stop'; 130, integer:0; 131, pair; 131, 132; 131, 133; 132, string:'hi_score'; 133, None; 134, pair; 134, 135; 134, 136; 135, string:'constraint_score'; 136, None; 137, for_statement; 137, 138; 137, 139; 137, 140; 138, identifier:gene; 139, identifier:gene_data; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:genes; 145, identifier:append; 146, argument_list; 146, 147; 147, call; 147, 148; 147, 149; 148, identifier:Gene; 149, argument_list; 149, 150; 149, 155; 149, 160; 149, 165; 149, 170; 149, 175; 149, 180; 149, 185; 149, 196; 149, 201; 149, 206; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:symbol; 152, subscript; 152, 153; 152, 154; 153, identifier:gene; 154, string:'hgnc_symbol'; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:hgnc_id; 157, subscript; 157, 158; 157, 159; 158, identifier:gene; 159, string:'hgnc_id'; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:ensembl_id; 162, subscript; 162, 163; 162, 164; 163, identifier:gene; 164, string:'ensembl_id'; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:description; 167, subscript; 167, 168; 167, 169; 168, identifier:gene; 169, string:'description'; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:chrom; 172, subscript; 172, 173; 172, 174; 173, identifier:gene; 174, string:'chrom'; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:start; 177, subscript; 177, 178; 177, 179; 178, identifier:gene; 179, string:'start'; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:stop; 182, subscript; 182, 183; 182, 184; 183, identifier:gene; 184, string:'stop'; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:location; 187, call; 187, 188; 187, 189; 188, identifier:get_cytoband_coord; 189, argument_list; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:gene; 192, string:'chrom'; 193, subscript; 193, 194; 193, 195; 194, identifier:gene; 195, string:'start'; 196, keyword_argument; 196, 197; 196, 198; 197, identifier:hi_score; 198, subscript; 198, 199; 198, 200; 199, identifier:gene; 200, string:'hi_score'; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:constraint_score; 203, subscript; 203, 204; 203, 205; 204, identifier:gene; 205, string:'constraint_score'; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:omim_number; 208, call; 208, 209; 208, 210; 209, identifier:get_omim_number; 210, argument_list; 210, 211; 211, subscript; 211, 212; 211, 213; 212, identifier:gene; 213, string:'hgnc_symbol'; 214, return_statement; 214, 215; 215, identifier:genes | def get_gene_info(ensembl_ids=None, hgnc_symbols=None):
"""Return the genes info based on the transcripts found
Args:
ensembl_ids (Optional[list]): list of Ensembl gene ids
hgnc_symbols (Optional[list]): list of HGNC gene symbols
Returns:
iterable: an iterable with `Gene` objects
"""
uniq_ensembl_ids = set(ensembl_id for ensembl_id in (ensembl_ids or []))
uniq_hgnc_symbols = set(hgnc_symbol for hgnc_symbol in (hgnc_symbols or []))
genes = []
gene_data = []
if uniq_ensembl_ids:
for ensembl_id in uniq_ensembl_ids:
for res in query_gene(ensembl_id=ensembl_id):
gene_data.append(res)
elif uniq_hgnc_symbols:
for hgnc_symbol in uniq_hgnc_symbols:
query_res = query_gene(hgnc_symbol=hgnc_symbol)
if query_res:
for res in query_res:
gene_data.append(res)
else:
# If no result we add just the symbol
gene_data.append({
'hgnc_symbol': hgnc_symbol,
'hgnc_id': None,
'ensembl_id': None,
'description': None,
'chrom': 'unknown',
'start': 0,
'stop': 0,
'hi_score': None,
'constraint_score': None,
})
for gene in gene_data:
genes.append(Gene(
symbol=gene ['hgnc_symbol'],
hgnc_id=gene['hgnc_id'],
ensembl_id=gene['ensembl_id'],
description=gene['description'],
chrom=gene['chrom'],
start=gene['start'],
stop=gene['stop'],
location=get_cytoband_coord(gene['chrom'], gene['start']),
hi_score=gene['hi_score'],
constraint_score=gene['constraint_score'],
omim_number=get_omim_number(gene['hgnc_symbol'])
))
return genes |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:parse_mapping; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:map_path; 6, default_parameter; 6, 7; 6, 8; 7, identifier:source; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:dotfiles; 11, None; 12, block; 12, 13; 12, 15; 12, 19; 12, 31; 12, 35; 12, 44; 12, 48; 12, 62; 12, 112; 12, 124; 12, 138; 12, 149; 12, 153; 12, 396; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:include_re; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:include_re; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:re; 25, identifier:compile; 26, argument_list; 26, 27; 26, 28; 27, identifier:include_re; 28, attribute; 28, 29; 28, 30; 29, identifier:re; 30, identifier:I; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:mapping_re; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:mapping_re; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:re; 41, identifier:compile; 42, argument_list; 42, 43; 43, identifier:mapping_re; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:filename; 47, None; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:map_path; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:path; 54, identifier:realpath; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:path; 59, identifier:expanduser; 60, argument_list; 60, 61; 61, identifier:map_path; 62, if_statement; 62, 63; 62, 69; 62, 74; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:path; 66, identifier:isfile; 67, argument_list; 67, 68; 68, identifier:map_path; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:filename; 73, identifier:map_path; 74, elif_clause; 74, 75; 74, 81; 74, 82; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:path; 78, identifier:isdir; 79, argument_list; 79, 80; 80, identifier:map_path; 81, comment; 82, block; 82, 83; 83, for_statement; 83, 84; 83, 85; 83, 88; 84, identifier:map_name; 85, expression_list; 85, 86; 85, 87; 86, string:'.dotfiles'; 87, string:'dotfiles'; 88, block; 88, 89; 88, 99; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:candidate; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:path; 95, identifier:join; 96, argument_list; 96, 97; 96, 98; 97, identifier:map_path; 98, identifier:map_name; 99, if_statement; 99, 100; 99, 106; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:path; 103, identifier:isfile; 104, argument_list; 104, 105; 105, identifier:candidate; 106, block; 106, 107; 106, 111; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:filename; 110, identifier:candidate; 111, break_statement; 112, if_statement; 112, 113; 112, 116; 113, comparison_operator:is; 113, 114; 113, 115; 114, identifier:filename; 115, None; 116, block; 116, 117; 117, raise_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:ValueError; 120, argument_list; 120, 121; 121, binary_operator:%; 121, 122; 121, 123; 122, string:'No dotfile mapping found in %s'; 123, identifier:map_path; 124, if_statement; 124, 125; 124, 128; 125, comparison_operator:is; 125, 126; 125, 127; 126, identifier:source; 127, None; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:source; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:path; 135, identifier:dirname; 136, argument_list; 136, 137; 137, identifier:map_path; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:dotfiles; 141, None; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:dotfiles; 146, call; 146, 147; 146, 148; 147, identifier:OrderedDict; 148, argument_list; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:lineno; 152, integer:0; 153, with_statement; 153, 154; 153, 163; 154, with_clause; 154, 155; 155, with_item; 155, 156; 156, as_pattern; 156, 157; 156, 161; 157, call; 157, 158; 157, 159; 158, identifier:open; 159, argument_list; 159, 160; 160, identifier:filename; 161, as_pattern_target; 161, 162; 162, identifier:fh; 163, block; 163, 164; 164, for_statement; 164, 165; 164, 166; 164, 167; 165, identifier:line; 166, identifier:fh; 167, block; 167, 168; 167, 172; 167, 180; 167, 189; 167, 299; 167, 312; 167, 321; 168, expression_statement; 168, 169; 169, augmented_assignment:+=; 169, 170; 169, 171; 170, identifier:lineno; 171, integer:1; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:content; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:line; 178, identifier:strip; 179, argument_list; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:match; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:include_re; 186, identifier:match; 187, argument_list; 187, 188; 188, identifier:content; 189, if_statement; 189, 190; 189, 191; 190, identifier:match; 191, block; 191, 192; 191, 206; 191, 253; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:include_path; 195, call; 195, 196; 195, 204; 196, attribute; 196, 197; 196, 203; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:match; 200, identifier:group; 201, argument_list; 201, 202; 202, integer:1; 203, identifier:strip; 204, argument_list; 204, 205; 205, string:'\'"'; 206, if_statement; 206, 207; 206, 221; 206, 236; 207, parenthesized_expression; 207, 208; 208, boolean_operator:or; 208, 209; 208, 215; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:include_path; 212, identifier:startswith; 213, argument_list; 213, 214; 214, string:'/'; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:include_path; 218, identifier:startswith; 219, argument_list; 219, 220; 220, string:'~'; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:include_path; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:path; 228, identifier:realpath; 229, argument_list; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:path; 233, identifier:expanduser; 234, argument_list; 234, 235; 235, identifier:include_path; 236, else_clause; 236, 237; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:include_path; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:path; 244, identifier:join; 245, argument_list; 245, 246; 245, 252; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, identifier:path; 249, identifier:dirname; 250, argument_list; 250, 251; 251, identifier:filename; 252, identifier:include_path; 253, if_statement; 253, 254; 253, 260; 253, 283; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:path; 257, identifier:exists; 258, argument_list; 258, 259; 259, identifier:include_path; 260, block; 260, 261; 260, 271; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 268; 263, attribute; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:self; 266, identifier:log; 267, identifier:debug; 268, argument_list; 268, 269; 268, 270; 269, string:'Recursively parsing mapping in %s'; 270, identifier:include_path; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:dotfiles; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:self; 277, identifier:parse_mapping; 278, argument_list; 278, 279; 278, 280; 279, identifier:include_path; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:dotfiles; 282, identifier:dotfiles; 283, else_clause; 283, 284; 284, block; 284, 285; 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:log; 291, identifier:warning; 292, argument_list; 292, 293; 292, 297; 292, 298; 293, concatenated_string; 293, 294; 293, 295; 293, 296; 294, string:'Include command points to file or '; 295, string:'directory that does not exist, "%s",'; 296, string:' on line %d'; 297, identifier:include_path; 298, identifier:lineno; 299, if_statement; 299, 300; 299, 309; 299, 310; 300, boolean_operator:or; 300, 301; 300, 303; 301, not_operator; 301, 302; 302, identifier:content; 303, call; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:content; 306, identifier:startswith; 307, argument_list; 307, 308; 308, string:'#'; 309, comment; 310, block; 310, 311; 311, continue_statement; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:match; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:mapping_re; 318, identifier:match; 319, argument_list; 319, 320; 320, identifier:content; 321, if_statement; 321, 322; 321, 323; 321, 382; 322, identifier:match; 323, block; 323, 324; 323, 334; 323, 349; 323, 367; 323, 376; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 329; 326, pattern_list; 326, 327; 326, 328; 327, identifier:source_path; 328, identifier:target_path; 329, call; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:match; 332, identifier:groups; 333, argument_list; 334, expression_statement; 334, 335; 335, assignment; 335, 336; 335, 337; 336, identifier:source_path; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:path; 340, identifier:join; 341, argument_list; 341, 342; 341, 343; 342, identifier:source; 343, call; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:source_path; 346, identifier:strip; 347, argument_list; 347, 348; 348, string:'\'"'; 349, if_statement; 349, 350; 349, 353; 350, comparison_operator:in; 350, 351; 350, 352; 351, identifier:source_path; 352, identifier:dotfiles; 353, block; 353, 354; 353, 366; 354, expression_statement; 354, 355; 355, call; 355, 356; 355, 361; 356, attribute; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:self; 359, identifier:log; 360, identifier:warning; 361, argument_list; 361, 362; 361, 365; 362, concatenated_string; 362, 363; 362, 364; 363, string:'Duplicate dotfile source "%s" '; 364, string:'on line #%d'; 365, identifier:lineno; 366, continue_statement; 367, if_statement; 367, 368; 367, 371; 368, comparison_operator:is; 368, 369; 368, 370; 369, identifier:target_path; 370, None; 371, block; 371, 372; 372, expression_statement; 372, 373; 373, assignment; 373, 374; 373, 375; 374, identifier:target_path; 375, identifier:source_path; 376, expression_statement; 376, 377; 377, assignment; 377, 378; 377, 381; 378, subscript; 378, 379; 378, 380; 379, identifier:dotfiles; 380, identifier:source_path; 381, identifier:target_path; 382, else_clause; 382, 383; 383, block; 383, 384; 384, expression_statement; 384, 385; 385, call; 385, 386; 385, 391; 386, attribute; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:self; 389, identifier:log; 390, identifier:warning; 391, argument_list; 391, 392; 391, 395; 392, concatenated_string; 392, 393; 392, 394; 393, string:'Dotfile mapping regex failed on line '; 394, string:'#%d'; 395, identifier:lineno; 396, return_statement; 396, 397; 397, identifier:dotfiles | def parse_mapping(self, map_path, source=None, dotfiles=None):
"""Do a simple parse of the dotfile mapping, using semicolons to
separate source file name from the target file paths."""
include_re = r"""^\s*#include\s+(".+"|'.+')"""
include_re = re.compile(include_re, re.I)
mapping_re = r"""^("[^"]+"|\'[^\']+\'|[^\'":]+)\s*(?::\s*(.*)\s*)?$"""
mapping_re = re.compile(mapping_re)
filename = None
map_path = path.realpath(path.expanduser(map_path))
if path.isfile(map_path):
filename = map_path
elif path.isdir(map_path):
# try finding a mapping in the target directory
for map_name in '.dotfiles', 'dotfiles':
candidate = path.join(map_path, map_name)
if path.isfile(candidate):
filename = candidate
break
if filename is None:
raise ValueError('No dotfile mapping found in %s' % map_path)
if source is None:
source = path.dirname(map_path)
if dotfiles is None:
dotfiles = OrderedDict()
lineno = 0
with open(filename) as fh:
for line in fh:
lineno += 1
content = line.strip()
match = include_re.match(content)
if match:
include_path = match.group(1).strip('\'"')
if (include_path.startswith('/') or
include_path.startswith('~')):
include_path = path.realpath(
path.expanduser(include_path))
else:
include_path = path.join(path.dirname(filename),
include_path)
if path.exists(include_path):
self.log.debug('Recursively parsing mapping in %s',
include_path)
dotfiles = self.parse_mapping(include_path,
dotfiles=dotfiles)
else:
self.log.warning('Include command points to file or '
'directory that does not exist, "%s",'
' on line %d', include_path, lineno)
if not content or content.startswith('#'):
# comment line or empty line
continue
match = mapping_re.match(content)
if match:
source_path, target_path = match.groups()
source_path = path.join(source, source_path.strip('\'"'))
if source_path in dotfiles:
self.log.warning('Duplicate dotfile source "%s" '
'on line #%d', lineno)
continue
if target_path is None:
target_path = source_path
dotfiles[source_path] = target_path
else:
self.log.warning('Dotfile mapping regex failed on line '
'#%d', lineno)
return dotfiles |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_add_compounds; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:variant_obj; 6, identifier:info_dict; 7, block; 7, 8; 7, 10; 7, 14; 7, 23; 7, 108; 7, 109; 7, 126; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:compound_list; 13, list:[]; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:compound_entry; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:info_dict; 20, identifier:get; 21, argument_list; 21, 22; 22, string:'Compounds'; 23, if_statement; 23, 24; 23, 25; 24, identifier:compound_entry; 25, block; 25, 26; 26, for_statement; 26, 27; 26, 28; 26, 34; 27, identifier:family_annotation; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:compound_entry; 31, identifier:split; 32, argument_list; 32, 33; 33, string:','; 34, block; 34, 35; 34, 52; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:compounds; 38, call; 38, 39; 38, 50; 39, attribute; 39, 40; 39, 49; 40, subscript; 40, 41; 40, 47; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:family_annotation; 44, identifier:split; 45, argument_list; 45, 46; 46, string:':'; 47, unary_operator:-; 47, 48; 48, integer:1; 49, identifier:split; 50, argument_list; 50, 51; 51, string:'|'; 52, for_statement; 52, 53; 52, 54; 52, 55; 53, identifier:compound; 54, identifier:compounds; 55, block; 55, 56; 55, 65; 55, 69; 55, 93; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:splitted_compound; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:compound; 62, identifier:split; 63, argument_list; 63, 64; 64, string:'>'; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:compound_score; 68, None; 69, if_statement; 69, 70; 69, 76; 70, comparison_operator:>; 70, 71; 70, 75; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, identifier:splitted_compound; 75, integer:1; 76, block; 76, 77; 76, 83; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:compound_id; 80, subscript; 80, 81; 80, 82; 81, identifier:splitted_compound; 82, integer:0; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:compound_score; 86, call; 86, 87; 86, 88; 87, identifier:int; 88, argument_list; 88, 89; 89, subscript; 89, 90; 89, 91; 90, identifier:splitted_compound; 91, unary_operator:-; 91, 92; 92, integer:1; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:compound_list; 97, identifier:append; 98, argument_list; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:Compound; 101, argument_list; 101, 102; 101, 105; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:variant_id; 104, identifier:compound_id; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:combined_score; 107, identifier:compound_score; 108, comment; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:compound_list; 113, identifier:sort; 114, argument_list; 114, 115; 114, 123; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:key; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:operator; 120, identifier:attrgetter; 121, argument_list; 121, 122; 122, string:'combined_score'; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:reverse; 125, True; 126, for_statement; 126, 127; 126, 128; 126, 129; 127, identifier:compound; 128, identifier:compound_list; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:variant_obj; 134, identifier:add_compound; 135, argument_list; 135, 136; 136, identifier:compound | def _add_compounds(self, variant_obj, info_dict):
"""Check if there are any compounds and add them to the variant
The compounds that are added should be sorted on rank score
"""
compound_list = []
compound_entry = info_dict.get('Compounds')
if compound_entry:
for family_annotation in compound_entry.split(','):
compounds = family_annotation.split(':')[-1].split('|')
for compound in compounds:
splitted_compound = compound.split('>')
compound_score = None
if len(splitted_compound) > 1:
compound_id = splitted_compound[0]
compound_score = int(splitted_compound[-1])
compound_list.append(Compound(
variant_id=compound_id,
combined_score=compound_score
)
)
#Sort the compounds based on rank score
compound_list.sort(key = operator.attrgetter('combined_score'), reverse=True)
for compound in compound_list:
variant_obj.add_compound(compound) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:dump_xml; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:props; 5, identifier:fp; 6, default_parameter; 6, 7; 6, 8; 7, identifier:comment; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:encoding; 11, string:'UTF-8'; 12, default_parameter; 12, 13; 12, 14; 13, identifier:sort_keys; 14, False; 15, block; 15, 16; 15, 18; 15, 35; 15, 51; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:fp; 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:codecs; 26, identifier:lookup; 27, argument_list; 27, 28; 28, identifier:encoding; 29, identifier:streamwriter; 30, argument_list; 30, 31; 30, 32; 31, identifier:fp; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:errors; 34, string:'xmlcharrefreplace'; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:print; 38, argument_list; 38, 39; 38, 48; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, string:'<?xml version="1.0" encoding={0} standalone="no"?>'; 42, identifier:format; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:quoteattr; 46, argument_list; 46, 47; 47, identifier:encoding; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:file; 50, identifier:fp; 51, for_statement; 51, 52; 51, 53; 51, 59; 52, identifier:s; 53, call; 53, 54; 53, 55; 54, identifier:_stream_xml; 55, argument_list; 55, 56; 55, 57; 55, 58; 56, identifier:props; 57, identifier:comment; 58, identifier:sort_keys; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:print; 63, argument_list; 63, 64; 63, 65; 64, identifier:s; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:file; 67, identifier:fp | def dump_xml(props, fp, comment=None, encoding='UTF-8', sort_keys=False):
"""
Write a series ``props`` of key-value pairs to a binary filehandle ``fp``
in the format of an XML properties file. The file will include both an XML
declaration and a doctype declaration.
:param props: A mapping or iterable of ``(key, value)`` pairs to write to
``fp``. All keys and values in ``props`` must be text strings. If
``sort_keys`` is `False`, the entries are output in iteration order.
:param fp: a file-like object to write the values of ``props`` to
:type fp: binary file-like object
:param comment: if non-`None`, ``comment`` will be output as a
``<comment>`` element before the ``<entry>`` elements
:type comment: text string or `None`
:param string encoding: the name of the encoding to use for the XML
document (also included in the XML declaration)
:param bool sort_keys: if true, the elements of ``props`` are sorted
lexicographically by key in the output
:return: `None`
"""
fp = codecs.lookup(encoding).streamwriter(fp, errors='xmlcharrefreplace')
print('<?xml version="1.0" encoding={0} standalone="no"?>'
.format(quoteattr(encoding)), file=fp)
for s in _stream_xml(props, comment, sort_keys):
print(s, file=fp) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:dumps_xml; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:props; 5, default_parameter; 5, 6; 5, 7; 6, identifier:comment; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort_keys; 10, False; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, string:''; 18, identifier:join; 19, generator_expression; 19, 20; 19, 23; 20, binary_operator:+; 20, 21; 20, 22; 21, identifier:s; 22, string:'\n'; 23, for_in_clause; 23, 24; 23, 25; 24, identifier:s; 25, call; 25, 26; 25, 27; 26, identifier:_stream_xml; 27, argument_list; 27, 28; 27, 29; 27, 30; 28, identifier:props; 29, identifier:comment; 30, identifier:sort_keys | def dumps_xml(props, comment=None, sort_keys=False):
"""
Convert a series ``props`` of key-value pairs to a text string containing
an XML properties document. The document will include a doctype
declaration but not an XML declaration.
:param props: A mapping or iterable of ``(key, value)`` pairs to serialize.
All keys and values in ``props`` must be text strings. If
``sort_keys`` is `False`, the entries are output in iteration order.
:param comment: if non-`None`, ``comment`` will be output as a
``<comment>`` element before the ``<entry>`` elements
:type comment: text string or `None`
:param bool sort_keys: if true, the elements of ``props`` are sorted
lexicographically by key in the output
:rtype: text string
"""
return ''.join(s + '\n' for s in _stream_xml(props, comment, sort_keys)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:gene_list; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:list_id; 6, None; 7, block; 7, 8; 7, 10; 7, 26; 7, 78; 7, 132; 7, 280; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:all_case_ids; 13, list_comprehension; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:case; 16, identifier:case_id; 17, for_in_clause; 17, 18; 17, 19; 18, identifier:case; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:app; 23, identifier:db; 24, identifier:cases; 25, argument_list; 26, if_statement; 26, 27; 26, 28; 27, identifier:list_id; 28, block; 28, 29; 28, 40; 28, 62; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:genelist_obj; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:app; 36, identifier:db; 37, identifier:gene_list; 38, argument_list; 38, 39; 39, identifier:list_id; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:case_ids; 43, list_comprehension; 43, 44; 43, 47; 43, 56; 44, attribute; 44, 45; 44, 46; 45, identifier:case; 46, identifier:case_id; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:case; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:app; 53, identifier:db; 54, identifier:cases; 55, argument_list; 56, if_clause; 56, 57; 57, comparison_operator:not; 57, 58; 57, 59; 58, identifier:case; 59, attribute; 59, 60; 59, 61; 60, identifier:genelist_obj; 61, identifier:cases; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:is; 63, 64; 63, 65; 64, identifier:genelist_obj; 65, None; 66, block; 66, 67; 67, return_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:abort; 70, argument_list; 70, 71; 70, 72; 71, integer:404; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, string:"gene list not found: {}"; 75, identifier:format; 76, argument_list; 76, 77; 77, identifier:list_id; 78, if_statement; 78, 79; 78, 84; 79, comparison_operator:in; 79, 80; 79, 81; 80, string:'download'; 81, attribute; 81, 82; 81, 83; 82, identifier:request; 83, identifier:args; 84, block; 84, 85; 84, 99; 84, 113; 84, 122; 84, 130; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:response; 88, call; 88, 89; 88, 90; 89, identifier:make_response; 90, argument_list; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, string:'\n'; 94, identifier:join; 95, argument_list; 95, 96; 96, attribute; 96, 97; 96, 98; 97, identifier:genelist_obj; 98, identifier:gene_ids; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:filename; 102, call; 102, 103; 102, 104; 103, identifier:secure_filename; 104, argument_list; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, string:"{}.txt"; 108, identifier:format; 109, argument_list; 109, 110; 110, attribute; 110, 111; 110, 112; 111, identifier:genelist_obj; 112, identifier:list_id; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:header; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, string:"attachment; filename={}"; 119, identifier:format; 120, argument_list; 120, 121; 121, identifier:filename; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 129; 124, subscript; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:response; 127, identifier:headers; 128, string:'Content-Disposition'; 129, identifier:header; 130, return_statement; 130, 131; 131, identifier:response; 132, if_statement; 132, 133; 132, 138; 133, comparison_operator:==; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:request; 136, identifier:method; 137, string:'POST'; 138, block; 138, 139; 139, if_statement; 139, 140; 139, 141; 139, 142; 139, 193; 140, identifier:list_id; 141, comment; 142, block; 142, 143; 142, 154; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:case_ids; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:request; 150, identifier:form; 151, identifier:getlist; 152, argument_list; 152, 153; 153, string:'case_id'; 154, for_statement; 154, 155; 154, 156; 154, 157; 155, identifier:case_id; 156, identifier:case_ids; 157, block; 157, 158; 157, 169; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:case_obj; 161, call; 161, 162; 161, 167; 162, attribute; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:app; 165, identifier:db; 166, identifier:case; 167, argument_list; 167, 168; 168, identifier:case_id; 169, if_statement; 169, 170; 169, 175; 170, comparison_operator:not; 170, 171; 170, 172; 171, identifier:case_obj; 172, attribute; 172, 173; 172, 174; 173, identifier:genelist_obj; 174, identifier:cases; 175, block; 175, 176; 175, 185; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:genelist_obj; 181, identifier:cases; 182, identifier:append; 183, argument_list; 183, 184; 184, identifier:case_obj; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 192; 187, attribute; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:app; 190, identifier:db; 191, identifier:save; 192, argument_list; 193, else_clause; 193, 194; 193, 195; 194, comment; 195, block; 195, 196; 195, 204; 195, 220; 195, 236; 195, 246; 195, 264; 195, 276; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:req_file; 199, subscript; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:request; 202, identifier:files; 203, string:'file'; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:new_listid; 207, parenthesized_expression; 207, 208; 208, boolean_operator:or; 208, 209; 208, 214; 209, subscript; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:request; 212, identifier:form; 213, string:'list_id'; 214, call; 214, 215; 214, 216; 215, identifier:secure_filename; 216, argument_list; 216, 217; 217, attribute; 217, 218; 217, 219; 218, identifier:req_file; 219, identifier:filename; 220, if_statement; 220, 221; 220, 229; 221, call; 221, 222; 221, 227; 222, attribute; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:app; 225, identifier:db; 226, identifier:gene_list; 227, argument_list; 227, 228; 228, identifier:new_listid; 229, block; 229, 230; 230, return_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:abort; 233, argument_list; 233, 234; 233, 235; 234, integer:500; 235, string:'Please provide a unique list name'; 236, if_statement; 236, 237; 236, 239; 237, not_operator; 237, 238; 238, identifier:req_file; 239, block; 239, 240; 240, return_statement; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:abort; 243, argument_list; 243, 244; 243, 245; 244, integer:500; 245, string:'Please provide a file for upload'; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:gene_ids; 249, list_comprehension; 249, 250; 249, 251; 249, 256; 250, identifier:line; 251, for_in_clause; 251, 252; 251, 253; 252, identifier:line; 253, attribute; 253, 254; 253, 255; 254, identifier:req_file; 255, identifier:stream; 256, if_clause; 256, 257; 257, not_operator; 257, 258; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:line; 261, identifier:startswith; 262, argument_list; 262, 263; 263, string:'#'; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:genelist_obj; 267, call; 267, 268; 267, 273; 268, attribute; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:app; 271, identifier:db; 272, identifier:add_genelist; 273, argument_list; 273, 274; 273, 275; 274, identifier:new_listid; 275, identifier:gene_ids; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:case_ids; 279, identifier:all_case_ids; 280, return_statement; 280, 281; 281, call; 281, 282; 281, 283; 282, identifier:render_template; 283, argument_list; 283, 284; 283, 285; 283, 288; 284, string:'gene_list.html'; 285, keyword_argument; 285, 286; 285, 287; 286, identifier:gene_list; 287, identifier:genelist_obj; 288, keyword_argument; 288, 289; 288, 290; 289, identifier:case_ids; 290, identifier:case_ids | def gene_list(list_id=None):
"""Display or add a gene list."""
all_case_ids = [case.case_id for case in app.db.cases()]
if list_id:
genelist_obj = app.db.gene_list(list_id)
case_ids = [case.case_id for case in app.db.cases()
if case not in genelist_obj.cases]
if genelist_obj is None:
return abort(404, "gene list not found: {}".format(list_id))
if 'download' in request.args:
response = make_response('\n'.join(genelist_obj.gene_ids))
filename = secure_filename("{}.txt".format(genelist_obj.list_id))
header = "attachment; filename={}".format(filename)
response.headers['Content-Disposition'] = header
return response
if request.method == 'POST':
if list_id:
# link a case to the gene list
case_ids = request.form.getlist('case_id')
for case_id in case_ids:
case_obj = app.db.case(case_id)
if case_obj not in genelist_obj.cases:
genelist_obj.cases.append(case_obj)
app.db.save()
else:
# upload a new gene list
req_file = request.files['file']
new_listid = (request.form['list_id'] or
secure_filename(req_file.filename))
if app.db.gene_list(new_listid):
return abort(500, 'Please provide a unique list name')
if not req_file:
return abort(500, 'Please provide a file for upload')
gene_ids = [line for line in req_file.stream
if not line.startswith('#')]
genelist_obj = app.db.add_genelist(new_listid, gene_ids)
case_ids = all_case_ids
return render_template('gene_list.html', gene_list=genelist_obj,
case_ids=case_ids) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_functarguments; 3, parameters; 3, 4; 4, identifier:func; 5, block; 5, 6; 5, 8; 5, 17; 5, 77; 5, 93; 5, 99; 5, 103; 5, 110; 5, 155; 5, 290; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:argspec; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:inspect; 14, identifier:getargspec; 15, argument_list; 15, 16; 16, identifier:func; 17, if_statement; 17, 18; 17, 23; 17, 65; 18, comparison_operator:is; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:argspec; 21, identifier:defaults; 22, None; 23, block; 23, 24; 23, 40; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:args; 27, subscript; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:argspec; 30, identifier:args; 31, slice; 31, 32; 31, 33; 32, colon; 33, unary_operator:-; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:len; 36, argument_list; 36, 37; 37, attribute; 37, 38; 37, 39; 38, identifier:argspec; 39, identifier:defaults; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:kwargs; 43, call; 43, 44; 43, 45; 44, identifier:dict; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:zip; 48, argument_list; 48, 49; 48, 62; 49, subscript; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:argspec; 52, identifier:args; 53, slice; 53, 54; 53, 61; 54, unary_operator:-; 54, 55; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, attribute; 58, 59; 58, 60; 59, identifier:argspec; 60, identifier:defaults; 61, colon; 62, attribute; 62, 63; 62, 64; 63, identifier:argspec; 64, identifier:defaults; 65, else_clause; 65, 66; 66, block; 66, 67; 66, 73; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:args; 70, attribute; 70, 71; 70, 72; 71, identifier:argspec; 72, identifier:args; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:kwargs; 76, dictionary; 77, if_statement; 77, 78; 77, 85; 78, boolean_operator:and; 78, 79; 78, 80; 79, identifier:args; 80, comparison_operator:==; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:args; 83, integer:0; 84, string:'self'; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:args; 90, identifier:pop; 91, argument_list; 91, 92; 92, integer:0; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:func; 97, identifier:__named__; 98, list:[]; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:arguments; 102, list:[]; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:shared; 106, call; 106, 107; 106, 108; 107, identifier:get_shared; 108, argument_list; 108, 109; 109, identifier:func; 110, for_statement; 110, 111; 110, 112; 110, 113; 111, identifier:arg; 112, identifier:args; 113, block; 113, 114; 113, 124; 113, 136; 113, 146; 114, if_statement; 114, 115; 114, 122; 115, comparison_operator:is; 115, 116; 115, 121; 116, call; 116, 117; 116, 118; 117, identifier:has_shared; 118, argument_list; 118, 119; 118, 120; 119, identifier:arg; 120, identifier:shared; 121, False; 122, block; 122, 123; 123, continue_statement; 124, if_statement; 124, 125; 124, 134; 125, comparison_operator:is; 125, 126; 125, 133; 126, call; 126, 127; 126, 128; 127, identifier:has_argument; 128, argument_list; 128, 129; 128, 130; 129, identifier:arg; 130, attribute; 130, 131; 130, 132; 131, identifier:func; 132, identifier:__cls__; 133, False; 134, block; 134, 135; 135, continue_statement; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:arguments; 140, identifier:append; 141, argument_list; 141, 142; 142, tuple; 142, 143; 142, 145; 143, list:[arg]; 143, 144; 144, identifier:arg; 145, dictionary; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 153; 148, attribute; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:func; 151, identifier:__named__; 152, identifier:append; 153, argument_list; 153, 154; 154, identifier:arg; 155, for_statement; 155, 156; 155, 159; 155, 164; 156, pattern_list; 156, 157; 156, 158; 157, identifier:key; 158, identifier:val; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:kwargs; 162, identifier:items; 163, argument_list; 164, block; 164, 165; 164, 175; 164, 187; 164, 272; 164, 281; 165, if_statement; 165, 166; 165, 173; 166, comparison_operator:is; 166, 167; 166, 172; 167, call; 167, 168; 167, 169; 168, identifier:has_shared; 169, argument_list; 169, 170; 169, 171; 170, identifier:key; 171, identifier:shared; 172, False; 173, block; 173, 174; 174, continue_statement; 175, if_statement; 175, 176; 175, 185; 176, comparison_operator:is; 176, 177; 176, 184; 177, call; 177, 178; 177, 179; 178, identifier:has_argument; 179, argument_list; 179, 180; 179, 181; 180, identifier:key; 181, attribute; 181, 182; 181, 183; 182, identifier:func; 183, identifier:__cls__; 184, False; 185, block; 185, 186; 186, continue_statement; 187, if_statement; 187, 188; 187, 193; 187, 244; 188, call; 188, 189; 188, 190; 189, identifier:isinstance; 190, argument_list; 190, 191; 190, 192; 191, identifier:val; 192, identifier:dict; 193, block; 193, 194; 193, 207; 193, 217; 193, 233; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:flags; 197, list:[val.pop('lflag', '--%s' % key)]; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:val; 201, identifier:pop; 202, argument_list; 202, 203; 202, 204; 203, string:'lflag'; 204, binary_operator:%; 204, 205; 204, 206; 205, string:'--%s'; 206, identifier:key; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:short; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:val; 213, identifier:pop; 214, argument_list; 214, 215; 214, 216; 215, string:'flag'; 216, None; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:dest; 220, call; 220, 221; 220, 230; 221, attribute; 221, 222; 221, 229; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:val; 225, identifier:get; 226, argument_list; 226, 227; 226, 228; 227, string:'dest'; 228, identifier:key; 229, identifier:replace; 230, argument_list; 230, 231; 230, 232; 231, string:'-'; 232, string:'_'; 233, if_statement; 233, 234; 233, 235; 234, identifier:short; 235, block; 235, 236; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:flags; 240, identifier:insert; 241, argument_list; 241, 242; 241, 243; 242, integer:0; 243, identifier:short; 244, else_clause; 244, 245; 245, block; 245, 246; 245, 253; 245, 262; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:flags; 249, list:['--%s' % key]; 249, 250; 250, binary_operator:%; 250, 251; 250, 252; 251, string:'--%s'; 252, identifier:key; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:val; 256, call; 256, 257; 256, 258; 257, identifier:dict; 258, argument_list; 258, 259; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:default; 261, identifier:val; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:dest; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:key; 268, identifier:replace; 269, argument_list; 269, 270; 269, 271; 270, string:'-'; 271, string:'_'; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 279; 274, attribute; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:func; 277, identifier:__named__; 278, identifier:append; 279, argument_list; 279, 280; 280, identifier:dest; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 286; 283, attribute; 283, 284; 283, 285; 284, identifier:arguments; 285, identifier:append; 286, argument_list; 286, 287; 287, tuple; 287, 288; 287, 289; 288, identifier:flags; 289, identifier:val; 290, return_statement; 290, 291; 291, identifier:arguments | def get_functarguments(func):
"""
Recupera gli argomenti dalla funzione stessa.
"""
argspec = inspect.getargspec(func)
if argspec.defaults is not None:
args = argspec.args[:-len(argspec.defaults)]
kwargs = dict(
zip(argspec.args[-len(argspec.defaults):], argspec.defaults))
else:
args = argspec.args
kwargs = {}
if args and args[0] == 'self':
args.pop(0)
func.__named__ = []
arguments = []
shared = get_shared(func)
for arg in args:
if has_shared(arg, shared) is not False:
continue
if has_argument(arg, func.__cls__) is not False:
continue
arguments.append(([arg], {}, ))
func.__named__.append(arg)
for key, val in kwargs.items():
if has_shared(key, shared) is not False:
continue
if has_argument(key, func.__cls__) is not False:
continue
if isinstance(val, dict):
flags = [val.pop('lflag', '--%s' % key)]
short = val.pop('flag', None)
dest = val.get('dest', key).replace('-', '_')
if short:
flags.insert(0, short)
else:
flags = ['--%s' % key]
val = dict(default=val)
dest = key.replace('-', '_')
func.__named__.append(dest)
arguments.append((flags, val, ))
return arguments |
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; 3, 8; 4, identifier:ctx; 5, identifier:variant_source; 6, identifier:family_file; 7, identifier:family_type; 8, identifier:root; 9, block; 9, 10; 9, 12; 9, 34; 9, 57; 9, 69; 9, 81; 9, 93; 9, 117; 9, 141; 9, 148; 9, 196; 9, 208; 9, 215; 9, 227; 9, 248; 9, 269; 9, 276; 9, 283; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:root; 15, boolean_operator:or; 15, 16; 15, 26; 16, boolean_operator:or; 16, 17; 16, 18; 17, identifier:root; 18, call; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:ctx; 22, identifier:obj; 23, identifier:get; 24, argument_list; 24, 25; 25, string:'root'; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:os; 30, identifier:path; 31, identifier:expanduser; 32, argument_list; 32, 33; 33, string:"~/.puzzle"; 34, if_statement; 34, 35; 34, 43; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:os; 39, identifier:path; 40, identifier:isfile; 41, argument_list; 41, 42; 42, identifier:root; 43, block; 43, 44; 43, 51; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:logger; 48, identifier:error; 49, argument_list; 49, 50; 50, string:"'root' can't be a file"; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:ctx; 55, identifier:abort; 56, argument_list; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:logger; 61, identifier:info; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, string:"Root directory is: {}"; 66, identifier:format; 67, argument_list; 67, 68; 68, identifier:root; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:db_path; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:os; 76, identifier:path; 77, identifier:join; 78, argument_list; 78, 79; 78, 80; 79, identifier:root; 80, string:'puzzle_db.sqlite3'; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:logger; 85, identifier:info; 86, argument_list; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, string:"db path is: {}"; 90, identifier:format; 91, argument_list; 91, 92; 92, identifier:db_path; 93, if_statement; 93, 94; 93, 103; 94, not_operator; 94, 95; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:os; 99, identifier:path; 100, identifier:exists; 101, argument_list; 101, 102; 102, identifier:db_path; 103, block; 103, 104; 103, 111; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:logger; 108, identifier:warn; 109, argument_list; 109, 110; 110, string:"database not initialized, run 'puzzle init'"; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:ctx; 115, identifier:abort; 116, argument_list; 117, if_statement; 117, 118; 117, 127; 118, not_operator; 118, 119; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:os; 123, identifier:path; 124, identifier:isfile; 125, argument_list; 125, 126; 126, identifier:variant_source; 127, block; 127, 128; 127, 135; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:logger; 132, identifier:error; 133, argument_list; 133, 134; 134, string:"Variant source has to be a file"; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:ctx; 139, identifier:abort; 140, argument_list; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:mode; 144, call; 144, 145; 144, 146; 145, identifier:get_file_type; 146, argument_list; 146, 147; 147, identifier:variant_source; 148, if_statement; 148, 149; 148, 152; 148, 166; 148, 167; 149, comparison_operator:==; 149, 150; 149, 151; 150, identifier:mode; 151, string:'unknown'; 152, block; 152, 153; 152, 160; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:logger; 157, identifier:error; 158, argument_list; 158, 159; 159, string:"Unknown file type"; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:ctx; 164, identifier:abort; 165, argument_list; 166, comment; 167, elif_clause; 167, 168; 167, 171; 168, comparison_operator:==; 168, 169; 168, 170; 169, identifier:mode; 170, string:'gemini'; 171, block; 171, 172; 171, 179; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:logger; 176, identifier:debug; 177, argument_list; 177, 178; 178, string:"Initialzing GEMINI plugin"; 179, if_statement; 179, 180; 179, 182; 180, not_operator; 180, 181; 181, identifier:GEMINI; 182, block; 182, 183; 182, 190; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:logger; 187, identifier:error; 188, argument_list; 188, 189; 189, string:"Need to have gemini installed to use gemini plugin"; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:ctx; 194, identifier:abort; 195, argument_list; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:logger; 200, identifier:debug; 201, argument_list; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, string:'Set puzzle backend to {0}'; 205, identifier:format; 206, argument_list; 206, 207; 207, identifier:mode; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:variant_type; 211, call; 211, 212; 211, 213; 212, identifier:get_variant_type; 213, argument_list; 213, 214; 214, identifier:variant_source; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:logger; 219, identifier:debug; 220, argument_list; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, string:'Set variant type to {0}'; 224, identifier:format; 225, argument_list; 225, 226; 226, identifier:variant_type; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:cases; 230, call; 230, 231; 230, 232; 231, identifier:get_cases; 232, argument_list; 232, 233; 232, 236; 232, 239; 232, 242; 232, 245; 233, keyword_argument; 233, 234; 233, 235; 234, identifier:variant_source; 235, identifier:variant_source; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:case_lines; 238, identifier:family_file; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:case_type; 241, identifier:family_type; 242, keyword_argument; 242, 243; 242, 244; 243, identifier:variant_type; 244, identifier:variant_type; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:variant_mode; 247, identifier:mode; 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:cases; 254, integer:0; 255, block; 255, 256; 255, 263; 256, expression_statement; 256, 257; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:logger; 260, identifier:warning; 261, argument_list; 261, 262; 262, string:"No cases found"; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:ctx; 267, identifier:abort; 268, argument_list; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:logger; 273, identifier:info; 274, argument_list; 274, 275; 275, string:"Initializing sqlite plugin"; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:store; 279, call; 279, 280; 279, 281; 280, identifier:SqlStore; 281, argument_list; 281, 282; 282, identifier:db_path; 283, for_statement; 283, 284; 283, 285; 283, 286; 284, identifier:case_obj; 285, identifier:cases; 286, block; 286, 287; 286, 314; 286, 315; 286, 329; 287, if_statement; 287, 288; 287, 298; 288, comparison_operator:is; 288, 289; 288, 297; 289, call; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:store; 292, identifier:case; 293, argument_list; 293, 294; 294, attribute; 294, 295; 294, 296; 295, identifier:case_obj; 296, identifier:case_id; 297, None; 298, block; 298, 299; 298, 313; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:logger; 303, identifier:warn; 304, argument_list; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, string:"{} already exists in the database"; 308, identifier:format; 309, argument_list; 309, 310; 310, attribute; 310, 311; 310, 312; 311, identifier:case_obj; 312, identifier:case_id; 313, continue_statement; 314, comment; 315, expression_statement; 315, 316; 316, call; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:logger; 319, identifier:debug; 320, argument_list; 320, 321; 321, call; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, string:"adding case: {} to puzzle db"; 324, identifier:format; 325, argument_list; 325, 326; 326, attribute; 326, 327; 326, 328; 327, identifier:case_obj; 328, identifier:case_id; 329, expression_statement; 329, 330; 330, call; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:store; 333, identifier:add_case; 334, argument_list; 334, 335; 334, 336; 334, 339; 335, identifier:case_obj; 336, keyword_argument; 336, 337; 336, 338; 337, identifier:vtype; 338, identifier:variant_type; 339, keyword_argument; 339, 340; 339, 341; 340, identifier:mode; 341, identifier:mode | def load(ctx, variant_source, family_file, family_type, root):
"""
Load a variant source into the database.
If no database was found run puzzle init first.
1. VCF: If a vcf file is used it can be loaded with a ped file
2. GEMINI: Ped information will be retreived from the gemini db
"""
root = root or ctx.obj.get('root') or os.path.expanduser("~/.puzzle")
if os.path.isfile(root):
logger.error("'root' can't be a file")
ctx.abort()
logger.info("Root directory is: {}".format(root))
db_path = os.path.join(root, 'puzzle_db.sqlite3')
logger.info("db path is: {}".format(db_path))
if not os.path.exists(db_path):
logger.warn("database not initialized, run 'puzzle init'")
ctx.abort()
if not os.path.isfile(variant_source):
logger.error("Variant source has to be a file")
ctx.abort()
mode = get_file_type(variant_source)
if mode == 'unknown':
logger.error("Unknown file type")
ctx.abort()
#Test if gemini is installed
elif mode == 'gemini':
logger.debug("Initialzing GEMINI plugin")
if not GEMINI:
logger.error("Need to have gemini installed to use gemini plugin")
ctx.abort()
logger.debug('Set puzzle backend to {0}'.format(mode))
variant_type = get_variant_type(variant_source)
logger.debug('Set variant type to {0}'.format(variant_type))
cases = get_cases(
variant_source=variant_source,
case_lines=family_file,
case_type=family_type,
variant_type=variant_type,
variant_mode=mode
)
if len(cases) == 0:
logger.warning("No cases found")
ctx.abort()
logger.info("Initializing sqlite plugin")
store = SqlStore(db_path)
for case_obj in cases:
if store.case(case_obj.case_id) is not None:
logger.warn("{} already exists in the database"
.format(case_obj.case_id))
continue
# extract case information
logger.debug("adding case: {} to puzzle db".format(case_obj.case_id))
store.add_case(case_obj, vtype=variant_type, mode=mode) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:toposort; 3, parameters; 3, 4; 3, 5; 4, identifier:graph; 5, default_parameter; 5, 6; 5, 7; 6, identifier:pick_first; 7, string:'head'; 8, block; 8, 9; 8, 11; 8, 15; 8, 56; 8, 61; 8, 65; 8, 71; 8, 157; 8, 158; 8, 176; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:in_deg; 14, dictionary; 15, for_statement; 15, 16; 15, 19; 15, 25; 16, pattern_list; 16, 17; 16, 18; 17, identifier:node; 18, identifier:next_nodes; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:six; 22, identifier:iteritems; 23, argument_list; 23, 24; 24, identifier:graph; 25, block; 25, 26; 26, for_statement; 26, 27; 26, 28; 26, 35; 27, identifier:next_node; 28, list:[next_nodes.head_node, next_nodes.update_node]; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:next_nodes; 31, identifier:head_node; 32, attribute; 32, 33; 32, 34; 33, identifier:next_nodes; 34, identifier:update_node; 35, block; 35, 36; 35, 42; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:next_node; 39, None; 40, block; 40, 41; 41, continue_statement; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:in_deg; 46, identifier:next_node; 47, binary_operator:+; 47, 48; 47, 55; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:in_deg; 51, identifier:get; 52, argument_list; 52, 53; 52, 54; 53, identifier:next_node; 54, integer:0; 55, integer:1; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:stk; 59, list:[FIRST]; 59, 60; 60, identifier:FIRST; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:ordered; 64, list:[]; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:visited; 68, call; 68, 69; 68, 70; 69, identifier:set; 70, argument_list; 71, while_statement; 71, 72; 71, 73; 72, identifier:stk; 73, block; 73, 74; 73, 82; 73, 89; 73, 101; 73, 117; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:node; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:stk; 80, identifier:pop; 81, argument_list; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:visited; 86, identifier:add; 87, argument_list; 87, 88; 88, identifier:node; 89, if_statement; 89, 90; 89, 93; 90, comparison_operator:!=; 90, 91; 90, 92; 91, identifier:node; 92, identifier:FIRST; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:ordered; 98, identifier:append; 99, argument_list; 99, 100; 100, identifier:node; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:traversal; 104, call; 104, 105; 104, 106; 105, identifier:_get_traversal; 106, argument_list; 106, 107; 106, 116; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:graph; 110, identifier:get; 111, argument_list; 111, 112; 111, 113; 112, identifier:node; 113, call; 113, 114; 113, 115; 114, identifier:BeforeNodes; 115, argument_list; 116, identifier:pick_first; 117, for_statement; 117, 118; 117, 119; 117, 120; 118, identifier:next_node; 119, identifier:traversal; 120, block; 120, 121; 120, 127; 120, 137; 120, 143; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:is; 122, 123; 122, 124; 123, identifier:next_node; 124, None; 125, block; 125, 126; 126, continue_statement; 127, if_statement; 127, 128; 127, 131; 128, comparison_operator:in; 128, 129; 128, 130; 129, identifier:next_node; 130, identifier:visited; 131, block; 131, 132; 132, raise_statement; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:ValueError; 135, argument_list; 135, 136; 136, string:'Graph has a cycle'; 137, expression_statement; 137, 138; 138, augmented_assignment:-=; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:in_deg; 141, identifier:next_node; 142, integer:1; 143, if_statement; 143, 144; 143, 149; 144, comparison_operator:==; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:in_deg; 147, identifier:next_node; 148, integer:0; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:stk; 154, identifier:append; 155, argument_list; 155, 156; 156, identifier:next_node; 157, comment; 158, if_statement; 158, 159; 158, 170; 159, comparison_operator:!=; 159, 160; 159, 164; 160, call; 160, 161; 160, 162; 161, identifier:len; 162, argument_list; 162, 163; 163, identifier:ordered; 164, binary_operator:-; 164, 165; 164, 169; 165, call; 165, 166; 165, 167; 166, identifier:len; 167, argument_list; 167, 168; 168, identifier:graph; 169, integer:1; 170, block; 170, 171; 171, raise_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:ValueError; 174, argument_list; 174, 175; 175, string:'Graph has a cycle'; 176, return_statement; 176, 177; 177, identifier:ordered | def toposort(graph, pick_first='head'):
"""Toplogically sorts a list match graph.
Tries to perform a topological sort using as tiebreaker the pick_first
argument. If the graph contains cycles, raise ValueError.
"""
in_deg = {}
for node, next_nodes in six.iteritems(graph):
for next_node in [next_nodes.head_node, next_nodes.update_node]:
if next_node is None:
continue
in_deg[next_node] = in_deg.get(next_node, 0) + 1
stk = [FIRST]
ordered = []
visited = set()
while stk:
node = stk.pop()
visited.add(node)
if node != FIRST:
ordered.append(node)
traversal = _get_traversal(graph.get(node, BeforeNodes()), pick_first)
for next_node in traversal:
if next_node is None:
continue
if next_node in visited:
raise ValueError('Graph has a cycle')
in_deg[next_node] -= 1
if in_deg[next_node] == 0:
stk.append(next_node)
# Nodes may not be walked because they don't reach in degree 0.
if len(ordered) != len(graph) - 1:
raise ValueError('Graph has a cycle')
return ordered |